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

import qt
import coral
DI=coral.lang.XMIDI20

def __interactiveSetColor(xmidi_elements,
                          property1,
                          property2,
                          default_triple,
                          dialog_str):
    """Asks user for the property1/property2 values with QColorDialog
    and sets it for @xmidi_elements, optionally creating the property
    if required."""
    if len(xmidi_elements) == 0: return

    p1 = None
    p2 = None
    color = None
    for e in xmidi_elements:
        for p in e.property:
            if p.key == property1:
                p1 = p
            if p.key == property2:
                p2 = p
            ## Give priority to second choice
            if p2:
                break
        if p2:
            color = qt.QColor(str(p2.value))
        elif p1:
            color = qt.QColor(str(p1.value))
        if color:
            break
    if not color:
        color = qt.QColor(default_triple[0], default_triple[1], default_triple[2])
        
    c = qt.QColorDialog.getColor(color, None, dialog_str)
    if not c.isValid():
        return
    
    tm = coral.getTransactionManager()
    tm.beginModifyCmd()
    try:
        for e in xmidi_elements:
            p1 = None
            p2 = None
            for p in e.property:
                if p.key == property1:
                    p1 = p
                if p.key == property2:
                    p2 = p
                if p1 and p2:
                    break
            if not p1:
                p1 = DI.Property()
                p1.key = property1
                e.property.append(p1)
            if not p2:
                p2 = DI.Property()
                p2.key = property2
                e.property.append(p2)
            p1.value = "#%02x%02x%02x" % (c.red(), c.green(), c.blue())
            p2.value = "#%02x%02x%02x" % (c.red(), c.green(), c.blue())
    finally:
        tm.endModifyCmd()
                          

def interactiveSetBGColor(xmidi_elements):
    """Asks user for "fill" property value with QColorDialog
    and sets it for @xmidi_elements, optionally creating the property
    if required."""
    __interactiveSetColor(xmidi_elements,
                          "fill",
                          "BackgroundColor",
                          (255, 255, 194),
                          'Choose Background Color')

def interactiveSetFGColor(xmidi_elements):
    """Asks user for "stroke" property value with QColorDialog
    and sets it for @xmidi_elements, optionally creating the property
    if required."""
    __interactiveSetColor(xmidi_elements,
                          "stroke",
                          "ForegroundColor",
                          (0, 0, 0),
                          'Choose Foreground Color')

def interactiveSetFontColor(xmidi_elements):
    """Asks user for "stroke" property value with QColorDialog
    and sets it for @xmidi_elements, optionally creating the property
    if required."""
    __interactiveSetColor(xmidi_elements,
                          "font-color",
                          "FontColor",
                          (0, 0, 0),
                          'Choose Font Color')
