📄 xrced.py
字号:
wx.EVT_MENU(self, self.ID_GENERATE_PYTHON, self.OnGeneratePython) wx.EVT_MENU(self, self.ID_PREFS, self.OnPrefs) wx.EVT_MENU(self, wx.ID_EXIT, self.OnExit) # Edit wx.EVT_MENU(self, wx.ID_UNDO, self.OnUndo) wx.EVT_MENU(self, wx.ID_REDO, self.OnRedo) wx.EVT_MENU(self, wx.ID_CUT, self.OnCutDelete) wx.EVT_MENU(self, wx.ID_COPY, self.OnCopy) wx.EVT_MENU(self, wx.ID_PASTE, self.OnPaste) wx.EVT_MENU(self, self.ID_TOOL_PASTE, self.OnPaste) wx.EVT_MENU(self, self.ID_DELETE, self.OnCutDelete) wx.EVT_MENU(self, self.ID_LOCATE, self.OnLocate) wx.EVT_MENU(self, self.ID_TOOL_LOCATE, self.OnLocate) # View wx.EVT_MENU(self, self.ID_EMBED_PANEL, self.OnEmbedPanel) wx.EVT_MENU(self, self.ID_SHOW_TOOLS, self.OnShowTools) wx.EVT_MENU(self, self.ID_TEST, self.OnTest) wx.EVT_MENU(self, self.ID_REFRESH, self.OnRefresh) wx.EVT_MENU(self, self.ID_AUTO_REFRESH, self.OnAutoRefresh) wx.EVT_MENU(self, self.ID_TEST_HIDE, self.OnTestHide) # Move wx.EVT_MENU(self, self.ID_MOVEUP, self.OnMoveUp) wx.EVT_MENU(self, self.ID_MOVEDOWN, self.OnMoveDown) wx.EVT_MENU(self, self.ID_MOVELEFT, self.OnMoveLeft) wx.EVT_MENU(self, self.ID_MOVERIGHT, self.OnMoveRight) # Help wx.EVT_MENU(self, wx.ID_ABOUT, self.OnAbout) wx.EVT_MENU(self, self.ID_README, self.OnReadme) # Update events wx.EVT_UPDATE_UI(self, wx.ID_SAVE, self.OnUpdateUI) wx.EVT_UPDATE_UI(self, wx.ID_CUT, self.OnUpdateUI) wx.EVT_UPDATE_UI(self, wx.ID_COPY, self.OnUpdateUI) wx.EVT_UPDATE_UI(self, wx.ID_PASTE, self.OnUpdateUI) wx.EVT_UPDATE_UI(self, self.ID_LOCATE, self.OnUpdateUI) wx.EVT_UPDATE_UI(self, self.ID_TOOL_LOCATE, self.OnUpdateUI) wx.EVT_UPDATE_UI(self, self.ID_TOOL_PASTE, self.OnUpdateUI) wx.EVT_UPDATE_UI(self, wx.ID_UNDO, self.OnUpdateUI) wx.EVT_UPDATE_UI(self, wx.ID_REDO, self.OnUpdateUI) wx.EVT_UPDATE_UI(self, self.ID_DELETE, self.OnUpdateUI) wx.EVT_UPDATE_UI(self, self.ID_TEST, self.OnUpdateUI) wx.EVT_UPDATE_UI(self, self.ID_MOVEUP, self.OnUpdateUI) wx.EVT_UPDATE_UI(self, self.ID_MOVEDOWN, self.OnUpdateUI) wx.EVT_UPDATE_UI(self, self.ID_MOVELEFT, self.OnUpdateUI) wx.EVT_UPDATE_UI(self, self.ID_MOVERIGHT, self.OnUpdateUI) wx.EVT_UPDATE_UI(self, self.ID_REFRESH, self.OnUpdateUI) # Build interface sizer = wx.BoxSizer(wx.VERTICAL) #sizer.Add(wx.StaticLine(self, -1), 0, wx.EXPAND) # Horizontal sizer for toolbar and splitter self.toolsSizer = sizer1 = wx.BoxSizer() splitter = wx.SplitterWindow(self, -1, style=wx.SP_3DSASH) self.splitter = splitter splitter.SetMinimumPaneSize(100) # Create tree global tree g.tree = tree = XML_Tree(splitter, -1) # Init pull-down menu data global pullDownMenu g.pullDownMenu = pullDownMenu = PullDownMenu(self) # Vertical toolbar for GUI buttons g.tools = tools = Tools(self) tools.Show(conf.showTools) if conf.showTools: sizer1.Add(tools, 0, wx.EXPAND) tree.RegisterKeyEvents() # Miniframe for split mode miniFrame = wx.MiniFrame(self, -1, 'Properties & Style', (conf.panelX, conf.panelY), (conf.panelWidth, conf.panelHeight)) self.miniFrame = miniFrame sizer2 = wx.BoxSizer() miniFrame.SetSizer(sizer2) # Create panel for parameters global panel if conf.embedPanel: panel = Panel(splitter) # Set plitter windows splitter.SplitVertically(tree, panel, conf.sashPos) else: panel = Panel(miniFrame) sizer2.Add(panel, 1, wx.EXPAND) miniFrame.Show(True) splitter.Initialize(tree) if wx.Platform == '__WXMAC__': sizer1.Add(splitter, 1, wx.EXPAND|wx.RIGHT, 5) else: sizer1.Add(splitter, 1, wx.EXPAND) sizer.Add(sizer1, 1, wx.EXPAND) self.SetAutoLayout(True) self.SetSizer(sizer) # Other events wx.EVT_IDLE(self, self.OnIdle) wx.EVT_CLOSE(self, self.OnCloseWindow) wx.EVT_KEY_DOWN(self, tools.OnKeyDown) wx.EVT_KEY_UP(self, tools.OnKeyUp) wx.EVT_ICONIZE(self, self.OnIconize) def OnRecentFile(self,evt): # open recently used file if not self.AskSave(): return wx.BeginBusyCursor() # get the pathname based on the menu ID fileNum = evt.GetId() - wx.ID_FILE1 path = g.fileHistory.GetHistoryFile(fileNum) if self.Open(path): self.SetStatusText('Data loaded') # add it back to the history so it will be moved up the list self.SaveRecent(path) else: self.SetStatusText('Failed') wx.EndBusyCursor() def OnNew(self, evt): if not self.AskSave(): return self.Clear() def OnOpen(self, evt): if not self.AskSave(): return dlg = wx.FileDialog(self, 'Open', os.path.dirname(self.dataFile), '', '*.xrc', wx.OPEN | wx.CHANGE_DIR) if dlg.ShowModal() == wx.ID_OK: path = dlg.GetPath() self.SetStatusText('Loading...') wx.BeginBusyCursor() try: if self.Open(path): self.SetStatusText('Data loaded') self.SaveRecent(path) else: self.SetStatusText('Failed') finally: wx.EndBusyCursor() dlg.Destroy() def OnSaveOrSaveAs(self, evt): if evt.GetId() == wx.ID_SAVEAS or not self.dataFile: if self.dataFile: name = '' else: name = defaultName dirname = os.path.abspath(os.path.dirname(self.dataFile)) dlg = wx.FileDialog(self, 'Save As', dirname, name, '*.xrc', wx.SAVE | wx.OVERWRITE_PROMPT | wx.CHANGE_DIR) if dlg.ShowModal() == wx.ID_OK: path = dlg.GetPath() if isinstance(path, unicode): path = path.encode(sys.getfilesystemencoding()) dlg.Destroy() else: dlg.Destroy() return if conf.localconf: # if we already have a localconf then it needs to be # copied to a new config with the new name lc = conf.localconf nc = self.CreateLocalConf(path) flag, key, idx = lc.GetFirstEntry() while flag: nc.Write(key, lc.Read(key)) flag, key, idx = lc.GetNextEntry(idx) conf.localconf = nc else: # otherwise create a new one conf.localconf = self.CreateLocalConf(path) else: path = self.dataFile self.SetStatusText('Saving...') wx.BeginBusyCursor() try: try: tmpFile,tmpName = tempfile.mkstemp(prefix='xrced-') os.close(tmpFile) self.Save(tmpName) # save temporary file first shutil.move(tmpName, path) self.dataFile = path self.SetModified(False) if conf.localconf.ReadBool("autogenerate", False): pypath = conf.localconf.Read("filename") embed = conf.localconf.ReadBool("embedResource", False) genGettext = conf.localconf.ReadBool("genGettext", False) self.GeneratePython(self.dataFile, pypath, embed, genGettext) self.SetStatusText('Data saved') self.SaveRecent(path) except IOError: self.SetStatusText('Failed') finally: wx.EndBusyCursor() def SaveRecent(self,path): # append to recently used files g.fileHistory.AddFileToHistory(path) def GeneratePython(self, dataFile, pypath, embed, genGettext): try: import wx.tools.pywxrc rescomp = wx.tools.pywxrc.XmlResourceCompiler() rescomp.MakePythonModule([dataFile], pypath, embed, genGettext) except: inf = sys.exc_info() wx.LogError(traceback.format_exception(inf[0], inf[1], None)[-1]) wx.LogError('Error generating python code : %s' % pypath) raise def OnGeneratePython(self, evt): if self.modified or not conf.localconf: wx.MessageBox("Save the XRC file first!", "Error") return dlg = PythonOptions(self, conf.localconf, self.dataFile) dlg.ShowModal() dlg.Destroy() def OnPrefs(self, evt): dlg = PrefsDialog(self) if dlg.ShowModal() == wx.ID_OK: # Fetch new preferences for id,cdp in dlg.checkControls.items(): c,d,p = cdp if dlg.FindWindowById(id).IsChecked(): d[p] = str(c.GetValue()) elif p in d: del d[p] g.conf.allowExec = ('ask', 'yes', 'no')[dlg.radio_allow_exec.GetSelection()] dlg.Destroy() def OnExit(self, evt): self.Close() def OnUndo(self, evt): # Extra check to not mess with idle updating if undoMan.CanUndo(): undoMan.Undo() g.panel.SetModified(False) if not undoMan.CanUndo(): self.SetModified(False) def OnRedo(self, evt): if undoMan.CanRedo(): undoMan.Redo() self.SetModified(True) def OnCopy(self, evt): selected = tree.selection if not selected: return # key pressed event xxx = tree.GetPyData(selected) if wx.TheClipboard.Open(): if xxx.isElement: data = wx.CustomDataObject('XRCED') # Set encoding in header # (False,True) s = xxx.node.toxml(encoding=expat.native_encoding) else: data = wx.CustomDataObject('XRCED_node') s = xxx.node.data data.SetData(cPickle.dumps(s)) wx.TheClipboard.SetData(data) wx.TheClipboard.Close() self.SetStatusText('Copied') else: wx.MessageBox("Unable to open the clipboard", "Error") def OnPaste(self, evt): selected = tree.selection if not selected: return # key pressed event # For pasting with Ctrl pressed appendChild = True if evt.GetId() == pullDownMenu.ID_PASTE_SIBLING: appendChild = False elif evt.GetId() == self.ID_TOOL_PASTE: if g.tree.ctrl: appendChild = False else: appendChild = not tree.NeedInsert(selected) else: appendChild = not tree.NeedInsert(selected) xxx = tree.GetPyData(selected) if not appendChild: # If has next item, insert, else append to parent nextItem = tree.GetNextSibling(selected) parentLeaf = tree.GetItemParent(selected) # Expanded container (must have children) elif tree.IsExpanded(selected) and tree.GetChildrenCount(selected, False): # Insert as first child nextItem = tree.GetFirstChild(selected)[0] parentLeaf = selected else: # No children or unexpanded item - appendChild stays True nextItem = wx.TreeItemId() # no next item parentLeaf = selected parent = tree.GetPyData(parentLeaf).treeObject() # Create a copy of clipboard pickled element success = success_node = False if wx.TheClipboard.Open(): try: data = wx.CustomDataObject('XRCED') if wx.TheClipboard.IsSupported(data.GetFormat()): try: success = wx.TheClipboard.GetData(data) except: # there is a problem if XRCED_node is in clipboard # but previous SetData was for XRCED pass if not success: # try other format data = wx.CustomDataObject('XRCED_node') if wx.TheClipboard.IsSupported(data.GetFormat()): success_node = wx.TheClipboard.GetData(data) finally: wx.TheClipboard.Close() if not success and not success_node: wx.MessageBox( "There is no data in the clipboard in the required format", "Error") return xml = cPickle.loads(data.GetData()) # xml representation of element if success: elem = minidom.parseString(xml).childNodes[0]
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -