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