void ModeManager::objectAdded(QObject *obj) { IMode *mode = Aggregation::query<IMode>(obj); if (!mode) return; d->m_mainWindow->addContextObject(mode); // Count the number of modes with a higher priority int index = 0; foreach (const IMode *m, d->m_modes) if (m->priority() > mode->priority()) ++index; d->m_modes.insert(index, mode); d->m_modeStack->insertTab(index, mode->widget(), mode->icon(), mode->displayName()); d->m_modeStack->setTabEnabled(index, mode->isEnabled()); // Register mode shortcut ActionManager *am = d->m_mainWindow->actionManager(); const QString shortcutId = QLatin1String("QtCreator.Mode.") + mode->id(); QShortcut *shortcut = new QShortcut(d->m_mainWindow); shortcut->setWhatsThis(tr("Switch to <b>%1</b> mode").arg(mode->displayName())); Command *cmd = am->registerShortcut(shortcut, shortcutId, Context(Constants::C_GLOBAL)); d->m_modeShortcuts.insert(index, cmd); connect(cmd, SIGNAL(keySequenceChanged()), this, SLOT(updateModeToolTip())); for (int i = 0; i < d->m_modeShortcuts.size(); ++i) { Command *currentCmd = d->m_modeShortcuts.at(i); // 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 bool currentlyHasDefaultSequence = (currentCmd->keySequence() == currentCmd->defaultKeySequence()); #ifdef Q_WS_MAC currentCmd->setDefaultKeySequence(QKeySequence(QString("Meta+%1").arg(i+1))); #else currentCmd->setDefaultKeySequence(QKeySequence(QString("Ctrl+%1").arg(i+1))); #endif if (currentlyHasDefaultSequence) currentCmd->setKeySequence(currentCmd->defaultKeySequence()); } d->m_signalMapper->setMapping(shortcut, mode->id()); connect(shortcut, SIGNAL(activated()), d->m_signalMapper, SLOT(map())); connect(mode, SIGNAL(enabledStateChanged(bool)), this, SLOT(enabledStateChanged())); }
void ModeManager::objectAdded(QObject *obj) { IMode *mode = qobject_cast<IMode *>(obj); if (!mode) return; d->m_mainWindow->addContextObject(mode); // Count the number of modes with a higher priority int index = 0; foreach (const IMode *m, d->m_modes) if (m->priority() > mode->priority()) ++index; d->m_modes.insert(index, mode); d->m_modeStack->insertTab(index, mode->widget(), mode->icon(), mode->displayName()); d->m_modeStack->setTabEnabled(index, mode->isEnabled()); // Register mode shortcut const Id actionId = mode->id().withPrefix("QtCreator.Mode."); QAction *action = new QAction(tr("Switch to <b>%1</b> mode").arg(mode->displayName()), this); Command *cmd = ActionManager::registerAction(action, actionId); d->m_modeCommands.insert(index, cmd); connect(cmd, &Command::keySequenceChanged, m_instance, &ModeManager::updateModeToolTip); for (int i = 0; i < d->m_modeCommands.size(); ++i) { Command *currentCmd = d->m_modeCommands.at(i); // 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 bool currentlyHasDefaultSequence = (currentCmd->keySequence() == currentCmd->defaultKeySequence()); currentCmd->setDefaultKeySequence(QKeySequence(UseMacShortcuts ? QString::fromLatin1("Meta+%1").arg(i+1) : QString::fromLatin1("Ctrl+%1").arg(i+1))); if (currentlyHasDefaultSequence) currentCmd->setKeySequence(currentCmd->defaultKeySequence()); } Id id = mode->id(); connect(action, &QAction::triggered, [id] { m_instance->activateMode(id); ICore::raiseWindow(d->m_modeStack); }); connect(mode, &IMode::enabledStateChanged, m_instance, &ModeManager::enabledStateChanged); }
void ModeManager::currentTabAboutToChange(int index) { if (index >= 0) { IMode *mode = d->m_modes.at(index); if (mode) emit currentModeAboutToChange(mode->id()); } }
void ModeManager::currentTabChanged(int index) { // Tab index changes to -1 when there is no tab left. if (index >= 0) { IMode *mode = d->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. ICore::updateAdditionalContexts(d->m_addedContexts, mode->context()); d->m_addedContexts = mode->context(); IMode *oldMode = 0; if (d->m_oldCurrent >= 0) oldMode = d->m_modes.at(d->m_oldCurrent); d->m_oldCurrent = index; emit currentModeChanged(mode ? mode->id() : Id(), oldMode ? oldMode->id() : Id()); } }
void ModeManager::enabledStateChanged() { IMode *mode = qobject_cast<IMode *>(sender()); QTC_ASSERT(mode, return); int index = d->m_modes.indexOf(mode); QTC_ASSERT(index >= 0, return); d->m_modeStack->setTabEnabled(index, mode->isEnabled()); // Make sure we leave any disabled mode to prevent possible crashes: if (mode->id() == 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; } } } }