forked from openlp/openlp
Fix theme loading mess
This commit is contained in:
parent
e9a8fa7084
commit
a037c3ee2b
File diff suppressed because it is too large
Load Diff
@ -1,380 +1,443 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
# vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4
|
# vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4
|
||||||
"""
|
"""
|
||||||
OpenLP - Open Source Lyrics Projection
|
OpenLP - Open Source Lyrics Projection
|
||||||
Copyright (c) 2009 Raoul Snyman
|
Copyright (c) 2009 Raoul Snyman
|
||||||
Portions copyright (c) 2009 Martin Thompson, Tim Bentley,
|
Portions copyright (c) 2009 Martin Thompson, Tim Bentley,
|
||||||
|
|
||||||
This program is free software; you can redistribute it and/or modify it under
|
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
|
the terms of the GNU General Public License as published by the Free Software
|
||||||
Foundation; version 2 of the License.
|
Foundation; version 2 of the License.
|
||||||
|
|
||||||
This program is distributed in the hope that it will be useful, but WITHOUT ANY
|
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
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
||||||
PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
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
|
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
|
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
|
||||||
Place, Suite 330, Boston, MA 02111-1307 USA
|
Place, Suite 330, Boston, MA 02111-1307 USA
|
||||||
"""
|
"""
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import zipfile
|
import zipfile
|
||||||
import shutil
|
import shutil
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from xml.etree.ElementTree import ElementTree, XML
|
from xml.etree.ElementTree import ElementTree, XML
|
||||||
from PyQt4 import QtCore, QtGui
|
from PyQt4 import QtCore, QtGui
|
||||||
|
|
||||||
from openlp.core.ui import AmendThemeForm, ServiceManager
|
from openlp.core.ui import AmendThemeForm, ServiceManager
|
||||||
from openlp.core.theme import Theme
|
from openlp.core.theme import Theme
|
||||||
from openlp.core.lib import PluginConfig, \
|
from openlp.core.lib import PluginConfig, OpenLPToolbar, ThemeXML, Renderer, \
|
||||||
OpenLPToolbar, ThemeXML, Renderer, translate, \
|
translate, str_to_bool, file_to_xml, buildIcon, Receiver
|
||||||
file_to_xml, buildIcon, Receiver
|
from openlp.core.utils import ConfigHelper
|
||||||
from openlp.core.utils import ConfigHelper
|
|
||||||
|
class ThemeManager(QtGui.QWidget):
|
||||||
class ThemeManager(QtGui.QWidget):
|
"""
|
||||||
"""
|
Manages the orders of Theme.
|
||||||
Manages the orders of Theme.
|
"""
|
||||||
"""
|
global log
|
||||||
global log
|
log = logging.getLogger(u'ThemeManager')
|
||||||
log = logging.getLogger(u'ThemeManager')
|
|
||||||
|
def __init__(self, parent):
|
||||||
def __init__(self, parent):
|
QtGui.QWidget.__init__(self, parent)
|
||||||
QtGui.QWidget.__init__(self, parent)
|
self.parent = parent
|
||||||
self.parent = parent
|
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)
|
self.amendThemeForm = AmendThemeForm(self)
|
||||||
self.amendThemeForm = AmendThemeForm(self)
|
self.Toolbar = OpenLPToolbar(self)
|
||||||
self.Toolbar = OpenLPToolbar(self)
|
self.Toolbar.addToolbarButton(
|
||||||
self.Toolbar.addToolbarButton(
|
translate(u'ThemeManager', u'New Theme'), u':/themes/theme_new.png',
|
||||||
translate(u'ThemeManager', u'New Theme'), u':/themes/theme_new.png',
|
translate(u'ThemeManager', u'Create a new theme'), self.onAddTheme)
|
||||||
translate(u'ThemeManager', u'Create a new theme'), self.onAddTheme)
|
self.Toolbar.addToolbarButton(
|
||||||
self.Toolbar.addToolbarButton(
|
translate(u'ThemeManager', u'Edit Theme'),
|
||||||
translate(u'ThemeManager', u'Edit Theme'), u':/themes/theme_edit.png',
|
u':/themes/theme_edit.png',
|
||||||
translate(u'ThemeManager', u'Edit a theme'), self.onEditTheme)
|
translate(u'ThemeManager', u'Edit a theme'), self.onEditTheme)
|
||||||
self.Toolbar.addToolbarButton(
|
self.Toolbar.addToolbarButton(
|
||||||
translate(u'ThemeManager', u'Delete Theme'), u':/themes/theme_delete.png',
|
translate(u'ThemeManager', u'Delete Theme'),
|
||||||
translate(u'ThemeManager', u'Delete a theme'), self.onDeleteTheme)
|
u':/themes/theme_delete.png',
|
||||||
self.Toolbar.addSeparator()
|
translate(u'ThemeManager', u'Delete a theme'), self.onDeleteTheme)
|
||||||
self.Toolbar.addToolbarButton(
|
self.Toolbar.addSeparator()
|
||||||
translate(u'ThemeManager', u'Import Theme'), u':/themes/theme_import.png',
|
self.Toolbar.addToolbarButton(
|
||||||
translate(u'ThemeManager', u'Import a theme'), self.onImportTheme)
|
translate(u'ThemeManager', u'Import Theme'),
|
||||||
self.Toolbar.addToolbarButton(
|
u':/themes/theme_import.png',
|
||||||
translate(u'ThemeManager', u'Export Theme'), u':/themes/theme_export.png',
|
translate(u'ThemeManager', u'Import a theme'), self.onImportTheme)
|
||||||
translate(u'ThemeManager', u'Export a theme'), self.onExportTheme)
|
self.Toolbar.addToolbarButton(
|
||||||
self.ThemeWidget = QtGui.QWidgetAction(self.Toolbar)
|
translate(u'ThemeManager', u'Export Theme'),
|
||||||
self.Layout.addWidget(self.Toolbar)
|
u':/themes/theme_export.png',
|
||||||
self.ThemeListWidget = QtGui.QListWidget(self)
|
translate(u'ThemeManager', u'Export a theme'), self.onExportTheme)
|
||||||
self.ThemeListWidget.setAlternatingRowColors(True)
|
self.ThemeWidget = QtGui.QWidgetAction(self.Toolbar)
|
||||||
self.ThemeListWidget.setIconSize(QtCore.QSize(88,50))
|
self.Layout.addWidget(self.Toolbar)
|
||||||
self.Layout.addWidget(self.ThemeListWidget)
|
self.ThemeListWidget = QtGui.QListWidget(self)
|
||||||
#Signals
|
self.ThemeListWidget.setAlternatingRowColors(True)
|
||||||
QtCore.QObject.connect(self.ThemeListWidget,
|
self.ThemeListWidget.setIconSize(QtCore.QSize(88,50))
|
||||||
QtCore.SIGNAL(u'doubleClicked(QModelIndex)'), self.changeGlobalFromScreen)
|
self.Layout.addWidget(self.ThemeListWidget)
|
||||||
QtCore.QObject.connect(Receiver.get_receiver(),
|
#Signals
|
||||||
QtCore.SIGNAL(u'update_global_theme'), self.changeGlobalFromTab)
|
QtCore.QObject.connect(self.ThemeListWidget,
|
||||||
#Variables
|
QtCore.SIGNAL(u'doubleClicked(QModelIndex)'),
|
||||||
self.themelist = []
|
self.changeGlobalFromScreen)
|
||||||
self.path = os.path.join(ConfigHelper.get_data_path(), u'themes')
|
QtCore.QObject.connect(Receiver.get_receiver(),
|
||||||
self.checkThemesExists(self.path)
|
QtCore.SIGNAL(u'update_global_theme'), self.changeGlobalFromTab)
|
||||||
self.amendThemeForm.path = self.path
|
#Variables
|
||||||
# Last little bits of setting up
|
self.themelist = []
|
||||||
self.config = PluginConfig(u'themes')
|
self.path = os.path.join(ConfigHelper.get_data_path(), u'themes')
|
||||||
self.servicePath = self.config.get_data_path()
|
self.checkThemesExists(self.path)
|
||||||
self.global_theme = unicode(self.config.get_config(u'theme global theme', u''))
|
self.amendThemeForm.path = self.path
|
||||||
|
# Last little bits of setting up
|
||||||
def changeGlobalFromTab(self, themeName):
|
self.config = PluginConfig(u'themes')
|
||||||
log.debug(u'changeGlobalFromTab %s', themeName)
|
self.servicePath = self.config.get_data_path()
|
||||||
for count in range (0, self.ThemeListWidget.count()):
|
self.global_theme = unicode(
|
||||||
#reset the old name
|
self.config.get_config(u'theme global theme', u''))
|
||||||
item = self.ThemeListWidget.item(count)
|
|
||||||
oldName = item.text()
|
def changeGlobalFromTab(self, themeName):
|
||||||
newName = unicode(item.data(QtCore.Qt.UserRole).toString())
|
log.debug(u'changeGlobalFromTab %s', themeName)
|
||||||
if oldName != newName:
|
for count in range (0, self.ThemeListWidget.count()):
|
||||||
self.ThemeListWidget.item(count).setText(newName)
|
#reset the old name
|
||||||
#Set the new name
|
item = self.ThemeListWidget.item(count)
|
||||||
if themeName == newName:
|
oldName = item.text()
|
||||||
name = u'%s (%s)' % (newName, translate(u'ThemeManager', u'default'))
|
newName = unicode(item.data(QtCore.Qt.UserRole).toString())
|
||||||
self.ThemeListWidget.item(count).setText(name)
|
if oldName != newName:
|
||||||
|
self.ThemeListWidget.item(count).setText(newName)
|
||||||
def changeGlobalFromScreen(self, index):
|
#Set the new name
|
||||||
log.debug(u'changeGlobalFromScreen %s', index)
|
if themeName == newName:
|
||||||
for count in range (0, self.ThemeListWidget.count()):
|
name = u'%s (%s)' % (newName, translate(u'ThemeManager',
|
||||||
item = self.ThemeListWidget.item(count)
|
u'default'))
|
||||||
oldName = item.text()
|
self.ThemeListWidget.item(count).setText(name)
|
||||||
#reset the old name
|
|
||||||
if oldName != unicode(item.data(QtCore.Qt.UserRole).toString()):
|
def changeGlobalFromScreen(self, index):
|
||||||
self.ThemeListWidget.item(count).setText(unicode(item.data(QtCore.Qt.UserRole).toString()))
|
log.debug(u'changeGlobalFromScreen %s', index)
|
||||||
#Set the new name
|
for count in range (0, self.ThemeListWidget.count()):
|
||||||
if count == index.row():
|
item = self.ThemeListWidget.item(count)
|
||||||
self.global_theme = unicode(self.ThemeListWidget.item(count).text())
|
oldName = item.text()
|
||||||
name = u'%s (%s)' % (self.global_theme, translate(u'ThemeManager', u'default'))
|
#reset the old name
|
||||||
self.ThemeListWidget.item(count).setText(name)
|
if oldName != unicode(item.data(QtCore.Qt.UserRole).toString()):
|
||||||
self.config.set_config(u'theme global theme', self.global_theme)
|
self.ThemeListWidget.item(count).setText(
|
||||||
Receiver().send_message(u'update_global_theme', self.global_theme )
|
unicode(item.data(QtCore.Qt.UserRole).toString()))
|
||||||
self.pushThemes()
|
#Set the new name
|
||||||
|
if count == index.row():
|
||||||
def onAddTheme(self):
|
self.global_theme = unicode(
|
||||||
self.amendThemeForm.loadTheme(None)
|
self.ThemeListWidget.item(count).text())
|
||||||
self.amendThemeForm.exec_()
|
name = u'%s (%s)' % (self.global_theme,
|
||||||
|
translate(u'ThemeManager', u'default'))
|
||||||
def onEditTheme(self):
|
self.ThemeListWidget.item(count).setText(name)
|
||||||
item = self.ThemeListWidget.currentItem()
|
self.config.set_config(u'theme global theme', self.global_theme)
|
||||||
if item is not None:
|
Receiver().send_message(u'update_global_theme',
|
||||||
self.amendThemeForm.loadTheme(unicode(item.data(QtCore.Qt.UserRole).toString()))
|
self.global_theme)
|
||||||
self.amendThemeForm.exec_()
|
self.pushThemes()
|
||||||
|
|
||||||
def onDeleteTheme(self):
|
def onAddTheme(self):
|
||||||
self.global_theme = unicode(self.config.get_config(u'theme global theme', u''))
|
self.amendThemeForm.loadTheme(None)
|
||||||
item = self.ThemeListWidget.currentItem()
|
self.amendThemeForm.exec_()
|
||||||
if item is not None:
|
|
||||||
theme = unicode(item.text())
|
def onEditTheme(self):
|
||||||
# should be the same unless default
|
item = self.ThemeListWidget.currentItem()
|
||||||
if theme != unicode(item.data(QtCore.Qt.UserRole).toString()):
|
if item is not None:
|
||||||
QtGui.QMessageBox.critical(self,
|
self.amendThemeForm.loadTheme(
|
||||||
translate(u'ThemeManager', u'Error'),
|
unicode(item.data(QtCore.Qt.UserRole).toString()))
|
||||||
translate(u'ThemeManager', u'You are unable to delete the default theme!'),
|
self.amendThemeForm.exec_()
|
||||||
QtGui.QMessageBox.StandardButtons(QtGui.QMessageBox.Ok))
|
|
||||||
else:
|
def onDeleteTheme(self):
|
||||||
self.themelist.remove(theme)
|
self.global_theme = unicode(
|
||||||
th = theme + u'.png'
|
self.config.get_config(u'theme global theme', u''))
|
||||||
row = self.ThemeListWidget.row(item)
|
item = self.ThemeListWidget.currentItem()
|
||||||
self.ThemeListWidget.takeItem(row)
|
if item is not None:
|
||||||
try:
|
theme = unicode(item.text())
|
||||||
os.remove(os.path.join(self.path, th))
|
# should be the same unless default
|
||||||
except:
|
if theme != unicode(item.data(QtCore.Qt.UserRole).toString()):
|
||||||
#if not present do not worry
|
QtGui.QMessageBox.critical(self,
|
||||||
pass
|
translate(u'ThemeManager', u'Error'),
|
||||||
try:
|
translate(u'ThemeManager',
|
||||||
shutil.rmtree(os.path.join(self.path, theme))
|
u'You are unable to delete the default theme!'),
|
||||||
except:
|
QtGui.QMessageBox.StandardButtons(QtGui.QMessageBox.Ok))
|
||||||
#if not present do not worry
|
else:
|
||||||
pass
|
self.themelist.remove(theme)
|
||||||
#As we do not reload the themes push out the change
|
th = theme + u'.png'
|
||||||
#Reaload the list as the internal lists and events need to be triggered
|
row = self.ThemeListWidget.row(item)
|
||||||
self.pushThemes()
|
self.ThemeListWidget.takeItem(row)
|
||||||
|
try:
|
||||||
def onExportTheme(self):
|
os.remove(os.path.join(self.path, th))
|
||||||
pass
|
except:
|
||||||
|
#if not present do not worry
|
||||||
def onImportTheme(self):
|
pass
|
||||||
files = QtGui.QFileDialog.getOpenFileNames(None,
|
try:
|
||||||
translate(u'ThemeManager', u'Select Theme Import File'),
|
shutil.rmtree(os.path.join(self.path, theme))
|
||||||
self.path, u'Theme (*.*)')
|
except:
|
||||||
log.info(u'New Themes %s', unicode(files))
|
#if not present do not worry
|
||||||
if len(files) > 0:
|
pass
|
||||||
for file in files:
|
# As we do not reload the themes push out the change
|
||||||
self.unzipTheme(file, self.path)
|
# Reaload the list as the internal lists and events need
|
||||||
self.loadThemes()
|
# to be triggered
|
||||||
|
self.pushThemes()
|
||||||
def loadThemes(self):
|
|
||||||
"""
|
def onExportTheme(self):
|
||||||
Loads the theme lists and triggers updates accross
|
pass
|
||||||
the whole system using direct calls or core functions
|
|
||||||
and events for the plugins.
|
def onImportTheme(self):
|
||||||
The plugins will call back in to get the real list if they want it.
|
files = QtGui.QFileDialog.getOpenFileNames(None,
|
||||||
"""
|
translate(u'ThemeManager', u'Select Theme Import File'),
|
||||||
log.debug(u'Load themes from dir')
|
self.path, u'Theme (*.*)')
|
||||||
self.themelist = []
|
log.info(u'New Themes %s', unicode(files))
|
||||||
self.ThemeListWidget.clear()
|
if len(files) > 0:
|
||||||
for root, dirs, files in os.walk(self.path):
|
for file in files:
|
||||||
for name in files:
|
self.unzipTheme(file, self.path)
|
||||||
if name.endswith(u'.png'):
|
self.loadThemes()
|
||||||
#check to see file is in theme root directory
|
|
||||||
theme = os.path.join(self.path, name)
|
def loadThemes(self):
|
||||||
if os.path.exists(theme):
|
"""
|
||||||
(path, filename) = os.path.split(unicode(file))
|
Loads the theme lists and triggers updates accross
|
||||||
textName = os.path.splitext(name)[0]
|
the whole system using direct calls or core functions
|
||||||
if textName == self.global_theme:
|
and events for the plugins.
|
||||||
name = u'%s (%s)' % (textName, translate(u'ThemeManager', u'default'))
|
The plugins will call back in to get the real list if they want it.
|
||||||
else:
|
"""
|
||||||
name = textName
|
log.debug(u'Load themes from dir')
|
||||||
item_name = QtGui.QListWidgetItem(name)
|
self.themelist = []
|
||||||
item_name.setIcon(buildIcon(theme))
|
self.ThemeListWidget.clear()
|
||||||
item_name.setData(QtCore.Qt.UserRole, QtCore.QVariant(textName))
|
for root, dirs, files in os.walk(self.path):
|
||||||
self.ThemeListWidget.addItem(item_name)
|
for name in files:
|
||||||
self.themelist.append(textName)
|
if name.endswith(u'.png'):
|
||||||
self.pushThemes()
|
#check to see file is in theme root directory
|
||||||
|
theme = os.path.join(self.path, name)
|
||||||
def pushThemes(self):
|
if os.path.exists(theme):
|
||||||
Receiver().send_message(u'update_themes', self.getThemes() )
|
(path, filename) = os.path.split(unicode(file))
|
||||||
|
textName = os.path.splitext(name)[0]
|
||||||
def getThemes(self):
|
if textName == self.global_theme:
|
||||||
return self.themelist
|
name = u'%s (%s)' % (textName,
|
||||||
|
translate(u'ThemeManager', u'default'))
|
||||||
def getThemeData(self, themename):
|
else:
|
||||||
log.debug(u'getthemedata for theme %s', themename)
|
name = textName
|
||||||
xml_file = os.path.join(self.path, unicode(themename), unicode(themename) + u'.xml')
|
item_name = QtGui.QListWidgetItem(name)
|
||||||
try:
|
item_name.setIcon(buildIcon(theme))
|
||||||
xml = file_to_xml(xml_file)
|
item_name.setData(QtCore.Qt.UserRole,
|
||||||
except:
|
QtCore.QVariant(textName))
|
||||||
newtheme = ThemeXML()
|
self.ThemeListWidget.addItem(item_name)
|
||||||
newtheme.new_document(u'New Theme')
|
self.themelist.append(textName)
|
||||||
newtheme.add_background_solid(unicode(u'#000000'))
|
self.pushThemes()
|
||||||
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')
|
def pushThemes(self):
|
||||||
newtheme.add_display(u'False', unicode(u'#FFFFFF'), u'False', unicode(u'#FFFFFF'),
|
Receiver().send_message(u'update_themes', self.getThemes() )
|
||||||
unicode(0), unicode(0), unicode(0))
|
|
||||||
xml = newtheme.extract_xml()
|
def getThemes(self):
|
||||||
theme = ThemeXML()
|
return self.themelist
|
||||||
theme.parse(xml)
|
|
||||||
theme.extend_image_filename(self.path)
|
def getThemeData(self, themename):
|
||||||
return theme
|
log.debug(u'getthemedata for theme %s', themename)
|
||||||
|
xml_file = os.path.join(self.path, unicode(themename),
|
||||||
def checkThemesExists(self, dir):
|
unicode(themename) + u'.xml')
|
||||||
log.debug(u'check themes')
|
try:
|
||||||
if os.path.exists(dir) == False:
|
xml = file_to_xml(xml_file)
|
||||||
os.mkdir(dir)
|
except:
|
||||||
|
newtheme = ThemeXML()
|
||||||
def unzipTheme(self, filename, dir):
|
newtheme.new_document(u'New Theme')
|
||||||
"""
|
newtheme.add_background_solid(unicode(u'#000000'))
|
||||||
Unzip the theme, remove the preview file if stored
|
newtheme.add_font(unicode(QtGui.QFont().family()),
|
||||||
Generate a new preview fileCheck the XML theme version and upgrade if
|
unicode(u'#FFFFFF'), unicode(30), u'False')
|
||||||
necessary.
|
newtheme.add_font(unicode(QtGui.QFont().family()),
|
||||||
"""
|
unicode(u'#FFFFFF'), unicode(12), u'False', u'footer')
|
||||||
log.debug(u'Unzipping theme %s', filename)
|
newtheme.add_display(u'False', unicode(u'#FFFFFF'), u'False',
|
||||||
zip = zipfile.ZipFile(unicode(filename))
|
unicode(u'#FFFFFF'), unicode(0), unicode(0), unicode(0))
|
||||||
filexml = None
|
xml = newtheme.extract_xml()
|
||||||
themename = None
|
theme = ThemeXML()
|
||||||
for file in zip.namelist():
|
theme.parse(xml)
|
||||||
if file.endswith(os.path.sep):
|
theme.extend_image_filename(self.path)
|
||||||
theme_dir = os.path.join(dir, file)
|
self.cleanTheme(theme)
|
||||||
if os.path.exists(theme_dir) == False:
|
return theme
|
||||||
os.mkdir(os.path.join(dir, file))
|
|
||||||
else:
|
def checkThemesExists(self, dir):
|
||||||
fullpath = os.path.join(dir, file)
|
log.debug(u'check themes')
|
||||||
names = file.split(os.path.sep)
|
if os.path.exists(dir) == False:
|
||||||
if len(names) > 1:
|
os.mkdir(dir)
|
||||||
# not preview file
|
|
||||||
if themename is None:
|
def unzipTheme(self, filename, dir):
|
||||||
themename = names[0]
|
"""
|
||||||
xml_data = zip.read(file)
|
Unzip the theme, remove the preview file if stored
|
||||||
if os.path.splitext(file)[1].lower() in [u'.xml']:
|
Generate a new preview fileCheck the XML theme version and upgrade if
|
||||||
if self.checkVersion1(xml_data):
|
necessary.
|
||||||
# upgrade theme xml
|
"""
|
||||||
filexml = self.migrateVersion122(filename, fullpath, xml_data)
|
log.debug(u'Unzipping theme %s', filename)
|
||||||
else:
|
zip = zipfile.ZipFile(unicode(filename))
|
||||||
filexml = xml_data
|
filexml = None
|
||||||
outfile = open(fullpath, u'w')
|
themename = None
|
||||||
outfile.write(filexml)
|
for file in zip.namelist():
|
||||||
outfile.close()
|
if file.endswith(os.path.sep):
|
||||||
else:
|
theme_dir = os.path.join(dir, file)
|
||||||
outfile = open(fullpath, u'w')
|
if os.path.exists(theme_dir) == False:
|
||||||
outfile.write(zip.read(file))
|
os.mkdir(os.path.join(dir, file))
|
||||||
outfile.close()
|
else:
|
||||||
self.generateAndSaveImage(dir, themename, filexml)
|
fullpath = os.path.join(dir, file)
|
||||||
|
names = file.split(os.path.sep)
|
||||||
def checkVersion1(self, xmlfile):
|
if len(names) > 1:
|
||||||
"""
|
# not preview file
|
||||||
Am I a version 1 theme
|
if themename is None:
|
||||||
"""
|
themename = names[0]
|
||||||
log.debug(u'checkVersion1 ')
|
xml_data = zip.read(file)
|
||||||
theme = xmlfile
|
if os.path.splitext(file)[1].lower() in [u'.xml']:
|
||||||
tree = ElementTree(element=XML(theme)).getroot()
|
if self.checkVersion1(xml_data):
|
||||||
if tree.find(u'BackgroundType') is None:
|
# upgrade theme xml
|
||||||
return False
|
filexml = self.migrateVersion122(filename,
|
||||||
else:
|
fullpath, xml_data)
|
||||||
return True
|
else:
|
||||||
|
filexml = xml_data
|
||||||
def migrateVersion122(self, filename, fullpath, xml_data):
|
outfile = open(fullpath, u'w')
|
||||||
"""
|
outfile.write(filexml)
|
||||||
Called by convert the xml data from version 1 format
|
outfile.close()
|
||||||
to the current format.
|
else:
|
||||||
New fields are defaulted but the new theme is useable
|
outfile = open(fullpath, u'w')
|
||||||
"""
|
outfile.write(zip.read(file))
|
||||||
log.debug(u'migrateVersion122 %s %s', filename, fullpath)
|
outfile.close()
|
||||||
theme = Theme(xml_data)
|
self.generateAndSaveImage(dir, themename, filexml)
|
||||||
newtheme = ThemeXML()
|
|
||||||
newtheme.new_document(theme.Name)
|
def checkVersion1(self, xmlfile):
|
||||||
if theme.BackgroundType == 0:
|
"""
|
||||||
newtheme.add_background_solid(unicode(theme.BackgroundParameter1.name()))
|
Am I a version 1 theme
|
||||||
elif theme.BackgroundType == 1:
|
"""
|
||||||
direction = u'vertical'
|
log.debug(u'checkVersion1 ')
|
||||||
if theme.BackgroundParameter3.name() == 1:
|
theme = xmlfile
|
||||||
direction = u'horizontal'
|
tree = ElementTree(element=XML(theme)).getroot()
|
||||||
newtheme.add_background_gradient(
|
if tree.find(u'BackgroundType') is None:
|
||||||
unicode(theme.BackgroundParameter1.name()),
|
return False
|
||||||
unicode(theme.BackgroundParameter2.name()), direction)
|
else:
|
||||||
else:
|
return True
|
||||||
newtheme.add_background_image(unicode(theme.BackgroundParameter1))
|
|
||||||
|
def migrateVersion122(self, filename, fullpath, xml_data):
|
||||||
newtheme.add_font(unicode(theme.FontName),
|
"""
|
||||||
unicode(theme.FontColor.name()),
|
Called by convert the xml data from version 1 format
|
||||||
unicode(theme.FontProportion * 2), u'False')
|
to the current format.
|
||||||
newtheme.add_font(unicode(theme.FontName),
|
New fields are defaulted but the new theme is useable
|
||||||
unicode(theme.FontColor.name()),
|
"""
|
||||||
unicode(12), u'False', u'footer')
|
log.debug(u'migrateVersion122 %s %s', filename, fullpath)
|
||||||
outline = False
|
theme = Theme(xml_data)
|
||||||
shadow = False
|
newtheme = ThemeXML()
|
||||||
if theme.Shadow == 1:
|
newtheme.new_document(theme.Name)
|
||||||
shadow = True
|
if theme.BackgroundType == 0:
|
||||||
if theme.Outline == 1:
|
newtheme.add_background_solid(unicode(
|
||||||
outline = True
|
theme.BackgroundParameter1.name()))
|
||||||
newtheme.add_display(unicode(shadow), unicode(theme.ShadowColor.name()),
|
elif theme.BackgroundType == 1:
|
||||||
unicode(outline), unicode(theme.OutlineColor.name()),
|
direction = u'vertical'
|
||||||
unicode(theme.HorizontalAlign), unicode(theme.VerticalAlign),
|
if theme.BackgroundParameter3.name() == 1:
|
||||||
unicode(theme.WrapStyle))
|
direction = u'horizontal'
|
||||||
return newtheme.extract_xml()
|
newtheme.add_background_gradient(
|
||||||
|
unicode(theme.BackgroundParameter1.name()),
|
||||||
def saveTheme(self, name, theme_xml, theme_pretty_xml, image_from,
|
unicode(theme.BackgroundParameter2.name()), direction)
|
||||||
image_to) :
|
else:
|
||||||
"""
|
newtheme.add_background_image(unicode(theme.BackgroundParameter1))
|
||||||
Called by thememaintenance Dialog to save the theme
|
|
||||||
and to trigger the reload of the theme list
|
newtheme.add_font(unicode(theme.FontName),
|
||||||
"""
|
unicode(theme.FontColor.name()),
|
||||||
log.debug(u'saveTheme %s %s', name, theme_xml)
|
unicode(theme.FontProportion * 2), u'False')
|
||||||
theme_dir = os.path.join(self.path, name)
|
newtheme.add_font(unicode(theme.FontName),
|
||||||
if os.path.exists(theme_dir) == False:
|
unicode(theme.FontColor.name()),
|
||||||
os.mkdir(os.path.join(self.path, name))
|
unicode(12), u'False', u'footer')
|
||||||
theme_file = os.path.join(theme_dir, name + u'.xml')
|
outline = False
|
||||||
log.debug(theme_file)
|
shadow = False
|
||||||
|
if theme.Shadow == 1:
|
||||||
result = QtGui.QMessageBox.Yes
|
shadow = True
|
||||||
if os.path.exists(theme_file):
|
if theme.Outline == 1:
|
||||||
result = QtGui.QMessageBox.question(
|
outline = True
|
||||||
self,
|
newtheme.add_display(unicode(shadow), unicode(theme.ShadowColor.name()),
|
||||||
translate(u'ThemeManager',u'Theme Exists'),
|
unicode(outline), unicode(theme.OutlineColor.name()),
|
||||||
translate(u'ThemeManager',u'A theme with this name already exists, would you like to overwrite it?'),
|
unicode(theme.HorizontalAlign), unicode(theme.VerticalAlign),
|
||||||
(QtGui.QMessageBox.Yes | QtGui.QMessageBox.No),
|
unicode(theme.WrapStyle))
|
||||||
QtGui.QMessageBox.No)
|
return newtheme.extract_xml()
|
||||||
if result == QtGui.QMessageBox.Yes:
|
|
||||||
# Save the theme, overwriting the existing theme if necessary.
|
def saveTheme(self, name, theme_xml, theme_pretty_xml, image_from,
|
||||||
outfile = open(theme_file, u'w')
|
image_to) :
|
||||||
outfile.write(theme_pretty_xml)
|
"""
|
||||||
outfile.close()
|
Called by thememaintenance Dialog to save the theme
|
||||||
if image_from is not None and image_from != image_to:
|
and to trigger the reload of the theme list
|
||||||
shutil.copyfile(image_from, image_to)
|
"""
|
||||||
|
log.debug(u'saveTheme %s %s', name, theme_xml)
|
||||||
self.generateAndSaveImage(self.path, name, theme_xml)
|
theme_dir = os.path.join(self.path, name)
|
||||||
self.loadThemes()
|
if os.path.exists(theme_dir) == False:
|
||||||
else:
|
os.mkdir(os.path.join(self.path, name))
|
||||||
# Don't close the dialog - allow the user to change the name of
|
theme_file = os.path.join(theme_dir, name + u'.xml')
|
||||||
# the theme or to cancel the theme dialog completely.
|
log.debug(theme_file)
|
||||||
return False
|
|
||||||
|
result = QtGui.QMessageBox.Yes
|
||||||
def generateAndSaveImage(self, dir, name, theme_xml):
|
if os.path.exists(theme_file):
|
||||||
log.debug(u'generateAndSaveImage %s %s %s', dir, name, theme_xml)
|
result = QtGui.QMessageBox.question(
|
||||||
theme = ThemeXML()
|
self,
|
||||||
theme.parse(theme_xml)
|
translate(u'ThemeManager', u'Theme Exists'),
|
||||||
theme.extend_image_filename(dir)
|
translate(u'ThemeManager', u'A theme with this name already exists, would you like to overwrite it?'),
|
||||||
frame = self.generateImage(theme)
|
(QtGui.QMessageBox.Yes | QtGui.QMessageBox.No),
|
||||||
samplepathname = os.path.join(self.path, name + u'.png')
|
QtGui.QMessageBox.No)
|
||||||
if os.path.exists(samplepathname):
|
if result == QtGui.QMessageBox.Yes:
|
||||||
os.unlink(samplepathname)
|
# Save the theme, overwriting the existing theme if necessary.
|
||||||
frame.save(samplepathname, u'png')
|
outfile = open(theme_file, u'w')
|
||||||
log.debug(u'Theme image written to %s', samplepathname)
|
outfile.write(theme_pretty_xml)
|
||||||
|
outfile.close()
|
||||||
def generateImage(self, themedata):
|
if image_from is not None and image_from != image_to:
|
||||||
"""
|
shutil.copyfile(image_from, image_to)
|
||||||
Call the RenderManager to build a Sample Image
|
|
||||||
"""
|
self.generateAndSaveImage(self.path, name, theme_xml)
|
||||||
log.debug(u'generateImage %s ', themedata)
|
self.loadThemes()
|
||||||
frame = self.parent.RenderManager.generate_preview(themedata)
|
else:
|
||||||
return frame
|
# Don't close the dialog - allow the user to change the name of
|
||||||
|
# the theme or to cancel the theme dialog completely.
|
||||||
def getPreviewImage(self, theme):
|
return False
|
||||||
log.debug(u'getPreviewImage %s ', theme)
|
|
||||||
image = os.path.join(self.path, theme + u'.png')
|
def generateAndSaveImage(self, dir, name, theme_xml):
|
||||||
return image
|
log.debug(u'generateAndSaveImage %s %s %s', dir, name, theme_xml)
|
||||||
|
theme = ThemeXML()
|
||||||
|
theme.parse(theme_xml)
|
||||||
|
theme.extend_image_filename(dir)
|
||||||
|
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')
|
||||||
|
log.debug(u'Theme image written to %s', samplepathname)
|
||||||
|
|
||||||
|
def generateImage(self, themedata):
|
||||||
|
"""
|
||||||
|
Call the RenderManager to build a Sample Image
|
||||||
|
"""
|
||||||
|
log.debug(u'generateImage %s ', themedata)
|
||||||
|
frame = self.parent.RenderManager.generate_preview(themedata)
|
||||||
|
return frame
|
||||||
|
|
||||||
|
def getPreviewImage(self, theme):
|
||||||
|
log.debug(u'getPreviewImage %s ', theme)
|
||||||
|
image = os.path.join(self.path, theme + u'.png')
|
||||||
|
return image
|
||||||
|
|
||||||
|
def cleanTheme(self, theme):
|
||||||
|
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 = 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 = theme.display_verticalAlign.strip()
|
||||||
|
theme.display_wrapStyle = theme.display_wrapStyle.strip()
|
||||||
|
theme.font_footer_color = theme.font_footer_color.strip()
|
||||||
|
theme.font_footer_height = 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 = theme.font_footer_proportion.strip()
|
||||||
|
theme.font_footer_weight = theme.font_footer_weight.strip()
|
||||||
|
theme.font_footer_width = theme.font_footer_width.strip()
|
||||||
|
theme.font_footer_x = theme.font_footer_x.strip()
|
||||||
|
theme.font_footer_y = theme.font_footer_y.strip()
|
||||||
|
theme.font_main_color = theme.font_main_color.strip()
|
||||||
|
theme.font_main_height = 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 = theme.font_main_proportion.strip()
|
||||||
|
theme.font_main_weight = theme.font_main_weight.strip()
|
||||||
|
theme.font_main_x = theme.font_main_x.strip()
|
||||||
|
theme.font_main_y = theme.font_main_y.strip()
|
||||||
|
#theme.theme_mode
|
||||||
|
theme.theme_name = theme.theme_name.strip()
|
||||||
|
#theme.theme_version
|
||||||
|
Loading…
Reference in New Issue
Block a user