11 # SnapWidget and its associated modules are PyQt4 ports of Ksnapshot. |
11 # SnapWidget and its associated modules are PyQt4 ports of Ksnapshot. |
12 # |
12 # |
13 |
13 |
14 import os |
14 import os |
15 |
15 |
16 from PyQt4.QtCore import pyqtSlot, QFile, QFileInfo, QTimer, QPoint, QMimeData, Qt, \ |
16 from PyQt4.QtCore import pyqtSlot, QFile, QFileInfo, QTimer, QPoint, \ |
17 QEvent, QRegExp, qVersion |
17 QMimeData, Qt, QEvent, QRegExp, qVersion |
18 from PyQt4.QtGui import QWidget, QImageWriter, QApplication, QPixmap, QCursor, QDrag, \ |
18 from PyQt4.QtGui import QWidget, QImageWriter, QApplication, QPixmap, \ |
19 QShortcut, QKeySequence, QDesktopServices |
19 QCursor, QDrag, QShortcut, QKeySequence, QDesktopServices |
20 |
20 |
21 from E5Gui import E5FileDialog, E5MessageBox |
21 from E5Gui import E5FileDialog, E5MessageBox |
22 |
22 |
23 from .Ui_SnapWidget import Ui_SnapWidget |
23 from .Ui_SnapWidget import Ui_SnapWidget |
24 |
24 |
66 index = self.modeCombo.findData(self.__mode) |
66 index = self.modeCombo.findData(self.__mode) |
67 if index == -1: |
67 if index == -1: |
68 index = 0 |
68 index = 0 |
69 self.modeCombo.setCurrentIndex(index) |
69 self.modeCombo.setCurrentIndex(index) |
70 |
70 |
71 self.__delay = int(Preferences.Prefs.settings.value("Snapshot/Delay", 0)) |
71 self.__delay = int( |
|
72 Preferences.Prefs.settings.value("Snapshot/Delay", 0)) |
72 self.delaySpin.setValue(self.__delay) |
73 self.delaySpin.setValue(self.__delay) |
73 |
74 |
74 self.__filename = Preferences.Prefs.settings.value("Snapshot/Filename", |
75 self.__filename = Preferences.Prefs.settings.value("Snapshot/Filename", |
75 os.path.join( |
76 os.path.join( |
76 QDesktopServices.storageLocation(QDesktopServices.PicturesLocation), |
77 QDesktopServices.storageLocation( |
|
78 QDesktopServices.PicturesLocation), |
77 self.trUtf8("snapshot") + "1.png")) |
79 self.trUtf8("snapshot") + "1.png")) |
78 |
80 |
79 self.__grabber = None |
81 self.__grabber = None |
80 self.__snapshot = QPixmap() |
82 self.__snapshot = QPixmap() |
81 self.__savedPosition = QPoint() |
83 self.__savedPosition = QPoint() |
138 |
140 |
139 def __initShortcuts(self): |
141 def __initShortcuts(self): |
140 """ |
142 """ |
141 Private method to initialize the keyboard shortcuts. |
143 Private method to initialize the keyboard shortcuts. |
142 """ |
144 """ |
143 self.__quitShortcut = QShortcut(QKeySequence(QKeySequence.Quit), self, self.close) |
145 self.__quitShortcut = QShortcut( |
|
146 QKeySequence(QKeySequence.Quit), self, self.close) |
144 |
147 |
145 self.__copyShortcut = QShortcut(QKeySequence(QKeySequence.Copy), self, |
148 self.__copyShortcut = QShortcut(QKeySequence(QKeySequence.Copy), self, |
146 self.copyButton.animateClick) |
149 self.copyButton.animateClick) |
147 |
150 |
148 self.__quickSaveShortcut = QShortcut(QKeySequence(Qt.Key_Q), self, |
151 self.__quickSaveShortcut = QShortcut(QKeySequence(Qt.Key_Q), self, |
243 """ |
246 """ |
244 # Extract the file name |
247 # Extract the file name |
245 name = os.path.basename(self.__filename) |
248 name = os.path.basename(self.__filename) |
246 |
249 |
247 # If the name contains a number, then increment it. |
250 # If the name contains a number, then increment it. |
248 numSearch = QRegExp("(^|[^\\d])(\\d+)") # We want to match as far left as |
251 numSearch = QRegExp("(^|[^\\d])(\\d+)") |
249 # possible, and when the number is |
252 # We want to match as far left as possible, and when the number is |
250 # at the start of the name. |
253 # at the start of the name. |
251 |
254 |
252 # Does it have a number? |
255 # Does it have a number? |
253 start = numSearch.lastIndexIn(name) |
256 start = numSearch.lastIndexIn(name) |
254 if start != -1: |
257 if start != -1: |
255 # It has a number, increment it. |
258 # It has a number, increment it. |
256 start = numSearch.pos(2) # Only the second group is of interest. |
259 start = numSearch.pos(2) # Only the second group is of interest. |
257 numAsStr = numSearch.capturedTexts()[2] |
260 numAsStr = numSearch.capturedTexts()[2] |
258 number = "{0:0{width}d}".format(int(numAsStr) + 1, width=len(numAsStr)) |
261 number = "{0:0{width}d}".format( |
|
262 int(numAsStr) + 1, width=len(numAsStr)) |
259 name = name[:start] + number + name[start + len(numAsStr):] |
263 name = name[:start] + number + name[start + len(numAsStr):] |
260 else: |
264 else: |
261 # no number |
265 # no number |
262 start = name.rfind('.') |
266 start = name.rfind('.') |
263 if start != -1: |
267 if start != -1: |
319 def __grabRectangle(self): |
323 def __grabRectangle(self): |
320 """ |
324 """ |
321 Private method to grab a rectangular screen region. |
325 Private method to grab a rectangular screen region. |
322 """ |
326 """ |
323 from .SnapshotRegionGrabber import SnapshotRegionGrabber |
327 from .SnapshotRegionGrabber import SnapshotRegionGrabber |
324 self.__grabber = SnapshotRegionGrabber(mode=SnapshotRegionGrabber.Rectangle) |
328 self.__grabber = SnapshotRegionGrabber( |
|
329 mode=SnapshotRegionGrabber.Rectangle) |
325 self.__grabber.grabbed.connect(self.__captured) |
330 self.__grabber.grabbed.connect(self.__captured) |
326 |
331 |
327 def __grabEllipse(self): |
332 def __grabEllipse(self): |
328 """ |
333 """ |
329 Private method to grab an elliptical screen region. |
334 Private method to grab an elliptical screen region. |
330 """ |
335 """ |
331 from .SnapshotRegionGrabber import SnapshotRegionGrabber |
336 from .SnapshotRegionGrabber import SnapshotRegionGrabber |
332 self.__grabber = SnapshotRegionGrabber(mode=SnapshotRegionGrabber.Ellipse) |
337 self.__grabber = SnapshotRegionGrabber( |
|
338 mode=SnapshotRegionGrabber.Ellipse) |
333 self.__grabber.grabbed.connect(self.__captured) |
339 self.__grabber.grabbed.connect(self.__captured) |
334 |
340 |
335 def __grabFreehand(self): |
341 def __grabFreehand(self): |
336 """ |
342 """ |
337 Private method to grab a non-rectangular screen region. |
343 Private method to grab a non-rectangular screen region. |
349 self.__grabTimer.stop() |
355 self.__grabTimer.stop() |
350 |
356 |
351 if self.__mode == SnapWidget.ModeFullscreen: |
357 if self.__mode == SnapWidget.ModeFullscreen: |
352 desktop = QApplication.desktop() |
358 desktop = QApplication.desktop() |
353 if qVersion() >= "5.0.0": |
359 if qVersion() >= "5.0.0": |
354 self.__snapshot = QApplication.screens()[0].grabWindow(desktop.winId(), |
360 self.__snapshot = QApplication.screens()[0].grabWindow( |
355 desktop.x(), desktop.y(), desktop.width(), desktop.height()) |
361 desktop.winId(), desktop.x(), desktop.y(), |
|
362 desktop.width(), desktop.height()) |
356 else: |
363 else: |
357 self.__snapshot = QPixmap.grabWindow(desktop.winId(), |
364 self.__snapshot = QPixmap.grabWindow( |
358 desktop.x(), desktop.y(), desktop.width(), desktop.height()) |
365 desktop.winId(), desktop.x(), desktop.y(), |
|
366 desktop.width(), desktop.height()) |
359 elif self.__mode == SnapWidget.ModeScreen: |
367 elif self.__mode == SnapWidget.ModeScreen: |
360 desktop = QApplication.desktop() |
368 desktop = QApplication.desktop() |
361 screenId = desktop.screenNumber(QCursor.pos()) |
369 screenId = desktop.screenNumber(QCursor.pos()) |
362 geom = desktop.screenGeometry(screenId) |
370 geom = desktop.screenGeometry(screenId) |
363 x = geom.x() |
371 x = geom.x() |
414 |
422 |
415 def __updatePreview(self): |
423 def __updatePreview(self): |
416 """ |
424 """ |
417 Private slot to update the preview picture. |
425 Private slot to update the preview picture. |
418 """ |
426 """ |
419 self.preview.setToolTip( |
427 self.preview.setToolTip(self.trUtf8( |
420 self.trUtf8("Preview of the snapshot image ({0:n} x {1:n})").format( |
428 "Preview of the snapshot image ({0:n} x {1:n})").format( |
421 self.__snapshot.width(), self.__snapshot.height())) |
429 self.__snapshot.width(), self.__snapshot.height())) |
422 self.preview.setPreview(self.__snapshot) |
430 self.preview.setPreview(self.__snapshot) |
423 self.preview.adjustSize() |
431 self.preview.adjustSize() |
424 |
432 |
425 def resizeEvent(self, evt): |
433 def resizeEvent(self, evt): |
447 |
455 |
448 @param obj reference to the object (QObject) |
456 @param obj reference to the object (QObject) |
449 @param evt reference to the event (QEvent) |
457 @param evt reference to the event (QEvent) |
450 @return flag indicating that the event should be filtered out (boolean) |
458 @return flag indicating that the event should be filtered out (boolean) |
451 """ |
459 """ |
452 if obj == self.__grabberWidget and evt.type() == QEvent.MouseButtonPress: |
460 if obj == self.__grabberWidget and \ |
|
461 evt.type() == QEvent.MouseButtonPress: |
453 if QWidget.mouseGrabber() != self.__grabberWidget: |
462 if QWidget.mouseGrabber() != self.__grabberWidget: |
454 return False |
463 return False |
455 if evt.button() == Qt.LeftButton: |
464 if evt.button() == Qt.LeftButton: |
456 self.__performGrab() |
465 self.__performGrab() |
457 |
466 |
464 @param evt close event (QCloseEvent) |
473 @param evt close event (QCloseEvent) |
465 """ |
474 """ |
466 if self.__modified: |
475 if self.__modified: |
467 res = E5MessageBox.question(self, |
476 res = E5MessageBox.question(self, |
468 self.trUtf8("eric5 Snapshot"), |
477 self.trUtf8("eric5 Snapshot"), |
469 self.trUtf8("""The application contains an unsaved snapshot."""), |
478 self.trUtf8( |
|
479 """The application contains an unsaved snapshot."""), |
470 E5MessageBox.StandardButtons( |
480 E5MessageBox.StandardButtons( |
471 E5MessageBox.Abort | \ |
481 E5MessageBox.Abort | \ |
472 E5MessageBox.Discard | \ |
482 E5MessageBox.Discard | \ |
473 E5MessageBox.Save)) |
483 E5MessageBox.Save)) |
474 if res == E5MessageBox.Abort: |
484 if res == E5MessageBox.Abort: |
475 evt.ignore() |
485 evt.ignore() |
476 return |
486 return |
477 elif res == E5MessageBox.Save: |
487 elif res == E5MessageBox.Save: |
478 self.on_saveButton_clicked() |
488 self.on_saveButton_clicked() |
479 |
489 |
480 Preferences.Prefs.settings.setValue("Snapshot/Delay", self.delaySpin.value()) |
490 Preferences.Prefs.settings.setValue( |
481 Preferences.Prefs.settings.setValue("Snapshot/Mode", |
491 "Snapshot/Delay", self.delaySpin.value()) |
|
492 Preferences.Prefs.settings.setValue( |
|
493 "Snapshot/Mode", |
482 self.modeCombo.itemData(self.modeCombo.currentIndex())) |
494 self.modeCombo.itemData(self.modeCombo.currentIndex())) |
483 Preferences.Prefs.settings.setValue("Snapshot/Filename", self.__filename) |
495 Preferences.Prefs.settings.setValue( |
|
496 "Snapshot/Filename", self.__filename) |
484 Preferences.Prefs.settings.sync() |
497 Preferences.Prefs.settings.sync() |
485 |
498 |
486 def __updateCaption(self): |
499 def __updateCaption(self): |
487 """ |
500 """ |
488 Private method to update the window caption. |
501 Private method to update the window caption. |