modemanager.cpp 10.1 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2 3 4
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
con's avatar
con committed
8
**
9
** Commercial Usage
10
**
11 12 13 14
** 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.
15
**
16
** GNU Lesser General Public License Usage
17
**
18 19 20 21 22 23
** 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.
24
**
25
** If you are unsure which license is appropriate for your use, please
hjk's avatar
hjk committed
26
** contact the sales department at http://qt.nokia.com/contact.
con's avatar
con committed
27
**
28
**************************************************************************/
hjk's avatar
hjk committed
29

con's avatar
con committed
30
#include "modemanager.h"
hjk's avatar
hjk committed
31

con's avatar
con committed
32 33
#include "fancytabwidget.h"
#include "fancyactionbar.h"
34
#include "icore.h"
con's avatar
con committed
35 36 37
#include "mainwindow.h"

#include <aggregation/aggregate.h>
hjk's avatar
hjk committed
38

39
#include <coreplugin/actionmanager/actionmanager.h>
con's avatar
con committed
40
#include <coreplugin/actionmanager/command.h>
con's avatar
con committed
41 42 43 44
#include <coreplugin/coreconstants.h>
#include <coreplugin/imode.h>
#include <coreplugin/uniqueidmanager.h>

45 46
#include <extensionsystem/pluginmanager.h>

hjk's avatar
hjk committed
47 48
#include <utils/qtcassert.h>

con's avatar
con committed
49
#include <QtCore/QDebug>
50 51 52 53
#include <QtCore/QList>
#include <QtCore/QMap>
#include <QtCore/QVector>

con's avatar
con committed
54
#include <QtCore/QSignalMapper>
55
#include <QtGui/QShortcut>
con's avatar
con committed
56 57
#include <QtGui/QAction>

58 59
namespace Core {

60 61
struct ModeManagerPrivate
{
62 63 64 65 66 67 68 69 70 71 72 73
    explicit ModeManagerPrivate(Internal::MainWindow *mainWindow,
                                Internal::FancyTabWidget *modeStack,
                                ModeManager *q);

    static ModeManager *m_instance;
    Internal::MainWindow *m_mainWindow;
    Internal::FancyTabWidget *m_modeStack;
    Internal::FancyActionBar *m_actionBar;
    QMap<Command*, int> m_actions;
    QVector<IMode*> m_modes;
    QVector<Command*> m_modeShortcuts;
    QSignalMapper *m_signalMapper;
74
    Context m_addedContexts;
75 76 77 78 79 80 81 82
    int m_oldCurrent;
};

ModeManager *ModeManagerPrivate::m_instance = 0;

ModeManagerPrivate::ModeManagerPrivate(Internal::MainWindow *mainWindow,
                                       Internal::FancyTabWidget *modeStack,
                                       ModeManager *q) :
con's avatar
con committed
83 84
    m_mainWindow(mainWindow),
    m_modeStack(modeStack),
85
    m_signalMapper(new QSignalMapper(q)),
86
    m_oldCurrent(-1)
87 88
{
}
89

90 91 92
ModeManager::ModeManager(Internal::MainWindow *mainWindow,
                         Internal::FancyTabWidget *modeStack) :
        d(new ModeManagerPrivate(mainWindow, modeStack, this))
con's avatar
con committed
93
{
94
    ModeManagerPrivate::m_instance = this;
con's avatar
con committed
95

96 97
    d->m_actionBar = new Internal::FancyActionBar(modeStack);
    d->m_modeStack->addCornerWidget(d->m_actionBar);
con's avatar
con committed
98

99 100 101
    connect(d->m_modeStack, SIGNAL(currentAboutToShow(int)), SLOT(currentTabAboutToChange(int)));
    connect(d->m_modeStack, SIGNAL(currentChanged(int)), SLOT(currentTabChanged(int)));
    connect(d->m_signalMapper, SIGNAL(mapped(QString)), this, SLOT(activateMode(QString)));
con's avatar
con committed
102 103 104 105 106 107 108 109 110 111
}

void ModeManager::init()
{
    QObject::connect(ExtensionSystem::PluginManager::instance(), SIGNAL(objectAdded(QObject*)),
                     this, SLOT(objectAdded(QObject*)));
    QObject::connect(ExtensionSystem::PluginManager::instance(), SIGNAL(aboutToRemoveObject(QObject*)),
                     this, SLOT(aboutToRemoveObject(QObject*)));
}

112 113 114 115 116 117
ModeManager::~ModeManager()
{
    delete d;
    ModeManagerPrivate::m_instance = 0;
}

con's avatar
con committed
118 119 120
void ModeManager::addWidget(QWidget *widget)
{
    // We want the actionbar to stay on the bottom
121
    // so d->m_modeStack->cornerWidgetCount() -1 inserts it at the position immediately above
con's avatar
con committed
122
    // the actionbar
123
    d->m_modeStack->insertCornerWidget(d->m_modeStack->cornerWidgetCount() -1, widget);
con's avatar
con committed
124 125 126 127
}

IMode *ModeManager::currentMode() const
{
128
    int currentIndex = d->m_modeStack->currentIndex();
con's avatar
con committed
129 130
    if (currentIndex < 0)
        return 0;
131
    return d->m_modes.at(currentIndex);
con's avatar
con committed
132 133 134 135
}

int ModeManager::indexOf(const QString &id) const
{
136 137
    for (int i = 0; i < d->m_modes.count(); ++i) {
        if (d->m_modes.at(i)->id() == id)
con's avatar
con committed
138 139 140 141 142 143 144 145 146 147
            return i;
    }
    qDebug() << "Warning, no such mode:" << id;
    return -1;
}

IMode *ModeManager::mode(const QString &id) const
{
    const int index = indexOf(id);
    if (index >= 0)
148
        return d->m_modes.at(index);
con's avatar
con committed
149 150 151
    return 0;
}

152 153 154 155 156 157 158 159 160 161 162 163 164
void ModeManager::activateModeType(const QString &type)
{
    int index = -1;
    for (int i = 0; i < d->m_modes.count(); ++i) {
        if (d->m_modes.at(i)->type() == type) {
            index = i;
            break;
        }
    }
    if (index != -1)
        d->m_modeStack->setCurrentIndex(index);
}

con's avatar
con committed
165 166 167 168
void ModeManager::activateMode(const QString &id)
{
    const int index = indexOf(id);
    if (index >= 0)
169
        d->m_modeStack->setCurrentIndex(index);
con's avatar
con committed
170 171 172 173 174 175 176 177
}

void ModeManager::objectAdded(QObject *obj)
{
    IMode *mode = Aggregation::query<IMode>(obj);
    if (!mode)
        return;

178
    d->m_mainWindow->addContextObject(mode);
con's avatar
con committed
179 180 181

    // Count the number of modes with a higher priority
    int index = 0;
182
    foreach (const IMode *m, d->m_modes)
con's avatar
con committed
183 184 185
        if (m->priority() > mode->priority())
            ++index;

186 187 188
    d->m_modes.insert(index, mode);
    d->m_modeStack->insertTab(index, mode->widget(), mode->icon(), mode->displayName());
    d->m_modeStack->setTabEnabled(index, mode->isEnabled());
con's avatar
con committed
189 190

    // Register mode shortcut
191
    ActionManager *am = d->m_mainWindow->actionManager();
192
    const QString shortcutId = QLatin1String("QtCreator.Mode.") + mode->id();
193
    QShortcut *shortcut = new QShortcut(d->m_mainWindow);
194
    shortcut->setWhatsThis(tr("Switch to <b>%1</b> mode").arg(mode->displayName()));
195
    Command *cmd = am->registerShortcut(shortcut, shortcutId, Context(Constants::C_GLOBAL));
con's avatar
con committed
196

197
    d->m_modeShortcuts.insert(index, cmd);
con's avatar
con committed
198
    connect(cmd, SIGNAL(keySequenceChanged()), this, SLOT(updateModeToolTip()));
199 200
    for (int i = 0; i < d->m_modeShortcuts.size(); ++i) {
        Command *currentCmd = d->m_modeShortcuts.at(i);
con's avatar
con committed
201 202
        bool currentlyHasDefaultSequence = (currentCmd->keySequence()
                                            == currentCmd->defaultKeySequence());
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
203
#ifdef Q_WS_MAC
con's avatar
con committed
204
        currentCmd->setDefaultKeySequence(QKeySequence(QString("Meta+%1").arg(i+1)));
con's avatar
con committed
205
#else
con's avatar
con committed
206
        currentCmd->setDefaultKeySequence(QKeySequence(QString("Ctrl+%1").arg(i+1)));
con's avatar
con committed
207
#endif
con's avatar
con committed
208 209
        if (currentlyHasDefaultSequence)
            currentCmd->setKeySequence(currentCmd->defaultKeySequence());
con's avatar
con committed
210 211
    }

212 213
    d->m_signalMapper->setMapping(shortcut, mode->id());
    connect(shortcut, SIGNAL(activated()), d->m_signalMapper, SLOT(map()));
con's avatar
con committed
214 215
    connect(mode, SIGNAL(enabledStateChanged(bool)),
            this, SLOT(enabledStateChanged()));
con's avatar
con committed
216 217 218 219
}

void ModeManager::updateModeToolTip()
{
con's avatar
con committed
220
    Command *cmd = qobject_cast<Command *>(sender());
con's avatar
con committed
221
    if (cmd) {
222
        int index = d->m_modeShortcuts.indexOf(cmd);
con's avatar
con committed
223
        if (index != -1)
224
            d->m_modeStack->setTabToolTip(index, cmd->stringWithAppendedShortcut(cmd->shortcut()->whatsThis()));
con's avatar
con committed
225 226 227
    }
}

con's avatar
con committed
228 229 230 231
void ModeManager::enabledStateChanged()
{
    IMode *mode = qobject_cast<IMode *>(sender());
    QTC_ASSERT(mode, return);
232
    int index = d->m_modes.indexOf(mode);
con's avatar
con committed
233
    QTC_ASSERT(index >= 0, return);
234
    d->m_modeStack->setTabEnabled(index, mode->isEnabled());
235 236 237 238 239 240 241 242 243 244 245 246

    // Make sure we leave any disabled mode to prevent possible crashes:
    if (mode == currentMode() && !mode->isEnabled()) {
        // This assumes that there is always at least one enabled mode.
        for (int i = 0; i < d->m_modes.count(); ++i) {
            if (d->m_modes.at(i) != mode &&
                d->m_modes.at(i)->isEnabled()) {
                activateMode(d->m_modes.at(i)->id());
                break;
            }
        }
    }
con's avatar
con committed
247 248
}

con's avatar
con committed
249 250 251 252 253 254
void ModeManager::aboutToRemoveObject(QObject *obj)
{
    IMode *mode = Aggregation::query<IMode>(obj);
    if (!mode)
        return;

255 256 257 258
    const int index = d->m_modes.indexOf(mode);
    d->m_modes.remove(index);
    d->m_modeShortcuts.remove(index);
    d->m_modeStack->removeTab(index);
con's avatar
con committed
259

260
    d->m_mainWindow->removeContextObject(mode);
con's avatar
con committed
261 262
}

263
void ModeManager::addAction(Command *command, int priority)
con's avatar
con committed
264
{
265
    d->m_actions.insert(command, priority);
con's avatar
con committed
266 267 268

    // Count the number of commands with a higher priority
    int index = 0;
269
    foreach (int p, d->m_actions) {
con's avatar
con committed
270 271
        if (p > priority)
            ++index;
con's avatar
con committed
272
    }
con's avatar
con committed
273

274
    d->m_actionBar->insertAction(index, command->action());
con's avatar
con committed
275 276
}

277 278
void ModeManager::addProjectSelector(QAction *action)
{
279
    d->m_actionBar->addProjectSelector(action);
280 281
}

con's avatar
con committed
282 283 284
void ModeManager::currentTabAboutToChange(int index)
{
    if (index >= 0) {
285
        IMode *mode = d->m_modes.at(index);
con's avatar
con committed
286 287 288 289 290 291 292 293 294
        if (mode)
            emit currentModeAboutToChange(mode);
    }
}

void ModeManager::currentTabChanged(int index)
{
    // Tab index changes to -1 when there is no tab left.
    if (index >= 0) {
295
        IMode *mode = d->m_modes.at(index);
con's avatar
con committed
296 297 298 299

        // FIXME: This hardcoded context update is required for the Debug and Edit modes, since
        // they use the editor widget, which is already a context widget so the main window won't
        // go further up the parent tree to find the mode context.
300
        ICore::instance()->updateAdditionalContexts(d->m_addedContexts, mode->context());
301
        d->m_addedContexts = mode->context();
302

303
        IMode *oldMode = 0;
304 305 306
        if (d->m_oldCurrent >= 0)
            oldMode = d->m_modes.at(d->m_oldCurrent);
        d->m_oldCurrent = index;
307
        emit currentModeChanged(mode, oldMode);
con's avatar
con committed
308 309 310 311 312 313
    }
}

void ModeManager::setFocusToCurrentMode()
{
    IMode *mode = currentMode();
hjk's avatar
hjk committed
314
    QTC_ASSERT(mode, return);
con's avatar
con committed
315 316 317 318 319 320 321 322 323
    QWidget *widget = mode->widget();
    if (widget) {
        QWidget *focusWidget = widget->focusWidget();
        if (focusWidget)
            focusWidget->setFocus();
        else
            widget->setFocus();
    }
}
324 325 326 327 328 329 330

ModeManager *ModeManager::instance()
{
    return ModeManagerPrivate::m_instance;
}

} // namespace Core