cpptoolssettings.cpp 10.5 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
2
**
3 4
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
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.
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.
23
**
hjk's avatar
hjk committed
24
****************************************************************************/
25

26
#include "cpptoolssettings.h"
27

28 29
#include "cpptoolsconstants.h"
#include "cppcodestylepreferences.h"
Jarek Kobus's avatar
Jarek Kobus committed
30
#include "cppcodestylepreferencesfactory.h"
31

32
#include <coreplugin/icore.h>
33 34
#include <texteditor/commentssettings.h>
#include <texteditor/completionsettingspage.h>
Jarek Kobus's avatar
Jarek Kobus committed
35
#include <texteditor/codestylepool.h>
36 37
#include <texteditor/tabsettings.h>
#include <texteditor/texteditorsettings.h>
38

39
#include <extensionsystem/pluginmanager.h>
40
#include <utils/qtcassert.h>
41 42
#include <utils/settingsutils.h>

43
#include <QSettings>
44

45
static const char idKey[] = "CppGlobal";
46

47
using namespace Core;
48
using namespace CppTools;
49
using namespace CppTools::Internal;
50
using namespace TextEditor;
51 52 53 54 55 56 57

namespace CppTools {
namespace Internal {

class CppToolsSettingsPrivate
{
public:
58 59 60 61
    CppToolsSettingsPrivate()
        : m_globalCodeStyle(0)
    {}

62
    CommentsSettings m_commentsSettings;
Jarek Kobus's avatar
Jarek Kobus committed
63
    CppCodeStylePreferences *m_globalCodeStyle;
64 65 66 67 68 69 70 71 72
};

} // namespace Internal
} // namespace CppTools

CppToolsSettings *CppToolsSettings::m_instance = 0;

CppToolsSettings::CppToolsSettings(QObject *parent)
    : QObject(parent)
hjk's avatar
hjk committed
73
    , d(new Internal::CppToolsSettingsPrivate)
74 75 76
{
    QTC_ASSERT(!m_instance, return);
    m_instance = this;
77

Jarek Kobus's avatar
Jarek Kobus committed
78 79
    qRegisterMetaType<CppTools::CppCodeStyleSettings>("CppTools::CppCodeStyleSettings");

80 81 82
    d->m_commentsSettings = TextEditorSettings::commentsSettings();
    connect(TextEditorSettings::instance(), &TextEditorSettings::commentsSettingsChanged,
            this, &CppToolsSettings::setCommentsSettings);
83

Jarek Kobus's avatar
Jarek Kobus committed
84
    // code style factory
85
    ICodeStylePreferencesFactory *factory = new CppCodeStylePreferencesFactory();
86
    TextEditorSettings::registerCodeStyleFactory(factory);
Jarek Kobus's avatar
Jarek Kobus committed
87 88

    // code style pool
89 90
    CodeStylePool *pool = new CodeStylePool(factory, this);
    TextEditorSettings::registerCodeStylePool(Constants::CPP_SETTINGS_ID, pool);
Jarek Kobus's avatar
Jarek Kobus committed
91 92 93 94 95

    // global code style settings
    d->m_globalCodeStyle = new CppCodeStylePreferences(this);
    d->m_globalCodeStyle->setDelegatingPool(pool);
    d->m_globalCodeStyle->setDisplayName(tr("Global", "Settings"));
96
    d->m_globalCodeStyle->setId(idKey);
Jarek Kobus's avatar
Jarek Kobus committed
97
    pool->addCodeStyle(d->m_globalCodeStyle);
98
    TextEditorSettings::registerCodeStyle(CppTools::Constants::CPP_SETTINGS_ID, d->m_globalCodeStyle);
Jarek Kobus's avatar
Jarek Kobus committed
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127

    /*
    For every language we have exactly 1 pool. The pool contains:
    1) All built-in code styles (Qt/GNU)
    2) All custom code styles (which will be added dynamically)
    3) A global code style

    If the code style gets a pool (setCodeStylePool()) it means it can behave
    like a proxy to one of the code styles from that pool
    (ICodeStylePreferences::setCurrentDelegate()).
    That's why the global code style gets a pool (it can point to any code style
    from the pool), while built-in and custom code styles don't get a pool
    (they can't point to any other code style).

    The instance of the language pool is shared. The same instance of the pool
    is used for all project code style settings and for global one.
    Project code style can point to one of built-in or custom code styles
    or to the global one as well. That's why the global code style is added
    to the pool. The proxy chain can look like:
    ProjectCodeStyle -> GlobalCodeStyle -> BuildInCodeStyle (e.g. Qt).

    With the global pool there is an exception - it gets a pool
    in which it exists itself. The case in which a code style point to itself
    is disallowed and is handled in ICodeStylePreferences::setCurrentDelegate().
    */

    // built-in settings
    // Qt style
    CppCodeStylePreferences *qtCodeStyle = new CppCodeStylePreferences();
128
    qtCodeStyle->setId("qt");
Jarek Kobus's avatar
Jarek Kobus committed
129 130 131 132 133 134 135 136 137 138 139 140
    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);

    // GNU style
    CppCodeStylePreferences *gnuCodeStyle = new CppCodeStylePreferences();
141
    gnuCodeStyle->setId("gnu");
Jarek Kobus's avatar
Jarek Kobus committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
    gnuCodeStyle->setDisplayName(tr("GNU"));
    gnuCodeStyle->setReadOnly(true);
    TabSettings gnuTabSettings;
    gnuTabSettings.m_tabPolicy = TabSettings::MixedTabPolicy;
    gnuTabSettings.m_tabSize = 8;
    gnuTabSettings.m_indentSize = 2;
    gnuTabSettings.m_continuationAlignBehavior = TabSettings::ContinuationAlignWithIndent;
    gnuCodeStyle->setTabSettings(gnuTabSettings);
    CppCodeStyleSettings gnuCodeStyleSettings;
    gnuCodeStyleSettings.indentNamespaceBody = true;
    gnuCodeStyleSettings.indentBlockBraces = true;
    gnuCodeStyleSettings.indentSwitchLabels = true;
    gnuCodeStyleSettings.indentBlocksRelativeToSwitchLabels = true;
    gnuCodeStyle->setCodeStyleSettings(gnuCodeStyleSettings);
    pool->addCodeStyle(gnuCodeStyle);

    // default delegate for global preferences
    d->m_globalCodeStyle->setCurrentDelegate(qtCodeStyle);

    pool->loadCustomCodeStyles();

163
    QSettings *s = ICore::settings();
Jarek Kobus's avatar
Jarek Kobus committed
164
    // load global settings (after built-in settings are added to the pool)
hjk's avatar
hjk committed
165
    d->m_globalCodeStyle->fromSettings(QLatin1String(CppTools::Constants::CPP_SETTINGS_ID), s);
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185

    // legacy handling start (Qt Creator Version < 2.4)
    const bool legacyTransformed =
                s->value(QLatin1String("CppCodeStyleSettings/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("CppTabPreferences")) ||
                                   groups.contains(QLatin1String("CppCodeStyleSettings"));
        if (needTransform) {
            CppCodeStyleSettings legacyCodeStyleSettings;
            if (groups.contains(QLatin1String("CppCodeStyleSettings"))) {
                Utils::fromSettings(QLatin1String("CppCodeStyleSettings"),
                                    QString(), s, &legacyCodeStyleSettings);
186
            }
187 188 189 190 191 192 193 194 195

            const QString currentFallback = s->value(QLatin1String("CppTabPreferences/CurrentFallback")).toString();
            TabSettings legacyTabSettings;
            if (currentFallback == QLatin1String("CppGlobal")) {
                // no delegate, global overwritten
                Utils::fromSettings(QLatin1String("CppTabPreferences"),
                                    QString(), s, &legacyTabSettings);
            } else {
                // delegating to global
196
                legacyTabSettings = TextEditorSettings::codeStyle()->currentTabSettings();
197 198 199 200 201
            }

            // create custom code style out of old settings
            QVariant v;
            v.setValue(legacyCodeStyleSettings);
202
            ICodeStylePreferences *oldCreator = pool->createCodeStyle(
203
                     "legacy", legacyTabSettings, v, tr("Old Creator"));
204 205 206

            // change the current delegate and save
            d->m_globalCodeStyle->setCurrentDelegate(oldCreator);
207
            d->m_globalCodeStyle->toSettings(QLatin1String(CppTools::Constants::CPP_SETTINGS_ID), s);
Jarek Kobus's avatar
Jarek Kobus committed
208
        }
209 210
        // mark old settings as transformed
        s->setValue(QLatin1String("CppCodeStyleSettings/LegacyTransformed"), true);
Jarek Kobus's avatar
Jarek Kobus committed
211
        // legacy handling stop
212
    }
Jarek Kobus's avatar
Jarek Kobus committed
213 214 215


    // mimetypes to be handled
216 217 218 219
    TextEditorSettings::registerMimeTypeForLanguageId(Constants::C_SOURCE_MIMETYPE, Constants::CPP_SETTINGS_ID);
    TextEditorSettings::registerMimeTypeForLanguageId(Constants::C_HEADER_MIMETYPE, Constants::CPP_SETTINGS_ID);
    TextEditorSettings::registerMimeTypeForLanguageId(Constants::CPP_SOURCE_MIMETYPE, Constants::CPP_SETTINGS_ID);
    TextEditorSettings::registerMimeTypeForLanguageId(Constants::CPP_HEADER_MIMETYPE, Constants::CPP_SETTINGS_ID);
220 221 222 223
}

CppToolsSettings::~CppToolsSettings()
{
224 225 226
    TextEditorSettings::unregisterCodeStyle(Constants::CPP_SETTINGS_ID);
    TextEditorSettings::unregisterCodeStylePool(Constants::CPP_SETTINGS_ID);
    TextEditorSettings::unregisterCodeStyleFactory(Constants::CPP_SETTINGS_ID);
jkobus's avatar
jkobus committed
227

hjk's avatar
hjk committed
228
    delete d;
229 230 231 232 233 234 235 236 237

    m_instance = 0;
}

CppToolsSettings *CppToolsSettings::instance()
{
    return m_instance;
}

Jarek Kobus's avatar
Jarek Kobus committed
238
CppCodeStylePreferences *CppToolsSettings::cppCodeStyle() const
239
{
Jarek Kobus's avatar
Jarek Kobus committed
240
    return d->m_globalCodeStyle;
241 242
}

243 244
const CommentsSettings &CppToolsSettings::commentsSettings() const
{
245 246 247 248 249 250
    return d->m_commentsSettings;
}

void CppToolsSettings::setCommentsSettings(const CommentsSettings &commentsSettings)
{
    d->m_commentsSettings = commentsSettings;
251
}
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269

static QString sortEditorDocumentOutlineKey()
{
    return QLatin1String(CppTools::Constants::CPPTOOLS_SETTINGSGROUP)
         + QLatin1Char('/')
         + QLatin1String(CppTools::Constants::CPPTOOLS_SORT_EDITOR_DOCUMENT_OUTLINE);
}

bool CppToolsSettings::sortedEditorDocumentOutline() const
{
    return ICore::settings()->value(sortEditorDocumentOutlineKey(), true).toBool();
}

void CppToolsSettings::setSortedEditorDocumentOutline(bool sorted)
{
    ICore::settings()->setValue(sortEditorDocumentOutlineKey(), sorted);
    emit editorDocumentOutlineSortingChanged(sorted);
}