summaryrefslogtreecommitdiff
path: root/lib/python2.7/site-packages/wx-3.0-msw/wx/tools/XRCed/XMLTree.py
blob: 6a3cd32720d821bc9d4ef13fd97e868d469834fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# Name:         XMLTree.py
# Purpose:      XMLTree class
# Author:       Roman Rolinsky <rolinsky@femagsoft.com>
# Created:      31.05.2007
# RCS-ID:       $Id: XMLTree.py 64627 2010-06-18 18:17:45Z ROL $

from globals import *
from model import Model
from component import Manager, Component, Container
import images

class XMLTree(wx.TreeCtrl):
    def __init__(self, parent):
        style = wx.TR_HAS_BUTTONS | wx.TR_MULTIPLE | \
                wx.TR_HIDE_ROOT | wx.TR_LINES_AT_ROOT
        wx.TreeCtrl.__init__(self, parent, style=style)

        self.locals = {}        # namespace for comment directives

        # Color scheme
        self.SetBackgroundColour(wx.Colour(222, 248, 222))
        self.COLOUR_COMMENT  = wx.Colour(0, 0, 255)
        self.COLOUR_REF      = wx.Colour(0, 0, 128)
        self.COLOUR_HIDDEN   = wx.Colour(128, 128, 128)
        self.COLOUR_HL       = wx.Colour(255, 0, 0)
        self.COLOUR_DT       = wx.Colour(0, 0, 255)

        # Comments use italic font
        self.fontComment = wx.FFont(self.GetFont().GetPointSize(),
                                    self.GetFont().GetFamily(),
                                    wx.FONTFLAG_ITALIC)

        # Create image list
        il = wx.ImageList(22, 22, True)
        # 0 is the default image index
        im = images.TreeDefault.GetImage()
        if im.GetWidth() != 22 or im.GetHeight() != 22:
            im.Resize((22,22), ((22-im.GetWidth())/2,(22-im.GetHeight())/2))
        il.Add(im.ConvertToBitmap())
        # 1 is the default container image
        im = images.TreeDefaultContainer.GetImage()
        if im.GetWidth() != 22 or im.GetHeight() != 22:
            im.Resize((22,22), ((22-im.GetWidth())/2,(22-im.GetHeight())/2))
        il.Add(im.ConvertToBitmap())
        # root icon
#        self.rootImage = il.Add(images.TreeRoot.GetImage().Scale(16,16).ConvertToBitmap())
        # Loop through registered components which have images
        for component in Manager.components.values():
            for im in component.images:
                # Resize image if necessary
                if im.GetWidth() != 22 or im.GetHeight() != 22:
                    im.Resize((22,22), ((22-im.GetWidth())/2,(22-im.GetHeight())/2))
                im.Id = il.Add(im.ConvertToBitmap())
        self.il = il
        self.SetImageList(il)

        self.root = self.AddRoot('XML tree') #, self.rootImage)
        self.SetItemHasChildren(self.root)

    def Clear(self):
        '''Clear everything except the root item.'''
        self.UnselectAll()
        self.DeleteChildren(self.root)

    # Add tree item for given parent item if node is DOM element node with
    # object/object_ref tag
    def AddNode(self, parent, node):
        # Append tree item
        try:
            comp = Manager.getNodeComp(node, None)
            className = comp.klass
        except:
            className = node.getAttribute('class')
            # Try to create some generic component on-the-fly
            attributes = []
            isContainer = False
            for n in node.childNodes:
                if is_element(n):
                    isContainer = True
                elif n.nodeType == node.ELEMENT_NODE and not n.tagName in attributes:
                    attributes.append(n.tagName)
            if isContainer:
                comp = Container(className, 'unknown', attributes)
            else:
                comp = Component(className, 'unknown', attributes)
            Manager.register(comp)
            wx.LogWarning('Unknown component class "%s", registered as generic' % className)
        item = self.AppendItem(parent, comp.getTreeText(node), 
                               image=comp.getTreeImageId(node),
                               data=wx.TreeItemData(node))
        self.SetItemStyle(item, node)
        # Try to find children objects
        if comp.isContainer():
            for n in filter(is_object, node.childNodes):
                self.AddNode(item, comp.getTreeNode(n))
        elif node.nodeType == node.COMMENT_NODE:
            if node.data and node.data[0] == '%' and g.conf.allowExec != 'no':
                if g.conf.allowExec == 'ask' and Model.allowExec is None:
                    say = wx.MessageBox('''This file contains executable comment directives. \
Allow to execute?''', 'Warning', wx.ICON_EXCLAMATION | wx.YES_NO)
                    if say == wx.YES:
                        Model.allowExec = True
                    else:
                        Model.allowExec = False
                if g.conf.allowExec == 'yes' or Model.allowExec:
                    code = node.data[1:] # skip '%'
                    self.ExecCode(code)

    # Maybe this should be moved to presenter?
    def ExecCode(self, code):
        logger.debug('executing comment pragma: \n%s', code)
        try:
            exec code in globals(), self.locals
        except:
            wx.LogError('exec error: "%s"' % code)
            logger.exception("execution of in-line comment failed")

    def SetItemStyle(self, item, node):
        # Different color for comments and references
        if node.nodeType == node.COMMENT_NODE:
            self.SetItemTextColour(item, self.COLOUR_COMMENT)
            self.SetItemFont(item, self.fontComment)
        elif node.tagName == 'object_ref':
            self.SetItemTextColour(item, self.COLOUR_REF)
#        elif treeObj.hasStyle and treeObj.params.get('hidden', False):
#            self.SetItemTextColour(item, self.COLOUR_HIDDEN)        

    def Flush(self):
        '''Update all items after changes in model.'''
        self.Clear()
        # Update root item
        self.SetPyData(self.root, Model.mainNode)
        # (first node is test node, skip it)
        for n in filter(is_object, Model.mainNode.childNodes[1:]):
            self.AddNode(self.root, n)

    def FlushSubtree(self, item, node):
        '''Update all items after changes in model.'''
        if item is None or item == self.root:
            self.Flush()
            return
        self.DeleteChildren(item)
        className = node.getAttribute('class')
        try:
            comp = Manager.components[className]
        except:
            # Flush completely if unknown
            self.Flush()
            return
        for n in filter(is_object, node.childNodes):
            self.AddNode(item, comp.getTreeNode(n))

    def ExpandAll(self):
        i, cookie = self.GetFirstChild(self.root)
        while i:
            self.ExpandAllChildren(i)
            i, cookie = self.GetNextChild(self.root, cookie)

    def CollapseAll(self):
        i, cookie = self.GetFirstChild(self.root)
        while i:
            self.CollapseAllChildren(i)
            i, cookie = self.GetNextChild(self.root, cookie)

    # Override to use same form as InsertItem
    def InsertItemBefore(self, parent, next, label, image=-1, data=None):
        prev = self.GetPrevSibling(next)
        if prev:
            return self.InsertItem(parent, prev, label, image, data=data)
        else:
            return self.PrependItem(parent, label, image, data=data)

    def GetSelection(self):
        if self.GetSelections():
            return self.GetSelections()[-1]
        else: return None

    # Return item index in parent
    def ItemIndex(self, item):
        n = 0                           # index of sibling
        prev = self.GetPrevSibling(item)
        while prev.IsOk():
            prev = self.GetPrevSibling(prev)
            n += 1
        return n

    # Full tree index of an item - list of positions
    def ItemFullIndex(self, item):
        if not item.IsOk(): return None
        l = []
        while item != self.root:
            l.insert(0, self.ItemIndex(item))
            item = self.GetItemParent(item)
        return l

    # Get item position from full index
    def ItemAtFullIndex(self, index):
        if index is None: return wx.TreeItemId()
        item = self.root
        for i in index:
            item = self.GetFirstChild(item)[0]
            for k in range(i): item = self.GetNextSibling(item)
        return item

    # Return item child index in parent (skip non-window items)
    def ItemIndexWin(self, item):
        n = 0                           # index of sibling
        prev = self.GetPrevSibling(item)
        while prev.IsOk():
            if self.GetPyData(prev).nodeType != Model.dom.COMMENT_NODE: 
                n += 1
            prev = self.GetPrevSibling(prev)
        return n

    # Get expanded state of all items
    def GetFullState(self, item=None):
        if not item: item = self.root
        states = []
        item = self.GetFirstChild(item)[0]
        while item:
            if self.ItemHasChildren(item):
                state = self.IsExpanded(item)
                states.append(state)
                if state: states.extend(self.GetFullState(item))
            item = self.GetNextSibling(item)
        return states

    # Set expanded state of all items
    def SetFullState(self, states, item=None):
        if not item:
            item = self.root
            states = states[:]
        item = self.GetFirstChild(item)[0]
        while item:
            if self.ItemHasChildren(item):
                state = states.pop(0)
                if state: self.Expand(item)
                else: self.Collapse(item)
                if state: self.SetFullState(states, item)
            item = self.GetNextSibling(item)

    # Find item with given data (node)
    def Find(self, item, name):
        node = self.GetPyData(item)
        if is_element(node) and node.getAttribute('name') == name:
            return item
        item,cookie = self.GetFirstChild(item)
        while item:
            found = self.Find(item, name)
            if found: return found
            item = self.GetNextSibling(item)
        return None

    # Fixes for broken and platform-incompatible functions

    def ItemHasChildren(self, item):
        return self.GetChildrenCount(item)

    if wx.Platform == '__WXMSW__':

        # Generate selection event
        def SelectItem(self, item):
            wx.TreeCtrl.SelectItem(self, item)
            evt = wx.TreeEvent(wx.EVT_TREE_SEL_CHANGED.typeId, self, item)
            wx.PostEvent(self, evt)