mesonoptions.h 4.88 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/* This file is part of KDevelop
    Copyright 2019 Daniel Mensinger <daniel@mensinger-ka.de>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#pragma once

#include <QString>
Eike Hein's avatar
Eike Hein committed
23
#include <QStringList>
24 25 26 27 28 29 30 31 32
#include <QVector>
#include <memory>

class QJsonArray;
class QJsonObject;
class QWidget;
class MesonOptionBase;
class MesonOptions;

33 34
using MesonOptionPtr = std::shared_ptr<MesonOptionBase>;
using MesonOptsPtr = std::shared_ptr<MesonOptions>;
35 36 37 38 39 40 41 42 43 44 45

/*!
 * Base class for a single meson option.
 */
class MesonOptionBase
{
public:
    enum Section { CORE, BACKEND, BASE, COMPILER, DIRECTORY, USER, TEST };
    enum Type { ARRAY, BOOLEAN, COMBO, INTEGER, STRING };

public:
46
    explicit MesonOptionBase(const QString& name, const QString& description, Section section);
47 48 49 50 51
    virtual ~MesonOptionBase();

    virtual Type type() const = 0;
    virtual QString value() const = 0;
    virtual QString initialValue() const = 0;
52
    virtual void setFromString(const QString& value) = 0;
53 54 55 56 57 58 59 60 61
    virtual void reset() = 0;

    QString name() const;
    QString description() const;
    Section section() const;

    QString mesonArg() const;
    bool isUpdated() const;

62
    static MesonOptionPtr fromJSON(const QJsonObject& obj);
63 64 65 66 67 68 69 70 71 72

private:
    QString m_name;
    QString m_description;
    Section m_section;
};

class MesonOptionArray : public MesonOptionBase
{
public:
73
    MesonOptionArray(const QString& name, const QString& description, Section section, QStringList value);
74 75 76 77

    MesonOptionBase::Type type() const override;
    QString value() const override;
    QString initialValue() const override;
78
    void setFromString(const QString& value) override;
79 80 81
    void reset() override;

    QStringList rawValue() const;
82
    void setValue(const QStringList& val);
83 84 85 86 87 88 89 90 91

private:
    QStringList m_value;
    QStringList m_initialValue;
};

class MesonOptionBool : public MesonOptionBase
{
public:
92
    MesonOptionBool(const QString& name, const QString& description, Section section, bool value);
93 94 95 96

    MesonOptionBase::Type type() const override;
    QString value() const override;
    QString initialValue() const override;
97
    void setFromString(const QString& value) override;
98 99 100 101 102 103 104 105 106 107 108 109 110
    void reset() override;

    bool rawValue() const;
    void setValue(bool val);

private:
    bool m_value;
    bool m_initialValue;
};

class MesonOptionCombo : public MesonOptionBase
{
public:
111
    MesonOptionCombo(const QString& name, const QString& description, Section section, QString value,
112
                     QStringList choices);
113 114 115 116

    MesonOptionBase::Type type() const override;
    QString value() const override;
    QString initialValue() const override;
117
    void setFromString(const QString& value) override;
118 119 120
    void reset() override;

    QString rawValue() const;
121
    void setValue(const QString& val);
122 123 124 125 126 127 128 129 130 131 132
    QStringList choices() const;

private:
    QString m_value;
    QString m_initialValue;
    QStringList m_choices;
};

class MesonOptionInteger : public MesonOptionBase
{
public:
133
    MesonOptionInteger(const QString& name, const QString& description, Section section, int value);
134 135 136 137

    MesonOptionBase::Type type() const override;
    QString value() const override;
    QString initialValue() const override;
138
    void setFromString(const QString& value) override;
139 140 141 142 143 144 145 146 147 148 149 150 151
    void reset() override;

    int rawValue() const;
    void setValue(int val);

private:
    int m_value;
    int m_initialValue;
};

class MesonOptionString : public MesonOptionBase
{
public:
152
    MesonOptionString(const QString& name, const QString& description, Section section, QString value);
153 154 155 156

    MesonOptionBase::Type type() const override;
    QString value() const override;
    QString initialValue() const override;
157
    void setFromString(const QString& value) override;
158 159 160
    void reset() override;

    QString rawValue() const;
161
    void setValue(const QString& val);
162 163 164 165 166 167 168 169 170 171 172 173

private:
    QString m_value;
    QString m_initialValue;
};

/*!
 * Container class for all meson project options.
 */
class MesonOptions
{
public:
174
    explicit MesonOptions(const QJsonArray& arr);
175 176 177 178 179

    int numChanged() const;
    QStringList getMesonArgs() const;
    void print() const;

180
    QVector<MesonOptionPtr> options();
181 182

private:
183
    QVector<MesonOptionPtr> m_options;
184

185
    void fromJSON(const QJsonArray& arr);
186
};