Theme manager cleanup

This commit is contained in:
Tim Bentley 2010-12-28 10:56:19 +00:00
parent 399970e7c7
commit ecc46c12c4
2 changed files with 167 additions and 188 deletions

View File

@ -600,7 +600,6 @@ class ThemeForm(QtGui.QWizard, Ui_ThemeWizard):
(QtGui.QMessageBox.Ok),
QtGui.QMessageBox.Ok)
return
self.accepted = True
saveFrom = None
saveTo = None
if self.theme.background_type == \
@ -608,9 +607,12 @@ class ThemeForm(QtGui.QWizard, Ui_ThemeWizard):
filename = \
os.path.split(unicode(self.theme.background_filename))[1]
saveTo = os.path.join(self.path, self.theme.theme_name, filename)
saveFrom = self.theme.background_filename
if self.thememanager.saveTheme(self.theme, saveFrom, saveTo):
saveFrom = self.theme.background_filename#
if self.thememanager.checkIfThemeExists(self.theme.theme_name):
self.thememanager.saveTheme(self.theme, saveFrom, saveTo)
self.accepted = True
return QtGui.QDialog.accept(self)
return
def _colorButton(self, field):
"""
@ -620,4 +622,4 @@ class ThemeForm(QtGui.QWizard, Ui_ThemeWizard):
QtGui.QColor(field), self)
if new_color.isValid():
field = new_color.name()
return field
return field

View File

@ -130,7 +130,6 @@ class ThemeManager(QtGui.QWidget):
self.checkDirectoryExists(self.thumbPath)
self.themeForm.path = self.path
self.oldBackgroundImage = None
self.editingDefault = False
# Last little bits of setting up
self.configUpdated()
@ -143,6 +142,10 @@ class ThemeManager(QtGui.QWidget):
QtCore.QVariant(u'')).toString())
def contextMenu(self, point):
"""
Build the Right Click Context menu and set state depending on
the type of theme.
"""
item = self.themeListWidget.itemAt(point)
if item is None:
return
@ -283,8 +286,6 @@ class ThemeManager(QtGui.QWidget):
'You must select a theme to edit.')):
item = self.themeListWidget.currentItem()
themeName = unicode(item.text())
if themeName != unicode(item.data(QtCore.Qt.UserRole).toString()):
self.editingDefault = True
theme = self.getThemeData(
unicode(item.data(QtCore.Qt.UserRole).toString()))
if theme.background_type == u'image':
@ -447,9 +448,9 @@ class ThemeManager(QtGui.QWidget):
unicode(themeName) + u'.xml')
xml = get_text_file_string(xmlFile)
if not xml:
return self.baseTheme()
return self._baseTheme()
else:
return self.createThemeFromXml(xml, self.path)
return self._createThemeFromXml(xml, self.path)
def checkDirectoryExists(self, dir):
"""
@ -520,7 +521,7 @@ class ThemeManager(QtGui.QWidget):
outfile = open(fullpath, u'wb')
outfile.write(zip.read(file))
if filexml:
theme = self.createThemeFromXml(filexml, self.path)
theme = self._createThemeFromXml(filexml, self.path)
self.generateAndSaveImage(dir, themename, theme)
else:
QtGui.QMessageBox.critical(self,
@ -554,9 +555,161 @@ class ThemeManager(QtGui.QWidget):
if tree.find(u'BackgroundType') is None:
return xml_data
else:
return self.migrateVersion122(xml_data)
return self._migrateVersion122(xml_data)
def migrateVersion122(self, xml_data):
def checkIfThemeExists(self, themeName):
"""
Check if theme already exists and displays error message
``themeName``
Name of the Theme to test
"""
theme_dir = os.path.join(self.path, themeName)
if os.path.exists(theme_dir):
QtGui.QMessageBox.critical(self,
translate('OpenLP.ThemeManager', 'Theme Exists'),
translate('OpenLP.ThemeManager',
'A theme with this name already exists.'),
(QtGui.QMessageBox.Ok), QtGui.QMessageBox.Ok)
return False
return True
def saveTheme(self, theme, imageFrom, imageTo):
"""
Called by thememaintenance Dialog to save the theme
and to trigger the reload of the theme list
"""
name = theme.theme_name
theme_pretty_xml = theme.extract_formatted_xml()
log.debug(u'saveTheme %s %s', name, theme_pretty_xml)
theme_dir = os.path.join(self.path, name)
if not os.path.exists(theme_dir):
os.mkdir(os.path.join(self.path, name))
theme_file = os.path.join(theme_dir, name + u'.xml')
if imageTo and self.oldBackgroundImage and \
imageTo != self.oldBackgroundImage:
try:
os.remove(self.oldBackgroundImage)
except OSError:
log.exception(u'Unable to remove old theme background')
outfile = None
try:
outfile = open(theme_file, u'w')
outfile.write(theme_pretty_xml)
except IOError:
log.exception(u'Saving theme to file failed')
finally:
if outfile:
outfile.close()
if imageFrom and imageFrom != imageTo:
try:
encoding = get_filesystem_encoding()
shutil.copyfile(
unicode(imageFrom).encode(encoding),
unicode(imageTo).encode(encoding))
except IOError:
log.exception(u'Failed to save theme image')
self.generateAndSaveImage(self.path, name, theme)
self.loadThemes()
self.pushThemes()
def generateAndSaveImage(self, dir, name, theme):
log.debug(u'generateAndSaveImage %s %s', dir, name)
theme_xml = theme.extract_xml()
frame = self.generateImage(theme)
samplepathname = os.path.join(self.path, name + u'.png')
if os.path.exists(samplepathname):
os.unlink(samplepathname)
frame.save(samplepathname, u'png')
thumb = os.path.join(self.thumbPath, u'%s.png' % name)
icon = build_icon(frame)
pixmap = icon.pixmap(QtCore.QSize(88, 50))
pixmap.save(thumb, u'png')
log.debug(u'Theme image written to %s', samplepathname)
def generateImage(self, themeData, forcePage=False):
"""
Call the RenderManager to build a Sample Image
``themeData``
The theme to generated a preview for.
``forcePage``
Flag to tell message lines per page need to be generated.
"""
log.debug(u'generateImage \n%s ', themeData)
return self.parent.renderManager.generate_preview(themeData, forcePage)
def getPreviewImage(self, theme):
"""
Return an image representing the look of the theme
``theme``
The theme to return the image for
"""
log.debug(u'getPreviewImage %s ', theme)
image = os.path.join(self.path, theme + u'.png')
return image
def _baseTheme(self):
"""
Provide a base theme with sensible defaults
"""
log.debug(u'base theme created')
newtheme = ThemeXML()
return newtheme
def _createThemeFromXml(self, themeXml, path):
"""
Return a theme object using information parsed from XML
``themeXml``
The XML data to load into the theme
"""
theme = ThemeXML()
theme.parse(themeXml)
theme.extend_image_filename(path)
return theme
def _validate_theme_action(self, action, testPlugin=True):
"""
Check to see if theme has been selected and the destructive action
is allowed.
"""
if check_item_selected(self.themeListWidget,
unicode(translate('OpenLP.ThemeManager',
'You must select a theme to %s.')) % action):
item = self.themeListWidget.currentItem()
theme = unicode(item.text())
# confirm destructive action
answer = QtGui.QMessageBox.question(self,
unicode(translate('OpenLP.ThemeManager', '%s Confirmation'))
% action,
unicode(translate('OpenLP.ThemeManager', '%s %s theme?'))
% (action, theme),
QtGui.QMessageBox.StandardButtons(QtGui.QMessageBox.Yes |
QtGui.QMessageBox.No), QtGui.QMessageBox.No)
if answer == QtGui.QMessageBox.No:
return False
# check for use in the system else where.
if testPlugin:
for plugin in self.parent.pluginManager.plugins:
if plugin.usesTheme(theme):
QtGui.QMessageBox.critical(self,
translate('OpenLP.ThemeManager', 'Error'),
unicode(translate('OpenLP.ThemeManager',
'Theme %s is used in the %s plugin.')) % \
(theme, plugin.name))
return False
if unicode(self.serviceComboBox.currentText()) == theme:
QtGui.QMessageBox.critical(self,
translate('OpenLP.ThemeManager', 'Error'),
unicode(translate('OpenLP.ThemeManager',
'Theme %s is used by the service manager.')) % theme)
return False
return True
def _migrateVersion122(self, xml_data):
"""
Convert the xml data from version 1 format to the current format.
@ -613,179 +766,3 @@ class ThemeManager(QtGui.QWidget):
newtheme.display_horizontal_align = theme.HorizontalAlign
newtheme.display_vertical_align = vAlignCorrection
return newtheme.extract_xml()
def checkIfThemeExists(self, themeName):
"""
Check if theme already exists and displays error message
"""
theme_dir = os.path.join(self.path, themeName)
if os.path.exists(theme_dir):
QtGui.QMessageBox.critical(self,
translate('OpenLP.ThemeManager', 'Theme Exists'),
translate('OpenLP.ThemeManager',
'A theme with this name already exists.'),
(QtGui.QMessageBox.Ok), QtGui.QMessageBox.Ok)
return False
return True
def saveTheme(self, theme, imageFrom, imageTo):
"""
Called by thememaintenance Dialog to save the theme
and to trigger the reload of the theme list
"""
name = theme.theme_name
theme_pretty_xml = theme.extract_formatted_xml()
log.debug(u'saveTheme %s %s', name, theme_pretty_xml)
theme_dir = os.path.join(self.path, name)
if not os.path.exists(theme_dir):
os.mkdir(os.path.join(self.path, name))
theme_file = os.path.join(theme_dir, name + u'.xml')
log.debug(theme_file)
editedServiceTheme = False
#result = QtGui.QMessageBox.Yes
#if result == QtGui.QMessageBox.Yes:
# Save the theme, overwriting the existing theme if necessary.
if imageTo and self.oldBackgroundImage and \
imageTo != self.oldBackgroundImage:
try:
os.remove(self.oldBackgroundImage)
except OSError:
log.exception(u'Unable to remove old theme background')
outfile = None
try:
outfile = open(theme_file, u'w')
outfile.write(theme_pretty_xml)
except IOError:
log.exception(u'Saving theme to file failed')
finally:
if outfile:
outfile.close()
if imageFrom and imageFrom != imageTo:
try:
encoding = get_filesystem_encoding()
shutil.copyfile(
unicode(imageFrom).encode(encoding),
unicode(imageTo).encode(encoding))
except IOError:
log.exception(u'Failed to save theme image')
self.generateAndSaveImage(self.path, name, theme)
self.loadThemes()
# Check if we need to set a new service theme
# if editedServiceTheme:
# newThemeIndex = self.serviceComboBox.findText(name)
# if newThemeIndex != -1:
# self.serviceComboBox.setCurrentIndex(newThemeIndex)
# if self.editingDefault:
# if self.saveThemeName != name:
# newThemeItem = self.themeListWidget.findItems(name,
# QtCore.Qt.MatchExactly)[0]
# newThemeIndex = self.themeListWidget.indexFromItem(
# newThemeItem).row()
# self.global_theme = unicode(
# self.themeListWidget.item(newThemeIndex).text())
# newName = unicode(translate('OpenLP.ThemeManager',
# '%s (default)')) % self.global_theme
# self.themeListWidget.item(newThemeIndex).setText(newName)
# QtCore.QSettings().setValue(
# self.settingsSection + u'/global theme',
# QtCore.QVariant(self.global_theme))
# Receiver.send_message(u'theme_update_global',
# self.global_theme)
# self.editingDefault = False
self.pushThemes()
def generateAndSaveImage(self, dir, name, theme):
log.debug(u'generateAndSaveImage %s %s', dir, name)
theme_xml = theme.extract_xml()
frame = self.generateImage(theme)
samplepathname = os.path.join(self.path, name + u'.png')
if os.path.exists(samplepathname):
os.unlink(samplepathname)
frame.save(samplepathname, u'png')
thumb = os.path.join(self.thumbPath, u'%s.png' % name)
icon = build_icon(frame)
pixmap = icon.pixmap(QtCore.QSize(88, 50))
pixmap.save(thumb, u'png')
log.debug(u'Theme image written to %s', samplepathname)
def generateImage(self, themeData, forcePage=False):
"""
Call the RenderManager to build a Sample Image
``themeData``
The theme to generated a preview for.
``forcePage``
Flag to tell message lines per page need to be generated.
"""
log.debug(u'generateImage \n%s ', themeData)
return self.parent.renderManager.generate_preview(themeData, forcePage)
def getPreviewImage(self, theme):
"""
Return an image representing the look of the theme
``theme``
The theme to return the image for
"""
log.debug(u'getPreviewImage %s ', theme)
image = os.path.join(self.path, theme + u'.png')
return image
def baseTheme(self):
"""
Provide a base theme with sensible defaults
"""
log.debug(u'base theme created')
newtheme = ThemeXML()
return newtheme
def createThemeFromXml(self, themeXml, path):
"""
Return a theme object using information parsed from XML
``themeXml``
The XML data to load into the theme
"""
theme = ThemeXML()
theme.parse(themeXml)
theme.extend_image_filename(path)
return theme
def _validate_theme_action(self, action, testPlugin=True):
"""
Check to see if theme has been selected and the destructive action
is allowed.
"""
if check_item_selected(self.themeListWidget,
unicode(translate('OpenLP.ThemeManager',
'You must select a theme to %s.')) % action):
item = self.themeListWidget.currentItem()
theme = unicode(item.text())
# confirm destructive action
answer = QtGui.QMessageBox.question(self,
unicode(translate('OpenLP.ThemeManager', '%s Confirmation'))
% action,
unicode(translate('OpenLP.ThemeManager', '%s %s theme?'))
% (action, theme),
QtGui.QMessageBox.StandardButtons(QtGui.QMessageBox.Yes |
QtGui.QMessageBox.No), QtGui.QMessageBox.No)
if answer == QtGui.QMessageBox.No:
return False
# check for use in the system else where.
if testPlugin:
for plugin in self.parent.pluginManager.plugins:
if plugin.usesTheme(theme):
QtGui.QMessageBox.critical(self,
translate('OpenLP.ThemeManager', 'Error'),
unicode(translate('OpenLP.ThemeManager',
'Theme %s is used in the %s plugin.')) % \
(theme, plugin.name))
return False
if unicode(self.serviceComboBox.currentText()) == theme:
QtGui.QMessageBox.critical(self,
translate('OpenLP.ThemeManager', 'Error'),
unicode(translate('OpenLP.ThemeManager',
'Theme %s is used by the service manager.')) % theme)
return False
return True