coretographics.py 7.55 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
#
#******************************************************************************
'''
Created on May 2, 2012

@author: angel
'''
import logging
logger = logging.getLogger(__name__)

from PySide.QtCore import QPointF
from msccore import MscVisitor
from msccore import MscMessage
from msccore import MscTimer
39
from msccore import MscCondition
Maxime Perrotin's avatar
Maxime Perrotin committed
40 41 42 43 44 45 46
from msccore import MscDocument

from mscgraphics import MscGraphBMSC
from mscgraphics import MscGraphBasicMSCScene
from mscgraphics import MscGraphInstance
from mscgraphics import MscGraphMessage
from mscgraphics import MscGraphTimer
47
from mscgraphics import MscGraphCondition
Maxime Perrotin's avatar
Maxime Perrotin committed
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


def coreToGraphics(document):
    u"""
    Return a collection of MscGraphicsScene with all MscCore Elements
    wrapped by MscGraph class.

    This is a module function that create new CoreToGraphicsVisitor each time
    that call this.
    """
    logger.info("Starting creation of mscgraph scenes for document '%s'",
                document.name())
    visitor = CoreToGraphicsVisitor()
    document.accept(visitor)
    logger.info("MscGraph creation finished")
    return visitor.obtainResult()


# TODO: It is possible to improve this converter. use the visitor pattern.
class CoreToGraphicsVisitor(MscVisitor):
    '''
    classdocs
    '''
    SPACEY = 40.0

    def __init__(self):
        '''
        Constructor
        '''
        self.incompleteMsg = dict()
        self.scenes = []

    def visitor(self, element):
        """ Help to use the correct function """
        if isinstance(element, MscDocument):
            self.visitorMscDocument(element)
        # TODO: When use the visitor pattern add more elements

    def visitorMscDocument(self, element):
        for msc in element.mscs():
            #msc.accept(self)
            self.scenes.append(self.toGraphicsBMsc(msc))

#    def visitorBMSC(self, element):
#        for i in element.instances():
#            print i

    def toGraphicsBMsc(self, msc):
        graphBMSC = MscGraphBMSC(bmscData=msc)
        mscScene = MscGraphBasicMSCScene(graphicMSC=graphBMSC)
        myX = 160
        spaceX = 250
        yFixInstance = graphBMSC.AxysYInsntance

        #Calculate start of instance body
        i = MscGraphInstance(y=yFixInstance, label="Hola", parent=graphBMSC)
        startY = i.upperLimitInstance().y()
        # print "STARTY = {}".format(startY)
        i.setParent(None)
        i = None

        # Add Comment
        if msc.comment() != u"":
            c = mscScene.addComment(graphBMSC, QPointF(5, 30))
            c.setCommentText(msc.comment())

        self.processEvents(graphBMSC, mscScene, startY + 20)
        #print self.incompleteMsg

        for i in msc.instances():
            inst = MscGraphInstance(data=i)
            inst.setBodySize(msc.eventsOnLoad * self.SPACEY)
            # print("Convert instance")
            mscScene.addInstance(myX, inst=inst)

            # Add Comment
            if i.comment() != u"":
                c = mscScene.addComment(item=inst,
                                        pos=QPointF(myX + (2 * inst.width) / 3,
                                                    yFixInstance - 30))
                c.setCommentText(i.comment())

            # Update the paint limits of Basic Msc
            # graphBMSC.updateLimits()
            self.toGraphicInstance(inst, mscScene)
            myX = myX + spaceX

        # Clen selected items and focus from scene
        mscScene.clearFocus()
        mscScene.clearSelection()
        return mscScene

    def toGraphicInstance(self, graphInst, scene):
        myY = graphInst.upperLimitInstance().y()
        # Start Point is upper limit axis of instance
        self.processEvents(graphInst, scene, myY + 20)

    def processEvents(self, graph, scene, startY=None):
        spaceY = self.SPACEY
        found = False

        for e in graph.mscData().events():
            event = e.element
            # Messages
152 153
            if isinstance(event, MscMessage):
                coreMsg = event
Maxime Perrotin's avatar
Maxime Perrotin committed
154 155 156 157

                # Look for the message in the table
                elem = self.incompleteMsg.pop(coreMsg, None)

158
                if elem is not None:
Maxime Perrotin's avatar
Maxime Perrotin committed
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
                    found = True
                else:
                    y = (spaceY * (coreMsg.absPos() - 1)) + startY
                    elem = MscGraphMessage(graph, graph, data=coreMsg)
                    elem.setPos(elem.pos().x(), y)

                if found:
                    if coreMsg.instanceSender() == graph.mscData():
                        elem.setStartInstance(graph)
                    else:
                        elem.setEndInstance(graph)

                    scene.addMessage2(elem)
                    found = False

                    # Add Comment
                    if coreMsg.comment() != u"":
                        pos = QPointF(elem.commentPosition().x() + 10,
                                      elem.commentPosition().y())
                        pos = elem.mapToScene(pos)
                        c = scene.addComment(item=elem,
                                             pos=pos)
                        c.setCommentText(coreMsg.comment())

                else:  # Not found
                    self.incompleteMsg[coreMsg] = elem

            # Timers
187
            elif isinstance(event, MscTimer):
Maxime Perrotin's avatar
Maxime Perrotin committed
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202

                y = (spaceY * (event.absPos() - 1)) + startY
                timer = MscGraphTimer(e.element)

                scene.addTimer2(graph, timer)
                timer.setPos(graph.mapFromScene(timer.pos().x(), y))

                if e.element.comment() != u"":
                    trect = timer.childrenBoundingRect() | timer.boundingRect()
                    pos = QPointF(trect.width() + 10, 0)
                    pos = elem.mapToScene(pos)
                    c = scene().addComment(item=timer,
                                           pos=pos)
                    c.setCommentText(e.element.comment())

203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
            # Conditions
            elif isinstance(event, MscCondition):
                y = (spaceY * (event.absPos() - 1)) + startY
                condition = MscGraphCondition(event)

                scene.addCondition(graph, condition)
                condition.setPos(graph.mapFromScene(timer.pos().x(), y))

                if event.comment() != u"":
                    trect = (condition.childrenBoundingRect()
                             | condition.boundingRect())
                    pos = QPointF(trect.width() + 10, 0)
                    pos = elem.mapToScene(pos)
                    c = scene().addComment(item=condition,
                                           pos=pos)
                    c.setCommentText(event.comment())

Maxime Perrotin's avatar
Maxime Perrotin committed
220 221
    def obtainResult(self):
        return self.scenes