openlp/openlp/core/lib/ui.py

372 lines
14 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2012-12-28 22:06:43 +00:00
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
###############################################################################
# OpenLP - Open Source Lyrics Projection #
# --------------------------------------------------------------------------- #
2012-12-29 20:56:56 +00:00
# Copyright (c) 2008-2013 Raoul Snyman #
# Portions copyright (c) 2008-2013 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 #
# --------------------------------------------------------------------------- #
# 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
2013-02-07 07:17:19 +00:00
from openlp.core.lib import Registry, UiStrings, build_icon, translate
from openlp.core.utils.actions import ActionList
2013-01-11 19:36:28 +00:00
log = logging.getLogger(__name__)
2012-04-22 19:50:18 +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()
2012-12-28 22:06:43 +00:00
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
2012-04-22 19:50:18 +00:00
2013-02-04 21:27:50 +00:00
def create_button_box(dialog, name, standard_buttons, custom_buttons=None):
2011-02-02 19:30:06 +00:00
"""
Creates a QDialogButtonBox with the given buttons. The ``accepted()`` and
``rejected()`` signals of the button box are connected with the dialogs
``accept()`` and ``reject()`` slots.
2011-02-05 01:24:19 +00:00
``dialog``
The parent object. This has to be a ``QDialog`` descendant.
``name``
A string which is set as object name.
``standard_buttons``
A list of strings for the used buttons. It might contain: ``ok``,
``save``, ``cancel``, ``close``, and ``defaults``.
2011-02-10 00:36:00 +00:00
``custom_buttons``
A list of additional buttons. If a item is a instance of
QtGui.QAbstractButton it is added with QDialogButtonBox.ActionRole.
Otherwhise the item has to be a tuple of a button and a ButtonRole.
2011-02-02 19:30:06 +00:00
"""
2013-02-04 21:27:50 +00:00
if custom_buttons is None:
custom_buttons = []
buttons = QtGui.QDialogButtonBox.NoButton
if u'ok' in standard_buttons:
buttons |= QtGui.QDialogButtonBox.Ok
if u'save' in standard_buttons:
buttons |= QtGui.QDialogButtonBox.Save
if u'cancel' in standard_buttons:
buttons |= QtGui.QDialogButtonBox.Cancel
if u'close' in standard_buttons:
buttons |= QtGui.QDialogButtonBox.Close
if u'defaults' in standard_buttons:
buttons |= QtGui.QDialogButtonBox.RestoreDefaults
button_box = QtGui.QDialogButtonBox(dialog)
button_box.setObjectName(name)
button_box.setStandardButtons(buttons)
for button in custom_buttons:
if isinstance(button, QtGui.QAbstractButton):
button_box.addButton(button, QtGui.QDialogButtonBox.ActionRole)
else:
button_box.addButton(*button)
2012-12-28 22:06:43 +00:00
QtCore.QObject.connect(button_box, QtCore.SIGNAL(u'accepted()'), dialog.accept)
QtCore.QObject.connect(button_box, QtCore.SIGNAL(u'rejected()'), dialog.reject)
return button_box
2012-04-22 19:50:18 +00:00
2012-12-28 22:06:43 +00:00
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:
return QtGui.QMessageBox.critical(parent, UiStrings().Error, message,
2012-12-28 22:06:43 +00:00
QtGui.QMessageBox.StandardButtons(QtGui.QMessageBox.Yes | QtGui.QMessageBox.No))
data = {u'message': message}
2013-02-05 21:42:15 +00:00
return Registry().get(u'main_window').error_message(title if title else UiStrings().Error, message)
2011-02-03 02:46:02 +00:00
2012-04-22 19:50:18 +00:00
def create_horizontal_adjusting_combo_box(parent, name):
2011-02-03 02:46:02 +00:00
"""
Creates a QComboBox with adapting width for media items.
``parent``
The parent widget.
``name``
A string set as object name for the combo box.
2011-02-03 02:46:02 +00:00
"""
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
2012-04-22 19:50:18 +00:00
def create_button(parent, name, **kwargs):
2011-02-03 17:56:22 +00:00
"""
Return an button with the object name set and the given parameters.
2012-04-04 07:26:51 +00:00
2011-02-05 01:24:19 +00:00
``parent``
A QtCore.QWidget for the buttons parent (required).
``name``
A string which is set as object name (required).
``role``
A string which can have one value out of ``delete``, ``up``, and
``down``. This decides about default values for properties like text,
icon, or tooltip.
``text``
A string for the action text.
2011-02-05 01:24:19 +00:00
``icon``
Either a QIcon, a resource string, or a file location string for the
action icon.
2011-02-05 01:24:19 +00:00
``tooltip``
A string for the action tool tip.
``enabled``
False in case the button should be disabled.
2011-02-03 18:19:56 +00:00
"""
if u'role' in kwargs:
role = kwargs.pop(u'role')
if role == u'delete':
kwargs.setdefault(u'text', UiStrings().Delete)
2012-12-28 22:06:43 +00:00
kwargs.setdefault(u'tooltip', translate('OpenLP.Ui', 'Delete the selected item.'))
elif role == u'up':
kwargs.setdefault(u'icon', u':/services/service_up.png')
2012-12-28 22:06:43 +00:00
kwargs.setdefault(u'tooltip', translate('OpenLP.Ui', 'Move selection up one position.'))
elif role == u'down':
kwargs.setdefault(u'icon', u':/services/service_down.png')
2012-12-28 22:06:43 +00:00
kwargs.setdefault(u'tooltip', translate('OpenLP.Ui', 'Move selection down one position.'))
else:
2012-04-02 19:34:49 +00:00
log.warn(u'The role "%s" is not defined in create_push_button().',
role)
if kwargs.pop(u'class', u'') == u'toolbutton':
button = QtGui.QToolButton(parent)
else:
button = QtGui.QPushButton(parent)
button.setObjectName(name)
if kwargs.get(u'text'):
button.setText(kwargs.pop(u'text'))
if kwargs.get(u'icon'):
button.setIcon(build_icon(kwargs.pop(u'icon')))
if kwargs.get(u'tooltip'):
button.setToolTip(kwargs.pop(u'tooltip'))
if not kwargs.pop(u'enabled', True):
button.setEnabled(False)
if kwargs.get(u'click'):
2012-12-28 22:06:43 +00:00
QtCore.QObject.connect(button, QtCore.SIGNAL(u'clicked()'), kwargs.pop(u'click'))
for key in kwargs.keys():
if key not in [u'text', u'icon', u'tooltip', u'click']:
log.warn(u'Parameter %s was not consumed in create_button().', key)
return button
2011-02-04 01:56:19 +00:00
2012-04-22 19:50:18 +00:00
def create_action(parent, name, **kwargs):
2011-02-04 01:56:19 +00:00
"""
Return an action with the object name set and the given parameters.
``parent``
A QtCore.QObject for the actions parent (required).
``name``
A string which is set as object name (required).
``text``
A string for the action text.
``icon``
Either a QIcon, a resource string, or a file location string for the
action icon.
``can_shortcuts``
Boolean stating if this action has shortcuts or if it can have shortcuts. If ``True`` the action is added to
shortcut dialog. **Note**: Never set the shortcuts yourselt; use the :class:`~openlp.core.lib.Settings` class
to define the action's shortcuts.
``tooltip``
A string for the action tool tip.
``statustip``
A string for the action status tip.
``checked``
A bool for the state. If ``None`` the Action is not checkable.
``enabled``
False in case the action should be disabled.
``visible``
False in case the action should be hidden.
``separator``
True in case the action will be considered a separator.
``data``
2012-12-27 21:41:11 +00:00
The action's data.
``context``
A context for the shortcut execution.
2011-03-30 18:12:05 +00:00
``category``
A category the action should be listed in the shortcut dialog.
2011-02-04 02:56:59 +00:00
``triggers``
A slot which is connected to the actions ``triggered()`` slot.
2011-02-04 02:56:59 +00:00
"""
2011-04-02 15:37:14 +00:00
action = QtGui.QAction(parent)
action.setObjectName(name)
if kwargs.get(u'text'):
action.setText(kwargs.pop(u'text'))
if kwargs.get(u'icon'):
action.setIcon(build_icon(kwargs.pop(u'icon')))
if kwargs.get(u'tooltip'):
action.setToolTip(kwargs.pop(u'tooltip'))
if kwargs.get(u'statustip'):
action.setStatusTip(kwargs.pop(u'statustip'))
if kwargs.get(u'checked') is not None:
2011-04-02 15:37:14 +00:00
action.setCheckable(True)
action.setChecked(kwargs.pop(u'checked'))
if not kwargs.pop(u'enabled', True):
action.setEnabled(False)
if not kwargs.pop(u'visible', True):
action.setVisible(False)
if kwargs.pop(u'separator', False):
action.setSeparator(True)
if u'data' in kwargs:
2012-05-17 15:13:09 +00:00
action.setData(kwargs.pop(u'data'))
if u'context' in kwargs:
action.setShortcutContext(kwargs.pop(u'context'))
if kwargs.get(u'triggers'):
QtCore.QObject.connect(action, QtCore.SIGNAL(u'triggered(bool)'), kwargs.pop(u'triggers'))
if kwargs.pop(u'can_shortcuts', False):
if not action.objectName():
raise Exception("objectName not set")
action_list = ActionList.get_instance()
action_list.add_action(action, kwargs.pop(u'category', None))
else:
pass
#print u'else', action.objectName()
for key in kwargs.keys():
if key not in [u'text', u'icon', u'tooltip', u'statustip', u'checked', u'category', u'triggers']:
log.warn(u'Parameter %s was not consumed in create_action().', key)
2011-02-04 02:56:59 +00:00
return action
2011-02-04 18:17:28 +00:00
2012-04-22 19:50:18 +00:00
def create_widget_action(parent, name=u'', **kwargs):
"""
Return a new QAction by calling ``create_action(parent, name, **kwargs)``.
The shortcut context defaults to ``QtCore.Qt.WidgetShortcut`` and the action
is added to the parents action list.
"""
2012-03-26 09:41:59 +00:00
kwargs.setdefault(u'context', QtCore.Qt.WidgetShortcut)
action = create_action(parent, name, **kwargs)
parent.addAction(action)
return action
2012-04-22 19:50:18 +00:00
def set_case_insensitive_completer(cache, widget):
"""
Sets a case insensitive text completer for a widget.
2011-02-05 01:24:19 +00:00
``cache``
The list of items to use as suggestions.
``widget``
A widget to set the completer (QComboBox or QTextEdit instance)
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
2012-04-22 19:50:18 +00:00
def create_valign_selection_widgets(parent):
2011-02-08 17:29:24 +00:00
"""
Creates a standard label and combo box for asking users to select a
vertical alignment.
``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
Returns a tuple of QLabel and QComboBox.
2011-02-08 17:29:24 +00:00
"""
label = QtGui.QLabel(parent)
label.setText(translate('OpenLP.Ui', '&Vertical Align:'))
combo_box = QtGui.QComboBox(parent)
2012-12-28 22:06:43 +00:00
combo_box.addItems([UiStrings().Top, UiStrings().Middle, UiStrings().Bottom])
label.setBuddy(combo_box)
return label, combo_box
2012-04-22 19:50:18 +00:00
2011-04-10 18:54:26 +00:00
def find_and_set_in_combo_box(combo_box, value_to_find):
"""
Find a string in a combo box and set it as the selected item if present
``combo_box``
The combo box to check for selected items
``value_to_find``
The value to find
"""
index = combo_box.findText(value_to_find, QtCore.Qt.MatchExactly)
if index == -1:
# Not Found.
index = 0
2011-04-15 21:55:11 +00:00
combo_box.setCurrentIndex(index)