summaryrefslogtreecommitdiff
path: root/src/plugins/mesonprojectmanager/mesoninfoparser/buildoptions.h
blob: 83d4deee8c8d2b04194808ece04c2ebba84d574b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/****************************************************************************
**
** Copyright (C) 2020 Alexis Jeandet.
** Contact: https://www.qt.io/licensing/
**
** 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
** 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.
**
** 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.
**
****************************************************************************/

#pragma once

#include <utils/fileutils.h>
#include <utils/optional.h>

#include <QCheckBox>
#include <QComboBox>
#include <QLabel>
#include <QLineEdit>
#include <QSpinBox>
#include <QVariant>

namespace MesonProjectManager {
namespace Internal {

struct ComboData
{
    QString value() const { return m_choices.at(m_currentIndex != -1 ? m_currentIndex : 0); }
    void setValue(const QString &value) { m_currentIndex = m_choices.indexOf(value); }
    ComboData(const QStringList &choices, const QString &value)
        : m_choices{choices}
    {
        setValue(value);
    }
    ComboData() = default;
    const QStringList &choices() const { return m_choices; }
    int currentIndex() const { return m_currentIndex; }

private:
    QStringList m_choices;
    int m_currentIndex = 0;
};

struct FeatureData : ComboData
{
    FeatureData()
        : ComboData({"enabled", "disabled", "auto"}, "disabled")
    {}
    FeatureData(const QString &value)
        : ComboData({"enabled", "disabled", "auto"}, value)
    {}
};

} // namespace Internal
} // namespace MesonProjectManager
Q_DECLARE_METATYPE(MesonProjectManager::Internal::FeatureData);
Q_DECLARE_METATYPE(MesonProjectManager::Internal::ComboData);

namespace MesonProjectManager {
namespace Internal {

struct BuildOption
{
    enum class Type { integer, string, feature, combo, array, boolean, unknown };
    const QString name;
    const QString section;
    const QString description;
    const Utils::optional<QString> subproject;
    virtual ~BuildOption() {}
    virtual QVariant value() const = 0;
    virtual QString valueStr() const = 0;
    virtual void setValue(const QVariant &) = 0;
    virtual Type type() const = 0;
    virtual BuildOption *copy() const = 0;
    inline QString fullName() const
    {
        if (subproject)
            return QString("%1:%2").arg(*subproject).arg(name);
        return name;
    }
    inline virtual QString mesonArg() const
    {
        return QString("-D%1=%2").arg(fullName()).arg(valueStr());
    }
    BuildOption(const QString &name, const QString &section, const QString &description)
        : name{name.contains(":") ? name.split(":").last() : name}
        , section{section}
        , description{description}
        , subproject{name.contains(":") ? Utils::optional<QString>(name.split(":").first())
                                        : Utils::nullopt}
    {}
}; // namespace Internal

struct IntegerBuildOption final : BuildOption
{
    QVariant value() const override { return m_currentValue; }
    QString valueStr() const override { return QString::number(m_currentValue); }
    void setValue(const QVariant &value) override { m_currentValue = value.toInt(); }
    Type type() const override { return Type::integer; }
    BuildOption *copy() const override { return new IntegerBuildOption{*this}; }
    IntegerBuildOption(const QString &name,
                       const QString &section,
                       const QString &description,
                       const QVariant &value)
        : BuildOption(name, section, description)
        , m_currentValue{value.toInt()}
    {}

protected:
    int m_currentValue;
};

struct StringBuildOption : BuildOption
{
    QVariant value() const override { return m_currentValue; }
    QString valueStr() const override { return m_currentValue; }
    void setValue(const QVariant &value) override { m_currentValue = value.toString(); }
    Type type() const override { return Type::string; }
    BuildOption *copy() const override { return new StringBuildOption{*this}; }

    StringBuildOption(const QString &name,
                      const QString &section,
                      const QString &description,
                      const QVariant &value)
        : BuildOption(name, section, description)
        , m_currentValue{value.toString()}
    {}

protected:
    QString m_currentValue;
};

struct FeatureBuildOption final : BuildOption
{
    QVariant value() const override { return QVariant::fromValue(m_currentValue); }
    QString valueStr() const override { return m_currentValue.value(); }
    void setValue(const QVariant &value) override { m_currentValue.setValue(value.toString()); }
    Type type() const override { return Type::feature; }
    BuildOption *copy() const override { return new FeatureBuildOption{*this}; }
    FeatureBuildOption(const QString &name,
                       const QString &section,
                       const QString &description,
                       const QVariant &value)
        : BuildOption(name, section, description)
    {
        setValue(value);
    }

protected:
    FeatureData m_currentValue;
};

struct ComboBuildOption : BuildOption
{
    const QStringList &choices() const { return m_currentValue.choices(); }
    QVariant value() const override { return QVariant::fromValue(m_currentValue); }
    QString valueStr() const override { return m_currentValue.value(); }
    void setValue(const QVariant &value) override { m_currentValue.setValue(value.toString()); }
    Type type() const override { return Type::combo; }
    BuildOption *copy() const override { return new ComboBuildOption{*this}; }
    ComboBuildOption(const QString &name,
                     const QString &section,
                     const QString &description,
                     const QStringList &choices,
                     const QVariant &value)
        : BuildOption(name, section, description)
        , m_currentValue{choices, value.toString()}
    {}

protected:
    ComboData m_currentValue;
};

static inline QStringList quoteAll(const QStringList &values)
{
    QStringList quoted;
    std::transform(std::cbegin(values),
                   std::cend(values),
                   std::back_inserter(quoted),
                   [](const QString &v) {
                       if (v.front() == '\'' && v.back() == '\'')
                           return v;
                       return QString("\'%1\'").arg(v);
                   });
    return quoted;
}
struct ArrayBuildOption : BuildOption
{
    QVariant value() const override { return m_currentValue; }
    QString valueStr() const override { return m_currentValue.join(" "); }
    void setValue(const QVariant &value) override
    {
        m_currentValue = quoteAll(value.toStringList());
    }
    Type type() const override { return Type::array; }
    BuildOption *copy() const override { return new ArrayBuildOption{*this}; }
    inline virtual QString mesonArg() const override
    {
        return QString("-D%1=[%2]").arg(fullName()).arg(quoteAll(m_currentValue).join(','));
    }
    ArrayBuildOption(const QString &name,
                     const QString &section,
                     const QString &description,
                     const QVariant &value)
        : BuildOption(name, section, description)
        , m_currentValue{quoteAll(value.toStringList())}
    {}

protected:
    QStringList m_currentValue;
};

struct BooleanBuildOption final : BuildOption
{
    QVariant value() const override { return m_currentValue; }
    QString valueStr() const override
    {
        return m_currentValue ? QString{"true"} : QString{"false"};
    }
    void setValue(const QVariant &value) override { m_currentValue = value.toBool(); }
    Type type() const override { return Type::boolean; }
    BuildOption *copy() const override { return new BooleanBuildOption{*this}; }

    BooleanBuildOption(const QString &name,
                       const QString &section,
                       const QString &description,
                       const QVariant &value)
        : BuildOption(name, section, description)
    {
        setValue(value);
    }

protected:
    bool m_currentValue;
};

struct UnknownBuildOption : BuildOption
{
    QVariant value() const override { return {"Unknown option"}; }
    QString valueStr() const override { return {"Unknown option"}; }
    void setValue(const QVariant &) override {}
    Type type() const override { return Type::unknown; }
    BuildOption *copy() const override { return new UnknownBuildOption{*this}; }

    UnknownBuildOption(const QString &name, const QString &section, const QString &description)
        : BuildOption(name, section, description)
    {}
};

using BuildOptionsList = std::vector<std::unique_ptr<BuildOption>>;

} // namespace Internal
} // namespace MesonProjectManager