analyzersettings.cpp 6.84 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
Mike McQuaid's avatar
Mike McQuaid committed
2
**
3
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
Mike McQuaid's avatar
Mike McQuaid committed
5 6
** Author: Milian Wolff, KDAB (milian.wolff@kdab.com)
**
hjk's avatar
hjk committed
7
** This file is part of Qt Creator.
Mike McQuaid's avatar
Mike McQuaid committed
8
**
hjk's avatar
hjk committed
9 10 11 12 13 14 15
** 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
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
Mike McQuaid's avatar
Mike McQuaid committed
16 17
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
18 19 20 21 22 23 24 25 26
** Alternatively, 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, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
Mike McQuaid's avatar
Mike McQuaid committed
27 28
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
29
****************************************************************************/
Mike McQuaid's avatar
Mike McQuaid committed
30 31 32 33 34 35 36 37 38 39 40

#include "analyzersettings.h"

#include "analyzermanager.h"
#include "ianalyzertool.h"
#include "analyzerplugin.h"
#include "analyzeroptionspage.h"

#include <coreplugin/icore.h>
#include <utils/qtcassert.h>

41
#include <QSettings>
Mike McQuaid's avatar
Mike McQuaid committed
42 43 44

using namespace Analyzer::Internal;

Friedemann Kleint's avatar
Friedemann Kleint committed
45
static const char groupC[] = "Analyzer";
46
static const char useGlobalC[] = "Analyzer.Project.UseGlobal";
Mike McQuaid's avatar
Mike McQuaid committed
47

hjk's avatar
hjk committed
48 49
namespace Analyzer {

Mike McQuaid's avatar
Mike McQuaid committed
50 51
AnalyzerGlobalSettings *AnalyzerGlobalSettings::m_instance = 0;

Milian Wolff's avatar
Milian Wolff committed
52
AnalyzerSettings::AnalyzerSettings(QObject *parent)
hjk's avatar
hjk committed
53
    : QObject(parent)
Mike McQuaid's avatar
Mike McQuaid committed
54 55 56
{
}

57
AnalyzerSettings::AnalyzerSettings(const AnalyzerSettings *other)
58
{
59
    Q_UNUSED(other);
60 61
}

62 63 64 65 66 67 68 69 70
QVariantMap AnalyzerSettings::defaults() const
{
    QVariantMap map;
    foreach (AbstractAnalyzerSubConfig *config, subConfigs()) {
        map.unite(config->defaults());
    }
    return map;
}

71
void AnalyzerSettings::fromMap(const QVariantMap &map)
72
{
73
    fromMap(map, &m_subConfigs);
74 75
}

76
void AnalyzerSettings::fromMap(const QVariantMap &map, QList<AbstractAnalyzerSubConfig *> *subConfigs)
Mike McQuaid's avatar
Mike McQuaid committed
77
{
78 79
    foreach (AbstractAnalyzerSubConfig *config, *subConfigs)
        config->fromMap(map);
Mike McQuaid's avatar
Mike McQuaid committed
80 81
}

82
QVariantMap AnalyzerSettings::toMap() const
Mike McQuaid's avatar
Mike McQuaid committed
83
{
84
    return toMap(m_subConfigs);
Mike McQuaid's avatar
Mike McQuaid committed
85 86
}

87
QVariantMap AnalyzerSettings::toMap(const QList<AbstractAnalyzerSubConfig *> &subConfigs) const
Mike McQuaid's avatar
Mike McQuaid committed
88 89
{
    QVariantMap map;
90
    foreach (AbstractAnalyzerSubConfig *config, subConfigs) {
Mike McQuaid's avatar
Mike McQuaid committed
91 92 93 94 95
        map.unite(config->toMap());
    }
    return map;
}

96

Milian Wolff's avatar
Milian Wolff committed
97 98
AnalyzerGlobalSettings::AnalyzerGlobalSettings(QObject *parent)
: AnalyzerSettings(parent)
Mike McQuaid's avatar
Mike McQuaid committed
99 100 101 102 103 104 105 106
{
    QTC_ASSERT(!m_instance, return);
    m_instance = this;
}

AnalyzerGlobalSettings *AnalyzerGlobalSettings::instance()
{
    if (!m_instance)
Milian Wolff's avatar
Milian Wolff committed
107 108
        m_instance = new AnalyzerGlobalSettings(AnalyzerPlugin::instance());

Mike McQuaid's avatar
Mike McQuaid committed
109 110 111 112 113 114
    return m_instance;
}

AnalyzerGlobalSettings::~AnalyzerGlobalSettings()
{
    m_instance = 0;
115
    qDeleteAll(m_subConfigs);
Mike McQuaid's avatar
Mike McQuaid committed
116 117 118 119
}

void AnalyzerGlobalSettings::readSettings()
{
hjk's avatar
hjk committed
120
    QSettings *settings = Core::ICore::settings();
Mike McQuaid's avatar
Mike McQuaid committed
121 122 123

    QVariantMap map;

Friedemann Kleint's avatar
Friedemann Kleint committed
124
    settings->beginGroup(QLatin1String(groupC));
Mike McQuaid's avatar
Mike McQuaid committed
125 126 127 128 129 130 131 132 133 134 135 136
    // read the values from config, using the keys from the defaults value map
    const QVariantMap def = defaults();
    for (QVariantMap::ConstIterator it = def.constBegin(); it != def.constEnd(); ++it)
        map.insert(it.key(), settings->value(it.key(), it.value()));
    settings->endGroup();

    // apply the values to our member variables
    fromMap(map);
}

void AnalyzerGlobalSettings::writeSettings() const
{
hjk's avatar
hjk committed
137
    QSettings *settings = Core::ICore::settings();
Friedemann Kleint's avatar
Friedemann Kleint committed
138
    settings->beginGroup(QLatin1String(groupC));
Mike McQuaid's avatar
Mike McQuaid committed
139 140 141 142 143 144
    const QVariantMap map = toMap();
    for (QVariantMap::ConstIterator it = map.begin(); it != map.end(); ++it)
        settings->setValue(it.key(), it.value());
    settings->endGroup();
}

145
void AnalyzerGlobalSettings::registerTool(IAnalyzerTool *tool)
Mike McQuaid's avatar
Mike McQuaid committed
146
{
147 148 149 150 151 152
    AbstractAnalyzerSubConfig *config = tool->createGlobalSettings();
    if (config) {
        m_subConfigs.append(config);
        AnalyzerPlugin::instance()->addAutoReleasedObject(new AnalyzerOptionsPage(config));
        readSettings();
    }
Mike McQuaid's avatar
Mike McQuaid committed
153 154 155
}


156
AnalyzerRunConfigurationAspect::AnalyzerRunConfigurationAspect()
157
    : AnalyzerSettings((QObject *)0), m_useGlobalSettings(true)
Mike McQuaid's avatar
Mike McQuaid committed
158
{
159
    QList<IAnalyzerTool*> tools = AnalyzerManager::tools();
Mike McQuaid's avatar
Mike McQuaid committed
160
    // add sub configs
161 162 163 164
    foreach (IAnalyzerTool *tool, tools) {
        AbstractAnalyzerSubConfig *config = tool->createProjectSettings();
        if (config)
            m_customConfigurations.append(config);
165
    }
Mike McQuaid's avatar
Mike McQuaid committed
166

167 168 169 170
    m_subConfigs = AnalyzerGlobalSettings::instance()->subConfigs();
    resetCustomToGlobalSettings();
}

171
AnalyzerRunConfigurationAspect::AnalyzerRunConfigurationAspect(const AnalyzerRunConfigurationAspect *other)
172 173 174 175 176 177 178 179 180 181 182 183
    : AnalyzerSettings(other), m_useGlobalSettings(other->m_useGlobalSettings)
{

    foreach (AbstractAnalyzerSubConfig *config, other->m_customConfigurations)
        m_customConfigurations.append(config->clone());

    if (m_useGlobalSettings)
        m_subConfigs = AnalyzerGlobalSettings::instance()->subConfigs();
    else
        m_subConfigs = m_customConfigurations;
}

184
AnalyzerRunConfigurationAspect::~AnalyzerRunConfigurationAspect()
185 186
{
    qDeleteAll(m_customConfigurations);
Mike McQuaid's avatar
Mike McQuaid committed
187 188
}

189
QString AnalyzerRunConfigurationAspect::displayName() const
Mike McQuaid's avatar
Mike McQuaid committed
190 191 192 193
{
    return tr("Analyzer Settings");
}

194
void AnalyzerRunConfigurationAspect::fromMap(const QVariantMap &map)
Mike McQuaid's avatar
Mike McQuaid committed
195
{
196
    AnalyzerSettings::fromMap(map, &m_customConfigurations);
197
    m_useGlobalSettings = map.value(QLatin1String(useGlobalC), true).toBool();
Mike McQuaid's avatar
Mike McQuaid committed
198 199
}

200
QVariantMap AnalyzerRunConfigurationAspect::toMap() const
Mike McQuaid's avatar
Mike McQuaid committed
201
{
202 203 204 205 206
    QVariantMap map = AnalyzerSettings::toMap(m_customConfigurations);
    map.insert(QLatin1String(useGlobalC), m_useGlobalSettings);
    return map;
}

207 208 209 210 211 212 213
AnalyzerRunConfigurationAspect *AnalyzerRunConfigurationAspect::clone(
        ProjectExplorer::RunConfiguration *parent) const
{
    Q_UNUSED(parent)
    return new AnalyzerRunConfigurationAspect(this);
}

214
void AnalyzerRunConfigurationAspect::setUsingGlobalSettings(bool value)
215 216 217 218
{
    if (value == m_useGlobalSettings)
        return;
    m_useGlobalSettings = value;
219
    if (m_useGlobalSettings)
220
        m_subConfigs = AnalyzerGlobalSettings::instance()->subConfigs();
221
    else
222 223 224
        m_subConfigs = m_customConfigurations;
}

225
void AnalyzerRunConfigurationAspect::resetCustomToGlobalSettings()
226 227 228
{
    AnalyzerGlobalSettings *gs = AnalyzerGlobalSettings::instance();
    AnalyzerSettings::fromMap(gs->toMap(), &m_customConfigurations);
Mike McQuaid's avatar
Mike McQuaid committed
229
}
hjk's avatar
hjk committed
230 231

} // namespace Analyzer