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

from qt import *
import math
import coral
L = coral.lang.DIML10
SMD = coral.lang.SMD01
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 BoxGER(DiagramElementRenderer):
    def drawShape( self, painter ):
        painter.drawRect(0,0,self.widget.width(),self.widget.height())


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)

    DIAMOND_SIDE_LENGTH = 11.0
    DIAMOND_STRUT = (DIAMOND_SIDE_LENGTH**2/2.0)**0.5

    def _drawArrow(self,painter,s):
        self._preparePainter(painter)
        painter.drawLine(0,0,0+s*self.ARROWHEAD_PROJ,0+self.ARROWHEAD_RISE)
        # The -PEN_THICKNESS makes the arrow look good in e.g. Postscript.
        # Weird but true.
        painter.drawLine(0,
                         -presentation.PEN_THICKNESS,
                         0 + s*self.ARROWHEAD_PROJ,
                         -presentation.PEN_THICKNESS - self.ARROWHEAD_RISE)

    def _drawDiamond(self,painter,fillDiamond,isBeginConnector):
	"""_drawDiamond draws a Diamond on the AssociationEnd which can represent an Composition(black diamond) or an Aggregation(white diamond) 
           fillDiamond=0(white diamond),fillDiamond=1(black diamond),isBeginConnector=1(beginConnector),isBeginConnector=0(endConnector)
        """
        if not fillDiamond:
            painter.setBrush(Qt.white)
        else:
            painter.setBrush(Qt.black)
        
        if isBeginConnector:
            k = 1
        else:
            k = -1

        diamond = QPointArray([0,0,
                               0+k*self.DIAMOND_STRUT,self.DIAMOND_STRUT,
                               # The -k makes stuff look better. Weird but
                               # true...
                               0+k*2.0*self.DIAMOND_STRUT - k, 0,
                               0+k*self.DIAMOND_STRUT,-self.DIAMOND_STRUT])
        painter.drawPolygon(diamond)

class StringSGER( DiagramElementRenderer ):

    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 MetaClassGER( StringSGER ):

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

    def getAlignmentFlags( self ):
        return Qt.AlignCenter|Qt.AlignVCenter
