From 36b3446d2cdc9e013f157624626ad16b3757286e Mon Sep 17 00:00:00 2001 From: Tim Bentley Date: Sat, 11 Sep 2010 07:59:36 +0100 Subject: [PATCH] Theme Cleanup up --- openlp/core/lib/htmlbuilder.py | 20 ++++++------ openlp/core/lib/theme.py | 45 ++++++++++++++++++------- openlp/core/ui/amendthemeform.py | 50 ++++++++++++++-------------- openlp/core/ui/maindisplay.py | 2 +- openlp/core/ui/thememanager.py | 56 -------------------------------- 5 files changed, 69 insertions(+), 104 deletions(-) diff --git a/openlp/core/lib/htmlbuilder.py b/openlp/core/lib/htmlbuilder.py index 7f4fc5082..f0194da02 100644 --- a/openlp/core/lib/htmlbuilder.py +++ b/openlp/core/lib/htmlbuilder.py @@ -281,7 +281,7 @@ def build_html(item, screen, alert, islive): build_alert_css(alert, width), build_footer_css(item, height), build_lyrics_css(item, webkitvers), - u'true' if theme and theme.display_slideTransition and islive \ + u'true' if theme and theme.display_slide_transition and islive \ else u'false', image, build_lyrics_html(item, webkitvers)) @@ -318,18 +318,18 @@ def build_background_css(item, width, height): background = \ u'background: ' \ u'-webkit-gradient(linear, left top, left bottom, ' \ - 'from(%s), to(%s))' % (theme.background_startColor, - theme.background_endColor) + 'from(%s), to(%s))' % (theme.background_start_color, + theme.background_end_color) elif theme.background_direction == u'vertical': background = \ u'background: -webkit-gradient(linear, left top, ' \ u'right top, from(%s), to(%s))' % \ - (theme.background_startColor, theme.background_endColor) + (theme.background_start_color, theme.background_end_color) else: background = \ u'background: -webkit-gradient(radial, %s 50%%, 100, %s ' \ u'50%%, %s, from(%s), to(%s))' % (width, width, width, - theme.background_startColor, theme.background_endColor) + theme.background_start_color, theme.background_end_color) return background def build_lyrics_css(item, webkitvers): @@ -446,15 +446,15 @@ def build_lyrics_format_css(theme, width, height): Height of the lyrics block """ - if theme.display_horizontalAlign == 2: + if theme.display_horizontal_align == 2: align = u'center' - elif theme.display_horizontalAlign == 1: + elif theme.display_horizontal_align == 1: align = u'right' else: align = u'left' - if theme.display_verticalAlign == 2: + if theme.display_vertical_align == 2: valign = u'bottom' - elif theme.display_verticalAlign == 1: + elif theme.display_vertical_align == 1: valign = u'middle' else: valign = u'top' @@ -518,7 +518,7 @@ def build_footer_css(item, height): font-size: %spt; color: %s; text-align: left; - white-space:nowrap; + white-space:nowrap; """ theme = item.themedata if not theme or not item.footer: diff --git a/openlp/core/lib/theme.py b/openlp/core/lib/theme.py index ee3418dca..0990f6906 100644 --- a/openlp/core/lib/theme.py +++ b/openlp/core/lib/theme.py @@ -27,6 +27,7 @@ Provide the theme XML and handling functions for OpenLP v2 themes. """ import os +import re from xml.dom.minidom import Document from xml.etree.ElementTree import ElementTree, XML @@ -86,6 +87,17 @@ class ThemeLevel(object): Service = 2 Song = 3 +boolean_list = [u'font_main_italics', u'font_main_override', \ +u'font_footer_italics', u'font_footer_override', u'display_outline', \ +u'display_shadow', u'display_slide_transition'] + +integer_list =[u'font_main_proportion', u'font_main_line_adjustment', \ +u'font_main_x', u'font_main_height', u'font_main_y', u'font_main_width', \ +u'font_footer_proportion', u'font_footer_line_adjustment', u'font_footer_x', \ +u'font_footer_height', u'font_footer_y', u'font_footer_width', \ +u'display_shadow_size', u'display_outline_size', u'display_horizontal_align',\ +u'display_vertical_align', u'display_wrap_style' ] + class ThemeXML(object): """ A class to encapsulate the Theme XML. @@ -372,10 +384,10 @@ class ThemeXML(object): if element.getchildren(): master = element.tag + u'_' else: - #background transparent tags have no children so special case + # background transparent tags have no children so special case if element.tag == u'background': for e in element.attrib.iteritems(): - setattr(self, element.tag + u'_' + e[0], e[1]) + self._create_attr(element.tag + u'_' + e[0], e[1]) if element.attrib: for e in element.attrib.iteritems(): if master == u'font_' and e[0] == u'type': @@ -383,22 +395,25 @@ class ThemeXML(object): elif master == u'display_' and (element.tag == u'shadow' \ or element.tag == u'outline' ): et = str_to_bool(element.text) - setattr(self, master + element.tag, et) - setattr(self, master + element.tag + u'_'+ e[0], e[1]) + self._create_attr(master + element.tag, et) + self._create_attr(master + element.tag + u'_'+ e[0], e[1]) else: field = master + e[0] - if e[1] == u'True' or e[1] == u'False': - setattr(self, field, str_to_bool(e[1])) - else: - setattr(self, field, e[1]) + self._create_attr(field, e[1]) else: if element.tag: field = master + element.tag element.text = element.text.strip().lstrip() - if element.text == u'True' or element.text == u'False': - setattr(self, field, str_to_bool(element.text)) - else: - setattr(self, field, element.text) + self._create_attr(field, element.text) + + def _create_attr(self, element, value): + field = self._de_hump(element) + if field in boolean_list: + setattr(self, field, str_to_bool(value)) + elif field in integer_list: + setattr(self, field, int(value)) + else: + setattr(self, field, unicode(value)) def __str__(self): """ @@ -409,3 +424,9 @@ class ThemeXML(object): if key[0:1] != u'_': theme_strings.append(u'%30s: %s' % (key, getattr(self, key))) return u'\n'.join(theme_strings) + + def _de_hump(self, name): + + s1 = re.sub(u'(.)([A-Z][a-z]+)', r'\1_\2', name) + return re.sub(u'([a-z0-9])([A-Z])', r'\1_\2', s1).lower() + diff --git a/openlp/core/ui/amendthemeform.py b/openlp/core/ui/amendthemeform.py index e23044a2b..b1352dedb 100644 --- a/openlp/core/ui/amendthemeform.py +++ b/openlp/core/ui/amendthemeform.py @@ -150,8 +150,8 @@ class AmendThemeForm(QtGui.QDialog, Ui_AmendThemeDialog): unicode(self.theme.background_color)) elif self.theme.background_type == u'gradient': new_theme.add_background_gradient( - unicode(self.theme.background_startColor), - unicode(self.theme.background_endColor), + unicode(self.theme.background_start_color), + unicode(self.theme.background_end_color), self.theme.background_direction) else: filename = \ @@ -185,10 +185,10 @@ class AmendThemeForm(QtGui.QDialog, Ui_AmendThemeDialog): unicode(self.theme.display_shadow_color), unicode(self.theme.display_outline), unicode(self.theme.display_outline_color), - unicode(self.theme.display_horizontalAlign), - unicode(self.theme.display_verticalAlign), - unicode(self.theme.display_wrapStyle), - unicode(self.theme.display_slideTransition), + unicode(self.theme.display_horizontal_align), + unicode(self.theme.display_vertical_align), + unicode(self.theme.display_wrap_style), + unicode(self.theme.display_slide_transition), unicode(self.theme.display_shadow_size), unicode(self.theme.display_outline_size)) theme = new_theme.extract_xml() @@ -407,10 +407,10 @@ class AmendThemeForm(QtGui.QDialog, Ui_AmendThemeDialog): self.theme.background_direction = u'vertical' else: self.theme.background_direction = u'circular' - if self.theme.background_startColor is None: - self.theme.background_startColor = u'#000000' - if self.theme.background_endColor is None: - self.theme.background_endColor = u'#ff0000' + if self.theme.background_start_color is None: + self.theme.background_start_color = u'#000000' + if self.theme.background_end_color is None: + self.theme.background_end_color = u'#ff0000' self.imageLineEdit.setText(u'') else: self.theme.background_type = u'image' @@ -427,20 +427,20 @@ class AmendThemeForm(QtGui.QDialog, Ui_AmendThemeDialog): unicode(self.theme.background_color)) else: new_color = QtGui.QColorDialog.getColor( - QtGui.QColor(self.theme.background_startColor), self) + QtGui.QColor(self.theme.background_start_color), self) if new_color.isValid(): - self.theme.background_startColor = new_color.name() + self.theme.background_start_color = new_color.name() self.color1PushButton.setStyleSheet(u'background-color: %s' % - unicode(self.theme.background_startColor)) + unicode(self.theme.background_start_color)) self.previewTheme() def onColor2PushButtonClicked(self): new_color = QtGui.QColorDialog.getColor( - QtGui.QColor(self.theme.background_endColor), self) + QtGui.QColor(self.theme.background_end_color), self) if new_color.isValid(): - self.theme.background_endColor = new_color.name() + self.theme.background_end_color = new_color.name() self.color2PushButton.setStyleSheet(u'background-color: %s' % - unicode(self.theme.background_endColor)) + unicode(self.theme.background_end_color)) self.previewTheme() # @@ -483,9 +483,9 @@ class AmendThemeForm(QtGui.QDialog, Ui_AmendThemeDialog): def onSlideTransitionCheckBoxChanged(self, value): if value == 2: # checked - self.theme.display_slideTransition = True + self.theme.display_slide_transition = True else: - self.theme.display_slideTransition = False + self.theme.display_slide_transition = False self.stateChanging(self.theme) self.previewTheme() @@ -499,12 +499,12 @@ class AmendThemeForm(QtGui.QDialog, Ui_AmendThemeDialog): self.previewTheme() def onHorizontalComboBoxSelected(self, currentIndex): - self.theme.display_horizontalAlign = currentIndex + self.theme.display_horizontal_align = currentIndex self.stateChanging(self.theme) self.previewTheme() def onVerticalComboBoxSelected(self, currentIndex): - self.theme.display_verticalAlign = currentIndex + self.theme.display_vertical_align = currentIndex self.stateChanging(self.theme) self.previewTheme() @@ -598,13 +598,13 @@ class AmendThemeForm(QtGui.QDialog, Ui_AmendThemeDialog): self.shadowCheckBox.setChecked(False) self.shadowColorPushButton.setEnabled(False) self.shadowSpinBox.setValue(int(self.theme.display_shadow_size)) - if self.theme.display_slideTransition: + if self.theme.display_slide_transition: self.slideTransitionCheckBox.setCheckState(QtCore.Qt.Checked) else: self.slideTransitionCheckBox.setCheckState(QtCore.Qt.Unchecked) self.horizontalComboBox.setCurrentIndex( - self.theme.display_horizontalAlign) - self.verticalComboBox.setCurrentIndex(self.theme.display_verticalAlign) + self.theme.display_horizontal_align) + self.verticalComboBox.setCurrentIndex(self.theme.display_vertical_align) def stateChanging(self, theme): self.backgroundTypeComboBox.setVisible(True) @@ -625,9 +625,9 @@ class AmendThemeForm(QtGui.QDialog, Ui_AmendThemeDialog): self.gradientComboBox.setVisible(False) elif theme.background_type == u'gradient': self.color1PushButton.setStyleSheet(u'background-color: %s' \ - % unicode(theme.background_startColor)) + % unicode(theme.background_start_color)) self.color2PushButton.setStyleSheet(u'background-color: %s' \ - % unicode(theme.background_endColor)) + % unicode(theme.background_end_color)) self.color1Label.setText( translate('OpenLP.AmendThemeForm', 'First color:')) self.color2Label.setText( diff --git a/openlp/core/ui/maindisplay.py b/openlp/core/ui/maindisplay.py index 3e0b070b9..6fbd7995c 100644 --- a/openlp/core/ui/maindisplay.py +++ b/openlp/core/ui/maindisplay.py @@ -307,7 +307,7 @@ class MainDisplay(DisplayWidget): # Wait for the fade to finish before geting the preview. # Important otherwise preview will have incorrect text if at all ! if self.serviceItem.themedata and \ - self.serviceItem.themedata.display_slideTransition: + self.serviceItem.themedata.display_slide_transition: while self.frame.evaluateJavaScript(u'show_text_complete()') \ .toString() == u'false': Receiver.send_message(u'openlp_process_events') diff --git a/openlp/core/ui/thememanager.py b/openlp/core/ui/thememanager.py index 11130293c..e76516b57 100644 --- a/openlp/core/ui/thememanager.py +++ b/openlp/core/ui/thememanager.py @@ -699,61 +699,5 @@ class ThemeManager(QtGui.QWidget): """ theme = ThemeXML() theme.parse(theme_xml) - self.cleanTheme(theme) theme.extend_image_filename(path) return theme - - def cleanTheme(self, theme): - """ - Clean a theme loaded from an XML file by removing stray whitespace and - making sure parameters are the correct type for the theme object - attributes - """ - theme.background_color = theme.background_color.strip() - theme.background_direction = theme.background_direction.strip() - theme.background_endColor = theme.background_endColor.strip() - if theme.background_filename: - theme.background_filename = theme.background_filename.strip() - #theme.background_mode - theme.background_startColor = theme.background_startColor.strip() - #theme.background_type - if theme.display_display: - theme.display_display = theme.display_display.strip() - theme.display_horizontalAlign = \ - int(theme.display_horizontalAlign.strip()) - theme.display_outline = str_to_bool(theme.display_outline) - #theme.display_outline_color - theme.display_shadow = str_to_bool(theme.display_shadow) - #theme.display_shadow_color - theme.display_verticalAlign = int(theme.display_verticalAlign.strip()) - theme.display_wrapStyle = theme.display_wrapStyle.strip() - theme.display_slideTransition = theme.display_slideTransition - theme.font_footer_color = theme.font_footer_color.strip() - theme.font_footer_height = int(theme.font_footer_height.strip()) - theme.font_footer_italics = str_to_bool(theme.font_footer_italics) - theme.font_footer_name = theme.font_footer_name.strip() - #theme.font_footer_override - theme.font_footer_proportion = \ - int(theme.font_footer_proportion.strip()) - theme.font_footer_weight = theme.font_footer_weight.strip() - theme.font_footer_width = int(theme.font_footer_width.strip()) - theme.font_footer_x = int(theme.font_footer_x.strip()) - theme.font_footer_y = int(theme.font_footer_y.strip()) - theme.font_main_color = theme.font_main_color.strip() - theme.font_main_height = int(theme.font_main_height.strip()) - theme.font_main_italics = str_to_bool(theme.font_main_italics) - theme.font_main_name = theme.font_main_name.strip() - #theme.font_main_override - theme.font_main_proportion = int(theme.font_main_proportion.strip()) - theme.font_main_weight = theme.font_main_weight.strip() - theme.font_main_width = int(theme.font_main_width.strip()) - theme.font_main_x = int(theme.font_main_x.strip()) - theme.font_main_y = int(theme.font_main_y.strip()) - #theme.theme_mode - theme.theme_name = theme.theme_name.strip() - #theme.theme_version - # Remove the Transparent settings as they are not relevent - if theme.background_mode == u'transparent': - theme.background_mode = u'opaque' - theme.background_type = u'solid' - theme.background_startColor = u'#000000'