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