designmode.cpp 8.05 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) 2010 Nokia Corporation and/or its subsidiary(-ies).
Lasse Holmstedt's avatar
Lasse Holmstedt committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** 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.
**
** 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.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/

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

#include <QtCore/QPair>
#include <QtCore/QFileInfo>
47
48
#include <QtCore/QStringList>
#include <QtCore/QDebug>
Lasse Holmstedt's avatar
Lasse Holmstedt committed
49

50
#include <QtGui/QAction>
Lasse Holmstedt's avatar
Lasse Holmstedt committed
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <QtGui/QPlainTextEdit>
#include <QtGui/QStackedWidget>

namespace Core {

class EditorManager;

enum {
    debug = false
};

namespace Internal {

64
65
66
67
68
69
70
71
72
class DesignModeCoreListener : public Core::ICoreListener
{
public:
    DesignModeCoreListener(DesignMode* mode);
    bool coreAboutToClose();
private:
    DesignMode *m_mode;
};

Lasse Holmstedt's avatar
Lasse Holmstedt committed
73
74
75
76
77
78
79
80
81
82
83
84
85
DesignModeCoreListener::DesignModeCoreListener(DesignMode *mode) :
        m_mode(mode)
{
}

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

} // namespace Internal

86
87
88
struct DesignEditorInfo {
    int widgetIndex;
    QStringList mimeTypes;
89
    Context context;
90
91
92
93
94
95
96
97
98
99
100
101
102
    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;
103
    Context m_activeContext;
104
105
106
107
108
109
110
111
112
113
};

DesignModePrivate::DesignModePrivate(DesignMode *q, EditorManager *editorManager) :
    m_coreListener(new Internal::DesignModeCoreListener(q)),
    m_isActive(false),
    m_editorManager(editorManager),
    m_stackWidget(new QStackedWidget)
{
}

Lasse Holmstedt's avatar
Lasse Holmstedt committed
114
DesignMode::DesignMode(EditorManager *editorManager) :
115
        IMode(), d(new DesignModePrivate(this, editorManager))
Lasse Holmstedt's avatar
Lasse Holmstedt committed
116
{
117
    setObjectName(QLatin1String("DesignMode"));
Lasse Holmstedt's avatar
Lasse Holmstedt committed
118
    setEnabled(false);
119
    ExtensionSystem::PluginManager::instance()->addObject(d->m_coreListener);
Lasse Holmstedt's avatar
Lasse Holmstedt committed
120
121
122

    connect(editorManager, SIGNAL(currentEditorChanged(Core::IEditor*)),
            this, SLOT(currentEditorChanged(Core::IEditor*)));
123
124
125

    connect(ModeManager::instance(), SIGNAL(currentModeChanged(Core::IMode*,Core::IMode*)),
            this, SLOT(updateContext(Core::IMode*,Core::IMode*)));
Lasse Holmstedt's avatar
Lasse Holmstedt committed
126
127
128
129
}

DesignMode::~DesignMode()
{
130
131
    ExtensionSystem::PluginManager::instance()->removeObject(d->m_coreListener);
    delete d->m_coreListener;
Lasse Holmstedt's avatar
Lasse Holmstedt committed
132

133
134
    qDeleteAll(d->m_editors);
    delete d;
Lasse Holmstedt's avatar
Lasse Holmstedt committed
135
136
}

137
Context DesignMode::context() const
Lasse Holmstedt's avatar
Lasse Holmstedt committed
138
{
139
    static Context contexts(Constants::C_DESIGN_MODE);
Lasse Holmstedt's avatar
Lasse Holmstedt committed
140
141
142
143
144
    return contexts;
}

QWidget *DesignMode::widget()
{
145
    return d->m_stackWidget;
Lasse Holmstedt's avatar
Lasse Holmstedt committed
146
147
148
149
}

QString DesignMode::displayName() const
{
Friedemann Kleint's avatar
Friedemann Kleint committed
150
    return tr("Design");
Lasse Holmstedt's avatar
Lasse Holmstedt committed
151
152
153
154
}

QIcon DesignMode::icon() const
{
155
    return QIcon(QLatin1String(":/fancyactionbar/images/mode_Design.png"));
Lasse Holmstedt's avatar
Lasse Holmstedt committed
156
157
158
159
160
161
162
163
164
165
166
167
}

int DesignMode::priority() const
{
    return Constants::P_MODE_DESIGN;
}

QString DesignMode::id() const
{
    return QLatin1String(Constants::MODE_DESIGN);
}

168
169
170
171
172
QString DesignMode::type() const
{
    return QLatin1String(Constants::MODE_DESIGN_TYPE);
}

173
174
175
QStringList DesignMode::registeredMimeTypes() const
{
    QStringList rc;
176
    foreach(const DesignEditorInfo *i, d->m_editors)
177
178
179
180
        rc += i->mimeTypes;
    return rc;
}

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

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

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

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

Lasse Holmstedt's avatar
Lasse Holmstedt committed
217
218
219
220
    bool mimeEditorAvailable = false;
    Core::ICore *core = Core::ICore::instance();

    if (editor && editor->file()) {
221
222
223
224
225
226
227
228
229
230
231
232
233
        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)
Lasse Holmstedt's avatar
Lasse Holmstedt committed
234
                    break;
235
            } // foreach editorInfo
Lasse Holmstedt's avatar
Lasse Holmstedt committed
236
237
        }
    }
238
239
240
    if (d->m_currentEditor)
        disconnect(d->m_currentEditor.data(), SIGNAL(changed()), this, SLOT(updateActions()));

241
    if (!mimeEditorAvailable) {
242
        setActiveContext(Context());
243
244
        if (core->modeManager()->currentMode() == this)
            core->modeManager()->activateMode(Core::Constants::MODE_EDIT);
Lasse Holmstedt's avatar
Lasse Holmstedt committed
245
        setEnabled(false);
246
247
248
249
        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
250

251
252
        if (d->m_currentEditor)
            connect(d->m_currentEditor.data(), SIGNAL(changed()), this, SLOT(updateActions()));
Lasse Holmstedt's avatar
Lasse Holmstedt committed
253

254
255
        emit actionsUpdated(d->m_currentEditor.data());
    }
Lasse Holmstedt's avatar
Lasse Holmstedt committed
256
257
258
259
}

void DesignMode::updateActions()
{
260
    emit actionsUpdated(d->m_currentEditor.data());
Lasse Holmstedt's avatar
Lasse Holmstedt committed
261
262
}

263
264
265
266
void DesignMode::updateContext(Core::IMode *newMode, Core::IMode *oldMode)
{
    if (newMode == this) {
        // Apply active context
267
        Core::ICore::instance()->updateAdditionalContexts(Context(), d->m_activeContext);
268
269
    } else if (oldMode == this) {
        // Remove active context
270
        Core::ICore::instance()->updateAdditionalContexts(d->m_activeContext, Context());
271
272
273
    }
}

274
void DesignMode::setActiveContext(const Context &context)
275
{
hjk's avatar
hjk committed
276
    if (d->m_activeContext == context)
277
278
        return;

279
280
281
    if (ModeManager::instance()->currentMode() == this)
        Core::ICore::instance()->updateAdditionalContexts(d->m_activeContext, context);

282
283
284
    d->m_activeContext = context;
}

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