comparison inkscape/MB_EditButton.py @ 248:1958bb2a87a2

* Check if the selected element is a group ot not. A symbol or button must be a group. * Reimplement the button editor. We will use another instance of the inkscape to edit the content of a button.
author wycc
date Sat, 03 Jan 2009 08:43:29 +0800
parents
children
comparison
equal deleted inserted replaced
247:d9a78c859660 248:1958bb2a87a2
1 #!/usr/bin/python
2 import inkex
3 import pygtk
4 import gtk
5 from copy import deepcopy
6 from lxml import etree
7 import os
8 import tempfile
9
10 class ConvertToButton(inkex.Effect):
11 def effect(self):
12 self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
13 self.window.set_position(gtk.WIN_POS_MOUSE)
14 self.defaultname = 'input symbol name here'
15 if self.fillcontent() == False:
16 self.window.show_all()
17 self.window.connect("delete_event", gtk.main_quit)
18 gtk.main()
19 def onQuit(self,data):
20 gtk.main_quit()
21 def onAssign(self,data):
22 text = self.text.get_text()
23 if text != self.defaultname:
24 self.node.set("mbname",text)
25 gtk.main_quit()
26
27 def confirm(self,msg):
28 vbox = gtk.VBox()
29 vbox.pack_start(gtk.Label(msg))
30 self.button = gtk.Button('OK')
31 vbox.pack_start(self.button)
32 self.button.connect("clicked", self.onQuit)
33 self.window.add(vbox)
34 def dumpattr(self,n):
35 s = ""
36 for a,v in n.attrib.items():
37 s = s + ("%s=%s" % (a,v))
38 return s
39
40 def dump(self,node,l=0):
41 print " " * l*2,"<", node.tag, self.dumpattr(node),">"
42 for n in node:
43 self.dump(n,l+1)
44 print " " * l * 2,"/>"
45
46 def hide_frame(self,frame):
47 frame.set('style','display:none')
48 def show_frame(self,frame):
49 frame.set('style','')
50
51 def EditNormalButton(self,event,node):
52 self.EditButton(node,'_normal')
53
54 def EditActiveButton(self,event,node):
55 self.EditButton(node,'_active')
56
57 def EditClickButton(self,event,node):
58 self.EditButton(node,'_click')
59
60 def EditFrame(self,node):
61 # Generate a SVG file with node and invoke inkscape to edit it
62 svg = etree.Element('svg')
63 for n in node:
64 svg.append(deepcopy(n))
65 fd,fname = tempfile.mkstemp(suffix='.svg')
66 f = os.fdopen(fd,"w")
67 f.write(etree.tostring(svg))
68 f.close()
69 os.system("inkscape %s >/dev/null 2>/dev/null" % fname)
70 svg = etree.parse(fname)
71 os.unlink(fname)
72 newnode=[]
73 for n in svg.getroot():
74 if n.tag == '{http://www.w3.org/2000/svg}g':
75 newnode.append(n)
76 if n.tag == '{http://www.w3.org/2000/svg}rect':
77 newnode.append(n)
78 if n.tag == '{http://www.w3.org/2000/svg}text':
79 newnode.append(n)
80 return newnode
81
82
83
84
85 def duplicateAttribute(self,new,old):
86 for k,v in old.attrib.items():
87 new.set(k,v)
88
89
90 def EditButton(self,node,mode):
91 name = node.get('mbname')
92 for frame in node:
93 if frame.get('mbname') == name+mode:
94 newnode = self.EditFrame(frame)
95 oldframe = deepcopy(frame)
96 frame.clear()
97 self.duplicateAttribute(frame,oldframe)
98 for n in newnode:
99 frame.append(n)
100 return
101 def DisplayNormalButton(self,event,node):
102 self.displayButton(node,'_normal')
103 def DisplayActiveButton(self,event,node):
104 self.displayButton(node,'_active')
105 def DisplayClickButton(self,event,node):
106 self.displayButton(node,'_click')
107 def displayButton(self,node,mode):
108 name = node.get('mbname')
109 for n in node:
110 if n.get('mbname') == name+mode:
111 n.set('style','')
112 else:
113 n.set('style','display:none')
114 gtk.main_quit()
115
116
117
118 def fillcontent(self):
119 if len(self.selected) != 1:
120 self.confirm('Please select one group only')
121 return False
122 for id,node in self.selected.iteritems():
123 #self.dump(node)
124 name = node.get("mbname")
125 if name == None:
126 self.confirm("The MadButterFly symbol is not defined yet. Please convert it to the symbol before convert it to button.")
127 return False
128 type = node.get("mbtype")
129 if type != 'button':
130 self.confirm('This is not a button')
131 return False
132 hbox = gtk.HBox()
133 self.window.add(hbox)
134 button = gtk.Button('Edit Normal')
135 hbox.pack_start(button)
136 button.connect("clicked", self.EditNormalButton,node)
137 button = gtk.Button('Edit Active')
138 hbox.pack_start(button)
139 button.connect("clicked", self.EditActiveButton,node)
140 button = gtk.Button('Edit Click')
141 hbox.pack_start(button)
142 button.connect("clicked", self.EditClickButton,node)
143 button = gtk.Button('Display Normal')
144 hbox.pack_start(button)
145 button.connect("clicked", self.DisplayNormalButton,node)
146 button = gtk.Button('Display Active')
147 hbox.pack_start(button)
148 button.connect("clicked", self.DisplayActiveButton,node)
149 button = gtk.Button('Display Click')
150 hbox.pack_start(button)
151 button.connect("clicked", self.DisplayClickButton,node)
152 return False
153
154
155 a=ConvertToButton()
156 a.affect()
157
158 # vim: set ts=4