# -*- coding: ISO-8859-1 -*-
""" capellaScript -- (c) Paul Villiger
>>> Scope Editor

Mit diesem Skript kann die Gültigkeit (Scope) für den Stimmenauszug oder das Vorspiel für ausgewählte Objekte angepasst werden.||
Weitere Informationen im Skripthandbuch unter wiki.sins942.ch
    
<<<

 26.09.2010 - Erste Ausgabe
 25.01.2011 - Bereichswahl Notenzeile oder ganze Partitur

"""
typeDefinitionSingle = [
    ('transposable', 'Transponierbare Symbole'),
    ('guitar', 'Gitarrenakkord'),
    ('text', 'Einfachtext'),
    ('richText', 'Textfeld'),
    ('slur', 'Bindebogen'),
    ('wedge', 'Crescendo/Decrescendo'),
    ('trill', 'Triller'),
    ('wavyLine', 'Schlangenlinie'),
    ('volta', 'Voltenklammer'),
    ('bracket', 'Triolenklammer'),
    ('octaveClef', 'Oktavklammer'),
    ('notelines', 'Notenlinie'),
    ('polygon', 'Polygon'),
    ('ellipse', 'Ellipse'),
    ('rectangle', 'Rechteck'),
    ('line', 'Linie'),
    ('group', 'Gruppe')
    ]
     
typeDefinitionGroup = [
    ('allSymbols', 'Alle Objekte'),
    ('dynamicSymbol', 'Dynamiksymbole'),
    ('trillSymbol', 'Trillersymbole'),
    ('articulationSymbol','Artikulationszeichen (Vortrag)'),
    ('geometricalObjects','Geometrische Objekte'),
    ('musicalSymbols', 'Musikalische Symbole')
    ]

scopeDefinition = [
    ('voice','Stimme'),
    ('staff','Notenzeile'),
    ('brace','Geschweifte Systemklammer'),
    ('bracket','Eckige Systemklammer'),
    ('system','Ganzes System')
    ]



staffList = activeScore().voiceList()
staffIndex = 0

sel = curSelection()
(sy,st,vo,no) = sel[0]
system = activeScore().system(sy)
staff = system.staff(st)
   
# Aktueller Eintrag im Mustersystem Bestimmen
i = 0
for descr in staffList:
    if staff.index() == system.staffIndexFromDescr(descr):
        staffIndex = i
    i += 1




def handleDialog():
    staffList = activeScore().voiceList()
    staffIndex = 0

    sel = curSelection()
    (sy,st,vo,no) = sel[0]
    system = activeScore().system(sy)
    staff = system.staff(st)
       
    # Aktueller Eintrag im Mustersystem Bestimmen
    i = 0
    for descr in staffList:
        if staff.index() == system.staffIndexFromDescr(descr):
            staffIndex = i
        i += 1
    staffList = ['Ganze Partitur'] + staffList
    staffIndex += 1


    execString = "dlg = Dialog('-- Scope Editor --',"
    execString += "VBox(["
    execString += "HBox(["
    execString += "VBox(["
    execString += "Label('-- Gültigkeit (Vorspiel/Stimmenauszug) --', width=30), Label(''),"

    radString = "_scope = Radio(["
    for key, value in scopeDefinition:
        radString += "'%s'," % value
    radString = radString[:-1]
    radString += "], value=0)"    # Radio
    exec(radString)
    
    execString += "_scope"

    execString += ",Label(''),Label(''), Label('-- Gruppen --'), Label('')"

    for key, value in typeDefinitionGroup:
        exec("_%s = CheckBox('', value = False)" % key)
        execString += ",HBox([_%s, Label('%s')]) " % (key, value)

    execString += "]) "  # VBox

    execString += ",VBox([Label('-- Einzelelemente --', width=30), Label('')"
    for key, value in typeDefinitionSingle:
        exec("_%s = CheckBox('', value = False)" % key)
        execString += ",HBox([_%s, Label('%s')]) " % (key, value)

    execString += ",Label('')"
    
    execString += "]) "  # VBox
    execString += "]) "  # HBox

    execString += ",Label('--- Bereichswahl   (Notenzeile / Ganze Partitur)---')"
    execString += ",Label('')"
    exec("selStaff = ComboBox(%s, value = %s, width = 40)" % (staffList, staffIndex))
    execString += ",selStaff"
    execString += ",Label('')"

    execString += "]) "  # VBox
    execString += ")"    # Dialog

    exec(execString)

    if dlg.run():
        selStaff = selStaff.value()
        if selStaff == 0:
            staffName = ''
        else:
            staffName = staffList[selStaff]
            

        scope, value = scopeDefinition[_scope.value()]
        if scope == 'voice':
            scope = ''
        for key, value in typeDefinitionSingle:
            if eval("_%s.value()" % key):
                exec("handleSingleElement('%s', scope, staffName)" % key)
        for key, value in typeDefinitionGroup:
            if eval("_%s.value()" % key):
                exec("handle_%s(scope, staffName)" % key)
                
       
def handleSingleElement(ty, scope, staffName):
    for system in activeScore().systems():
        staffIndex = system.staffIndexFromDescr(staffName)
        for staff in system.staves():
            if staffName and staff.index() <> staffIndex:
                continue 
            for no in staff.noteObjs():
                for i in range(no.nDrawObjs()):
                    do = no.drawObj(i)
                    if do['type'] == ty:
                        do['scope'] = scope
                        no.replaceDrawObj(i, do)
                    
def handle_geometricalObjects(scope, staffName):
    for system in activeScore().systems():
        staffIndex = system.staffIndexFromDescr(staffName)
        for staff in system.staves():
            if staffName and staff.index() <> staffIndex:
                continue 
            for no in staff.noteObjs():
                for i in range(no.nDrawObjs()):
                    do = no.drawObj(i)
                    if do['type'] in ['polygon', 'ellipse', 'rectangle', 'line']:
                        do['scope'] = scope
                        no.replaceDrawObj(i, do)

def handle_allSymbols(scope, staffName):                
    for system in activeScore().systems():
        staffIndex = system.staffIndexFromDescr(staffName)
        for staff in system.staves():
            if staffName and staff.index() <> staffIndex:
                continue 
            for no in staff.noteObjs():
                for i in range(no.nDrawObjs()):
                    do = no.drawObj(i)
                    do['scope'] = scope
                    no.replaceDrawObj(i, do)
    
def handle_dynamicSymbol(scope, staffName):
    for system in activeScore().systems():
        staffIndex = system.staffIndexFromDescr(staffName)
        for staff in system.staves():
            if staffName and staff.index() <> staffIndex:
                continue 
            for no in staff.noteObjs():
                for i in range(no.nDrawObjs()):
                    do = no.drawObj(i)
                    if do['type'] == 'text':
                        font = do['font']
                        if 'capella' in font['face']:
                            if do['content'][0] in ('f','g','h','i','j','p','q','r','s','z','{','|','Ô','Õ','Ö','×'):
                                do['scope'] = scope
                                no.replaceDrawObj(i, do)

def handle_trillSymbol(scope, staffName):
    for system in activeScore().systems():
        staffIndex = system.staffIndexFromDescr(staffName)
        for staff in system.staves():
            if staffName and staff.index() <> staffIndex:
                continue 
            for no in staff.noteObjs():
                for i in range(no.nDrawObjs()):
                    do = no.drawObj(i)
                    if do['type'] == 'text':
                        font = do['font']
                        if 'capella' in font['face']:
                            if do['content'][0] in ('l','t','w','x','½','¾','¿','À','Á','Â','Ã','Ä','Å','Æ','Ç','Ð','Ñ','Ò','Ó'):
                                do['scope'] = scope
                                no.replaceDrawObj(i, do)

def handle_articulationSymbol(scope, staffName):
    for system in activeScore().systems():
        staffIndex = system.staffIndexFromDescr(staffName)
        for staff in system.staves():
            if staffName and staff.index() <> staffIndex:
                continue 
            for no in staff.noteObjs():
                for i in range(no.nDrawObjs()):
                    do = no.drawObj(i)
                    if do['type'] == 'text':
                        font = do['font']
                        if 'capella' in font['face']:
                            if do['content'][0] in 'u.ÉÍÊË³Z¶¹kÈÎÏÌµ~Y·¸ab¬­»¼V²WX':
                                do['scope'] = scope
                                no.replaceDrawObj(i, do)

def handle_musicalSymbols(scope, staffName):
    for system in activeScore().systems():
        staffIndex = system.staffIndexFromDescr(staffName)
        for staff in system.staves():
            if staffName and staff.index() <> staffIndex:
                continue 
            for no in staff.noteObjs():
                for i in range(no.nDrawObjs()):
                    do = no.drawObj(i)
                    if do['type'] in ['volta', 'slur', 'bracket', 'wedge', 'guitar', 'notelines', 'octaveClef', 'trill', 'wavyLine']:
                        do['scope'] = scope
                        no.replaceDrawObj(i, do)

if activeScore():
    activeScore().registerUndo('Scope Editor')
    handleDialog()                       
    






