7 Module implementing a grabber widget for a freehand snapshot region. |
7 Module implementing a grabber widget for a freehand 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 QPolygon, QPen, QBrush, QPaintEngine |
13 QPixmap, QColor, QRegion, QPainter, QPalette, QPolygon, QPen, QBrush, |
|
14 QPaintEngine |
|
15 ) |
14 from PyQt5.QtWidgets import QWidget, QApplication, QToolTip |
16 from PyQt5.QtWidgets import QWidget, QApplication, QToolTip |
15 |
17 |
16 |
18 |
17 def drawPolygon(painter, polygon, outline, fill=None): |
19 def drawPolygon(painter, polygon, outline, fill=None): |
18 """ |
20 """ |
166 ) |
168 ) |
167 textRect = painter.boundingRect(self.rect(), Qt.AlignLeft, txt) |
169 textRect = painter.boundingRect(self.rect(), Qt.AlignLeft, txt) |
168 boundingRect = textRect.adjusted(-4, 0, 0, 0) |
170 boundingRect = textRect.adjusted(-4, 0, 0, 0) |
169 |
171 |
170 polBoundingRect = pol.boundingRect() |
172 polBoundingRect = pol.boundingRect() |
171 if (textRect.width() < |
173 if ( |
172 polBoundingRect.width() - 2 * self.__handleSize) and \ |
174 (textRect.width() < |
173 (textRect.height() < |
175 polBoundingRect.width() - 2 * self.__handleSize) and |
174 polBoundingRect.height() - 2 * self.__handleSize) and \ |
176 (textRect.height() < |
175 polBoundingRect.width() > 100 and \ |
177 polBoundingRect.height() - 2 * self.__handleSize) and |
176 polBoundingRect.height() > 100: |
178 polBoundingRect.width() > 100 and |
|
179 polBoundingRect.height() > 100 |
|
180 ): |
177 # center, unsuitable for small selections |
181 # center, unsuitable for small selections |
178 boundingRect.moveCenter(polBoundingRect.center()) |
182 boundingRect.moveCenter(polBoundingRect.center()) |
179 textRect.moveCenter(polBoundingRect.center()) |
183 textRect.moveCenter(polBoundingRect.center()) |
180 elif polBoundingRect.y() - 3 > textRect.height() and \ |
184 elif ( |
181 polBoundingRect.x() + textRect.width() < self.rect().width(): |
185 polBoundingRect.y() - 3 > textRect.height() and |
|
186 polBoundingRect.x() + textRect.width() < self.rect().width() |
|
187 ): |
182 # on top, left aligned |
188 # on top, left aligned |
183 boundingRect.moveBottomLeft( |
189 boundingRect.moveBottomLeft( |
184 QPoint(polBoundingRect.x(), polBoundingRect.y() - 3)) |
190 QPoint(polBoundingRect.x(), polBoundingRect.y() - 3)) |
185 textRect.moveBottomLeft( |
191 textRect.moveBottomLeft( |
186 QPoint(polBoundingRect.x() + 2, polBoundingRect.y() - 3)) |
192 QPoint(polBoundingRect.x() + 2, polBoundingRect.y() - 3)) |
188 # left, top aligned |
194 # left, top aligned |
189 boundingRect.moveTopRight( |
195 boundingRect.moveTopRight( |
190 QPoint(polBoundingRect.x() - 3, polBoundingRect.y())) |
196 QPoint(polBoundingRect.x() - 3, polBoundingRect.y())) |
191 textRect.moveTopRight( |
197 textRect.moveTopRight( |
192 QPoint(polBoundingRect.x() - 5, polBoundingRect.y())) |
198 QPoint(polBoundingRect.x() - 5, polBoundingRect.y())) |
193 elif (polBoundingRect.bottom() + 3 + textRect.height() < |
199 elif ( |
194 self.rect().bottom()) and \ |
200 (polBoundingRect.bottom() + 3 + textRect.height() < |
195 polBoundingRect.right() > textRect.width(): |
201 self.rect().bottom()) and |
|
202 polBoundingRect.right() > textRect.width() |
|
203 ): |
196 # at bottom, right aligned |
204 # at bottom, right aligned |
197 boundingRect.moveTopRight( |
205 boundingRect.moveTopRight( |
198 QPoint(polBoundingRect.right(), polBoundingRect.bottom() + 3)) |
206 QPoint(polBoundingRect.right(), polBoundingRect.bottom() + 3)) |
199 textRect.moveTopRight( |
207 textRect.moveTopRight( |
200 QPoint(polBoundingRect.right() - 2, |
208 QPoint(polBoundingRect.right() - 2, |
201 polBoundingRect.bottom() + 3)) |
209 polBoundingRect.bottom() + 3)) |
202 elif polBoundingRect.right() + textRect.width() + 3 < \ |
210 elif ( |
203 self.rect().width(): |
211 polBoundingRect.right() + textRect.width() + 3 < |
|
212 self.rect().width() |
|
213 ): |
204 # right, bottom aligned |
214 # right, bottom aligned |
205 boundingRect.moveBottomLeft( |
215 boundingRect.moveBottomLeft( |
206 QPoint(polBoundingRect.right() + 3, polBoundingRect.bottom())) |
216 QPoint(polBoundingRect.right() + 3, polBoundingRect.bottom())) |
207 textRect.moveBottomLeft( |
217 textRect.moveBottomLeft( |
208 QPoint(polBoundingRect.right() + 5, polBoundingRect.bottom())) |
218 QPoint(polBoundingRect.right() + 5, polBoundingRect.bottom())) |
210 # If the above didn't catch it, you are running on a very |
220 # If the above didn't catch it, you are running on a very |
211 # tiny screen... |
221 # tiny screen... |
212 drawPolygon(painter, boundingRect, textColor, textBackgroundColor) |
222 drawPolygon(painter, boundingRect, textColor, textBackgroundColor) |
213 painter.drawText(textRect, Qt.AlignHCenter, txt) |
223 painter.drawText(textRect, Qt.AlignHCenter, txt) |
214 |
224 |
215 if (polBoundingRect.height() > self.__handleSize * 2 and |
225 if ( |
216 polBoundingRect.width() > self.__handleSize * 2) or \ |
226 (polBoundingRect.height() > self.__handleSize * 2 and |
217 not self.__mouseDown: |
227 polBoundingRect.width() > self.__handleSize * 2) or |
|
228 not self.__mouseDown |
|
229 ): |
218 painter.setBrush(Qt.transparent) |
230 painter.setBrush(Qt.transparent) |
219 painter.setClipRegion(QRegion(pol)) |
231 painter.setClipRegion(QRegion(pol)) |
220 painter.drawPolygon(QPolygon(self.rect())) |
232 painter.drawPolygon(QPolygon(self.rect())) |
221 |
233 |
222 def mousePressEvent(self, evt): |
234 def mousePressEvent(self, evt): |