File: group_props.py

package info (click to toggle)
dia 0.97.3%2Bgit20160930-9
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 54,372 kB
  • sloc: ansic: 155,065; xml: 16,326; python: 6,641; cpp: 4,935; makefile: 3,833; sh: 540; perl: 137; sed: 19
file content (182 lines) | stat: -rw-r--r-- 5,030 bytes parent folder | download | duplicates (3)
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
#    Group Properties Prototype
#    Copyright (c) 2003, Hans Breuer <hans@breuer.org>
#
#        with multiple selected (but not grouped) objects builds a dialog
#    which contains widgets to change all different properties of the
#    objects.
#
#    Known Issues :
#	- Currently the options to be changed are represented by strings
#	  This is a limitation of PyDia, which does not wrap Dia's UI
#	  elements yet
#	- ...

#    This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

import sys, dia

class CPropsDialog :
	def __init__(self, diagram, data, props) :
		import pygtk
		pygtk.require("2.0")
		import gtk

		self.diagram = diagram
		self.data = data
		self.props = props

		self.win = gtk.Window ()
		self.win.connect("delete_event", self.on_delete)
		self.win.set_title("Group Properties")

		box1 = gtk.VBox()
		self.win.add(box1)
		box1.show()

		box2 = gtk.VBox(spacing=2)
		box2.set_border_width(10)
		box1.pack_start(box2)
		box2.show()

		self.checkboxes = []
		self.optionmenues = []
		if len(props) :
			table = gtk.Table(2, len(props), 0)
		else :
			table = gtk.Table(2, 1, 0)
		table.set_row_spacings(2)
		table.set_col_spacings(5)
		table.set_border_width(5)
		if len(props) :
			y = 0
			for s in props.keys() :
				w = gtk.CheckButton(s)
				self.checkboxes.append(w)
				table.attach(w, 0, 1, y, y+1)
				w.show()
				menu = gtk.Menu()
				milist = None
				for opt in props[s].opts :
					#print opt
					menuitem = gtk.RadioMenuItem (milist, str(opt.value))
					milist = menuitem # GSlist
					menu.append(menuitem)
					menuitem.show()
				menu.show ()
				w = gtk.OptionMenu()
				w.set_menu(menu)
				self.optionmenues.append(w)
				table.attach(w, 1, 2, y, y+1)
				w.show()
				y = y + 1
		else :
			w = gtk.Label("The selected objects don't share any\n properties to change at once.") 
			table.attach(w, 0, 1, y, y+1)
			w.show()
		box2.pack_start(table)
		table.show()

		separator = gtk.HSeparator()
		box1.pack_start(separator, expand=0)
		separator.show()

		box2 = gtk.VBox(spacing=10)
		box2.set_border_width(10)
		box1.pack_start(box2, expand=0)
		box2.show()

		button = gtk.Button("Ok")
		button.connect("clicked", self.on_ok)
		box2.pack_start(button)
		button.set_flags(gtk.CAN_DEFAULT)
		button.grab_default()
		button.show()
		self.win.show()

	def on_ok (self, *args) :
		grp = self.diagram.get_sorted_selected()
		# change the requested properties
		for i in range(0, len(self.checkboxes)) :
			cb = self.checkboxes[i]
			om = self.optionmenues[i]
			if cb.get_active() :
				for o in grp :
					s = self.props.keys()[i]
					o.properties[s] = self.props[s].opts[om.get_history()]
		self.data.update_extents ()
		self.diagram.flush()
		self.win.destroy()

	def on_delete (self, *args) :
		self.win.destroy ()

class PropInfo :
	def __init__ (self, t, n, o) :
		self.num = 1
		self.type = t
		self.name = n
		self.opts = [o]
	def __str__ (self) :
		return self.name + ":" + str(self.opts)
	def Add (self, o) :
		self.num = self.num + 1
		self.opts.append(o)

def dia_objects_props_cb (data, flags) :
	d = dia.active_display().diagram
	grp = d.get_sorted_selected()
	allProps = {}
	numProps = len(grp)
	# check for properties common to all select objects
	for o in grp :
		props = o.properties
		for s in props.keys() :
			if props[s].visible :
				if allProps.has_key(s) :
					allProps[s].Add(props[s])
				else :
					allProps[s] = PropInfo(props[s].type, props[s].name, props[s])
	# now eliminate all props not common ...
	for s in allProps.keys() :
		if allProps[s].num < numProps :
			del allProps[s]
	# ... and all props already equal
	for s in allProps.keys() :
		o1 = allProps[s].opts[0]
		for o in allProps[s].opts :
			if o1.value != o.value :
				o1 = None
				break
		if o1 != None :
			del allProps[s]
		else :
			# if there is something left ensure unique values
			uniques = {}
			for o in allProps[s].opts :
				if uniques.has_key(o.value) :
					continue
				uniques[o.value] = o
			allProps[s].opts = []
			for v in uniques.keys() :
				allProps[s].opts.append(uniques[v])
	# display the dialog
	try :
		dlg = CPropsDialog(d, data, allProps)
	except ImportError :
		dia.message(0, "Dialog creation failed. Missing pygtk?")

dia.register_action ("DialogsGroupproperties", "Dia Group Properties", 
                      "/DisplayMenu/Dialogs/DialogsExtensionStart", 
                       dia_objects_props_cb)