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

from qt import *
import math
import coral
coral.lang.FSM10.__import__(globals())
DI = coral.lang.XMIDI20
from coral.modeler.diagram.presentation import DiagramElementRenderer

import coral.modeler.diagram.di_support as DISUPP
import coral.modeler.diagram.di_support_gfx as DISUPPGFX

class EmptyGER(DiagramElementRenderer):

    def drawShape( self, painter ):
        pass


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 TransitionGER(RelationGER):

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

class StateGER(DiagramElementRenderer):

    def drawShape( self, painter ):
        painter.drawEllipse(1,1,self.widget.width()-1,self.widget.height()-1)


class AcceptingStateGER(DiagramElementRenderer):

    def drawShape( self, painter ):
        pen = QPen(painter.pen())
        pen.setStyle(Qt.SolidLine)
        pen.setWidth(3)
        painter.setPen(pen)
        painter.drawEllipse(0,0,self.widget.width()-3,self.widget.height()-3)


class SimpleDiagramElementGER( DiagramElementRenderer ):

    pass

class StringSGER( SimpleDiagramElementGER ):

    def getString( self ):
        return ""
    
    def getAlignmentFlags( self ):
        return Qt.AlignAuto|Qt.AlignVCenter

    def drawShape( self, painter ):
        painter.setPen(DISUPPGFX.getFontColor(self.ge))
	painter.drawText(0,0,self.widget.width(),self.widget.height(),
                         self.getAlignmentFlags(),self.getString())


class NameSGER( StringSGER ):

    def getString( self ):
        if not self.element:
            self.element = DISUPP.findUpwardsModelElement(self.ge)
        if self.element:
            return unicode(self.element.name)
        else:
            return ""
        
