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