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