46 #include <QtCore/QObject>
47 #include <QtCore/QDebug>
48 #include <QtCore/QSignalMapper>
53 #include <QVBoxLayout>
56 using namespace Core::Internal;
61 : m_mainWindow(mainWindow)
62 , m_modeStack(modeStack)
63 , m_signalMapper(new QSignalMapper(this))
64 , m_isReprioritizing(false)
70 connect(m_modeStack, SIGNAL(currentChanged(
int)),
this, SLOT(currentTabChanged(
int)));
71 connect(m_modeStack, SIGNAL(tabMoved(
int,
int)),
this, SLOT(tabMoved(
int,
int)));
72 connect(m_signalMapper, SIGNAL(mapped(QString)),
this, SLOT(
activateMode(QString)));
77 QObject::connect(ExtensionSystem::PluginManager::instance(), SIGNAL(objectAdded(QObject *)),
78 this, SLOT(objectAdded(QObject *)));
79 QObject::connect(ExtensionSystem::PluginManager::instance(),
80 SIGNAL(aboutToRemoveObject(QObject *)),
this,
81 SLOT(aboutToRemoveObject(QObject *)));
96 if (m_modes.count() > m_modeStack->currentIndex())
97 return m_modes.at(m_modeStack->currentIndex());
99 m_modeStack->setCurrentIndex(0);
103 int ModeManager::indexOf(
const QString &
id)
const
105 for (
int i = 0;
i < m_modes.count(); ++
i) {
106 if (m_modes.at(
i)->uniqueModeName() == id)
115 const int index = indexOf(
id);
117 return m_modes.at(index);
123 const int index = indexOf(
id);
125 m_modeStack->setCurrentIndex(index);
130 for (
int i = 0;
i < m_modes.count(); ++
i) {
131 if (m_modes.at(
i)->name() == id) {
132 m_modeStack->setCurrentIndex(
i);
138 void ModeManager::objectAdded(QObject *obj)
140 IMode *
mode = Aggregation::query<IMode>(obj);
148 foreach (
const IMode *
m, m_modes)
152 m_modes.insert(index, mode);
153 m_modeStack->insertTab(index, mode->
widget(), mode->
icon(), mode->
name());
157 const QString shortcutId = QLatin1String(
"GCS.Mode.") + mode->
uniqueModeName();
158 QShortcut *shortcut =
new QShortcut(m_mainWindow);
159 shortcut->setWhatsThis(tr(
"Switch to %1 mode").arg(mode->
name()));
163 m_modeShortcuts.insert(index, cmd);
164 connect(cmd, SIGNAL(keySequenceChanged()),
this, SLOT(updateModeToolTip()));
166 setDefaultKeyshortcuts();
169 connect(shortcut, SIGNAL(activated()), m_signalMapper, SLOT(map()));
174 void ModeManager::setDefaultKeyshortcuts()
176 for (
int i = 0;
i < m_modeShortcuts.size(); ++
i) {
177 Command *currentCmd = m_modeShortcuts.at(
i);
178 bool currentlyHasDefaultSequence =
185 if (currentlyHasDefaultSequence)
190 void ModeManager::updateModeToolTip()
194 int index = m_modeShortcuts.indexOf(cmd);
196 m_modeStack->setTabToolTip(
206 m_modeStack->setTabIcon(index, icon);
207 m_modeStack->setTabText(index, label);
210 void ModeManager::aboutToRemoveObject(QObject *obj)
212 IMode *mode = Aggregation::query<IMode>(obj);
216 const int index = m_modes.indexOf(mode);
217 m_modes.remove(index);
218 m_modeShortcuts.remove(index);
219 disconnect(m_modeStack, SIGNAL(currentChanged(
int)),
this, SLOT(currentTabChanged(
int)));
220 m_modeStack->removeTab(index);
221 connect(m_modeStack, SIGNAL(currentChanged(
int)),
this, SLOT(currentTabChanged(
int)));
232 m_actions.insert(command, priority);
236 foreach (
int p, m_actions.values())
243 void ModeManager::currentTabAboutToChange(
int index)
246 IMode *mode = m_modes.at(index);
252 void ModeManager::currentTabChanged(
int index)
257 IMode *mode = m_modes.at(index);
263 foreach (
const int context, m_addedContexts)
266 m_addedContexts = mode->
context();
267 foreach (
const int context, m_addedContexts)
274 void ModeManager::tabMoved(
int from,
int to)
276 IMode *mode = m_modes.at(from);
277 m_modes.remove(from);
278 m_modes.insert(to, mode);
279 Command *cmd = m_modeShortcuts.at(from);
280 m_modeShortcuts.remove(from);
281 m_modeShortcuts.insert(to, cmd);
282 setDefaultKeyshortcuts();
284 if (!m_isReprioritizing) {
285 for (
int i = 0;
i < m_modes.count(); ++
i) {
286 m_modes.at(
i)->setPriority(100 -
i);
294 foreach (
IMode *mode, m_modes)
298 m_isReprioritizing =
true;
301 bool swapped =
false;
304 for (
int i = 0;
i < m_modes.count() - 1; ++
i) {
305 IMode *mode1 = m_modes.at(
i);
306 IMode *mode2 = m_modes.at(
i + 1);
316 m_isReprioritizing =
false;
317 m_modeStack->setCurrentIndex(0);
325 QTC_ASSERT(mode,
return );
326 QWidget *widget = mode->
widget();
328 QWidget *focusWidget = widget->focusWidget();
330 focusWidget->setFocus();
338 foreach (
Command *command, m_actions.keys()) {
339 if (command->
action()->objectName() == actionId) {
340 command->
action()->trigger();
void setFocusToCurrentMode()
virtual QString stringWithAppendedShortcut(const QString &str) const =0
virtual QIcon icon() const =0
ModeManager(Internal::MainWindow *mainWindow, MyTabWidget *modeStack)
virtual int priority() const =0
virtual QAction * action() const =0
void currentModeAboutToChange(Core::IMode *mode)
The ICore class allows access to the different part that make up the basic functionality of the GCS...
IMode * mode(const QString &id) const
virtual QKeySequence keySequence() const =0
IMode * currentMode() const
void addAction(Command *command, int priority, QMenu *menu=nullptr)
virtual QString name() const =0
void addWidget(QWidget *widget)
virtual const char * uniqueModeName() const =0
virtual void setPriority(int priority)=0
void activateModeByWorkspaceName(const QString &id)
static ICore * instance()
void updateModeNameIcon(IMode *mode, const QIcon &icon, const QString &label)
virtual void addAdditionalContext(int context)=0
Register additional context to be currently active.
virtual void updateContext()=0
Update the list of active contexts after adding or removing additional ones.
virtual QShortcut * shortcut() const =0
virtual void setKeySequence(const QKeySequence &key)=0
virtual void setDefaultKeySequence(const QKeySequence &key)=0
The action manager is responsible for registration of menus and menu items and keyboard shortcuts...
virtual QList< int > context() const =0
virtual Command * registerShortcut(QShortcut *shortcut, const QString &id, const QList< int > &context)=0
Makes a shortcut known to the system under the specified string id.
virtual QKeySequence defaultKeySequence() const =0
virtual void removeAdditionalContext(int context)=0
Removes the given context from the list of currently active contexts.
virtual QWidget * widget()=0
void activateMode(const QString &id)
void currentModeChanged(Core::IMode *mode)
void newModeOrder(QVector< IMode * > modes)
void reorderModes(QMap< QString, int > priorities)
void addContextObject(IContext *contex)
void removeContextObject(IContext *contex)
The class Command represents an action like a menu item, tool button, or shortcut. You don't create Command objects directly, instead use {ActionManager::registerAction()} 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.
void triggerAction(const QString &actionId)
Core::ActionManager * actionManager() const