| 1 |
""" |
|---|
| 2 |
The diagram package contains items (to be drawn on the diagram), tools |
|---|
| 3 |
(used for interacting with the diagram) and interfaces (used for adapting the |
|---|
| 4 |
diagram). |
|---|
| 5 |
""" |
|---|
| 6 |
|
|---|
| 7 |
__version__ = '$revision$' |
|---|
| 8 |
__author__ = 'Arjan J. Molenaar' |
|---|
| 9 |
__date__ = '$date$' |
|---|
| 10 |
|
|---|
| 11 |
import inspect |
|---|
| 12 |
import gobject |
|---|
| 13 |
|
|---|
| 14 |
from gaphor.misc import uniqueid |
|---|
| 15 |
from gaphor.diagram.style import Style |
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
_uml_to_item_map = { } |
|---|
| 19 |
|
|---|
| 20 |
def create(type): |
|---|
| 21 |
return create_as(type, uniqueid.generate_id()) |
|---|
| 22 |
|
|---|
| 23 |
def create_as(type, id): |
|---|
| 24 |
return type(id) |
|---|
| 25 |
|
|---|
| 26 |
def get_diagram_item(element): |
|---|
| 27 |
global _uml_to_item_map |
|---|
| 28 |
return _uml_to_item_map.get(element) |
|---|
| 29 |
|
|---|
| 30 |
def set_diagram_item(element, item): |
|---|
| 31 |
global _uml_to_item_map |
|---|
| 32 |
_uml_to_item_map[element] = item |
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
class DiagramItemMeta(type): |
|---|
| 36 |
""" |
|---|
| 37 |
Initialize a new diagram item. |
|---|
| 38 |
1. Register UML.Elements by means of the __uml__ attribute (see |
|---|
| 39 |
map_uml_class method). |
|---|
| 40 |
2. Set items style information. |
|---|
| 41 |
|
|---|
| 42 |
@ivar style: style information |
|---|
| 43 |
""" |
|---|
| 44 |
|
|---|
| 45 |
def __init__(self, name, bases, data): |
|---|
| 46 |
type.__init__(self, name, bases, data) |
|---|
| 47 |
|
|---|
| 48 |
self.map_uml_class(data) |
|---|
| 49 |
self.set_style(data) |
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
def map_uml_class(self, data): |
|---|
| 53 |
""" |
|---|
| 54 |
Map UML class to diagram item. |
|---|
| 55 |
|
|---|
| 56 |
@param cls: new instance of item class |
|---|
| 57 |
@param data: metaclass data with UML class information |
|---|
| 58 |
|
|---|
| 59 |
""" |
|---|
| 60 |
if '__uml__' in data: |
|---|
| 61 |
obj = data['__uml__'] |
|---|
| 62 |
if isinstance(obj, (tuple, set, list)): |
|---|
| 63 |
for c in obj: |
|---|
| 64 |
set_diagram_item(c, self) |
|---|
| 65 |
else: |
|---|
| 66 |
set_diagram_item(obj, self) |
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
def set_style(self, data): |
|---|
| 70 |
""" |
|---|
| 71 |
Set item style information by merging provided information with |
|---|
| 72 |
style information from base classes. |
|---|
| 73 |
|
|---|
| 74 |
@param cls: new instance of diagram item class |
|---|
| 75 |
@param bases: base classes of an item |
|---|
| 76 |
@param data: metaclass data with style information |
|---|
| 77 |
""" |
|---|
| 78 |
style = Style() |
|---|
| 79 |
for c in self.__bases__: |
|---|
| 80 |
if hasattr(c, 'style'): |
|---|
| 81 |
for (name, value) in c.style.items(): |
|---|
| 82 |
style.add(name, value) |
|---|
| 83 |
|
|---|
| 84 |
if '__style__' in data: |
|---|
| 85 |
for (name, value) in data['__style__'].iteritems(): |
|---|
| 86 |
style.add(name, value) |
|---|
| 87 |
|
|---|
| 88 |
self.style = style |
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 |
|
|---|