replace SPDX identifier GPL-2.0+ with GPL-2.0-or-later.
[metze/wireshark/wip.git] / ui / qt / models / url_link_delegate.cpp
1 /* url_link_delegate.cpp
2  * Delegates for displaying links as links, including elide model
3  *
4  * Wireshark - Network traffic analyzer
5  * By Gerald Combs <gerald@wireshark.org>
6  * Copyright 1998 Gerald Combs
7  *
8  * SPDX-License-Identifier: GPL-2.0-or-later
9  */
10
11 #include <ui/qt/models/url_link_delegate.h>
12
13 #include <QPainter>
14 #include <QRegExp>
15
16 UrlLinkDelegate::UrlLinkDelegate(QObject *parent)
17  : QStyledItemDelegate(parent),
18    re_col_(-1),
19    url_re_(new QRegExp())
20 {}
21
22 UrlLinkDelegate::~UrlLinkDelegate()
23 {
24     delete url_re_;
25 }
26
27 void UrlLinkDelegate::setColCheck(int column, QString &pattern)
28 {
29     re_col_ = column;
30     url_re_->setPattern(pattern);
31 }
32
33 void UrlLinkDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
34     if (re_col_ >= 0 && url_re_) {
35         QModelIndex re_idx = index.model()->index(index.row(), re_col_);
36         QString col_text = index.model()->data(re_idx).toString();
37         if (url_re_->indexIn(col_text) < 0) {
38             QStyledItemDelegate::paint(painter, option, index);
39             return;
40         }
41     }
42
43     QStyleOptionViewItem opt = option;
44     initStyleOption(&opt, index);
45
46     opt.font.setUnderline(true);
47     opt.palette.setColor(QPalette::Text, opt.palette.link().color());
48
49     QStyledItemDelegate::paint(painter, opt, index);
50 }
51
52 /*
53  * Editor modelines
54  *
55  * Local Variables:
56  * c-basic-offset: 4
57  * tab-width: 8
58  * indent-tabs-mode: nil
59  * End:
60  *
61  * ex: set shiftwidth=4 tabstop=8 expandtab:
62  * :indentSize=4:tabSize=8:noTabs=true:
63  */