mscinstancekind.py 7.58 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 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
#
#******************************************************************************
import logging
from PySide.QtCore import QRegExp

import mscregexp
from mscelement import MscElement

logger = logging.getLogger(__name__)


class MscInstanceKind(MscElement):
    u"""
    The "MscInstanceKind" represents information unique to a specific
    instance.
    """
    # When data is changed signal
    #dataHasChanged = Signal()

    _identifier = None
    _qualifier = None
    _denominator = None

    def __init__(self, identifier=None, qualifier=None, denominator=None):
        super(MscInstanceKind, self).__init__()
        self._identifier = "Undefined"
        self._qualifier = None
        self._denominator = None
        self.identifierRegExp = QRegExp(mscregexp.Name)
        # Modified over mscregexp.InstanceKind
        self.totalKindRegExp = QRegExp('(' + mscregexp.Name + ')' +
                '|' + '(' + mscregexp.Name + ')?' + '(?:\s?)' +
                      '(' + mscregexp.Qualifier + ')?' + '(?:\s?)' +
                      '(' + mscregexp.Name + ')')

        self.setIdentifier(identifier)
        if  qualifier:
            self._qualifier = qualifier
        if denominator:
            self._denominator = qualifier
        # This class is very simple because only contents changed when
        # internal data change
        self.dataHasChanged.connect(self._contentsChanged)

    def delete(self):
        u""" Delete the Kind """
        # Emit 'delete' signal for other objects know that I was deleted
        super(MscInstanceKind, self).delete()
        self.deleteLater()

    def name(self):
        return self.kindString()

    def identifier(self):
        """ Return identifier property of kind """
        if self._identifier:
            return self._identifier
        return u""

    def qualifier(self):
        """ Return qualifier property of kind """
        if self._qualifier:
            return self._qualifier
        return u""

    def denominator(self):
        """ Return denominator property of kind """
        if self._denominator:
            return self._denominator
        return u""

    def setIdentifier(self, ident):
        """
        Set identifier property of kind

        Identifier always have a value different of "" or None
        """
        logger.debug("setIdentifier to '%s'", ident)
        if not self.identifierRegExp.exactMatch(ident):
            return False
        logger.debug("Change identifier of kind '%s' to '%s'", 
                     ident, self._identifier)
        self._identifier = ident
        self.dataHasChanged.emit()
        return True

    def setQualifier(self, qual):
        u"""
        Set qualifier value.

        Qualifier should be start with '<<' and terminate with '>>', but if
        forget it the function will add '<<' '>>'.
        If 'qual' is empty string "" then none qualifier is set.
        If 'qual' is '<<>>' then '<<>>' qualifier is set.
        """
        # Check if qual have '<<' and '>>'. Is a qualifier.
        if mscregexp.MscQualifierRegExp.exactMatch(qual):
            qualifier = qual
        elif qual == "" or qual == None:  # If "" none is set
            qualifier = None
        elif mscregexp.MscTextRegExp.exactMatch(qual):  # Add << >>
            qualifier = (mscregexp.qualifier_left + qual +
                         mscregexp.qualifier_right)
        else:
            # No change in qualifier
            return False
        logger.debug("Change qualifier of kind '%s' to '%s'",
                     qualifier, self._qualifier)
        self._qualifier = qualifier
        self.dataHasChanged.emit()
        return True

    def setDenominator(self, denom):
        """
        Set denominaotr property

        Denominator is <name> lexical unit as said in ITU-T Z.120.
        If 'denom' is empty string "" or none then none is set.
        """
        if mscregexp.MscNameRegExp.exactMatch(denom):
            denominator = denom
        elif denom == "" or denom == None:
            denominator = None
        else:
            return False
        logger.debug("Change denominator of kind '%s' to '%s'",
                     denominator, self._denominator)
        self._denominator = denominator
        self.dataHasChanged.emit()
        return True

    def setExpression(self, expr):
        """ """

        if not self.totalKindRegExp.exactMatch(expr):
            logger.debug("Setting incorrect expression in kind '%s'.\n"
                         "Captures = %s", 
                         self.kindString(), 
                         self.totalKindRegExp.capturedTexts())
            return False

        listCap = self.totalKindRegExp.capturedTexts()

        if listCap[1] == u"":
            self.setDenominator(listCap[2])
            self.setQualifier(listCap[3])
            self.setIdentifier(listCap[4])
        else:
            self.setIdentifier(listCap[1])
        return True

    def kindString(self):
        u"""
        Return the string that represent the instance Kind
        """
        if self.denominator() != "" and self.qualifier() != "":
            kindStr = u"{} {} {}".format(self.denominator(), self.qualifier(),
                                      self.identifier())
        elif self.denominator() == "" and self.qualifier() == "":
            kindStr = self.identifier()
        elif self.denominator() == "":
            kindStr = u"{} {}".format(self.qualifier(), self.identifier())
        elif self.qualifier() == "":
            kindStr = u"{} {}".format(self.denominator(), self.identifier())
        return kindStr

    def __eq__(self, other):
        return self.__str__() == other.__str__()

    def __unicode__(self):
#        qualifier = self.qualifier()
#        if qualifier != u"":
#            return u"{} <<{}>> {}".format(self.denominator(), qualifier,
#                                          self.identifier())
#        else:
#            return u"{} {}".format(self.denominator(), self.identifier())
        return self.kindString()

    def __str__(self, *args, **kwargs):
        u""" Return the string of Instance Kind """
#        if self.denominator() != u"" and self.qualifier() != u"":
#            kindStr = u"{} {} {}".format(self.denominator(), self.qualifier(),
#                                      self.identifier())
#        elif self.denominator() == u"" and self.qualifier() == u"":
#            kindStr = u"{}".format(self.identifier())
#
#        elif self.denominator() == u"":
#            kindStr = u"{} {}".format(self.qualifier(), self.identifier())
#        elif self.qualifier() == u"":
#            kindStr = u"{} {}".format(self.denominator(), self.identifier())

        return self.kindString()