aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/baremetal/debugserverproviderssettingspage.cpp
blob: ba34d4e591246bcf8b77a7eb3066f57e9ac9cfbe (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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
// Copyright (C) 2016 Denis Shienkov <denis.shienkov@gmail.com>
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#include "baremetalconstants.h"

#include "debugserverprovidermanager.h"
#include "debugserverproviderssettingspage.h"
#include "idebugserverprovider.h"

#include <coreplugin/icore.h>
#include <extensionsystem/pluginmanager.h>
#include <projectexplorer/projectexplorerconstants.h>

#include <utils/algorithm.h>
#include <utils/detailswidget.h>
#include <utils/qtcassert.h>

#include <QAction>
#include <QApplication>
#include <QGroupBox>
#include <QHBoxLayout>
#include <QHeaderView>
#include <QItemSelectionModel>
#include <QMenu>
#include <QMessageBox>
#include <QPushButton>
#include <QSpacerItem>
#include <QTextStream>
#include <QTreeView>
#include <QVBoxLayout>

using namespace Debugger;
using namespace Utils;

namespace BareMetal {
namespace Internal {

// DebugServerProviderNode

enum {
    ProviderNameColumn = 0,
    ProviderTypeColumn,
    ProviderEngineColumn
};

static QString engineTypeName(DebuggerEngineType engineType)
{
    switch (engineType) {
    case NoEngineType:
        return DebugServerProviderModel::tr("Not recognized");
    case GdbEngineType:
        return DebugServerProviderModel::tr("GDB");
    case UvscEngineType:
        return DebugServerProviderModel::tr("UVSC");
    default:
        return {};
    }
}

static QString engineTypeDescription(DebuggerEngineType engineType)
{
    switch (engineType) {
    case NoEngineType:
        return DebugServerProviderModel::tr("Not recognized");
    case GdbEngineType:
        return DebugServerProviderModel::tr("GDB compatible provider engine\n" \
                                            "(used together with the GDB debuggers).");
    case UvscEngineType:
        return DebugServerProviderModel::tr("UVSC compatible provider engine\n" \
                                            "(used together with the KEIL uVision).");
    default:
        return {};
    }
}

class DebugServerProviderNode final : public TreeItem
{
public:
    explicit DebugServerProviderNode(IDebugServerProvider *provider, bool changed = false)
        : provider(provider), changed(changed)
    {
    }

    QVariant data(int column, int role) const final
    {
        if (role == Qt::FontRole) {
            QFont f = QApplication::font();
            if (changed)
                f.setBold(true);
            return f;
        }

        if (role == Qt::DisplayRole) {
            if (column == ProviderNameColumn)
                return provider->displayName();
            if (column == ProviderTypeColumn)
                return provider->typeDisplayName();
            if (column == ProviderEngineColumn)
                return engineTypeName(provider->engineType());
        } else if (role == Qt::ToolTipRole) {
            if (column == ProviderEngineColumn)
                return engineTypeDescription(provider->engineType());
        }

        return {};
    }

    IDebugServerProvider *provider = nullptr;
    IDebugServerProviderConfigWidget *widget = nullptr;
    bool changed = false;
};

// DebugServerProviderModel

DebugServerProviderModel::DebugServerProviderModel()
{
    setHeader({tr("Name"), tr("Type"), tr("Engine")});

    const DebugServerProviderManager *manager = DebugServerProviderManager::instance();

    connect(manager, &DebugServerProviderManager::providerAdded,
            this, &DebugServerProviderModel::addProvider);
    connect(manager, &DebugServerProviderManager::providerRemoved,
            this, &DebugServerProviderModel::removeProvider);

    for (IDebugServerProvider *p : DebugServerProviderManager::providers())
        addProvider(p);
}

IDebugServerProvider *DebugServerProviderModel::provider(const QModelIndex &index) const
{
    if (const DebugServerProviderNode *node = nodeForIndex(index))
        return node->provider;

    return nullptr;
}

DebugServerProviderNode *DebugServerProviderModel::nodeForIndex(const QModelIndex &index) const
{
    if (!index.isValid())
        return nullptr;

    return static_cast<DebugServerProviderNode *>(itemForIndex(index));
}

void DebugServerProviderModel::apply()
{
    // Remove unused providers
    for (IDebugServerProvider *provider : qAsConst(m_providersToRemove))
        DebugServerProviderManager::deregisterProvider(provider);
    QTC_ASSERT(m_providersToRemove.isEmpty(), m_providersToRemove.clear());

    // Update providers
    for (TreeItem *item : *rootItem()) {
        const auto n = static_cast<DebugServerProviderNode *>(item);
        if (!n->changed)
            continue;

        QTC_CHECK(n->provider);
        if (n->widget)
            n->widget->apply();

        n->changed = false;
        n->update();
    }

    // Add new (and already updated) providers
    QStringList skippedProviders;
    for (IDebugServerProvider *provider: qAsConst(m_providersToAdd)) {
        if (!DebugServerProviderManager::registerProvider(provider))
            skippedProviders << provider->displayName();
    }

    m_providersToAdd.clear();

    if (!skippedProviders.isEmpty()) {
        QMessageBox::warning(Core::ICore::dialogParent(),
                             tr("Duplicate Providers Detected"),
                             tr("The following providers were already configured:<br>"
                                "&nbsp;%1<br>"
                                "They were not configured again.")
                             .arg(skippedProviders.join(",<br>&nbsp;")));
    }
}

DebugServerProviderNode *DebugServerProviderModel::findNode(const IDebugServerProvider *provider) const
{
    auto test = [provider](TreeItem *item) {
        return static_cast<DebugServerProviderNode *>(item)->provider == provider;
    };

    return static_cast<DebugServerProviderNode *>(Utils::findOrDefault(*rootItem(), test));
}

QModelIndex DebugServerProviderModel::indexForProvider(IDebugServerProvider *provider) const
{
    const DebugServerProviderNode *n = findNode(provider);
    return n ? indexForItem(n) : QModelIndex();
}

void DebugServerProviderModel::markForRemoval(IDebugServerProvider *provider)
{
    DebugServerProviderNode *n = findNode(provider);
    QTC_ASSERT(n, return);
    destroyItem(n);

    if (m_providersToAdd.contains(provider)) {
        m_providersToAdd.removeOne(provider);
        delete provider;
    } else {
        m_providersToRemove.append(provider);
    }
}

void DebugServerProviderModel::markForAddition(IDebugServerProvider *provider)
{
    DebugServerProviderNode *n = createNode(provider, true);
    rootItem()->appendChild(n);
    m_providersToAdd.append(provider);
}

DebugServerProviderNode *DebugServerProviderModel::createNode(
        IDebugServerProvider *provider, bool changed)
{
    const auto node = new DebugServerProviderNode(provider, changed);
    node->widget = provider->configurationWidget();
    connect(node->widget, &IDebugServerProviderConfigWidget::dirty, this, [node] {
        node->changed = true;
        node->update();
    });
    return node;
}

void DebugServerProviderModel::addProvider(IDebugServerProvider *provider)
{
    if (findNode(provider))
        m_providersToAdd.removeOne(provider);
    else
        rootItem()->appendChild(createNode(provider, false));

    emit providerStateChanged();
}

void DebugServerProviderModel::removeProvider(IDebugServerProvider *provider)
{
    m_providersToRemove.removeAll(provider);
    if (DebugServerProviderNode *n = findNode(provider))
        destroyItem(n);

    emit providerStateChanged();
}

// DebugServerProvidersSettingsWidget

class DebugServerProvidersSettingsWidget final : public Core::IOptionsPageWidget
{
    Q_DECLARE_TR_FUNCTIONS(BareMetal::Internal::DebugServerProvidersSettingsPage)

public:
    DebugServerProvidersSettingsWidget();

    void apply() final { m_model.apply(); }

    void providerSelectionChanged();
    void removeProvider();
    void updateState();

private:
    void addProviderToModel(IDebugServerProvider *p);
    QModelIndex currentIndex() const;

    DebugServerProviderModel m_model;
    QItemSelectionModel *m_selectionModel = nullptr;
    QTreeView *m_providerView = nullptr;
    Utils::DetailsWidget *m_container = nullptr;
    QPushButton *m_addButton = nullptr;
    QPushButton *m_cloneButton = nullptr;
    QPushButton *m_delButton = nullptr;
};

DebugServerProvidersSettingsWidget::DebugServerProvidersSettingsWidget()
{
    m_providerView = new QTreeView(this);
    m_providerView->setUniformRowHeights(true);
    m_providerView->header()->setStretchLastSection(false);

    m_addButton = new QPushButton(tr("Add"), this);
    m_cloneButton = new QPushButton(tr("Clone"), this);
    m_delButton = new QPushButton(tr("Remove"), this);

    m_container = new Utils::DetailsWidget(this);
    m_container->setState(Utils::DetailsWidget::NoSummary);
    m_container->setMinimumWidth(500);
    m_container->setVisible(false);

    const auto buttonLayout = new QHBoxLayout;
    buttonLayout->setSpacing(6);
    buttonLayout->setContentsMargins(0, 0, 0, 0);
    buttonLayout->addWidget(m_addButton);
    buttonLayout->addWidget(m_cloneButton);
    buttonLayout->addWidget(m_delButton);
    const auto spacerItem = new QSpacerItem(40, 10, QSizePolicy::Expanding, QSizePolicy::Minimum);
    buttonLayout->addItem(spacerItem);

    const auto verticalLayout = new QVBoxLayout;
    verticalLayout->addWidget(m_providerView);
    verticalLayout->addLayout(buttonLayout);

    const auto horizontalLayout = new QHBoxLayout;
    horizontalLayout->addLayout(verticalLayout);
    horizontalLayout->addWidget(m_container);

    const auto groupBox = new QGroupBox(tr("Debug Server Providers"), this);
    groupBox->setLayout(horizontalLayout);

    const auto topLayout = new QVBoxLayout(this);
    topLayout->addWidget(groupBox);

    connect(&m_model, &DebugServerProviderModel::providerStateChanged,
            this, &DebugServerProvidersSettingsWidget::updateState);

    m_providerView->setModel(&m_model);

    const auto headerView = m_providerView->header();
    headerView->setSectionResizeMode(0, QHeaderView::ResizeToContents);
    headerView->setSectionResizeMode(1, QHeaderView::Stretch);
    m_providerView->expandAll();

    m_selectionModel = m_providerView->selectionModel();

    connect(m_selectionModel, &QItemSelectionModel::selectionChanged,
            this, &DebugServerProvidersSettingsWidget::providerSelectionChanged);

    connect(DebugServerProviderManager::instance(), &DebugServerProviderManager::providersChanged,
            this, &DebugServerProvidersSettingsWidget::providerSelectionChanged);

    // Set up add menu:
    const auto addMenu = new QMenu(m_addButton);

    for (const auto f : DebugServerProviderManager::factories()) {
        const auto action = new QAction(addMenu);
        action->setText(f->displayName());
        connect(action, &QAction::triggered, this, [this, f] { addProviderToModel(f->create()); });
        addMenu->addAction(action);
    }

    connect(m_cloneButton, &QAbstractButton::clicked, this, [this] {
        if (const IDebugServerProvider *old = m_model.provider(currentIndex())) {
            QString id = old->id();
            for (const auto f : DebugServerProviderManager::factories()) {
                if (id.startsWith(f->id())) {
                    IDebugServerProvider *p = f->create();
                    p->fromMap(old->toMap());
                    p->setDisplayName(tr("Clone of %1").arg(old->displayName()));
                    p->resetId();
                    addProviderToModel(p);
                }
            }
        }
    });

    m_addButton->setMenu(addMenu);

    connect(m_delButton, &QPushButton::clicked,
            this, &DebugServerProvidersSettingsWidget::removeProvider);

    updateState();
}

void DebugServerProvidersSettingsWidget::providerSelectionChanged()
{
    if (!m_container)
        return;
    const QModelIndex current = currentIndex();
    QWidget *w = m_container->takeWidget(); // Prevent deletion.
    if (w)
        w->setVisible(false);

    const DebugServerProviderNode *node = m_model.nodeForIndex(current);
    w = node ? node->widget : nullptr;
    m_container->setWidget(w);
    m_container->setVisible(w != nullptr);
    updateState();
}

void DebugServerProvidersSettingsWidget::addProviderToModel(IDebugServerProvider *provider)
{
    QTC_ASSERT(provider, return);
    m_model.markForAddition(provider);

    m_selectionModel->select(m_model.indexForProvider(provider),
                             QItemSelectionModel::Clear
                             | QItemSelectionModel::SelectCurrent
                             | QItemSelectionModel::Rows);
}

void DebugServerProvidersSettingsWidget::removeProvider()
{
    if (IDebugServerProvider *p = m_model.provider(currentIndex()))
        m_model.markForRemoval(p);
}

void DebugServerProvidersSettingsWidget::updateState()
{
    if (!m_cloneButton)
        return;

    bool canCopy = false;
    bool canDelete = false;
    if (const IDebugServerProvider *p = m_model.provider(currentIndex())) {
        canCopy = p->isValid();
        canDelete = true;
    }

    m_cloneButton->setEnabled(canCopy);
    m_delButton->setEnabled(canDelete);
}

QModelIndex DebugServerProvidersSettingsWidget::currentIndex() const
{
    if (!m_selectionModel)
        return {};

    const QModelIndexList rows = m_selectionModel->selectedRows();
    if (rows.count() != 1)
        return {};
    return rows.at(0);
}

// DebugServerProvidersSettingsPage

DebugServerProvidersSettingsPage::DebugServerProvidersSettingsPage()
{
    setId(Constants::DEBUG_SERVER_PROVIDERS_SETTINGS_ID);
    setDisplayName(DebugServerProvidersSettingsWidget::tr("Bare Metal"));
    setCategory(ProjectExplorer::Constants::DEVICE_SETTINGS_CATEGORY);
    setWidgetCreator([] { return new DebugServerProvidersSettingsWidget; });
}

} // namespace Internal
} // namespace BareMetal