1 /* pref_delegate.cpp
2  * Delegates for editing prefereneces.
3  *
4  * Wireshark - Network traffic analyzer
5  * By Gerald Combs <gerald@wireshark.org>
6  * Copyright 1998 Gerald Combs
7  *
8  * SPDX-License-Identifier: GPL-2.0-or-later
9  */
10 
11 #include <ui/qt/models/pref_delegate.h>
12 #include <epan/prefs-int.h>
13 
14 #include <ui/qt/manager/preference_manager.h>
15 #include <ui/qt/manager/wireshark_preference.h>
16 
AdvancedPrefDelegate(QObject * parent)17 AdvancedPrefDelegate::AdvancedPrefDelegate(QObject *parent) : QStyledItemDelegate(parent)
18 {
19 }
20 
indexToPref(const QModelIndex & index) const21 PrefsItem* AdvancedPrefDelegate::indexToPref(const QModelIndex &index) const
22 {
23     const QVariant v = index.model()->data(index, Qt::UserRole);
24     return VariantPointer<PrefsItem>::asPtr(v);
25 }
26 
createEditor(QWidget * parent,const QStyleOptionViewItem & option,const QModelIndex & index) const27 QWidget *AdvancedPrefDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option,
28                                   const QModelIndex &index) const
29 {
30     PrefsItem* pref;
31     QString filename;
32 
33     switch(index.column())
34     {
35     case AdvancedPrefsModel::colName:
36     case AdvancedPrefsModel::colStatus:
37     case AdvancedPrefsModel::colType:
38         //If user clicks on any of these columns, reset preference back to default
39         //There is no need to launch an editor
40         const_cast<QAbstractItemModel*>(index.model())->setData(index, QVariant(), Qt::EditRole);
41         break;
42     case AdvancedPrefsModel::colValue:
43         pref = indexToPref(index);
44         WiresharkPreference * wspref = PreferenceManager::instance()->getPreference(pref);
45         if (wspref) {
46             QWidget *editor = wspref->editor(parent, option, index);
47             if (editor) {
48                 editor->setAutoFillBackground(true);
49             }
50             return editor;
51         }
52         break;
53     }
54 
55     return Q_NULLPTR;
56 }
57 
setEditorData(QWidget * editor,const QModelIndex & index) const58 void AdvancedPrefDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
59 {
60     PrefsItem* pref = indexToPref(index);
61 
62     WiresharkPreference * wspref = PreferenceManager::instance()->getPreference(pref);
63     if (wspref)
64     {
65         wspref->setData(editor, index);
66         return;
67     }
68 
69     Q_ASSERT(FALSE);
70 }
71 
setModelData(QWidget * editor,QAbstractItemModel * model,const QModelIndex & index) const72 void AdvancedPrefDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
73                               const QModelIndex &index) const
74 {
75     PrefsItem* pref = indexToPref(index);
76 
77     WiresharkPreference * wspref = PreferenceManager::instance()->getPreference(pref);
78     if (wspref)
79     {
80         wspref->setModelData(editor, model, index);
81         return;
82     }
83 
84     Q_ASSERT(FALSE);
85 }
86