printrun-src/printrun/gui/zbuttons.py

Sat, 04 Jun 2016 09:22:51 +0200

author
mbayer
date
Sat, 04 Jun 2016 09:22:51 +0200
changeset 20
03b34402d405
parent 15
0bbb006204fc
child 46
cce0af6351f0
permissions
-rw-r--r--

Code cleanup

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
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
17 from .bufferedcanvas import BufferedCanvas
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
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
25 class ZButtons(BufferedCanvas):
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()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
47 self.bgcolor.SetFromName(bgcolor)
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
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
50 BufferedCanvas.__init__(self, parent, ID, size=self.bg_bmp.GetSize())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
51
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
52 # Set up mouse and keyboard event capture
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
53 self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
54 self.Bind(wx.EVT_LEFT_DCLICK, self.OnLeftDown)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
55 self.Bind(wx.EVT_MOTION, self.OnMotion)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
56 self.Bind(wx.EVT_LEAVE_WINDOW, self.OnLeaveWindow)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
57
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
58 def disable(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
59 self.enabled = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
60 self.update()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
61
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
62 def enable(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
63 self.enabled = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
64 self.update()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
65
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
66 def repeatLast(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
67 if self.lastValue:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
68 self.moveCallback(self.lastValue)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
69
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
70 def clearRepeat(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
71 self.lastValue = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
72
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
73 def lookupRange(self, ydist):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
74 idx = -1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
75 for d in self.button_ydistances:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
76 if ydist < d:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
77 return idx
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
78 idx += 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
79 return None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
80
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
81 def highlight(self, gc, rng, dir):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
82 assert(rng >= -1 and rng <= 3)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
83 assert(dir >= -1 and dir <= 1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
84
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
85 fudge = 11
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
86 x = 0 + fudge
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
87 w = 59 - fudge * 2
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
88 if rng >= 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
89 k = 1 if dir > 0 else 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
90 y = self.center[1] - (dir * self.button_ydistances[rng + k])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
91 h = self.button_ydistances[rng + 1] - self.button_ydistances[rng]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
92 gc.DrawRoundedRectangle(x, y, w, h, 4)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
93 # gc.DrawRectangle(x, y, w, h)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
94 # 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
95
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
96 def getRangeDir(self, pos):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
97 ydelta = self.center[1] - pos[1]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
98 return (self.lookupRange(abs(ydelta)), sign(ydelta))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
99
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
100 def draw(self, dc, w, h):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
101 dc.SetBackground(wx.Brush(self.bgcolor))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
102 dc.Clear()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
103 gc = wx.GraphicsContext.Create(dc)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
104 if self.bg_bmp:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
105 w, h = (self.bg_bmp.GetWidth(), self.bg_bmp.GetHeight())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
106 gc.DrawBitmap(self.bg_bmp, 0, 0, w, h)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
107
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
108 if self.enabled and self.IsEnabled():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
109 # Draw label overlays
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
110 gc.SetPen(wx.Pen(wx.Colour(255, 255, 255, 128), 1))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
111 gc.SetBrush(wx.Brush(wx.Colour(255, 255, 255, 128 + 64)))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
112 for idx, kpos in self.label_overlay_positions.items():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
113 if idx != self.range:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
114 r = kpos[2]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
115 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
116
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
117 # Top 'layer' is the mouse-over highlights
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
118 gc.SetPen(wx.Pen(wx.Colour(100, 100, 100, 172), 4))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
119 gc.SetBrush(wx.Brush(wx.Colour(0, 0, 0, 128)))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
120 if self.range is not None and self.direction is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
121 self.highlight(gc, self.range, self.direction)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
122 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
123 gc.SetPen(wx.Pen(self.bgcolor, 0))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
124 gc.SetBrush(wx.Brush(self.bgcolormask))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
125 gc.DrawRectangle(0, 0, w, h)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
126
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
127 # ------ #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
128 # Events #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
129 # ------ #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
130
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
131 def OnMotion(self, event):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
132 if not self.enabled:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
133 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
134
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
135 oldr, oldd = self.range, self.direction
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
136
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
137 mpos = event.GetPosition()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
138 self.range, self.direction = self.getRangeDir(mpos)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
139
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
140 if oldr != self.range or oldd != self.direction:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
141 self.update()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
142
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
143 def OnLeftDown(self, event):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
144 if not self.enabled:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
145 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
146
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
147 mpos = event.GetPosition()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
148 r, d = self.getRangeDir(mpos)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
149 if r >= 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
150 value = d * self.move_values[r]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
151 if self.moveCallback:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
152 self.lastValue = value
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
153 self.moveCallback(value)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
154
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
155 def OnLeaveWindow(self, evt):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
156 self.range = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
157 self.direction = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
158 self.update()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
159
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
160 class ZButtonsMini(ZButtons):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
161 button_ydistances = [7, 30, 55]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
162 center = (30, 84)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
163 label_overlay_positions = {
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
164 0: (1, 18, 9),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
165 1: (1, 42.8, 12.9),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
166 }
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
167 imagename = "control_z_mini.png"
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
168 move_values = [0.1, 10]

mercurial