openlp/openlp/core/lib/ui.py

335 lines
14 KiB
Python
Raw Normal View History

# -*- 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 #
2011-03-24 19:04:02 +00:00
# Gorven, Scott Guerrieri, Matthias Hub, Meinert Jordan, Armin Köhler, #
# Andreas Preikschat, Mattias Põldaru, Christian Richter, Philip Ridout, #
# Maikel Stuivenberg, Martin Thompson, Jon Tibble, 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:`ui` module provides standard UI components for OpenLP.
"""
import logging
from PyQt4 import QtCore, QtGui
2011-02-03 22:00:14 +00:00
from openlp.core.lib import build_icon, Receiver, translate
from openlp.core.utils.actions import ActionList
log = logging.getLogger(__name__)
2011-02-09 05:04:12 +00:00
class UiStrings(object):
"""
Provide standard strings for objects to use.
"""
# These strings should need a good reason to be retranslated elsewhere.
# Should some/more/less of these have an & attached?
2011-02-12 04:09:03 +00:00
About = translate('OpenLP.Ui', 'About')
2011-02-09 05:04:12 +00:00
Add = translate('OpenLP.Ui', '&Add')
2011-02-10 04:06:16 +00:00
Advanced = translate('OpenLP.Ui', 'Advanced')
2011-02-09 05:04:12 +00:00
AllFiles = translate('OpenLP.Ui', 'All Files')
2011-02-18 03:15:09 +00:00
Bottom = translate('OpenLP.Ui', 'Bottom')
2011-02-12 04:09:03 +00:00
Browse = translate('OpenLP.Ui', 'Browse...')
2011-02-18 01:07:55 +00:00
Cancel = translate('OpenLP.Ui', 'Cancel')
CCLINumberLabel = translate('OpenLP.Ui', 'CCLI number:')
2011-02-14 17:25:51 +00:00
CreateService = translate('OpenLP.Ui', 'Create a new service.')
Default = unicode(translate('OpenLP.Ui', 'Default'))
2011-02-09 05:04:12 +00:00
Delete = translate('OpenLP.Ui', '&Delete')
Edit = translate('OpenLP.Ui', '&Edit')
2011-02-11 21:17:56 +00:00
EmptyField = translate('OpenLP.Ui', 'Empty Field')
2011-02-09 05:04:12 +00:00
Error = translate('OpenLP.Ui', 'Error')
2011-02-12 02:43:13 +00:00
Export = translate('OpenLP.Ui', 'Export')
FontSizePtUnit = translate('OpenLP.Ui', 'pt',
'Abbreviated font pointsize unit')
2011-03-26 19:50:15 +00:00
Hours = translate('OpenLP.Ui', 'h', 'The abbreviated unit for hours')
2011-02-12 04:09:03 +00:00
Image = translate('OpenLP.Ui', 'Image')
2011-02-09 05:04:12 +00:00
Import = translate('OpenLP.Ui', 'Import')
2011-02-13 13:11:15 +00:00
LengthTime = unicode(translate('OpenLP.Ui', 'Length %s'))
2011-02-09 05:04:12 +00:00
Live = translate('OpenLP.Ui', 'Live')
2011-02-14 16:08:17 +00:00
LiveBGError = translate('OpenLP.Ui', 'Live Background Error')
2011-02-11 22:15:13 +00:00
LivePanel = translate('OpenLP.Ui', 'Live Panel')
2011-02-09 05:04:12 +00:00
Load = translate('OpenLP.Ui', 'Load')
2011-03-26 19:50:15 +00:00
Minutes = translate('OpenLP.Ui', 'm', 'The abbreviated unit for minutes')
2011-02-18 03:15:09 +00:00
Middle = translate('OpenLP.Ui', 'Middle')
2011-02-09 05:04:12 +00:00
New = translate('OpenLP.Ui', 'New')
NewService = translate('OpenLP.Ui', 'New Service')
2011-02-18 01:07:55 +00:00
NewTheme = translate('OpenLP.Ui', 'New Theme')
2011-02-11 21:17:56 +00:00
NFSs = translate('OpenLP.Ui', 'No File Selected', 'Singular')
NFSp = translate('OpenLP.Ui', 'No Files Selected', 'Plural')
NISs = translate('OpenLP.Ui', 'No Item Selected', 'Singular')
NISp = translate('OpenLP.Ui', 'No Items Selected', 'Plural')
2011-02-12 20:25:40 +00:00
OLPV1 = translate('OpenLP.Ui', 'openlp.org 1.x')
2011-02-09 05:04:12 +00:00
OLPV2 = translate('OpenLP.Ui', 'OpenLP 2.0')
2011-03-26 19:34:01 +00:00
OpenLPStart = translate('OpenLP.Ui', 'OpenLP is already running. Do you '
2011-03-27 16:30:40 +00:00
'wish to continue?')
2011-02-14 17:25:51 +00:00
OpenService = translate('OpenLP.Ui', 'Open Service')
2011-02-09 05:04:12 +00:00
Preview = translate('OpenLP.Ui', 'Preview')
2011-02-11 22:15:13 +00:00
PreviewPanel = translate('OpenLP.Ui', 'Preview Panel')
PrintServiceOrder = translate('OpenLP.Ui', 'Print Service Order')
2011-02-10 00:36:00 +00:00
ReplaceBG = translate('OpenLP.Ui', 'Replace Background')
ReplaceLiveBG = translate('OpenLP.Ui', 'Replace Live Background')
ResetBG = translate('OpenLP.Ui', 'Reset Background')
ResetLiveBG = translate('OpenLP.Ui', 'Reset Live Background')
2011-03-26 19:50:15 +00:00
Seconds = translate('OpenLP.Ui', 's', 'The abbreviated unit for seconds')
SaveAndPreview = translate('OpenLP.Ui', 'Save && Preview')
2011-02-12 04:09:03 +00:00
Search = translate('OpenLP.Ui', 'Search')
2011-02-13 01:09:04 +00:00
SelectDelete = translate('OpenLP.Ui', 'You must select an item to delete.')
2011-02-13 14:21:12 +00:00
SelectEdit = translate('OpenLP.Ui', 'You must select an item to edit.')
2011-02-14 17:25:51 +00:00
SaveService = translate('OpenLP.Ui', 'Save Service')
2011-02-09 05:04:12 +00:00
Service = translate('OpenLP.Ui', 'Service')
2011-02-13 13:11:15 +00:00
StartTimeCode = unicode(translate('OpenLP.Ui', 'Start %s'))
2011-02-11 21:17:56 +00:00
Theme = translate('OpenLP.Ui', 'Theme', 'Singular')
Themes = translate('OpenLP.Ui', 'Themes', 'Plural')
2011-02-18 03:15:09 +00:00
Top = translate('OpenLP.Ui', 'Top')
2011-02-12 04:09:03 +00:00
Version = translate('OpenLP.Ui', 'Version')
2011-02-09 05:04:12 +00:00
def add_welcome_page(parent, image):
"""
Generate an opening welcome page for a wizard using a provided image.
2011-02-05 01:24:19 +00:00
``parent``
A ``QWizard`` object to add the welcome page to.
``image``
A splash image for the wizard.
"""
parent.welcomePage = QtGui.QWizardPage()
parent.welcomePage.setPixmap(QtGui.QWizard.WatermarkPixmap,
QtGui.QPixmap(image))
parent.welcomePage.setObjectName(u'WelcomePage')
parent.welcomeLayout = QtGui.QVBoxLayout(parent.welcomePage)
parent.welcomeLayout.setObjectName(u'WelcomeLayout')
parent.titleLabel = QtGui.QLabel(parent.welcomePage)
parent.titleLabel.setObjectName(u'TitleLabel')
parent.welcomeLayout.addWidget(parent.titleLabel)
parent.welcomeLayout.addSpacing(40)
parent.informationLabel = QtGui.QLabel(parent.welcomePage)
parent.informationLabel.setWordWrap(True)
parent.informationLabel.setObjectName(u'InformationLabel')
parent.welcomeLayout.addWidget(parent.informationLabel)
parent.welcomeLayout.addStretch()
parent.addPage(parent.welcomePage)
2011-02-02 19:30:06 +00:00
2011-02-10 00:36:00 +00:00
def create_accept_reject_button_box(parent, okay=False):
2011-02-02 19:30:06 +00:00
"""
2011-02-10 00:36:00 +00:00
Creates a standard dialog button box with two buttons. The buttons default
to save and cancel but the ``okay`` parameter can be used to make the
buttons okay and cancel instead.
The button box is connected to the parent's ``accept()`` and ``reject()``
2011-02-05 01:24:19 +00:00
methods to handle the default ``accepted()`` and ``rejected()`` signals.
``parent``
2011-02-25 17:05:01 +00:00
The parent object. This should be a ``QWidget`` descendant.
2011-02-10 00:36:00 +00:00
``okay``
If true creates an okay/cancel combination instead of save/cancel.
2011-02-02 19:30:06 +00:00
"""
button_box = QtGui.QDialogButtonBox(parent)
2011-02-10 00:36:00 +00:00
accept_button = QtGui.QDialogButtonBox.Save
if okay:
accept_button = QtGui.QDialogButtonBox.Ok
button_box.setStandardButtons(accept_button | QtGui.QDialogButtonBox.Cancel)
2011-02-02 19:30:06 +00:00
button_box.setObjectName(u'%sButtonBox' % parent)
QtCore.QObject.connect(button_box, QtCore.SIGNAL(u'accepted()'),
parent.accept)
QtCore.QObject.connect(button_box, QtCore.SIGNAL(u'rejected()'),
parent.reject)
return button_box
def critical_error_message_box(title=None, message=None, parent=None,
question=False):
"""
Provides a standard critical message box for errors that OpenLP displays
to users.
``title``
The title for the message box.
``message``
The message to display to the user.
``parent``
The parent UI element to attach the dialog to.
``question``
Should this message box question the user.
"""
if question:
2011-02-09 14:04:50 +00:00
return QtGui.QMessageBox.critical(parent, UiStrings.Error, message,
QtGui.QMessageBox.StandardButtons(
QtGui.QMessageBox.Yes | QtGui.QMessageBox.No))
data = {u'message': message}
2011-02-09 05:04:12 +00:00
data[u'title'] = title if title else UiStrings.Error
return Receiver.send_message(u'openlp_error_message', data)
2011-02-03 02:46:02 +00:00
def media_item_combo_box(parent, name):
"""
Provide a standard combo box for media items.
"""
combo = QtGui.QComboBox(parent)
combo.setObjectName(name)
combo.setSizeAdjustPolicy(QtGui.QComboBox.AdjustToMinimumContentsLength)
combo.setSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Fixed)
return combo
2011-02-03 17:56:22 +00:00
2011-02-16 03:06:34 +00:00
def create_delete_push_button(parent, icon=None):
2011-02-03 17:56:22 +00:00
"""
2011-02-25 17:05:01 +00:00
Creates a standard push button with a delete label and optional icon. The
2011-02-05 01:24:19 +00:00
button is connected to the parent's ``onDeleteButtonClicked()`` method to
handle the ``clicked()`` signal.
``parent``
2011-02-25 17:05:01 +00:00
The parent object. This should be a ``QWidget`` descendant.
2011-02-05 01:24:19 +00:00
``icon``
2011-02-25 17:05:01 +00:00
An icon to display on the button. This can be either a ``QIcon``, a
2011-02-05 01:24:19 +00:00
resource path or a file name.
2011-02-03 17:56:22 +00:00
"""
delete_button = QtGui.QPushButton(parent)
delete_button.setObjectName(u'deleteButton')
delete_icon = icon if icon else u':/general/general_delete.png'
delete_button.setIcon(build_icon(delete_icon))
2011-02-09 05:04:12 +00:00
delete_button.setText(UiStrings.Delete)
2011-02-16 03:06:34 +00:00
delete_button.setToolTip(
translate('OpenLP.Ui', 'Delete the selected item.'))
2011-02-03 17:56:22 +00:00
QtCore.QObject.connect(delete_button,
QtCore.SIGNAL(u'clicked()'), parent.onDeleteButtonClicked)
return delete_button
2011-02-03 18:19:56 +00:00
2011-02-05 01:24:19 +00:00
def create_up_down_push_button_set(parent):
2011-02-03 18:19:56 +00:00
"""
2011-02-05 01:24:19 +00:00
Creates a standard set of two push buttons, one for up and the other for
2011-02-25 17:05:01 +00:00
down, for use with lists. The buttons use arrow icons and no text and are
2011-02-05 01:24:19 +00:00
connected to the parent's ``onUpButtonClicked()`` and
``onDownButtonClicked()`` to handle their respective ``clicked()`` signals.
``parent``
2011-02-25 17:05:01 +00:00
The parent object. This should be a ``QWidget`` descendant.
2011-02-03 18:19:56 +00:00
"""
up_button = QtGui.QPushButton(parent)
up_button.setIcon(build_icon(u':/services/service_up.png'))
up_button.setObjectName(u'upButton')
up_button.setToolTip(
translate('OpenLP.Ui', 'Move selection up one position.'))
down_button = QtGui.QPushButton(parent)
down_button.setIcon(build_icon(u':/services/service_down.png'))
down_button.setObjectName(u'downButton')
down_button.setToolTip(
translate('OpenLP.Ui', 'Move selection down one position.'))
QtCore.QObject.connect(up_button,
QtCore.SIGNAL(u'clicked()'), parent.onUpButtonClicked)
QtCore.QObject.connect(down_button,
QtCore.SIGNAL(u'clicked()'), parent.onDownButtonClicked)
return up_button, down_button
2011-02-04 01:56:19 +00:00
2011-03-29 16:44:36 +00:00
def base_action(parent, name, category=None):
2011-02-04 01:56:19 +00:00
"""
Return the most basic action with the object name set.
2011-03-30 18:12:05 +00:00
``category``
The category the action should be listed in the shortcut dialog. If you
not wish, that this action is added to the shortcut dialog, then do not
state any.
2011-02-04 01:56:19 +00:00
"""
action = QtGui.QAction(parent)
action.setObjectName(name)
2011-03-29 16:44:36 +00:00
if category is not None:
ActionList.add_action(action, category)
2011-02-04 01:56:19 +00:00
return action
2011-03-29 16:44:36 +00:00
def checkable_action(parent, name, checked=None, category=None):
2011-02-04 01:56:19 +00:00
"""
Return a standard action with the checkable attribute set.
"""
2011-03-29 16:44:36 +00:00
action = base_action(parent, name, category)
2011-02-04 01:56:19 +00:00
action.setCheckable(True)
if checked is not None:
action.setChecked(checked)
return action
2011-03-29 16:44:36 +00:00
def icon_action(parent, name, icon, checked=None, category=None):
2011-02-04 01:56:19 +00:00
"""
Return a standard action with an icon.
"""
if checked is not None:
2011-03-29 16:44:36 +00:00
action = checkable_action(parent, name, checked, category)
2011-02-04 01:56:19 +00:00
else:
2011-03-29 16:44:36 +00:00
action = base_action(parent, name, category)
2011-02-04 01:56:19 +00:00
action.setIcon(build_icon(icon))
return action
2011-02-04 02:56:59 +00:00
2011-03-31 18:53:50 +00:00
def shortcut_action(parent, name, shortcuts, function, icon=None, checked=None,
category=None):
2011-02-04 02:56:59 +00:00
"""
Return a shortcut enabled action.
"""
2011-03-31 18:53:50 +00:00
if icon is not None:
action = icon_action(parent, name, icon, checked, category)
elif checked is not None:
action = checkable_action(parent, name, checked, category)
else:
action = base_action(parent, name, category)
2011-02-04 02:56:59 +00:00
action.setShortcuts(shortcuts)
action.setShortcutContext(QtCore.Qt.WindowShortcut)
# We have to save the default shortcut again, as the action's shortcut was
# set after adding the shortcut to the action list.
action.defaultShortcuts = action.shortcuts()
2011-02-04 02:56:59 +00:00
QtCore.QObject.connect(action, QtCore.SIGNAL(u'triggered()'), function)
return action
2011-02-04 18:17:28 +00:00
def add_widget_completer(cache, widget):
"""
2011-02-05 01:24:19 +00:00
Adds a text autocompleter to a widget.
``cache``
The list of items to use as suggestions.
``widget``
The object to use the completer.
2011-02-04 18:17:28 +00:00
"""
completer = QtGui.QCompleter(cache)
completer.setCaseSensitivity(QtCore.Qt.CaseInsensitive)
widget.setCompleter(completer)
2011-02-08 17:29:24 +00:00
2011-02-08 19:31:55 +00:00
def create_valign_combo(form, parent, layout):
2011-02-08 17:29:24 +00:00
"""
Creates a standard label and combo box for asking users to select a
vertical alignment.
2011-02-08 19:31:55 +00:00
``form``
The UI screen that the label and combo will appear on.
2011-02-08 17:29:24 +00:00
``parent``
2011-02-25 17:05:01 +00:00
The parent object. This should be a ``QWidget`` descendant.
2011-02-08 17:29:24 +00:00
``layout``
A layout object to add the label and combo widgets to.
"""
verticalLabel = QtGui.QLabel(parent)
verticalLabel.setObjectName(u'VerticalLabel')
verticalLabel.setText(translate('OpenLP.Ui', '&Vertical Align:'))
2011-02-08 19:31:55 +00:00
form.verticalComboBox = QtGui.QComboBox(parent)
form.verticalComboBox.setObjectName(u'VerticalComboBox')
2011-02-18 03:15:09 +00:00
form.verticalComboBox.addItem(UiStrings.Top)
form.verticalComboBox.addItem(UiStrings.Middle)
form.verticalComboBox.addItem(UiStrings.Bottom)
2011-02-08 19:31:55 +00:00
verticalLabel.setBuddy(form.verticalComboBox)
layout.addRow(verticalLabel, form.verticalComboBox)