modemanager.cpp 8.4 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 50 51
#include <QtCore/QObject>
#include <QtCore/QDebug>
#include <QtCore/QSignalMapper>
52
#include <QtGui/QShortcut>
hjk's avatar
hjk committed
53

con's avatar
con committed
54 55 56 57 58 59 60 61 62
#include <QtGui/QAction>
#include <QtGui/QTabWidget>
#include <QtGui/QVBoxLayout>

using namespace Core;
using namespace Core::Internal;

ModeManager *ModeManager::m_instance = 0;

hjk's avatar
hjk committed
63
ModeManager::ModeManager(Internal::MainWindow *mainWindow, FancyTabWidget *modeStack) :
con's avatar
con committed
64 65
    m_mainWindow(mainWindow),
    m_modeStack(modeStack),
66 67 68
    m_signalMapper(new QSignalMapper(this)),
    m_oldCurrent(-1)

con's avatar
con committed
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
{
    m_instance = this;

    m_actionBar = new FancyActionBar(modeStack);
    m_modeStack->addCornerWidget(m_actionBar);

    connect(m_modeStack, SIGNAL(currentAboutToShow(int)), SLOT(currentTabAboutToChange(int)));
    connect(m_modeStack, SIGNAL(currentChanged(int)), SLOT(currentTabChanged(int)));
    connect(m_signalMapper, SIGNAL(mapped(QString)), this, SLOT(activateMode(QString)));
}

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*)));
}

void ModeManager::addWidget(QWidget *widget)
{
    // We want the actionbar to stay on the bottom
    // so m_modeStack->cornerWidgetCount() -1 inserts it at the position immediately above
    // the actionbar
    m_modeStack->insertCornerWidget(m_modeStack->cornerWidgetCount() -1, widget);
}

IMode *ModeManager::currentMode() const
{
con's avatar
con committed
98 99 100 101
    int currentIndex = m_modeStack->currentIndex();
    if (currentIndex < 0)
        return 0;
    return m_modes.at(currentIndex);
con's avatar
con committed
102 103 104 105 106
}

int ModeManager::indexOf(const QString &id) const
{
    for (int i = 0; i < m_modes.count(); ++i) {
107
        if (m_modes.at(i)->id() == id)
con's avatar
con committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
            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)
        return m_modes.at(index);
    return 0;
}

void ModeManager::activateMode(const QString &id)
{
    const int index = indexOf(id);
    if (index >= 0)
        m_modeStack->setCurrentIndex(index);
}

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

    m_mainWindow->addContextObject(mode);

    // Count the number of modes with a higher priority
    int index = 0;
    foreach (const IMode *m, m_modes)
        if (m->priority() > mode->priority())
            ++index;

    m_modes.insert(index, mode);
144
    m_modeStack->insertTab(index, mode->widget(), mode->icon(), mode->displayName());
con's avatar
con committed
145
    m_modeStack->setTabEnabled(index, mode->isEnabled());
con's avatar
con committed
146 147

    // Register mode shortcut
148
    ActionManager *am = m_mainWindow->actionManager();
149
    const QString shortcutId = QLatin1String("QtCreator.Mode.") + mode->id();
con's avatar
con committed
150
    QShortcut *shortcut = new QShortcut(m_mainWindow);
151
    shortcut->setWhatsThis(tr("Switch to %1 mode").arg(mode->displayName()));
con's avatar
con committed
152
    Command *cmd = am->registerShortcut(shortcut, shortcutId, QList<int>() << Constants::C_GLOBAL_ID);
con's avatar
con committed
153 154 155 156

    m_modeShortcuts.insert(index, cmd);
    connect(cmd, SIGNAL(keySequenceChanged()), this, SLOT(updateModeToolTip()));
    for (int i = 0; i < m_modeShortcuts.size(); ++i) {
con's avatar
con committed
157 158 159
        Command *currentCmd = m_modeShortcuts.at(i);
        bool currentlyHasDefaultSequence = (currentCmd->keySequence()
                                            == currentCmd->defaultKeySequence());
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
160
#ifdef Q_WS_MAC
con's avatar
con committed
161
        currentCmd->setDefaultKeySequence(QKeySequence(QString("Meta+%1").arg(i+1)));
con's avatar
con committed
162
#else
con's avatar
con committed
163
        currentCmd->setDefaultKeySequence(QKeySequence(QString("Ctrl+%1").arg(i+1)));
con's avatar
con committed
164
#endif
con's avatar
con committed
165 166
        if (currentlyHasDefaultSequence)
            currentCmd->setKeySequence(currentCmd->defaultKeySequence());
con's avatar
con committed
167 168
    }

169
    m_signalMapper->setMapping(shortcut, mode->id());
con's avatar
con committed
170
    connect(shortcut, SIGNAL(activated()), m_signalMapper, SLOT(map()));
con's avatar
con committed
171 172
    connect(mode, SIGNAL(enabledStateChanged(bool)),
            this, SLOT(enabledStateChanged()));
con's avatar
con committed
173 174 175 176
}

void ModeManager::updateModeToolTip()
{
con's avatar
con committed
177
    Command *cmd = qobject_cast<Command *>(sender());
con's avatar
con committed
178 179 180 181 182 183 184
    if (cmd) {
        int index = m_modeShortcuts.indexOf(cmd);
        if (index != -1)
            m_modeStack->setTabToolTip(index, cmd->stringWithAppendedShortcut(cmd->shortcut()->whatsThis()));
    }
}

con's avatar
con committed
185 186 187 188 189 190 191 192 193
void ModeManager::enabledStateChanged()
{
    IMode *mode = qobject_cast<IMode *>(sender());
    QTC_ASSERT(mode, return);
    int index = m_modes.indexOf(mode);
    QTC_ASSERT(index >= 0, return);
    m_modeStack->setTabEnabled(index, mode->isEnabled());
}

con's avatar
con committed
194 195 196 197 198 199 200 201 202 203 204 205 206 207
void ModeManager::aboutToRemoveObject(QObject *obj)
{
    IMode *mode = Aggregation::query<IMode>(obj);
    if (!mode)
        return;

    const int index = m_modes.indexOf(mode);
    m_modes.remove(index);
    m_modeShortcuts.remove(index);
    m_modeStack->removeTab(index);

    m_mainWindow->removeContextObject(mode);
}

208
void ModeManager::addAction(Command *command, int priority)
con's avatar
con committed
209 210 211 212 213
{
    m_actions.insert(command, priority);

    // Count the number of commands with a higher priority
    int index = 0;
con's avatar
con committed
214
    foreach (int p, m_actions) {
con's avatar
con committed
215 216
        if (p > priority)
            ++index;
con's avatar
con committed
217
    }
con's avatar
con committed
218

219
    m_actionBar->insertAction(index, command->action());
con's avatar
con committed
220 221
}

222 223 224 225 226
void ModeManager::addProjectSelector(QAction *action)
{
    m_actionBar->addProjectSelector(action);
}

con's avatar
con committed
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
void ModeManager::currentTabAboutToChange(int index)
{
    if (index >= 0) {
        IMode *mode = m_modes.at(index);
        if (mode)
            emit currentModeAboutToChange(mode);
    }
}

void ModeManager::currentTabChanged(int index)
{
    // Tab index changes to -1 when there is no tab left.
    if (index >= 0) {
        IMode *mode = m_modes.at(index);

        // 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.
245
        ICore *core = ICore::instance();
con's avatar
con committed
246 247 248 249 250 251
        foreach (const int context, m_addedContexts)
            core->removeAdditionalContext(context);

        m_addedContexts = mode->context();
        foreach (const int context, m_addedContexts)
            core->addAdditionalContext(context);
252 253 254 255 256
        IMode *oldMode = 0;
        if (m_oldCurrent >= 0)
            oldMode = m_modes.at(m_oldCurrent);
        m_oldCurrent = index;
        emit currentModeChanged(mode, oldMode);
con's avatar
con committed
257 258 259 260 261 262 263
        core->updateContext();
    }
}

void ModeManager::setFocusToCurrentMode()
{
    IMode *mode = currentMode();
hjk's avatar
hjk committed
264
    QTC_ASSERT(mode, return);
con's avatar
con committed
265 266 267 268 269 270 271 272 273
    QWidget *widget = mode->widget();
    if (widget) {
        QWidget *focusWidget = widget->focusWidget();
        if (focusWidget)
            focusWidget->setFocus();
        else
            widget->setFocus();
    }
}