annotate pyink/unlink_clone.py @ 1532:4a92b639a1cd

Clear selection set when switching current scene. To clear selection set after switching away from current to another scene. It avoids Inkscape select on nodes they are not saw after switching.
author Thinker K.F. Li <thinker@codemud.net>
date Fri, 30 Sep 2011 12:31:33 +0800
parents 0b5ee9c90af7
children
rev   line source
1299
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
1 import dom_event
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
2 from data_monitor import data_monitor
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
3
1300
2a35a1cb6cdf Add comment for unlink_clone.py
Thinker K.F. Li <thinker@codemud.net>
parents: 1299
diff changeset
4 ## \brief Tracking relation-ship btween copy nodes and source nodes.
2a35a1cb6cdf Add comment for unlink_clone.py
Thinker K.F. Li <thinker@codemud.net>
parents: 1299
diff changeset
5 #
2a35a1cb6cdf Add comment for unlink_clone.py
Thinker K.F. Li <thinker@codemud.net>
parents: 1299
diff changeset
6 # To trace the relation-ship between source node and copy node, we add
2a35a1cb6cdf Add comment for unlink_clone.py
Thinker K.F. Li <thinker@codemud.net>
parents: 1299
diff changeset
7 # 'ns0:duplicate-src' attribute to copy nodes. But after unlink a
2a35a1cb6cdf Add comment for unlink_clone.py
Thinker K.F. Li <thinker@codemud.net>
parents: 1299
diff changeset
8 # clone node, inkscape does not copy attributes of 'svg:use' node to
2a35a1cb6cdf Add comment for unlink_clone.py
Thinker K.F. Li <thinker@codemud.net>
parents: 1299
diff changeset
9 # new nodes inserted after unlinking. So, we can not just putting
2a35a1cb6cdf Add comment for unlink_clone.py
Thinker K.F. Li <thinker@codemud.net>
parents: 1299
diff changeset
10 # 'ns0:duplicate-src' on 'svg:use' and hoping it is still working for
2a35a1cb6cdf Add comment for unlink_clone.py
Thinker K.F. Li <thinker@codemud.net>
parents: 1299
diff changeset
11 # us after unlinking.
2a35a1cb6cdf Add comment for unlink_clone.py
Thinker K.F. Li <thinker@codemud.net>
parents: 1299
diff changeset
12 #
2a35a1cb6cdf Add comment for unlink_clone.py
Thinker K.F. Li <thinker@codemud.net>
parents: 1299
diff changeset
13 # The solution is to duplicate value of 'id' attribute to 'saved_id'
2a35a1cb6cdf Add comment for unlink_clone.py
Thinker K.F. Li <thinker@codemud.net>
parents: 1299
diff changeset
14 # attribute for every node. Whenever Inkscape copying nodes for a
2a35a1cb6cdf Add comment for unlink_clone.py
Thinker K.F. Li <thinker@codemud.net>
parents: 1299
diff changeset
15 # copy & paste or an unlinking, the value of 'saved_id' attribute
2a35a1cb6cdf Add comment for unlink_clone.py
Thinker K.F. Li <thinker@codemud.net>
parents: 1299
diff changeset
16 # would be copied to copy nodes from source nodes. We copy the value
2a35a1cb6cdf Add comment for unlink_clone.py
Thinker K.F. Li <thinker@codemud.net>
parents: 1299
diff changeset
17 # of 'saved_id' attribute to 'ns0:duplicate-src', for copy node, to
2a35a1cb6cdf Add comment for unlink_clone.py
Thinker K.F. Li <thinker@codemud.net>
parents: 1299
diff changeset
18 # preserve the relation-ship of copy node and source node. After copy
2a35a1cb6cdf Add comment for unlink_clone.py
Thinker K.F. Li <thinker@codemud.net>
parents: 1299
diff changeset
19 # 'saved_id' to 'ns0:duplicate-src', 'saved_id' is re-assigned to the
2a35a1cb6cdf Add comment for unlink_clone.py
Thinker K.F. Li <thinker@codemud.net>
parents: 1299
diff changeset
20 # value of ID of copy node. A copy node is the new node of a copying.
2a35a1cb6cdf Add comment for unlink_clone.py
Thinker K.F. Li <thinker@codemud.net>
parents: 1299
diff changeset
21 #
1299
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
22 class unlink_clone_checker(object):
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
23 __metaclass__ = data_monitor
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
24
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
25 _no_change_attrs = ('ns0:duplicate-src', 'saved_id')
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
26
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
27 def __init__(self, domview_ui):
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
28 self._domviewui = domview_ui
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
29 self._locker = domview_ui
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
30 self._doc = None
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
31 self._root = None
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
32 pass
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
33
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
34 def _start_check(self):
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
35 doc = self._doc
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
36 dom_event.addEventListener(doc, 'DOMNodeInserted',
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
37 self.do_insert_node, None)
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
38 dom_event.addEventListener(doc, 'DOMAttrModified',
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
39 self.do_attr_modified, None)
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
40 pass
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
41
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
42 def handle_doc_root(self, doc, root):
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
43 self._doc = doc
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
44 self._root = root
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
45
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
46 self._start_check()
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
47 pass
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
48
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
49 def _handle_unlinked_or_copied_nodes(self, node, child):
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
50 try:
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
51 saved_id = child.getAttribute('saved_id')
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
52 except: # Skip it for losting saved_id
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
53 pass
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
54 else:
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
55 child.setAttribute('ns0:duplicate-src', saved_id)
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
56 pass
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
57
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
58 try:
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
59 child_id = child.getAttribute('id')
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
60 except: # still have no ID.
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
61 pass # Assign saved_id later with attr
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
62 # modified event.
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
63 else:
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
64 child.setAttribute('saved_id', child_id)
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
65 pass
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
66 pass
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
67
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
68 def _handle_new_nodes(self, node, child):
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
69 if child.name() == 'svg:use':
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
70 return
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
71
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
72 try:
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
73 child_id = child.getAttribute('id')
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
74 except KeyError:
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
75 return
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
76 child.setAttribute('saved_id', child_id)
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
77 pass
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
78
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
79 ## \brief Check inserted node recurisvely.
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
80 #
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
81 # Travel the tree in post-order.
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
82 #
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
83 def _handle_insert_node_recursive(self, node, child):
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
84 #
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
85 # Traveling forest of children.
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
86 #
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
87 for cchild in child.childList():
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
88 self._handle_insert_node_recursive(child, cchild)
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
89 pass
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
90
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
91 #
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
92 # Visit the node
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
93 #
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
94 try:
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
95 child_id = child.getAttribute('saved_id')
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
96 except KeyError: # have no saved_id
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
97 pass
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
98 else:
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
99 self._handle_unlinked_or_copied_nodes(node, child)
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
100 return
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
101
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
102 # have no saved_id
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
103 self._handle_new_nodes(node, child)
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
104 pass
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
105
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
106 def do_insert_node(self, node, child):
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
107 self._handle_insert_node_recursive(node, child)
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
108 pass
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
109
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
110 def do_attr_modified(self, node, name, old_value, new_value):
1336
0b5ee9c90af7 Update components and timelines list for async changes on DOM
Thinker K.F. Li <thinker@codemud.net>
parents: 1300
diff changeset
111 if name == 'id' and node.name() != 'svg:use' and new_value:
1299
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
112 #
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
113 # The ID of a node may not be assigned when it being
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
114 # inserted, and be assigned later. So, we checking
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
115 # attribute modification event to assign value of
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
116 # saved_id.
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
117 #
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
118 node.setAttribute('saved_id', new_value)
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
119 elif old_value and (name in self._no_change_attrs):
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
120 #
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
121 # Restore to old value for attributes that is not allowed
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
122 # to be changed by the user.
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
123 #
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
124 node.setAttribute(name, old_value)
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
125 pass
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
126 pass
6949e2b6cae2 Add unlink clone checker.
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
127 pass