2012-01-14 00:16:16 +00:00
|
|
|
/* label_stack.cpp
|
|
|
|
*
|
|
|
|
* 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
|
2012-06-28 22:56:06 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2012-01-14 00:16:16 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "label_stack.h"
|
2015-03-04 16:12:58 +00:00
|
|
|
|
2012-12-30 19:33:05 +00:00
|
|
|
#include <QContextMenuEvent>
|
2015-03-04 16:12:58 +00:00
|
|
|
#include <QPainter>
|
|
|
|
#include <QMouseEvent>
|
2015-03-07 17:23:03 +00:00
|
|
|
#include <QStyleOption>
|
2012-01-14 00:16:16 +00:00
|
|
|
|
2012-11-30 23:14:53 +00:00
|
|
|
#include "tango_colors.h"
|
2012-01-14 00:16:16 +00:00
|
|
|
|
|
|
|
/* Temporary message timeouts */
|
2012-11-30 23:14:53 +00:00
|
|
|
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;
|
2012-01-14 00:16:16 +00:00
|
|
|
|
|
|
|
LabelStack::LabelStack(QWidget *parent) :
|
2015-03-04 16:12:58 +00:00
|
|
|
QLabel(parent),
|
|
|
|
temporary_ctx_(-1),
|
|
|
|
shrinkable_(false)
|
2012-01-14 00:16:16 +00:00
|
|
|
{
|
2013-04-24 11:07:25 +00:00
|
|
|
#ifdef Q_OS_MAC
|
2012-08-01 00:06:21 +00:00
|
|
|
setAttribute(Qt::WA_MacSmallSize, true);
|
|
|
|
#endif
|
2012-01-14 00:16:16 +00:00
|
|
|
fillLabel();
|
2012-11-30 23:14:53 +00:00
|
|
|
|
|
|
|
connect(&temporary_timer_, SIGNAL(timeout()), this, SLOT(updateTemporaryStatus()));
|
2012-01-14 00:16:16 +00:00
|
|
|
}
|
|
|
|
|
2013-08-08 00:26:57 +00:00
|
|
|
void LabelStack::setTemporaryContext(const int ctx) {
|
2012-11-30 23:14:53 +00:00
|
|
|
temporary_ctx_ = ctx;
|
2012-01-14 00:16:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LabelStack::fillLabel() {
|
2014-10-27 01:48:14 +00:00
|
|
|
StackItem si;
|
2012-11-30 23:14:53 +00:00
|
|
|
QString style_sheet;
|
2012-01-14 00:16:16 +00:00
|
|
|
|
2012-11-30 23:14:53 +00:00
|
|
|
style_sheet =
|
2012-01-14 00:16:16 +00:00
|
|
|
"QLabel {"
|
2012-11-30 23:14:53 +00:00
|
|
|
" margin-left: 0.5em;";
|
2012-01-14 00:16:16 +00:00
|
|
|
|
2012-11-30 23:14:53 +00:00
|
|
|
if (labels_.isEmpty()) {
|
2012-01-14 00:16:16 +00:00
|
|
|
clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-11-30 23:14:53 +00:00
|
|
|
si = labels_.first();
|
|
|
|
|
2014-10-27 01:48:14 +00:00
|
|
|
if (si.ctx == temporary_ctx_) {
|
2012-11-30 23:14:53 +00:00
|
|
|
style_sheet += QString(
|
2012-12-07 20:08:00 +00:00
|
|
|
" border-radius: 0.25em;"
|
|
|
|
" color: #%1;"
|
|
|
|
" background-color: #%2;"
|
|
|
|
)
|
2012-12-21 17:37:57 +00:00
|
|
|
.arg(ws_css_warn_text, 6, 16, QChar('0'))
|
|
|
|
.arg(ws_css_warn_background, 6, 16, QChar('0'));
|
2012-01-14 00:16:16 +00:00
|
|
|
}
|
|
|
|
|
2012-11-30 23:14:53 +00:00
|
|
|
style_sheet += "}";
|
2016-07-21 17:27:45 +00:00
|
|
|
if (styleSheet().size() != style_sheet.size()) {
|
|
|
|
// Can be computationally expensive.
|
|
|
|
setStyleSheet(style_sheet);
|
|
|
|
}
|
2014-10-27 01:48:14 +00:00
|
|
|
setText(si.text);
|
2012-01-14 00:16:16 +00:00
|
|
|
}
|
|
|
|
|
2015-03-04 16:12:58 +00:00
|
|
|
void LabelStack::pushText(const QString &text, int ctx) {
|
2014-10-27 01:48:14 +00:00
|
|
|
popText(ctx);
|
2012-01-14 00:16:16 +00:00
|
|
|
|
2012-11-30 23:14:53 +00:00
|
|
|
if (ctx == temporary_ctx_) {
|
2012-12-15 01:04:39 +00:00
|
|
|
temporary_timer_.stop();
|
|
|
|
|
2012-11-30 23:14:53 +00:00
|
|
|
temporary_epoch_.start();
|
|
|
|
temporary_timer_.start(temporary_flash_timeout_);
|
|
|
|
emit toggleTemporaryFlash(true);
|
2012-01-14 00:16:16 +00:00
|
|
|
}
|
|
|
|
|
2014-10-27 01:48:14 +00:00
|
|
|
StackItem si;
|
|
|
|
si.text = text;
|
|
|
|
si.ctx = ctx;
|
2012-12-15 01:04:39 +00:00
|
|
|
labels_.prepend(si);
|
2012-01-14 00:16:16 +00:00
|
|
|
fillLabel();
|
|
|
|
}
|
|
|
|
|
2015-03-04 16:12:58 +00:00
|
|
|
void LabelStack::setShrinkable(bool shrinkable)
|
|
|
|
{
|
|
|
|
shrinkable_ = shrinkable;
|
|
|
|
int min_width = 0;
|
|
|
|
|
|
|
|
if (shrinkable) {
|
|
|
|
min_width = fontMetrics().height() * 5; // em-widths
|
|
|
|
}
|
|
|
|
setMinimumWidth(min_width);
|
|
|
|
fillLabel();
|
|
|
|
}
|
|
|
|
|
2012-12-30 19:33:05 +00:00
|
|
|
void LabelStack::mousePressEvent(QMouseEvent *event)
|
|
|
|
{
|
|
|
|
if (event->button() == Qt::LeftButton)
|
|
|
|
emit mousePressedAt(QPoint(event->globalPos()), Qt::LeftButton);
|
|
|
|
}
|
|
|
|
|
2015-07-07 21:47:20 +00:00
|
|
|
void LabelStack::mouseReleaseEvent(QMouseEvent *)
|
2012-12-30 19:33:05 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-07-07 21:47:20 +00:00
|
|
|
void LabelStack::mouseDoubleClickEvent(QMouseEvent *)
|
2012-12-30 19:33:05 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-07-07 21:47:20 +00:00
|
|
|
void LabelStack::mouseMoveEvent(QMouseEvent *)
|
2012-12-30 19:33:05 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void LabelStack::contextMenuEvent(QContextMenuEvent *event)
|
|
|
|
{
|
|
|
|
emit mousePressedAt(QPoint(event->globalPos()), Qt::RightButton);
|
|
|
|
}
|
|
|
|
|
2015-03-04 16:12:58 +00:00
|
|
|
void LabelStack::paintEvent(QPaintEvent *event)
|
|
|
|
{
|
|
|
|
if (!shrinkable_) {
|
|
|
|
QLabel::paintEvent(event);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-07 17:23:03 +00:00
|
|
|
QFrame::paintEvent(event);
|
|
|
|
|
2015-03-04 16:12:58 +00:00
|
|
|
QString elided_text = fontMetrics().elidedText(text(), Qt::ElideMiddle, width());
|
2015-03-07 17:23:03 +00:00
|
|
|
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());
|
2015-03-04 16:12:58 +00:00
|
|
|
}
|
|
|
|
|
2012-01-14 00:16:16 +00:00
|
|
|
void LabelStack::popText(int ctx) {
|
2014-10-27 01:48:14 +00:00
|
|
|
QMutableListIterator<StackItem> iter(labels_);
|
2012-01-14 00:16:16 +00:00
|
|
|
|
|
|
|
while (iter.hasNext()) {
|
2014-10-27 01:48:14 +00:00
|
|
|
if (iter.next().ctx == ctx) {
|
2012-01-14 00:16:16 +00:00
|
|
|
iter.remove();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fillLabel();
|
|
|
|
}
|
|
|
|
|
2012-11-30 23:14:53 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-01-14 00:16:16 +00:00
|
|
|
}
|
2012-09-04 08:18:31 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Editor modelines
|
|
|
|
*
|
|
|
|
* Local Variables:
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* tab-width: 8
|
|
|
|
* indent-tabs-mode: nil
|
|
|
|
* End:
|
|
|
|
*
|
|
|
|
* ex: set shiftwidth=4 tabstop=8 expandtab:
|
|
|
|
* :indentSize=4:tabSize=8:noTabs=true:
|
|
|
|
*/
|