openlp/openlp/core/lib/ui.py

360 lines
13 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-10-13 20:36:42 +00:00
from openlp.core.common import UiStrings, translate
from openlp.core.lib import Registry, build_icon
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.
"""
2013-03-07 08:05:43 +00:00
parent.welcome_page = QtGui.QWizardPage()
parent.welcome_page.setPixmap(QtGui.QWizard.WatermarkPixmap, QtGui.QPixmap(image))
2013-08-31 18:17:38 +00:00
parent.welcome_page.setObjectName('welcome_page')
2013-03-07 08:05:43 +00:00
parent.welcome_layout = QtGui.QVBoxLayout(parent.welcome_page)
2013-08-31 18:17:38 +00:00
parent.welcome_layout.setObjectName('WelcomeLayout')
2013-03-07 08:05:43 +00:00
parent.title_label = QtGui.QLabel(parent.welcome_page)
2013-08-31 18:17:38 +00:00
parent.title_label.setObjectName('title_label')
2013-03-07 08:05:43 +00:00
parent.welcome_layout.addWidget(parent.title_label)
parent.welcome_layout.addSpacing(40)
parent.information_label = QtGui.QLabel(parent.welcome_page)
parent.information_label.setWordWrap(True)
2013-08-31 18:17:38 +00:00
parent.information_label.setObjectName('information_label')
2013-03-07 08:05:43 +00:00
parent.welcome_layout.addWidget(parent.information_label)
parent.welcome_layout.addStretch()
parent.addPage(parent.welcome_page)
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
2013-08-31 18:17:38 +00:00
if 'ok' in standard_buttons:
buttons |= QtGui.QDialogButtonBox.Ok
2013-08-31 18:17:38 +00:00
if 'save' in standard_buttons:
buttons |= QtGui.QDialogButtonBox.Save
2013-08-31 18:17:38 +00:00
if 'cancel' in standard_buttons:
buttons |= QtGui.QDialogButtonBox.Cancel
2013-08-31 18:17:38 +00:00
if 'close' in standard_buttons:
buttons |= QtGui.QDialogButtonBox.Close
2013-08-31 18:17:38 +00:00
if '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)
2013-02-14 21:50:10 +00:00
button_box.accepted.connect(dialog.accept)
button_box.rejected.connect(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))
2013-08-31 18:17:38 +00:00
return Registry().get('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
"""
2013-08-31 18:17:38 +00:00
if 'role' in kwargs:
role = kwargs.pop('role')
if role == 'delete':
kwargs.setdefault('text', UiStrings().Delete)
kwargs.setdefault('tooltip', translate('OpenLP.Ui', 'Delete the selected item.'))
elif role == 'up':
kwargs.setdefault('icon', ':/services/service_up.png')
kwargs.setdefault('tooltip', translate('OpenLP.Ui', 'Move selection up one position.'))
elif role == 'down':
kwargs.setdefault('icon', ':/services/service_down.png')
kwargs.setdefault('tooltip', translate('OpenLP.Ui', 'Move selection down one position.'))
else:
2013-08-31 18:17:38 +00:00
log.warn('The role "%s" is not defined in create_push_button().', role)
if kwargs.pop('class', '') == 'toolbutton':
button = QtGui.QToolButton(parent)
else:
button = QtGui.QPushButton(parent)
button.setObjectName(name)
2013-08-31 18:17:38 +00:00
if kwargs.get('text'):
button.setText(kwargs.pop('text'))
if kwargs.get('icon'):
button.setIcon(build_icon(kwargs.pop('icon')))
if kwargs.get('tooltip'):
button.setToolTip(kwargs.pop('tooltip'))
if not kwargs.pop('enabled', True):
button.setEnabled(False)
2013-08-31 18:17:38 +00:00
if kwargs.get('click'):
button.clicked.connect(kwargs.pop('click'))
for key in list(kwargs.keys()):
if key not in ['text', 'icon', 'tooltip', 'click']:
log.warn('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.
``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.
2013-02-19 09:33:07 +00:00
``can_shortcuts``
2013-02-20 06:45:00 +00:00
Capability stating if this action can have shortcuts. If ``True`` the action is added to shortcut dialog
otherwise it it not. Define your shortcut in the :class:`~openlp.core.lib.Settings` class. *Note*: When *not*
``True`` you *must not* set a shortcuts at all.
2013-02-19 09:33:07 +00:00
``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)
2013-08-31 18:17:38 +00:00
if kwargs.get('text'):
action.setText(kwargs.pop('text'))
if kwargs.get('icon'):
action.setIcon(build_icon(kwargs.pop('icon')))
if kwargs.get('tooltip'):
action.setToolTip(kwargs.pop('tooltip'))
if kwargs.get('statustip'):
action.setStatusTip(kwargs.pop('statustip'))
if kwargs.get('checked') is not None:
2011-04-02 15:37:14 +00:00
action.setCheckable(True)
2013-08-31 18:17:38 +00:00
action.setChecked(kwargs.pop('checked'))
if not kwargs.pop('enabled', True):
action.setEnabled(False)
2013-08-31 18:17:38 +00:00
if not kwargs.pop('visible', True):
action.setVisible(False)
2013-08-31 18:17:38 +00:00
if kwargs.pop('separator', False):
action.setSeparator(True)
2013-08-31 18:17:38 +00:00
if 'data' in kwargs:
action.setData(kwargs.pop('data'))
if kwargs.pop('can_shortcuts', False):
action_list = ActionList.get_instance()
2013-08-31 18:17:38 +00:00
action_list.add_action(action, kwargs.pop('category', None))
if 'context' in kwargs:
action.setShortcutContext(kwargs.pop('context'))
if kwargs.get('triggers'):
action.triggered.connect(kwargs.pop('triggers'))
for key in list(kwargs.keys()):
if key not in ['text', 'icon', 'tooltip', 'statustip', 'checked', 'can_shortcuts',
'category', 'triggers']:
log.warn('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
2013-08-31 18:17:38 +00:00
def create_widget_action(parent, name='', **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.
"""
2013-08-31 18:17:38 +00:00
kwargs.setdefault('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.
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
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)