2009-02-25 09:15:00 +01:00
|
|
|
/**************************************************************************
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
|
**
|
2011-01-11 16:28:15 +01:00
|
|
|
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2009-06-17 00:01:27 +10:00
|
|
|
** Contact: Nokia Corporation (qt-info@nokia.com)
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** No Commercial Usage
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** This file contains pre-release code and may not be distributed.
|
|
|
|
|
** You may use this file in accordance with the terms and conditions
|
|
|
|
|
** contained in the Technology Preview License Agreement accompanying
|
|
|
|
|
** this package.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** GNU Lesser General Public License Usage
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** 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.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** In addition, as a special exception, Nokia gives you certain additional
|
|
|
|
|
** rights. These rights are described in the Nokia Qt LGPL Exception
|
|
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
|
|
|
|
** If you have questions regarding the use of this file, please contact
|
|
|
|
|
** Nokia at qt-info@nokia.com.
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
**************************************************************************/
|
2008-12-02 14:09:21 +01:00
|
|
|
|
2009-01-13 13:07:29 +01:00
|
|
|
#include "actionmanager_p.h"
|
2008-12-02 12:01:29 +01:00
|
|
|
#include "mainwindow.h"
|
2009-01-14 12:25:54 +01:00
|
|
|
#include "actioncontainer_p.h"
|
2009-01-14 12:58:06 +01:00
|
|
|
#include "command_p.h"
|
2008-12-02 12:01:29 +01:00
|
|
|
#include "uniqueidmanager.h"
|
|
|
|
|
|
|
|
|
|
#include <coreplugin/coreconstants.h>
|
2010-01-13 16:51:28 +01:00
|
|
|
#include <coreplugin/icore.h>
|
2010-12-06 17:21:03 +01:00
|
|
|
#include <utils/qtcassert.h>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
#include <QtCore/QDebug>
|
|
|
|
|
#include <QtCore/QSettings>
|
|
|
|
|
#include <QtGui/QMenu>
|
|
|
|
|
#include <QtGui/QAction>
|
|
|
|
|
#include <QtGui/QShortcut>
|
|
|
|
|
#include <QtGui/QMenuBar>
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
enum { warnAboutFindFailures = 0 };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*!
|
2009-01-13 13:39:31 +01:00
|
|
|
\class Core::ActionManager
|
2008-12-02 12:01:29 +01:00
|
|
|
\mainclass
|
|
|
|
|
|
2009-01-13 19:02:54 +01:00
|
|
|
\brief The action manager is responsible for registration of menus and
|
|
|
|
|
menu items and keyboard shortcuts.
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2009-01-13 13:39:31 +01:00
|
|
|
The ActionManager is the central bookkeeper of actions and their shortcuts and layout.
|
2009-01-13 19:02:54 +01:00
|
|
|
You get the only implementation of this class from the core interface
|
|
|
|
|
ICore::actionManager() method, e.g.
|
|
|
|
|
\code
|
2009-01-20 11:52:04 +01:00
|
|
|
Core::ICore::instance()->actionManager()
|
2009-01-13 19:02:54 +01:00
|
|
|
\endcode
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
The main reasons for the need of this class is to provide a central place where the user
|
|
|
|
|
can specify all his keyboard shortcuts, and to provide a solution for actions that should
|
|
|
|
|
behave differently in different contexts (like the copy/replace/undo/redo actions).
|
|
|
|
|
|
2009-05-04 15:09:31 +02:00
|
|
|
\section1 Contexts
|
|
|
|
|
|
|
|
|
|
All actions that are registered with the same string ID (but different context lists)
|
2009-01-13 19:02:54 +01:00
|
|
|
are considered to be overloads of the same command, represented by an instance
|
2009-01-14 13:17:53 +01:00
|
|
|
of the Command class.
|
2009-05-04 15:09:31 +02:00
|
|
|
Exactly only one of the registered actions with the same ID is active at any time.
|
|
|
|
|
Which action this is, is defined by the context list that the actions were registered
|
|
|
|
|
with:
|
|
|
|
|
|
|
|
|
|
If the current focus widget was registered via \l{ICore::addContextObject()},
|
|
|
|
|
all the contexts returned by its IContext object are active. In addition all
|
|
|
|
|
contexts set via \l{ICore::addAdditionalContext()} are active as well. If one
|
|
|
|
|
of the actions was registered for one of these active contexts, it is the one
|
|
|
|
|
active action, and receives \c triggered and \c toggled signals. Also the
|
|
|
|
|
appearance of the visible action for this ID might be adapted to this
|
|
|
|
|
active action (depending on the settings of the corresponding \l{Command} object).
|
|
|
|
|
|
2009-01-14 13:17:53 +01:00
|
|
|
The action that is visible to the user is the one returned by Command::action().
|
2009-01-13 19:02:54 +01:00
|
|
|
If you provide yourself a user visible representation of your action you need
|
2009-01-14 13:17:53 +01:00
|
|
|
to use Command::action() for this.
|
2009-01-13 19:02:54 +01:00
|
|
|
When this action is invoked by the user,
|
|
|
|
|
the signal is forwarded to the registered action that is valid for the current context.
|
|
|
|
|
|
2009-05-04 15:09:31 +02:00
|
|
|
\section1 Registering Actions
|
|
|
|
|
|
|
|
|
|
To register a globally active action "My Action"
|
2009-01-13 19:02:54 +01:00
|
|
|
put the following in your plugin's IPlugin::initialize method:
|
|
|
|
|
\code
|
2009-01-20 11:52:04 +01:00
|
|
|
Core::ActionManager *am = Core::ICore::instance()->actionManager();
|
2009-01-13 19:02:54 +01:00
|
|
|
QAction *myAction = new QAction(tr("My Action"), this);
|
2009-01-14 13:17:53 +01:00
|
|
|
Core::Command *cmd = am->registerAction(myAction,
|
2009-01-13 19:02:54 +01:00
|
|
|
"myplugin.myaction",
|
2010-06-28 14:30:03 +02:00
|
|
|
Core::Context(C_GLOBAL));
|
2009-01-13 19:02:54 +01:00
|
|
|
cmd->setDefaultKeySequence(QKeySequence(tr("Ctrl+Alt+u")));
|
|
|
|
|
connect(myAction, SIGNAL(triggered()), this, SLOT(performMyAction()));
|
|
|
|
|
\endcode
|
|
|
|
|
|
|
|
|
|
So the \c connect is done to your own QAction instance. If you create e.g.
|
|
|
|
|
a tool button that should represent the action you add the action
|
2009-01-14 13:17:53 +01:00
|
|
|
from Command::action() to it:
|
2009-01-13 19:02:54 +01:00
|
|
|
\code
|
|
|
|
|
QToolButton *myButton = new QToolButton(someParentWidget);
|
|
|
|
|
myButton->setDefaultAction(cmd->action());
|
|
|
|
|
\endcode
|
|
|
|
|
|
|
|
|
|
Also use the ActionManager to add items to registered
|
|
|
|
|
action containers like the applications menu bar or menus in that menu bar.
|
|
|
|
|
To do this, you register your action via the
|
2009-05-04 15:09:31 +02:00
|
|
|
registerAction methods, get the action container for a specific ID (like specified in
|
2009-01-13 19:02:54 +01:00
|
|
|
the Core::Constants namespace) with a call of
|
2008-12-02 12:01:29 +01:00
|
|
|
actionContainer(const QString&) and add your command to this container.
|
|
|
|
|
|
2009-01-13 19:02:54 +01:00
|
|
|
Following the example adding "My Action" to the "Tools" menu would be done by
|
|
|
|
|
\code
|
|
|
|
|
am->actionContainer(Core::M_TOOLS)->addAction(cmd);
|
|
|
|
|
\endcode
|
|
|
|
|
|
2009-05-04 15:09:31 +02:00
|
|
|
\section1 Important Guidelines:
|
2008-12-02 12:01:29 +01:00
|
|
|
\list
|
|
|
|
|
\o Always register your actions and shortcuts!
|
2009-05-04 15:09:31 +02:00
|
|
|
\o Register your actions and shortcuts during your plugin's \l{ExtensionSystem::IPlugin::initialize()}
|
|
|
|
|
or \l{ExtensionSystem::IPlugin::extensionsInitialized()} methods, otherwise the shortcuts won't appear
|
2009-01-13 19:02:54 +01:00
|
|
|
in the keyboard settings dialog from the beginning.
|
2009-05-04 15:09:31 +02:00
|
|
|
\o When registering an action with \c{cmd=registerAction(action, id, contexts)} be sure to connect
|
|
|
|
|
your own action \c{connect(action, SIGNAL...)} but make \c{cmd->action()} visible to the user, i.e.
|
|
|
|
|
\c{widget->addAction(cmd->action())}.
|
2008-12-02 12:01:29 +01:00
|
|
|
\o Use this class to add actions to the applications menus
|
|
|
|
|
\endlist
|
|
|
|
|
|
2009-01-13 19:02:54 +01:00
|
|
|
\sa Core::ICore
|
2009-01-14 13:17:53 +01:00
|
|
|
\sa Core::Command
|
2009-01-14 12:39:59 +01:00
|
|
|
\sa Core::ActionContainer
|
2009-01-13 19:02:54 +01:00
|
|
|
\sa Core::IContext
|
2008-12-02 12:01:29 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*!
|
2009-01-14 12:39:59 +01:00
|
|
|
\fn ActionContainer *ActionManager::createMenu(const QString &id)
|
2009-01-13 19:02:54 +01:00
|
|
|
\brief Creates a new menu with the given string \a id.
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2009-01-14 12:39:59 +01:00
|
|
|
Returns a new ActionContainer that you can use to get the QMenu instance
|
2009-01-13 19:02:54 +01:00
|
|
|
or to add menu items to the menu. The ActionManager owns
|
2009-01-14 12:39:59 +01:00
|
|
|
the returned ActionContainer.
|
2009-01-13 19:02:54 +01:00
|
|
|
Add your menu to some other menu or a menu bar via the
|
2009-01-14 12:39:59 +01:00
|
|
|
ActionManager::actionContainer and ActionContainer::addMenu methods.
|
2008-12-02 12:01:29 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*!
|
2009-01-14 12:39:59 +01:00
|
|
|
\fn ActionContainer *ActionManager::createMenuBar(const QString &id)
|
2009-01-13 19:02:54 +01:00
|
|
|
\brief Creates a new menu bar with the given string \a id.
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2009-01-14 12:39:59 +01:00
|
|
|
Returns a new ActionContainer that you can use to get the QMenuBar instance
|
2009-01-13 19:02:54 +01:00
|
|
|
or to add menus to the menu bar. The ActionManager owns
|
2009-01-14 12:39:59 +01:00
|
|
|
the returned ActionContainer.
|
2008-12-02 12:01:29 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*!
|
2010-12-20 10:35:30 +01:00
|
|
|
\fn Command *ActionManager::registerAction(QAction *action, const QString &id, const Context &context, bool scriptable)
|
2009-01-13 19:02:54 +01:00
|
|
|
\brief Makes an \a action known to the system under the specified string \a id.
|
|
|
|
|
|
|
|
|
|
Returns a command object that represents the action in the application and is
|
|
|
|
|
owned by the ActionManager. You can registered several actions with the
|
|
|
|
|
same \a id as long as the \a context is different. In this case
|
|
|
|
|
a trigger of the actual action is forwarded to the registered QAction
|
|
|
|
|
for the currently active context.
|
2010-12-20 10:35:30 +01:00
|
|
|
A scriptable action can be called from a script without the need for the user
|
|
|
|
|
to interact with it.
|
2008-12-02 12:01:29 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*!
|
2010-12-20 10:35:30 +01:00
|
|
|
\fn Command *ActionManager::registerShortcut(QShortcut *shortcut, const QString &id, const Context &context, bool scriptable)
|
2009-01-13 19:02:54 +01:00
|
|
|
\brief Makes a \a shortcut known to the system under the specified string \a id.
|
|
|
|
|
|
|
|
|
|
Returns a command object that represents the shortcut in the application and is
|
|
|
|
|
owned by the ActionManager. You can registered several shortcuts with the
|
|
|
|
|
same \a id as long as the \a context is different. In this case
|
|
|
|
|
a trigger of the actual shortcut is forwarded to the registered QShortcut
|
|
|
|
|
for the currently active context.
|
2010-12-20 10:35:30 +01:00
|
|
|
A scriptable shortcut can be called from a script without the need for the user
|
|
|
|
|
to interact with it.
|
2008-12-02 12:01:29 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*!
|
2009-01-14 13:17:53 +01:00
|
|
|
\fn Command *ActionManager::command(const QString &id) const
|
|
|
|
|
\brief Returns the Command object that is known to the system
|
2009-01-13 19:02:54 +01:00
|
|
|
under the given string \a id.
|
|
|
|
|
|
|
|
|
|
\sa ActionManager::registerAction()
|
2008-12-02 12:01:29 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*!
|
2009-01-14 12:39:59 +01:00
|
|
|
\fn ActionContainer *ActionManager::actionContainer(const QString &id) const
|
2009-01-13 19:02:54 +01:00
|
|
|
\brief Returns the IActionContainter object that is know to the system
|
|
|
|
|
under the given string \a id.
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2009-01-13 19:02:54 +01:00
|
|
|
\sa ActionManager::createMenu()
|
|
|
|
|
\sa ActionManager::createMenuBar()
|
|
|
|
|
*/
|
2010-12-06 17:21:03 +01:00
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
\fn Command *ActionManager::unregisterAction(QAction *action, const QString &id)
|
|
|
|
|
\brief Removes the knowledge about an \a action under the specified string \a id.
|
|
|
|
|
|
|
|
|
|
Usually you do not need to unregister actions. The only valid use case for unregistering
|
|
|
|
|
actions, is for actions that represent user definable actions, like for the custom Locator
|
|
|
|
|
filters. If the user removes such an action, it also has to be unregistered from the action manager,
|
|
|
|
|
to make it disappear from shortcut settings etc.
|
|
|
|
|
*/
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
/*!
|
2009-01-13 19:02:54 +01:00
|
|
|
\fn ActionManager::ActionManager(QObject *parent)
|
|
|
|
|
\internal
|
2008-12-02 12:01:29 +01:00
|
|
|
*/
|
|
|
|
|
/*!
|
2009-01-13 19:02:54 +01:00
|
|
|
\fn ActionManager::~ActionManager()
|
|
|
|
|
\internal
|
2008-12-02 12:01:29 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using namespace Core;
|
|
|
|
|
using namespace Core::Internal;
|
|
|
|
|
|
2009-01-13 13:07:29 +01:00
|
|
|
ActionManagerPrivate* ActionManagerPrivate::m_instance = 0;
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
/*!
|
2009-01-13 19:02:54 +01:00
|
|
|
\class ActionManagerPrivate
|
|
|
|
|
\inheaderfile actionmanager_p.h
|
|
|
|
|
\internal
|
2008-12-02 12:01:29 +01:00
|
|
|
*/
|
|
|
|
|
|
2009-01-26 13:03:38 +01:00
|
|
|
ActionManagerPrivate::ActionManagerPrivate(MainWindow *mainWnd)
|
|
|
|
|
: ActionManager(mainWnd),
|
2008-12-02 12:01:29 +01:00
|
|
|
m_mainWnd(mainWnd)
|
|
|
|
|
{
|
|
|
|
|
m_instance = this;
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-13 13:07:29 +01:00
|
|
|
ActionManagerPrivate::~ActionManagerPrivate()
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
|
|
|
|
qDeleteAll(m_idCmdMap.values());
|
|
|
|
|
qDeleteAll(m_idContainerMap.values());
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-21 12:49:20 +01:00
|
|
|
ActionManagerPrivate *ActionManagerPrivate::instance()
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
|
|
|
|
return m_instance;
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-22 13:56:50 +01:00
|
|
|
QList<Command *> ActionManagerPrivate::commands() const
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2010-01-22 13:56:50 +01:00
|
|
|
// transform list of CommandPrivate into list of Command
|
|
|
|
|
QList<Command *> result;
|
2010-06-25 18:05:09 +02:00
|
|
|
foreach (Command *cmd, m_idCmdMap.values())
|
2010-01-22 13:56:50 +01:00
|
|
|
result << cmd;
|
|
|
|
|
return result;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2009-01-13 13:07:29 +01:00
|
|
|
bool ActionManagerPrivate::hasContext(int context) const
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2010-06-25 18:05:09 +02:00
|
|
|
return m_context.contains(context);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2010-06-28 10:30:45 +02:00
|
|
|
QDebug operator<<(QDebug in, const Context &context)
|
|
|
|
|
{
|
|
|
|
|
UniqueIDManager *uidm = UniqueIDManager::instance();
|
|
|
|
|
in << "CONTEXT: ";
|
|
|
|
|
foreach (int c, context)
|
|
|
|
|
in << " " << c << uidm->stringForUniqueIdentifier(c);
|
|
|
|
|
return in;
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-25 12:56:16 +02:00
|
|
|
void ActionManagerPrivate::setContext(const Context &context)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
|
|
|
|
// here are possibilities for speed optimization if necessary:
|
|
|
|
|
// let commands (de-)register themselves for contexts
|
|
|
|
|
// and only update commands that are either in old or new contexts
|
|
|
|
|
m_context = context;
|
|
|
|
|
const IdCmdMap::const_iterator cmdcend = m_idCmdMap.constEnd();
|
|
|
|
|
for (IdCmdMap::const_iterator it = m_idCmdMap.constBegin(); it != cmdcend; ++it)
|
|
|
|
|
it.value()->setCurrentContext(m_context);
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-25 12:56:16 +02:00
|
|
|
bool ActionManagerPrivate::hasContext(const Context &context) const
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2010-06-25 18:05:09 +02:00
|
|
|
for (int i = 0; i < m_context.size(); ++i) {
|
|
|
|
|
if (context.contains(m_context.at(i)))
|
2008-12-02 12:01:29 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-10 14:13:19 +02:00
|
|
|
ActionContainer *ActionManagerPrivate::createMenu(const Id &id)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2009-01-26 13:03:38 +01:00
|
|
|
const int uid = UniqueIDManager::instance()->uniqueIdentifier(id);
|
2008-12-02 12:01:29 +01:00
|
|
|
const IdContainerMap::const_iterator it = m_idContainerMap.constFind(uid);
|
|
|
|
|
if (it != m_idContainerMap.constEnd())
|
|
|
|
|
return it.value();
|
|
|
|
|
|
|
|
|
|
QMenu *m = new QMenu(m_mainWnd);
|
|
|
|
|
m->setObjectName(id);
|
|
|
|
|
|
|
|
|
|
MenuActionContainer *mc = new MenuActionContainer(uid);
|
|
|
|
|
mc->setMenu(m);
|
|
|
|
|
|
|
|
|
|
m_idContainerMap.insert(uid, mc);
|
|
|
|
|
|
|
|
|
|
return mc;
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-10 14:13:19 +02:00
|
|
|
ActionContainer *ActionManagerPrivate::createMenuBar(const Id &id)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2009-01-26 13:03:38 +01:00
|
|
|
const int uid = UniqueIDManager::instance()->uniqueIdentifier(id);
|
2008-12-02 12:01:29 +01:00
|
|
|
const IdContainerMap::const_iterator it = m_idContainerMap.constFind(uid);
|
|
|
|
|
if (it != m_idContainerMap.constEnd())
|
|
|
|
|
return it.value();
|
|
|
|
|
|
|
|
|
|
QMenuBar *mb = new QMenuBar; // No parent (System menu bar on Mac OS X)
|
|
|
|
|
mb->setObjectName(id);
|
|
|
|
|
|
|
|
|
|
MenuBarActionContainer *mbc = new MenuBarActionContainer(uid);
|
|
|
|
|
mbc->setMenuBar(mb);
|
|
|
|
|
|
|
|
|
|
m_idContainerMap.insert(uid, mbc);
|
|
|
|
|
|
|
|
|
|
return mbc;
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-20 10:35:30 +01:00
|
|
|
Command *ActionManagerPrivate::registerAction(QAction *action, const Id &id, const Context &context, bool scriptable)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2011-01-12 09:45:19 +01:00
|
|
|
Action *a = overridableAction(id);
|
|
|
|
|
if (a) {
|
2010-12-20 10:35:30 +01:00
|
|
|
a->addOverrideAction(action, context, scriptable);
|
2011-01-12 09:45:19 +01:00
|
|
|
emit commandListChanged();
|
|
|
|
|
emit commandAdded(id);
|
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
return a;
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-12 09:45:19 +01:00
|
|
|
Action *ActionManagerPrivate::overridableAction(const Id &id)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2010-01-14 15:33:58 +01:00
|
|
|
Action *a = 0;
|
2009-01-26 13:03:38 +01:00
|
|
|
const int uid = UniqueIDManager::instance()->uniqueIdentifier(id);
|
2009-01-14 12:58:06 +01:00
|
|
|
if (CommandPrivate *c = m_idCmdMap.value(uid, 0)) {
|
2010-01-14 15:33:58 +01:00
|
|
|
a = qobject_cast<Action *>(c);
|
2009-05-06 16:06:43 +02:00
|
|
|
if (!a) {
|
2008-12-02 12:01:29 +01:00
|
|
|
qWarning() << "registerAction: id" << id << "is registered with a different command type.";
|
2011-01-12 09:45:19 +01:00
|
|
|
return 0;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
2009-05-06 16:06:43 +02:00
|
|
|
} else {
|
2010-01-14 15:33:58 +01:00
|
|
|
a = new Action(uid);
|
2008-12-02 12:01:29 +01:00
|
|
|
m_idCmdMap.insert(uid, a);
|
2011-01-12 09:45:19 +01:00
|
|
|
m_mainWnd->addAction(a->action());
|
|
|
|
|
a->action()->setObjectName(id);
|
|
|
|
|
a->action()->setShortcutContext(Qt::ApplicationShortcut);
|
2011-01-18 17:03:14 +01:00
|
|
|
a->setCurrentContext(m_context);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return a;
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-06 17:21:03 +01:00
|
|
|
void ActionManagerPrivate::unregisterAction(QAction *action, const Id &id)
|
|
|
|
|
{
|
|
|
|
|
Action *a = 0;
|
|
|
|
|
const int uid = UniqueIDManager::instance()->uniqueIdentifier(id);
|
|
|
|
|
CommandPrivate *c = m_idCmdMap.value(uid, 0);
|
|
|
|
|
QTC_ASSERT(c, return);
|
|
|
|
|
a = qobject_cast<Action *>(c);
|
|
|
|
|
if (!a) {
|
|
|
|
|
qWarning() << "registerAction: id" << id << "is registered with a different command type.";
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
a->removeOverrideAction(action);
|
|
|
|
|
if (a->isEmpty()) {
|
|
|
|
|
// clean up
|
|
|
|
|
m_mainWnd->removeAction(a->action());
|
|
|
|
|
delete a->action();
|
|
|
|
|
m_idCmdMap.remove(uid);
|
|
|
|
|
delete a;
|
|
|
|
|
}
|
|
|
|
|
emit commandListChanged();
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-20 10:35:30 +01:00
|
|
|
Command *ActionManagerPrivate::registerShortcut(QShortcut *shortcut, const Id &id, const Context &context, bool scriptable)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
|
|
|
|
Shortcut *sc = 0;
|
2009-01-26 13:03:38 +01:00
|
|
|
int uid = UniqueIDManager::instance()->uniqueIdentifier(id);
|
2009-01-14 12:58:06 +01:00
|
|
|
if (CommandPrivate *c = m_idCmdMap.value(uid, 0)) {
|
2009-05-06 16:06:43 +02:00
|
|
|
sc = qobject_cast<Shortcut *>(c);
|
|
|
|
|
if (!sc) {
|
2008-12-02 12:01:29 +01:00
|
|
|
qWarning() << "registerShortcut: id" << id << "is registered with a different command type.";
|
|
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
sc = new Shortcut(uid);
|
|
|
|
|
m_idCmdMap.insert(uid, sc);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (sc->shortcut()) {
|
|
|
|
|
qWarning() << "registerShortcut: action already registered (id" << id << ".";
|
|
|
|
|
return sc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!hasContext(context))
|
|
|
|
|
shortcut->setEnabled(false);
|
|
|
|
|
shortcut->setObjectName(id);
|
|
|
|
|
shortcut->setParent(m_mainWnd);
|
|
|
|
|
sc->setShortcut(shortcut);
|
2010-12-20 10:35:30 +01:00
|
|
|
sc->setScriptable(scriptable);
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-06-25 18:05:09 +02:00
|
|
|
if (context.isEmpty())
|
2010-06-25 12:56:16 +02:00
|
|
|
sc->setContext(Context(0));
|
2008-12-02 12:01:29 +01:00
|
|
|
else
|
|
|
|
|
sc->setContext(context);
|
|
|
|
|
|
2010-12-06 17:21:03 +01:00
|
|
|
emit commandListChanged();
|
2010-12-20 10:35:30 +01:00
|
|
|
emit commandAdded(id);
|
2008-12-02 12:01:29 +01:00
|
|
|
return sc;
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-10 14:13:19 +02:00
|
|
|
Command *ActionManagerPrivate::command(const Id &id) const
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2009-01-26 13:03:38 +01:00
|
|
|
const int uid = UniqueIDManager::instance()->uniqueIdentifier(id);
|
2008-12-02 12:01:29 +01:00
|
|
|
const IdCmdMap::const_iterator it = m_idCmdMap.constFind(uid);
|
|
|
|
|
if (it == m_idCmdMap.constEnd()) {
|
|
|
|
|
if (warnAboutFindFailures)
|
2009-01-13 13:07:29 +01:00
|
|
|
qWarning() << "ActionManagerPrivate::command(): failed to find :" << id << '/' << uid;
|
2008-12-02 12:01:29 +01:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
return it.value();
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-10 14:13:19 +02:00
|
|
|
ActionContainer *ActionManagerPrivate::actionContainer(const Id &id) const
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2009-01-26 13:03:38 +01:00
|
|
|
const int uid = UniqueIDManager::instance()->uniqueIdentifier(id);
|
|
|
|
|
const IdContainerMap::const_iterator it = m_idContainerMap.constFind(uid);
|
|
|
|
|
if (it == m_idContainerMap.constEnd()) {
|
2008-12-02 12:01:29 +01:00
|
|
|
if (warnAboutFindFailures)
|
2009-01-13 13:07:29 +01:00
|
|
|
qWarning() << "ActionManagerPrivate::actionContainer(): failed to find :" << id << '/' << uid;
|
2008-12-02 12:01:29 +01:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
return it.value();
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-14 13:17:53 +01:00
|
|
|
Command *ActionManagerPrivate::command(int uid) const
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
|
|
|
|
const IdCmdMap::const_iterator it = m_idCmdMap.constFind(uid);
|
|
|
|
|
if (it == m_idCmdMap.constEnd()) {
|
|
|
|
|
if (warnAboutFindFailures)
|
2009-01-26 13:03:38 +01:00
|
|
|
qWarning() << "ActionManagerPrivate::command(): failed to find :" << UniqueIDManager::instance()->stringForUniqueIdentifier(uid) << '/' << uid;
|
2008-12-02 12:01:29 +01:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
return it.value();
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-14 12:39:59 +01:00
|
|
|
ActionContainer *ActionManagerPrivate::actionContainer(int uid) const
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
|
|
|
|
const IdContainerMap::const_iterator it = m_idContainerMap.constFind(uid);
|
|
|
|
|
if (it == m_idContainerMap.constEnd()) {
|
|
|
|
|
if (warnAboutFindFailures)
|
2009-01-26 13:03:38 +01:00
|
|
|
qWarning() << "ActionManagerPrivate::actionContainer(): failed to find :" << UniqueIDManager::instance()->stringForUniqueIdentifier(uid) << uid;
|
2008-12-02 12:01:29 +01:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
return it.value();
|
|
|
|
|
}
|
2008-12-02 14:09:21 +01:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
static const char *settingsGroup = "KeyBindings";
|
|
|
|
|
static const char *idKey = "ID";
|
|
|
|
|
static const char *sequenceKey = "Keysequence";
|
|
|
|
|
|
2009-01-13 13:07:29 +01:00
|
|
|
void ActionManagerPrivate::initialize()
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2010-01-13 16:51:28 +01:00
|
|
|
QSettings *settings = Core::ICore::instance()->settings();
|
2008-12-02 12:01:29 +01:00
|
|
|
const int shortcuts = settings->beginReadArray(QLatin1String(settingsGroup));
|
|
|
|
|
for (int i=0; i<shortcuts; ++i) {
|
|
|
|
|
settings->setArrayIndex(i);
|
|
|
|
|
const QString sid = settings->value(QLatin1String(idKey)).toString();
|
|
|
|
|
const QKeySequence key(settings->value(QLatin1String(sequenceKey)).toString());
|
2009-01-26 13:03:38 +01:00
|
|
|
const int id = UniqueIDManager::instance()->uniqueIdentifier(sid);
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2009-01-14 13:17:53 +01:00
|
|
|
Command *cmd = command(id);
|
2008-12-02 12:01:29 +01:00
|
|
|
if (cmd)
|
|
|
|
|
cmd->setKeySequence(key);
|
|
|
|
|
}
|
|
|
|
|
settings->endArray();
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-13 13:07:29 +01:00
|
|
|
void ActionManagerPrivate::saveSettings(QSettings *settings)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
|
|
|
|
settings->beginWriteArray(QLatin1String(settingsGroup));
|
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
|
|
const IdCmdMap::const_iterator cmdcend = m_idCmdMap.constEnd();
|
|
|
|
|
for (IdCmdMap::const_iterator j = m_idCmdMap.constBegin(); j != cmdcend; ++j) {
|
|
|
|
|
const int id = j.key();
|
2009-01-14 12:58:06 +01:00
|
|
|
CommandPrivate *cmd = j.value();
|
2008-12-02 12:01:29 +01:00
|
|
|
QKeySequence key = cmd->keySequence();
|
|
|
|
|
if (key != cmd->defaultKeySequence()) {
|
2009-01-26 13:03:38 +01:00
|
|
|
const QString sid = UniqueIDManager::instance()->stringForUniqueIdentifier(id);
|
2008-12-02 12:01:29 +01:00
|
|
|
settings->setArrayIndex(count);
|
|
|
|
|
settings->setValue(QLatin1String(idKey), sid);
|
|
|
|
|
settings->setValue(QLatin1String(sequenceKey), key.toString());
|
|
|
|
|
count++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
settings->endArray();
|
|
|
|
|
}
|