modemanager.cpp 10.6 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2 3 4
**
** This file is part of Qt Creator
**
con's avatar
con committed
5
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
**
hjk's avatar
hjk committed
7
** Contact: Nokia Corporation (info@qt.nokia.com)
con's avatar
con committed
8
**
9
**
10
** GNU Lesser General Public License Usage
11
**
hjk's avatar
hjk committed
12 13 14 15 16 17
** 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.
18
**
con's avatar
con committed
19
** In addition, as a special exception, Nokia gives you certain additional
hjk's avatar
hjk committed
20
** rights. These rights are described in the Nokia Qt LGPL Exception
con's avatar
con committed
21 22
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
23 24 25 26 27
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
con's avatar
con committed
28
** If you have questions regarding the use of this file, please contact
Tobias Hunger's avatar
Tobias Hunger committed
29
** Nokia at info@qt.nokia.com.
con's avatar
con committed
30
**
31
**************************************************************************/
hjk's avatar
hjk committed
32

con's avatar
con committed
33
#include "modemanager.h"
hjk's avatar
hjk committed
34

con's avatar
con committed
35 36
#include "fancytabwidget.h"
#include "fancyactionbar.h"
37
#include "icore.h"
con's avatar
con committed
38 39 40
#include "mainwindow.h"

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

42
#include <coreplugin/actionmanager/actionmanager.h>
con's avatar
con committed
43
#include <coreplugin/actionmanager/command.h>
con's avatar
con committed
44 45 46 47
#include <coreplugin/coreconstants.h>
#include <coreplugin/imode.h>
#include <coreplugin/uniqueidmanager.h>

48 49
#include <extensionsystem/pluginmanager.h>

hjk's avatar
hjk committed
50 51
#include <utils/qtcassert.h>

con's avatar
con committed
52
#include <QtCore/QDebug>
53 54 55 56
#include <QtCore/QList>
#include <QtCore/QMap>
#include <QtCore/QVector>

con's avatar
con committed
57
#include <QtCore/QSignalMapper>
58
#include <QtGui/QShortcut>
con's avatar
con committed
59 60
#include <QtGui/QAction>

61 62
namespace Core {

63 64
struct ModeManagerPrivate
{
65 66 67 68 69 70 71 72
    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;
73
    QMap<QAction*, int> m_actions;
74 75 76
    QVector<IMode*> m_modes;
    QVector<Command*> m_modeShortcuts;
    QSignalMapper *m_signalMapper;
77
    Context m_addedContexts;
78 79 80 81 82 83 84 85
    int m_oldCurrent;
};

ModeManager *ModeManagerPrivate::m_instance = 0;

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

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

99 100
    d->m_actionBar = new Internal::FancyActionBar(modeStack);
    d->m_modeStack->addCornerWidget(d->m_actionBar);
con's avatar
con committed
101

102 103 104
    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
105 106 107 108 109 110 111 112 113 114
}

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

115 116 117 118 119 120
ModeManager::~ModeManager()
{
    delete d;
    ModeManagerPrivate::m_instance = 0;
}

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

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

int ModeManager::indexOf(const QString &id) const
{
139 140
    for (int i = 0; i < d->m_modes.count(); ++i) {
        if (d->m_modes.at(i)->id() == id)
con's avatar
con committed
141 142 143 144 145 146 147 148 149 150
            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)
151
        return d->m_modes.at(index);
con's avatar
con committed
152 153 154
    return 0;
}

155 156
void ModeManager::activateModeType(const QString &type)
{
157 158
    if (currentMode() && currentMode()->type() == type)
        return;
159 160 161 162 163 164 165 166 167 168 169
    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
170 171 172 173
void ModeManager::activateMode(const QString &id)
{
    const int index = indexOf(id);
    if (index >= 0)
174
        d->m_modeStack->setCurrentIndex(index);
con's avatar
con committed
175 176 177 178 179 180 181 182
}

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

183
    d->m_mainWindow->addContextObject(mode);
con's avatar
con committed
184 185 186

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

191 192 193
    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
194 195

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

202
    d->m_modeShortcuts.insert(index, cmd);
con's avatar
con committed
203
    connect(cmd, SIGNAL(keySequenceChanged()), this, SLOT(updateModeToolTip()));
204 205
    for (int i = 0; i < d->m_modeShortcuts.size(); ++i) {
        Command *currentCmd = d->m_modeShortcuts.at(i);
206 207 208
        // we need this hack with currentlyHasDefaultSequence
        // because we call setDefaultShortcut multiple times on the same cmd
        // and still expect the current shortcut to change with it
con's avatar
con committed
209 210
        bool currentlyHasDefaultSequence = (currentCmd->keySequence()
                                            == currentCmd->defaultKeySequence());
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
211
#ifdef Q_WS_MAC
con's avatar
con committed
212
        currentCmd->setDefaultKeySequence(QKeySequence(QString("Meta+%1").arg(i+1)));
con's avatar
con committed
213
#else
con's avatar
con committed
214
        currentCmd->setDefaultKeySequence(QKeySequence(QString("Ctrl+%1").arg(i+1)));
con's avatar
con committed
215
#endif
con's avatar
con committed
216 217
        if (currentlyHasDefaultSequence)
            currentCmd->setKeySequence(currentCmd->defaultKeySequence());
con's avatar
con committed
218 219
    }

220 221
    d->m_signalMapper->setMapping(shortcut, mode->id());
    connect(shortcut, SIGNAL(activated()), d->m_signalMapper, SLOT(map()));
con's avatar
con committed
222 223
    connect(mode, SIGNAL(enabledStateChanged(bool)),
            this, SLOT(enabledStateChanged()));
con's avatar
con committed
224 225 226 227
}

void ModeManager::updateModeToolTip()
{
con's avatar
con committed
228
    Command *cmd = qobject_cast<Command *>(sender());
con's avatar
con committed
229
    if (cmd) {
230
        int index = d->m_modeShortcuts.indexOf(cmd);
con's avatar
con committed
231
        if (index != -1)
232
            d->m_modeStack->setTabToolTip(index, cmd->stringWithAppendedShortcut(cmd->shortcut()->whatsThis()));
con's avatar
con committed
233 234 235
    }
}

con's avatar
con committed
236 237 238 239
void ModeManager::enabledStateChanged()
{
    IMode *mode = qobject_cast<IMode *>(sender());
    QTC_ASSERT(mode, return);
240
    int index = d->m_modes.indexOf(mode);
con's avatar
con committed
241
    QTC_ASSERT(index >= 0, return);
242
    d->m_modeStack->setTabEnabled(index, mode->isEnabled());
243 244 245 246 247 248 249 250 251 252 253 254

    // 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
255 256
}

con's avatar
con committed
257 258 259 260 261 262
void ModeManager::aboutToRemoveObject(QObject *obj)
{
    IMode *mode = Aggregation::query<IMode>(obj);
    if (!mode)
        return;

263 264 265 266
    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
267

268
    d->m_mainWindow->removeContextObject(mode);
con's avatar
con committed
269 270
}

271
void ModeManager::addAction(QAction *action, int priority)
con's avatar
con committed
272
{
273
    d->m_actions.insert(action, priority);
con's avatar
con committed
274 275 276

    // Count the number of commands with a higher priority
    int index = 0;
277
    foreach (int p, d->m_actions) {
con's avatar
con committed
278 279
        if (p > priority)
            ++index;
con's avatar
con committed
280
    }
con's avatar
con committed
281

282
    d->m_actionBar->insertAction(index, action);
con's avatar
con committed
283 284
}

285 286
void ModeManager::addProjectSelector(QAction *action)
{
287
    d->m_actionBar->addProjectSelector(action);
288
    d->m_actions.insert(0, INT_MAX);
289 290
}

con's avatar
con committed
291 292 293
void ModeManager::currentTabAboutToChange(int index)
{
    if (index >= 0) {
294
        IMode *mode = d->m_modes.at(index);
con's avatar
con committed
295 296 297 298 299 300 301 302 303
        if (mode)
            emit currentModeAboutToChange(mode);
    }
}

void ModeManager::currentTabChanged(int index)
{
    // Tab index changes to -1 when there is no tab left.
    if (index >= 0) {
304
        IMode *mode = d->m_modes.at(index);
con's avatar
con committed
305 306 307 308

        // 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.
309
        ICore::instance()->updateAdditionalContexts(d->m_addedContexts, mode->context());
310
        d->m_addedContexts = mode->context();
311

312
        IMode *oldMode = 0;
313 314 315
        if (d->m_oldCurrent >= 0)
            oldMode = d->m_modes.at(d->m_oldCurrent);
        d->m_oldCurrent = index;
316
        emit currentModeChanged(mode, oldMode);
con's avatar
con committed
317 318 319 320 321 322
    }
}

void ModeManager::setFocusToCurrentMode()
{
    IMode *mode = currentMode();
hjk's avatar
hjk committed
323
    QTC_ASSERT(mode, return);
con's avatar
con committed
324 325 326 327 328 329 330 331 332
    QWidget *widget = mode->widget();
    if (widget) {
        QWidget *focusWidget = widget->focusWidget();
        if (focusWidget)
            focusWidget->setFocus();
        else
            widget->setFocus();
    }
}
333

334 335 336 337 338
void ModeManager::setModeBarHidden(bool hidden)
{
    d->m_modeStack->setSelectionWidgetHidden(hidden);
}

339 340 341 342 343 344
ModeManager *ModeManager::instance()
{
    return ModeManagerPrivate::m_instance;
}

} // namespace Core