dia r4321 - in trunk: . plug-ins/python samples/Self



Author: hans
Date: Sun Mar 15 19:04:21 2009
New Revision: 4321
URL: http://svn.gnome.org/viewvc/dia?rev=4321&view=rev

Log:
2009-03-15  Hans Breuer  <hans breuer org>

	* plug-ins/python/allprops.py : alpha-numeric sorting by type; after 
	by number of users
	* plug-ins/python/aobjects.py : to make the resulting diagram more 
	interesting we set every sting property with it's name
	* plug-ins/python/bbox.py : create annotated bounding boxes
	* plug-ins/python/diagx.py : handling more tags/types like Ellipsis,
	EnumValue and Function, some other are silently ignored
	* plug-ins/python/doxrev.py : try to arrange before inserting the
	classes into the diagram
	* plug-ins/python/arrange.py : added for the above
	* plug-ins/python/otypes.py : a better fix for running standalone or
	from the toolbox menu

	* plug-ins/python/pydia-paperinfo.c : fixed typo in docu
	
	* samples/Self/PyDiaObjects.dia : updated to 0.97 interface (e.g. PaperInfo
	and fixed the same typo)



Added:
   trunk/plug-ins/python/arrange.py   (contents, props changed)
Modified:
   trunk/ChangeLog
   trunk/plug-ins/python/allprops.py
   trunk/plug-ins/python/aobjects.py
   trunk/plug-ins/python/bbox.py
   trunk/plug-ins/python/diagx.py
   trunk/plug-ins/python/otypes.py
   trunk/plug-ins/python/pydia-paperinfo.c
   trunk/samples/Self/PyDiaObjects.dia

Modified: trunk/plug-ins/python/allprops.py
==============================================================================
--- trunk/plug-ins/python/allprops.py	(original)
+++ trunk/plug-ins/python/allprops.py	Sun Mar 15 19:04:21 2009
@@ -72,9 +72,9 @@
 	ot = dia.get_object_type("UML - Class")
 
 	props_keys = props_by_name.keys()
-	# alpha-numeric sorting by type; than by number of users
-	props_keys.sort (lambda a,b : cmp(props_by_name[b][0].type, props_by_name[a][0].type))
+	# alpha-numeric sorting by type; after by number of users
 	props_keys.sort (lambda a,b : len(props_by_name[b][1]) - len(props_by_name[a][1]))
+	props_keys.sort (lambda a,b : cmp(props_by_name[a][0].type, props_by_name[b][0].type))
 	
 	almost_all = 98 * len(otypes) / 100 # 98 %
 

Modified: trunk/plug-ins/python/aobjects.py
==============================================================================
--- trunk/plug-ins/python/aobjects.py	(original)
+++ trunk/plug-ins/python/aobjects.py	Sun Mar 15 19:04:21 2009
@@ -21,6 +21,16 @@
 
 import sys, dia, string
 
+def set_object_string (o) :
+	keys = o.properties.keys()
+	for s in keys :
+		p = o.properties[s]
+		if p.type in ["string", "text"] :
+			if s in ["name", "text"] :
+				o.properties[s] = o.type.name
+			else :
+				o.properties[s] = s
+
 def aobjects_cb(data, flags) :
 
 	# copied from otypes.py
@@ -66,9 +76,8 @@
 				continue # can't create empty group
 			#print st
 			o, h1, h2 = dia.get_object_type(st).create (cx, cy)
-			# make it a bit more interesting
-			if o.properties.has_key("name") :
-				o.properties["name"] = st
+			# to make the resulting diagram more interesting we set every sting property with it's name
+			set_object_string (o)
 			w = o.bounding_box.right - o.bounding_box.left
 			h = o.bounding_box.bottom - o.bounding_box.top
 			o.move (cx, cy)

Added: trunk/plug-ins/python/arrange.py
==============================================================================
--- (empty file)
+++ trunk/plug-ins/python/arrange.py	Sun Mar 15 19:04:21 2009
@@ -0,0 +1,116 @@
+# Copyright (c) 2007  Hans Breuer <hans breuer org>
+
+#    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 dia
+import math
+
+def DeepCalc (dict, key, seen = None) :
+	# calculate all (deep) dependencies
+	# give a directory with component name as key and dependency components as list in dict[key][2]
+	if not seen :
+		seen = {}
+	for k in dict[key][2] :
+		if seen and seen.has_key (k) :
+			continue
+		seen[k] = 1
+		DeepCalc (dict, k, seen)
+	return len(seen.keys())
+
+def arrange_connected (data, flags) :
+	objs = data.get_sorted_selected()
+	if len(objs) == 0 :
+		objs = data.active_layer.objects
+	# all objects having at least one connection
+	bs = {}
+	print "objs", len(objs)
+	edges = {}
+	for o in objs :
+		for c in o.connections: # ConnectionPoint
+			for n in c.connected: # connection object
+				if not (n.handles[0].connected_to and n.handles[1].connected_to):
+					continue
+				a = n.handles[0].connected_to.object
+				b = n.handles[1].connected_to.object
+				ak = a.properties["name"].value
+				bk = b.properties["name"].value
+				# create an edge key
+				ek = ak + "->" + bk
+				if edges.has_key (ek) :
+					continue # already seen
+				print ek
+				edges[ek] = 1
+				if bs.has_key (ak) :
+					use = bs[ak]
+					use[2].append (bk)
+					bs[ak] = use
+				else :
+					bs[ak] = [a, 0, [bk]]
+				if bs.has_key (bk) :
+					use = bs[bk]
+					use[1] += 1
+					bs[bk] = use
+				else :
+					bs[bk] = [b, 1, []]
+	# sort by number of connections
+	bst = []
+	dx = 0
+	dy = 0
+	for key in bs.keys() :
+		o = bs[key][0]
+		if not o.properties["elem_width"] :
+			print o
+			continue
+		if o.properties["elem_width"].value > dx : dx = o.properties["elem_width"].value
+		if o.properties["elem_height"].value > dy : dy = o.properties["elem_height"].value
+		n = bs[key][1] # (use count, dependencies)
+		bst.append ((o, n, DeepCalc (bs, key)))
+	bst.sort (lambda a, b : cmp(a[1], b[1]))
+	if len(bs) < 2 :
+		return
+	# average weight gives the number of rows
+	aw = 0.0
+	for t in bst :
+		aw += (t[1] + t[2])
+	aw = aw / len(bst)
+	rows = int (len(bst) / math.sqrt(aw))
+	if rows < 2 :
+		rows = 2
+	offsets = []
+	for i in range(0, rows) : offsets.append(0)
+	dx *= 1.4
+	dy *= 1.8
+	for t in bst :
+		y = t[1] # t[1] = 0 :  start, noone uses it
+		# correction to move further down
+		c = rows - (rows * t[2] / aw)
+		if t[1] : # the less users the far below
+			if t[2] == 0 :
+				y = rows - 1
+			elif t[1] >= rows :
+				# compensate for some of the weight, FIXME: better guess needed
+				y = int(rows - 2 * aw / (t[2] + t[1]))
+		print t[0].properties["name"].value, t[1], t[2], y, c
+		# move the object to it's new place
+		x = offsets[y]
+		t[0].move (x * dx, y * dy)
+		offsets[y] += 1
+	data.update_extents ()
+
+# this module is loaded by some other plug-ins but can also work on it's own
+# if it is loaded first as Dia plug-in and later as Python module everything works
+# fine due to Pythoninitializing the module only once
+dia.register_callback ("Arrange Objects", 
+                       "<Display>/Objects/Arrange", 
+                       arrange_connected)

Modified: trunk/plug-ins/python/bbox.py
==============================================================================
--- trunk/plug-ins/python/bbox.py	(original)
+++ trunk/plug-ins/python/bbox.py	Sun Mar 15 19:04:21 2009
@@ -19,7 +19,7 @@
 
 import sys, dia, string
 
-def work_cb(data, flags) :
+def bbox_cb (data, flags) :
 
 	layer = data.active_layer
 	dest = data.add_layer ("BBox of '%s' (%s)" % (layer.name, sys.platform), -1)
@@ -34,7 +34,24 @@
 		b.properties["line_colour"] = 'red'
 		dest.add_object (b)
 
+def annotate_cb (data, flags) :
 
-dia.register_action ("DrawBoundingbox", "Dia BoundingBox Drawing", 
+	layer = data.active_layer
+	dest = data.add_layer ("Annotated '%s' (%s)" % (layer.name, sys.platform), -1)
+	ann_type = dia.get_object_type ("Standard - Text")
+
+	for o in layer.objects :
+		bb = o.bounding_box
+		a, h1, h2 = ann_type.create (bb.right, bb.top)
+		
+		a.properties["text"] = "h: %g w: %g" % (bb.bottom - bb.top, bb.right - bb.left)
+
+		dest.add_object (a)
+
+dia.register_action ("DrawBoundingbox", "Draw BoundingBox", 
+                     "/DisplayMenu/Debug/DebugExtensionStart", 
+                     bbox_cb)
+
+dia.register_action ("AnnotateMeasurements", "Annotate", 
                      "/DisplayMenu/Debug/DebugExtensionStart", 
-                     work_cb)
+                     annotate_cb)

Modified: trunk/plug-ins/python/diagx.py
==============================================================================
--- trunk/plug-ins/python/diagx.py	(original)
+++ trunk/plug-ins/python/diagx.py	Sun Mar 15 19:04:21 2009
@@ -28,12 +28,18 @@
 		return 0
 	def IsUnion (self) :
 		return 0
+	def IsClass (self) :
+		return 0
 	def Name (self) :
 		return self.name
 
 class Type(Node) :
 	def __init__ (self, name) :
 		Node.__init__(self, name)
+		self.names = []
+	def AddName (self, name) :
+		# for enumerations
+		self.names.append(name)
 
 class Union(Node) :
 	def __init__ (self, name) :
@@ -45,6 +51,8 @@
 		self.names.append(name)
 	def Name (self) :
 		ms = []
+		# shortcut to avoid endless recursion with self referencing uions
+		return self.name
 		for s in self.names :
 			ms.append (g_nodes[s].Name())
 		return string.join(ms, "; ")
@@ -76,32 +84,17 @@
 		elif "private" == self.access : return 1 #UML_PRIVATE
 		return 0 #UML_PUBLIC
 
-class Method(Node) :
+class Function(Node) :
 	def __init__ (self, name, type) :
 		Node.__init__(self, name)
 		self.returns = type
 		self.params = []
-		self.const = 0
-		self.static = 0
-		self.access = "public"
-		self.virtual = 0
-	def IsMethod (self) :
-		return 1
 	def AddArg (self, arg) :
 		self.params.append (arg)
 	def Type (self) :
 		if g_nodes.has_key (self.returns) :
 			return g_nodes[self.returns].Name()
 		return ""
-	def Visibility (self) :
-		"This function is mapping from string to dia enum; should probably not be done here"
-		if "protected" == self.access : return 2 #UML_PROTECTED
-		elif "private" == self.access : return 1 #UML_PRIVATE
-		return 0 #UML_PUBLIC
-	def InheritanceType (self) :
-		if self.virtual > 1 : return 0 #UML_ABSTRACT
-		if self.virtual > 0 : return 1 #UML_POLYMORPHIC
-		return 2
 	def Signature (self) :
 		args = []
 		ret = ""
@@ -116,6 +109,25 @@
 			ret = g_nodes[self.returns].Name() + " "
 		return ret + self.name + " (" + string.join(args, ", ") + ")"
 
+class Method(Function) :
+	def __init__ (self, name, type) :
+		Function.__init__(self, name, type)
+		self.const = 0
+		self.static = 0
+		self.access = "public"
+		self.virtual = 0
+	def IsMethod (self) :
+		return 1
+	def Visibility (self) :
+		"This function is mapping from string to dia enum; should probably not be done here"
+		if "protected" == self.access : return 2 #UML_PROTECTED
+		elif "private" == self.access : return 1 #UML_PRIVATE
+		return 0 #UML_PUBLIC
+	def InheritanceType (self) :
+		if self.virtual > 1 : return 0 #UML_ABSTRACT
+		if self.virtual > 0 : return 1 #UML_POLYMORPHIC
+		return 2
+
 class Klass(Node) :
 	def __init__ (self, name) :
 		Node.__init__(self, name)
@@ -126,6 +138,8 @@
 		self.parents.append (id)
 	def AddMember (self, id) :
 		self.members.append (id)
+	def IsClass (self) :
+		return 1
 	def Name (self) :
 		if g_nodes.has_key (self.context) and g_nodes[self.context].Name() != "" :
 			return g_nodes[self.context].Name() + "::" + self.name
@@ -151,7 +165,10 @@
 			elif g_nodes[id].IsUnion() :
 				print "\t" + g_nodes[id].Name() 
 			else :
-				print "\t" + g_nodes[id].Name() + ":" + g_nodes[g_nodes[id].type].Name()			
+				try :
+					print "\t" + g_nodes[id].Name() + ":" + g_nodes[g_nodes[id].type].Name()
+				except AttributeError :
+					print "AttributeError:", g_nodes[id]
 
 class Namespace(Node) :
 	def __init__ (self, name) :
@@ -172,7 +189,7 @@
 	import xml.parsers.expat
 	global g_classes
 
-	ctx = [] 
+	ctx = []
 	def start_element(name, attrs) :
 		o = None
 		if name in ["Class", "Struct"] :
@@ -234,7 +251,25 @@
 			if ctx[-1][1] :
 				ctx[-1][1].AddArg (o)
 			o = None # lookup not possible
-			
+		elif "Ellipsis" == name :
+			o = Argument ("", "...")
+			if ctx[-1][1] :
+				ctx[-1][1].AddArg (o)
+			o = None # lookup not possible			
+		elif "EnumValue" == name :
+			if ctx[-1][1] :
+				ctx[-1][1].AddName (attrs["name"])
+		elif name in ["Function", "OperatorFunction", "FunctionType"] :
+			if attrs.has_key("name") :
+				o = Function (attrs["returns"], attrs["name"])
+			else : # function & type
+				o = Function (attrs["returns"], attrs["id"])				
+		elif "Variable" == name :
+			pass # not ofinterest?
+		elif "File" == name :
+			pass # FIXME: thrown away
+		else :
+			print "Unhandled:", name
 		if o :
 			if attrs.has_key("context") :
 				#print attrs["context"]
@@ -354,9 +389,16 @@
 if __name__ == '__main__': 
 	Process(sys.argv[1])
 	for c in g_classes :
-		if c.Name()[:5] == "OBS::" :
-			c.Dump()
-
-import dia
-#dia.register_import("Cpp via GCC_XML", "cpp", ImportCpp)
-dia.register_import("XML from GCC_XML", "xml", ImportXml)
+		if c.Name()[:2] != "__" and c.Name()[:5] != "std::" :
+			try :
+				if c.IsClass() :
+					c.Dump()
+			except KeyError :
+				pass
+else :
+	try :
+		import dia
+		#dia.register_import("Cpp via GCC_XML", "cpp", ImportCpp)
+		dia.register_import("XML from GCC_XML", "xml", ImportXml)
+	except ImportError :
+		pass

Modified: trunk/plug-ins/python/otypes.py
==============================================================================
--- trunk/plug-ins/python/otypes.py	(original)
+++ trunk/plug-ins/python/otypes.py	Sun Mar 15 19:04:21 2009
@@ -32,12 +32,12 @@
 
 def otypes_cb(data, flags) :
 
-	if dia.active_display () :
+	if data :
+		diagram = None # we may be running w/o GUI
+	else :
 		diagram = dia.new("Object Types.dia")
-		layer = diagram.data.active_layer
-	else : # non-interactive
-		diagram = None
-		layer = data.active_layer
+		data = diagram.data
+	layer = data.active_layer
 
 	otypes = dia.registered_types()
 	keys = otypes.keys()
@@ -78,7 +78,6 @@
 
 	for sp in packages.keys() :
 		pkg = packages[sp]
-		print "Package", sp
 		op, h1, h2 = dtp.create(0.0, cy + 1.0)
 		op.properties["name"] = sp
 		layer.add_object(op)
@@ -173,13 +172,12 @@
 		op.move_handle(h,(maxx + 1.0, maxy + 1.0), 0, 0)
 		cy = maxy + 2.0
 		maxx = 0 # every package a new size
-	print "Size:", cx, cy
-	layer.update_extents ()
-	data.update_extents ()
+	data.update_extents()
 	if diagram :
 		diagram.display()
-		diagram.update_extents()
 		diagram.flush()
+	# make it work standalone
+	return data
 
 dia.register_action ("HelpOtypes", "Dia Object Types",
                      "/ToolboxMenu/Help/HelpExtensionStart", 

Modified: trunk/plug-ins/python/pydia-paperinfo.c
==============================================================================
--- trunk/plug-ins/python/pydia-paperinfo.c	(original)
+++ trunk/plug-ins/python/pydia-paperinfo.c	Sun Mar 15 19:04:21 2009
@@ -132,5 +132,5 @@
     (setattrofunc)0,
     (PyBufferProcs *)0,
     0L, /* Flags */
-    "dia.Paperinfo is part of dia.DiagramData escribing the paper"
+    "dia.Paperinfo is part of dia.DiagramData describing the paper"
 };

Modified: trunk/samples/Self/PyDiaObjects.dia
==============================================================================
Binary files. No diff available.



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]