zbuttons.py 5.24 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
        self.bg_bmp = wx.Image(imagefile("control_z.png"), wx.BITMAP_TYPE_PNG).ConvertToBitmap()
37 38 39 40
        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
        BufferedCanvas.__init__(self, parent, ID, size=wx.Size(59, 244))
50 51 52 53 54

        # 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)
55
        self.Bind(wx.EVT_LEAVE_WINDOW, self.OnLeaveWindow)
56

57 58
    def disable(self):
        self.enabled = False
59
        self.update()
60

61 62
    def enable(self):
        self.enabled = True
63
        self.update()
64

65 66 67 68 69 70 71
    def repeatLast(self):
        if self.lastValue:
            self.moveCallback(self.lastValue)

    def clearRepeat(self):
        self.lastValue = None

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

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

84
        fudge = 11
85
        x = 0 + fudge
86
        w = 59 - fudge*2
87
        if rng >= 0:
88 89 90
            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]
91 92
            gc.DrawRoundedRectangle(x, y, w, h, 4)
            # gc.DrawRectangle(x, y, w, h)
93
        # self.drawPartialPie(dc, center, r1-inner_ring_radius, r2-inner_ring_radius, a1+fudge, a2-fudge)
94

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

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

        if self.enabled:
108
            # Draw label overlays
109 110
            gc.SetPen(wx.Pen(wx.Colour(255, 255, 255, 128), 1))
            gc.SetBrush(wx.Brush(wx.Colour(255, 255, 255, 128+64)))
111 112 113 114
            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)
115

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

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

130
    def OnMotion(self, event):
131 132
        if not self.enabled:
            return
133

134 135 136 137 138 139 140 141 142
        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):
143 144 145
        if not self.enabled:
            return

146 147 148 149 150
        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:
151
                self.lastValue = value
152 153
                self.moveCallback(value)

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