2010-02-26 11:08:17 +01:00
|
|
|
/**************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
|
**
|
2011-01-11 16:28:15 +01:00
|
|
|
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
2010-02-26 11:08:17 +01:00
|
|
|
**
|
|
|
|
|
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** No Commercial Usage
|
2010-02-26 11:08:17 +01:00
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** This file contains pre-release code and may not be distributed.
|
|
|
|
|
** You may use this file in accordance with the terms and conditions
|
|
|
|
|
** contained in the Technology Preview License Agreement accompanying
|
|
|
|
|
** this package.
|
2010-02-26 11:08:17 +01:00
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
|
**
|
|
|
|
|
** 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.
|
|
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** In addition, as a special exception, Nokia gives you certain additional
|
|
|
|
|
** rights. These rights are described in the Nokia Qt LGPL Exception
|
|
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
|
|
|
|
** If you have questions regarding the use of this file, please contact
|
|
|
|
|
** Nokia at qt-info@nokia.com.
|
2010-02-26 11:08:17 +01:00
|
|
|
**
|
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "designmode.h"
|
|
|
|
|
|
|
|
|
|
#include <coreplugin/icore.h>
|
|
|
|
|
#include <coreplugin/modemanager.h>
|
|
|
|
|
#include <coreplugin/editormanager/editormanager.h>
|
|
|
|
|
#include <coreplugin/editormanager/openeditorsmodel.h>
|
|
|
|
|
#include <coreplugin/actionmanager/actionmanager.h>
|
|
|
|
|
#include <coreplugin/actionmanager/command.h>
|
|
|
|
|
#include <coreplugin/coreconstants.h>
|
|
|
|
|
#include <coreplugin/mimedatabase.h>
|
2010-03-17 17:44:46 +01:00
|
|
|
#include <coreplugin/icorelistener.h>
|
|
|
|
|
#include <coreplugin/editormanager/ieditor.h>
|
2010-02-26 11:08:17 +01:00
|
|
|
#include <extensionsystem/pluginmanager.h>
|
2010-03-30 18:16:05 +02:00
|
|
|
#include <utils/qtcassert.h>
|
2010-02-26 11:08:17 +01:00
|
|
|
|
|
|
|
|
#include <QtCore/QPair>
|
|
|
|
|
#include <QtCore/QFileInfo>
|
2010-03-17 17:44:46 +01:00
|
|
|
#include <QtCore/QStringList>
|
|
|
|
|
#include <QtCore/QDebug>
|
2010-02-26 11:08:17 +01:00
|
|
|
|
2010-03-17 17:44:46 +01:00
|
|
|
#include <QtGui/QAction>
|
2010-02-26 11:08:17 +01:00
|
|
|
#include <QtGui/QPlainTextEdit>
|
|
|
|
|
#include <QtGui/QStackedWidget>
|
|
|
|
|
|
|
|
|
|
namespace Core {
|
|
|
|
|
|
|
|
|
|
class EditorManager;
|
|
|
|
|
|
|
|
|
|
enum {
|
|
|
|
|
debug = false
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
2010-03-17 17:44:46 +01:00
|
|
|
class DesignModeCoreListener : public Core::ICoreListener
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
DesignModeCoreListener(DesignMode* mode);
|
|
|
|
|
bool coreAboutToClose();
|
|
|
|
|
private:
|
|
|
|
|
DesignMode *m_mode;
|
|
|
|
|
};
|
|
|
|
|
|
2010-02-26 11:08:17 +01:00
|
|
|
DesignModeCoreListener::DesignModeCoreListener(DesignMode *mode) :
|
|
|
|
|
m_mode(mode)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DesignModeCoreListener::coreAboutToClose()
|
|
|
|
|
{
|
|
|
|
|
m_mode->currentEditorChanged(0);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Internal
|
|
|
|
|
|
2010-03-17 17:44:46 +01:00
|
|
|
struct DesignEditorInfo {
|
|
|
|
|
int widgetIndex;
|
|
|
|
|
QStringList mimeTypes;
|
2010-06-25 12:56:16 +02:00
|
|
|
Context context;
|
2010-03-17 17:44:46 +01:00
|
|
|
QWidget *widget;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct DesignModePrivate {
|
|
|
|
|
explicit DesignModePrivate(DesignMode *q, EditorManager *editorManager);
|
|
|
|
|
Internal::DesignModeCoreListener *m_coreListener;
|
|
|
|
|
QWeakPointer<Core::IEditor> m_currentEditor;
|
|
|
|
|
bool m_isActive;
|
|
|
|
|
|
|
|
|
|
QList<DesignEditorInfo*> m_editors;
|
|
|
|
|
|
|
|
|
|
EditorManager *m_editorManager;
|
|
|
|
|
QStackedWidget *m_stackWidget;
|
2010-06-25 12:56:16 +02:00
|
|
|
Context m_activeContext;
|
2010-03-17 17:44:46 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
DesignModePrivate::DesignModePrivate(DesignMode *q, EditorManager *editorManager) :
|
|
|
|
|
m_coreListener(new Internal::DesignModeCoreListener(q)),
|
|
|
|
|
m_isActive(false),
|
|
|
|
|
m_editorManager(editorManager),
|
|
|
|
|
m_stackWidget(new QStackedWidget)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-26 11:08:17 +01:00
|
|
|
DesignMode::DesignMode(EditorManager *editorManager) :
|
2010-03-17 17:44:46 +01:00
|
|
|
IMode(), d(new DesignModePrivate(this, editorManager))
|
2010-02-26 11:08:17 +01:00
|
|
|
{
|
2010-12-03 17:30:09 +01:00
|
|
|
setObjectName(QLatin1String("DesignMode"));
|
2010-02-26 11:08:17 +01:00
|
|
|
setEnabled(false);
|
2010-03-17 17:44:46 +01:00
|
|
|
ExtensionSystem::PluginManager::instance()->addObject(d->m_coreListener);
|
2010-02-26 11:08:17 +01:00
|
|
|
|
|
|
|
|
connect(editorManager, SIGNAL(currentEditorChanged(Core::IEditor*)),
|
|
|
|
|
this, SLOT(currentEditorChanged(Core::IEditor*)));
|
2010-03-19 16:07:04 +01:00
|
|
|
|
|
|
|
|
connect(ModeManager::instance(), SIGNAL(currentModeChanged(Core::IMode*,Core::IMode*)),
|
|
|
|
|
this, SLOT(updateContext(Core::IMode*,Core::IMode*)));
|
2010-02-26 11:08:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DesignMode::~DesignMode()
|
|
|
|
|
{
|
2010-03-17 17:44:46 +01:00
|
|
|
ExtensionSystem::PluginManager::instance()->removeObject(d->m_coreListener);
|
|
|
|
|
delete d->m_coreListener;
|
2010-02-26 11:08:17 +01:00
|
|
|
|
2010-03-17 17:44:46 +01:00
|
|
|
qDeleteAll(d->m_editors);
|
|
|
|
|
delete d;
|
2010-02-26 11:08:17 +01:00
|
|
|
}
|
|
|
|
|
|
2010-06-25 12:56:16 +02:00
|
|
|
Context DesignMode::context() const
|
2010-02-26 11:08:17 +01:00
|
|
|
{
|
2010-06-25 17:37:59 +02:00
|
|
|
static Context contexts(Constants::C_DESIGN_MODE);
|
2010-02-26 11:08:17 +01:00
|
|
|
return contexts;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QWidget *DesignMode::widget()
|
|
|
|
|
{
|
2010-03-17 17:44:46 +01:00
|
|
|
return d->m_stackWidget;
|
2010-02-26 11:08:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString DesignMode::displayName() const
|
|
|
|
|
{
|
2010-02-26 14:19:19 +01:00
|
|
|
return tr("Design");
|
2010-02-26 11:08:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QIcon DesignMode::icon() const
|
|
|
|
|
{
|
2010-03-01 13:31:57 +01:00
|
|
|
return QIcon(QLatin1String(":/fancyactionbar/images/mode_Design.png"));
|
2010-02-26 11:08:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int DesignMode::priority() const
|
|
|
|
|
{
|
|
|
|
|
return Constants::P_MODE_DESIGN;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString DesignMode::id() const
|
|
|
|
|
{
|
|
|
|
|
return QLatin1String(Constants::MODE_DESIGN);
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-14 15:15:57 +02:00
|
|
|
QString DesignMode::type() const
|
|
|
|
|
{
|
|
|
|
|
return QLatin1String(Constants::MODE_DESIGN_TYPE);
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-16 16:51:45 +01:00
|
|
|
QStringList DesignMode::registeredMimeTypes() const
|
|
|
|
|
{
|
|
|
|
|
QStringList rc;
|
2010-03-17 17:44:46 +01:00
|
|
|
foreach(const DesignEditorInfo *i, d->m_editors)
|
2010-03-16 16:51:45 +01:00
|
|
|
rc += i->mimeTypes;
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-26 12:39:26 +01:00
|
|
|
/**
|
|
|
|
|
* Registers a widget to be displayed when an editor with a file specified in
|
|
|
|
|
* mimeTypes is opened. This also appends the additionalContext in ICore to
|
|
|
|
|
* the context, specified here.
|
|
|
|
|
*/
|
2010-03-19 16:07:04 +01:00
|
|
|
void DesignMode::registerDesignWidget(QWidget *widget,
|
|
|
|
|
const QStringList &mimeTypes,
|
2010-06-25 12:56:16 +02:00
|
|
|
const Context &context)
|
2010-02-26 11:08:17 +01:00
|
|
|
{
|
2010-03-17 17:44:46 +01:00
|
|
|
int index = d->m_stackWidget->addWidget(widget);
|
2010-02-26 11:08:17 +01:00
|
|
|
|
|
|
|
|
DesignEditorInfo *info = new DesignEditorInfo;
|
|
|
|
|
info->mimeTypes = mimeTypes;
|
2010-03-19 16:07:04 +01:00
|
|
|
info->context = context;
|
2010-02-26 11:08:17 +01:00
|
|
|
info->widgetIndex = index;
|
|
|
|
|
info->widget = widget;
|
2010-03-17 17:44:46 +01:00
|
|
|
d->m_editors.append(info);
|
2010-02-26 11:08:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DesignMode::unregisterDesignWidget(QWidget *widget)
|
|
|
|
|
{
|
2010-03-17 17:44:46 +01:00
|
|
|
d->m_stackWidget->removeWidget(widget);
|
|
|
|
|
foreach(DesignEditorInfo *info, d->m_editors) {
|
2010-02-26 11:08:17 +01:00
|
|
|
if (info->widget == widget) {
|
2010-03-17 17:44:46 +01:00
|
|
|
d->m_editors.removeAll(info);
|
2010-02-26 11:08:17 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if editor changes, check if we have valid mimetype registered.
|
|
|
|
|
void DesignMode::currentEditorChanged(Core::IEditor *editor)
|
|
|
|
|
{
|
2010-04-06 15:10:20 +02:00
|
|
|
if (editor && (d->m_currentEditor.data() == editor))
|
2010-03-30 18:16:05 +02:00
|
|
|
return;
|
|
|
|
|
|
2010-02-26 11:08:17 +01:00
|
|
|
bool mimeEditorAvailable = false;
|
|
|
|
|
Core::ICore *core = Core::ICore::instance();
|
|
|
|
|
|
|
|
|
|
if (editor && editor->file()) {
|
2010-05-07 14:49:57 +02:00
|
|
|
const QString mimeType = editor->file()->mimeType();
|
|
|
|
|
if (!mimeType.isEmpty()) {
|
|
|
|
|
foreach (DesignEditorInfo *editorInfo, d->m_editors) {
|
|
|
|
|
foreach (const QString &mime, editorInfo->mimeTypes) {
|
|
|
|
|
if (mime == mimeType) {
|
|
|
|
|
d->m_stackWidget->setCurrentIndex(editorInfo->widgetIndex);
|
|
|
|
|
setActiveContext(editorInfo->context);
|
|
|
|
|
mimeEditorAvailable = true;
|
|
|
|
|
setEnabled(true);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
} // foreach mime
|
|
|
|
|
if (mimeEditorAvailable)
|
2010-02-26 11:08:17 +01:00
|
|
|
break;
|
2010-05-07 14:49:57 +02:00
|
|
|
} // foreach editorInfo
|
2010-02-26 11:08:17 +01:00
|
|
|
}
|
|
|
|
|
}
|
2010-03-30 18:16:05 +02:00
|
|
|
if (d->m_currentEditor)
|
|
|
|
|
disconnect(d->m_currentEditor.data(), SIGNAL(changed()), this, SLOT(updateActions()));
|
|
|
|
|
|
2010-03-19 16:07:04 +01:00
|
|
|
if (!mimeEditorAvailable) {
|
2010-06-25 12:56:16 +02:00
|
|
|
setActiveContext(Context());
|
2010-04-06 15:10:20 +02:00
|
|
|
if (core->modeManager()->currentMode() == this)
|
|
|
|
|
core->modeManager()->activateMode(Core::Constants::MODE_EDIT);
|
2010-02-26 11:08:17 +01:00
|
|
|
setEnabled(false);
|
2010-03-30 18:16:05 +02:00
|
|
|
d->m_currentEditor = QWeakPointer<Core::IEditor>();
|
|
|
|
|
emit actionsUpdated(d->m_currentEditor.data());
|
|
|
|
|
} else {
|
|
|
|
|
d->m_currentEditor = QWeakPointer<Core::IEditor>(editor);
|
2010-02-26 11:08:17 +01:00
|
|
|
|
2010-03-30 18:16:05 +02:00
|
|
|
if (d->m_currentEditor)
|
|
|
|
|
connect(d->m_currentEditor.data(), SIGNAL(changed()), this, SLOT(updateActions()));
|
2010-02-26 11:08:17 +01:00
|
|
|
|
2010-03-30 18:16:05 +02:00
|
|
|
emit actionsUpdated(d->m_currentEditor.data());
|
|
|
|
|
}
|
2010-02-26 11:08:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DesignMode::updateActions()
|
|
|
|
|
{
|
2010-03-17 17:44:46 +01:00
|
|
|
emit actionsUpdated(d->m_currentEditor.data());
|
2010-02-26 11:08:17 +01:00
|
|
|
}
|
|
|
|
|
|
2010-03-19 16:07:04 +01:00
|
|
|
void DesignMode::updateContext(Core::IMode *newMode, Core::IMode *oldMode)
|
|
|
|
|
{
|
|
|
|
|
if (newMode == this) {
|
|
|
|
|
// Apply active context
|
2010-06-25 12:56:16 +02:00
|
|
|
Core::ICore::instance()->updateAdditionalContexts(Context(), d->m_activeContext);
|
2010-03-19 16:07:04 +01:00
|
|
|
} else if (oldMode == this) {
|
|
|
|
|
// Remove active context
|
2010-06-25 12:56:16 +02:00
|
|
|
Core::ICore::instance()->updateAdditionalContexts(d->m_activeContext, Context());
|
2010-03-19 16:07:04 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-25 12:56:16 +02:00
|
|
|
void DesignMode::setActiveContext(const Context &context)
|
2010-03-19 16:07:04 +01:00
|
|
|
{
|
2010-06-25 18:05:09 +02:00
|
|
|
if (d->m_activeContext == context)
|
2010-03-19 16:07:04 +01:00
|
|
|
return;
|
|
|
|
|
|
2010-03-22 18:05:22 +01:00
|
|
|
if (ModeManager::instance()->currentMode() == this)
|
|
|
|
|
Core::ICore::instance()->updateAdditionalContexts(d->m_activeContext, context);
|
|
|
|
|
|
2010-03-19 16:07:04 +01:00
|
|
|
d->m_activeContext = context;
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-26 11:08:17 +01:00
|
|
|
} // namespace Core
|