mscGraphBMSC.py 13.3 KB
Newer Older
Maxime Perrotin's avatar
Maxime Perrotin committed
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
Maxime Perrotin's avatar
Maxime Perrotin committed
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.
Maxime Perrotin's avatar
Maxime Perrotin committed
17
#
Maxime Perrotin's avatar
Maxime Perrotin committed
18
#  You should have received a copy of the GNU Lesser General Public License
Maxime Perrotin's avatar
Maxime Perrotin committed
19 20 21 22 23
#  along with TASTE Msc Diagram Editor.  If not, see
#  <http://www.gnu.org/licenses/>.
#
#  Author: Angel Esquinas <aesquina@datsi.fi.upm.es>
#
Maxime Perrotin's avatar
Maxime Perrotin committed
24
#  Copyright (c) 2012 UPM and European Space Agency
Maxime Perrotin's avatar
Maxime Perrotin committed
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 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 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 332 333 334 335 336 337 338 339 340 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
#
#******************************************************************************
from PySide.QtGui import QGraphicsSimpleTextItem, QPainterPath,\
                        QGraphicsPathItem, QPainterPathStroker, QPen, \
                        QPolygonF
from PySide.QtCore import QPointF, Qt, Slot, Signal, QRectF

import msccore
from mscgraphics import MscGraphItem, MscLabelable
from mscGraphInstance import MscGraphInstance


class MscGraphBMSC(MscGraphItem):
    FixedTitle = "msc "
    BTextHeight = 25
    TextOffset = 3
    MinHeight = 300
    MinWidth = 500

    # Y coordenate to fix the position (0,0) of new instances
    AxysYInsntance = 90

    # Signal that emit when mscName has changed
    nameChanged = Signal(str)

    def __init__(self, title=None, bmscData=None, parent=None):
        super(MscGraphBMSC, self).__init__(parent)

        if title == None:
            title = "Untitled"

        # If no mscCore.BasicMsc object is passed as argument
        # Create New mscCore.BasicMsc object that contain the data
        if bmscData == None:
            self.setMscData(msccore.BasicMsc(title))
        else:
            self.setMscData(bmscData)

        # Title form
        self.aroundText = QGraphicsPathItem(self)
        self.createTitle()

        self.setRect(0, 0, 700, 500)

        # Set resizable, corner Grabbers are set when selected
        self.setResizable(True)
        # BMsc signal when selected
        self.setSignalWhenSelected(True)

    def readMscData(self):
        u"""
        Get all the data from bmscData and set these
        """
        self.setMscName(self.mscData().name())

    @Slot(str)
    def setDataName(self, name):
        self.mscData().setName(name)

    #---------- Comment
    def commentPosition(self):
        return QPointF(self.drawOriginX, self.drawOriginY + self.BTextHeight)

    #**************************************************************************
    # Get and set
    #**************************************************************************
    def getName(self):
        return self.mscData().name()

    @Slot(str)
    def setMscName(self, name):
        'Change the name of the BMSC and emit the right signal'
        self.updateTitle()
        self.nameChanged.emit(name)

    def setRect(self, x, y, width, height):
        'Set the rectangle of around box'
        self.rect = QRectF(x, y, width, height)
        self.updateDraw()

    def setTitle(self, title):
        self.title = title

    #**************************************************************************
    # Items of BMSC
    #**************************************************************************
    def addInstance(self, instance, new=True, x=0):
        u"""Asociate the instance with this basis MSC"""
        # Instance now are my child
        instance.setParentItem(self)

        # Control the graphical changes
        instance.setFixAxisY(False)
        instance.setPos(x, self.AxysYInsntance)
        instance.setFixAxisY(True)  # Fix the instance in the Y Axis

        # Connect the signals
        instance.xChanged.connect(self.updateLimits)
        instance.itemResized.connect(self.updateLimits)

        # Add the data object of instance to the data object basic Msc
        if new:
            self.mscData().addInstance(instance.mscData())

        # Update the paint limits of Basic Msc
        self.updateLimits()

    def removeInstance(self, instance):
        instance.setParentItem(None)
        self.mscData().removeInstance(instance.mscData())

    def addMessage(self, message, y=0, new=True):
        u"""Asociate the message with the basic MSC"""
        message.setParentItem(self)
        if message.scene() != self.scene():
            self.scene().addItem(message)
        if new:
            self.mscData().addMessage(message.mscData())
        # Add the data object of message to the data object basic Msc
        #self.mscData().addMessage(message.mscData())

    def removeMessage(self, message):
        message.setParentItem(None)
        self.mscData().removeMessage(message.mscData())

    def addMessageEvent(self, message):
        u""" This function is only to be compatible with mscGraphMessage """
        # TODO: FIX IT
        pass

    def removeMessageEvent(self, message):
        u""" This function is only to be compatible with mscGraphMessage.
        When a message is deleted call this function from it sender and
        receiver because it can now when are deleted
        """
        # TODO: Change for signals
        pass
    #**************************************************************************
    # Events Functions
    #**************************************************************************
    def upperLimitInstance(self, sender=True):
        ''' Return Axis start coordinate in based of the parent '''
        if sender:
            return QPointF(self.drawOriginX, self.drawOriginY)
        else:
            return QPointF(self.drawOriginX + self.width, self.drawOriginY)

    def bottomLimitInstance(self, sender=True):
        ''' Return the y coordinate in based of the parent '''
        if sender:
            return QPointF(self.drawOriginX, self.drawOriginY + self.height)
        else:
            return QPointF(self.drawOriginX + self.width, self.drawOriginY + self.height)
    #**************************************************************************
    # PAINT FUNCTIONS
    #**************************************************************************
    #**************************************************************************
    # Update Draw
    #**************************************************************************
    def updateDraw(self):
        r = self.rect
        self.prepareGeometryChange()
        self.drawOriginX = r.x()
        self.drawOriginY = r.y()
        self.width = r.width()
        self.height = r.height()
        self.updatePath()
        self.setResizeHandlersPositions()
        self.updatePosTitle()

    def updatePath(self):
        'Update the size of the path of mscGraphBMSC class'
        self.path = QPainterPath()
        self.path.addRect(self.drawOriginX, self.drawOriginY,
                     self.width, self.height)

    def updatePosTitle(self):
        '''Update the position of the text frame. When bmsc element is resize
        you must call this function'''
        self.aroundText.setPos(self.drawOriginX, self.drawOriginY)

    #**************************************************************************
    # Update Title
    #**************************************************************************
    def updateTitle(self, newTitle=None):
        # Calculate the witdh of titles text and recolocate
        rfixed = self.fixedLabel.boundingRect()
        pfixed = self.fixedLabel.pos()

        self.label.setTextLabel(self.mscData().getName())
        self.label.setPos(pfixed.x() + rfixed.width(),
                          pfixed.y())
        self.textWidth = rfixed.width() + self.label.boundingRect().width()
        # Create the path around title
        self.aroundText.setPath(self.createPathTitle())

    def createPathTitle(self):
        'Create the path around title'
        path = QPainterPath(QPointF(0, self.BTextHeight))
        path.lineTo(self.textWidth, self.BTextHeight)
        path.lineTo(self.textWidth + (self.BTextHeight / 2),
                    self.BTextHeight / 2)
        path.lineTo(self.textWidth + (self.BTextHeight / 2), 0)
        return path

    def createTitle(self):
        # "msc" label
        self.fixedLabel = QGraphicsSimpleTextItem(self.FixedTitle, self.aroundText)
        self.fixedLabel.setPos(self.TextOffset, self.TextOffset)
        # "name of msc"
        self.label = MscLabelable(self.mscData().getName(), self.aroundText)
        self.label.labelChanged.connect(self.setDataName)
        self.updateTitle()

    #**************************************************************************
    # Update Limits
    #**************************************************************************
    def updateLimits(self):
        n = 1
        # Region occuped for instances 
        pol = QPolygonF()
        for i in self.childItems():
            if isinstance(i, MscGraphInstance):

                pol = pol.united(i.mapToParent(i.boundingRect()))

                n = n + 1
        if pol.isEmpty():
            elemRect = None
        else:
            elemRect = pol.boundingRect()

        # Initilize Aux Variables
        newX = self.drawOriginX
        newY = self.drawOriginY  # Dont Change
        newWidth = self.width
        newHeight = self.height

        # Check if are in the limits
        if elemRect != None:
            if newX > elemRect.x():
                newWidth = (newX - elemRect.x()) + newWidth
                newX = elemRect.x()
            if newX + newWidth < elemRect.x() + elemRect.width():
                newWidth = (elemRect.x() - newX) + elemRect.width()
            if newY + newHeight < elemRect.y() + elemRect.height():
                newHeight = (elemRect.y() - newY) + elemRect.height()

        if newWidth < self.MinWidth:
            newWidth = self.MinWidth
        if newHeight < self.MinHeight:
            newHeight = self.MinHeight
        self.setRect(newX, newY, newWidth, newHeight)
        self.xChanged.emit()

    #**************************************************************************
    # Resize Item
    #**************************************************************************
    def resizeItem(self, watched, pos):
        #n = 1
        # Region occupied for instances
        pol = QPolygonF()
        for i in self.childItems():
            if isinstance(i, MscGraphInstance):
                pol = pol.united(i.mapToParent(i.boundingRect()))

        if pol.isEmpty():
            elemRect = None
        else:

            elemRect = pol.boundingRect()

        # Initilize Aux Variables
        newX = self.drawOriginX
        newY = self.drawOriginY  # Don't Change
        newWidth = self.width
        newHeight = self.height
        deltaX = pos.x() - self.drawOriginX
        deltaY = pos.y() - self.drawOriginY

        if watched.posNumber == self.TopLeft:
            newX = pos.x()

            newWidth = self.width + (-deltaX)

        if watched.posNumber == self.TopRight:
            newWidth = deltaX

        if watched.posNumber == self.BottomLeft:
            newX = pos.x()
            newWidth = self.width + (-deltaX)
            newHeight = deltaY

        if watched.posNumber == self.BottomRight:
            newWidth = deltaX
            newHeight = deltaY

        # Check if are in the limits
        if elemRect != None:
            if newX > elemRect.x():
                deltaX = elemRect.x() - self.drawOriginX
                newWidth = self.width + (-deltaX)
                newX = elemRect.x()
            if newX + newWidth < elemRect.x() + elemRect.width():
                newWidth = (elemRect.x() - newX) + elemRect.width()
            if newY + newHeight < elemRect.y() + elemRect.height():
                newHeight = (elemRect.y() - newY) + elemRect.height()

        if newWidth < self.MinWidth:
            if (watched.posNumber == self.BottomLeft or
                watched.posNumber == self.TopLeft):
                newX = self.drawOriginX
            newWidth = self.MinWidth
        if newHeight < self.MinHeight:
            newHeight = self.MinHeight
        self.setRect(newX, newY, newWidth, newHeight)
        self.xChanged.emit()
        super(MscGraphBMSC, self).resizeItem(watched, pos)

    #**************************************************************************
    # Graphics Item Functions
    #**************************************************************************
    def shape(self):
        '''
        Override Shape. Only The outline of the background are
        valid to select the the frame
        '''
        strokePath = QPainterPathStroker()
        strokePath.setWidth(2 * self.Offset)
        return strokePath.createStroke(self.path)

    def paint(self, painter, option, widget=None):
        painter.setPen(QPen(self.pen).setColor(Qt.black))
        painter.drawPath(self.path)

    #**************************************************************************
    # Event Functions
    #**************************************************************************
    def mousePressEvent(self, event):
        MscGraphItem.mousePressEvent(self, event)

    def mouseMoveEvent(self, event):
        MscGraphItem.mouseMoveEvent(self, event)