Commit 81552f9d11c371f19e909a65ed4ead6fa3bde228
1 parent
40466824
Exists in
master
Created a class with showing and edit listctrl features and subbclassed at mask, surface, measures
Showing
1 changed file
with
79 additions
and
160 deletions
Show diff stats
invesalius/gui/data_notebook.py
@@ -351,10 +351,84 @@ class ButtonControlPanel(wx.Panel): | @@ -351,10 +351,84 @@ class ButtonControlPanel(wx.Panel): | ||
351 | else: | 351 | else: |
352 | dlg.MaskSelectionRequiredForDuplication() | 352 | dlg.MaskSelectionRequiredForDuplication() |
353 | 353 | ||
354 | -class MasksListCtrlPanel(wx.ListCtrl): | 354 | + |
355 | +class InvListCtrl(wx.ListCtrl): | ||
355 | def __init__(self, parent, ID=-1, pos=wx.DefaultPosition, | 356 | def __init__(self, parent, ID=-1, pos=wx.DefaultPosition, |
356 | size=wx.DefaultSize, style=wx.LC_REPORT | wx.LC_EDIT_LABELS): | 357 | size=wx.DefaultSize, style=wx.LC_REPORT | wx.LC_EDIT_LABELS): |
357 | wx.ListCtrl.__init__(self, parent, ID, pos, size, style=style) | 358 | wx.ListCtrl.__init__(self, parent, ID, pos, size, style=style) |
359 | + self.__bind_events_wx() | ||
360 | + | ||
361 | + def __bind_events_wx(self): | ||
362 | + self.Bind(wx.EVT_LEFT_UP, self.OnClickItem) | ||
363 | + self.Bind(wx.EVT_LEFT_DCLICK, self.OnDblClickItem) | ||
364 | + | ||
365 | + def CreateColourBitmap(self, colour): | ||
366 | + """ | ||
367 | + Create a wx Image with a mask colour. | ||
368 | + colour: colour in rgb format(0 - 1) | ||
369 | + """ | ||
370 | + image = self.image_gray | ||
371 | + new_image = Image.new("RGB", image.size) | ||
372 | + for x in range(image.size[0]): | ||
373 | + for y in range(image.size[1]): | ||
374 | + pixel_colour = [int(i*image.getpixel((x,y))) | ||
375 | + for i in colour] | ||
376 | + new_image.putpixel((x,y), tuple(pixel_colour)) | ||
377 | + | ||
378 | + wx_image = wx.Image(new_image.size[0], | ||
379 | + new_image.size[1]) | ||
380 | + try: | ||
381 | + wx_image.SetData(new_image.tostring()) | ||
382 | + except Exception: | ||
383 | + wx_image.SetData(new_image.tobytes()) | ||
384 | + return wx.Bitmap(wx_image.Scale(16, 16)) | ||
385 | + | ||
386 | + def OnClickItem(self, evt): | ||
387 | + self._click_check = False | ||
388 | + item_idx, flag = (self.HitTest(evt.GetPosition())) | ||
389 | + column_clicked = self.get_column_clicked(evt.GetPosition()) | ||
390 | + if column_clicked == 0: | ||
391 | + self._click_check = True | ||
392 | + item = self.GetItem(item_idx, 0) | ||
393 | + flag = not bool(item.GetImage()) | ||
394 | + self.SetItemImage(item_idx, int(flag)) | ||
395 | + self.OnCheckItem(item_idx, flag) | ||
396 | + evt.Skip() | ||
397 | + | ||
398 | + def OnDblClickItem(self, evt): | ||
399 | + self._click_check = False | ||
400 | + item_idx, flag = (self.HitTest(evt.GetPosition())) | ||
401 | + column_clicked = self.get_column_clicked(evt.GetPosition()) | ||
402 | + if column_clicked == 1: | ||
403 | + item = self.GetItem(item_idx, 1) | ||
404 | + self.enter_edition(item) | ||
405 | + evt.Skip() | ||
406 | + | ||
407 | + def enter_edition(self, item): | ||
408 | + ctrl = self.EditLabel(item.GetId()) | ||
409 | + w, h = ctrl.GetClientSize() | ||
410 | + w = self.GetColumnWidth(1) | ||
411 | + ctrl.SetClientSize(w, h) | ||
412 | + ctrl.SetValue(item.GetText()) | ||
413 | + ctrl.SelectAll() | ||
414 | + | ||
415 | + | ||
416 | + def get_column_clicked(self, position): | ||
417 | + epx, epy = position | ||
418 | + wpx, wpy = self.GetPosition() | ||
419 | + width_sum = 0 | ||
420 | + for i in range(self.GetColumnCount()): | ||
421 | + width_sum += self.GetColumnWidth(i) | ||
422 | + if (epx - wpx) <= width_sum: | ||
423 | + return i | ||
424 | + return -1 | ||
425 | + | ||
426 | + | ||
427 | + | ||
428 | +class MasksListCtrlPanel(InvListCtrl): | ||
429 | + def __init__(self, parent, ID=-1, pos=wx.DefaultPosition, | ||
430 | + size=wx.DefaultSize, style=wx.LC_REPORT | wx.LC_EDIT_LABELS): | ||
431 | + super().__init__(parent, ID, pos, size, style=style) | ||
358 | self._click_check = False | 432 | self._click_check = False |
359 | self.mask_list_index = {} | 433 | self.mask_list_index = {} |
360 | self.current_index = 0 | 434 | self.current_index = 0 |
@@ -366,9 +440,6 @@ class MasksListCtrlPanel(wx.ListCtrl): | @@ -366,9 +440,6 @@ class MasksListCtrlPanel(wx.ListCtrl): | ||
366 | def __bind_events_wx(self): | 440 | def __bind_events_wx(self): |
367 | self.Bind(wx.EVT_LIST_END_LABEL_EDIT, self.OnEditLabel) | 441 | self.Bind(wx.EVT_LIST_END_LABEL_EDIT, self.OnEditLabel) |
368 | self.Bind(wx.EVT_KEY_UP, self.OnKeyEvent) | 442 | self.Bind(wx.EVT_KEY_UP, self.OnKeyEvent) |
369 | - # self.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.OnItemActivated) | ||
370 | - self.Bind(wx.EVT_LEFT_UP, self.OnClickItem) | ||
371 | - self.Bind(wx.EVT_LEFT_DCLICK, self.OnDblClickItem) | ||
372 | 443 | ||
373 | def __bind_events(self): | 444 | def __bind_events(self): |
374 | Publisher.subscribe(self.AddMask, 'Add mask') | 445 | Publisher.subscribe(self.AddMask, 'Add mask') |
@@ -483,16 +554,6 @@ class MasksListCtrlPanel(wx.ListCtrl): | @@ -483,16 +554,6 @@ class MasksListCtrlPanel(wx.ListCtrl): | ||
483 | 554 | ||
484 | self.image_gray = Image.open(os.path.join(inv_paths.ICON_DIR, "object_colour.jpg")) | 555 | self.image_gray = Image.open(os.path.join(inv_paths.ICON_DIR, "object_colour.jpg")) |
485 | 556 | ||
486 | - def get_column_clicked(self, position): | ||
487 | - epx, epy = position | ||
488 | - wpx, wpy = self.GetPosition() | ||
489 | - width_sum = 0 | ||
490 | - for i in range(self.GetColumnCount()): | ||
491 | - width_sum += self.GetColumnWidth(i) | ||
492 | - if (epx - wpx) <= width_sum: | ||
493 | - return i | ||
494 | - return -1 | ||
495 | - | ||
496 | def OnEditLabel(self, evt): | 557 | def OnEditLabel(self, evt): |
497 | if not evt.IsEditCancelled(): | 558 | if not evt.IsEditCancelled(): |
498 | index = evt.GetIndex() | 559 | index = evt.GetIndex() |
@@ -510,56 +571,7 @@ class MasksListCtrlPanel(wx.ListCtrl): | @@ -510,56 +571,7 @@ class MasksListCtrlPanel(wx.ListCtrl): | ||
510 | self.current_index = index | 571 | self.current_index = index |
511 | Publisher.sendMessage('Show mask', index=index, value=flag) | 572 | Publisher.sendMessage('Show mask', index=index, value=flag) |
512 | 573 | ||
513 | - def OnClickItem(self, evt): | ||
514 | - self._click_check = False | ||
515 | - item_idx, flag = (self.HitTest(evt.GetPosition())) | ||
516 | - column_clicked = self.get_column_clicked(evt.GetPosition()) | ||
517 | - if column_clicked == 0: | ||
518 | - self._click_check = True | ||
519 | - item = self.GetItem(item_idx, 0) | ||
520 | - flag = not bool(item.GetImage()) | ||
521 | - self.SetItemImage(item_idx, int(flag)) | ||
522 | - self.OnCheckItem(item_idx, flag) | ||
523 | - evt.Skip() | ||
524 | - | ||
525 | - def OnDblClickItem(self, evt): | ||
526 | - self._click_check = False | ||
527 | - item_idx, flag = (self.HitTest(evt.GetPosition())) | ||
528 | - column_clicked = self.get_column_clicked(evt.GetPosition()) | ||
529 | - if column_clicked == 1: | ||
530 | - item = self.GetItem(item_idx, 1) | ||
531 | - self.enter_edition(item) | ||
532 | - evt.Skip() | ||
533 | - | ||
534 | - def enter_edition(self, item): | ||
535 | - print("Enter edition") | ||
536 | - ctrl = self.EditLabel(item.GetId()) | ||
537 | - w, h = ctrl.GetClientSize() | ||
538 | - w = self.GetColumnWidth(1) | ||
539 | - ctrl.SetClientSize(w, h) | ||
540 | - ctrl.SetValue(item.GetText()) | ||
541 | - ctrl.SelectAll() | ||
542 | - | ||
543 | - def CreateColourBitmap(self, colour): | ||
544 | - """ | ||
545 | - Create a wx Image with a mask colour. | ||
546 | - colour: colour in rgb format(0 - 1) | ||
547 | - """ | ||
548 | - image = self.image_gray | ||
549 | - new_image = Image.new("RGB", image.size) | ||
550 | - for x in range(image.size[0]): | ||
551 | - for y in range(image.size[1]): | ||
552 | - pixel_colour = [int(i*image.getpixel((x,y))) | ||
553 | - for i in colour] | ||
554 | - new_image.putpixel((x,y), tuple(pixel_colour)) | ||
555 | 574 | ||
556 | - wx_image = wx.Image(new_image.size[0], | ||
557 | - new_image.size[1]) | ||
558 | - try: | ||
559 | - wx_image.SetData(new_image.tostring()) | ||
560 | - except Exception: | ||
561 | - wx_image.SetData(new_image.tobytes()) | ||
562 | - return wx.Bitmap(wx_image.Scale(16, 16)) | ||
563 | 575 | ||
564 | def InsertNewItem(self, index=0, label=_("Mask"), threshold="(1000, 4500)", | 576 | def InsertNewItem(self, index=0, label=_("Mask"), threshold="(1000, 4500)", |
565 | colour=None): | 577 | colour=None): |
@@ -743,10 +755,10 @@ class SurfaceButtonControlPanel(wx.Panel): | @@ -743,10 +755,10 @@ class SurfaceButtonControlPanel(wx.Panel): | ||
743 | def AffineStatus(self, affine, status): | 755 | def AffineStatus(self, affine, status): |
744 | self.affinestatus = status | 756 | self.affinestatus = status |
745 | 757 | ||
746 | -class SurfacesListCtrlPanel(wx.ListCtrl): | 758 | +class SurfacesListCtrlPanel(InvListCtrl): |
747 | def __init__(self, parent, ID=-1, pos=wx.DefaultPosition, | 759 | def __init__(self, parent, ID=-1, pos=wx.DefaultPosition, |
748 | size=wx.DefaultSize, style=wx.LC_REPORT | wx.LC_EDIT_LABELS): | 760 | size=wx.DefaultSize, style=wx.LC_REPORT | wx.LC_EDIT_LABELS): |
749 | - wx.ListCtrl.__init__(self, parent, ID, pos, size, style=style) | 761 | + super().__init__(parent, ID, pos, size, style=style) |
750 | self._click_check = False | 762 | self._click_check = False |
751 | self.__init_columns() | 763 | self.__init_columns() |
752 | self.__init_image_list() | 764 | self.__init_image_list() |
@@ -772,30 +784,6 @@ class SurfacesListCtrlPanel(wx.ListCtrl): | @@ -772,30 +784,6 @@ class SurfacesListCtrlPanel(wx.ListCtrl): | ||
772 | self.Bind(wx.EVT_LIST_END_LABEL_EDIT, self.OnEditLabel) | 784 | self.Bind(wx.EVT_LIST_END_LABEL_EDIT, self.OnEditLabel) |
773 | #self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected_) | 785 | #self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected_) |
774 | self.Bind(wx.EVT_KEY_UP, self.OnKeyEvent) | 786 | self.Bind(wx.EVT_KEY_UP, self.OnKeyEvent) |
775 | - self.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.OnItemActivated) | ||
776 | - self.Bind(wx.EVT_LEFT_UP, self.OnClickItem) | ||
777 | - | ||
778 | - def OnClickItem(self, evt): | ||
779 | - self._click_check = False | ||
780 | - item_idx, flag = (self.HitTest(evt.GetPosition())) | ||
781 | - if flag == wx.LIST_HITTEST_ONITEMICON: | ||
782 | - self._click_check = True | ||
783 | - item = self.GetItem(item_idx, 0) | ||
784 | - flag = not bool(item.GetImage()) | ||
785 | - self.SetItemImage(item_idx, int(flag)) | ||
786 | - self.OnCheckItem(item_idx, flag) | ||
787 | - evt.Skip() | ||
788 | - | ||
789 | - def OnItemActivated(self, evt): | ||
790 | - if not self._click_check: | ||
791 | - item = self.GetItem(evt.GetItem().GetId(), 1) | ||
792 | - ctrl = self.EditLabel(item.GetId()) | ||
793 | - w, h = ctrl.GetClientSize() | ||
794 | - w = self.GetColumnWidth(1) | ||
795 | - ctrl.SetClientSize(w, h) | ||
796 | - ctrl.SetValue(item.GetText()) | ||
797 | - ctrl.SelectAll() | ||
798 | - evt.Skip() | ||
799 | 787 | ||
800 | def OnKeyEvent(self, event): | 788 | def OnKeyEvent(self, event): |
801 | keycode = event.GetKeyCode() | 789 | keycode = event.GetKeyCode() |
@@ -982,28 +970,6 @@ class SurfacesListCtrlPanel(wx.ListCtrl): | @@ -982,28 +970,6 @@ class SurfacesListCtrlPanel(wx.ListCtrl): | ||
982 | self.SetItem(index, 4, transparency) | 970 | self.SetItem(index, 4, transparency) |
983 | self.SetItemImage(index, 1) | 971 | self.SetItemImage(index, 1) |
984 | 972 | ||
985 | - def CreateColourBitmap(self, colour): | ||
986 | - """ | ||
987 | - Create a wx Image with a mask colour. | ||
988 | - colour: colour in rgb format(0 - 1) | ||
989 | - """ | ||
990 | - image = self.image_gray | ||
991 | - new_image = Image.new("RGB", image.size) | ||
992 | - for x in range(image.size[0]): | ||
993 | - for y in range(image.size[1]): | ||
994 | - pixel_colour = [int(i*image.getpixel((x,y))) | ||
995 | - for i in colour] | ||
996 | - new_image.putpixel((x,y), tuple(pixel_colour)) | ||
997 | - | ||
998 | - wx_image = wx.Image(new_image.size[0], | ||
999 | - new_image.size[1]) | ||
1000 | - try: | ||
1001 | - wx_image.SetData(new_image.tostring()) | ||
1002 | - except Exception: | ||
1003 | - wx_image.SetData(new_image.tobytes()) | ||
1004 | - | ||
1005 | - return wx.Bitmap(wx_image.Scale(16, 16)) | ||
1006 | - | ||
1007 | def EditSurfaceTransparency(self, surface_index, transparency): | 973 | def EditSurfaceTransparency(self, surface_index, transparency): |
1008 | """ | 974 | """ |
1009 | Set actor transparency (oposite to opacity) according to given actor | 975 | Set actor transparency (oposite to opacity) according to given actor |
@@ -1023,11 +989,10 @@ class SurfacesListCtrlPanel(wx.ListCtrl): | @@ -1023,11 +989,10 @@ class SurfacesListCtrlPanel(wx.ListCtrl): | ||
1023 | #------------------------------------------------- | 989 | #------------------------------------------------- |
1024 | #------------------------------------------------- | 990 | #------------------------------------------------- |
1025 | 991 | ||
1026 | -class MeasuresListCtrlPanel(wx.ListCtrl): | ||
1027 | - | 992 | +class MeasuresListCtrlPanel(InvListCtrl): |
1028 | def __init__(self, parent, ID=-1, pos=wx.DefaultPosition, | 993 | def __init__(self, parent, ID=-1, pos=wx.DefaultPosition, |
1029 | size=wx.DefaultSize, style=wx.LC_REPORT | wx.LC_EDIT_LABELS): | 994 | size=wx.DefaultSize, style=wx.LC_REPORT | wx.LC_EDIT_LABELS): |
1030 | - wx.ListCtrl.__init__(self, parent, ID, pos, size, style=style) | 995 | + super().__init__(parent, ID, pos, size, style=style) |
1031 | self._click_check = False | 996 | self._click_check = False |
1032 | self.__init_columns() | 997 | self.__init_columns() |
1033 | self.__init_image_list() | 998 | self.__init_image_list() |
@@ -1051,31 +1016,6 @@ class MeasuresListCtrlPanel(wx.ListCtrl): | @@ -1051,31 +1016,6 @@ class MeasuresListCtrlPanel(wx.ListCtrl): | ||
1051 | self.Bind(wx.EVT_LIST_END_LABEL_EDIT, self.OnEditLabel) | 1016 | self.Bind(wx.EVT_LIST_END_LABEL_EDIT, self.OnEditLabel) |
1052 | self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected_) | 1017 | self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected_) |
1053 | self.Bind(wx.EVT_KEY_UP, self.OnKeyEvent) | 1018 | self.Bind(wx.EVT_KEY_UP, self.OnKeyEvent) |
1054 | - self.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.OnItemActivated) | ||
1055 | - self.Bind(wx.EVT_LEFT_UP, self.OnClickItem) | ||
1056 | - | ||
1057 | - def OnClickItem(self, evt): | ||
1058 | - self._click_check = False | ||
1059 | - item_idx, flag = (self.HitTest(evt.GetPosition())) | ||
1060 | - if flag == wx.LIST_HITTEST_ONITEMICON: | ||
1061 | - self._click_check = True | ||
1062 | - item = self.GetItem(item_idx, 0) | ||
1063 | - flag = not bool(item.GetImage()) | ||
1064 | - self.SetItemImage(item_idx, int(flag)) | ||
1065 | - self.OnCheckItem(item_idx, flag) | ||
1066 | - evt.Skip() | ||
1067 | - | ||
1068 | - def OnItemActivated(self, evt): | ||
1069 | - if not self._click_check: | ||
1070 | - item = self.GetItem(evt.GetItem().GetId(), 1) | ||
1071 | - ctrl = self.EditLabel(item.GetId()) | ||
1072 | - w, h = ctrl.GetClientSize() | ||
1073 | - w = self.GetColumnWidth(1) | ||
1074 | - ctrl.SetClientSize(w, h) | ||
1075 | - ctrl.SetValue(item.GetText()) | ||
1076 | - ctrl.SelectAll() | ||
1077 | - evt.Skip() | ||
1078 | - | ||
1079 | 1019 | ||
1080 | def OnKeyEvent(self, event): | 1020 | def OnKeyEvent(self, event): |
1081 | keycode = event.GetKeyCode() | 1021 | keycode = event.GetKeyCode() |
@@ -1289,27 +1229,6 @@ class MeasuresListCtrlPanel(wx.ListCtrl): | @@ -1289,27 +1229,6 @@ class MeasuresListCtrlPanel(wx.ListCtrl): | ||
1289 | self.SetItemImage(index, 1) | 1229 | self.SetItemImage(index, 1) |
1290 | self.Refresh() | 1230 | self.Refresh() |
1291 | 1231 | ||
1292 | - def CreateColourBitmap(self, colour): | ||
1293 | - """ | ||
1294 | - Create a wx Image with a mask colour. | ||
1295 | - colour: colour in rgb format(0 - 1) | ||
1296 | - """ | ||
1297 | - image = self.image_gray | ||
1298 | - new_image = Image.new("RGB", image.size) | ||
1299 | - for x in range(image.size[0]): | ||
1300 | - for y in range(image.size[1]): | ||
1301 | - pixel_colour = [int(i*image.getpixel((x,y))) | ||
1302 | - for i in colour] | ||
1303 | - new_image.putpixel((x,y), tuple(pixel_colour)) | ||
1304 | - | ||
1305 | - wx_image = wx.Image(new_image.size[0], | ||
1306 | - new_image.size[1]) | ||
1307 | - try: | ||
1308 | - wx_image.SetData(new_image.tostring()) | ||
1309 | - except: | ||
1310 | - wx_image.SetData(new_image.tobytes()) | ||
1311 | - return wx.Bitmap(wx_image.Scale(16, 16)) | ||
1312 | - | ||
1313 | def EditItemColour(self, measure_index, colour): | 1232 | def EditItemColour(self, measure_index, colour): |
1314 | """ | 1233 | """ |
1315 | """ | 1234 | """ |