2009-02-21 19:23:54 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4
|
2009-07-14 13:51:27 +00:00
|
|
|
|
2009-09-08 19:58:05 +00:00
|
|
|
###############################################################################
|
|
|
|
# OpenLP - Open Source Lyrics Projection #
|
|
|
|
# --------------------------------------------------------------------------- #
|
2010-12-26 11:04:47 +00:00
|
|
|
# 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 #
|
2009-09-08 19:58:05 +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 #
|
|
|
|
###############################################################################
|
2010-06-19 17:31:42 +00:00
|
|
|
"""
|
|
|
|
Provide common toolbar handling for OpenLP
|
|
|
|
"""
|
2009-07-08 06:55:08 +00:00
|
|
|
import logging
|
2009-02-21 19:23:54 +00:00
|
|
|
|
2011-01-21 19:09:56 +00:00
|
|
|
from PyQt4 import QtCore, QtGui
|
|
|
|
|
2009-11-30 20:29:26 +00:00
|
|
|
from openlp.core.lib import build_icon
|
2009-09-29 02:54:32 +00:00
|
|
|
|
2010-03-04 22:09:03 +00:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2009-09-29 12:51:38 +00:00
|
|
|
class OpenLPToolbar(QtGui.QToolBar):
|
2009-02-21 19:23:54 +00:00
|
|
|
"""
|
2009-07-08 06:55:08 +00:00
|
|
|
Lots of toolbars around the place, so it makes sense to have a common way
|
|
|
|
to manage them. This is the base toolbar class.
|
2009-02-21 19:23:54 +00:00
|
|
|
"""
|
|
|
|
def __init__(self, parent):
|
2009-07-08 06:55:08 +00:00
|
|
|
"""
|
|
|
|
Initialise the toolbar.
|
|
|
|
"""
|
2010-07-06 21:59:52 +00:00
|
|
|
QtGui.QToolBar.__init__(self, parent)
|
2009-05-01 22:26:43 +00:00
|
|
|
# useful to be able to reuse button icons...
|
|
|
|
self.icons = {}
|
2009-09-29 12:51:38 +00:00
|
|
|
self.setIconSize(QtCore.QSize(20, 20))
|
2009-08-27 05:17:20 +00:00
|
|
|
self.actions = {}
|
2011-03-12 17:02:24 +00:00
|
|
|
log.debug(u'Init done for %s' % parent.__class__.__name__)
|
2009-05-01 22:26:43 +00:00
|
|
|
|
2009-09-15 18:56:56 +00:00
|
|
|
def addToolbarButton(self, title, icon, tooltip=None, slot=None,
|
2011-03-31 18:53:50 +00:00
|
|
|
checkable=False, shortcuts=None, context=QtCore.Qt.WidgetShortcut):
|
2009-02-21 19:23:54 +00:00
|
|
|
"""
|
|
|
|
A method to help developers easily add a button to the toolbar.
|
2009-07-08 06:55:08 +00:00
|
|
|
|
|
|
|
``title``
|
|
|
|
The title of the button.
|
|
|
|
|
|
|
|
``icon``
|
|
|
|
The icon of the button. This can be an instance of QIcon, or a
|
2011-02-05 18:01:08 +00:00
|
|
|
string containing either the absolute path to the image, or an
|
2009-07-08 06:55:08 +00:00
|
|
|
internal resource path starting with ':/'.
|
|
|
|
|
|
|
|
``tooltip``
|
|
|
|
A hint or tooltip for this button.
|
|
|
|
|
|
|
|
``slot``
|
|
|
|
The method to run when this button is clicked.
|
|
|
|
|
2011-01-18 20:14:44 +00:00
|
|
|
``checkable``
|
|
|
|
If *True* the button has two, *off* and *on*, states. Default is
|
|
|
|
*False*, which means the buttons has only one state.
|
2011-01-31 20:39:55 +00:00
|
|
|
|
2011-03-31 18:53:50 +00:00
|
|
|
``shortcuts``
|
|
|
|
The list of shortcuts for this action
|
2011-01-31 20:39:55 +00:00
|
|
|
|
2011-01-19 21:17:32 +00:00
|
|
|
``context``
|
|
|
|
Specify the context in which this shortcut is valid
|
2009-02-21 19:23:54 +00:00
|
|
|
"""
|
2009-12-06 08:39:56 +00:00
|
|
|
if icon:
|
2011-01-05 16:50:28 +00:00
|
|
|
actionIcon = build_icon(icon)
|
2009-11-13 18:46:47 +00:00
|
|
|
if slot and not checkable:
|
2011-01-05 16:50:28 +00:00
|
|
|
newAction = self.addAction(actionIcon, title, slot)
|
2009-08-28 17:40:07 +00:00
|
|
|
else:
|
2011-01-05 16:50:28 +00:00
|
|
|
newAction = self.addAction(actionIcon, title)
|
|
|
|
self.icons[title] = actionIcon
|
2009-12-06 08:39:56 +00:00
|
|
|
else:
|
2011-04-02 13:08:54 +00:00
|
|
|
newAction = QtGui.QAction(title, self)
|
2011-01-05 16:50:28 +00:00
|
|
|
self.addAction(newAction)
|
|
|
|
QtCore.QObject.connect(newAction,
|
2009-12-06 08:39:56 +00:00
|
|
|
QtCore.SIGNAL(u'triggered()'), slot)
|
|
|
|
if tooltip:
|
2011-01-05 16:50:28 +00:00
|
|
|
newAction.setToolTip(tooltip)
|
2009-12-06 08:39:56 +00:00
|
|
|
if checkable:
|
2011-01-05 16:50:28 +00:00
|
|
|
newAction.setCheckable(True)
|
|
|
|
QtCore.QObject.connect(newAction,
|
2009-12-06 08:39:56 +00:00
|
|
|
QtCore.SIGNAL(u'toggled(bool)'), slot)
|
2011-01-05 16:50:28 +00:00
|
|
|
self.actions[title] = newAction
|
2011-03-31 18:53:50 +00:00
|
|
|
if shortcuts is not None:
|
|
|
|
newAction.setShortcuts(shortcuts)
|
|
|
|
newAction.setShortcutContext(context)
|
2011-01-05 16:50:28 +00:00
|
|
|
return newAction
|
2009-05-01 22:26:43 +00:00
|
|
|
|
2009-08-28 17:40:07 +00:00
|
|
|
def addToolbarSeparator(self, handle):
|
|
|
|
"""
|
|
|
|
Add a Separator bar to the toolbar and store it's Handle
|
|
|
|
"""
|
|
|
|
action = self.addSeparator()
|
|
|
|
self.actions[handle] = action
|
|
|
|
|
|
|
|
def addToolbarWidget(self, handle, widget):
|
|
|
|
"""
|
|
|
|
Add a Widget to the toolbar and store it's Handle
|
|
|
|
"""
|
|
|
|
action = self.addWidget(widget)
|
|
|
|
self.actions[handle] = action
|
2009-05-01 22:26:43 +00:00
|
|
|
|
2009-02-21 19:23:54 +00:00
|
|
|
def getIconFromTitle(self, title):
|
2009-07-08 06:55:08 +00:00
|
|
|
"""
|
|
|
|
Search through the list of icons for an icon with a particular title,
|
|
|
|
and return that icon.
|
|
|
|
|
|
|
|
``title``
|
|
|
|
The title of the icon to search for.
|
|
|
|
"""
|
2009-12-02 08:55:15 +00:00
|
|
|
title = QtCore.QString(title)
|
2010-04-16 22:06:28 +00:00
|
|
|
try:
|
|
|
|
if self.icons[title]:
|
|
|
|
return self.icons[title]
|
2010-06-06 23:24:45 +00:00
|
|
|
except KeyError:
|
2010-05-05 17:17:00 +00:00
|
|
|
log.exception(u'getIconFromTitle - no icon for %s' % title)
|
2009-09-29 12:51:38 +00:00
|
|
|
return QtGui.QIcon()
|
2009-08-28 17:40:07 +00:00
|
|
|
|
|
|
|
def makeWidgetsInvisible(self, widgets):
|
2009-09-04 22:50:19 +00:00
|
|
|
"""
|
|
|
|
Hide a set of widgets.
|
|
|
|
|
|
|
|
``widgets``
|
|
|
|
The list of names of widgets to be hidden.
|
|
|
|
"""
|
2009-08-28 17:40:07 +00:00
|
|
|
for widget in widgets:
|
|
|
|
self.actions[widget].setVisible(False)
|
|
|
|
|
|
|
|
def makeWidgetsVisible(self, widgets):
|
2009-09-04 22:50:19 +00:00
|
|
|
"""
|
|
|
|
Show a set of widgets.
|
|
|
|
|
|
|
|
``widgets``
|
|
|
|
The list of names of widgets to be shown.
|
|
|
|
"""
|
2009-08-28 17:40:07 +00:00
|
|
|
for widget in widgets:
|
|
|
|
self.actions[widget].setVisible(True)
|
2009-10-01 23:43:16 +00:00
|
|
|
|
2009-11-30 20:29:26 +00:00
|
|
|
def addPushButton(self, image_file=None, text=u''):
|
2009-10-01 23:43:16 +00:00
|
|
|
"""
|
|
|
|
Adds a push button to the toolbar.
|
|
|
|
|
|
|
|
Returns the push button
|
|
|
|
"""
|
2009-11-30 20:29:26 +00:00
|
|
|
push_button = QtGui.QPushButton(build_icon(image_file), text)
|
|
|
|
push_button.setCheckable(True)
|
|
|
|
push_button.setFlat(True)
|
|
|
|
self.addWidget(push_button)
|
2011-01-05 16:50:28 +00:00
|
|
|
return push_button
|