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