~bzoltan/kubuntu-packaging/decouple_cmake_plugin

« back to all changes in this revision

Viewing changes to src/plugins/projectexplorer/editorconfiguration.cpp

  • Committer: Timo Jyrinki
  • Date: 2013-11-15 12:25:23 UTC
  • mfrom: (1.1.28)
  • Revision ID: timo.jyrinki@canonical.com-20131115122523-i2kyamsu4gs2mu1m
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
#include "project.h"
34
34
 
35
35
#include <coreplugin/id.h>
 
36
#include <coreplugin/icore.h>
36
37
#include <texteditor/basetexteditor.h>
37
38
#include <texteditor/texteditorsettings.h>
38
39
#include <texteditor/simplecodestylepreferences.h>
53
54
static const QLatin1String kCodec("EditorConfiguration.Codec");
54
55
static const QLatin1String kCodeStylePrefix("EditorConfiguration.CodeStyle.");
55
56
static const QLatin1String kCodeStyleCount("EditorConfiguration.CodeStyle.Count");
56
 
static const QLatin1String kId("Project");
57
57
 
58
58
using namespace TextEditor;
59
59
 
63
63
{
64
64
    EditorConfigurationPrivate()
65
65
        : m_useGlobal(true)
66
 
        , m_typingSettings(TextEditorSettings::instance()->typingSettings())
67
 
        , m_storageSettings(TextEditorSettings::instance()->storageSettings())
68
 
        , m_behaviorSettings(TextEditorSettings::instance()->behaviorSettings())
69
 
        , m_extraEncodingSettings(TextEditorSettings::instance()->extraEncodingSettings())
70
 
        , m_textCodec(Core::EditorManager::instance()->defaultTextCodec())
 
66
        , m_typingSettings(TextEditorSettings::typingSettings())
 
67
        , m_storageSettings(TextEditorSettings::storageSettings())
 
68
        , m_behaviorSettings(TextEditorSettings::behaviorSettings())
 
69
        , m_extraEncodingSettings(TextEditorSettings::extraEncodingSettings())
 
70
        , m_textCodec(Core::EditorManager::defaultTextCodec())
71
71
    {
72
72
    }
73
73
 
84
84
 
85
85
EditorConfiguration::EditorConfiguration() : d(new EditorConfigurationPrivate)
86
86
{
87
 
    TextEditorSettings *textEditorSettings = TextEditorSettings::instance();
88
 
 
89
 
    const QMap<Core::Id, ICodeStylePreferences *> languageCodeStylePreferences = textEditorSettings->codeStyles();
 
87
    const QMap<Core::Id, ICodeStylePreferences *> languageCodeStylePreferences = TextEditorSettings::codeStyles();
90
88
    QMapIterator<Core::Id, ICodeStylePreferences *> itCodeStyle(languageCodeStylePreferences);
91
89
    while (itCodeStyle.hasNext()) {
92
90
        itCodeStyle.next();
93
91
        Core::Id languageId = itCodeStyle.key();
 
92
        // global prefs for language
94
93
        ICodeStylePreferences *originalPreferences = itCodeStyle.value();
95
 
        ICodeStylePreferencesFactory *factory = textEditorSettings->codeStyleFactory(languageId);
 
94
        ICodeStylePreferencesFactory *factory = TextEditorSettings::codeStyleFactory(languageId);
 
95
        // clone of global prefs for language - it will became project prefs for language
96
96
        ICodeStylePreferences *preferences = factory->createCodeStyle();
97
 
        preferences->setDelegatingPool(textEditorSettings->codeStylePool(languageId));
98
 
        preferences->setId(languageId.toString() + QLatin1String("Project"));
 
97
        // project prefs can point to the global language pool, which contains also the global language prefs
 
98
        preferences->setDelegatingPool(TextEditorSettings::codeStylePool(languageId));
 
99
        preferences->setId(languageId.name() + "Project");
99
100
        preferences->setDisplayName(tr("Project %1", "Settings, %1 is a language (C++ or QML)").arg(factory->displayName()));
 
101
        // project prefs by default point to global prefs (which in turn can delegate to anything else or not)
100
102
        preferences->setCurrentDelegate(originalPreferences);
101
103
        d->m_languageCodeStylePreferences.insert(languageId, preferences);
102
104
    }
103
105
 
 
106
    // clone of global prefs (not language specific), for project scope
104
107
    d->m_defaultCodeStyle = new SimpleCodeStylePreferences(this);
105
 
    d->m_defaultCodeStyle->setDelegatingPool(textEditorSettings->codeStylePool());
 
108
    d->m_defaultCodeStyle->setDelegatingPool(TextEditorSettings::codeStylePool());
106
109
    d->m_defaultCodeStyle->setDisplayName(tr("Project", "Settings"));
107
 
    d->m_defaultCodeStyle->setId(kId);
 
110
    d->m_defaultCodeStyle->setId("Project");
 
111
    // if setCurrentDelegate is 0 values are read from *this prefs
108
112
    d->m_defaultCodeStyle->setCurrentDelegate(d->m_useGlobal
109
 
                    ? TextEditorSettings::instance()->codeStyle() : 0);
 
113
                    ? TextEditorSettings::codeStyle() : 0);
 
114
 
 
115
    connect(SessionManager::instance(), SIGNAL(aboutToRemoveProject(ProjectExplorer::Project*)),
 
116
            this, SLOT(slotAboutToRemoveProject(ProjectExplorer::Project*)));
110
117
}
111
118
 
112
119
EditorConfiguration::~EditorConfiguration()
122
129
 
123
130
void EditorConfiguration::cloneGlobalSettings()
124
131
{
125
 
    TextEditorSettings *textEditorSettings = TextEditorSettings::instance();
126
 
 
127
 
    d->m_defaultCodeStyle->setTabSettings(textEditorSettings->codeStyle()->tabSettings());
128
 
    setTypingSettings(textEditorSettings->typingSettings());
129
 
    setStorageSettings(textEditorSettings->storageSettings());
130
 
    setBehaviorSettings(textEditorSettings->behaviorSettings());
131
 
    setExtraEncodingSettings(textEditorSettings->extraEncodingSettings());
132
 
    d->m_textCodec = Core::EditorManager::instance()->defaultTextCodec();
 
132
    d->m_defaultCodeStyle->setTabSettings(TextEditorSettings::codeStyle()->tabSettings());
 
133
    setTypingSettings(TextEditorSettings::typingSettings());
 
134
    setStorageSettings(TextEditorSettings::storageSettings());
 
135
    setBehaviorSettings(TextEditorSettings::behaviorSettings());
 
136
    setExtraEncodingSettings(TextEditorSettings::extraEncodingSettings());
 
137
    d->m_textCodec = Core::EditorManager::defaultTextCodec();
133
138
}
134
139
 
135
140
QTextCodec *EditorConfiguration::textCodec() const
208
213
    const QByteArray &codecName = map.value(kCodec, d->m_textCodec->name()).toByteArray();
209
214
    d->m_textCodec = QTextCodec::codecForName(codecName);
210
215
    if (!d->m_textCodec)
211
 
        d->m_textCodec = Core::EditorManager::instance()->defaultTextCodec();
 
216
        d->m_textCodec = Core::EditorManager::defaultTextCodec();
212
217
 
213
218
    const int codeStyleCount = map.value(kCodeStyleCount, 0).toInt();
214
219
    for (int i = 0; i < codeStyleCount; ++i) {
237
242
    if (baseTextEditor)
238
243
        baseTextEditor->setCodeStyle(codeStyle(baseTextEditor->languageSettingsId()));
239
244
    if (!d->m_useGlobal) {
240
 
        textEditor->setTextCodec(d->m_textCodec, ITextEditor::TextCodecFromProjectSetting);
 
245
        textEditor->textDocument()->setCodec(d->m_textCodec);
241
246
        if (baseTextEditor)
242
247
            switchSettings(baseTextEditor);
243
248
    }
244
249
}
245
250
 
 
251
void EditorConfiguration::deconfigureEditor(ITextEditor *textEditor) const
 
252
{
 
253
    BaseTextEditorWidget *baseTextEditor = qobject_cast<BaseTextEditorWidget *>(textEditor->widget());
 
254
    if (baseTextEditor)
 
255
        baseTextEditor->setCodeStyle(TextEditorSettings::codeStyle(baseTextEditor->languageSettingsId()));
 
256
 
 
257
    // TODO: what about text codec and switching settings?
 
258
}
 
259
 
246
260
void EditorConfiguration::setUseGlobalSettings(bool use)
247
261
{
248
262
    d->m_useGlobal = use;
249
263
    d->m_defaultCodeStyle->setCurrentDelegate(d->m_useGlobal
250
 
                    ? TextEditorSettings::instance()->codeStyle() : 0);
251
 
    const SessionManager *session = ProjectExplorerPlugin::instance()->session();
252
 
    QList<Core::IEditor *> opened = Core::EditorManager::instance()->openedEditors();
 
264
                    ? TextEditorSettings::codeStyle() : 0);
 
265
    QList<Core::IEditor *> opened = Core::EditorManager::documentModel()->editorsForDocuments(
 
266
                Core::EditorManager::documentModel()->openedDocuments());
253
267
    foreach (Core::IEditor *editor, opened) {
254
268
        if (BaseTextEditorWidget *baseTextEditor = qobject_cast<BaseTextEditorWidget *>(editor->widget())) {
255
 
            Project *project = session->projectForFile(editor->document()->fileName());
 
269
            Project *project = SessionManager::projectForFile(editor->document()->filePath());
256
270
            if (project && project->editorConfiguration() == this)
257
271
                switchSettings(baseTextEditor);
258
272
        }
259
273
    }
260
274
}
261
275
 
262
 
void EditorConfiguration::switchSettings(BaseTextEditorWidget *baseTextEditor) const
263
 
{
264
 
    if (d->m_useGlobal)
265
 
        switchSettings_helper(TextEditorSettings::instance(), this, baseTextEditor);
266
 
    else
267
 
        switchSettings_helper(this, TextEditorSettings::instance(), baseTextEditor);
268
 
}
269
 
 
270
 
template <class NewSenderT, class OldSenderT>
271
 
void EditorConfiguration::switchSettings_helper(const NewSenderT *newSender,
272
 
                                                const OldSenderT *oldSender,
273
 
                                                BaseTextEditorWidget *baseTextEditor) const
274
 
{
275
 
    baseTextEditor->setTypingSettings(newSender->typingSettings());
276
 
    baseTextEditor->setStorageSettings(newSender->storageSettings());
277
 
    baseTextEditor->setBehaviorSettings(newSender->behaviorSettings());
278
 
    baseTextEditor->setExtraEncodingSettings(newSender->extraEncodingSettings());
279
 
 
280
 
    disconnect(oldSender, SIGNAL(typingSettingsChanged(TextEditor::TypingSettings)),
 
276
static void switchSettings_helper(const QObject *newSender, const QObject *oldSender,
 
277
                                                BaseTextEditorWidget *baseTextEditor)
 
278
{
 
279
    QObject::disconnect(oldSender, SIGNAL(typingSettingsChanged(TextEditor::TypingSettings)),
281
280
               baseTextEditor, SLOT(setTypingSettings(TextEditor::TypingSettings)));
282
 
    disconnect(oldSender, SIGNAL(storageSettingsChanged(TextEditor::StorageSettings)),
 
281
    QObject::disconnect(oldSender, SIGNAL(storageSettingsChanged(TextEditor::StorageSettings)),
283
282
               baseTextEditor, SLOT(setStorageSettings(TextEditor::StorageSettings)));
284
 
    disconnect(oldSender, SIGNAL(behaviorSettingsChanged(TextEditor::BehaviorSettings)),
 
283
    QObject::disconnect(oldSender, SIGNAL(behaviorSettingsChanged(TextEditor::BehaviorSettings)),
285
284
               baseTextEditor, SLOT(setBehaviorSettings(TextEditor::BehaviorSettings)));
286
 
    disconnect(oldSender, SIGNAL(extraEncodingSettingsChanged(TextEditor::ExtraEncodingSettings)),
 
285
    QObject::disconnect(oldSender, SIGNAL(extraEncodingSettingsChanged(TextEditor::ExtraEncodingSettings)),
287
286
               baseTextEditor, SLOT(setExtraEncodingSettings(TextEditor::ExtraEncodingSettings)));
288
287
 
289
 
    connect(newSender, SIGNAL(typingSettingsChanged(TextEditor::TypingSettings)),
 
288
    QObject::connect(newSender, SIGNAL(typingSettingsChanged(TextEditor::TypingSettings)),
290
289
            baseTextEditor, SLOT(setTypingSettings(TextEditor::TypingSettings)));
291
 
    connect(newSender, SIGNAL(storageSettingsChanged(TextEditor::StorageSettings)),
 
290
    QObject::connect(newSender, SIGNAL(storageSettingsChanged(TextEditor::StorageSettings)),
292
291
            baseTextEditor, SLOT(setStorageSettings(TextEditor::StorageSettings)));
293
 
    connect(newSender, SIGNAL(behaviorSettingsChanged(TextEditor::BehaviorSettings)),
 
292
    QObject::connect(newSender, SIGNAL(behaviorSettingsChanged(TextEditor::BehaviorSettings)),
294
293
            baseTextEditor, SLOT(setBehaviorSettings(TextEditor::BehaviorSettings)));
295
 
    connect(newSender, SIGNAL(extraEncodingSettingsChanged(TextEditor::ExtraEncodingSettings)),
 
294
    QObject::connect(newSender, SIGNAL(extraEncodingSettingsChanged(TextEditor::ExtraEncodingSettings)),
296
295
            baseTextEditor, SLOT(setExtraEncodingSettings(TextEditor::ExtraEncodingSettings)));
297
296
}
298
297
 
 
298
void EditorConfiguration::switchSettings(BaseTextEditorWidget *baseTextEditor) const
 
299
{
 
300
    if (d->m_useGlobal) {
 
301
        baseTextEditor->setTypingSettings(TextEditorSettings::typingSettings());
 
302
        baseTextEditor->setStorageSettings(TextEditorSettings::storageSettings());
 
303
        baseTextEditor->setBehaviorSettings(TextEditorSettings::behaviorSettings());
 
304
        baseTextEditor->setExtraEncodingSettings(TextEditorSettings::extraEncodingSettings());
 
305
        switchSettings_helper(TextEditorSettings::instance(), this, baseTextEditor);
 
306
    } else {
 
307
        baseTextEditor->setTypingSettings(typingSettings());
 
308
        baseTextEditor->setStorageSettings(storageSettings());
 
309
        baseTextEditor->setBehaviorSettings(behaviorSettings());
 
310
        baseTextEditor->setExtraEncodingSettings(extraEncodingSettings());
 
311
        switchSettings_helper(this, TextEditorSettings::instance(), baseTextEditor);
 
312
    }
 
313
}
 
314
 
299
315
void EditorConfiguration::setTypingSettings(const TextEditor::TypingSettings &settings)
300
316
{
301
317
    d->m_typingSettings = settings;
325
341
    d->m_textCodec = textCodec;
326
342
}
327
343
 
 
344
void EditorConfiguration::slotAboutToRemoveProject(ProjectExplorer::Project *project)
 
345
{
 
346
    if (project->editorConfiguration() != this)
 
347
        return;
 
348
 
 
349
    Core::DocumentModel *model = Core::EditorManager::documentModel();
 
350
    QList<Core::IEditor *> editors = model->editorsForDocuments(model->openedDocuments());
 
351
    foreach (Core::IEditor *editor, editors) {
 
352
        if (TextEditor::ITextEditor *textEditor = qobject_cast<TextEditor::ITextEditor*>(editor)) {
 
353
            Core::IDocument *document = editor->document();
 
354
            if (document) {
 
355
                Project *editorProject = SessionManager::projectForFile(document->filePath());
 
356
                if (project == editorProject)
 
357
                    deconfigureEditor(textEditor);
 
358
            }
 
359
        }
 
360
    }
 
361
}
 
362
 
328
363
TabSettings actualTabSettings(const QString &fileName,
329
 
                                     const BaseTextEditorWidget *baseTextEditor)
 
364
                              const BaseTextEditorWidget *baseTextEditor)
330
365
{
331
 
    if (baseTextEditor) {
 
366
    if (baseTextEditor)
332
367
        return baseTextEditor->tabSettings();
333
 
    } else {
334
 
        const SessionManager *session = ProjectExplorerPlugin::instance()->session();
335
 
        if (Project *project = session->projectForFile(fileName))
336
 
            return project->editorConfiguration()->codeStyle()->tabSettings();
337
 
        else
338
 
            return TextEditorSettings::instance()->codeStyle()->tabSettings();
339
 
    }
 
368
    if (Project *project = SessionManager::projectForFile(fileName))
 
369
        return project->editorConfiguration()->codeStyle()->tabSettings();
 
370
    return TextEditorSettings::codeStyle()->tabSettings();
340
371
}
341
372
 
342
373
} // ProjectExplorer