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