cppcodemodelsettings.cpp 5.12 KB
Newer Older
1 2
/****************************************************************************
**
3 4
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
5 6 7 8 9 10 11
**
** This file is part of Qt Creator.
**
** 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 24 25 26
**
****************************************************************************/

#include "cppcodemodelsettings.h"
27 28

#include "clangdiagnosticconfigsmodel.h"
29 30
#include "cpptoolsconstants.h"

31 32
#include <utils/qtcassert.h>

33 34
#include <QSettings>

35 36
using namespace CppTools;

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
static Core::Id initialClangDiagnosticConfigId()
{ return Core::Id(Constants::CPP_CLANG_BUILTIN_CONFIG_ID_EVERYTHING_WITH_EXCEPTIONS); }

static CppCodeModelSettings::PCHUsage initialPchUsage()
{ return CppCodeModelSettings::PchUse_None; }

static QString clangDiagnosticConfigKey()
{ return QStringLiteral("ClangDiagnosticConfig"); }

static QString clangDiagnosticConfigsArrayKey()
{ return QStringLiteral("ClangDiagnosticConfigs"); }

static QString clangDiagnosticConfigsArrayIdKey()
{ return QLatin1String("id"); }

static QString clangDiagnosticConfigsArrayDisplayNameKey()
{ return QLatin1String("displayName"); }

static QString clangDiagnosticConfigsArrayOptionsKey()
{ return QLatin1String("diagnosticOptions"); }

static QString pchUsageKey()
{ return QLatin1String(Constants::CPPTOOLS_MODEL_MANAGER_PCH_USAGE); }
60

61 62 63
void CppCodeModelSettings::fromSettings(QSettings *s)
{
    s->beginGroup(QLatin1String(Constants::CPPTOOLS_SETTINGSGROUP));
64

65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
    const int size = s->beginReadArray(clangDiagnosticConfigsArrayKey());
    for (int i = 0; i < size; ++i) {
        s->setArrayIndex(i);

        ClangDiagnosticConfig config;
        config.setId(Core::Id::fromSetting(s->value(clangDiagnosticConfigsArrayIdKey())));
        config.setDisplayName(s->value(clangDiagnosticConfigsArrayDisplayNameKey()).toString());
        config.setCommandLineOptions(s->value(clangDiagnosticConfigsArrayOptionsKey()).toStringList());
        m_clangCustomDiagnosticConfigs.append(config);
    }
    s->endArray();

    const Core::Id diagnosticConfigId = Core::Id::fromSetting(
                                            s->value(clangDiagnosticConfigKey(),
                                                     initialClangDiagnosticConfigId().toSetting()));
    setClangDiagnosticConfigId(diagnosticConfigId);

    const QVariant pchUsageVariant = s->value(pchUsageKey(), initialPchUsage());
    setPCHUsage(static_cast<PCHUsage>(pchUsageVariant.toInt()));
84
    s->endGroup();
85 86

    emit changed();
87 88 89 90 91
}

void CppCodeModelSettings::toSettings(QSettings *s)
{
    s->beginGroup(QLatin1String(Constants::CPPTOOLS_SETTINGSGROUP));
92

93 94 95 96 97 98 99 100 101 102 103 104 105
    s->beginWriteArray(clangDiagnosticConfigsArrayKey());
    for (int i = 0, size = m_clangCustomDiagnosticConfigs.size(); i < size; ++i) {
        const ClangDiagnosticConfig &config = m_clangCustomDiagnosticConfigs.at(i);

        s->setArrayIndex(i);
        s->setValue(clangDiagnosticConfigsArrayIdKey(), config.id().toSetting());
        s->setValue(clangDiagnosticConfigsArrayDisplayNameKey(), config.displayName());
        s->setValue(clangDiagnosticConfigsArrayOptionsKey(), config.commandLineOptions());
    }
    s->endArray();

    s->setValue(clangDiagnosticConfigKey(), clangDiagnosticConfigId().toSetting());
    s->setValue(pchUsageKey(), pchUsage());
106

107
    s->endGroup();
108

109 110 111
    emit changed();
}

112 113 114 115 116 117
Core::Id CppCodeModelSettings::clangDiagnosticConfigId() const
{
    return m_clangDiagnosticConfigId;
}

void CppCodeModelSettings::setClangDiagnosticConfigId(const Core::Id &configId)
118
{
119 120 121 122 123 124 125 126
    m_clangDiagnosticConfigId = configId;
}

const ClangDiagnosticConfig CppCodeModelSettings::clangDiagnosticConfig() const
{
    const Internal::ClangDiagnosticConfigsModel configsModel(m_clangCustomDiagnosticConfigs);

    return configsModel.configWithId(clangDiagnosticConfigId());
127 128
}

129
ClangDiagnosticConfigs CppCodeModelSettings::clangCustomDiagnosticConfigs() const
130
{
131
    return m_clangCustomDiagnosticConfigs;
132 133
}

134
void CppCodeModelSettings::setClangCustomDiagnosticConfigs(const ClangDiagnosticConfigs &configs)
135
{
136
    m_clangCustomDiagnosticConfigs = configs;
137 138
}

139
CppCodeModelSettings::PCHUsage CppCodeModelSettings::pchUsage() const
140
{
141 142 143 144 145 146 147 148 149 150 151
    return m_pchUsage;
}

void CppCodeModelSettings::setPCHUsage(CppCodeModelSettings::PCHUsage pchUsage)
{
    m_pchUsage = pchUsage;
}

void CppCodeModelSettings::emitChanged()
{
    emit changed();
152
}