cpptoolssettings.cpp 9.48 KB
Newer Older
Christian Kamm's avatar
Christian Kamm committed
1 2 3 4 5 6
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
Christian Kamm's avatar
Christian Kamm committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
**
**
** GNU Lesser General Public License Usage
**
** 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.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** 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.
**
** If you have questions regarding the use of this file, please contact
29
** Nokia at qt-info@nokia.com.
Christian Kamm's avatar
Christian Kamm committed
30 31 32
**
**************************************************************************/

33 34 35
#include "cpptoolssettings.h"
#include "cpptoolsconstants.h"
#include "cppcodestylepreferences.h"
Jarek Kobus's avatar
Jarek Kobus committed
36
#include "cppcodestylepreferencesfactory.h"
37 38

#include <texteditor/texteditorsettings.h>
Jarek Kobus's avatar
Jarek Kobus committed
39 40 41
#include <texteditor/texteditorsettings.h>
#include <texteditor/tabsettings.h>
#include <texteditor/codestylepool.h>
42

Jarek Kobus's avatar
Jarek Kobus committed
43
#include <utils/settingsutils.h>
44 45 46 47 48 49 50
#include <utils/qtcassert.h>
#include <coreplugin/icore.h>
#include <QtCore/QSettings>

static const char *idKey = "CppGlobal";

using namespace CppTools;
Jarek Kobus's avatar
Jarek Kobus committed
51
using TextEditor::TabSettings;
52 53 54 55

namespace CppTools {
namespace Internal {

Jarek Kobus's avatar
Jarek Kobus committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
class LegacySettings
{
public:
    LegacySettings()
        : m_legacyTransformed(false)
    { }
    void fromMap(const QString &prefix, const QVariantMap &map)
    {
        m_fallbackId = map.value(prefix + QLatin1String("CurrentFallback")).toString();
        m_legacyTransformed = map.value(prefix + QLatin1String("LegacyTransformed"), false).toBool();
    }
    void toMap(const QString &prefix, QVariantMap *map) const
    {
        map->insert(prefix + QLatin1String("LegacyTransformed"), true);
    }
    QString m_fallbackId;
    bool m_legacyTransformed;
};

75 76 77
class CppToolsSettingsPrivate
{
public:
Jarek Kobus's avatar
Jarek Kobus committed
78
    CppCodeStylePreferences *m_globalCodeStyle;
79 80 81 82 83 84 85 86 87
};

} // namespace Internal
} // namespace CppTools

CppToolsSettings *CppToolsSettings::m_instance = 0;

CppToolsSettings::CppToolsSettings(QObject *parent)
    : QObject(parent)
hjk's avatar
hjk committed
88
    , d(new Internal::CppToolsSettingsPrivate)
89 90 91
{
    QTC_ASSERT(!m_instance, return);
    m_instance = this;
Jarek Kobus's avatar
Jarek Kobus committed
92 93 94 95 96 97 98 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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
    qRegisterMetaType<CppTools::CppCodeStyleSettings>("CppTools::CppCodeStyleSettings");

    TextEditor::TextEditorSettings *textEditorSettings = TextEditor::TextEditorSettings::instance();

    // code style factory
    TextEditor::ICodeStylePreferencesFactory *factory = new CppTools::CppCodeStylePreferencesFactory();
    textEditorSettings->registerCodeStyleFactory(factory);

    // code style pool
    TextEditor::CodeStylePool *pool = new TextEditor::CodeStylePool(factory, this);
    textEditorSettings->registerCodeStylePool(Constants::CPP_SETTINGS_ID, pool);

    // global code style settings
    d->m_globalCodeStyle = new CppCodeStylePreferences(this);
    d->m_globalCodeStyle->setDelegatingPool(pool);
    d->m_globalCodeStyle->setDisplayName(tr("Global", "Settings"));
    d->m_globalCodeStyle->setId(idKey);
    pool->addCodeStyle(d->m_globalCodeStyle);
    textEditorSettings->registerCodeStyle(CppTools::Constants::CPP_SETTINGS_ID, d->m_globalCodeStyle);

    /*
    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();
    qtCodeStyle->setId(QLatin1String("qt"));
    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();
    gnuCodeStyle->setId(QLatin1String("gnu"));
    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();

    // load global settings (after built-in settings are added to the pool)
    if (QSettings *s = Core::ICore::instance()->settings()) {
        d->m_globalCodeStyle->fromSettings(CppTools::Constants::CPP_SETTINGS_ID, s);

        // legacy handling start (Qt Creator <= 2.3)
        Internal::LegacySettings legacySettings;

        TabSettings legacyTabSettings;
        Utils::fromSettings(QLatin1String("TabPreferences"),
                            QLatin1String("Cpp"), s, &legacySettings);
        if (legacySettings.m_fallbackId == QLatin1String("CppGlobal")) {
            Utils::fromSettings(QLatin1String("TabPreferences"),
                                QLatin1String("Cpp"), s, &legacyTabSettings);
        } else {
            legacyTabSettings = textEditorSettings->codeStyle()->currentTabSettings();
        }

        CppCodeStyleSettings legacyCodeStyleSettings;
        Utils::fromSettings(QLatin1String("CodeStyleSettings"),
                            QLatin1String("Cpp"), s, &legacySettings);
        if (!legacySettings.m_legacyTransformed
            && legacySettings.m_fallbackId == QLatin1String("CppGlobal")) {
            Utils::fromSettings(QLatin1String("CodeStyleSettings"),
                                QLatin1String("Cpp"), s, &legacyCodeStyleSettings);
            // create custom code style out of old settings
            QVariant v;
            v.setValue(legacyCodeStyleSettings);
            TextEditor::ICodeStylePreferences *oldCreator = pool->createCodeStyle(
                     QLatin1String("legacy"), legacyTabSettings,
                     v, tr("Old Creator"));
            // change the current delegate and save
            d->m_globalCodeStyle->setCurrentDelegate(oldCreator);
            d->m_globalCodeStyle->toSettings(CppTools::Constants::CPP_SETTINGS_ID, s);

            // mark old settings as transformed,
            // we create only once "Old Creator" custom settings
            Utils::toSettings(QLatin1String("CodeStyleSettings"),
                              QLatin1String("Cpp"), s, &legacySettings);
        }
        // legacy handling stop
215
    }
Jarek Kobus's avatar
Jarek Kobus committed
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230


    // mimetypes to be handled
    textEditorSettings->registerMimeTypeForLanguageId(
                QLatin1String(Constants::C_SOURCE_MIMETYPE),
                Constants::CPP_SETTINGS_ID);
    textEditorSettings->registerMimeTypeForLanguageId(
                QLatin1String(Constants::C_HEADER_MIMETYPE),
                Constants::CPP_SETTINGS_ID);
    textEditorSettings->registerMimeTypeForLanguageId(
                QLatin1String(Constants::CPP_SOURCE_MIMETYPE),
                Constants::CPP_SETTINGS_ID);
    textEditorSettings->registerMimeTypeForLanguageId(
                QLatin1String(Constants::CPP_HEADER_MIMETYPE),
                Constants::CPP_SETTINGS_ID);
231 232 233 234
}

CppToolsSettings::~CppToolsSettings()
{
hjk's avatar
hjk committed
235
    delete d;
236 237 238 239 240 241 242 243 244

    m_instance = 0;
}

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

Jarek Kobus's avatar
Jarek Kobus committed
245
CppCodeStylePreferences *CppToolsSettings::cppCodeStyle() const
246
{
Jarek Kobus's avatar
Jarek Kobus committed
247
    return d->m_globalCodeStyle;
248 249
}