~ubuntu-branches/ubuntu/maverick/scidavis/maverick

« back to all changes in this revision

Viewing changes to scidavis/src/future/lib/ActionManager.h

  • Committer: Bazaar Package Importer
  • Author(s): Ruben Molina
  • Date: 2009-09-06 11:34:04 UTC
  • Revision ID: james.westby@ubuntu.com-20090906113404-4awaey82l3686w4q
Tags: upstream-0.2.3
ImportĀ upstreamĀ versionĀ 0.2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
    File                 : ActionManager.h
 
3
    Project              : SciDAVis
 
4
    Description          : Manages QActions and their shortcuts
 
5
    --------------------------------------------------------------------
 
6
    Copyright            : (C) 2008-2009 Tilman Benkert (thzs*gmx.net)
 
7
                           (replace * with @ in the email addresses) 
 
8
                           
 
9
 ***************************************************************************/
 
10
 
 
11
/***************************************************************************
 
12
 *                                                                         *
 
13
 *  This program is free software; you can redistribute it and/or modify   *
 
14
 *  it under the terms of the GNU General Public License as published by   *
 
15
 *  the Free Software Foundation; either version 2 of the License, or      *
 
16
 *  (at your option) any later version.                                    *
 
17
 *                                                                         *
 
18
 *  This program is distributed in the hope that it will be useful,        *
 
19
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
 
20
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
 
21
 *  GNU General Public License for more details.                           *
 
22
 *                                                                         *
 
23
 *   You should have received a copy of the GNU General Public License     *
 
24
 *   along with this program; if not, write to the Free Software           *
 
25
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor,                    *
 
26
 *   Boston, MA  02110-1301  USA                                           *
 
27
 *                                                                         *
 
28
 ***************************************************************************/
 
29
 
 
30
#ifndef ACTIONMANAGER_H
 
31
#define ACTIONMANAGER_H
 
32
 
 
33
#include <QObject>
 
34
#include <QAction>
 
35
#include <QList>
 
36
#include <QMap>
 
37
#include "lib/macros.h"
 
38
class QString;
 
39
class QKeySequence;
 
40
 
 
41
//! Manages QActions and their shortcuts
 
42
/**
 
43
 * An ActionManager object is meant to manage all actions for one
 
44
 * widget class. All actions are addressed by their internal name 
 
45
 * (a string that must be unique within the widget class). The manager
 
46
 * stores multiple QActions per internal name, i.e., one action
 
47
 * per instance of the widget class. The text of the action is the
 
48
 * localized string of the action while the internal name is never
 
49
 * translated as it is meant to be used to identify the action in the
 
50
 * configuration of the application. The localized text is taken
 
51
 * from the last added action or removed action for each internal name.
 
52
 * The managed widgets can change their language as often as needed 
 
53
 * but should have the same language all the time. Otherwise, the 
 
54
 * language of actionText() will depend on the order of the action addition. 
 
55
 * Actions that are deleted (e.g., when their widget is deleted) are automatically
 
56
 * removed from the manager. The keyboard shortcuts assigned to an internal name
 
57
 * are preserved even when no action of the type exists. 
 
58
 * If setShortcuts() has been called before addAction() the action's shortcuts 
 
59
 * are replaced in addAction(). If setShortcuts() has not been called 
 
60
 * before the first call to addAction() (for a specific name that is) the
 
61
 * shortcuts of the first added action will be taken for all other added actions.
 
62
 *
 
63
 * The typical usage of ActionManager is:
 
64
 * - read configured shortcuts from the configuration files of the application and
 
65
 *   call ActionManager::setShortcuts() for each of them
 
66
 * - create an instance of the widget class
 
67
 *   -# create actions for the widget
 
68
 *   -# call ActionManager::addAction() for each action
 
69
 *   -# if the configuration files contained a shortcut for the action, it will
 
70
 *      be set for the added action, otherwise the default shortcut set in
 
71
 *      the widget initialization code will be preserved
 
72
 * - create more instances of the widget class and register their actions
 
73
 *   in the same way
 
74
 * - call ActionManager::setShortcuts() to change the shortcut for all actions
 
75
 *   addressed by the same internal name in all existing widget instances
 
76
 *
 
77
 * There is one more thing to consider:
 
78
 * As long as addShortcut() is called and addAction() is not, actionText() will
 
79
 * return the internal name instead of the localized name. It might therefore
 
80
 * be a good idea to create an instance of the corresponding widget at application
 
81
 * startup, create all actions for it, and immediately delete it again. 
 
82
 */
 
83
class ActionManager : public QObject
 
84
{
 
85
        Q_OBJECT
 
86
 
 
87
        public:
 
88
                ActionManager();
 
89
                ~ActionManager();
 
90
                
 
91
                void addAction(QAction * action, const QString& internal_name);
 
92
                QList<QKeySequence> shortcuts(const QString& internal_name) const;
 
93
                void setShortcuts(const QString& internal_name, const QList<QKeySequence>& sequences);
 
94
                QString actionText(const QString& internal_name) const;
 
95
                QList<QString> internalNames() const;
 
96
                CLASS_ACCESSOR(QString, d_title, title, Title);
 
97
 
 
98
        public slots:
 
99
                void removeAction(QAction * action);
 
100
                void removeAction(QObject * action);
 
101
        
 
102
        private:
 
103
                QMap< QString, QList<QAction *> * > d_action_registry;
 
104
                QMap< QString, QList<QKeySequence> > d_action_shortcuts;
 
105
                QMap< QString, QString > d_action_texts;
 
106
                QString d_title;
 
107
};
 
108
 
 
109
#endif // ACTIONMANAGER_H