1 /* timeline_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/timeline_delegate.h>
11 
12 #include <ui/qt/utils/color_utils.h>
13 
14 #include <QApplication>
15 #include <QPainter>
16 
17 // XXX We might want to move this to conversation_dialog.cpp.
18 
19 // PercentBarDelegate uses a stronger blend value, but its bars are also
20 // more of a prominent feature. Make the blend weaker here so that we don't
21 // obscure our text.
22 static const double bar_blend_ = 0.08;
23 
TimelineDelegate(QWidget * parent)24 TimelineDelegate::TimelineDelegate(QWidget *parent) :
25     QStyledItemDelegate(parent)
26 {}
27 
paint(QPainter * painter,const QStyleOptionViewItem & option,const QModelIndex & index) const28 void TimelineDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
29                                const QModelIndex &index) const
30 {
31     QStyleOptionViewItem option_vi = option;
32     QStyledItemDelegate::initStyleOption(&option_vi, index);
33 
34     struct timeline_span span_px = index.data(Qt::UserRole).value<struct timeline_span>();
35 
36     // Paint our rect with no text using the current style, then draw our
37     // bar and text over it.
38     QStyledItemDelegate::paint(painter, option, index);
39 
40     if (QApplication::style()->objectName().contains("vista")) {
41         // QWindowsVistaStyle::drawControl does this internally. Unfortunately there
42         // doesn't appear to be a more general way to do this.
43         option_vi.palette.setColor(QPalette::All, QPalette::HighlightedText,
44                                option_vi.palette.color(QPalette::Active, QPalette::Text));
45     }
46 
47     QPalette::ColorGroup cg = option_vi.state & QStyle::State_Enabled
48                               ? QPalette::Normal : QPalette::Disabled;
49     QColor text_color = option_vi.palette.color(cg, QPalette::Text);
50     QColor bar_color = ColorUtils::alphaBlend(option_vi.palette.windowText(),
51                                               option_vi.palette.window(), bar_blend_);
52 
53     if (cg == QPalette::Normal && !(option_vi.state & QStyle::State_Active))
54         cg = QPalette::Inactive;
55     if (option_vi.state & QStyle::State_Selected) {
56         text_color = option_vi.palette.color(cg, QPalette::HighlightedText);
57         bar_color = ColorUtils::alphaBlend(option_vi.palette.color(cg, QPalette::Window),
58                                            option_vi.palette.color(cg, QPalette::Highlight),
59                                            bar_blend_);
60     }
61 
62     painter->save();
63     int border_radius = 3; // We use 3 px elsewhere, e.g. filter combos.
64     QRect timeline_rect = option.rect;
65     timeline_rect.adjust(span_px.start, 1, 0, -1);
66     timeline_rect.setWidth(span_px.width);
67     painter->setClipRect(option.rect);
68     painter->setPen(Qt::NoPen);
69     painter->setBrush(bar_color);
70     painter->drawRoundedRect(timeline_rect, border_radius, border_radius);
71     painter->restore();
72 
73     painter->save();
74     painter->setPen(text_color);
75     painter->drawText(option.rect, Qt::AlignCenter, index.data(Qt::DisplayRole).toString());
76     painter->restore();
77 }
78