Files
qt-creator/src/plugins/coreplugin/actionmanager/command.cpp

428 lines
12 KiB
C++
Raw Normal View History

/****************************************************************************
2008-12-02 12:01:29 +01:00
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
2008-12-02 12:01:29 +01:00
**
** This file is part of Qt Creator.
2008-12-02 12:01:29 +01:00
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
2010-12-17 16:01:08 +01:00
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
2008-12-02 14:09:21 +01:00
#include "command_p.h"
#include <coreplugin/icontext.h>
#include <coreplugin/id.h>
#include <utils/hostosinfo.h>
#include <QDebug>
#include <QTextStream>
#include <QAction>
#include <QShortcut>
2008-12-02 12:01:29 +01:00
/*!
\class Core::Command
2008-12-02 12:01:29 +01:00
\mainclass
\brief The Command class represents an action, such as a menu item, tool button, or shortcut.
You do not create Command objects directly, but use \l{ActionManager::registerAction()}
2009-05-05 18:37:18 +02:00
to register an action and retrieve a Command. The Command object represents the user visible
action and its properties. If multiple actions are registered with the same ID (but
different contexts) the returned Command is the shared one between these actions.
2008-12-02 12:01:29 +01:00
A Command has two basic properties: a default shortcut and a default text. The default
2009-05-05 18:37:18 +02:00
shortcut is a key sequence that the user can use to trigger the active action that
the Command represents. The default text is e.g. used for representing the Command
in the keyboard shortcut preference pane. If the default text is empty, the text
of the visible action is used.
The user visible action is updated to represent the state of the active action (if any).
For performance reasons only the enabled and visible state are considered by default though.
You can tell a Command to also update the actions icon and text by setting the
corresponding \l{Command::CommandAttribute}{attribute}.
If there is no active action, the default behavior of the visible action is to be disabled.
You can change that behavior to make the visible action hide instead via the Command's
\l{Command::CommandAttribute}{attributes}.
2008-12-02 12:01:29 +01:00
*/
/*!
\enum Command::CommandAttribute
This enum defines how the user visible action is updated when the active action changes.
2009-05-06 18:29:37 +02:00
The default is to update the enabled and visible state, and to disable the
user visible action when there is no active action.
\omitvalue CA_Mask
\value CA_UpdateText
Also update the actions text.
\value CA_UpdateIcon
Also update the actions icon.
\value CA_Hide
When there is no active action, hide the user "visible" action, instead of just
disabling it.
\value CA_NonConfigurable
2009-05-06 18:29:37 +02:00
Flag to indicate that the keyboard shortcut of this Command should not be
configurable by the user.
2008-12-02 12:01:29 +01:00
*/
/*!
2009-05-06 18:29:37 +02:00
\fn void Command::setDefaultKeySequence(const QKeySequence &key)
Sets the default keyboard shortcut that can be used to activate this command to \a key.
2009-05-06 18:29:37 +02:00
This is used if the user didn't customize the shortcut, or resets the shortcut
to the default one.
2008-12-02 12:01:29 +01:00
*/
/*!
2009-05-06 18:29:37 +02:00
\fn void Command::defaultKeySequence() const
Returns the default keyboard shortcut that can be used to activate this command.
\sa setDefaultKeySequence()
2008-12-02 12:01:29 +01:00
*/
/*!
2009-05-06 18:29:37 +02:00
\fn void Command::keySequenceChanged()
Sent when the keyboard shortcut assigned to this Command changes, e.g.
when the user sets it in the keyboard shortcut settings dialog.
2008-12-02 12:01:29 +01:00
*/
/*!
2009-05-06 18:29:37 +02:00
\fn QKeySequence Command::keySequence() const
Returns the current keyboard shortcut assigned to this Command.
\sa defaultKeySequence()
2008-12-02 12:01:29 +01:00
*/
/*!
2009-05-06 18:29:37 +02:00
\fn void Command::setKeySequence(const QKeySequence &key)
\internal
*/
/*!
\fn void Command::setDescription(const QString &text)
Sets the \a text that is used to represent the Command in the
keyboard shortcut settings dialog. If you do not set this,
2009-05-06 18:29:37 +02:00
the current text from the user visible action is taken (which
is fine in many cases).
2009-05-06 18:29:37 +02:00
*/
/*!
\fn QString Command::description() const
2009-05-06 18:29:37 +02:00
Returns the text that is used to present this Command to the user.
\sa setDescription()
2009-05-06 18:29:37 +02:00
*/
/*!
\fn int Command::id() const
\internal
*/
/*!
\fn QString Command::stringWithAppendedShortcut(const QString &string) const
Returns the \a string with an appended representation of the keyboard shortcut
that is currently assigned to this Command.
*/
/*!
\fn QAction *Command::action() const
Returns the user visible action for this Command.
If the Command represents a shortcut, it returns null.
Use this action to put it on e.g. tool buttons. The action
automatically forwards trigger and toggle signals to the
action that is currently active for this Command.
It also shows the current keyboard shortcut in its
tool tip (in addition to the tool tip of the active action)
and gets disabled/hidden when there is
no active action for the current context.
2008-12-02 12:01:29 +01:00
*/
/*!
2009-05-06 18:29:37 +02:00
\fn QShortcut *Command::shortcut() const
Returns the shortcut for this Command.
If the Command represents an action, it returns null.
2008-12-02 12:01:29 +01:00
*/
/*!
2009-05-06 18:29:37 +02:00
\fn void Command::setAttribute(CommandAttribute attribute)
Adds \a attribute to the attributes of this Command.
2009-05-06 18:29:37 +02:00
\sa CommandAttribute
\sa removeAttribute()
\sa hasAttribute()
2008-12-02 12:01:29 +01:00
*/
/*!
2009-05-06 18:29:37 +02:00
\fn void Command::removeAttribute(CommandAttribute attribute)
Removes \a attribute from the attributes of this Command.
2009-05-06 18:29:37 +02:00
\sa CommandAttribute
\sa setAttribute()
2008-12-02 12:01:29 +01:00
*/
/*!
2009-05-06 18:29:37 +02:00
\fn bool Command::hasAttribute(CommandAttribute attribute) const
Returns whether the Command has the \a attribute set.
2009-05-06 18:29:37 +02:00
\sa CommandAttribute
\sa removeAttribute()
\sa setAttribute()
*/
/*!
\fn bool Command::isActive() const
Returns whether the Command has an active action or shortcut for the current
2009-05-06 18:29:37 +02:00
context.
*/
/*!
\fn bool Command::isScriptable() const
Returns whether the Command is scriptable. A scriptable command can be called
from a script without the need for the user to interact with it.
*/
/*!
\fn bool Command::isScriptable(const Context &) const
Returns whether the Command is scriptable for the given context.
A scriptable command can be called from a script without the need for the user to
interact with it.
*/
using namespace Core;
2008-12-02 12:01:29 +01:00
using namespace Core::Internal;
/*!
\class Action
\internal
2008-12-02 12:01:29 +01:00
*/
Action::Action(Id id)
: m_attributes(0),
m_id(id),
m_isKeyInitialized(false),
m_action(new Utils::ProxyAction(this)),
m_active(false),
m_contextInitialized(false)
{
m_action->setShortcutVisibleInToolTip(true);
connect(m_action, SIGNAL(changed()), this, SLOT(updateActiveState()));
}
2008-12-02 12:01:29 +01:00
Id Action::id() const
2008-12-02 12:01:29 +01:00
{
return m_id;
2008-12-02 12:01:29 +01:00
}
void Action::setDefaultKeySequence(const QKeySequence &key)
2008-12-02 12:01:29 +01:00
{
if (!m_isKeyInitialized)
setKeySequence(key);
2008-12-02 12:01:29 +01:00
m_defaultKey = key;
}
QKeySequence Action::defaultKeySequence() const
2008-12-02 12:01:29 +01:00
{
return m_defaultKey;
}
QAction *Action::action() const
2008-12-02 12:01:29 +01:00
{
return m_action;
2008-12-02 12:01:29 +01:00
}
QString Action::stringWithAppendedShortcut(const QString &str) const
2008-12-02 12:01:29 +01:00
{
return Utils::ProxyAction::stringWithAppendedShortcut(str, keySequence());
2008-12-02 12:01:29 +01:00
}
Context Action::context() const
2008-12-02 12:01:29 +01:00
{
return m_context;
}
void Action::setKeySequence(const QKeySequence &key)
2008-12-02 12:01:29 +01:00
{
m_isKeyInitialized = true;
m_action->setShortcut(key);
2008-12-02 12:01:29 +01:00
emit keySequenceChanged();
}
QKeySequence Action::keySequence() const
2008-12-02 12:01:29 +01:00
{
return m_action->shortcut();
2008-12-02 12:01:29 +01:00
}
void Action::setDescription(const QString &text)
2008-12-02 12:01:29 +01:00
{
m_defaultText = text;
2008-12-02 12:01:29 +01:00
}
QString Action::description() const
2008-12-02 12:01:29 +01:00
{
if (!m_defaultText.isEmpty())
return m_defaultText;
if (action()) {
QString text = action()->text();
text.remove(QRegExp(QLatin1String("&(?!&)")));
if (!text.isEmpty())
return text;
}
return id().toString();
2008-12-02 12:01:29 +01:00
}
void Action::setCurrentContext(const Core::Context &context)
2008-12-02 12:01:29 +01:00
{
m_context = context;
QAction *currentAction = 0;
2008-12-02 12:01:29 +01:00
for (int i = 0; i < m_context.size(); ++i) {
if (QAction *a = m_contextActionMap.value(m_context.at(i), 0)) {
currentAction = a;
2008-12-02 12:01:29 +01:00
break;
}
}
m_action->setAction(currentAction);
updateActiveState();
}
2008-12-02 12:01:29 +01:00
void Action::updateActiveState()
{
setActive(m_action->isEnabled() && m_action->isVisible() && !m_action->isSeparator());
2008-12-02 12:01:29 +01:00
}
static QString msgActionWarning(QAction *newAction, Id id, QAction *oldAction)
{
QString msg;
QTextStream str(&msg);
str << "addOverrideAction " << newAction->objectName() << '/' << newAction->text()
<< ": Action ";
if (oldAction)
str << oldAction->objectName() << '/' << oldAction->text();
str << " is already registered for context " << id.uniqueIdentifier() << ' '
<< id.toString() << '.';
return msg;
}
void Action::addOverrideAction(QAction *action, const Core::Context &context, bool scriptable)
2008-12-02 12:01:29 +01:00
{
if (Utils::HostOsInfo::isMacHost())
action->setIconVisibleInMenu(false);
if (isEmpty())
m_action->initialize(action);
2010-06-25 18:05:09 +02:00
if (context.isEmpty()) {
2008-12-02 12:01:29 +01:00
m_contextActionMap.insert(0, action);
} else {
for (int i = 0; i < context.size(); ++i) {
Id id = context.at(i);
if (m_contextActionMap.contains(id))
qWarning("%s", qPrintable(msgActionWarning(action, id, m_contextActionMap.value(id, 0))));
m_contextActionMap.insert(id, action);
2008-12-02 12:01:29 +01:00
}
}
m_scriptableMap[action] = scriptable;
setCurrentContext(m_context);
2008-12-02 12:01:29 +01:00
}
void Action::removeOverrideAction(QAction *action)
{
QMutableMapIterator<Id, QPointer<QAction> > it(m_contextActionMap);
while (it.hasNext()) {
it.next();
if (it.value() == 0)
it.remove();
else if (it.value() == action)
it.remove();
}
setCurrentContext(m_context);
}
bool Action::isActive() const
2008-12-02 12:01:29 +01:00
{
return m_active;
}
void Action::setActive(bool state)
{
if (state != m_active) {
m_active = state;
emit activeStateChanged();
}
}
bool Action::isEmpty() const
{
return m_contextActionMap.isEmpty();
}
bool Action::isScriptable() const
{
return m_scriptableMap.values().contains(true);
}
bool Action::isScriptable(const Core::Context &context) const
{
if (context == m_context && m_scriptableMap.contains(m_action->action()))
return m_scriptableMap.value(m_action->action());
for (int i = 0; i < context.size(); ++i) {
if (QAction *a = m_contextActionMap.value(context.at(i), 0)) {
if (m_scriptableMap.contains(a) && m_scriptableMap.value(a))
return true;
}
}
return false;
}
void Action::setAttribute(CommandAttribute attr)
{
m_attributes |= attr;
switch (attr) {
case Core::Command::CA_Hide:
m_action->setAttribute(Utils::ProxyAction::Hide);
break;
case Core::Command::CA_UpdateText:
m_action->setAttribute(Utils::ProxyAction::UpdateText);
break;
case Core::Command::CA_UpdateIcon:
m_action->setAttribute(Utils::ProxyAction::UpdateIcon);
break;
case Core::Command::CA_NonConfigurable:
break;
}
}
void Action::removeAttribute(CommandAttribute attr)
{
m_attributes &= ~attr;
switch (attr) {
case Core::Command::CA_Hide:
m_action->removeAttribute(Utils::ProxyAction::Hide);
break;
case Core::Command::CA_UpdateText:
m_action->removeAttribute(Utils::ProxyAction::UpdateText);
break;
case Core::Command::CA_UpdateIcon:
m_action->removeAttribute(Utils::ProxyAction::UpdateIcon);
break;
case Core::Command::CA_NonConfigurable:
break;
}
}
bool Action::hasAttribute(Command::CommandAttribute attr) const
{
return (m_attributes & attr);
}