Commit 90e1222dc6e5d548fc9e3d0f6c9757ef3ba3d105
1 parent
849fd417
Exists in
master
and in
26 other branches
Created a method to draw text inside a box
Showing
2 changed files
with
51 additions
and
10 deletions
Show diff stats
invesalius/data/measures.py
| ... | ... | @@ -561,19 +561,19 @@ class LinearMeasure(object): |
| 561 | 561 | a.GetProperty().SetOpacity(0.75) |
| 562 | 562 | self.text_actor = a |
| 563 | 563 | |
| 564 | - def draw_to_canvas(self, gc, viewer): | |
| 564 | + def draw_to_canvas(self, gc, canvas): | |
| 565 | 565 | """ |
| 566 | 566 | Draws to an wx.GraphicsContext. |
| 567 | 567 | |
| 568 | 568 | Parameters: |
| 569 | 569 | gc: is a wx.GraphicsContext |
| 570 | - viewer: the viewer where the canvas is in. | |
| 570 | + canvas: the canvas it's being drawn. | |
| 571 | 571 | """ |
| 572 | 572 | coord = vtk.vtkCoordinate() |
| 573 | 573 | lines = [] |
| 574 | 574 | for p in self.points: |
| 575 | 575 | coord.SetValue(p) |
| 576 | - cx, cy = coord.GetComputedDisplayValue(viewer.slice_data.renderer) | |
| 576 | + cx, cy = coord.GetComputedDisplayValue(canvas.viewer.slice_data.renderer) | |
| 577 | 577 | cy = -cy |
| 578 | 578 | lines.append((cx, cy)) |
| 579 | 579 | path = gc.CreatePath() |
| ... | ... | @@ -590,7 +590,8 @@ class LinearMeasure(object): |
| 590 | 590 | gc.StrokePath(path) |
| 591 | 591 | |
| 592 | 592 | txt = u"%.3f mm" % self.GetValue() |
| 593 | - gc.DrawText(txt, *lines[0]) | |
| 593 | + # gc.DrawText(txt, *lines[0]) | |
| 594 | + canvas.draw_text_box(txt, (lines[0][0], -lines[0][1])) | |
| 594 | 595 | |
| 595 | 596 | def GetNumberOfPoints(self): |
| 596 | 597 | return len(self.points) |
| ... | ... | @@ -827,20 +828,20 @@ class AngularMeasure(object): |
| 827 | 828 | a.GetPositionCoordinate().SetValue(x,y,z) |
| 828 | 829 | self.text_actor = a |
| 829 | 830 | |
| 830 | - def draw_to_canvas(self, gc, viewer): | |
| 831 | + def draw_to_canvas(self, gc, canvas): | |
| 831 | 832 | """ |
| 832 | 833 | Draws to an wx.GraphicsContext. |
| 833 | 834 | |
| 834 | 835 | Parameters: |
| 835 | 836 | gc: is a wx.GraphicsContext |
| 836 | - viewer: the viewer where the canvas is in. | |
| 837 | + canvas: the canvas it's being drawn. | |
| 837 | 838 | """ |
| 838 | 839 | coord = vtk.vtkCoordinate() |
| 839 | 840 | lines = [] |
| 840 | 841 | for p in self.points: |
| 841 | 842 | print p |
| 842 | 843 | coord.SetValue(p) |
| 843 | - cx, cy = coord.GetComputedDisplayValue(viewer.slice_data.renderer) | |
| 844 | + cx, cy = coord.GetComputedDisplayValue(canvas.viewer.slice_data.renderer) | |
| 844 | 845 | cy = -cy |
| 845 | 846 | lines.append((cx, cy)) |
| 846 | 847 | path = gc.CreatePath() |
| ... | ... | @@ -858,7 +859,6 @@ class AngularMeasure(object): |
| 858 | 859 | |
| 859 | 860 | if len(lines) == 3: |
| 860 | 861 | txt = u"%.3f° / %.3f°" % (self.GetValue(), 360.0 - self.GetValue()) |
| 861 | - gc.DrawText(txt, *lines[0]) | |
| 862 | 862 | |
| 863 | 863 | path = gc.CreatePath() |
| 864 | 864 | |
| ... | ... | @@ -882,7 +882,8 @@ class AngularMeasure(object): |
| 882 | 882 | path.AddArc((c[0], c[1]), min(s0, s1), sa, ea) |
| 883 | 883 | gc.StrokePath(path) |
| 884 | 884 | |
| 885 | - gc.DrawText(txt, *lines[0]) | |
| 885 | + # gc.DrawText(txt, *lines[0]) | |
| 886 | + canvas.draw_text_box(txt, (lines[0][0], -lines[0][1])) | |
| 886 | 887 | |
| 887 | 888 | def GetNumberOfPoints(self): |
| 888 | 889 | return self.number_of_points | ... | ... |
invesalius/data/viewer_slice.py
| ... | ... | @@ -151,6 +151,7 @@ class CanvasRendererCTX: |
| 151 | 151 | self.viewer = viewer |
| 152 | 152 | self.canvas_renderer = viewer.slice_data.canvas_renderer |
| 153 | 153 | self._size = self.canvas_renderer.GetSize() |
| 154 | + self.gc = None | |
| 154 | 155 | self._init_canvas() |
| 155 | 156 | viewer.slice_data.renderer.AddObserver("StartEvent", self.OnPaint) |
| 156 | 157 | |
| ... | ... | @@ -205,6 +206,8 @@ class CanvasRendererCTX: |
| 205 | 206 | gc = wx.GraphicsContext.Create(self.image) |
| 206 | 207 | gc.SetAntialiasMode(0) |
| 207 | 208 | |
| 209 | + self.gc = gc | |
| 210 | + | |
| 208 | 211 | font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT) |
| 209 | 212 | # font.SetWeight(wx.BOLD) |
| 210 | 213 | font = gc.CreateFont(font, (0, 0, 255)) |
| ... | ... | @@ -219,15 +222,52 @@ class CanvasRendererCTX: |
| 219 | 222 | for (m, mr) in self.viewer.measures.get(self.viewer.orientation, self.viewer.slice_data.number): |
| 220 | 223 | if not m.visible: |
| 221 | 224 | continue |
| 222 | - mr.draw_to_canvas(gc, self.viewer) | |
| 225 | + mr.draw_to_canvas(gc, self) | |
| 223 | 226 | |
| 224 | 227 | gc.Destroy() |
| 225 | 228 | |
| 229 | + self.gc = None | |
| 230 | + | |
| 226 | 231 | self.bitmap = self.image.ConvertToBitmap() |
| 227 | 232 | self.bitmap.CopyToBuffer(self._array, wx.BitmapBufferFormat_RGBA) |
| 228 | 233 | |
| 229 | 234 | self._cv_image.Modified() |
| 230 | 235 | |
| 236 | + def draw_text_box(self, text, pos, font=None, txt_colour=(0.0, 0.0, 0.0), bg_colour=(128, 0, 0, 128), border=5): | |
| 237 | + """ | |
| 238 | + Draw text inside a text box. | |
| 239 | + | |
| 240 | + Params: | |
| 241 | + text: an unicode text. | |
| 242 | + pos: (x, y) position. | |
| 243 | + font: if None it'll use the default gui font. | |
| 244 | + txt_colour: RGB text colour | |
| 245 | + bg_colour: RGBA box colour | |
| 246 | + border: the border size. | |
| 247 | + """ | |
| 248 | + if self.gc is None: | |
| 249 | + return None | |
| 250 | + gc = self.gc | |
| 251 | + | |
| 252 | + if font is None: | |
| 253 | + font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT) | |
| 254 | + | |
| 255 | + font = gc.CreateFont(font, txt_colour) | |
| 256 | + gc.SetFont(font) | |
| 257 | + w, h = gc.GetTextExtent(text) | |
| 258 | + | |
| 259 | + px, py = pos | |
| 260 | + py = -py | |
| 261 | + | |
| 262 | + # Drawing the box | |
| 263 | + cw, ch = w + border * 2, h + border * 2 | |
| 264 | + gc.SetBrush(wx.Brush(bg_colour)) | |
| 265 | + gc.SetPen(wx.Pen(bg_colour)) | |
| 266 | + gc.DrawRectangle(px, py, cw, ch) | |
| 267 | + | |
| 268 | + tpx, tpy = px + border, py + border | |
| 269 | + gc.DrawText(text, tpx, tpy) | |
| 270 | + | |
| 231 | 271 | |
| 232 | 272 | class Viewer(wx.Panel): |
| 233 | 273 | ... | ... |