2010-10-27 15:35:15 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# OpenLP - Open Source Lyrics Projection #
|
|
|
|
# --------------------------------------------------------------------------- #
|
2010-12-26 11:04:47 +00:00
|
|
|
# Copyright (c) 2008-2011 Raoul Snyman #
|
2011-05-26 16:25:54 +00:00
|
|
|
# Portions copyright (c) 2008-2011 Tim Bentley, Gerald Britton, Jonathan #
|
|
|
|
# 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 #
|
2010-10-27 15:35:15 +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 #
|
|
|
|
###############################################################################
|
|
|
|
"""
|
|
|
|
The :mod:`~openlp.core.utils.actions` module provides action list classes used
|
|
|
|
by the shortcuts system.
|
|
|
|
"""
|
2011-03-30 17:50:36 +00:00
|
|
|
from PyQt4 import QtCore, QtGui
|
|
|
|
|
2011-03-30 16:34:40 +00:00
|
|
|
class ActionCategory(object):
|
2010-10-27 15:35:15 +00:00
|
|
|
"""
|
2011-03-30 16:34:40 +00:00
|
|
|
The :class:`~openlp.core.utils.ActionCategory` class encapsulates a
|
2010-10-27 15:35:15 +00:00
|
|
|
category for the :class:`~openlp.core.utils.CategoryList` class.
|
|
|
|
"""
|
|
|
|
def __init__(self, name, weight=0):
|
|
|
|
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):
|
|
|
|
self.index = 0
|
|
|
|
self.actions = []
|
|
|
|
|
2010-10-28 05:21:45 +00:00
|
|
|
def __getitem__(self, key):
|
|
|
|
for weight, action in self.actions:
|
|
|
|
if action.text() == key:
|
|
|
|
return action
|
|
|
|
raise KeyError(u'Action "%s" does not exist.' % key)
|
|
|
|
|
|
|
|
def __contains__(self, item):
|
|
|
|
return self.has_key(item)
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return len(self.actions)
|
|
|
|
|
2010-10-27 15:35:15 +00:00
|
|
|
def __iter__(self):
|
|
|
|
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
|
2010-10-27 15:35:15 +00:00
|
|
|
raise StopIteration
|
|
|
|
else:
|
|
|
|
self.index += 1
|
2011-03-30 10:27:27 +00:00
|
|
|
return self.actions[self.index - 1][1]
|
2010-10-27 15:35:15 +00:00
|
|
|
|
|
|
|
def next(self):
|
|
|
|
"""
|
|
|
|
Python 2 "next" method.
|
|
|
|
"""
|
|
|
|
return self.__next__()
|
|
|
|
|
2010-11-03 17:19:44 +00:00
|
|
|
def has_key(self, key):
|
2010-10-28 05:21:45 +00:00
|
|
|
for weight, action in self.actions:
|
|
|
|
if action.text() == key:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def append(self, name):
|
|
|
|
weight = 0
|
|
|
|
if len(self.actions) > 0:
|
|
|
|
weight = self.actions[-1][0] + 1
|
|
|
|
self.add(name, weight)
|
|
|
|
|
2010-10-27 15:35:15 +00:00
|
|
|
def add(self, action, weight=0):
|
2011-03-30 10:27:27 +00:00
|
|
|
self.actions.append((weight, action))
|
2010-10-27 15:35:15 +00:00
|
|
|
self.actions.sort(key=lambda act: act[0])
|
|
|
|
|
2011-03-30 10:27:27 +00:00
|
|
|
def remove(self, remove_action):
|
|
|
|
for action in self.actions:
|
|
|
|
if action[1] == remove_action:
|
|
|
|
self.actions.remove(action)
|
|
|
|
return
|
|
|
|
|
2010-10-27 15:35:15 +00:00
|
|
|
|
|
|
|
class CategoryList(object):
|
|
|
|
"""
|
2010-10-28 05:21:45 +00:00
|
|
|
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.
|
2010-10-27 15:35:15 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.index = 0
|
2010-10-28 05:21:45 +00:00
|
|
|
self.categories = []
|
|
|
|
|
|
|
|
def __getitem__(self, key):
|
|
|
|
for category in self.categories:
|
|
|
|
if category.name == key:
|
|
|
|
return category
|
|
|
|
raise KeyError(u'Category "%s" does not exist.' % key)
|
|
|
|
|
|
|
|
def __contains__(self, item):
|
|
|
|
return self.has_key(item)
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return len(self.categories)
|
2010-10-27 15:35:15 +00:00
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
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
|
2010-10-27 15:35:15 +00:00
|
|
|
raise StopIteration
|
|
|
|
else:
|
|
|
|
self.index += 1
|
|
|
|
return self.categories[self.index - 1]
|
|
|
|
|
|
|
|
def next(self):
|
|
|
|
"""
|
|
|
|
Python 2 "next" method for iterator.
|
|
|
|
"""
|
|
|
|
return self.__next__()
|
|
|
|
|
2010-10-28 06:36:18 +00:00
|
|
|
def has_key(self, key):
|
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):
|
2011-04-12 21:01:27 +00:00
|
|
|
weight = 0
|
|
|
|
if len(self.categories) > 0:
|
|
|
|
weight = self.categories[-1].weight + 1
|
2010-10-28 05:21:45 +00:00
|
|
|
if actions:
|
2011-04-12 21:01:27 +00:00
|
|
|
self.add(name, weight, actions)
|
2010-10-28 05:21:45 +00:00
|
|
|
else:
|
2011-04-12 21:01:27 +00:00
|
|
|
self.add(name, weight)
|
2010-10-28 05:21:45 +00:00
|
|
|
|
2010-11-03 17:19:44 +00:00
|
|
|
def add(self, name, weight=0, actions=None):
|
2011-03-30 16:34:40 +00:00
|
|
|
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)
|
2010-10-27 15:35:15 +00:00
|
|
|
|
2011-04-01 14:28:25 +00:00
|
|
|
def remove(self, name):
|
|
|
|
for category in self.categories:
|
|
|
|
if category.name == name:
|
|
|
|
self.categories.remove(category)
|
|
|
|
|
2010-10-27 15:35:15 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.categories = CategoryList()
|
2010-10-27 15:35:15 +00:00
|
|
|
|
2011-03-31 08:43:19 +00:00
|
|
|
@staticmethod
|
2011-04-09 16:11:02 +00:00
|
|
|
def get_instance():
|
|
|
|
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.
|
|
|
|
|
|
|
|
``action``
|
2011-05-25 11:26:18 +00:00
|
|
|
The action to add (QAction). **Note**, the action must not have an
|
|
|
|
empty ``objectName``.
|
2011-04-03 14:20:55 +00:00
|
|
|
|
|
|
|
``category``
|
|
|
|
The category this action belongs to. The category can be a QString
|
|
|
|
or python unicode string. **Note**, 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.
|
|
|
|
|
|
|
|
``weight``
|
|
|
|
The weight specifies how important a category is. However, this only
|
|
|
|
has an impact on the order the categories are displayed.
|
|
|
|
"""
|
|
|
|
if category is not None:
|
|
|
|
category = unicode(category)
|
2011-04-09 16:11:02 +00:00
|
|
|
if category not in self.categories:
|
|
|
|
self.categories.append(category)
|
2011-03-30 16:34:40 +00:00
|
|
|
action.defaultShortcuts = action.shortcuts()
|
2010-10-28 06:36:18 +00:00
|
|
|
if weight is None:
|
2011-04-09 16:11:02 +00:00
|
|
|
self.categories[category].actions.append(action)
|
2010-10-28 06:36:18 +00:00
|
|
|
else:
|
2011-04-09 16:11:02 +00:00
|
|
|
self.categories[category].actions.add(action, weight)
|
2011-04-03 14:20:55 +00:00
|
|
|
if category is None:
|
|
|
|
# Stop here, as this action is not configurable.
|
|
|
|
return
|
2011-03-30 17:50:36 +00:00
|
|
|
# Load the shortcut from the config.
|
|
|
|
settings = QtCore.QSettings()
|
|
|
|
settings.beginGroup(u'shortcuts')
|
|
|
|
shortcuts = settings.value(action.objectName(),
|
|
|
|
QtCore.QVariant(action.shortcuts())).toStringList()
|
|
|
|
action.setShortcuts(
|
|
|
|
[QtGui.QKeySequence(shortcut) for shortcut in shortcuts])
|
|
|
|
settings.endGroup()
|
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.
|
|
|
|
|
|
|
|
``action``
|
|
|
|
The QAction object to be removed.
|
|
|
|
|
|
|
|
``category``
|
|
|
|
The name (unicode string) of the category, which contains the
|
|
|
|
action. Defaults to None.
|
|
|
|
"""
|
2011-04-03 14:20:55 +00:00
|
|
|
if category is not None:
|
|
|
|
category = unicode(category)
|
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)
|
2011-04-01 14:28:25 +00:00
|
|
|
# Remove empty categories.
|
2011-04-09 16:11:02 +00:00
|
|
|
if len(self.categories[category].actions) == 0:
|
|
|
|
self.categories.remove(category)
|
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 ony convenient
|
|
|
|
for categories with a given weight.
|
|
|
|
|
|
|
|
``name``
|
|
|
|
The category's name.
|
|
|
|
|
|
|
|
``weight``
|
|
|
|
The category's weight (int).
|
|
|
|
"""
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
class CategoryOrder(object):
|
|
|
|
"""
|
|
|
|
An enumeration class for category weights.
|
|
|
|
"""
|
2011-04-12 21:01:27 +00:00
|
|
|
standardMenu = -20
|
|
|
|
standardToolbar = -10
|