from propertyeditors import *

DI=coral.lang.XMIDI20


class ColorSwatch( QButton ):

    def __init__( self, parent=None ):
        QButton.__init__(self,parent)

        self.color = None
        self.setSizePolicy(QSizePolicy.Fixed,QSizePolicy.Fixed)

        self.setCursor(QCursor(Qt.PointingHandCursor))

        self.connect(self,SIGNAL("clicked()"),self._clicked)


    def sizeHint( self ):
        return QSize(28,18)


    def setColor( self, hexcolor=None ):
        if hexcolor:
            self.color = QColor(hexcolor)
        else:
            self.color = None

        self.update()

    def getColor( self ):
        if self.color:
            return "#%02x%02x%02x" % (self.color.red(),self.color.green(),self.color.blue())
        else:
            return None


    def paintEvent( self, e ):
        p = QPainter(self)

        if self.color:
            c = self.color
        else:
            c = self.paletteBackgroundColor()
        p.fillRect(0,0,self.width(),self.height(),QBrush(c))

        p.drawRect(0,0,self.width(),self.height())

        if not self.color:
            p.drawLine(0,0,self.width()-1,self.height()-1)
            p.drawLine(self.width()-1,0,0,self.height()-1)
        p.end()


    def _clicked( self ):
        if not self.color:
            c = QColor(255,255,255)
        else:
            c = self.color
        c = QColorDialog.getColor(c, None, "Pick color")
        if not c.isValid():
            return
        self.color = c
        self.update()

        self.emit(PYSIGNAL("colorSet"),(self.color,))


    def _noColor( self ):
        self.setColor(None)
        self.emit(PYSIGNAL("colorSet"),(self.color,))


    def contextMenuEvent( self, e ):
        e.accept()

        m = QPopupMenu(self)
        m.setItemEnabled(m.insertItem("No color",self._noColor),self.color!=None)
        m.popup(e.globalPos())


class ColorPicker( QHBox ):

    def __init__( self, parent=None ):
        QHBox.__init__(self,parent)

        self._swatch = ColorSwatch(self)

        self.clearButton = QButton(self)


class ExtendedDiagramElementEditor(DiagramElementEditor):

    def __init__(self,parent,manager=None):
        DiagramElementEditor.__init__(self,parent,manager)

        self.frame1 = QGroupBox(3,Qt.Vertical,"Style",self)
        self.frame1.setGeometry(QRect(40,60,200,100))

        self.box1 = QHBox(self.frame1)
        QLabel("Fill color:",self.box1)
        self.fillSwatch = ColorSwatch(self.box1)
        #self.layout1.addWidget(self.box1)

        self.box2 = QHBox(self.frame1)
        QLabel("Stroke color:",self.box2)
        self.strokeSwatch = ColorSwatch(self.box2)
        #self.layout1.addWidget(self.box2)

        self.box3 = QHBox(self.frame1)
        QLabel("Font color:",self.box3)
        self.fontColorSwatch = ColorSwatch(self.box3)
        #self.layout1.addWidget(self.box3)

        self.frame1.updateGeometry()
        self.frame1.show()

        for sw in (self.fillSwatch,self.strokeSwatch,self.fontColorSwatch):
            self.connect(sw,PYSIGNAL('colorSet'),self._changeAndApply)


    def focusOn( self, element ):
        DiagramElementEditor.focusOn(self,element)

        if element:
            fill = None
            stroke = None
            font = None
            for prop in element.property:
                if unicode(prop.key) in ("fill","BackgroundColor"):
                    fill = unicode(prop.value)
                elif unicode(prop.key) in ("stroke","ForegroundColor"):
                    stroke = unicode(prop.value)
                elif unicode(prop.key) in ("font-color","FontColor"):
                    font = unicode(prop.value)
            self.fillSwatch.setColor(fill)
            self.strokeSwatch.setColor(stroke)
            self.fontColorSwatch.setColor(font)


    def apply( self ):
        if not self.edited:
            return

        DiagramElementEditor.apply(self)

        coral.getTransactionManager().beginModifyCmd()
        try:
            self._setPresentationProperty("fill",self.fillSwatch.getColor())
            self._setPresentationProperty("BackgroundColor",self.fillSwatch.getColor())
            self._setPresentationProperty("stroke",self.strokeSwatch.getColor())
            self._setPresentationProperty("ForegroundColor",self.strokeSwatch.getColor())
            self._setPresentationProperty("font-color",self.fontColorSwatch.getColor())
            self._setPresentationProperty("FontColor",self.fontColorSwatch.getColor())
            
        finally:
            coral.getTransactionManager().endModifyCmd()

        self.setEdited(0)
        

    def _changeAndApply( self ):
        self.changed()
        self.apply()

    def _setPresentationProperty( self, key, value ):
        props = self.focus.property.select(lambda e:e.key==key)
        if value==None:
            # remove the property
            for p in props:
                self.focus.property.drop(p)
        else:
            # change the property
            foundit = False
            for p in props:
                foundit = True
                p.value = value
            if not foundit:
                self.focus.property.add(DI.Property(key=key,value=value))
            


class ExtendedContainedEditor(ContainedEditor):

    def __init__(self,parent,manager):
        CompositePropertyEditor.__init__(self,parent,manager)


    def getSubEditors(self):
        return [ExtendedDiagramElementEditor,]
