2009-04-06 18:45:45 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4
|
|
|
|
"""
|
|
|
|
OpenLP - Open Source Lyrics Projection
|
|
|
|
Copyright (c) 2008 Raoul Snyman
|
|
|
|
Portions copyright (c) 2008-2009 Martin Thompson, Tim Bentley, Carsten Tinggaard
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it under
|
|
|
|
the terms of the GNU General Public License as published by the Free Software
|
|
|
|
Foundation; version 2 of the License.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
|
|
|
PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along with
|
|
|
|
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
|
|
|
|
Place, Suite 330, Boston, MA 02111-1307 USA
|
2009-03-28 20:12:22 +00:00
|
|
|
from xml.dom.minidom import Document
|
|
|
|
from xml.etree.ElementTree import ElementTree, XML, dump
|
|
|
|
|
2009-04-06 18:45:45 +00:00
|
|
|
For XML Schema see wiki.openlp.org
|
2009-03-28 20:12:22 +00:00
|
|
|
"""
|
2009-05-16 19:47:30 +00:00
|
|
|
import os, os.path
|
2009-05-21 05:15:51 +00:00
|
|
|
from openlp.core.lib import str_to_bool
|
2009-04-06 18:45:45 +00:00
|
|
|
from xml.dom.minidom import Document
|
|
|
|
from xml.etree.ElementTree import ElementTree, XML, dump
|
|
|
|
|
2009-04-15 04:58:51 +00:00
|
|
|
blankthemexml=\
|
|
|
|
'''<?xml version="1.0" encoding="iso-8859-1"?>
|
|
|
|
<theme version="1.0">
|
|
|
|
<name>BlankStyle</name>
|
|
|
|
<background mode="transparent"/>
|
|
|
|
<background type="solid" mode="opaque">
|
|
|
|
<color>#000000</color>
|
|
|
|
</background>
|
|
|
|
<background type="gradient" mode="opaque">
|
|
|
|
<startColor>#000000</startColor>
|
|
|
|
<endColor>#000000</endColor>
|
|
|
|
<direction>vertical</direction>
|
|
|
|
</background>
|
|
|
|
<background type="image" mode="opaque">
|
2009-05-16 10:24:03 +00:00
|
|
|
<filename></filename>
|
2009-04-15 04:58:51 +00:00
|
|
|
</background>
|
|
|
|
<font type="main">
|
|
|
|
<name>Arial</name>
|
|
|
|
<color>#000000</color>
|
|
|
|
<proportion>30</proportion>
|
2009-04-20 20:03:04 +00:00
|
|
|
<location override="False" x="0" y="0" width="0" height="0"/>
|
2009-04-15 04:58:51 +00:00
|
|
|
</font>
|
|
|
|
<font type="footer">
|
|
|
|
<name>Arial</name>
|
|
|
|
<color>#000000</color>
|
|
|
|
<proportion>12</proportion>
|
2009-04-20 20:03:04 +00:00
|
|
|
<location override="False" x="0" y="0" width="0" height="0"/>
|
2009-04-15 04:58:51 +00:00
|
|
|
</font>
|
|
|
|
<display>
|
|
|
|
<shadow color="#000000">True</shadow>
|
|
|
|
<outline color="#000000">False</outline>
|
|
|
|
<horizontalAlign>0</horizontalAlign>
|
|
|
|
<verticalAlign>0</verticalAlign>
|
|
|
|
<wrapStyle>0</wrapStyle>
|
|
|
|
</display>
|
|
|
|
</theme>
|
|
|
|
'''
|
|
|
|
|
2009-04-07 19:45:21 +00:00
|
|
|
class ThemeXML():
|
2009-03-28 20:12:22 +00:00
|
|
|
def __init__(self):
|
|
|
|
# Create the minidom document
|
|
|
|
self.theme_xml = Document()
|
|
|
|
|
2009-05-16 19:47:30 +00:00
|
|
|
def extend_filename(self, path):
|
|
|
|
if self.background_filename is not None:
|
|
|
|
self.background_filename = os.path.join(path, self.theme_name, self.background_filename)
|
|
|
|
|
2009-03-28 20:12:22 +00:00
|
|
|
def new_document(self, name):
|
|
|
|
# Create the <song> base element
|
2009-04-15 04:58:51 +00:00
|
|
|
self.theme = self.theme_xml.createElement(u'theme')
|
2009-03-28 20:12:22 +00:00
|
|
|
self.theme_xml.appendChild(self.theme)
|
|
|
|
self.theme.setAttribute(u'version', u'1.0')
|
|
|
|
|
2009-04-15 04:58:51 +00:00
|
|
|
self.name = self.theme_xml.createElement(u'name')
|
2009-03-28 20:12:22 +00:00
|
|
|
ctn = self.theme_xml.createTextNode(name)
|
|
|
|
self.name.appendChild(ctn)
|
|
|
|
self.theme.appendChild(self.name)
|
|
|
|
|
|
|
|
def add_background_transparent(self):
|
|
|
|
# Create the main <lyrics> element
|
2009-04-06 18:45:45 +00:00
|
|
|
background = self.theme_xml.createElement(u'background')
|
2009-03-28 20:12:22 +00:00
|
|
|
background.setAttribute(u'mode', u'transparent')
|
|
|
|
self.theme.appendChild(background)
|
|
|
|
|
|
|
|
def add_background_solid(self, bkcolor):
|
2009-04-06 18:45:45 +00:00
|
|
|
background = self.theme_xml.createElement(u'background')
|
2009-03-28 20:12:22 +00:00
|
|
|
background.setAttribute(u'mode', u'opaque')
|
|
|
|
background.setAttribute(u'type', u'solid')
|
|
|
|
self.theme.appendChild(background)
|
|
|
|
|
2009-04-15 04:58:51 +00:00
|
|
|
color = self.theme_xml.createElement(u'color')
|
2009-03-28 20:12:22 +00:00
|
|
|
bkc = self.theme_xml.createTextNode(bkcolor)
|
|
|
|
color.appendChild(bkc)
|
|
|
|
background.appendChild(color)
|
|
|
|
|
2009-04-06 18:45:45 +00:00
|
|
|
def add_background_gradient(self, startcolor, endcolor, direction):
|
|
|
|
background = self.theme_xml.createElement(u'background')
|
2009-03-28 20:12:22 +00:00
|
|
|
background.setAttribute(u'mode', u'opaque')
|
2009-04-11 07:33:45 +00:00
|
|
|
background.setAttribute(u'type', u'gradient')
|
2009-03-28 20:12:22 +00:00
|
|
|
self.theme.appendChild(background)
|
|
|
|
|
2009-04-19 20:07:52 +00:00
|
|
|
color = self.theme_xml.createElement(u'startColor')
|
2009-03-28 20:12:22 +00:00
|
|
|
bkc = self.theme_xml.createTextNode(startcolor)
|
|
|
|
color.appendChild(bkc)
|
|
|
|
background.appendChild(color)
|
|
|
|
|
2009-04-19 20:07:52 +00:00
|
|
|
color = self.theme_xml.createElement(u'endColor')
|
2009-03-28 20:12:22 +00:00
|
|
|
bkc = self.theme_xml.createTextNode(endcolor)
|
|
|
|
color.appendChild(bkc)
|
|
|
|
background.appendChild(color)
|
|
|
|
|
2009-04-06 18:45:45 +00:00
|
|
|
color = self.theme_xml.createElement(u'direction')
|
|
|
|
bkc = self.theme_xml.createTextNode(direction)
|
|
|
|
color.appendChild(bkc)
|
|
|
|
background.appendChild(color)
|
|
|
|
|
|
|
|
def add_background_image(self, filename):
|
|
|
|
background = self.theme_xml.createElement(u'background')
|
2009-03-28 20:12:22 +00:00
|
|
|
background.setAttribute(u'mode', u'opaque')
|
|
|
|
background.setAttribute(u'type', u'image')
|
|
|
|
self.theme.appendChild(background)
|
|
|
|
|
|
|
|
color = self.theme_xml.createElement(u'filename')
|
|
|
|
bkc = self.theme_xml.createCDATASection(filename)
|
|
|
|
color.appendChild(bkc)
|
|
|
|
background.appendChild(color)
|
|
|
|
|
2009-04-15 04:58:51 +00:00
|
|
|
def add_font(self, name, color, proportion, override, fonttype=u'main', xpos=0, ypos=0 ,width=0, height=0):
|
2009-04-06 18:45:45 +00:00
|
|
|
background = self.theme_xml.createElement(u'font')
|
|
|
|
background.setAttribute(u'type',fonttype)
|
|
|
|
self.theme.appendChild(background)
|
|
|
|
|
2009-04-15 04:58:51 +00:00
|
|
|
element = self.theme_xml.createElement(u'name')
|
|
|
|
fn = self.theme_xml.createTextNode(name)
|
|
|
|
element.appendChild(fn)
|
|
|
|
background.appendChild(element)
|
|
|
|
|
|
|
|
element = self.theme_xml.createElement(u'color')
|
|
|
|
fn = self.theme_xml.createTextNode(color)
|
|
|
|
element.appendChild(fn)
|
|
|
|
background.appendChild(element)
|
|
|
|
|
|
|
|
element = self.theme_xml.createElement(u'proportion')
|
|
|
|
fn = self.theme_xml.createTextNode(proportion)
|
|
|
|
element.appendChild(fn)
|
|
|
|
background.appendChild(element)
|
|
|
|
|
|
|
|
element = self.theme_xml.createElement(u'location')
|
|
|
|
element.setAttribute(u'override',override)
|
2009-04-30 21:02:28 +00:00
|
|
|
|
|
|
|
if override == u'True':
|
|
|
|
element.setAttribute(u'x',xpos)
|
|
|
|
element.setAttribute(u'y',ypos)
|
|
|
|
element.setAttribute(u'width',width)
|
|
|
|
element.setAttribute(u'height',height)
|
2009-04-15 04:58:51 +00:00
|
|
|
background.appendChild(element)
|
2009-04-10 05:48:57 +00:00
|
|
|
|
2009-04-06 18:45:45 +00:00
|
|
|
def add_display(self, shadow, shadowColor, outline, outlineColor, horizontal, vertical, wrap):
|
|
|
|
background = self.theme_xml.createElement(u'display')
|
|
|
|
self.theme.appendChild(background)
|
2009-03-28 20:12:22 +00:00
|
|
|
|
2009-04-06 18:45:45 +00:00
|
|
|
tagElement = self.theme_xml.createElement(u'shadow')
|
|
|
|
tagElement.setAttribute(u'color',shadowColor)
|
|
|
|
tagValue = self.theme_xml.createTextNode(shadow)
|
|
|
|
tagElement.appendChild(tagValue)
|
|
|
|
background.appendChild(tagElement)
|
|
|
|
|
|
|
|
tagElement = self.theme_xml.createElement(u'outline')
|
|
|
|
tagElement.setAttribute(u'color',outlineColor)
|
|
|
|
tagValue = self.theme_xml.createTextNode(outline)
|
|
|
|
tagElement.appendChild(tagValue)
|
|
|
|
background.appendChild(tagElement)
|
|
|
|
|
|
|
|
tagElement = self.theme_xml.createElement(u'horizontalAlign')
|
|
|
|
tagValue = self.theme_xml.createTextNode(horizontal)
|
|
|
|
tagElement.appendChild(tagValue)
|
|
|
|
background.appendChild(tagElement)
|
|
|
|
|
|
|
|
tagElement = self.theme_xml.createElement(u'verticalAlign')
|
|
|
|
tagValue = self.theme_xml.createTextNode(vertical)
|
|
|
|
tagElement.appendChild(tagValue)
|
|
|
|
background.appendChild(tagElement)
|
|
|
|
|
|
|
|
tagElement = self.theme_xml.createElement(u'wrapStyle')
|
|
|
|
tagValue = self.theme_xml.createTextNode(wrap)
|
|
|
|
tagElement.appendChild(tagValue)
|
|
|
|
background.appendChild(tagElement)
|
|
|
|
|
|
|
|
def child_element(self, tag, value):
|
|
|
|
tagElement = self.theme_xml.createElement(tag)
|
|
|
|
tagValue = self.theme_xml.createTextNode(value)
|
|
|
|
tagElement.appendChild(ftagValue)
|
|
|
|
self.background.appendChild(tagElement)
|
2009-03-28 20:12:22 +00:00
|
|
|
|
|
|
|
def dump_xml(self):
|
|
|
|
# Debugging aid to see what we have
|
|
|
|
print self.theme_xml.toprettyxml(indent=" ")
|
|
|
|
|
|
|
|
def extract_xml(self):
|
|
|
|
# Print our newly created XML
|
|
|
|
return self.theme_xml.toxml()
|
|
|
|
|
2009-04-07 19:45:21 +00:00
|
|
|
def parse(self, xml):
|
2009-04-15 04:58:51 +00:00
|
|
|
self.baseParseXml()
|
|
|
|
self.parse_xml(xml)
|
|
|
|
|
|
|
|
def baseParseXml(self):
|
|
|
|
self.parse_xml(blankthemexml)
|
|
|
|
|
|
|
|
def parse_xml(self, xml):
|
2009-04-06 18:45:45 +00:00
|
|
|
theme_xml = ElementTree(element=XML(xml))
|
|
|
|
iter=theme_xml.getiterator()
|
|
|
|
master = u''
|
2009-03-28 20:12:22 +00:00
|
|
|
for element in iter:
|
2009-04-06 18:45:45 +00:00
|
|
|
#print element.tag, element.text
|
|
|
|
if len(element.getchildren()) > 0:
|
|
|
|
master= element.tag + u'_'
|
|
|
|
if len(element.attrib) > 0:
|
|
|
|
#print "D", element.tag , element.attrib
|
|
|
|
for e in element.attrib.iteritems():
|
|
|
|
#print "A", master, e[0], e[1]
|
|
|
|
if master == u'font_' and e[0] == u'type':
|
|
|
|
master += e[1] + u'_'
|
|
|
|
elif master == u'display_' and (element.tag == u'shadow' or element.tag == u'outline'):
|
|
|
|
#print "b", master, element.tag, element.text, e[0], e[1]
|
2009-05-21 05:15:51 +00:00
|
|
|
et = str_to_bool(element.text)
|
2009-04-15 04:58:51 +00:00
|
|
|
setattr(self, master + element.tag , et)
|
2009-04-06 18:45:45 +00:00
|
|
|
setattr(self, master + element.tag +u'_'+ e[0], e[1])
|
|
|
|
else:
|
|
|
|
field = master + e[0]
|
2009-04-21 19:45:50 +00:00
|
|
|
e1 = e[1]
|
|
|
|
if e[1] == u'True' or e[1] == u'False':
|
2009-05-21 05:15:51 +00:00
|
|
|
e1 = str_to_bool(e[1])
|
2009-04-21 19:45:50 +00:00
|
|
|
setattr(self, field, e1)
|
2009-04-06 18:45:45 +00:00
|
|
|
else:
|
2009-04-15 04:58:51 +00:00
|
|
|
#print "c", element.tag, element.text
|
2009-04-06 18:45:45 +00:00
|
|
|
if element.tag is not None :
|
|
|
|
field = master + element.tag
|
|
|
|
setattr(self, field, element.text)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
s = u''
|
|
|
|
for k in dir(self):
|
|
|
|
if k[0:1] != u'_':
|
|
|
|
s+= u'%30s : %s\n' %(k,getattr(self,k))
|
|
|
|
return s
|