eric6/Graphics/ModuleItem.py

changeset 6942
2602857055c5
parent 6645
ad476851d7e0
child 7229
53054eb5b15a
equal deleted inserted replaced
6941:f99d60d6b59b 6942:2602857055c5
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2007 - 2019 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing a module item.
8 """
9
10 from __future__ import unicode_literals
11
12 from PyQt5.QtGui import QFont
13 from PyQt5.QtWidgets import QGraphicsSimpleTextItem, QStyle
14
15 from .UMLItem import UMLModel, UMLItem
16
17
18 class ModuleModel(UMLModel):
19 """
20 Class implementing the module model.
21 """
22 def __init__(self, name, classlist=None):
23 """
24 Constructor
25
26 @param name the module name (string)
27 @param classlist list of class names (list of strings)
28 """
29 super(ModuleModel, self).__init__(name)
30
31 self.classlist = [] if classlist is None else classlist[:]
32
33 def addClass(self, classname):
34 """
35 Public method to add a class to the module model.
36
37 @param classname class name to be added (string)
38 """
39 self.classlist.append(classname)
40
41 def getClasses(self):
42 """
43 Public method to retrieve the classes of the module.
44
45 @return list of class names (list of strings)
46 """
47 return self.classlist[:]
48
49
50 class ModuleItem(UMLItem):
51 """
52 Class implementing a module item.
53 """
54 ItemType = "module"
55
56 def __init__(self, model=None, x=0, y=0, rounded=False,
57 parent=None, scene=None):
58 """
59 Constructor
60
61 @param model module model containing the module data (ModuleModel)
62 @param x x-coordinate (integer)
63 @param y y-coordinate (integer)
64 @keyparam rounded flag indicating a rounded corner (boolean)
65 @keyparam parent reference to the parent object (QGraphicsItem)
66 @keyparam scene reference to the scene object (QGraphicsScene)
67 """
68 UMLItem.__init__(self, model, x, y, rounded, parent)
69
70 scene.addItem(self)
71
72 if self.model:
73 self.__createTexts()
74 self.__calculateSize()
75
76 def __createTexts(self):
77 """
78 Private method to create the text items of the module item.
79 """
80 if self.model is None:
81 return
82
83 boldFont = QFont(self.font)
84 boldFont.setBold(True)
85
86 classes = self.model.getClasses()
87
88 x = self.margin + self.rect().x()
89 y = self.margin + self.rect().y()
90 self.header = QGraphicsSimpleTextItem(self)
91 self.header.setFont(boldFont)
92 self.header.setText(self.model.getName())
93 self.header.setPos(x, y)
94 y += self.header.boundingRect().height() + self.margin
95 if classes:
96 txt = "\n".join(classes)
97 else:
98 txt = " "
99 self.classes = QGraphicsSimpleTextItem(self)
100 self.classes.setFont(self.font)
101 self.classes.setText(txt)
102 self.classes.setPos(x, y)
103
104 def __calculateSize(self):
105 """
106 Private method to calculate the size of the module item.
107 """
108 if self.model is None:
109 return
110
111 width = self.header.boundingRect().width()
112 height = self.header.boundingRect().height()
113 if self.classes:
114 width = max(width, self.classes.boundingRect().width())
115 height = height + self.classes.boundingRect().height()
116 self.setSize(width + 2 * self.margin, height + 2 * self.margin)
117
118 def setModel(self, model):
119 """
120 Public method to set the module model.
121
122 @param model module model containing the module data (ModuleModel)
123 """
124 self.scene().removeItem(self.header)
125 self.header = None
126 if self.classes:
127 self.scene().removeItem(self.classes)
128 self.meths = None
129 self.model = model
130 self.__createTexts()
131 self.__calculateSize()
132
133 def paint(self, painter, option, widget=None):
134 """
135 Public method to paint the item in local coordinates.
136
137 @param painter reference to the painter object (QPainter)
138 @param option style options (QStyleOptionGraphicsItem)
139 @param widget optional reference to the widget painted on (QWidget)
140 """
141 pen = self.pen()
142 if (option.state & QStyle.State_Selected) == \
143 QStyle.State(QStyle.State_Selected):
144 pen.setWidth(2)
145 else:
146 pen.setWidth(1)
147
148 painter.setPen(pen)
149 painter.setBrush(self.brush())
150 painter.setFont(self.font)
151
152 offsetX = self.rect().x()
153 offsetY = self.rect().y()
154 w = self.rect().width()
155 h = self.rect().height()
156
157 painter.drawRect(offsetX, offsetY, w, h)
158 y = self.margin + self.header.boundingRect().height()
159 painter.drawLine(offsetX, offsetY + y, offsetX + w - 1, offsetY + y)
160
161 self.adjustAssociations()
162
163 def buildItemDataString(self):
164 """
165 Public method to build a string to persist the specific item data.
166
167 This string must start with ", " and should be built like
168 "attribute=value" with pairs separated by ", ". value must not
169 contain ", " or newlines.
170
171 @return persistence data (string)
172 """
173 entries = [
174 "name={0}".format(self.model.getName()),
175 ]
176 classes = self.model.getClasses()
177 if classes:
178 entries.append("classes={0}".format("||".join(classes)))
179
180 return ", " + ", ".join(entries)
181
182 def parseItemDataString(self, version, data):
183 """
184 Public method to parse the given persistence data.
185
186 @param version version of the data (string)
187 @param data persisted data to be parsed (string)
188 @return flag indicating success (boolean)
189 """
190 parts = data.split(", ")
191 if len(parts) < 1:
192 return False
193
194 name = ""
195 classes = []
196
197 for part in parts:
198 key, value = part.split("=", 1)
199 if key == "name":
200 name = value.strip()
201 elif key == "classes":
202 classes = value.strip().split("||")
203 else:
204 return False
205
206 self.model = ModuleModel(name, classes)
207 self.__createTexts()
208 self.__calculateSize()
209
210 return True

eric ide

mercurial