chiark / gitweb /
Fix resources, imports and indentations.
[cura.git] / Cura / gui / toolbarUtil.py
1 # coding=utf-8
2 from __future__ import absolute_import
3 from __future__ import division
4
5 import wx
6 from wx.lib import buttons
7
8 from util import profile
9 from util.resources import getPathForImage
10
11
12 #######################################################
13 # toolbarUtil contains help classes and functions for
14 # toolbar buttons.
15 #######################################################
16
17 class Toolbar(wx.ToolBar):
18         def __init__(self, parent):
19                 super(Toolbar, self).__init__(parent, -1, style=wx.TB_HORIZONTAL | wx.NO_BORDER)
20                 self.SetToolBitmapSize(( 21, 21 ))
21
22                 if not hasattr(parent, 'popup'):
23                         # Create popup window
24                         parent.popup = wx.PopupWindow(parent, flags=wx.BORDER_SIMPLE)
25                         parent.popup.SetBackgroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_INFOBK))
26                         parent.popup.text = wx.StaticText(parent.popup, -1, '')
27                         parent.popup.sizer = wx.BoxSizer()
28                         parent.popup.sizer.Add(parent.popup.text, flag=wx.EXPAND | wx.ALL, border=1)
29                         parent.popup.SetSizer(parent.popup.sizer)
30                         parent.popup.owner = None
31
32         def OnPopupDisplay(self, e):
33                 self.UpdatePopup(e.GetEventObject())
34                 self.GetParent().popup.Show(True)
35
36         def OnPopupHide(self, e):
37                 if self.GetParent().popup.owner == e.GetEventObject():
38                         self.GetParent().popup.Show(False)
39
40         def UpdatePopup(self, control):
41                 popup = self.GetParent().popup
42                 popup.owner = control
43                 popup.text.SetLabel(control.helpText)
44                 popup.text.Wrap(350)
45                 popup.Fit();
46                 x, y = control.ClientToScreenXY(0, 0)
47                 sx, sy = control.GetSizeTuple()
48                 popup.SetPosition((x, y + sy))
49
50
51 class ToggleButton(buttons.GenBitmapToggleButton):
52         def __init__(self, parent, profileSetting, bitmapFilenameOn, bitmapFilenameOff,
53                      helpText='', id=-1, callback=None, size=(20, 20)):
54                 self.bitmapOn = wx.Bitmap(getPathForImage(bitmapFilenameOn))
55                 self.bitmapOff = wx.Bitmap(getPathForImage(bitmapFilenameOff))
56
57                 super(ToggleButton, self).__init__(parent, id, self.bitmapOff, size=size)
58
59                 self.callback = callback
60                 self.profileSetting = profileSetting
61                 self.helpText = helpText
62
63                 self.SetBezelWidth(1)
64                 self.SetUseFocusIndicator(False)
65
66                 if self.profileSetting != '':
67                         self.SetValue(profile.getProfileSetting(self.profileSetting) == 'True')
68                         self.Bind(wx.EVT_BUTTON, self.OnButtonProfile)
69                 else:
70                         self.Bind(wx.EVT_BUTTON, self.OnButton)
71
72                 self.Bind(wx.EVT_ENTER_WINDOW, self.OnMouseEnter)
73                 self.Bind(wx.EVT_LEAVE_WINDOW, self.OnMouseLeave)
74
75                 parent.AddControl(self)
76
77         def SetBitmap(self, boolValue):
78                 if boolValue:
79                         self.SetBitmapLabel(self.bitmapOn, False)
80                 else:
81                         self.SetBitmapLabel(self.bitmapOff, False)
82
83         def SetValue(self, boolValue):
84                 self.SetBitmap(boolValue)
85                 super(ToggleButton, self).SetValue(boolValue)
86
87         def OnButton(self, event):
88                 self.SetBitmap(self.GetValue())
89                 if self.callback != None:
90                         self.callback()
91                 event.Skip()
92
93         def OnButtonProfile(self, event):
94                 if buttons.GenBitmapToggleButton.GetValue(self):
95                         self.SetBitmap(True)
96                         profile.putProfileSetting(self.profileSetting, 'True')
97                 else:
98                         self.SetBitmap(False)
99                         profile.putProfileSetting(self.profileSetting, 'False')
100                 if self.callback != None:
101                         self.callback()
102                 event.Skip()
103
104         def OnMouseEnter(self, event):
105                 self.GetParent().OnPopupDisplay(event)
106                 self.SetBitmap(True)
107                 self.Refresh()
108                 event.Skip()
109
110         def OnMouseLeave(self, event):
111                 self.GetParent().OnPopupHide(event)
112                 self.SetBitmap(self.GetValue())
113                 self.Refresh()
114                 event.Skip()
115
116
117 class RadioButton(buttons.GenBitmapButton):
118         def __init__(self, parent, group, bitmapFilenameOn, bitmapFilenameOff,
119                      helpText='', id=-1, callback=None, size=(20, 20)):
120                 self.bitmapOn = wx.Bitmap(getPathForImage(bitmapFilenameOn))
121                 self.bitmapOff = wx.Bitmap(getPathForImage(bitmapFilenameOff))
122
123                 super(RadioButton, self).__init__(parent, id, self.bitmapOff, size=size)
124
125                 self.group = group
126                 group.append(self)
127                 self.callback = callback
128                 self.helpText = helpText
129                 self._value = False
130
131                 self.SetBezelWidth(1)
132                 self.SetUseFocusIndicator(False)
133
134                 self.Bind(wx.EVT_BUTTON, self.OnButton)
135
136                 self.Bind(wx.EVT_ENTER_WINDOW, self.OnMouseEnter)
137                 self.Bind(wx.EVT_LEAVE_WINDOW, self.OnMouseLeave)
138
139                 if len(group) == 1:
140                         self.SetValue(True)
141
142                 parent.AddControl(self)
143
144         def SetBitmap(self, boolValue):
145                 if boolValue:
146                         self.SetBitmapLabel(self.bitmapOn, False)
147                 else:
148                         self.SetBitmapLabel(self.bitmapOff, False)
149                 self.Refresh()
150
151         def SetValue(self, boolValue):
152                 self._value = boolValue
153                 self.SetBitmap(self.GetValue())
154                 if boolValue == True:
155                         for other in self.group:
156                                 if other != self:
157                                         other.SetValue(False)
158
159         def GetValue(self):
160                 return self._value
161
162         def OnButton(self, event):
163                 self.SetValue(True)
164                 self.callback()
165                 event.Skip()
166
167         def OnMouseEnter(self, event):
168                 self.GetParent().OnPopupDisplay(event)
169                 self.SetBitmap(True)
170                 self.Refresh()
171                 event.Skip()
172
173         def OnMouseLeave(self, event):
174                 self.GetParent().OnPopupHide(event)
175                 self.SetBitmap(self.GetValue())
176                 self.Refresh()
177                 event.Skip()
178
179
180 class NormalButton(buttons.GenBitmapButton):
181         def __init__(self, parent, callback, bitmapFilename,
182                      helpText='', id=-1, size=(20, 20)):
183                 self.bitmap = wx.Bitmap(getPathForImage(bitmapFilename))
184                 super(NormalButton, self).__init__(parent, id, self.bitmap, size=size)
185
186                 self.helpText = helpText
187                 self.callback = callback
188
189                 self.SetBezelWidth(1)
190                 self.SetUseFocusIndicator(False)
191
192                 self.Bind(wx.EVT_ENTER_WINDOW, self.OnMouseEnter)
193                 self.Bind(wx.EVT_LEAVE_WINDOW, self.OnMouseLeave)
194
195                 self.Bind(wx.EVT_BUTTON, self.OnButton)
196
197                 parent.AddControl(self)
198
199         def OnButton(self, event):
200                 self.GetParent().OnPopupHide(event)
201                 self.callback(event)
202
203         def OnMouseEnter(self, event):
204                 self.GetParent().OnPopupDisplay(event)
205                 event.Skip()
206
207         def OnMouseLeave(self, event):
208                 self.GetParent().OnPopupHide(event)
209                 event.Skip()
210