⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tree.py

📁 用python写的ide开发环境,巨强大,不过需要wxpython的支持
💻 PY
📖 第 1 页 / 共 4 页
字号:
        parent = node.parentNode        parent.removeChild(node)        self.Delete(leaf)        return node        # Find position relative to the top-level window    def FindNodePos(self, item, obj=None):        # Root at (0,0)        if item == g.testWin.item: return wx.Point(0, 0)        itemParent = self.GetItemParent(item)        # Select book page        if not obj: obj = self.FindNodeObject(item)        if self.GetPyData(itemParent).treeObject().__class__ in \               [xxxNotebook, xxxChoicebook, xxxListbook]:            book = self.FindNodeObject(itemParent)            # Find position            for i in range(book.GetPageCount()):                if book.GetPage(i) == obj:                    if book.GetSelection() != i:                        book.SetSelection(i)                        # Remove highlight - otherwise highlight window won't be visible                        if g.testWin.highLight:                            g.testWin.highLight.Remove()                    break        # For sizers and notebooks we must select the first window-like parent        winParent = itemParent        while self.GetPyData(winParent).isSizer:            winParent = self.GetItemParent(winParent)        # Notebook children are layed out in a little strange way        # wxGTK places NB panels relative to the NB parent        if wx.Platform == '__WXGTK__':            if self.GetPyData(itemParent).treeObject().__class__ == xxxNotebook:                winParent = self.GetItemParent(winParent)        parentPos = self.FindNodePos(winParent)        pos = obj.GetPosition()        # Position (-1,-1) is really (0,0)        if pos == (-1,-1): pos = (0,0)        return parentPos + pos    # Find window (or sizer) corresponding to a tree item.    def FindNodeObject(self, item):        testWin = g.testWin        # If top-level, return testWin (or panel its panel)        if item == testWin.item: return testWin.panel        itemParent = self.GetItemParent(item)        xxx = self.GetPyData(item).treeObject()        parentWin = self.FindNodeObject(itemParent)        # Top-level sizer? return window's sizer        if xxx.isSizer and isinstance(parentWin, wx.Window):            return parentWin.GetSizer()        elif xxx.__class__ in [xxxMenu, xxxMenuItem, xxxSeparator]:  return None        elif xxx.__class__ in [xxxToolBar, xxxMenuBar]:            # If it's the main toolbar or menubar, we can't really select it            if xxx.parent.__class__ == xxxFrame:  return None        elif isinstance(xxx.parent, xxxToolBar):            # Select complete toolbar            return parentWin        elif isinstance(xxx.parent, xxxStdDialogButtonSizer):            # This sizer returns non-existing children            for ch in parentWin.GetChildren():                if ch.GetWindow() and ch.GetWindow().GetName() == xxx.name:                    return ch.GetWindow()            return None        elif xxx.parent.__class__ in [xxxChoicebook, xxxListbook]:            # First window is controld            return parentWin.GetChildren()[self.ItemIndex(item)+1]        # Otherwise get parent's object and it's child        child = parentWin.GetChildren()[self.WindowIndex(item)]        # Return window or sizer for sizer items        if child.GetClassName() == 'wxSizerItem':            if child.IsWindow(): child = child.GetWindow()            elif child.IsSizer():                child = child.GetSizer()        return child    def OnSelChanged(self, evt):        if self.selectionChanging: return        self.selectionChanging = True        wx.TreeCtrl.UnselectAll(self)        self.ChangeSelection(evt.GetItem())        wx.TreeCtrl.SelectItem(self, evt.GetItem())        self.selectionChanging = False        g.frame.SetStatusText('')        evt.Skip()    # Override to use like single-selection tree    def GetSelection(self):        return self.selection    def SelectItem(self, item):        self.UnselectAll()        self.ChangeSelection(item)        wx.TreeCtrl.SelectItem(self, item)    def UnselectAll(self):        self.selection = None        g.tools.UpdateUI()        wx.TreeCtrl.UnselectAll(self)        #wx.Yield()    def ChangeSelection(self, item):        # Apply changes        # !!! problem with wxGTK - GetOldItem is Ok if nothing selected        #oldItem = evt.GetOldItem()        status = ''        oldItem = self.selection        if oldItem:            xxx = self.GetPyData(oldItem)            # If some data was modified, apply changes            if g.panel.IsModified():                self.Apply(xxx, oldItem)                if g.testWin:                    if g.testWin.highLight:                        g.testWin.highLight.Remove()                    self.needUpdate = True                status = 'Changes were applied'        if status: g.frame.SetStatusText(status)        # Generate view        if not item:            self.selection = None            return        else:            self.selection = item        xxx = self.GetPyData(item)        # Update panel        g.panel.SetData(xxx)        # Update tools        g.tools.UpdateUI()        # Highlighting is done in OnIdle        self.pendingHighLight = self.selection    # Check if item is in testWin subtree    def IsHighlatable(self, item):        if item == g.testWin.item: return False        while item != self.root:            item = self.GetItemParent(item)            if item == g.testWin.item:                return True        return False    # Highlight selected item    def HighLight(self, item):        self.pendingHighLight = None        # Can highlight only with some top-level windows        if not g.testWin or self.GetPyData(g.testWin.item).treeObject().__class__ \            not in [xxxDialog, xxxPanel, xxxFrame]:            return        # If a control from another window is selected, remove highlight        if not self.IsHighlatable(item):            if g.testWin.highLight: g.testWin.highLight.Remove()            return        # Get window/sizer object        obj = self.FindNodeObject(item)        xxx = self.GetPyData(item).treeObject()        # Remove existing HL if item not found or is hidden        if not obj or xxx.hasStyle and xxx.params.get('hidden', False):            if g.testWin.highLight: g.testWin.highLight.Remove()            return        pos = self.FindNodePos(item, obj)                 size = obj.GetSize()        # Highlight        # Negative positions are not working quite well        # If highlight object has the same size SetDimension does not repaint it        # so we must remove the old HL window        g.testWin.highLight = updateHL(g.testWin.highLight, HighLightBox, pos, size)        g.testWin.highLight.item = item        g.testWin.highLight.obj = obj    def ShowTestWindow(self, item):        xxx = self.GetPyData(item)        if g.panel.IsModified():            self.Apply(xxx, item)       # apply changes        availableViews = ['wxFrame', 'wxPanel', 'wxDialog',                            'wxMenuBar', 'wxToolBar', 'wxWizard',                            'wxWizardPageSimple']        originalItem = item        # Walk up the tree until we find an item that has a view        while item and self.GetPyData(item).treeObject().className not in availableViews:            item = self.GetItemParent(item)        if not item or not item.IsOk():            wx.LogMessage('No view for this element (yet)')            return        # Show item in bold        if g.testWin:     # Reset old            self.UnselectAll()            self.SetItemBold(g.testWin.item, False)        try:            wx.BeginBusyCursor()            self.CreateTestWin(item)        finally:            wx.EndBusyCursor()        # Maybe an error occurred, so we need to test        if g.testWin:            self.SetItemBold(g.testWin.item)            # Select original item            self.ChangeSelection(originalItem)    # Double-click on Linux    def OnItemActivated(self, evt):        if evt.GetItem() != self.root:            self.ShowTestWindow(evt.GetItem())    # Double-click on Windows    def OnDClick(self, evt):        item, flags = self.HitTest(evt.GetPosition())        if flags in [wx.TREE_HITTEST_ONITEMBUTTON, wx.TREE_HITTEST_ONITEMLABEL]:            if item != self.root: self.ShowTestWindow(item)        else:            evt.Skip()    def OnItemExpandedCollapsed(self, evt):        # Update tool palette        g.tools.UpdateUI()        evt.Skip()    # (re)create test window    def CreateTestWin(self, item):        testWin = g.testWin        # Create a window with this resource        xxx = self.GetPyData(item).treeObject()        # Close old window, remember where it was        highLight = None        if testWin:            pos = testWin.GetPosition()            if item == testWin.item:                # Remember highlight if same top-level window                if testWin.highLight:                    highLight = testWin.highLight.item                if xxx.className == 'wxPanel':                    if testWin.highLight:                        testWin.pendingHighLight = highLight                        testWin.highLight.Remove()                    testWin.panel.Destroy()                    testWin.panel = None                else:                    testWin.Destroy()                    testWin = g.testWin = None            else:                testWin.Destroy()                testWin = g.testWin = None        else:            pos = g.testWinPos        # Save in memory FS        memFile = MemoryFile('xxx.xrc')        # Create memory XML file        elem = xxx.node.cloneNode(True)        if not xxx.name:            name = 'noname'        else:            name = xxx.name        elem.setAttribute('name', STD_NAME)        oldTestNode = self.testElem        self.testElem = elem        self.mainNode.replaceChild(elem, oldTestNode)        oldTestNode.unlink()        # Replace wizard page class temporarily        if xxx.__class__ in [xxxWizardPage, xxxWizardPageSimple]:            oldCl = elem.getAttribute('class')            elem.setAttribute('class', 'wxPanel')        parent = elem.parentNode        encd = self.rootObj.params['encoding'].value()        if not encd: encd = None        try:            self.dom.writexml(memFile, encoding=encd)        except:            inf = sys.exc_info()            wx.LogError(traceback.format_exception(inf[0], inf[1], None)[-1])            wx.LogError('Error writing temporary file')        memFile.close()                 # write to wxMemoryFS        xmlFlags = xrc.XRC_NO_SUBCLASSING        # Use translations if encoding is not specified        if not g.currentEncoding:            xmlFlags != xrc.XRC_USE_LOCALE        res = xrc.XmlResource('', xmlFlags)        res.InitAllHandlers()        xrc.XmlResource.Set(res)        # set as global        # Register handlers        Manager.addXmlHandlers(res)     # !!! add if g.use_custom        # Same module list        res.Load('memory:xxx.xrc')        try:            if xxx.__class__ == xxxFrame:                # Frame can't have many children,                # but it's first child possibly can...    #            child = self.GetFirstChild(item)[0]    #            if child.IsOk() and self.GetPyData(child).__class__ == xxxPanel:    #                # Clean-up before recursive call or error    #                wx.MemoryFSHandler.RemoveFile('xxx.xrc')    #                wx.EndBusyCursor()    #                self.CreateTestWin(child)    #                return                # This currently works under GTK, but not under MSW                testWin = g.testWin = wx.PreFrame()                res.LoadOnFrame(testWin, g.frame, STD_NAME)                # Create status bar                testWin.panel = testWin                #testWin.CreateStatusBar()                testWin.SetClientSize(testWin.GetBestSize())                testWin.SetPosition(pos)                testWin.Show(True)            elif xxx.__class__ == xxxPanel:                # Create new frame                if not testWin:                    testWin = g.testWin = wx.Frame(g.frame, -1, 'Panel: ' + name,                                                   pos=pos, name=STD_NAME)                testWin.panel = res.LoadPanel(testWin, STD_NAME)                testWin.panel.SetSize(testWin.GetClientSize())                #testWin.SetClientSize(testWin.GetSize())                testWin.Show(True)            elif xxx.__class__ == xxxDialog:                testWin = g.testWin = res.LoadDialog(g.frame, STD_NAME)                testWin.panel = testWin                testWin.Layout()                testWin.SetPosition(pos)                testWin.Show(True)                # Dialog's default code does not produce wx.EVT_CLOSE                wx.EVT_BUTTON(testWin, wx.ID_OK, self.OnCloseTestWin)                wx.EVT_BUTTON(testWin, wx.ID_CANCEL, self.OnCloseTestWin)            elif xxx.__class__ == xxxWizard:                wiz = wx.wizard.PreWizard()                res.LoadOnObject(wiz, g.frame, STD_NAME, 'wxWizard')                # Find first page (don't know better way)                firstPage = None                for w in wiz.GetChildren():                    if isinstance(w, wx.wizard.WizardPage):                        firstPage = w                        break                if not firstPage:                    wx.LogError('Wizard is empty')                else:                    # Wizard should be modal                    self.SetItemBold(item)                    wiz.RunWizard(w)                    self.SetItemBold(item, False)                    wiz.Destroy()            elif xxx.__class__ in [xxxWizardPage, xxxWizardPageSimple]:                # Create new frame                if not testWin:                    testWin = g.testWin = wx.Frame(g.frame, -1, 'Wizard page: ' + name,                                                  pos=pos, name=STD_NAME)                testWin.panel = wx.PrePanel()                res.LoadOnObject(testWin.panel, testWin, STD_NAME, 'wxPanel')                testWin.SetClientSize(testWin.GetBestSize())                testWin.Show(True)            elif xxx.__class__ == xxxMenuBar:                testWin = g.testWin = wx.Frame(g.frame, -1, 'MenuBar: ' + name,                                              pos=pos, name=STD_NAME)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -