[010c251] | 1 | |
---|
| 2 | import wx |
---|
[ba924ed] | 3 | import os |
---|
[010c251] | 4 | from wx import ToolBar as Tbar |
---|
[4e4d3bb] | 5 | from wx.lib.platebtn import PlateButton |
---|
[f036c692] | 6 | from sans.guiframe.gui_style import GUIFRAME_ID |
---|
[ba924ed] | 7 | from sans.guiframe.gui_style import GUIFRAME_ICON |
---|
[4e4d3bb] | 8 | from wx.lib.platebtn import PB_STYLE_SQUARE, PB_STYLE_DROPARROW |
---|
[ba924ed] | 9 | |
---|
[010c251] | 10 | |
---|
[ba924ed] | 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 |
---|
[010c251] | 22 | |
---|
[f036c692] | 23 | class GUIToolBar(Tbar): |
---|
[ba924ed] | 24 | """ |
---|
| 25 | Implement toolbar for guiframe |
---|
| 26 | """ |
---|
[010c251] | 27 | def __init__(self, parent, *args, **kwds): |
---|
| 28 | Tbar.__init__(self, parent, *args, **kwds) |
---|
| 29 | self.parent = parent |
---|
[4e4d3bb] | 30 | self._bookmark_menu = None |
---|
| 31 | self._bookmark_bt = None |
---|
[010c251] | 32 | self.do_layout() |
---|
| 33 | self.on_bind_button() |
---|
[ba924ed] | 34 | |
---|
[010c251] | 35 | def do_layout(self): |
---|
| 36 | """ |
---|
| 37 | """ |
---|
[ba924ed] | 38 | tbar_size = (22, 22) |
---|
| 39 | button_type = wx.ITEM_NORMAL |
---|
| 40 | save_im = GUIFRAME_ICON.SAVE_ICON |
---|
| 41 | save_im.Rescale(tbar_size[0], tbar_size[1], wx.IMAGE_QUALITY_HIGH) |
---|
| 42 | save_bmp = save_im.ConvertToBitmap() |
---|
| 43 | #disable_save_bmp = clear_image(save_im).ConvertToBitmap() |
---|
| 44 | disable_save_bmp = wx.NullBitmap |
---|
| 45 | self.AddLabelTool(GUIFRAME_ID.SAVE_ID, 'Save', save_bmp, |
---|
| 46 | disable_save_bmp,button_type, shortHelp='Save') |
---|
[73b3ae2] | 47 | self.AddSeparator() |
---|
[ba924ed] | 48 | zoom_in_im = GUIFRAME_ICON.ZOOM_IN_ICON |
---|
| 49 | zoom_in_im.Rescale(tbar_size[0], tbar_size[1], wx.IMAGE_QUALITY_HIGH) |
---|
| 50 | zoom_in_bmp = zoom_in_im.ConvertToBitmap() |
---|
| 51 | #disable_zoom_in_bmp = clear_image(zoom_in_im).ConvertToBitmap() |
---|
| 52 | disable_zoom_in_bmp = wx.NullBitmap |
---|
| 53 | self.AddLabelTool(GUIFRAME_ID.ZOOM_IN_ID, 'Zoom In', zoom_in_bmp, |
---|
| 54 | disable_zoom_in_bmp, button_type,'Zoom In') |
---|
[73b3ae2] | 55 | self.AddSeparator() |
---|
[ba924ed] | 56 | zoom_out_im = GUIFRAME_ICON.ZOOM_OUT_ICON |
---|
| 57 | zoom_out_im.Rescale(tbar_size[0], tbar_size[1], wx.IMAGE_QUALITY_HIGH) |
---|
| 58 | zoom_out_bmp = zoom_out_im.ConvertToBitmap() |
---|
| 59 | #disable_zoom_out_bmp = clear_image(zoom_out_im).ConvertToBitmap() |
---|
| 60 | disable_zoom_out_bmp = wx.NullBitmap |
---|
| 61 | self.AddLabelTool(GUIFRAME_ID.ZOOM_OUT_ID, 'Zoom Out', zoom_out_bmp, |
---|
| 62 | disable_zoom_out_bmp, button_type,'Zoom Out') |
---|
[73b3ae2] | 63 | self.AddSeparator() |
---|
[ba924ed] | 64 | zoom_im = GUIFRAME_ICON.ZOOM_ICON |
---|
| 65 | zoom_im.Rescale(tbar_size[0], tbar_size[1], wx.IMAGE_QUALITY_HIGH) |
---|
| 66 | zoom_bmp = zoom_im.ConvertToBitmap() |
---|
| 67 | #disable_zoom_bmp = clear_image(zoom_im).ConvertToBitmap() |
---|
| 68 | disable_zoom_bmp = wx.NullBitmap |
---|
| 69 | self.AddLabelTool(GUIFRAME_ID.ZOOM_ID, 'Zoom', zoom_bmp, |
---|
| 70 | disable_zoom_bmp, button_type,'Zoom In') |
---|
[73b3ae2] | 71 | self.AddSeparator() |
---|
[041cf9e] | 72 | reset_im = GUIFRAME_ICON.RESET_ICON |
---|
| 73 | reset_im.Rescale(tbar_size[0], tbar_size[1], wx.IMAGE_QUALITY_HIGH) |
---|
| 74 | reset_bmp = reset_im.ConvertToBitmap() |
---|
| 75 | #disable_reset_bmp = clear_image(reset_im).ConvertToBitmap() |
---|
| 76 | disable_reset_bmp = wx.NullBitmap |
---|
| 77 | self.AddLabelTool(GUIFRAME_ID.RESET_ID, 'Reset', reset_bmp, |
---|
[18ec684] | 78 | disable_reset_bmp, button_type,'Reset') |
---|
[041cf9e] | 79 | self.AddSeparator() |
---|
[ba924ed] | 80 | drag_im = GUIFRAME_ICON.DRAG_ICON |
---|
| 81 | drag_im.Rescale(tbar_size[0], tbar_size[1], wx.IMAGE_QUALITY_HIGH) |
---|
| 82 | drag_bmp = drag_im.ConvertToBitmap() |
---|
| 83 | #disable_drag_bmp = clear_image(drag_im).ConvertToBitmap() |
---|
| 84 | disable_drag_bmp = wx.NullBitmap |
---|
| 85 | self.AddLabelTool(GUIFRAME_ID.DRAG_ID, 'Drag', drag_bmp, |
---|
| 86 | disable_drag_bmp, button_type,'Drag') |
---|
[73b3ae2] | 87 | self.AddSeparator() |
---|
[ba924ed] | 88 | report_im = GUIFRAME_ICON.REPORT_ICON |
---|
| 89 | report_im.Rescale(tbar_size[0], tbar_size[1], wx.IMAGE_QUALITY_HIGH) |
---|
| 90 | report_bmp = report_im.ConvertToBitmap() |
---|
| 91 | #disable_report_bmp = clear_image(report_im).ConvertToBitmap() |
---|
| 92 | disable_report_bmp = wx.NullBitmap |
---|
| 93 | self.AddLabelTool(GUIFRAME_ID.PREVIEW_ID, 'Report', report_bmp, |
---|
| 94 | disable_report_bmp, button_type,'Report') |
---|
[73b3ae2] | 95 | self.AddSeparator() |
---|
[ba924ed] | 96 | print_im = GUIFRAME_ICON.PRINT_ICON |
---|
| 97 | print_im.Rescale(tbar_size[0], tbar_size[1], wx.IMAGE_QUALITY_HIGH) |
---|
| 98 | print_bmp = print_im.ConvertToBitmap() |
---|
| 99 | #disable_print_bmp = clear_image(print_im).ConvertToBitmap() |
---|
| 100 | disable_print_bmp = wx.NullBitmap |
---|
| 101 | self.AddLabelTool(GUIFRAME_ID.PRINT_ID, 'Print', print_bmp, |
---|
| 102 | disable_print_bmp, button_type, 'Print') |
---|
[73b3ae2] | 103 | self.AddSeparator() |
---|
[ba924ed] | 104 | undo_im = GUIFRAME_ICON.UNDO_ICON |
---|
| 105 | undo_im.Rescale(tbar_size[0], tbar_size[1], wx.IMAGE_QUALITY_HIGH) |
---|
| 106 | undo_bmp = undo_im.ConvertToBitmap() |
---|
| 107 | #disable_undo_bmp = clear_image(undo_im).ConvertToBitmap() |
---|
| 108 | disable_undo_bmp = wx.NullBitmap |
---|
| 109 | self.AddLabelTool(GUIFRAME_ID.UNDO_ID, 'Undo', undo_bmp, |
---|
| 110 | disable_undo_bmp, button_type,'Undo') |
---|
[73b3ae2] | 111 | self.AddSeparator() |
---|
[ba924ed] | 112 | redo_im = GUIFRAME_ICON.REDO_ICON |
---|
| 113 | redo_im.Rescale(tbar_size[0], tbar_size[1], wx.IMAGE_QUALITY_HIGH) |
---|
| 114 | redo_bmp = redo_im.ConvertToBitmap() |
---|
| 115 | #disable_redo_bmp = clear_image(redo_im).ConvertToBitmap() |
---|
| 116 | disable_redo_bmp = wx.NullBitmap |
---|
| 117 | self.AddLabelTool(GUIFRAME_ID.REDO_ID, 'Redo', redo_bmp, |
---|
| 118 | disable_redo_bmp, button_type,'Redo') |
---|
[73b3ae2] | 119 | self.AddSeparator() |
---|
[a45037aa] | 120 | #add button for the current application |
---|
[73b3ae2] | 121 | self.button_application = wx.StaticText(self, -1, 'Welcome') |
---|
[a45037aa] | 122 | self.button_application.SetForegroundColour('black') |
---|
| 123 | self.button_application.SetBackgroundColour('#1874CD') |
---|
[ba924ed] | 124 | hint = 'Active Application' |
---|
| 125 | self.button_application.SetToolTipString(hint) |
---|
[a45037aa] | 126 | self.AddControl(self.button_application) |
---|
[73b3ae2] | 127 | self.AddSeparator() |
---|
[a45037aa] | 128 | #add button for the panel on focus |
---|
[73b3ae2] | 129 | self.button_panel = wx.StaticText(self, -1, 'No Panel') |
---|
[a45037aa] | 130 | self.button_panel.SetForegroundColour('black') |
---|
| 131 | self.button_panel.SetBackgroundColour('#90EE90') |
---|
[ba924ed] | 132 | hint = 'Panel on Focus' |
---|
| 133 | self.button_panel.SetToolTipString(hint) |
---|
[a45037aa] | 134 | self.AddControl(self.button_panel) |
---|
[4e4d3bb] | 135 | self._bookmark_bt = PlateButton(self, -1, 'bookmark', |
---|
| 136 | GUIFRAME_ICON.BOOKMARK_ICON.ConvertToBitmap(), |
---|
| 137 | style=PB_STYLE_SQUARE|PB_STYLE_DROPARROW) |
---|
| 138 | self._bookmark_bt.Disable() |
---|
| 139 | self._bookmark_menu = wx.Menu() |
---|
| 140 | id = wx.NewId() |
---|
| 141 | self._bookmark_menu.Append(id, 'Add bookmark') |
---|
| 142 | self._bookmark_menu.AppendSeparator() |
---|
| 143 | wx.EVT_MENU(self, id, self.on_bookmark) |
---|
| 144 | |
---|
| 145 | self._bookmark_bt.SetMenu(self._bookmark_menu) |
---|
| 146 | self.AddControl(self._bookmark_bt) |
---|
[ba924ed] | 147 | |
---|
[010c251] | 148 | self.SetToolBitmapSize(tbar_size) |
---|
| 149 | self.Realize() |
---|
| 150 | |
---|
| 151 | def on_bind_button(self): |
---|
| 152 | """ |
---|
| 153 | """ |
---|
| 154 | if self.parent is not None: |
---|
[4e4d3bb] | 155 | |
---|
[f036c692] | 156 | self.parent.Bind(wx.EVT_TOOL, self.parent.on_redo_panel, |
---|
| 157 | id=GUIFRAME_ID.REDO_ID) |
---|
| 158 | self.parent.Bind(wx.EVT_TOOL, self.parent.on_undo_panel, |
---|
| 159 | id=GUIFRAME_ID.UNDO_ID) |
---|
| 160 | self.parent.Bind(wx.EVT_TOOL, self.parent.on_save_panel, |
---|
| 161 | id=GUIFRAME_ID.SAVE_ID) |
---|
| 162 | self.parent.Bind(wx.EVT_TOOL, self.parent.on_zoom_in_panel, |
---|
| 163 | id=GUIFRAME_ID.ZOOM_IN_ID) |
---|
| 164 | self.parent.Bind(wx.EVT_TOOL, self.parent.on_zoom_out_panel, |
---|
| 165 | id=GUIFRAME_ID.ZOOM_OUT_ID) |
---|
| 166 | self.parent.Bind(wx.EVT_TOOL, self.parent.on_zoom_panel, |
---|
| 167 | id=GUIFRAME_ID.ZOOM_ID) |
---|
| 168 | self.parent.Bind(wx.EVT_TOOL, self.parent.on_drag_panel, |
---|
| 169 | id=GUIFRAME_ID.DRAG_ID) |
---|
| 170 | self.parent.Bind(wx.EVT_TOOL, self.parent.on_reset_panel, |
---|
| 171 | id=GUIFRAME_ID.RESET_ID) |
---|
| 172 | self.parent.Bind(wx.EVT_TOOL, self.parent.on_preview_panel, |
---|
| 173 | id=GUIFRAME_ID.PREVIEW_ID) |
---|
| 174 | self.parent.Bind(wx.EVT_TOOL, self.parent.on_print_panel, |
---|
| 175 | id=GUIFRAME_ID.PRINT_ID) |
---|
[010c251] | 176 | |
---|
[a45037aa] | 177 | def update_button(self, application_name='', panel_name=''): |
---|
[010c251] | 178 | """ |
---|
| 179 | """ |
---|
[a45037aa] | 180 | self.button_application.SetLabel(str(application_name)) |
---|
| 181 | self.button_panel.SetLabel(str(panel_name)) |
---|
[ba924ed] | 182 | |
---|
[a45037aa] | 183 | def update_toolbar(self, panel=None): |
---|
[010c251] | 184 | """ |
---|
| 185 | """ |
---|
| 186 | if panel is None: |
---|
[f036c692] | 187 | self.EnableTool(GUIFRAME_ID.PRINT_ID, False) |
---|
| 188 | self.EnableTool(GUIFRAME_ID.UNDO_ID,False) |
---|
| 189 | self.EnableTool(GUIFRAME_ID.REDO_ID, False) |
---|
| 190 | self.EnableTool(GUIFRAME_ID.ZOOM_ID, False) |
---|
| 191 | self.EnableTool(GUIFRAME_ID.ZOOM_IN_ID, False) |
---|
| 192 | self.EnableTool(GUIFRAME_ID.ZOOM_OUT_ID, False) |
---|
| 193 | self.EnableTool(GUIFRAME_ID.PREVIEW_ID, False) |
---|
| 194 | self.EnableTool(GUIFRAME_ID.SAVE_ID, False) |
---|
| 195 | self.EnableTool(GUIFRAME_ID.DRAG_ID, False) |
---|
| 196 | self.EnableTool(GUIFRAME_ID.RESET_ID, False) |
---|
[4e4d3bb] | 197 | self._bookmark_bt.Disable() |
---|
| 198 | |
---|
[010c251] | 199 | else: |
---|
[f036c692] | 200 | self.EnableTool(GUIFRAME_ID.PRINT_ID, panel.get_print_flag()) |
---|
| 201 | self.EnableTool(GUIFRAME_ID.UNDO_ID, panel.get_undo_flag()) |
---|
| 202 | self.EnableTool(GUIFRAME_ID.REDO_ID, panel.get_redo_flag()) |
---|
| 203 | self.EnableTool(GUIFRAME_ID.ZOOM_ID, panel.get_zoom_flag()) |
---|
| 204 | self.EnableTool(GUIFRAME_ID.ZOOM_IN_ID, panel.get_zoom_in_flag()) |
---|
| 205 | self.EnableTool(GUIFRAME_ID.ZOOM_OUT_ID, panel.get_zoom_out_flag()) |
---|
| 206 | self.EnableTool(GUIFRAME_ID.PREVIEW_ID, panel.get_preview_flag()) |
---|
| 207 | self.EnableTool(GUIFRAME_ID.SAVE_ID, panel.get_save_flag()) |
---|
| 208 | self.EnableTool(GUIFRAME_ID.DRAG_ID, panel.get_drag_flag()) |
---|
| 209 | self.EnableTool(GUIFRAME_ID.RESET_ID, panel.get_reset_flag()) |
---|
[4e4d3bb] | 210 | self._bookmark_bt.Enable(panel.get_bookmark_flag()) |
---|
[ba924ed] | 211 | self.Realize() |
---|
[03314e7] | 212 | |
---|
| 213 | def enable_undo(self, panel): |
---|
| 214 | self.EnableTool(GUIFRAME_ID.PRINT_ID, panel.get_print_flag()) |
---|
| 215 | self.Realize() |
---|
| 216 | |
---|
| 217 | def enable_redo(self, panel): |
---|
| 218 | self.EnableTool(GUIFRAME_ID.REDO_ID, panel.get_redo_flag()) |
---|
| 219 | self.Realize() |
---|
| 220 | |
---|
| 221 | def enable_print(self, panel): |
---|
| 222 | self.EnableTool(GUIFRAME_ID.PRINT_ID, panel.get_print_flag()) |
---|
| 223 | self.Realize() |
---|
| 224 | |
---|
| 225 | def enable_zoom(self, panel): |
---|
| 226 | self.EnableTool(GUIFRAME_ID.ZOOM_ID, panel.get_zoom_flag()) |
---|
| 227 | self.Realize() |
---|
| 228 | |
---|
| 229 | def enable_zoom_in(self, panel): |
---|
| 230 | self.EnableTool(GUIFRAME_ID.ZOOM_IN_ID, panel.get_zoom_in_flag()) |
---|
| 231 | self.Realize() |
---|
| 232 | |
---|
| 233 | def enable_zoom_out(self, panel): |
---|
| 234 | self.EnableTool(GUIFRAME_ID.ZOOM_OUT_ID, panel.get_zoom_out_flag()) |
---|
| 235 | self.Realize() |
---|
| 236 | |
---|
| 237 | def enable_bookmark(self, panel): |
---|
[4e4d3bb] | 238 | flag = panel.get_bookmark_flag() |
---|
| 239 | self._bookmark_bt.Enable(flag) |
---|
[03314e7] | 240 | self.Realize() |
---|
| 241 | |
---|
| 242 | def enable_save(self, panel): |
---|
| 243 | self.EnableTool(GUIFRAME_ID.SAVE_ID, panel.get_save_flag()) |
---|
| 244 | self.Realize() |
---|
| 245 | |
---|
| 246 | def enable_reset(self, panel): |
---|
| 247 | self.EnableTool(GUIFRAME_ID.RESET_ID, panel.get_reset_flag()) |
---|
| 248 | self.Realize() |
---|
| 249 | |
---|
| 250 | def enable_preview(self, panel): |
---|
| 251 | self.EnableTool(GUIFRAME_ID.PREVIEW_ID, panel.get_preview_flag()) |
---|
| 252 | self.Realize() |
---|
[4e4d3bb] | 253 | |
---|
| 254 | def on_bookmark(self, event): |
---|
| 255 | """ |
---|
| 256 | add book mark |
---|
| 257 | """ |
---|
| 258 | if self.parent is not None: |
---|
| 259 | self.parent.on_bookmark_panel(event) |
---|
| 260 | |
---|
| 261 | def append_bookmark(self, event): |
---|
| 262 | """ |
---|
| 263 | receive item to append on the toolbar button bookmark |
---|
| 264 | """ |
---|
| 265 | title = event.title |
---|
| 266 | hint = event.hint |
---|
| 267 | handler = event.handler |
---|
| 268 | id = wx.NewId() |
---|
| 269 | self._bookmark_menu.Append(id, str(title), str(hint)) |
---|
| 270 | wx.EVT_MENU(self, id, handler) |
---|
| 271 | |
---|