~raoul-snyman/openlp/bug-1608194

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# -*- 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      #
# Gorven, Scott Guerrieri, Meinert Jordan, Armin Köhler, Andreas Preikschat,  #
# Christian Richter, Philip Ridout, Maikel Stuivenberg, Martin Thompson, Jon  #
# Tibble, Carsten Tinggaard, 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                          #
###############################################################################
"""
Provide common toolbar handling for OpenLP
"""
import logging

from PyQt4 import QtCore, QtGui

from openlp.core.lib import build_icon

log = logging.getLogger(__name__)

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.
        """
        QtGui.QToolBar.__init__(self, parent)
        # useful to be able to reuse button icons...
        self.icons = {}
        self.setIconSize(QtCore.QSize(20, 20))
        self.actions = {}
        log.debug(u'Init done')

    def addToolbarButton(self, title, icon, tooltip=None, slot=None,
        checkable=False, shortcut=0, alternate=0,
        context=QtCore.Qt.WidgetShortcut):
        """
        A method to help developers easily add a button to the toolbar.

        ``title``
            The title of the button.

        ``icon``
            The icon of the button. This can be an instance of QIcon, or a
            string containing either the absolute path to the image, or an
            internal resource path starting with ':/'.

        ``tooltip``
            A hint or tooltip for this button.

        ``slot``
            The method to run when this button is clicked.

        ``checkable``
            If *True* the button has two, *off* and *on*, states. Default is
            *False*, which means the buttons has only one state.

        ``shortcut``
            The primary shortcut for this action

        ``alternate``
            The alternate shortcut for this action

        ``context``
            Specify the context in which this shortcut is valid
        """
        newAction = None
        if icon:
            actionIcon = build_icon(icon)
            if slot and not checkable:
                newAction = self.addAction(actionIcon, title, slot)
            else:
                newAction = self.addAction(actionIcon, title)
            self.icons[title] = actionIcon
        else:
            newAction = QtGui.QAction(title, newAction)
            self.addAction(newAction)
            QtCore.QObject.connect(newAction,
                QtCore.SIGNAL(u'triggered()'), slot)
        if tooltip:
            newAction.setToolTip(tooltip)
        if checkable:
            newAction.setCheckable(True)
            QtCore.QObject.connect(newAction,
                QtCore.SIGNAL(u'toggled(bool)'), slot)
        self.actions[title] = newAction
        newAction.setShortcuts([shortcut, alternate])
        newAction.setShortcutContext(context)
        return newAction

    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.
        """
        title = QtCore.QString(title)
        try:
            if self.icons[title]:
                return self.icons[title]
        except KeyError:
            log.exception(u'getIconFromTitle - no icon for %s' % title)
            return QtGui.QIcon()

    def makeWidgetsInvisible(self, widgets):
        """
        Hide a set of widgets.

        ``widgets``
            The list of names of widgets to be hidden.
        """
        for widget in widgets:
            self.actions[widget].setVisible(False)

    def makeWidgetsVisible(self, widgets):
        """
        Show a set of widgets.

        ``widgets``
            The list of names of widgets to be shown.
        """
        for widget in widgets:
            self.actions[widget].setVisible(True)

    def addPushButton(self, image_file=None, text=u''):
        """
        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