Commit 9435ad91e2c0ec5099abba2ec77051f64d13d125

Authored by tfmoraes
1 parent f240a926

ENH: Using methods from project to add and remove measures

invesalius/data/measures.py
@@ -8,6 +8,7 @@ import wx.lib.pubsub as ps @@ -8,6 +8,7 @@ import wx.lib.pubsub as ps
8 import vtk 8 import vtk
9 9
10 import constants as const 10 import constants as const
  11 +import project as prj
11 12
12 TYPE = {const.LINEAR: _(u"Linear"), 13 TYPE = {const.LINEAR: _(u"Linear"),
13 const.ANGULAR: _(u"Angular"), 14 const.ANGULAR: _(u"Angular"),
@@ -31,6 +32,8 @@ class MeasurementManager(object): @@ -31,6 +32,8 @@ class MeasurementManager(object):
31 32
32 def _bind_events(self): 33 def _bind_events(self):
33 ps.Publisher().subscribe(self._add_point, "Add measurement point") 34 ps.Publisher().subscribe(self._add_point, "Add measurement point")
  35 + ps.Publisher().subscribe(self._change_name, "Change measurement name")
  36 + ps.Publisher().subscribe(self._remove_measurements, "Remove measurements")
34 ps.Publisher().subscribe(self._set_visibility, "Show measurement") 37 ps.Publisher().subscribe(self._set_visibility, "Show measurement")
35 38
36 def _add_point(self, pubsub_evt): 39 def _add_point(self, pubsub_evt):
@@ -53,6 +56,7 @@ class MeasurementManager(object): @@ -53,6 +56,7 @@ class MeasurementManager(object):
53 56
54 if to_create: 57 if to_create:
55 m = Measurement() 58 m = Measurement()
  59 + m.index = len(self.measures)
56 m.location = location 60 m.location = location
57 m.points.append(position) 61 m.points.append(position)
58 m.slice_number = slice_number 62 m.slice_number = slice_number
@@ -64,11 +68,12 @@ class MeasurementManager(object): @@ -64,11 +68,12 @@ class MeasurementManager(object):
64 68
65 x, y, z = position 69 x, y, z = position
66 actors = self.current[1].AddPoint(x, y, z) 70 actors = self.current[1].AddPoint(x, y, z)
67 - ps.Publisher().sendMessage(("Add Actors", location), actors) 71 + ps.Publisher().sendMessage(("Add actors", location), actors)
68 72
69 if self.current[1].IsComplete(): 73 if self.current[1].IsComplete():
  74 + index = prj.Project().AddMeasurement(self.current[0])
  75 + self.current[0].index = index
70 self.measures.append(self.current) 76 self.measures.append(self.current)
71 - index = self.current[0].index  
72 name = self.current[0].name 77 name = self.current[0].name
73 colour = self.current[0].colour 78 colour = self.current[0].colour
74 self.current[0].value = self.current[1].GetValue() 79 self.current[0].value = self.current[1].GetValue()
@@ -83,11 +88,26 @@ class MeasurementManager(object): @@ -83,11 +88,26 @@ class MeasurementManager(object):
83 value)) 88 value))
84 self.current = None 89 self.current = None
85 90
  91 + def _change_name(self, pubsub_evt):
  92 + index, new_name = pubsub_evt.data
  93 + self.measures[index][0].name = new_name
  94 +
  95 + def _remove_measurements(self, pubsub_evt):
  96 + indexes = pubsub_evt.data
  97 + print indexes
  98 + for index in indexes:
  99 + m, mr = self.measures.pop(index)
  100 + actors = mr.GetActors()
  101 + prj.Project().RemoveMeasurement(m)
  102 + ps.Publisher().sendMessage(('Remove actors', m.location), actors)
  103 + ps.Publisher().sendMessage('Update slice viewer')
  104 +
86 def _set_visibility(self, pubsub_evt): 105 def _set_visibility(self, pubsub_evt):
87 index, visibility = pubsub_evt.data 106 index, visibility = pubsub_evt.data
88 m, mr = self.measures[index] 107 m, mr = self.measures[index]
89 m.is_shown = visibility 108 m.is_shown = visibility
90 mr.SetVisibility(visibility) 109 mr.SetVisibility(visibility)
  110 + ps.Publisher().sendMessage('Update slice viewer')
91 111
92 112
93 class Measurement(): 113 class Measurement():
@@ -313,6 +333,21 @@ class LinearMeasure(object): @@ -313,6 +333,21 @@ class LinearMeasure(object):
313 self.line_actor.SetVisibility(v) 333 self.line_actor.SetVisibility(v)
314 self.text_actor.SetVisibility(v) 334 self.text_actor.SetVisibility(v)
315 335
  336 + def GetActors(self):
  337 + """
  338 + Get the actors already created in this measure.
  339 + """
  340 + actors = []
  341 + if self.point_actor1:
  342 + actors.append(self.point_actor1)
  343 + if self.point_actor2:
  344 + actors.append(self.point_actor2)
  345 + if self.line_actor:
  346 + actors.append(self.line_actor)
  347 + if self.text_actor:
  348 + actors.append(self.text_actor)
  349 + return actors
  350 +
316 def Remove(self): 351 def Remove(self):
317 if self.point_actor1: 352 if self.point_actor1:
318 self.render.RemoveActor(self.point_actor1) 353 self.render.RemoveActor(self.point_actor1)
@@ -330,8 +365,8 @@ class LinearMeasure(object): @@ -330,8 +365,8 @@ class LinearMeasure(object):
330 self.render.RemoveActor(self.text_actor) 365 self.render.RemoveActor(self.text_actor)
331 del self.text_actor 366 del self.text_actor
332 367
333 - def __del__(self):  
334 - self.Remove() 368 + # def __del__(self):
  369 + # self.Remove()
335 370
336 371
337 class AngularMeasure(object): 372 class AngularMeasure(object):
@@ -472,6 +507,23 @@ class AngularMeasure(object): @@ -472,6 +507,23 @@ class AngularMeasure(object):
472 self.line_actor.SetVisibility(v) 507 self.line_actor.SetVisibility(v)
473 self.text_actor.SetVisibility(v) 508 self.text_actor.SetVisibility(v)
474 509
  510 + def GetActors(self):
  511 + """
  512 + Get the actors already created in this measure.
  513 + """
  514 + actors = []
  515 + if self.point_actor1:
  516 + actors.append(self.point_actor1)
  517 + if self.point_actor2:
  518 + actors.append(self.point_actor2)
  519 + if self.point_actor3:
  520 + actors.append(self.point_actor3)
  521 + if self.line_actor:
  522 + actors.append(self.line_actor)
  523 + if self.text_actor:
  524 + actors.append(self.text_actor)
  525 + return actors
  526 +
475 def CalculateAngle(self): 527 def CalculateAngle(self):
476 """ 528 """
477 Calculate the angle between 2 vectors in 3D space. It is based on law of 529 Calculate the angle between 2 vectors in 3D space. It is based on law of
@@ -532,5 +584,5 @@ class AngularMeasure(object): @@ -532,5 +584,5 @@ class AngularMeasure(object):
532 584
533 self.render = renderer 585 self.render = renderer
534 586
535 - def __del__(self):  
536 - self.Remove() 587 + # def __del__(self):
  588 + # self.Remove()
invesalius/data/viewer_slice.py
@@ -863,7 +863,8 @@ class Viewer(wx.Panel): @@ -863,7 +863,8 @@ class Viewer(wx.Panel):
863 ps.Publisher().subscribe(self.OnExportPicture,'Export picture to file') 863 ps.Publisher().subscribe(self.OnExportPicture,'Export picture to file')
864 ps.Publisher().subscribe(self.SetDefaultCursor, 'Set interactor default cursor') 864 ps.Publisher().subscribe(self.SetDefaultCursor, 'Set interactor default cursor')
865 865
866 - ps.Publisher().subscribe(self.AddActors, ('Add Actors', ORIENTATIONS[self.orientation])) 866 + ps.Publisher().subscribe(self.AddActors, ('Add actors', ORIENTATIONS[self.orientation]))
  867 + ps.Publisher().subscribe(self.RemoveActors, ('Remove actors', ORIENTATIONS[self.orientation]))
867 868
868 def SetDefaultCursor(self, pusub_evt): 869 def SetDefaultCursor(self, pusub_evt):
869 self.interactor.SetCursor(wx.StockCursor(wx.CURSOR_DEFAULT)) 870 self.interactor.SetCursor(wx.StockCursor(wx.CURSOR_DEFAULT))
@@ -1494,3 +1495,9 @@ class Viewer(wx.Panel): @@ -1494,3 +1495,9 @@ class Viewer(wx.Panel):
1494 actors = pubsub_evt.data 1495 actors = pubsub_evt.data
1495 for actor in actors: 1496 for actor in actors:
1496 self.render_to_add.AddActor(actor) 1497 self.render_to_add.AddActor(actor)
  1498 +
  1499 + def RemoveActors(self, pubsub_evt):
  1500 + "Remove a list of actors"
  1501 + actors = pubsub_evt.data
  1502 + for actor in actors:
  1503 + self.render_to_add.RemoveActor(actor)
invesalius/data/viewer_volume.py
@@ -142,7 +142,7 @@ class Viewer(wx.Panel): @@ -142,7 +142,7 @@ class Viewer(wx.Panel):
142 ps.Publisher().subscribe(self.OnHideText, 142 ps.Publisher().subscribe(self.OnHideText,
143 'Hide text actors on viewers') 143 'Hide text actors on viewers')
144 144
145 - ps.Publisher().subscribe(self.AddActors, ('Add Actors', const.SURFACE)) 145 + ps.Publisher().subscribe(self.AddActors, ('Add actors', const.SURFACE))
146 146
147 ps.Publisher().subscribe(self.OnShowText, 147 ps.Publisher().subscribe(self.OnShowText,
148 'Show text actors on viewers') 148 'Show text actors on viewers')