[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 |
---|
[1b1bbf9] | 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 | |
---|
[010c251] | 16 | |
---|
[ba924ed] | 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 |
---|
[010c251] | 28 | |
---|
[f036c692] | 29 | class GUIToolBar(Tbar): |
---|
[ba924ed] | 30 | """ |
---|
| 31 | Implement toolbar for guiframe |
---|
| 32 | """ |
---|
[010c251] | 33 | def __init__(self, parent, *args, **kwds): |
---|
| 34 | Tbar.__init__(self, parent, *args, **kwds) |
---|
[1b1bbf9] | 35 | #Set window's font size |
---|
| 36 | self.SetWindowVariant(variant=FONT_VARIANT) |
---|
[010c251] | 37 | self.parent = parent |
---|
[4e4d3bb] | 38 | self._bookmark_menu = None |
---|
| 39 | self._bookmark_bt = None |
---|
[010c251] | 40 | self.do_layout() |
---|
| 41 | self.on_bind_button() |
---|
[ba924ed] | 42 | |
---|
[010c251] | 43 | def do_layout(self): |
---|
| 44 | """ |
---|
| 45 | """ |
---|
[6d727ae] | 46 | tbar_size = (20, 15) |
---|
[ba924ed] | 47 | button_type = wx.ITEM_NORMAL |
---|
[8e4355e] | 48 | |
---|
[041cf9e] | 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, |
---|
[18ec684] | 55 | disable_reset_bmp, button_type,'Reset') |
---|
[041cf9e] | 56 | self.AddSeparator() |
---|
[ba924ed] | 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() |
---|
[73b3ae2] | 60 | self.AddSeparator() |
---|
[f932c02] | 61 | save_im = GUIFRAME_ICON.SAVE_ICON |
---|
| 62 | save_im.Rescale(tbar_size[0], tbar_size[1], wx.IMAGE_QUALITY_HIGH) |
---|
| 63 | save_bmp = save_im.ConvertToBitmap() |
---|
| 64 | disable_save_bmp = wx.NullBitmap |
---|
| 65 | self.AddLabelTool(GUIFRAME_ID.SAVE_ID, 'Save', save_bmp, |
---|
| 66 | disable_save_bmp, button_type,'Save') |
---|
| 67 | self.AddSeparator() |
---|
[ba924ed] | 68 | report_im = GUIFRAME_ICON.REPORT_ICON |
---|
| 69 | report_im.Rescale(tbar_size[0], tbar_size[1], wx.IMAGE_QUALITY_HIGH) |
---|
| 70 | report_bmp = report_im.ConvertToBitmap() |
---|
| 71 | #disable_report_bmp = clear_image(report_im).ConvertToBitmap() |
---|
| 72 | disable_report_bmp = wx.NullBitmap |
---|
| 73 | self.AddLabelTool(GUIFRAME_ID.PREVIEW_ID, 'Report', report_bmp, |
---|
| 74 | disable_report_bmp, button_type,'Report') |
---|
[73b3ae2] | 75 | self.AddSeparator() |
---|
[ba924ed] | 76 | print_im = GUIFRAME_ICON.PRINT_ICON |
---|
| 77 | print_im.Rescale(tbar_size[0], tbar_size[1], wx.IMAGE_QUALITY_HIGH) |
---|
| 78 | print_bmp = print_im.ConvertToBitmap() |
---|
| 79 | #disable_print_bmp = clear_image(print_im).ConvertToBitmap() |
---|
| 80 | disable_print_bmp = wx.NullBitmap |
---|
| 81 | self.AddLabelTool(GUIFRAME_ID.PRINT_ID, 'Print', print_bmp, |
---|
| 82 | disable_print_bmp, button_type, 'Print') |
---|
[73b3ae2] | 83 | self.AddSeparator() |
---|
[ba924ed] | 84 | undo_im = GUIFRAME_ICON.UNDO_ICON |
---|
| 85 | undo_im.Rescale(tbar_size[0], tbar_size[1], wx.IMAGE_QUALITY_HIGH) |
---|
| 86 | undo_bmp = undo_im.ConvertToBitmap() |
---|
| 87 | #disable_undo_bmp = clear_image(undo_im).ConvertToBitmap() |
---|
| 88 | disable_undo_bmp = wx.NullBitmap |
---|
| 89 | self.AddLabelTool(GUIFRAME_ID.UNDO_ID, 'Undo', undo_bmp, |
---|
| 90 | disable_undo_bmp, button_type,'Undo') |
---|
[73b3ae2] | 91 | self.AddSeparator() |
---|
[ba924ed] | 92 | redo_im = GUIFRAME_ICON.REDO_ICON |
---|
| 93 | redo_im.Rescale(tbar_size[0], tbar_size[1], wx.IMAGE_QUALITY_HIGH) |
---|
| 94 | redo_bmp = redo_im.ConvertToBitmap() |
---|
| 95 | #disable_redo_bmp = clear_image(redo_im).ConvertToBitmap() |
---|
| 96 | disable_redo_bmp = wx.NullBitmap |
---|
| 97 | self.AddLabelTool(GUIFRAME_ID.REDO_ID, 'Redo', redo_bmp, |
---|
| 98 | disable_redo_bmp, button_type,'Redo') |
---|
[73b3ae2] | 99 | self.AddSeparator() |
---|
[a45037aa] | 100 | #add button for the current application |
---|
[a1a252e] | 101 | #self.button_application = wx.StaticText(self, -1, 'Welcome') |
---|
| 102 | #self.button_application.SetForegroundColour('black') |
---|
| 103 | #self.button_application.SetBackgroundColour('#1874CD') |
---|
| 104 | #hint = 'Active Application' |
---|
| 105 | #self.button_application.SetToolTipString(hint) |
---|
| 106 | #self.AddControl(self.button_application) |
---|
| 107 | #self.AddSeparator() |
---|
| 108 | |
---|
[4e4d3bb] | 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() |
---|
[fadf925] | 114 | self.add_bookmark_default() |
---|
| 115 | |
---|
[4e4d3bb] | 116 | |
---|
| 117 | self._bookmark_bt.SetMenu(self._bookmark_menu) |
---|
| 118 | self.AddControl(self._bookmark_bt) |
---|
[fadf925] | 119 | |
---|
[010c251] | 120 | self.SetToolBitmapSize(tbar_size) |
---|
[a1a252e] | 121 | self.AddSeparator() |
---|
| 122 | #add button for the panel on focus |
---|
| 123 | self.button_panel = wx.StaticText(self, -1, 'No Panel', |
---|
| 124 | style=wx.SUNKEN_BORDER|wx.ALIGN_LEFT) |
---|
| 125 | #self.button_panel.SetForegroundColour('black') |
---|
| 126 | #self.button_panel.SetBackgroundColour('white') |
---|
| 127 | button_panel_font = self.button_panel.GetFont() |
---|
| 128 | button_panel_font.SetWeight(wx.BOLD) |
---|
| 129 | self.button_panel.SetFont(button_panel_font) |
---|
| 130 | #self.button_panel.SetClientSize((80,20)) |
---|
| 131 | hint = 'Control Panel on Focus' |
---|
| 132 | self.button_panel.SetToolTipString(hint) |
---|
| 133 | self.AddControl(self.button_panel) |
---|
| 134 | |
---|
[010c251] | 135 | self.Realize() |
---|
[fadf925] | 136 | |
---|
| 137 | def add_bookmark_default(self): |
---|
| 138 | """ |
---|
| 139 | Add default items in bookmark menu |
---|
| 140 | """ |
---|
| 141 | id = wx.NewId() |
---|
| 142 | self._bookmark_menu.Append(id, 'Add bookmark') |
---|
| 143 | self._bookmark_menu.AppendSeparator() |
---|
| 144 | wx.EVT_MENU(self, id, self.on_bookmark) |
---|
| 145 | |
---|
[010c251] | 146 | def on_bind_button(self): |
---|
| 147 | """ |
---|
| 148 | """ |
---|
| 149 | if self.parent is not None: |
---|
[4e4d3bb] | 150 | |
---|
[f036c692] | 151 | self.parent.Bind(wx.EVT_TOOL, self.parent.on_redo_panel, |
---|
| 152 | id=GUIFRAME_ID.REDO_ID) |
---|
| 153 | self.parent.Bind(wx.EVT_TOOL, self.parent.on_undo_panel, |
---|
| 154 | id=GUIFRAME_ID.UNDO_ID) |
---|
| 155 | self.parent.Bind(wx.EVT_TOOL, self.parent.on_reset_panel, |
---|
| 156 | id=GUIFRAME_ID.RESET_ID) |
---|
[f932c02] | 157 | self.parent.Bind(wx.EVT_TOOL, self.parent.on_save_panel, |
---|
| 158 | id=GUIFRAME_ID.SAVE_ID) |
---|
[f036c692] | 159 | self.parent.Bind(wx.EVT_TOOL, self.parent.on_preview_panel, |
---|
| 160 | id=GUIFRAME_ID.PREVIEW_ID) |
---|
| 161 | self.parent.Bind(wx.EVT_TOOL, self.parent.on_print_panel, |
---|
| 162 | id=GUIFRAME_ID.PRINT_ID) |
---|
[010c251] | 163 | |
---|
[a45037aa] | 164 | def update_button(self, application_name='', panel_name=''): |
---|
[010c251] | 165 | """ |
---|
| 166 | """ |
---|
[a1a252e] | 167 | #self.button_application.SetLabel(str(application_name)) |
---|
[a45037aa] | 168 | self.button_panel.SetLabel(str(panel_name)) |
---|
[ba924ed] | 169 | |
---|
[a45037aa] | 170 | def update_toolbar(self, panel=None): |
---|
[010c251] | 171 | """ |
---|
| 172 | """ |
---|
| 173 | if panel is None: |
---|
[f036c692] | 174 | self.EnableTool(GUIFRAME_ID.PRINT_ID, False) |
---|
| 175 | self.EnableTool(GUIFRAME_ID.UNDO_ID,False) |
---|
| 176 | self.EnableTool(GUIFRAME_ID.REDO_ID, False) |
---|
| 177 | self.EnableTool(GUIFRAME_ID.PREVIEW_ID, False) |
---|
| 178 | self.EnableTool(GUIFRAME_ID.RESET_ID, False) |
---|
[f932c02] | 179 | self.EnableTool(GUIFRAME_ID.SAVE_ID, False) |
---|
[4e4d3bb] | 180 | self._bookmark_bt.Disable() |
---|
| 181 | |
---|
[010c251] | 182 | else: |
---|
[f036c692] | 183 | self.EnableTool(GUIFRAME_ID.PRINT_ID, panel.get_print_flag()) |
---|
| 184 | self.EnableTool(GUIFRAME_ID.UNDO_ID, panel.get_undo_flag()) |
---|
| 185 | self.EnableTool(GUIFRAME_ID.REDO_ID, panel.get_redo_flag()) |
---|
| 186 | self.EnableTool(GUIFRAME_ID.PREVIEW_ID, panel.get_preview_flag()) |
---|
| 187 | self.EnableTool(GUIFRAME_ID.RESET_ID, panel.get_reset_flag()) |
---|
[f932c02] | 188 | self.EnableTool(GUIFRAME_ID.SAVE_ID, panel.get_save_flag()) |
---|
[4e4d3bb] | 189 | self._bookmark_bt.Enable(panel.get_bookmark_flag()) |
---|
[ba924ed] | 190 | self.Realize() |
---|
[03314e7] | 191 | |
---|
| 192 | def enable_undo(self, panel): |
---|
[ecc85443] | 193 | self.EnableTool(GUIFRAME_ID.UNDO_ID, panel.get_undo_flag()) |
---|
[03314e7] | 194 | self.Realize() |
---|
| 195 | |
---|
| 196 | def enable_redo(self, panel): |
---|
| 197 | self.EnableTool(GUIFRAME_ID.REDO_ID, panel.get_redo_flag()) |
---|
| 198 | self.Realize() |
---|
| 199 | |
---|
| 200 | def enable_print(self, panel): |
---|
| 201 | self.EnableTool(GUIFRAME_ID.PRINT_ID, panel.get_print_flag()) |
---|
| 202 | self.Realize() |
---|
| 203 | |
---|
| 204 | def enable_zoom(self, panel): |
---|
| 205 | self.EnableTool(GUIFRAME_ID.ZOOM_ID, panel.get_zoom_flag()) |
---|
| 206 | self.Realize() |
---|
| 207 | |
---|
| 208 | def enable_zoom_in(self, panel): |
---|
| 209 | self.EnableTool(GUIFRAME_ID.ZOOM_IN_ID, panel.get_zoom_in_flag()) |
---|
| 210 | self.Realize() |
---|
| 211 | |
---|
| 212 | def enable_zoom_out(self, panel): |
---|
| 213 | self.EnableTool(GUIFRAME_ID.ZOOM_OUT_ID, panel.get_zoom_out_flag()) |
---|
| 214 | self.Realize() |
---|
| 215 | |
---|
| 216 | def enable_bookmark(self, panel): |
---|
[4e4d3bb] | 217 | flag = panel.get_bookmark_flag() |
---|
| 218 | self._bookmark_bt.Enable(flag) |
---|
[03314e7] | 219 | self.Realize() |
---|
| 220 | |
---|
| 221 | def enable_save(self, panel): |
---|
| 222 | self.EnableTool(GUIFRAME_ID.SAVE_ID, panel.get_save_flag()) |
---|
| 223 | self.Realize() |
---|
| 224 | |
---|
| 225 | def enable_reset(self, panel): |
---|
| 226 | self.EnableTool(GUIFRAME_ID.RESET_ID, panel.get_reset_flag()) |
---|
| 227 | self.Realize() |
---|
| 228 | |
---|
| 229 | def enable_preview(self, panel): |
---|
| 230 | self.EnableTool(GUIFRAME_ID.PREVIEW_ID, panel.get_preview_flag()) |
---|
| 231 | self.Realize() |
---|
[4e4d3bb] | 232 | |
---|
| 233 | def on_bookmark(self, event): |
---|
| 234 | """ |
---|
| 235 | add book mark |
---|
| 236 | """ |
---|
| 237 | if self.parent is not None: |
---|
| 238 | self.parent.on_bookmark_panel(event) |
---|
| 239 | |
---|
| 240 | def append_bookmark(self, event): |
---|
| 241 | """ |
---|
| 242 | receive item to append on the toolbar button bookmark |
---|
| 243 | """ |
---|
| 244 | title = event.title |
---|
| 245 | hint = event.hint |
---|
| 246 | handler = event.handler |
---|
| 247 | id = wx.NewId() |
---|
| 248 | self._bookmark_menu.Append(id, str(title), str(hint)) |
---|
| 249 | wx.EVT_MENU(self, id, handler) |
---|
[fadf925] | 250 | |
---|
| 251 | def remove_bookmark_item(self, item): |
---|
| 252 | """ |
---|
| 253 | Remove a bookmark item |
---|
| 254 | """ |
---|
| 255 | self._bookmark_menu.DestroyItem(item) |
---|
| 256 | |
---|
| 257 | def get_bookmark_items(self): |
---|
| 258 | """ |
---|
| 259 | Get bookmark menu items |
---|
| 260 | """ |
---|
| 261 | return self._bookmark_menu.GetMenuItems() |
---|
| 262 | |
---|
| 263 | def append_bookmark_item(self, id, label): |
---|
| 264 | """ |
---|
| 265 | Append a item in bookmark |
---|
| 266 | """ |
---|
| 267 | self._bookmark_menu.Append(id, label) |
---|