coretographics.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
#******************************************************************************
#
# 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
#
#******************************************************************************
'''
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
from msccore import MscDocument

from mscgraphics import MscGraphBMSC
from mscgraphics import MscGraphBasicMSCScene
from mscgraphics import MscGraphInstance
from mscgraphics import MscGraphMessage
from mscgraphics import MscGraphTimer


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():
            #print e.element
            event = e.element
            # Messages
            if isinstance(e.element, MscMessage):
                coreMsg = e.element
                #msgHash = coreMsg.hash()
                #print msgHash

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

                if elem != None:
                    found = True
#                    print 'FOUND: {0}'.format(elem)
                else:
                    y = (spaceY * (coreMsg.absPos() - 1)) + startY
#                    print "PUNTO Y: {}".format(y)
                    elem = MscGraphMessage(graph, graph, data=coreMsg)
                    elem.setPos(elem.pos().x(), y)

                if found:
                    #print "FOUND {}".format(elem)
                    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
            if isinstance(event, MscTimer):

                y = (spaceY * (event.absPos() - 1)) + startY
#                print "PUNTO Y: {}".format(y)
                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())

    def obtainResult(self):
        return self.scenes