diff --git a/ui/qt/CMakeLists.txt b/ui/qt/CMakeLists.txt index a826ff1f6e..7b4a8439ed 100644 --- a/ui/qt/CMakeLists.txt +++ b/ui/qt/CMakeLists.txt @@ -46,11 +46,17 @@ set(WIRESHARK_WIDGET_HEADERS widgets/overlay_scroll_bar.h widgets/pref_module_view.h widgets/qcustomplot.h + widgets/range_syntax_lineedit.h widgets/stock_icon_tool_button.h widgets/syntax_line_edit.h widgets/tabnav_tree_view.h ) +set(WIRESHARK_MANAGER_HEADERS + manager/preference_manager.h + manager/wireshark_preference.h +) + set(WIRESHARK_UTILS_HEADERS utils/color_utils.h utils/data_printer.h @@ -272,11 +278,17 @@ set(WIRESHARK_WIDGET_SRCS widgets/overlay_scroll_bar.cpp widgets/pref_module_view.cpp widgets/qcustomplot.cpp + widgets/range_syntax_lineedit.cpp widgets/stock_icon_tool_button.cpp widgets/syntax_line_edit.cpp widgets/tabnav_tree_view.cpp ) +set(WIRESHARK_MANAGER_SRCS + manager/preference_manager.cpp + manager/wireshark_preference.cpp +) + set(WIRESHARK_UTILS_SRCS utils/color_utils.cpp utils/data_printer.cpp @@ -660,16 +672,20 @@ source_group("ui\\Utils Headers Files" FILES ${WIRESHARK_UTILS_HEADERS}) source_group("ui\\Utils Source" FILES ${WIRESHARK_UTILS_SRCS}) source_group("ui\\Models Headers" FILES ${WIRESHARK_MODEL_HEADERS}) source_group("ui\\Models Source" FILES ${WIRESHARK_MODEL_SRCS}) +source_group("ui\\Manager Headers" FILES ${WIRESHARK_MANAGER_HEADERS}) +source_group("ui\\Manager Source" FILES ${WIRESHARK_MANAGER_SRCS}) add_library(qtui STATIC #Included so that Visual Studio can properly put header files in solution ${WIRESHARK_QT_HEADERS} ${WIRESHARK_WIDGET_HEADERS} + ${WIRESHARK_MANAGER_HEADERS} ${WIRESHARK_UTILS_HEADERS} ${WIRESHARK_MODEL_HEADERS} ${WIRESHARK_QT_SRC} ${WIRESHARK_WIDGET_SRCS} + ${WIRESHARK_MANAGER_SRCS} ${WIRESHARK_UTILS_SRCS} ${WIRESHARK_MODEL_SRCS} diff --git a/ui/qt/Makefile.am b/ui/qt/Makefile.am index 5be775ba7f..da04af33b8 100644 --- a/ui/qt/Makefile.am +++ b/ui/qt/Makefile.am @@ -175,11 +175,18 @@ MOC_WIDGET_HDRS = \ widgets/label_stack.h \ widgets/overlay_scroll_bar.h \ widgets/pref_module_view.h \ + widgets/range_syntax_lineedit.h \ widgets/syntax_line_edit.h \ widgets/stock_icon_tool_button.h \ widgets/tabnav_tree_view.h \ widgets/qcustomplot.h + +# Files that are general manager classes with +MOC_MANAGER_HDRS = \ + manager/preference_manager.h \ + manager/wireshark_preference.h + # Files that are utility classes with multi-purpose, but no widgets MOC_UTILS_HDRS = \ utils/color_utils.h \ @@ -351,6 +358,7 @@ MOC_HDRS = \ wireshark_dialog.h \ wlan_statistics_dialog.h \ $(MOC_WIDGET_HDRS) \ + $(MOC_MANAGER_HDRS) \ $(MOC_UTILS_HDRS) \ $(MOC_MODELS_HDRS) @@ -516,10 +524,16 @@ WIRESHARK_QT_WIDGET_SRC = \ widgets/overlay_scroll_bar.cpp \ widgets/pref_module_view.cpp \ widgets/stock_icon_tool_button.cpp \ + widgets/range_syntax_lineedit.cpp \ widgets/syntax_line_edit.cpp \ widgets/tabnav_tree_view.cpp \ widgets/qcustomplot.cpp +# Files that are general manager classes with +WIRESHARK_QT_MANAGER_SRC = \ + manager/preference_manager.cpp \ + manager/wireshark_preference.cpp + WIRESHARK_QT_UTILS_SRC = \ utils/color_utils.cpp \ utils/data_printer.cpp \ @@ -676,6 +690,7 @@ WIRESHARK_QT_SRC = \ wireshark_dialog.cpp \ $(WIRESHARK_QT_WIDGET_SRC) \ $(WIRESHARK_QT_UTILS_SRC) \ + $(WIRESHARK_QT_MANAGER_SRC) \ ${WIRESHARK_QT_MODELS_SRCS} WIRESHARK_QT_TAP_SRC = \ diff --git a/ui/qt/manager/preference_manager.cpp b/ui/qt/manager/preference_manager.cpp new file mode 100644 index 0000000000..f979eb6c96 --- /dev/null +++ b/ui/qt/manager/preference_manager.cpp @@ -0,0 +1,78 @@ +/* preference_manager.cpp + * + * Wireshark - Network traffic analyzer + * By Gerald Combs + * Copyright 1998 Gerald Combs + * + * SPDX-License-Identifier: GPL-2.0+ + */ + +#include "config.h" + +#include +#include + +#include + +PreferenceFactory::~PreferenceFactory() {} + +QMap PreferenceManager::factories; + +PreferenceManager::PreferenceManager(QObject * parent) + : QObject(parent) +{} + +PreferenceManager::~PreferenceManager() +{ + /* As this is a singleton, this is the point, where we can clear the registry */ + PreferenceManager::factories.clear(); +} + +PreferenceManager * PreferenceManager::instance() +{ + static PreferenceManager* _inst = 0; + if ( ! _inst ) + _inst = new PreferenceManager(); + + return _inst; +} + +void PreferenceManager::registerType(int pref, PreferenceFactory * factory) +{ + Q_ASSERT(pref >= 0); + + if ( PreferenceManager::factories.contains(pref) || ! factory ) + return; + + PreferenceManager::factories[pref] = factory; +} + +WiresharkPreference * PreferenceManager::getPreference(PrefsItem * pref) +{ + if ( ! pref ) + return Q_NULLPTR; + + int key = pref->getPrefType(); + if ( ! PreferenceManager::factories.contains(key) ) + return Q_NULLPTR; + + /* All actions are parented with this manager, to clear the objects together with the manager */ + WiresharkPreference * wspref = qobject_cast(PreferenceManager::factories[key]->create(this)); + if ( wspref ) + wspref->setPrefsItem(pref); + + return wspref; +} + +/* + * 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: + */ diff --git a/ui/qt/manager/preference_manager.h b/ui/qt/manager/preference_manager.h new file mode 100644 index 0000000000..6ae224ee66 --- /dev/null +++ b/ui/qt/manager/preference_manager.h @@ -0,0 +1,80 @@ +/* preference_manager.h + * + * Wireshark - Network traffic analyzer + * By Gerald Combs + * Copyright 1998 Gerald Combs + * + * SPDX-License-Identifier: GPL-2.0+ + */ + +#ifndef PREFERENCE_MANAGER_H +#define PREFERENCE_MANAGER_H + +#include + +#include +#include +#include +#include + +#include +#include + +class PreferenceFactory; +class WiresharkPreference; + +class PreferenceManager : public QObject +{ + Q_OBJECT + +public: + static PreferenceManager* instance(); + virtual ~PreferenceManager(); + + void registerType(int pref, PreferenceFactory * factory); + void reuseType(int pref, int reuseFor); + WiresharkPreference * getPreference(PrefsItem * item); + +protected: + explicit PreferenceManager(QObject * parent = Q_NULLPTR); + +private: + static QMap factories; +}; + +class PreferenceFactory : public QObject +{ + Q_OBJECT +public: + virtual ~PreferenceFactory(); + virtual WiresharkPreference * create(QObject * parent = Q_NULLPTR) = 0; +}; + +#define REGISTER_PREFERENCE_TYPE(pref_id, preference_class) \ + class preference_class##pref_id##Factory : public PreferenceFactory { \ + public: \ + preference_class##pref_id##Factory() \ + { \ + PreferenceManager::instance()->registerType(pref_id, this); \ + } \ + virtual WiresharkPreference *create(QObject * parent) { \ + WiresharkPreference * newPrefHandler = new preference_class(parent); \ + return newPrefHandler; \ + } \ + }; \ + static preference_class##pref_id##Factory global_##preference_class##pref_id##Factory; + +#endif // PREFERENCE_MANAGER_H + +/* + * 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: + */ diff --git a/ui/qt/manager/wireshark_preference.cpp b/ui/qt/manager/wireshark_preference.cpp new file mode 100644 index 0000000000..323a703206 --- /dev/null +++ b/ui/qt/manager/wireshark_preference.cpp @@ -0,0 +1,241 @@ +/* wireshark_preference.cpp + * + * Wireshark - Network traffic analyzer + * By Gerald Combs + * Copyright 1998 Gerald Combs + * + * SPDX-License-Identifier: GPL-2.0+ + */ + +#include "config.h" + +#include +#include + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +WiresharkPreference::WiresharkPreference(QObject * parent) : QObject(parent) +{} + +QWidget * WiresharkPreference::editor(QWidget * /*parent*/, const QStyleOptionViewItem &/*option*/, const QModelIndex &/*index*/) +{ + return Q_NULLPTR; +} + +void WiresharkPreference::setData(QWidget */*editor*/, const QModelIndex &/*index*/) {} +void WiresharkPreference::setModelData(QWidget */*editor*/, QAbstractItemModel */*model*/, const QModelIndex &/*index*/) {} + +void WiresharkPreference::setPrefsItem(PrefsItem * item) +{ + _prefsItem = item; +} + +PrefsItem * WiresharkPreference::prefsItem() const +{ + return _prefsItem; +} + +class BoolPreference : public WiresharkPreference +{ +public: + BoolPreference(QObject * parent = Q_NULLPTR) : WiresharkPreference(parent) {} + virtual QWidget * editor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) + { + ((QAbstractItemModel*)index.model())->setData(index, QString("BOOL"), Qt::EditRole); + return WiresharkPreference::editor(parent, option, index); + }; +}; +REGISTER_PREFERENCE_TYPE(PREF_BOOL, BoolPreference); + +class StringPreference : public WiresharkPreference +{ +public: + StringPreference(QObject * parent = Q_NULLPTR) : WiresharkPreference(parent) {} + virtual QWidget * editor(QWidget *parent, const QStyleOptionViewItem &/*option*/, const QModelIndex &/*index*/) + { + return new QLineEdit(parent); + }; + + virtual void setData(QWidget *editor, const QModelIndex &index) + { + QLineEdit* line = static_cast(editor); + line->setText(index.model()->data(index, Qt::DisplayRole).toString()); + }; + + virtual void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) + { + QLineEdit* line = static_cast(editor); + model->setData(index, line->text(), Qt::EditRole); + }; +}; +REGISTER_PREFERENCE_TYPE(PREF_STRING, StringPreference); + +class UIntPreference : public StringPreference +{ +public: + UIntPreference(QObject * parent = Q_NULLPTR) : StringPreference(parent) {} +}; +REGISTER_PREFERENCE_TYPE(PREF_UINT, UIntPreference); +REGISTER_PREFERENCE_TYPE(PREF_DECODE_AS_UINT, UIntPreference); + +class EnumPreference : public WiresharkPreference +{ +public: + EnumPreference(QObject * parent = Q_NULLPTR) : WiresharkPreference(parent) {} + virtual QWidget * editor(QWidget *parent, const QStyleOptionViewItem &/*option*/, const QModelIndex &/*index*/) + { + return new QComboBox(parent); + }; + + virtual void setData(QWidget *editor, const QModelIndex &index) + { + QComboBox* combo = static_cast(editor); + const enum_val_t *ev; + PrefsItem* pref = VariantPointer::asPtr(index.model()->data(index, Qt::UserRole)); + for (ev = prefs_get_enumvals(pref->getPref()); ev && ev->description; ev++) { + combo->addItem(ev->description, QVariant(ev->value)); + if (prefs_get_enum_value(pref->getPref(), pref_stashed) == ev->value) + combo->setCurrentIndex(combo->count() - 1); + } + }; + + virtual void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) + { + QComboBox* combo = static_cast(editor); + model->setData(index, combo->itemData(combo->currentIndex()), Qt::EditRole); + }; +}; +REGISTER_PREFERENCE_TYPE(PREF_ENUM, EnumPreference); + +class RangePreference : public WiresharkPreference +{ +public: + RangePreference(QObject * parent = Q_NULLPTR) : WiresharkPreference(parent) {} + virtual QWidget * editor(QWidget *parent, const QStyleOptionViewItem &/*option*/, const QModelIndex &/*index*/) + { + return new RangeSyntaxLineEdit(parent); + }; + + virtual void setData(QWidget *editor, const QModelIndex &index) + { + RangeSyntaxLineEdit* syntax = static_cast(editor); + syntax->setText(index.model()->data(index, Qt::DisplayRole).toString()); + }; + + virtual void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) + { + RangeSyntaxLineEdit* syntax = static_cast(editor); + model->setData(index, syntax->text(), Qt::EditRole); + }; +}; +REGISTER_PREFERENCE_TYPE(PREF_RANGE, RangePreference); +REGISTER_PREFERENCE_TYPE(PREF_DECODE_AS_RANGE, RangePreference); + +class ColorPreference : public WiresharkPreference +{ +public: + ColorPreference(QObject * parent = Q_NULLPTR) : WiresharkPreference(parent) {} + virtual QWidget * editor(QWidget * parent, const QStyleOptionViewItem &option, const QModelIndex &index) + { + QColorDialog color_dlg; + color_t color = *prefs_get_color_value(prefsItem()->getPref(), pref_stashed); + + color_dlg.setCurrentColor(QColor( + color.red >> 8, + color.green >> 8, + color.blue >> 8 + )); + if (color_dlg.exec() == QDialog::Accepted) + ((QAbstractItemModel*)index.model())->setData(index, color_dlg.currentColor().name(), Qt::EditRole); + + return WiresharkPreference::editor(parent, option, index); + }; +}; +REGISTER_PREFERENCE_TYPE(PREF_COLOR, ColorPreference); + +class SaveFilePreference : public WiresharkPreference +{ +public: + SaveFilePreference(QObject * parent = Q_NULLPTR) : WiresharkPreference(parent) {} + virtual QWidget * editor(QWidget * parent, const QStyleOptionViewItem &option, const QModelIndex &index) + { + QString filename = QFileDialog::getSaveFileName(parent, wsApp->windowTitleString(prefs_get_title(prefsItem()->getPref())), + index.model()->data(index, Qt::DisplayRole).toString()); + if (!filename.isEmpty()) { + ((QAbstractItemModel*)index.model())->setData(index, QDir::toNativeSeparators(filename), Qt::EditRole); + } + return WiresharkPreference::editor(parent, option, index); + }; +}; +REGISTER_PREFERENCE_TYPE(PREF_SAVE_FILENAME, SaveFilePreference); + +class OpenFilePreference : public WiresharkPreference +{ +public: + OpenFilePreference(QObject * parent = Q_NULLPTR) : WiresharkPreference(parent) {} + virtual QWidget * editor(QWidget * parent, const QStyleOptionViewItem &option, const QModelIndex &index) + { + QString filename = QFileDialog::getOpenFileName(parent, wsApp->windowTitleString(prefs_get_title(prefsItem()->getPref())), + index.model()->data(index, Qt::DisplayRole).toString()); + if (!filename.isEmpty()) { + ((QAbstractItemModel*)index.model())->setData(index, QDir::toNativeSeparators(filename), Qt::EditRole); + } + return WiresharkPreference::editor(parent, option, index); + }; +}; +REGISTER_PREFERENCE_TYPE(PREF_OPEN_FILENAME, OpenFilePreference); + +class DirNamePreference : public WiresharkPreference +{ +public: + DirNamePreference(QObject * parent = Q_NULLPTR) : WiresharkPreference(parent) {} + virtual QWidget * editor(QWidget * parent, const QStyleOptionViewItem &option, const QModelIndex &index) + { + QString filename = QFileDialog::getExistingDirectory(parent, wsApp->windowTitleString(prefs_get_title(prefsItem()->getPref())), + index.model()->data(index, Qt::DisplayRole).toString()); + if (!filename.isEmpty()) { + ((QAbstractItemModel*)index.model())->setData(index, QDir::toNativeSeparators(filename), Qt::EditRole); + } + return WiresharkPreference::editor(parent, option, index); + }; +}; +REGISTER_PREFERENCE_TYPE(PREF_DIRNAME, DirNamePreference); + +class UatPreference : public WiresharkPreference +{ +public: + UatPreference(QObject * parent = Q_NULLPTR) : WiresharkPreference(parent) {} + virtual QWidget * editor(QWidget * parent, const QStyleOptionViewItem &option, const QModelIndex &index) + { + if (prefsItem()->getPrefGUIType() == GUI_ALL || prefsItem()->getPrefGUIType() == GUI_QT) { + UatDialog uat_dlg(parent, prefs_get_uat_value(prefsItem()->getPref())); + uat_dlg.exec(); + } + return WiresharkPreference::editor(parent, option, index); + }; +}; +REGISTER_PREFERENCE_TYPE(PREF_UAT, UatPreference); + + +/* + * 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: + */ diff --git a/ui/qt/manager/wireshark_preference.h b/ui/qt/manager/wireshark_preference.h new file mode 100644 index 0000000000..d8f124190d --- /dev/null +++ b/ui/qt/manager/wireshark_preference.h @@ -0,0 +1,39 @@ +/* wireshark_preference.h + * + * Wireshark - Network traffic analyzer + * By Gerald Combs + * Copyright 1998 Gerald Combs + * + * SPDX-License-Identifier: GPL-2.0+ + */ + +#ifndef WIRESHARK_PREFERENCE_H +#define WIRESHARK_PREFERENCE_H + +#include + +#include +#include +#include + +class WiresharkPreference : public QObject +{ + Q_OBJECT +public: + explicit Q_INVOKABLE WiresharkPreference(QObject * parent = Q_NULLPTR); + + virtual QWidget * editor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index); + virtual void setData(QWidget *editor, const QModelIndex &index); + virtual void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index); + + void setPrefsItem(PrefsItem *); + +protected: + PrefsItem * prefsItem() const; + +private: + PrefsItem * _prefsItem; + +}; + +#endif // WIRESHARK_PREFERENCE_H diff --git a/ui/qt/models/pref_delegate.cpp b/ui/qt/models/pref_delegate.cpp index 8a4b83a68f..ab25b6ceaa 100644 --- a/ui/qt/models/pref_delegate.cpp +++ b/ui/qt/models/pref_delegate.cpp @@ -11,43 +11,8 @@ #include #include -#include -#include -#include -#include - -#include "uat_dialog.h" -#include "wireshark_application.h" - -#include - -RangeSyntaxLineEdit::RangeSyntaxLineEdit(QWidget *parent) - : SyntaxLineEdit(parent), - maxRange_(0xFFFFFFFF) -{ - connect(this, SIGNAL(textChanged(QString)), this, SLOT(checkRange(QString))); -} - -void RangeSyntaxLineEdit::checkRange(QString range) -{ - if (range.isEmpty()) { - setSyntaxState(SyntaxLineEdit::Empty); - return; - } - - range_t *newrange; - convert_ret_t ret = range_convert_str(NULL, &newrange, range.toUtf8().constData(), maxRange_); - - if (ret == CVT_NO_ERROR) { - setSyntaxState(SyntaxLineEdit::Valid); - wmem_free(NULL, newrange); - } else { - setSyntaxState(SyntaxLineEdit::Invalid); - } -} - - - +#include +#include AdvancedPrefDelegate::AdvancedPrefDelegate(QObject *parent) : QStyledItemDelegate(parent) { @@ -76,185 +41,42 @@ QWidget *AdvancedPrefDelegate::createEditor(QWidget *parent, const QStyleOptionV break; case AdvancedPrefsModel::colValue: pref = indexToPref(index); - switch(pref->getPrefType()) - { - case PREF_DECODE_AS_UINT: - case PREF_UINT: - { - QLineEdit* editor = new QLineEdit(parent); -#if 0 - //XXX - Do we want some help handling formatting the number? - editor->setInputMask("0000000009;"); -#endif - return editor; - } - case PREF_BOOL: - //Setting any non-NULL value will invert boolean value - ((QAbstractItemModel*)index.model())->setData(index, QString("BOOL"), Qt::EditRole); - break; - case PREF_ENUM: - { - QComboBox* editor = new QComboBox(parent); - return editor; - } - case PREF_STRING: - { - //Separated from UINT in case formatting needs to be applied to UINT - QLineEdit* editor = new QLineEdit(parent); - return editor; - } - case PREF_DECODE_AS_RANGE: - case PREF_RANGE: - { - RangeSyntaxLineEdit *editor = new RangeSyntaxLineEdit(parent); - return editor; - } - case PREF_UAT: - { - if (pref->getPrefGUIType() == GUI_ALL || pref->getPrefGUIType() == GUI_QT) { - UatDialog uat_dlg(parent, prefs_get_uat_value(pref->getPref())); - uat_dlg.exec(); - } - } - break; - case PREF_SAVE_FILENAME: - filename = QFileDialog::getSaveFileName(parent, wsApp->windowTitleString(prefs_get_title(pref->getPref())), - index.model()->data(index, Qt::DisplayRole).toString()); - if (!filename.isEmpty()) { - ((QAbstractItemModel*)index.model())->setData(index, QDir::toNativeSeparators(filename), Qt::EditRole); - } - break; - case PREF_OPEN_FILENAME: - filename = QFileDialog::getOpenFileName(parent, wsApp->windowTitleString(prefs_get_title(pref->getPref())), - index.model()->data(index, Qt::DisplayRole).toString()); - if (!filename.isEmpty()) { - ((QAbstractItemModel*)index.model())->setData(index, QDir::toNativeSeparators(filename), Qt::EditRole); - } - break; - case PREF_DIRNAME: - filename = QFileDialog::getExistingDirectory(parent, wsApp->windowTitleString(prefs_get_title(pref->getPref())), - index.model()->data(index, Qt::DisplayRole).toString()); - if (!filename.isEmpty()) { - ((QAbstractItemModel*)index.model())->setData(index, QDir::toNativeSeparators(filename), Qt::EditRole); - } - break; - case PREF_COLOR: - { - QColorDialog color_dlg; - color_t color = *prefs_get_color_value(pref->getPref(), pref_stashed); - - color_dlg.setCurrentColor(QColor( - color.red >> 8, - color.green >> 8, - color.blue >> 8 - )); - if (color_dlg.exec() == QDialog::Accepted) { - ((QAbstractItemModel*)index.model())->setData(index, color_dlg.currentColor().name(), Qt::EditRole); - } - break; - } - - } + WiresharkPreference * wspref = PreferenceManager::instance()->getPreference(pref); + if ( wspref ) + return wspref->editor(parent, option, index); break; } - return 0; + return Q_NULLPTR; } void AdvancedPrefDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const { PrefsItem* pref = indexToPref(index); - switch(pref->getPrefType()) + WiresharkPreference * wspref = PreferenceManager::instance()->getPreference(pref); + if ( wspref ) { - case PREF_DECODE_AS_UINT: - case PREF_UINT: - { - QLineEdit* line = static_cast(editor); - line->setText(index.model()->data(index, Qt::DisplayRole).toString()); - } - break; - case PREF_ENUM: - { - QComboBox* combo = static_cast(editor); - const enum_val_t *ev; - PrefsItem* pref = VariantPointer::asPtr(index.model()->data(index, Qt::UserRole)); - for (ev = prefs_get_enumvals(pref->getPref()); ev && ev->description; ev++) { - combo->addItem(ev->description, QVariant(ev->value)); - if (prefs_get_enum_value(pref->getPref(), pref_stashed) == ev->value) - combo->setCurrentIndex(combo->count() - 1); - } - } - break; - case PREF_STRING: - { - QLineEdit* line = static_cast(editor); - line->setText(index.model()->data(index, Qt::DisplayRole).toString()); - } - break; - case PREF_DECODE_AS_RANGE: - case PREF_RANGE: - { - RangeSyntaxLineEdit* syntax = static_cast(editor); - syntax->setText(index.model()->data(index, Qt::DisplayRole).toString()); - } - break; - case PREF_UAT: - case PREF_SAVE_FILENAME: - case PREF_OPEN_FILENAME: - case PREF_DIRNAME: - case PREF_COLOR: - //Handled by the dialogs created - break; - default: - //Ensure any new preference types are handled - Q_ASSERT(FALSE); - break; + wspref->setData(editor, index); + return; } + + Q_ASSERT(FALSE); } void AdvancedPrefDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const { PrefsItem* pref = indexToPref(index); - switch(pref->getPrefType()) + + WiresharkPreference * wspref = PreferenceManager::instance()->getPreference(pref); + if ( wspref ) { - case PREF_DECODE_AS_UINT: - case PREF_UINT: - case PREF_STRING: - { - QLineEdit* line = static_cast(editor); - model->setData(index, line->text(), Qt::EditRole); - break; - } - case PREF_ENUM: - { - QComboBox* combo = static_cast(editor); - model->setData(index, combo->itemData(combo->currentIndex()), Qt::EditRole); - } - break; - case PREF_DECODE_AS_RANGE: - case PREF_RANGE: - { - RangeSyntaxLineEdit* syntax = static_cast(editor); - model->setData(index, syntax->text(), Qt::EditRole); - break; - } - case PREF_UAT: - //do nothing because UAT values aren't shown in table - break; - case PREF_SAVE_FILENAME: - case PREF_OPEN_FILENAME: - case PREF_DIRNAME: - case PREF_COLOR: - //do nothing, dialog signals will update table - pref = NULL; - break; - default: - //Ensure any new preference types are handled - Q_ASSERT(FALSE); - break; + wspref->setModelData(editor, model, index); + return; } + + Q_ASSERT(FALSE); } /* * Editor modelines diff --git a/ui/qt/models/pref_delegate.h b/ui/qt/models/pref_delegate.h index 1f693e6ade..23e8744bb0 100644 --- a/ui/qt/models/pref_delegate.h +++ b/ui/qt/models/pref_delegate.h @@ -14,7 +14,6 @@ #include #include -#include #include #include @@ -36,19 +35,17 @@ private: PrefsItem* indexToPref(const QModelIndex &index) const; }; -//Utility class for range preferences -class RangeSyntaxLineEdit : public SyntaxLineEdit -{ - Q_OBJECT -public: - explicit RangeSyntaxLineEdit(QWidget *parent = 0); - void setMaxRange(unsigned int max) {maxRange_ = max;} - -public slots: - void checkRange(QString range); - -private: - unsigned int maxRange_; -}; - #endif // PREF_DELEGATE_H + +/* + * 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: + */ diff --git a/ui/qt/widgets/range_syntax_lineedit.cpp b/ui/qt/widgets/range_syntax_lineedit.cpp new file mode 100644 index 0000000000..1175f592d1 --- /dev/null +++ b/ui/qt/widgets/range_syntax_lineedit.cpp @@ -0,0 +1,57 @@ +/* range_syntax_lineedit.cpp + * Delegates for editing prefereneces. + * + * Wireshark - Network traffic analyzer + * By Gerald Combs + * Copyright 1998 Gerald Combs + * + * SPDX-License-Identifier: GPL-2.0+ + */ + +#include + +#include + +RangeSyntaxLineEdit::RangeSyntaxLineEdit(QWidget *parent) + : SyntaxLineEdit(parent), + maxRange_(0xFFFFFFFF) +{ + connect(this, SIGNAL(textChanged(QString)), this, SLOT(checkRange(QString))); +} + +void RangeSyntaxLineEdit::setMaxRange(unsigned int max) +{ + maxRange_ = max; +} + +void RangeSyntaxLineEdit::checkRange(QString range) +{ + if (range.isEmpty()) { + setSyntaxState(SyntaxLineEdit::Empty); + return; + } + + range_t *newrange; + convert_ret_t ret = range_convert_str(NULL, &newrange, range.toUtf8().constData(), maxRange_); + + if (ret == CVT_NO_ERROR) { + setSyntaxState(SyntaxLineEdit::Valid); + wmem_free(NULL, newrange); + } else { + setSyntaxState(SyntaxLineEdit::Invalid); + } +} + + +/* + * 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: + */ diff --git a/ui/qt/widgets/range_syntax_lineedit.h b/ui/qt/widgets/range_syntax_lineedit.h new file mode 100644 index 0000000000..5b069d55d8 --- /dev/null +++ b/ui/qt/widgets/range_syntax_lineedit.h @@ -0,0 +1,45 @@ +/* range_syntax_lineedit.h + * Delegates for editing prefereneces. + * + * Wireshark - Network traffic analyzer + * By Gerald Combs + * Copyright 1998 Gerald Combs + * + * SPDX-License-Identifier: GPL-2.0+ + */ + +#ifndef RANGE_SYNTAX_LINEEDIT_H +#define RANGE_SYNTAX_LINEEDIT_H + +#include + +#include + +class RangeSyntaxLineEdit : public SyntaxLineEdit +{ + Q_OBJECT +public: + explicit RangeSyntaxLineEdit(QWidget *parent = 0); + void setMaxRange(unsigned int max); + +public slots: + void checkRange(QString range); + +private: + unsigned int maxRange_; +}; + +#endif // RANGE_SYNTAX_LINEEDIT_H + +/* + * 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: + */