mscGraphCondition.py 5.33 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 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
#******************************************************************************
#
# 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
from PySide.QtCore import Qt, Slot

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


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

    DefaultWidth = 100
    DefaultHeight = 100

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

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

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

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

    def itemChange(self, change, value):
        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)

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