Commit 6a2fe195f10ee152dee13c99203f19c8eda531e8

Authored by tfmoraes
1 parent 8cb54de9

FIX: Not raising a exception when removing a point anterior to a selected one

Showing 1 changed file with 60 additions and 59 deletions   Show diff stats
invesalius/gui/widgets/clut_raycasting.py
... ... @@ -33,8 +33,8 @@ class CLUTRaycastingWidget(wx.Panel):
33 33 parent -- parent of this frame
34 34 """
35 35 super(CLUTRaycastingWidget, self).__init__(parent, id)
36   - self.points = []#plistlib.readPlist(sys.argv[-1])['16bitClutCurves']
37   - self.colours = []#plistlib.readPlist(sys.argv[-1])['16bitClutColors']
  36 + self.points = []
  37 + self.colours = []
38 38 self.init = -1024
39 39 self.end = 2000
40 40 self.padding = 5
... ... @@ -43,17 +43,10 @@ class CLUTRaycastingWidget(wx.Panel):
43 43 self.histogram_pixel_points = [[0,0]]
44 44 self.histogram_array = [100,100]
45 45 self.CreatePixelArray()
46   - #self.sizer = wx.BoxSizer(wx.HORIZONTAL)
47   - #self.SetSizer(self.sizer)
48   - #self.DrawControls()
49 46 self.dragged = False
50 47 self.point_dragged = None
51   - self.DoBind()
52   - #self.__bind_events()
53   - #self.SetAutoLayout(True)
54   - #self.sizer.Fit(self)
  48 + self.__bind_events_wx()
55 49 self.Show()
56   - #self.LoadVolume()
57 50  
58 51 def SetRange(self, range):
59 52 self.init, self.end = range
... ... @@ -63,7 +56,7 @@ class CLUTRaycastingWidget(wx.Panel):
63 56 def SetPadding(self, padding):
64 57 self.padding = padding
65 58  
66   - def DoBind(self):
  59 + def __bind_events_wx(self):
67 60 self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
68 61 self.Bind(wx.EVT_LEFT_DOWN , self.OnClick)
69 62 self.Bind(wx.EVT_LEFT_DCLICK , self.OnDoubleClick)
... ... @@ -102,6 +95,9 @@ class CLUTRaycastingWidget(wx.Panel):
102 95 evt.Skip()
103 96  
104 97 def OnDoubleClick(self, evt):
  98 + """
  99 + Used to change the colour of a point
  100 + """
105 101 point = self._has_clicked_in_a_point(evt.GetPositionTuple())
106 102 if point:
107 103 colour = wx.GetColourFromUser(self)
... ... @@ -117,45 +113,15 @@ class CLUTRaycastingWidget(wx.Panel):
117 113 return
118 114 evt.Skip()
119 115  
120   - def _has_clicked_in_a_point(self, position):
  116 + def OnRighClick(self, evt):
121 117 """
122   - returns the index from the selected point
  118 + Used to remove a point
123 119 """
124   - for i,curve in enumerate(self.pixels_points):
125   - for j,point in enumerate(curve):
126   - if self._calculate_distance(point, position) <= RADIUS:
127   - return (i, j)
128   - return None
129   -
130   - def _has_clicked_in_line(self, position):
131   - for n, point in enumerate(self.pixels_points):
132   - p = bisect.bisect([i[0] for i in point], position[0])
133   - print p
134   - if p != 0 and p != len(point):
135   - x1, y1 = point[p-1]
136   - x2, y2 = position
137   - x3, y3 = point[p]
138   - if int(float(x2 - x1) / (x3 - x2)) - int(float(y2 - y1) / (y3 - y2)) == 0:
139   - return (n, p)
140   - return None
141   -
142   - def _calculate_distance(self, p1, p2):
143   - return ((p1[0]-p2[0])**2 + (p1[1]-p2[1])**2) ** 0.5
144   -
145   - def OnRighClick(self, evt):
146 120 point = self._has_clicked_in_a_point(evt.GetPositionTuple())
147 121 if point:
148 122 i, j = point
149 123 print "RightClick", i, j
150   - self.pixels_points[i].pop(j)
151   - self.points[i].pop(j)
152   - self.colours[i].pop(j)
153   - if (i, j) == self.point_dragged:
154   - self.point_dragged = None
155   - if len(self.points[i]) == 1:
156   - self.points.pop(i)
157   - self.pixels_points.pop(i)
158   - self.colours.pop(i)
  124 + self.RemovePoint(i, j)
159 125 self.Refresh()
160 126 nevt = CLUTEvent(myEVT_CLUT_POINT_CHANGED, self.GetId())
161 127 self.GetEventHandler().ProcessEvent(nevt)
... ... @@ -226,7 +192,7 @@ class CLUTRaycastingWidget(wx.Panel):
226 192 def OnSize(self, evt):
227 193 self.CreatePixelArray()
228 194 self.Refresh()
229   -
  195 +
230 196 def _draw_gradient(self, ctx, height):
231 197 #The gradient
232 198 height += self.padding
... ... @@ -249,6 +215,55 @@ class CLUTRaycastingWidget(wx.Panel):
249 215 ctx.close_path()
250 216 ctx.set_source(gradient)
251 217 ctx.fill()
  218 + def _has_clicked_in_a_point(self, position):
  219 + """
  220 + returns the index from the selected point
  221 + """
  222 + for i,curve in enumerate(self.pixels_points):
  223 + for j,point in enumerate(curve):
  224 + if self._calculate_distance(point, position) <= RADIUS:
  225 + return (i, j)
  226 + return None
  227 +
  228 + def _has_clicked_in_line(self, position):
  229 + for n, point in enumerate(self.pixels_points):
  230 + p = bisect.bisect([i[0] for i in point], position[0])
  231 + print p
  232 + if p != 0 and p != len(point):
  233 + x1, y1 = point[p-1]
  234 + x2, y2 = position
  235 + x3, y3 = point[p]
  236 + if int(float(x2 - x1) / (x3 - x2)) - int(float(y2 - y1) / (y3 - y2)) == 0:
  237 + return (n, p)
  238 + return None
  239 +
  240 + def _calculate_distance(self, p1, p2):
  241 + return ((p1[0]-p2[0])**2 + (p1[1]-p2[1])**2) ** 0.5
  242 +
  243 + def RemovePoint(self, i, j):
  244 + """
  245 + The point the point in the given i,j index
  246 + """
  247 + self.pixels_points[i].pop(j)
  248 + self.points[i].pop(j)
  249 + self.colours[i].pop(j)
  250 + # If the point to removed is that was selected before and have a
  251 + # textbox, then remove the point and the textbox
  252 + if (i, j) == self.point_dragged:
  253 + self.point_dragged = None
  254 + # If there is textbox and the point to remove is before it, then
  255 + # decrement the index referenced to point that have the textbox.
  256 + elif self.point_dragged and i == self.point_dragged[0] \
  257 + and j < self.point_dragged[1]:
  258 + new_i = self.point_dragged[0]
  259 + new_j = self.point_dragged[1] - 1
  260 + self.point_dragged = (new_i, new_j)
  261 + # Can't have only one point in the curve
  262 + if len(self.points[i]) == 1:
  263 + self.points.pop(i)
  264 + self.pixels_points.pop(i)
  265 + self.colours.pop(i)
  266 + self.point_dragged = None
252 267  
253 268 def _draw_curves(self, ctx):
254 269 #Drawing the lines
... ... @@ -388,7 +403,6 @@ class CLUTRaycastingWidget(wx.Panel):
388 403 y = height - y * proportion_y
389 404 self.histogram_pixel_points.append((x, y))
390 405  
391   -
392 406 def CreatePixelArray(self):
393 407 self.pixels_points = []
394 408 for curve in self.points:
... ... @@ -459,16 +473,3 @@ EVT_CLUT_POINT = wx.PyEventBinder(myEVT_CLUT_POINT, 1)
459 473 # Occurs when a CLUT point was changed
460 474 myEVT_CLUT_POINT_CHANGED = wx.NewEventType()
461 475 EVT_CLUT_POINT_CHANGED = wx.PyEventBinder(myEVT_CLUT_POINT_CHANGED, 1)
462   -
463   -class App(wx.App):
464   - def OnInit(self):
465   - str_type = sys.argv[-1].split("/")[-1].split(".")[0]
466   - self.frame = CLUTRaycastingWidget(None, -1, "InVesalius 3 - Raycasting: "+ str_type)
467   - self.frame.SetPreset(plistlib.readPlist(sys.argv[-1]))
468   - self.frame.Center()
469   - self.SetTopWindow(self.frame)
470   - return True
471   -
472   -if __name__ == '__main__':
473   - app = App()
474   - app.MainLoop()
... ...