designmode.cpp 7.91 KB
Newer Older
Lasse Holmstedt's avatar
Lasse Holmstedt committed
1 2 3 4
/**************************************************************************
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
Lasse Holmstedt's avatar
Lasse Holmstedt committed
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
Lasse Holmstedt's avatar
Lasse Holmstedt committed
8 9 10 11
**
**
** GNU Lesser General Public License Usage
**
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.
Lasse Holmstedt's avatar
Lasse Holmstedt committed
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
29
** Nokia at qt-info@nokia.com.
Lasse Holmstedt's avatar
Lasse Holmstedt committed
30 31 32 33 34 35 36 37 38 39 40 41 42
**
**************************************************************************/

#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>
43 44
#include <coreplugin/icorelistener.h>
#include <coreplugin/editormanager/ieditor.h>
Lasse Holmstedt's avatar
Lasse Holmstedt committed
45
#include <extensionsystem/pluginmanager.h>
46
#include <utils/qtcassert.h>
Lasse Holmstedt's avatar
Lasse Holmstedt committed
47

48 49 50 51 52 53 54 55
#include <QPair>
#include <QFileInfo>
#include <QStringList>
#include <QDebug>

#include <QAction>
#include <QPlainTextEdit>
#include <QStackedWidget>
Lasse Holmstedt's avatar
Lasse Holmstedt committed
56

57 58
static Core::DesignMode *m_instance = 0;

Lasse Holmstedt's avatar
Lasse Holmstedt committed
59 60 61 62 63 64 65 66 67 68
namespace Core {

class EditorManager;

enum {
    debug = false
};

namespace Internal {

69 70 71 72 73 74 75 76 77
class DesignModeCoreListener : public Core::ICoreListener
{
public:
    DesignModeCoreListener(DesignMode* mode);
    bool coreAboutToClose();
private:
    DesignMode *m_mode;
};

Lasse Holmstedt's avatar
Lasse Holmstedt committed
78 79 80 81 82 83 84 85 86 87 88 89 90
DesignModeCoreListener::DesignModeCoreListener(DesignMode *mode) :
        m_mode(mode)
{
}

bool DesignModeCoreListener::coreAboutToClose()
{
    m_mode->currentEditorChanged(0);
    return true;
}

} // namespace Internal

hjk's avatar
hjk committed
91 92
struct DesignEditorInfo
{
93 94
    int widgetIndex;
    QStringList mimeTypes;
95
    Context context;
96 97 98
    QWidget *widget;
};

hjk's avatar
hjk committed
99 100 101 102 103 104
class DesignModePrivate
{
public:
    explicit DesignModePrivate(DesignMode *q);

public:
105 106 107
    Internal::DesignModeCoreListener *m_coreListener;
    QWeakPointer<Core::IEditor> m_currentEditor;
    bool m_isActive;
108
    bool m_isRequired;
109 110
    QList<DesignEditorInfo*> m_editors;
    QStackedWidget *m_stackWidget;
111
    Context m_activeContext;
112 113
};

hjk's avatar
hjk committed
114 115
DesignModePrivate::DesignModePrivate(DesignMode *q)
  : m_coreListener(new Internal::DesignModeCoreListener(q)),
116
    m_isActive(false),
117
    m_isRequired(false),
118 119 120 121
    m_stackWidget(new QStackedWidget)
{
}

hjk's avatar
hjk committed
122 123
DesignMode::DesignMode()
    : d(new DesignModePrivate(this))
Lasse Holmstedt's avatar
Lasse Holmstedt committed
124
{
125
    m_instance = this;
126
    setObjectName(QLatin1String("DesignMode"));
Lasse Holmstedt's avatar
Lasse Holmstedt committed
127
    setEnabled(false);
128 129
    setContext(Context(Constants::C_DESIGN_MODE));
    setWidget(d->m_stackWidget);
130 131 132
    setDisplayName(tr("Design"));
    setIcon(QIcon(QLatin1String(":/fancyactionbar/images/mode_Design.png")));
    setPriority(Constants::P_MODE_DESIGN);
133 134
    setId(QLatin1String(Constants::MODE_DESIGN));
    setType(QLatin1String(Constants::MODE_DESIGN_TYPE));
135

136
    ExtensionSystem::PluginManager::instance()->addObject(d->m_coreListener);
Lasse Holmstedt's avatar
Lasse Holmstedt committed
137

hjk's avatar
hjk committed
138
    connect(EditorManager::instance(), SIGNAL(currentEditorChanged(Core::IEditor*)),
Lasse Holmstedt's avatar
Lasse Holmstedt committed
139
            this, SLOT(currentEditorChanged(Core::IEditor*)));
140 141 142

    connect(ModeManager::instance(), SIGNAL(currentModeChanged(Core::IMode*,Core::IMode*)),
            this, SLOT(updateContext(Core::IMode*,Core::IMode*)));
Lasse Holmstedt's avatar
Lasse Holmstedt committed
143 144 145 146
}

DesignMode::~DesignMode()
{
147 148
    ExtensionSystem::PluginManager::instance()->removeObject(d->m_coreListener);
    delete d->m_coreListener;
Lasse Holmstedt's avatar
Lasse Holmstedt committed
149

150 151
    qDeleteAll(d->m_editors);
    delete d;
Lasse Holmstedt's avatar
Lasse Holmstedt committed
152 153
}

154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
DesignMode *DesignMode::instance()
{
    return m_instance;
}

void DesignMode::setDesignModeIsRequired()
{
    d->m_isRequired = true;
}

bool DesignMode::designModeIsRequired() const
{
    return d->m_isRequired;
}

169 170 171
QStringList DesignMode::registeredMimeTypes() const
{
    QStringList rc;
172
    foreach(const DesignEditorInfo *i, d->m_editors)
173 174 175 176
        rc += i->mimeTypes;
    return rc;
}

177 178 179 180 181
/**
  * 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.
  */
182 183
void DesignMode::registerDesignWidget(QWidget *widget,
                                      const QStringList &mimeTypes,
184
                                      const Context &context)
Lasse Holmstedt's avatar
Lasse Holmstedt committed
185
{
186
    setDesignModeIsRequired();
187
    int index = d->m_stackWidget->addWidget(widget);
Lasse Holmstedt's avatar
Lasse Holmstedt committed
188 189 190

    DesignEditorInfo *info = new DesignEditorInfo;
    info->mimeTypes = mimeTypes;
191
    info->context = context;
Lasse Holmstedt's avatar
Lasse Holmstedt committed
192 193
    info->widgetIndex = index;
    info->widget = widget;
194
    d->m_editors.append(info);
Lasse Holmstedt's avatar
Lasse Holmstedt committed
195 196 197 198
}

void DesignMode::unregisterDesignWidget(QWidget *widget)
{
199 200
    d->m_stackWidget->removeWidget(widget);
    foreach(DesignEditorInfo *info, d->m_editors) {
Lasse Holmstedt's avatar
Lasse Holmstedt committed
201
        if (info->widget == widget) {
202
            d->m_editors.removeAll(info);
Lasse Holmstedt's avatar
Lasse Holmstedt committed
203 204 205 206 207 208 209 210
            break;
        }
    }
}

// if editor changes, check if we have valid mimetype registered.
void DesignMode::currentEditorChanged(Core::IEditor *editor)
{
211
    if (editor && (d->m_currentEditor.data() == editor))
212 213
        return;

Lasse Holmstedt's avatar
Lasse Holmstedt committed
214 215
    bool mimeEditorAvailable = false;

216 217
    if (editor && editor->document()) {
        const QString mimeType = editor->document()->mimeType();
218 219 220 221 222 223 224 225 226 227 228 229
        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)
Lasse Holmstedt's avatar
Lasse Holmstedt committed
230
                    break;
231
            } // foreach editorInfo
Lasse Holmstedt's avatar
Lasse Holmstedt committed
232 233
        }
    }
234 235 236
    if (d->m_currentEditor)
        disconnect(d->m_currentEditor.data(), SIGNAL(changed()), this, SLOT(updateActions()));

237
    if (!mimeEditorAvailable) {
238
        setActiveContext(Context());
239 240
        if (ModeManager::currentMode() == this)
            ModeManager::activateMode(QLatin1String(Core::Constants::MODE_EDIT));
Lasse Holmstedt's avatar
Lasse Holmstedt committed
241
        setEnabled(false);
242 243 244 245
        d->m_currentEditor = QWeakPointer<Core::IEditor>();
        emit actionsUpdated(d->m_currentEditor.data());
    } else {
        d->m_currentEditor = QWeakPointer<Core::IEditor>(editor);
Lasse Holmstedt's avatar
Lasse Holmstedt committed
246

247 248
        if (d->m_currentEditor)
            connect(d->m_currentEditor.data(), SIGNAL(changed()), this, SLOT(updateActions()));
Lasse Holmstedt's avatar
Lasse Holmstedt committed
249

250 251
        emit actionsUpdated(d->m_currentEditor.data());
    }
Lasse Holmstedt's avatar
Lasse Holmstedt committed
252 253 254 255
}

void DesignMode::updateActions()
{
256
    emit actionsUpdated(d->m_currentEditor.data());
Lasse Holmstedt's avatar
Lasse Holmstedt committed
257 258
}

259 260 261 262
void DesignMode::updateContext(Core::IMode *newMode, Core::IMode *oldMode)
{
    if (newMode == this) {
        // Apply active context
hjk's avatar
hjk committed
263
        Core::ICore::updateAdditionalContexts(Context(), d->m_activeContext);
264 265
    } else if (oldMode == this) {
        // Remove active context
hjk's avatar
hjk committed
266
        Core::ICore::updateAdditionalContexts(d->m_activeContext, Context());
267 268 269
    }
}

270
void DesignMode::setActiveContext(const Context &context)
271
{
hjk's avatar
hjk committed
272
    if (d->m_activeContext == context)
273 274
        return;

275
    if (ModeManager::currentMode() == this)
hjk's avatar
hjk committed
276
        Core::ICore::updateAdditionalContexts(d->m_activeContext, context);
277

278 279 280
    d->m_activeContext = context;
}

Lasse Holmstedt's avatar
Lasse Holmstedt committed
281
} // namespace Core