[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 |
---|
[ba924ed] | 9 | |
---|
[010c251] | 10 | |
---|
[ba924ed] | 11 | def clear_image(image): |
---|
| 12 | w, h = image.GetSize() |
---|
| 13 | factor = 155 |
---|
| 14 | compress = lambda x: int(x * factor/255.) + factor |
---|
| 15 | for y in range(h): |
---|
| 16 | for x in range(w): |
---|
| 17 | grey = compress(image.GetGreen(x, y)) |
---|
| 18 | image.SetRGB(x, y, grey, grey, grey) |
---|
| 19 | if image.HasAlpha(): |
---|
| 20 | image.ConvertAlphaToMask() |
---|
| 21 | return image |
---|
[010c251] | 22 | |
---|
[f036c692] | 23 | class GUIToolBar(Tbar): |
---|
[ba924ed] | 24 | """ |
---|
| 25 | Implement toolbar for guiframe |
---|
| 26 | """ |
---|
[010c251] | 27 | def __init__(self, parent, *args, **kwds): |
---|
| 28 | Tbar.__init__(self, parent, *args, **kwds) |
---|
| 29 | self.parent = parent |
---|
[4e4d3bb] | 30 | self._bookmark_menu = None |
---|
| 31 | self._bookmark_bt = None |
---|
[010c251] | 32 | self.do_layout() |
---|
| 33 | self.on_bind_button() |
---|
[ba924ed] | 34 | |
---|
[010c251] | 35 | def do_layout(self): |
---|
| 36 | """ |
---|
| 37 | """ |
---|
[ba924ed] | 38 | tbar_size = (22, 22) |
---|
| 39 | button_type = wx.ITEM_NORMAL |
---|
[8e4355e] | 40 | |
---|
[73b3ae2] | 41 | self.AddSeparator() |
---|
[041cf9e] | 42 | reset_im = GUIFRAME_ICON.RESET_ICON |
---|
| 43 | reset_im.Rescale(tbar_size[0], tbar_size[1], wx.IMAGE_QUALITY_HIGH) |
---|
| 44 | reset_bmp = reset_im.ConvertToBitmap() |
---|
| 45 | #disable_reset_bmp = clear_image(reset_im).ConvertToBitmap() |
---|
| 46 | disable_reset_bmp = wx.NullBitmap |
---|
| 47 | self.AddLabelTool(GUIFRAME_ID.RESET_ID, 'Reset', reset_bmp, |
---|
[18ec684] | 48 | disable_reset_bmp, button_type,'Reset') |
---|
[041cf9e] | 49 | self.AddSeparator() |
---|
[ba924ed] | 50 | drag_im = GUIFRAME_ICON.DRAG_ICON |
---|
| 51 | drag_im.Rescale(tbar_size[0], tbar_size[1], wx.IMAGE_QUALITY_HIGH) |
---|
| 52 | drag_bmp = drag_im.ConvertToBitmap() |
---|
[73b3ae2] | 53 | self.AddSeparator() |
---|
[ba924ed] | 54 | report_im = GUIFRAME_ICON.REPORT_ICON |
---|
| 55 | report_im.Rescale(tbar_size[0], tbar_size[1], wx.IMAGE_QUALITY_HIGH) |
---|
| 56 | report_bmp = report_im.ConvertToBitmap() |
---|
| 57 | #disable_report_bmp = clear_image(report_im).ConvertToBitmap() |
---|
| 58 | disable_report_bmp = wx.NullBitmap |
---|
| 59 | self.AddLabelTool(GUIFRAME_ID.PREVIEW_ID, 'Report', report_bmp, |
---|
| 60 | disable_report_bmp, button_type,'Report') |
---|
[73b3ae2] | 61 | self.AddSeparator() |
---|
[ba924ed] | 62 | print_im = GUIFRAME_ICON.PRINT_ICON |
---|
| 63 | print_im.Rescale(tbar_size[0], tbar_size[1], wx.IMAGE_QUALITY_HIGH) |
---|
| 64 | print_bmp = print_im.ConvertToBitmap() |
---|
| 65 | #disable_print_bmp = clear_image(print_im).ConvertToBitmap() |
---|
| 66 | disable_print_bmp = wx.NullBitmap |
---|
| 67 | self.AddLabelTool(GUIFRAME_ID.PRINT_ID, 'Print', print_bmp, |
---|
| 68 | disable_print_bmp, button_type, 'Print') |
---|
[73b3ae2] | 69 | self.AddSeparator() |
---|
[ba924ed] | 70 | undo_im = GUIFRAME_ICON.UNDO_ICON |
---|
| 71 | undo_im.Rescale(tbar_size[0], tbar_size[1], wx.IMAGE_QUALITY_HIGH) |
---|
| 72 | undo_bmp = undo_im.ConvertToBitmap() |
---|
| 73 | #disable_undo_bmp = clear_image(undo_im).ConvertToBitmap() |
---|
| 74 | disable_undo_bmp = wx.NullBitmap |
---|
| 75 | self.AddLabelTool(GUIFRAME_ID.UNDO_ID, 'Undo', undo_bmp, |
---|
| 76 | disable_undo_bmp, button_type,'Undo') |
---|
[73b3ae2] | 77 | self.AddSeparator() |
---|
[ba924ed] | 78 | redo_im = GUIFRAME_ICON.REDO_ICON |
---|
| 79 | redo_im.Rescale(tbar_size[0], tbar_size[1], wx.IMAGE_QUALITY_HIGH) |
---|
| 80 | redo_bmp = redo_im.ConvertToBitmap() |
---|
| 81 | #disable_redo_bmp = clear_image(redo_im).ConvertToBitmap() |
---|
| 82 | disable_redo_bmp = wx.NullBitmap |
---|
| 83 | self.AddLabelTool(GUIFRAME_ID.REDO_ID, 'Redo', redo_bmp, |
---|
| 84 | disable_redo_bmp, button_type,'Redo') |
---|
[73b3ae2] | 85 | self.AddSeparator() |
---|
[a45037aa] | 86 | #add button for the current application |
---|
[73b3ae2] | 87 | self.button_application = wx.StaticText(self, -1, 'Welcome') |
---|
[a45037aa] | 88 | self.button_application.SetForegroundColour('black') |
---|
| 89 | self.button_application.SetBackgroundColour('#1874CD') |
---|
[ba924ed] | 90 | hint = 'Active Application' |
---|
| 91 | self.button_application.SetToolTipString(hint) |
---|
[a45037aa] | 92 | self.AddControl(self.button_application) |
---|
[73b3ae2] | 93 | self.AddSeparator() |
---|
[a45037aa] | 94 | #add button for the panel on focus |
---|
[73b3ae2] | 95 | self.button_panel = wx.StaticText(self, -1, 'No Panel') |
---|
[a45037aa] | 96 | self.button_panel.SetForegroundColour('black') |
---|
| 97 | self.button_panel.SetBackgroundColour('#90EE90') |
---|
[ba924ed] | 98 | hint = 'Panel on Focus' |
---|
| 99 | self.button_panel.SetToolTipString(hint) |
---|
[a45037aa] | 100 | self.AddControl(self.button_panel) |
---|
[4e4d3bb] | 101 | self._bookmark_bt = PlateButton(self, -1, 'bookmark', |
---|
| 102 | GUIFRAME_ICON.BOOKMARK_ICON.ConvertToBitmap(), |
---|
| 103 | style=PB_STYLE_SQUARE|PB_STYLE_DROPARROW) |
---|
| 104 | self._bookmark_bt.Disable() |
---|
| 105 | self._bookmark_menu = wx.Menu() |
---|
| 106 | id = wx.NewId() |
---|
| 107 | self._bookmark_menu.Append(id, 'Add bookmark') |
---|
| 108 | self._bookmark_menu.AppendSeparator() |
---|
| 109 | wx.EVT_MENU(self, id, self.on_bookmark) |
---|
| 110 | |
---|
| 111 | self._bookmark_bt.SetMenu(self._bookmark_menu) |
---|
| 112 | self.AddControl(self._bookmark_bt) |
---|
[ba924ed] | 113 | |
---|
[010c251] | 114 | self.SetToolBitmapSize(tbar_size) |
---|
| 115 | self.Realize() |
---|
| 116 | |
---|
| 117 | def on_bind_button(self): |
---|
| 118 | """ |
---|
| 119 | """ |
---|
| 120 | if self.parent is not None: |
---|
[4e4d3bb] | 121 | |
---|
[f036c692] | 122 | self.parent.Bind(wx.EVT_TOOL, self.parent.on_redo_panel, |
---|
| 123 | id=GUIFRAME_ID.REDO_ID) |
---|
| 124 | self.parent.Bind(wx.EVT_TOOL, self.parent.on_undo_panel, |
---|
| 125 | id=GUIFRAME_ID.UNDO_ID) |
---|
| 126 | self.parent.Bind(wx.EVT_TOOL, self.parent.on_reset_panel, |
---|
| 127 | id=GUIFRAME_ID.RESET_ID) |
---|
| 128 | self.parent.Bind(wx.EVT_TOOL, self.parent.on_preview_panel, |
---|
| 129 | id=GUIFRAME_ID.PREVIEW_ID) |
---|
| 130 | self.parent.Bind(wx.EVT_TOOL, self.parent.on_print_panel, |
---|
| 131 | id=GUIFRAME_ID.PRINT_ID) |
---|
[010c251] | 132 | |
---|
[a45037aa] | 133 | def update_button(self, application_name='', panel_name=''): |
---|
[010c251] | 134 | """ |
---|
| 135 | """ |
---|
[a45037aa] | 136 | self.button_application.SetLabel(str(application_name)) |
---|
| 137 | self.button_panel.SetLabel(str(panel_name)) |
---|
[ba924ed] | 138 | |
---|
[a45037aa] | 139 | def update_toolbar(self, panel=None): |
---|
[010c251] | 140 | """ |
---|
| 141 | """ |
---|
| 142 | if panel is None: |
---|
[f036c692] | 143 | self.EnableTool(GUIFRAME_ID.PRINT_ID, False) |
---|
| 144 | self.EnableTool(GUIFRAME_ID.UNDO_ID,False) |
---|
| 145 | self.EnableTool(GUIFRAME_ID.REDO_ID, False) |
---|
| 146 | self.EnableTool(GUIFRAME_ID.PREVIEW_ID, False) |
---|
| 147 | self.EnableTool(GUIFRAME_ID.RESET_ID, False) |
---|
[4e4d3bb] | 148 | self._bookmark_bt.Disable() |
---|
| 149 | |
---|
[010c251] | 150 | else: |
---|
[f036c692] | 151 | self.EnableTool(GUIFRAME_ID.PRINT_ID, panel.get_print_flag()) |
---|
| 152 | self.EnableTool(GUIFRAME_ID.UNDO_ID, panel.get_undo_flag()) |
---|
| 153 | self.EnableTool(GUIFRAME_ID.REDO_ID, panel.get_redo_flag()) |
---|
| 154 | self.EnableTool(GUIFRAME_ID.PREVIEW_ID, panel.get_preview_flag()) |
---|
| 155 | self.EnableTool(GUIFRAME_ID.RESET_ID, panel.get_reset_flag()) |
---|
[4e4d3bb] | 156 | self._bookmark_bt.Enable(panel.get_bookmark_flag()) |
---|
[ba924ed] | 157 | self.Realize() |
---|
[03314e7] | 158 | |
---|
| 159 | def enable_undo(self, panel): |
---|
[ecc85443] | 160 | self.EnableTool(GUIFRAME_ID.UNDO_ID, panel.get_undo_flag()) |
---|
[03314e7] | 161 | self.Realize() |
---|
| 162 | |
---|
| 163 | def enable_redo(self, panel): |
---|
| 164 | self.EnableTool(GUIFRAME_ID.REDO_ID, panel.get_redo_flag()) |
---|
| 165 | self.Realize() |
---|
| 166 | |
---|
| 167 | def enable_print(self, panel): |
---|
| 168 | self.EnableTool(GUIFRAME_ID.PRINT_ID, panel.get_print_flag()) |
---|
| 169 | self.Realize() |
---|
| 170 | |
---|
| 171 | def enable_zoom(self, panel): |
---|
| 172 | self.EnableTool(GUIFRAME_ID.ZOOM_ID, panel.get_zoom_flag()) |
---|
| 173 | self.Realize() |
---|
| 174 | |
---|
| 175 | def enable_zoom_in(self, panel): |
---|
| 176 | self.EnableTool(GUIFRAME_ID.ZOOM_IN_ID, panel.get_zoom_in_flag()) |
---|
| 177 | self.Realize() |
---|
| 178 | |
---|
| 179 | def enable_zoom_out(self, panel): |
---|
| 180 | self.EnableTool(GUIFRAME_ID.ZOOM_OUT_ID, panel.get_zoom_out_flag()) |
---|
| 181 | self.Realize() |
---|
| 182 | |
---|
| 183 | def enable_bookmark(self, panel): |
---|
[4e4d3bb] | 184 | flag = panel.get_bookmark_flag() |
---|
| 185 | self._bookmark_bt.Enable(flag) |
---|
[03314e7] | 186 | self.Realize() |
---|
| 187 | |
---|
| 188 | def enable_save(self, panel): |
---|
| 189 | self.EnableTool(GUIFRAME_ID.SAVE_ID, panel.get_save_flag()) |
---|
| 190 | self.Realize() |
---|
| 191 | |
---|
| 192 | def enable_reset(self, panel): |
---|
| 193 | self.EnableTool(GUIFRAME_ID.RESET_ID, panel.get_reset_flag()) |
---|
| 194 | self.Realize() |
---|
| 195 | |
---|
| 196 | def enable_preview(self, panel): |
---|
| 197 | self.EnableTool(GUIFRAME_ID.PREVIEW_ID, panel.get_preview_flag()) |
---|
| 198 | self.Realize() |
---|
[4e4d3bb] | 199 | |
---|
| 200 | def on_bookmark(self, event): |
---|
| 201 | """ |
---|
| 202 | add book mark |
---|
| 203 | """ |
---|
| 204 | if self.parent is not None: |
---|
| 205 | self.parent.on_bookmark_panel(event) |
---|
| 206 | |
---|
| 207 | def append_bookmark(self, event): |
---|
| 208 | """ |
---|
| 209 | receive item to append on the toolbar button bookmark |
---|
| 210 | """ |
---|
| 211 | title = event.title |
---|
| 212 | hint = event.hint |
---|
| 213 | handler = event.handler |
---|
| 214 | id = wx.NewId() |
---|
| 215 | self._bookmark_menu.Append(id, str(title), str(hint)) |
---|
| 216 | wx.EVT_MENU(self, id, handler) |
---|
| 217 | |
---|