openlp/openlp/core/theme/theme.py

243 lines
8.5 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 #
# --------------------------------------------------------------------------- #
2011-12-27 10:33:55 +00:00
# Copyright (c) 2008-2012 Raoul Snyman #
# Portions copyright (c) 2008-2012 Tim Bentley, Gerald Britton, Jonathan #
# Corwin, Samuel Findlay, Michael Gorven, Scott Guerrieri, Matthias Hub, #
2012-11-11 21:16:14 +00:00
# Meinert Jordan, Armin Köhler, Erik Lundin, Edwin Lunando, Brian T. Meyer. #
2012-10-21 13:16:22 +00:00
# Joshua Miller, Stevan Pettit, Andreas Preikschat, Mattias Põldaru, #
# Christian Richter, Philip Ridout, Simon Scudder, Jeffrey Smith, #
# Maikel Stuivenberg, Martin Thompson, Jon Tibble, Dave Warnock, #
2012-11-07 21:37:01 +00:00
# Frode Woldsund, Martin Zibricky #
# --------------------------------------------------------------------------- #
# 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
2011-02-08 03:25:50 +00:00
DELPHI_COLORS = {
u'clAqua': 0x00FFFF, u'clBlack': 0x000000, u'clBlue': 0x0000FF,
u'clFuchsia': 0xFF00FF, u'clGray': 0x808080, u'clGreen': 0x008000,
u'clLime': 0x00FF00, u'clMaroon': 0x800000, u'clNavy': 0x000080,
u'clOlive': 0x808000, u'clPurple': 0x800080, u'clRed': 0xFF0000,
u'clSilver': 0xC0C0C0, u'clTeal': 0x008080, u'clWhite': 0xFFFFFF,
u'clYellow': 0xFFFF00
}
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``
2010-12-10 05:09:03 +00:00
The behaviour of the background. Valid modes are:
* ``0`` - Transparent
* ``1`` - Opaque
2010-06-10 14:09:32 +00:00
``BackgroundType``
2010-12-10 05:09:03 +00:00
The content of the background. Valid types are:
* ``0`` - solid color
* ``1`` - gradient color
* ``2`` - image
2010-06-10 14:09:32 +00:00
``BackgroundParameter1``
2010-12-10 05:09:03 +00:00
Extra information about the background. The contents of this attribute
2010-06-10 14:09:32 +00:00
depend on the BackgroundType:
2010-12-10 05:09:03 +00:00
* ``image`` - image filename
* ``gradient`` - start color
* ``solid`` - color
2010-06-10 14:09:32 +00:00
``BackgroundParameter2``
2011-02-25 17:05:01 +00:00
Extra information about the background. The contents of this attribute
2010-06-10 14:09:32 +00:00
depend on the BackgroundType:
2010-12-10 05:09:03 +00:00
* ``image`` - border color
* ``gradient`` - end color
* ``solid`` - N/A
2010-06-10 14:09:32 +00:00
``BackgroundParameter3``
2011-02-25 17:05:01 +00:00
Extra information about the background. The contents of this attribute
2010-06-10 14:09:32 +00:00
depend on the BackgroundType:
2010-12-10 05:09:03 +00:00
* ``image`` - N/A
* ``gradient`` - The direction of the gradient. Valid entries are:
* ``0`` - vertical
* ``1`` - horizontal
* ``solid`` - N/A
2010-06-10 14:09:32 +00:00
``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.
2010-12-10 05:09:03 +00:00
* ``0`` - no shadow
* non-zero - use shadow
2010-06-10 14:09:32 +00:00
``ShadowColor``
Color for the shadow
``Outline``
The outline to apply to the main font
2010-12-10 05:09:03 +00:00
* ``0`` - no outline
* non-zero - use outline
2010-06-10 14:09:32 +00:00
``OutlineColor``
Color for the outline (or None if Outline is 0)
``HorizontalAlign``
2011-02-25 17:05:01 +00:00
The horizontal alignment to apply to text. Valid alignments are:
2010-12-10 05:09:03 +00:00
* ``0`` - left align
* ``1`` - right align
* ``2`` - centre align
2010-06-10 14:09:32 +00:00
``VerticalAlign``
The vertical alignment to apply to the text. Valid alignments are:
2010-12-10 05:09:03 +00:00
* ``0`` - top align
* ``1`` - bottom align
* ``2`` - centre align
2010-06-10 14:09:32 +00:00
``WrapStyle``
2011-02-25 17:05:01 +00:00
The wrap style to apply to the text. Valid styles are:
2010-12-10 05:09:03 +00:00
* ``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
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
elif element_text in DELPHI_COLORS:
2010-05-28 15:01:50 +00:00
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-08-27 18:48:06 +00:00
isinstance(val, int))):
# 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-27 09:32:52 +00:00
return u'\n'.join(theme_strings)