summaryrefslogtreecommitdiffstats
path: root/ui/qt/supported_protocols_dialog.cpp
blob: eea2d738d6fdfe474579f758088cb4111d275147 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* supported_protocols_dialog.cpp
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "config.h"

#include "supported_protocols_dialog.h"
#include <ui_supported_protocols_dialog.h>
#include <epan/prefs.h>

#include <QElapsedTimer>

#include "main_application.h"

SupportedProtocolsDialog::SupportedProtocolsDialog(QWidget *parent) :
    GeometryStateDialog(parent),
    ui(new Ui::SupportedProtocolsDialog),
    supported_protocols_model_(new SupportedProtocolsModel()),
    proxyModel_(new SupportedProtocolsProxyModel(this))
{
    ui->setupUi(this);

    proxyModel_->setSourceModel(supported_protocols_model_);
    ui->supportedProtocolsTreeView->setModel(proxyModel_);

    //always sort by protocol/field name
    proxyModel_->sort(SupportedProtocolsModel::colName);

    if (parent)
        loadGeometry(parent->width() * 3 / 4, parent->height());
    setAttribute(Qt::WA_DeleteOnClose, true);

    setWindowTitle(mainApp->windowTitleString(tr("Supported Protocols")));

    // Some of our names are unreasonably long.
    int one_em = fontMetrics().height();
    ui->supportedProtocolsTreeView->setColumnWidth(SupportedProtocolsModel::colName, one_em * 15);
    ui->supportedProtocolsTreeView->setColumnWidth(SupportedProtocolsModel::colFilter, one_em * 10);
    ui->supportedProtocolsTreeView->setColumnWidth(SupportedProtocolsModel::colType, one_em * 12);
    ui->supportedProtocolsTreeView->setColumnWidth(SupportedProtocolsModel::colDescription, one_em * 30);

    QTimer::singleShot(0, this, SLOT(fillTree()));

    /* Create a single-shot timer for debouncing calls to
     * updateSearchLineEdit() */
    searchLineEditTimer = new QTimer(this);
    searchLineEditTimer->setSingleShot(true);
    connect(searchLineEditTimer, &QTimer::timeout, this, &SupportedProtocolsDialog::updateSearchLineEdit);
}

SupportedProtocolsDialog::~SupportedProtocolsDialog()
{
    delete searchLineEditTimer;
    delete ui;
    delete supported_protocols_model_;
    delete proxyModel_;
}

void SupportedProtocolsDialog::updateStatistics()
{
    QLocale locale = QLocale::system();
    QString hint = tr("%1 protocols, %2 fields.")
            .arg(locale.toString(supported_protocols_model_->rowCount()))
            .arg(locale.toString(supported_protocols_model_->fieldCount()));
    ui->hintLabel->setText(hint);
}

void SupportedProtocolsDialog::fillTree()
{
    supported_protocols_model_->populate();
    updateStatistics();
}

void SupportedProtocolsDialog::updateSearchLineEdit()
{
    proxyModel_->setFilter(searchLineEditText);
}

void SupportedProtocolsDialog::on_searchLineEdit_textChanged(const QString &search_re)
{
    /* As filtering the list of protocols takes a noticeable amount
     * of time and so would introduce significant lag while typing a string
     * into the Search box, we instead debounce the call to
     * proxyModel_->setFilter(), so that it doesn't run until a set amount of
     * time has elapsed with no updates to the Search field.
     *
     * If the user types something before the timer elapses, the timer restarts
     * the countdown.
     */
    searchLineEditText = search_re;
    guint gui_debounce_timer = prefs_get_uint_value("gui", "debounce.timer");
    searchLineEditTimer->start(gui_debounce_timer);
}