openlp/openlp/core/theme/theme.py

225 lines
7.9 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
# vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4
###############################################################################
# OpenLP - Open Source Lyrics Projection #
# --------------------------------------------------------------------------- #
2009-12-31 12:52:01 +00:00
# Copyright (c) 2008-2010 Raoul Snyman #
# Portions copyright (c) 2008-2010 Tim Bentley, Jonathan Corwin, Michael #
2010-07-24 22:10:47 +00:00
# Gorven, Scott Guerrieri, Meinert Jordan, Andreas Preikschat, Christian #
# Richter, Philip Ridout, Maikel Stuivenberg, Martin Thompson, Jon Tibble, #
# Carsten Tinggaard, Frode Woldsund #
# --------------------------------------------------------------------------- #
# 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 #
###############################################################################
2010-06-10 01:57:59 +00:00
"""
OpenLP version 1 theme handling
2010-06-10 01:57:59 +00:00
Provides reference data, a default v1 XML theme and class wrapper for
processing version 1 themes in OpenLP version 2.
"""
2009-09-25 00:43:42 +00:00
from xml.etree.ElementTree import ElementTree, XML
2009-09-29 12:51:38 +00:00
from PyQt4 import QtGui
2008-06-16 19:51:19 +00:00
2010-05-28 15:01:50 +00:00
DELPHI_COLORS = {"clRed":0xFF0000,
"clBlue":0x0000FF,
"clYellow":0xFFFF00,
"clBlack":0x000000,
"clWhite":0xFFFFFF}
2008-04-03 17:54:13 +00:00
2010-05-28 15:01:50 +00:00
BLANK_STYLE_XML = \
2008-04-03 17:54:13 +00:00
'''<?xml version="1.0" encoding="iso-8859-1"?>
<Theme>
<Name>BlankStyle</Name>
<BackgroundMode>1</BackgroundMode>
<BackgroundType>0</BackgroundType>
2008-04-03 17:54:13 +00:00
<BackgroundParameter1>$000000</BackgroundParameter1>
<BackgroundParameter2/>
<BackgroundParameter3/>
<FontName>Arial</FontName>
<FontColor>clWhite</FontColor>
<FontProportion>30</FontProportion>
<FontUnits>pixels</FontUnits>
2008-04-03 17:54:13 +00:00
<Shadow>0</Shadow>
<Outline>0</Outline>
<HorizontalAlign>0</HorizontalAlign>
<VerticalAlign>0</VerticalAlign>
2008-06-16 19:51:19 +00:00
<WrapStyle>0</WrapStyle>
2008-04-03 17:54:13 +00:00
</Theme>
'''
class Theme(object):
2010-06-10 01:57:59 +00:00
"""
Provide a class wrapper storing data from an XML theme
2010-06-10 14:09:32 +00:00
``name``
Theme name
``BackgroundMode``
The behaviour of the background. Valid modes are:
- 0 - Transparent
- 1 - Opaque
``BackgroundType``
The content of the background. Valid types are:
- 0 - solid color
- 1 - gradient color
- 2 - image
``BackgroundParameter1``
Extra information about the background. The contents of this attribute
depend on the BackgroundType:
- image: image filename
- gradient: start color
- solid: color
``BackgroundParameter2``
Extra information about the background. The contents of this attribute
depend on the BackgroundType:
- image: border color
- gradient: end color
- solid: N/A
``BackgroundParameter3``
Extra information about the background. The contents of this attribute
depend on the BackgroundType:
- image: N/A
- gradient: The direction of the gradient. Valid entries are:
- 0 -> vertical
- 1 -> horizontal
- solid: N/A
``FontName``
Name of the font to use for the main font.
``FontColor``
The color for the main font
``FontProportion``
The size of the main font
``FontUnits``
The units for FontProportion, either <pixels> or <points>
``Shadow``
The shadow type to apply to the main font.
- 0 - no shadow
- non-zero - use shadow
``ShadowColor``
Color for the shadow
``Outline``
The outline to apply to the main font
- 0 - no outline
- non-zero - use outline
``OutlineColor``
Color for the outline (or None if Outline is 0)
``HorizontalAlign``
The horizontal alignment to apply to text. Valid alignments are:
- 0 - left align
- 1 - right align
- 2 - centre align
``VerticalAlign``
The vertical alignment to apply to the text. Valid alignments are:
- 0 - top align
- 1 - bottom align
- 2 - centre align
``WrapStyle``
The wrap style to apply to the text. Valid styles are:
- 0 - normal
- 1 - lyrics
2010-06-10 01:57:59 +00:00
"""
def __init__(self, xml):
2010-06-10 01:57:59 +00:00
"""
Initialise a theme with data from xml
2010-06-10 14:09:32 +00:00
``xml``
The data to initialise the theme with
2008-04-03 17:54:13 +00:00
"""
# init to defaults
2010-06-12 20:22:58 +00:00
self._set_from_xml(BLANK_STYLE_XML)
self._set_from_xml(xml)
def _get_as_string(self):
2010-06-10 01:57:59 +00:00
"""
Return single line string representation of a theme
"""
theme_strings = []
keys = dir(self)
keys.sort()
for key in keys:
if key[0:1] != u'_':
theme_strings.append(u'_%s_' % (getattr(self, key)))
return u''.join(theme_strings)
2010-06-29 07:07:03 +00:00
def _set_from_xml(self, xml):
2010-06-10 01:57:59 +00:00
"""
Set theme class attributes with data from XML
2010-06-10 14:09:32 +00:00
``xml``
The data to apply to the theme
2010-06-10 01:57:59 +00:00
"""
2010-06-19 17:56:21 +00:00
root = ElementTree(element=XML(xml.encode(u'ascii',
u'xmlcharrefreplace')))
2010-06-12 20:22:58 +00:00
xml_iter = root.getiterator()
for element in xml_iter:
delphi_color_change = False
if element.tag != u'Theme':
2010-05-27 14:41:47 +00:00
element_text = element.text
val = 0
# easy!
2010-05-27 14:41:47 +00:00
if element_text is None:
val = element_text
# strings need special handling to sort the colours out
2010-06-09 17:09:32 +00:00
if isinstance(element_text, basestring):
2010-05-27 14:41:47 +00:00
if element_text[0] == u'$': # might be a hex number
try:
2010-05-27 14:41:47 +00:00
val = int(element_text[1:], 16)
except ValueError: # nope
pass
2010-05-28 15:01:50 +00:00
elif DELPHI_COLORS.has_key(element_text):
val = DELPHI_COLORS[element_text]
2010-06-12 20:22:58 +00:00
delphi_color_change = True
else:
try:
2010-05-27 14:41:47 +00:00
val = int(element_text)
except ValueError:
2010-05-27 14:41:47 +00:00
val = element_text
if (element.tag.find(u'Color') > 0 or
2010-05-24 22:37:20 +00:00
(element.tag.find(u'BackgroundParameter') == 0 and
2010-07-08 08:14:10 +00:00
isinstance(int, val))):
# convert to a wx.Colour
2010-06-12 20:22:58 +00:00
if not delphi_color_change:
2010-05-24 22:37:20 +00:00
val = QtGui.QColor(
val&0xFF, (val>>8)&0xFF, (val>>16)&0xFF)
else:
val = QtGui.QColor(
(val>>16)&0xFF, (val>>8)&0xFF, val&0xFF)
setattr(self, element.tag, val)
def __str__(self):
2010-06-10 01:57:59 +00:00
"""
Provide Python string representation for the class (multiline output)
"""
theme_strings = []
for key in dir(self):
if key[0:1] != u'_':
theme_strings.append(u'%30s : %s' % (key, getattr(self, key)))
2010-07-24 22:10:47 +00:00
return u'\n'.join(theme_strings)