zbuttons.py 5.25 KB
Newer Older
1
# This file is part of the Printrun suite.
2
#
3 4 5 6
# Printrun is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
7
#
8 9 10 11
# Printrun is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
12
#
13 14 15
# You should have received a copy of the GNU General Public License
# along with Printrun.  If not, see <http://www.gnu.org/licenses/>.

16 17
import wx, os, math
from bufferedcanvas import *
18
from printrun_utils import *
19 20 21 22 23 24 25

def sign(n):
    if n < 0: return -1
    elif n > 0: return 1
    else: return 0

class ZButtons(BufferedCanvas):
26
    button_ydistances = [7, 30, 55, 83] # ,112
27
    center = (30, 118)
28 29 30 31 32 33
    label_overlay_positions = {
        0: (1, 18, 11),
        1: (1, 41, 13),
        2: (1, 67, 15),
        3: None
    }
34

35
    def __init__(self, parent, moveCallback = None, bgcolor = "#FFFFFF", ID=-1):
36 37 38 39 40
        self.bg_bmp = wx.Image(imagefile("control_z.png"),wx.BITMAP_TYPE_PNG).ConvertToBitmap()
        self.range = None
        self.direction = None
        self.orderOfMagnitudeIdx = 0 # 0 means '1', 1 means '10', 2 means '100', etc.
        self.moveCallback = moveCallback
41
        self.enabled = False
42 43
        # Remember the last clicked value, so we can repeat when spacebar pressed
        self.lastValue = None
44

45 46 47
        self.bgcolor = wx.Colour()
        self.bgcolor.SetFromName(bgcolor)
        self.bgcolormask = wx.Colour(self.bgcolor.Red(), self.bgcolor.Green(), self.bgcolor.Blue(), 128)
48

49 50
        BufferedCanvas.__init__(self, parent, ID)

51
        self.SetSize(wx.Size(59, 244))
52 53 54 55 56

        # Set up mouse and keyboard event capture
        self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
        self.Bind(wx.EVT_LEFT_DCLICK, self.OnLeftDown)
        self.Bind(wx.EVT_MOTION, self.OnMotion)
57
        self.Bind(wx.EVT_LEAVE_WINDOW, self.OnLeaveWindow)
58

59 60
    def disable(self):
        self.enabled = False
61
        self.update()
62

63 64
    def enable(self):
        self.enabled = True
65
        self.update()
66

67 68 69 70 71 72 73
    def repeatLast(self):
        if self.lastValue:
            self.moveCallback(self.lastValue)

    def clearRepeat(self):
        self.lastValue = None

74 75 76 77 78 79 80
    def lookupRange(self, ydist):
        idx = -1
        for d in ZButtons.button_ydistances:
            if ydist < d:
                return idx
            idx += 1
        return None
81

82
    def highlight(self, gc, rng, dir):
83
        assert(rng >= -1 and rng <= 3)
84 85
        assert(dir >= -1 and dir <= 1)

86
        fudge = 11
87
        x = 0 + fudge
88
        w = 59 - fudge*2
89
        if rng >= 0:
90 91 92
            k = 1 if dir > 0 else 0
            y = ZButtons.center[1] - (dir * ZButtons.button_ydistances[rng+k])
            h = ZButtons.button_ydistances[rng+1] - ZButtons.button_ydistances[rng]
93 94
            gc.DrawRoundedRectangle(x, y, w, h, 4)
            # gc.DrawRectangle(x, y, w, h)
95
        # self.drawPartialPie(dc, center, r1-inner_ring_radius, r2-inner_ring_radius, a1+fudge, a2-fudge)
96

97 98 99 100
    def getRangeDir(self, pos):
        ydelta = ZButtons.center[1] - pos[1]
        return (self.lookupRange(abs(ydelta)), sign(ydelta))

101
    def draw(self, dc, w, h):
102
        dc.SetBackground(wx.Brush(self.bgcolor))
103 104
        dc.Clear()
        gc = wx.GraphicsContext.Create(dc)
105 106 107
        if self.bg_bmp:
            w, h = (self.bg_bmp.GetWidth(), self.bg_bmp.GetHeight())
            gc.DrawBitmap(self.bg_bmp, 0, 0, w, h)
108 109

        if self.enabled:
110
            # Draw label overlays
111 112
            gc.SetPen(wx.Pen(wx.Colour(255, 255, 255, 128), 1))
            gc.SetBrush(wx.Brush(wx.Colour(255, 255, 255, 128+64)))
113 114 115 116
            for idx, kpos in ZButtons.label_overlay_positions.items():
                if kpos and idx != self.range:
                    r = kpos[2]
                    gc.DrawEllipse(ZButtons.center[0]-kpos[0]-r, ZButtons.center[1]-kpos[1]-r, r*2, r*2)
117

118
            # Top 'layer' is the mouse-over highlights
119 120
            gc.SetPen(wx.Pen(wx.Colour(100, 100, 100, 172), 4))
            gc.SetBrush(wx.Brush(wx.Colour(0, 0,0, 128)))
121 122 123
            if self.range != None and self.direction != None:
                self.highlight(gc, self.range, self.direction)
        else:
124 125
            gc.SetPen(wx.Pen(self.bgcolor, 0))
            gc.SetBrush(wx.Brush(self.bgcolormask))
126 127 128 129 130 131
            gc.DrawRectangle(0, 0, w, h)

    ## ------ ##
    ## Events ##
    ## ------ ##

132
    def OnMotion(self, event):
133 134
        if not self.enabled:
            return
135

136 137 138 139 140 141 142 143 144
        oldr, oldd = self.range, self.direction

        mpos = event.GetPosition()
        self.range, self.direction = self.getRangeDir(mpos)

        if oldr != self.range or oldd != self.direction:
            self.update()

    def OnLeftDown(self, event):
145 146 147
        if not self.enabled:
            return

148 149 150 151 152
        mpos = event.GetPosition()
        r, d = self.getRangeDir(mpos)
        if r >= 0:
            value = math.pow(10, self.orderOfMagnitudeIdx) * math.pow(10, r - 1) * d
            if self.moveCallback:
153
                self.lastValue = value
154 155
                self.moveCallback(value)

156 157 158 159
    def OnLeaveWindow(self, evt):
        self.range = None
        self.direction = None
        self.update()