Changeset 1049

Show
Ignore:
Timestamp:
10/28/06 10:08:09 (2 years ago)
Author:
wrobell
Message:

- update item style information

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • gaphor/branches/new-canvas/gaphor/diagram/__init__.py

    r1022 r1049  
    3131    global _uml_to_item_map 
    3232    _uml_to_item_map[element] = item 
     33 
     34 
     35 
     36class Styles(object): 
     37    """ 
     38    Item style information. Style information is provided through object's 
     39    attributes, i.e. 
     40 
     41        __style__ = { 
     42            'name-align': ('center', 'top'), 
     43        } 
     44 
     45    is translated to 
     46 
     47        >>> print style.name_align 
     48        ('center', 'top') 
     49    """ 
     50    def add(self, name, value): 
     51        """ 
     52        Add style variable. 
     53 
     54        Variable name can contain hyphens, which is converted to 
     55        underscode, i.e. 'name-align' -> 'name_align'. 
     56 
     57        name  - style variable name 
     58        value - style variable value 
     59        """ 
     60        name = name.replace('-', '_') 
     61        setattr(self, name, value) 
     62 
     63 
     64    def items(self): 
     65        """ 
     66        Return iterator of (name, value) style information items. 
     67        """ 
     68        return self.__dict__.iteritems() 
    3369 
    3470 
     
    110146class DiagramItemMeta(type): 
    111147    """Initialize a new diagram item. 
    112     1. Register UML.Elements by means of the __uml__ attribute. 
     148    1. Register UML.Elements by means of the __uml__ attribute (see 
     149       mapUMLClass method). 
     150    1. Set items styles information. 
     151 
     152    styles - style information 
    113153    """ 
    114154 
    115     def __new__(self, name, bases, data): 
    116         # map uml classes to diagram items 
    117         cls = type.__new__(self, name, bases, data) 
     155    def __init__(self, name, bases, data): 
     156#        cls = type.__new__(self, name, bases, data) 
     157        type.__init__(self, name, bases, data) 
     158 
     159        self.mapUMLClass(data) 
     160        self.setStyles(bases, data) 
     161 
     162    def mapUMLClass(self, data): 
     163        """ 
     164        Map UML class to diagram item. 
     165 
     166        cls  - new instance of item class 
     167        data - metaclass data with UML class information  
     168 
     169        """ 
    118170        if '__uml__' in data: 
    119171            obj = data['__uml__'] 
    120172            if isinstance(obj, (tuple, set, list)): 
    121173                for c in obj: 
    122                     set_diagram_item(c, cls
     174                    set_diagram_item(c, self
    123175            else: 
    124                 set_diagram_item(obj, cls) 
    125  
    126         return cls 
    127  
    128  
    129     def __init__(self, name, bases, data): 
    130         # stereotype align information 
    131         align = ItemAlign() # center, top 
    132         align.outside = getattr(self, '__o_align__', False) 
    133         if align.outside: 
    134             align.margin = (0, 2) * 4 
    135         else: 
    136             align.margin = (5, 30) * 2 
    137         self.set_cls_align('s', align, data) 
    138  
    139  
    140     def set_cls_align(self, kind, align, data): 
    141         assert kind in ('s', 'n') 
    142  
    143         hn = '__%s_align__' % kind 
    144         vn = '__%s_valign__' % kind 
    145  
    146         if hn in data: 
    147             align.align = data[hn] 
    148         if vn in data: 
    149             align.valign = data[vn] 
    150  
    151         setattr(self, '%s_align' % kind, align) 
     176                set_diagram_item(obj, self) 
     177 
     178 
     179    def setStyles(self, bases, data): 
     180        """ 
     181        Set item styles information by merging provided information with 
     182        style information from base classes. 
     183 
     184        cls   - new instance of diagram item class 
     185        bases - base classes of an item 
     186        data  - metaclass data with styles information 
     187        """ 
     188        styles = Styles() 
     189        for c in bases: 
     190            if hasattr(c, 'styles'): 
     191                for (name, value) in c.styles.items(): 
     192                    styles.add(name, value) 
     193 
     194        if '__style__' in data: 
     195            for (name, value) in data['__style__'].iteritems(): 
     196                styles.add(name, value) 
     197 
     198        self.styles = styles 
     199 
    152200 
    153201 
  • gaphor/branches/new-canvas/gaphor/diagram/diagramitem.py

    r1026 r1049  
    88from gaphor.misc import uniqueid 
    99from gaphor.UML import Element, Presentation 
     10from gaphor.diagram import DiagramItemMeta 
    1011 
    1112STEREOTYPE_OPEN  = '\xc2\xab' # '<<' 
     
    2930            ... 
    3031    """ 
     32 
     33    __metaclass__ = DiagramItemMeta 
    3134 
    3235    stereotype_list = [] 
  • gaphor/branches/new-canvas/gaphor/diagram/elementitem.py

    r972 r1049  
    88import gaphas 
    99from diagramitem import DiagramItem 
    10 from gaphor.diagram import DiagramItemMeta 
    1110 
    1211__version__ = '$Revision$' 
    1312 
    1413class ElementItem(gaphas.Element, DiagramItem): 
    15     __metaclass__ = DiagramItemMeta 
    16  
    1714    def __init__(self, id=None): 
    1815        gaphas.Element.__init__(self) 
  • gaphor/branches/new-canvas/setup.py

    r1044 r1049  
    2020    'gaphor.adapters.tests.test_connector', 
    2121    'gaphor.adapters.tests.test_editor', 
     22    'gaphor.diagram.tests.test_diagramitem', 
    2223    'gaphor.diagram.tests.test_class', 
    2324    'gaphor.diagram.tests.test_action',