openlp/openlp/core/ui/wizard.py

307 lines
13 KiB
Python
Raw Normal View History

2011-01-13 17:55:29 +00:00
# -*- coding: utf-8 -*-
2012-12-29 15:25:29 +00:00
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
2011-01-13 17:55:29 +00:00
###############################################################################
# OpenLP - Open Source Lyrics Projection #
# --------------------------------------------------------------------------- #
2013-12-24 08:56:50 +00:00
# Copyright (c) 2008-2014 Raoul Snyman #
# Portions copyright (c) 2008-2014 Tim Bentley, Gerald Britton, Jonathan #
# Corwin, Samuel Findlay, Michael Gorven, Scott Guerrieri, Matthias Hub, #
2012-11-11 21:16:14 +00:00
# Meinert Jordan, Armin Köhler, Erik Lundin, Edwin Lunando, Brian T. Meyer. #
2012-10-21 13:16:22 +00:00
# Joshua Miller, Stevan Pettit, Andreas Preikschat, Mattias Põldaru, #
# Christian Richter, Philip Ridout, Simon Scudder, Jeffrey Smith, #
# Maikel Stuivenberg, Martin Thompson, Jon Tibble, Dave Warnock, #
# Frode Woldsund, Martin Zibricky, Patrick Zimmermann #
2011-01-13 17:55:29 +00:00
# --------------------------------------------------------------------------- #
# 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 Foundation; version 2 of the License. #
# #
# 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 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 this program; if not, write to the Free Software Foundation, Inc., 59 #
# Temple Place, Suite 330, Boston, MA 02111-1307 USA #
###############################################################################
"""
The :mod:``wizard`` module provides generic wizard tools for OpenLP.
"""
import logging
2011-02-10 21:07:28 +00:00
import os
2011-01-13 17:55:29 +00:00
2013-10-13 20:36:42 +00:00
from PyQt4 import QtGui
2011-01-13 17:55:29 +00:00
2014-03-16 21:25:23 +00:00
from openlp.core.common import Registry, RegistryProperties, Settings, UiStrings, translate
2013-12-13 17:44:05 +00:00
from openlp.core.lib import build_icon
from openlp.core.lib.ui import add_welcome_page
2011-01-13 17:55:29 +00:00
log = logging.getLogger(__name__)
2013-02-01 21:34:23 +00:00
2011-02-12 02:43:13 +00:00
class WizardStrings(object):
"""
Provide standard strings for wizards to use.
"""
2011-02-12 20:25:40 +00:00
# Applications/Formats we import from or export to. These get used in
# multiple places but do not need translating unless you find evidence of
# the writers translating their own product name.
2013-08-31 18:17:38 +00:00
CSV = 'CSV'
OS = 'OpenSong'
OSIS = 'OSIS'
2011-02-12 02:43:13 +00:00
# These strings should need a good reason to be retranslated elsewhere.
FinishedImport = translate('OpenLP.Ui', 'Finished import.')
2011-02-12 02:43:13 +00:00
FormatLabel = translate('OpenLP.Ui', 'Format:')
2013-08-31 18:17:38 +00:00
HeaderStyle = '<span style="font-size:14pt; font-weight:600;">%s</span>'
2011-02-12 15:37:02 +00:00
Importing = translate('OpenLP.Ui', 'Importing')
2012-05-17 18:57:01 +00:00
ImportingType = translate('OpenLP.Ui', 'Importing "%s"...')
2011-02-12 02:43:13 +00:00
ImportSelect = translate('OpenLP.Ui', 'Select Import Source')
ImportSelectLong = translate('OpenLP.Ui', 'Select the import format and the location to import from.')
2012-05-17 18:57:01 +00:00
OpenTypeFile = translate('OpenLP.Ui', 'Open %s File')
2012-10-03 16:38:06 +00:00
OpenTypeFolder = translate('OpenLP.Ui', 'Open %s Folder')
2012-05-17 18:57:01 +00:00
PercentSymbolFormat = translate('OpenLP.Ui', '%p%')
2011-02-13 01:09:04 +00:00
Ready = translate('OpenLP.Ui', 'Ready.')
StartingImport = translate('OpenLP.Ui', 'Starting import...')
2014-03-17 19:05:55 +00:00
YouSpecifyFile = translate('OpenLP.Ui', 'You need to specify one %s file to import from.',
'A file type e.g. OpenSong')
YouSpecifyFiles = translate('OpenLP.Ui', 'You need to specify at least one %s file to import from.',
'A file type e.g. OpenSong')
YouSpecifyFolder = translate('OpenLP.Ui', 'You need to specify one %s folder to import from.',
'A song format e.g. PowerSong')
2011-02-12 02:43:13 +00:00
2014-03-16 21:25:23 +00:00
class OpenLPWizard(QtGui.QWizard, RegistryProperties):
2011-01-13 17:55:29 +00:00
"""
Generic OpenLP wizard to provide generic functionality and a unified look
and feel.
2013-01-22 21:35:53 +00:00
``parent``
The QWidget-derived parent of the wizard.
``plugin``
Plugin this wizard is part of. The plugin will be saved in the "plugin" variable.
The plugin will also be used as basis for the file dialog methods this class provides.
``name``
The object name this wizard should have.
``image``
The image to display on the "welcome" page of the wizard. Should be 163x350.
``add_progress_page``
2013-01-22 21:35:53 +00:00
Whether to add a progress page with a progressbar at the end of the wizard.
2011-01-13 17:55:29 +00:00
"""
def __init__(self, parent, plugin, name, image, add_progress_page=True):
2013-02-01 21:34:23 +00:00
"""
Constructor
"""
2013-07-18 14:32:23 +00:00
super(OpenLPWizard, self).__init__(parent)
2011-02-12 02:43:13 +00:00
self.plugin = plugin
self.with_progress_page = add_progress_page
2011-01-13 17:55:29 +00:00
self.setObjectName(name)
2013-08-31 18:17:38 +00:00
self.open_icon = build_icon(':/general/general_open.png')
self.delete_icon = build_icon(':/general/general_delete.png')
2013-03-07 08:05:43 +00:00
self.finish_button = self.button(QtGui.QWizard.FinishButton)
self.cancel_button = self.button(QtGui.QWizard.CancelButton)
2011-01-13 17:55:29 +00:00
self.setupUi(image)
2013-03-07 08:05:43 +00:00
self.register_fields()
2013-03-14 22:22:18 +00:00
self.custom_init()
self.custom_signals()
2013-03-07 08:24:12 +00:00
self.currentIdChanged.connect(self.on_current_id_changed)
if self.with_progress_page:
2013-04-06 10:20:17 +00:00
self.error_copy_to_button.clicked.connect(self.on_error_copy_to_button_clicked)
self.error_save_to_button.clicked.connect(self.on_error_save_to_button_clicked)
2011-01-13 17:55:29 +00:00
def setupUi(self, image):
"""
2012-06-01 07:32:58 +00:00
Set up the wizard UI.
2011-01-13 17:55:29 +00:00
"""
self.setModal(True)
self.setWizardStyle(QtGui.QWizard.ModernStyle)
self.setOptions(QtGui.QWizard.IndependentPages |
2014-03-17 19:05:55 +00:00
QtGui.QWizard.NoBackButtonOnStartPage | QtGui.QWizard.NoBackButtonOnLastPage)
add_welcome_page(self, image)
2013-03-14 22:22:18 +00:00
self.add_custom_pages()
if self.with_progress_page:
2013-04-06 10:20:17 +00:00
self.add_progress_page()
2011-01-13 17:55:29 +00:00
self.retranslateUi()
2013-03-07 08:05:43 +00:00
def register_fields(self):
2011-02-10 19:28:17 +00:00
"""
Hook method for wizards to register any fields they need.
"""
pass
2013-03-14 22:22:18 +00:00
def custom_init(self):
"""
Hook method for custom initialisation
"""
pass
def custom_signals(self):
"""
Hook method for adding custom signals
"""
pass
def add_custom_pages(self):
"""
Hook method for wizards to add extra pages
"""
pass
def add_progress_page(self):
2011-01-13 17:55:29 +00:00
"""
2011-01-18 18:53:09 +00:00
Add the progress page for the wizard. This page informs the user how
2011-01-13 17:55:29 +00:00
the wizard is progressing with its task.
"""
2013-03-07 08:05:43 +00:00
self.progress_page = QtGui.QWizardPage()
2013-08-31 18:17:38 +00:00
self.progress_page.setObjectName('progress_page')
2013-03-07 08:05:43 +00:00
self.progress_layout = QtGui.QVBoxLayout(self.progress_page)
self.progress_layout.setMargin(48)
2013-08-31 18:17:38 +00:00
self.progress_layout.setObjectName('progress_layout')
2013-03-07 08:05:43 +00:00
self.progress_label = QtGui.QLabel(self.progress_page)
2013-08-31 18:17:38 +00:00
self.progress_label.setObjectName('progress_label')
2013-03-07 08:05:43 +00:00
self.progress_label.setWordWrap(True)
self.progress_layout.addWidget(self.progress_label)
self.progress_bar = QtGui.QProgressBar(self.progress_page)
2013-08-31 18:17:38 +00:00
self.progress_bar.setObjectName('progress_bar')
2013-03-07 08:05:43 +00:00
self.progress_layout.addWidget(self.progress_bar)
# Add a QTextEdit and a copy to file and copy to clipboard button to be
# able to provide feedback to the user. Hidden by default.
2013-03-07 08:05:43 +00:00
self.error_report_text_edit = QtGui.QTextEdit(self.progress_page)
2013-08-31 18:17:38 +00:00
self.error_report_text_edit.setObjectName('error_report_text_edit')
2013-03-07 08:05:43 +00:00
self.error_report_text_edit.setHidden(True)
self.error_report_text_edit.setReadOnly(True)
self.progress_layout.addWidget(self.error_report_text_edit)
self.error_button_layout = QtGui.QHBoxLayout()
2013-08-31 18:17:38 +00:00
self.error_button_layout.setObjectName('error_button_layout')
2012-12-29 15:25:29 +00:00
spacer = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
2013-03-07 08:05:43 +00:00
self.error_button_layout.addItem(spacer)
self.error_copy_to_button = QtGui.QPushButton(self.progress_page)
2013-08-31 18:17:38 +00:00
self.error_copy_to_button.setObjectName('error_copy_to_button')
2013-03-07 08:05:43 +00:00
self.error_copy_to_button.setHidden(True)
2013-08-31 18:17:38 +00:00
self.error_copy_to_button.setIcon(build_icon(':/system/system_edit_copy.png'))
2013-03-07 08:05:43 +00:00
self.error_button_layout.addWidget(self.error_copy_to_button)
self.error_save_to_button = QtGui.QPushButton(self.progress_page)
2013-08-31 18:17:38 +00:00
self.error_save_to_button.setObjectName('error_save_to_button')
2013-03-07 08:05:43 +00:00
self.error_save_to_button.setHidden(True)
2013-08-31 18:17:38 +00:00
self.error_save_to_button.setIcon(build_icon(':/general/general_save.png'))
2013-03-07 08:05:43 +00:00
self.error_button_layout.addWidget(self.error_save_to_button)
self.progress_layout.addLayout(self.error_button_layout)
self.addPage(self.progress_page)
2011-01-13 17:55:29 +00:00
def exec_(self):
"""
Run the wizard.
"""
self.setDefaults()
return QtGui.QWizard.exec_(self)
def reject(self):
"""
Stop the wizard on cancel button, close button or ESC key.
"""
2013-08-31 18:17:38 +00:00
log.debug('Wizard cancelled by user.')
2013-04-06 10:20:17 +00:00
if self.with_progress_page and self.currentPage() == self.progress_page:
2013-08-31 18:17:38 +00:00
Registry().execute('openlp_stop_wizard')
2011-01-18 18:53:09 +00:00
self.done(QtGui.QDialog.Rejected)
2011-01-13 17:55:29 +00:00
2014-03-17 19:05:55 +00:00
def on_current_id_changed(self, page_id):
2011-01-13 17:55:29 +00:00
"""
Perform necessary functions depending on which wizard page is active.
"""
2014-03-17 19:05:55 +00:00
if self.with_progress_page and self.page(page_id) == self.progress_page:
2013-03-07 08:05:43 +00:00
self.pre_wizard()
2014-03-04 18:49:30 +00:00
self.perform_wizard()
2013-03-14 22:22:18 +00:00
self.post_wizard()
else:
2014-03-17 19:05:55 +00:00
self.custom_page_changed(page_id)
2014-03-17 19:05:55 +00:00
def custom_page_changed(self, page_id):
"""
Called when changing to a page other than the progress page
"""
pass
2011-01-13 17:55:29 +00:00
2013-03-07 08:05:43 +00:00
def on_error_copy_to_button_clicked(self):
"""
2013-03-07 08:24:12 +00:00
Called when the ``error_copy_to_button`` has been clicked.
"""
pass
2013-03-07 08:05:43 +00:00
def on_error_save_to_button_clicked(self):
"""
2013-03-07 08:24:12 +00:00
Called when the ``error_save_to_button`` has been clicked.
"""
pass
2013-03-07 08:05:43 +00:00
def increment_progress_bar(self, status_text, increment=1):
2011-01-13 17:55:29 +00:00
"""
Update the wizard progress page.
2014-03-17 19:05:55 +00:00
:param status_text: Current status information to display.
:param increment: The value to increment the progress bar by.
2011-01-13 17:55:29 +00:00
"""
2013-08-31 18:17:38 +00:00
log.debug('IncrementBar %s', status_text)
2013-03-07 08:05:43 +00:00
self.progress_label.setText(status_text)
2011-01-13 17:55:29 +00:00
if increment > 0:
2013-03-07 08:05:43 +00:00
self.progress_bar.setValue(self.progress_bar.value() + increment)
2013-02-03 19:23:12 +00:00
self.application.process_events()
2011-01-13 17:55:29 +00:00
2013-03-07 08:05:43 +00:00
def pre_wizard(self):
2011-01-13 17:55:29 +00:00
"""
Prepare the UI for the import.
"""
2013-03-07 08:05:43 +00:00
self.finish_button.setVisible(False)
self.progress_bar.setMinimum(0)
self.progress_bar.setMaximum(1188)
self.progress_bar.setValue(0)
2011-01-13 17:55:29 +00:00
2013-03-14 22:22:18 +00:00
def post_wizard(self):
2011-01-13 17:55:29 +00:00
"""
Clean up the UI after the import has finished.
"""
2013-03-07 08:05:43 +00:00
self.progress_bar.setValue(self.progress_bar.maximum())
self.finish_button.setVisible(True)
self.cancel_button.setVisible(False)
2013-02-03 19:23:12 +00:00
self.application.process_events()
2011-02-10 19:37:02 +00:00
2013-08-31 18:17:38 +00:00
def get_file_name(self, title, editbox, setting_name, filters=''):
2011-02-10 19:37:02 +00:00
"""
Opens a QFileDialog and saves the filename to the given editbox.
2014-03-17 19:05:55 +00:00
:param title: The title of the dialog (unicode).
:param editbox: An editbox (QLineEdit).
:param setting_name: The place where to save the last opened directory.
:param filters: The file extension filters. It should contain the file description
2011-02-10 19:37:02 +00:00
as well as the file extension. For example::
u'OpenLP 2.0 Databases (*.sqlite)'
"""
if filters:
2013-08-31 18:17:38 +00:00
filters += ';;'
filters += '%s (*)' % UiStrings().AllFiles
2014-03-04 18:49:30 +00:00
filename = QtGui.QFileDialog.getOpenFileName(
self, title, os.path.dirname(Settings().value(self.plugin.settings_section + '/' + setting_name)), filters)
2011-02-10 19:37:02 +00:00
if filename:
editbox.setText(filename)
2013-08-31 18:17:38 +00:00
Settings().setValue(self.plugin.settings_section + '/' + setting_name, filename)
2013-03-07 08:05:43 +00:00
def get_folder(self, title, editbox, setting_name):
2012-05-07 10:36:39 +00:00
"""
Opens a QFileDialog and saves the selected folder to the given editbox.
2014-03-17 19:05:55 +00:00
:param title: The title of the dialog (unicode).
:param editbox: An editbox (QLineEdit).
:param setting_name: The place where to save the last opened directory.
2012-05-07 10:36:39 +00:00
"""
2014-03-04 18:49:30 +00:00
folder = QtGui.QFileDialog.getExistingDirectory(
self, title, Settings().value(self.plugin.settings_section + '/' + setting_name),
QtGui.QFileDialog.ShowDirsOnly)
2012-05-07 10:36:39 +00:00
if folder:
editbox.setText(folder)
2014-03-20 19:10:31 +00:00
Settings().setValue(self.plugin.settings_section + '/' + setting_name, folder)