1 /* stats_tree_dialog.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 "stats_tree_dialog.h"
14 #include "epan/stats_tree_priv.h"
16 #include <ui/qt/utils/qt_ui_utils.h>
18 #include <ui/qt/utils/variant_pointer.h>
20 #include <QHeaderView>
21 #include <QMessageBox>
22 #include <QTreeWidget>
23 #include <QTreeWidgetItemIterator>
25 const int item_col_
= 0;
27 const int sn_type_
= 1000;
28 class StatsTreeWidgetItem
: public QTreeWidgetItem
31 StatsTreeWidgetItem(int type
= sn_type_
) : QTreeWidgetItem (type
)
33 for (int col
= 1; col
< columnCount(); col
++) {
34 setTextAlignment(col
, Qt::AlignRight
);
37 bool operator< (const QTreeWidgetItem
&other
) const
39 stat_node
*thisnode
= VariantPointer
<stat_node
>::asPtr(data(item_col_
, Qt::UserRole
));
40 stat_node
*othernode
= VariantPointer
<stat_node
>::asPtr(other
.data(item_col_
, Qt::UserRole
));
41 Qt::SortOrder order
= treeWidget()->header()->sortIndicatorOrder();
44 result
= stats_tree_sort_compare(thisnode
, othernode
, treeWidget()->sortColumn(),
45 order
==Qt::DescendingOrder
);
46 if (order
==Qt::DescendingOrder
) {
53 StatsTreeDialog::StatsTreeDialog(QWidget
&parent
, CaptureFile
&cf
, const char *cfg_abbr
) :
54 TapParameterDialog(parent
, cf
),
58 loadGeometry(800, height(), cfg_abbr
);
59 st_cfg_
= stats_tree_get_cfg_by_abbr(cfg_abbr
);
60 memset(&cfg_pr_
, 0, sizeof(struct _tree_cfg_pres
));
62 addTreeCollapseAllActions();
65 QMessageBox::critical(this, tr("Configuration not found"),
66 tr("Unable to find configuration for %1.").arg(cfg_abbr
));
67 QMetaObject::invokeMethod(this, "reject", Qt::QueuedConnection
);
71 StatsTreeDialog::~StatsTreeDialog()
78 // Adds a node to the QTreeWidget
79 // Note: We're passing QTreeWidgetItem pointers as st_node_pres pointers
80 void StatsTreeDialog::setupNode(stat_node
* node
)
82 if (!node
|| !node
->st
|| !node
->st
->cfg
|| !node
->st
->cfg
->pr
83 || !node
->st
->cfg
->pr
->st_dlg
) return;
84 StatsTreeDialog
*st_dlg
= node
->st
->cfg
->pr
->st_dlg
;
86 QTreeWidgetItem
*ti
= new StatsTreeWidgetItem(), *parent
= NULL
;
88 ti
->setText(item_col_
, node
->name
);
89 ti
->setData(item_col_
, Qt::UserRole
, VariantPointer
<stat_node
>::asQVariant(node
));
90 node
->pr
= (st_node_pres
*) ti
;
91 if (node
->parent
&& node
->parent
->pr
) {
92 parent
= (QTreeWidgetItem
*) node
->parent
->pr
;
93 parent
->setExpanded(true);
98 st_dlg
->statsTreeWidget()->addTopLevelItem(ti
);
102 void StatsTreeDialog::fillTree()
104 if (!st_cfg_
|| file_closed_
) return;
106 // The GTK+ UI appended "Stats Tree" to the window title. If we do the same
107 // here we should expand the name completely, e.g. to "Statistics Tree".
108 setWindowSubtitle(st_cfg_
->title
);
110 st_cfg_
->pr
= &cfg_pr_
;
111 cfg_pr_
.st_dlg
= this;
114 stats_tree_free(st_
);
116 QString display_filter
= displayFilter();
117 st_
= stats_tree_new(st_cfg_
, NULL
, display_filter
.toUtf8().constData());
119 // Add number of columns for this stats_tree
120 QStringList header_labels
;
121 for (int count
= 0; count
<st_
->num_columns
; count
++) {
122 header_labels
.push_back(stats_tree_get_column_name(st_cfg_
, count
));
124 statsTreeWidget()->setColumnCount(static_cast<int>(header_labels
.count()));
125 statsTreeWidget()->setHeaderLabels(header_labels
);
126 statsTreeWidget()->setSortingEnabled(false);
128 if (!registerTapListener(st_cfg_
->tapname
,
135 reject(); // XXX Stay open instead?
139 cap_file_
.retapPackets();
142 removeTapListeners();
145 statsTreeWidget()->setSortingEnabled(true);
146 statsTreeWidget()->resizeColumnToContents(item_col_
);
149 void StatsTreeDialog::resetTap(void *st_ptr
)
151 stats_tree
*st
= (stats_tree
*) st_ptr
;
152 if (!st
|| !st
->cfg
|| !st
->cfg
->pr
|| !st
->cfg
->pr
->st_dlg
) return;
154 st
->cfg
->pr
->st_dlg
->statsTreeWidget()->clear();
158 void StatsTreeDialog::drawTreeItems(void *st_ptr
)
160 stats_tree
*st
= (stats_tree
*) st_ptr
;
161 if (!st
|| !st
->cfg
|| !st
->cfg
->pr
|| !st
->cfg
->pr
->st_dlg
) return;
162 TapParameterDialog
*st_dlg
= st
->cfg
->pr
->st_dlg
;
163 QTreeWidgetItemIterator
iter(st_dlg
->statsTreeWidget());
166 stat_node
*node
= VariantPointer
<stat_node
>::asPtr((*iter
)->data(item_col_
, Qt::UserRole
));
168 char **valstrs
= stats_tree_get_values_from_node(node
);
169 for (int count
= 0; count
<st
->num_columns
; count
++) {
170 (*iter
)->setText(count
,valstrs
[count
]);
171 g_free(valstrs
[count
]);
173 (*iter
)->setExpanded((node
->parent
==(&st
->root
)) &&
174 (!(node
->st_flags
&ST_FLG_DEF_NOEXPAND
)));
180 st_dlg
->drawTreeItems();
183 QByteArray
StatsTreeDialog::getTreeAsString(st_format_type format
)
187 // produce output in selected format using current sort information
188 str_tree
= stats_tree_format_as_str(st_
, format
, statsTreeWidget()->sortColumn(),
189 statsTreeWidget()->header()->sortIndicatorOrder()==Qt::DescendingOrder
);
191 return gstring_free_to_qbytearray(str_tree
);
196 void register_tap_listener_qt_stats_tree_stat(void);
199 register_tap_listener_qt_stats_tree_stat(void)
201 stats_tree_presentation(NULL
,
202 StatsTreeDialog::setupNode
,