Snapshot/SnapshotFreehandGrabber.py

branch
Py2 comp.
changeset 3057
10516539f238
parent 2525
8b507a9a2d40
parent 2997
7f0ef975da9e
child 3058
0a02c433f52d
equal deleted inserted replaced
3056:9986ec0e559a 3057:10516539f238
8 """ 8 """
9 9
10 from __future__ import unicode_literals # __IGNORE_WARNING__ 10 from __future__ import unicode_literals # __IGNORE_WARNING__
11 11
12 from PyQt4.QtCore import pyqtSignal, Qt, QRect, QPoint, QTimer, qVersion 12 from PyQt4.QtCore import pyqtSignal, Qt, QRect, QPoint, QTimer, qVersion
13 from PyQt4.QtGui import QWidget, QPixmap, QColor, QRegion, QApplication, QPainter, \ 13 from PyQt4.QtGui import QWidget, QPixmap, QColor, QRegion, QApplication, \
14 QPalette, QToolTip, QPolygon, QPen, QBrush, QPaintEngine 14 QPainter, QPalette, QToolTip, QPolygon, QPen, QBrush, QPaintEngine
15 15
16 16
17 def drawPolygon(painter, polygon, outline, fill=QColor()): 17 def drawPolygon(painter, polygon, outline, fill=QColor()):
18 """ 18 """
19 Module function to draw a polygon with the given parameters. 19 Module function to draw a polygon with the given parameters.
63 self.__dragStartPoint = QPoint() 63 self.__dragStartPoint = QPoint()
64 self.__selectionBeforeDrag = QPolygon() 64 self.__selectionBeforeDrag = QPolygon()
65 65
66 self.__helpTextRect = QRect() 66 self.__helpTextRect = QRect()
67 self.__helpText = self.trUtf8( 67 self.__helpText = self.trUtf8(
68 "Select a region using the mouse. To take the snapshot, press the Enter key" 68 "Select a region using the mouse. To take the snapshot,"
69 " or double click. Press Esc to quit.") 69 " press the Enter key or double click. Press Esc to quit.")
70 70
71 self.__pixmap = QPixmap() 71 self.__pixmap = QPixmap()
72 self.__pBefore = QPoint() 72 self.__pBefore = QPoint()
73 73
74 self.setMouseTracking(True) 74 self.setMouseTracking(True)
119 painter.setFont(font) 119 painter.setFont(font)
120 120
121 pol = QPolygon(self.__selection) 121 pol = QPolygon(self.__selection)
122 if not self.__selection.boundingRect().isNull(): 122 if not self.__selection.boundingRect().isNull():
123 # Draw outline around selection. 123 # Draw outline around selection.
124 # Important: the 1px-wide outline is *also* part of the captured free-region 124 # Important: the 1px-wide outline is *also* part of the
125 pen = QPen(handleColor, 1, Qt.SolidLine, Qt.SquareCap, Qt.BevelJoin) 125 # captured free-region
126 pen = QPen(handleColor, 1, Qt.SolidLine, Qt.SquareCap,
127 Qt.BevelJoin)
126 painter.setPen(pen) 128 painter.setPen(pen)
127 painter.drawPolygon(pol) 129 painter.drawPolygon(pol)
128 130
129 # Draw the grey area around the selection. 131 # Draw the grey area around the selection.
130 grey = QRegion(self.rect()) 132 grey = QRegion(self.rect())
137 drawPolygon(painter, pol, handleColor) 139 drawPolygon(painter, pol, handleColor)
138 140
139 if self.__showHelp: 141 if self.__showHelp:
140 painter.setPen(textColor) 142 painter.setPen(textColor)
141 painter.setBrush(textBackgroundColor) 143 painter.setBrush(textBackgroundColor)
142 self.__helpTextRect = painter.boundingRect(self.rect().adjusted(2, 2, -2, -2), 144 self.__helpTextRect = painter.boundingRect(
145 self.rect().adjusted(2, 2, -2, -2),
143 Qt.TextWordWrap, self.__helpText).translated( 146 Qt.TextWordWrap, self.__helpText).translated(
144 -self.__desktop.x(), -self.__desktop.y()) 147 -self.__desktop.x(), -self.__desktop.y())
145 self.__helpTextRect.adjust(-2, -2, 4, 2) 148 self.__helpTextRect.adjust(-2, -2, 4, 2)
146 drawPolygon(painter, self.__helpTextRect, textColor, textBackgroundColor) 149 drawPolygon(painter, self.__helpTextRect, textColor,
150 textBackgroundColor)
147 painter.drawText(self.__helpTextRect.adjusted(3, 3, -3, -3), 151 painter.drawText(self.__helpTextRect.adjusted(3, 3, -3, -3),
148 Qt.TextWordWrap, self.__helpText) 152 Qt.TextWordWrap, self.__helpText)
149 153
150 if self.__selection.isEmpty(): 154 if self.__selection.isEmpty():
151 return 155 return
152 156
153 # The grabbed region is everything which is covered by the drawn 157 # The grabbed region is everything which is covered by the drawn
154 # rectangles (border included). This means that there is no 0px 158 # rectangles (border included). This means that there is no 0px
155 # selection, since a 0px wide rectangle will always be drawn as a line. 159 # selection, since a 0px wide rectangle will always be drawn as a line.
156 boundingRect = self.__selection.boundingRect() 160 boundingRect = self.__selection.boundingRect()
157 txt = "{0:n}, {1:n} ({2:n} x {3:n})".format(boundingRect.x(), boundingRect.y(), 161 txt = "{0:n}, {1:n} ({2:n} x {3:n})".format(
162 boundingRect.x(), boundingRect.y(),
158 boundingRect.width(), boundingRect.height()) 163 boundingRect.width(), boundingRect.height())
159 textRect = painter.boundingRect(self.rect(), Qt.AlignLeft, txt) 164 textRect = painter.boundingRect(self.rect(), Qt.AlignLeft, txt)
160 boundingRect = textRect.adjusted(-4, 0, 0, 0) 165 boundingRect = textRect.adjusted(-4, 0, 0, 0)
161 166
162 polBoundingRect = pol.boundingRect() 167 polBoundingRect = pol.boundingRect()
163 if textRect.width() < polBoundingRect.width() - 2 * self.__handleSize and \ 168 if (textRect.width() <
164 textRect.height() < polBoundingRect.height() - 2 * self.__handleSize and \ 169 polBoundingRect.width() - 2 * self.__handleSize) and \
170 (textRect.height() <
171 polBoundingRect.height() - 2 * self.__handleSize) and \
165 polBoundingRect.width() > 100 and \ 172 polBoundingRect.width() > 100 and \
166 polBoundingRect.height() > 100: 173 polBoundingRect.height() > 100:
167 # center, unsuitable for small selections 174 # center, unsuitable for small selections
168 boundingRect.moveCenter(polBoundingRect.center()) 175 boundingRect.moveCenter(polBoundingRect.center())
169 textRect.moveCenter(polBoundingRect.center()) 176 textRect.moveCenter(polBoundingRect.center())
178 # left, top aligned 185 # left, top aligned
179 boundingRect.moveTopRight( 186 boundingRect.moveTopRight(
180 QPoint(polBoundingRect.x() - 3, polBoundingRect.y())) 187 QPoint(polBoundingRect.x() - 3, polBoundingRect.y()))
181 textRect.moveTopRight( 188 textRect.moveTopRight(
182 QPoint(polBoundingRect.x() - 5, polBoundingRect.y())) 189 QPoint(polBoundingRect.x() - 5, polBoundingRect.y()))
183 elif polBoundingRect.bottom() + 3 + textRect.height() < self.rect().bottom() and \ 190 elif (polBoundingRect.bottom() + 3 + textRect.height() <
191 self.rect().bottom()) and \
184 polBoundingRect.right() > textRect.width(): 192 polBoundingRect.right() > textRect.width():
185 # at bottom, right aligned 193 # at bottom, right aligned
186 boundingRect.moveTopRight( 194 boundingRect.moveTopRight(
187 QPoint(polBoundingRect.right(), polBoundingRect.bottom() + 3)) 195 QPoint(polBoundingRect.right(), polBoundingRect.bottom() + 3))
188 textRect.moveTopRight( 196 textRect.moveTopRight(
189 QPoint(polBoundingRect.right() - 2, polBoundingRect.bottom() + 3)) 197 QPoint(polBoundingRect.right() - 2,
190 elif polBoundingRect.right() + textRect.width() + 3 < self.rect().width(): 198 polBoundingRect.bottom() + 3))
199 elif polBoundingRect.right() + textRect.width() + 3 < \
200 self.rect().width():
191 # right, bottom aligned 201 # right, bottom aligned
192 boundingRect.moveBottomLeft( 202 boundingRect.moveBottomLeft(
193 QPoint(polBoundingRect.right() + 3, polBoundingRect.bottom())) 203 QPoint(polBoundingRect.right() + 3, polBoundingRect.bottom()))
194 textRect.moveBottomLeft( 204 textRect.moveBottomLeft(
195 QPoint(polBoundingRect.right() + 5, polBoundingRect.bottom())) 205 QPoint(polBoundingRect.right() + 5, polBoundingRect.bottom()))
196 206
197 # If the above didn't catch it, you are running on a very tiny screen... 207 # If the above didn't catch it, you are running on a very
208 # tiny screen...
198 drawPolygon(painter, boundingRect, textColor, textBackgroundColor) 209 drawPolygon(painter, boundingRect, textColor, textBackgroundColor)
199 painter.drawText(textRect, Qt.AlignHCenter, txt) 210 painter.drawText(textRect, Qt.AlignHCenter, txt)
200 211
201 if (polBoundingRect.height() > self.__handleSize * 2 and \ 212 if (polBoundingRect.height() > self.__handleSize * 2 and \
202 polBoundingRect.width() > self.__handleSize * 2) or \ 213 polBoundingRect.width() > self.__handleSize * 2) or \
245 p = evt.pos() 256 p = evt.pos()
246 self.__selection.append(p) 257 self.__selection.append(p)
247 else: 258 else:
248 # moving the whole selection 259 # moving the whole selection
249 p = evt.pos() - self.__pBefore # Offset 260 p = evt.pos() - self.__pBefore # Offset
250 self.__pBefore = evt.pos() # save position for next iteration 261 self.__pBefore = evt.pos() # save position for
262 # next iteration
251 self.__selection.translate(p) 263 self.__selection.translate(p)
252 264
253 self.update() 265 self.update()
254 else: 266 else:
255 if self.__selection.boundingRect().isEmpty(): 267 if self.__selection.boundingRect().isEmpty():
309 pixmap2.fill(Qt.transparent) 321 pixmap2.fill(Qt.transparent)
310 322
311 pt = QPainter() 323 pt = QPainter()
312 pt.begin(pixmap2) 324 pt.begin(pixmap2)
313 if pt.paintEngine().hasFeature(QPaintEngine.PorterDuff): 325 if pt.paintEngine().hasFeature(QPaintEngine.PorterDuff):
314 pt.setRenderHints(QPainter.Antialiasing | \ 326 pt.setRenderHints(
315 QPainter.HighQualityAntialiasing | QPainter.SmoothPixmapTransform, 327 QPainter.Antialiasing | \
328 QPainter.HighQualityAntialiasing | \
329 QPainter.SmoothPixmapTransform,
316 True) 330 True)
317 pt.setBrush(Qt.black) 331 pt.setBrush(Qt.black)
318 pt.setPen(QPen(QBrush(Qt.black), 0.5)) 332 pt.setPen(QPen(QBrush(Qt.black), 0.5))
319 pt.drawPolygon(translatedPol) 333 pt.drawPolygon(translatedPol)
320 pt.setCompositionMode(QPainter.CompositionMode_SourceIn) 334 pt.setCompositionMode(QPainter.CompositionMode_SourceIn)

eric ide

mercurial