aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/utils/infobar.cpp
blob: 214a00700d176ee62facd3a00108d92305faf703 (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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
// Copyright (C) 2020 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "infobar.h"

#include "algorithm.h"
#include "infolabel.h"
#include "qtcassert.h"
#include "qtcsettings.h"
#include "utilsicons.h"
#include "utilstr.h"

#include <QComboBox>
#include <QHBoxLayout>
#include <QLabel>
#include <QPainter>
#include <QToolButton>
#include <QVBoxLayout>

static const char C_SUPPRESSED_WARNINGS[] = "SuppressedWarnings";

namespace Utils {

QSet<Id> InfoBar::globallySuppressed;
QtcSettings *InfoBar::m_settings = nullptr;

Theme::Color foregroundThemeColor(InfoLabel::InfoType infoType)
{
    switch (infoType) {
    case InfoLabel::Information:
        return Theme::Token_Notification_Neutral_Default;
    case InfoLabel::Warning:
        return Theme::Token_Notification_Alert_Default;
    case InfoLabel::Error:
    case InfoLabel::NotOk:
        return Theme::Token_Notification_Danger_Default;
    case InfoLabel::Ok:
        return Theme::Token_Notification_Success_Default;
    default:
        return Theme::Token_Text_Default;
    }
}

Theme::Color backgroundThemeColor(InfoLabel::InfoType infoType)
{
    switch (infoType) {
    case InfoLabel::Information:
        return Theme::Token_Notification_Neutral_Subtle;
    case InfoLabel::Warning:
        return Theme::Token_Notification_Alert_Subtle;
    case InfoLabel::Error:
    case InfoLabel::NotOk:
        return Theme::Token_Notification_Danger_Subtle;
    case InfoLabel::Ok:
        return Theme::Token_Notification_Success_Subtle;
    default:
        return Theme::InfoBarBackground;
    }
}

class InfoBarWidget : public QWidget
{
public:
    InfoBarWidget(Qt::Edge edge, InfoLabel::InfoType infoType = InfoLabel::None,
                  QWidget *parent = nullptr);

protected:
    void paintEvent(QPaintEvent *event) override;

private:
    const Utils::Icon &icon() const;

    const Qt::Edge m_edge;
    const InfoLabel::InfoType m_infoType;
};

InfoBarWidget::InfoBarWidget(Qt::Edge edge, InfoLabel::InfoType infoType, QWidget *parent)
    : QWidget(parent)
    , m_edge(edge)
    , m_infoType(infoType)
{
    const bool topEdge = m_edge == Qt::TopEdge;
    const int leftMargin = m_infoType == InfoLabel::None ? 2 : 26;
    setContentsMargins(leftMargin, topEdge ? 0 : 1, 0, topEdge ? 1 : 0);
}

void InfoBarWidget::paintEvent(QPaintEvent *event)
{
    QWidget::paintEvent(event);
    QPainter p(this);
    p.fillRect(rect(), creatorColor(backgroundThemeColor(m_infoType)));
    if (m_infoType != InfoLabel::None) {
        const QPixmap pixmap = icon().pixmap();
        const int iconY = (height() - pixmap.deviceIndependentSize().height()) / 2;
        p.drawPixmap(2, iconY, pixmap);
    }
    const QRectF adjustedRect = QRectF(rect()).adjusted(0.5, 0.5, -0.5, -0.5);
    const bool topEdge = m_edge == Qt::TopEdge;
    p.setPen(creatorColor(Theme::FancyToolBarSeparatorColor));
    p.drawLine(QLineF(topEdge ? adjustedRect.bottomLeft() : adjustedRect.topLeft(),
                      topEdge ? adjustedRect.bottomRight() : adjustedRect.topRight()));
}

const Icon &InfoBarEntry::icon(InfoLabel::InfoType infoType)
{
    switch (infoType) {
    case InfoLabel::Information: {
        const static Utils::Icon icon(
            {{":/utils/images/infolarge.png", foregroundThemeColor(infoType)}},
            Icon::Tint);
        return icon;
    }
    case InfoLabel::Warning: {
        const static Utils::Icon icon(
            {{":/utils/images/warninglarge.png", foregroundThemeColor(infoType)}},
            Icon::Tint);
        return icon;
    }
    case InfoLabel::Error:
    case InfoLabel::NotOk: {
        const static Utils::Icon icon(
            {{":/utils/images/errorlarge.png", foregroundThemeColor(infoType)}},
            Icon::Tint);
        return icon;
    }
    case InfoLabel::Ok: {
        const static Utils::Icon icon(
            {{":/utils/images/oklarge.png", foregroundThemeColor(infoType)}},
            Icon::Tint);
        return icon;
    }
    default: {
        const static Utils::Icon icon;
        return icon;
    }
    }
}

const Icon &InfoBarWidget::icon() const
{
    return InfoBarEntry::icon(m_infoType);
}

InfoBarEntry::InfoBarEntry(Id _id, const QString &_infoText, GlobalSuppression _globalSuppression)
    : m_id(_id)
    , m_infoText(_infoText)
    , m_globalSuppression(_globalSuppression)
{
}

Id InfoBarEntry::id() const
{
    return m_id;
}

QString InfoBarEntry::text() const
{
    return m_infoText;
}

QString InfoBarEntry::title() const
{
    return m_title;
}

/*!
    Sets the \a title that is used if the entry is shown in popup form.
*/
void InfoBarEntry::setTitle(const QString &title)
{
    m_title = title;
}

InfoBarEntry::GlobalSuppression InfoBarEntry::globalSuppression() const
{
    return m_globalSuppression;
}

void InfoBarEntry::addCustomButton(
    const QString &buttonText,
    CallBack callBack,
    const QString &tooltip,
    ButtonAction action,
    bool enabled)
{
    m_buttons.append({buttonText, callBack, tooltip, action, enabled});
}

void InfoBarEntry::setCancelButtonInfo(CallBack callBack)
{
    m_useCancelButton = true;
    m_cancelButtonCallBack = callBack;
}

void InfoBarEntry::setCancelButtonInfo(const QString &_cancelButtonText, CallBack callBack)
{
    m_useCancelButton = true;
    m_cancelButtonText = _cancelButtonText;
    m_cancelButtonCallBack = callBack;
}

void InfoBarEntry::setComboInfo(const QStringList &list,
                                ComboCallBack callBack,
                                const QString &tooltip,
                                int currentIndex)
{
    auto comboInfos = transform(list, [](const QString &string) {
        return ComboInfo{string, string};
    });
    setComboInfo(comboInfos, callBack, tooltip, currentIndex);
}

void InfoBarEntry::setComboInfo(const QList<ComboInfo> &list,
                                ComboCallBack callBack,
                                const QString &tooltip,
                                int currentIndex)
{
    m_combo.entries = list;
    m_combo.callback = callBack;
    m_combo.tooltip = tooltip;
    m_combo.currentIndex = currentIndex;
}

InfoBarEntry::Combo InfoBarEntry::combo() const
{
    return m_combo;
}

void InfoBarEntry::removeCancelButton()
{
    m_useCancelButton = false;
    m_cancelButtonText.clear();
    m_cancelButtonCallBack = nullptr;
}

bool InfoBarEntry::hasCancelButton() const
{
    return m_useCancelButton;
}

QString InfoBarEntry::cancelButtonText() const
{
    return m_cancelButtonText;
}

InfoBarEntry::CallBack InfoBarEntry::cancelButtonCallback() const
{
    return m_cancelButtonCallBack;
}

QList<InfoBarEntry::Button> InfoBarEntry::buttons() const
{
    return m_buttons;
}

void InfoBarEntry::setDetailsWidgetCreator(const InfoBarEntry::DetailsWidgetCreator &creator)
{
    m_detailsWidgetCreator = creator;
}

InfoBarEntry::DetailsWidgetCreator InfoBarEntry::detailsWidgetCreator() const
{
    return m_detailsWidgetCreator;
}

void InfoBarEntry::setInfoType(InfoLabel::InfoType infoType)
{
    m_infoType = infoType;
}

InfoLabel::InfoType InfoBarEntry::infoType() const
{
    return m_infoType;
}

void InfoBar::addInfo(const InfoBarEntry &info)
{
    m_infoBarEntries << info;
    emit changed();
}

void InfoBar::removeInfo(Id id)
{
    const int size = m_infoBarEntries.size();
    Utils::erase(m_infoBarEntries, equal(&InfoBarEntry::id, id));
    if (size != m_infoBarEntries.size())
        emit changed();
}

bool InfoBar::containsInfo(Id id) const
{
    return anyOf(m_infoBarEntries, equal(&InfoBarEntry::id, id));
}

// Remove and suppress id
void InfoBar::suppressInfo(Id id)
{
    removeInfo(id);
    m_suppressed << id;
}

// Info cannot be added more than once, or if it is suppressed
bool InfoBar::canInfoBeAdded(Id id) const
{
    return !containsInfo(id) && !m_suppressed.contains(id) && !globallySuppressed.contains(id);
}

void InfoBar::unsuppressInfo(Id id)
{
    m_suppressed.remove(id);
}

void InfoBar::clear()
{
    if (!m_infoBarEntries.isEmpty()) {
        m_infoBarEntries.clear();
        emit changed();
    }
}

void InfoBar::globallySuppressInfo(Id id)
{
    globallySuppressed.insert(id);
    writeGloballySuppressedToSettings();
}

void InfoBar::globallyUnsuppressInfo(Id id)
{
    globallySuppressed.remove(id);
    writeGloballySuppressedToSettings();
}

void InfoBar::initialize(QtcSettings *settings)
{
    m_settings = settings;

    if (QTC_GUARD(m_settings)) {
        const QStringList list = m_settings->value(C_SUPPRESSED_WARNINGS).toStringList();
        globallySuppressed = transform<QSet>(list, Id::fromString);
    }
}

QtcSettings *InfoBar::settings()
{
    return m_settings;
}

QList<InfoBarEntry> InfoBar::entries() const
{
    return m_infoBarEntries;
}

void InfoBar::triggerButton(const Id &entryId, const InfoBarEntry::Button &button)
{
    switch (button.action) {
    case InfoBarEntry::ButtonAction::Hide:
        removeInfo(entryId);
        break;
    case InfoBarEntry::ButtonAction::Suppress:
        suppressInfo(entryId); // hiding is implicit
        break;
    case InfoBarEntry::ButtonAction::SuppressPersistently:
        removeInfo(entryId);
        globallySuppressInfo(entryId);
        break;
        break;
    case InfoBarEntry::ButtonAction::None:
        break;
    }
    button.callback();
}

void InfoBar::clearGloballySuppressed()
{
    globallySuppressed.clear();
    if (m_settings)
        m_settings->remove(C_SUPPRESSED_WARNINGS);
}

bool InfoBar::anyGloballySuppressed()
{
    return !globallySuppressed.isEmpty();
}

void InfoBar::writeGloballySuppressedToSettings()
{
    if (!m_settings)
        return;
    const QStringList list = transform<QList>(globallySuppressed, &Id::toString);
    m_settings->setValueWithDefault(C_SUPPRESSED_WARNINGS, list);
}


InfoBarDisplay::InfoBarDisplay(QObject *parent)
    : QObject(parent)
{
}

void InfoBarDisplay::setTarget(QBoxLayout *layout, int index)
{
    m_boxLayout = layout;
    m_boxIndex = index;
}

void InfoBarDisplay::setInfoBar(InfoBar *infoBar)
{
    if (m_infoBar == infoBar)
        return;

    if (m_infoBar)
        m_infoBar->disconnect(this);
    m_infoBar = infoBar;
    if (m_infoBar) {
        connect(m_infoBar, &InfoBar::changed, this, &InfoBarDisplay::update);
        connect(m_infoBar, &QObject::destroyed, this, &InfoBarDisplay::infoBarDestroyed);
    }
    update();
}

void InfoBarDisplay::setEdge(Qt::Edge edge)
{
    m_edge = edge;
    update();
}

InfoBar *InfoBarDisplay::infoBar() const
{
    return m_infoBar;
}

void InfoBarDisplay::infoBarDestroyed()
{
    m_infoBar = nullptr;
    // Calling update() here causes a complicated crash on shutdown.
    // So instead we rely on the view now being either destroyed (in which case it
    // will delete the widgets itself) or setInfoBar() being called explicitly.
}

void InfoBarDisplay::update()
{
    for (QWidget *widget : std::as_const(m_infoWidgets)) {
        widget->hide(); // Late deletion can cause duplicate infos. Hide immediately to prevent it.
        widget->deleteLater();
    }
    m_infoWidgets.clear();

    if (!m_infoBar)
        return;

    const QList<InfoBarEntry> entries = m_infoBar->entries();
    for (const InfoBarEntry &info : entries) {
        auto infoWidget = new InfoBarWidget(m_edge, info.infoType());

        auto hbox = new QHBoxLayout;
        hbox->setContentsMargins(2, 2, 2, 2);

        auto vbox = new QVBoxLayout(infoWidget);
        vbox->setContentsMargins(0, 0, 0, 0);
        vbox->addLayout(hbox);

        QLabel *infoWidgetLabel = new QLabel(info.text());
        infoWidgetLabel->setWordWrap(true);
        infoWidgetLabel->setOpenExternalLinks(true);
        hbox->addWidget(infoWidgetLabel, 1);

        if (info.detailsWidgetCreator()) {
            if (m_isShowingDetailsWidget) {
                QWidget *detailsWidget = info.detailsWidgetCreator()();
                vbox->addWidget(detailsWidget);
            }

            auto showDetailsButton = new QToolButton;
            showDetailsButton->setCheckable(true);
            showDetailsButton->setChecked(m_isShowingDetailsWidget);
            showDetailsButton->setText(Tr::tr("&Show Details"));
            connect(showDetailsButton, &QToolButton::clicked, this, [this, vbox, info] (bool) {
                QWidget *detailsWidget = vbox->count() == 2 ? vbox->itemAt(1)->widget() : nullptr;
                if (!detailsWidget) {
                    detailsWidget = info.detailsWidgetCreator()();
                    vbox->addWidget(detailsWidget);
                }

                m_isShowingDetailsWidget = !m_isShowingDetailsWidget;
                detailsWidget->setVisible(m_isShowingDetailsWidget);
            });

            hbox->addWidget(showDetailsButton);
        } else {
            m_isShowingDetailsWidget = false;
        }

        const InfoBarEntry::Combo combo = info.combo();
        if (!combo.entries.isEmpty()) {
            auto cb = new QComboBox();
            cb->setToolTip(combo.tooltip);
            for (const InfoBarEntry::ComboInfo &comboInfo : std::as_const(combo.entries))
                cb->addItem(comboInfo.displayText, comboInfo.data);
            if (combo.currentIndex >= 0 && combo.currentIndex < cb->count())
                cb->setCurrentIndex(combo.currentIndex);
            connect(
                cb,
                &QComboBox::currentIndexChanged,
                this,
                [cb, combo] { combo.callback({cb->currentText(), cb->currentData()}); },
                Qt::QueuedConnection);

            hbox->addWidget(cb);
        }

        const QList<InfoBarEntry::Button> buttons = info.buttons();
        for (const InfoBarEntry::Button &button : buttons) {
            auto infoWidgetButton = new QToolButton;
            infoWidgetButton->setText(button.text);
            infoWidgetButton->setToolTip(button.tooltip);
            infoWidgetButton->setEnabled(button.enabled);
            connect(infoWidgetButton, &QAbstractButton::clicked, this, [button, this, id = info.id()] {
                m_infoBar->triggerButton(id, button);
            });
            hbox->addWidget(infoWidgetButton);
        }

        const Id id = info.id();
        QToolButton *infoWidgetSuppressButton = nullptr;
        if (info.globalSuppression() == InfoBarEntry::GlobalSuppression::Enabled) {
            infoWidgetSuppressButton = new QToolButton;
            infoWidgetSuppressButton->setText(Tr::tr("Do Not Show Again"));
            connect(infoWidgetSuppressButton, &QAbstractButton::clicked, this, [this, id] {
                m_infoBar->removeInfo(id);
                InfoBar::globallySuppressInfo(id);
            });
        }

        QToolButton *infoWidgetCloseButton = nullptr;
        if (info.hasCancelButton()) {
            infoWidgetCloseButton = new QToolButton;
            // need to connect to cancelObjectbefore connecting to cancelButtonClicked,
            // because the latter removes the button and with it any connect
            if (info.cancelButtonCallback())
                connect(infoWidgetCloseButton, &QAbstractButton::clicked, info.cancelButtonCallback());
            connect(infoWidgetCloseButton, &QAbstractButton::clicked, this, [this, id] {
                m_infoBar->removeInfo(id);
            });
        }

        if (infoWidgetCloseButton) {
            if (info.cancelButtonText().isEmpty()) {
                infoWidgetCloseButton->setAutoRaise(true);
                infoWidgetCloseButton->setIcon(Icons::CLOSE_FOREGROUND.icon());
                infoWidgetCloseButton->setToolTip(Tr::tr("Close"));
            } else {
                infoWidgetCloseButton->setText(info.cancelButtonText());
            }
        }

        if (infoWidgetSuppressButton)
            hbox->addWidget(infoWidgetSuppressButton);

        if (infoWidgetCloseButton)
            hbox->addWidget(infoWidgetCloseButton);

        connect(infoWidget, &QObject::destroyed, this, [this, infoWidget] {
            m_infoWidgets.removeOne(infoWidget);
        });
        m_boxLayout->insertWidget(m_boxIndex, infoWidget);
        m_infoWidgets << infoWidget;
    }
}

} // namespace Utils