Commit 4a771b8c3975c89b840700e8858a7c0053518a86
1 parent
bfb249be
Exists in
master
and in
68 other branches
ENH: A better and cleaner code to draw textbox in the clut raycasting widget
Showing
1 changed file
with
31 additions
and
23 deletions
Show diff stats
invesalius/gui/widgets/clut_raycasting.py
@@ -14,6 +14,7 @@ HISTOGRAM_LINE_WIDTH = 1 | @@ -14,6 +14,7 @@ HISTOGRAM_LINE_WIDTH = 1 | ||
14 | HISTOGRAM_LINE_COLOUR = (0.5, 0.5, 0.5) | 14 | HISTOGRAM_LINE_COLOUR = (0.5, 0.5, 0.5) |
15 | HISTOGRAM_FILL_COLOUR = (0.25, 0.25, 0.25) | 15 | HISTOGRAM_FILL_COLOUR = (0.25, 0.25, 0.25) |
16 | BACKGROUND_TEXT_COLOUR_RGBA = (1, 0, 0, 0.5) | 16 | BACKGROUND_TEXT_COLOUR_RGBA = (1, 0, 0, 0.5) |
17 | +TEXT_COLOUR = (1, 1, 1) | ||
17 | GRADIENT_RGBA = 0.75 | 18 | GRADIENT_RGBA = 0.75 |
18 | RADIUS = 5 | 19 | RADIUS = 5 |
19 | SELECTION_SIZE = 10 | 20 | SELECTION_SIZE = 10 |
@@ -423,43 +424,50 @@ class CLUTRaycastingWidget(wx.Panel): | @@ -423,43 +424,50 @@ class CLUTRaycastingWidget(wx.Panel): | ||
423 | x,y = node.x, node.y | 424 | x,y = node.x, node.y |
424 | value = node.graylevel | 425 | value = node.graylevel |
425 | alpha = node.opacity | 426 | alpha = node.opacity |
426 | - x_bearing, y_bearing, width, height, x_advance, y_advance\ | ||
427 | - = ctx.text_extents("Value %6d" % value) | ||
428 | - | ||
429 | widget_width = self.GetVirtualSizeTuple()[0] | 427 | widget_width = self.GetVirtualSizeTuple()[0] |
430 | 428 | ||
431 | - fheight = ctx.font_extents()[2] | ||
432 | - y_superior = y - RADIUS * 2 - 2 + y_bearing * 2 | ||
433 | - y_inferior = fheight * 2 | 429 | + # To better understand text in cairo, see: |
430 | + # http://www.tortall.net/mu/wiki/CairoTutorial#understanding-text | ||
431 | + if ctx.text_extents("Value %d" % value)[2] > \ | ||
432 | + ctx.text_extents("Alpha: %.3f" % alpha)[2]: | ||
433 | + text = "Value: %6d" % value | ||
434 | + else: | ||
435 | + text = "Alpha: %.3f" % alpha | ||
436 | + | ||
437 | + x_bearing, y_bearing, width, height, x_advance, y_advance\ | ||
438 | + = ctx.text_extents(text) | ||
439 | + fascent, fdescent, fheight, fxadvance, fyadvance = ctx.font_extents() | ||
440 | + | ||
441 | + # The text box height is the double of text height plus 2, that is text | ||
442 | + # box border | ||
443 | + box_height = fheight * 2 + 2 | ||
444 | + box_y = y - RADIUS - 1 - box_height | ||
434 | 445 | ||
435 | # The bottom position of the text box mustn't be upper than the top of | 446 | # The bottom position of the text box mustn't be upper than the top of |
436 | # the width to always appears in the widget | 447 | # the width to always appears in the widget |
437 | - if y_superior <= self.padding: | ||
438 | - y_superior = y | ||
439 | - y_text1 = y + height | ||
440 | - y_text2 = y_text1 + 1 + fheight | ||
441 | - else: | ||
442 | - y_text2 = y - RADIUS - 1 | ||
443 | - y_text1 = y_text2 - 1 - fheight | 448 | + if box_y <= self.padding: |
449 | + box_y = y + RADIUS + 1 | ||
450 | + | ||
451 | + y_text1 = box_y + fascent | ||
452 | + y_text2 = y_text1 + fheight | ||
444 | 453 | ||
445 | x_left = x + RADIUS + 1 | 454 | x_left = x + RADIUS + 1 |
446 | - rectangle_width = width + RADIUS + 1 | 455 | + box_width = width + 2 |
447 | # The right position of the text box mustn't be in the widget area to | 456 | # The right position of the text box mustn't be in the widget area to |
448 | # always appears in the widget | 457 | # always appears in the widget |
449 | - if x_left + rectangle_width > widget_width: | ||
450 | - x_left = x - rectangle_width - 1 - RADIUS | ||
451 | - x_text = x_left - 1 | ||
452 | - else: | ||
453 | - x_text = x + RADIUS + 1 | 458 | + if x_left + box_width > widget_width: |
459 | + x_left = x - box_width - 1 - RADIUS | ||
460 | + | ||
461 | + x_text = x_left + 1 | ||
454 | 462 | ||
455 | ctx.set_source_rgba(*BACKGROUND_TEXT_COLOUR_RGBA) | 463 | ctx.set_source_rgba(*BACKGROUND_TEXT_COLOUR_RGBA) |
456 | - ctx.rectangle(x_left, y_superior, | ||
457 | - rectangle_width, y_inferior) | 464 | + ctx.rectangle(x_left, box_y, |
465 | + box_width, box_height) | ||
458 | ctx.fill() | 466 | ctx.fill() |
459 | 467 | ||
460 | - ctx.set_source_rgb(1, 1, 1) | 468 | + ctx.set_source_rgb(*TEXT_COLOUR) |
461 | ctx.move_to(x_text, y_text1) | 469 | ctx.move_to(x_text, y_text1) |
462 | - ctx.show_text("Value: %6d" % value) | 470 | + ctx.show_text("Value: %d" % value) |
463 | ctx.move_to(x_text, y_text2) | 471 | ctx.move_to(x_text, y_text2) |
464 | ctx.show_text("Alpha: %.3f" % alpha) | 472 | ctx.show_text("Alpha: %.3f" % alpha) |
465 | 473 |