mscGraphInstance.py 12.7 KB
Newer Older
Maxime Perrotin's avatar
Maxime Perrotin committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#******************************************************************************
#
# TASTE Msc Diagram Editor
# http://taste.tuxfamily.org/
#
#  This file is part of TASTE Msc Editor.
#
#  TASTE Msc Diagram Editor is free software: you can redistribute it and/or
#  modify it under the terms of the GNU General Public License as published
#  by the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  TASTE Msc Diagram Editor is distributed in the hope that it will be
#  useful,(but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with TASTE Msc Diagram Editor.  If not, see
#  <http://www.gnu.org/licenses/>.
#
#  Author: Angel Esquinas <aesquina@datsi.fi.upm.es>
#
#  Copyright (c) 2012 European Space Agency
#
#******************************************************************************
27
from PySide.QtGui import QPen, QGraphicsRectItem, QLinearGradient, QPainter
Maxime Perrotin's avatar
Maxime Perrotin committed
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
from PySide.QtCore import Qt, QPointF, Slot

import msccore

from mscgraphics import MscGraphItem
from mscgraphics import MscGraphTextItem


class MscGraphInstance(MscGraphItem):
    # Resize, Normal = range(2)
    StartHeadHeight = 30
    EndHeadHeight = 15
    MinHeadWidth = 120
    MinBody = 40
    MarginOffset = 5
    shapeGradient = QLinearGradient()
    shapeGradient.setCoordinateMode(QLinearGradient.ObjectBoundingMode)

    def __init__(self, x=0, y=0, data=None, label="", parent=None):
        u"""
        Create new MscGraphInstance with mscCore 'data' associated.
        Set the position to ('x', 'y')
        """
        super(MscGraphInstance, self).__init__(parent)

        self.flagFixAxisY = False
        self.setPos(x, y)

        self.initilizeGraphical()

        self.mode = self.Normal
        self.messages = []
        self.events = []
        self.initialMousePos = None

        if label == None:
            label = "Instance"
        # If no mscCore.MscInstance object is passed as argument
        # Create New object that contain the data
        if data == None:
            self.setMscData(msccore.MscInstance(name=label))
        else:
            self.setMscData(data)

        # Label Of Instance
        self.addLabel()
        self.addKindLabel()

        # The Item is movable
        self.setFlag(MscGraphInstance.ItemIsMovable, True)
        # Instance signal when selected
        self.setSignalWhenSelected(True)

        self.flagFixAxisY = True
        # Initialize MscGraphInstance Variables
        self.updateHead()
        self.updateEnd()
        self.updateDraw()

    #**************************************************************************
    # DATA FUNCTIONS
    #**************************************************************************
    @Slot()
    def _labelLostFocus(self):
        text = self.label.toPlainText()
        self.mscData().setName(text)

    @Slot()
    def readMscData(self):
        self.updateLabel()
        self.updateKindLabel()
        super(MscGraphInstance, self).readMscData()

    def delete(self):
        mes_copy = self.messages[:]
        for msg in mes_copy:
            msg.delete()

        for event in self.events:
            event.delete()

        self.mscData().delete()

    #**************************************************************************
    # Messages
    #**************************************************************************
    def addMessageEvent(self, message):
        yMes = self.mapFromParent(message.pos()).y()
        if yMes > self.getBodySize():
            self.setBodySize(yMes + self.Offset)
        self.messages.append(message)
        message.yHasChanged.connect(self.resizeBody)

    def removeMessageEvent(self, message):
        self.messages.remove(message)

    #**************************************************************************
    # Events
    #**************************************************************************
    def addEvent(self, mscEvent, inCore=False):
        u"""
        Add mscEvent as child event of instance.
        """
        mscEvent.setParentItem(self)
        mscEvent.setPos(self.upperLimit())
        self.events.append(mscEvent)
        mscEvent.yHasChanged.connect(self.resizeBody)
        if not inCore:
            self.mscData().addEvent(mscEvent.mscData())

    def deleteMscEvent(self, mscEvent):
        u"""
        Remove mscEvent from list of events.
        The event must be child of instance.
        """
        mscEvent.setParentItem(None)
        self.events.remove(mscEvent)
        self.mscData().delEvent(mscEvent.mscData())

    #**************************************************************************
    # Resize Body
    #**************************************************************************
    @Slot(MscGraphItem)
    def resizeBody(self, who=None):
        """
        Resize the body to 'who' position if it is greater than than the
        body of instance
        """
        yMes = self.mapFromScene(who.scenePos()).y()
        if yMes > (self.getBodySize() - self.Offset):
            self.setBodySize(yMes)

    #**************************************************************************
    # Comment Position
    #**************************************************************************
    def commentPosition(self):
        return QPointF(0, 0)

    #**************************************************************************
    # GRAPHICAL FUNCTIONS
    #**************************************************************************
    def initilizeGraphical(self):
        u""" Initialize graphical parameters of new instance"""
        # Lenght of the Instance Axis
        self.bodySize = 160
        self.head = QGraphicsRectItem(self)
        self.end = QGraphicsRectItem(self)
        # Set resizable
        self.setResizable(True)
        # Prepare the brush
        self.shapeGradient.setColorAt(0, Qt.white)
        self.shapeGradient.setColorAt(1, self.BaseColor)
        # Inilized Variables
        self.headHeight = self.StartHeadHeight
        self.headWidth = self.MinHeadWidth

    def setBodySize(self, newSize):
        u"""Set the body size.
        Check if any event or message are in axis position outside the new
        size. If it happens the new size of instance will be greater or equal.
        """
        lowLimit = self.MinBody
        for i in self.messages:
            y = self.mapFromParent(i.pos()).y()
            if y > lowLimit:
                lowLimit = y

        for e in self.events:
            y = self.mapFromItem(e, 0, 0).y()
            if y > lowLimit:
                lowLimit = y

        if newSize < lowLimit:
            self.bodySize = lowLimit + (2 * self.Offset)
        else:
            self.bodySize = newSize + (2 * self.Offset)
        self.updateDraw()
        self.itemResized.emit()

    def getBodySize(self):
        return self.bodySize

    def updateDraw(self):
        ''' Update the internal variables about size and position '''
        self.prepareGeometryChange()
        if self.headWidth < self.MinHeadWidth:
            self.headWidth = self.MinHeadWidth

        self.drawOriginX = - (self.headWidth / 2)
        self.drawOriginY = - (self.headHeight)

        self.height = (self.headHeight  # For HeadHeight
                       + self.bodySize
                       + self.EndHeadHeight)  # End
        self.width = self.headWidth

        self.updateHead()
        self.updateEnd()
        self.setResizeHandlersPositions()

    def updateHead(self):
        ''' Update the Head. Set the new Rect'''
        head = self.head
        headWidth = self.headWidth
        headHeight = self.StartHeadHeight

        head.setPen(self.pen)
        head.setBrush(self.shapeGradient)
        head.setRect(-(headWidth / 2), -headHeight,
                     headWidth, headHeight)

    def updateEnd(self):
        ''' Update de End. Set the new Rect'''
        end = self.end
        HeadWidth = self.headWidth
        HeadHeight = self.EndHeadHeight

        end.setPen(self.pen)
        end.setBrush(self.LineColor)
        # End height is half HeadHeight
        end.setRect(-(HeadWidth / 2), -HeadHeight,
                     HeadWidth, HeadHeight)
        end.setPos(0, self.bodySize + HeadHeight)

    def upperLimitInstance(self, sender=True):
        ''' Return Axis start coordinate in based of the parent '''
        return self.mapToParent(0, self.Offset)

    def bottomLimitInstance(self, sender=True):
        ''' Return the y coordinate in based of the parent '''
        return self.mapToParent(0, self.bodySize - self.Offset)

    def upperLimit(self, sender=True):
        u""" Return Axis y start in local coordinates """
        return QPointF(0, self.Offset)

    def bottomLimit(self, sender=True):
        u""" Return Axis y end in local coordinates """
        return QPointF(0, self.bodySize - self.Offset)

    #**************************************************************************
    # Event Area
    #**************************************************************************
    def setFixAxisY(self, value):
        self.flagFixAxisY = value

    #**************************************************************************
    # Item Change
    #**************************************************************************
    # If change position, repaint messages
    def itemChange(self, change, value):
        if change == MscGraphItem.ItemPositionChange:
            if self.flagFixAxisY:
                value.setY(self.pos().y())
#            for mess in self.messages:
#                mess.updatePosition()

        return MscGraphItem.itemChange(self, change, value)

    #**************************************************************************
    # Label Functions
    #**************************************************************************
    def addLabel(self):
#        labelValidator = QRegExpValidator(mscregexp.MscNameRegExp, self)
        self.label = MscGraphTextItem(self.mscData().name(), self)
#        self.label.setValidator(labelValidator)
        self.label.setEditable(True)
        self.label.setCenterPos(QPointF(0, -15))
        self.label.lostFocus.connect(self._labelLostFocus)

    def updateLabel(self):
        self.label.setPlainText(self.mscData().getName())
        self.label.setCenterPos(QPointF(0, -15))
        self.headWidth = (self.label.boundingRect().width()
                          + (2 * self.MarginOffset))
        self.updateDraw()

    def addKindLabel(self):
        self.kindLabel = MscGraphTextItem(parent=self)
        self.kindLabel.setEditable(False)
        self.updateKindLabel()

    def updateKindLabel(self):
        kind = self.mscData().instanceKind()
        if kind != None:
            self.kindLabel.setPlainText(u"{}".format(kind))
        else:
            self.kindLabel.setPlainText(u"")
        self.kindLabel.setCenterPos(QPointF(0, -45))

    #**************************************************************************
    # Paint Functions
    #**************************************************************************
    def paint(self, painter, option, widget=None):
        ''' Paint the instance Axis Symbol '''
        painter.setPen(QPen(self.pen).setColor(Qt.black))
324
        painter.setRenderHint(QPainter.Antialiasing, True)
Maxime Perrotin's avatar
Maxime Perrotin committed
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
        painter.drawLine(0, 0, 0, self.bodySize)

    #**************************************************************************
    # Resize Functions
    #**************************************************************************
    def resizeItem(self, watched, pos):
        ''' Resize Instance '''
        lowLimit = self.MinBody
        # Move in Axis Y
        if (watched.posNumber == self.BottomLeft or
            watched.posNumber == self.BottomRight):
            for i in self.messages:
                y = self.mapFromParent(i.pos()).y() + self.Offset
                if y > lowLimit:
                    lowLimit = y

            endBody = pos.y() - self.EndHeadHeight
            if endBody < lowLimit:
                self.bodySize = lowLimit
            else:
                self.bodySize = endBody

        x = pos.x()
        newWidth = abs(x * 2)

        self.headWidth = newWidth
        labelWidth = self.label.boundingRect().width()
        if self.headWidth < labelWidth:
            self.headWidth = labelWidth + (2 * self.Offset)

        #self.updateEnd()
        self.updateDraw()
        MscGraphItem.resizeItem(self, watched, pos)

    #**************************************************************************
    # Events Functions
    #**************************************************************************
    def mouseDoubleClickEvent(self, event):
        self.label.startEdition()
        event.accept()