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