| Wireshark 4.7.0 The Wireshark network protocol analyzer

Loading...
Searching...
No Matches
lua_debugger_item_utils.h
1/* lua_debugger_item_utils.h
2 *
3 * Helpers for QTreeView + QStandardItemModel rows (multi-column items).
4 *
5 * These follow Qt’s model/view split: the view owns expansion state; use
6 * isExpanded/setExpanded here rather than QStandardItem APIs. Prefer
7 * QModelIndex and model data (e.g. rowColumnDisplayText) at view boundaries
8 * so a future QSortFilterProxyModel can sit in front of the model without
9 * rewriting call sites. QStandardItem navigation remains appropriate for
10 * lazy tree construction (appendRow, child lookup).
11 *
12 * Wireshark - Network traffic analyzer
13 * By Gerald Combs <gerald@wireshark.org>
14 * Copyright 1998 Gerald Combs
15 *
16 * SPDX-License-Identifier: GPL-2.0-or-later
17 */
18
19#ifndef LUA_DEBUGGER_ITEM_UTILS_H
20#define LUA_DEBUGGER_ITEM_UTILS_H
21
22#include <QBrush>
23#include <QFont>
24#include <QIcon>
25#include <QModelIndex>
26#include <QStandardItem>
27#include <QStandardItemModel>
28#include <QString>
29#include <QTreeView>
30#include <QVariant>
31
32 namespace LuaDebuggerItems
33{
34
36 inline QString
37rowColumnDisplayText(const QModelIndex &indexInRow, int col)
38{
39 if (!indexInRow.isValid())
40 {
41 return QString();
42 }
43 return indexInRow.sibling(indexInRow.row(), col).data(Qt::DisplayRole).toString();
44}
45
47 inline QStandardItem *
48rowCol0(QStandardItemModel *model, QStandardItem *cell)
49{
50 if (!model || !cell)
51 {
52 return nullptr;
53 }
54 const QModelIndex ix = model->indexFromItem(cell);
55 if (!ix.isValid())
56 {
57 return nullptr;
58 }
59 return model->itemFromIndex(ix.sibling(ix.row(), 0));
60}
61
63 inline QStandardItem *
64cellAt(QStandardItemModel *model, QStandardItem *col0, int col)
65{
66 if (!model || !col0 || col0->column() != 0)
67 {
68 return nullptr;
69 }
70 QStandardItem *par = col0->parent();
71 if (!par)
72 {
73 return model->item(col0->row(), col);
74 }
75 return par->child(col0->row(), col);
76}
77
78 inline QString
79text(QStandardItemModel *model, QStandardItem *col0, int col)
80{
81 QStandardItem *c = cellAt(model, col0, col);
82 return c ? c->text() : QString();
83}
84
85 inline void
86setText(QStandardItemModel *model, QStandardItem *col0, int col, const QString &t)
87{
88 QStandardItem *c = cellAt(model, col0, col);
89 if (c)
90 {
91 c->setText(t);
92 }
93}
94
95 inline void
96setToolTip(QStandardItemModel *model, QStandardItem *col0, int col, const QString &tip)
97{
98 QStandardItem *c = cellAt(model, col0, col);
99 if (c)
100 {
101 c->setToolTip(tip);
102 }
103}
104
105 inline void
106setFont(QStandardItemModel *model, QStandardItem *col0, int col, const QFont &font)
107{
108 QStandardItem *c = cellAt(model, col0, col);
109 if (c)
110 {
111 c->setFont(font);
112 }
113}
114
115 inline void
116setForeground(QStandardItemModel *model, QStandardItem *col0, int col,
117 const QBrush &brush)
118{
119 QStandardItem *c = cellAt(model, col0, col);
120 if (c)
121 {
122 c->setForeground(brush);
123 }
124}
125
126 inline void
127setBackground(QStandardItemModel *model, QStandardItem *col0, int col,
128 const QBrush &brush)
129{
130 QStandardItem *c = cellAt(model, col0, col);
131 if (c)
132 {
133 c->setBackground(brush);
134 }
135}
136
137 inline void
138setIcon(QStandardItemModel *model, QStandardItem *col0, int col, const QIcon &icon)
139{
140 QStandardItem *c = cellAt(model, col0, col);
141 if (c)
142 {
143 c->setIcon(icon);
144 }
145}
146
147 inline void
148setTextAlignment(QStandardItemModel *model, QStandardItem *col0, int col,
149 Qt::Alignment align)
150{
151 QStandardItem *c = cellAt(model, col0, col);
152 if (c)
153 {
154 c->setTextAlignment(align);
155 }
156}
157
158 inline QModelIndex
159indexCol0(QStandardItemModel *model, QStandardItem *col0)
160{
161 if (!model || !col0 || col0->column() != 0)
162 {
163 return QModelIndex();
164 }
165 return model->indexFromItem(col0);
166}
167
168 inline bool
169isExpanded(QTreeView *tree, QStandardItemModel *model, QStandardItem *col0)
170{
171 const QModelIndex ix = indexCol0(model, col0);
172 return ix.isValid() && tree->isExpanded(ix);
173}
174
175 inline void
176setExpanded(QTreeView *tree, QStandardItemModel *model, QStandardItem *col0,
177 bool expanded)
178{
179 const QModelIndex ix = indexCol0(model, col0);
180 if (ix.isValid())
181 {
182 tree->setExpanded(ix, expanded);
183 }
184}
185
186} // namespace LuaDebuggerItems
187
188#endif /* LUA_DEBUGGER_ITEM_UTILS_H */