mscGraphBasicMSCScene.py 13.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
#******************************************************************************
#
# 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/>.
#
22
23
#  Original Author: Angel Esquinas <aesquina@datsi.fi.upm.es>
#  Maintainer: Maxime Perrotin <maxime.perrotin@esa.int>
Maxime Perrotin's avatar
Maxime Perrotin committed
24
#
25
#  Copyright (c) 2012-2015 European Space Agency
Maxime Perrotin's avatar
Maxime Perrotin committed
26
27
28
29
30
31
32
33
34
35
36
37
#
#******************************************************************************
import logging

from PySide import QtGui
from PySide.QtCore import Qt, QLineF, QPointF, Signal, Slot
from PySide.QtGui import QGraphicsScene, QGraphicsLineItem, QPen
from PySide.QtGui import QAction
from PySide.QtGui import QMenu

from mscGraphInstance import MscGraphInstance
from mscGraphTimer import MscGraphTimer
38
from mscGraphCondition import MscGraphCondition
Maxime Perrotin's avatar
Maxime Perrotin committed
39
from mscGraphProcedure import MscGraphProcedure
Maxime Perrotin's avatar
Maxime Perrotin committed
40
41
42
43
44
45
46
47
48
49
50
from mscGraphBMSC import MscGraphBMSC
from mscGraphMessage import MscGraphMessage
from mscgraphics import MscGraphComment
from mscgraphics import MscGraphItem

logger = logging.getLogger(__name__)


class MscGraphBasicMSCScene(QGraphicsScene):
    # Mode of the scene
    (MoveItem, InsertInstance, InsertMessage, InsertTimer,
51
     InsertComment, InsertCondition) = range(6)
Maxime Perrotin's avatar
Maxime Perrotin committed
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

    # Signals
    itemInserted = Signal(MscGraphItem)

    itemSelected = Signal(MscGraphItem)

    customContextMenuRequested = Signal(QGraphicsScene, QPointF)

    nameChanged = Signal(str)

    def __init__(self, title=None, graphicMSC=None, parent=None):

        super(MscGraphBasicMSCScene, self).__init__(parent)
        #Initialize variables
        self._sceneMode = self.MoveItem
        self._bmsc = None
        self.line = None
        self.color = Qt.black
        self.setCustomContextMenuPolicy(False)

        self.createActions()

        self.timerCount = 1
        self.elementCount = 1

        if graphicMSC == None:
            graphicMSC = MscGraphBMSC(title=title)
        self.setBMscItem(graphicMSC)

    # Mode
    @Slot(int)
    def setMode(self, mode):
        self._sceneMode = mode

    def setItemType(self, itemType):
        self._itemType = itemType

    def getName(self):
        'Return the name of the scene, It is the same of the bmsc inside'
        return self._bmsc.getName()

    #**************************************************************************
    # Add Items Functions
    #**************************************************************************
    def setBMscItem(self, msc):
        # Change for ItemCreated
        msc.nameChanged.connect(self.nameChanged)
        self.addItem(msc)
        self._bmsc = msc
        self._bmsc.setParent(self)
        msc.itemSelected.connect(self.itemSelected)
        #Emit signal
        self.itemInserted.emit(msc)

    def bmsc(self):
        u""" Return the graphic bmsc object associated with the scene """
        return self._bmsc

    #------------- Instances
    @Slot(float, float, str, result=MscGraphInstance)
    def addInstance(self, x, y=0, label=None, inst=None):
        new = True

        if label == None and inst == None:
            label = u"Inst_{}".format(self.elementCount)
            self.elementCount += 1

        # we disable the ordered events in instance for stream mode
        if inst == None:
            instance = MscGraphInstance(label=label)
        else:
            instance = inst
            new = False

        # Add the instance to graphical diagram
        self._bmsc.addInstance(instance, x=x, new=new)

        # Connect Signal
        instance.itemSelected.connect(self.itemSelected)
        #Emit signal
        self.itemInserted.emit(instance)
        return instance

    def removeInstance(self, instance):
        u""" Remove a Instance from Scene """
137
        self.bmsc().removeInstance(instance)
Maxime Perrotin's avatar
Maxime Perrotin committed
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
        self.removeItem(instance)
        instance.delete()

    #-------------- Timers
    def addTimer(self, instance, pos=None, label=None):
        u"""
        Create new Timer with label and timer to instance
        """

        if label == None:
            label = u"Timer_{}".format(self.timerCount)
            self.timerCount += 1

        timer = MscGraphTimer(label=label, typeTimer=self._itemType)
        instance.addEvent(timer)
        if pos != None:
            timer.setPos(pos)

        timer.itemSelected.connect(self.itemSelected)
        self.itemInserted.emit(timer)
        return timer

    def addTimer2(self, instance, timer, pos=None):
        instance.addEvent(timer, inCore=True)
        if pos != None:
            timer.setPos(pos)

        timer.itemSelected.connect(self.itemSelected)
        self.itemInserted.emit(timer)
        return timer

    def removeTimer(self, timer):
        timer.delete()
        self.removeItem(timer)

    #-------------- Messages
    @Slot(MscGraphInstance, MscGraphInstance, float, str)
    def addMessage(self, start, end, y, label=None, msg=None):
        new = True
        if label == None:
            label = u"Msg_{}".format(self.elementCount)
            self.elementCount += 1

        if msg == None:
            message = MscGraphMessage(startInstance=start,
                                  endInstance=end, y=y, label=label)
        else:
            message = msg
            new = False

        self._bmsc.addMessage(message, new=new)
        message.itemSelected.connect(self.itemSelected)
        self.itemInserted.emit(message)
        return message

    def addMessage2(self, msg):
        message = msg
        self._bmsc.addMessage(message, new=False)
        message.itemSelected.connect(self.itemSelected)
        self.itemInserted.emit(message)
        return message

    def removeMessage(self, message):
        u"""Remove a Message from Scene"""
        self.bmsc().removeMessage(message)
        if message.scene():
            self.removeItem(message)
        message.delete()

    #--------------- Comments
    def addComment(self, item=None, pos=None):
        u"""
210
            Add new "comment" to the scene and position it in (optional) "pos"
Maxime Perrotin's avatar
Maxime Perrotin committed
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
        """
        if item != None and item.comment() != None:
            return

        comment = MscGraphComment(itemGraph=item, parent=self.bmsc())
        if pos != None:
            if item != None:
                comment.setPos(item.mapFromScene(pos))
            else:
                comment.setPos(pos)
        comment.itemSelected.connect(self.itemSelected)
        self.itemInserted.emit(comment)
        return comment

    def removeComment(self, comment):
        comment.delete()
        self.removeItem(comment)

229
    #--------------- Conditions
230
    def addCondition(self, instance, y=None, label=''):
231
        u"""
232
        Add new "condition" to the scene and position it in y pos if any
233
        """
234
235
        condition = MscGraphCondition(label=label, y=y,
                                      parent=self.bmsc(), instance=instance)
236
        instance.addEvent(condition)
237
238
        if y is not None:
            condition.setY(instance.mapFromScene(0, y).y())
239
240
241
242
243
244
        condition.itemSelected.connect(self.itemSelected)
        self.itemInserted.emit(condition)
        return condition

    def removeCondition(self, condition):
        condition.delete()
245
246
        if condition.scene():
            self.removeItem(condition)
247
248
249
250
251
252
253
254
255
256
257
258
259

    #---------------  Local Procedure Call
    def addProcedure(self, instance, y=None, label=''):
        u"""
        Add new "Procedure call " to the scene and position it in y pos if any
        """
        procedure = MscGraphProcedure(label=label, y=y,
                                      parent=self.bmsc(), instance=instance)
        instance.addEvent(procedure)
        if y is not None:
            procedure.setY(instance.mapFromScene(0, y).y())
        procedure.itemSelected.connect(self.itemSelected)
        self.itemInserted.emit(procedure)
Maxime Perrotin's avatar
Maxime Perrotin committed
260
        return procedure
261
262
263
264
265
266

    def removeProcedure(self, procedure):
        procedure.delete()
        if procedure.scene():
            self.removeItem(procedure)

Maxime Perrotin's avatar
Maxime Perrotin committed
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
324
325
326
327
328
329
330
331
    #**************************************************************************
    # Actions
    #**************************************************************************
    def createActions(self):
        icon1 = QtGui.QIcon()
        icon1.addPixmap(QtGui.QPixmap(":/actions/delete.svg"),
                        QtGui.QIcon.Normal, QtGui.QIcon.Off)

        self.deleteAction = QAction("Delete", self)
        self.deleteAction.setShortcut(QtGui.QKeySequence.Delete)
        self.deleteAction.setIcon(icon1)

    #**************************************************************************
    # Events
    #**************************************************************************
    # Context Menu Event
    def setCustomContextMenuPolicy(self, value=True):
        self.customContextMenu = value

    def contextMenuEvent(self, contextMenuEvent):
        logger.debug("ContextMenuEvent at position: %s",
                     contextMenuEvent.scenePos())
        if self.customContextMenu:
            self.customContextMenuRequested.emit(self,
                                                 contextMenuEvent.scenePos())
            contextMenuEvent.accept()
        else:
            actions = []

            items = self.items(contextMenuEvent.scenePos())
            if len > 0:
                for i in items:
                    if isinstance(i, MscGraphItem):
                        actions.append(self.deleteAction)

            QMenu.exec_(actions, contextMenuEvent.screenPos())

    # Mouse Events
    def mousePressEvent(self, mouseEvent):
        # If not are the Left Button return
        if (mouseEvent.button() != Qt.LeftButton):
            return

        # Click to add message
        if self._sceneMode == self.InsertMessage:

            self.line = QGraphicsLineItem(QLineF(mouseEvent.scenePos(),
                                         mouseEvent.scenePos()))
            self.line.setPen(QPen(self.color, 1))
            self.addItem(self.line)

        # Click to add an Instance
        elif self._sceneMode == self.InsertInstance:
            self.addInstance(mouseEvent.scenePos().x(),
                             mouseEvent.scenePos().y())

        # Click to add a Timer
        elif self._sceneMode == self.InsertTimer:
            items = self.items(mouseEvent.scenePos())
            if len(items) > 0:
                instance = items[0]
                if isinstance(instance, MscGraphInstance):
                    self.addTimer(instance,
                                  instance.mapFromScene(mouseEvent.scenePos()))

332
333
334
335
336
337
338
339
340
        # Click to add a Condition
        elif self._sceneMode == self.InsertCondition:
            items = self.items(mouseEvent.scenePos())
            if len(items) > 0:
                instance = items[0]
                if isinstance(instance, MscGraphInstance):
                    self.addCondition(instance,
                                  instance.mapFromScene(mouseEvent.scenePos()))

Maxime Perrotin's avatar
Maxime Perrotin committed
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
        # Click to add Comment
        elif self._sceneMode == self.InsertComment:
            items = self.items(mouseEvent.scenePos())
            commented = self.firstMscGraphItem(items)
            if commented != None:
                self.addComment(item=commented,
                                pos=mouseEvent.scenePos())
            else:
                self.addComment(pos=mouseEvent.scenePos())

        QGraphicsScene.mousePressEvent(self, mouseEvent)

    def firstMscGraphItem(self, items):
        for i in items:
            if isinstance(i, MscGraphItem):
                return i
        return None

    def mouseMoveEvent(self, mouseEvent):
        # Show the message
        if self._sceneMode == self.InsertMessage and self.line:
            newLine = QLineF(self.line.line().p1(), mouseEvent.scenePos())
            self.line.setLine(newLine)

        elif self._sceneMode == self.MoveItem:
            QGraphicsScene.mouseMoveEvent(self, mouseEvent)
            return mouseEvent

    def mouseReleaseEvent(self, mouseEvent):
        # When we are creating the message, I insert there
        if (self.line and self._sceneMode == self.InsertMessage):
            ''' StartItems: items positioning in the start of the line'''
            startItems = self.items(self.line.line().p1())
            if len(startItems) and startItems[0] == self.line:
                startItems.pop(0)
            ''' endItems: items positioned in the end of the line'''
            endItems = self.items(self.line.line().p2())
            if len(endItems) and endItems[0] == self.line:
                endItems.pop(0)

            ''' Erase the preliminar message '''
            self.removeItem(self.line)
            self.line = None

            if len(startItems) and len(endItems):
                if((isinstance(startItems[0], MscGraphInstance) and isinstance(endItems[0], MscGraphInstance)) or
                   (isinstance(startItems[0], MscGraphBMSC) and isinstance(endItems[0], MscGraphInstance)) or
                   (isinstance(startItems[0], MscGraphInstance) and isinstance(endItems[0], MscGraphBMSC)))\
                and startItems[0] != endItems[0]:
                    self.addMessage(startItems[0], endItems[0],
                                    mouseEvent.scenePos().y())

        self.line = None
        QGraphicsScene.mouseReleaseEvent(self, mouseEvent)