[e4f773e] | 1 | |
---|
| 2 | import wx |
---|
| 3 | import os |
---|
| 4 | from wx import ToolBar as Tbar |
---|
| 5 | from wx.lib.platebtn import PlateButton |
---|
| 6 | from sans.guiframe.gui_style import GUIFRAME_ID |
---|
| 7 | from sans.guiframe.gui_style import GUIFRAME_ICON |
---|
| 8 | from wx.lib.platebtn import PB_STYLE_SQUARE, PB_STYLE_DROPARROW |
---|
| 9 | #Control panel width |
---|
| 10 | import sys |
---|
| 11 | if sys.platform.count("darwin")==0: |
---|
| 12 | FONT_VARIANT = 0 |
---|
| 13 | NAME_BOX = wx.DefaultSize |
---|
| 14 | TB_H = 21 |
---|
| 15 | IS_MAC = False |
---|
| 16 | else: |
---|
| 17 | FONT_VARIANT = 1 |
---|
| 18 | NAME_BOX = (200, 25) |
---|
| 19 | TB_H = 25 |
---|
| 20 | IS_MAC = True |
---|
| 21 | |
---|
| 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 |
---|
| 33 | |
---|
| 34 | class GUIToolBar(Tbar): |
---|
| 35 | """ |
---|
| 36 | Implement toolbar for guiframe |
---|
| 37 | """ |
---|
| 38 | def __init__(self, parent, *args, **kwds): |
---|
| 39 | Tbar.__init__(self, parent, *args, **kwds) |
---|
| 40 | #Set window's font size |
---|
| 41 | self.SetWindowVariant(variant=FONT_VARIANT) |
---|
| 42 | self.parent = parent |
---|
| 43 | self._bookmark_menu = None |
---|
| 44 | self._bookmark_bt = None |
---|
| 45 | self.do_layout() |
---|
| 46 | self.on_bind_button() |
---|
| 47 | |
---|
| 48 | def do_layout(self): |
---|
| 49 | """ |
---|
| 50 | """ |
---|
| 51 | t_size = TB_H |
---|
| 52 | tbar_size = (t_size, t_size) |
---|
| 53 | button_type = wx.ITEM_NORMAL |
---|
| 54 | |
---|
| 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, |
---|
| 61 | disable_reset_bmp, button_type,'Reset') |
---|
| 62 | self.AddSeparator() |
---|
| 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() |
---|
| 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') |
---|
| 77 | self.AddSeparator() |
---|
| 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() |
---|
| 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') |
---|
| 93 | self.AddSeparator() |
---|
| 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') |
---|
| 101 | self.AddSeparator() |
---|
| 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() |
---|
| 119 | #add button for the current application |
---|
| 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 | |
---|
| 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() |
---|
| 133 | self.add_bookmark_default() |
---|
| 134 | |
---|
| 135 | |
---|
| 136 | self._bookmark_bt.SetMenu(self._bookmark_menu) |
---|
| 137 | self.AddControl(self._bookmark_bt) |
---|
| 138 | |
---|
| 139 | self.SetToolBitmapSize(tbar_size) |
---|
| 140 | self.AddSeparator() |
---|
| 141 | #add button for the panel on focus |
---|
| 142 | self.button_panel = wx.StaticText(self, -1, 'No Panel', |
---|
| 143 | pos=wx.DefaultPosition, |
---|
| 144 | size=NAME_BOX, |
---|
| 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) |
---|
| 155 | self.AddSeparator() |
---|
| 156 | self.Realize() |
---|
| 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 | |
---|
| 167 | def on_bind_button(self): |
---|
| 168 | """ |
---|
| 169 | """ |
---|
| 170 | if self.parent is not None: |
---|
| 171 | |
---|
| 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) |
---|
| 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) |
---|
| 180 | self.parent.Bind(wx.EVT_TOOL, self.parent.on_reset_panel, |
---|
| 181 | id=GUIFRAME_ID.RESET_ID) |
---|
| 182 | self.parent.Bind(wx.EVT_TOOL, self.parent.on_save_panel, |
---|
| 183 | id=GUIFRAME_ID.SAVE_ID) |
---|
| 184 | self.parent.Bind(wx.EVT_TOOL, self.parent.on_preview_panel, |
---|
| 185 | id=GUIFRAME_ID.PREVIEW_ID) |
---|
| 186 | #self.parent.Bind(wx.EVT_TOOL, self.parent.on_print_panel, |
---|
| 187 | # id=GUIFRAME_ID.PRINT_ID) |
---|
| 188 | |
---|
| 189 | def update_button(self, application_name='', panel_name=''): |
---|
| 190 | """ |
---|
| 191 | """ |
---|
| 192 | #self.button_application.SetLabel(str(application_name)) |
---|
| 193 | self.button_panel.SetLabel(str(panel_name)) |
---|
| 194 | self.button_panel.SetToolTipString(str(panel_name)) |
---|
| 195 | |
---|
| 196 | def update_toolbar(self, panel=None): |
---|
| 197 | """ |
---|
| 198 | """ |
---|
| 199 | if panel is None: |
---|
| 200 | #self.EnableTool(GUIFRAME_ID.PRINT_ID, False) |
---|
| 201 | self.EnableTool(GUIFRAME_ID.UNDO_ID,False) |
---|
| 202 | self.EnableTool(GUIFRAME_ID.REDO_ID, False) |
---|
| 203 | self.EnableTool(GUIFRAME_ID.COPY_ID,False) |
---|
| 204 | self.EnableTool(GUIFRAME_ID.PASTE_ID, False) |
---|
| 205 | self.EnableTool(GUIFRAME_ID.PREVIEW_ID, False) |
---|
| 206 | self.EnableTool(GUIFRAME_ID.RESET_ID, False) |
---|
| 207 | self.EnableTool(GUIFRAME_ID.SAVE_ID, False) |
---|
| 208 | self._bookmark_bt.Disable() |
---|
| 209 | |
---|
| 210 | else: |
---|
| 211 | #self.EnableTool(GUIFRAME_ID.PRINT_ID, panel.get_print_flag()) |
---|
| 212 | self.EnableTool(GUIFRAME_ID.UNDO_ID, panel.get_undo_flag()) |
---|
| 213 | self.EnableTool(GUIFRAME_ID.REDO_ID, panel.get_redo_flag()) |
---|
| 214 | self.EnableTool(GUIFRAME_ID.COPY_ID, panel.get_copy_flag()) |
---|
| 215 | self.EnableTool(GUIFRAME_ID.PASTE_ID, panel.get_paste_flag()) |
---|
| 216 | self.EnableTool(GUIFRAME_ID.PREVIEW_ID, panel.get_preview_flag()) |
---|
| 217 | self.EnableTool(GUIFRAME_ID.RESET_ID, panel.get_reset_flag()) |
---|
| 218 | self.EnableTool(GUIFRAME_ID.SAVE_ID, panel.get_save_flag()) |
---|
| 219 | self._bookmark_bt.Enable(panel.get_bookmark_flag()) |
---|
| 220 | self.Realize() |
---|
| 221 | |
---|
| 222 | def enable_undo(self, panel): |
---|
| 223 | self.EnableTool(GUIFRAME_ID.UNDO_ID, panel.get_undo_flag()) |
---|
| 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 | |
---|
| 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 | |
---|
| 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): |
---|
| 255 | flag = panel.get_bookmark_flag() |
---|
| 256 | self._bookmark_bt.Enable(flag) |
---|
| 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() |
---|
| 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) |
---|
| 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) |
---|