-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgui_tk.py
More file actions
863 lines (762 loc) · 24.6 KB
/
Copy pathgui_tk.py
File metadata and controls
863 lines (762 loc) · 24.6 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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
#!/usr/bin/env python3
''' Assorted Tkinter based GUI widgets and also `BaseTkCommand`
which subclasses `cs.cmdutils.BaseCommand`.
'''
from abc import ABC
from collections import namedtuple
from contextlib import contextmanager
import os
from os.path import (
basename,
expanduser,
splitext,
)
import platform
import tkinter as tk
from tkinter import Tk, ttk
from typing import Iterable, List
from uuid import uuid4
from icontract import require, ensure
from PIL import Image, ImageTk, UnidentifiedImageError
from typeguard import typechecked
from cs.cache import convof
from cs.cmdutils import BaseCommand
from cs.deco import Promotable, promote
from cs.fs import shortpath
from cs.fstags import FSTags, TaggedPath, TaggedPathSet, uses_fstags
from cs.lex import cutprefix
from cs.logutils import warning
from cs.pfx import pfx, pfx_method, pfx_call
from cs.progress import Progress
from cs.resources import RunState, uses_runstate
from cs.tagset import Tag
from cs.lex import r
from cs.x import X
is_darwin = platform.system() == "Darwin"
# used by pngfor()
CONVCACHE_ROOT_ENVVAR = 'CONVCACHE_ROOT'
CONVCACHE_ROOT = os.environ.get(
CONVCACHE_ROOT_ENVVAR, expanduser('~/var/cache/convof')
)
class BaseTkCommand(BaseCommand):
''' A subclass of `cs.cmdutils.BaseCommand`
intended for commands with a GUI.
This prepares a Tk top level widget in `run_context()` as
`self.options.tk_app` and calls its `mainloop()` at the end
of `run()` if the command returns `None`.
By default the top level widget is a `tk.Frame`
unless overridden in `__init__` by the `widget_class` keyword parameter.
'''
DEFAULT_WIDGET_CLASS = tk.Frame
def __init__(self, argv=None, *, widget_class=None, **kw):
''' Initialise the command as for `BaseCommand.__init__`.
The keyword parameter `widget_class` may be used to specify the top
level widget class to use instead of `tk.Frame`.
'''
if widget_class is None:
widget_class = self.DEFAULT_WIDGET_CLASS
super().__init__(argv, **kw)
self.widget_class = widget_class
@contextmanager
def run_context(self):
''' In addition to the behaviour of `BaseCommand.run_context()`,
also create a top level `tk.Frame` as `self.options.tk_app`.
'''
with super().run_context():
app = self.options.tk_app = self.widget_class(Tk())
X("app.gid...")
app.grid()
X("yield")
yield
@uses_runstate
def run(self, runstate: RunState, **kw):
''' Run a command.
Returns the exit status of the command.
In addition to the behaviour of `BaseCommand.run()`,
this implementation treats the command return value specially:
if it is `None` and not `self.options.runstate.cancelled`,
this context manager will call:
self.options.tk_app.mainloop()
on return from the command. This supports commands
just doing the application setup.
'''
xit = super().run(**kw)
if xit is None:
# the command did GUI setup - run the app now
if not runstate.cancelled:
with runstate:
self.options.tk_app.mainloop()
return xit
def ispng(pathname):
''' Is `pathname` that of a PNG image?
Just tests the filename extension at present.
'''
return splitext(basename(pathname))[1].lower() == '.png'
@uses_fstags
def image_size(path, *, fstags: FSTags):
''' Return the pixel size of the image file at `path`
as an `(dx,dy)` tuple, or `None` if the contents cannot be parsed.
'''
tagged = fstags[path]
try:
size = tagged['pil.size']
except KeyError:
try:
with Image.open(path) as im:
tagged['pil.format'] = im.format
size = tagged['pil.size'] = im.size
tagged['mime_type'] = 'image/' + im.format.lower()
except UnidentifiedImageError as e:
warning(f'image_size({path!r}): unhandled image: {e}')
size = tagged['pil.size'] = None
if size is not None:
size = tuple(size)
return size
@pfx
@uses_fstags
def imgof(
path,
max_size=None,
*,
min_size=None,
fmt='png',
force=False,
fstags: FSTags,
):
''' Create a version of the image at `path`,
scaled to fit within some size constraints.
Return the pathname of the new file.
Parameters:
* `max_size`: optional `(width,height)` tuple, default `(1920,1800)`
* `min_size`: optional `(width,height)` tuple, default half of `max_size`
* `fmt`: optional output format, default `'png'`
* `force`: optional flag (default `False`)
to force recreation of the PNG version and associated cache entry
'''
if len(fmt) == 0 or '/' in fmt or '.' in fmt:
raise ValueError(
f'{fmt=} must be a nonempty string containing no slashes or dots'
)
FMT = fmt.upper()
fmt = fmt.lower()
if max_size is None:
max_size = 1920, 1080
if min_size is None:
min_size = max_size[0] // 2, max_size[1] // 2
tagged = fstags[path]
path = tagged.fspath
size = image_size(path)
if size is None:
# cannot determine source image size
return None
# choose a target size
if size[0] > max_size[0] or size[1] > max_size[1]:
# scale down
scale = min(max_size[0] / size[0], max_size[1] / size[1])
re_size = int(size[0] * scale), int(size[1] * scale)
elif size[0] < min_size[0] or size[1] < min_size[1]:
# scale up
scale = min(min_size[0] / size[0], min_size[1] / size[1])
re_size = int(size[0] * scale), int(size[1] * scale)
else:
re_size = None
if tagged.get('pil.format').lower() == fmt:
# already an image of the right size and format
return path
def resize(srcpath, dstpath):
''' Rescale and format `srcpath`, save as `dstpath`.
'''
with Image.open(srcpath) as im:
if re_size is None:
pfx_call(im.save, dstpath, FMT)
else:
im2 = im.resize(re_size)
pfx_call(im2.save, dstpath, FMT)
convsize = re_size or size
return convof(
path,
f'{fmt}/{convsize[0]}x{convsize[1]}',
resize,
ext=fmt,
force=force,
)
def pngfor(path, **imgof_kw):
''' Call `imgof` specifying PNG output.
'''
return imgof(path, fmt='png', **imgof_kw)
##@require(lambda x1: x1 >= 0)
@require(lambda dx1: dx1 > 0)
##@require(lambda x2: x2 >= 0)
@require(lambda dx2: dx2 > 0)
@ensure(lambda result, dx1: result is None or result[1] <= dx1)
@ensure(lambda result, dx2: result is None or result[1] <= dx2)
def overlap1(x1, dx1, x2, dx2):
''' Compute the overlap of 2 ranges,
return `None` for no overlap
or `(overlap_x,overlap_dx)` if they overlap.
'''
if dx1 <= 0 or dx2 <= 0:
# zero width spans cannot overlap
return None
if x1 <= x2 < x1 + dx1:
# span 1 left of span 2 and overlapping
return x2, min(x1 + dx1, x2 + dx2) - x2
if x2 <= x1 < x2 + dx2:
# span 2 left of span 1 and overlapping
return x1, min(x1 + dx1, x2 + dx2) - x1
return None
class WidgetGeometry(namedtuple('WidgetGeometry', 'x y dx dy')):
''' A geometry tuple and associated methods.
'''
@classmethod
def of(cls, w):
''' The geometry of the widget `w` in root coordinates.
'''
x, y = w.winfo_rootx(), w.winfo_rooty()
dx, dy = w.winfo_width(), w.winfo_height()
return cls(x, y, dx, dy)
def overlap(self, other):
''' Compute an overlap rectangle between two `WidgetGeometry` objects.
Returns `None` if there is no overlap,
otherwise a new `WidgetGeometry` indicating the overlap.
'''
# compute the horizontal overlap
if self.dx <= 0 or self.dy <= 0:
return None
over = overlap1(self.x, self.dx, other.x, other.dx)
if over is None:
return None
over_x, over_dx = over
# compute the vertical overlap
over = overlap1(self.y, self.dy, other.y, other.dy)
if over is None:
return None
over_y, over_dy = over
return type(self)(over_x, over_y, over_dx, over_dy)
class _Widget(ABC):
WIDGET_BACKGROUND = 'black'
WIDGET_FOREGROUND = 'white'
##@pfx_method
@typechecked
def __init__(self, parent, *tk_a, key=None, fixed_size=None, **tk_kw):
# apply the type(self).WIDGET_* defaults if not present
for attr in dir(self):
K = cutprefix(attr, 'WIDGET_')
if K is not attr:
v = getattr(self, attr)
if v is not None:
tk_kw.setdefault(K.lower(), v)
##X("CALLER = %s", caller())
##X("type(self)=%r", type(self))
##X("type(parent)=%s", type(parent))
##X("parent=%s", r(parent))
##X(
## "%s: _Widget.__init__: super().__init__(parent=%s,*a=%r,**kw=%r)...",
## type(self), r(parent), a, kw
##)
if fixed_size is None:
fixed_size = (None, None)
self.__parent = parent
self.fixed_size = fixed_size
if self.fixed_width is not None:
tk_kw.update(width=self.fixed_width)
if self.fixed_height is not None:
tk_kw.update(height=self.fixed_height)
if key is None:
key = uuid4()
self.key = key
super().__init__(parent, *tk_a, **tk_kw)
@property
def parent(self):
''' The widget's parent as noted at initialisation.
'''
return self.__parent
@property
def fixed_width(self):
''' The width componet of the `fixed_size`.
'''
return self.fixed_size[0]
@property
def fixed_height(self):
''' The height componet of the `fixed_size`.
'''
return self.fixed_size[1]
def is_visible(self):
''' Is this widget visible:
- it and all ancestors are mapped
- its rectangle overlaps its parent
- its parent is visible
'''
##assert self.winfo_ismapped() is self.winfo_viewable(), (
## "%s.winfo_ismapped()=%s but %s.winfo_viewable()=%s" %
## (self, self.winfo_ismapped(), self, self.winfo_viewable())
##)
if not self.winfo_ismapped() or not self.winfo_viewable():
return False
g = WidgetGeometry.of(self)
p = self.parent
while p is not None:
pg = WidgetGeometry.of(p)
overlap = g.overlap(pg)
if not overlap:
return False
p = p.parent
return True
# local shims for the tk and ttk widgets
BaseButton = tk.Button
is_tk_button = True
if is_darwin:
try:
from tkmacosx import Button as BaseButton
is_tk_button = False
except ImportError as import_e:
warning(
"import tkmacosx: %s; buttons will look better with tkmacos on Darwin",
import_e
)
# pylint: disable=too-many-ancestors
class Button(_Widget, BaseButton):
''' Button `_Widget` subclass.
'''
def __init__(self, *a, background=None, highlightbackground=None, **kw):
if background is None:
background = self.WIDGET_BACKGROUND
if not is_tk_button:
kw.update(background=background)
if highlightbackground is None:
highlightbackground = background
kw.update(highlightbackground=highlightbackground)
super().__init__(*a, **kw)
# pylint: disable=too-many-ancestors
class Canvas(_Widget, tk.Canvas):
''' Canvas `_Widget` subclass.
'''
WIDGET_FOREGROUND = None
def scroll_bbox_x(self):
''' Configure the canvas height and scrollregion for the current contents.
'''
bbox = self.bbox("all")
self.configure(scrollregion=bbox)
self.configure(height=bbox[3])
# pylint: disable=too-many-ancestors
class Combobox(_Widget, ttk.Combobox):
''' Combobox `_Widget` subclass.
'''
# pylint: disable=too-many-ancestors
class Entry(_Widget, tk.Entry):
''' Entry `_Widget` subclass.
'''
# pylint: disable=too-many-ancestors
class Frame(_Widget, tk.Frame):
''' Frame `_Widget` subclass.
'''
WIDGET_FOREGROUND = None
# pylint: disable=too-many-ancestors
class Label(_Widget, tk.Label):
''' Label `_Widget` subclass.
'''
# pylint: disable=too-many-ancestors
class LabelFrame(_Widget, tk.LabelFrame):
''' LabelFrame `_Widget` subclass.
'''
# pylint: disable=too-many-ancestors
class Listbox(_Widget, tk.Listbox):
''' Listbox `_Widget` subclass.
'''
# pylint: disable=too-many-ancestors
class PanedWindow(_Widget, tk.PanedWindow):
''' PanedWindow `_Widget` subclass.
'''
# pylint: disable=too-many-ancestors
class Scrollbar(_Widget, tk.Scrollbar):
''' Scrollbar `_Widget` subclass.
'''
WIDGET_FOREGROUND = None
# pylint: disable=too-many-ancestors
class Text(_Widget, tk.Text):
''' Text `_Widget` subclass.
'''
class _ImageWidget(_Widget):
def __init__(self, parent, *, path, **kw):
''' Initialise the image widget to display `path`.
'''
kw.setdefault('bitmap', 'gray25')
kw.setdefault('text', shortpath(path) if path else "NONE")
super().__init__(parent, **kw)
self.fspath = path
self.image = None
self._image_for = None
@property
def fspath(self):
''' The filesystem path of the current display.
'''
return self._fspath
@fspath.setter
def fspath(self, new_fspath):
self._fspath = new_fspath
self.configure(text=new_fspath or r(new_fspath))
def ev_set_image(ev):
''' Set the image once visible, fired at idle time.
It is essential that this is queued with `after_idle`.
If this ran directly during widget construction
the `wait_visibility` call would block the follow construction.
'''
if self._image_for == self._fspath:
return
if not self.is_visible():
return
imgpath = self._fspath
if imgpath is None:
display_fspath = None
else:
size = self.fixed_size or (self.width, self.height)
try:
display_fspath = pngfor(expanduser(imgpath), max_size=size)
except (OSError, ValueError) as e:
warning("%r: %s", imgpath, e)
display_fspath = None
if display_fspath is None:
self._image_for = None
self.image = None
self.configure(image=None) # pylint: disable=no-member
else:
img = Image.open(display_fspath)
image = ImageTk.PhotoImage(img)
self.configure(
text=basename(imgpath),
image=image,
width=size[0],
height=size[1],
) # pylint: disable=no-member
self.image = image
self._image_for = self._fspath
self.bind('<Configure>', ev_set_image) # pylint: disable=no-member
self.bind('<Map>', ev_set_image) # pylint: disable=no-member
self.bind('<Unmap>', ev_set_image) # pylint: disable=no-member
class ImageWidget(_ImageWidget, Label):
''' An image widget which can show anything Pillow can read.
'''
class ImageButton(_ImageWidget, tk.Button):
''' An image button which can show anything Pillow can read.
'''
class TaggedPathSetVar(tk.StringVar, Promotable):
''' A subclass for `tk.StringVar` maintaining a `TaggedPathSet`.
'''
@promote
@typechecked
def __init__(self, paths: TaggedPathSet = None, *, display=None):
''' Initialise the `TaggedPathSetVar`.
If the optional `paths` is supplied, use that as the backing store.
'''
self.display = display or shortpath
if paths is None:
paths = TaggedPathSet()
self.taggedpaths = paths
super().__init__()
self.set(paths.fspaths)
def get(self):
''' Get the current display paths in the expected `('v1',...)` form.
'''
return ''.join(
'(',
','.join(repr(self.display(path.fspath)) for path in self.taggedpaths),
')',
)
def set(self, new_paths=Iterable[str]):
''' Update the list of paths.
'''
paths = self.taggedpaths
paths.clear()
paths.update(new_paths)
super().set(' '.join(self.display(path.fspath) for path in paths))
@classmethod
def promote(cls, obj):
''' Promote an object to a `TaggedPathSet`.
'''
if isinstance(obj, cls):
return obj
return cls(paths=obj)
class HasTaggedPathSet:
''' A mixin with methods for maintaining a list of filesystem paths.
This maintains a `.taggedpaths` attribute holding a `TaggedPathSet`.
'''
@promote
def __init__(
self,
paths: TaggedPathSetVar = None,
*,
display=None,
):
''' Initialise `self.pathsvar`.
Parameters:
* `paths`: used to intialise `.taggedpaths`, optional; an existing
`TaggedPathSet` or an iterable of `str` or `TaggedPath`
* `display`: optional callable computing the friendly form of a path,
default `cs.fs.shortpath`
'''
if paths is None:
paths = TaggedPathSetVar(paths=paths, display=display)
elif display is not None:
paths.display - display
self.pathsvar = paths
@property
def taggedpaths(self):
''' The `TaggedPathSet` instance.
'''
return self.pathsvar.taggedpaths
@property
def fspaths(self):
''' A list of the filesystem paths.
'''
return [path.fspath for path in self.taggedpaths]
@fspaths.setter
def fspaths(self, new_fspaths):
''' Setting `.fspaths` calls `self.set(new_fspaths)`.
'''
self.set(new_fspaths)
@property
def display(self):
''' The display function.
'''
return self.pathsvar.display
def display_paths(self):
''' Return a list of the friendly form of each path.
'''
display = self.display
return [display(path.fspath) for path in self.taggedpaths]
def set(self, paths):
''' Set the `paths`.
'''
self.pathsvar.set(paths)
class PathList_Listbox(Listbox, HasTaggedPathSet):
''' A `Listbox` displaying filesystem paths.
'''
@promote
@typechecked
def __init__(
self, parent, paths: TaggedPathSetVar = None, *, command, **listbox_kw
):
HasTaggedPathSet.__init__(self, paths=paths)
Listbox.__init__(self, parent, listvariable=self.pathsvar, **listbox_kw)
self.command = command
self.bind('<Button-1>', self._clicked)
def _clicked(self, event):
''' Call `self.command(nearest_index, self.fspaths[nearest_index])`.
'''
nearest = self.nearest(event.y)
self.command(nearest, self.fspaths[nearest])
def _update_display(self):
''' Update the displayed list, to be called when `self.taggedpaths` is modified.
'''
display_paths = self.display_paths()
self.pathsvar.set(' '.join(display_paths))
if self.fixed_width is None:
self.config(
width=(max(map(len, display_paths)) if display_paths else 0) + 10
)
def setpaths(self, paths):
self.pathsvar.set(paths)
@pfx_method
@promote
def show_fspath(self, fspath: TaggedPath, select=False):
''' Adjust the list box so that `fspath` is visible.
'''
paths = self.taggedpaths
if fspath not in paths:
paths.add(fspath)
self._update_display()
index = paths.find(fspath)
self.see(index)
if select:
self.selection_clear(0, len(self.taggedpaths) - 1)
self.selection_set(index)
class TagValueStringVar(tk.StringVar):
''' A `StringVar` which holds a `Tag` value transcription.
'''
def __init__(self, value, **kw):
''' Initialise the `TagValueStringVar` with `value`.
Keyword arguments are passed to `tk.StringVar.__init__`.
'''
super().__init__(master=None, value=None, **kw)
self.set(value)
def set(self, value):
''' Set the contents to `Tag.transcribe_value(value)`.
'''
super().set(Tag.transcribe_value(value))
@pfx_method
def get(self):
''' Return the value derived from the contents via `Tag.parse_value`.
An attempt is made to cope with unparsed values.
'''
value0 = super().get()
try:
value, offset = pfx_call(Tag.parse_value, value0)
except ValueError as e:
warning(str(e))
value = value0
else:
if offset < len(value0):
warning("unparsed: %r", value0[offset:])
if isinstance(value, str):
value += value0[offset:]
else:
value = value0
return value
class EditValueWidget(Frame):
''' A widget to edit a `Tag` value,
a `Frame` containing a value specific editing widget.
'''
def __init__(self, parent, value, alt_values=None, **kw):
super().__init__(parent, **kw)
if value is None:
value = ""
if alt_values and not isinstance(value, (dict, list)):
tv = TagValueStringVar(value)
edit_widget = Combobox(self, textvariable=tv, values=sorted(alt_values))
if value is None or value == "":
edit_widget.set(sorted(alt_values)[0])
get_value = lambda _: tv.get()
elif isinstance(value, str):
if '\n' in value:
edit_widget = Text(self)
edit_widget.insert(tk.END, value)
get_value = lambda w: w.get(1.0, tk.END).rstrip('\n')
else:
tv = TagValueStringVar(value)
edit_widget = Entry(self, textvariable=tv)
get_value = lambda _: tv.get()
elif isinstance(value, (int, float)):
tv = TagValueStringVar(value)
edit_widget = Entry(self, textvariable=tv)
get_value = lambda _: tv.get()
else:
edit_text = Tag.transcribe_value(
value,
json_options=dict(indent=2, sort_keys=True, ensure_ascii=False)
)
edit_widget = Text(self)
edit_widget.insert(tk.END, edit_text)
def get_value(w):
''' Obtain the new value from the widget contents.
'''
edited = w.get(1.0, tk.END).rstrip('\n')
try:
new_value, offset = pfx_call(Tag.parse_value, edited)
except ValueError as e:
warning("toggle_editmode: %s", e)
new_value = edited
else:
if offset < len(edited):
warning("unparsed: %r", edited[offset:])
if isinstance(new_value, str):
new_value += edited[offset:]
else:
new_value = edited
return new_value
edit_widget.grid(sticky=tk.W + tk.E + tk.N + tk.S)
self.edit_widget = edit_widget
self.get = lambda: get_value(edit_widget)
def focus_set(self):
''' Setting the focus should focus the `edit_widget`.
'''
self.edit_widget.focus_set()
@staticmethod
def _parse_value(value_s):
try:
value, offset = pfx_call(Tag.parse_value, value_s)
except ValueError as e:
warning("EditValue._parse_value(%s): %s", r(value_s), e)
value = value_s
else:
if offset < len(value_s):
warning("unparsed: %r", value_s[offset:])
if isinstance(value, str):
value += value_s[offset:]
else:
value = value_s
return value
class ThumbNailScrubber(Frame, HasTaggedPathSet):
''' A row of thumbnails for a list of filesystem paths.
'''
THUMB_X = 64
THUMB_Y = 64
def __init__(self, parent, paths: List[str], *, command, **frame_kw):
HasTaggedPathSet.__init__(self, paths)
Frame.__init__(self, parent, **frame_kw)
self.command = command
self.make_subwidget = (
lambda i, fspath: ImageButton(
self,
path=fspath,
command=lambda i=i, path=fspath: self.command(i, path),
fixed_size=(self.THUMB_X, self.THUMB_Y),
)
)
def set(self, new_fspaths):
''' Setting new filesystem paths updates `.pathsvar` and remakes
the thumbnail widgets.
'''
super().set(new_fspaths)
display_paths = self.display_paths()
for child in list(self.grid_slaves()):
child.grid_remove()
for i, display_path in enumerate(display_paths):
thumbnail = self.make_subwidget(i, display_path)
thumbnail.grid(column=i, row=0)
@pfx_method
def show_fspath(self, fspath):
''' TODO: bring the correspnding thumbnail into view.
'''
warning("UNIMPLEMENTED: scrubber thumbnail not yet scrolled into view")
class ProgressVar(tk.DoubleVar, Promotable):
''' A subclass of `tk.DoubleVar` maintaining a `cs.progress.Progress`.
'''
@promote
@typechecked
def __init__(self, progress: Progress):
self.progress = progress
def set(self, position):
''' Setting the control variable updates the `Progress` instance.
'''
self.progress.position = position
super().set(position)
def promote(cls, obj):
''' Promote a `Progress` to a `ProgressVar`.
'''
if isinstance(obj, cls):
return obj
return cls(obj)
class ProgressBar(ttk.Progressbar):
''' A `ttk.ProgressBar` with an associated `cs.progress.Progress` instance.
'''
@promote
def __init__(
self,
parent,
variable: ProgressVar,
*,
maximum=None,
mode=None,
**ttkp_options,
):
if variable is None:
variable = ProgressVar(Progress(total=maximum))
progress = variable.progress
if maximum is None:
maximum = progress.total
if mode is None:
mode = 'indeterminate' if progress.total is None else 'determinate'
self.progressvar = variable
super().__init__(
parent, maximum=maximum, mode=mode, variable=variable, **ttkp_options
)
@property
def progress(self):
''' The underlying `Progress` instance.
'''
return self.progressvar.progress
def step(self, delta=1.0):
''' Bump the underlying `Progress.position`, then call `ttk.ProgressBar.step(delta)`.
'''
self.progress.position += delta
super().step(delta)