|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2012 - 2021 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the snapshot widget. |
|
8 """ |
|
9 |
|
10 # |
|
11 # SnapWidget and its associated modules are PyQt5 ports of Ksnapshot. |
|
12 # |
|
13 |
|
14 import os |
|
15 import re |
|
16 import contextlib |
|
17 |
|
18 from PyQt5.QtCore import ( |
|
19 pyqtSlot, Qt, QFile, QFileInfo, QTimer, QPoint, QMimeData, QLocale, |
|
20 QStandardPaths |
|
21 ) |
|
22 from PyQt5.QtGui import QImageWriter, QPixmap, QDrag, QKeySequence |
|
23 from PyQt5.QtWidgets import QWidget, QApplication, QShortcut |
|
24 |
|
25 from E5Gui import E5FileDialog, E5MessageBox |
|
26 |
|
27 from .Ui_SnapWidget import Ui_SnapWidget |
|
28 |
|
29 import UI.PixmapCache |
|
30 import Preferences |
|
31 import Globals |
|
32 |
|
33 from .SnapshotModes import SnapshotModes |
|
34 |
|
35 |
|
36 class SnapWidget(QWidget, Ui_SnapWidget): |
|
37 """ |
|
38 Class implementing the snapshot widget. |
|
39 """ |
|
40 def __init__(self, parent=None): |
|
41 """ |
|
42 Constructor |
|
43 |
|
44 @param parent reference to the parent widget (QWidget) |
|
45 """ |
|
46 super().__init__(parent) |
|
47 self.setupUi(self) |
|
48 |
|
49 self.saveButton.setIcon(UI.PixmapCache.getIcon("fileSaveAs")) |
|
50 self.takeButton.setIcon(UI.PixmapCache.getIcon("cameraPhoto")) |
|
51 self.copyButton.setIcon(UI.PixmapCache.getIcon("editCopy")) |
|
52 self.copyPreviewButton.setIcon(UI.PixmapCache.getIcon("editCopy")) |
|
53 self.setWindowIcon(UI.PixmapCache.getIcon("ericSnap")) |
|
54 |
|
55 if Globals.isWaylandSession(): |
|
56 from .SnapshotWaylandGrabber import SnapshotWaylandGrabber |
|
57 self.__grabber = SnapshotWaylandGrabber(self) |
|
58 else: |
|
59 from .SnapshotDefaultGrabber import SnapshotDefaultGrabber |
|
60 self.__grabber = SnapshotDefaultGrabber(self) |
|
61 self.decorationsCheckBox.hide() |
|
62 self.mouseCursorCheckBox.hide() |
|
63 self.__grabber.grabbed.connect(self.__captured) |
|
64 supportedModes = self.__grabber.supportedModes() |
|
65 |
|
66 if SnapshotModes.FULLSCREEN in supportedModes: |
|
67 self.modeCombo.addItem(self.tr("Fullscreen"), |
|
68 SnapshotModes.FULLSCREEN) |
|
69 if ( |
|
70 SnapshotModes.SELECTEDSCREEN in supportedModes and |
|
71 len(QApplication.screens()) > 1 |
|
72 ): |
|
73 self.modeCombo.addItem(self.tr("Select Screen"), |
|
74 SnapshotModes.SELECTEDSCREEN) |
|
75 if SnapshotModes.SELECTEDWINDOW in supportedModes: |
|
76 self.modeCombo.addItem(self.tr("Select Window"), |
|
77 SnapshotModes.SELECTEDWINDOW) |
|
78 if SnapshotModes.RECTANGLE in supportedModes: |
|
79 self.modeCombo.addItem(self.tr("Rectangular Selection"), |
|
80 SnapshotModes.RECTANGLE) |
|
81 if SnapshotModes.ELLIPSE in supportedModes: |
|
82 self.modeCombo.addItem(self.tr("Elliptical Selection"), |
|
83 SnapshotModes.ELLIPSE) |
|
84 if SnapshotModes.FREEHAND in supportedModes: |
|
85 self.modeCombo.addItem(self.tr("Freehand Selection"), |
|
86 SnapshotModes.FREEHAND) |
|
87 mode = int(Preferences.Prefs.settings.value("Snapshot/Mode", 0)) |
|
88 index = self.modeCombo.findData(SnapshotModes(mode)) |
|
89 if index == -1: |
|
90 index = 0 |
|
91 self.modeCombo.setCurrentIndex(index) |
|
92 |
|
93 delay = int(Preferences.Prefs.settings.value("Snapshot/Delay", 0)) |
|
94 self.delaySpin.setValue(delay) |
|
95 |
|
96 picturesLocation = QStandardPaths.writableLocation( |
|
97 QStandardPaths.StandardLocation.PicturesLocation) |
|
98 self.__filename = Preferences.Prefs.settings.value( |
|
99 "Snapshot/Filename", |
|
100 os.path.join(picturesLocation, |
|
101 self.tr("snapshot") + "1.png")) |
|
102 |
|
103 self.__snapshot = QPixmap() |
|
104 self.__savedPosition = QPoint() |
|
105 self.__modified = False |
|
106 self.__locale = QLocale() |
|
107 |
|
108 self.__initFileFilters() |
|
109 self.__initShortcuts() |
|
110 |
|
111 self.preview.startDrag.connect(self.__dragSnapshot) |
|
112 |
|
113 self.__updateTimer = QTimer() |
|
114 self.__updateTimer.setSingleShot(True) |
|
115 self.__updateTimer.timeout.connect(self.__updatePreview) |
|
116 |
|
117 self.__updateCaption() |
|
118 self.takeButton.setFocus() |
|
119 |
|
120 def __initFileFilters(self): |
|
121 """ |
|
122 Private method to define the supported image file filters. |
|
123 """ |
|
124 filters = { |
|
125 'bmp': self.tr("Windows Bitmap File (*.bmp)"), |
|
126 'gif': self.tr("Graphic Interchange Format File (*.gif)"), |
|
127 'ico': self.tr("Windows Icon File (*.ico)"), |
|
128 'jpg': self.tr("JPEG File (*.jpg)"), |
|
129 'mng': self.tr("Multiple-Image Network Graphics File (*.mng)"), |
|
130 'pbm': self.tr("Portable Bitmap File (*.pbm)"), |
|
131 'pcx': self.tr("Paintbrush Bitmap File (*.pcx)"), |
|
132 'pgm': self.tr("Portable Graymap File (*.pgm)"), |
|
133 'png': self.tr("Portable Network Graphics File (*.png)"), |
|
134 'ppm': self.tr("Portable Pixmap File (*.ppm)"), |
|
135 'sgi': self.tr("Silicon Graphics Image File (*.sgi)"), |
|
136 'svg': self.tr("Scalable Vector Graphics File (*.svg)"), |
|
137 'tga': self.tr("Targa Graphic File (*.tga)"), |
|
138 'tif': self.tr("TIFF File (*.tif)"), |
|
139 'xbm': self.tr("X11 Bitmap File (*.xbm)"), |
|
140 'xpm': self.tr("X11 Pixmap File (*.xpm)"), |
|
141 } |
|
142 |
|
143 outputFormats = [] |
|
144 writeFormats = QImageWriter.supportedImageFormats() |
|
145 for writeFormat in writeFormats: |
|
146 with contextlib.suppress(KeyError): |
|
147 outputFormats.append(filters[bytes(writeFormat).decode()]) |
|
148 outputFormats.sort() |
|
149 self.__outputFilter = ';;'.join(outputFormats) |
|
150 |
|
151 self.__defaultFilter = filters['png'] |
|
152 |
|
153 def __initShortcuts(self): |
|
154 """ |
|
155 Private method to initialize the keyboard shortcuts. |
|
156 """ |
|
157 self.__quitShortcut = QShortcut( |
|
158 QKeySequence(QKeySequence.StandardKey.Quit), self, self.close) |
|
159 |
|
160 self.__copyShortcut = QShortcut( |
|
161 QKeySequence(QKeySequence.StandardKey.Copy), self, |
|
162 self.copyButton.animateClick) |
|
163 |
|
164 self.__quickSaveShortcut = QShortcut( |
|
165 QKeySequence(Qt.Key.Key_Q), self, self.__quickSave) |
|
166 |
|
167 self.__save1Shortcut = QShortcut( |
|
168 QKeySequence(QKeySequence.StandardKey.Save), self, |
|
169 self.saveButton.animateClick) |
|
170 self.__save2Shortcut = QShortcut( |
|
171 QKeySequence(Qt.Key.Key_S), self, self.saveButton.animateClick) |
|
172 |
|
173 self.__grab1Shortcut = QShortcut( |
|
174 QKeySequence(QKeySequence.StandardKey.New), |
|
175 self, self.takeButton.animateClick) |
|
176 self.__grab2Shortcut = QShortcut( |
|
177 QKeySequence(Qt.Key.Key_N), self, self.takeButton.animateClick) |
|
178 self.__grab3Shortcut = QShortcut( |
|
179 QKeySequence(Qt.Key.Key_Space), self, self.takeButton.animateClick) |
|
180 |
|
181 def __quickSave(self): |
|
182 """ |
|
183 Private slot to save the snapshot bypassing the file selection dialog. |
|
184 """ |
|
185 if not self.__snapshot.isNull(): |
|
186 while os.path.exists(self.__filename): |
|
187 self.__autoIncFilename() |
|
188 |
|
189 if self.__saveImage(self.__filename): |
|
190 self.__modified = False |
|
191 self.__autoIncFilename() |
|
192 self.__updateCaption() |
|
193 |
|
194 @pyqtSlot() |
|
195 def on_saveButton_clicked(self): |
|
196 """ |
|
197 Private slot to save the snapshot. |
|
198 """ |
|
199 if not self.__snapshot.isNull(): |
|
200 while os.path.exists(self.__filename): |
|
201 self.__autoIncFilename() |
|
202 |
|
203 fileName, selectedFilter = E5FileDialog.getSaveFileNameAndFilter( |
|
204 self, |
|
205 self.tr("Save Snapshot"), |
|
206 self.__filename, |
|
207 self.__outputFilter, |
|
208 self.__defaultFilter, |
|
209 E5FileDialog.Options(E5FileDialog.DontConfirmOverwrite)) |
|
210 if not fileName: |
|
211 return |
|
212 |
|
213 ext = QFileInfo(fileName).suffix() |
|
214 if not ext: |
|
215 ex = selectedFilter.split("(*")[1].split(")")[0] |
|
216 if ex: |
|
217 fileName += ex |
|
218 |
|
219 if self.__saveImage(fileName): |
|
220 self.__modified = False |
|
221 self.__filename = fileName |
|
222 self.__autoIncFilename() |
|
223 self.__updateCaption() |
|
224 |
|
225 def __saveImage(self, fileName): |
|
226 """ |
|
227 Private method to save the snapshot. |
|
228 |
|
229 @param fileName name of the file to save to (string) |
|
230 @return flag indicating success (boolean) |
|
231 """ |
|
232 if QFileInfo(fileName).exists(): |
|
233 res = E5MessageBox.yesNo( |
|
234 self, |
|
235 self.tr("Save Snapshot"), |
|
236 self.tr("<p>The file <b>{0}</b> already exists." |
|
237 " Overwrite it?</p>").format(fileName), |
|
238 icon=E5MessageBox.Warning) |
|
239 if not res: |
|
240 return False |
|
241 |
|
242 file = QFile(fileName) |
|
243 if not file.open(QFile.WriteOnly): |
|
244 E5MessageBox.warning( |
|
245 self, self.tr("Save Snapshot"), |
|
246 self.tr("Cannot write file '{0}:\n{1}.") |
|
247 .format(fileName, file.errorString())) |
|
248 return False |
|
249 |
|
250 ok = self.__snapshot.save(file) |
|
251 file.close() |
|
252 |
|
253 if not ok: |
|
254 E5MessageBox.warning( |
|
255 self, self.tr("Save Snapshot"), |
|
256 self.tr("Cannot write file '{0}:\n{1}.") |
|
257 .format(fileName, file.errorString())) |
|
258 |
|
259 return ok |
|
260 |
|
261 def __autoIncFilename(self): |
|
262 """ |
|
263 Private method to auto-increment the file name. |
|
264 """ |
|
265 # Extract the file name |
|
266 name = os.path.basename(self.__filename) |
|
267 |
|
268 # If the name contains a number, then increment it. |
|
269 numSearch = re.compile("(^|[^\\d])(\\d+)") |
|
270 # We want to match as far left as possible, and when the number is |
|
271 # at the start of the name. |
|
272 |
|
273 # Does it have a number? |
|
274 matches = list(numSearch.finditer(name)) |
|
275 if matches: |
|
276 # It has a number, increment it. |
|
277 match = matches[-1] |
|
278 start = match.start(2) |
|
279 # Only the second group is of interest. |
|
280 numAsStr = match.group(2) |
|
281 number = "{0:0{width}d}".format( |
|
282 int(numAsStr) + 1, width=len(numAsStr)) |
|
283 name = name[:start] + number + name[start + len(numAsStr):] |
|
284 else: |
|
285 # no number |
|
286 start = name.rfind('.') |
|
287 if start != -1: |
|
288 # has a '.' somewhere, e.g. it has an extension |
|
289 name = name[:start] + '-1' + name[start:] |
|
290 else: |
|
291 # no extension, just tack it on to the end |
|
292 name += '-1' |
|
293 |
|
294 self.__filename = os.path.join(os.path.dirname(self.__filename), name) |
|
295 self.__updateCaption() |
|
296 |
|
297 @pyqtSlot() |
|
298 def on_takeButton_clicked(self): |
|
299 """ |
|
300 Private slot to take a snapshot. |
|
301 """ |
|
302 self.__savedPosition = self.pos() |
|
303 self.hide() |
|
304 |
|
305 self.__grabber.grab( |
|
306 self.modeCombo.itemData(self.modeCombo.currentIndex()), |
|
307 self.delaySpin.value(), |
|
308 self.mouseCursorCheckBox.isChecked(), |
|
309 self.decorationsCheckBox.isChecked(), |
|
310 ) |
|
311 |
|
312 def __redisplay(self): |
|
313 """ |
|
314 Private method to redisplay the window. |
|
315 """ |
|
316 self.__updatePreview() |
|
317 if not self.__savedPosition.isNull(): |
|
318 self.move(self.__savedPosition) |
|
319 self.show() |
|
320 self.raise_() |
|
321 |
|
322 self.saveButton.setEnabled(not self.__snapshot.isNull()) |
|
323 self.copyButton.setEnabled(not self.__snapshot.isNull()) |
|
324 self.copyPreviewButton.setEnabled(not self.__snapshot.isNull()) |
|
325 |
|
326 @pyqtSlot() |
|
327 def on_copyButton_clicked(self): |
|
328 """ |
|
329 Private slot to copy the snapshot to the clipboard. |
|
330 """ |
|
331 if not self.__snapshot.isNull(): |
|
332 QApplication.clipboard().setPixmap(QPixmap(self.__snapshot)) |
|
333 |
|
334 @pyqtSlot() |
|
335 def on_copyPreviewButton_clicked(self): |
|
336 """ |
|
337 Private slot to copy the snapshot preview to the clipboard. |
|
338 """ |
|
339 QApplication.clipboard().setPixmap(self.preview.pixmap()) |
|
340 |
|
341 def __captured(self, pixmap): |
|
342 """ |
|
343 Private slot to show a preview of the snapshot. |
|
344 |
|
345 @param pixmap pixmap of the snapshot (QPixmap) |
|
346 """ |
|
347 self.__snapshot = QPixmap(pixmap) |
|
348 |
|
349 self.__redisplay() |
|
350 self.__modified = not pixmap.isNull() |
|
351 self.__updateCaption() |
|
352 |
|
353 def __updatePreview(self): |
|
354 """ |
|
355 Private slot to update the preview picture. |
|
356 """ |
|
357 self.preview.setToolTip(self.tr( |
|
358 "Preview of the snapshot image ({0} x {1})").format( |
|
359 self.__locale.toString(self.__snapshot.width()), |
|
360 self.__locale.toString(self.__snapshot.height())) |
|
361 ) |
|
362 self.preview.setPreview(self.__snapshot) |
|
363 self.preview.adjustSize() |
|
364 |
|
365 def resizeEvent(self, evt): |
|
366 """ |
|
367 Protected method handling a resizing of the window. |
|
368 |
|
369 @param evt resize event (QResizeEvent) |
|
370 """ |
|
371 self.__updateTimer.start(200) |
|
372 |
|
373 def __dragSnapshot(self): |
|
374 """ |
|
375 Private slot handling the dragging of the preview picture. |
|
376 """ |
|
377 drag = QDrag(self) |
|
378 mimeData = QMimeData() |
|
379 mimeData.setImageData(self.__snapshot) |
|
380 drag.setMimeData(mimeData) |
|
381 drag.setPixmap(self.preview.pixmap()) |
|
382 drag.exec(Qt.DropAction.CopyAction) |
|
383 |
|
384 def closeEvent(self, evt): |
|
385 """ |
|
386 Protected method handling the close event. |
|
387 |
|
388 @param evt close event (QCloseEvent) |
|
389 """ |
|
390 if self.__modified: |
|
391 res = E5MessageBox.question( |
|
392 self, |
|
393 self.tr("eric Snapshot"), |
|
394 self.tr( |
|
395 """The application contains an unsaved snapshot."""), |
|
396 E5MessageBox.StandardButtons( |
|
397 E5MessageBox.Abort | |
|
398 E5MessageBox.Discard | |
|
399 E5MessageBox.Save)) |
|
400 if res == E5MessageBox.Abort: |
|
401 evt.ignore() |
|
402 return |
|
403 elif res == E5MessageBox.Save: |
|
404 self.on_saveButton_clicked() |
|
405 |
|
406 Preferences.Prefs.settings.setValue( |
|
407 "Snapshot/Delay", self.delaySpin.value()) |
|
408 modeData = self.modeCombo.itemData(self.modeCombo.currentIndex()) |
|
409 if modeData is not None: |
|
410 Preferences.Prefs.settings.setValue( |
|
411 "Snapshot/Mode", |
|
412 modeData.value) |
|
413 Preferences.Prefs.settings.setValue( |
|
414 "Snapshot/Filename", self.__filename) |
|
415 Preferences.Prefs.settings.sync() |
|
416 |
|
417 def __updateCaption(self): |
|
418 """ |
|
419 Private method to update the window caption. |
|
420 """ |
|
421 self.setWindowTitle("{0}[*] - {1}".format( |
|
422 os.path.basename(self.__filename), |
|
423 self.tr("eric Snapshot"))) |
|
424 self.setWindowModified(self.__modified) |
|
425 self.pathNameEdit.setText(os.path.dirname(self.__filename)) |
|
426 |
|
427 @pyqtSlot(int) |
|
428 def on_modeCombo_currentIndexChanged(self, index): |
|
429 """ |
|
430 Private slot handling the selection of a screenshot mode. |
|
431 |
|
432 @param index index of the selection |
|
433 @type int |
|
434 """ |
|
435 isWindowMode = False |
|
436 if index >= 0: |
|
437 mode = self.modeCombo.itemData(index) |
|
438 isWindowMode = (mode == SnapshotModes.SELECTEDWINDOW) |
|
439 |
|
440 self.decorationsCheckBox.setEnabled(isWindowMode) |
|
441 self.decorationsCheckBox.setChecked(isWindowMode) |