diff --git a/invesalius/data/measures.py b/invesalius/data/measures.py index a7252c1..d256d76 100644 --- a/invesalius/data/measures.py +++ b/invesalius/data/measures.py @@ -584,8 +584,16 @@ class LinearMeasure(object): r, g, b = self.colour canvas.draw_line(p0, p1, colour=(r*255, g*255, b*255, 255)) + v = np.array(p1) - np.array(p0) + t = np.linalg.norm(v) + nv = v / t + m = (np.array(p1) + np.array(p0)) / 2.0 + angle = np.arctan2(v[1], v[0]) txt = u"%.3f mm" % self.GetValue() - canvas.draw_text_box(txt, ((points[0][0]+points[1][0])/2.0, (points[0][1]+points[1][1])/2.0), txt_colour=MEASURE_TEXT_COLOUR, bg_colour=MEASURE_TEXTBOX_COLOUR) + w, h = canvas.calc_text_size(txt) + d = t/2.0 - w/2.0 + p = np.array(p0) + nv*d + canvas.draw_text_box(txt, p, angle=angle, txt_colour=MEASURE_TEXT_COLOUR, bg_colour=MEASURE_TEXTBOX_COLOUR) def GetNumberOfPoints(self): return len(self.points) diff --git a/invesalius/data/viewer_slice.py b/invesalius/data/viewer_slice.py index 3bfaf21..e9948bf 100755 --- a/invesalius/data/viewer_slice.py +++ b/invesalius/data/viewer_slice.py @@ -217,7 +217,7 @@ class CanvasRendererCTX: self.image.SetAlphaBuffer(self.alpha) self.image.Clear() gc = wx.GraphicsContext.Create(self.image) - gc.SetAntialiasMode(0) + gc.SetAntialiasMode(1) self.gc = gc @@ -389,7 +389,7 @@ class CanvasRendererCTX: px, py = pos gc.DrawText(text, px, py) - def draw_text_box(self, text, pos, font=None, txt_colour=(255, 255, 255), bg_colour=(128, 128, 128, 128), border=5): + def draw_text_box(self, text, pos, angle=0.0, font=None, txt_colour=(255, 255, 255), bg_colour=(128, 128, 128, 128), border=5): """ Draw text inside a text box. @@ -415,13 +415,19 @@ class CanvasRendererCTX: px, py = pos py = -py + gc.PushState() + gc.Translate(px, py) + gc.Rotate(-angle) + # Drawing the box cw, ch = w + border * 2, h + border * 2 - self.draw_rectangle((px, py), cw, ch, bg_colour, bg_colour) + self.draw_rectangle((0, 0), cw, ch, bg_colour, bg_colour) # Drawing the text tpx, tpy = px + border, py + border - self.draw_text(text, (tpx, tpy), font, txt_colour) + self.draw_text(text, (border, border), font, txt_colour) + + gc.PopState() def draw_arc(self, center, p0, p1, line_colour=(255, 0, 0, 128), width=2): """ @@ -465,6 +471,19 @@ class CanvasRendererCTX: path.AddArc((c[0], c[1]), min(s0, s1), sa, ea) gc.StrokePath(path) + def calc_text_size(self, text, font=None): + if self.gc is None: + return None + gc = self.gc + + if font is None: + font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT) + + _font = gc.CreateFont(font) + gc.SetFont(_font) + w, h = gc.GetTextExtent(text) + return w, h + class Viewer(wx.Panel): -- libgit2 0.21.2