📄 toolbar.py
字号:
self.bitmap2.startDirectory = directory def remove_tool(self, event): """\ Event handler called when the Remove button is clicked """ if self.selected_index >= 0: for s in (self.id, self.label, self.help_str, self.long_help_str, self.event_handler): s.SetValue("") for s in (self.bitmap1, self.bitmap2): s.SetValue("", False) self.check_radio.SetSelection(0) self.tool_items.DeleteItem(self.selected_index) if not self.tool_items.GetItemCount(): for s in (self.id, self.label, self.help_str, self.long_help_str, self.bitmap1, self.bitmap2, self.check_radio, self.event_handler): s.Enable(False) def add_tools(self, tools): """\ adds the content of 'tools' to self.tool_items. tools is a sequence of (simple) tool items for the toolbar. At the moment there is no control support, but I hope to add it soon """ set_item = self.tool_items.SetStringItem add_item = self.tool_items.InsertStringItem index = [0] def add(tool): i = index[0] add_item(i, misc.wxstr(tool.label)) set_item(i, 1, misc.wxstr(tool.id)) set_item(i, 2, misc.wxstr(tool.bitmap1)) set_item(i, 3, misc.wxstr(tool.bitmap2)) set_item(i, 4, misc.wxstr(tool.short_help)) set_item(i, 5, misc.wxstr(tool.long_help)) set_item(i, 7, misc.wxstr(tool.handler)) item_type = 0 set_item(i, 6, misc.wxstr(tool.type)) index[0] += 1 for tool in tools: add(tool) if self.tool_items.GetItemCount(): for s in (self.id, self.label, self.help_str, self.long_help_str, self.bitmap1, self.bitmap2, self.check_radio, self.event_handler): s.Enable(True) def get_tools(self): """\ returns the contents of self.tool_items as a list of tools that describes the contents of the ToolBar """ def get(i, j): return self.tool_items.GetItem(i, j).m_text tools = [] def add(index): label = get(index, 0) id = get(index, 1) bitmap1 = get(index, 2) bitmap2 = get(index, 3) short_help = get(index, 4) long_help = get(index, 5) event_handler = get(index, 7) try: item_type = int(get(index, 6)) except ValueError: item_type = 0 tools.append(Tool(label=label, id=id, type=item_type, short_help=short_help, long_help=long_help, bitmap1=bitmap1, bitmap2=bitmap2, handler=event_handler)) for index in range(self.tool_items.GetItemCount()): add(index) return tools def move_item_up(self, event): """\ moves the selected tool before the previous one at the same level in self.tool_items """ self.tool_items.SetFocus() if self.selected_index > 0: index = self.selected_index - 1 vals1 = [ self.tool_items.GetItem(self.selected_index, i).m_text \ for i in range(8) ] vals2 = [ self.tool_items.GetItem(index, i).m_text \ for i in range(8) ] for i in range(8): self.tool_items.SetStringItem(index, i, vals1[i]) self.tool_items.SetStringItem(self.selected_index, i, vals2[i]) state = wx.LIST_STATE_SELECTED | wx.LIST_STATE_FOCUSED self.tool_items.SetItemState(index, state, state) self.selected_index = index def move_item_down(self, event): """\ moves the selected tool after the next one at the same level in self.tool_items """ self.tool_items.SetFocus() if self.selected_index < self.tool_items.GetItemCount()-1: index = self.selected_index + 1 vals1 = [ self.tool_items.GetItem(self.selected_index, i).m_text \ for i in range(8) ] vals2 = [ self.tool_items.GetItem(index, i).m_text \ for i in range(8) ] for i in range(8): self.tool_items.SetStringItem(index, i, vals1[i]) self.tool_items.SetStringItem(self.selected_index, i, vals2[i]) state = wx.LIST_STATE_SELECTED | wx.LIST_STATE_FOCUSED self.tool_items.SetItemState(index, state, state) self.selected_index = index def on_apply(self, event): self.owner.set_tools(self.get_tools()) common.app_tree.app.saved = False# end of class ToolsDialogclass ToolsProperty(Property): """\ Property to edit the tools of an EditToolBar instance. """ def __init__(self, owner, name, parent): Property.__init__(self, owner, name, parent) self.panel = None self.tools = {} if parent is not None: self.display(parent) def display(self, parent): self.panel = wx.Panel(parent, -1) edit_btn_id = wx.NewId() self.edit_btn = wx.Button(self.panel, edit_btn_id, _("Edit tools...")) sizer = wx.BoxSizer(wx.HORIZONTAL) sizer.Add(self.edit_btn, 1, wx.EXPAND|wx.ALIGN_CENTER|wx.TOP|wx.BOTTOM, 4) self.panel.SetAutoLayout(1) self.panel.SetSizer(sizer) self.panel.SetSize(sizer.GetMinSize()) wx.EVT_BUTTON(self.panel, edit_btn_id, self.edit_tools) def bind_event(*args): pass def edit_tools(self, event): dialog = ToolsDialog(self.panel, self.owner, items=self.owner.get_tools()) if dialog.ShowModal() == wx.ID_OK: self.owner.set_tools(dialog.get_tools()) common.app_tree.app.saved = False # update the status of the app def write(self, outfile, tabs): fwrite = outfile.write fwrite(' ' * tabs + '<tools>\n') for tool in self.owner[self.name][0](): tool.write(outfile, tabs+1) fwrite(' ' * tabs + '</tools>\n')# end of class ToolsPropertyclass EditToolBar(EditBase, PreviewMixin): def __init__(self, name, klass, parent, property_window): custom_class = parent is None EditBase.__init__(self, name, klass, parent, wx.NewId(), property_window, custom_class=custom_class, show=False) self.base = 'wx.ToolBar' def nil(*args): return () self.tools = [] # list of Tool objects self._tb = None # the real toolbar self.style = 0 self.access_functions['style'] = (self.get_style, self.set_style) self.style_pos = [wx.TB_FLAT, wx.TB_DOCKABLE, wx.TB_3DBUTTONS] if misc.check_wx_version(2, 3, 3): self.style_pos += [wx.TB_TEXT, wx.TB_NOICONS, wx.TB_NODIVIDER, wx.TB_NOALIGN] if misc.check_wx_version(2, 5, 0): self.style_pos += [wx.TB_HORZ_LAYOUT, wx.TB_HORZ_TEXT] style_labels = ['#section#' + _('Style'), 'wxTB_FLAT', 'wxTB_DOCKABLE', 'wxTB_3DBUTTONS'] if misc.check_wx_version(2, 3, 3): style_labels += ['wxTB_TEXT', 'wxTB_NOICONS', 'wxTB_NODIVIDER', 'wxTB_NOALIGN'] if misc.check_wx_version(2, 5, 0): style_labels += ['wxTB_HORZ_LAYOUT', 'wxTB_HORZ_TEXT'] self.properties['style'] = CheckListProperty(self, 'style', None, style_labels) self.bitmapsize = '16, 15' self.access_functions['bitmapsize'] = (self.get_bitmapsize, self.set_bitmapsize) self.properties['bitmapsize'] = TextProperty(self, 'bitmapsize', None, can_disable=True, label=_("bitmapsize")) self.margins = '0, 0' self.access_functions['margins'] = (self.get_margins, self.set_margins) self.properties['margins'] = TextProperty(self, 'margins', None, can_disable=True, label=_("margins")) self.access_functions['tools'] = (self.get_tools, self.set_tools) prop = self.properties['tools'] = ToolsProperty(self, 'tools', None) self.packing = 1 self.access_functions['packing'] = (self.get_packing, self.set_packing) self.properties['packing'] = SpinProperty(self, 'packing', None, r=(0, 100), can_disable=True, label=_("packing")) self.separation = 5 self.access_functions['separation'] = (self.get_separation, self.set_separation) self.properties['separation'] = SpinProperty( self, 'separation', None, r=(0, 100), can_disable=True, label=_("separation")) # 2003-05-07 preview support PreviewMixin.__init__(self) def create_widget(self): tb_style = wx.TB_HORIZONTAL|self.style if wx.Platform == '__WXGTK__': tb_style |= wx.TB_DOCKABLE|wx.TB_FLAT if self.parent: self.widget = self._tb = wx.ToolBar( self.parent.widget, -1, style=tb_style) self.parent.widget.SetToolBar(self.widget) else: # "top-level" toolbar self.widget = wx.Frame(None, -1, misc.design_title(self.name)) self.widget.SetClientSize((400, 30)) self._tb = wx.ToolBar(self.widget, -1, style=tb_style) self.widget.SetToolBar(self._tb) self.widget.SetBackgroundColour(self._tb.GetBackgroundColour()) import os icon = wx.EmptyIcon() xpm = os.path.join(common.wxglade_path, 'icons', 'toolbar.xpm') icon.CopyFromBitmap(misc.get_xpm_bitmap(xpm)) self.widget.SetIcon(icon) wx.EVT_CLOSE(self.widget, lambda e: self.hide_widget()) wx.EVT_LEFT_DOWN(self._tb, self.on_set_focus) if wx.Platform == '__WXMSW__': # MSW isn't smart enough to avoid overlapping windows, so # at least move it away from the 3 wxGlade frames self.widget.CenterOnScreen() wx.EVT_LEFT_DOWN(self.widget, self.on_set_focus) # set the various property values prop = self.properties if prop['bitmapsize'].is_active(): self.set_bitmapsize(self.bitmapsize, refresh=False) if prop['margins'].is_active(): self.set_margins(self.margins, refresh=False) if prop['packing'].is_active(): self.set_packing(self.packing, refresh=False) if prop['separation'].is_active(): self.set_separation(self.separation, refresh=False) self.set_tools(self.tools) # show the menus def create_properties(self): EditBase.create_properties(self) page = self._common_panel sizer = page.GetSizer() self.properties['bitmapsize'].display(page) self.properties['margins'].display(page) self.properties['packing'].display(page) self.properties['separation'].display(page) self.properties['style'].display(page) self.properties['tools'].display(page) if not sizer: sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add(self.name_prop.panel, 0, wx.EXPAND) sizer.Add(self.klass_prop.panel, 0, wx.EXPAND) page.SetAutoLayout(1) page.SetSizer(sizer) sizer.Add(self.properties['bitmapsize'].panel, 0, wx.EXPAND) sizer.Add(self.properties['margins'].panel, 0, wx.EXPAND) sizer.Add(self.properties['packing'].panel, 0, wx.EXPAND) sizer.Add(self.properties['separation'].panel, 0, wx.EXPAND) sizer.Add(self.properties['style'].panel, 0, wx.EXPAND) sizer.Add(self.properties['tools'].panel, 0, wx.ALL|wx.EXPAND, 3) sizer.Layout() sizer.Fit(page) w, h = page.GetClientSize() self.notebook.AddPage(page, _("Common")) if self.parent is not None: self.property_window.Layout() page.SetScrollbars(1, 5, 1, int(math.ceil(h/5.0))) else: PreviewMixin.create_properties(self) def __getitem__(self, key): return self.access_functions[key] def get_style(self): retval = [0] * len(self.style_pos) try: for i in range(len(self.style_pos)): if (self.style & self.style_pos[i]) == self.style_pos[i]: retval[i] = 1 except AttributeError: pass return retval def set_style(self, value, refresh=True): value = self.properties['style'].prepare_value(value) self.style = 0 for v in range(len(value)):
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -