scidavis  2.3.0
About: SciDAVis is a free application for Scientific Data Analysis and Visualization (a fork off of QtiPlot).
  Fossies Dox: scidavis-2.3.0.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

ActionManager Class Reference

Manages QActions and their shortcuts. More...

#include <ActionManager.h>

Inheritance diagram for ActionManager:
[legend]
Collaboration diagram for ActionManager:
[legend]

Public Slots

void removeAction (QAction *action)
 
void removeAction (QObject *action)
 

Public Member Functions

 ActionManager ()
 
 ~ActionManager ()
 
void addAction (QAction *action, const QString &internal_name)
 
QList< QKeySequence > shortcuts (const QString &internal_name) const
 
void setShortcuts (const QString &internal_name, const QList< QKeySequence > &sequences)
 
QString actionText (const QString &internal_name) const
 
QList< QString > internalNames () const
 
 CLASS_ACCESSOR (QString, d_title, title, Title)
 

Private Attributes

QMap< QString, QList< QAction * > * > d_action_registry
 
QMap< QString, QList< QKeySequence > > d_action_shortcuts
 
QMap< QString, QString > d_action_texts
 
QString d_title
 

Detailed Description

Manages QActions and their shortcuts.

An ActionManager object is meant to manage all actions for one widget class. All actions are addressed by their internal name (a string that must be unique within the widget class). The manager stores multiple QActions per internal name, i.e., one action per instance of the widget class. The text of the action is the localized string of the action while the internal name is never translated as it is meant to be used to identify the action in the configuration of the application. The localized text is taken from the last added action or removed action for each internal name. The managed widgets can change their language as often as needed but should have the same language all the time. Otherwise, the language of actionText() will depend on the order of the action addition. Actions that are deleted (e.g., when their widget is deleted) are automatically removed from the manager. The keyboard shortcuts assigned to an internal name are preserved even when no action of the type exists. If setShortcuts() has been called before addAction() the action's shortcuts are replaced in addAction(). If setShortcuts() has not been called before the first call to addAction() (for a specific name that is) the shortcuts of the first added action will be taken for all other added actions.

The typical usage of ActionManager is:

  • read configured shortcuts from the configuration files of the application and call ActionManager::setShortcuts() for each of them
  • create an instance of the widget class
    1. create actions for the widget
    2. call ActionManager::addAction() for each action
    3. if the configuration files contained a shortcut for the action, it will be set for the added action, otherwise the default shortcut set in the widget initialization code will be preserved
  • create more instances of the widget class and register their actions in the same way
  • call ActionManager::setShortcuts() to change the shortcut for all actions addressed by the same internal name in all existing widget instances

There is one more thing to consider: As long as addShortcut() is called and addAction() is not, actionText() will return the internal name instead of the localized name. It might therefore be a good idea to create an instance of the corresponding widget at application startup, create all actions for it, and immediately delete it again.

Definition at line 83 of file ActionManager.h.

Constructor & Destructor Documentation

◆ ActionManager()

ActionManager::ActionManager ( )

File : ActionManager.cpp Project : SciDAVis

Description : Manages QActions and their shortcuts

Copyright : (C) 2008-2009 Tilman Benkert (thzs*gmx.net) (replace * with @ in the email addresses)

  • 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 2 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, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301 USA *

Definition at line 33 of file ActionManager.cpp.

34 {
35 }

◆ ~ActionManager()

ActionManager::~ActionManager ( )

Definition at line 37 of file ActionManager.cpp.

38 {
39  QMutableMapIterator<QString, QList<QAction *> * > it(d_action_registry);
40  while (it.hasNext())
41  delete it.next().value();
42 }

References d_action_registry.

Member Function Documentation

◆ actionText()

QString ActionManager::actionText ( const QString &  internal_name) const

Definition at line 95 of file ActionManager.cpp.

96 {
97  if(!d_action_registry.contains(internal_name))
98  return QString();
99 
100  QList<QAction *> * list = d_action_registry.value(internal_name);
101  if (list->isEmpty())
102  {
103  if (d_action_texts.contains(internal_name))
104  return d_action_texts.value(internal_name);
105  else
106  return internal_name;
107  }
108  return list->last()->text();
109 }

References d_action_registry, and d_action_texts.

◆ addAction()

void ActionManager::addAction ( QAction *  action,
const QString &  internal_name 
)

Definition at line 44 of file ActionManager.cpp.

45 {
46  if (!action)
47  return;
48  if (!d_action_registry.contains(internal_name)) // new internal name
49  {
50  QList<QAction *> * list = new QList<QAction *>();
51  list->append(action);
52  d_action_registry.insert(internal_name, list);
53  d_action_shortcuts.insert(internal_name, action->shortcuts());
54  connect(action, SIGNAL(destroyed(QObject *)), this, SLOT(removeAction(QObject *)));
55  }
56  else
57  {
58  QList<QAction *> * list = d_action_registry.value(internal_name);
59  if (!list->contains(action))
60  {
61  list->append(action);
62  connect(action, SIGNAL(destroyed(QObject *)), this, SLOT(removeAction(QObject *)));
63  }
64  QList<QKeySequence> sequences = d_action_shortcuts.value(internal_name);
65  action->setShortcuts(sequences);
66  }
67  d_action_texts.insert(internal_name, action->text());
68 }

References d_action_registry, d_action_shortcuts, d_action_texts, and removeAction().

Referenced by future::Matrix::createActions(), and future::Table::createActions().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ CLASS_ACCESSOR()

ActionManager::CLASS_ACCESSOR ( QString  ,
d_title  ,
title  ,
Title   
)

◆ internalNames()

QList< QString > ActionManager::internalNames ( ) const

Definition at line 111 of file ActionManager.cpp.

112 {
113  return d_action_registry.keys();
114 }

References d_action_registry.

◆ removeAction [1/2]

void ActionManager::removeAction ( QAction *  action)
slot

Definition at line 121 of file ActionManager.cpp.

122 {
123  if(!action)
124  return;
125 
126  disconnect(action, SIGNAL(destroyed(QObject *)), this, SLOT(removeAction(QObject *)));
127 
128  QMutableMapIterator<QString, QList<QAction *> * > it(d_action_registry);
129  while (it.hasNext())
130  {
131  it.next();
132  if (it.value()->contains(action))
133  {
134  d_action_texts.insert(it.key(), action->text());
135  it.value()->removeAll(action);
136  }
137  }
138 }

References d_action_registry, and d_action_texts.

Referenced by addAction(), and removeAction().

Here is the caller graph for this function:

◆ removeAction [2/2]

void ActionManager::removeAction ( QObject *  action)
slot

Definition at line 116 of file ActionManager.cpp.

117 {
118  removeAction(static_cast<QAction *>(action));
119 }

References removeAction().

Here is the call graph for this function:

◆ setShortcuts()

void ActionManager::setShortcuts ( const QString &  internal_name,
const QList< QKeySequence > &  sequences 
)

Definition at line 78 of file ActionManager.cpp.

79 {
80  if(!d_action_registry.contains(internal_name)) // new internal name
81  {
82  QList<QAction *> * list = new QList<QAction *>();
83  d_action_registry.insert(internal_name, list);
84  d_action_shortcuts.insert(internal_name, sequences);
85  }
86  else
87  {
88  QList<QAction *> * list = d_action_registry.value(internal_name);
89  foreach(QAction * action, *list)
90  action->setShortcuts(sequences);
91  d_action_shortcuts.insert(internal_name, sequences);
92  }
93 }

References d_action_registry, and d_action_shortcuts.

◆ shortcuts()

QList< QKeySequence > ActionManager::shortcuts ( const QString &  internal_name) const

Definition at line 70 of file ActionManager.cpp.

71 {
72  if(!d_action_shortcuts.contains(internal_name))
73  return QList<QKeySequence>();
74 
75  return d_action_shortcuts.value(internal_name);
76 }

References d_action_shortcuts.

Member Data Documentation

◆ d_action_registry

QMap< QString, QList<QAction *> * > ActionManager::d_action_registry
private

◆ d_action_shortcuts

QMap< QString, QList<QKeySequence> > ActionManager::d_action_shortcuts
private

Definition at line 104 of file ActionManager.h.

Referenced by addAction(), setShortcuts(), and shortcuts().

◆ d_action_texts

QMap< QString, QString > ActionManager::d_action_texts
private

Definition at line 105 of file ActionManager.h.

Referenced by actionText(), addAction(), and removeAction().

◆ d_title

QString ActionManager::d_title
private

Definition at line 106 of file ActionManager.h.


The documentation for this class was generated from the following files:
ActionManager::removeAction
void removeAction(QAction *action)
Definition: ActionManager.cpp:121
ActionManager::d_action_texts
QMap< QString, QString > d_action_texts
Definition: ActionManager.h:105
ActionManager::d_action_shortcuts
QMap< QString, QList< QKeySequence > > d_action_shortcuts
Definition: ActionManager.h:104
ActionManager::d_action_registry
QMap< QString, QList< QAction * > * > d_action_registry
Definition: ActionManager.h:103