1 | |
---|
2 | import wx |
---|
3 | import os |
---|
4 | from wx import ToolBar as Tbar |
---|
5 | from wx.lib.platebtn import PlateButton |
---|
6 | from sas.guiframe.gui_style import GUIFRAME_ID |
---|
7 | from sas.guiframe.gui_style import GUIFRAME_ICON |
---|
8 | from wx.lib.platebtn import PB_STYLE_DEFAULT, PB_STYLE_DROPARROW,PB_STYLE_NOBG |
---|
9 | #Control panel width |
---|
10 | import sys |
---|
11 | if sys.platform.count("darwin")==0: |
---|
12 | FONT_VARIANT = 0 |
---|
13 | NAME_BOX = wx.DefaultSize |
---|
14 | TB_H = 21 |
---|
15 | IS_MAC = False |
---|
16 | else: |
---|
17 | FONT_VARIANT = 1 |
---|
18 | NAME_BOX = (200, 25) |
---|
19 | TB_H = 25 |
---|
20 | IS_MAC = True |
---|
21 | |
---|
22 | def clear_image(image): |
---|
23 | w, h = image.GetSize() |
---|
24 | factor = 155 |
---|
25 | compress = lambda x: int(x * factor/255.) + factor |
---|
26 | for y in range(h): |
---|
27 | for x in range(w): |
---|
28 | grey = compress(image.GetGreen(x, y)) |
---|
29 | image.SetRGB(x, y, grey, grey, grey) |
---|
30 | if image.HasAlpha(): |
---|
31 | image.ConvertAlphaToMask() |
---|
32 | return image |
---|
33 | |
---|
34 | class GUIToolBar(Tbar): |
---|
35 | """ |
---|
36 | Implement toolbar for guiframe |
---|
37 | """ |
---|
38 | ID_BOOKMARK = wx.NewId() |
---|
39 | def __init__(self, parent, *args, **kwds): |
---|
40 | Tbar.__init__(self, parent, *args, **kwds) |
---|
41 | #Set window's font size |
---|
42 | self.SetWindowVariant(variant=FONT_VARIANT) |
---|
43 | self.parent = parent |
---|
44 | self._bookmark_menu = None |
---|
45 | self._bookmark_bt = None |
---|
46 | self.do_layout() |
---|
47 | self.on_bind_button() |
---|
48 | |
---|
49 | def do_layout(self): |
---|
50 | """ |
---|
51 | """ |
---|
52 | t_size = TB_H |
---|
53 | tbar_size = (t_size, t_size) |
---|
54 | button_type = wx.ITEM_NORMAL |
---|
55 | |
---|
56 | reset_im = GUIFRAME_ICON.RESET_ICON |
---|
57 | reset_im.Rescale(tbar_size[0], tbar_size[1], wx.IMAGE_QUALITY_HIGH) |
---|
58 | reset_bmp = reset_im.ConvertToBitmap() |
---|
59 | #disable_reset_bmp = clear_image(reset_im).ConvertToBitmap() |
---|
60 | disable_reset_bmp = wx.NullBitmap |
---|
61 | self.AddLabelTool(GUIFRAME_ID.RESET_ID, 'Reset', reset_bmp, |
---|
62 | disable_reset_bmp, button_type,'Reset') |
---|
63 | self.AddSeparator() |
---|
64 | save_im = GUIFRAME_ICON.SAVE_ICON |
---|
65 | save_im.Rescale(tbar_size[0], tbar_size[1], wx.IMAGE_QUALITY_HIGH) |
---|
66 | save_bmp = save_im.ConvertToBitmap() |
---|
67 | disable_save_bmp = wx.NullBitmap |
---|
68 | self.AddLabelTool(GUIFRAME_ID.SAVE_ID, 'Save', save_bmp, |
---|
69 | disable_save_bmp, button_type,'Save') |
---|
70 | self.AddSeparator() |
---|
71 | report_im = GUIFRAME_ICON.REPORT_ICON |
---|
72 | report_im.Rescale(tbar_size[0], tbar_size[1], wx.IMAGE_QUALITY_HIGH) |
---|
73 | report_bmp = report_im.ConvertToBitmap() |
---|
74 | #disable_report_bmp = clear_image(report_im).ConvertToBitmap() |
---|
75 | disable_report_bmp = wx.NullBitmap |
---|
76 | self.AddLabelTool(GUIFRAME_ID.PREVIEW_ID, 'Report', report_bmp, |
---|
77 | disable_report_bmp, button_type,'Report') |
---|
78 | self.AddSeparator() |
---|
79 | undo_im = GUIFRAME_ICON.UNDO_ICON |
---|
80 | undo_im.Rescale(tbar_size[0], tbar_size[1], wx.IMAGE_QUALITY_HIGH) |
---|
81 | undo_bmp = undo_im.ConvertToBitmap() |
---|
82 | disable_undo_bmp = wx.NullBitmap |
---|
83 | self.AddLabelTool(GUIFRAME_ID.UNDO_ID, 'Undo', undo_bmp, |
---|
84 | disable_undo_bmp, button_type,'Undo') |
---|
85 | self.AddSeparator() |
---|
86 | redo_im = GUIFRAME_ICON.REDO_ICON |
---|
87 | redo_im.Rescale(tbar_size[0], tbar_size[1], wx.IMAGE_QUALITY_HIGH) |
---|
88 | redo_bmp = redo_im.ConvertToBitmap() |
---|
89 | disable_redo_bmp = wx.NullBitmap |
---|
90 | self.AddLabelTool(GUIFRAME_ID.REDO_ID, 'Redo', redo_bmp, |
---|
91 | disable_redo_bmp, button_type,'Redo') |
---|
92 | self.AddSeparator() |
---|
93 | copy_im = GUIFRAME_ICON.COPY_ICON |
---|
94 | copy_im.Rescale(tbar_size[0], tbar_size[1], wx.IMAGE_QUALITY_HIGH) |
---|
95 | copy_bmp = copy_im.ConvertToBitmap() |
---|
96 | disable_copy_bmp = wx.NullBitmap |
---|
97 | self.AddLabelTool(GUIFRAME_ID.COPY_ID, 'Copy', copy_bmp, |
---|
98 | disable_copy_bmp, button_type,'Copy parameter values') |
---|
99 | self.AddSeparator() |
---|
100 | paste_im = GUIFRAME_ICON.PASTE_ICON |
---|
101 | paste_im.Rescale(tbar_size[0], tbar_size[1], wx.IMAGE_QUALITY_HIGH) |
---|
102 | paste_bmp = paste_im.ConvertToBitmap() |
---|
103 | disable_paste_bmp = wx.NullBitmap |
---|
104 | self.AddLabelTool(GUIFRAME_ID.PASTE_ID, 'Paste', paste_bmp, |
---|
105 | disable_paste_bmp, button_type,'Paste parameter values') |
---|
106 | self.AddSeparator() |
---|
107 | self._bookmark_bt = PlateButton(self, -1, 'Bookmarks', |
---|
108 | GUIFRAME_ICON.BOOKMARK_ICON.ConvertToBitmap(), |
---|
109 | style=PB_STYLE_DEFAULT|PB_STYLE_DROPARROW|PB_STYLE_NOBG) |
---|
110 | self._bookmark_bt.SetWindowVariant(FONT_VARIANT) |
---|
111 | self._bookmark_bt.Disable() |
---|
112 | self._bookmark_menu = wx.Menu() |
---|
113 | self.add_bookmark_default() |
---|
114 | self._bookmark_bt.SetMenu(self._bookmark_menu) |
---|
115 | self.AddControl(self._bookmark_bt) |
---|
116 | self.SetToolBitmapSize(tbar_size) |
---|
117 | self.AddSeparator() |
---|
118 | #add button for the panel on focus |
---|
119 | self.button_panel = wx.StaticText(self, -1, 'No Panel', |
---|
120 | pos=wx.DefaultPosition, |
---|
121 | size=NAME_BOX, |
---|
122 | style=wx.SUNKEN_BORDER|wx.ALIGN_LEFT) |
---|
123 | button_panel_font = self.button_panel.GetFont() |
---|
124 | button_panel_font.SetWeight(wx.BOLD) |
---|
125 | self.button_panel.SetFont(button_panel_font) |
---|
126 | hint = 'Control Panel on Focus' |
---|
127 | self.button_panel.SetToolTipString(hint) |
---|
128 | self.AddControl(self.button_panel) |
---|
129 | self.AddSeparator() |
---|
130 | self.Realize() |
---|
131 | |
---|
132 | def add_bookmark_default(self): |
---|
133 | """ |
---|
134 | Add default items in bookmark menu |
---|
135 | """ |
---|
136 | self._bookmark_menu.Append(self.ID_BOOKMARK, 'Bookmark This Page State') |
---|
137 | self._bookmark_menu.AppendSeparator() |
---|
138 | wx.EVT_MENU(self, self.ID_BOOKMARK, self.on_bookmark) |
---|
139 | |
---|
140 | def on_bind_button(self): |
---|
141 | """ |
---|
142 | Bind the buttons |
---|
143 | """ |
---|
144 | if self.parent is not None: |
---|
145 | |
---|
146 | self.parent.Bind(wx.EVT_TOOL, self.parent.on_redo_panel, |
---|
147 | id=GUIFRAME_ID.REDO_ID) |
---|
148 | self.parent.Bind(wx.EVT_TOOL, self.parent.on_undo_panel, |
---|
149 | id=GUIFRAME_ID.UNDO_ID) |
---|
150 | self.parent.Bind(wx.EVT_TOOL, self.parent.on_copy_panel, |
---|
151 | id=GUIFRAME_ID.COPY_ID) |
---|
152 | self.parent.Bind(wx.EVT_TOOL, self.parent.on_paste_panel, |
---|
153 | id=GUIFRAME_ID.PASTE_ID) |
---|
154 | self.parent.Bind(wx.EVT_TOOL, self.parent.on_reset_panel, |
---|
155 | id=GUIFRAME_ID.RESET_ID) |
---|
156 | self.parent.Bind(wx.EVT_TOOL, self.parent.on_save_panel, |
---|
157 | id=GUIFRAME_ID.SAVE_ID) |
---|
158 | self.parent.Bind(wx.EVT_TOOL, self.parent.on_preview_panel, |
---|
159 | id=GUIFRAME_ID.PREVIEW_ID) |
---|
160 | #self.parent.Bind(wx.EVT_TOOL, self.parent.on_print_panel, |
---|
161 | # id=GUIFRAME_ID.PRINT_ID) |
---|
162 | |
---|
163 | def update_button(self, application_name='', panel_name=''): |
---|
164 | """ |
---|
165 | """ |
---|
166 | #self.button_application.SetLabel(str(application_name)) |
---|
167 | self.button_panel.SetLabel(str(panel_name)) |
---|
168 | self.button_panel.SetToolTipString(str(panel_name)) |
---|
169 | |
---|
170 | def update_toolbar(self, panel=None): |
---|
171 | """ |
---|
172 | """ |
---|
173 | if panel is None: |
---|
174 | #self.EnableTool(GUIFRAME_ID.PRINT_ID, False) |
---|
175 | self.EnableTool(GUIFRAME_ID.UNDO_ID,False) |
---|
176 | self.EnableTool(GUIFRAME_ID.REDO_ID, False) |
---|
177 | self.EnableTool(GUIFRAME_ID.COPY_ID,False) |
---|
178 | self.EnableTool(GUIFRAME_ID.PASTE_ID, False) |
---|
179 | self.EnableTool(GUIFRAME_ID.PREVIEW_ID, False) |
---|
180 | self.EnableTool(GUIFRAME_ID.RESET_ID, False) |
---|
181 | self.EnableTool(GUIFRAME_ID.SAVE_ID, False) |
---|
182 | self._bookmark_bt.Disable() |
---|
183 | |
---|
184 | else: |
---|
185 | #self.EnableTool(GUIFRAME_ID.PRINT_ID, panel.get_print_flag()) |
---|
186 | self.EnableTool(GUIFRAME_ID.UNDO_ID, panel.get_undo_flag()) |
---|
187 | self.EnableTool(GUIFRAME_ID.REDO_ID, panel.get_redo_flag()) |
---|
188 | self.EnableTool(GUIFRAME_ID.COPY_ID, panel.get_copy_flag()) |
---|
189 | self.EnableTool(GUIFRAME_ID.PASTE_ID, panel.get_paste_flag()) |
---|
190 | self.EnableTool(GUIFRAME_ID.PREVIEW_ID, panel.get_preview_flag()) |
---|
191 | self.EnableTool(GUIFRAME_ID.RESET_ID, panel.get_reset_flag()) |
---|
192 | self.EnableTool(GUIFRAME_ID.SAVE_ID, panel.get_save_flag()) |
---|
193 | self._bookmark_bt.Enable(panel.get_bookmark_flag()) |
---|
194 | self.Realize() |
---|
195 | |
---|
196 | def enable_undo(self, panel): |
---|
197 | self.EnableTool(GUIFRAME_ID.UNDO_ID, panel.get_undo_flag()) |
---|
198 | self.Realize() |
---|
199 | |
---|
200 | def enable_redo(self, panel): |
---|
201 | self.EnableTool(GUIFRAME_ID.REDO_ID, panel.get_redo_flag()) |
---|
202 | self.Realize() |
---|
203 | |
---|
204 | def enable_copy(self, panel): |
---|
205 | self.EnableTool(GUIFRAME_ID.COPY_ID, panel.get_copy_flag()) |
---|
206 | self.Realize() |
---|
207 | |
---|
208 | def enable_paste(self, panel): |
---|
209 | self.EnableTool(GUIFRAME_ID.PASTE_ID, panel.get_paste_flag()) |
---|
210 | self.Realize() |
---|
211 | |
---|
212 | def enable_print(self, panel): |
---|
213 | self.EnableTool(GUIFRAME_ID.PRINT_ID, panel.get_print_flag()) |
---|
214 | self.Realize() |
---|
215 | |
---|
216 | def enable_zoom(self, panel): |
---|
217 | self.EnableTool(GUIFRAME_ID.ZOOM_ID, panel.get_zoom_flag()) |
---|
218 | self.Realize() |
---|
219 | |
---|
220 | def enable_zoom_in(self, panel): |
---|
221 | self.EnableTool(GUIFRAME_ID.ZOOM_IN_ID, panel.get_zoom_in_flag()) |
---|
222 | self.Realize() |
---|
223 | |
---|
224 | def enable_zoom_out(self, panel): |
---|
225 | self.EnableTool(GUIFRAME_ID.ZOOM_OUT_ID, panel.get_zoom_out_flag()) |
---|
226 | self.Realize() |
---|
227 | |
---|
228 | def enable_bookmark(self, panel): |
---|
229 | flag = panel.get_bookmark_flag() |
---|
230 | self._bookmark_bt.Enable(flag) |
---|
231 | self.Realize() |
---|
232 | |
---|
233 | def enable_save(self, panel): |
---|
234 | self.EnableTool(GUIFRAME_ID.SAVE_ID, panel.get_save_flag()) |
---|
235 | self.Realize() |
---|
236 | |
---|
237 | def enable_reset(self, panel): |
---|
238 | self.EnableTool(GUIFRAME_ID.RESET_ID, panel.get_reset_flag()) |
---|
239 | self.Realize() |
---|
240 | |
---|
241 | def enable_preview(self, panel): |
---|
242 | self.EnableTool(GUIFRAME_ID.PREVIEW_ID, panel.get_preview_flag()) |
---|
243 | self.Realize() |
---|
244 | |
---|
245 | def on_bookmark(self, event): |
---|
246 | """ |
---|
247 | add book mark |
---|
248 | """ |
---|
249 | if self.parent is not None: |
---|
250 | self.parent.on_bookmark_panel(event) |
---|
251 | |
---|
252 | def append_bookmark(self, event): |
---|
253 | """ |
---|
254 | receive item to append on the toolbar button bookmark |
---|
255 | """ |
---|
256 | title = event.title |
---|
257 | hint = event.hint |
---|
258 | handler = event.handler |
---|
259 | id = wx.NewId() |
---|
260 | self._bookmark_menu.Append(id, str(title), str(hint)) |
---|
261 | wx.EVT_MENU(self, id, handler) |
---|
262 | |
---|
263 | def remove_bookmark_item(self, item): |
---|
264 | """ |
---|
265 | Remove a bookmark item |
---|
266 | """ |
---|
267 | self._bookmark_menu.DestroyItem(item) |
---|
268 | |
---|
269 | def get_bookmark_items(self): |
---|
270 | """ |
---|
271 | Get bookmark menu items |
---|
272 | """ |
---|
273 | return self._bookmark_menu.GetMenuItems() |
---|
274 | |
---|
275 | def append_bookmark_item(self, id, label): |
---|
276 | """ |
---|
277 | Append a item in bookmark |
---|
278 | """ |
---|
279 | self._bookmark_menu.Append(id, label) |
---|