summaryrefslogtreecommitdiffstats
path: root/ui/qt/models/pref_delegate.cpp
blob: e33bb13f23fa3ec5da4374a694df90e3ab0327ab (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
/* pref_delegate.cpp
 * Delegates for editing prefereneces.
 *
 * 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/pref_delegate.h>
#include <epan/prefs-int.h>

#include <ui/qt/manager/preference_manager.h>
#include <ui/qt/manager/wireshark_preference.h>

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

PrefsItem* AdvancedPrefDelegate::indexToPref(const QModelIndex &index) const
{
    const QVariant v = index.model()->data(index, Qt::UserRole);
    return VariantPointer<PrefsItem>::asPtr(v);
}

QWidget *AdvancedPrefDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option,
                                  const QModelIndex &index) const
{
    PrefsItem* pref;
    QString filename;

    switch(index.column())
    {
    case AdvancedPrefsModel::colName:
    case AdvancedPrefsModel::colStatus:
    case AdvancedPrefsModel::colType:
        //If user clicks on any of these columns, reset preference back to default
        //There is no need to launch an editor
        const_cast<QAbstractItemModel*>(index.model())->setData(index, QVariant(), Qt::EditRole);
        break;
    case AdvancedPrefsModel::colValue:
        pref = indexToPref(index);
        WiresharkPreference * wspref = PreferenceManager::instance()->getPreference(pref);
        if (wspref) {
            QWidget *editor = wspref->editor(parent, option, index);
            if (editor) {
                editor->setAutoFillBackground(true);
            }
            return editor;
        }
        break;
    }

    return Q_NULLPTR;
}

void AdvancedPrefDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    PrefsItem* pref = indexToPref(index);

    WiresharkPreference * wspref = PreferenceManager::instance()->getPreference(pref);
    if (wspref)
    {
        wspref->setData(editor, index);
        return;
    }

    Q_ASSERT(FALSE);
}

void AdvancedPrefDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
                              const QModelIndex &index) const
{
    PrefsItem* pref = indexToPref(index);

    WiresharkPreference * wspref = PreferenceManager::instance()->getPreference(pref);
    if (wspref)
    {
        wspref->setModelData(editor, model, index);
        return;
    }

    Q_ASSERT(FALSE);
}