aboutsummaryrefslogtreecommitdiffstats
path: root/ui/qt/models/column_list_model.cpp
blob: 48cf4e275735fb7d11216b185fe8aa0bc6bf5fe0 (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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/* column_list_models.cpp
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include <ui/qt/models/column_list_model.h>
#include <ui/qt/utils/qt_ui_utils.h>
#include <ui/qt/widgets/field_filter_edit.h>
#include <ui/qt/widgets/syntax_line_edit.h>
#include <ui/qt/utils/wireshark_mime_data.h>

#include <glib.h>
#include <epan/column-info.h>
#include <epan/column.h>
#include <epan/prefs.h>
#include <epan/proto.h>
#include <ui/preference_utils.h>

#include <QLineEdit>
#include <QStringList>
#include <QComboBox>

struct ListElement
{
    QString title;
    QString customFields;
    int nr;
    int type;
    int originalType;
    int occurrence;
    bool displayed;
    bool changed;
};

static QList<ListElement> store_;

ColumnProxyModel::ColumnProxyModel(QObject * parent) :
    QSortFilterProxyModel(parent),
    showDisplayedOnly_(false)
{}

bool ColumnProxyModel::filterAcceptsRow(int source_row, const QModelIndex &/*source_parent*/) const
{
    bool displayed = false;
    if (sourceModel() &&
         sourceModel()->index(source_row, ColumnListModel::COL_DISPLAYED).data(ColumnListModel::DisplayedState).toBool())
        displayed = true;

    if (showDisplayedOnly_ && ! displayed)
        return false;

    return true;
}

void ColumnProxyModel::setShowDisplayedOnly(bool set)
{
    showDisplayedOnly_ = set;
    invalidateFilter();
}

ColumnTypeDelegate::ColumnTypeDelegate(QObject * parent) :
    QStyledItemDelegate(parent)
{}

QWidget *ColumnTypeDelegate::createEditor(QWidget *parent,
                                       const QStyleOptionViewItem &option,
                                       const QModelIndex &index) const
{
    if (index.column() == ColumnListModel::COL_TYPE)
    {
        QComboBox *editor = new QComboBox(parent);

        for (int i = 0; i < NUM_COL_FMTS; i++)
        {
            editor->addItem(col_format_desc(i), QVariant(i));
            if (i == index.data().toInt())
                editor->setCurrentIndex(i);
        }

        editor->setFrame(false);

        return editor;
    }
    else if (index.column() == ColumnListModel::COL_FIELDS)
    {
        FieldFilterEdit * editor = new FieldFilterEdit(parent);
        editor->setText(index.data().toString());
        return editor;
    }
    else if (index.column() == ColumnListModel::COL_OCCURRENCE)
    {
        SyntaxLineEdit * editor = new SyntaxLineEdit(parent);
        connect(editor, &SyntaxLineEdit::textChanged, editor, &SyntaxLineEdit::checkInteger);
        editor->setText(index.data().toString());
        return editor;
    }

    return QStyledItemDelegate::createEditor(parent, option, index);
}

void ColumnTypeDelegate::setEditorData(QWidget *editor,
                                    const QModelIndex &index) const
{
    QVariant data = index.model()->data(index);
    if (index.column() == ColumnListModel::COL_TYPE)
    {
        QComboBox *comboBox = static_cast<QComboBox*>(editor);
        comboBox->setCurrentText(data.toString());
    }
    else if (index.column() == ColumnListModel::COL_FIELDS)
    {
        if (qobject_cast<FieldFilterEdit *>(editor))
            qobject_cast<FieldFilterEdit *>(editor)->setText(data.toString());
    }
    else if (index.column() == ColumnListModel::COL_OCCURRENCE)
    {
        if (qobject_cast<SyntaxLineEdit *>(editor))
            qobject_cast<SyntaxLineEdit *>(editor)->setText(data.toString());
    }
    else
    {
        if (qobject_cast<QLineEdit *>(editor))
            qobject_cast<QLineEdit *>(editor)->setText(data.toString());
    }
}

void ColumnTypeDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
                                   const QModelIndex &index) const
{
    if (index.column() == ColumnListModel::COL_TYPE)
    {
        QComboBox *comboBox = static_cast<QComboBox*>(editor);
        bool ok = false;
        int value = comboBox->currentData().toInt(&ok);

        if (ok)
            model->setData(index, value, Qt::EditRole);
    }
    else if (index.column() == ColumnListModel::COL_FIELDS)
    {
        FieldFilterEdit * ffe = qobject_cast<FieldFilterEdit *>(editor);
        if (ffe && ffe->checkFilter())
        {
            QModelIndex typeIndex = index.sibling(index.row(), ColumnListModel::COL_TYPE);
            model->setData(typeIndex, COL_CUSTOM, Qt::EditRole);
            model->setData(index, ffe->text(), Qt::EditRole);
        }
        else
            ffe->setText(index.data().toString());

        if (index.data().toString().length() == 0)
        {
            QModelIndex typeIndex = index.sibling(index.row(), ColumnListModel::COL_TYPE);
            model->setData(typeIndex, index.data(ColumnListModel::OriginalType).toInt(), Qt::EditRole);

        }
    }
    else if (index.column() == ColumnListModel::COL_OCCURRENCE)
    {
        SyntaxLineEdit * sle = qobject_cast<SyntaxLineEdit *>(editor);
        bool ok = false;
        if (sle)
        {
            sle->checkInteger(index.data().toString());
            if (sle->syntaxState() == SyntaxLineEdit::Valid)
                ok = true;
        }

        if (ok)
        {
            QModelIndex typeIndex = index.sibling(index.row(), ColumnListModel::COL_TYPE);
            model->setData(typeIndex, COL_CUSTOM, Qt::EditRole);
            model->setData(index, sle->text(), Qt::EditRole);
        }
        else
            sle->setText(index.data().toString());

        if (index.data().toString().length() == 0)
        {
            QModelIndex typeIndex = index.sibling(index.row(), ColumnListModel::COL_TYPE);
            model->setData(typeIndex, index.data(ColumnListModel::OriginalType).toInt(), Qt::EditRole);

        }
    }
    else
        QStyledItemDelegate::setModelData(editor, model, index);
}

void ColumnTypeDelegate::updateEditorGeometry(QWidget *editor,
                                           const QStyleOptionViewItem &option,
                                           const QModelIndex &/* index */) const
{
    editor->setGeometry(option.rect);
}

ColumnListModel::ColumnListModel(QObject * parent):
    QAbstractTableModel(parent)
{
    populate();
}

QVariant ColumnListModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (section > ColumnListModel::COL_OCCURRENCE || orientation != Qt::Horizontal ||
        role != Qt::DisplayRole)
        return QVariant();

    return headerTitle(section);
}

int ColumnListModel::rowCount(const QModelIndex &/*parent*/) const
{
    return store_.count();
}

int ColumnListModel::columnCount(const QModelIndex &/*parent*/) const
{
    return ColumnListModel::COL_OCCURRENCE + 1;
}

QString ColumnListModel::headerTitle(int section) const
{
    switch (section)
    {
        case ColumnListModel::COL_DISPLAYED:
            return tr("Displayed");
        case ColumnListModel::COL_TITLE:
            return tr("Title");
        case ColumnListModel::COL_TYPE:
            return tr("Type");
        case ColumnListModel::COL_FIELDS:
            return tr("Fields");
        case ColumnListModel::COL_OCCURRENCE:
            return tr("Field Occurence");
    }

    return QString();
}

void ColumnListModel::populate()
{
    store_.clear();

    int nr = 0;

    for (GList *cur = g_list_first(prefs.col_list); cur != NULL && cur->data != NULL; cur = cur->next) {
        fmt_data *cfmt = (fmt_data *) cur->data;
        ListElement ne;
        ne.nr = nr;
        ne.displayed = cfmt->visible;
        ne.title = cfmt->title;
        ne.type = ne.originalType = cfmt->fmt;
        ne.customFields = cfmt->custom_fields;
        ne.occurrence = cfmt->custom_occurrence;

        nr++;
        store_ << ne;
    }
}

QVariant ColumnListModel::data(const QModelIndex &index, int role) const
{
    if (! index.isValid() || index.column() >= store_.count())
        return QVariant();

    ListElement ne = store_.at(index.row());

    if (role == Qt::DisplayRole)
    {
        switch (index.column())
        {
            case COL_DISPLAYED:
                return QVariant();
            case ColumnListModel::COL_TITLE:
                return ne.title;
            case ColumnListModel::COL_TYPE:
                return col_format_desc(ne.type);
            case ColumnListModel::COL_FIELDS:
                return ne.customFields;
            case ColumnListModel::COL_OCCURRENCE:
                return ne.customFields.length() > 0 ? qVariantFromValue(ne.occurrence) : QVariant();
        }
    }
    else if (role == Qt::CheckStateRole)
    {
        if (index.column() == COL_DISPLAYED)
        {
            return ne.displayed ? Qt::Checked : Qt::Unchecked;
        }
    }
    else if (role == OriginalType)
        return qVariantFromValue(ne.originalType);
    else if (role == DisplayedState)
        return qVariantFromValue(ne.displayed);

    return QVariant();
}

Qt::ItemFlags ColumnListModel::flags(const QModelIndex &index) const
{
    Qt::ItemFlags defaultFlags = QAbstractTableModel::flags(index);
    if (index.isValid() && index.row() < store_.count())
    {
        ListElement ne = store_.at(index.row());

        Qt::ItemFlags flags = Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | defaultFlags;

        if (index.column() == COL_DISPLAYED)
            flags |= Qt::ItemIsUserCheckable;

        flags |= Qt::ItemIsEditable;

        return flags;
    }
    else
        return Qt::ItemIsDropEnabled | defaultFlags;
}

QStringList ColumnListModel::mimeTypes() const
{
    return QStringList() << WiresharkMimeData::ColumnListMimeType;
}

QMimeData *ColumnListModel::mimeData(const QModelIndexList &indexes) const
{
    QMimeData *mimeData = new QMimeData;

    int row = -1;
    if (indexes.count() > 0)
        row = indexes.at(0).row();

    mimeData->setData(WiresharkMimeData::ColumnListMimeType, QString::number(row).toUtf8());
    return mimeData;
}

bool ColumnListModel::canDropMimeData(const QMimeData *data,
    Qt::DropAction /* action */, int /* row */, int /* column */, const QModelIndex &parent) const
{
    if (parent.isValid() || ! data->hasFormat(WiresharkMimeData::ColumnListMimeType))
        return false;

    return true;
}

bool ColumnListModel::dropMimeData(const QMimeData *data,
    Qt::DropAction action, int row, int column, const QModelIndex &parent)
{
    int moveTo;

    if (!canDropMimeData(data, action, row, column, parent))
        return false;

    if (action == Qt::IgnoreAction || parent.isValid())
        return true;

    if (row != -1)
        moveTo = row;
    else
        moveTo = rowCount(QModelIndex());

    bool ok = false;
    int moveFrom = QString(data->data(WiresharkMimeData::ColumnListMimeType)).toInt(&ok);
    if (! ok)
        return false;

    if (moveFrom < moveTo)
        moveTo = moveTo - 1;

    if (moveTo >= store_.count())
        moveTo = store_.count() - 1;

    emit beginResetModel();
    store_.move(moveFrom, moveTo);
    emit endResetModel();

    return true;
}

Qt::DropActions ColumnListModel::supportedDropActions() const
{
    return Qt::MoveAction;
}

bool ColumnListModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if (!index.isValid() || ! value.isValid())
        return false;

    bool change = false;
    if (role == Qt::CheckStateRole && index.column() == ColumnListModel::COL_DISPLAYED)
    {
        store_[index.row()].displayed = value.toInt() == Qt::Checked ? true : false;
        change = true;
    }
    else if (index.column() == ColumnListModel::COL_TYPE)
    {
        bool ok = false;
        int val = value.toInt(&ok);
        if (ok)
            store_[index.row()].type = val;
    }
    else if (index.column() == ColumnListModel::COL_TITLE)
    {
        store_[index.row()].title = value.toString();
    }
    else if (index.column() == ColumnListModel::COL_FIELDS)
    {
        store_[index.row()].customFields = value.toString();
    }
    else if (index.column() == ColumnListModel::COL_OCCURRENCE)
    {
        bool ok = false;
        int val = value.toInt(&ok);
        if (ok)
            store_[index.row()].occurrence = val;
    }

    if (change)
        emit dataChanged(index, index);

    return change;
}

void ColumnListModel::saveColumns()
{
    GList *new_col_list = Q_NULLPTR;

    for (int row = 0; row < store_.count(); row++)
    {
        fmt_data * cfmt = g_new0(fmt_data, 1);
        ListElement elem = store_.at(row);

        cfmt->title = qstring_strdup(elem.title);
        cfmt->visible = elem.displayed;
        cfmt->fmt = elem.type;
        cfmt->resolved = TRUE;
        if (cfmt->fmt == COL_CUSTOM)
        {
            cfmt->custom_fields = qstring_strdup(elem.customFields);
            cfmt->custom_occurrence = elem.occurrence;
        }

        new_col_list = g_list_append(new_col_list, cfmt);
    }

    while (prefs.col_list)
        column_prefs_remove_link(prefs.col_list);

    prefs.col_list = new_col_list;
}

void ColumnListModel::addEntry()
{
    emit beginInsertRows(QModelIndex(), rowCount(), rowCount());
    ListElement elem;
    elem.nr = rowCount();
    elem.title = tr("New Column");
    elem.displayed = true;
    elem.type = elem.originalType = COL_NUMBER;
    elem.occurrence = 0;
    elem.customFields = QString();
    store_ << elem;
    emit endInsertRows();
}

void ColumnListModel::deleteEntry(int row)
{
    emit beginRemoveRows(QModelIndex(), row, row);
    store_.removeAt(row);
    emit endRemoveRows();
}

void ColumnListModel::reset()
{
    emit beginResetModel();
    populate();
    emit endResetModel();
}

/*
 * 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:
 */