openlp/openlp/core/widgets/toolbar.py

116 lines
4.5 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2019-04-13 13:00:22 +00:00
##########################################################################
# OpenLP - Open Source Lyrics Projection #
# ---------------------------------------------------------------------- #
2022-02-01 10:10:57 +00:00
# Copyright (c) 2008-2022 OpenLP Developers #
2019-04-13 13:00:22 +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, either version 3 of the License, or #
# (at your option) any later version. #
# #
# 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, see <https://www.gnu.org/licenses/>. #
##########################################################################
2010-06-19 17:31:42 +00:00
"""
Provide common toolbar handling for OpenLP
"""
import logging
2015-11-07 00:49:40 +00:00
from PyQt5 import QtCore, QtWidgets
2011-01-21 19:09:56 +00:00
from openlp.core.lib.ui import create_widget_action
2018-10-02 04:39:42 +00:00
2010-03-04 22:09:03 +00:00
log = logging.getLogger(__name__)
2013-02-01 19:58:18 +00:00
2015-11-07 00:49:40 +00:00
class OpenLPToolbar(QtWidgets.QToolBar):
"""
2013-03-07 08:05:43 +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.
"""
def __init__(self, parent):
"""
Initialise the toolbar.
"""
2017-10-23 22:09:57 +00:00
super().__init__(parent)
# useful to be able to reuse button icons...
2009-09-29 12:51:38 +00:00
self.setIconSize(QtCore.QSize(20, 20))
self.actions = {}
2013-08-31 18:17:38 +00:00
log.debug('Init done for %s' % parent.__class__.__name__)
2013-03-07 06:48:09 +00:00
def add_toolbar_action(self, name, **kwargs):
"""
2013-03-07 08:05:43 +00:00
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)
self.actions[name] = action
return action
2013-03-07 06:48:09 +00:00
def add_toolbar_widget(self, widget):
2009-08-28 17:40:07 +00:00
"""
Add a widget and store it's handle under the widgets object name.
2009-08-28 17:40:07 +00:00
"""
action = self.addWidget(widget)
2012-05-17 18:57:01 +00:00
self.actions[widget.objectName()] = action
2009-09-04 22:50:19 +00:00
2013-03-07 06:48:09 +00:00
def set_widget_visible(self, widgets, visible=True):
2009-09-04 22:50:19 +00:00
"""
2013-12-24 08:56:50 +00:00
Set the visibility for a widget or a list of widgets.
2009-08-28 17:40:07 +00:00
2018-02-23 09:17:21 +00:00
:param widgets: A list of strings or individual string with widget object names.
2014-03-17 19:05:55 +00:00
:param visible: The new state as bool.
2009-09-04 22:50:19 +00:00
"""
2018-02-23 09:17:21 +00:00
if isinstance(widgets, list):
for handle in widgets:
if handle in self.actions:
self.actions[handle].setVisible(visible)
else:
log.warning('No handle "%s" in actions list.', str(handle))
else:
if widgets in self.actions:
self.actions[widgets].setVisible(visible)
else:
2018-02-23 09:17:21 +00:00
log.warning('No handle "%s" in actions list.', str(widgets))
def set_widget_enabled(self, widgets, enabled=True):
"""
Set the enabled state for a widget or a list of widgets.
:param widgets: A list of string with widget object names.
:param enabled: The new state as bool.
"""
for handle in widgets:
if handle in self.actions:
self.actions[handle].setEnabled(enabled)
else:
log.warning('No handle "%s" in actions list.', str(handle))
2020-04-26 21:37:35 +00:00
def remove_widget(self, name):
"""
Find and remove an action from the toolbar
:param name: The name of the action to me removed
:return:
"""
try:
act = self.actions[name]
self.removeAction(act)
except KeyError:
log.warning(f'No handle {name} in actions list.')
def add_spacer(self):
size_policy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Preferred)
separator = QtWidgets.QWidget()
separator.setSizePolicy(size_policy)
self.addWidget(separator)
pass