mscGraphCondition.py 6 KB
Newer Older
1 2 3 4 5 6 7 8
#******************************************************************************
#
# 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
Maxime Perrotin's avatar
Maxime Perrotin committed
9
#  modify it under the terms of the GNU Lesser General Public License as published
10 11 12 13 14 15
#  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
Maxime Perrotin's avatar
Maxime Perrotin committed
16
#  GNU Lesser General Public License for more details.
17
#
Maxime Perrotin's avatar
Maxime Perrotin committed
18
#  You should have received a copy of the GNU Lesser General Public License
19 20 21 22 23 24
#  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>
#
Maxime Perrotin's avatar
Maxime Perrotin committed
25
#  Copyright (c) 2012 UPM and European Space Agency
26 27
#
#******************************************************************************
28
from PySide.QtGui import QPainterPath, QPen, QBrush, QColor, QPainter
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

    #**************************************************************************
    # Paint Functions
    #**************************************************************************
    def initilizeGraphics(self):
136
        self.color = Qt.black
137
        self.pen = QPen(self.color, 1, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin)
138
        self.brush = QBrush(QColor(255, 255, 202))
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156

    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()
157 158 159 160 161 162 163 164 165 166 167
        self.path.moveTo(5, 0)
        self.path.lineTo(0, self.DefaultHeight / 2)
        self.path.lineTo(5, self.DefaultHeight)
        self.path.lineTo(self.DefaultWidth - 5, self.DefaultHeight)
        self.path.lineTo(self.DefaultWidth, self.DefaultHeight / 2)
        self.path.lineTo(self.DefaultWidth - 5, 0)
        self.path.lineTo(5, 0)
        self.path.translate(-self.DefaultWidth / 2, 0)

        #self.path.addRect(-self.DefaultWidth / 2, 0,
        #                  self.DefaultWidth, self.DefaultHeight)
168 169 170 171 172
        # Set correct values of x, y, width and height
        self.updateBounding()

    def paint(self, painter, option, widget):
        painter.setPen(self.pen)
173
        painter.setBrush(self.brush)
174
        painter.setRenderHint(QPainter.Antialiasing, True)
175
        painter.drawPath(self.path)