analyzersettings.h 5.82 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

#ifndef ANALYZER_INTERNAL_ANALYZERSETTINGS_H
#define ANALYZER_INTERNAL_ANALYZERSETTINGS_H

34 35
#include <QObject>
#include <QVariant>
Mike McQuaid's avatar
Mike McQuaid committed
36 37 38

#include "analyzerbase_global.h"

hjk's avatar
hjk committed
39
#include <coreplugin/id.h>
Mike McQuaid's avatar
Mike McQuaid committed
40 41 42 43
#include <projectexplorer/runconfiguration.h>

namespace Analyzer {

44 45
class IAnalyzerTool;

Mike McQuaid's avatar
Mike McQuaid committed
46 47 48
/**
 * Utility function to set @p val if @p key is present in @p map.
 */
hjk's avatar
hjk committed
49
template <typename T> void setIfPresent(const QVariantMap &map, const QString &key, T *val)
Mike McQuaid's avatar
Mike McQuaid committed
50 51 52
{
    if (!map.contains(key))
        return;
53
    *val = map.value(key).template value<T>();
Mike McQuaid's avatar
Mike McQuaid committed
54 55 56 57
}

/**
 * Subclass this to add configuration to your analyzer tool.
58 59 60
 *
 * If global and project-specific settings differ for your tool,
 * create one subclass for each.
Mike McQuaid's avatar
Mike McQuaid committed
61 62 63 64
 */
class ANALYZER_EXPORT AbstractAnalyzerSubConfig : public QObject
{
    Q_OBJECT
hjk's avatar
hjk committed
65

Mike McQuaid's avatar
Mike McQuaid committed
66
public:
67
    AbstractAnalyzerSubConfig() {}
Mike McQuaid's avatar
Mike McQuaid committed
68

69
    /// return a list of default values
Mike McQuaid's avatar
Mike McQuaid committed
70
    virtual QVariantMap defaults() const = 0;
71
    /// convert current configuration into map for storage
Mike McQuaid's avatar
Mike McQuaid committed
72
    virtual QVariantMap toMap() const = 0;
73
    /// read configuration from @p map
74
    virtual void fromMap(const QVariantMap &map) = 0;
Mike McQuaid's avatar
Mike McQuaid committed
75

76
    /// unique ID for this configuration
hjk's avatar
hjk committed
77
    virtual Core::Id id() const = 0;
78
    /// user readable display name for this configuration
Mike McQuaid's avatar
Mike McQuaid committed
79
    virtual QString displayName() const = 0;
80
    /// create a configuration widget for this configuration
81
    virtual QWidget *createConfigWidget(QWidget *parent) = 0;
82 83
    /// clones s AbstractAnalyzerSubConfig
    virtual AbstractAnalyzerSubConfig *clone() = 0;
Mike McQuaid's avatar
Mike McQuaid committed
84 85 86 87 88 89 90 91 92 93 94
};

/**
 * Shared interface for the global and per-project settings.
 *
 * Use this to get the subConfig for your tool.
 */
class ANALYZER_EXPORT AnalyzerSettings : public QObject
{
    Q_OBJECT

hjk's avatar
hjk committed
95
public:
Mike McQuaid's avatar
Mike McQuaid committed
96
    template<class T>
97
    T *subConfig() const
Mike McQuaid's avatar
Mike McQuaid committed
98
    {
99 100 101 102 103
        foreach (AbstractAnalyzerSubConfig *subConfig, subConfigs()) {
            if (T *config = qobject_cast<T *>(subConfig))
                return config;
        }
        return 0;
Mike McQuaid's avatar
Mike McQuaid committed
104 105
    }

106
    QList<AbstractAnalyzerSubConfig *> subConfigs() const
Mike McQuaid's avatar
Mike McQuaid committed
107
    {
108
        return m_subConfigs;
Mike McQuaid's avatar
Mike McQuaid committed
109 110 111 112 113 114
    }

    QVariantMap defaults() const;
    virtual QVariantMap toMap() const;

protected:
115
    virtual void fromMap(const QVariantMap &map);
Mike McQuaid's avatar
Mike McQuaid committed
116

117
    QVariantMap toMap(const QList<AbstractAnalyzerSubConfig *> &subConfigs) const;
118
    void fromMap(const QVariantMap &map, QList<AbstractAnalyzerSubConfig *> *subConfigs);
119

Milian Wolff's avatar
Milian Wolff committed
120
    AnalyzerSettings(QObject *parent);
121
    AnalyzerSettings(const AnalyzerSettings *other);
122
    QList<AbstractAnalyzerSubConfig *> m_subConfigs;
Mike McQuaid's avatar
Mike McQuaid committed
123 124 125 126 127 128 129
};


// global and local settings are loaded and saved differently, and they also handle suppressions
// differently.
/**
 * Global settings
130 131 132 133 134
 *
 * To access your custom configuration use:
 * @code
 * AnalyzerGlobalSettings::instance()->subConfig<YourGlobalConfig>()->...
 * @endcode
Mike McQuaid's avatar
Mike McQuaid committed
135 136 137 138
 */
class ANALYZER_EXPORT AnalyzerGlobalSettings : public AnalyzerSettings
{
    Q_OBJECT
hjk's avatar
hjk committed
139

Mike McQuaid's avatar
Mike McQuaid committed
140 141 142 143 144 145 146
public:
    static AnalyzerGlobalSettings *instance();
    ~AnalyzerGlobalSettings();

    void writeSettings() const;
    void readSettings();

147
    void registerTool(IAnalyzerTool *tool);
Mike McQuaid's avatar
Mike McQuaid committed
148 149

private:
Milian Wolff's avatar
Milian Wolff committed
150
    AnalyzerGlobalSettings(QObject *parent);
Mike McQuaid's avatar
Mike McQuaid committed
151 152 153 154 155
    static AnalyzerGlobalSettings *m_instance;
};

/**
 * Settings associated with a single project/run configuration
156 157 158 159
 *
 * To access your custom configuration use:
 * @code
 * ProjectExplorer::RunConfiguration *rc = ...;
160
 * rc->extraAspect<AnalyzerRunConfigurationAspect>()->subConfig<YourProjectConfig>()->...
161
 * @endcode
Mike McQuaid's avatar
Mike McQuaid committed
162
 */
163
class ANALYZER_EXPORT AnalyzerRunConfigurationAspect
hjk's avatar
hjk committed
164
    : public AnalyzerSettings, public ProjectExplorer::IRunConfigurationAspect
Mike McQuaid's avatar
Mike McQuaid committed
165 166
{
    Q_OBJECT
hjk's avatar
hjk committed
167

Mike McQuaid's avatar
Mike McQuaid committed
168
public:
169
    AnalyzerRunConfigurationAspect();
170
    AnalyzerRunConfigurationAspect(const AnalyzerRunConfigurationAspect *other);
171
    ~AnalyzerRunConfigurationAspect();
Mike McQuaid's avatar
Mike McQuaid committed
172 173 174

    QString displayName() const;
    virtual QVariantMap toMap() const;
175
    AnalyzerRunConfigurationAspect *clone(ProjectExplorer::RunConfiguration *parent) const;
Mike McQuaid's avatar
Mike McQuaid committed
176

177 178 179 180 181 182
    bool isUsingGlobalSettings() const { return m_useGlobalSettings; }
    void setUsingGlobalSettings(bool value);
    void resetCustomToGlobalSettings();

    QList<AbstractAnalyzerSubConfig *> customSubConfigs() const { return m_customConfigurations; }

Mike McQuaid's avatar
Mike McQuaid committed
183
protected:
184
    virtual void fromMap(const QVariantMap &map);
185 186 187 188

private:
    bool m_useGlobalSettings;
    QList<AbstractAnalyzerSubConfig *> m_customConfigurations;
Mike McQuaid's avatar
Mike McQuaid committed
189 190
};

hjk's avatar
hjk committed
191
} // namespace Analyzer
Mike McQuaid's avatar
Mike McQuaid committed
192 193

#endif // ANALYZER_INTERNAL_ANALYZERSETTINGS_H