Moved Theme specific bits from xmlrootclass to theme

bzr-revno: 210
This commit is contained in:
Martin Thompson 2008-12-10 21:40:19 +00:00
parent c64f1bd792
commit 822d3d9a0e
3 changed files with 24 additions and 17 deletions

View File

@ -31,15 +31,15 @@ else:
from elementtree import ElementTree, XML from elementtree import ElementTree, XML
# borrowed from theme - can be common
DelphiColors={"clRed":0xFF0000,
"clBlack":0x000000,
"clWhite":0xFFFFFF}
class XmlRootClass(object): class XmlRootClass(object):
"""Root class for themes, songs etc """Root class for themes, songs etc
provides interface for parsing xml files into object attributes provides interface for parsing xml files into object attributes
if you overload this class and provide a function called
post_tag_hook, it will be called thusly for each tag,value pair:
(element.tag, val) = self.post_tag_hook(element.tag, val)
""" """
def _setFromXml(self, xml, rootTag): def _setFromXml(self, xml, rootTag):
"""Set song properties from given xml content """Set song properties from given xml content
@ -66,9 +66,6 @@ class XmlRootClass(object):
except ValueError: # nope except ValueError: # nope
#print "nope", #print "nope",
pass pass
elif DelphiColors.has_key(t):
#print "colour",
val=DelphiColors[t]
else: else:
#print "last chance", #print "last chance",
try: try:
@ -77,11 +74,8 @@ class XmlRootClass(object):
except ValueError: except ValueError:
#print "give up", #print "give up",
val=t val=t
if (element.tag.find("Color") > 0 or if hasattr(self, "post_tag_hook"):
(element.tag.find("BackgroundParameter") == 0 and type(val) == type(0))): (element.tag, val) = self.post_tag_hook(element.tag, val)
# convert to a QtGui.Color
val= QtGui.QColor((val>>16) & 0xFF, (val>>8)&0xFF, val&0xFF)
#print [val]
setattr(self,element.tag, val) setattr(self,element.tag, val)
pass pass

View File

@ -20,10 +20,10 @@ import os
import sys import sys
mypath=os.path.split(os.path.abspath(__file__))[0] mypath=os.path.split(os.path.abspath(__file__))[0]
sys.path.insert(0,(os.path.join(mypath, '..' ,'..', '..'))) sys.path.insert(0,(os.path.join(mypath, '..' ,'..', '..','..')))
print sys.path print sys.path
from openlp.theme import Theme from openlp.core.theme import Theme
import os.path import os.path
from PyQt4 import QtGui from PyQt4 import QtGui
def test_read_theme(): def test_read_theme():

View File

@ -45,6 +45,10 @@ blankstylexml=\
</Theme> </Theme>
''' '''
DelphiColors={"clRed":0xFF0000,
"clBlack":0x000000,
"clWhite":0xFFFFFF}
class Theme(XmlRootClass): class Theme(XmlRootClass):
def __init__(self, xmlfile=None): def __init__(self, xmlfile=None):
""" stores the info about a theme """ stores the info about a theme
@ -83,10 +87,19 @@ class Theme(XmlRootClass):
1 - lyrics 1 - lyrics
""" """
# init to defaults # init to defaults
self._set_from_XML(blankstylexml) self._setFromXml(blankstylexml, 'Theme')
if xmlfile != None: if xmlfile != None:
# init from xmlfile # init from xmlfile
file=open(xmlfile) file=open(xmlfile)
t=''.join(file.readlines()) # read the file and change list to a string t=''.join(file.readlines()) # read the file and change list to a string
self._set_from_XML(t) self._setFromXml(t, 'Theme')
def post_tag_hook(self, tag, val):
if DelphiColors.has_key(val):
val=DelphiColors[val]
if (tag.find("Color") > 0 or
(tag.find("BackgroundParameter") == 0 and type(val) == type(0))):
# convert to a QtGui.Color
val= QtGui.QColor((val>>16) & 0xFF, (val>>8)&0xFF, val&0xFF)
return (tag, val)