# -*- coding: ISO-8859-1 -*-
""" capellaScript -- 06.04.2005 Andreas Herzog
>>> Fingersätze

    Mit diesem Skript Fingersätze einfügen.|
    |
    
    |
    Version 1.2c    |
    
    |
    
        |

<<<


"""
# Version 1.0b: Ursprungsversion
# Version 1.1: Haltebogen für stummen Fingerwechsel eingefügt
# Verison 1.2: Handwechselzeichen möglich
# Verison 1.2c: 15.03.08 Bindebögen hinter die Noten (Vlg) 

import xml.dom
import string


from xml.dom.minidom import NodeList


ZahlenDarstellung = [' ','1','2','3','4','5','_']
ZahlenCode = [' ','1','2','3','4','5','_']
FingerFontGroessen = ['3','4','5','6','7','8','9','10','11','12','13','14','15','16']
BogenStaerken = ['1fach', '2fach', '3fach','4fach','5fach','6fach']
BogenCode = ['0.03125','0.0625','0.09375','0.125','0.15625','0.1875']
LineStaerken = ['0.5fach', '1fach', '1.5fach','2fach','2.5fach','3.0fach', '3.5fach', '4fach','4.5fach','5fach']
SchriftArten = ['Arial','ComicSans','Garamond','TimesNewRoman']

# doc = [] # parentNode von score

def latin1_e(u):
    return u.encode('Latin-1')
def latin1_d(u):
    return u.decode('Latin-1')
    
class settings:
    def __init__(self):
        self.HandwechselX = -0.5
        self.HandwechselY = 0.75
        self.FingersatzX = 0.5   
        self.FingersatzY = 7.0
        self.FingerSpaltenAbstand = 2.0
        self.ZeilenAbstand = 1.0
        self.FingerUeberUnter = 1
        self.FingerFontGroesse = 4
        self.BogenStaerke = 1
        self.NichtUnterUeber = 1
        self.FingerGruppierung = 0
        self.BogenAbstand = 0.1
        self.HandwechselStaerke = 1
  
     
defaults = settings()
dlgSet = settings()

options = ScriptOptions() 
opt = options.get()


def getOptions():
    global FingersatzX, FingersatzY, FingerSpaltenAbstand, ZeilenAbstand, BogenAbstand, FingerUeberUnter, FingerFontGroesse, BogenStaerke, StrichAbstand, NichtUnterUeber, FingerGruppierung
    global HandwechselY, HandwechselX, HandwechselStaerke
    HandwechselStaerke = eval(opt.get('HandwechselStaerke', str(HandwechselStaerke)))
    HandwechselX    = eval(opt.get('HandwechselX',str(HandwechselX)))
    HandwechselY    = eval(opt.get('HandwechselY',str(HandwechselY)))
    FingersatzX    = eval(opt.get('FingersatzX',str(FingersatzX)))
    FingersatzY    = eval(opt.get('FingersatzY',str(FingersatzY)))
    FingerSpaltenAbstand  = eval(opt.get('FingerSpaltenAbstand',str(FingerSpaltenAbstand)))
    ZeilenAbstand   = eval(opt.get('ZeilenAbstand',str(ZeilenAbstand)))
    FingerUeberUnter    = eval(opt.get('FingerUeberUnter',str(FingerUeberUnter)))
    FingerFontGroesse = eval(opt.get('FingerFontGroesse',str(FingerFontGroesse)))
    BogenStaerke = eval(opt.get('BogenStaerke',str(BogenStaerke)))
    NichtUnterUeber = eval(opt.get('NichtUnterUeber',str(NichtUnterUeber)))
    FingerGruppierung = eval(opt.get('FingerGruppierung', str(FingerGruppierung)))
    BogenAbstand = eval(opt.get('BogenAbstand', str(BogenAbstand)))


def setOptions():
    global dlgSet
    opt.update(dict(HandwechselStaerke    = str(HandwechselStaerke),
                    HandwechselX        = str(HandwechselX),
                    HandwechselY        = str(HandwechselY),
                    FingersatzX         = str(FingersatzX),
                    FingersatzY         = str(FingersatzY),
                    FingerSpaltenAbstand  = str(FingerSpaltenAbstand),
                    ZeilenAbstand   = str(ZeilenAbstand),
                    FingerUeberUnter    = str(FingerUeberUnter),
                    FingerFontGroesse     = str(FingerFontGroesse),
                    BogenStaerke = str(BogenStaerke),
                    NichtUnterUeber = str(NichtUnterUeber),
                    FingerGruppierung = str(FingerGruppierung),
                    BogenAbstand = str(BogenAbstand)
                    
                    
                    ))
    options.set(opt)
    
def addElementNode(el,tagName):
    # add new Node to el if Node "tagName" does not exist
    # otherwise return the existing Node
    global doc
    childs = el.childNodes
    for n in range(childs.length):
        if childs[n].nodeType ==childs[n].ELEMENT_NODE and childs[n].tagName == tagName:
            return childs[n]
    newChild = doc.createElement(tagName)
    el.appendChild(newChild)
    return newChild
    

def addNewElementNode(el,tagName):
    # add new Node with tagName "tagName" to el 
    global doc
    newChild = doc.createElement(tagName)
    el.appendChild(newChild)
    return newChild

    
def getDialogValues1():
    
    global FingerUeberUnter, FingersatzY, FingersatzX, NurLoeschen, Handwechsel, ZeilenAbstand, NichtUnterUeber
    global FingerSpaltenAbstand, FingerGruppierung, FingerFontGroesse, GesamtePartitur, BogenStaerke, BogenHoehe, BogenAbstand, BogenStaerke
    global Ton11, Ton12, Ton13, Ton21, Ton22, Ton23, Fuss, FingersatzSchriftart
    global StummerWechsel1, StummerWechsel2, StummerWechsel3
    global HandwechselY, HandwechselX, HandwechselBreite, HandwechselLaenge, HandwechselStaerke
    

    rad1 = Radio(['über der Notenzeile', 'unter der Notenzeile'], text='', padding = 8, value=FingerUeberUnter)
    rad2 = Radio(['keine Handwechsel', 'Handwechsel links / oben',  'Handwechsel links / unten', 'Handwechsel rechts / oben','Handwechsel rechts / unten'], text='Handwechsel', padding = 8, value=Handwechsel)
    edit1  = Edit (str(ZeilenAbstand), text = 'Zeilenabstand', width=4)
    edit2  = Edit (str(FingersatzY), text = 'Abstand von der Mittelstimme', width=4)
    edit3  = Edit (str(FingersatzX), text = 'Korrektur der horizontalen Lage', width=4)
    edit4  = Edit (str(FingerSpaltenAbstand), text = 'Spaltenabstand', width=4)
    edit5  = Edit (str(BogenHoehe), text = 'Bogenhöhe', width=4)
    edit6  = Edit (str(BogenAbstand), text = 'Vertikaler Abstand des Bogens', width=4)
    
    edit7  = Edit (str(HandwechselY), text = 'Vertikaler Abstand zur Note', width=4)
    edit8  = Edit (str(HandwechselX), text = 'Korrektur der horizontalen Lage', width=4)
    edit9  = Edit (str(HandwechselBreite), text = 'Breite des Handwechselzeichens', width=4)
    edit10  = Edit (str(HandwechselLaenge), text = 'Höhe des Handwechselzeichens', width=4)
    
    lableer = Label(' ', width = 4)
    lab0a = Label('Fontgröße:   ', width = 4)
    lab0b = Label('Bogenstärke: ', width = 4)
    lab0c = Label('Linienstärke:  ', width = 4)
    lab0d = Label('Schriftart:', width = 4)
    lab1 = Label('Zeilenabstand', width = 4)
    
    lab2 = Label('Abstand von der Mittelstimme', width = 4)
    lab3 = Label('Korrektur der horizontalen Lage', width = 4)
    lab4 = Label('Spaltenabstand', width = 4)
    lab5 = Label('Bogenhöhe', width = 4)
    lab6 = Label('Vertikaler Abstand des Bogens', width = 4)
    
    lab7 = Label('Vertikaler Abstand zur Note', width = 4)
    lab8 = Label('Korrektur der horizontalen Lage', width = 4)
    lab9 = Label('Breite des Handwechselzeichens', width = 4)
    lab10 = Label('Höhe des Handwechselzeichens', width = 4)

    NurLoeschenCheck = CheckBox('Nur Elemente dieses Skripts löschen!', value=0)
    FingerGruppierungCheck = CheckBox('Elemente gruppieren', value=FingerGruppierung)
    GesamtePartiturCheck = CheckBox('Formatierung für die gesamte Partitur übernehmen', value=0)
    NichtUnterUeberCheck = CheckBox('Position über/unter der Notenzeile belassen', value=NichtUnterUeber)
    FingerFontGroesseBox = ComboBox(FingerFontGroessen, value=FingerFontGroesse, width=5)
    BogenStaerkeBox = ComboBox(BogenStaerken, value=(BogenStaerke), width=8)
    LineStaerkeBox = ComboBox(LineStaerken, value=(HandwechselStaerke-1), width=8)
    SchriftartenBox = ComboBox(SchriftArten, value=(FingersatzSchriftart-1), width=8)

    
    Ton11Box = ComboBox(ZahlenDarstellung, value=Ton11, width=5)
    Ton12Box = ComboBox(ZahlenDarstellung, value=Ton12, width=5)
    Ton13Box = ComboBox(ZahlenDarstellung, value=Ton13, width=5)
    
      
    hbox11 = HBox([Ton11Box], padding = 3)
    hbox12 = HBox([Ton12Box], padding = 3)
    hbox13 = HBox([Ton13Box], padding = 3)
    
    
    Ton21Box = ComboBox(ZahlenDarstellung, value=Ton21, width=5)
    Ton22Box = ComboBox(ZahlenDarstellung, value=Ton22, width=5)
    Ton23Box = ComboBox(ZahlenDarstellung, value=Ton23, width=5)
    
    hbox21 = HBox([Ton21Box], padding = 3)
    hbox22 = HBox([Ton22Box], padding = 3)
    hbox23 = HBox([Ton23Box], padding = 3)
    
    BogenCheck1 = CheckBox('Stummer Wechsel', value=StummerWechsel1)    
    BogenCheck2 = CheckBox('Stummer Wechsel', value=StummerWechsel2)    
    BogenCheck3 = CheckBox('Stummer Wechsel', value=StummerWechsel3)    
    
    vbox1a = VBox([hbox11, hbox12, hbox13], padding = 16)
    vbox1b = VBox([hbox21, hbox22, hbox23], padding = 16)
    vbox1ca = VBox([BogenCheck1,BogenCheck2,BogenCheck3], padding = 16)
    vbox1c = VBox([lableer, vbox1ca], padding = 8)
    
    hbox2a = HBox([vbox1a], padding = 8,text = '1.Spalte')
    hbox2b = HBox([vbox1b], padding = 8,text = '2.Spalte')
    hbox2c = HBox([vbox1c], padding = 8,text = '')
    hbox2d = HBox([rad2], padding = 8,text = '')
      
    hbox3a = HBox([hbox2a, hbox2b, hbox2c], padding = 8, text = 'Fingersätze')
    hbox3 = HBox([hbox3a, rad2], padding = 8)
    hbox4 = HBox([edit1, lab1], padding = 8)    
    hbox5 = HBox([edit2, lab2], padding = 8)
    hbox6 = HBox([edit3, lab3], padding = 8)
    hbox7 = HBox([edit4, lab4], padding = 8)
    hbox8 = HBox([edit5, lab5], padding = 8)
    hbox9 = HBox([edit6, lab6], padding = 8)
    hbox21 = HBox([edit7, lab7], padding = 8)
    hbox22 = HBox([edit8, lab8], padding = 8)
    hbox23 = HBox([edit9, lab9], padding = 8)
    hbox24 = HBox([edit10, lab10], padding = 8)
    
    hbox10a = HBox([lab0a, FingerFontGroesseBox], padding = 8)
    hbox10b = HBox([lab0b, BogenStaerkeBox], padding = 8)
    hbox10c = HBox([lab0c, LineStaerkeBox], padding = 8)
    hbox10d = HBox([lab0d, SchriftartenBox], padding = 8)
    vbox10a =  VBox([hbox10a], padding = 8)
    vbox10b =  VBox([hbox10b, hbox10c], padding = 8)
    
    vbox11 = VBox([NurLoeschenCheck, FingerGruppierungCheck, GesamtePartiturCheck, NichtUnterUeberCheck], padding = 8)
    hbox11 = HBox([vbox11], padding = 8, text = 'Formatierungsoptionen')
    vbox12a = VBox([hbox5, hbox4, hbox6, hbox7, hbox8, hbox9], padding = 8)
    vbox12b = VBox([hbox21, hbox22, hbox23,hbox24], padding = 8)
    hbox12a = HBox([vbox12a], padding = 8, text = 'Abstände der Fingersätze')
    hbox12b = HBox([vbox12b], padding = 8, text = 'Abstände der Handwechselzeichen')
    hbox12 = HBox([hbox12a, hbox12b], padding = 8)
    hbox13 =  HBox([rad1, vbox10a, vbox10b], padding = 8)
    hbox14 = HBox([hbox13], padding = 8, text = 'Position, Schriftgröße und Linienstärken' )
    
    
    lab1 = Label('Zwischenräume', width = 4)
    vbox2= VBox([ hbox3,  hbox12, hbox11, hbox14], padding = 8)
    
    dlg = Dialog('Bitte wählen: ', vbox2)

    
    if dlg.run():
        FingerUeberUnter = rad1.value()
        ZeilenAbstand = string.atof(edit1.value())
        FingersatzY = string.atof(edit2.value())
        FingersatzX = string.atof(edit3.value())
        FingerSpaltenAbstand= string.atof(edit4.value())
        BogenHoehe= string.atof(edit5.value())
        BogenAbstand= string.atof(edit6.value())
        HandwechselY = string.atof(edit7.value())
        HandwechselX = string.atof(edit8.value())
        HandwechselBreite = string.atof(edit9.value())
        HandwechselLaenge = string.atof(edit10.value())
        NurLoeschen = NurLoeschenCheck.value()
        FingerGruppierung = FingerGruppierungCheck.value()
        Handwechsel = rad2.value()
        FingerFontGroesse = FingerFontGroesseBox.value()
        BogenStaerke = BogenStaerkeBox.value()
        HandwechselStaerke = LineStaerkeBox.value() + 1
        GesamtePartitur = GesamtePartiturCheck.value()
        NichtUnterUeber = NichtUnterUeberCheck.value()
        Ton11 = Ton11Box.value()
        Ton12 = Ton12Box.value()
        Ton13 = Ton13Box.value()
        Ton21 = Ton21Box.value()
        Ton22 = Ton22Box.value()
        Ton23 = Ton23Box.value()
        StummerWechsel1 = BogenCheck1.value()
        StummerWechsel2 = BogenCheck2.value()
        StummerWechsel3 = BogenCheck3.value()
        if Ton11 == 0 and Ton12 == 0 and Ton13 == 0 and Handwechsel == 0:
            return False
        else: 
            return True
    else:
        return False

def newLine(drawObjAncer, width1, width2, height1, height2, IndexTag):
    global HandwechselStaerke
    drawObj = addNewElementNode(drawObjAncer,'drawObj')
    line = addNewElementNode(drawObj,'line')
    line.setAttribute('x1',width1)   
    line.setAttribute('x2',width2)
    line.setAttribute('y1',height1)
    line.setAttribute('y2',height2)
    line.setAttribute('lineWidth',str(0.05 * HandwechselStaerke))
    GraficalTag = '56294-16'+IndexTag
    basic = addElementNode(line,'basic')                            
    basic.setAttribute('tag',GraficalTag) 
    basic.setAttribute('vertAlign','1')
        
        
def newText(drawObjAncer,  width, height, contents, IndexTag):
    global FingerGruppierung, FingerFontGroesse, BogenStaerke, StrichAbstand
    
    drawObj = addNewElementNode(drawObjAncer,'drawObj')
        
    text = addNewElementNode(drawObj,'text')    
        
    text.setAttribute('x',width)
    text.setAttribute('y',height)

    content = addNewElementNode(text,'content')
    textNode = doc.createTextNode(contents)
    content.appendChild(textNode)
    text.setAttribute('align','center')
    GraficalTag = '56294-16'+IndexTag
    basic = addElementNode(drawObj,'basic')                            
    basic.setAttribute('tag',GraficalTag)    

    font = addNewElementNode(text,'font')
    font.setAttribute('face','TimesNewRoman')
    font.setAttribute('color','')
    font.setAttribute('height',FingerFontGroessen[FingerFontGroesse])
    font.setAttribute('charSet','1')
    font.setAttribute('pitchAndFamily','2')     

    return text

def newSlur(drawObjAncer,  width1, width2,width3, width4, height1, height2, height3, height4, IndexTag, midWidth, visible):
    global FingerGruppierung, FingerFontGroesse, SlurWidth, StrichAbstand
    

    drawObj = addNewElementNode(drawObjAncer,'drawObj')
        
    slur = addNewElementNode(drawObj,'slur')    
    slur.setAttribute('x1',width1)   
    slur.setAttribute('x2',width2)
    slur.setAttribute('x3',width3)   
    slur.setAttribute('x4',width4)
    slur.setAttribute('y1',height1 )
    slur.setAttribute('y2',height2 )
    slur.setAttribute('y3',height3 )
    slur.setAttribute('y4',height4 )
    if visible == 0:
        slur.setAttribute('color','FFFFFF')
    if visible == 1:
        slur.setAttribute('color','000000')
    form = addNewElementNode(slur,'form')        
    form.setAttribute('midWidth',midWidth)    
    
    GraficalTag = '56294-16'+IndexTag    
    basic = addNewElementNode(drawObj,'basic')                            
    basic.setAttribute('tag',GraficalTag)
    basic.setAttribute('behindNotes','true')
    
    return slur
    

def getContent(chord):                                                          # Holt die Werte einer eventuell bestehende Generalbass-
    global Ton11, Ton12, Ton13, Ton21, Ton22, Ton23                             # bezifferung zurück
    global StummerWechsel1, StummerWechsel2, StummerWechsel3, Handwechsel
    global FingersatzY, FingersatzX, FingerUeberUnter, ZeilenAbstand, FingerSpaltenAbstand, BogenStaerke, BogenHoehe, BogenAbstand
    global HandwechselY, HandwechselX, HandwechselBreite, HandwechselLaenge
    
    for drawObj in chord.getElementsByTagName('drawObj'):
        tag = '0'    
        for content in drawObj.getElementsByTagName('content'):
            ObjectContent = content.firstChild.nodeValue
        for text in drawObj.getElementsByTagName('text'):
            Textposx = round(string.atof(text.getAttribute('x')),1)
            Textposy = round(string.atof(text.getAttribute('y')),1)
        for slur in drawObj.getElementsByTagName('slur'):
            Slurposx1 = round(string.atof(slur.getAttribute('x1')),1)
            Slurposx2 = round(string.atof(slur.getAttribute('x2')),1)
            Slurposy1 = round(string.atof(slur.getAttribute('y1')),1)
            Slurposy2 = round(string.atof(slur.getAttribute('y2')),1)
            slurvisible = 1
            if slur.hasAttribute('color'):
                if slur.getAttribute('color') == '000000':
                    slurvisible = 1
                if slur.getAttribute('color') == 'FFFFFF':
                    slurvisible = 0

                    
            Slurwidth = '0.1875'
            
            for form in slur.getElementsByTagName('form'):
                Slurwidth = form.getAttribute('midWidth')
            
                
        for line in drawObj.getElementsByTagName('line'):
            Lineposx1 = round(string.atof(line.getAttribute('x1')),1)
            Lineposx2 = round(string.atof(line.getAttribute('x2')),1)
            Lineposy1 = round(string.atof(line.getAttribute('y1')),2)
            Lineposy2 = round(string.atof(line.getAttribute('y2')),2)    
            Linestaerke = 0.1
            if line.hasAttribute('lineWidth'):
                Linestaerke = line.getAttribute('lineWidth')
                        
            
        for basic in drawObj.getElementsByTagName('basic'):
            tag = basic.getAttribute('tag')
        if tag == '56294-1611':
            Ton11 = ZahlenCode.index(str(ObjectContent))
            if Textposy <= 0.0:
                FingerUeberUnter = 0
                FingersatzY = 0.0 - Textposy
            if Textposy > 0.0:
                FingerUeberUnter = 1                
                FingersatzY = Textposy
            ErsteSpalteX = Textposx
        if tag == '56294-1612':
            Ton12 = ZahlenCode.index(str(ObjectContent))
            if Textposy <= 0.0:
                ZeilenAbstand = Textposy - (FingersatzY* (-1))
            if Textposy > 0.0:
                ZeilenAbstand = Textposy -FingersatzY                
        if tag == '56294-1613':
            Ton13 = ZahlenCode.index(str(ObjectContent))
        if tag == '56294-1621':
            Ton21 = ZahlenCode.index(str(ObjectContent))
            if ErsteSpalteX < 0 and Textposx <0:
                FingerSpaltenAbstand = (ErsteSpalteX * (-1)) - (Textposx * (-1))
            if ErsteSpalteX < 0 and Textposx >=0:
                FingerSpaltenAbstand = (Textposx ) + (ErsteSpalteX * (-1))
            if ErsteSpalteX >= 0 and Textposx >=0:
                FingerSpaltenAbstand = (Textposx ) - (ErsteSpalteX)                

        if tag == '56294-1622':
            Ton22 = ZahlenCode.index(str(ObjectContent))
        if tag == '56294-1623':
            Ton23 = ZahlenCode.index(str(ObjectContent))
        if tag == '56294-1631':    
            StummerWechsel1 = slurvisible
            BogenStaerke = BogenCode.index(Slurwidth)
            BogenHoehe = Slurposy2-Slurposy1
            if FingerUeberUnter == 0:
                BogenAbstand = (FingersatzY + Slurposy1) 
            if FingerUeberUnter == 1:
                BogenAbstand = (FingersatzY - Slurposy1) * (-1)
            
        if tag == '56294-1632':    
            StummerWechsel2 = slurvisible                    
        if tag == '56294-1633':    
            StummerWechsel3 = slurvisible    
            
        if tag == '56294-1641':    
            Handwechsel = 1    
            HandwechselY = Lineposy1
            HandwechselX = Lineposx1            
            HandwechselBreite = Lineposx2-Lineposx1
        if tag == '56294-1643':    
            Handwechsel = 2
            HandwechselY = Lineposy1 * (-1)        
            HandwechselX = Lineposx1
            HandwechselBreite = Lineposx2-Lineposx1
        if tag == '56294-1645':    
            Handwechsel = 3
            HandwechselY = Lineposy1
            HandwechselX = Lineposx1         
            HandwechselBreite = Lineposx2-Lineposx1
        if tag == '56294-1647':     
            Handwechsel = 4
            HandwechselY = Lineposy1 * (-1)
            HandwechselX = Lineposx1            
            HandwechselBreite = Lineposx2-Lineposx1            
        if (tag == '56294-1642') or (tag == '56294-1644') or (tag == '56294-1646') or (tag == '56294-1648') :
            HandwechselLaenge = round(Lineposy2-Lineposy1,1)
            HandwechselStaerke = 2
            if HandwechselLaenge < 0:
                HandwechselLaenge = HandwechselLaenge * (-1)
                HandwechselStaerke = Linestaerke * 20

                        
def handleNotes(score, voice, chord):
    global FingersatzY, FingersatzX, NoteHight, ZeilenAbstand,  FingerUeberUnter, Handwechsel, TextPositionY
    global Ton11, Ton12, Ton13, Ton21, Ton22, Ton23 
    global StummerWechsel1, StummerWechsel2, StummerWechsel3
    global HandwechselY, HandwechselX, HandwechselBreite, HandwechselLaenge
    
                                                
    for drawObjects in chord.getElementsByTagName('drawObjects'):                    # Alle drawObj mit tag == GraficalTag löschen
          for drawObj in drawObjects.childNodes:
             if drawObj.nodeType == drawObj.ELEMENT_NODE and drawObj.tagName == 'drawObj':
                 for basic in drawObj.getElementsByTagName('basic'):
                     if basic.hasAttribute('tag') and basic.getAttribute('tag') == '56294-1600':
                        drawObjects.removeChild(drawObj)
              
    for drawObjects in chord.getElementsByTagName('drawObjects'):                    # Alle drawObj mit tag == GraficalTag löschen
         for drawObj in drawObjects.childNodes:
             if drawObj.nodeType == drawObj.ELEMENT_NODE and drawObj.tagName == 'drawObj':
                for basic in drawObj.getElementsByTagName('basic'):
                     if basic.hasAttribute('tag') and basic.getAttribute('tag')[0:8] == '56294-16':
                        drawObjects.removeChild(drawObj)         
                                            
         if drawObjects.getElementsByTagName('drawObj').length == 0:                    # Wenn drawObjects keine Elemente mehr enthält, dann löschen  
             drawObjects.parentNode.removeChild(drawObjects)

    drawObjs = chord.getElementsByTagName('drawObjs')
                   
    for head in chord.getElementsByTagName('head'):
                    
            if FingerUeberUnter == 0:                                        # Positionen festlegen
                TextPositionY = 0.0-FingersatzY

            if FingerUeberUnter == 1:
                TextPositionY = FingersatzY


    if NurLoeschen == 0:           
        drawObjects = addElementNode(chord,'drawObjects')                # Notennamen
        
        FingersatzX1 = str(FingersatzX)
        FingersatzX2 = str(FingersatzX + FingerSpaltenAbstand)
        FingersatzX3 = str(FingersatzX + (FingerSpaltenAbstand * 2))
        FingersatzX4 = str(FingersatzX + (FingerSpaltenAbstand * 3))
        
        FingersatzY1 = str(TextPositionY + ZeilenAbstand*0)
        FingersatzY2 = str(TextPositionY + ZeilenAbstand*1)
        FingersatzY3 = str(TextPositionY + ZeilenAbstand*2)
        
        SlurPosX1 = FingersatzX1
        SlurPosX2 = FingersatzX2
        SlurPosY1a = str(string.atof(FingersatzY1) + BogenAbstand)
        SlurPosY2a = str(string.atof(FingersatzY1) +  BogenHoehe + BogenAbstand)
        SlurPosY1b = str(string.atof(FingersatzY2) + BogenAbstand)
        SlurPosY2b = str(string.atof(FingersatzY2) +  BogenHoehe + BogenAbstand)       
        SlurPosY1c = str(string.atof(FingersatzY3) + BogenAbstand)
        SlurPosY2c = str(string.atof(FingersatzY3) +  BogenHoehe + BogenAbstand) 
        

        LinePosX1 = str(HandwechselX)
        LinePosX2 = str(HandwechselX + HandwechselBreite)
        HandwechselY1 = str(HandwechselY * -1) 
        HandwechselY2 = str(HandwechselY)
        HandwechselY3 = str(string.atof(HandwechselY1) + HandwechselLaenge)
        HandwechselY4 = str(string.atof(HandwechselY2) - HandwechselLaenge)
        
        
        
        if FingerGruppierung == 0:
            drawObjAncer = drawObjects
    
        if FingerGruppierung == 1:
            groupDrawObj = addNewElementNode(drawObjects,'drawObj')
            drawObjAncer = addNewElementNode(groupDrawObj,'group')    
            
        
        text11 = newText(drawObjAncer, FingersatzX1, FingersatzY1, ZahlenCode[Ton11], '11')
        text12 = newText(drawObjAncer, FingersatzX1, FingersatzY2, ZahlenCode[Ton12], '12')
        text13 = newText(drawObjAncer, FingersatzX1, FingersatzY3, ZahlenCode[Ton13], '13')
        
        text21 = newText(drawObjAncer, FingersatzX2, FingersatzY1, ZahlenCode[Ton21], '21')
        text22 = newText(drawObjAncer, FingersatzX2, FingersatzY2, ZahlenCode[Ton22], '22')
        text23 = newText(drawObjAncer, FingersatzX2, FingersatzY3, ZahlenCode[Ton23], '23')
        
        bogen1 = newSlur(drawObjAncer, SlurPosX1,SlurPosX1,SlurPosX2,SlurPosX2,  SlurPosY1a, SlurPosY2a, SlurPosY2a,SlurPosY1a,'31',BogenCode[BogenStaerke],StummerWechsel1)
        bogen2 = newSlur(drawObjAncer, SlurPosX1,SlurPosX1,SlurPosX2,SlurPosX2,  SlurPosY1b, SlurPosY2b, SlurPosY2b,SlurPosY1b,'32',BogenCode[BogenStaerke],StummerWechsel2)
        bogen3 = newSlur(drawObjAncer, SlurPosX1,SlurPosX1,SlurPosX2,SlurPosX2,  SlurPosY1c, SlurPosY2c, SlurPosY2c,SlurPosY1c,'33',BogenCode[BogenStaerke],StummerWechsel3)
 
        if Handwechsel == 1:
            line11 = newLine(drawObjAncer, LinePosX1, LinePosX2, HandwechselY2, HandwechselY2,'41')
            line12 = newLine(drawObjAncer, LinePosX1, LinePosX1, HandwechselY2, HandwechselY4,'42')
        if Handwechsel == 2:
            line11 = newLine(drawObjAncer, LinePosX1, LinePosX2, HandwechselY1, HandwechselY1,'43')
            line12 = newLine(drawObjAncer, LinePosX1, LinePosX1, HandwechselY1, HandwechselY3,'44')
        if Handwechsel == 3:
            line11 = newLine(drawObjAncer, LinePosX1, LinePosX2, HandwechselY2, HandwechselY2,'45')
            line12 = newLine(drawObjAncer, LinePosX2, LinePosX2, HandwechselY2, HandwechselY4,'46')           

        if Handwechsel == 4:
            line11 = newLine(drawObjAncer, LinePosX1, LinePosX2, HandwechselY1, HandwechselY1,'47')
            line12 = newLine(drawObjAncer, LinePosX2, LinePosX2, HandwechselY1, HandwechselY3,'48')                          
        if FingerGruppierung == 1:
            basic = addElementNode(groupDrawObj,'basic')                            
            basic.setAttribute('tag','56294-1600')
            basic.setAttribute('behindNotes','true')

            
def getCursor():
    sel = curSelection()
    result = None
    if sel == 0:
        messageBox('Fehler', 'keine aktive Partitur')
        return result
    result = sel[0]
    return result

def getElementObjects(objList):  # returns a List
    newList = NodeList()
    for n in range(objList.length):
        if objList[n].nodeType == objList[n].ELEMENT_NODE:
            newList.append(objList[n])
    return newList

def textChange(changeText,drawObjectTag, changeTag, changeX, changeY):
    global TextGroesse, FingerUeberUnter, NichtUnterUeber
    
    if drawObjectTag == changeTag:
        changeText.setAttribute('x',changeX)
        
        
        if NichtUnterUeber == 0: 
            changeText.setAttribute('y',changeY)        
        if NichtUnterUeber == 1: 
            if (string.atof(changeText.getAttribute('y')) >= 0 and FingerUeberUnter == 1) or (string.atof(changeText.getAttribute('y')) <= 0 and FingerUeberUnter == 0):
                changeText.setAttribute('y',changeY)
    for font in changeText.getElementsByTagName('font'):
        font.setAttribute('height',FingerFontGroessen[FingerFontGroesse])


def slurChange(changeSlur,drawObjectTag, changeTag, width1, width2,width3, width4, height1, height2, height3, height4, midWidth, visible):    
    global BogenStaerke, StrichAbstand, FingerUeberUnter, NichtUnterUeber
    
    if drawObjectTag == changeTag:
        changeSlur.setAttribute('x1',width1)   
        changeSlur.setAttribute('x2',width2)
        changeSlur.setAttribute('x3',width3)   
        changeSlur.setAttribute('x4',width4)
        if NichtUnterUeber == 0: 
            changeSlur.setAttribute('y1',height1 )
            changeSlur.setAttribute('y2',height2 )
            changeSlur.setAttribute('y3',height3 )
            changeSlur.setAttribute('y4',height4 )
        if NichtUnterUeber == 1: 
            if (string.atof(changeSlur.getAttribute('y1')) >= 0 and FingerUeberUnter == 1) or (string.atof(changeSlur.getAttribute('y1')) <= 0 and FingerUeberUnter == 0):
                changeSlur.setAttribute('y1',height1 )
                changeSlur.setAttribute('y2',height2 )
                changeSlur.setAttribute('y3',height3 )
                changeSlur.setAttribute('y4',height4 )        
        formcount = 0

        for form in changeSlur.getElementsByTagName('form'):
            formcount += 1
        if formcount ==0:
            form = addNewElementNode(changeSlur,'form')
        form.setAttribute('midWidth',midWidth)    
        
def lineChange(changeLine,drawObjectTag,changeTag, changeX1, changeX2,changeY1, changeY2):
    
    if drawObjectTag == changeTag:
        changeLine.setAttribute('x1',changeX1)   
        changeLine.setAttribute('x2',changeX2)
        changeLine.setAttribute('y1',changeY1)   
        changeLine.setAttribute('y2',changeY2)                
        changeLine.setAttribute('lineWidth',str(0.05 * HandwechselStaerke))
    
def changeAll(score):
    global FingersatzX, FingerSpaltenAbstand, AltAbstand,ZeilenAbstand, TextPositionY
    
    FingersatzX1 = str(FingersatzX)
    FingersatzX2 = str(FingersatzX + FingerSpaltenAbstand)
    FingersatzX3 = str(FingersatzX + (FingerSpaltenAbstand * 2))
    FingersatzX4 = str(FingersatzX + (FingerSpaltenAbstand * 3))
        
    FingersatzY1 = str(TextPositionY + ZeilenAbstand*0)
    FingersatzY2 = str(TextPositionY + ZeilenAbstand*1)
    FingersatzY3 = str(TextPositionY + ZeilenAbstand*2)
        
    SlurPosX1 = FingersatzX1
    SlurPosX2 = FingersatzX2
    SlurPosY1a = str(string.atof(FingersatzY1) + BogenAbstand)
    SlurPosY2a = str(string.atof(FingersatzY1) +  BogenHoehe + BogenAbstand)
    SlurPosY1b = str(string.atof(FingersatzY2) + BogenAbstand)
    SlurPosY2b = str(string.atof(FingersatzY2) +  BogenHoehe + BogenAbstand)       
    SlurPosY1c = str(string.atof(FingersatzY3) + BogenAbstand)
    SlurPosY2c = str(string.atof(FingersatzY3) +  BogenHoehe + BogenAbstand) 
        

    LinePosX1 = str(HandwechselX)
    LinePosX2 = str(HandwechselX + HandwechselBreite)
    
    HandwechselY1 = str(HandwechselY * -1) 
    HandwechselY2 = str(HandwechselY)
    HandwechselY3 = str(string.atof(HandwechselY1) + HandwechselLaenge)
    HandwechselY4 = str(string.atof(HandwechselY2) - HandwechselLaenge)
    
    for system in score.getElementsByTagName('system'):
        for drawObj in system.getElementsByTagName('drawObj'):
            for basic in drawObj.getElementsByTagName('basic'):
                drawObjectTag = basic.getAttribute('tag')
                if (drawObjectTag[0:8] == '56294-16') and (drawObjectTag <> '56294-1600'):
                    changeObject = basic.parentNode    
                    for text in changeObject.getElementsByTagName('text'):
                        changeText = changeObject.getElementsByTagName('text')[0]
                    
                        textChange(changeText,drawObjectTag, '56294-1611',FingersatzX1,FingersatzY1)
                        textChange(changeText,drawObjectTag, '56294-1612',FingersatzX1,FingersatzY2)
                        textChange(changeText,drawObjectTag, '56294-1613',FingersatzX1,FingersatzY3)

                        textChange(changeText,drawObjectTag, '56294-1621',FingersatzX2,FingersatzY1)
                        textChange(changeText,drawObjectTag, '56294-1622',FingersatzX2,FingersatzY2)
                        textChange(changeText,drawObjectTag, '56294-1623',FingersatzX2,FingersatzY3)

        


                    for slur in changeObject.getElementsByTagName('slur'):
                        changeSlur = changeObject.getElementsByTagName('slur')[0]

                        slurChange(changeSlur,drawObjectTag,'56294-1631', SlurPosX1,SlurPosX1,SlurPosX2,SlurPosX2,  SlurPosY1a, SlurPosY2a, SlurPosY2a,SlurPosY1a,BogenCode[BogenStaerke],StummerWechsel1)
                        slurChange(changeSlur,drawObjectTag,'56294-1632', SlurPosX1,SlurPosX1,SlurPosX2,SlurPosX2,  SlurPosY1b, SlurPosY2b, SlurPosY2b,SlurPosY1b,BogenCode[BogenStaerke],StummerWechsel2)
                        slurChange(changeSlur,drawObjectTag,'56294-1633', SlurPosX1,SlurPosX1,SlurPosX2,SlurPosX2,  SlurPosY1c, SlurPosY2c, SlurPosY2c,SlurPosY1c,BogenCode[BogenStaerke],StummerWechsel3)
                    
                    for line in changeObject.getElementsByTagName('line'):
                        changeLine = changeObject.getElementsByTagName('line')[0]
                        if Handwechsel == 1:
                            lineChange(changeLine,drawObjectTag,'56294-1641', LinePosX1, LinePosX2, HandwechselY2, HandwechselY2)
                            lineChange(changeLine,drawObjectTag,'56294-1642', LinePosX1, LinePosX1, HandwechselY2, HandwechselY4)
                        if Handwechsel == 2:
                            lineChange(changeLine,drawObjectTag,'56294-1643', LinePosX1, LinePosX2, HandwechselY1, HandwechselY1)
                            lineChange(changeLine,drawObjectTag,'56294-1644', LinePosX1, LinePosX1, HandwechselY1, HandwechselY3)
                        if Handwechsel == 3:
                            lineChange(changeLine,drawObjectTag,'56294-1645', LinePosX1, LinePosX2, HandwechselY2, HandwechselY2)
                            lineChange(changeLine,drawObjectTag,'56294-1646', LinePosX2, LinePosX2, HandwechselY2, HandwechselY4)           
                        if Handwechsel == 4:
                            lineChange(changeLine,drawObjectTag,'56294-1647', LinePosX1, LinePosX2, HandwechselY1, HandwechselY1)
                            lineChange(changeLine,drawObjectTag,'56294-1648', LinePosX2, LinePosX2, HandwechselY1, HandwechselY3) 

                    
            
                #messageBox('Inhalt', drawObjectTag[8])

def changeDoc(score):
    global Ton11, Ton12, Ton13, Ton21, Ton22, Ton23, Ton31, Ton32, Ton33
    global HandwechselY, HandwechselX, HandwechselBreite, HandwechselLaenge, HandwechselStaerke
    global ZeilenAbstand, FingersatzY, FingersatzX, FingerUeberUnter, BogenHoehe, FingerSpaltenAbstand, BogenAbstand
    global FingerFontGroesse, GesamtePartitur, BogenStaerke, StrichAbstand, NichtUnterUeber, FingerGruppierung, FingersatzSchriftart
    global StummerWechsel1, StummerWechsel2, StummerWechsel3, Slurwidth, Handwechsel
    

    
    Slurwidth = '0.03125'
    BogenStaerke = 1
    FingerFontGroesse = 4
    Ton11 = 0
    Ton12 = 0
    Ton13 = 0
    Ton21 = 0
    Ton22 = 0
    Ton23 = 0
    
    StummerWechsel1 = 0
    StummerWechsel2 = 0
    StummerWechsel3 = 0
    FingersatzSchriftart = 3
    HandwechselY = 0.75
    HandwechselX = -0.5
    HandwechselBreite = 2.0
    HandwechselLaenge = 4.0
    Handwechsel = 0
    HandwechselStaerke = 1
    NichtUnterUeber = 1    
    FingersatzX = 0.5
    FingersatzY = 7.0
    FingerSpaltenAbstand = 2.0
    ZeilenAbstand = 1.0
    FingerUeberUnter = 1
    GesamtePartitur = 0
    BogenHoehe = 0.3
    BogenAbstand = 0.1
    FingerGruppierung = 0
    getOptions()
    
    sel = getCursor()
    if sel == None:
        #
        return
    else:
        system = score.getElementsByTagName('system')[sel[0]]
        staff = system.getElementsByTagName('staff')[sel[1]]
        voice = staff.getElementsByTagName('voice')[sel[2]]
        noteObject = voice.getElementsByTagName('noteObjects')[0]
        objList = getElementObjects(noteObject.childNodes)
        if objList.length <= sel[3]:
            return

        
        obj = objList[sel[3]]
        if obj.tagName == 'chord':
            getContent(obj)
            if getDialogValues1():
                handleNotes(score, voice, obj)
                setOptions()

    if GesamtePartitur == 1:
        changeAll(score)
        
# Hauptprogramm:

from caplib.capDOM import ScoreChange
import tempfile

class ScoreChange(ScoreChange):
    
    def changeScore(self, score):
       global doc
       doc = score.parentNode  
       changeDoc(score)
        
        

if activeScore():
    
    activeScore().registerUndo("Fingersätze")
    tempInput = tempfile.mktemp('.capx')
    tempOutput = tempfile.mktemp('.capx')
    activeScore().write(tempInput)
    ScoreChange(tempInput, tempOutput)
    activeScore().read(tempOutput)
    os.remove(tempInput)
    os.remove(tempOutput)

