analyzersettings.cpp 6.39 KB
Newer Older
Mike McQuaid's avatar
Mike McQuaid committed
1 2 3 4
/**************************************************************************
**
** This file is part of Qt Creator Instrumentation Tools
**
hjk's avatar
hjk committed
5
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
Mike McQuaid's avatar
Mike McQuaid committed
6 7 8
**
** Author: Milian Wolff, KDAB (milian.wolff@kdab.com)
**
Eike Ziller's avatar
Eike Ziller committed
9
** Contact: http://www.qt-project.org/
Mike McQuaid's avatar
Mike McQuaid committed
10 11 12 13
**
**
** GNU Lesser General Public License Usage
**
hjk's avatar
hjk committed
14 15 16 17 18 19
** 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.
Mike McQuaid's avatar
Mike McQuaid committed
20 21
**
** In addition, as a special exception, Nokia gives you certain additional
hjk's avatar
hjk committed
22
** rights. These rights are described in the Nokia Qt LGPL Exception
Mike McQuaid's avatar
Mike McQuaid committed
23 24
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
25 26 27 28 29
** 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.
**
Mike McQuaid's avatar
Mike McQuaid committed
30 31 32 33 34 35 36 37 38 39 40 41 42
**
**************************************************************************/

#include "analyzersettings.h"

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

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

43
#include <QSettings>
Mike McQuaid's avatar
Mike McQuaid committed
44 45 46

using namespace Analyzer::Internal;

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

hjk's avatar
hjk committed
50 51
namespace Analyzer {

Mike McQuaid's avatar
Mike McQuaid committed
52 53
AnalyzerGlobalSettings *AnalyzerGlobalSettings::m_instance = 0;

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

59 60
AnalyzerSettings::AnalyzerSettings(AnalyzerSettings *other)
{
61
    Q_UNUSED(other);
62 63
}

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

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

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

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

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

98

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

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

Mike McQuaid's avatar
Mike McQuaid committed
111 112 113 114 115 116
    return m_instance;
}

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

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

    QVariantMap map;

Friedemann Kleint's avatar
Friedemann Kleint committed
126
    settings->beginGroup(QLatin1String(groupC));
Mike McQuaid's avatar
Mike McQuaid committed
127 128 129 130 131 132 133 134 135 136 137 138
    // 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
139
    QSettings *settings = Core::ICore::settings();
Friedemann Kleint's avatar
Friedemann Kleint committed
140
    settings->beginGroup(QLatin1String(groupC));
Mike McQuaid's avatar
Mike McQuaid committed
141 142 143 144 145 146
    const QVariantMap map = toMap();
    for (QVariantMap::ConstIterator it = map.begin(); it != map.end(); ++it)
        settings->setValue(it.key(), it.value());
    settings->endGroup();
}

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


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

169 170 171 172
    m_subConfigs = AnalyzerGlobalSettings::instance()->subConfigs();
    resetCustomToGlobalSettings();
}

173 174 175 176 177 178 179 180 181 182 183 184 185
AnalyzerRunConfigurationAspect::AnalyzerRunConfigurationAspect(AnalyzerRunConfigurationAspect *other)
    : 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;
}

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

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

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

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

209
void AnalyzerRunConfigurationAspect::setUsingGlobalSettings(bool value)
210 211 212 213 214 215 216 217 218 219 220
{
    if (value == m_useGlobalSettings)
        return;
    m_useGlobalSettings = value;
    if (m_useGlobalSettings) {
        m_subConfigs = AnalyzerGlobalSettings::instance()->subConfigs();
    } else {
        m_subConfigs = m_customConfigurations;
    }
}

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

} // namespace Analyzer