qmljstoolssettings.cpp 6.58 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
Jarek Kobus's avatar
Jarek Kobus committed
2
**
3 4
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
Jarek Kobus's avatar
Jarek Kobus committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
Jarek Kobus's avatar
Jarek Kobus committed
7
**
hjk's avatar
hjk committed
8 9 10 11
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
12 13 14
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
Jarek Kobus's avatar
Jarek Kobus committed
15
**
16 17 18 19 20 21 22
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
Jarek Kobus's avatar
Jarek Kobus committed
23
**
hjk's avatar
hjk committed
24
****************************************************************************/
Jarek Kobus's avatar
Jarek Kobus committed
25

Jarek Kobus's avatar
Jarek Kobus committed
26 27
#include "qmljstoolssettings.h"
#include "qmljstoolsconstants.h"
Jarek Kobus's avatar
Jarek Kobus committed
28
#include "qmljscodestylepreferencesfactory.h"
Jarek Kobus's avatar
Jarek Kobus committed
29 30

#include <texteditor/texteditorsettings.h>
Jarek Kobus's avatar
Jarek Kobus committed
31 32 33
#include <texteditor/simplecodestylepreferences.h>
#include <texteditor/tabsettings.h>
#include <texteditor/codestylepool.h>
Jarek Kobus's avatar
Jarek Kobus committed
34

35
#include <utils/settingsutils.h>
Jarek Kobus's avatar
Jarek Kobus committed
36 37
#include <utils/qtcassert.h>
#include <coreplugin/icore.h>
Jarek Kobus's avatar
Jarek Kobus committed
38

39
#include <QSettings>
Jarek Kobus's avatar
Jarek Kobus committed
40

hjk's avatar
hjk committed
41
using namespace TextEditor;
Jarek Kobus's avatar
Jarek Kobus committed
42 43 44

namespace QmlJSTools {

hjk's avatar
hjk committed
45
const char idKey[] = "QmlJSGlobal";
Jarek Kobus's avatar
Jarek Kobus committed
46

47
static SimpleCodeStylePreferences *m_globalCodeStyle = 0;
Jarek Kobus's avatar
Jarek Kobus committed
48 49 50 51

QmlJSToolsSettings::QmlJSToolsSettings(QObject *parent)
    : QObject(parent)
{
hjk's avatar
hjk committed
52
    QTC_ASSERT(!m_globalCodeStyle, return);
Jarek Kobus's avatar
Jarek Kobus committed
53

Jarek Kobus's avatar
Jarek Kobus committed
54
    // code style factory
hjk's avatar
hjk committed
55
    ICodeStylePreferencesFactory *factory = new QmlJSCodeStylePreferencesFactory();
56
    TextEditorSettings::registerCodeStyleFactory(factory);
Jarek Kobus's avatar
Jarek Kobus committed
57 58

    // code style pool
hjk's avatar
hjk committed
59
    CodeStylePool *pool = new CodeStylePool(factory, this);
60
    TextEditorSettings::registerCodeStylePool(Constants::QML_JS_SETTINGS_ID, pool);
Jarek Kobus's avatar
Jarek Kobus committed
61 62

    // global code style settings
hjk's avatar
hjk committed
63 64 65
    m_globalCodeStyle = new SimpleCodeStylePreferences(this);
    m_globalCodeStyle->setDelegatingPool(pool);
    m_globalCodeStyle->setDisplayName(tr("Global", "Settings"));
66
    m_globalCodeStyle->setId(idKey);
hjk's avatar
hjk committed
67
    pool->addCodeStyle(m_globalCodeStyle);
68
    TextEditorSettings::registerCodeStyle(QmlJSTools::Constants::QML_JS_SETTINGS_ID, m_globalCodeStyle);
Jarek Kobus's avatar
Jarek Kobus committed
69 70 71

    // built-in settings
    // Qt style
hjk's avatar
hjk committed
72
    SimpleCodeStylePreferences *qtCodeStyle = new SimpleCodeStylePreferences();
73
    qtCodeStyle->setId("qt");
Jarek Kobus's avatar
Jarek Kobus committed
74 75 76 77 78 79 80 81 82 83 84
    qtCodeStyle->setDisplayName(tr("Qt"));
    qtCodeStyle->setReadOnly(true);
    TabSettings qtTabSettings;
    qtTabSettings.m_tabPolicy = TabSettings::SpacesOnlyTabPolicy;
    qtTabSettings.m_tabSize = 4;
    qtTabSettings.m_indentSize = 4;
    qtTabSettings.m_continuationAlignBehavior = TabSettings::ContinuationAlignWithIndent;
    qtCodeStyle->setTabSettings(qtTabSettings);
    pool->addCodeStyle(qtCodeStyle);

    // default delegate for global preferences
hjk's avatar
hjk committed
85
    m_globalCodeStyle->setCurrentDelegate(qtCodeStyle);
Jarek Kobus's avatar
Jarek Kobus committed
86 87 88 89

    pool->loadCustomCodeStyles();

    // load global settings (after built-in settings are added to the pool)
90
    QSettings *s = Core::ICore::settings();
91
    m_globalCodeStyle->fromSettings(QLatin1String(QmlJSTools::Constants::QML_JS_SETTINGS_ID), s);
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115

    // legacy handling start (Qt Creator Version < 2.4)
    const bool legacyTransformed =
                s->value(QLatin1String("QmlJSTabPreferences/LegacyTransformed"), false).toBool();

    if (!legacyTransformed) {
        // creator 2.4 didn't mark yet the transformation (first run of creator 2.4)

        // we need to transform the settings only if at least one from
        // below settings was already written - otherwise we use
        // defaults like it would be the first run of creator 2.4 without stored settings
        const QStringList groups = s->childGroups();
        const bool needTransform = groups.contains(QLatin1String("textTabPreferences")) ||
                                   groups.contains(QLatin1String("QmlJSTabPreferences"));

        if (needTransform) {
            const QString currentFallback = s->value(QLatin1String("QmlJSTabPreferences/CurrentFallback")).toString();
            TabSettings legacyTabSettings;
            if (currentFallback == QLatin1String("QmlJSGlobal")) {
                // no delegate, global overwritten
                Utils::fromSettings(QLatin1String("QmlJSTabPreferences"),
                                    QString(), s, &legacyTabSettings);
            } else {
                // delegating to global
116
                legacyTabSettings = TextEditorSettings::codeStyle()->currentTabSettings();
117
            }
118 119

            // create custom code style out of old settings
hjk's avatar
hjk committed
120
            ICodeStylePreferences *oldCreator = pool->createCodeStyle(
121
                     "legacy", legacyTabSettings, QVariant(), tr("Old Creator"));
122 123

            // change the current delegate and save
hjk's avatar
hjk committed
124
            m_globalCodeStyle->setCurrentDelegate(oldCreator);
125
            m_globalCodeStyle->toSettings(QLatin1String(QmlJSTools::Constants::QML_JS_SETTINGS_ID), s);
126
        }
127 128
        // mark old settings as transformed
        s->setValue(QLatin1String("QmlJSTabPreferences/LegacyTransformed"), true);
129
        // legacy handling stop
Jarek Kobus's avatar
Jarek Kobus committed
130
    }
Jarek Kobus's avatar
Jarek Kobus committed
131 132

    // mimetypes to be handled
133 134 135 136 137 138
    TextEditorSettings::registerMimeTypeForLanguageId(Constants::QML_MIMETYPE, Constants::QML_JS_SETTINGS_ID);
    TextEditorSettings::registerMimeTypeForLanguageId(Constants::QBS_MIMETYPE, Constants::QML_JS_SETTINGS_ID);
    TextEditorSettings::registerMimeTypeForLanguageId(Constants::QMLPROJECT_MIMETYPE, Constants::QML_JS_SETTINGS_ID);
    TextEditorSettings::registerMimeTypeForLanguageId(Constants::QMLTYPES_MIMETYPE, Constants::QML_JS_SETTINGS_ID);
    TextEditorSettings::registerMimeTypeForLanguageId(Constants::JS_MIMETYPE, Constants::QML_JS_SETTINGS_ID);
    TextEditorSettings::registerMimeTypeForLanguageId(Constants::JSON_MIMETYPE, Constants::QML_JS_SETTINGS_ID);
Jarek Kobus's avatar
Jarek Kobus committed
139 140 141 142
}

QmlJSToolsSettings::~QmlJSToolsSettings()
{
143 144 145
    TextEditorSettings::unregisterCodeStyle(QmlJSTools::Constants::QML_JS_SETTINGS_ID);
    TextEditorSettings::unregisterCodeStylePool(QmlJSTools::Constants::QML_JS_SETTINGS_ID);
    TextEditorSettings::unregisterCodeStyleFactory(QmlJSTools::Constants::QML_JS_SETTINGS_ID);
jkobus's avatar
jkobus committed
146

hjk's avatar
hjk committed
147 148
    delete m_globalCodeStyle;
    m_globalCodeStyle = 0;
Jarek Kobus's avatar
Jarek Kobus committed
149 150
}

hjk's avatar
hjk committed
151
SimpleCodeStylePreferences *QmlJSToolsSettings::globalCodeStyle()
Jarek Kobus's avatar
Jarek Kobus committed
152
{
hjk's avatar
hjk committed
153
    return m_globalCodeStyle;
Jarek Kobus's avatar
Jarek Kobus committed
154 155
}

hjk's avatar
hjk committed
156
} // namespace QmlJSTools