summaryrefslogtreecommitdiff
path: root/jfr_playoff/gui/frames/__init__.py
blob: 97f55bf223574fe427f7918b51bebc6395e3002a (plain)
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
#coding=utf-8

from functools import partial
import types

import tkinter as tk
from tkinter import ttk
import tkMessageBox

from ..variables import BoolVar
from ..variables import NotifyStringVar, NotifyNumericVar, NumericVar

def setPanelState(frame, state):
    for child in frame.winfo_children():
        if isinstance(child, tk.Frame):
            setPanelState(child, state)
        else:
            child.configure(state=state)

class WidgetRepeater(tk.Frame):
    def __init__(self, master, widgetClass, headers=None, classParams=None,
                 onAdd=None, *args, **kwargs):
        widgetList = widgetClass
        if not isinstance(widgetClass, list):
            widgetList = [widgetClass]
        for widget in widgetList:
            if not issubclass(widget, RepeatableFrame):
                raise AttributeError(
                    'WidgetRepeater widget must be a RepeatableFrame')
        tk.Frame.__init__(self, master, **kwargs)
        self.widgetClass = widgetClass
        self.widgetClassParams = classParams
        self.widgets = []
        self.headers = headers
        self.headerFrame = None
        self.addButton = ttk.Button(
            self, text='[+]', width=5, command=self._addWidget)
        self.onAdd = onAdd
        self.renderContent()

    def _findWidget(self, row, column):
        for children in self.children.values():
            info = children.grid_info()
            if info['row'] == str(row) and info['column'] == str(column):
                return children
        return None

    def _createWidget(self, widgetClass, widgetClassParams=None):
        headeridx = int(self.headerFrame is not None)
        removeButton = ttk.Button(
            self, text='[-]', width=5,
            command=lambda i=len(self.widgets): self._removeWidget(i))
        removeButton.grid(
            row=len(self.widgets)+headeridx, column=0, sticky=tk.N)
        widget = widgetClass(self)
        if widgetClassParams is not None:
            widget.configureContent(**widgetClassParams)
        self.widgets.append(widget)
        self._updateGrid()
        if self.onAdd is not None:
            self.onAdd(widget)

    def _handleWidgetSelection(self, selected):
        if selected < len(self.widgetClass):
            params = None
            if isinstance(self.widgetClassParams, list) and \
               selected < len(self.widgetClassParams):
                params = self.widgetClassParams[selected]
            self._createWidget(self.widgetClass[selected], params)

    def _widgetSelectionDialog(self):
        dialog = tk.Toplevel(self)
        dialog.title('Wybór elementu do dodania')
        dialog.geometry('%dx%d' % (300, len(self.widgetClass) * 20 + 30))
        dialog.grab_set()
        dialog.focus_force()
        frame = WidgetSelectionFrame(
            dialog, vertical=True,
            widgets=self.widgetClass, callback=self._handleWidgetSelection)
        frame.pack(side=tk.TOP, fill=tk.BOTH, expand=True)

    def _addWidget(self):
        if isinstance(self.widgetClass, list):
            self._widgetSelectionDialog()
        else:
            self._createWidget(self.widgetClass, self.widgetClassParams)

    def _removeWidget(self, idx):
        self.widgets.pop(idx).destroy()
        self._findWidget(row=len(self.widgets), column=0).destroy()
        self._updateGrid()

    def _updateGrid(self):
        headeridx = int(self.headerFrame is not None)
        for idx, widget in enumerate(self.widgets):
            widget.grid(
                row=idx+headeridx, column=1, sticky=tk.W+tk.E+tk.N+tk.S)
        if self.headerFrame is not None:
            self.headerFrame.grid(row=0, column=1, sticky=tk.W+tk.E+tk.N+tk.S)
        self.addButton.grid(
            row=len(self.widgets)+headeridx, column=0, columnspan=1,
            sticky=tk.W+tk.N)
        self.event_generate('<<ValueChanged>>', when='tail')

    def _renderHeader(self):
        if self.headers:
            self.headerFrame = tk.Frame(self)
            for idx, header in enumerate(self.headers):
                self.headerFrame.columnconfigure(idx, weight=1)
                widget = header[0](self.headerFrame, **header[1])
                widget.grid(row=0, column=idx, sticky=tk.W+tk.E+tk.N)
            (tk.Label(self, text=' ')).grid(
                row=0, column=0, sticky=tk.W+tk.E+tk.N)

    def renderContent(self):
        self.columnconfigure(1, weight=1)
        self._renderHeader()
        self._updateGrid()

    def getValue(self):
        return [widget.getValue() for widget in self.widgets]

    def _getParamsForWidgetClass(self, widgetClass):
        if not isinstance(self.widgetClass, list):
            return self.widgetClassParams
        if not isinstance(self.widgetClassParams, list):
            return self.widgetClassParams
        for idx, widget in enumerate(self.widgetClass):
            if widget == widgetClass:
                return self.widgetClassParams[idx]
        return None

    def setValue(self, values):
        for i, value in enumerate(values):
            typedWidget = isinstance(value, tuple) \
                and isinstance(value[0], (types.TypeType, types.ClassType))
            if i >= len(self.widgets):
                if typedWidget:
                    self._createWidget(
                        value[0], self._getParamsForWidgetClass(value[0]))
                else:
                    self._addWidget()
            self.widgets[i].setValue(value[1] if typedWidget else value)
        for idx in range(len(values), len(self.widgets)):
            self._removeWidget(len(self.widgets)-1)


class GuiFrame(tk.Frame):
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)
        self.renderContent()

    def renderContent(self):
        pass

    def getSpecificParent(self, parentType):
        obj = self
        while not isinstance(obj, parentType):
            obj = obj.master
            if obj is None:
                break
        return obj


class RepeatableFrame(GuiFrame):
    def configureContent(self, **kwargs):
        pass

    def getValue(self):
        pass

    def setValue(self, value):
        pass

    @classmethod
    def info(cls):
        return cls.__name__

class RepeatableEntry(RepeatableFrame):
    def renderContent(self):
        self.value = NotifyStringVar()
        self.field = ttk.Entry(self, textvariable=self.value)
        self.field.pack(expand=True, fill=tk.BOTH)

    def configureContent(self, **kwargs):
        for param, value in kwargs.iteritems():
            self.field[param] = value

    def getValue(self):
        return self.value.get()

    def setValue(self, value):
        return self.value.set(value)

class ScrollableFrame(tk.Frame):
    def __init__(self, *args, **kwargs):
        vertical = False
        if 'vertical' in kwargs:
            vertical = kwargs['vertical']
            del kwargs['vertical']
        horizontal = False
        if 'horizontal' in kwargs:
            horizontal = kwargs['horizontal']
            del kwargs['horizontal']
        tk.Frame.__init__(self, *args, **kwargs)
        self.canvas = tk.Canvas(self, borderwidth=0, highlightthickness=0)
        if horizontal:
            hscroll = tk.Scrollbar(
                self, orient=tk.HORIZONTAL, command=self.canvas.xview)
            hscroll.pack(side=tk.BOTTOM, fill=tk.X)
            self.canvas.configure(xscrollcommand=hscroll.set)
        if vertical:
            vscroll = tk.Scrollbar(
                self, orient=tk.VERTICAL, command=self.canvas.yview)
            vscroll.pack(side=tk.RIGHT, fill=tk.Y)
            self.canvas.configure(yscrollcommand=vscroll.set)
        frame = tk.Frame(self.canvas, borderwidth=0, highlightthickness=0)
        self.canvas.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
        self.canvasFrame = self.canvas.create_window(
            (0,0), window=frame, anchor=tk.N+tk.W)
        frame.bind('<Configure>', self._onFrameConfigure)
        self.canvas.bind('<Configure>', self._onCanvasConfigure)
        self.bind('<Enter>', partial(self._setScroll, value=True))
        self.bind('<Leave>', partial(self._setScroll, value=False))
        self.renderContent(frame)

    def _setScroll(self, event, value):
        if value:
            self.bind_all('<MouseWheel>', self._onVscroll)
            self.bind_all('<Shift-MouseWheel>', self._onHscroll)
        else:
            self.unbind_all('<MouseWheel>')
            self.unbind_all('<Shift-MouseWheel>')

    def _onHscroll(self, event):
        self._onScroll(tk.X, -1 if event.delta > 0 else 1)

    def _onVscroll(self, event):
        self._onScroll(tk.Y, -1 if event.delta > 0 else 1)

    def _onScroll(self, direction, delta):
        getattr(
            self.canvas, '%sview' % (direction))(tk.SCROLL, delta, tk.UNITS)

    def _onFrameConfigure(self, event):
        self.canvas.configure(scrollregion=self.canvas.bbox('all'))

    def _onCanvasConfigure(self, event):
        self.canvas.itemconfig(self.canvasFrame, width=event.width)

    def renderContent(self, container):
        pass


class WidgetSelectionFrame(ScrollableFrame):
    def __init__(self, *args, **kwargs):
        self.widgets = []
        self.callback = None
        for var in ['widgets', 'callback']:
            if var in kwargs:
                setattr(self, var, kwargs[var])
                del kwargs[var]
        ScrollableFrame.__init__(self, *args, **kwargs)
        addBtn = ttk.Button(
            self.master, text='Dodaj', command=self._onConfirm)
        addBtn.pack(side=tk.BOTTOM)

    def renderContent(self, container):
        self.value = tk.IntVar()
        for idx, widget in enumerate(self.widgets):
            (ttk.Radiobutton(
                container, variable=self.value, value=idx,
                text=widget.info())).pack(side=tk.TOP, fill=tk.X, expand=True)

    def _onConfirm(self):
        if self.callback is not None:
            self.callback(self.value.get())
        self.winfo_toplevel().destroy()

class SelectionButton(ttk.Button):
    @property
    def defaultPrompt(self):
        pass

    @property
    def title(self):
        pass

    @property
    def errorMessage(self):
        pass

    def getOptions(self):
        pass

    def __init__(self, *args, **kwargs):
        for arg in ['callback', 'prompt', 'dialogclass']:
            setattr(self, arg, kwargs[arg] if arg in kwargs else None)
            if arg in kwargs:
                del kwargs[arg]
        kwargs['command'] = self._choosePositions
        if self.prompt is None:
            self.prompt = self.defaultPrompt
        self._trackChanges = False
        ttk.Button.__init__(self, *args, **kwargs)
        self.setPositions([])

    def setPositions(self, values):
        self.selected = values
        self.configure(
            text='[wybrano: %d]' % (len(values)))
        if self.callback is not None:
            self.callback(values)
        if self._trackChanges:
            self.event_generate('<<ValueChanged>>', when='tail')

    def _choosePositions(self):
        options = self.getOptions()
        if not len(options):
            tkMessageBox.showerror(
                self.title, self.errorMessage)
            self.setPositions([])
        else:
            dialog = tk.Toplevel(self)
            dialog.title(self.title)
            dialog.grab_set()
            dialog.focus_force()
            self._trackChanges = True
            selectionFrame = self.dialogclass(
                dialog, title=self.prompt,
                options=options,
                selected=self.selected,
                callback=self.setPositions, vertical=True)
            selectionFrame.pack(fill=tk.BOTH, expand=True, padx=5, pady=5)

class SelectionFrame(ScrollableFrame):
    def __init__(self, master, title='', options=[],
                 selected=None, callback=None, *args, **kwargs):
        self.values = {}
        self.title = title
        self.options = options
        self.selected = selected
        self.callback = callback
        ScrollableFrame.__init__(self, master=master, *args, **kwargs)
        (ttk.Button(master, text='Zapisz', command=self._save)).pack(
            side=tk.BOTTOM, fill=tk.Y)

    def renderOption(self, container, option, idx):
        pass

    def _mapValue(self, idx, value):
        return idx + 1

    def _save(self):
        if self.callback:
            self.callback(
                [idx for idx, value
                 in self.values.iteritems() if value.get()])
        self.master.destroy()

    def renderHeader(self, container):
        container.columnconfigure(1, weight=1)
        (ttk.Label(container, text=self.title)).grid(
            row=0, column=0, columnspan=2)

    def renderContent(self, container):
        self.renderHeader(container)
        for idx, option in enumerate(self.options):
            key = self._mapValue(idx, option)
            self.values[key] = BoolVar()
            self.renderOption(container, option, idx)
            if self.selected and key in self.selected:
                self.values[key].set(True)

class RefreshableOptionMenu(ttk.OptionMenu):
    def __init__(self, master, variable, *args, **kwargs):
        self._valueVariable = variable
        self._valueVariable.trace('w', self._valueSet)
        self._lastValue = variable.get()
        newVar = NotifyStringVar()
        ttk.OptionMenu.__init__(self, master, newVar, *args, **kwargs)
        self._valueLock = False
        self.refreshOptions()

    class _setit(tk._setit):
        def __init__(self, var, valVar, label, value, callback=None):
            tk._setit.__init__(self, var, label, callback)
            self._valueVariable = valVar
            self._properValue = value
        def __call__(self, *args):
            self.__var.set(self.__value)
            self._valueVariable.set(self._properValue)
            if self.__callback:
                self.__callback(self._valueVariable, *args)

    def refreshOptions(self, *args):
        try:
            options = self.getOptions()
            self['menu'].delete(0, tk.END)
            for label, option in options:
                self['menu'].add_command(
                    label=label,
                    command=self._setit(
                        self._variable, self._valueVariable,
                        label, option, self._callback))
            self._valueLock = True
            self._valueVariable.set(
                self._lastValue
                if self._lastValue in [option[1] for option in options]
                else '')
            self._valueLock = False
        except tk.TclError:
            # we're probably being destroyed, ignore
            pass

    def getOptions(self):
        return [
            (self.getLabel(value), self.getVarValue(value))
            for value in self.getValues()]

    def getLabel(self, value):
        pass

    def getValues(self):
        pass

    def getVarValue(self, value):
        return self.getLabel(value)

    def _valueSet(self, *args):
        if not self._valueLock:
            self._lastValue = self._valueVariable.get()
        options = self.getOptions()
        value = self._valueVariable.get()
        for label, val in options:
            if unicode(value) == unicode(val):
                tk._setit(self._variable, label)()
                return
        tk._setit(self._variable, '')()

class TraceableText(tk.Text):
    def __init__(self, *args, **kwargs):
        self._variable = None
        self._variableLock = False
        if 'variable' in kwargs:
            self._variable = kwargs['variable']
            del kwargs['variable']
        tk.Text.__init__(self, *args, **kwargs)
        if self._variable is not None:
            self._orig = self._w + '_orig'
            self.tk.call('rename', self._w, self._orig)
            self.tk.createcommand(self._w, self._proxy)
            self._variable.trace('w', self._fromVariable)

    def _fromVariable(self, *args):
        if not self._variableLock:
            self._variableLock = True
            self.delete('1.0', tk.END)
            self.insert(tk.END, self._variable.get())
            self._variableLock = False

    def _proxy(self, command, *args):
        cmd = (self._orig, command) + args
        # https://stackoverflow.com/a/53418346 <- it's his fault.
        try:
            result = self.tk.call(cmd)
        except:
            return None
        if command in ('insert', 'delete', 'replace') and \
           not self._variableLock:
            text = self.get('1.0', tk.END).strip()
            self._variableLock = True
            self._variable.set(text)
            self._variableLock = False
        return result

class NumericSpinbox(tk.Spinbox):
    def __init__(self, *args, **kwargs):
        kwargs['justify'] = tk.RIGHT
        self._variable = None
        if 'textvariable' in kwargs:
            self._variable = kwargs['textvariable']
        self._default = kwargs['from_'] if 'from_' in kwargs else 0
        tk.Spinbox.__init__(self, *args, **kwargs)
        if self._variable is not None:
            if not isinstance(self._variable, NumericVar):
                raise AttributeError(
                    'NumericSpinbox variable must be NumericVar')
            self._variable.trace('w', self._onChange)

    def _onChange(self, *args):
        val = self._variable.get()
        if val is None:
            self._variable.set(self._default)

class LabelButton(ttk.Button):
    def __init__(self, *args, **kwargs):
        self.label = None
        self.tooltip = None
        self._prevTooltip = None
        for param in ['label', 'tooltip']:
            if param in kwargs:
                setattr(self, param, kwargs[param])
                del kwargs[param]
        ttk.Button.__init__(self, *args, **kwargs)
        if self.label and self.tooltip:
            self.bind('<Enter>', self._onEnter)
            self.bind('<Leave>', self._onLeave)

    def _onEnter(self, *args):
        self._prevTooltip = self.label.cget('text')
        self.label.configure(text=self.tooltip)

    def _onLeave(self, *args):
        self.label.configure(text=self._prevTooltip)