Snapshot/SnapWidget.py

changeset 1770
c17e67e69ef5
child 1772
f325dfdc8f6b
equal deleted inserted replaced
1768:8a04ce23e083 1770:c17e67e69ef5
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2012 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing the snapshot widget.
8 """
9
10 from PyQt4.QtCore import pyqtSlot, QFile, QFileInfo
11 from PyQt4.QtGui import QWidget, QImageWriter, QApplication
12
13 from E5Gui import E5FileDialog, E5MessageBox
14
15 from .Ui_SnapWidget import Ui_SnapWidget
16
17 from .SnapshotRegionGrabber import SnapshotRegionGrabber
18
19 import UI.PixmapCache
20 import Preferences
21
22
23 class SnapWidget(QWidget, Ui_SnapWidget):
24 """
25 Class implementing the snapshot widget.
26 """
27 def __init__(self, parent=None):
28 """
29 Constructor
30
31 @param parent reference to the parent widget (QWidget)
32 """
33 super().__init__(parent)
34 self.setupUi(self)
35
36 self.saveButton.setIcon(UI.PixmapCache.getIcon("fileSaveAs.png"))
37 self.takeButton.setIcon(UI.PixmapCache.getIcon("cameraPhoto.png"))
38 self.copyButton.setIcon(UI.PixmapCache.getIcon("editCopy.png"))
39 self.setWindowIcon(UI.PixmapCache.getIcon("ericSnap.png"))
40
41 self.modeCombo.addItem(self.trUtf8("Fullscreen"),
42 SnapshotRegionGrabber.ModeFullscreen)
43 self.modeCombo.addItem(self.trUtf8("Rectangular Selection"),
44 SnapshotRegionGrabber.ModeRectangle)
45 mode = int(Preferences.Prefs.settings.value("Snapshot/Mode", 0))
46 index = self.modeCombo.findData(mode)
47 self.modeCombo.setCurrentIndex(index)
48
49 self.delaySpin.setValue(int(Preferences.Prefs.settings.value(
50 "Snapshot/Delay", 0)))
51
52 self.__grabber = None
53
54 self.__initFileFilters()
55
56 def __initFileFilters(self):
57 """
58 Private method to define the supported image file filters.
59 """
60 filters = {
61 'bmp': self.trUtf8("Windows Bitmap File (*.bmp)"),
62 'gif': self.trUtf8("Graphic Interchange Format File (*.gif)"),
63 'ico': self.trUtf8("Windows Icon File (*.ico)"),
64 'jpg': self.trUtf8("JPEG File (*.jpg)"),
65 'mng': self.trUtf8("Multiple-Image Network Graphics File (*.mng)"),
66 'pbm': self.trUtf8("Portable Bitmap File (*.pbm)"),
67 'pcx': self.trUtf8("Paintbrush Bitmap File (*.pcx)"),
68 'pgm': self.trUtf8("Portable Graymap File (*.pgm)"),
69 'png': self.trUtf8("Portable Network Graphics File (*.png)"),
70 'ppm': self.trUtf8("Portable Pixmap File (*.ppm)"),
71 'sgi': self.trUtf8("Silicon Graphics Image File (*.sgi)"),
72 'svg': self.trUtf8("Scalable Vector Graphics File (*.svg)"),
73 'tga': self.trUtf8("Targa Graphic File (*.tga)"),
74 'tif': self.trUtf8("TIFF File (*.tif)"),
75 'xbm': self.trUtf8("X11 Bitmap File (*.xbm)"),
76 'xpm': self.trUtf8("X11 Pixmap File (*.xpm)"),
77 }
78
79 outputFormats = []
80 writeFormats = QImageWriter.supportedImageFormats()
81 for writeFormat in writeFormats:
82 try:
83 outputFormats.append(filters[bytes(writeFormat).decode()])
84 except KeyError:
85 pass
86 outputFormats.sort()
87 self.__outputFilter = ';;'.join(outputFormats)
88
89 self.__defaultFilter = filters['png']
90
91 @pyqtSlot(bool)
92 def on_saveButton_clicked(self, checked):
93 """
94 Private slot to save the snapshot.
95 """
96 fileName, selectedFilter = E5FileDialog.getSaveFileNameAndFilter(
97 self,
98 self.trUtf8("Save Snapshot"),
99 "",
100 self.__outputFilter,
101 self.__defaultFilter,
102 E5FileDialog.Options(E5FileDialog.DontConfirmOverwrite))
103 if not fileName:
104 return
105
106 ext = QFileInfo(fileName).suffix()
107 if not ext:
108 ex = selectedFilter.split("(*")[1].split(")")[0]
109 if ex:
110 fileName += ex
111 if QFileInfo(fileName).exists():
112 res = E5MessageBox.yesNo(self,
113 self.trUtf8("Save Snapshot"),
114 self.trUtf8("<p>The file <b>{0}</b> already exists."
115 " Overwrite it?</p>").format(fileName),
116 icon=E5MessageBox.Warning)
117 if not res:
118 return
119
120 file = QFile(fileName)
121 if not file.open(QFile.WriteOnly):
122 E5MessageBox.warning(self, self.trUtf8("Save Snapshot"),
123 self.trUtf8("Cannot write file '{0}:\n{1}.")\
124 .format(fileName, file.errorString()))
125 return
126
127 res = self.preview.pixmap().save(file)
128 file.close()
129
130 if not res:
131 E5MessageBox.warning(self, self.trUtf8("Save Snapshot"),
132 self.trUtf8("Cannot write file '{0}:\n{1}.")\
133 .format(fileName, file.errorString()))
134
135 @pyqtSlot(bool)
136 def on_takeButton_clicked(self, checked):
137 """
138 Private slot to take a snapshot.
139 """
140 mode = self.modeCombo.itemData(self.modeCombo.currentIndex())
141 delay = self.delaySpin.value()
142
143 Preferences.Prefs.settings.setValue("Snapshot/Delay", delay)
144 Preferences.Prefs.settings.setValue("Snapshot/Mode", mode)
145
146 self.hide()
147
148 self.__grabber = SnapshotRegionGrabber(mode, delay)
149 self.__grabber.grabbed.connect(self.__captured)
150
151 @pyqtSlot()
152 def on_copyButton_clicked(self):
153 """
154 Private slot to copy the snapshot to the clipboard.
155 """
156 QApplication.clipboard().setPixmap(self.preview.pixmap())
157
158 def __captured(self, pixmap):
159 """
160 Private slot to show a preview of the snapshot.
161
162 @param pixmap pixmap of the snapshot (QPixmap)
163 """
164 self.__grabber.close()
165 self.preview.setPixmap(pixmap)
166
167 self.__grabber = None
168
169 self.saveButton.setEnabled(True)
170 self.copyButton.setEnabled(True)
171 self.show()

eric ide

mercurial