diff options
author | Gerald Combs <gerald@wireshark.org> | 2015-06-17 08:41:41 -0700 |
---|---|---|
committer | Gerald Combs <gerald@wireshark.org> | 2015-06-18 00:46:50 +0000 |
commit | 9c28594529b79619643948128dc2bbbaa3dcbb0e (patch) | |
tree | 2bcff16babc1acceea9b0d83f7068c4b06f41443 /ui/qt/preference_editor_frame.cpp | |
parent | 2dd030d4f65286aad17f54c12b41471ec153ed4f (diff) |
Add a preferences editor frame.
This replaces the single preference editor dialog in the GTK+ UI.
Change-Id: I10e030981e9f7d1ec121811593586b65cf0797c5
Reviewed-on: https://code.wireshark.org/review/8966
Petri-Dish: Gerald Combs <gerald@wireshark.org>
Tested-by: Petri Dish Buildbot <buildbot-no-reply@wireshark.org>
Reviewed-by: Gerald Combs <gerald@wireshark.org>
Diffstat (limited to 'ui/qt/preference_editor_frame.cpp')
-rw-r--r-- | ui/qt/preference_editor_frame.cpp | 241 |
1 files changed, 241 insertions, 0 deletions
diff --git a/ui/qt/preference_editor_frame.cpp b/ui/qt/preference_editor_frame.cpp new file mode 100644 index 0000000000..794c9b5859 --- /dev/null +++ b/ui/qt/preference_editor_frame.cpp @@ -0,0 +1,241 @@ +/* preference_editor_frame.h + * + * Wireshark - Network traffic analyzer + * By Gerald Combs <gerald@wireshark.org> + * Copyright 1998 Gerald Combs + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" + +#include <glib.h> + +#include <epan/prefs.h> +#include <epan/prefs-int.h> + +#include <ui/preference_utils.h> + +#include "preference_editor_frame.h" +#include "ui_preference_editor_frame.h" + +#include "qt_ui_utils.h" + +#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0) +// Qt::escape +#include <QTextDocument> +#endif + +// To do: +// - Handle PREF_FILENAME and PREF_DIRNAME. + +PreferenceEditorFrame::PreferenceEditorFrame(QWidget *parent) : + AccordionFrame(parent), + ui(new Ui::PreferenceEditorFrame), + module_(NULL), + pref_(NULL) +{ + ui->setupUi(this); +} + +PreferenceEditorFrame::~PreferenceEditorFrame() +{ + delete ui; +} + +void PreferenceEditorFrame::editPreference(preference *pref, pref_module *module) +{ + pref_ = pref; + module_ = module; + + if (!pref || !module) { + hide(); + return; + } + + ui->modulePreferencesToolButton->setText(tr("Open %1 preferences").arg(module_->title)); + + pref_stash(pref_, NULL); + ui->preferenceTitleLabel->setText(pref->title); + + // Convert the pref description from plain text to rich text. + QString description; +#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0) + description = Qt::escape(pref->description); +#else + description = QString(pref->description).toHtmlEscaped(); +#endif + description.replace('\n', "<br>"); + QString tooltip = QString("<span>%1</span>").arg(description); + ui->preferenceTitleLabel->setToolTip(tooltip); + ui->preferenceLineEdit->setToolTip(tooltip); + + ui->preferenceLineEdit->clear(); + ui->preferenceLineEdit->setSyntaxState(SyntaxLineEdit::Empty); + disconnect(ui->preferenceLineEdit); + + bool show = false; + + switch (pref_->type) { + case PREF_UINT: + new_uint_ = pref->stashed_val.uint; + connect(ui->preferenceLineEdit, SIGNAL(textEdited(QString)), + this, SLOT(uintLineEditTextEdited(QString))); + show = true; + break; + case PREF_STRING: + new_str_ = pref->stashed_val.string; + connect(ui->preferenceLineEdit, SIGNAL(textEdited(QString)), + this, SLOT(stringLineEditTextEdited(QString))); + show = true; + break; + case PREF_RANGE: + g_free(new_range_); + new_range_ = range_copy(pref->stashed_val.range); + connect(ui->preferenceLineEdit, SIGNAL(textEdited(QString)), + this, SLOT(rangeLineEditTextEdited(QString))); + show = true; + break; + default: + break; + } + + if (show) { + ui->preferenceLineEdit->setText(gchar_free_to_qstring(prefs_pref_to_str(pref_, pref_stashed)).remove(QRegExp("\n\t"))); + animatedShow(); + } +} + +void PreferenceEditorFrame::uintLineEditTextEdited(const QString &new_str) +{ + if (new_str.isEmpty()) { + new_uint_ = pref_->stashed_val.uint; + ui->preferenceLineEdit->setSyntaxState(SyntaxLineEdit::Empty); + ui->okButton->setEnabled(true); + return; + } + + bool ok; + uint new_uint = new_str.toUInt(&ok); + if (ok) { + new_uint_ = new_uint; + ui->preferenceLineEdit->setSyntaxState(SyntaxLineEdit::Valid); + } else { + new_uint_ = pref_->stashed_val.uint; + ui->preferenceLineEdit->setSyntaxState(SyntaxLineEdit::Invalid); + } + ui->okButton->setEnabled(ok); +} + +void PreferenceEditorFrame::stringLineEditTextEdited(const QString &new_str) +{ + new_str_ = new_str; +} + +void PreferenceEditorFrame::rangeLineEditTextEdited(const QString &new_str) +{ + range_t *new_range = NULL; + + convert_ret_t ret = range_convert_str(&new_range, new_str.toUtf8().constData(), pref_->info.max_value); + g_free(new_range_); + new_range_ = new_range; + + if (ret == CVT_NO_ERROR) { + if (new_str.isEmpty()) { + ui->preferenceLineEdit->setSyntaxState(SyntaxLineEdit::Empty); + } else { + ui->preferenceLineEdit->setSyntaxState(SyntaxLineEdit::Valid); + } + } else { + ui->preferenceLineEdit->setSyntaxState(SyntaxLineEdit::Invalid); + } +} + +void PreferenceEditorFrame::on_modulePreferencesToolButton_clicked() +{ + on_cancelButton_clicked(); + if (module_) { + QString module_name = module_->name; + emit showProtocolPreferences(module_name); + } +} + +void PreferenceEditorFrame::on_preferenceLineEdit_returnPressed() +{ + if (ui->okButton->isEnabled()) { + on_okButton_clicked(); + } +} + +void PreferenceEditorFrame::on_okButton_clicked() +{ + bool apply = false; + switch(pref_->type) { + case PREF_UINT: + if (pref_->stashed_val.uint != new_uint_) { + pref_->stashed_val.uint = new_uint_; + apply = true; + } + break; + case PREF_STRING: + if (new_str_.compare(pref_->stashed_val.string) != 0) { + g_free(pref_->stashed_val.string); + pref_->stashed_val.string = qstring_strdup(new_str_); + apply = true; + } + break; + case PREF_RANGE: + if (!ranges_are_equal(pref_->stashed_val.range, new_range_)) { + g_free(pref_->stashed_val.range); + pref_->stashed_val.range = range_copy(new_range_); + apply = true; + } + break; + default: + break; + } + + if (apply && module_) { + pref_unstash(pref_, &module_->prefs_changed); + prefs_apply(module_); + if (!prefs.gui_use_pref_save) { + prefs_main_write(); + } + } + on_cancelButton_clicked(); +} + +void PreferenceEditorFrame::on_cancelButton_clicked() +{ + pref_ = NULL; + module_ = NULL; + g_free(new_range_); + new_range_ = NULL; + ui->preferenceLineEdit->clear(); + animatedHide(); +} + +/* + * 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: + */ |