mscGraphCondition.py 5.4 KB
Newer Older
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
27
28
#******************************************************************************
#
# 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>
#  Author of this module: Maxime Perrotin <maxime.perrotin@esa.int>
#
#  Copyright (c) 2012 European Space Agency
#
#******************************************************************************
from PySide.QtGui import QPainterPath, QPen
29
from PySide.QtCore import Qt, Slot, QPointF
30
31
32
33
34
35
36
37
38
39
40

from msccore import MscCondition
from mscgraphics import MscGraphItem
from mscgraphics import MscLabelable


class MscGraphCondition(MscGraphItem):
    '''
        Class that draws a Condition symbol
    '''

41
42
    DefaultWidth = 150
    DefaultHeight = 30
43

44
    def __init__(self, data=None, y=0, label=None, parent=None, instance=None):
45
46
        super(MscGraphCondition, self).__init__(parent)
        self.setFlag(super(MscGraphCondition, self).ItemIsMovable, True)
47
        self.instance = instance
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63

        if label is None:
            label = "Condition"

        if data is None:
            self.setMscData(MscCondition(name=label))
        else:
            self.setMscData(data)

        self.initilizeGraphics()
        self.addLabel()
        # Signal when selected
        self.setSignalWhenSelected(True)

        # Create Path
        self.createPath()
64
        self.setPos(self.pos().x(), y or 0)
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

    #**************************************************************************
    # Data Functions
    #**************************************************************************
    @Slot(str)
    def setDataName(self, name):
        self.mscData().setName(name)

    @Slot()
    def readMscData(self):
        self.updateLabel()

    def delete(self):
        u"""
        Delete the MscGraphCondition.
        Reimplemented from MscGraphItem.
        """
        # Ask parentItem to delete the event
        # NOTE: This could be implemented with signals

        parent = self.parentItem()
        if parent != None:
            parent.deleteMscEvent(self)

    #**************************************************************************
    # Graphical Text
    #**************************************************************************
    def addLabel(self):
        self.label = MscLabelable(self.mscData().getName(), self)
        self.updateLabelPosition()
        self.label.labelChanged.connect(self.setDataName)

    def updateLabel(self):
        self.label.setTextLabel(self.mscData().getName())

    def updateLabelPosition(self):
101
        self.label.setCenterPos(QPointF(0, 15))
102
103
104
105
106
107

    #**************************************************************************
    # Item Change
    #**************************************************************************
    def limitsOfItem(self, value):
        u""" Check the limits of the event area of parent """
108
        parent = self.instance
109
110
111
112
        if parent == None:
            return value

        upperY = parent.upperLimit().y()
113
        #bottomY = parent.bottomLimit().y()
114
115
116
117
        valueY = value.y()

        if valueY < upperY:
            valueY = upperY
118
119
        new_pos = QPointF(self.pos().x(), valueY)
        return new_pos
120
121

    def itemChange(self, change, value):
122
        ''' On position change, check that item stays in an allowed area '''
123
124
125
126
127
128
129
        if (change == super(MscGraphCondition, self).ItemPositionChange):
            value = self.limitsOfItem(value)

        elif (change == super(MscGraphCondition, self).ItemPositionHasChanged):
            self.mscData().setAbsPos(self.mapToScene(0, 0).y())
            self.yHasChanged.emit(self)

130
        return value #MscGraphItem.itemChange(self, change, value)
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

    #**************************************************************************
    # Paint Functions
    #**************************************************************************
    def initilizeGraphics(self):
        self.color = Qt.blue
        self.pen = QPen(self.color, 1, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin)

    def updateBounding(self):
        u"""
            Update internal drawing coordinates
        """
        self.prepareGeometryChange()
        rect = self.path.boundingRect()
        self.drawOriginX = rect.x()
        self.drawOriginY = rect.y()
        self.width = rect.width()
        self.height = rect.height()
        self.updateLabelPosition()

    def createPath(self):
        u"""
        Create the path of Timer
        """
        self.path = QPainterPath()
        self.path.addRect(-self.DefaultWidth / 2, 0,
                          self.DefaultWidth, self.DefaultHeight)
        # Set correct values of x, y, width and height
        self.updateBounding()

    def paint(self, painter, option, widget):
        painter.setPen(self.pen)
        painter.drawPath(self.path)