Merged in f5soh/librepilot/update_credits (pull request #529)
[librepilot.git] / ground / gcs / src / plugins / coreplugin / actionmanager / command.cpp
blob88f554b1510000d20e4c93a901a1fc076f8a2556
1 /**
2 ******************************************************************************
4 * @file command.cpp
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
6 * Parts by Nokia Corporation (qt-info@nokia.com) Copyright (C) 2009.
7 * @addtogroup GCSPlugins GCS Plugins
8 * @{
9 * @addtogroup CorePlugin Core Plugin
10 * @{
11 * @brief The Core GCS plugin
12 *****************************************************************************/
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * for more details.
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include <QtCore/QDebug>
30 #include <QAction>
31 #include <QShortcut>
33 #include "command_p.h"
35 /*!
36 \class Core::Command
37 \mainclass
39 \brief The class Command represents an action like a menu item, tool button, or shortcut.
40 You don't create Command objects directly, instead use \l{ActionManager::registerAction()}
41 to register an action and retrieve a Command. The Command object represents the user visible
42 action and its properties. If multiple actions are registered with the same ID (but
43 different contexts) the returned Command is the shared one between these actions.
45 A Command has two basic properties: A default shortcut and a default text. The default
46 shortcut is a key sequence that the user can use to trigger the active action that
47 the Command represents. The default text is e.g. used for representing the Command
48 in the keyboard shortcut preference pane. If the default text is empty, the text
49 of the visible action is used.
51 The user visible action is updated to represent the state of the active action (if any).
52 For performance reasons only the enabled and visible state are considered by default though.
53 You can tell a Command to also update the actions icon and text by setting the
54 corresponding \l{Command::CommandAttribute}{attribute}.
56 If there is no active action, the default behavior of the visible action is to be disabled.
57 You can change that behavior to make the visible action hide instead via the Command's
58 \l{Command::CommandAttribute}{attributes}.
61 /*!
62 \enum Command::CommandAttribute
63 Defines how the user visible action is updated when the active action changes.
64 The default is to update the enabled and visible state, and to disable the
65 user visible action when there is no active action.
66 \omitvalue CA_Mask
67 \value CA_UpdateText
68 Also update the actions text.
69 \value CA_UpdateIcon
70 Also update the actions icon.
71 \value CA_Hide
72 When there is no active action, hide the user "visible" action, instead of just
73 disabling it.
74 \value CA_NonConfigureable
75 Flag to indicate that the keyboard shortcut of this Command should not be
76 configurable by the user.
79 /*!
80 \fn void Command::setDefaultKeySequence(const QKeySequence &key)
81 Set the default keyboard shortcut that can be used to activate this command to \a key.
82 This is used if the user didn't customize the shortcut, or resets the shortcut
83 to the default one.
86 /*!
87 \fn void Command::defaultKeySequence() const
88 Returns the default keyboard shortcut that can be used to activate this command.
89 \sa setDefaultKeySequence()
92 /*!
93 \fn void Command::keySequenceChanged()
94 Sent when the keyboard shortcut assigned to this Command changes, e.g.
95 when the user sets it in the keyboard shortcut settings dialog.
98 /*!
99 \fn QKeySequence Command::keySequence() const
100 Returns the current keyboard shortcut assigned to this Command.
101 \sa defaultKeySequence()
105 \fn void Command::setKeySequence(const QKeySequence &key)
106 \internal
110 \fn void Command::setDefaultText(const QString &text)
111 Set the \a text that is used to represent the Command in the
112 keyboard shortcut settings dialog. If you don't set this,
113 the current text from the user visible action is taken (which
114 is ok in many cases).
118 \fn QString Command::defaultText() const
119 Returns the text that is used to present this Command to the user.
120 \sa setDefaultText()
124 \fn int Command::id() const
125 \internal
129 \fn QString Command::stringWithAppendedShortcut(const QString &string) const
130 Returns the \a string with an appended representation of the keyboard shortcut
131 that is currently assigned to this Command.
135 \fn QAction *Command::action() const
136 Returns the user visible action for this Command.
137 If the Command represents a shortcut, it returns null.
138 Use this action to put it on e.g. tool buttons. The action
139 automatically forwards trigger and toggle signals to the
140 action that is currently active for this Command.
141 It also shows the current keyboard shortcut in its
142 tool tip (in addition to the tool tip of the active action)
143 and gets disabled/hidden when there is
144 no active action for the current context.
148 \fn QShortcut *Command::shortcut() const
149 Returns the shortcut for this Command.
150 If the Command represents an action, it returns null.
154 \fn void Command::setAttribute(CommandAttribute attribute)
155 Add the \a attribute to the attributes of this Command.
156 \sa CommandAttribute
157 \sa removeAttribute()
158 \sa hasAttribute()
162 \fn void Command::removeAttribute(CommandAttribute attribute)
163 Remove the \a attribute from the attributes of this Command.
164 \sa CommandAttribute
165 \sa setAttribute()
169 \fn bool Command::hasAttribute(CommandAttribute attribute) const
170 Returns if the Command has the \a attribute set.
171 \sa CommandAttribute
172 \sa removeAttribute()
173 \sa setAttribute()
177 \fn bool Command::isActive() const
178 Returns if the Command has an active action/shortcut for the current
179 context.
183 \fn Command::~Command()
184 \internal
187 using namespace Core::Internal;
190 \class CommandPrivate
191 \internal
194 CommandPrivate::CommandPrivate(int id)
195 : m_attributes(0), m_id(id)
198 void CommandPrivate::setDefaultKeySequence(const QKeySequence &key)
200 m_defaultKey = key;
203 QKeySequence CommandPrivate::defaultKeySequence() const
205 return m_defaultKey;
208 void CommandPrivate::setDefaultText(const QString &text)
210 m_defaultText = text;
213 QString CommandPrivate::defaultText() const
215 return m_defaultText;
218 int CommandPrivate::id() const
220 return m_id;
223 QAction *CommandPrivate::action() const
225 return 0;
228 QShortcut *CommandPrivate::shortcut() const
230 return 0;
233 void CommandPrivate::setAttribute(CommandAttribute attr)
235 m_attributes |= attr;
238 void CommandPrivate::removeAttribute(CommandAttribute attr)
240 m_attributes &= ~attr;
243 bool CommandPrivate::hasAttribute(CommandAttribute attr) const
245 return m_attributes & attr;
248 QString CommandPrivate::stringWithAppendedShortcut(const QString &str) const
250 return QString("%1 <span style=\"color: gray; font-size: small\">%2</span>").arg(str).arg(
251 keySequence().toString(QKeySequence::NativeText));
254 // ---------- Shortcut ------------
257 \class Shortcut
258 \internal
261 Shortcut::Shortcut(int id)
262 : CommandPrivate(id), m_shortcut(0)
265 QString Shortcut::name() const
267 if (!m_shortcut) {
268 return QString();
271 return m_shortcut->whatsThis();
274 void Shortcut::setShortcut(QShortcut *shortcut)
276 m_shortcut = shortcut;
279 QShortcut *Shortcut::shortcut() const
281 return m_shortcut;
284 void Shortcut::setContext(const QList<int> &context)
286 m_context = context;
289 QList<int> Shortcut::context() const
291 return m_context;
294 void Shortcut::setDefaultKeySequence(const QKeySequence &key)
296 if (m_shortcut->key().isEmpty()) {
297 setKeySequence(key);
299 CommandPrivate::setDefaultKeySequence(key);
302 void Shortcut::setKeySequence(const QKeySequence &key)
304 m_shortcut->setKey(key);
305 emit keySequenceChanged();
308 QKeySequence Shortcut::keySequence() const
310 return m_shortcut->key();
313 void Shortcut::setDefaultText(const QString &text)
315 m_defaultText = text;
318 QString Shortcut::defaultText() const
320 return m_defaultText;
323 bool Shortcut::setCurrentContext(const QList<int> &context)
325 foreach(int ctxt, m_context) {
326 if (context.contains(ctxt)) {
327 m_shortcut->setEnabled(true);
328 return true;
331 m_shortcut->setEnabled(false);
332 return false;
335 bool Shortcut::isActive() const
337 return m_shortcut->isEnabled();
340 // ---------- Action ------------
343 \class Action
344 \internal
346 Action::Action(int id)
347 : CommandPrivate(id), m_action(0)
350 QString Action::name() const
352 if (!m_action) {
353 return QString();
356 return m_action->text();
359 void Action::setAction(QAction *action)
361 m_action = action;
362 if (m_action) {
363 m_action->setParent(this);
364 m_toolTip = m_action->toolTip();
368 QAction *Action::action() const
370 return m_action;
373 void Action::setLocations(const QList<CommandLocation> &locations)
375 m_locations = locations;
378 QList<CommandLocation> Action::locations() const
380 return m_locations;
383 void Action::setDefaultKeySequence(const QKeySequence &key)
385 if (m_action->shortcut().isEmpty()) {
386 setKeySequence(key);
388 CommandPrivate::setDefaultKeySequence(key);
391 void Action::setKeySequence(const QKeySequence &key)
393 m_action->setShortcut(key);
394 updateToolTipWithKeySequence();
395 emit keySequenceChanged();
398 void Action::updateToolTipWithKeySequence()
400 if (m_action->shortcut().isEmpty()) {
401 m_action->setToolTip(m_toolTip);
402 } else {
403 m_action->setToolTip(stringWithAppendedShortcut(m_toolTip));
407 QKeySequence Action::keySequence() const
409 return m_action->shortcut();
412 // ---------- OverrideableAction ------------
415 \class OverrideableAction
416 \internal
419 OverrideableAction::OverrideableAction(int id)
420 : Action(id), m_currentAction(0), m_active(false),
421 m_contextInitialized(false)
424 void OverrideableAction::setAction(QAction *action)
426 Action::setAction(action);
429 bool OverrideableAction::setCurrentContext(const QList<int> &context)
431 m_context = context;
433 QAction *oldAction = m_currentAction;
434 m_currentAction = 0;
435 for (int i = 0; i < m_context.size(); ++i) {
436 if (QAction * a = m_contextActionMap.value(m_context.at(i), 0)) {
437 m_currentAction = a;
438 break;
442 if (m_currentAction == oldAction && m_contextInitialized) {
443 return true;
445 m_contextInitialized = true;
447 if (oldAction) {
448 disconnect(oldAction, SIGNAL(changed()), this, SLOT(actionChanged()));
449 disconnect(m_action, SIGNAL(triggered(bool)), oldAction, SIGNAL(triggered(bool)));
450 disconnect(m_action, SIGNAL(toggled(bool)), oldAction, SLOT(setChecked(bool)));
452 if (m_currentAction) {
453 connect(m_currentAction, SIGNAL(changed()), this, SLOT(actionChanged()));
454 // we want to avoid the toggling semantic on slot trigger(), so we just connect the signals
455 connect(m_action, SIGNAL(triggered(bool)), m_currentAction, SIGNAL(triggered(bool)));
456 // we need to update the checked state, so we connect to setChecked slot, which also fires a toggled signal
457 connect(m_action, SIGNAL(toggled(bool)), m_currentAction, SLOT(setChecked(bool)));
458 actionChanged();
459 m_active = true;
460 return true;
462 if (hasAttribute(CA_Hide)) {
463 m_action->setVisible(false);
465 m_action->setEnabled(false);
466 m_active = false;
467 return false;
470 void OverrideableAction::addOverrideAction(QAction *action, const QList<int> &context)
472 if (context.isEmpty()) {
473 m_contextActionMap.insert(0, action);
474 } else {
475 for (int i = 0; i < context.size(); ++i) {
476 int k = context.at(i);
477 if (m_contextActionMap.contains(k)) {
478 qWarning() << QString("addOverrideAction: action already registered for context when registering '%1'").arg(action->text());
480 m_contextActionMap.insert(k, action);
485 void OverrideableAction::actionChanged()
487 if (hasAttribute(CA_UpdateIcon)) {
488 m_action->setIcon(m_currentAction->icon());
489 m_action->setIconText(m_currentAction->iconText());
491 if (hasAttribute(CA_UpdateText)) {
492 m_action->setText(m_currentAction->text());
493 m_toolTip = m_currentAction->toolTip();
494 updateToolTipWithKeySequence();
495 m_action->setStatusTip(m_currentAction->statusTip());
496 m_action->setWhatsThis(m_currentAction->whatsThis());
499 m_action->setCheckable(m_currentAction->isCheckable());
500 bool block = m_action->blockSignals(true);
501 m_action->setChecked(m_currentAction->isChecked());
502 m_action->blockSignals(block);
504 m_action->setEnabled(m_currentAction->isEnabled());
505 m_action->setVisible(m_currentAction->isVisible());
508 bool OverrideableAction::isActive() const
510 return m_active;