summaryrefslogtreecommitdiffstats
path: root/ui/qt/widgets/label_stack.cpp
blob: 4cac15b799ed8bcec4764d95870389e24d2aeab5 (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
/* label_stack.cpp
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include <ui/qt/widgets/label_stack.h>

#include <QContextMenuEvent>
#include <QPainter>
#include <QMouseEvent>
#include <QStyleOption>

#include <ui/qt/utils/color_utils.h>

/* Temporary message timeouts */
const int temporary_interval_ = 1000;
const int temporary_msg_timeout_ = temporary_interval_ * 9;
const int temporary_flash_timeout_ = temporary_interval_ / 5;
const int num_flashes_ = 3;

LabelStack::LabelStack(QWidget *parent) :
    QLabel(parent),
    temporary_ctx_(-1),
    shrinkable_(false)
{
#ifdef Q_OS_MAC
    setAttribute(Qt::WA_MacSmallSize, true);
#endif
    fillLabel();

    connect(&temporary_timer_, &QTimer::timeout, this, &LabelStack::updateTemporaryStatus);
}

void LabelStack::setTemporaryContext(const int ctx) {
    temporary_ctx_ = ctx;
}

void LabelStack::fillLabel() {
    StackItem si;
    QString style_sheet;

    style_sheet =
            "QLabel {"
            "  margin-left: 0.5em;";

    if (labels_.isEmpty()) {
        clear();
        return;
    }

    si = labels_.first();

    if (si.ctx == temporary_ctx_) {
        style_sheet += QString(
                    "  border-radius: 0.25em;"
                    "  background-color: %2;"
                    )
                .arg(ColorUtils::warningBackground().name());
    }

    style_sheet += "}";
    if (styleSheet().size() != style_sheet.size()) {
        // Can be computationally expensive.
        setStyleSheet(style_sheet);
    }
    setText(si.text);
}

void LabelStack::pushText(const QString &text, int ctx) {
    popText(ctx);

    if (ctx == temporary_ctx_) {
        temporary_timer_.stop();

        temporary_epoch_.start();
        temporary_timer_.start(temporary_flash_timeout_);
        emit toggleTemporaryFlash(true);
    }

    StackItem si;
    si.text = text;
    si.ctx = ctx;
    labels_.prepend(si);
    fillLabel();
}

void LabelStack::setShrinkable(bool shrinkable)
{
    shrinkable_ = shrinkable;
    int min_width = 0;

    if (shrinkable) {
        min_width = fontMetrics().height() * 5; // em-widths
    }
    setMinimumWidth(min_width);
    fillLabel();
}

void LabelStack::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
#if QT_VERSION >= QT_VERSION_CHECK(6, 0 ,0)
        emit mousePressedAt(event->globalPosition().toPoint(), Qt::LeftButton);
#else
        emit mousePressedAt(event->globalPos(), Qt::LeftButton);
#endif
    }
}

void LabelStack::mouseReleaseEvent(QMouseEvent *)
{
}

void LabelStack::mouseDoubleClickEvent(QMouseEvent *)
{
}

void LabelStack::mouseMoveEvent(QMouseEvent *)
{
}

void LabelStack::contextMenuEvent(QContextMenuEvent *event)
{
    emit mousePressedAt(QPoint(event->globalPos()), Qt::RightButton);
}

void LabelStack::paintEvent(QPaintEvent *event)
{
    if (!shrinkable_) {
        QLabel::paintEvent(event);
        return;
    }

    QFrame::paintEvent(event);

    QString elided_text = fontMetrics().elidedText(text(), Qt::ElideMiddle, width());
    QPainter painter(this);
    QRect contents_rect = contentsRect();
    QStyleOption opt;

    contents_rect.adjust(margin(), margin(), -margin(), -margin());
    opt.initFrom(this);

    style()->drawItemText(&painter, contents_rect, alignment(), opt.palette,
                          isEnabled(), elided_text, foregroundRole());
}

void LabelStack::popText(int ctx) {
    QMutableListIterator<StackItem> iter(labels_);

    while (iter.hasNext()) {
        if (iter.next().ctx == ctx) {
            iter.remove();
            break;
        }
    }

    fillLabel();
}

void LabelStack::updateTemporaryStatus() {
    if (temporary_epoch_.elapsed() >= temporary_msg_timeout_) {
        popText(temporary_ctx_);
        emit toggleTemporaryFlash(false);
        temporary_timer_.stop();
    } else {
        for (int i = (num_flashes_ * 2); i > 0; i--) {
            if (temporary_epoch_.elapsed() >= temporary_flash_timeout_ * i) {
                emit toggleTemporaryFlash(i % 2);
                break;
            }
        }
    }
}