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