Commit d30339af1f527ec883d0d1f295b2af754816d455

Authored by tfmoraes
1 parent 27d086a4

ENH: Added a option to set the padding, the text doesn't desappear anymore

Showing 1 changed file with 49 additions and 18 deletions   Show diff stats
invesalius/gui/widgets/clut_raycasting.py
... ... @@ -37,7 +37,7 @@ class CLUTRaycastingWidget(wx.Panel):
37 37 self.colours = []#plistlib.readPlist(sys.argv[-1])['16bitClutColors']
38 38 self.init = -1024
39 39 self.end = 2000
40   - self.padding = 10
  40 + self.padding = 5
41 41 self.to_render = False
42 42 self.to_draw_points = 0
43 43 self.histogram_pixel_points = [[0,0]]
... ... @@ -60,6 +60,9 @@ class CLUTRaycastingWidget(wx.Panel):
60 60 print "Range", range
61 61 self.CreatePixelArray()
62 62  
  63 + def SetPadding(self, padding):
  64 + self.padding = padding
  65 +
63 66 def DoBind(self):
64 67 self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
65 68 self.Bind(wx.EVT_LEFT_DOWN , self.OnClick)
... ... @@ -181,17 +184,19 @@ class CLUTRaycastingWidget(wx.Panel):
181 184 x = evt.GetX()
182 185 y = evt.GetY()
183 186  
184   - if y > self.GetVirtualSizeTuple()[1] - self.padding:
185   - y = self.GetVirtualSizeTuple()[1] - self.padding
  187 + width, height= self.GetVirtualSizeTuple()
  188 +
  189 + if y >= height - self.padding:
  190 + y = height - self.padding
186 191  
187   - if y <= 0:
188   - y = 0
  192 + if y <= self.padding:
  193 + y = self.padding
189 194  
190 195 if x < 0:
191 196 x = 0
192 197  
193   - if x > self.GetVirtualSizeTuple()[0]:
194   - x = self.GetVirtualSizeTuple()[0]
  198 + if x > width:
  199 + x = width
195 200  
196 201 # A point must be greater than the previous one, but the first one
197 202 if j > 0 and x <= self.pixels_points[i][j-1][0]:
... ... @@ -224,6 +229,7 @@ class CLUTRaycastingWidget(wx.Panel):
224 229  
225 230 def _draw_gradient(self, ctx, height):
226 231 #The gradient
  232 + height += self.padding
227 233 for i, curve in enumerate(self.pixels_points):
228 234 x, y = curve[0]
229 235 xini, yini = curve[0]
... ... @@ -280,18 +286,42 @@ class CLUTRaycastingWidget(wx.Panel):
280 286 x_bearing, y_bearing, width, height, x_advance, y_advance\
281 287 = ctx.text_extents("Value %6d" % value)
282 288  
  289 + widget_width = self.GetVirtualSizeTuple()[0]
  290 +
283 291 fheight = ctx.font_extents()[2]
  292 + y_superior = y - RADIUS * 2 - 2 + y_bearing * 2
  293 + y_inferior = fheight * 2
  294 +
  295 + # The bottom position of the text box mustn't be upper thant the top of
  296 + # the width to always appears in the widget
  297 + if y_superior <= self.padding:
  298 + y_superior = y
  299 + y_text1 = y + height
  300 + y_text2 = y_text1 + 1 + fheight
  301 + else:
  302 + y_text1 = y - RADIUS - 1
  303 + y_text2 = y_text1 - 1 - fheight
  304 +
  305 + x_left = x + RADIUS + 1
  306 + rectangle_width = width + RADIUS + 1
  307 + # The right position of the text box mustn't be in the widget area to
  308 + # always appears in the widget
  309 + if x_left + rectangle_width > widget_width:
  310 + x_left = x - rectangle_width - 1 - RADIUS
  311 + x_text = x_left - 1
  312 + else:
  313 + x_text = x + RADIUS + 1
284 314  
285 315 ctx.set_source_rgba(*BACKGROUND_TEXT_COLOUR_RGBA)
286   - ctx.rectangle(x + RADIUS + 1 + x_bearing, y - RADIUS * 2 - 2 +
287   - y_bearing * 2, width + RADIUS + 1, fheight * 2)
  316 + ctx.rectangle(x_left, y_superior,
  317 + rectangle_width, y_inferior)
288 318 ctx.fill()
289 319  
290 320 ctx.set_source_rgb(1, 1, 1)
291   - ctx.move_to(x + RADIUS + 1, y - RADIUS - 1)
292   - ctx.show_text("Alpha: %.3f" % alpha)
293   - ctx.move_to(x + RADIUS + 1, y - RADIUS - 1 - fheight)
  321 + ctx.move_to(x_text, y_text1)
294 322 ctx.show_text("Value: %6d" % value)
  323 + ctx.move_to(x_text, y_text2)
  324 + ctx.show_text("Alpha: %.3f" % alpha)
295 325  
296 326 def _draw_histogram(self, ctx, height):
297 327 # The histogram
... ... @@ -323,7 +353,7 @@ class CLUTRaycastingWidget(wx.Panel):
323 353 def Render(self, dc):
324 354 ctx = wx.lib.wxcairo.ContextFromDC(dc)
325 355 width, height= self.GetVirtualSizeTuple()
326   - height -= self.padding
  356 + height -= (self.padding * 2)
327 357 width -= self.padding
328 358  
329 359 self._draw_histogram(ctx, height)
... ... @@ -369,12 +399,13 @@ class CLUTRaycastingWidget(wx.Panel):
369 399 """
370 400 Given a Hounsfield point(graylevel, opacity), returns a pixel point in the canvas.
371 401 """
372   - width, height= self.GetVirtualSizeTuple()
  402 + width,height = self.GetVirtualSizeTuple()
373 403 width -= self.padding
374   - height -= self.padding
  404 + height -= (self.padding * 2)
375 405 proportion = width * 1.0 / (self.end - self.init)
376 406 x = (h_pt['x'] - self.init) * proportion
377   - y = height - (h_pt['y'] * height)
  407 + y = height - (h_pt['y'] * height) + self.padding
  408 + print y
378 409 return [x,y]
379 410  
380 411 def PixelToHounsfield(self, i, j):
... ... @@ -383,10 +414,10 @@ class CLUTRaycastingWidget(wx.Panel):
383 414 """
384 415 width, height= self.GetVirtualSizeTuple()
385 416 width -= self.padding
386   - height -= self.padding
  417 + height -= (self.padding * 2)
387 418 proportion = width * 1.0 / (self.end - self.init)
388 419 x = self.pixels_points[i][j][0] / proportion - abs(self.init)
389   - y = (height - self.pixels_points[i][j][1]) * 1.0 / height
  420 + y = (height - self.pixels_points[i][j][1] + self.padding) * 1.0 / height
390 421 self.points[i][j]['x'] = x
391 422 self.points[i][j]['y'] = y
392 423 self.colours[i][j]
... ...