openlp/openlp/core/ui/wizard.py

213 lines
8.4 KiB
Python
Raw Normal View History

2011-01-13 17:55:29 +00:00
# -*- coding: utf-8 -*-
# vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4
###############################################################################
# OpenLP - Open Source Lyrics Projection #
# --------------------------------------------------------------------------- #
# Copyright (c) 2008-2011 Raoul Snyman #
# Portions copyright (c) 2008-2011 Tim Bentley, Jonathan Corwin, Michael #
# Gorven, Scott Guerrieri, Meinert Jordan, Andreas Preikschat, Christian #
# Richter, Philip Ridout, Maikel Stuivenberg, Martin Thompson, Jon Tibble, #
# Carsten Tinggaard, Frode Woldsund #
# --------------------------------------------------------------------------- #
# 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
from PyQt4 import QtCore, QtGui
2011-02-12 04:09:03 +00:00
from openlp.core.lib import build_icon, Receiver, SettingsManager, translate, \
StringContent
2011-02-10 21:07:28 +00:00
from openlp.core.lib.ui import UiStrings, add_welcome_page
2011-01-13 17:55:29 +00:00
log = logging.getLogger(__name__)
2011-02-12 02:43:13 +00:00
class WizardStrings(object):
"""
Provide standard strings for wizards to use.
"""
# These strings should need a good reason to be retranslated elsewhere.
Description = unicode(translate('OpenLP.Ui', 'This wizard will help you '
'to %s %s from a variety of formats. Click the next button '
'below to start the process by selecting a format to %s from.',
'Variable 1 & 3 are import/export. Variable 2 is a type like Bibles'))
FinishedType = unicode(translate('OpenLP.Ui', 'Finished %s.'))
FormatLabel = translate('OpenLP.Ui', 'Format:')
2011-02-12 15:37:02 +00:00
Importing = translate('OpenLP.Ui', 'Importing')
2011-02-12 02:43:13 +00:00
ImportSelect = translate('OpenLP.Ui', 'Select Import Source')
ImportSelectLong = unicode(translate('OpenLP.Ui',
'Select the import format and the location to import from.'))
Welcome = u'<span style="font-size:14pt; font-weight:600;">%s</span>' % \
translate('OpenLP.Ui', 'Welcome to the %s %s Wizard',
'Variable 1 is the type e.g. Bible and variable 2 is import/export')
2011-01-13 17:55:29 +00:00
class OpenLPWizard(QtGui.QWizard):
"""
Generic OpenLP wizard to provide generic functionality and a unified look
and feel.
"""
2011-02-12 04:09:03 +00:00
def __init__(self, parent, plugin, name, image, direction):
2011-01-13 17:55:29 +00:00
QtGui.QWizard.__init__(self, parent)
2011-02-12 02:43:13 +00:00
self.plugin = plugin
2011-01-13 17:55:29 +00:00
self.setObjectName(name)
2011-02-12 04:09:03 +00:00
self.itemType = self.plugin.getString(StringContent.Name)
self.direction = direction
2011-01-13 17:55:29 +00:00
self.openIcon = build_icon(u':/general/general_open.png')
self.deleteIcon = build_icon(u':/general/general_delete.png')
self.finishButton = self.button(QtGui.QWizard.FinishButton)
self.cancelButton = self.button(QtGui.QWizard.CancelButton)
self.setupUi(image)
self.registerFields()
self.customInit()
self.customSignals()
QtCore.QObject.connect(self, QtCore.SIGNAL(u'currentIdChanged(int)'),
self.onCurrentIdChanged)
def setupUi(self, image):
"""
Set up the wizard UI
"""
self.setModal(True)
self.setWizardStyle(QtGui.QWizard.ModernStyle)
self.setOptions(QtGui.QWizard.IndependentPages |
QtGui.QWizard.NoBackButtonOnStartPage |
QtGui.QWizard.NoBackButtonOnLastPage)
add_welcome_page(self, image)
2011-01-13 17:55:29 +00:00
self.addCustomPages()
self.addProgressPage()
self.retranslateUi()
QtCore.QMetaObject.connectSlotsByName(self)
2011-02-12 04:09:03 +00:00
def retranslateUi(self):
"""
Provides generic wizard localisation
"""
self.titleLabel.setText(WizardStrings.Welcome %
(self.itemType[u'singular'], self.direction))
2011-02-10 19:28:17 +00:00
def registerFields(self):
"""
Hook method for wizards to register any fields they need.
"""
pass
2011-01-13 17:55:29 +00:00
def addProgressPage(self):
"""
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.
"""
self.progressPage = QtGui.QWizardPage()
self.progressPage.setObjectName(u'progressPage')
self.progressLayout = QtGui.QVBoxLayout(self.progressPage)
self.progressLayout.setMargin(48)
self.progressLayout.setObjectName(u'progressLayout')
self.progressLabel = QtGui.QLabel(self.progressPage)
self.progressLabel.setObjectName(u'progressLabel')
self.progressLayout.addWidget(self.progressLabel)
self.progressBar = QtGui.QProgressBar(self.progressPage)
self.progressBar.setObjectName(u'progressBar')
self.progressLayout.addWidget(self.progressBar)
self.addPage(self.progressPage)
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.
"""
log.debug(u'Wizard cancelled by user.')
if self.currentPage() == self.progressPage:
Receiver.send_message(u'openlp_stop_wizard')
2011-01-18 18:53:09 +00:00
self.done(QtGui.QDialog.Rejected)
2011-01-13 17:55:29 +00:00
def onCurrentIdChanged(self, pageId):
"""
Perform necessary functions depending on which wizard page is active.
"""
if self.page(pageId) == self.progressPage:
self.preWizard()
self.performWizard()
self.postWizard()
def incrementProgressBar(self, status_text, increment=1):
"""
Update the wizard progress page.
``status_text``
Current status information to display.
``increment``
The value to increment the progress bar by.
"""
log.debug(u'IncrementBar %s', status_text)
self.progressLabel.setText(status_text)
if increment > 0:
self.progressBar.setValue(self.progressBar.value() + increment)
Receiver.send_message(u'openlp_process_events')
def preWizard(self):
"""
Prepare the UI for the import.
"""
self.finishButton.setVisible(False)
self.progressBar.setMinimum(0)
self.progressBar.setMaximum(1188)
self.progressBar.setValue(0)
def postWizard(self):
"""
Clean up the UI after the import has finished.
"""
self.progressBar.setValue(self.progressBar.maximum())
self.finishButton.setVisible(True)
self.cancelButton.setVisible(False)
Receiver.send_message(u'openlp_process_events')
2011-02-10 19:37:02 +00:00
def getFileName(self, title, editbox, filters=u''):
"""
Opens a QFileDialog and saves the filename to the given editbox.
``title``
The title of the dialog (unicode).
``editbox``
A editbox (QLineEdit).
``filters``
The file extension filters. It should contain the file description
as well as the file extension. For example::
u'OpenLP 2.0 Databases (*.sqlite)'
"""
if filters:
filters += u';;'
filters += u'%s (*)' % UiStrings.AllFiles
filename = QtGui.QFileDialog.getOpenFileName(self, title,
os.path.dirname(SettingsManager.get_last_dir(
self.plugin.settingsSection, 1)), filters)
if filename:
editbox.setText(filename)
SettingsManager.set_last_dir(self.plugin.settingsSection,
filename, 1)