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 | self.AddSeparator() |
---|
50 | reset_im = GUIFRAME_ICON.RESET_ICON |
---|
51 | reset_im.Rescale(tbar_size[0], tbar_size[1], wx.IMAGE_QUALITY_HIGH) |
---|
52 | reset_bmp = reset_im.ConvertToBitmap() |
---|
53 | #disable_reset_bmp = clear_image(reset_im).ConvertToBitmap() |
---|
54 | disable_reset_bmp = wx.NullBitmap |
---|
55 | self.AddLabelTool(GUIFRAME_ID.RESET_ID, 'Reset', reset_bmp, |
---|
56 | disable_reset_bmp, button_type,'Reset') |
---|
57 | self.AddSeparator() |
---|
58 | drag_im = GUIFRAME_ICON.DRAG_ICON |
---|
59 | drag_im.Rescale(tbar_size[0], tbar_size[1], wx.IMAGE_QUALITY_HIGH) |
---|
60 | drag_bmp = drag_im.ConvertToBitmap() |
---|
61 | self.AddSeparator() |
---|
62 | report_im = GUIFRAME_ICON.REPORT_ICON |
---|
63 | report_im.Rescale(tbar_size[0], tbar_size[1], wx.IMAGE_QUALITY_HIGH) |
---|
64 | report_bmp = report_im.ConvertToBitmap() |
---|
65 | #disable_report_bmp = clear_image(report_im).ConvertToBitmap() |
---|
66 | disable_report_bmp = wx.NullBitmap |
---|
67 | self.AddLabelTool(GUIFRAME_ID.PREVIEW_ID, 'Report', report_bmp, |
---|
68 | disable_report_bmp, button_type,'Report') |
---|
69 | self.AddSeparator() |
---|
70 | print_im = GUIFRAME_ICON.PRINT_ICON |
---|
71 | print_im.Rescale(tbar_size[0], tbar_size[1], wx.IMAGE_QUALITY_HIGH) |
---|
72 | print_bmp = print_im.ConvertToBitmap() |
---|
73 | #disable_print_bmp = clear_image(print_im).ConvertToBitmap() |
---|
74 | disable_print_bmp = wx.NullBitmap |
---|
75 | self.AddLabelTool(GUIFRAME_ID.PRINT_ID, 'Print', print_bmp, |
---|
76 | disable_print_bmp, button_type, 'Print') |
---|
77 | self.AddSeparator() |
---|
78 | undo_im = GUIFRAME_ICON.UNDO_ICON |
---|
79 | undo_im.Rescale(tbar_size[0], tbar_size[1], wx.IMAGE_QUALITY_HIGH) |
---|
80 | undo_bmp = undo_im.ConvertToBitmap() |
---|
81 | #disable_undo_bmp = clear_image(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 = clear_image(redo_im).ConvertToBitmap() |
---|
90 | disable_redo_bmp = wx.NullBitmap |
---|
91 | self.AddLabelTool(GUIFRAME_ID.REDO_ID, 'Redo', redo_bmp, |
---|
92 | disable_redo_bmp, button_type,'Redo') |
---|
93 | self.AddSeparator() |
---|
94 | #add button for the current application |
---|
95 | self.button_application = wx.StaticText(self, -1, 'Welcome') |
---|
96 | self.button_application.SetForegroundColour('black') |
---|
97 | self.button_application.SetBackgroundColour('#1874CD') |
---|
98 | hint = 'Active Application' |
---|
99 | self.button_application.SetToolTipString(hint) |
---|
100 | self.AddControl(self.button_application) |
---|
101 | self.AddSeparator() |
---|
102 | #add button for the panel on focus |
---|
103 | self.button_panel = wx.StaticText(self, -1, 'No Panel') |
---|
104 | self.button_panel.SetForegroundColour('black') |
---|
105 | self.button_panel.SetBackgroundColour('#90EE90') |
---|
106 | hint = 'Panel on Focus' |
---|
107 | self.button_panel.SetToolTipString(hint) |
---|
108 | self.AddControl(self.button_panel) |
---|
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.Realize() |
---|
124 | |
---|
125 | def on_bind_button(self): |
---|
126 | """ |
---|
127 | """ |
---|
128 | if self.parent is not None: |
---|
129 | |
---|
130 | self.parent.Bind(wx.EVT_TOOL, self.parent.on_redo_panel, |
---|
131 | id=GUIFRAME_ID.REDO_ID) |
---|
132 | self.parent.Bind(wx.EVT_TOOL, self.parent.on_undo_panel, |
---|
133 | id=GUIFRAME_ID.UNDO_ID) |
---|
134 | self.parent.Bind(wx.EVT_TOOL, self.parent.on_reset_panel, |
---|
135 | id=GUIFRAME_ID.RESET_ID) |
---|
136 | self.parent.Bind(wx.EVT_TOOL, self.parent.on_preview_panel, |
---|
137 | id=GUIFRAME_ID.PREVIEW_ID) |
---|
138 | self.parent.Bind(wx.EVT_TOOL, self.parent.on_print_panel, |
---|
139 | id=GUIFRAME_ID.PRINT_ID) |
---|
140 | |
---|
141 | def update_button(self, application_name='', panel_name=''): |
---|
142 | """ |
---|
143 | """ |
---|
144 | self.button_application.SetLabel(str(application_name)) |
---|
145 | self.button_panel.SetLabel(str(panel_name)) |
---|
146 | |
---|
147 | def update_toolbar(self, panel=None): |
---|
148 | """ |
---|
149 | """ |
---|
150 | if panel is None: |
---|
151 | self.EnableTool(GUIFRAME_ID.PRINT_ID, False) |
---|
152 | self.EnableTool(GUIFRAME_ID.UNDO_ID,False) |
---|
153 | self.EnableTool(GUIFRAME_ID.REDO_ID, False) |
---|
154 | self.EnableTool(GUIFRAME_ID.PREVIEW_ID, False) |
---|
155 | self.EnableTool(GUIFRAME_ID.RESET_ID, False) |
---|
156 | self._bookmark_bt.Disable() |
---|
157 | |
---|
158 | else: |
---|
159 | self.EnableTool(GUIFRAME_ID.PRINT_ID, panel.get_print_flag()) |
---|
160 | self.EnableTool(GUIFRAME_ID.UNDO_ID, panel.get_undo_flag()) |
---|
161 | self.EnableTool(GUIFRAME_ID.REDO_ID, panel.get_redo_flag()) |
---|
162 | self.EnableTool(GUIFRAME_ID.PREVIEW_ID, panel.get_preview_flag()) |
---|
163 | self.EnableTool(GUIFRAME_ID.RESET_ID, panel.get_reset_flag()) |
---|
164 | self._bookmark_bt.Enable(panel.get_bookmark_flag()) |
---|
165 | self.Realize() |
---|
166 | |
---|
167 | def enable_undo(self, panel): |
---|
168 | self.EnableTool(GUIFRAME_ID.UNDO_ID, panel.get_undo_flag()) |
---|
169 | self.Realize() |
---|
170 | |
---|
171 | def enable_redo(self, panel): |
---|
172 | self.EnableTool(GUIFRAME_ID.REDO_ID, panel.get_redo_flag()) |
---|
173 | self.Realize() |
---|
174 | |
---|
175 | def enable_print(self, panel): |
---|
176 | self.EnableTool(GUIFRAME_ID.PRINT_ID, panel.get_print_flag()) |
---|
177 | self.Realize() |
---|
178 | |
---|
179 | def enable_zoom(self, panel): |
---|
180 | self.EnableTool(GUIFRAME_ID.ZOOM_ID, panel.get_zoom_flag()) |
---|
181 | self.Realize() |
---|
182 | |
---|
183 | def enable_zoom_in(self, panel): |
---|
184 | self.EnableTool(GUIFRAME_ID.ZOOM_IN_ID, panel.get_zoom_in_flag()) |
---|
185 | self.Realize() |
---|
186 | |
---|
187 | def enable_zoom_out(self, panel): |
---|
188 | self.EnableTool(GUIFRAME_ID.ZOOM_OUT_ID, panel.get_zoom_out_flag()) |
---|
189 | self.Realize() |
---|
190 | |
---|
191 | def enable_bookmark(self, panel): |
---|
192 | flag = panel.get_bookmark_flag() |
---|
193 | self._bookmark_bt.Enable(flag) |
---|
194 | self.Realize() |
---|
195 | |
---|
196 | def enable_save(self, panel): |
---|
197 | self.EnableTool(GUIFRAME_ID.SAVE_ID, panel.get_save_flag()) |
---|
198 | self.Realize() |
---|
199 | |
---|
200 | def enable_reset(self, panel): |
---|
201 | self.EnableTool(GUIFRAME_ID.RESET_ID, panel.get_reset_flag()) |
---|
202 | self.Realize() |
---|
203 | |
---|
204 | def enable_preview(self, panel): |
---|
205 | self.EnableTool(GUIFRAME_ID.PREVIEW_ID, panel.get_preview_flag()) |
---|
206 | self.Realize() |
---|
207 | |
---|
208 | def on_bookmark(self, event): |
---|
209 | """ |
---|
210 | add book mark |
---|
211 | """ |
---|
212 | if self.parent is not None: |
---|
213 | self.parent.on_bookmark_panel(event) |
---|
214 | |
---|
215 | def append_bookmark(self, event): |
---|
216 | """ |
---|
217 | receive item to append on the toolbar button bookmark |
---|
218 | """ |
---|
219 | title = event.title |
---|
220 | hint = event.hint |
---|
221 | handler = event.handler |
---|
222 | id = wx.NewId() |
---|
223 | self._bookmark_menu.Append(id, str(title), str(hint)) |
---|
224 | wx.EVT_MENU(self, id, handler) |
---|
225 | |
---|