aboutsummaryrefslogtreecommitdiffstats
path: root/ui/qt/models/proto_tree_model.cpp
blob: 276b5a8f26320c452b15a45ec30540d5d37af9c0 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/* proto_tree_model.cpp
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0+
 */

#include <ui/qt/models/proto_tree_model.h>

#include <epan/prefs.h>

#include <ui/qt/utils/color_utils.h>

#include <QApplication>
#include <QPalette>

// To do:
// - Add ProtoTreeDelegate
// - Add ProtoTreeModel to CaptureFile

ProtoTreeModel::ProtoTreeModel(QObject * parent) :
    QAbstractItemModel(parent),
    root_node_(0)
{}

Qt::ItemFlags ProtoTreeModel::flags(const QModelIndex &index) const
{
    Qt::ItemFlags item_flags = QAbstractItemModel::flags(index);
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
    if (rowCount(index) < 1) {
        item_flags |= Qt::ItemNeverHasChildren;
    }
#endif
    return item_flags;
}

QModelIndex ProtoTreeModel::index(int row, int, const QModelIndex &parent) const
{
    ProtoNode parent_node(root_node_);

    if (parent.isValid()) {
        // index is not a top level item.
        parent_node = protoNodeFromIndex(parent);
    }

    if (! parent_node.isValid() )
        return QModelIndex();

    int cur_row = 0;
    ProtoNode::ChildIterator kids = parent_node.children();
    while ( kids.element().isValid() )
    {
        if ( cur_row == row )
            break;
        cur_row++;
        kids.next();
    }
    if ( ! kids.element().isValid() ) {
        return QModelIndex();
    }

    return createIndex(row, 0, static_cast<void *>(kids.element().protoNode()));
}

QModelIndex ProtoTreeModel::parent(const QModelIndex &index) const
{
    ProtoNode parent_node = protoNodeFromIndex(index).parentNode();
    return indexFromProtoNode(parent_node);
}

int ProtoTreeModel::rowCount(const QModelIndex &parent) const
{
    if (parent.isValid()) {
        return protoNodeFromIndex(parent).childrenCount();
    }
    return ProtoNode(root_node_).childrenCount();
}

// The QItemDelegate documentation says
// "When displaying items from a custom model in a standard view, it is
//  often sufficient to simply ensure that the model returns appropriate
//  data for each of the roles that determine the appearance of items in
//  views."
// We might want to move this to a delegate regardless.
QVariant ProtoTreeModel::data(const QModelIndex &index, int role) const
{
    ProtoNode index_node = protoNodeFromIndex(index);
    FieldInformation finfo(index_node.protoNode());
    if (!finfo.isValid()) {
        return QVariant();
    }

    switch (role) {
    case Qt::DisplayRole:
        return index_node.labelText();
    case Qt::BackgroundRole:
    {
        switch(finfo.flag(PI_SEVERITY_MASK)) {
        case(0):
            break;
        case(PI_COMMENT):
            return ColorUtils::expert_color_comment;
        case(PI_CHAT):
            return ColorUtils::expert_color_chat;
        case(PI_NOTE):
            return ColorUtils::expert_color_note;
        case(PI_WARN):
            return ColorUtils::expert_color_warn;
        case(PI_ERROR):
            return ColorUtils::expert_color_error;
        default:
            g_warning("%s:%d Unhandled severity flag: %u", G_STRFUNC, __LINE__, finfo.flag(PI_SEVERITY_MASK));
        }
        if(finfo.headerInfo().type == FT_PROTOCOL) {
            return QApplication::palette().window();
        }
        return QApplication::palette().base();
    }
    case Qt::ForegroundRole:
    {
        if(finfo.flag(PI_SEVERITY_MASK)) {
            return ColorUtils::expert_color_foreground;
        }
        if (finfo.isLink()) {
            return QApplication::palette().link();
        }
        if(finfo.headerInfo().type == FT_PROTOCOL) {
            return QApplication::palette().windowText();
        }
        return QApplication::palette().text();
    }
    case Qt::FontRole:
        if (finfo.isLink()) {
            QFont font;
            font.setUnderline(true);
            return font;
        }
    default:
        break;
    }

    return QVariant();
}

void ProtoTreeModel::setRootNode(proto_node *root_node)
{
    beginResetModel();
    root_node_ = root_node;
    endResetModel();
    if (!root_node) return;

    int row_count = ProtoNode(root_node_).childrenCount();
    if (row_count < 1) return;
    beginInsertRows(QModelIndex(), 0, row_count - 1);
    endInsertRows();
}

ProtoNode ProtoTreeModel::protoNodeFromIndex(const QModelIndex &index) const
{
    return ProtoNode(static_cast<proto_node*>(index.internalPointer()));
}

QModelIndex ProtoTreeModel::indexFromProtoNode(ProtoNode &index_node) const
{
    int row = index_node.row();

    if (!index_node.isValid() || row < 0) {
        return QModelIndex();
    }

    return createIndex(row, 0, static_cast<void *>(index_node.protoNode()));
}

struct find_hfid_ {
    int hfid;
    ProtoNode node;
};

void ProtoTreeModel::foreachFindHfid(proto_node *node, gpointer find_hfid_ptr)
{
    struct find_hfid_ *find_hfid = (struct find_hfid_ *) find_hfid_ptr;
    if (PNODE_FINFO(node)->hfinfo->id == find_hfid->hfid) {
        find_hfid->node = ProtoNode(node);
        return;
    }
    proto_tree_children_foreach(node, foreachFindHfid, find_hfid);
}

QModelIndex ProtoTreeModel::findFirstHfid(int hf_id)
{
    if (!root_node_ || hf_id < 0) return QModelIndex();

    struct find_hfid_ find_hfid;
    find_hfid.hfid = hf_id;

    proto_tree_children_foreach(root_node_, foreachFindHfid, &find_hfid);

    if (find_hfid.node.isValid()) {
        return indexFromProtoNode(find_hfid.node);
    }
    return QModelIndex();
}

struct find_field_info_ {
    field_info *fi;
    ProtoNode node;
};

void ProtoTreeModel::foreachFindField(proto_node *node, gpointer find_finfo_ptr)
{
    struct find_field_info_ *find_finfo = (struct find_field_info_ *) find_finfo_ptr;
    if (PNODE_FINFO(node) == find_finfo->fi) {
        find_finfo->node = ProtoNode(node);
        return;
    }
    proto_tree_children_foreach(node, foreachFindField, find_finfo);
}

QModelIndex ProtoTreeModel::findFieldInformation(FieldInformation *finfo)
{
    if (!root_node_ || !finfo) return QModelIndex();
    field_info * fi = finfo->fieldInfo();
    if (!fi) return QModelIndex();

    struct find_field_info_ find_finfo;
    find_finfo.fi = fi;

    proto_tree_children_foreach(root_node_, foreachFindField, &find_finfo);
    if (find_finfo.node.isValid()) {
        return indexFromProtoNode(find_finfo.node);
    }
    return QModelIndex();
}

/*
 * Editor modelines
 *
 * Local Variables:
 * c-basic-offset: 4
 * tab-width: 8
 * indent-tabs-mode: nil
 * End:
 *
 * ex: set shiftwidth=4 tabstop=8 expandtab:
 * :indentSize=4:tabSize=8:noTabs=true:
 */