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 | report_im = GUIFRAME_ICON.REPORT_ICON |
---|
62 | report_im.Rescale(tbar_size[0], tbar_size[1], wx.IMAGE_QUALITY_HIGH) |
---|
63 | report_bmp = report_im.ConvertToBitmap() |
---|
64 | #disable_report_bmp = clear_image(report_im).ConvertToBitmap() |
---|
65 | disable_report_bmp = wx.NullBitmap |
---|
66 | self.AddLabelTool(GUIFRAME_ID.PREVIEW_ID, 'Report', report_bmp, |
---|
67 | disable_report_bmp, button_type,'Report') |
---|
68 | self.AddSeparator() |
---|
69 | print_im = GUIFRAME_ICON.PRINT_ICON |
---|
70 | print_im.Rescale(tbar_size[0], tbar_size[1], wx.IMAGE_QUALITY_HIGH) |
---|
71 | print_bmp = print_im.ConvertToBitmap() |
---|
72 | #disable_print_bmp = clear_image(print_im).ConvertToBitmap() |
---|
73 | disable_print_bmp = wx.NullBitmap |
---|
74 | self.AddLabelTool(GUIFRAME_ID.PRINT_ID, 'Print', print_bmp, |
---|
75 | disable_print_bmp, button_type, 'Print') |
---|
76 | self.AddSeparator() |
---|
77 | undo_im = GUIFRAME_ICON.UNDO_ICON |
---|
78 | undo_im.Rescale(tbar_size[0], tbar_size[1], wx.IMAGE_QUALITY_HIGH) |
---|
79 | undo_bmp = undo_im.ConvertToBitmap() |
---|
80 | #disable_undo_bmp = clear_image(undo_im).ConvertToBitmap() |
---|
81 | disable_undo_bmp = wx.NullBitmap |
---|
82 | self.AddLabelTool(GUIFRAME_ID.UNDO_ID, 'Undo', undo_bmp, |
---|
83 | disable_undo_bmp, button_type,'Undo') |
---|
84 | self.AddSeparator() |
---|
85 | redo_im = GUIFRAME_ICON.REDO_ICON |
---|
86 | redo_im.Rescale(tbar_size[0], tbar_size[1], wx.IMAGE_QUALITY_HIGH) |
---|
87 | redo_bmp = redo_im.ConvertToBitmap() |
---|
88 | #disable_redo_bmp = clear_image(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 | #add button for the current application |
---|
94 | #self.button_application = wx.StaticText(self, -1, 'Welcome') |
---|
95 | #self.button_application.SetForegroundColour('black') |
---|
96 | #self.button_application.SetBackgroundColour('#1874CD') |
---|
97 | #hint = 'Active Application' |
---|
98 | #self.button_application.SetToolTipString(hint) |
---|
99 | #self.AddControl(self.button_application) |
---|
100 | #self.AddSeparator() |
---|
101 | |
---|
102 | self._bookmark_bt = PlateButton(self, -1, 'bookmark', |
---|
103 | GUIFRAME_ICON.BOOKMARK_ICON.ConvertToBitmap(), |
---|
104 | style=PB_STYLE_SQUARE|PB_STYLE_DROPARROW) |
---|
105 | self._bookmark_bt.Disable() |
---|
106 | self._bookmark_menu = wx.Menu() |
---|
107 | id = wx.NewId() |
---|
108 | self._bookmark_menu.Append(id, 'Add bookmark') |
---|
109 | self._bookmark_menu.AppendSeparator() |
---|
110 | wx.EVT_MENU(self, id, self.on_bookmark) |
---|
111 | |
---|
112 | self._bookmark_bt.SetMenu(self._bookmark_menu) |
---|
113 | self.AddControl(self._bookmark_bt) |
---|
114 | |
---|
115 | self.SetToolBitmapSize(tbar_size) |
---|
116 | self.AddSeparator() |
---|
117 | #add button for the panel on focus |
---|
118 | self.button_panel = wx.StaticText(self, -1, 'No Panel', |
---|
119 | style=wx.SUNKEN_BORDER|wx.ALIGN_LEFT) |
---|
120 | #self.button_panel.SetForegroundColour('black') |
---|
121 | #self.button_panel.SetBackgroundColour('white') |
---|
122 | button_panel_font = self.button_panel.GetFont() |
---|
123 | button_panel_font.SetWeight(wx.BOLD) |
---|
124 | self.button_panel.SetFont(button_panel_font) |
---|
125 | #self.button_panel.SetClientSize((80,20)) |
---|
126 | hint = 'Control Panel on Focus' |
---|
127 | self.button_panel.SetToolTipString(hint) |
---|
128 | self.AddControl(self.button_panel) |
---|
129 | |
---|
130 | self.Realize() |
---|
131 | |
---|
132 | def on_bind_button(self): |
---|
133 | """ |
---|
134 | """ |
---|
135 | if self.parent is not None: |
---|
136 | |
---|
137 | self.parent.Bind(wx.EVT_TOOL, self.parent.on_redo_panel, |
---|
138 | id=GUIFRAME_ID.REDO_ID) |
---|
139 | self.parent.Bind(wx.EVT_TOOL, self.parent.on_undo_panel, |
---|
140 | id=GUIFRAME_ID.UNDO_ID) |
---|
141 | self.parent.Bind(wx.EVT_TOOL, self.parent.on_reset_panel, |
---|
142 | id=GUIFRAME_ID.RESET_ID) |
---|
143 | self.parent.Bind(wx.EVT_TOOL, self.parent.on_preview_panel, |
---|
144 | id=GUIFRAME_ID.PREVIEW_ID) |
---|
145 | self.parent.Bind(wx.EVT_TOOL, self.parent.on_print_panel, |
---|
146 | id=GUIFRAME_ID.PRINT_ID) |
---|
147 | |
---|
148 | def update_button(self, application_name='', panel_name=''): |
---|
149 | """ |
---|
150 | """ |
---|
151 | #self.button_application.SetLabel(str(application_name)) |
---|
152 | self.button_panel.SetLabel(str(panel_name)) |
---|
153 | |
---|
154 | def update_toolbar(self, panel=None): |
---|
155 | """ |
---|
156 | """ |
---|
157 | if panel is None: |
---|
158 | self.EnableTool(GUIFRAME_ID.PRINT_ID, False) |
---|
159 | self.EnableTool(GUIFRAME_ID.UNDO_ID,False) |
---|
160 | self.EnableTool(GUIFRAME_ID.REDO_ID, False) |
---|
161 | self.EnableTool(GUIFRAME_ID.PREVIEW_ID, False) |
---|
162 | self.EnableTool(GUIFRAME_ID.RESET_ID, False) |
---|
163 | self._bookmark_bt.Disable() |
---|
164 | |
---|
165 | else: |
---|
166 | self.EnableTool(GUIFRAME_ID.PRINT_ID, panel.get_print_flag()) |
---|
167 | self.EnableTool(GUIFRAME_ID.UNDO_ID, panel.get_undo_flag()) |
---|
168 | self.EnableTool(GUIFRAME_ID.REDO_ID, panel.get_redo_flag()) |
---|
169 | self.EnableTool(GUIFRAME_ID.PREVIEW_ID, panel.get_preview_flag()) |
---|
170 | self.EnableTool(GUIFRAME_ID.RESET_ID, panel.get_reset_flag()) |
---|
171 | self._bookmark_bt.Enable(panel.get_bookmark_flag()) |
---|
172 | self.Realize() |
---|
173 | |
---|
174 | def enable_undo(self, panel): |
---|
175 | self.EnableTool(GUIFRAME_ID.UNDO_ID, panel.get_undo_flag()) |
---|
176 | self.Realize() |
---|
177 | |
---|
178 | def enable_redo(self, panel): |
---|
179 | self.EnableTool(GUIFRAME_ID.REDO_ID, panel.get_redo_flag()) |
---|
180 | self.Realize() |
---|
181 | |
---|
182 | def enable_print(self, panel): |
---|
183 | self.EnableTool(GUIFRAME_ID.PRINT_ID, panel.get_print_flag()) |
---|
184 | self.Realize() |
---|
185 | |
---|
186 | def enable_zoom(self, panel): |
---|
187 | self.EnableTool(GUIFRAME_ID.ZOOM_ID, panel.get_zoom_flag()) |
---|
188 | self.Realize() |
---|
189 | |
---|
190 | def enable_zoom_in(self, panel): |
---|
191 | self.EnableTool(GUIFRAME_ID.ZOOM_IN_ID, panel.get_zoom_in_flag()) |
---|
192 | self.Realize() |
---|
193 | |
---|
194 | def enable_zoom_out(self, panel): |
---|
195 | self.EnableTool(GUIFRAME_ID.ZOOM_OUT_ID, panel.get_zoom_out_flag()) |
---|
196 | self.Realize() |
---|
197 | |
---|
198 | def enable_bookmark(self, panel): |
---|
199 | flag = panel.get_bookmark_flag() |
---|
200 | self._bookmark_bt.Enable(flag) |
---|
201 | self.Realize() |
---|
202 | |
---|
203 | def enable_save(self, panel): |
---|
204 | self.EnableTool(GUIFRAME_ID.SAVE_ID, panel.get_save_flag()) |
---|
205 | self.Realize() |
---|
206 | |
---|
207 | def enable_reset(self, panel): |
---|
208 | self.EnableTool(GUIFRAME_ID.RESET_ID, panel.get_reset_flag()) |
---|
209 | self.Realize() |
---|
210 | |
---|
211 | def enable_preview(self, panel): |
---|
212 | self.EnableTool(GUIFRAME_ID.PREVIEW_ID, panel.get_preview_flag()) |
---|
213 | self.Realize() |
---|
214 | |
---|
215 | def on_bookmark(self, event): |
---|
216 | """ |
---|
217 | add book mark |
---|
218 | """ |
---|
219 | if self.parent is not None: |
---|
220 | self.parent.on_bookmark_panel(event) |
---|
221 | |
---|
222 | def append_bookmark(self, event): |
---|
223 | """ |
---|
224 | receive item to append on the toolbar button bookmark |
---|
225 | """ |
---|
226 | title = event.title |
---|
227 | hint = event.hint |
---|
228 | handler = event.handler |
---|
229 | id = wx.NewId() |
---|
230 | self._bookmark_menu.Append(id, str(title), str(hint)) |
---|
231 | wx.EVT_MENU(self, id, handler) |
---|
232 | |
---|