forked from futscdav/Chunkmogrify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qtutil.py
256 lines (219 loc) · 7.5 KB
/
qtutil.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
#
# Author: David Futschik
# Provided as part of the Chunkmogrify project, 2021.
#
import os
import re
import numpy as np
import PIL.Image as Image
from enum import Enum
from threading import Thread
from threading import Timer
from time import time, perf_counter
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
class dotdict(dict):
__getattr__ = dict.__getitem__
__setattr__ = dict.__setitem__
__delattr__ = dict.__delitem__
def has_set(self, attr):
return attr in self and self[attr] is not None
class MouseButton(Enum):
LEFT = 1
RIGHT = 2
MIDDLE = 4
SIDE_1 = 8
SIDE_2 = 16
def QHLine():
w = QFrame()
w.setFrameShape(QFrame.HLine)
w.setFrameShadow(QFrame.Sunken)
return w
def QVLine():
w = QFrame()
w.setFrameShape(QFrame.VLine)
w.setFrameShadow(QFrame.Raised)
return w
def qpixmap_loader(path: str):
if not os.path.exists(path):
print(f"Warning: {path} does not exist, but queued for load as pixmap")
else:
print(f"Loading {path}")
return QPixmap(path)
def npy_loader(path: str):
if not os.path.exists(path):
raise RuntimeError(f"{path} does not exist")
return np.array(Image.open(path))
def make_dirs_if_not_exists(path):
if not os.path.exists(path) or not os.path.isdir(path):
os.makedirs(path)
def export_image(prefix, npy_image):
if not os.path.exists(prefix):
os.mkdir(prefix)
ls = os.listdir(prefix)
prev_imgs = [re.match(r'^\d+', x) for x in ls]
prev_imgs = [int(x.group()) for x in prev_imgs if x is not None]
cur_id = max(prev_imgs, default=-1) + 1
fname = os.path.join(prefix, f'{cur_id:05d}.png')
Image.fromarray(npy_image).save(fname)
return fname
def image_save(npy_image, path):
Image.fromarray(npy_image).save(path)
def qim2np(qimage, swapaxes=True):
# assumes BGRA if swapaxes is True
# assert(qimage.format() == QImage.Format_8888)
bytes_per_pxl = 4
if qimage.format() == QImage.Format_Grayscale8:
bytes_per_pxl = 1
qimage.__array_interface__ = {
'shape': (qimage.height(), qimage.width(), bytes_per_pxl),
'typestr': "|u1",
'data': (int(qimage.bits()), False),
'version': 3
}
npim = np.asarray(qimage)
# npim is now BGRA, cast it into RGBA
if bytes_per_pxl == 4 and swapaxes:
npim = npim[...,(2,1,0,3)]
return npim
def np2qim(nparr, fmt_select='auto', do_copy=True):
h, w = nparr.shape[0:2]
# the copy is required unless reference is desired!
if do_copy:
nparr = nparr.astype('uint8').copy()
else:
nparr = np.ascontiguousarray(nparr.astype('uint8'))
if nparr.ndim == 2:
# or Alpha8 when it has no 3rd dimension (masks)
fmt = QImage.Format_Grayscale8 #Alpha8
elif fmt_select != 'auto':
fmt = fmt_select
else:
fmt = {3: QImage.Format_RGB888, 4: QImage.Format_RGBA8888}[nparr.shape[2]]
qim = QImage(nparr, w, h, nparr.strides[0], fmt) # _Premultiplied
return qim
def destroy_layout(layout):
if layout is not None:
while layout.count():
item = layout.takeAt(0)
widget = item.widget()
if widget is not None:
widget.deleteLater()
else:
destroy_layout(item.layout())
class NotifyWait(QObject):
acquire = pyqtSignal(str)
release = pyqtSignal()
def __init__(self, parent=None) -> None:
super().__init__(parent=parent)
self.msg = ""
d = QProgressDialog(self.msg, "Cancel", 0, 0, parent=get_default_parent())
d.setCancelButton(None)
d.setWindowTitle("Working")
d.setWindowFlags(d.windowFlags() & ~Qt.WindowCloseButtonHint)
self.d = d
self.d.cancel()
def _show(self):
self.d.setLabelText(self.msg)
self.d.exec_()
def _hide(self):
self.d.done(0)
class RaiseError(QObject):
raiseme = pyqtSignal(str, str, bool) # is_fatal
def __init__(self, parent=None) -> None:
super().__init__(parent=parent)
def _show(self, where, what, is_fatal):
msg = QMessageBox(self.parent())
msg.setText(where)
msg.setInformativeText(what)
msg.setWindowTitle("Error")
msg.setIcon(QMessageBox.Critical)
msgresizer = QSpacerItem(500, 0, QSizePolicy.Minimum, QSizePolicy.Expanding)
msg.layout().addItem(msgresizer, msg.layout().rowCount(), 0, 1, msg.layout().columnCount())
msg.exec_()
if is_fatal:
exit(1)
_global_parent = None
_notify_wait = None
_global_error = None
def set_default_parent(parent):
global _global_parent
global _notify_wait
global _global_error
_global_parent = parent
_notify_wait = NotifyWait(_global_parent)
_global_error = RaiseError(_global_parent)
def s(msg):
_notify_wait.msg = msg
_notify_wait._show()
def e():
_notify_wait._hide()
_notify_wait.acquire.connect(s)
_notify_wait.release.connect(e)
def e(where, what, is_fatal):
_global_error._show(where, what, is_fatal)
_global_error.raiseme.connect(e)
def get_default_parent():
global _global_parent
return _global_parent
def get_notify_wait():
global _notify_wait
return _notify_wait
def get_global_error():
global _global_error
return _global_error
def notify_user_wait(message):
d = QProgressDialog(message, "Cancel", 0, 0, parent=get_default_parent())
d.setCancelButton(None)
d.setWindowTitle("Working")
d.setWindowFlags(d.windowFlags() & ~Qt.WindowCloseButtonHint)
return d
def notify_user_error(where, what, parent=None):
msg = QMessageBox(parent)
msg.setText(where)
msg.setInformativeText(what)
msg.setWindowTitle("Error")
msg.setIcon(QMessageBox.Critical)
msgresizer = QSpacerItem(500, 0, QSizePolicy.Minimum, QSizePolicy.Expanding)
msg.layout().addItem(msgresizer, msg.layout().rowCount(), 0, 1, msg.layout().columnCount())
msg.exec_()
# Really only works when called from main thread.
def execute_with_wait(message, fn, *args, **kwargs):
d = notify_user_wait(message)
def fn_impl():
fn(*args, **kwargs)
d.done(0)
t = Thread(target=fn_impl)
t.start()
d.exec_()
class NowOrDelayTimer:
def __init__(self, interval):
self.interval = interval
self.last_ok_time = 0
self.timer = None
def update(self, do_run):
if self.last_ok_time < time() - self.interval:
self.last_ok_time = time()
do_run()
# cancel pending update
if self.timer: self.timer.cancel()
else:
# cancel last pending update
if self.timer: self.timer.cancel()
def closure():
self.update(do_run)
self.timer = Timer(self.last_ok_time + self.interval - time(), closure)
self.timer.start()
class MeasureTime:
def __init__(self, name, disable=False):
self.name = name
self.start = None
self.disable = disable
def __enter__(self):
if not self.disable:
self.start = perf_counter()
def __exit__(self, type, value, traceback):
if not self.disable:
end = perf_counter()
print(f'{self.name}: {end-self.start:.3f}')