1 __copyright__ = "Copyright (C) 2013 David Braam - Released under terms of the AGPLv3 License"
2 from __future__ import division
5 from wx.lib import buttons
7 from Cura.util import profile
8 from Cura.util.resources import getPathForImage
11 #######################################################
12 # toolbarUtil contains help classes and functions for
14 #######################################################
16 class Toolbar(wx.ToolBar):
17 def __init__(self, parent):
18 super(Toolbar, self).__init__(parent, -1, style=wx.TB_HORIZONTAL | wx.NO_BORDER)
19 self.SetToolBitmapSize(( 21, 21 ))
21 if not hasattr(parent, 'popup'):
23 parent.popup = wx.PopupWindow(parent, flags=wx.BORDER_SIMPLE)
24 parent.popup.SetBackgroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_INFOBK))
25 parent.popup.text = wx.StaticText(parent.popup, -1, '')
26 parent.popup.sizer = wx.BoxSizer()
27 parent.popup.sizer.Add(parent.popup.text, flag=wx.EXPAND | wx.ALL, border=1)
28 parent.popup.SetSizer(parent.popup.sizer)
29 parent.popup.owner = None
31 def OnPopupDisplay(self, e):
32 self.UpdatePopup(e.GetEventObject())
33 self.GetParent().popup.Show(True)
35 def OnPopupHide(self, e):
36 if self.GetParent().popup.owner == e.GetEventObject():
37 self.GetParent().popup.Show(False)
39 def UpdatePopup(self, control):
40 popup = self.GetParent().popup
42 popup.text.SetLabel(control.helpText)
45 x, y = control.ClientToScreenXY(0, 0)
46 sx, sy = control.GetSizeTuple()
47 popup.SetPosition((x, y + sy))
50 class ToggleButton(buttons.GenBitmapToggleButton):
51 def __init__(self, parent, profileSetting, bitmapFilenameOn, bitmapFilenameOff,
52 helpText='', id=-1, callback=None, size=(20, 20)):
53 self.bitmapOn = wx.Bitmap(getPathForImage(bitmapFilenameOn))
54 self.bitmapOff = wx.Bitmap(getPathForImage(bitmapFilenameOff))
56 super(ToggleButton, self).__init__(parent, id, self.bitmapOff, size=size)
58 self.callback = callback
59 self.profileSetting = profileSetting
60 self.helpText = helpText
63 self.SetUseFocusIndicator(False)
65 if self.profileSetting != '':
66 self.SetValue(profile.getProfileSetting(self.profileSetting) == 'True')
67 self.Bind(wx.EVT_BUTTON, self.OnButtonProfile)
69 self.Bind(wx.EVT_BUTTON, self.OnButton)
71 self.Bind(wx.EVT_ENTER_WINDOW, self.OnMouseEnter)
72 self.Bind(wx.EVT_LEAVE_WINDOW, self.OnMouseLeave)
74 parent.AddControl(self)
76 def SetBitmap(self, boolValue):
78 self.SetBitmapLabel(self.bitmapOn, False)
80 self.SetBitmapLabel(self.bitmapOff, False)
82 def SetValue(self, boolValue):
83 self.SetBitmap(boolValue)
84 super(ToggleButton, self).SetValue(boolValue)
86 def OnButton(self, event):
87 self.SetBitmap(self.GetValue())
88 if self.callback != None:
92 def OnButtonProfile(self, event):
93 if buttons.GenBitmapToggleButton.GetValue(self):
95 profile.putProfileSetting(self.profileSetting, 'True')
98 profile.putProfileSetting(self.profileSetting, 'False')
99 if self.callback != None:
103 def OnMouseEnter(self, event):
104 self.GetParent().OnPopupDisplay(event)
109 def OnMouseLeave(self, event):
110 self.GetParent().OnPopupHide(event)
111 self.SetBitmap(self.GetValue())
116 class RadioButton(buttons.GenBitmapButton):
117 def __init__(self, parent, group, bitmapFilenameOn, bitmapFilenameOff,
118 helpText='', id=-1, callback=None):
119 self.bitmapOn = wx.Bitmap(getPathForImage(bitmapFilenameOn))
120 self.bitmapOff = wx.Bitmap(getPathForImage(bitmapFilenameOff))
122 super(RadioButton, self).__init__(parent, id, self.bitmapOff, size=self.bitmapOn.GetSize() + (4, 4))
126 self.callback = callback
127 self.helpText = helpText
130 self.SetBezelWidth(1)
131 self.SetUseFocusIndicator(False)
133 self.Bind(wx.EVT_BUTTON, self.OnButton)
135 self.Bind(wx.EVT_ENTER_WINDOW, self.OnMouseEnter)
136 self.Bind(wx.EVT_LEAVE_WINDOW, self.OnMouseLeave)
141 parent.AddControl(self)
143 def SetBitmap(self, boolValue):
145 self.SetBitmapLabel(self.bitmapOn, False)
147 self.SetBitmapLabel(self.bitmapOff, False)
150 def SetValue(self, boolValue):
151 self._value = boolValue
152 self.SetBitmap(self.GetValue())
154 for other in self.group:
156 other.SetValue(False)
161 def OnButton(self, event):
166 def OnMouseEnter(self, event):
167 self.GetParent().OnPopupDisplay(event)
172 def OnMouseLeave(self, event):
173 self.GetParent().OnPopupHide(event)
174 self.SetBitmap(self.GetValue())
179 class NormalButton(buttons.GenBitmapButton):
180 def __init__(self, parent, callback, bitmapFilename,
181 helpText='', id=-1, size=(20, 20)):
182 self.bitmap = wx.Bitmap(getPathForImage(bitmapFilename))
183 super(NormalButton, self).__init__(parent, id, self.bitmap, size=size)
185 self.helpText = helpText
186 self.callback = callback
188 self.SetBezelWidth(1)
189 self.SetUseFocusIndicator(False)
191 self.Bind(wx.EVT_ENTER_WINDOW, self.OnMouseEnter)
192 self.Bind(wx.EVT_LEAVE_WINDOW, self.OnMouseLeave)
194 self.Bind(wx.EVT_BUTTON, self.OnButton)
196 parent.AddControl(self)
198 def OnButton(self, event):
199 self.GetParent().OnPopupHide(event)
202 def OnMouseEnter(self, event):
203 self.GetParent().OnPopupDisplay(event)
206 def OnMouseLeave(self, event):
207 self.GetParent().OnPopupHide(event)