# -*- coding: ISO-8859-1 -*-
""" capellaScript -- (c) Paul Villiger
>>> System ausblenden

    Mit diesem Skript wird das aktuelle System an der Cursorposition weiss eingefärbt:
    Notenzeilen, Systemklammern, feste Taktstriche, Schlüssel, Tonart, Takt, Noten, Pausen|
    Folgende Elemente werden nicht eingefärbt: Automatische Taktstriche, Halte-, Bindebögen, Text- und Grafikobjekte.|
    Ist das System bereits ausgeblendet (weisse Notenzeile), so wird es wieder schwarz eingefärbt.
     
<<<

History:  26.06.2007 - Erste Ausgabe
          04.07.2007 - Kommentar ergänzt

"""

import xml.dom, new
from xml.dom.minidom import parseString, NodeList, Node, Element

doc = parseString('<score/>')

def gotoChild(self, name, new=False):
    newEl = None
    if new:
        pass
    else:
        for child in self.childNodes:
            if child.nodeType == child.ELEMENT_NODE and child.tagName == name:
                newEl = child
                break
    if newEl == None:
        newEl = doc.createElement(name)
        self.appendChild(newEl)
    return newEl
Node.gotoChild = new.instancemethod(gotoChild,None,Node)
                    
def getCursor():
    sel = curSelection()
    result = None
    if sel == 0:
        messageBox('Fehler', 'keine aktive Partitur')
        return result
    result = sel
    return result
    
def changeDoc(score):
    newColor = 'FFFFFF'     # weiss
    selectedSystem = sel[0][0]
    actualSystem = 0
    for system in score.getElementsByTagName('system'):
        if actualSystem == selectedSystem:
            
            # Farbe umkehren wenn Notenlinien bereits weiss sind
            for staff in system.getElementsByTagName('staff'):
                if staff.hasAttribute('lineColor'):
                    lineColor = staff.getAttribute('lineColor')
                    if lineColor == newColor:
                        newColor = '000000'     # schwarz

            system.setAttribute('bracketColor',newColor)
            for staff in system.getElementsByTagName('staff'):
                staff.setAttribute('lineColor',newColor)
            for clefSign in system.getElementsByTagName('clefSign'):
                clefSign.setAttribute('color',newColor)
            for keySign in system.getElementsByTagName('keySign'):
                keySign.setAttribute('color',newColor)
            for timeSign in system.getElementsByTagName('timeSign'):
                timeSign.setAttribute('color',newColor)
            for barline in system.getElementsByTagName('barline'):
                barline.setAttribute('color',newColor)
            for chord in system.getElementsByTagName('chord'):
                display = chord.gotoChild('display')
                display.setAttribute('color',newColor)
            for rest in system.getElementsByTagName('rest'):
                display = rest.gotoChild('display')
                display.setAttribute('color',newColor)
            break

        actualSystem += 1        
        

# Hauptprogramm:

from caplib.capDOM import ScoreChange
import tempfile

class ScoreChange(ScoreChange):
    def changeScore(self, score):
        changeDoc(score)


if activeScore():
    sel = getCursor()
    if sel <> None:
        activeScore().registerUndo("System_ausblenden")
        tempInput = tempfile.mktemp('.capx')
        tempOutput = tempfile.mktemp('.capx')
        activeScore().write(tempInput)
        
        ScoreChange(tempInput, tempOutput)

        activeScore().read(tempOutput)
        os.remove(tempInput)
        os.remove(tempOutput)

