-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontrols.py
247 lines (203 loc) · 7.56 KB
/
controls.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
# -*- coding: utf-8 -*-
from functools import partial
import tkinter as tk
from tkinter import ttk
import colors
import dims
# Names for the states of buttons. Values are indices into lists.
DISABLED = 0
ENABLED = 1
ACTIVE = 2
class Button(tk.Button):
"""Class to implement a 3-state button."""
tk_states = (tk.DISABLED, tk.NORMAL, tk.NORMAL)
def __init__(self, parent, **kwargs):
# User can supply a list of texts for the 3 states.
# If this is supplied, there is no need to supply the standard text
self.texts = kwargs.pop("texts", [])
if self.texts:
kwargs["text"] = self.texts[1]
else:
self.texts = None
# user can supply a custom color for ENABLED and/or ACTIVE states
colors = None
color1 = kwargs.pop("color1", None)
color2 = kwargs.pop("color2", None)
if color1 or color2:
colors = ["SystemButtonFace"] * 3
if color1:
colors[ENABLED] = color1
if color2:
colors[ACTIVE] = color2
self.colors = colors
super().__init__(parent, **kwargs)
self._state = ENABLED
@property
def state(self):
return self._state
@state.setter
def state(self, state):
assert isinstance(state, int)
if state != self._state:
self._state = state
kwargs = {"state": Button.tk_states[state]}
if self.colors:
kwargs["bg"] = self.colors[state]
if self.texts:
kwargs["text"] = self.texts[state]
self.configure(**kwargs)
class Control:
"""Abstract base class for widgets."""
"""Base class for customized widgets."""
def __init__(self, label):
self.label = label
self.callback = None
self.dataname = None
def action(self, x=None):
self.callback(self.dataname)
def get(self):
return self.var.get()
def set(self, value):
self.ctl.set(value)
def set_data(self, dataname, data):
"""Construct a tkinter variable that is compatible with our data."""
self.dataname = dataname
value = getattr(data, dataname)
datatype = type(value)
if datatype is int or datatype is bool:
self.var = tk.IntVar()
elif datatype is float:
self.var = tk.DoubleVar()
elif datatype is str:
self.var = tk.StringVar()
else:
raise TypeError
class CheckControl(Control):
"""Class to manage a ttk.CheckButton widget."""
def __init__(self, label, underline=-1):
self.underline = underline
super().__init__(label)
def add_control(self, frame, row, col, **kwargs):
self.ctl = ttk.Checkbutton(
frame, text=self.label, variable=self.var, underline=self.underline, command=self.action
)
self.ctl.grid(row=row, column=col, sticky=tk.W, **kwargs)
self.ctl.hint_id = self.dataname
def set(self, value):
if isinstance(value, str):
value = 1 if value == "True" else 0
else:
value = int(value)
self.var.set(value)
def xor(self):
self.var.set(self.var.get() ^ 1)
class ComboControl(Control):
"""Class to manage a ttk.Combobox widget."""
def __init__(self, label, values):
self.values = values
super().__init__(label)
def add_control(self, frame, row, col, **kwargs):
ctl = tk.Label(frame, text=self.label)
ctl.grid(row=row, column=0, sticky=tk.SW)
self.ctl = ttk.Combobox(
frame,
state="readonly",
width=4,
values=self.values,
)
self.ctl.grid(row=row, column=col, sticky=tk.W, **kwargs)
self.ctl.bind("<<ComboboxSelected>>", self.action)
self.ctl.hint_id = self.dataname
def get(self):
return self.ctl.get()
class SlideControl(Control):
"""Class to manage a tk.Scale widget."""
def __init__(self, label, from_, to, res):
self.fr = from_
self.to = to
self.res = res
super().__init__(label)
def add_control(self, frame, row, col, **kwargs):
ctl = tk.Label(frame, text=self.label)
ctl.grid(row=row, column=col - 1, sticky=tk.SW)
self.ctl = tk.Scale(
frame,
from_=self.fr,
to=self.to,
resolution=self.res,
orient=tk.HORIZONTAL,
command=self.action,
)
self.ctl.grid(row=row, column=col, sticky=tk.W, **kwargs)
self.ctl.hint_id = self.dataname
def get(self):
return self.ctl.get()
def step(self, units):
"""Step the number of units specified."""
self.ctl.set(self.ctl.get() + self.res * units)
class PlaneControl:
"""A class to manage tkinter controls for a single plane."""
def __init__(self, frame, row, dim1, dim2, app):
self.frame = frame
self.row = row
self.dim1 = dim1
self.dim2 = dim2
self.app = app
self.active = False
def add_controls(self):
dim1str = dims.labels[self.dim1]
dim2str = dims.labels[self.dim2]
text = f"{dim1str}-{dim2str}"
self.planes = tk.Label(self.frame, text=text)
self.planes.grid(row=self.row, column=0, sticky=tk.EW, padx=2, pady=2)
# create a subframe for the rotation controls
self.rot_frame = tk.Frame(self.frame)
self.rot_frame.grid(row=self.row, column=1)
# insert rotation controls
self.rotate1 = tk.Button(
self.rot_frame, text=" < ", command=partial(self.app.on_rotate, "+", self)
)
self.rotate1.grid(row=0, column=0, sticky=tk.W, padx=2, pady=2)
self.rotate1.hint_id = "rotate"
self.rotate2 = tk.Button(
self.rot_frame, text=" > ", command=partial(self.app.on_rotate, "-", self)
)
self.rotate2.grid(row=0, column=1, sticky=tk.W, padx=2, pady=2)
self.rotate2.hint_id = "rotate"
# insert information about colors of dimensions
self.swatch1 = tk.Label(self.frame, text=f"{dim1str}: ████", bg=colors.html_bg)
self.swatch1.grid(row=self.row, column=2, sticky=tk.NSEW)
self.swatch2 = tk.Label(self.frame, text=f"{dim2str}: ████", bg=colors.html_bg)
self.swatch2.grid(row=self.row, column=3, sticky=tk.NSEW)
self.swatch3 = tk.Label(self.frame, text=f"████", bg=colors.html_bg)
self.swatch3.grid(row=self.row, column=4, sticky=tk.NSEW)
self.active = True
# self.show_colors()
def delete_controls(self):
self.rot_frame.destroy()
self.planes.destroy()
self.rotate1.destroy()
self.rotate2.destroy()
self.swatch1.destroy()
self.swatch2.destroy()
self.swatch3.destroy()
self.active = False
def show_colors(self, data):
if self.active:
no_color = colors.html_bg
if data.show_edges:
if data.show_4_gray and (self.dim1 > 2 or self.dim2 > 2):
color1 = color2 = colors.html_dim4gray
else:
color1 = colors.html[self.dim1]
color2 = colors.html[self.dim2]
else:
color1 = color2 = no_color
if data.show_faces:
bgr = colors.face([self.dim1, self.dim2])
color3 = colors.bgr_to_html(*bgr)
else:
color3 = no_color
self.swatch1.configure(fg=color1)
self.swatch2.configure(fg=color2)
self.swatch3.configure(fg=color3)