def __init__(self, parent): wx.Panel.__init__(self, parent) try: default_colour = wx.SystemSettings.GetColour(wx.SYS_COLOUR_MENUBAR) except AttributeError: default_colour = wx.SystemSettings_GetColour(wx.SYS_COLOUR_MENUBAR) self.SetBackgroundColour(default_colour) ## LINE 1 text1 = wx.StaticText(self, -1, _("Choose brush type, size or operation:")) ## LINE 2 menu = wx.Menu() CIRCLE_BMP = wx.Bitmap( os.path.join(const.ICON_DIR, "brush_circle.jpg"), wx.BITMAP_TYPE_JPEG) item = wx.MenuItem(menu, MENU_BRUSH_CIRCLE, _("Circle")) item.SetBitmap(CIRCLE_BMP) SQUARE_BMP = wx.Bitmap( os.path.join(const.ICON_DIR, "brush_square.jpg"), wx.BITMAP_TYPE_JPEG) item2 = wx.MenuItem(menu, MENU_BRUSH_SQUARE, _("Square")) item2.SetBitmap(SQUARE_BMP) menu.Append(item) menu.Append(item2) bmp_brush_format = { const.BRUSH_CIRCLE: CIRCLE_BMP, const.BRUSH_SQUARE: SQUARE_BMP } selected_bmp = bmp_brush_format[const.DEFAULT_BRUSH_FORMAT] btn_brush_format = pbtn.PlateButton(self, wx.ID_ANY, "", selected_bmp, style=pbtn.PB_STYLE_SQUARE) btn_brush_format.SetMenu(menu) self.btn_brush_format = btn_brush_format spin_brush_size = InvSpinCtrl(self, -1, value=const.BRUSH_SIZE, min_value=1, max_value=1000, spin_button=False) # To calculate best width to spinctrl spin_brush_size.CalcSizeFromTextSize("MMMM") spin_brush_size.Bind(wx.EVT_SPINCTRL, self.OnBrushSize) self.spin = spin_brush_size combo_brush_op = wx.ComboBox(self, -1, "", size=(15, -1), choices=const.BRUSH_OP_NAME, style=wx.CB_DROPDOWN | wx.CB_READONLY) combo_brush_op.SetSelection(const.DEFAULT_BRUSH_OP) if sys.platform != 'win32': combo_brush_op.SetWindowVariant(wx.WINDOW_VARIANT_SMALL) self.combo_brush_op = combo_brush_op # Sizer which represents the second line line2 = wx.BoxSizer(wx.HORIZONTAL) line2.Add(btn_brush_format, 0, wx.EXPAND | wx.GROW | wx.RIGHT, 5) line2.Add(spin_brush_size, 0, wx.RIGHT | wx.ALIGN_CENTER_VERTICAL, 5) line2.Add(combo_brush_op, 1, wx.RIGHT | wx.LEFT | wx.ALIGN_CENTER_VERTICAL, 5) ## LINE 3 text_thresh = wx.StaticText(self, -1, _("Brush threshold range:")) ## LINE 4 gradient_thresh = grad.GradientCtrl(self, -1, 0, 5000, 0, 5000, (0, 0, 255, 100)) self.gradient_thresh = gradient_thresh self.bind_evt_gradient = True # Add lines into main sizer sizer = wx.BoxSizer(wx.VERTICAL) sizer.AddSpacer(7) sizer.Add(text1, 0, wx.GROW | wx.EXPAND | wx.LEFT | wx.RIGHT, 5) sizer.AddSpacer(2) sizer.Add(line2, 0, wx.GROW | wx.EXPAND | wx.LEFT | wx.RIGHT, 5) sizer.AddSpacer(5) sizer.Add(text_thresh, 0, wx.GROW | wx.EXPAND | wx.LEFT | wx.RIGHT, 5) sizer.AddSpacer(5) sizer.Add(gradient_thresh, 0, wx.EXPAND | wx.LEFT | wx.RIGHT, 5) sizer.AddSpacer(7) sizer.Fit(self) self.SetSizerAndFit(sizer) self.Update() self.SetAutoLayout(1) self.__bind_events() self.__bind_events_wx()
def __init__(self, parent): wx.Panel.__init__(self, parent) try: default_colour = wx.SystemSettings.GetColour(wx.SYS_COLOUR_MENUBAR) except AttributeError: default_colour = wx.SystemSettings_GetColour(wx.SYS_COLOUR_MENUBAR) self.SetBackgroundColour(default_colour) ## LINE 1 text1 = wx.StaticText(self, -1, _("Choose brush type, size or operation:")) ## LINE 2 menu = wx.Menu() CIRCLE_BMP = wx.Bitmap( os.path.join(const.ICON_DIR, "brush_circle.jpg"), wx.BITMAP_TYPE_JPEG) item = wx.MenuItem(menu, MENU_BRUSH_CIRCLE, _("Circle")) item.SetBitmap(CIRCLE_BMP) SQUARE_BMP = wx.Bitmap( os.path.join(const.ICON_DIR, "brush_square.jpg"), wx.BITMAP_TYPE_JPEG) item2 = wx.MenuItem(menu, MENU_BRUSH_SQUARE, _("Square")) item2.SetBitmap(SQUARE_BMP) menu.Append(item) menu.Append(item2) bmp_brush_format = { const.BRUSH_CIRCLE: CIRCLE_BMP, const.BRUSH_SQUARE: SQUARE_BMP } selected_bmp = bmp_brush_format[const.DEFAULT_BRUSH_FORMAT] btn_brush_format = pbtn.PlateButton(self, wx.ID_ANY, "", selected_bmp, style=pbtn.PB_STYLE_SQUARE) btn_brush_format.SetMenu(menu) self.btn_brush_format = btn_brush_format spin_brush_size = InvSpinCtrl(self, -1, value=const.BRUSH_SIZE, min_value=1, max_value=1000, spin_button=False) # To calculate best width to spinctrl spin_brush_size.CalcSizeFromTextSize("MMMM") spin_brush_size.Bind(wx.EVT_SPINCTRL, self.OnBrushSize) self.spin = spin_brush_size combo_brush_op = wx.ComboBox(self, -1, "", size=(15, -1), choices=(_("Foreground"), _("Background"), _("Erase")), style=wx.CB_DROPDOWN | wx.CB_READONLY) combo_brush_op.SetSelection(0) if sys.platform != 'win32': combo_brush_op.SetWindowVariant(wx.WINDOW_VARIANT_SMALL) self.combo_brush_op = combo_brush_op # Sizer which represents the second line line2 = wx.BoxSizer(wx.HORIZONTAL) line2.Add(btn_brush_format, 0, wx.EXPAND | wx.GROW | wx.RIGHT, 5) line2.Add(spin_brush_size, 0, wx.RIGHT | wx.ALIGN_CENTER_VERTICAL, 5) line2.Add(combo_brush_op, 1, wx.RIGHT | wx.LEFT | wx.ALIGN_CENTER_VERTICAL, 5) ## LINE 3 ## LINE 4 # LINE 5 check_box = wx.CheckBox(self, -1, _("Overwrite mask")) ww_wl_cbox = wx.CheckBox(self, -1, _("Use WW&WL")) ww_wl_cbox.SetValue(True) self.check_box = check_box self.ww_wl_cbox = ww_wl_cbox # Line 6 bmp = wx.Bitmap(os.path.join(const.ICON_DIR, "configuration.png"), wx.BITMAP_TYPE_PNG) self.btn_wconfig = wx.BitmapButton(self, -1, bitmap=bmp, size=(bmp.GetWidth() + 10, bmp.GetHeight() + 10)) self.btn_exp_watershed = wx.Button(self, -1, _('Expand watershed to 3D')) sizer_btns = wx.BoxSizer(wx.HORIZONTAL) sizer_btns.Add(self.btn_wconfig, 0, wx.ALIGN_LEFT | wx.LEFT | wx.TOP | wx.DOWN, 5) sizer_btns.Add(self.btn_exp_watershed, 0, wx.GROW | wx.EXPAND | wx.ALL, 5) # Add lines into main sizer sizer = wx.BoxSizer(wx.VERTICAL) sizer.AddSpacer(7) sizer.Add(text1, 0, wx.GROW | wx.EXPAND | wx.LEFT | wx.RIGHT, 5) sizer.AddSpacer(2) sizer.Add(line2, 0, wx.GROW | wx.EXPAND | wx.LEFT | wx.RIGHT, 5) sizer.AddSpacer(5) sizer.Add(check_box, 0, wx.GROW | wx.EXPAND | wx.LEFT | wx.RIGHT, 5) sizer.AddSpacer(2) sizer.Add(ww_wl_cbox, 0, wx.GROW | wx.EXPAND | wx.LEFT | wx.RIGHT, 5) sizer.AddSpacer(5) sizer.Add(sizer_btns, 0, wx.EXPAND) sizer.AddSpacer(7) sizer.Fit(self) self.SetSizerAndFit(sizer) self.Update() self.SetAutoLayout(1) self.__bind_events_wx() self.__bind_pubsub_evt()
class GradientCtrl(wx.Panel): def __init__(self, parent, id, minRange, maxRange, minValue, maxValue, colour): super(GradientCtrl, self).__init__(parent, id) self.min_range = minRange self.max_range = maxRange self.minimun = minValue self.maximun = maxValue self.colour = colour[:3] self.changed = False self._draw_controls() self._bind_events_wx() self.Show() def _draw_controls(self): self.gradient_slider = GradientSlider( self, -1, self.min_range, self.max_range, self.minimun, self.maximun, self.colour, ) self.spin_min = InvSpinCtrl( self, value=self.minimun, min_value=self.min_range, max_value=self.max_range, spin_button=False, ) if sys.platform != "win32": self.spin_min.SetWindowVariant(wx.WINDOW_VARIANT_SMALL) self.spin_max = InvSpinCtrl( self, value=self.maximun, min_value=self.min_range, max_value=self.max_range, spin_button=False, ) if sys.platform != "win32": self.spin_max.SetWindowVariant(wx.WINDOW_VARIANT_SMALL) self.spin_min.CalcSizeFromTextSize() self.spin_max.CalcSizeFromTextSize() sizer = wx.BoxSizer(wx.HORIZONTAL) sizer.Add(self.spin_min, 0, wx.RIGHT, 2) sizer.Add(self.gradient_slider, 1, wx.EXPAND) sizer.Add(self.spin_max, 0, wx.LEFT, 2) self.sizer = wx.BoxSizer(wx.HORIZONTAL) self.sizer.Add(sizer, 1, wx.EXPAND) self.SetSizer(self.sizer) self.sizer.Fit(self) # self.SetAutoLayout(1) def _bind_events_wx(self): self.gradient_slider.Bind(EVT_SLIDER_CHANGING, self.OnSliding) self.gradient_slider.Bind(EVT_SLIDER_CHANGED, self.OnSlider) self.spin_min.Bind(wx.EVT_SPINCTRL, self.OnMinMouseWheel) self.spin_max.Bind(wx.EVT_SPINCTRL, self.OnMaxMouseWheel) def OnSlider(self, evt): self.spin_min.SetValue(evt.minimun) self.spin_max.SetValue(evt.maximun) self.minimun = evt.minimun self.maximun = evt.maximun self._GenerateEvent(myEVT_THRESHOLD_CHANGED) def OnSliding(self, evt): self.spin_min.SetValue(evt.minimun) self.spin_max.SetValue(evt.maximun) self.minimun = evt.minimun self.maximun = evt.maximun self._GenerateEvent(myEVT_THRESHOLD_CHANGING) def _FireSpinMinChange(self, evt): evt.Skip() value = int(self.spin_min.GetValue()) if value < self.min_range or value > self.max_range: self.spin_min.SetValue(self.minimun) return if value != self.GetMinValue() or self.changed: self.SetMinValue(value) self._GenerateEvent(myEVT_THRESHOLD_CHANGED) def _FireSpinMinChanged(self, evt): if self.changed: self._GenerateEvent(myEVT_THRESHOLD_CHANGED) def _FireSpinMaxChange(self, evt): evt.Skip() value = int(self.spin_max.GetValue()) if value < self.min_range or value > self.max_range: self.spin_max.SetValue(self.maximun) return if value != self.GetMaxValue() or self.changed: self.SetMaxValue(value) self._GenerateEvent(myEVT_THRESHOLD_CHANGED) def _FireSpinMaxChanged(self, evt): if self.changed: self._GenerateEvent(myEVT_THRESHOLD_CHANGED) def OnMinMouseWheel(self, e): """ When the user wheel the mouse over min texbox """ v = self.spin_min.GetValue() self.SetMinValue(v) self._GenerateEvent(myEVT_THRESHOLD_CHANGED) def OnMaxMouseWheel(self, e): """ When the user wheel the mouse over max texbox """ v = self.spin_max.GetValue() self.SetMaxValue(v) self._GenerateEvent(myEVT_THRESHOLD_CHANGED) def SetColour(self, colour): colour = list(colour[:3]) + [90] self.colour = colour self.gradient_slider.SetColour(colour) self.gradient_slider.Refresh() def SetMaxRange(self, value): self.spin_min.SetMax(value) self.spin_max.SetMax(value) self.spin_min.CalcSizeFromTextSize() self.spin_max.CalcSizeFromTextSize() self.gradient_slider.SetMaxRange(value) self.max_range = value if value > self.max_range: value = self.max_range self.spin_min.CalcSizeFromTextSize() self.spin_max.CalcSizeFromTextSize() self.Layout() def SetMinRange(self, value): self.spin_min.SetMin(value) self.spin_max.SetMin(value) self.spin_min.CalcSizeFromTextSize() self.spin_max.CalcSizeFromTextSize() self.gradient_slider.SetMinRange(value) self.min_range = value if value < self.min_range: value = self.min_range self.spin_min.CalcSizeFromTextSize() self.spin_max.CalcSizeFromTextSize() self.Layout() def SetMaxValue(self, value): if value is not None: value = int(value) if value > self.max_range: value = int(self.max_range) if value < self.min_range: value = int(self.min_range) if value < self.minimun: value = int(self.minimun) self.spin_max.SetValue(value) self.gradient_slider.SetMaximun(value) self.maximun = value def SetMinValue(self, value): if value is not None: value = int(value) if value < self.min_range: value = int(self.min_range) if value > self.max_range: value = int(self.max_range) if value > self.maximun: value = int(self.maximun) self.spin_min.SetValue(value) self.gradient_slider.SetMinimun(value) self.minimun = value def ChangeMinValue(self, e): # Why do I need to change slide min value if it has been changed for # the user? if not self.slided: self.gradient_slider.SetMinValue(int(self.spin_min.GetValue())) self._GenerateEvent(myEVT_THRESHOLD_CHANGE) def ChangeMaxValue(self, e): # Why do I need to change slide max value if it has been changed for # the user? if not self.slided: self.gradient_slider.SetMaxValue(int(self.spin_max.GetValue())) self._GenerateEvent(myEVT_THRESHOLD_CHANGE) def GetMaxValue(self): return self.maximun def GetMinValue(self): return self.minimun def _GenerateEvent(self, event): if event == myEVT_THRESHOLD_CHANGING: self.changed = True elif event == myEVT_THRESHOLD_CHANGED: self.changed = False evt = SliderEvent( event, self.GetId(), self.min_range, self.max_range, self.minimun, self.maximun, ) self.GetEventHandler().ProcessEvent(evt)