Fix comment end after SPDX identifier
[gd/wireshark/.git] / ui / qt / models / percent_bar_delegate.cpp
1 /* percent_bar_delegate.cpp
2  *
3  * Wireshark - Network traffic analyzer
4  * By Gerald Combs <gerald@wireshark.org>
5  * Copyright 1998 Gerald Combs
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  */
9
10 #include <ui/qt/models/percent_bar_delegate.h>
11
12 #include <ui/qt/utils/color_utils.h>
13
14 #include <QApplication>
15 #include <QPainter>
16
17 static const int bar_em_width_ = 8;
18 static const double bar_blend_ = 0.15;
19
20 void PercentBarDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
21                                const QModelIndex &index) const
22 {
23     QStyleOptionViewItem option_vi = option;
24     QStyledItemDelegate::initStyleOption(&option_vi, index);
25
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);
29
30     bool ok = false;
31     double value = index.data(Qt::UserRole).toDouble(&ok);
32
33     if (!ok || !index.data(Qt::DisplayRole).toString().isEmpty()) {
34         // We don't have a valid value or the item has visible text.
35         return;
36     }
37
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);
41     if (value < 0) {
42         value = 0;
43     }
44     if (value > 100.0) {
45         value = 100.0;
46     }
47
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));
53     }
54
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_);
60
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),
67                                            bar_blend_);
68     }
69
70     painter->save();
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);
78     painter->restore();
79
80     painter->save();
81     painter->setPen(text_color);
82     painter->drawText(option.rect, Qt::AlignCenter, pct_str);
83     painter->restore();
84 }
85
86 QSize PercentBarDelegate::sizeHint(const QStyleOptionViewItem &option,
87                                    const QModelIndex &index) const
88 {
89     return QSize(option.fontMetrics.height() * bar_em_width_,
90                  QStyledItemDelegate::sizeHint(option, index).height());
91 }
92
93 /*
94  * Editor modelines
95  *
96  * Local Variables:
97  * c-basic-offset: 4
98  * tab-width: 8
99  * indent-tabs-mode: nil
100  * End:
101  *
102  * ex: set shiftwidth=4 tabstop=8 expandtab:
103  * :indentSize=4:tabSize=8:noTabs=true:
104  */