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

from functools import partial

import tkinter as tk
from tkinter import ttk

def getIntVal(widget, default=0):
    try:
        return int(widget.get().strip())
    except ValueError:
        return default

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,
                 *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.addButton = ttk.Button(
            self, text='[+]', width=5, command=self._addWidget)
        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):
        removeButton = ttk.Button(
            self, text='[-]', width=5,
            command=lambda i=len(self.widgets): self._removeWidget(i))
        removeButton.grid(row=len(self.widgets), column=0, sticky=tk.N)
        widget = widgetClass(self)
        if widgetClassParams is not None:
            widget.configureContent(**widgetClassParams)
        self.widgets.append(widget)
        self._updateGrid()

    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))
        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):
        for idx, widget in enumerate(self.widgets):
            widget.grid(row=idx, column=1, sticky=tk.W+tk.E+tk.N+tk.S)
        self.addButton.grid(
            row=len(self.widgets), column=0, columnspan=1, sticky=tk.W+tk.N)

    def _renderHeader(self):
        if self.headers:
            headerFrame = tk.Frame(self)
            for idx, header in enumerate(self.headers):
                headerFrame.columnconfigure(idx, weight=1)
                widget = header[0](headerFrame, **header[1])
                widget.grid(row=0, column=idx, sticky=tk.W+tk.E+tk.N)
            self.widgets.append(headerFrame)
            (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
                if isinstance(widget, self.widgetClass)]

    def setValue(self, value):
        for i in range(0, len(value)):
            if i >= len(self.widgets):
                self._addWidget()
            self.widgets[i].setValue(value[i])

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

    def renderContent(self):
        pass

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

    def renderContent(self):
        pass

    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 = tk.StringVar()
        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)
        canvas = tk.Canvas(self, borderwidth=0, highlightthickness=0)
        if horizontal:
            hscroll = tk.Scrollbar(
                self, orient=tk.HORIZONTAL, command=canvas.xview)
            hscroll.pack(side=tk.BOTTOM, fill=tk.X)
            canvas.configure(xscrollcommand=hscroll.set)
        if vertical:
            vscroll = tk.Scrollbar(
                self, orient=tk.VERTICAL, command=canvas.yview)
            vscroll.pack(side=tk.RIGHT, fill=tk.Y)
            canvas.configure(yscrollcommand=vscroll.set)
        frame = tk.Frame(canvas, borderwidth=0, highlightthickness=0)
        canvas.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
        canvas.create_window((0,0), window=frame, anchor=tk.N+tk.W)
        frame.bind(
            '<Configure>',
            lambda ev: canvas.configure(scrollregion=canvas.bbox('all')))
        self.renderContent(frame)

    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()