# -*- coding: ISO-8859-1 -*-7
""" capellaScript -- (c) Paul Villiger
>>> Leeres Notenbatt

    Diese Skript erzeugt ein leeres Notenblatt. Folgende Einstellungen sind möglich:|
    - Seitenränder|
    - Notenlinienabstand|
    - Anzahl Notenzeilen|
    - Schlüssel: Violin-,  Alt-, Bass-, Schlagzeug- und kein Schlüssel  |
    - 5- oder 1-zeilige Notenzeilen |
    - Landscape/Portrait (Blatt muss zuvor gespeichert werden|
    |
    Die Papiergrösse muss vor dem Aufruf in der Druckereinstellung festgelegt werden. |
    |
    Das Programm kann sooft durchlaufen werden, bis die Einstellungen stimmen. Die Einstellungen werden nicht zwischengespeichert.
    
    <<<
History:  23.01.2004 Erstausgabe
          07.02.2010 Fehlerhafter Layouteintrag
"""
landscape  = 0 # 'false'
pageTop    = '20'
pageBottom = '20'
pageLeft   = '20'
pageRight  = '20'

linesNormal = '1.8'
linesSmall  = '0.8'

pageJustified = 'all'

notelines  = '5'

stavesNumber = 12
clefSigns  = ['treble','alto','bass','P3','N3'] 
clef       = 4 # Kein Schlüssel

terminate = False



def getDialog():
    global landscape, pageTop, pageBottom, pageLeft, pageRight, linesNormal, notelines, stavesNumber, clef, terminate

    lab10   = Label(' ',width = 5, padding = 8)
    lab1    = Label('Oben', width = 5, padding = 8)
    edit1   = Edit(pageTop, width = 5,padding = 8)
    hbox1   = HBox([lab10, lab1, edit1])
    
    lab2    = Label('Unten', width = 5, padding = 8)
    edit2   = Edit(pageBottom, width = 5,padding = 8)
    hbox2   = HBox([lab10, lab2, edit2])

    lab3    = Label('Links', width = 5, padding = 8)
    edit3   = Edit(pageLeft, width = 5,  padding = 8)
    lab4    = Label('  Rechts', width = 5, padding = 8)
    edit4   = Edit(pageRight, width = 5,  padding = 8)
    hbox34  = HBox([lab3, edit3, lab10, edit4, lab4])
    

    lab5    = Label('Zeilenhöhe', width = 11, padding = 8)
    edit5   = Edit(linesNormal, padding = 8)
    hbox5   = HBox([lab5, edit5])

    lab7    = Label('Schlüssel', width = 10, padding = 8)
    cobox7  = ComboBox(['Violinschlüssel',
                        'Altschlüssel',
                        'Bassschlüssel',
                        'Schlagzeugschlüssel',
                        'Kein Schlüssel'], value = clef, width = 20, padding = 8)
    hbox7  = HBox([lab7, cobox7], padding = 8)

    lab8    = Label('Notenzeilen', width = 10, padding = 8)
    cobox8  = ComboBox(['5-Linien', '1-Linie'],width = 20, value = notelines , padding = 8)
    hbox8  = HBox([lab8, cobox8], padding = 8)

    lab9    = Label('Anzahl Zeilen', width = 10, padding = 8)
    edit9   = Edit(str(stavesNumber), padding = 8)
    hbox9   = HBox([lab9, edit9], padding = 8)

    lab6    = Label('Landscape', width = 11, padding = 8)
    chbox6  = CheckBox('', value = landscape,  padding = 8)
    hbox6   = HBox([lab6, chbox6])
        
    lab11    = Label('** Beenden**', width = 11, padding = 8)
    chbox11  = CheckBox('   (erst beenden wenn Seite stimmt)', value = 0,  padding = 8)
    hbox11   = HBox([lab11, chbox11])
    
    vbox  = VBox([hbox1, hbox34, hbox2, hbox5, hbox9, hbox7, hbox8, hbox6, hbox11, lab10], padding = 8)

    dlg = Dialog('--- Leeres Notenblatt ---', vbox)

    if dlg.run():
        if chbox11.value() == 1:
            terminate = True
            return True
        pageTop    = edit1.value()
        pageBottom = edit2.value()
        pageLeft   = edit3.value()
        pageRight  = edit4.value()
        landscape  = chbox6.value()
        notelines  = cobox8.value()
        linesNormal = edit5.value()
        clef       = cobox7.value()
        stavesNumber = eval(edit9.value())
        return True
    else:
        terminate = True
        return False
    

import new
from xml.dom.minidom import Node, Element

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 changeDoc(score):
    # **** LAYOUT ****
    layout = score.gotoChild('layout')
    layout.parentNode.removeChild(layout).unlink()
    layout = score.gotoChild('layout')

    pages = layout.gotoChild('pages') # Seitenraender
    pages.setAttribute('landscape',['false','true'][landscape])
    pages.setAttribute('top',pageTop)
    pages.setAttribute('left',pageLeft)
    pages.setAttribute('right',pageRight)
    pages.setAttribute('bottom',pageBottom)

    distances = layout.gotoChild('distances')

    staffLines = distances.gotoChild('staffLines')
    staffLines.setAttribute('small',linesSmall)
    staffLines.setAttribute('normal',linesNormal)
    staffLines.setAttribute('pageObj','1.8')

    systems = distances.gotoChild('systems')
    systems.setAttribute('top','0')
    systems.setAttribute('between','0')
    systems.setAttribute('pageJustified',pageJustified)

    instrumentNames = layout.gotoChild('instrumentNames')
    
    font = instrumentNames.gotoChild('font')
    font.setAttribute('face','Arial')
    font.setAttribute('height','12')

    staves = layout.gotoChild('staves')

    staffLayout = staves.gotoChild('staffLayout')
    staffLayout.setAttribute('description','Stimme_1')

    notation = staffLayout.gotoChild('notation')
    notation.setAttribute('defaultClef','treble')
    notation.setAttribute('notelines',['5','1'][notelines])

    barlines = notation.gotoChild('barlines')
    barlines.setAttribute('mode','internal')
    barlines.setAttribute('from','3')
    barlines.setAttribute('to','7')

    distances = staffLayout.gotoChild('distances')
    distances.setAttribute('top','2')
    distances.setAttribute('bottom','2')

    instrument = staffLayout.gotoChild('instrument')
    instrument.setAttribute('name','')
    instrument.setAttribute('abbrev','')

    sound = staffLayout.gotoChild('sound')
    sound.setAttribute('instr','0')
    sound.setAttribute('volume','80')
    
    spacing = layout.gotoChild('spacing')
    spacing.setAttribute('rel','75')        
    spacing.setAttribute('abs','16')        

    beamFlattening = layout.gotoChild('beamFlattening')
    beamFlattening.setAttribute('from','49')
    beamFlattening.setAttribute('to','29')
    beamFlattening.setAttribute('fromMax','148')
    beamFlattening.setAttribute('toMax','55')

    # **** SYSTEMS ****
    systems = score.gotoChild('systems') 
    systems.parentNode.removeChild(systems).unlink()
    
    systems = score.gotoChild('systems') 
    
    system = systems.gotoChild('system')
    system.setAttribute('leftIndent','0')
    system.setAttribute('instrNotation','long')
    system.setAttribute('justified','true')

    staves = system.gotoChild('staves')
    
    staff = staves.gotoChild('staff')
    staff.setAttribute('defaultTime','infinite')
    staff.setAttribute('layout','Stimme_1')

    voices = staff.gotoChild('voices')

    voice = voices.gotoChild('voice')

    lyricsSettings = voice.gotoChild('lyricsSettings')
    lyricsSettings.setAttribute('lineDist','1.5')
    lyricsSettings.setAttribute('firstLine','5')

    font = lyricsSettings.gotoChild('font')
    font.setAttribute('height','12')
    font.setAttribute('face','Arial')

    noteObjects = voice.gotoChild('noteObjects')

    clefSign = noteObjects.gotoChild('clefSign')
    clefSign.setAttribute('clef', clefSigns[clef])

    timeSign = noteObjects.gotoChild('timeSign')
    timeSign.setAttribute('time','infinite')

    for i in range(stavesNumber-1):
        newSystem = system.cloneNode(True)
        systems.appendChild(newSystem)


from caplib.capDOM import ScoreChange
import tempfile

class ScoreChange(ScoreChange):

    def changeScore(self, score):
        global doc
        doc = score.parentNode
        changed = False
        if getDialog():
            changeDoc(score)
            changed = True
        return changed    
        
# Hauptprogramm:

if activeScore():

    while not terminate:
        tempInput = tempfile.mktemp('.capx')
        tempOutput = tempfile.mktemp('.capx')
        activeScore().write(tempInput)
    
        if ScoreChange(tempInput, tempOutput):
            activeScore().registerUndo("Triolen_Klammern_Fix")
            activeScore().read(tempOutput)

        os.remove(tempInput)
        os.remove(tempOutput)   
