# -*- coding: ISO-8859-1 -*- """ capellaScript -- Copyright (c) 2006 Peter Becker >>> FaulenzerZaehler : Faulenzer durchnummerieren. Die zu nummerierenden Faulenzer mit dem Cursor markieren, und dann das Script aufrufen|| Bitte nur aufeinanderfolgende Faulenzer markieren !|| Bei Problemen bitte eine Mail an peter_becker@freenet.de <<< Tags : 2086-60 History: 25.09.06 - Erste Ausgabe 07.02.07 - bessere Positionierung des Counts 04.12.07 - freie Schriftwahl """ import xml.dom, tempfile import tkFont import Tkinter from caplib.capDOM import ScoreChange from xml.dom.minidom import NodeList, Node, Element doc = [] # parentNode von score def latin1_e(u): return u.encode('Latin-1') 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 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 addCount(score,chord,count): global doc, Font, Hoehe, Schriftschnitt 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-60': 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',Font) font.setAttribute('height',str(Hoehe)) font.setAttribute('pitchAndFamily','0') if Schriftschnitt == 0: weight = '0' elif Schriftschnitt == 1: weight = '0' font.setAttribute('italic','true') elif Schriftschnitt == 2: weight = '700' elif Schriftschnitt == 3: weight = '700' font.setAttribute('italic','true') font.setAttribute('weight',weight) text.setAttribute('y','-2.5') text.setAttribute('x','0.1') if int(count) >= 10: text.setAttribute('x','-0.3125') basic = addNewElementNode(drawObj,'basic') basic.setAttribute('tag','2086-60') #messageBox('ADD',str(score) + '\n' + str(chord) + '\n' + str(count)) content.appendChild(score.parentNode.createTextNode(str(count))) return def changeDoc(score): global doc, startcnt 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) if objList.length <= selvon[3]: ix = ix + 1 return obj = objList[selvon[3]+ix] ix = ix + 1 dos = findElementNode(obj,'drawObjects') if dos <> 'NONE': objList2 = getElementObjects(dos.childNodes) doix = 0 while doix < len(objList2): objList3 = getElementObjects(objList2[doix].childNodes) cont = findElementNode(objList3[0],'content') doix = doix+1 #messageBox('CONT',str(objList3) + '\n' + str(cont)) if cont <> 'NONE': # RTF überspringen if cont.firstChild.nodeValue == 'W': addCount(score, obj, str(startcnt)) startcnt = startcnt + 1 return def getFonts(): root = Tkinter.Tk() FontListT= tkFont.families() FontList = [] FontList2 = [] i=0 while i < len(FontListT): FontList += [latin1_e(FontListT[i])] i+=1 j=0 while j < len(FontList): FontName = FontList[j] FontList[j] = FontName[0].upper() + FontName[1:] j+=1 FontList.sort() root.destroy() return FontList def pqDialog(FontList): global startcnt, Hoehe, Font, Schriftschnitt sc = '2' # Startcount options = ScriptOptions() opt = options.get() Font = 'Times New Roman' Hoehe = '8' Schriftschnitt = '0' if opt: Font = opt.get('FontTyp',Font) Hoehe = opt.get('FontHigh',Hoehe) Schriftschnitt = opt.get('FontWeight',Schriftschnitt) FontList = [Font] + FontList Schriftschnitte = ['Standard','Kursiv','Fett','Fett Kursiv'] placeholder1 = Label(' ',width=1) placeholder2 = Label(' ',width=1) placeholder3 = Label(' ',width=1) placeholder4 = Label(' ',width=1) lablSc = Label('Zähler für ersten Faulenzer ',width=6) editSc = Edit (sc,width=10) subboxSc = HBox([lablSc,editSc,placeholder4],padding=0) box1 = HBox([subboxSc,placeholder4],padding=4) radSchnitt = Radio(Schriftschnitte, value=int(opt.get('Schriftschnitt',str(Schriftschnitt))), padding = 0) editHeight = Edit(Hoehe, width=4, min = 1, max = 99) comboFont = ComboBox(FontList, width=20, value=0) box1ba = HBox([radSchnitt], padding = 0,text='Schriftschnitt') box1bb = VBox([editHeight], padding = 0,text='') box1bb = HBox([box1bb, placeholder2], padding = 5,text='Schriftgröße') box1bc = VBox([comboFont], padding = 0,text='') box1bc = HBox([box1bc], padding = 50,text='Schriftart') box1bd = HBox([box1ba,box1bb], padding = 8, text = '') box1be = VBox([box1bd, box1bc], padding = 8,text='') box1b = HBox([box1be], padding = 8,text='Schrift') mainboxSc = VBox([box1],text='Faulenzerzähler') box = VBox([mainboxSc,box1b,placeholder1],padding=1) dlg = Dialog('FaulenzerZaehler', box) if dlg.run(): startcnt = int(editSc.value()) Hoehe = editHeight.value() Font = FontList[comboFont.value()] Schriftschnitt = radSchnitt.value() opt = dict( FontTyp = str(Font), FontHigh = str(Hoehe), FontWeight = str(Schriftschnitt) ) options.set(opt) return True else: return False # Hauptprogramm: from caplib.capDOM import ScoreChange import tempfile class ScoreChange(ScoreChange): def changeScore(self, score): changeDoc(score) if activeScore(): FontList = getFonts() if pqDialog(FontList): global tempInput, tempOutput activeScore().registerUndo("FaulenzerZaehler") tempInput = tempfile.mktemp('.capx') tempOutput = tempfile.mktemp('.capx') activeScore().write(tempInput) ScoreChange(tempInput, tempOutput) activeScore().read(tempOutput) os.remove(tempInput) os.remove(tempOutput)