openlp/openlp/core/utils/actions.py

199 lines
6.6 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 #
# --------------------------------------------------------------------------- #
2010-12-26 11:04:47 +00:00
# Copyright (c) 2008-2011 Raoul Snyman #
# Portions copyright (c) 2008-2011 Tim Bentley, Jonathan Corwin, Michael #
2011-03-24 19:04:02 +00:00
# Gorven, Scott Guerrieri, Matthias Hub, Meinert Jordan, Armin Köhler, #
# Andreas Preikschat, Mattias Põldaru, Christian Richter, Philip Ridout, #
# 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 #
###############################################################################
"""
The :mod:`~openlp.core.utils.actions` module provides action list classes used
by the shortcuts system.
"""
2011-03-30 10:12:39 +00:00
class Category(object):
"""
2011-03-30 10:12:39 +00:00
The :class:`~openlp.core.utils.Category` class encapsulates a
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)
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
raise StopIteration
else:
self.index += 1
2011-03-30 10:27:27 +00:00
return self.actions[self.index - 1][1]
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)
def add(self, action, weight=0):
2011-03-30 10:27:27 +00:00
self.actions.append((weight, action))
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
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.
"""
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)
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
raise StopIteration
else:
self.index += 1
return self.categories[self.index - 1]
def next(self):
"""
Python 2 "next" method for iterator.
"""
return self.__next__()
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):
2010-10-28 05:21:45 +00:00
weight = 0
if len(self.categories) > 0:
weight = self.categories[-1].weight + 1
if actions:
self.add(name, weight, actions)
else:
self.add(name, weight)
2010-11-03 17:19:44 +00:00
def add(self, name, weight=0, actions=None):
2011-03-30 10:12:39 +00:00
category = Category(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)
self.categories.sort(key=lambda cat: cat.weight)
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.
"""
def __init__(self):
self.categories = CategoryList()
2011-03-30 10:27:27 +00:00
def add_action(self, action, category, weight=None):
2010-10-28 05:21:45 +00:00
if category not in self.categories:
self.categories.append(category)
if weight is None:
self.categories[category].actions.append(action)
else:
self.categories[category].actions.add(action, weight)
2011-03-29 13:56:49 +00:00
2011-03-30 10:27:27 +00:00
def remove_action(self, action, category):
if category not in self.categories:
return
self.categories[category].actions.remove(action)
2011-03-29 13:56:49 +00:00
actionList = ActionList()