Save themes in formatted XML and clean touched files

This commit is contained in:
Jon Tibble 2009-09-04 23:27:53 +01:00
parent a4034c4768
commit cda62a2617
3 changed files with 145 additions and 73 deletions

View File

@ -161,8 +161,8 @@ class ThemeXML(object):
#Create Filename element
self.child_element(background, u'filename', filename)
def add_font(self, name, color, proportion, override, fonttype=u'main', weight=u'Normal', italics=u'False',
xpos=0, ypos=0, width=0, height=0):
def add_font(self, name, color, proportion, override, fonttype=u'main',
weight=u'Normal', italics=u'False', xpos=0, ypos=0, width=0, height=0):
"""
Add a Font.
@ -223,7 +223,7 @@ class ThemeXML(object):
background.appendChild(element)
def add_display(self, shadow, shadow_color, outline, outline_color,
horizontal, vertical, wrap):
horizontal, vertical, wrap):
"""
Add a Display options.
@ -301,6 +301,12 @@ class ThemeXML(object):
# Print our newly created XML
return self.theme_xml.toxml()
def extract_formatted_xml(self):
"""
Pull out the XML string formatted for human consumption
"""
return self.theme_xml.toprettyxml(indent=' ', newl='\n')
def parse(self, xml):
"""
Read in an XML string and parse it.
@ -340,8 +346,8 @@ class ThemeXML(object):
for e in element.attrib.iteritems():
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'):
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])

View File

@ -44,7 +44,8 @@ class AmendThemeForm(QtGui.QDialog, Ui_AmendThemeDialog):
QtCore.QObject.connect(self.FontMainColorPushButton,
QtCore.SIGNAL(u'pressed()'), self.onFontMainColorPushButtonClicked)
QtCore.QObject.connect(self.FontFooterColorPushButton,
QtCore.SIGNAL(u'pressed()'), self.onFontFooterColorPushButtonClicked)
QtCore.SIGNAL(u'pressed()'),
self.onFontFooterColorPushButtonClicked)
QtCore.QObject.connect(self.OutlineColorPushButton,
QtCore.SIGNAL(u'pressed()'), self.onOutlineColorPushButtonClicked)
QtCore.QObject.connect(self.ShadowColorPushButton,
@ -55,46 +56,59 @@ class AmendThemeForm(QtGui.QDialog, Ui_AmendThemeDialog):
QtCore.QObject.connect(self.BackgroundComboBox,
QtCore.SIGNAL(u'activated(int)'), self.onBackgroundComboBoxSelected)
QtCore.QObject.connect(self.BackgroundTypeComboBox,
QtCore.SIGNAL(u'activated(int)'), self.onBackgroundTypeComboBoxSelected)
QtCore.SIGNAL(u'activated(int)'),
self.onBackgroundTypeComboBoxSelected)
QtCore.QObject.connect(self.GradientComboBox,
QtCore.SIGNAL(u'activated(int)'), self.onGradientComboBoxSelected)
QtCore.QObject.connect(self.FontMainComboBox,
QtCore.SIGNAL(u'activated(int)'), self.onFontMainComboBoxSelected)
QtCore.QObject.connect(self.FontMainWeightComboBox,
QtCore.SIGNAL(u'activated(int)'), self.onFontMainWeightComboBoxSelected)
QtCore.SIGNAL(u'activated(int)'),
self.onFontMainWeightComboBoxSelected)
QtCore.QObject.connect(self.FontFooterComboBox,
QtCore.SIGNAL(u'activated(int)'), self.onFontFooterComboBoxSelected)
QtCore.QObject.connect(self.FontFooterWeightComboBox,
QtCore.SIGNAL(u'activated(int)'), self.onFontFooterWeightComboBoxSelected)
QtCore.SIGNAL(u'activated(int)'),
self.onFontFooterWeightComboBoxSelected)
QtCore.QObject.connect(self.HorizontalComboBox,
QtCore.SIGNAL(u'activated(int)'), self.onHorizontalComboBoxSelected)
QtCore.QObject.connect(self.VerticalComboBox,
QtCore.SIGNAL(u'activated(int)'), self.onVerticalComboBoxSelected)
#Spin boxes
QtCore.QObject.connect(self.FontMainSizeSpinBox,
QtCore.SIGNAL(u'editingFinished()'), self.onFontMainSizeSpinBoxChanged)
QtCore.SIGNAL(u'editingFinished()'),
self.onFontMainSizeSpinBoxChanged)
QtCore.QObject.connect(self.FontFooterSizeSpinBox,
QtCore.SIGNAL(u'editingFinished()'), self.onFontFooterSizeSpinBoxChanged)
QtCore.SIGNAL(u'editingFinished()'),
self.onFontFooterSizeSpinBoxChanged)
QtCore.QObject.connect(self.FontMainDefaultCheckBox,
QtCore.SIGNAL(u'stateChanged(int)'), self.onFontMainDefaultCheckBoxChanged)
QtCore.SIGNAL(u'stateChanged(int)'),
self.onFontMainDefaultCheckBoxChanged)
QtCore.QObject.connect(self.FontMainXSpinBox,
QtCore.SIGNAL(u'editingFinished()'), self.onFontMainXSpinBoxChanged)
QtCore.QObject.connect(self.FontMainYSpinBox,
QtCore.SIGNAL(u'editingFinished()'), self.onFontMainYSpinBoxChanged)
QtCore.QObject.connect(self.FontMainWidthSpinBox,
QtCore.SIGNAL(u'editingFinished()'), self.onFontMainWidthSpinBoxChanged)
QtCore.SIGNAL(u'editingFinished()'),
self.onFontMainWidthSpinBoxChanged)
QtCore.QObject.connect(self.FontMainHeightSpinBox,
QtCore.SIGNAL(u'editingFinished()'), self.onFontMainHeightSpinBoxChanged)
QtCore.SIGNAL(u'editingFinished()'),
self.onFontMainHeightSpinBoxChanged)
QtCore.QObject.connect(self.FontFooterDefaultCheckBox,
QtCore.SIGNAL(u'stateChanged(int)'), self.onFontFooterDefaultCheckBoxChanged)
QtCore.SIGNAL(u'stateChanged(int)'),
self.onFontFooterDefaultCheckBoxChanged)
QtCore.QObject.connect(self.FontFooterXSpinBox,
QtCore.SIGNAL(u'editingFinished()'), self.onFontFooterXSpinBoxChanged)
QtCore.SIGNAL(u'editingFinished()'),
self.onFontFooterXSpinBoxChanged)
QtCore.QObject.connect(self.FontFooterYSpinBox,
QtCore.SIGNAL(u'editingFinished()'), self.onFontFooterYSpinBoxChanged)
QtCore.SIGNAL(u'editingFinished()'),
self.onFontFooterYSpinBoxChanged)
QtCore.QObject.connect(self.FontFooterWidthSpinBox,
QtCore.SIGNAL(u'editingFinished()'), self.onFontFooterWidthSpinBoxChanged)
QtCore.SIGNAL(u'editingFinished()'),
self.onFontFooterWidthSpinBoxChanged)
QtCore.QObject.connect(self.FontFooterHeightSpinBox,
QtCore.SIGNAL(u'editingFinished()'), self.onFontFooterHeightSpinBoxChanged)
QtCore.SIGNAL(u'editingFinished()'),
self.onFontFooterHeightSpinBoxChanged)
QtCore.QObject.connect(self.OutlineCheckBox,
QtCore.SIGNAL(u'stateChanged(int)'), self.onOutlineCheckBoxChanged)
QtCore.QObject.connect(self.ShadowCheckBox,
@ -110,32 +124,51 @@ class AmendThemeForm(QtGui.QDialog, Ui_AmendThemeDialog):
new_theme.add_background_transparent()
else:
if self.theme.background_type == u'solid':
new_theme.add_background_solid(unicode(self.theme.background_color))
new_theme.add_background_solid( \
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), self.theme.background_direction)
new_theme.add_background_gradient( \
unicode(self.theme.background_startColor),
unicode(self.theme.background_endColor),
self.theme.background_direction)
else:
(path, filename) =os.path.split(unicode(self.theme.background_filename))
(path, filename) = \
os.path.split(unicode(self.theme.background_filename))
new_theme.add_background_image(filename)
save_to= os.path.join(self.path, theme_name, filename )
save_from = self.theme.background_filename
new_theme.add_font(unicode(self.theme.font_main_name), unicode(self.theme.font_main_color),
unicode(self.theme.font_main_proportion), unicode(self.theme.font_main_override), u'main',
unicode(self.theme.font_main_weight), unicode(self.theme.font_main_italics),
unicode(self.theme.font_main_x), unicode(self.theme.font_main_y), unicode(self.theme.font_main_width),
new_theme.add_font(unicode(self.theme.font_main_name),
unicode(self.theme.font_main_color),
unicode(self.theme.font_main_proportion),
unicode(self.theme.font_main_override), u'main',
unicode(self.theme.font_main_weight),
unicode(self.theme.font_main_italics),
unicode(self.theme.font_main_x),
unicode(self.theme.font_main_y),
unicode(self.theme.font_main_width),
unicode(self.theme.font_main_height))
new_theme.add_font(unicode(self.theme.font_footer_name), unicode(self.theme.font_footer_color),
unicode(self.theme.font_footer_proportion), unicode(self.theme.font_footer_override), u'footer',
unicode(self.theme.font_footer_weight), unicode(self.theme.font_footer_italics),
unicode(self.theme.font_footer_x), unicode(self.theme.font_footer_y), unicode(self.theme.font_footer_width),
new_theme.add_font(unicode(self.theme.font_footer_name),
unicode(self.theme.font_footer_color),
unicode(self.theme.font_footer_proportion),
unicode(self.theme.font_footer_override), u'footer',
unicode(self.theme.font_footer_weight),
unicode(self.theme.font_footer_italics),
unicode(self.theme.font_footer_x),
unicode(self.theme.font_footer_y),
unicode(self.theme.font_footer_width),
unicode(self.theme.font_footer_height) )
new_theme.add_display(unicode(self.theme.display_shadow), 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),
new_theme.add_display(unicode(self.theme.display_shadow),
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))
theme = new_theme.extract_xml()
if self.thememanager.saveTheme(theme_name, theme, save_from, save_to) is not False:
pretty_theme = new_theme.extract_formatted_xml()
if self.thememanager.saveTheme(theme_name, theme, pretty_theme,
save_from, save_to) is not False:
return QtGui.QDialog.accept(self)
def loadTheme(self, theme):
@ -199,8 +232,10 @@ class AmendThemeForm(QtGui.QDialog, Ui_AmendThemeDialog):
else:
self.theme.font_main_override = True
if int(self.theme.font_main_x) == 0 and int(self.theme.font_main_y) == 0 and \
int(self.theme.font_main_width) == 0 and int(self.theme.font_main_height) == 0:
if int(self.theme.font_main_x) == 0 and \
int(self.theme.font_main_y) == 0 and \
int(self.theme.font_main_width) == 0 and \
int(self.theme.font_main_height) == 0:
self.theme.font_main_x = u'10'
self.theme.font_main_y = u'10'
self.theme.font_main_width = u'1024'
@ -208,7 +243,8 @@ class AmendThemeForm(QtGui.QDialog, Ui_AmendThemeDialog):
self.FontMainXSpinBox.setValue(int(self.theme.font_main_x))
self.FontMainYSpinBox.setValue(int(self.theme.font_main_y))
self.FontMainWidthSpinBox.setValue(int(self.theme.font_main_width))
self.FontMainHeightSpinBox.setValue(int(self.theme.font_main_height))
self.FontMainHeightSpinBox.setValue(int( \
self.theme.font_main_height))
self.stateChanging(self.theme)
self.previewTheme(self.theme)
@ -235,7 +271,8 @@ class AmendThemeForm(QtGui.QDialog, Ui_AmendThemeDialog):
#Footer Font Tab
#
def onFontFooterComboBoxSelected(self):
self.theme.font_footer_name = self.FontFooterComboBox.currentFont().family()
self.theme.font_footer_name = \
self.FontFooterComboBox.currentFont().family()
self.previewTheme(self.theme)
def onFontFooterWeightComboBoxSelected(self, value):
@ -262,8 +299,10 @@ class AmendThemeForm(QtGui.QDialog, Ui_AmendThemeDialog):
self.previewTheme(self.theme)
def onFontFooterSizeSpinBoxChanged(self):
if self.theme.font_footer_proportion != self.FontFooterSizeSpinBox.value():
self.theme.font_footer_proportion = self.FontFooterSizeSpinBox.value()
if self.theme.font_footer_proportion != \
self.FontFooterSizeSpinBox.value():
self.theme.font_footer_proportion = \
self.FontFooterSizeSpinBox.value()
self.previewTheme(self.theme)
def onFontFooterDefaultCheckBoxChanged(self, value):
@ -272,8 +311,10 @@ class AmendThemeForm(QtGui.QDialog, Ui_AmendThemeDialog):
else:
self.theme.font_footer_override = True
if int(self.theme.font_footer_x) == 0 and int(self.theme.font_footer_y) == 0 and \
int(self.theme.font_footer_width) == 0 and int(self.theme.font_footer_height) == 0:
if int(self.theme.font_footer_x) == 0 and \
int(self.theme.font_footer_y) == 0 and \
int(self.theme.font_footer_width) == 0 and \
int(self.theme.font_footer_height) == 0:
self.theme.font_footer_x = u'10'
self.theme.font_footer_y = u'730'
self.theme.font_footer_width = u'1024'
@ -281,8 +322,10 @@ class AmendThemeForm(QtGui.QDialog, Ui_AmendThemeDialog):
self.FontFooterXSpinBox.setValue(int(self.theme.font_footer_x))
self.FontFooterYSpinBox.setValue(int(self.theme.font_footer_y))
self.FontFooterWidthSpinBox.setValue(int(self.theme.font_footer_width))
self.FontFooterHeightSpinBox.setValue(int(self.theme.font_footer_height))
self.FontFooterWidthSpinBox.setValue(int( \
self.theme.font_footer_width))
self.FontFooterHeightSpinBox.setValue(int( \
self.theme.font_footer_height))
self.stateChanging(self.theme)
self.previewTheme(self.theme)
@ -303,14 +346,16 @@ class AmendThemeForm(QtGui.QDialog, Ui_AmendThemeDialog):
self.previewTheme(self.theme)
def onFontFooterHeightSpinBoxChanged(self):
if self.theme.font_footer_height != self.FontFooterHeightSpinBox.value():
if self.theme.font_footer_height != \
self.FontFooterHeightSpinBox.value():
self.theme.font_footer_height = self.FontFooterHeightSpinBox.value()
self.previewTheme(self.theme)
#
#Background Tab
#
def onGradientComboBoxSelected(self, currentIndex):
self.setBackground(self.BackgroundTypeComboBox.currentIndex(), currentIndex)
self.setBackground(self.BackgroundTypeComboBox.currentIndex(),
currentIndex)
def onBackgroundComboBoxSelected(self, currentIndex):
if currentIndex == 0: # Opaque
@ -355,7 +400,8 @@ class AmendThemeForm(QtGui.QDialog, Ui_AmendThemeDialog):
self.theme.background_startColor = QtGui.QColorDialog.getColor(
QtGui.QColor(self.theme.background_startColor), self).name()
self.Color1PushButton.setStyleSheet(
u'background-color: %s' % unicode(self.theme.background_startColor))
u'background-color: %s' % \
unicode(self.theme.background_startColor))
self.previewTheme(self.theme)
@ -416,9 +462,12 @@ class AmendThemeForm(QtGui.QDialog, Ui_AmendThemeDialog):
newtheme = ThemeXML()
newtheme.new_document(u'New Theme')
newtheme.add_background_solid(unicode(u'#000000'))
newtheme.add_font(unicode(QtGui.QFont().family()), unicode(u'#FFFFFF'), unicode(30), u'False')
newtheme.add_font(unicode(QtGui.QFont().family()), unicode(u'#FFFFFF'), unicode(12), u'False', u'footer')
newtheme.add_display(u'False', unicode(u'#FFFFFF'), u'False', unicode(u'#FFFFFF'),
newtheme.add_font(unicode(QtGui.QFont().family()), unicode(u'#FFFFFF'),
unicode(30), u'False')
newtheme.add_font(unicode(QtGui.QFont().family()), unicode(u'#FFFFFF'),
unicode(12), u'False', u'footer')
newtheme.add_display(u'False', unicode(u'#FFFFFF'), u'False',
unicode(u'#FFFFFF'),
unicode(0), unicode(0), unicode(0))
return newtheme.extract_xml()
@ -446,11 +495,14 @@ class AmendThemeForm(QtGui.QDialog, Ui_AmendThemeDialog):
self.GradientComboBox.setCurrentIndex(2)
self.FontMainSizeSpinBox.setValue(int(self.theme.font_main_proportion))
if not self.theme.font_main_italics and self.theme.font_main_weight == u'Normal':
if not self.theme.font_main_italics and \
self.theme.font_main_weight == u'Normal':
self.FontMainWeightComboBox.setCurrentIndex(0)
elif not self.theme.font_main_italics and self.theme.font_main_weight == u'Bold':
elif not self.theme.font_main_italics and \
self.theme.font_main_weight == u'Bold':
self.FontMainWeightComboBox.setCurrentIndex(1)
elif self.theme.font_main_italics and self.theme.font_main_weight == u'Normal':
elif self.theme.font_main_italics and \
self.theme.font_main_weight == u'Normal':
self.FontMainWeightComboBox.setCurrentIndex(2)
else:
self.FontMainWeightComboBox.setCurrentIndex(3)
@ -459,19 +511,24 @@ class AmendThemeForm(QtGui.QDialog, Ui_AmendThemeDialog):
self.FontMainYSpinBox.setValue(int(self.theme.font_main_y))
self.FontMainWidthSpinBox.setValue(int(self.theme.font_main_width))
self.FontMainHeightSpinBox.setValue(int(self.theme.font_main_height))
self.FontFooterSizeSpinBox.setValue(int(self.theme.font_footer_proportion))
if not self.theme.font_footer_italics and self.theme.font_footer_weight == u'Normal':
self.FontFooterSizeSpinBox.setValue(int( \
self.theme.font_footer_proportion))
if not self.theme.font_footer_italics and \
self.theme.font_footer_weight == u'Normal':
self.FontFooterWeightComboBox.setCurrentIndex(0)
elif not self.theme.font_footer_italics and self.theme.font_footer_weight == u'Bold':
elif not self.theme.font_footer_italics and \
self.theme.font_footer_weight == u'Bold':
self.FontFooterWeightComboBox.setCurrentIndex(1)
elif self.theme.font_footer_italics and self.theme.font_footer_weight == u'Normal':
elif self.theme.font_footer_italics and \
self.theme.font_footer_weight == u'Normal':
self.FontFooterWeightComboBox.setCurrentIndex(2)
else:
self.FontFooterWeightComboBox.setCurrentIndex(3)
self.FontFooterXSpinBox.setValue(int(self.theme.font_footer_x))
self.FontFooterYSpinBox.setValue(int(self.theme.font_footer_y))
self.FontFooterWidthSpinBox.setValue(int(self.theme.font_footer_width))
self.FontFooterHeightSpinBox.setValue(int(self.theme.font_footer_height))
self.FontFooterHeightSpinBox.setValue(int( \
self.theme.font_footer_height))
self.FontMainColorPushButton.setStyleSheet(
u'background-color: %s' % unicode(theme.font_main_color))
self.FontFooterColorPushButton.setStyleSheet(
@ -506,8 +563,10 @@ class AmendThemeForm(QtGui.QDialog, Ui_AmendThemeDialog):
self.ShadowCheckBox.setChecked(False)
self.ShadowColorPushButton.setEnabled(False)
self.HorizontalComboBox.setCurrentIndex(int(self.theme.display_horizontalAlign))
self.VerticalComboBox.setCurrentIndex(int(self.theme.display_verticalAlign))
self.HorizontalComboBox.setCurrentIndex(int( \
self.theme.display_horizontalAlign))
self.VerticalComboBox.setCurrentIndex(int( \
self.theme.display_verticalAlign))
def stateChanging(self, theme):
if theme.background_mode == u'transparent':
@ -528,7 +587,8 @@ class AmendThemeForm(QtGui.QDialog, Ui_AmendThemeDialog):
if theme.background_type == u'solid':
self.Color1PushButton.setStyleSheet(
u'background-color: %s' % unicode(theme.background_color))
self.Color1Label.setText(translate(u'ThemeManager', u'Background Color:'))
self.Color1Label.setText(translate(u'ThemeManager',
u'Background Color:'))
self.Color1Label.setVisible(True)
self.Color1PushButton.setVisible(True)
self.Color2Label.setVisible(False)
@ -539,12 +599,14 @@ class AmendThemeForm(QtGui.QDialog, Ui_AmendThemeDialog):
self.GradientLabel.setVisible(False)
self.GradientComboBox.setVisible(False)
elif theme.background_type == u'gradient':
self.Color1PushButton.setStyleSheet(
u'background-color: %s' % unicode(theme.background_startColor))
self.Color2PushButton.setStyleSheet(
u'background-color: %s' % unicode(theme.background_endColor))
self.Color1Label.setText(translate(u'ThemeManager', u'First Color:'))
self.Color2Label.setText(translate(u'ThemeManager', u'Second Color:'))
self.Color1PushButton.setStyleSheet(u'background-color: %s' \
% unicode(theme.background_startColor))
self.Color2PushButton.setStyleSheet(u'background-color: %s' \
% unicode(theme.background_endColor))
self.Color1Label.setText(translate(u'ThemeManager',
u'First Color:'))
self.Color2Label.setText(translate(u'ThemeManager',
u'Second Color:'))
self.Color1Label.setVisible(True)
self.Color1PushButton.setVisible(True)
self.Color2Label.setVisible(True)

View File

@ -300,9 +300,11 @@ class ThemeManager(QtGui.QWidget):
else:
newtheme.add_background_image(unicode(theme.BackgroundParameter1))
newtheme.add_font(unicode(theme.FontName), unicode(theme.FontColor.name()),
newtheme.add_font(unicode(theme.FontName),
unicode(theme.FontColor.name()),
unicode(theme.FontProportion * 2), u'False')
newtheme.add_font(unicode(theme.FontName), unicode(theme.FontColor.name()),
newtheme.add_font(unicode(theme.FontName),
unicode(theme.FontColor.name()),
unicode(12), u'False', u'footer')
outline = False
shadow = False
@ -316,7 +318,8 @@ class ThemeManager(QtGui.QWidget):
unicode(theme.WrapStyle))
return newtheme.extract_xml()
def saveTheme(self, name, theme_xml, image_from, image_to) :
def saveTheme(self, name, theme_xml, theme_pretty_xml, image_from,
image_to) :
"""
Called by thememaintenance Dialog to save the theme
and to trigger the reload of the theme list
@ -339,7 +342,7 @@ class ThemeManager(QtGui.QWidget):
if result == QtGui.QMessageBox.Yes:
# Save the theme, overwriting the existing theme if necessary.
outfile = open(theme_file, u'w')
outfile.write(theme_xml)
outfile.write(theme_pretty_xml)
outfile.close()
if image_from is not None and image_from != image_to:
shutil.copyfile(image_from, image_to)
@ -347,7 +350,8 @@ class ThemeManager(QtGui.QWidget):
self.generateAndSaveImage(self.path, name, theme_xml)
self.loadThemes()
else:
# Don't close the dialog - allow the user to change the name of the theme or to cancel the theme dialog completely.
# Don't close the dialog - allow the user to change the name of
# the theme or to cancel the theme dialog completely.
return False
def generateAndSaveImage(self, dir, name, theme_xml):