aboutsummaryrefslogtreecommitdiffstats
path: root/ui/qt/label_stack.cpp
blob: ad71b132d63f5dd022652b63782de3b0346f0230 (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
/* label_stack.cpp
 *
 * $Id: mainStatus_bar.cpp 40378 2012-01-04 22:13:01Z gerald $
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "label_stack.h"

#include <QTimer>

/* Temporary message timeouts */
#define TEMPORARY_MSG_TIMEOUT (7 * 1000)
//#define TEMPORARY_FLASH_TIMEOUT (1 * 1000)
//#define TEMPORARY_FLASH_INTERVAL (TEMPORARY_FLASH_TIMEOUT / 4)

LabelStack::LabelStack(QWidget *parent) :
    QLabel(parent)
{
    m_temporaryCtx = -1;
    fillLabel();
}

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

void LabelStack::fillLabel() {
    StackItem *si;

    setStyleSheet(
            "QLabel {"
            "  margin-left: 0.5em;"
            "}"
            );

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

    si = m_labels.first();

    if (si->ctx == m_temporaryCtx) {
        setStyleSheet(
                // Tango "Scarlet Red"
                "QLabel {"
                "  margin-left: 0.5em;"
                "  border-radius: 3px;"
                "  color: white;"
                "  background-color: rgba(239, 41, 41, 128);"
                "}"
                );
    }

    setText(si->text);
}

void LabelStack::pushText(QString &text, int ctx) {
    StackItem *si = new StackItem;
    si->text = text;
    si->ctx = ctx;
    m_labels.prepend(si);

    if (ctx == m_temporaryCtx) {
        QTimer::singleShot(TEMPORARY_MSG_TIMEOUT, this, SLOT(popTemporaryText()));
    }

    fillLabel();
}

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

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

    fillLabel();
}

void LabelStack::popTemporaryText() {
    popText(m_temporaryCtx);
}