# -*- coding: ISO-8859-1 -*- """ capellaScript -- Copyright (c) 2010 Peter Becker >>> AlteVorzeichen : Vorzeichen an markierten Noten unsichtbar machen und klein über die Note schreiben || Bei Problemen bitte eine Mail an peter_becker@freenet.de <<< Tags : 2086-62 History: 22.04.10 - Erste Ausgabe """ import xml.dom, tempfile import tkFont import Tkinter from caplib.capDOM import * from xml.dom.minidom import NodeList, Node, Element doc = [] # parentNode von score def getCursor(): sel = curSelection() result = None if sel == 0: messageBox('Fehler', 'keine aktive Partitur') return result return sel 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 findElementNode(el,tagName): 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] return 'NONE' def addElementNode(el,tagName): # Neue Node zu "el" hinzufügen wenn Node "tagName" nicht existiert # ansonsten existierende Node zurückmelden 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 hasAlter(pi,obj): # Hat die Note ein Vorzeichen ? takt1 = [] list0 = [ ] list1 = ['F'] list2 = ['F','C'] list3 = ['F','C','G'] list4 = ['F','C','G','D'] list5 = ['F','C','G','D','A'] list6 = ['F','C','G','D','A','E'] list7 = ['F','C','G','D','A','E','B'] listM1 = ['B'] listM2 = ['B','E'] listM3 = ['B','E','A'] listM4 = ['B','E','A','D'] listM5 = ['B','E','A','D','G'] listM6 = ['B','E','A','D','G','C'] listM7 = ['B','E','A','D','G','C','F'] key = obj.curKey() if key == 0: liste = list0 elif key == 1: liste = list1 elif key == 2: liste = list2 elif key == 3: liste = list3 elif key == 4: liste = list4 elif key == 5: liste = list5 elif key == 6: liste = list6 elif key == 7: liste = list7 elif key == -1: liste = listM1 elif key == -2: liste = listM2 elif key == -3: liste = listM3 elif key == -4: liste = listM4 elif key == -5: liste = listM5 elif key == -6: liste = listM6 elif key == -7: liste = listM7 if pi[0] in liste: # Ton hat in der Tonart normalerweise ein Vorzeichen #messageBox('HasAlter','norm VZ') vz = 0 else: vz = 1 #messageBox('HasAlter','kein VZ') return vz def setVorzeichen(score,chord,step,vorz,pi,no): global doc, size vz = hasAlter(pi,no) #messageBox('SV',str(vz) + '\n' + str(step)) if vz == 0 and step <> '0' and vorz == 1: return #messageBox('VZ',str(vz)) drawObjects = addElementNode(chord,'drawObjects') drawObjectL = getElementObjects(drawObjects.childNodes) ix = 0 while ix < len(drawObjectL): basic = findElementNode(drawObjectL[ix],'basic') if basic <> 'NONE' and basic.hasAttribute('tag'): tag = basic.getAttribute('tag') if tag == '2086-62': drawObjectL[ix].parentNode.removeChild(drawObjectL[ix]) ix = ix+1 drawObj = addNewElementNode(drawObjects,'drawObj') text = addNewElementNode(drawObj,'text') font = addNewElementNode(text,'font') content = addNewElementNode(text,'content') font.setAttribute('face','capella3') font.setAttribute('height',str(size)) font.setAttribute('charSet','2') font.setAttribute('pitchAndFamily','2') text.setAttribute('y','-4') text.setAttribute('x','0.5') #messageBox('STEP',str(step)) if step == '1': #messageBox('STEP1',str(step)) content.appendChild(score.parentNode.createTextNode('S')) elif step == '-1': #messageBox('STEP2',str(step)) content.appendChild(score.parentNode.createTextNode('Q')) elif step == '0': #messageBox('STEP3',str(step)) content.appendChild(score.parentNode.createTextNode('R')) basic = addNewElementNode(drawObj,'basic') basic.setAttribute('tag','2086-62') return def changeDoc(score): global doc, vorz #messageBox('vorz',str(vorz)) doc = score.parentNode sel = getCursor() if sel == None: # return else: selvon = sel[0] selbis = sel[1] ix = 0 while selvon[3]+ix < selbis[3]: system = score.getElementsByTagName('system')[selvon[0]] staff = system.getElementsByTagName('staff')[selvon[1]] voice = staff.getElementsByTagName('voice')[selvon[2]] noteObject = voice.getElementsByTagName('noteObjects')[0] objList = getElementObjects(noteObject.childNodes) sy = activeScore().system(selvon[0]) st = sy.staff(selvon[1]) vo = st.voice(0) no = vo.noteObj(selvon[3]+ix) if objList.length <= selvon[3]: ix = ix + 1 return obj = objList[selvon[3]+ix] hd = obj.getElementsByTagName('head') pi = hd[0].getAttribute('pitch') ix = ix + 1 #messageBox('OBJ',str(obj.tagName)) if obj.tagName == 'chord': alter = obj.getElementsByTagName('alter') if alter : step = alter[0].getAttribute('step') if step: #messageBox('Step1',str(step)) alter[0].setAttribute('display','suppress') if vorz == 2: alter[0].setAttribute('step','1') step = '1' elif vorz == 3: alter[0].setAttribute('step','-1') step = '-1' elif vorz == 4: alter[0].removeAttribute('step') step = '0' setVorzeichen(score, obj, step, vorz, pi, no) else: #messageBox('Step1a',str(step)) display = alter[0].getAttribute('display') #messageBox('Step1a',str(display)) if display == 'force': force = 'force' else: force = 'none' #if display <> 'auto': # alter[0].setAttribute('display','suppress') #messageBox('Step2',str(step) + '\n' + str(alter[0])) if (vorz == 0 or vorz == 1) and force and not step: step = '0' elif vorz == 2: alter[0].setAttribute('step','1') step = '1' elif vorz == 3: alter[0].setAttribute('step','-1') step = '-1' elif vorz == 4: if alter[0].hasAttribute('step'): alter[0].removeAttribute('step') step = '0' setVorzeichen(score, obj, step, vorz, pi, no) else: head = obj.getElementsByTagName('head') alter = addNewElementNode(head[0],'alter') alter.setAttribute('display','suppress') if vorz <> 0 and vorz <> 1: if vorz == 2: step = '1' alter.setAttribute('step','1') elif vorz == 3: step = '-1' alter.setAttribute('step','-1') elif vorz == 4: if alter.hasAttribute('step'): alter.removeAttribute('step') step = '0' #messageBox('Step3',str(step) + '\n' + str(obj)) setVorzeichen(score, obj, step, vorz, pi, no) else: #messageBox('STEP4a','kein Vorzeichen gefunden') #messageBox('STEP4b',str(pi) + '\n' + str(no)) vz = hasAlter(pi,no) # hat der Ton normalerweise ein Vorzeichen ? #messageBox('STEP4c',str(vz)) if vz == 0: step = '0' setVorzeichen(score, obj, step, vorz, pi, no) else: return return def pqDialog(): global vorz, size options = ScriptOptions() opt = options.get() #messageBox('OPT',str(opt)) Sopt = ['8','9','10', '11', '12', '13'] #comboS = ComboBox(Sopt, width=12, value=1) comboS = ComboBox(Sopt, width=12, value=int(opt.get('size', '1'))) Aopt = ['Vorzeichen ersetzen - auch tonartbedingte Vorzeichen','Vorzeichen ersetzen - ohne tonartbedingte Vorzeichen','Kreuz setzen - Ton wird gegebenenfalls erhöht','b setzen - Ton wird gegebenenfalls vermindert','Vorzeichen auflösen - vorhandenes Vorzeichen wird gegebenenfalls entfernt'] #radioA = Radio(Aopt, value=('vorz', 0)) radioA = Radio(Aopt, value=int(opt.get('vorz', 0))) hBox1 = HBox([Label('Zeichengröße '), comboS]) box = VBox([radioA, Label(' '), hBox1, Label(' ')]) dlg = Dialog('Aktion für markierte Noten', box) if dlg.run(): vorz = radioA.value() size = comboS.value() opt.update(dict(size=size, vorz=vorz)) options.set(opt) size = size+8 return True else: return False # Hauptprogramm: from caplib.capDOM import ScoreChange import tempfile class ScoreChange(ScoreChange): def changeScore(self, score): changeDoc(score) if activeScore(): global tempInput, tempOutput if pqDialog(): activeScore().registerUndo("AlteVorzeichen") tempInput = tempfile.mktemp('.capx') tempOutput = tempfile.mktemp('.capx') activeScore().write(tempInput) ScoreChange(tempInput, tempOutput) activeScore().read(tempOutput) os.remove(tempInput) os.remove(tempOutput)