More Theme Management cleanups

This commit is contained in:
Tim Bentley 2010-12-27 10:18:09 +00:00
parent a464ab791b
commit 399970e7c7
4 changed files with 108 additions and 118 deletions

View File

@ -52,4 +52,4 @@ class FileRenameForm(QtGui.QDialog, Ui_FileRenameDialog):
else: else:
self.setWindowTitle(translate('OpenLP.FileRenameForm', self.setWindowTitle(translate('OpenLP.FileRenameForm',
'File Rename')) 'File Rename'))
return QtGui.QDialog.exec_(self) return QtGui.QDialog.exec_(self)

View File

@ -108,11 +108,11 @@ class ServiceManager(QtGui.QWidget):
self.suffixes = [] self.suffixes = []
self.droppos = 0 self.droppos = 0
self.expandTabs = False self.expandTabs = False
#is a new service and has not been saved # is a new service and has not been saved
self.isNew = True self.isNew = True
self.serviceNoteForm = ServiceNoteForm(self.parent) self.serviceNoteForm = ServiceNoteForm(self.parent)
self.serviceItemEditForm = ServiceItemEditForm(self.parent) self.serviceItemEditForm = ServiceItemEditForm(self.parent)
#start with the layout # start with the layout
self.layout = QtGui.QVBoxLayout(self) self.layout = QtGui.QVBoxLayout(self)
self.layout.setSpacing(0) self.layout.setSpacing(0)
self.layout.setMargin(0) self.layout.setMargin(0)
@ -245,7 +245,7 @@ class ServiceManager(QtGui.QWidget):
self.parent.serviceSettingsSection + u'/service theme', self.parent.serviceSettingsSection + u'/service theme',
QtCore.QVariant(u'')).toString()) QtCore.QVariant(u'')).toString())
self.servicePath = AppLocation.get_section_data_path(u'servicemanager') self.servicePath = AppLocation.get_section_data_path(u'servicemanager')
#build the drag and drop context menu # build the drag and drop context menu
self.dndMenu = QtGui.QMenu() self.dndMenu = QtGui.QMenu()
self.newAction = self.dndMenu.addAction( self.newAction = self.dndMenu.addAction(
translate('OpenLP.ServiceManager', '&Add New Item')) translate('OpenLP.ServiceManager', '&Add New Item'))
@ -1119,4 +1119,4 @@ class ServiceManager(QtGui.QWidget):
data_item[u'notes'] = unicode(service_item.notes) data_item[u'notes'] = unicode(service_item.notes)
data_item[u'selected'] = (item == curitem) data_item[u'selected'] = (item == curitem)
data.append(data_item) data.append(data_item)
Receiver.send_message(u'servicemanager_list_response', data) Receiver.send_message(u'servicemanager_list_response', data)

View File

@ -114,22 +114,30 @@ class ThemeManager(QtGui.QWidget):
self.exportAction = self.menu.addAction( self.exportAction = self.menu.addAction(
translate('OpenLP.ThemeManager', '&Export Theme')) translate('OpenLP.ThemeManager', '&Export Theme'))
self.exportAction.setIcon(build_icon(u':/general/general_export.png')) self.exportAction.setIcon(build_icon(u':/general/general_export.png'))
#Signals # Signals
QtCore.QObject.connect(self.themeListWidget, QtCore.QObject.connect(self.themeListWidget,
QtCore.SIGNAL(u'doubleClicked(QModelIndex)'), QtCore.SIGNAL(u'doubleClicked(QModelIndex)'),
self.changeGlobalFromScreen) self.changeGlobalFromScreen)
QtCore.QObject.connect(Receiver.get_receiver(), QtCore.QObject.connect(Receiver.get_receiver(),
QtCore.SIGNAL(u'theme_update_global'), self.changeGlobalFromTab) QtCore.SIGNAL(u'theme_update_global'), self.changeGlobalFromTab)
#Variables QtCore.QObject.connect(Receiver.get_receiver(),
QtCore.SIGNAL(u'config_updated'), self.configUpdated)
# Variables
self.themelist = [] self.themelist = []
self.path = AppLocation.get_section_data_path(self.settingsSection) self.path = AppLocation.get_section_data_path(self.settingsSection)
self.checkThemesExists(self.path) self.checkDirectoryExists(self.path)
self.thumbPath = os.path.join(self.path, u'thumbnails') self.thumbPath = os.path.join(self.path, u'thumbnails')
self.checkThemesExists(self.thumbPath) self.checkDirectoryExists(self.thumbPath)
self.themeForm.path = self.path self.themeForm.path = self.path
self.oldBackgroundImage = None self.oldBackgroundImage = None
self.editingDefault = False self.editingDefault = False
# Last little bits of setting up # Last little bits of setting up
self.configUpdated()
def configUpdated(self, firstTime=False):
"""
Triggered when Config dialog is updated.
"""
self.global_theme = unicode(QtCore.QSettings().value( self.global_theme = unicode(QtCore.QSettings().value(
self.settingsSection + u'/global theme', self.settingsSection + u'/global theme',
QtCore.QVariant(u'')).toString()) QtCore.QVariant(u'')).toString())
@ -215,7 +223,6 @@ class ThemeManager(QtGui.QWidget):
editing form for the user to make their customisations. editing form for the user to make their customisations.
""" """
theme = ThemeXML() theme = ThemeXML()
self.saveThemeName = u''
self.themeForm.theme = theme self.themeForm.theme = theme
self.themeForm.exec_() self.themeForm.exec_()
@ -228,15 +235,15 @@ class ThemeManager(QtGui.QWidget):
item = self.themeListWidget.currentItem() item = self.themeListWidget.currentItem()
oldThemeName = unicode(item.data(QtCore.Qt.UserRole).toString()) oldThemeName = unicode(item.data(QtCore.Qt.UserRole).toString())
self.fileRenameForm.fileNameEdit.setText(oldThemeName) self.fileRenameForm.fileNameEdit.setText(oldThemeName)
self.saveThemeName = oldThemeName
if self.fileRenameForm.exec_(): if self.fileRenameForm.exec_():
newThemeName = unicode(self.fileRenameForm.fileNameEdit.text()) newThemeName = unicode(self.fileRenameForm.fileNameEdit.text())
oldThemeData = self.getThemeData(oldThemeName) if self.checkIfThemeExists(newThemeName):
self.deleteTheme(oldThemeName) oldThemeData = self.getThemeData(oldThemeName)
self.cloneThemeData(oldThemeData, newThemeName) self.deleteTheme(oldThemeName)
for plugin in self.parent.pluginManager.plugins: self.cloneThemeData(oldThemeData, newThemeName)
if plugin.usesTheme(oldThemeName): for plugin in self.parent.pluginManager.plugins:
plugin.renameTheme(oldThemeName, newThemeName) if plugin.usesTheme(oldThemeName):
plugin.renameTheme(oldThemeName, newThemeName)
def onCopyTheme(self): def onCopyTheme(self):
""" """
@ -245,12 +252,12 @@ class ThemeManager(QtGui.QWidget):
item = self.themeListWidget.currentItem() item = self.themeListWidget.currentItem()
oldThemeName = unicode(item.data(QtCore.Qt.UserRole).toString()) oldThemeName = unicode(item.data(QtCore.Qt.UserRole).toString())
self.fileRenameForm.fileNameEdit.setText(oldThemeName) self.fileRenameForm.fileNameEdit.setText(oldThemeName)
self.saveThemeName = u''
if self.fileRenameForm.exec_(True): if self.fileRenameForm.exec_(True):
newThemeName = unicode(self.fileRenameForm.fileNameEdit.text()) newThemeName = unicode(self.fileRenameForm.fileNameEdit.text())
themeData = self.getThemeData(oldThemeName) if self.checkIfThemeExists(newThemeName):
self.cloneThemeData(themeData, newThemeName) themeData = self.getThemeData(oldThemeName)
self.loadThemes() self.cloneThemeData(themeData, newThemeName)
self.loadThemes()
def cloneThemeData(self, themeData, newThemeName): def cloneThemeData(self, themeData, newThemeName):
""" """
@ -282,8 +289,6 @@ class ThemeManager(QtGui.QWidget):
unicode(item.data(QtCore.Qt.UserRole).toString())) unicode(item.data(QtCore.Qt.UserRole).toString()))
if theme.background_type == u'image': if theme.background_type == u'image':
self.oldBackgroundImage = theme.background_filename self.oldBackgroundImage = theme.background_filename
self.saveThemeName = unicode(
item.data(QtCore.Qt.UserRole).toString())
self.themeForm.theme = theme self.themeForm.theme = theme
self.themeForm.exec_(True) self.themeForm.exec_(True)
@ -446,7 +451,7 @@ class ThemeManager(QtGui.QWidget):
else: else:
return self.createThemeFromXml(xml, self.path) return self.createThemeFromXml(xml, self.path)
def checkThemesExists(self, dir): def checkDirectoryExists(self, dir):
""" """
Check a theme directory exists and if not create it Check a theme directory exists and if not create it
@ -609,6 +614,20 @@ class ThemeManager(QtGui.QWidget):
newtheme.display_vertical_align = vAlignCorrection newtheme.display_vertical_align = vAlignCorrection
return newtheme.extract_xml() 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): def saveTheme(self, theme, imageFrom, imageTo):
""" """
Called by thememaintenance Dialog to save the theme Called by thememaintenance Dialog to save the theme
@ -623,77 +642,57 @@ class ThemeManager(QtGui.QWidget):
theme_file = os.path.join(theme_dir, name + u'.xml') theme_file = os.path.join(theme_dir, name + u'.xml')
log.debug(theme_file) log.debug(theme_file)
editedServiceTheme = False editedServiceTheme = False
result = QtGui.QMessageBox.Yes #result = QtGui.QMessageBox.Yes
if self.saveThemeName != name: #if result == QtGui.QMessageBox.Yes:
if os.path.exists(theme_file):
result = QtGui.QMessageBox.question(self,
translate('OpenLP.ThemeManager', 'Theme Exists'),
translate('OpenLP.ThemeManager',
'A theme with this name already '
'exists. Would you like to overwrite it?'),
(QtGui.QMessageBox.Yes | QtGui.QMessageBox.No),
QtGui.QMessageBox.No)
if self.saveThemeName != u'':
for plugin in self.parent.pluginManager.plugins:
if plugin.usesTheme(self.saveThemeName):
plugin.renameTheme(self.saveThemeName, name)
if unicode(self.serviceComboBox.currentText()) == name:
editedServiceTheme = True
if result == QtGui.QMessageBox.Yes:
# Save the theme, overwriting the existing theme if necessary. # Save the theme, overwriting the existing theme if necessary.
if imageTo and self.oldBackgroundImage and \ if imageTo and self.oldBackgroundImage and \
imageTo != self.oldBackgroundImage: imageTo != self.oldBackgroundImage:
try:
os.remove(self.oldBackgroundImage)
except OSError:
log.exception(u'Unable to remove old theme background')
outfile = None
try: try:
outfile = open(theme_file, u'w') os.remove(self.oldBackgroundImage)
outfile.write(theme_pretty_xml) 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: except IOError:
log.exception(u'Saving theme to file failed') log.exception(u'Failed to save theme image')
finally: self.generateAndSaveImage(self.path, name, theme)
if outfile: self.loadThemes()
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 # Check if we need to set a new service theme
if editedServiceTheme: # if editedServiceTheme:
newThemeIndex = self.serviceComboBox.findText(name) # newThemeIndex = self.serviceComboBox.findText(name)
if newThemeIndex != -1: # if newThemeIndex != -1:
self.serviceComboBox.setCurrentIndex(newThemeIndex) # self.serviceComboBox.setCurrentIndex(newThemeIndex)
if self.editingDefault: # if self.editingDefault:
if self.saveThemeName != name: # if self.saveThemeName != name:
newThemeItem = self.themeListWidget.findItems(name, # newThemeItem = self.themeListWidget.findItems(name,
QtCore.Qt.MatchExactly)[0] # QtCore.Qt.MatchExactly)[0]
newThemeIndex = self.themeListWidget.indexFromItem( # newThemeIndex = self.themeListWidget.indexFromItem(
newThemeItem).row() # newThemeItem).row()
self.global_theme = unicode( # self.global_theme = unicode(
self.themeListWidget.item(newThemeIndex).text()) # self.themeListWidget.item(newThemeIndex).text())
newName = unicode(translate('OpenLP.ThemeManager', # newName = unicode(translate('OpenLP.ThemeManager',
'%s (default)')) % self.global_theme # '%s (default)')) % self.global_theme
self.themeListWidget.item(newThemeIndex).setText(newName) # self.themeListWidget.item(newThemeIndex).setText(newName)
QtCore.QSettings().setValue( # QtCore.QSettings().setValue(
self.settingsSection + u'/global theme', # self.settingsSection + u'/global theme',
QtCore.QVariant(self.global_theme)) # QtCore.QVariant(self.global_theme))
Receiver.send_message(u'theme_update_global', # Receiver.send_message(u'theme_update_global',
self.global_theme) # self.global_theme)
self.editingDefault = False # self.editingDefault = False
self.pushThemes() self.pushThemes()
return True
else:
# 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): def generateAndSaveImage(self, dir, name, theme):
log.debug(u'generateAndSaveImage %s %s', dir, name) log.debug(u'generateAndSaveImage %s %s', dir, name)
@ -758,15 +757,12 @@ class ThemeManager(QtGui.QWidget):
Check to see if theme has been selected and the destructive action Check to see if theme has been selected and the destructive action
is allowed. is allowed.
""" """
self.global_theme = unicode(QtCore.QSettings().value(
self.settingsSection + u'/global theme',
QtCore.QVariant(u'')).toString())
if check_item_selected(self.themeListWidget, if check_item_selected(self.themeListWidget,
unicode(translate('OpenLP.ThemeManager', unicode(translate('OpenLP.ThemeManager',
'You must select a theme to %s.')) % action): 'You must select a theme to %s.')) % action):
item = self.themeListWidget.currentItem() item = self.themeListWidget.currentItem()
theme = unicode(item.text()) theme = unicode(item.text())
# confirm deletion # confirm destructive action
answer = QtGui.QMessageBox.question(self, answer = QtGui.QMessageBox.question(self,
unicode(translate('OpenLP.ThemeManager', '%s Confirmation')) unicode(translate('OpenLP.ThemeManager', '%s Confirmation'))
% action, % action,
@ -776,26 +772,20 @@ class ThemeManager(QtGui.QWidget):
QtGui.QMessageBox.No), QtGui.QMessageBox.No) QtGui.QMessageBox.No), QtGui.QMessageBox.No)
if answer == QtGui.QMessageBox.No: if answer == QtGui.QMessageBox.No:
return False return False
# should be the same unless default # check for use in the system else where.
if theme != unicode(item.data(QtCore.Qt.UserRole).toString()): 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, QtGui.QMessageBox.critical(self,
translate('OpenLP.ThemeManager', 'Error'), translate('OpenLP.ThemeManager', 'Error'),
translate('OpenLP.ThemeManager', unicode(translate('OpenLP.ThemeManager',
'You are unable to delete the default theme.')) 'Theme %s is used by the service manager.')) % theme)
else: return False
if testPlugin: return True
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

View File

@ -197,7 +197,7 @@ class SongMediaItem(MediaManagerItem):
elif search_type == 3: elif search_type == 3:
log.debug(u'Theme Search') log.debug(u'Theme Search')
search_results = self.parent.manager.get_all_objects(Song, search_results = self.parent.manager.get_all_objects(Song,
Song.theme_name.like(u'%' + search_keywords + u'%'), Song.theme_name == search_keywords,
Song.search_lyrics.asc()) Song.search_lyrics.asc())
self.displayResultsSong(search_results) self.displayResultsSong(search_results)
@ -465,4 +465,4 @@ class SongMediaItem(MediaManagerItem):
""" """
Locale aware collation of song titles Locale aware collation of song titles
""" """
return locale.strcoll(unicode(song_1.title), unicode(song_2.title)) return locale.strcoll(unicode(song_1.title), unicode(song_2.title))