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

469 lines
9.2 KiB
C++
Raw Normal View History

/**************************************************************************
2008-12-02 12:01:29 +01:00
**
** This file is part of Qt Creator
**
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
2008-12-02 12:01:29 +01:00
**
** Contact: Qt Software Information (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** 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.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
2008-12-02 12:01:29 +01:00
**
**************************************************************************/
2008-12-02 14:09:21 +01:00
2008-12-02 12:01:29 +01:00
#include <QtCore/QDebug>
#include <QtGui/QAction>
#include <QtGui/QShortcut>
#include "command_p.h"
2008-12-02 12:01:29 +01:00
/*!
\class Core::Command
2008-12-02 12:01:29 +01:00
\mainclass
\brief The class Command represents an action like a menu item, tool button, or shortcut.
2008-12-02 12:01:29 +01:00
*/
/*!
\enum Command::CommandAttribute
2008-12-02 12:01:29 +01:00
*/
/*!
\fn virtual void Command::setDefaultKeySequence(const QKeySequence &key)
2008-12-02 12:01:29 +01:00
*/
/*!
\fn virtual int Command::id() const
2008-12-02 12:01:29 +01:00
*/
/*!
\fn virtual QAction *Command::action() const
2008-12-02 12:01:29 +01:00
*/
/*!
\fn virtual QShortcut *Command::shortcut() const
2008-12-02 12:01:29 +01:00
*/
/*!
\fn virtual void Command::setAttribute(CommandAttribute attr)
2008-12-02 12:01:29 +01:00
*/
/*!
\fn virtual void Command::removeAttribute(CommandAttribute attr)
2008-12-02 12:01:29 +01:00
*/
/*!
\fn virtual bool Command::hasAttribute(CommandAttribute attr) const
2008-12-02 12:01:29 +01:00
*/
/*!
\fn virtual bool Command::isActive() const
2008-12-02 12:01:29 +01:00
*/
/*!
\fn virtual Command::~Command()
2008-12-02 12:01:29 +01:00
*/
using namespace Core::Internal;
/*!
\class CommandPrivate
\inheaderfile command_p.h
\internal
2008-12-02 12:01:29 +01:00
*/
2009-05-06 16:06:43 +02:00
CommandPrivate::CommandPrivate(int id)
: m_attributes(0), m_id(id)
2008-12-02 12:01:29 +01:00
{
}
void CommandPrivate::setDefaultKeySequence(const QKeySequence &key)
2008-12-02 12:01:29 +01:00
{
m_defaultKey = key;
}
QKeySequence CommandPrivate::defaultKeySequence() const
2008-12-02 12:01:29 +01:00
{
return m_defaultKey;
}
void CommandPrivate::setDefaultText(const QString &text)
2008-12-02 12:01:29 +01:00
{
m_defaultText = text;
}
QString CommandPrivate::defaultText() const
2008-12-02 12:01:29 +01:00
{
return m_defaultText;
}
int CommandPrivate::id() const
2008-12-02 12:01:29 +01:00
{
return m_id;
}
QAction *CommandPrivate::action() const
2008-12-02 12:01:29 +01:00
{
return 0;
}
QShortcut *CommandPrivate::shortcut() const
2008-12-02 12:01:29 +01:00
{
return 0;
}
void CommandPrivate::setAttribute(CommandAttribute attr)
2008-12-02 12:01:29 +01:00
{
2009-05-06 16:06:43 +02:00
m_attributes |= attr;
2008-12-02 12:01:29 +01:00
}
void CommandPrivate::removeAttribute(CommandAttribute attr)
2008-12-02 12:01:29 +01:00
{
2009-05-06 16:06:43 +02:00
m_attributes &= ~attr;
2008-12-02 12:01:29 +01:00
}
bool CommandPrivate::hasAttribute(CommandAttribute attr) const
2008-12-02 12:01:29 +01:00
{
2009-05-06 16:06:43 +02:00
return (m_attributes & attr);
2008-12-02 12:01:29 +01:00
}
QString CommandPrivate::stringWithAppendedShortcut(const QString &str) const
2008-12-02 12:01:29 +01:00
{
return QString("%1 <span style=\"color: gray; font-size: small\">%2</span>").arg(str).arg(
keySequence().toString(QKeySequence::NativeText));
}
// ---------- Shortcut ------------
/*!
\class Shortcut
*/
/*!
...
*/
Shortcut::Shortcut(int id)
2009-05-06 16:06:43 +02:00
: CommandPrivate(id), m_shortcut(0)
2008-12-02 12:01:29 +01:00
{
}
/*!
...
*/
QString Shortcut::name() const
{
if (!m_shortcut)
return QString();
return m_shortcut->whatsThis();
}
/*!
...
*/
void Shortcut::setShortcut(QShortcut *shortcut)
{
m_shortcut = shortcut;
}
/*!
...
*/
QShortcut *Shortcut::shortcut() const
{
return m_shortcut;
}
/*!
...
*/
void Shortcut::setContext(const QList<int> &context)
{
m_context = context;
}
/*!
...
*/
QList<int> Shortcut::context() const
{
return m_context;
}
/*!
...
*/
void Shortcut::setDefaultKeySequence(const QKeySequence &key)
{
setKeySequence(key);
CommandPrivate::setDefaultKeySequence(key);
2008-12-02 12:01:29 +01:00
}
void Shortcut::setKeySequence(const QKeySequence &key)
{
m_shortcut->setKey(key);
emit keySequenceChanged();
}
QKeySequence Shortcut::keySequence() const
{
return m_shortcut->key();
}
void Shortcut::setDefaultText(const QString &text)
{
m_defaultText = text;
}
QString Shortcut::defaultText() const
{
return m_defaultText;
}
/*!
...
*/
bool Shortcut::setCurrentContext(const QList<int> &context)
{
foreach (int ctxt, m_context) {
if (context.contains(ctxt)) {
m_shortcut->setEnabled(true);
return true;
}
}
m_shortcut->setEnabled(false);
return false;
}
/*!
...
*/
bool Shortcut::isActive() const
{
return m_shortcut->isEnabled();
}
// ---------- Action ------------
/*!
\class Action
*/
/*!
...
*/
2009-05-06 16:06:43 +02:00
Action::Action(int id)
: CommandPrivate(id), m_action(0)
2008-12-02 12:01:29 +01:00
{
}
/*!
...
*/
QString Action::name() const
{
if (!m_action)
return QString();
return m_action->text();
}
/*!
...
*/
void Action::setAction(QAction *action)
{
m_action = action;
if (m_action) {
m_action->setParent(this);
m_toolTip = m_action->toolTip();
}
}
/*!
...
*/
QAction *Action::action() const
{
return m_action;
}
/*!
...
*/
void Action::setLocations(const QList<CommandLocation> &locations)
{
m_locations = locations;
}
/*!
...
*/
QList<CommandLocation> Action::locations() const
{
return m_locations;
}
/*!
...
*/
void Action::setDefaultKeySequence(const QKeySequence &key)
{
setKeySequence(key);
CommandPrivate::setDefaultKeySequence(key);
2008-12-02 12:01:29 +01:00
}
void Action::setKeySequence(const QKeySequence &key)
{
m_action->setShortcut(key);
updateToolTipWithKeySequence();
emit keySequenceChanged();
}
void Action::updateToolTipWithKeySequence()
{
if (m_action->shortcut().isEmpty())
m_action->setToolTip(m_toolTip);
else
m_action->setToolTip(stringWithAppendedShortcut(m_toolTip));
}
QKeySequence Action::keySequence() const
{
return m_action->shortcut();
}
// ---------- OverrideableAction ------------
/*!
\class OverrideableAction
*/
/*!
...
*/
OverrideableAction::OverrideableAction(int id)
2009-05-06 16:06:43 +02:00
: Action(id), m_currentAction(0), m_active(false),
2008-12-02 12:01:29 +01:00
m_contextInitialized(false)
{
}
/*!
...
*/
void OverrideableAction::setAction(QAction *action)
{
Action::setAction(action);
}
/*!
...
*/
bool OverrideableAction::setCurrentContext(const QList<int> &context)
{
m_context = context;
QAction *oldAction = m_currentAction;
m_currentAction = 0;
for (int i = 0; i < m_context.size(); ++i) {
if (QAction *a = m_contextActionMap.value(m_context.at(i), 0)) {
m_currentAction = a;
break;
}
}
if (m_currentAction == oldAction && m_contextInitialized)
return true;
m_contextInitialized = true;
if (oldAction) {
disconnect(oldAction, SIGNAL(changed()), this, SLOT(actionChanged()));
disconnect(m_action, SIGNAL(triggered(bool)), oldAction, SIGNAL(triggered(bool)));
disconnect(m_action, SIGNAL(toggled(bool)), oldAction, SLOT(setChecked(bool)));
}
if (m_currentAction) {
connect(m_currentAction, SIGNAL(changed()), this, SLOT(actionChanged()));
// we want to avoid the toggling semantic on slot trigger(), so we just connect the signals
connect(m_action, SIGNAL(triggered(bool)), m_currentAction, SIGNAL(triggered(bool)));
// we need to update the checked state, so we connect to setChecked slot, which also fires a toggled signal
connect(m_action, SIGNAL(toggled(bool)), m_currentAction, SLOT(setChecked(bool)));
actionChanged();
m_active = true;
return true;
}
if (hasAttribute(CA_Hide))
m_action->setVisible(false);
m_action->setEnabled(false);
m_active = false;
return false;
}
/*!
...
*/
void OverrideableAction::addOverrideAction(QAction *action, const QList<int> &context)
{
if (context.isEmpty()) {
m_contextActionMap.insert(0, action);
} else {
for (int i=0; i<context.size(); ++i) {
int k = context.at(i);
if (m_contextActionMap.contains(k))
qWarning() << QString("addOverrideAction: action already registered for context when registering '%1'").arg(action->text());
m_contextActionMap.insert(k, action);
}
}
}
/*!
...
*/
void OverrideableAction::actionChanged()
{
if (hasAttribute(CA_UpdateIcon)) {
m_action->setIcon(m_currentAction->icon());
m_action->setIconText(m_currentAction->iconText());
}
if (hasAttribute(CA_UpdateText)) {
m_action->setText(m_currentAction->text());
m_toolTip = m_currentAction->toolTip();
updateToolTipWithKeySequence();
m_action->setStatusTip(m_currentAction->statusTip());
m_action->setWhatsThis(m_currentAction->whatsThis());
}
bool block = m_action->blockSignals(true);
m_action->setChecked(m_currentAction->isChecked());
m_action->blockSignals(block);
m_action->setEnabled(m_currentAction->isEnabled());
m_action->setVisible(m_currentAction->isVisible());
}
/*!
...
*/
bool OverrideableAction::isActive() const
{
return m_active;
}