Commit 479b67693cfeca8809d7ee32463a13a29446e419

Authored by tfmoraes
1 parent 83242a06

ENH: changing the mouse cursor when over push buttons

Showing 1 changed file with 45 additions and 7 deletions   Show diff stats
invesalius/gui/widgets/gradient.py
... ... @@ -43,7 +43,15 @@ class SliderEvent(wx.PyCommandEvent):
43 43 self.maximun = maxValue
44 44  
45 45 class GradientSlider(wx.Panel):
  46 + # This widget is formed by a gradient background (black-white), two push
  47 + # buttons change the min and max values respectively and a slider which you can drag to
  48 + # change the both min and max values.
46 49 def __init__(self, parent, id, minRange, maxRange, minValue, maxValue, colour):
  50 + # minRange: the minimal value
  51 + # maxrange: the maximum value
  52 + # minValue: the least value in the range
  53 + # maxValue: the most value in the range
  54 + # colour: colour used in this widget.
47 55 super(GradientSlider, self).__init__(parent, id, size = (100, 25))
48 56 self._bind_events_wx()
49 57  
... ... @@ -65,6 +73,7 @@ class GradientSlider(wx.Panel):
65 73 self.Bind(wx.EVT_SIZE, self.OnSize)
66 74  
67 75 def OnPaint(self, evt):
  76 + # Where the magic happens. Here the controls are drawn.
68 77 dc = wx.BufferedPaintDC(self)
69 78 dc.Clear()
70 79  
... ... @@ -79,26 +88,32 @@ class GradientSlider(wx.Panel):
79 88  
80 89 width_transparency = self.max_position - self.min_position
81 90  
  91 + # Drawing the left blank area.
82 92 pen = wx.Pen((0, 0, 0))
83 93 brush = wx.Brush((0, 0, 0))
84 94 dc.SetPen(pen)
85 95 dc.SetBrush(brush)
86 96 dc.DrawRectangle(0, 0, PUSH_WIDTH, h)
87 97  
  98 + # Drawing the right blank area.
88 99 pen = wx.Pen((255, 255, 255))
89 100 brush = wx.Brush((255, 255, 255))
90 101 dc.SetPen(pen)
91 102 dc.SetBrush(brush)
92 103 dc.DrawRectangle(x_init_gradient + width_gradient, 0, PUSH_WIDTH, h)
93 104  
  105 + # Drawing the gradient.
94 106 dc.GradientFillLinear((x_init_gradient, y_init_gradient,
95 107 width_gradient, height_gradient),
96 108 (0, 0, 0), (255,255, 255))
97 109  
98 110 n = wx.RendererNative_Get()
  111 +
  112 + # Drawing the push buttons
99 113 n.DrawPushButton(self, dc, (x_init_push1, 0, PUSH_WIDTH, h))
100 114 n.DrawPushButton(self, dc, (x_init_push2, 0, PUSH_WIDTH, h))
101 115  
  116 + # Drawing the transparent slider.
102 117 bytes = numpy.array(self.colour * width_transparency * h, 'B')
103 118 try:
104 119 slider = wx.BitmapFromBufferRGBA(width_transparency, h, bytes)
... ... @@ -108,12 +123,23 @@ class GradientSlider(wx.Panel):
108 123 dc.DrawBitmap(slider, self.min_position, 0, True)
109 124  
110 125 def OnEraseBackGround(self, evt):
  126 + # Only to avoid this widget to flick.
111 127 pass
112 128  
113 129 def OnMotion(self, evt):
114 130 x = evt.GetX()
115 131 w, h = self.GetSize()
116   - if self.selected == 1:
  132 +
  133 + # user is only moving the mouse, not changing the max our min values
  134 + if not self.selected:
  135 + # The user is over a push button, change the cursor.
  136 + if self._is_over_what(x) in (1, 2):
  137 + self.SetCursor(wx.StockCursor(wx.CURSOR_SIZEWE))
  138 + else:
  139 + self.SetCursor(wx.NullCursor)
  140 +
  141 + # The user is moving the first PUSH (Min)
  142 + elif self.selected == 1:
117 143 x -= self._delta
118 144 if x - PUSH_WIDTH < 0:
119 145 x = PUSH_WIDTH
... ... @@ -126,6 +152,7 @@ class GradientSlider(wx.Panel):
126 152 self.Refresh()
127 153 self._generate_event()
128 154  
  155 + # The user is moving the second push (Max)
129 156 elif self.selected == 2:
130 157 x -= self._delta
131 158 if x + PUSH_WIDTH > w:
... ... @@ -139,6 +166,7 @@ class GradientSlider(wx.Panel):
139 166 self.Refresh()
140 167 self._generate_event()
141 168  
  169 + # The user is moving the slide.
142 170 elif self.selected == 3:
143 171 x -= self._delta
144 172 slider_size = self.max_position - self.min_position
... ... @@ -169,14 +197,12 @@ class GradientSlider(wx.Panel):
169 197  
170 198 def OnClick(self, evt):
171 199 x = evt.GetX()
172   - if self.min_position - PUSH_WIDTH <= x <= self.min_position:
173   - self.selected = 1
  200 + self.selected = self._is_over_what(x)
  201 + if self.selected == 1:
174 202 self._delta = x - self.min_position
175   - elif self.max_position <= x <= self.max_position + PUSH_WIDTH:
176   - self.selected = 2
  203 + elif self.selected == 2:
177 204 self._delta = x - self.max_position
178   - elif self.min_position <= x <= self.max_position:
179   - self.selected = 3
  205 + elif self.selected == 3:
180 206 self._delta = x - self.min_position
181 207 evt.Skip()
182 208  
... ... @@ -227,6 +253,18 @@ class GradientSlider(wx.Panel):
227 253  
228 254 return minimun
229 255  
  256 + def _is_over_what(self, position_x):
  257 + # Test if the given position (x) is over some object. Return 1 to first
  258 + # pus, 2 to second push, 3 to slide and 0 to nothing.
  259 + if self.min_position - PUSH_WIDTH <= position_x <= self.min_position:
  260 + return 1
  261 + elif self.max_position <= position_x <= self.max_position + PUSH_WIDTH:
  262 + return 2
  263 + elif self.min_position <= position_x <= self.max_position:
  264 + return 3
  265 + else:
  266 + return 0
  267 +
230 268 def SetColour(self, colour):
231 269 self.colour = colour
232 270  
... ...