ScreenMagnifier 1.11.0.post8__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of ScreenMagnifier might be problematic. Click here for more details.

Files changed (117) hide show
  1. ScreenMagnifier/__init__.py +68 -0
  2. ScreenMagnifier/__main__.py +17 -0
  3. ScreenMagnifier/advancedoverlay.py +593 -0
  4. ScreenMagnifier/advancedpanel.py +521 -0
  5. ScreenMagnifier/analysiscontroller.py +533 -0
  6. ScreenMagnifier/analysisserver.py +404 -0
  7. ScreenMagnifier/colordetector/__init__.py +28 -0
  8. ScreenMagnifier/colordetector/colordetector.py +570 -0
  9. ScreenMagnifier/colordetector/core.py +434 -0
  10. ScreenMagnifier/colordetector/main.py +23 -0
  11. ScreenMagnifier/colordetector/mainwindow.py +182 -0
  12. ScreenMagnifier/colorpanel.py +424 -0
  13. ScreenMagnifier/colorswatches/__init__.py +16 -0
  14. ScreenMagnifier/colorswatches/colorswatches.py +316 -0
  15. ScreenMagnifier/colorswatches/main.py +23 -0
  16. ScreenMagnifier/colorswatches/mainwindow.py +60 -0
  17. ScreenMagnifier/controlpanel.py +234 -0
  18. ScreenMagnifier/detectionpanel.py +790 -0
  19. ScreenMagnifier/facedetector/__init__.py +24 -0
  20. ScreenMagnifier/facedetector/core.py +314 -0
  21. ScreenMagnifier/facedetector/facedetector.py +288 -0
  22. ScreenMagnifier/facedetector/main.py +23 -0
  23. ScreenMagnifier/facedetector/mainwindow.py +122 -0
  24. ScreenMagnifier/facedetector/models/__init__.py +4 -0
  25. ScreenMagnifier/facepointer.py +1121 -0
  26. ScreenMagnifier/features.py +50 -0
  27. ScreenMagnifier/hotkeys/__init__.py +16 -0
  28. ScreenMagnifier/hotkeys/hotkeys.py +379 -0
  29. ScreenMagnifier/hotkeys/main.py +23 -0
  30. ScreenMagnifier/hotkeys/mainwindow.py +56 -0
  31. ScreenMagnifier/imagefilters/__init__.py +24 -0
  32. ScreenMagnifier/imagefilters/core.py +172 -0
  33. ScreenMagnifier/imagefilters/imagefilters.py +220 -0
  34. ScreenMagnifier/imagefilters/main.py +23 -0
  35. ScreenMagnifier/imagefilters/mainwindow.py +120 -0
  36. ScreenMagnifier/magnifierview/__init__.py +16 -0
  37. ScreenMagnifier/magnifierview/magnifierview.py +1249 -0
  38. ScreenMagnifier/magnifierview/main.py +23 -0
  39. ScreenMagnifier/magnifierview/mainwindow.py +91 -0
  40. ScreenMagnifier/main.py +157 -0
  41. ScreenMagnifier/mainwindow.py +1368 -0
  42. ScreenMagnifier/mousecatcher/__init__.py +16 -0
  43. ScreenMagnifier/mousecatcher/main.py +23 -0
  44. ScreenMagnifier/mousecatcher/mainwindow.py +78 -0
  45. ScreenMagnifier/mousecatcher/mousecatcher.py +251 -0
  46. ScreenMagnifier/mousemove.py +259 -0
  47. ScreenMagnifier/netprotocol/__init__.py +25 -0
  48. ScreenMagnifier/netprotocol/main.py +23 -0
  49. ScreenMagnifier/netprotocol/mainwindow.py +91 -0
  50. ScreenMagnifier/netprotocol/netprotocol.py +372 -0
  51. ScreenMagnifier/netprotocol/wire.py +189 -0
  52. ScreenMagnifier/regionselector/__init__.py +16 -0
  53. ScreenMagnifier/regionselector/main.py +23 -0
  54. ScreenMagnifier/regionselector/mainwindow.py +70 -0
  55. ScreenMagnifier/regionselector/regionselector.py +549 -0
  56. ScreenMagnifier/remoteclient.py +636 -0
  57. ScreenMagnifier/resources/icon-128.png +0 -0
  58. ScreenMagnifier/resources/icon-16.png +0 -0
  59. ScreenMagnifier/resources/icon-24.png +0 -0
  60. ScreenMagnifier/resources/icon-256.png +0 -0
  61. ScreenMagnifier/resources/icon-32.png +0 -0
  62. ScreenMagnifier/resources/icon-48.png +0 -0
  63. ScreenMagnifier/resources/icon-512.png +0 -0
  64. ScreenMagnifier/resources/icon-64.png +0 -0
  65. ScreenMagnifier/resources/icon.icns +0 -0
  66. ScreenMagnifier/resources/icon.ico +0 -0
  67. ScreenMagnifier/screengrabber/__init__.py +16 -0
  68. ScreenMagnifier/screengrabber/main.py +23 -0
  69. ScreenMagnifier/screengrabber/mainwindow.py +79 -0
  70. ScreenMagnifier/screengrabber/screengrabber.py +558 -0
  71. ScreenMagnifier/server.py +150 -0
  72. ScreenMagnifier/serverpanel.py +379 -0
  73. ScreenMagnifier/shortcuteditor/__init__.py +16 -0
  74. ScreenMagnifier/shortcuteditor/main.py +23 -0
  75. ScreenMagnifier/shortcuteditor/mainwindow.py +54 -0
  76. ScreenMagnifier/shortcuteditor/shortcuteditor.py +411 -0
  77. ScreenMagnifier/shortcuts.py +118 -0
  78. ScreenMagnifier/shortcutspanel.py +140 -0
  79. ScreenMagnifier/support.py +127 -0
  80. ScreenMagnifier/translations/ar.json +387 -0
  81. ScreenMagnifier/translations/de.json +387 -0
  82. ScreenMagnifier/translations/es.json +387 -0
  83. ScreenMagnifier/translations/fr.json +387 -0
  84. ScreenMagnifier/translations/it.json +387 -0
  85. ScreenMagnifier/translations/ja.json +387 -0
  86. ScreenMagnifier/translations/ko.json +387 -0
  87. ScreenMagnifier/translations/pt_BR.json +387 -0
  88. ScreenMagnifier/translations/ru.json +387 -0
  89. ScreenMagnifier/translations/tr.json +387 -0
  90. ScreenMagnifier/translations/zh_CN.json +387 -0
  91. ScreenMagnifier/translator/__init__.py +16 -0
  92. ScreenMagnifier/translator/main.py +23 -0
  93. ScreenMagnifier/translator/mainwindow.py +62 -0
  94. ScreenMagnifier/translator/translator.py +284 -0
  95. ScreenMagnifier/upscaler/__init__.py +24 -0
  96. ScreenMagnifier/upscaler/core.py +315 -0
  97. ScreenMagnifier/upscaler/main.py +23 -0
  98. ScreenMagnifier/upscaler/mainwindow.py +128 -0
  99. ScreenMagnifier/upscaler/models/FSRCNN-small_x2.pb +0 -0
  100. ScreenMagnifier/upscaler/models/FSRCNN-small_x3.pb +0 -0
  101. ScreenMagnifier/upscaler/models/FSRCNN-small_x4.pb +0 -0
  102. ScreenMagnifier/upscaler/models/FSRCNN_x2.pb +0 -0
  103. ScreenMagnifier/upscaler/models/FSRCNN_x3.pb +0 -0
  104. ScreenMagnifier/upscaler/models/FSRCNN_x4.pb +0 -0
  105. ScreenMagnifier/upscaler/models/LICENSE-FSRCNN +201 -0
  106. ScreenMagnifier/upscaler/models/__init__.py +4 -0
  107. ScreenMagnifier/upscaler/upscaler.py +285 -0
  108. ScreenMagnifier/wincheck.py +65 -0
  109. ScreenMagnifier/zoomoverlay/__init__.py +16 -0
  110. ScreenMagnifier/zoomoverlay/main.py +23 -0
  111. ScreenMagnifier/zoomoverlay/mainwindow.py +99 -0
  112. ScreenMagnifier/zoomoverlay/zoomoverlay.py +1779 -0
  113. screenmagnifier-1.11.0.post8.dist-info/METADATA +591 -0
  114. screenmagnifier-1.11.0.post8.dist-info/RECORD +117 -0
  115. screenmagnifier-1.11.0.post8.dist-info/WHEEL +5 -0
  116. screenmagnifier-1.11.0.post8.dist-info/entry_points.txt +5 -0
  117. screenmagnifier-1.11.0.post8.dist-info/top_level.txt +1 -0
@@ -0,0 +1,70 @@
1
+ # coding=utf-8
2
+ """
3
+ Demo window for RegionSelector.
4
+ """
5
+ from ManyQt.QtWidgets import QMainWindow, QVBoxLayout, QWidget, QLabel, QPushButton
6
+ from ManyQt.QtCore import Qt, QRect, QTimer, pyqtSlot
7
+ from os.path import dirname
8
+ from sys import path
9
+
10
+ if not __package__ and dirname(__file__) not in path:
11
+ path.append(dirname(__file__))
12
+
13
+ try:
14
+ from .regionselector import RegionSelector
15
+ except:
16
+ from regionselector import RegionSelector
17
+
18
+
19
+ class MainWindow(QMainWindow):
20
+ """
21
+ MainWindow class.
22
+ """
23
+
24
+ def __init__(self, *args, **kwargs):
25
+ """
26
+ :param args: any
27
+ :param kwargs: any
28
+ """
29
+ super(MainWindow, self).__init__(*args, **kwargs)
30
+ self.setWindowTitle(self.tr(u'RegionSelector demo'))
31
+ self.resize(520, 220)
32
+ self.__m_selector = RegionSelector(borderColor='#4F8BFF', loupeZoom=8, regionSelected=self.onSelected,
33
+ selectionCanceled=self.onCanceled) # type: RegionSelector
34
+ button = QPushButton(self.tr(u'Select a screen region...')) # type: QPushButton
35
+ button.setMinimumHeight(40)
36
+ button.clicked.connect(self.select)
37
+ self.__m_result = QLabel(self.tr(u'No region selected yet.')) # type: QLabel
38
+ self.__m_result.setAlignment(Qt.AlignCenter)
39
+ wdg = QWidget() # type: QWidget
40
+ layout = QVBoxLayout(wdg) # type: QVBoxLayout
41
+ layout.addWidget(button)
42
+ layout.addWidget(self.__m_result)
43
+ self.setCentralWidget(wdg)
44
+
45
+ @pyqtSlot()
46
+ def select(self):
47
+ """
48
+ Hide this window, give the compositor time to remove it, then freeze the desktop.
49
+ :return:
50
+ """
51
+ self.hide()
52
+ QTimer.singleShot(250, self.__m_selector.start)
53
+
54
+ @pyqtSlot(QRect)
55
+ def onSelected(self, rect):
56
+ """
57
+ :param rect: QRect
58
+ :return:
59
+ """
60
+ self.__m_result.setText(self.tr(u'Selected: x={}, y={}, {} x {}').format(
61
+ rect.x(), rect.y(), rect.width(), rect.height()))
62
+ self.show()
63
+
64
+ @pyqtSlot()
65
+ def onCanceled(self):
66
+ """
67
+ :return:
68
+ """
69
+ self.__m_result.setText(self.tr(u'Selection canceled.'))
70
+ self.show()
@@ -0,0 +1,549 @@
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ RegionSelector: full-desktop overlay to pick a screen region with the mouse.
4
+ - Freezes the whole virtual desktop (all monitors) and dims it.
5
+ - Drag to draw the selection; the selection is shown un-dimmed with corner handles and a size label.
6
+ - A pixel loupe follows the cursor for pixel-exact selection (coordinates + color under the cursor).
7
+ - Arrow keys move the cursor by 1 px (10 px with Shift). Esc / right click cancels.
8
+ - Emits regionSelected(QRect) in global, device-independent coordinates.
9
+ """
10
+ from ManyQt.QtGui import QPainter, QColor, QPen, QPainterPath, QPixmap, QFont, QFontMetrics, QCursor, QGuiApplication, \
11
+ QImage
12
+ from ManyQt.QtCore import Qt, QRect, QRectF, QPoint, QPointF, QSize, QLineF, pyqtSignal, pyqtSlot, pyqtProperty
13
+ from ManyQt.QtWidgets import QWidget
14
+
15
+
16
+ def clamp(x, a, b):
17
+ """
18
+ :param x: float | int
19
+ :param a: float | int
20
+ :param b: float | int
21
+ :return: float | int
22
+ """
23
+ return a if x < a else b if x > b else x
24
+
25
+
26
+ def virtualGeometry():
27
+ """
28
+ :return: (QRect) Union of all screen geometries.
29
+ """
30
+ rect = QRect() # type: QRect
31
+ for screen in QGuiApplication.screens():
32
+ rect = rect.united(screen.geometry()) # type: QRect
33
+ return rect
34
+
35
+
36
+ def grabDesktop():
37
+ """
38
+ Compose a screenshot of every screen into one pixmap covering the virtual desktop.
39
+ :return: (tuple[QPixmap, float, QRect]) Pixmap in physical pixels, its scale and the virtual geometry.
40
+ """
41
+ vg = virtualGeometry() # type: QRect
42
+ dpr = max([float(s.devicePixelRatio()) for s in QGuiApplication.screens()] or [1.0]) # type: float
43
+ pm = QPixmap(max(1, int(vg.width() * dpr)), max(1, int(vg.height() * dpr))) # type: QPixmap
44
+ pm.fill(Qt.black)
45
+ p = QPainter(pm) # type: QPainter
46
+ p.setRenderHint(QPainter.SmoothPixmapTransform, True)
47
+ for screen in QGuiApplication.screens():
48
+ g = screen.geometry() # type: QRect
49
+ shot = screen.grabWindow(0) # type: QPixmap
50
+ if shot.isNull():
51
+ continue
52
+ shot.setDevicePixelRatio(1.0)
53
+ p.drawPixmap(QRectF((g.x() - vg.x()) * dpr, (g.y() - vg.y()) * dpr, g.width() * dpr, g.height() * dpr),
54
+ shot, QRectF(0, 0, shot.width(), shot.height()))
55
+ p.end()
56
+ return pm, dpr, vg
57
+
58
+
59
+ class RegionSelector(QWidget):
60
+ """
61
+ RegionSelector class.
62
+ """
63
+ regionSelected = pyqtSignal(QRect) # type: pyqtSignal
64
+ selectionCanceled = pyqtSignal() # type: pyqtSignal
65
+
66
+ def __init__(self, *args, **kwargs):
67
+ """
68
+ :param dimColor: (QColor | Qt.GlobalColor | int | str) Overlay drawn outside the selection.
69
+ :param borderColor: QColor | Qt.GlobalColor | int | str
70
+ :param borderWidth: (float | int) px
71
+ :param showLoupe: bool
72
+ :param loupeZoom: (int) Loupe magnification.
73
+ :param loupeSize: (int) Loupe diameter in px.
74
+ :param minSize: (QSize) Smallest accepted selection.
75
+ :param hintText: str | unicode | QString
76
+ :param regionSelected: callable | None
77
+ :param selectionCanceled: callable | None
78
+ :param args: any
79
+ :param kwargs: any
80
+ """
81
+ dimColor = kwargs.pop('dimColor', QColor(8, 10, 18, 140)) # type: QColor
82
+ borderColor = kwargs.pop('borderColor', '#4F8BFF') # type: QColor
83
+ borderWidth = kwargs.pop('borderWidth', 2) # type: float
84
+ showLoupe = kwargs.pop('showLoupe', True) # type: bool
85
+ loupeZoom = kwargs.pop('loupeZoom', 8) # type: int
86
+ loupeSize = kwargs.pop('loupeSize', 132) # type: int
87
+ minSize = kwargs.pop('minSize', QSize(8, 8)) # type: QSize
88
+ hintText = kwargs.pop('hintText', u'Drag to select a region \u2022 Arrows: 1 px nudge \u2022 '
89
+ 'Esc / right-click: cancel') # type: str
90
+ regionSelected = kwargs.pop('regionSelected', None) # type: callable | None
91
+ selectionCanceled = kwargs.pop('selectionCanceled', None) # type: callable | None
92
+ super(RegionSelector, self).__init__(*args, **kwargs)
93
+ if callable(regionSelected):
94
+ self.regionSelected.connect(regionSelected)
95
+ if callable(selectionCanceled):
96
+ self.selectionCanceled.connect(selectionCanceled)
97
+ # Options.
98
+ self.__m_dimColor = dimColor if isinstance(dimColor, QColor) else QColor(dimColor) # type: QColor
99
+ self.__m_borderColor = borderColor if isinstance(borderColor, QColor) else QColor(borderColor) # type: QColor
100
+ self.__m_borderWidth = max(0.0, float(borderWidth)) # type: float
101
+ self.__m_showLoupe = bool(showLoupe) # type: bool
102
+ self.__m_loupeZoom = max(2, int(loupeZoom)) # type: int
103
+ self.__m_loupeSize = max(48, int(loupeSize)) # type: int
104
+ self.__m_minSize = QSize(minSize) # type: QSize
105
+ self.__m_hintText = hintText # type: str
106
+ # State.
107
+ self.__m_snapshot = QPixmap() # type: QPixmap
108
+ self.__m_snapshotImage = None # type: QImage | None
109
+ self.__m_dpr = 1.0 # type: float
110
+ self.__m_anchor = None # type: QPoint | None
111
+ self.__m_cursor = QPoint(-1, -1) # type: QPoint
112
+ self.__m_selection = QRect() # type: QRect
113
+ # Widget flags.
114
+ self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint | Qt.Tool)
115
+ self.setAttribute(Qt.WA_DeleteOnClose, False)
116
+ self.setAttribute(Qt.WA_OpaquePaintEvent, True)
117
+ self.setMouseTracking(True)
118
+ self.setCursor(Qt.CrossCursor)
119
+ self.setFocusPolicy(Qt.StrongFocus)
120
+
121
+ # ---------- Helpers ----------
122
+ def _selectionFrom(self, a, b):
123
+ """
124
+ :param a: QPoint
125
+ :param b: QPoint
126
+ :return: (QRect) Rect spanning both points (exclusive of the far edge), clipped to the overlay.
127
+ """
128
+ x0, x1 = min(a.x(), b.x()), max(a.x(), b.x()) # type: int, int
129
+ y0, y1 = min(a.y(), b.y()), max(a.y(), b.y()) # type: int, int
130
+ return QRect(x0, y0, x1 - x0, y1 - y0).intersected(self.rect())
131
+
132
+ def _pixelColor(self, pos):
133
+ """
134
+ :param pos: (QPoint) Local logical position.
135
+ :return: QColor
136
+ """
137
+ img = self.__m_snapshotImage
138
+ if img is None:
139
+ return QColor()
140
+ x = int(clamp(pos.x() * self.__m_dpr, 0, img.width() - 1)) # type: int
141
+ y = int(clamp(pos.y() * self.__m_dpr, 0, img.height() - 1)) # type: int
142
+ return QColor(img.pixel(x, y))
143
+
144
+ def _finish(self, rect):
145
+ """
146
+ :param rect: (QRect | None) Local selection, None to cancel.
147
+ :return:
148
+ """
149
+ self.releaseKeyboard()
150
+ self.hide()
151
+ self.__m_snapshot = QPixmap() # type: QPixmap
152
+ self.__m_snapshotImage = None
153
+ self.__m_anchor = None
154
+ if rect is None:
155
+ self.selectionCanceled.emit()
156
+ else:
157
+ self.regionSelected.emit(rect.translated(self.geometry().topLeft()))
158
+
159
+ # ---------- Public API ----------
160
+ @pyqtSlot()
161
+ def start(self):
162
+ """
163
+ Freeze the desktop and show the overlay. Hide your own windows before calling.
164
+ :return:
165
+ """
166
+ self.__m_snapshot, self.__m_dpr, vg = grabDesktop()
167
+ self.__m_snapshotImage = self.__m_snapshot.toImage()
168
+ self.__m_anchor = None
169
+ self.__m_selection = QRect() # type: QRect
170
+ self.setGeometry(vg)
171
+ self.__m_cursor = self.mapFromGlobal(QCursor.pos()) # type: QPoint
172
+ self.show()
173
+ self.raise_()
174
+ self.activateWindow()
175
+ self.setFocus()
176
+ self.grabKeyboard()
177
+
178
+ @pyqtSlot()
179
+ def cancel(self):
180
+ """
181
+ :return:
182
+ """
183
+ if self.isVisible():
184
+ self._finish(None)
185
+
186
+ def getSelection(self):
187
+ """
188
+ :return: (QRect) Current selection in global coordinates.
189
+ """
190
+ return self.__m_selection.translated(self.geometry().topLeft())
191
+
192
+ def getDimColor(self):
193
+ """
194
+ :return: QColor
195
+ """
196
+ return self.__m_dimColor
197
+
198
+ @pyqtSlot(QColor)
199
+ @pyqtSlot(Qt.GlobalColor)
200
+ @pyqtSlot(int)
201
+ @pyqtSlot(str)
202
+ @pyqtSlot('QString')
203
+ def setDimColor(self, dimColor):
204
+ """
205
+ :param dimColor: QColor | Qt.GlobalColor | int | str | QString
206
+ :return:
207
+ """
208
+ self.__m_dimColor = dimColor if isinstance(dimColor, QColor) else QColor(dimColor) # type: QColor
209
+ self.update()
210
+
211
+ def getBorderColor(self):
212
+ """
213
+ :return: QColor
214
+ """
215
+ return self.__m_borderColor
216
+
217
+ @pyqtSlot(QColor)
218
+ @pyqtSlot(Qt.GlobalColor)
219
+ @pyqtSlot(int)
220
+ @pyqtSlot(str)
221
+ @pyqtSlot('QString')
222
+ def setBorderColor(self, borderColor):
223
+ """
224
+ :param borderColor: QColor | Qt.GlobalColor | int | str | QString
225
+ :return:
226
+ """
227
+ self.__m_borderColor = borderColor if isinstance(borderColor, QColor) else QColor(borderColor) # type: QColor
228
+ self.update()
229
+
230
+ def getBorderWidth(self):
231
+ """
232
+ :return: float | int
233
+ """
234
+ return self.__m_borderWidth
235
+
236
+ @pyqtSlot(float)
237
+ @pyqtSlot(int)
238
+ def setBorderWidth(self, borderWidth):
239
+ """
240
+ :param borderWidth: float | int
241
+ :return:
242
+ """
243
+ self.__m_borderWidth = max(0.0, float(borderWidth)) # type: float
244
+ self.update()
245
+
246
+ def isShowLoupe(self):
247
+ """
248
+ :return: bool
249
+ """
250
+ return self.__m_showLoupe
251
+
252
+ @pyqtSlot(bool)
253
+ @pyqtSlot(int)
254
+ def setShowLoupe(self, showLoupe):
255
+ """
256
+ :param showLoupe: bool
257
+ :return:
258
+ """
259
+ self.__m_showLoupe = bool(showLoupe) # type: bool
260
+ self.update()
261
+
262
+ def getLoupeZoom(self):
263
+ """
264
+ :return: int
265
+ """
266
+ return self.__m_loupeZoom
267
+
268
+ @pyqtSlot(int)
269
+ def setLoupeZoom(self, loupeZoom):
270
+ """
271
+ :param loupeZoom: int
272
+ :return:
273
+ """
274
+ self.__m_loupeZoom = max(2, int(loupeZoom)) # type: int
275
+ self.update()
276
+
277
+ def getLoupeSize(self):
278
+ """
279
+ :return: int
280
+ """
281
+ return self.__m_loupeSize
282
+
283
+ @pyqtSlot(int)
284
+ def setLoupeSize(self, loupeSize):
285
+ """
286
+ :param loupeSize: int
287
+ :return:
288
+ """
289
+ self.__m_loupeSize = max(48, int(loupeSize)) # type: int
290
+ self.update()
291
+
292
+ def getMinSize(self):
293
+ """
294
+ :return: QSize
295
+ """
296
+ return QSize(self.__m_minSize)
297
+
298
+ @pyqtSlot(QSize)
299
+ def setMinSize(self, minSize):
300
+ """
301
+ :param minSize: QSize
302
+ :return:
303
+ """
304
+ self.__m_minSize = QSize(minSize) # type: QSize
305
+
306
+ def getHintText(self):
307
+ """
308
+ :return: str | unicode | QString
309
+ """
310
+ return self.__m_hintText
311
+
312
+ @pyqtSlot('QString')
313
+ @pyqtSlot(str)
314
+ def setHintText(self, hintText):
315
+ """
316
+ :param hintText: str | unicode | QString
317
+ :return:
318
+ """
319
+ self.__m_hintText = hintText # type: str
320
+ self.update()
321
+
322
+ # ---------- Qt overrides ----------
323
+ def mousePressEvent(self, e):
324
+ """
325
+ :param e: QMouseEvent
326
+ :return:
327
+ """
328
+ if e.button() == Qt.RightButton:
329
+ self._finish(None)
330
+ return
331
+ if e.button() == Qt.LeftButton:
332
+ self.__m_anchor = e.pos() # type: QPoint
333
+ self.__m_selection = QRect() # type: QRect
334
+ self.update()
335
+ super(RegionSelector, self).mousePressEvent(e)
336
+
337
+ def mouseMoveEvent(self, e):
338
+ """
339
+ :param e: QMouseEvent
340
+ :return:
341
+ """
342
+ self.__m_cursor = e.pos() # type: QPoint
343
+ if self.__m_anchor is not None:
344
+ self.__m_selection = self._selectionFrom(self.__m_anchor, e.pos()) # type: QRect
345
+ self.update()
346
+ super(RegionSelector, self).mouseMoveEvent(e)
347
+
348
+ def mouseReleaseEvent(self, e):
349
+ """
350
+ :param e: QMouseEvent
351
+ :return: None
352
+ """
353
+ if e.button() == Qt.LeftButton and self.__m_anchor is not None:
354
+ sel = self._selectionFrom(self.__m_anchor, e.pos()) # type: QRect
355
+ self.__m_anchor = None
356
+ if sel.width() >= self.__m_minSize.width() and sel.height() >= self.__m_minSize.height():
357
+ self._finish(sel)
358
+ return
359
+ self.__m_selection = QRect() # type: QRect
360
+ self.update()
361
+ super(RegionSelector, self).mouseReleaseEvent(e)
362
+
363
+ def keyPressEvent(self, e):
364
+ """
365
+ :param e: QKeyEvent
366
+ :return:
367
+ """
368
+ if e.key() == Qt.Key_Escape:
369
+ self._finish(None)
370
+ return
371
+ steps = {Qt.Key_Left: (-1, 0), Qt.Key_Right: (1, 0), Qt.Key_Up: (0, -1), Qt.Key_Down: (0, 1)}
372
+ if e.key() in steps:
373
+ dx, dy = steps[e.key()]
374
+ k = 10 if e.modifiers() & Qt.ShiftModifier else 1 # type: int
375
+ QCursor.setPos(QCursor.pos() + QPoint(dx * k, dy * k))
376
+ return
377
+ super(RegionSelector, self).keyPressEvent(e)
378
+
379
+ def leaveEvent(self, e):
380
+ """
381
+ :param e: QEvent
382
+ :return:
383
+ """
384
+ self.__m_cursor = QPoint(-1, -1) # type: QPoint
385
+ self.update()
386
+ super(RegionSelector, self).leaveEvent(e)
387
+
388
+ # ---------- Painting ----------
389
+ def paintEvent(self, event):
390
+ """
391
+ :param event: QPaintEvent
392
+ :return:
393
+ """
394
+ p = QPainter(self) # type: QPainter
395
+ p.setRenderHints(QPainter.Antialiasing | QPainter.TextAntialiasing, True)
396
+ r = QRectF(self.rect()) # type: QRectF
397
+ if self.__m_snapshot.isNull():
398
+ p.fillRect(r, Qt.black)
399
+ else:
400
+ p.drawPixmap(r, self.__m_snapshot, QRectF(0, 0, self.__m_snapshot.width(), self.__m_snapshot.height()))
401
+ sel = QRectF(self.__m_selection) # type: QRectF
402
+ dim = QPainterPath() # type: QPainterPath
403
+ dim.addRect(r)
404
+ if not sel.isEmpty():
405
+ hole = QPainterPath() # type: QPainterPath
406
+ hole.addRect(sel)
407
+ dim = dim.subtracted(hole) # type: QPainterPath
408
+ p.fillPath(dim, self.__m_dimColor)
409
+ if not sel.isEmpty():
410
+ self._paintSelection(p, sel)
411
+ self._paintHint(p)
412
+ if self.__m_showLoupe and self.rect().contains(self.__m_cursor):
413
+ self._paintLoupe(p)
414
+ p.end()
415
+
416
+ def _paintSelection(self, p, sel):
417
+ """
418
+ :param p: QPainter
419
+ :param sel: QRectF
420
+ :return:
421
+ """
422
+ pen = QPen(self.__m_borderColor, self.__m_borderWidth) # type: QPen
423
+ pen.setJoinStyle(Qt.MiterJoin)
424
+ p.setPen(pen)
425
+ p.setBrush(Qt.NoBrush)
426
+ hw = self.__m_borderWidth / 2.0 # type: float
427
+ p.drawRect(sel.adjusted(-hw, -hw, hw, hw))
428
+ p.setPen(QPen(Qt.white, 1.5))
429
+ p.setBrush(self.__m_borderColor)
430
+ for c in (sel.topLeft(), sel.topRight(), sel.bottomLeft(), sel.bottomRight()):
431
+ p.drawEllipse(c, 4.5, 4.5)
432
+ # Size label.
433
+ font = QFont(self.font()) # type: QFont
434
+ font.setPointSizeF(9.5)
435
+ font.setBold(True)
436
+ p.setFont(font)
437
+ dpr = self.__m_dpr # type: float
438
+ text = '{} \u00d7 {}'.format(int(sel.width()), int(sel.height())) # type: str
439
+ if dpr != 1.0:
440
+ text += ' ({} \u00d7 {} px)'.format(int(sel.width() * dpr), int(sel.height() * dpr))
441
+ fm = QFontMetrics(font) # type: QFontMetrics
442
+ tw = fm.horizontalAdvance(text) if hasattr(fm, 'horizontalAdvance') else fm.width(text) # type: int
443
+ box = QRectF(sel.left(), sel.top() - fm.height() - 12, tw + 16, fm.height() + 6) # type: QRectF
444
+ if box.top() < 2:
445
+ box.moveTop(sel.top() + 6)
446
+ box.moveLeft(sel.left() + 6)
447
+ p.setPen(Qt.NoPen)
448
+ p.setBrush(self.__m_borderColor)
449
+ p.drawRoundedRect(box, 5, 5)
450
+ p.setPen(Qt.white)
451
+ p.drawText(box, Qt.AlignCenter, text)
452
+
453
+ def _paintHint(self, p):
454
+ """
455
+ :param p: QPainter
456
+ :return: None
457
+ """
458
+ if not self.__m_hintText:
459
+ return
460
+ screen = QGuiApplication.screenAt(self.mapToGlobal(self.__m_cursor)) if hasattr(
461
+ QGuiApplication, 'screenAt') else None
462
+ screen = screen or QGuiApplication.primaryScreen()
463
+ sg = screen.geometry().translated(-self.geometry().topLeft()) # type: QRect
464
+ font = QFont(self.font()) # type: QFont
465
+ font.setPointSizeF(10.5)
466
+ p.setFont(font)
467
+ fm = QFontMetrics(font) # type: QFontMetrics
468
+ tw = fm.horizontalAdvance(self.__m_hintText) if hasattr(fm, 'horizontalAdvance') else fm.width(
469
+ self.__m_hintText) # type: int
470
+ box = QRectF(sg.center().x() - (tw + 32) / 2.0, sg.top() + 24, tw + 32, fm.height() + 16) # type: QRectF
471
+ if box.intersects(QRectF(self.__m_selection)) or box.contains(QPointF(self.__m_cursor)):
472
+ box.moveBottom(sg.bottom() - 24)
473
+ p.setPen(Qt.NoPen)
474
+ p.setBrush(QColor(20, 22, 30, 215))
475
+ p.drawRoundedRect(box, box.height() / 2.0, box.height() / 2.0)
476
+ p.setPen(QColor(235, 238, 245))
477
+ p.drawText(box, Qt.AlignCenter, self.__m_hintText)
478
+
479
+ def _paintLoupe(self, p):
480
+ """
481
+ :param p: QPainter
482
+ :return:
483
+ """
484
+ size = float(self.__m_loupeSize) # type: float
485
+ zoom = self.__m_loupeZoom # type: int
486
+ cells = int(size / zoom) | 1 # type: int # odd -> a true centre pixel
487
+ size = float(cells * zoom) # type: float
488
+ c = self.__m_cursor # type: QPoint
489
+ dpr = self.__m_dpr # type: float
490
+ # Place the loupe bottom-right of the cursor, flip near the edges.
491
+ x = c.x() + 24.0 # type: float
492
+ y = c.y() + 24.0 # type: float
493
+ if x + size > self.width() - 4:
494
+ x = c.x() - 24.0 - size
495
+ if y + size + 40 > self.height() - 4:
496
+ y = c.y() - 24.0 - size - 40
497
+ target = QRectF(x, y, size, size) # type: QRectF
498
+ half = cells // 2 # type: int
499
+ px, py = int(c.x() * dpr), int(c.y() * dpr) # type: int, int
500
+ src = QRectF(px - half, py - half, cells, cells) # type: QRectF
501
+ clip = QPainterPath() # type: QPainterPath
502
+ clip.addRoundedRect(target, 10, 10)
503
+ p.save()
504
+ p.setClipPath(clip)
505
+ p.fillRect(target, Qt.black)
506
+ p.setRenderHint(QPainter.SmoothPixmapTransform, False)
507
+ p.drawPixmap(target, self.__m_snapshot, src)
508
+ grid = QPen(QColor(0, 0, 0, 50), 1) # type: QPen
509
+ p.setPen(grid)
510
+ lines = [] # type: list[QLineF]
511
+ for i in range(1, cells):
512
+ lines.append(QLineF(x + i * zoom, y, x + i * zoom, y + size))
513
+ lines.append(QLineF(x, y + i * zoom, x + size, y + i * zoom))
514
+ p.drawLines(lines)
515
+ centre = QRectF(x + half * zoom, y + half * zoom, zoom, zoom) # type: QRectF
516
+ p.setPen(QPen(Qt.white, 1))
517
+ p.drawRect(centre.adjusted(-1, -1, 1, 1))
518
+ p.setPen(QPen(Qt.black, 1))
519
+ p.drawRect(centre)
520
+ p.restore()
521
+ p.setPen(QPen(self.__m_borderColor, 2))
522
+ p.setBrush(Qt.NoBrush)
523
+ p.drawRoundedRect(target, 10, 10)
524
+ # Info box under the loupe.
525
+ col = self._pixelColor(c) # type: QColor
526
+ g = self.mapToGlobal(c) # type: QPoint
527
+ info = QRectF(x, y + size + 6, size, 34) # type: QRectF
528
+ p.setPen(Qt.NoPen)
529
+ p.setBrush(QColor(20, 22, 30, 225))
530
+ p.drawRoundedRect(info, 7, 7)
531
+ font = QFont(self.font()) # type: QFont
532
+ font.setPointSizeF(8.5)
533
+ p.setFont(font)
534
+ p.setBrush(col)
535
+ p.setPen(QPen(QColor(255, 255, 255, 160), 1))
536
+ p.drawRoundedRect(QRectF(info.left() + 7, info.top() + 9, 16, 16), 3, 3)
537
+ p.setPen(QColor(235, 238, 245))
538
+ p.drawText(info.adjusted(30, 2, -4, -2), Qt.AlignVCenter | Qt.AlignLeft,
539
+ '{}, {}\n{}'.format(g.x(), g.y(), col.name().upper()))
540
+
541
+ # ---------- Properties ----------
542
+ dimColor = pyqtProperty(QColor, fget=getDimColor, fset=setDimColor) # type: pyqtProperty
543
+ borderColor = pyqtProperty(QColor, fget=getBorderColor, fset=setBorderColor) # type: pyqtProperty
544
+ borderWidth = pyqtProperty(float, fget=getBorderWidth, fset=setBorderWidth) # type: pyqtProperty
545
+ showLoupe = pyqtProperty(bool, fget=isShowLoupe, fset=setShowLoupe) # type: pyqtProperty
546
+ loupeZoom = pyqtProperty(int, fget=getLoupeZoom, fset=setLoupeZoom) # type: pyqtProperty
547
+ loupeSize = pyqtProperty(int, fget=getLoupeSize, fset=setLoupeSize) # type: pyqtProperty
548
+ minSize = pyqtProperty(QSize, fget=getMinSize, fset=setMinSize) # type: pyqtProperty
549
+ hintText = pyqtProperty('QString', fget=getHintText, fset=setHintText) # type: pyqtProperty