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,7 +37,7 @@ class CLUTRaycastingWidget(wx.Panel):
37 self.colours = []#plistlib.readPlist(sys.argv[-1])['16bitClutColors'] 37 self.colours = []#plistlib.readPlist(sys.argv[-1])['16bitClutColors']
38 self.init = -1024 38 self.init = -1024
39 self.end = 2000 39 self.end = 2000
40 - self.padding = 10 40 + self.padding = 5
41 self.to_render = False 41 self.to_render = False
42 self.to_draw_points = 0 42 self.to_draw_points = 0
43 self.histogram_pixel_points = [[0,0]] 43 self.histogram_pixel_points = [[0,0]]
@@ -60,6 +60,9 @@ class CLUTRaycastingWidget(wx.Panel): @@ -60,6 +60,9 @@ class CLUTRaycastingWidget(wx.Panel):
60 print "Range", range 60 print "Range", range
61 self.CreatePixelArray() 61 self.CreatePixelArray()
62 62
  63 + def SetPadding(self, padding):
  64 + self.padding = padding
  65 +
63 def DoBind(self): 66 def DoBind(self):
64 self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground) 67 self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
65 self.Bind(wx.EVT_LEFT_DOWN , self.OnClick) 68 self.Bind(wx.EVT_LEFT_DOWN , self.OnClick)
@@ -181,17 +184,19 @@ class CLUTRaycastingWidget(wx.Panel): @@ -181,17 +184,19 @@ class CLUTRaycastingWidget(wx.Panel):
181 x = evt.GetX() 184 x = evt.GetX()
182 y = evt.GetY() 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 if x < 0: 195 if x < 0:
191 x = 0 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 # A point must be greater than the previous one, but the first one 201 # A point must be greater than the previous one, but the first one
197 if j > 0 and x <= self.pixels_points[i][j-1][0]: 202 if j > 0 and x <= self.pixels_points[i][j-1][0]:
@@ -224,6 +229,7 @@ class CLUTRaycastingWidget(wx.Panel): @@ -224,6 +229,7 @@ class CLUTRaycastingWidget(wx.Panel):
224 229
225 def _draw_gradient(self, ctx, height): 230 def _draw_gradient(self, ctx, height):
226 #The gradient 231 #The gradient
  232 + height += self.padding
227 for i, curve in enumerate(self.pixels_points): 233 for i, curve in enumerate(self.pixels_points):
228 x, y = curve[0] 234 x, y = curve[0]
229 xini, yini = curve[0] 235 xini, yini = curve[0]
@@ -280,18 +286,42 @@ class CLUTRaycastingWidget(wx.Panel): @@ -280,18 +286,42 @@ class CLUTRaycastingWidget(wx.Panel):
280 x_bearing, y_bearing, width, height, x_advance, y_advance\ 286 x_bearing, y_bearing, width, height, x_advance, y_advance\
281 = ctx.text_extents("Value %6d" % value) 287 = ctx.text_extents("Value %6d" % value)
282 288
  289 + widget_width = self.GetVirtualSizeTuple()[0]
  290 +
283 fheight = ctx.font_extents()[2] 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 ctx.set_source_rgba(*BACKGROUND_TEXT_COLOUR_RGBA) 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 ctx.fill() 318 ctx.fill()
289 319
290 ctx.set_source_rgb(1, 1, 1) 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 ctx.show_text("Value: %6d" % value) 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 def _draw_histogram(self, ctx, height): 326 def _draw_histogram(self, ctx, height):
297 # The histogram 327 # The histogram
@@ -323,7 +353,7 @@ class CLUTRaycastingWidget(wx.Panel): @@ -323,7 +353,7 @@ class CLUTRaycastingWidget(wx.Panel):
323 def Render(self, dc): 353 def Render(self, dc):
324 ctx = wx.lib.wxcairo.ContextFromDC(dc) 354 ctx = wx.lib.wxcairo.ContextFromDC(dc)
325 width, height= self.GetVirtualSizeTuple() 355 width, height= self.GetVirtualSizeTuple()
326 - height -= self.padding 356 + height -= (self.padding * 2)
327 width -= self.padding 357 width -= self.padding
328 358
329 self._draw_histogram(ctx, height) 359 self._draw_histogram(ctx, height)
@@ -369,12 +399,13 @@ class CLUTRaycastingWidget(wx.Panel): @@ -369,12 +399,13 @@ class CLUTRaycastingWidget(wx.Panel):
369 """ 399 """
370 Given a Hounsfield point(graylevel, opacity), returns a pixel point in the canvas. 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 width -= self.padding 403 width -= self.padding
374 - height -= self.padding 404 + height -= (self.padding * 2)
375 proportion = width * 1.0 / (self.end - self.init) 405 proportion = width * 1.0 / (self.end - self.init)
376 x = (h_pt['x'] - self.init) * proportion 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 return [x,y] 409 return [x,y]
379 410
380 def PixelToHounsfield(self, i, j): 411 def PixelToHounsfield(self, i, j):
@@ -383,10 +414,10 @@ class CLUTRaycastingWidget(wx.Panel): @@ -383,10 +414,10 @@ class CLUTRaycastingWidget(wx.Panel):
383 """ 414 """
384 width, height= self.GetVirtualSizeTuple() 415 width, height= self.GetVirtualSizeTuple()
385 width -= self.padding 416 width -= self.padding
386 - height -= self.padding 417 + height -= (self.padding * 2)
387 proportion = width * 1.0 / (self.end - self.init) 418 proportion = width * 1.0 / (self.end - self.init)
388 x = self.pixels_points[i][j][0] / proportion - abs(self.init) 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 self.points[i][j]['x'] = x 421 self.points[i][j]['x'] = x
391 self.points[i][j]['y'] = y 422 self.points[i][j]['y'] = y
392 self.colours[i][j] 423 self.colours[i][j]