-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgui.py
More file actions
388 lines (336 loc) · 9.33 KB
/
Copy pathgui.py
File metadata and controls
388 lines (336 loc) · 9.33 KB
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#!/usr/bin/env python3
#
''' Some easy to assemble simple GUI widgets, currently based on PyQt5.
'''
from collections import defaultdict, namedtuple
from PIL import Image as PILImage
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QColor, QPixmap
from PyQt5.QtWidgets import (
QApplication, QMainWindow,
QWidget, QTabWidget, QLabel, QGroupBox,
QHBoxLayout, QVBoxLayout, QGridLayout, QScrollArea,
)
from cs.imageutils import ThumbnailCache
from cs.pfx import Pfx
from cs.x import X
class App(object):
''' The GUI application main instance (not the main GUI window).
'''
def __init__(self, argv):
self.app = QApplication(argv)
def start(self):
''' Start the GUI application main loop.
'''
return self.app.exec_()
Position = namedtuple('Position', 'left top')
Size = namedtuple('Size', 'width height')
_RGBColor = namedtuple('RGBColor', 'red green blue')
class RGBColor(_RGBColor):
''' A colour in RGB space.
'''
@property
def htmlcolor(self):
''' Transcription of the colour for HTML.
'''
return '#%02x%02x%02x' % (self.red, self.green, self.blue)
RED = RGBColor(255, 0, 0)
GREEN = RGBColor(0, 255, 0)
BLUE = RGBColor(0, 0, 255)
WHITE = RGBColor(255, 255, 255)
BLACK = RGBColor(0, 0, 0)
DEFAULT_FGCOLOR = GREEN
DEFAULT_BGCOLOR = RGBColor(128, 128, 128) # was BLACK
class _Element(object):
''' The base class for all widgets.
'''
def __init__(
self, widget,
title=None,
bgcolor=None, fgcolor=None,
size=None,
):
if widget is None:
widget = QWidget()
if title is None:
title = type(self).__name__
if bgcolor is None:
bgcolor = DEFAULT_BGCOLOR
if fgcolor is None:
fgcolor = DEFAULT_FGCOLOR
self.widget = widget
self.title = title
self.bgcolor = bgcolor
self.fgcolor = fgcolor
if size is not None:
self.size = size
def show(self):
''' Show the widget.
'''
self.widget.show()
@property
def title(self):
''' The widget title.
'''
return self._title
@title.setter
def title(self, new_title):
''' Set the widget title.
'''
assert isinstance(new_title, str)
self._title = new_title
self.widget.setWindowTitle(new_title)
@property
def position(self):
''' The widget position.
'''
geom = self.widget.geometry()
return Position(geom.x(), geom.y())
@position.setter
def position(self, new_position):
''' Set the widget position.
'''
new_position = Position(*new_position)
size = self.size
self.widget.setGeometry(
new_position.left,
new_position.top,
size.width,
size.height)
@property
def size(self):
''' The widget size.
'''
w = self.widget
return Size(w.width(), w.height())
@size.setter
def size(self, new_size):
''' Set the widget size.
'''
new_size = Size(*new_size)
self._size = new_size
position = self.position
self.widget.setGeometry(
position.left,
position.top,
new_size.width,
new_size.height)
@property
def bgcolor(self):
''' The background colour.
'''
return self._bgcolor
@bgcolor.setter
def bgcolor(self, new_color):
''' Set the background colour.
'''
new_color = RGBColor(*new_color)
self._bgcolor = new_color
P = self.widget.palette()
P.setColor(self.widget.backgroundRole(), QColor(*new_color))
self.widget.setAutoFillBackground(True)
self.widget.setPalette(P)
@property
def fgcolor(self):
''' The foreground colour.
'''
return self._fgcolor
@fgcolor.setter
def fgcolor(self, new_color):
''' Set the forground colour.
'''
new_color = RGBColor(*new_color)
self._fgcolor = new_color
class Widget(_Element):
''' A basic widget, and _Element with a default kit widget.
'''
def __init__(self, widget=None, **kw):
if widget is None:
widget = QWidget()
super().__init__(widget, **kw)
class _Layout(Widget):
def __init__(self, layout, **kw):
super().__init__(**kw)
self.layout = layout
self.cells = []
def add(self, new_widget):
''' Append a widget to the layout.
'''
assert isinstance(new_widget, _Element)
self.cells.append(new_widget)
self.layout.addWidget(new_widget.widget)
return new_widget
class HBox(_Layout):
''' A scrollable horizontal stack of widgets.
'''
def __init__(self, widget=None, **kw):
if widget is None:
widget = QWidget()
scrollarea = QScrollArea()
scrollarea.setWidget(widget)
scrollarea.setWidgetResizable(True)
scrollarea.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
scrollarea.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
layout = QHBoxLayout()
widget.setLayout(layout)
super().__init__(layout, widget=scrollarea, **kw)
self.bgcolor = RGBColor(40, 40, 40)
class VBox(_Layout):
''' A vertical stack of widgets.
'''
def __init__(self, widget=None, **kw):
if widget is None:
widget = QWidget()
scrollarea = QScrollArea()
scrollarea.setWidget(widget)
scrollarea.setWidgetResizable(True)
scrollarea.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
scrollarea.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
layout = QVBoxLayout()
widget.setLayout(layout)
super().__init__(layout, widget=scrollarea, **kw)
class Grid(_Layout):
''' A grid arrangement of smaller widgets.
'''
def __init__(self, **kw):
widget = QWidget()
scrollarea = QScrollArea()
scrollarea.setWidget(widget)
scrollarea.setWidgetResizable(True)
scrollarea.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
scrollarea.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
layout = QGridLayout()
widget.setLayout(layout)
super().__init__(layout, widget=scrollarea, **kw)
self.grid = defaultdict(dict)
def __getitem__(self, x):
return self.grid[x]
def __setitem__(self, xy, element):
x, y = xy
assert isinstance(x, int)
assert isinstance(y, int)
assert isinstance(element, _Element)
self.grid[x][y] = element
self.layout.addWidget(element.widget, y, x)
class TabSet(_Element):
''' A tab set, showing one of a set of Elements.
'''
def __init__(self, **kw):
super().__init__(QTabWidget(), **kw)
self.tabs = []
def add(self, new_tab):
''' Add an element to the tab set.
'''
assert isinstance(new_tab, _Element)
self.tabs.append(new_tab)
X("add qt %s to tab set %s", new_tab.widget, self.widget)
self.widget.addTab(new_tab.widget, new_tab.title)
return new_tab
class Label(_Element):
''' A text label.
'''
def __init__(self, text, **kw):
super().__init__(QLabel(), **kw)
self.text = text
@property
def text(self):
''' The label's text.
'''
return self._text
@text.setter
def text(self, new_text):
''' Set the text on the label.
'''
self._text = new_text
html = (
"<font color='%s'>%s</font>"
% (
self.fgcolor.htmlcolor,
new_text.replace('&', '&').replace('<', '<').replace('>', '>')
)
)
self.widget.setText(html)
class Image(_Element):
''' A view of an image.
'''
def __init__(self, image_path, title=None, **kw):
if title is None:
title = image_path
super().__init__(QLabel(), title=title, **kw)
self.image_path = image_path
self._pixmap = None
@property
def pixmap(self):
''' The image pixmap.
'''
pm = self._pixmap
if not pm:
X("load pixmap from %r", self.image_path)
pm = self._pixmap = QPixmap(self.image_path)
return pm
def clear(self):
''' Clear the label.
This is to save memory, permits when out of sight.
'''
self._pixmap = None
self.widget.clear()
def expose(self):
''' Ensure the label pixmap is set.
'''
self.widget.setPixmap(self.pixmap)
class Thumbnail(Image):
''' A thumbnail view of an image.
'''
cache = ThumbnailCache()
def __init__(self, image_path, size=None, **kw):
if size is None:
size = Size(128, 128)
super().__init__(image_path, size=size, **kw)
@property
def path(self):
''' The pathname of the thumbnail image file.
'''
dx, dy = self.size
return self.cache.thumb_for_path(dx, dy, self.image_path)
@property
def pixmap(self):
''' The thumbnail pixmap.
'''
pm = self._pixmap
if pm is None:
osize = self.size
pm = self._pixmap = QPixmap(self.path).scaled(
osize.width, osize.height, Qt.KeepAspectRatio)
return pm
class MainWindow(_Element):
''' An application main window.
'''
def __init__(self, **kw):
super().__init__(QMainWindow(), **kw)
self._statusline = None
self._inner = None
@property
def inner(self):
''' The main inner widget.
'''
return self._inner
@inner.setter
def inner(self, inner_element):
''' Set the main inner widget.
'''
assert isinstance(inner_element, _Element)
self._inner = inner_element
self.widget.setCentralWidget(inner_element.widget)
@property
def statusline(self):
''' The current status line value.
'''
return self._statusline
@statusline.setter
def statusline(self, new_status):
''' Set the status line value.
'''
assert new_status is None or isinstance(new_status, str)
self._statusline = new_status
X("statusbar => %r", new_status)
self.widget.statusBar().showMessage(new_status if new_status else '')