aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/mesonprojectmanager/project/buildoptions/optionsmodel/buildoptionsmodel.cpp
blob: 14fc20bbb1b2c20080f331f453434978d9a41c98 (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
// Copyright (C) 2020 Alexis Jeandet.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#include "buildoptionsmodel.h"

#include "arrayoptionlineedit.h"

#include <QComboBox>
#include <QLabel>
#include <QLineEdit>
#include <QMap>
#include <QStyledItemDelegate>
#include <QTextEdit>

namespace MesonProjectManager {
namespace Internal {

// this could be relaxed once we have something able to link reliably meson build type
// to QTC build type and update it, this must not break any features like tests/debug/profiling...
static const QStringList lockedOptions = {"buildtype", "debug", "backend", "optimization"};

inline Utils::TreeItem *makeBuildOptionTreeItem(CancellableOption *buildOption)
{
    return new BuildOptionTreeItem(buildOption);
}

BuidOptionsModel::BuidOptionsModel(QObject *parent)
    : Utils::TreeModel<>(parent)
{
    setHeader({tr("Key"), tr("Value")});
}

inline void groupPerSubprojectAndSection(
    const CancellableOptionsList &options,
    QMap<QString, QMap<QString, std::vector<CancellableOption *>>> &subprojectOptions,
    QMap<QString, std::vector<CancellableOption *>> &perSectionOptions)
{
    for (const std::unique_ptr<CancellableOption> &option : options) {
        if (option->subproject()) {
            subprojectOptions[*option->subproject()][option->section()].push_back(option.get());
        } else {
            perSectionOptions[option->section()].push_back(option.get());
        }
    }
}

void makeTree(Utils::TreeItem *root,
              const QMap<QString, std::vector<CancellableOption *>> &perSectioOptions)
{
    std::for_each(perSectioOptions.constKeyValueBegin(),
                  perSectioOptions.constKeyValueEnd(),
                  [root](const std::pair<QString, std::vector<CancellableOption *>> kv) {
                      const auto &options = kv.second;
                      auto sectionNode = new Utils::StaticTreeItem(kv.first);
                      for (CancellableOption *option : options) {
                          sectionNode->appendChild(makeBuildOptionTreeItem(option));
                      }
                      root->appendChild(sectionNode);
                  });
}

void BuidOptionsModel::setConfiguration(const BuildOptionsList &options)
{
    clear();
    m_options = decltype(m_options)();
    for (const BuildOptionsList::value_type &option : options) {
        m_options.emplace_back(
            std::make_unique<CancellableOption>(option.get(), lockedOptions.contains(option->name)));
    }
    {
        QMap<QString, QMap<QString, std::vector<CancellableOption *>>> subprojectOptions;
        QMap<QString, std::vector<CancellableOption *>> perSectionOptions;
        groupPerSubprojectAndSection(m_options, subprojectOptions, perSectionOptions);
        auto root = new Utils::TreeItem;
        makeTree(root, perSectionOptions);
        auto subProjects = new Utils::StaticTreeItem{"Subprojects"};
        std::for_each(subprojectOptions.constKeyValueBegin(),
                      subprojectOptions.constKeyValueEnd(),
                      [subProjects](
                          const std::pair<QString, QMap<QString, std::vector<CancellableOption *>>> kv) {
                          auto subProject = new Utils::StaticTreeItem{kv.first};
                          makeTree(subProject, kv.second);
                          subProjects->appendChild(subProject);
                      });
        root->appendChild(subProjects);
        setRootItem(root);
    }
}

bool BuidOptionsModel::setData(const QModelIndex &idx, const QVariant &data, int role)
{
    bool result = Utils::TreeModel<>::setData(idx, data, role);
    if (hasChanges())
        emit configurationChanged();
    return result;
}

QStringList BuidOptionsModel::changesAsMesonArgs()
{
    QStringList args;
    for (const std::unique_ptr<CancellableOption> &option : m_options) {
        if (option->hasChanged()) {
            args.push_back(option->mesonArg());
        }
    }
    return args;
}

bool BuidOptionsModel::hasChanges() const
{
    for (const std::unique_ptr<CancellableOption> &option : m_options) {
        if (option->hasChanged())
            return true;
    }
    return false;
}

QWidget *BuildOptionDelegate::makeWidget(QWidget *parent, const QVariant &data)
{
    auto type = data.userType();
    switch (type) {
    case QVariant::Int: {
        auto w = new QSpinBox{parent};
        w->setValue(data.toInt());
        return w;
    }
    case QVariant::Bool: {
        auto w = new QComboBox{parent};
        w->addItems({"false", "true"});
        w->setCurrentIndex(data.toBool());
        return w;
    }
    case QVariant::StringList: {
        auto w = new ArrayOptionLineEdit{parent};
        w->setPlainText(data.toStringList().join(" "));
        return w;
    }
    case QVariant::String: {
        auto w = new QLineEdit{parent};
        w->setText(data.toString());
        return w;
    }
    default: {
        if (type == qMetaTypeId<ComboData>()) {
            auto w = new QComboBox{parent};
            auto value = data.value<ComboData>();
            w->addItems(value.choices());
            w->setCurrentIndex(value.currentIndex());
            return w;
        }
        if (type == qMetaTypeId<FeatureData>()) {
            auto w = new QComboBox{parent};
            auto value = data.value<FeatureData>();
            w->addItems(value.choices());
            w->setCurrentIndex(value.currentIndex());
            return w;
        }
        return nullptr;
    }
    }
}

BuildOptionDelegate::BuildOptionDelegate(QObject *parent)
    : QStyledItemDelegate{parent}
{}

QWidget *BuildOptionDelegate::createEditor(QWidget *parent,
                                           const QStyleOptionViewItem &option,
                                           const QModelIndex &index) const
{
    auto data = index.data(Qt::EditRole);
    bool readOnly = index.data(Qt::UserRole).toBool();
    auto widget = makeWidget(parent, data);
    if (widget) {
        widget->setFocusPolicy(Qt::StrongFocus);
        widget->setDisabled(readOnly);
        return widget;
    }
    return QStyledItemDelegate::createEditor(parent, option, index);
}

void BuildOptionDelegate::setModelData(QWidget *editor,
                                       QAbstractItemModel *model,
                                       const QModelIndex &index) const
{
    ArrayOptionLineEdit *arrayWidget = qobject_cast<ArrayOptionLineEdit *>(editor);
    if (arrayWidget) {
        model->setData(index, QVariant::fromValue(arrayWidget->options()));
    } else {
        QStyledItemDelegate::setModelData(editor, model, index);
    }
}

} // namespace Internal
} // namespace MesonProjectManager