7 Module implementing a grabber widget for a rectangular snapshot region. |
7 Module implementing a grabber widget for a rectangular snapshot region. |
8 """ |
8 """ |
9 |
9 |
10 |
10 |
11 from PyQt5.QtCore import pyqtSignal, Qt, QRect, QPoint, QTimer, QLocale |
11 from PyQt5.QtCore import pyqtSignal, Qt, QRect, QPoint, QTimer, QLocale |
12 from PyQt5.QtGui import QPixmap, QColor, QRegion, QPainter, QPalette, \ |
12 from PyQt5.QtGui import ( |
13 QPaintEngine, QPen, QBrush |
13 QPixmap, QColor, QRegion, QPainter, QPalette, QPaintEngine, QPen, QBrush |
|
14 ) |
14 from PyQt5.QtWidgets import QWidget, QApplication, QToolTip |
15 from PyQt5.QtWidgets import QWidget, QApplication, QToolTip |
15 |
16 |
16 |
17 |
17 def drawRect(painter, rect, outline, fill=None): |
18 def drawRect(painter, rect, outline, fill=None): |
18 """ |
19 """ |
187 self.__locale.toString(self.__selection.height()) |
188 self.__locale.toString(self.__selection.height()) |
188 ) |
189 ) |
189 textRect = painter.boundingRect(self.rect(), Qt.AlignLeft, txt) |
190 textRect = painter.boundingRect(self.rect(), Qt.AlignLeft, txt) |
190 boundingRect = textRect.adjusted(-4, 0, 0, 0) |
191 boundingRect = textRect.adjusted(-4, 0, 0, 0) |
191 |
192 |
192 if textRect.width() < r.width() - 2 * self.__handleSize and \ |
193 if ( |
193 textRect.height() < r.height() - 2 * self.__handleSize and \ |
194 textRect.width() < r.width() - 2 * self.__handleSize and |
194 r.width() > 100 and \ |
195 textRect.height() < r.height() - 2 * self.__handleSize and |
195 r.height() > 100: |
196 r.width() > 100 and |
|
197 r.height() > 100 |
|
198 ): |
196 # center, unsuitable for small selections |
199 # center, unsuitable for small selections |
197 boundingRect.moveCenter(r.center()) |
200 boundingRect.moveCenter(r.center()) |
198 textRect.moveCenter(r.center()) |
201 textRect.moveCenter(r.center()) |
199 elif r.y() - 3 > textRect.height() and \ |
202 elif ( |
200 r.x() + textRect.width() < self.rect().width(): |
203 r.y() - 3 > textRect.height() and |
|
204 r.x() + textRect.width() < self.rect().width() |
|
205 ): |
201 # on top, left aligned |
206 # on top, left aligned |
202 boundingRect.moveBottomLeft(QPoint(r.x(), r.y() - 3)) |
207 boundingRect.moveBottomLeft(QPoint(r.x(), r.y() - 3)) |
203 textRect.moveBottomLeft(QPoint(r.x() + 2, r.y() - 3)) |
208 textRect.moveBottomLeft(QPoint(r.x() + 2, r.y() - 3)) |
204 elif r.x() - 3 > textRect.width(): |
209 elif r.x() - 3 > textRect.width(): |
205 # left, top aligned |
210 # left, top aligned |
206 boundingRect.moveTopRight(QPoint(r.x() - 3, r.y())) |
211 boundingRect.moveTopRight(QPoint(r.x() - 3, r.y())) |
207 textRect.moveTopRight(QPoint(r.x() - 5, r.y())) |
212 textRect.moveTopRight(QPoint(r.x() - 5, r.y())) |
208 elif r.bottom() + 3 + textRect.height() < self.rect().bottom() and \ |
213 elif ( |
209 r.right() > textRect.width(): |
214 r.bottom() + 3 + textRect.height() < self.rect().bottom() and |
|
215 r.right() > textRect.width() |
|
216 ): |
210 # at bottom, right aligned |
217 # at bottom, right aligned |
211 boundingRect.moveTopRight(QPoint(r.right(), r.bottom() + 3)) |
218 boundingRect.moveTopRight(QPoint(r.right(), r.bottom() + 3)) |
212 textRect.moveTopRight(QPoint(r.right() - 2, r.bottom() + 3)) |
219 textRect.moveTopRight(QPoint(r.right() - 2, r.bottom() + 3)) |
213 elif r.right() + textRect.width() + 3 < self.rect().width(): |
220 elif r.right() + textRect.width() + 3 < self.rect().width(): |
214 # right, bottom aligned |
221 # right, bottom aligned |
218 # If the above didn't catch it, you are running on a very |
225 # If the above didn't catch it, you are running on a very |
219 # tiny screen... |
226 # tiny screen... |
220 drawRect(painter, boundingRect, textColor, textBackgroundColor) |
227 drawRect(painter, boundingRect, textColor, textBackgroundColor) |
221 painter.drawText(textRect, Qt.AlignHCenter, txt) |
228 painter.drawText(textRect, Qt.AlignHCenter, txt) |
222 |
229 |
223 if (r.height() > self.__handleSize * 2 and |
230 if ( |
224 r.width() > self.__handleSize * 2) or \ |
231 (r.height() > self.__handleSize * 2 and |
225 not self.__mouseDown: |
232 r.width() > self.__handleSize * 2) or |
|
233 not self.__mouseDown |
|
234 ): |
226 self.__updateHandles() |
235 self.__updateHandles() |
227 painter.setPen(Qt.NoPen) |
236 painter.setPen(Qt.NoPen) |
228 painter.setBrush(handleColor) |
237 painter.setBrush(handleColor) |
229 painter.setClipRegion( |
238 painter.setClipRegion( |
230 self.__handleMask(SnapshotRegionGrabber.StrokeMask)) |
239 self.__handleMask(SnapshotRegionGrabber.StrokeMask)) |
306 else: |
315 else: |
307 # dragging a handle |
316 # dragging a handle |
308 r = QRect(self.__selectionBeforeDrag) |
317 r = QRect(self.__selectionBeforeDrag) |
309 offset = evt.pos() - self.__dragStartPoint |
318 offset = evt.pos() - self.__dragStartPoint |
310 |
319 |
311 if self.__mouseOverHandle in \ |
320 if self.__mouseOverHandle in [ |
312 [self.__TLHandle, self.__THandle, self.__TRHandle]: |
321 self.__TLHandle, self.__THandle, self.__TRHandle]: |
313 r.setTop(r.top() + offset.y()) |
322 r.setTop(r.top() + offset.y()) |
314 |
323 |
315 if self.__mouseOverHandle in \ |
324 if self.__mouseOverHandle in [ |
316 [self.__TLHandle, self.__LHandle, self.__BLHandle]: |
325 self.__TLHandle, self.__LHandle, self.__BLHandle]: |
317 r.setLeft(r.left() + offset.x()) |
326 r.setLeft(r.left() + offset.x()) |
318 |
327 |
319 if self.__mouseOverHandle in \ |
328 if self.__mouseOverHandle in [ |
320 [self.__BLHandle, self.__BHandle, self.__BRHandle]: |
329 self.__BLHandle, self.__BHandle, self.__BRHandle]: |
321 r.setBottom(r.bottom() + offset.y()) |
330 r.setBottom(r.bottom() + offset.y()) |
322 |
331 |
323 if self.__mouseOverHandle in \ |
332 if self.__mouseOverHandle in [ |
324 [self.__TRHandle, self.__RHandle, self.__BRHandle]: |
333 self.__TRHandle, self.__RHandle, self.__BRHandle]: |
325 r.setRight(r.right() + offset.x()) |
334 r.setRight(r.right() + offset.x()) |
326 |
335 |
327 r.setTopLeft(self.__limitPointToRect(r.topLeft(), self.rect())) |
336 r.setTopLeft(self.__limitPointToRect(r.topLeft(), self.rect())) |
328 r.setBottomRight( |
337 r.setBottomRight( |
329 self.__limitPointToRect(r.bottomRight(), self.rect())) |
338 self.__limitPointToRect(r.bottomRight(), self.rect())) |
367 |
376 |
368 @param evt mouse release event (QMouseEvent) |
377 @param evt mouse release event (QMouseEvent) |
369 """ |
378 """ |
370 self.__mouseDown = False |
379 self.__mouseDown = False |
371 self.__newSelection = False |
380 self.__newSelection = False |
372 if self.__mouseOverHandle is None and \ |
381 if ( |
373 self.__selection.contains(evt.pos()): |
382 self.__mouseOverHandle is None and |
|
383 self.__selection.contains(evt.pos()) |
|
384 ): |
374 self.setCursor(Qt.OpenHandCursor) |
385 self.setCursor(Qt.OpenHandCursor) |
375 self.update() |
386 self.update() |
376 |
387 |
377 def mouseDoubleClickEvent(self, evt): |
388 def mouseDoubleClickEvent(self, evt): |
378 """ |
389 """ |