# -*- coding: ISO-8859-1 -*-
""" capellaScript -- 06.02.2005 Andreas Herzog
>>> Notentext

    Mit diesem Skript kann der Notentext einer Zeile bzw. aller Zeilen bequem formatiert werden.|
    
    
        |
    
	Version 1.1: Möglichkeit der Positionierung über / unter der Zeile, neue Fontauswahlmöglichkeit
        |
	
<<<

# Version 1.0a: Kleine Fehlerkorrektur
# Version 1.1: Möglichkeit der Positionierung über / unter der Zeile, neue Fontauswahlmöglichkeit
# Version 1.1a: Fehlerkorrektur Tkinter-Modul wird nun wieder geschlossen

"""

import xml.dom
import string
import tkFont
import Tkinter

Schriftschnitte = ['unverändert','Standard','Kursiv','Fett','Fett Kursiv']
Farben = ['- unverändert -','Schwarz','Dunkelrot','Grün','Olivgrün','Marineblau','Purpur', 'Blaugrün','Grau','Silber', 'Rot', 'Gelbgrün', 'Gelb','Blau', 'Violett', 'Aquamarin','Weiß']
FarbenNummer = ['000000','800000', '008000','808000','000080','800080','008080','808080','C0C0C0','FF0000','00FF00','FFFF00','0000FF','FF00FF','00FFFF','FFFFFF']
maxVoice = 0
Voice1Sel = 0
Voice2Sel = 0
Voice3Sel = 0
Voice4Sel = 0
Voice5Sel = 0
Voice6Sel = 0

def latin1_e(u):
    return u.encode('Latin-1')
def latin1_d(u):
    return u.decode('Latin-1')



def getDialogValues1(FontList):
	 
    global Auswahl, Farbe, Schriftschnitt, Striche, Hoehe, Font, FirstLine, LineDist, FontTyp
       
    lab1 = Label(' ', width = 1)
    labFarbe = Label('Farbe', width = 16)
    labFont = Label('Schriftart', width = 16)
    labFirstLine = Label('vor der ersten Strophe:   ', width = 6)
    labDistance = Label('zwischen den Strophen: ', width = 6)
    labNotenstufen = Label('Notenstufen', width = 16)
    labHeight = Label('00 = unverändert')
    
    radVoices = Radio(['Alle Zeilen','nur Zeile mit Cursor'], value=1, padding = 8)
    radSchriftschnitt = Radio(Schriftschnitte, value=0, padding = 0)
    radStriche = Radio(['unverändert','ohne Striche','Unterstrichen','Durchgestrichen','Durch- und Unterstrichen'], value = 0, padding = 0)	
    radPosition = Radio(['über der Zeile','unter der Zeile'], value=1, padding = 8)
    
    editFirstLine= Edit('00', width=4, min = 0, max = 99)
    editLineDist = Edit('00', width=4, min = 0, max = 99)
    editHeight = Edit('00', width=4, min = 0, max = 99)
    editFont = Edit('unverändert',width=25)
    comboColour = ComboBox(Farben, width=14, value=0)
    comboFont = ComboBox(FontList, width=20, value=0)
    
    box1a= VBox([radStriche,labFarbe, comboColour], padding = 8,text='')    
    box1a= HBox([box1a], padding = 8,text='Effekt')    
    box1ba= HBox([radSchriftschnitt], padding = 0,text='Schriftschnitt')    
    box1bb= VBox([editHeight, labHeight ], padding = 0,text='')
    box1bb= HBox([box1bb, lab1 ], padding = 0,text='Schriftgrad')
    box1bc= VBox([labFont, comboFont], padding = 8,text='')
    box1bd= HBox([box1ba,box1bb, lab1], padding = 8, text = '') 
    box1be= VBox([box1bd, box1bc], padding = 8,text='')
    box1b= HBox([box1be], padding = 8,text='Schriftart')    
    box1da= HBox([labFirstLine, editFirstLine, labNotenstufen], padding = 4,text='')   	    
    box1db= HBox([labDistance,  editLineDist, labNotenstufen], padding = 4,text='')  
    box1dc= VBox([box1da,box1db],padding = 0) 	    
    box1dd= VBox([box1dc, labHeight], padding = 4,text='')   	    
    box1ea = HBox([radPosition], text = 'Position:')
    box1eb = HBox([radVoices], text = 'Bearbeiten:')
    box1e = HBox([box1ea, box1eb], padding = 4)
    box1d= HBox([box1dd], padding = 8,text='Abstände')   	    

    vbox2  = VBox([box1b,box1a,box1d,box1e], text='', padding=8)
    
    dlg = Dialog('Notentext formatieren:', vbox2)

    if dlg.run():

        Auswahl = radVoices.value()
        Farbe = comboColour.value()
        Striche = radStriche.value()
        Hoehe = editHeight.value()
        Font = FontList[comboFont.value()]
        #FontTyp = comboFontTyp.value()
        #messageBox('Test',Font)
        Schriftschnitt = radSchriftschnitt.value()
        
        if radPosition.value() == 0:
        	FirstLine = '-' + editFirstLine.value()
        if radPosition.value() == 1:
        	FirstLine = editFirstLine.value()        	
        LineDist = editLineDist.value()
        return True
    else:
        return False
		
def getDialogValues2():
    global Voice1Sel, Voice2Sel, Voice3Sel, Voice4Sel, Voice5Sel, Voice6Sel
	
    CheckVoice1 = CheckBox('1.Stimme', padding = 2, value = 1)
    CheckVoice2 = CheckBox('2.Stimme', padding = 2, value = 1)
    CheckVoice3 = CheckBox('3.Stimme', padding = 2, value = 1)
    CheckVoice4 = CheckBox('4.Stimme', padding = 2, value = 1)
    CheckVoice5 = CheckBox('5.Stimme', padding = 2, value = 1)
    CheckVoice6 = CheckBox('6.Stimme', padding = 2, value = 1)
    	
    lab1 = Label(' ', width = 1)
    if maxVoice == 2:
    	vbox  = VBox([CheckVoice1, CheckVoice2, lab1], text='', padding=8)
    if maxVoice == 3:
    	vbox  = VBox([CheckVoice1, CheckVoice2, CheckVoice3, lab1], text='', padding=8)
    if maxVoice == 4:
    	vbox  = VBox([CheckVoice1, CheckVoice2, CheckVoice3, CheckVoice4, lab1], text='', padding=8)
    if maxVoice == 5:
    	vbox  = VBox([CheckVoice1, CheckVoice2, CheckVoice3, CheckVoice4, CheckVoice5, lab1], text='', padding=8)
    if maxVoice == 6:
    	vbox  = VBox([CheckVoice1, CheckVoice2, CheckVoice3, CheckVoice4, CheckVoice5, CheckVoice6, lab1], text='', padding=8)    	    	    	    	
    dlg = Dialog('Stimmen auswählen: ', vbox)

    if dlg.run():
        Voice1Sel = CheckVoice1.value()
        Voice2Sel = CheckVoice2.value()
        Voice3Sel = CheckVoice3.value()
        Voice4Sel = CheckVoice4.value()
        Voice5Sel = CheckVoice5.value()
        Voice6Sel = CheckVoice6.value()
  
        return True
    else:
        return False
					
def handleVoice(voice):
    global Auswahl, Farbe, Schriftschnitt, Striche, Hoehe, Font, FirstLine, LineDist
    global Voice1Sel, Voice2Sel, Voice3Sel, Voice4Sel, Voice5Sel, Voice6Sel

    
    lyricsSettings = voice.getElementsByTagName('lyricsSettings')[0]
    font = lyricsSettings.getElementsByTagName('font')[0]
    
    if Farbe <> 0:																# Setzen der Farbe
    	font.setAttribute('color',FarbenNummer[Farbe-1])
    if Schriftschnitt <> 0:														# Kursiv, Fett, beides oder nichts
    	if Schriftschnitt == 1:
    		font.setAttribute('italic','')
    		font.setAttribute('weight','') 
    	if Schriftschnitt == 2:
    		font.setAttribute('italic','true')
    		font.setAttribute('weight','')    		
    	if Schriftschnitt == 3:
    		font.setAttribute('italic','')
    		font.setAttribute('weight','700')         
    	if Schriftschnitt == 4:
    		font.setAttribute('italic','true')
    		font.setAttribute('weight','700')   
    if Striche <> 0:															# Unterstriche bzw. Durchgestrochen
    	if Striche == 1:
    		font.setAttribute('underline','')    	
    		font.setAttribute('strikeOut','')    	    		
    	if Striche == 2:
    		font.setAttribute('underline','true')   
    		font.setAttribute('strikeOut','')    	
    	if Striche == 3:
    		font.setAttribute('underline','')    	    		
    		font.setAttribute('strikeOut','true')    	    		    		
    	if Striche == 4:
    		font.setAttribute('underline','true')    	    		
    		font.setAttribute('strikeOut','true')    	    		    		
    if Hoehe <> '00':    		    											# Schriftgröße
    	font.setAttribute('height',Hoehe)	
    if Font <> 'unverändert':    		    									# Font
    	font.setAttribute('pitchAndFamily','') 
    	font.setAttribute('charSet',str(1)) 
    	font.setAttribute('face',Font) 
    if FirstLine <> '00': 								    					# Abstand zur ersten Zeile
    	lyricsSettings.setAttribute('firstLine',str(string.atof(FirstLine)/2))	    	
    if LineDist <> '00':    		    										# Distance
    	lyricsSettings.setAttribute('lineDist',str(string.atof(LineDist)/2))	    	
    	
def getCursor():
    sel = curSelection()
    result = None
    if sel == 0:
        messageBox('Fehler', 'keine aktive Partitur')
        return result
    result = sel[0]
    return result

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()
    
    return FontList


def changeDoc(score):
    global maxVoice
    
    FontList = ['unverändert']
    FontList += getFonts()
	

    sel = getCursor()															# Beschreibung der Zeile mit Cursor
    if sel == None:
    	return
    else:
    	systemSel = score.getElementsByTagName('system')[sel[0]]
    	staffSel = systemSel.getElementsByTagName('staff')[sel[1]]
    	layoutSel = staffSel.getAttribute('layout')

	staffs = score.getElementsByTagName('staff')

        for staff in staffs:
         if staff.getAttribute('layout') == layoutSel:
         	voiceCounter = 0
         	for voice in staff.getElementsByTagName('voice'):
         		voiceCounter += 1
         	if voiceCounter > maxVoice:
         		maxVoice = voiceCounter
        		
    if getDialogValues1(FontList):
    	if (maxVoice == 1) or (maxVoice > 1 and getDialogValues2()):
    	 for staff in staffs:
          if staff.getAttribute('layout') == layoutSel or Auswahl == 0:        
            voiceCounter = 0
            for voice in staff.getElementsByTagName('voice'):
           	  voiceCounter += 1
           	  if maxVoice == 1 or Auswahl == 0:
           	  	handleVoice(voice)
           	  if maxVoice > 1:
           	  	if (voiceCounter == 1 and Voice1Sel == 1) or (voiceCounter == 2 and Voice2Sel == 1) or (voiceCounter == 3 and Voice3Sel == 1) or (voiceCounter == 4 and Voice4Sel == 1) or (voiceCounter == 5 and Voice5Sel == 1) or (voiceCounter == 6 and Voice6Sel == 1):
           	  			handleVoice(voice)
           	  	
    	

# Hauptprogramm:

from caplib.capDOM import ScoreChange
import tempfile

class ScoreChange(ScoreChange):
    def changeScore(self, score):
        changeDoc(score)

if activeScore():
    activeScore().registerUndo("Notentexter")
    tempInput = tempfile.mktemp('.capx')
    tempOutput = tempfile.mktemp('.capx')
    activeScore().write(tempInput)

    ScoreChange(tempInput, tempOutput)

    activeScore().read(tempOutput)
    os.remove(tempInput)
    os.remove(tempOutput)

