chiark / gitweb /
Remove all the absolute imports, as they are unneeded.
[cura.git] / Cura / gui / util / toolbarUtil.py
1 __copyright__ = "Copyright (C) 2013 David Braam - Released under terms of the AGPLv3 License"
2 from __future__ import division
3
4 import wx
5 from wx.lib import buttons
6
7 from Cura.util import profile
8 from Cura.util.resources import getPathForImage
9
10
11 #######################################################
12 # toolbarUtil contains help classes and functions for
13 # toolbar buttons.
14 #######################################################
15
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 ))
20
21                 if not hasattr(parent, 'popup'):
22                         # Create popup window
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
30
31         def OnPopupDisplay(self, e):
32                 self.UpdatePopup(e.GetEventObject())
33                 self.GetParent().popup.Show(True)
34
35         def OnPopupHide(self, e):
36                 if self.GetParent().popup.owner == e.GetEventObject():
37                         self.GetParent().popup.Show(False)
38
39         def UpdatePopup(self, control):
40                 popup = self.GetParent().popup
41                 popup.owner = control
42                 popup.text.SetLabel(control.helpText)
43                 popup.text.Wrap(350)
44                 popup.Fit();
45                 x, y = control.ClientToScreenXY(0, 0)
46                 sx, sy = control.GetSizeTuple()
47                 popup.SetPosition((x, y + sy))
48
49
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))
55
56                 super(ToggleButton, self).__init__(parent, id, self.bitmapOff, size=size)
57
58                 self.callback = callback
59                 self.profileSetting = profileSetting
60                 self.helpText = helpText
61
62                 self.SetBezelWidth(1)
63                 self.SetUseFocusIndicator(False)
64
65                 if self.profileSetting != '':
66                         self.SetValue(profile.getProfileSetting(self.profileSetting) == 'True')
67                         self.Bind(wx.EVT_BUTTON, self.OnButtonProfile)
68                 else:
69                         self.Bind(wx.EVT_BUTTON, self.OnButton)
70
71                 self.Bind(wx.EVT_ENTER_WINDOW, self.OnMouseEnter)
72                 self.Bind(wx.EVT_LEAVE_WINDOW, self.OnMouseLeave)
73
74                 parent.AddControl(self)
75
76         def SetBitmap(self, boolValue):
77                 if boolValue:
78                         self.SetBitmapLabel(self.bitmapOn, False)
79                 else:
80                         self.SetBitmapLabel(self.bitmapOff, False)
81
82         def SetValue(self, boolValue):
83                 self.SetBitmap(boolValue)
84                 super(ToggleButton, self).SetValue(boolValue)
85
86         def OnButton(self, event):
87                 self.SetBitmap(self.GetValue())
88                 if self.callback != None:
89                         self.callback()
90                 event.Skip()
91
92         def OnButtonProfile(self, event):
93                 if buttons.GenBitmapToggleButton.GetValue(self):
94                         self.SetBitmap(True)
95                         profile.putProfileSetting(self.profileSetting, 'True')
96                 else:
97                         self.SetBitmap(False)
98                         profile.putProfileSetting(self.profileSetting, 'False')
99                 if self.callback != None:
100                         self.callback()
101                 event.Skip()
102
103         def OnMouseEnter(self, event):
104                 self.GetParent().OnPopupDisplay(event)
105                 self.SetBitmap(True)
106                 self.Refresh()
107                 event.Skip()
108
109         def OnMouseLeave(self, event):
110                 self.GetParent().OnPopupHide(event)
111                 self.SetBitmap(self.GetValue())
112                 self.Refresh()
113                 event.Skip()
114
115
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))
121
122                 super(RadioButton, self).__init__(parent, id, self.bitmapOff, size=self.bitmapOn.GetSize() + (4, 4))
123
124                 self.group = group
125                 group.append(self)
126                 self.callback = callback
127                 self.helpText = helpText
128                 self._value = False
129
130                 self.SetBezelWidth(1)
131                 self.SetUseFocusIndicator(False)
132
133                 self.Bind(wx.EVT_BUTTON, self.OnButton)
134
135                 self.Bind(wx.EVT_ENTER_WINDOW, self.OnMouseEnter)
136                 self.Bind(wx.EVT_LEAVE_WINDOW, self.OnMouseLeave)
137
138                 if len(group) == 1:
139                         self.SetValue(True)
140
141                 parent.AddControl(self)
142
143         def SetBitmap(self, boolValue):
144                 if boolValue:
145                         self.SetBitmapLabel(self.bitmapOn, False)
146                 else:
147                         self.SetBitmapLabel(self.bitmapOff, False)
148                 self.Refresh()
149
150         def SetValue(self, boolValue):
151                 self._value = boolValue
152                 self.SetBitmap(self.GetValue())
153                 if boolValue:
154                         for other in self.group:
155                                 if other != self:
156                                         other.SetValue(False)
157
158         def GetValue(self):
159                 return self._value
160
161         def OnButton(self, event):
162                 self.SetValue(True)
163                 self.callback()
164                 event.Skip()
165
166         def OnMouseEnter(self, event):
167                 self.GetParent().OnPopupDisplay(event)
168                 self.SetBitmap(True)
169                 self.Refresh()
170                 event.Skip()
171
172         def OnMouseLeave(self, event):
173                 self.GetParent().OnPopupHide(event)
174                 self.SetBitmap(self.GetValue())
175                 self.Refresh()
176                 event.Skip()
177
178
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)
184
185                 self.helpText = helpText
186                 self.callback = callback
187
188                 self.SetBezelWidth(1)
189                 self.SetUseFocusIndicator(False)
190
191                 self.Bind(wx.EVT_ENTER_WINDOW, self.OnMouseEnter)
192                 self.Bind(wx.EVT_LEAVE_WINDOW, self.OnMouseLeave)
193
194                 self.Bind(wx.EVT_BUTTON, self.OnButton)
195
196                 parent.AddControl(self)
197
198         def OnButton(self, event):
199                 self.GetParent().OnPopupHide(event)
200                 self.callback(event)
201
202         def OnMouseEnter(self, event):
203                 self.GetParent().OnPopupDisplay(event)
204                 event.Skip()
205
206         def OnMouseLeave(self, event):
207                 self.GetParent().OnPopupHide(event)
208                 event.Skip()
209