openlp/openlp/core/common/actions.py

391 lines
15 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2012-12-29 09:35:24 +00:00
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
###############################################################################
# OpenLP - Open Source Lyrics Projection #
# --------------------------------------------------------------------------- #
2016-12-31 11:01:36 +00:00
# Copyright (c) 2008-2017 OpenLP Developers #
# --------------------------------------------------------------------------- #
# 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:`~openlp.core.utils.actions` module provides action list classes used
by the shortcuts system.
"""
import logging
2015-11-07 00:49:40 +00:00
from PyQt5 import QtCore, QtGui, QtWidgets
2011-03-30 17:50:36 +00:00
2013-10-13 20:36:42 +00:00
from openlp.core.common import Settings
2012-05-17 15:13:09 +00:00
2011-03-30 17:50:36 +00:00
log = logging.getLogger(__name__)
class ActionCategory(object):
"""
The :class:`~openlp.core.utils.ActionCategory` class encapsulates a category for the
:class:`~openlp.core.utils.CategoryList` class.
"""
def __init__(self, name, weight=0):
2013-02-02 20:18:34 +00:00
"""
Constructor
"""
self.name = name
self.weight = weight
self.actions = CategoryActionList()
class CategoryActionList(object):
"""
The :class:`~openlp.core.utils.CategoryActionList` class provides a sorted list of actions within a category.
"""
def __init__(self):
2013-02-02 20:18:34 +00:00
"""
Constructor
"""
self.index = 0
self.actions = []
2014-04-26 09:01:12 +00:00
def __contains__(self, key):
2013-02-02 20:18:34 +00:00
"""
Implement the __contains__() method to make this class a dictionary type
"""
2014-04-22 10:29:15 +00:00
for weight, action in self.actions:
2014-04-26 09:01:12 +00:00
if action == key:
2014-04-22 10:29:15 +00:00
return True
return False
2010-10-28 05:21:45 +00:00
def __len__(self):
2013-02-02 20:18:34 +00:00
"""
Implement the __len__() method to make this class a dictionary type
"""
2010-10-28 05:21:45 +00:00
return len(self.actions)
def __iter__(self):
2013-02-02 20:18:34 +00:00
"""
Implement the __getitem__() method to make this class iterable
"""
return self
def __next__(self):
"""
Python 3 "next" method.
"""
if self.index >= len(self.actions):
2011-03-30 10:12:39 +00:00
self.index = 0
raise StopIteration
else:
self.index += 1
2011-03-30 10:27:27 +00:00
return self.actions[self.index - 1][1]
2014-04-22 15:06:21 +00:00
def append(self, action):
2013-02-02 20:18:34 +00:00
"""
Append an action
"""
2010-10-28 05:21:45 +00:00
weight = 0
if self.actions:
2010-10-28 05:21:45 +00:00
weight = self.actions[-1][0] + 1
2014-04-22 15:06:21 +00:00
self.add(action, weight)
2010-10-28 05:21:45 +00:00
def add(self, action, weight=0):
2013-02-02 20:18:34 +00:00
"""
Add an action.
"""
2011-03-30 10:27:27 +00:00
self.actions.append((weight, action))
self.actions.sort(key=lambda act: act[0])
2014-04-26 09:35:50 +00:00
def remove(self, action):
2013-02-02 20:18:34 +00:00
"""
Remove an action
"""
2014-04-26 09:35:50 +00:00
for item in self.actions:
if item[1] == action:
self.actions.remove(item)
2011-03-30 10:27:27 +00:00
return
raise ValueError('Action "{action}" does not exist.'.format(action=action))
2011-03-30 10:27:27 +00:00
class CategoryList(object):
"""
The :class:`~openlp.core.utils.CategoryList` class encapsulates a category list for the
:class:`~openlp.core.utils.ActionList` class and provides an iterator interface for walking through the list of
actions in this category.
"""
def __init__(self):
2013-02-02 20:18:34 +00:00
"""
Constructor
"""
self.index = 0
2010-10-28 05:21:45 +00:00
self.categories = []
def __getitem__(self, key):
2013-02-02 20:18:34 +00:00
"""
Implement the __getitem__() method to make this class like a dictionary
"""
2010-10-28 05:21:45 +00:00
for category in self.categories:
if category.name == key:
return category
2016-07-01 21:17:20 +00:00
raise KeyError('Category "{key}" does not exist.'.format(key=key))
2010-10-28 05:21:45 +00:00
def __len__(self):
2013-02-02 20:18:34 +00:00
"""
Implement the __len__() method to make this class like a dictionary
"""
2010-10-28 05:21:45 +00:00
return len(self.categories)
def __iter__(self):
2013-02-02 20:18:34 +00:00
"""
Implement the __iter__() method to make this class like a dictionary
"""
return self
def __next__(self):
"""
Python 3 "next" method for iterator.
"""
if self.index >= len(self.categories):
2011-03-30 10:12:39 +00:00
self.index = 0
raise StopIteration
else:
self.index += 1
return self.categories[self.index - 1]
2014-04-26 17:36:47 +00:00
def __contains__(self, key):
2013-02-02 20:18:34 +00:00
"""
2014-04-26 17:36:47 +00:00
Implement the __contains__() method to make this class like a dictionary
2013-02-02 20:18:34 +00:00
"""
2010-10-28 05:21:45 +00:00
for category in self.categories:
if category.name == key:
return True
return False
2010-11-03 17:19:44 +00:00
def append(self, name, actions=None):
2013-02-02 20:18:34 +00:00
"""
Append a category
"""
2011-04-12 21:01:27 +00:00
weight = 0
if self.categories:
2011-04-12 21:01:27 +00:00
weight = self.categories[-1].weight + 1
2014-04-26 17:36:47 +00:00
self.add(name, weight, actions)
2010-10-28 05:21:45 +00:00
2010-11-03 17:19:44 +00:00
def add(self, name, weight=0, actions=None):
2013-02-02 20:18:34 +00:00
"""
Add a category
"""
category = ActionCategory(name, weight)
2010-10-28 05:21:45 +00:00
if actions:
for action in actions:
if isinstance(action, tuple):
category.actions.add(action[0], action[1])
else:
category.actions.append(action)
self.categories.append(category)
2011-04-12 21:01:27 +00:00
self.categories.sort(key=lambda cat: cat.weight)
def remove(self, name):
2013-02-02 20:18:34 +00:00
"""
Remove a category
"""
for category in self.categories:
if category.name == name:
self.categories.remove(category)
2014-04-26 17:40:26 +00:00
return
raise ValueError('Category "{name}" does not exist.'.format(name=name))
class ActionList(object):
"""
The :class:`~openlp.core.utils.ActionList` class contains a list of menu actions and categories associated with
those actions. Each category also has a weight by which it is sorted when iterating through the list of actions or
categories.
"""
2011-04-09 16:11:02 +00:00
instance = None
shortcut_map = {}
2011-04-09 16:11:02 +00:00
def __init__(self):
2013-02-02 20:18:34 +00:00
"""
Constructor
"""
2011-04-09 16:11:02 +00:00
self.categories = CategoryList()
@staticmethod
2011-04-09 16:11:02 +00:00
def get_instance():
2013-02-02 20:18:34 +00:00
"""
Get the instance of this class.
"""
2011-04-09 16:11:02 +00:00
if ActionList.instance is None:
ActionList.instance = ActionList()
return ActionList.instance
def add_action(self, action, category=None, weight=None):
2011-04-03 14:20:55 +00:00
"""
Add an action to the list of actions.
**Note**: The action's objectName must be set when you want to add it!
2014-03-17 19:05:55 +00:00
:param action: The action to add (QAction). **Note**, the action must not have an empty ``objectName``.
:param category: The category this action belongs to. The category has to be a python string. . **Note**,
2015-09-08 19:13:59 +00:00
if the category is ``None``, the category and its actions are being hidden in the shortcut dialog. However,
if they are added, it is possible to avoid assigning shortcuts twice, which is important.
2014-03-17 19:05:55 +00:00
:param weight: The weight specifies how important a category is. However, this only has an impact on the order
2015-09-08 19:13:59 +00:00
the categories are displayed.
2011-04-03 14:20:55 +00:00
"""
2011-04-09 16:11:02 +00:00
if category not in self.categories:
self.categories.append(category)
settings = Settings()
2013-08-31 18:17:38 +00:00
settings.beginGroup('shortcuts')
# Get the default shortcut from the config.
2014-04-22 10:29:15 +00:00
action.default_shortcuts = settings.get_default_value(action.objectName())
if weight is None:
2011-04-09 16:11:02 +00:00
self.categories[category].actions.append(action)
else:
2011-04-09 16:11:02 +00:00
self.categories[category].actions.add(action, weight)
2011-03-30 17:50:36 +00:00
# Load the shortcut from the config.
shortcuts = settings.value(action.objectName())
settings.endGroup()
if not shortcuts:
action.setShortcuts([])
return
# We have to do this to ensure that the loaded shortcut list e. g. STRG+O (German) is converted to CTRL+O,
# which is only done when we convert the strings in this way (QKeySequencet -> uncode).
2013-08-31 18:17:38 +00:00
shortcuts = list(map(QtGui.QKeySequence.toString, list(map(QtGui.QKeySequence, shortcuts))))
# Check the alternate shortcut first, to avoid problems when the alternate shortcut becomes the primary shortcut
# after removing the (initial) primary shortcut due to conflicts.
if len(shortcuts) == 2:
existing_actions = ActionList.shortcut_map.get(shortcuts[1], [])
# Check for conflicts with other actions considering the shortcut context.
2011-12-10 14:03:41 +00:00
if self._is_shortcut_available(existing_actions, action):
actions = ActionList.shortcut_map.get(shortcuts[1], [])
actions.append(action)
ActionList.shortcut_map[shortcuts[1]] = actions
else:
log.warning('Shortcut "{shortcut}" is removed from "{action}" because another '
'action already uses this shortcut.'.format(shortcut=shortcuts[1],
action=action.objectName()))
shortcuts.remove(shortcuts[1])
# Check the primary shortcut.
existing_actions = ActionList.shortcut_map.get(shortcuts[0], [])
# Check for conflicts with other actions considering the shortcut context.
2011-12-10 14:03:41 +00:00
if self._is_shortcut_available(existing_actions, action):
actions = ActionList.shortcut_map.get(shortcuts[0], [])
actions.append(action)
ActionList.shortcut_map[shortcuts[0]] = actions
else:
2016-05-15 17:32:04 +00:00
log.warning('Shortcut "{shortcut}" is removed from "{action}" '
'because another action already uses this shortcut.'.format(shortcut=shortcuts[0],
action=action.objectName()))
2013-03-07 11:20:57 +00:00
shortcuts.remove(shortcuts[0])
action.setShortcuts([QtGui.QKeySequence(shortcut) for shortcut in shortcuts])
2011-03-29 13:56:49 +00:00
2011-04-09 16:11:02 +00:00
def remove_action(self, action, category=None):
2011-04-09 14:50:44 +00:00
"""
This removes an action from its category. Empty categories are automatically removed.
2011-04-09 14:50:44 +00:00
2014-03-17 19:05:55 +00:00
:param action: The ``QAction`` object to be removed.
:param category: The name (unicode string) of the category, which contains the action. Defaults to None.
2011-04-09 14:50:44 +00:00
"""
2011-04-09 16:11:02 +00:00
if category not in self.categories:
2011-03-30 10:27:27 +00:00
return
2011-04-09 16:11:02 +00:00
self.categories[category].actions.remove(action)
# Remove empty categories.
if not self.categories[category].actions:
2011-04-09 16:11:02 +00:00
self.categories.remove(category)
2013-08-31 18:17:38 +00:00
shortcuts = list(map(QtGui.QKeySequence.toString, action.shortcuts()))
for shortcut in shortcuts:
# Remove action from the list of actions which are using this shortcut.
ActionList.shortcut_map[shortcut].remove(action)
# Remove empty entries.
if not ActionList.shortcut_map[shortcut]:
del ActionList.shortcut_map[shortcut]
2011-04-09 14:50:44 +00:00
2011-04-09 16:11:02 +00:00
def add_category(self, name, weight):
2011-04-09 14:50:44 +00:00
"""
Add an empty category to the list of categories. This is only convenient for categories with a given weight.
2011-04-09 14:50:44 +00:00
2014-03-17 19:05:55 +00:00
:param name: The category's name.
:param weight: The category's weight (int).
2011-04-09 14:50:44 +00:00
"""
2011-04-09 16:11:02 +00:00
if name in self.categories:
2011-04-09 14:50:44 +00:00
# Only change the weight and resort the categories again.
2011-04-09 16:11:02 +00:00
for category in self.categories:
2011-04-09 14:50:44 +00:00
if category.name == name:
category.weight = weight
2011-04-12 21:01:27 +00:00
self.categories.categories.sort(key=lambda cat: cat.weight)
2011-04-09 14:50:44 +00:00
return
2011-04-09 16:11:02 +00:00
self.categories.add(name, weight)
2011-12-10 14:03:41 +00:00
def update_shortcut_map(self, action, old_shortcuts):
"""
Remove the action for the given ``old_shortcuts`` from the ``shortcut_map`` to ensure its up-to-dateness.
**Note**: The new action's shortcuts **must** be assigned to the given ``action`` **before** calling this
method.
2011-12-10 14:03:41 +00:00
2014-03-17 19:05:55 +00:00
:param action: The action whose shortcuts are supposed to be updated in the ``shortcut_map``.
:param old_shortcuts: A list of unicode key sequences.
2011-12-10 14:03:41 +00:00
"""
for old_shortcut in old_shortcuts:
# Remove action from the list of actions which are using this shortcut.
ActionList.shortcut_map[old_shortcut].remove(action)
# Remove empty entries.
if not ActionList.shortcut_map[old_shortcut]:
del ActionList.shortcut_map[old_shortcut]
2013-08-31 18:17:38 +00:00
new_shortcuts = list(map(QtGui.QKeySequence.toString, action.shortcuts()))
# Add the new shortcuts to the map.
2011-12-10 14:03:41 +00:00
for new_shortcut in new_shortcuts:
existing_actions = ActionList.shortcut_map.get(new_shortcut, [])
existing_actions.append(action)
ActionList.shortcut_map[new_shortcut] = existing_actions
def _is_shortcut_available(self, existing_actions, action):
"""
Checks if the given ``action`` may use its assigned shortcut(s) or not. Returns ``True`` or ``False.
2014-03-17 19:05:55 +00:00
:param existing_actions: A list of actions which already use a particular shortcut.
:param action: The action which wants to use a particular shortcut.
"""
2013-02-19 18:36:43 +00:00
global_context = action.shortcutContext() in [QtCore.Qt.WindowShortcut, QtCore.Qt.ApplicationShortcut]
affected_actions = []
2013-02-19 18:36:43 +00:00
if global_context:
2015-11-07 00:49:40 +00:00
affected_actions = [a for a in self.get_all_child_objects(action.parent()) if isinstance(a,
QtWidgets.QAction)]
for existing_action in existing_actions:
if action is existing_action:
continue
2013-02-27 12:41:04 +00:00
if existing_action in affected_actions:
2011-12-31 17:36:58 +00:00
return False
2012-12-29 09:35:24 +00:00
if existing_action.shortcutContext() in [QtCore.Qt.WindowShortcut, QtCore.Qt.ApplicationShortcut]:
return False
elif action in self.get_all_child_objects(existing_action.parent()):
return False
return True
2012-04-04 07:26:51 +00:00
def get_all_child_objects(self, qobject):
"""
Goes recursively through the children of ``qobject`` and returns a list of all child objects.
"""
children = qobject.children()
# Append the children's children.
2013-08-31 18:17:38 +00:00
children.extend(list(map(self.get_all_child_objects, children)))
return children
2011-04-09 16:11:02 +00:00
class CategoryOrder(object):
"""
An enumeration class for category weights.
"""
standard_menu = -20
standard_toolbar = -10