|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2007 - 2009 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a module item. |
|
8 """ |
|
9 |
|
10 from PyQt4.QtCore import * |
|
11 from PyQt4.QtGui import * |
|
12 |
|
13 from UMLItem import UMLItem |
|
14 |
|
15 class ModuleModel(object): |
|
16 """ |
|
17 Class implementing the module model. |
|
18 """ |
|
19 def __init__(self, name, classlist=[]): |
|
20 """ |
|
21 Constructor |
|
22 |
|
23 @param name the module name (string) |
|
24 @param classlist list of class names (list of strings) |
|
25 """ |
|
26 self.name = name |
|
27 self.classlist = classlist |
|
28 |
|
29 def addClass(self, classname): |
|
30 """ |
|
31 Method to add a class to the module model. |
|
32 |
|
33 @param classname class name to be added (string) |
|
34 """ |
|
35 self.classlist.append(classname) |
|
36 |
|
37 def getClasses(self): |
|
38 """ |
|
39 Method to retrieve the classes of the module. |
|
40 |
|
41 @return list of class names (list of strings) |
|
42 """ |
|
43 return self.classlist[:] |
|
44 |
|
45 def getName(self): |
|
46 """ |
|
47 Method to retrieve the module name. |
|
48 |
|
49 @return module name (string) |
|
50 """ |
|
51 return self.name |
|
52 |
|
53 class ModuleItem(UMLItem): |
|
54 """ |
|
55 Class implementing a module item. |
|
56 """ |
|
57 def __init__(self, model = None, x = 0, y = 0, rounded = False, |
|
58 parent = None, scene = None): |
|
59 """ |
|
60 Constructor |
|
61 |
|
62 @param model module model containing the module data (ModuleModel) |
|
63 @param x x-coordinate (integer) |
|
64 @param y y-coordinate (integer) |
|
65 @keyparam rounded flag indicating a rounded corner (boolean) |
|
66 @keyparam parent reference to the parent object (QGraphicsItem) |
|
67 @keyparam scene reference to the scene object (QGraphicsScene) |
|
68 """ |
|
69 UMLItem.__init__(self, x, y, rounded, parent) |
|
70 self.model = model |
|
71 |
|
72 scene.addItem(self) |
|
73 |
|
74 if self.model: |
|
75 self.__createTexts() |
|
76 self.__calculateSize() |
|
77 |
|
78 def __createTexts(self): |
|
79 """ |
|
80 Private method to create the text items of the module item. |
|
81 """ |
|
82 if self.model is None: |
|
83 return |
|
84 |
|
85 boldFont = QFont(self.font) |
|
86 boldFont.setBold(True) |
|
87 |
|
88 classes = self.model.getClasses() |
|
89 |
|
90 x = self.margin + self.rect().x() |
|
91 y = self.margin + self.rect().y() |
|
92 self.header = QGraphicsSimpleTextItem(self) |
|
93 self.header.setFont(boldFont) |
|
94 self.header.setText(self.model.getName()) |
|
95 self.header.setPos(x, y) |
|
96 y += self.header.boundingRect().height() + self.margin |
|
97 if classes: |
|
98 txt = "\n".join(classes) |
|
99 else: |
|
100 txt = " " |
|
101 self.classes = QGraphicsSimpleTextItem(self) |
|
102 self.classes.setFont(self.font) |
|
103 self.classes.setText(txt) |
|
104 self.classes.setPos(x, y) |
|
105 |
|
106 def __calculateSize(self): |
|
107 """ |
|
108 Private method to calculate the size of the module item. |
|
109 """ |
|
110 if self.model is None: |
|
111 return |
|
112 |
|
113 width = self.header.boundingRect().width() |
|
114 height = self.header.boundingRect().height() |
|
115 if self.classes: |
|
116 width = max(width, self.classes.boundingRect().width()) |
|
117 height = height + self.classes.boundingRect().height() |
|
118 self.setSize(width + 2 * self.margin, height + 2 * self.margin) |
|
119 |
|
120 def setModel(self, model): |
|
121 """ |
|
122 Method to set the module model. |
|
123 |
|
124 @param model module model containing the module data (ModuleModel) |
|
125 """ |
|
126 self.scene().removeItem(self.header) |
|
127 self.header = None |
|
128 if self.classes: |
|
129 self.scene().removeItem(self.classes) |
|
130 self.meths = None |
|
131 self.model = model |
|
132 self.__createTexts() |
|
133 self.__calculateSize() |
|
134 |
|
135 def paint(self, painter, option, widget = None): |
|
136 """ |
|
137 Public method to paint the item in local coordinates. |
|
138 |
|
139 @param painter reference to the painter object (QPainter) |
|
140 @param option style options (QStyleOptionGraphicsItem) |
|
141 @param widget optional reference to the widget painted on (QWidget) |
|
142 """ |
|
143 pen = self.pen() |
|
144 if (option.state & QStyle.State_Selected) == QStyle.State(QStyle.State_Selected): |
|
145 pen.setWidth(2) |
|
146 else: |
|
147 pen.setWidth(1) |
|
148 |
|
149 painter.setPen(pen) |
|
150 painter.setBrush(self.brush()) |
|
151 painter.setFont(self.font) |
|
152 |
|
153 offsetX = self.rect().x() |
|
154 offsetY = self.rect().y() |
|
155 w = self.rect().width() |
|
156 h = self.rect().height() |
|
157 |
|
158 painter.drawRect(offsetX, offsetY, w, h) |
|
159 y = self.margin + self.header.boundingRect().height() |
|
160 painter.drawLine(offsetX, offsetY + y, offsetX + w - 1, offsetY + y) |
|
161 |
|
162 self.adjustAssociations() |