-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmisc.py
326 lines (252 loc) · 11.4 KB
/
misc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# !/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Oct 31 16:23:13 2018
@author: samschott
"""
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import Qt, QRect
from PyQt5.QtGui import QBrush, QImage, QPainter, QPixmap, QWindow
_USER_DIALOG_ICON_SIZE = 70
def elide_string(string, font=None, pixels=200, side="right"):
"""
Elides a string to fit into the given width.
:param str string: String to elide.
:param font: Font to calculate size. If not given, the current style's default font
for a QLabel is used.
:param int pixels: Maximum width in pixels.
:param str side: Side to truncate. Can be "right" or "left", defaults to "right".
:return: Truncated string.
:rtype: str
"""
if not font:
font = QtWidgets.QLabel().font()
metrics = QtGui.QFontMetrics(font)
mode = Qt.ElideRight if side is "right" else Qt.ElideLeft
return metrics.elidedText(string, mode, pixels)
def get_scaled_font(scaling=1.0, bold=False, italic=False):
"""
Returns the current style's default font for a QLabel but scaled by the given factor.
:param float scaling: Scaling factor.
:param bool bold: Sets the returned font to bold (defaults to ``False``)
:param bool italic: Sets the returned font to italic (defaults to ``False``)
:return: `QFont`` instance.
"""
label = QtWidgets.QLabel()
font = label.font()
font.setBold(bold)
font.setItalic(italic)
font_size = round(font.pointSize()*scaling)
# noinspection PyTypeChecker
font.setPointSize(font_size)
return font
def icon_to_pixmap(icon, width, height=None):
"""Converts a given icon to a pixmap. Automatically adjusts to high-DPI scaling.
:param icon: Icon to convert.
:param int width: Target point height.
:param int height: Target point height.
:return: ``QPixmap`` instance.
"""
if not height:
height = width
is_hidpi = QtCore.QCoreApplication.testAttribute(Qt.AA_UseHighDpiPixmaps)
pr = QWindow().devicePixelRatio()
if not is_hidpi:
width = width*pr
height = height*pr
px = icon.pixmap(width, height)
if not is_hidpi:
px.setDevicePixelRatio(pr)
return px
class Worker(QtCore.QObject):
"""A worker object. To be used in QThreads."""
sig_done = QtCore.pyqtSignal(object)
def __init__(self, target=None, args=None, kwargs=None):
QtCore.QObject.__init__(self)
self._target = target
self._args = args or ()
self._kwargs = kwargs or {}
def start(self):
res = self._target(*self._args, **self._kwargs)
self.sig_done.emit(res)
class BackgroundTask(QtCore.QObject):
"""A utility class to manage a worker thread."""
sig_done = QtCore.pyqtSignal(object)
def __init__(self, parent=None, target=None, args=None, kwargs=None, autostart=True):
QtCore.QObject.__init__(self, parent)
self._target = target
self._args = args or ()
self._kwargs = kwargs or {}
if autostart:
self.start()
def start(self):
self.thread = QtCore.QThread(self)
self.worker = Worker(target=self._target, args=self._args, kwargs=self._kwargs)
self.worker.sig_done.connect(self.sig_done.emit)
self.worker.sig_done.connect(self.thread.quit)
self.worker.moveToThread(self.thread)
self.thread.started.connect(self.worker.start)
self.thread.start()
def wait(self, timeout=None):
if timeout:
self.thread.wait(msecs=timeout)
else:
self.thread.wait()
class BackgroundTaskProgressDialog(QtWidgets.QDialog):
"""A progress dialog to show during long-running background tasks."""
def __init__(self, icon, title, message="", cancel=True, parent=None, width=450, icon_size=_USER_DIALOG_ICON_SIZE):
super(self.__class__, self).__init__(parent=parent)
self.setModal(True)
self.setWindowModality(Qt.WindowModal)
self.setWindowFlags(Qt.WindowStaysOnTopHint | Qt.Sheet | Qt.WindowTitleHint | Qt.CustomizeWindowHint)
self.setAttribute(Qt.WA_DeleteOnClose)
self.setWindowTitle("")
self.setFixedWidth(width)
self.gridLayout = QtWidgets.QGridLayout()
self.setLayout(self.gridLayout)
self.iconLabel = QtWidgets.QLabel(self)
self.titleLabel = QtWidgets.QLabel(self)
self.infoLabel = QtWidgets.QLabel(self)
self.progressBar = QtWidgets.QProgressBar()
self.buttonBox = QtWidgets.QDialogButtonBox(QtWidgets.QDialogButtonBox.Cancel)
self.iconLabel.setMinimumSize(icon_size, icon_size)
self.iconLabel.setMaximumSize(icon_size, icon_size)
self.iconLabel.setAlignment(Qt.AlignTop)
self.titleLabel.setFont(get_scaled_font(bold=True))
self.infoLabel.setFont(get_scaled_font(scaling=0.9))
self.infoLabel.setFixedWidth(width-150)
self.infoLabel.setSizePolicy(QtWidgets.QSizePolicy.MinimumExpanding, QtWidgets.QSizePolicy.MinimumExpanding)
self.infoLabel.setWordWrap(True)
self.infoLabel.setOpenExternalLinks(True)
self.iconLabel.setPixmap(icon_to_pixmap(icon, icon_size))
self.titleLabel.setText(title)
self.infoLabel.setText(message)
self.buttonBox.rejected.connect(self.reject)
self.progressBar.setMinimum(0)
self.progressBar.setMaximum(0)
self.gridLayout.addWidget(self.iconLabel, 0, 0, 3, 1)
self.gridLayout.addWidget(self.titleLabel, 0, 1, 1, 1)
if message:
self.gridLayout.addWidget(self.infoLabel, 1, 1, 1, 1)
self.gridLayout.addWidget(self.progressBar, 2, 1, 1, 1)
else:
self.gridLayout.addWidget(self.progressBar, 1, 1, 1, 1)
if message and cancel:
self.gridLayout.addWidget(self.buttonBox, 3, 1, -1, -1)
elif cancel:
self.gridLayout.addWidget(self.buttonBox, 2, 1, -1, -1)
self.adjustSize()
class UserDialog(QtWidgets.QDialog):
"""A template user dialog. Shows a traceback if given in constructor."""
def __init__(self, icon, title, message, details=None, parent=None, icon_size=_USER_DIALOG_ICON_SIZE):
super(self.__class__, self).__init__(parent=parent)
self.setModal(True)
self.setWindowModality(Qt.WindowModal)
self.setWindowFlags(Qt.WindowStaysOnTopHint | Qt.Sheet | Qt.WindowTitleHint |
Qt.CustomizeWindowHint)
self.setAttribute(Qt.WA_DeleteOnClose)
self.setWindowTitle("")
width = 550 if details else 450
self.setFixedWidth(width)
self.gridLayout = QtWidgets.QGridLayout()
self.setLayout(self.gridLayout)
self.iconLabel = QtWidgets.QLabel(self)
self.titleLabel = QtWidgets.QLabel(self)
self.infoLabel = QtWidgets.QLabel(self)
self.iconLabel.setMinimumSize(icon_size, icon_size)
self.iconLabel.setMaximumSize(icon_size, icon_size)
self.titleLabel.setFont(get_scaled_font(bold=True))
self.infoLabel.setFont(get_scaled_font(scaling=0.9))
self.infoLabel.setFixedWidth(width-150)
self.infoLabel.setSizePolicy(QtWidgets.QSizePolicy.MinimumExpanding,
QtWidgets.QSizePolicy.MinimumExpanding)
self.infoLabel.setWordWrap(True)
self.infoLabel.setOpenExternalLinks(True)
self.iconLabel.setPixmap(icon_to_pixmap(icon, icon_size))
self.titleLabel.setText(title)
self.infoLabel.setText(message)
if details:
self.details = QtWidgets.QTextBrowser(self)
self.details.setText("".join(details))
self.details.setOpenExternalLinks(True)
self.buttonBox = QtWidgets.QDialogButtonBox(QtWidgets.QDialogButtonBox.Ok)
self.buttonBox.accepted.connect(self.accept)
self.gridLayout.addWidget(self.iconLabel, 0, 0, 2, 1)
self.gridLayout.addWidget(self.titleLabel, 0, 1, 1, 1)
self.gridLayout.addWidget(self.infoLabel, 1, 1, 1, 1)
if details:
self.gridLayout.addWidget(self.details, 2, 1, 1, 1)
self.gridLayout.addWidget(self.buttonBox, 3, 1, -1, -1)
self.adjustSize()
def setAcceptButtonName(self, name):
self.buttonBox.buttons()[0].setText(name)
def addCancelButton(self, name="Cancel"):
self._cancelButton = self.buttonBox.addButton(QtWidgets.QDialogButtonBox.Cancel)
self._cancelButton.setText(name)
self._cancelButton.clicked.connect(self.close)
def setCancelButtonName(self, name):
self._cancelButton.setText(name)
def addSecondAcceptButton(self, name, icon="dialog-ok"):
self._acceptButton2 = self.buttonBox.addButton(QtWidgets.QDialogButtonBox.Ignore)
self._acceptButton2.setText(name)
if isinstance(icon, QtGui.QIcon):
self._acceptButton2.setIcon(icon)
elif isinstance(icon, str):
self._acceptButton2.setIcon(QtGui.QIcon.fromTheme(icon))
self._acceptButton2.clicked.connect(lambda: self.setResult(2))
self._acceptButton2.clicked.connect(self.close)
def setSecondAcceptButtonName(self, name):
self._acceptButton2.setText(name)
def get_masked_image(path, size=64, overlay_text=""):
"""
Returns a ``QPixmap`` from an image file masked with a smooth circle.
The returned pixmap will have a size of *size* × *size* pixels.
:param str path: Path to image file.
:param int size: Target size. Will be the diameter of the masked image.
:param overlay_text: Overlay text. This will be shown in white sans-serif on top of
the image.
:return: `QPixmap`` instance.
"""
with open(path, "rb") as f:
imgdata = f.read()
imgtype = path.split(".")[-1]
# Load image and convert to 32-bit ARGB (adds an alpha channel):
image = QImage.fromData(imgdata, imgtype)
image.convertToFormat(QImage.Format_ARGB32)
# Crop image to a square:
imgsize = min(image.width(), image.height())
rect = QRect(
(image.width() - imgsize) / 2,
(image.height() - imgsize) / 2,
imgsize,
imgsize,
)
image = image.copy(rect)
# Create the output image with the same dimensions and an alpha channel
# and make it completely transparent:
out_img = QImage(imgsize, imgsize, QImage.Format_ARGB32)
out_img.fill(Qt.transparent)
# Create a texture brush and paint a circle with the original image onto
# the output image:
brush = QBrush(image) # Create texture brush
painter = QPainter(out_img) # Paint the output image
painter.setBrush(brush) # Use the image texture brush
painter.setPen(Qt.NoPen) # Don't draw an outline
painter.setRenderHint(QPainter.Antialiasing, True) # Use AA
painter.drawEllipse(0, 0, imgsize, imgsize) # Actually draw the circle
if overlay_text:
# draw text
font = QtGui.QFont("Arial Rounded MT Bold")
font.setPointSize(imgsize * 0.4)
painter.setFont(font)
painter.setPen(Qt.white)
painter.drawText(QRect(0, 0, imgsize, imgsize), Qt.AlignCenter, overlay_text)
painter.end() # We are done (segfault if you forget this)
# Convert the image to a pixmap and rescale it. Take pixel ratio into
# account to get a sharp image on retina displays:
pr = QWindow().devicePixelRatio()
pm = QPixmap.fromImage(out_img)
pm.setDevicePixelRatio(pr)
size *= pr
pm = pm.scaled(size, size, Qt.KeepAspectRatio, Qt.SmoothTransformation)
return pm