printrun-src/printrun/gui/zbuttons.py

Wed, 20 Jan 2021 10:15:13 +0100

author
mdd
date
Wed, 20 Jan 2021 10:15:13 +0100
changeset 46
cce0af6351f0
parent 15
0bbb006204fc
permissions
-rw-r--r--

updated and added new files for printrun

15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1 # This file is part of the Printrun suite.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
2 #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
3 # Printrun is free software: you can redistribute it and/or modify
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
4 # it under the terms of the GNU General Public License as published by
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
5 # the Free Software Foundation, either version 3 of the License, or
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
6 # (at your option) any later version.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
7 #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
8 # Printrun is distributed in the hope that it will be useful,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
11 # GNU General Public License for more details.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
12 #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
13 # You should have received a copy of the GNU General Public License
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
14 # along with Printrun. If not, see <http://www.gnu.org/licenses/>.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
16 import wx
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
17 from printrun.gui.xybuttons import FocusCanvas
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
18 from printrun.utils import imagefile
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
19
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
20 def sign(n):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
21 if n < 0: return -1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
22 elif n > 0: return 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
23 else: return 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
24
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
25 class ZButtons(FocusCanvas):
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
26 button_ydistances = [7, 30, 55, 83] # ,112
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
27 move_values = [0.1, 1, 10]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
28 center = (30, 118)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
29 label_overlay_positions = {
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
30 0: (1.1, 18, 9),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
31 1: (1.1, 41.5, 10.6),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
32 2: (1.1, 68, 13),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
33 }
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
34 imagename = "control_z.png"
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
35
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
36 def __init__(self, parent, moveCallback = None, bgcolor = "#FFFFFF", ID=-1):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
37 self.bg_bmp = wx.Image(imagefile(self.imagename), wx.BITMAP_TYPE_PNG).ConvertToBitmap()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
38 self.range = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
39 self.direction = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
40 self.orderOfMagnitudeIdx = 0 # 0 means '1', 1 means '10', 2 means '100', etc.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
41 self.moveCallback = moveCallback
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
42 self.enabled = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
43 # Remember the last clicked value, so we can repeat when spacebar pressed
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
44 self.lastValue = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
45
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
46 self.bgcolor = wx.Colour()
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
47 self.bgcolor.Set(bgcolor)
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
48 self.bgcolormask = wx.Colour(self.bgcolor.Red(), self.bgcolor.Green(), self.bgcolor.Blue(), 128)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
49
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
50 # On MS Windows super(style=WANTS_CHARS) prevents tab cycling
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
51 # pass empty style explicitly
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
52 super().__init__(parent, ID, size=self.bg_bmp.GetSize(), style=0)
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
53
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
54 # Set up mouse and keyboard event capture
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
55 self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
56 self.Bind(wx.EVT_LEFT_DCLICK, self.OnLeftDown)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
57 self.Bind(wx.EVT_MOTION, self.OnMotion)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
58 self.Bind(wx.EVT_LEAVE_WINDOW, self.OnLeaveWindow)
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
59 self.Bind(wx.EVT_SET_FOCUS, self.RefreshFocus)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
60 self.Bind(wx.EVT_KILL_FOCUS, self.RefreshFocus)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
61
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
62 def RefreshFocus(self, evt):
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
63 self.Refresh()
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
64 evt.Skip()
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
65
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
66 def disable(self):
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
67 self.Enabled = False # prevents focus
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
68 self.enabled = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
69 self.update()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
70
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
71 def enable(self):
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
72 self.Enabled = True
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
73 self.enabled = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
74 self.update()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
75
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
76 def repeatLast(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
77 if self.lastValue:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
78 self.moveCallback(self.lastValue)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
79
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
80 def clearRepeat(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
81 self.lastValue = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
82
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
83 def lookupRange(self, ydist):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
84 idx = -1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
85 for d in self.button_ydistances:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
86 if ydist < d:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
87 return idx
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
88 idx += 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
89 return None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
90
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
91 def highlight(self, gc, rng, dir):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
92 assert(rng >= -1 and rng <= 3)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
93 assert(dir >= -1 and dir <= 1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
94
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
95 fudge = 11
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
96 x = 0 + fudge
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
97 w = 59 - fudge * 2
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
98 if rng >= 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
99 k = 1 if dir > 0 else 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
100 y = self.center[1] - (dir * self.button_ydistances[rng + k])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
101 h = self.button_ydistances[rng + 1] - self.button_ydistances[rng]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
102 gc.DrawRoundedRectangle(x, y, w, h, 4)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
103 # gc.DrawRectangle(x, y, w, h)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
104 # self.drawPartialPie(dc, center, r1-inner_ring_radius, r2-inner_ring_radius, a1+fudge, a2-fudge)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
105
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
106 def getRangeDir(self, pos):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
107 ydelta = self.center[1] - pos[1]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
108 return (self.lookupRange(abs(ydelta)), sign(ydelta))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
109
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
110 def draw(self, dc, w, h):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
111 dc.SetBackground(wx.Brush(self.bgcolor))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
112 dc.Clear()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
113 gc = wx.GraphicsContext.Create(dc)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
114 if self.bg_bmp:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
115 w, h = (self.bg_bmp.GetWidth(), self.bg_bmp.GetHeight())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
116 gc.DrawBitmap(self.bg_bmp, 0, 0, w, h)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
117
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
118 if self.enabled and self.IsEnabled():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
119 # Draw label overlays
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
120 gc.SetPen(wx.Pen(wx.Colour(255, 255, 255, 128), 1))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
121 gc.SetBrush(wx.Brush(wx.Colour(255, 255, 255, 128 + 64)))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
122 for idx, kpos in self.label_overlay_positions.items():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
123 if idx != self.range:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
124 r = kpos[2]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
125 gc.DrawEllipse(self.center[0] - kpos[0] - r, self.center[1] - kpos[1] - r, r * 2, r * 2)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
126
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
127 # Top 'layer' is the mouse-over highlights
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
128 gc.SetPen(wx.Pen(wx.Colour(100, 100, 100, 172), 4))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
129 gc.SetBrush(wx.Brush(wx.Colour(0, 0, 0, 128)))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
130 if self.range is not None and self.direction is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
131 self.highlight(gc, self.range, self.direction)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
132 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
133 gc.SetPen(wx.Pen(self.bgcolor, 0))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
134 gc.SetBrush(wx.Brush(self.bgcolormask))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
135 gc.DrawRectangle(0, 0, w, h)
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
136 self.drawFocusRect(dc)
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
137
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
138 # ------ #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
139 # Events #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
140 # ------ #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
141
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
142 def OnMotion(self, event):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
143 if not self.enabled:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
144 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
145
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
146 oldr, oldd = self.range, self.direction
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
147
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
148 mpos = event.GetPosition()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
149 self.range, self.direction = self.getRangeDir(mpos)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
150
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
151 if oldr != self.range or oldd != self.direction:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
152 self.update()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
153
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
154 def OnLeftDown(self, event):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
155 if not self.enabled:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
156 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
157
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
158 mpos = event.GetPosition()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
159 r, d = self.getRangeDir(mpos)
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
160 if r is not None and r >= 0:
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
161 value = d * self.move_values[r]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
162 if self.moveCallback:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
163 self.lastValue = value
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
164 self.moveCallback(value)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
165
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
166 def OnLeaveWindow(self, evt):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
167 self.range = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
168 self.direction = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
169 self.update()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
170
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
171 class ZButtonsMini(ZButtons):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
172 button_ydistances = [7, 30, 55]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
173 center = (30, 84)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
174 label_overlay_positions = {
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
175 0: (1, 18, 9),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
176 1: (1, 42.8, 12.9),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
177 }
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
178 imagename = "control_z_mini.png"
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
179 move_values = [0.1, 10]

mercurial