CourseGraph

Webs: Faemalia -:- Greatprawn -:- Playground -:- Technical -:- Tweak
Technical Web Sections: Register -:- Users -:- Changes -:- Index -:- Search -:- Statistics

This is a small real-world example. My fiancee is a teacher, and wants to make a diagram of course objectives. In this graph, the size of the fonts in the nodes should be larger for topics with many other topics depending on them -- the core topics.

In graph terms: Go through all nodes (of a certain kind) and set the font size for each depending on the number of out edges. An out edge is an edge with an arrow on the other end.

The first thing is to have a plugin that registers itself with Dia. Here's a skeleton piece:

import dia
def hello_callback(data, flags):
  print "Hello, Dia!\n"
dia.register_callback("Hello, world",
                      "<Display>/Plugins/Hello",
                      hello_callback)

-- LarsClausen - 26 Dec 2002

Next step: Install required python libs. Happens to cause a major amount of upgrade just to install python-gtk2 and python2.2-dev. Apt makes all this easy.

Now configure with --with-python and see what happens.

-- LarsClausen - 15 Jan 2003

Next thing that happens is that it breaks because it can't find the startup file (python-startup.py). It turns out to be looking for it in /usr/local/share/dia even though I run it from the source script. Needs fixing, but till then I can do make install and have that work. Added warning for when that happens.

Next problem: It crashes in what looks like the pygtk init part. I'm a little worried as to whether it's finding the right pygtk, for 1.2 or 2.0. Can't see a place in the configure rule to check a version, and the python module seems to be called the same regardless of version. That's not very nice during a change-over period.

-- LarsClausen - 15 Jan 2003

Got some hints from the pygtk FAQ showed me how to force the right version, which is now done in python-startup.py.

The script shown above is put into ~/.dia/python/hello.py, and I restart Dia. Le Voila! A new menu item turns up in Dia. We're halfway there.

-- LarsClausen - 15 Jan 2003

Onwards and forwards and all that! Created a new script from hello, called courses.py, and took some code from debug_objects.py (which goes through the layers and objects). So far so good. After a little look at the API, I can go through nodes and check how many connections there are in each:

import sys,dia

def courses_callback(data, flags):
  print "Updating nodes..."
  for layer in data.layers :
    for o in layer.objects :
      connected = 0;
      for c in o.connections :
        connected = connected + len(c.connected)
      if connected > 2 :
        print str(o), " is big"

dia.register_callback("Courses Script",
                      "/Plugins/Courses",
                      courses_callback)

And this works! Now comes the tricky part -- changing size dependent on the number of connections.

Hit upon a snag in this part: If the plugin has a compile error, the python plugin just says it fails to load, no explanation. But aha! Since syntax checking at least is done before modules are loaded, I can just run the script on the command line: python courses.py. That gives me the error.

Now comes the problem: How to set a value in a property? For starters, I want to set the line width (a real). But it complains about a type error -- value is an object. I don't know how to make an object out of a real.

Besides, I don't know if this fits into the undo system, I see no traces of it.


Edit -:- Attach -:- Ref-By -:- Printable -:- More