aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/compilerexplorer/compilerexploreraspects.cpp
blob: 52c776a65a6565dcb56fae7c3e9965e1384041f0 (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
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "compilerexploreraspects.h"
#include "compilerexplorertr.h"

#include "api/library.h"

#include <utils/algorithm.h>
#include <utils/layoutbuilder.h>

#include <QComboBox>
#include <QCompleter>
#include <QPushButton>
#include <QStackedWidget>

using namespace Utils;

namespace CompilerExplorer {

LibrarySelectionAspect::LibrarySelectionAspect(AspectContainer *container)
    : TypedAspect<QMap<QString, QString>>(container)
{}

void LibrarySelectionAspect::bufferToGui()
{
    if (!m_model)
        return;

    for (int i = 0; i < m_model->rowCount(); i++) {
        QModelIndex idx = m_model->index(i, 0);
        const QString libId = idx.data(LibraryData).value<Api::Library>().id;
        if (m_buffer.contains(libId))
            m_model->setData(idx, m_buffer[libId], SelectedVersion);
        else
            m_model->setData(idx, QVariant(), SelectedVersion);
    }

    handleGuiChanged();
}

bool LibrarySelectionAspect::guiToBuffer()
{
    if (!m_model)
        return false;

    auto oldBuffer = m_buffer;

    m_buffer.clear();

    for (int i = 0; i < m_model->rowCount(); i++) {
        if (m_model->item(i)->data(SelectedVersion).isValid()) {
            m_buffer.insert(qvariant_cast<Api::Library>(m_model->item(i)->data(LibraryData)).id,
                            m_model->item(i)->data(SelectedVersion).toString());
        }
    }
    return oldBuffer != m_buffer;
}

QVariantMap toVariantMap(const QMap<QString, QString> &map)
{
    QVariantMap variant;
    for (auto it = map.begin(); it != map.end(); ++it)
        variant.insert(it.key(), *it);
    return variant;
}

QVariant LibrarySelectionAspect::variantValue() const
{
    return toVariantMap(m_internal);
}

QVariant LibrarySelectionAspect::volatileVariantValue() const
{
    return toVariantMap(m_buffer);
}

QVariant LibrarySelectionAspect::defaultVariantValue() const
{
    return toVariantMap(defaultValue());
}

void LibrarySelectionAspect::setVariantValue(const QVariant &value, Announcement howToAnnounce)
{
    QMap<QString, QString> map;
    const Store store = storeFromVariant(value);
    for (auto it = store.begin(); it != store.end(); ++it)
        map[stringFromKey(it.key())] = it->toString();
    setValue(map, howToAnnounce);
}

void LibrarySelectionAspect::addToLayoutImpl(Layouting::Layout &parent)
{
    using namespace Layouting;

    QTC_ASSERT(m_fillCallback, return);

    auto cb = [this](const QList<QStandardItem *> &items) {
        for (QStandardItem *item : items)
            m_model->appendRow(item);

        bufferToGui();
    };

    if (!m_model) {
        m_model = new QStandardItemModel(this);

        connect(this, &LibrarySelectionAspect::refillRequested, this, [this, cb] {
            m_model->clear();
            m_fillCallback(cb);
        });

        m_fillCallback(cb);
    }

    QComboBox *nameCombo = new QComboBox();
    nameCombo->setInsertPolicy(QComboBox::InsertPolicy::NoInsert);
    nameCombo->setEditable(true);
    nameCombo->completer()->setCompletionMode(QCompleter::PopupCompletion);
    nameCombo->completer()->setFilterMode(Qt::MatchContains);

    nameCombo->setModel(m_model);

    QComboBox *versionCombo = new QComboBox();
    versionCombo->addItem("--");

    auto refreshVersionCombo = [nameCombo, versionCombo] {
        versionCombo->clear();
        versionCombo->addItem("--");
        QString selected = nameCombo->currentData(SelectedVersion).toString();
        Api::Library lib = qvariant_cast<Api::Library>(nameCombo->currentData(LibraryData));
        for (const auto &version : std::as_const(lib.versions)) {
            versionCombo->addItem(version.version, version.id);
            if (version.id == selected)
                versionCombo->setCurrentIndex(versionCombo->count() - 1);
        }
    };

    refreshVersionCombo();

    connect(nameCombo, &QComboBox::currentIndexChanged, this, refreshVersionCombo);

    connect(versionCombo, &QComboBox::activated, this, [this, nameCombo, versionCombo] {
        if (undoStack()) {
            QVariant old = m_model->data(m_model->index(nameCombo->currentIndex(), 0),
                                         SelectedVersion);
            undoStack()->push(new SelectLibraryVersionCommand(this,
                                                              nameCombo->currentIndex(),
                                                              versionCombo->currentData(),
                                                              old));

            handleGuiChanged();
            return;
        }

        m_model->setData(m_model->index(nameCombo->currentIndex(), 0),
                         versionCombo->currentData(),
                         SelectedVersion);
        handleGuiChanged();
    });

    QPushButton *clearBtn = new QPushButton("Clear All");
    connect(clearBtn, &QPushButton::clicked, clearBtn, [this, refreshVersionCombo] {
        if (undoStack()) {
            undoStack()->beginMacro(Tr::tr("Reset used libraries"));
            for (int i = 0; i < m_model->rowCount(); i++) {
                QModelIndex idx = m_model->index(i, 0);
                if (idx.data(SelectedVersion).isValid())
                    undoStack()->push(new SelectLibraryVersionCommand(this,
                                                                      i,
                                                                      QVariant(),
                                                                      idx.data(SelectedVersion)));
            }
            undoStack()->endMacro();

            handleGuiChanged();
            refreshVersionCombo();
            return;
        }

        for (int i = 0; i < m_model->rowCount(); i++)
            m_model->setData(m_model->index(i, 0), QVariant(), SelectedVersion);
        handleGuiChanged();
        refreshVersionCombo();
    });

    ElidingLabel *displayLabel = new ElidingLabel();

    auto updateLabel = [displayLabel, this] {
        QStringList libs;
        for (int i = 0; i < m_model->rowCount(); i++) {
            QModelIndex idx = m_model->index(i, 0);
            if (idx.data(LibraryData).isValid() && idx.data(SelectedVersion).isValid()) {
                auto libData = idx.data(LibraryData).value<Api::Library>();
                auto id = idx.data(SelectedVersion).toString();

                auto versionIt = std::find_if(libData.versions.begin(),
                                              libData.versions.end(),
                                              [id](const Api::Library::Version &v) {
                                                  return v.id == id;
                                              });
                const QString versionName = versionIt == libData.versions.end()
                                                ? id
                                                : versionIt->version;

                libs.append(QString("%1 %2").arg(libData.name).arg(versionName));
            }
        }
        if (libs.empty())
            displayLabel->setText(Tr::tr("No libraries selected"));
        else
            displayLabel->setText(libs.join(", "));
    };

    connect(m_model, &QStandardItemModel::itemChanged, displayLabel, updateLabel);

    updateLabel();

    QPushButton *editBtn = new QPushButton(Tr::tr("Edit"));

    // clang-format off
    QStackedWidget *stack = static_cast<QStackedWidget*>(
        Stack {
            Row { noMargin, displayLabel, editBtn },
            Row { noMargin, nameCombo, versionCombo, clearBtn }
        }.emerge()
    );
    // clang-format on
    stack->setContentsMargins({});
    connect(editBtn, &QPushButton::clicked, stack, [stack] { stack->setCurrentIndex(1); });
    connect(this, &LibrarySelectionAspect::returnToDisplay, stack, [stack] {
        stack->setCurrentIndex(0);
    });

    addLabeledItem(parent, stack);
}

} // namespace CompilerExplorer