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

📄 tree.py

📁 Wxpython Implemented on Windows CE, Source code
💻 PY
📖 第 1 页 / 共 4 页
字号:
            parent.element.insertBefore(elem, node)
            # Inserting before is difficult, se we insert after or first child
            index = self.ItemIndex(nextItem)
            newItem = self.InsertItemBefore(itemParent, index,
                        xxx.treeName(), image=xxx.treeImage())
            self.SetPyData(newItem, xxx)
        else:
            parent.element.appendChild(elem)
            newItem = self.AppendItem(itemParent, xxx.treeName(), image=xxx.treeImage(),
                                      data=wx.TreeItemData(xxx))
        # Different color for references
        if xxx.treeObject().ref:  self.SetItemTextColour(newItem, 'DarkGreen')
        # Add children items
        if xxx.hasChildren:
            treeObj = xxx.treeObject()
            for n in treeObj.element.childNodes:
                if IsObject(n):
                    self.AddNode(newItem, treeObj, n)
        return newItem

    # Remove leaf of tree, return it's data object
    def RemoveLeaf(self, leaf):
        xxx = self.GetPyData(leaf)
        node = xxx.element
        parent = node.parentNode
        parent.removeChild(node)
        self.Delete(leaf)
        # Reset selection object
        self.selection = None
        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
        # Find first ancestor which is a wxWindow (not a sizer)
        winParent = itemParent
        while self.GetPyData(winParent).isSizer:
            winParent = self.GetItemParent(winParent)
        # Notebook children are layed out in a little strange way
        if self.GetPyData(itemParent).treeObject().__class__ == xxxNotebook:
            parentPos = wx.Point(0,0)
        else:
            parentPos = self.FindNodePos(winParent)
        # Position (-1,-1) is really (0,0)
        pos = obj.GetPosition()
        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
        self.UnselectAll()
        self.SelectItem(evt.GetItem())
        self.selectionChanging = False

    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 conf.autoRefresh:
                if g.testWin:
                    if g.testWin.highLight:
                        g.testWin.highLight.Remove()
                    self.needUpdate = True
                status = 'Changes were applied'
        g.frame.SetStatusText(status)
        # Generate view
        self.selection = item
        if not self.selection.IsOk():
            self.selection = None
            return
        xxx = self.GetPyData(self.selection)
        # 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)
        if not obj:
            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 g.testWin.highLight:
            g.testWin.highLight.Replace(pos, size)
        else:
            g.testWin.highLight = HighLightBox(pos, size)
        g.testWin.highLight.Refresh()
        g.testWin.highLight.item = item

    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.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()

        # If frame
#        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

        # 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.element.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.Load('memory:xxx.xrc')

⌨️ 快捷键说明

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