# This file is part of the Coral project.
# Please read the copyright notice in the LICENSE file.

from qt import *
import math
import re
import coral
coral.lang.UML14.__import__(globals())
DI = coral.lang.XMIDI20
from coral.modeler.diagram.presentation import *
#from util import *

 

class RelationGER(DiagramElementRenderer):

    # This is the base class for relationship renderers: Association,
    # Generalization, Dependency and Realization

    ARROWHEAD_ANGLE = math.radians(20.0)
    ARROWHEAD_LENGTH = 12.0
    ARROWHEAD_PROJ = ARROWHEAD_LENGTH*math.cos(ARROWHEAD_ANGLE)
    ARROWHEAD_RISE = ARROWHEAD_LENGTH*math.sin(ARROWHEAD_ANGLE)

    def _drawArrow(self,painter,s):
        self._preparePainter(painter)
        painter.drawLine(0,0,0+s*self.ARROWHEAD_PROJ,0+self.ARROWHEAD_RISE)
        painter.drawLine(0,0,0+s*self.ARROWHEAD_PROJ,0-self.ARROWHEAD_RISE)


class DependencyGER( RelationGER ):

    def _preparePainter( self, painter ):
        RelationGER._preparePainter(self,painter)
        pen = QPen(painter.pen())
        pen.setStyle(Qt.DashLine)
        
        color=None

        for tv in self.element.taggedValue:
            if str(tv.name) == "targetHasChanged" and str(tv.dataValue)=="True":
                color=QColor("yellow")
            if str(tv.name) == "sourceHasChanged" and str(tv.dataValue)=="True":
                color=QColor("red")          
            if str(tv.name) == "type" and str(tv.dataValue)=="ManagedModelRelationship" and color is None:
                color=QColor("green")
                
        pen=QPen(painter.pen())
        if color:
            pen.setColor(color)
        else:
            pen.setColor(QColor("black"))
            pen.setStyle(Qt.DashLine)
        painter.setPen(pen)

    def drawEndConnector( self, painter ):
        RelationGER.drawEndConnector(self,painter)
        self._drawArrow(painter,-1)


class ModelRelationshipGER( RelationGER ):

    def _preparePainter( self, painter ):
        RelationGER._preparePainter(self,painter)
        pen = QPen(painter.pen())
        pen.setStyle(Qt.DashLine)
        
        color=QColor("green")

        if self.element.targetHasChanged:
            color=QColor("yellow")
        if self.element.sourceHasChanged:
            color=QColor("red")
       
                 
        pen=QPen(painter.pen())
        pen.setColor(color)
        painter.setPen(pen)

    def drawEndConnector( self, painter ):
        RelationGER.drawEndConnector(self,painter)
        self._drawArrow(painter,-1)

class InputParameterGER(ModelRelationshipGER):
    pass

class OutputParameterGER(ModelRelationshipGER):
    pass


class ManagedModelGER(DiagramElementRenderer):

    def drawShape( self, painter ):

        painter.drawRect(0,0,self.widget.width(),self.widget.height())

class StepGER(DiagramElementRenderer):

    def drawShape( self, painter ):
        b=QBrush(QBrush.SolidPattern)
        if self.element.error:
            b.setColor(QColor("red"))
        else:
            b.setColor(QColor("white"))
        
        painter.setBrush(b)
        
        painter.drawRect(0,0,self.widget.width(),self.widget.height())
