openlp/openlp/core/lib/toolbar.py

132 lines
5.1 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 #
# --------------------------------------------------------------------------- #
2011-12-27 10:33:55 +00:00
# Copyright (c) 2008-2012 Raoul Snyman #
# Portions copyright (c) 2008-2012 Tim Bentley, Gerald Britton, Jonathan #
2011-05-26 16:25:54 +00:00
# Corwin, Michael Gorven, Scott Guerrieri, Matthias Hub, Meinert Jordan, #
2011-05-26 17:11:22 +00:00
# Armin Köhler, Joshua Miller, Stevan Pettit, Andreas Preikschat, Mattias #
2011-06-12 16:02:52 +00:00
# Põldaru, Christian Richter, Philip Ridout, Simon Scudder, Jeffrey Smith, #
2011-06-12 15:41:01 +00:00
# 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 #
###############################################################################
2010-06-19 17:31:42 +00:00
"""
Provide common toolbar handling for OpenLP
"""
import logging
2011-01-21 19:09:56 +00:00
from PyQt4 import QtCore, QtGui
from openlp.core.lib import build_icon
from openlp.core.lib.ui import create_widget_action
2010-03-04 22:09:03 +00:00
log = logging.getLogger(__name__)
2009-09-29 12:51:38 +00:00
class OpenLPToolbar(QtGui.QToolBar):
"""
Lots of toolbars around the place, so it makes sense to have a common way
to manage them. This is the base toolbar class.
"""
def __init__(self, parent):
"""
Initialise the toolbar.
"""
2010-07-06 21:59:52 +00:00
QtGui.QToolBar.__init__(self, parent)
# useful to be able to reuse button icons...
self.icons = {}
2009-09-29 12:51:38 +00:00
self.setIconSize(QtCore.QSize(20, 20))
self.actions = {}
log.debug(u'Init done for %s' % parent.__class__.__name__)
def addToolbarButton(self, name, **kwargs):
"""
A method to help developers easily add a button to the toolbar. A new
QAction is created by calling ``create_action()``. The action is added
to the toolbar and the toolbar is set as parent. For more details please
look at openlp.core.lib.ui.create_action()
"""
action = create_widget_action(self, name, **kwargs)
# The ObjectNames should be used as keys. So translators can't break
# anything.
title = kwargs.get(u'text', u'')
self.actions[title] = action
if u'icon' in kwargs:
self.icons[title] = action.icon()
return action
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
def getIconFromTitle(self, title):
"""
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
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
"""
push_button = QtGui.QPushButton(build_icon(image_file), text)
push_button.setCheckable(True)
push_button.setFlat(True)
self.addWidget(push_button)
return push_button