1 /* percent_bar_delegate.cpp
3 * Wireshark - Network traffic analyzer
4 * By Gerald Combs <gerald@wireshark.org>
5 * Copyright 1998 Gerald Combs
7 * SPDX-License-Identifier: GPL-2.0-or-later
10 #include <ui/qt/models/percent_bar_delegate.h>
12 #include <ui/qt/utils/color_utils.h>
14 #include <QApplication>
17 static const int bar_em_width_
= 8;
18 static const double bar_blend_
= 0.15;
20 void PercentBarDelegate::paint(QPainter
*painter
, const QStyleOptionViewItem
&option
,
21 const QModelIndex
&index
) const
23 QStyleOptionViewItem option_vi
= option
;
24 QStyledItemDelegate::initStyleOption(&option_vi
, index
);
26 // Paint our rect with no text using the current style, then draw our
27 // bar and text over it.
28 QStyledItemDelegate::paint(painter
, option
, index
);
31 double value
= index
.data(Qt::UserRole
).toDouble(&ok
);
33 if (!ok
|| !index
.data(Qt::DisplayRole
).toString().isEmpty()) {
34 // We don't have a valid value or the item has visible text.
38 // If our value is out range our caller has a bug. Clamp the graph and
39 // Print the numeric value so that the bug is obvious.
40 QString pct_str
= QString::number(value
, 'f', 1);
48 if (QApplication::style()->objectName().contains("vista")) {
49 // QWindowsVistaStyle::drawControl does this internally. Unfortunately there
50 // doesn't appear to be a more general way to do this.
51 option_vi
.palette
.setColor(QPalette::All
, QPalette::HighlightedText
,
52 option_vi
.palette
.color(QPalette::Active
, QPalette::Text
));
55 QPalette::ColorGroup cg
= option_vi
.state
& QStyle::State_Enabled
56 ? QPalette::Normal
: QPalette::Disabled
;
57 QColor text_color
= option_vi
.palette
.color(cg
, QPalette::Text
);
58 QColor bar_color
= ColorUtils::alphaBlend(option_vi
.palette
.windowText(),
59 option_vi
.palette
.window(), bar_blend_
);
61 if (cg
== QPalette::Normal
&& !(option_vi
.state
& QStyle::State_Active
))
62 cg
= QPalette::Inactive
;
63 if (option_vi
.state
& QStyle::State_Selected
) {
64 text_color
= option_vi
.palette
.color(cg
, QPalette::HighlightedText
);
65 bar_color
= ColorUtils::alphaBlend(option_vi
.palette
.color(cg
, QPalette::Window
),
66 option_vi
.palette
.color(cg
, QPalette::Highlight
),
71 int border_radius
= 3; // We use 3 px elsewhere, e.g. filter combos.
72 QRect pct_rect
= option
.rect
;
73 pct_rect
.adjust(1, 1, -1, -1);
74 pct_rect
.setWidth(((pct_rect
.width() * value
) / 100.0) + 0.5);
75 painter
->setPen(Qt::NoPen
);
76 painter
->setBrush(bar_color
);
77 painter
->drawRoundedRect(pct_rect
, border_radius
, border_radius
);
81 painter
->setPen(text_color
);
82 painter
->drawText(option
.rect
, Qt::AlignCenter
, pct_str
);
86 QSize
PercentBarDelegate::sizeHint(const QStyleOptionViewItem
&option
,
87 const QModelIndex
&index
) const
89 return QSize(option
.fontMetrics
.height() * bar_em_width_
,
90 QStyledItemDelegate::sizeHint(option
, index
).height());