Changeset 1351

Show
Ignore:
Timestamp:
06/11/07 04:15:48 (1 year ago)
Author:
arj..@yirdis.nl
Message:

Diagram layout and Python source code import kinda work again.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • gaphor/trunk/gaphor/plugins/diagramlayout/__init__.py

    r543 r1351  
    1 # vim:sw=4:et 
    2 """This module provides a means to automatocally layout diagrams. 
     1""" 
     2This module provides a means to automatocally layout diagrams. 
    33 
    44The layout is done like this: 
     
    1010""" 
    1111 
    12 import gaphor.plugin 
     12__plugin__ = 'diagram_layout' 
     13__version__ = '0.1' 
     14__author__ = 'Arjan Molenaar' 
     15 
     16from zope import interface, component 
     17from gaphor.core import _, inject, action, build_action_group 
     18from gaphor.interfaces import IService, IActionProvider 
     19 
    1320import random 
    14 from gaphor import diagram 
     21from gaphor.diagram import items 
    1522import toposort 
    1623 
    17 class DiagramLayoutAction(gaphor.plugin.Action): 
     24 
     25class DiagramLayout(object): 
     26 
     27    interface.implements(IService, IActionProvider) 
     28 
     29    gui_manager = inject('gui_manager') 
     30 
     31    menu_xml = """ 
     32      <ui> 
     33        <menubar action="mainwindow"> 
     34          <menu action="diagram"> 
     35            <menuitem action="diagram-layout" /> 
     36          </menu> 
     37        </menubar> 
     38      </ui>""" 
     39 
     40    def __init__(self): 
     41        self.action_group = build_action_group(self) 
     42 
     43    def init(self, app): 
     44        pass 
     45 
     46    def shutdown(self): 
     47        pass 
    1848 
    1949    def update(self): 
    2050        self.sensitive = bool(self.get_window().get_current_diagram()) 
    2151 
     52    @action(name='diagram-layout', label='Layout diagram', 
     53            tooltip='simple diagram layout') 
    2254    def execute(self): 
    23         d = self.get_window().get_current_diagram() 
    24         if d: 
    25             layout_diagram(d) 
     55        d = self.gui_manager.main_window.get_current_diagram() 
     56        layout_diagram(d) 
     57 
     58    def layout_diagram(self, diag): 
     59        layout_diagram(diag) 
    2660 
    2761 
     
    2963 
    3064def layout_diagram(diag): 
    31     """So an attempt to layout (order) the items on a diagram. The items 
     65    """ 
     66    So an attempt to layout (order) the items on a diagram. The items 
    3267    should already be placed on the diagram and the items should already be 
    3368    connected. 
     
    4782    # First extract data from the diagram (which ones are the nodes, and 
    4883    # the relationships). 
    49     for item in diag.canvas.root.children
    50         if isinstance(item, (diagram.GeneralizationItem, 
    51                              diagram.ImplementationItem)): 
     84    for item in diag.canvas.get_root_items()
     85        if isinstance(item, (items.GeneralizationItem, 
     86                             items.ImplementationItem)): 
    5287            # Primary relationships, should be drawn top-down 
    5388            try: 
     
    5792            except Exception, e: 
    5893                log.error(e) 
    59         elif isinstance(item, diagram.diagramline.DiagramLine): 
     94        elif isinstance(item, items.DiagramLine): 
    6095            # Secondary (associations, dependencies) may be drawn top-down 
    6196            # or left-right 
     
    113148                continue 
    114149            maxy = max(maxy, item.height) 
    115             a = item.get_property('affine') 
     150            a = item.matrix 
    116151            a = (a[0], a[1], a[2], a[3], x, y) 
    117             item.set_property('affine', a) 
     152            item.matrix = a 
     153            item.request_update() 
    118154            x += item.width + MARGIN 
    119155        y += maxy + MARGIN 
     
    124160 
    125161def simple_layout_lines(diag): 
    126     """Just do the layout of the lines in a diagram. The nodes (class, package) 
     162    """ 
     163    Just do the layout of the lines in a diagram. The nodes (class, package) 
    127164    are left where they are (use layout_diagram() if you want to reorder 
    128165    everything). 
     
    132169    """ 
    133170    lines = {} 
    134     for item in diag.canvas.root.children
    135         if isinstance(item, diagram.diagramline.DiagramLine): 
     171    for item in diag.canvas.get_root_items()
     172        if isinstance(item, items.DiagramLine): 
    136173            # Secondary (associations, dependencies) may be drawn top-down 
    137174            # or left-right 
     
    172209 
    173210def find_related_nodes(item, relations): 
    174     """Find related nodes of item, given a list of tuples. 
     211    """ 
     212    Find related nodes of item, given a list of tuples. 
    175213    References to itself are ignored. 
    176214    """ 
     
    187225 
    188226def find_row(item, related_items, sorted): 
    189     """Find the row that contains the most references to item. 
     227    """ 
     228    Find the row that contains the most references to item. 
    190229    """ 
    191230    max_refs = 0 
     
    199238 
    200239def find_center(item): 
    201     """Find the center point of the item, in world coordinates 
     240    """ 
     241    Find the center point of the item, in world coordinates 
    202242    """ 
    203243    x = item.width / 2.0 
    204244    y = item.height / 2.0 
    205     return item.affine_point_i2w(x, y) 
     245    return item.canvas.get_matrix_i2w(item).transform_point(x, y) 
     246 
     247# vim:sw=4:et 
  • gaphor/trunk/gaphor/plugins/pynsource/__init__.py

    r1244 r1351  
    1 # vim:sw=4:et: 
    2  
    3  
    4 import sys 
    5 import os 
     1""" 
     2Code reverse engineer plugin for Python source code. 
     3 
     4This plugin uses PyNSource, written by Andy Bulka 
     5[http://www.atug.com/andypatterns/pynsource.htm]. 
     6 
     7Depends on the Diagram Layout plugin. 
     8""" 
     9 
     10__plugin__ = 'PyNSource' 
     11__version__ = '0.1' 
     12__author__ = 'Arjan Molenaar' 
     13 
    614import gobject 
    7 import pango 
    815import gtk 
    9 import gaphor 
    10 from gaphor.ui.abstractwindow import AbstractWindow 
    11 from gaphor.core import inject 
    12 import gaphor.plugin 
    13 from pynsource import PySourceAsText 
     16from zope import interface, component 
     17from gaphor.core import _, inject, action, build_action_group 
     18from gaphor.interfaces import IService, IActionProvider 
     19 
    1420from engineer import Engineer 
    1521 
     22 
    1623NAME_COLUMN = 0 
    1724 
    1825 
    19 class PyNSourceAction(gaphor.plugin.Action): 
    20  
    21     action_manager = inject('action_manager') 
     26class PyNSource(object): 
     27 
     28    interface.implements(IService, IActionProvider) 
     29 
     30    gui_manager = inject('gui_manager') 
     31 
     32    menu_xml = """ 
     33      <ui> 
     34        <menubar action="mainwindow"> 
     35          <menu action="file"> 
     36            <menu action="file-import"> 
     37              <menuitem action="file-import-pynsource" /> 
     38            </menu> 
     39          </menu> 
     40        </menubar> 
     41      </ui>""" 
    2242 
    2343    def __init__(self): 
    24         gaphor.plugin.Action.__init__(self) 
    2544        self.win = None 
    26  
     45        self.action_group = build_action_group(self) 
     46 
     47    def init(self, app): 
     48        pass 
     49 
     50    def shutdown(self): 
     51        pass 
     52 
     53    @action(name='file-import-pynsource', label='Python source code', 
     54            tooltip='Import Python source code') 
    2755    def execute(self): 
    2856        dialog = self.create_dialog() 
     
    4977        engineer.process(files) 
    5078 
    51         main_window = self.get_window() 
     79        main_window = self.gui_manager.main_window 
    5280        # Open and select the new diagram in the main window: 
    5381        main_window.select_element(engineer.diagram) 
    54         self.action_manager.execute('OpenModelElement'
     82        main_window.show_diagram(engineer.diagram
    5583 
    5684    def create_dialog(self): 
    5785        dialog = gtk.Dialog("Import Python files", 
    58                             self.get_window().get_window()
     86                            self.gui_manager.main_window.window
    5987                            gtk.DIALOG_MODAL, 
    6088                            (gtk.STOCK_OK, gtk.RESPONSE_OK, 
     
    177205 
    178206 
    179 if __name__ in ('__main__', '__builtin__'): 
    180     print 'Loading...' 
    181     import gtk 
    182     win = PyNSourceWindow() 
    183     win.construct() 
    184     win.get_window().connect('destroy', lambda e: gtk.main_quit()) 
    185     gtk.main() 
    186  
     207# vim:sw=4:et:ai 
  • gaphor/trunk/gaphor/plugins/pynsource/engineer.py

    r1219 r1351  
    44""" 
    55 
     6from zope import component 
    67from gaphor import UML 
    7 from gaphor import diagram 
    8 from gaphor.plugin import import_plugin 
     8from gaphor.diagram import items 
     9from gaphor.core import inject 
     10from gaphor.diagram.interfaces import IConnect 
    911 
    1012from pynsource import PySourceAsText 
     
    1315 
    1416class Engineer(object): 
    15     """The Engineer class will create a Gaphor model based on a list of Python 
     17    """ 
     18    The Engineer class will create a Gaphor model based on a list of Python 
    1619    files. 
    1720    """ 
     21 
     22    element_factory = inject('element_factory') 
     23    diagram_layout = inject('diagram_layout') 
    1824 
    1925    def process(self, files=None): 
     
    3541         
    3642        try: 
    37             self._root_package = UML.lselect(lambda e: isinstance(e, UML.Package) and not e.namespace)[0] 
     43            self._root_package = self.element_factory.lselect(lambda e: isinstance(e, UML.Package) and not e.namespace)[0] 
    3844        except IndexError: 
    3945            pass # running as test? 
     
    4349 
    4450        # Step 0: create a diagram to put the newly created elements on 
    45         self.diagram = UML.create(UML.Diagram) 
     51        self.diagram = self.element_factory.create(UML.Diagram) 
    4652        self.diagram.name = 'New classes' 
    4753        self.diagram.package = self._root_package 
     
    6470            self._create_methods(clazz) 
    6571 
    66         layout_diagram = None 
    67         try: 
    68             layout_diagram = import_plugin('diagramlayout').layout_diagram 
    69         except ImportError, e: 
    70             log.error('Could not import diagramlayout module', e) 
    71         else: 
    72             layout_diagram(self.diagram) 
     72        self.diagram_layout.layout_diagram(self.diagram) 
    7373 
    7474    def _create_class(self, clazz, name): 
    75         c = UML.create(UML.Class) 
     75        c = self.element_factory.create(UML.Class) 
    7676        c.name = name 
    7777        c.package = self.diagram.namespace 
    78         ci = self.diagram.create(diagram.ClassItem) 
     78        ci = self.diagram.create(items.ClassItem) 
    7979        ci.subject = c 
    8080        clazz.gaphor_class = c 
     
    9191                except KeyError, e: 
    9292                    print 'No class found named', superclassname 
    93                     others = UML.lselect(lambda e: isinstance(e, UML.Class) and e.name == superclassname) 
     93                    others = self.element_factory.lselect(lambda e: isinstance(e, UML.Class) and e.name == superclassname) 
    9494                    if others: 
    9595                        superclass = others[0] 
    9696                        print 'Found class in factory: %s' % superclass.name 
    97                         superclass_item = self.diagram.create(diagram.ClassItem) 
     97                        superclass_item = self.diagram.create(items.ClassItem) 
    9898                        superclass_item.subject = superclass 
    9999                    else: 
     
    101101                # Finally, create the generalization relationship 
    102102                print 'Creating Generalization for %s' % clazz, superclass 
    103                 gen = UML.create(UML.Generalization) 
     103                gen = self.element_factory.create(UML.Generalization) 
    104104                gen.general = superclass 
    105105                gen.specific = clazz.gaphor_class 
    106                 geni = self.diagram.create(diagram.GeneralizationItem) 
     106                geni = self.diagram.create(items.GeneralizationItem) 
    107107                geni.subject = gen 
    108                 superclass_item.connect_handle(geni.handles[0]) 
    109                 clazz.gaphor_class_item.connect_handle(geni.handles[-1]) 
     108                 
     109                adapter = component.queryMultiAdapter((superclass_item, geni), IConnect) 
     110                assert adapter 
     111                handle = geni.handles()[0] 
     112                adapter.connect(handle, handle.x, handle.y) 
     113                #clazz.gaphor_class_item.connect_handle(geni.handles[-1]) 
     114                adapter = component.queryMultiAdapter((clazz.gaphor_class_item, geni), IConnect) 
     115                assert adapter 
     116                handle = geni.handles()[-1] 
     117                adapter.connect(handle, handle.x, handle.y) 
    110118 
    111119    def _create_attributes(self, clazz): 
     
    117125    def _create_methods(self, clazz): 
    118126        for adef in clazz.defs: 
    119             op = UML.create(UML.Operation) 
     127            op = self.element_factory.create(UML.Operation) 
    120128            op.name = adef 
    121129            clazz.gaphor_class.ownedOperation = op 
     
    127135        except KeyError, e: 
    128136            print 'No class found named', classname 
    129             others = UML.lselect(lambda e: isinstance(e, UML.Class) and e.name == classname) 
     137            others = self.element_factory.lselect(lambda e: isinstance(e, UML.Class) and e.name == classname) 
    130138            if others: 
    131139                superclass = others[0] 
    132140                print 'Found class in factory: %s' % superclass.name 
    133                 superclass_item = self.diagram.create(diagram.ClassItem) 
     141                superclass_item = self.diagram.create(items.ClassItem) 
    134142                superclass_item.subject = superclass 
    135143            else: 
     
    164172            head_type_item = clazz.gaphor_class_item 
    165173 
    166             relation = UML.create(UML.Association) 
    167             head_end = UML.create(UML.Property) 
    168             head_end.lowerValue = UML.create(UML.LiteralSpecification) 
    169             tail_end = UML.create(UML.Property) 
     174            relation = self.element_factory.create(UML.Association) 
     175            head_end = self.element_factory.create(UML.Property) 
     176            head_end.lowerValue = self.element_factory.create(UML.LiteralSpecification) 
     177            tail_end = self.element_factory.create(UML.Property) 
    170178            tail_end.name = attr.attrname 
    171179            tail_end.visibility = self._visibility(attr.attrname) 
    172180            tail_end.aggregation = 'composite' 
    173             tail_end.lowerValue = UML.create(UML.LiteralSpecification) 
     181            tail_end.lowerValue = self.element_factory.create(UML.LiteralSpecification) 
    174182            relation.package = self.diagram.namespace 
    175183            relation.memberEnd = head_end 
     
    181189 
    182190            # Now create the diagram item: 
    183             association = self.diagram.create(diagram.AssociationItem) 
     191            association = self.diagram.create(items.AssociationItem) 
    184192 
    185193            # First connect one handle: 
    186             head_type_item.connect_handle(association.handles[0]) 
     194            #head_type_item.connect_handle(association.handles[0]) 
     195            adapter = component.queryMultiAdapter((head_type_item, association), IConnect) 
     196            assert adapter 
     197            handle = association.handles()[0] 
     198            adapter.connect(handle, handle.x, handle.y) 
     199 
    187200             
    188201            # Now the subject 
     
    194207            # an Association. in stead the association applied as subject 
    195208            # is used. 
    196             tail_type_item.connect_handle(association.handles[-1]) 
     209            #tail_type_item.connect_handle(association.handles[-1]) 
     210            adapter = component.queryMultiAdapter((tail_type_item, association), IConnect) 
     211            assert adapter 
     212            handle = association.handles()[-1] 
     213            adapter.connect(handle, handle.x, handle.y) 
    197214 
    198215        else: 
    199216            # Create a simple attribute: 
    200217            #print "%s %s" % (attr.attrname, static) 
    201             prop = UML.create(UML.Property) 
     218            prop = self.element_factory.create(UML.Property) 
    202219            prop.name = attr.attrname 
    203220            prop.visibility = self._visibility(attr.attrname) 
  • gaphor/trunk/gaphor/plugins/xmiexport/__init__.py

    r1348 r1351  
    66__version__ = "0.1" 
    77__author__ = "Jeroen Vloothuis" 
    8 __description__ = 'This plugin extends Gaphor with XMI export functionality.' 
    98 
    109import gtk 
  • gaphor/trunk/setup.py

    r1348 r1351  
    129129            'copy = gaphor.services.copyservice:CopyService', 
    130130            'xmi_export = gaphor.plugins.xmiexport:XMIExport', 
     131            'diagram_layout = gaphor.plugins.diagramlayout:DiagramLayout', 
     132            'pynsource = gaphor.plugins.pynsource:PyNSource', 
    131133        ], 
    132134        'gaphor.uicomponents': [