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