Changeset 1059

Show
Ignore:
Timestamp:
11/02/06 20:30:28 (2 years ago)
Author:
wrobell
Message:

- calculate aligned text position properly (0, 0 point of text is in

left, bottom corner not left, top one)

- style classes, functions and constants moved to gaphor.diagram.style

package and more styles tests added

- already modified named items code cleanup after above changes

Files:

Legend:

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

    r1053 r1059  
    1313 
    1414from gaphor.misc import uniqueid 
    15 from gaphor.diagram.align import ItemAlign 
     15from gaphor.diagram.style import Style 
    1616 
    1717# Map UML elements to their (default) representation. 
     
    3131    global _uml_to_item_map 
    3232    _uml_to_item_map[element] = item 
    33  
    34  
    35  
    36 class Style(object): 
    37     """ 
    38     Item style information. Style information is provided through object's 
    39     attributes, i.e.:: 
    40  
    41         class InitialNodeItem 
    42             __style__ = { 
    43                 'name-align': ('center', 'top'), 
    44             } 
    45  
    46     is translated to:: 
    47  
    48         >>> print style.name_align 
    49         ('center', 'top') 
    50     """ 
    51     def add(self, name, value): 
    52         """ 
    53         Add style variable. 
    54  
    55         Variable name can contain hyphens, which is converted to 
    56         underscode, i.e. 'name-align' -> 'name_align'. 
    57  
    58         @param name:  style variable name 
    59         @param value: style variable value 
    60         """ 
    61         name = name.replace('-', '_') 
    62         setattr(self, name, value) 
    63  
    64  
    65     def items(self): 
    66         """ 
    67         Return iterator of (name, value) style information items. 
    68         """ 
    69         return self.__dict__.iteritems() 
    7033 
    7134 
  • gaphor/branches/new-canvas/gaphor/diagram/action.py

    r1058 r1059  
    66 
    77from gaphor import UML 
    8 from gaphor.diagram.nameditem import NamedItem, ALIGN_CENTER, ALIGN_MIDDLE 
     8from gaphor.diagram.nameditem import NamedItem 
     9from gaphor.diagram.style import ALIGN_CENTER, ALIGN_MIDDLE 
    910from gaphas.util import text_align, text_extents 
    1011 
  • gaphor/branches/new-canvas/gaphor/diagram/activitynodes.py

    r1046 r1059  
    1414#from gaphor.diagram.groupable import GroupBase 
    1515from gaphor.diagram.nameditem import NamedItem 
    16 from gaphor.diagram.align import H_ALIGN_LEFT, H_ALIGN_RIGHT, V_ALIGN_BOTTOM 
    1716 
    1817 
     
    151150 
    152151    __uml__   = UML.DecisionNode 
    153     __s_align__ = H_ALIGN_LEFT 
     152#__s_align__ = H_ALIGN_LEFT 
    154153 
    155154    RADIUS = 15 
     
    182181    """ 
    183182    __uml__      = UML.ForkNode 
    184     __s_valign__ = V_ALIGN_BOTTOM 
     183#__s_valign__ = V_ALIGN_BOTTOM 
    185184 
    186185    WIDTH  =  6.0 
  • gaphor/branches/new-canvas/gaphor/diagram/interface.py

    r1011 r1059  
    66from gaphas.item import NW, SE 
    77from gaphor import UML 
    8 from gaphor.diagram.align import V_ALIGN_BOTTOM 
    98from gaphor.diagram.dependency import DependencyItem 
    109from gaphor.diagram.implementation import ImplementationItem 
  • gaphor/branches/new-canvas/gaphor/diagram/nameditem.py

    r1058 r1059  
    44""" 
    55 
    6 # padding 
    7 PADDING_TOP, PADDING_RIGHT, PADDING_BOTTOM, PADDING_LEFT = range(4) 
    8 # horizontal align 
    9 ALIGN_LEFT, ALIGN_CENTER, ALIGN_RIGHT = range(3) 
    10 # vertical align 
    11 ALIGN_TOP, ALIGN_MIDDLE, ALIGN_BOTTOM = range(3) 
    12  
     6from gaphas.util import text_extents 
    137 
    148from gaphor.diagram.elementitem import ElementItem 
    15 from gaphas.util import text_align, text_extents 
     9from gaphor.diagram.style import get_min_size, get_text_point, \ 
     10        ALIGN_CENTER, ALIGN_TOP 
    1611 
    1712 
     
    4439        self.width      = self.min_width 
    4540        self.height     = self.min_height 
     41        self.name_x     = 0 
     42        self.name_y     = 0 
    4643 
    4744 
    4845    def pre_update(self, context): 
     46        """ 
     47        Calculate minimal size of named item. 
     48        """ 
     49        cr = context.cairo 
     50        text = self.subject.name 
     51        if text: 
     52            width, height = text_extents(cr, text) 
     53            self.min_width, self.min_height = get_min_size(width, height, 
     54                    self.style.name_padding) 
     55        super(NamedItem, self).pre_update(context) 
     56 
     57 
     58    def update(self, context): 
    4959        """ 
    5060        Calculate position of item's name. 
     
    5464        if text: 
    5565            width, height = text_extents(cr, text) 
    56             self.min_width, self.min_height = \ 
    57                 get_min_size(width, height, self.style.name_padding) 
    58         super(NamedItem, self).pre_update(context) 
     66            self.name_x, self.name_y = get_text_point(text_extents(cr, text), 
     67                    self.width, self.height, 
     68                    self.style.name_align, self.style.name_padding, 
     69                    self.style.name_outside) 
     70        super(NamedItem, self).update(context) 
    5971 
    6072 
     
    6779        text = self.subject.name 
    6880        if text: 
    69             x, y = get_pos(cr, text, self.width, self.height, 
    70                     self.style.name_align, self.style.name_padding, 
    71                     self.style.name_outside) 
    72             cr.move_to(x, y) 
     81            cr.move_to(self.name_x, self.name_y) 
    7382            cr.show_text(text) 
    7483        super(NamedItem, self).draw(context) 
    75  
    76  
    77          
    78  
    79 def get_min_size(width, height, padding): 
    80     """ 
    81     Get minimum size of an object using padding information. 
    82  
    83     @param width: object width 
    84     @param height: object height 
    85     @param padding: padding information as a tuple 
    86         (top, right, bottom, left) 
    87  
    88     fixme: move this method outside the class some utility function to 
    89         other package? 
    90     """ 
    91     return width + padding[PADDING_LEFT] + padding[PADDING_RIGHT], \ 
    92         height + padding[PADDING_TOP] + padding[PADDING_BOTTOM] 
    93  
    94  
    95 def get_pos(cr, text, width, height, align, padding, outside): 
    96     """ 
    97     Calculate position of the text relative to containing box defined by 
    98     tuple (0, 0, width, height).  Text is aligned using align and padding 
    99     information. It can be also placed outside the box if @C{outside} 
    100     parameter is set to @C{True}. 
    101  
    102     @param width:   width of the containing box 
    103     @param height:  height of the containing box 
    104     @param align:   text align information (center, top, etc.) 
    105     @param padding: text padding 
    106     @param outside: should text be put outside containing box 
    107  
    108     fixme: move this method outside the class some utility function to 
    109         other package? 
    110     """ 
    111     assert text 
    112  
    113     x_bear, y_bear, w, h, x_adv, y_adv = cr.text_extents(text) 
    114  
    115     halign, valign = align 
    116  
    117     if outside: 
    118         if halign == ALIGN_LEFT: 
    119             x = -w - padding[PADDING_LEFT] 
    120         elif halign == ALIGN_CENTER: 
    121             x = (width - w) / 2 
    122         elif halign == ALIGN_RIGHT: 
    123             x = width + padding[PADDING_RIGHT] 
    124         else: 
    125             assert False 
    126  
    127         if valign == ALIGN_TOP: 
    128             y = -h - padding[PADDING_TOP] 
    129         elif valign == ALIGN_MIDDLE: 
    130             y = (height - h) / 2 
    131         elif valign == ALIGN_BOTTOM: 
    132             y = height + padding[PADDING_BOTTOM] 
    133         else: 
    134             assert False 
    135  
    136     else: 
    137         if halign == ALIGN_LEFT: 
    138             x = padding[PADDING_LEFT] 
    139         elif halign == ALIGN_CENTER: 
    140             x = (width - w) / 2 
    141         elif halign == ALIGN_RIGHT: 
    142             x = width - w - padding[PADDING_RIGHT] 
    143         else: 
    144             assert False 
    145  
    146         if valign == ALIGN_TOP: 
    147             y = h + padding[PADDING_TOP] 
    148         elif valign == ALIGN_MIDDLE: 
    149             y = (height + h) / 2 
    150         elif valign == ALIGN_BOTTOM: 
    151             y = height - h - padding[PADDING_BOTTOM] 
    152         else: 
    153             assert False 
    154     return x, y 
    155  
    156  
    157 # maybe useful for align routines, we will see 
    158 ###A###    def update_name(self, affine): 
    159 ###A###        def set_st_pos(text, x, y, width, height): 
    160 ###A###            text.set_pos((x, y)) 
    161 ###A###            text.set_max_width(width) 
    162 ###A###            text.set_max_height(height) 
    163 ###A### 
    164 ###A###        nalign = self.n_align 
    165 ###A###        salign = self.s_align 
    166 ###A### 
    167 ###A###        if self._has_stereotype: 
    168 ###A###            sw, sh = self.get_text_size(self._stereotype) 
    169 ###A###            nw, nh = self.get_text_size(self._name) 
    170 ###A### 
    171 ###A###            width = max(sw, nw) 
    172 ###A###            height = sh + nh 
    173 ###A### 
    174 ###A###            # set stereotype position 
    175 ###A###            sx, sy = salign.get_pos(self._stereotype, width, height, self.width, self.height) 
    176 ###A###            set_st_pos(self._stereotype, sx, sy, sw, sh) 
    177 ###A### 
    178 ###A###            # place name below stereotype 
    179 ###A###            nx = sx + (sw - nw) / 2.0 
    180 ###A###            ny = sy + sh 
    181 ###A###            set_st_pos(self._name, nx, ny, nw, nh) 
    182 ###A### 
    183 ###A###            # determine position and size of stereotype and name placed 
    184 ###A###            # together 
    185 ###A###            x = min(sx, nx) 
    186 ###A###            y = sy 
    187 ###A### 
    188 ###A###            align = salign 
    189 ###A###        else: 
    190 ###A###            width, height = self.get_text_size(self._name) 
    191 ###A###            x, y = nalign.get_pos(self._name, width, height, self.width, self.height) 
    192 ###A###            set_st_pos(self._name, x, y, width, height) 
    193 ###A### 
    194 ###A###            align = nalign 
    195 ###A### 
    196 ###A###        if not align.outside: 
    197 ###A###            min_width, min_height = align.get_min_size(width, height) 
    198 ###A###            self.set(min_width = min_width, min_height = min_height) 
    199 ###A### 
    200 ###A###        return align, x, y, width, height 
    201 ###A### 
    202 ###A###    def on_update(self, affine): 
    203 ###A###        align, x, y, width, height = self.update_name(affine) 
    204 ###A### 
    205 ###A###        ElementItem.on_update(self, affine) 
    206 ###A### 
    207 ###A###        if align.outside: 
    208 ###A###            wx, hy = x + width, y + height 
    209 ###A###            self.set_bounds((min(0, x), min(0, y), 
    210 ###A###                max(self.width, wx), max(self.height, hy))) 
    211 ###A### 
    212 ###A###        self.draw_border() 
    213 ###A###        self.expand_bounds(1.0) 
    214  
    215  
    216 # vim:sw=4:et 
  • gaphor/branches/new-canvas/gaphor/diagram/package.py

    r1058 r1059  
    1515    } 
    1616    __style__ = { 
    17         'name-padding': (35, 10, 5, 10), 
     17        'name-padding': (25, 10, 5, 10), 
    1818    } 
    1919 
    2020    TAB_X = 50 
    2121    TAB_Y = 20 
    22  
    23     def __init__(self, id): 
    24         NamedItem.__init__(self, id, 120, 60) 
    25  
    2622 
    2723    def pre_update(self, context): 
  • gaphor/branches/new-canvas/gaphor/diagram/usecase.py

    r1058 r1059  
    77from gaphor import UML 
    88from gaphor.diagram.classifier import ClassifierItem 
    9 from gaphor.diagram.nameditem import ALIGN_CENTER, ALIGN_MIDDLE 
     9from gaphor.diagram.style import ALIGN_CENTER, ALIGN_MIDDLE 
    1010from gaphas.util import text_align, text_extents, path_ellipse 
    1111 
  • gaphor/branches/new-canvas/setup.py

    r1049 r1059  
    2525    'gaphor.diagram.tests.test_handletool', 
    2626    'gaphor.diagram.tests.test_interfaces', 
     27    'gaphor.diagram.tests.test_style', 
    2728    'gaphor.ui.tests.test_diagramtab', 
    2829    'gaphor.ui.tests.test_mainwindow',