mscGraphTimer.py 7.02 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 27 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
#******************************************************************************
#
# 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
#
#******************************************************************************
from PySide.QtGui import QPainterPath, QMatrix, QPen
from PySide.QtCore import Qt, Slot

from msccore import MscTimer
from mscgraphics import MscGraphItem
from mscgraphics import MscLabelable


class MscGraphTimer(MscGraphItem):
    '''
    Class that draw a Timer simbol of MSC Standard
    '''

    TimerWidth = 15
    TimerLine = 40
    ArrowWidth = 15
    ArrowHeight = (2 * ArrowWidth) / 3.0

    def __init__(self, data=None, typeTimer=MscTimer.StartTimer, y=0,
                 label=None, parent=None):
        u"""
        Initize
        """
        super(MscGraphTimer, self).__init__(parent)
        self.setFlag(super(MscGraphTimer, self).ItemIsMovable, True)

        if label == None:
            label = "Timer"

        if data == None:
            self.setMscData(MscTimer(name=label,
                                     typeTimer=typeTimer))
        else:
            self.setMscData(data)

        self.initilizeGraphics()
        self.addLabel()
        # Timer signal when selected
        self.setSignalWhenSelected(True)

        # Create Path
        self.createPath()

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

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

    def delete(self):
        u"""
        Delete the MscGraphTimer.
        Reimplemented from MscGraphItem.
        """
        # Say parentItem to delete the event
        # TODO: 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.messageText.setFlag(MscLabelable.ItemIsMovable, True)
        self.updateLabelPosition()
        self.label.labelChanged.connect(self.setDataName)

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

    def updateLabelPosition(self):
        self.label.setPos(self.width + 10,
                          -self.label.boundingRect().height() / 2)

    #**************************************************************************
    # Item Change
    #**************************************************************************
    def limitsOfItem(self, value):
        u""" Check the limits of the event area of parent """
        parent = self.parentItem()
        if parent == None:
            return value

        upperY = parent.upperLimit().y()
        bottomY = parent.bottomLimit().y()
        valueY = value.y()

        if valueY < upperY:
            valueY = upperY
#        if valueY > bottomY:
#            valueY = bottomY

        value.setX(self.pos().x())
        value.setY(valueY)
        return value

    def itemChange(self, change, value):
        if (change == super(MscGraphTimer, self).ItemPositionChange):
            value = self.limitsOfItem(value)

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

        return MscGraphItem.itemChange(self, change, value)

    #**************************************************************************
    # 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"""
        Updete internal draw 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
        """
        # To rotate 180
        matrix = QMatrix()
        matrix.rotate(180)
        # Triangle of timer
        triangle = QPainterPath()
        triangle.lineTo(-self.TimerWidth / 2, -self.TimerWidth / 2)
        triangle.lineTo(self.TimerWidth / 2, -self.TimerWidth / 2)
        triangle.closeSubpath()

        # Second triangle, rotate 180 the first triangle
        triangle2 = QPainterPath(matrix.map(triangle))

        typeTimer = self.mscData().timerType()

        self.path = QPainterPath()
        self.path.lineTo(self.TimerLine, 0)
        if typeTimer == MscTimer.StartTimer:
            triangle.translate(self.TimerLine, 0)
            triangle2.translate(self.TimerLine, 0)
            self.path.addPath(triangle)
            self.path.addPath(triangle2)
        elif typeTimer == MscTimer.StopTimer:
            cross = QPainterPath()
            cross.moveTo(-self.TimerWidth / 2, -self.TimerWidth / 2)
            cross.lineTo(self.TimerWidth / 2, self.TimerWidth / 2)
            cross.moveTo(-self.TimerWidth / 2, self.TimerWidth / 2)
            cross.lineTo(self.TimerWidth / 2, -self.TimerWidth / 2)
            cross.translate(self.TimerLine, 0)
            self.path.addPath(cross)
        elif typeTimer == MscTimer.TimeOut:
            triangle.translate(self.TimerLine, 0)
            triangle2.translate(self.TimerLine, 0)

            arrow = QPainterPath()
            arrow.lineTo(self.ArrowWidth, -(self.ArrowHeight / 2))
            arrow.lineTo(self.ArrowWidth, self.ArrowHeight / 2)
            arrow.closeSubpath()

            self.path.addPath(arrow)
            self.path.addPath(triangle)
            self.path.addPath(triangle2)

        # 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)