mscevent.py 3.85 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
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
16
#  GNU Lesser General Public License for more details.
Maxime Perrotin's avatar
Maxime Perrotin committed
17
#
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>
#
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
#
#******************************************************************************
import logging

from mscelement import MscElement

logger = logging.getLogger(__name__)


class MscEvent(MscElement):
    '''
    classdocs
    '''
    TYPE = 'event'

    def __init__(self, elem=None, parent=None):
        '''
        Create new MscEvent with 'elem' associated.
        '''
        super(MscEvent, self).__init__(parent=parent,
                                       elemType=self.TYPE)
        self.element = None
        self.setElement(elem)

    def isElement(self, elem):
        '''
        Return True if 'elem' passed as argument is the same MscElement 
        that event has associated.
        '''
        if elem == self.element:
            return True
        else:
            return False

    def setElement(self, elem):
        """ Set the Msc Element class associated with this event """
        if self.element != None:
            self.element.deleted.disconnect(self._elementDeleted)
            self.element.contentsChanged.disconnect(self.contentsChanged)
        self.element = elem

        if elem != None:
            self.element.deleted.connect(self._elementDeleted)
            self.element.contentsChanged.connect(self.contentsChanged)

    def pos(self):
        #TODO: Change the algorithm of position
        return self.element.absPos()

    def delete(self):
        u"""
        Delete the Event Object.
        Emit 'deleted' signal before delete the element associated
        """
        pname = None
        ename = None
        if self.parent() != None:
            pname = self.parent().name()
        if self.element != None:
            ename = self.element.name()
        logger.debug("Deleting MscEvent '%s' associated with instance '%s' "
                     "and element '%s'",
                     self, pname, ename)

        self.deleted.emit(self)
        # Disassocited element
        element = self.element
        self.setElement(None)
        if element != None:
            element.delete()
        self.deleteLater()

    def _elementDeleted(self, elem):
        u"""
        Handler for 'deleted' signal for MscElement 'element' associated.
        When the element associated is deleted, then the event must be deleted
        """
        if elem != self.element:
            logger.error("Trying to delete Event from Element not associated")
            return False
        # The element will be deleted, set element as None
        self.setElement(None)
        # MscEvent is a wrapper for element, then delete himself
        self.delete()

    def __str__(self):
        return '<<{0}: {1} -> {2}>>'.format(self.elemType, self._name,
                                            self.parent())

    def show(self, n=1):
        tab = ''
        i = 1
        while (i < n):
            tab = tab + '  '
            i = i + 1
        print tab + '{0}'.format(self.element)