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