From ff7a6305a7eaa3c3815e813acc78a1660ae82293 Mon Sep 17 00:00:00 2001 From: Michael Mann Date: Sun, 30 Jul 2017 23:30:59 -0400 Subject: [PATCH] Use proper tab order for "file types" in UAT "table". Directory and File "UAT types" have been using a separately launched dialog, instead of an "editor", so it breaks "tab rules". Create and treat (File) dialog as an editor. Change-Id: I983728abefb0cdd79899468a800328f1b56e2910 Reviewed-on: https://code.wireshark.org/review/22886 Reviewed-by: Michael Mann Petri-Dish: Michael Mann Tested-by: Petri Dish Buildbot Reviewed-by: Anders Broman --- ui/qt/CMakeLists.txt | 2 + ui/qt/Makefile.am | 2 + ui/qt/models/uat_delegate.cpp | 109 ++++++++++++++++----------- ui/qt/models/uat_delegate.h | 9 ++- ui/qt/widgets/editor_file_dialog.cpp | 55 ++++++++++++++ ui/qt/widgets/editor_file_dialog.h | 59 +++++++++++++++ 6 files changed, 190 insertions(+), 46 deletions(-) create mode 100644 ui/qt/widgets/editor_file_dialog.cpp create mode 100644 ui/qt/widgets/editor_file_dialog.h diff --git a/ui/qt/CMakeLists.txt b/ui/qt/CMakeLists.txt index 197621fce4..1c63d693f1 100644 --- a/ui/qt/CMakeLists.txt +++ b/ui/qt/CMakeLists.txt @@ -30,6 +30,7 @@ set(WIRESHARK_WIDGET_HEADERS widgets/display_filter_combo.h widgets/display_filter_edit.h widgets/drag_drop_toolbar.h + widgets/editor_file_dialog.h widgets/elided_label.h widgets/field_filter_edit.h widgets/find_line_edit.h @@ -227,6 +228,7 @@ set(WIRESHARK_WIDGET_SRCS widgets/display_filter_combo.cpp widgets/display_filter_edit.cpp widgets/drag_drop_toolbar.cpp + widgets/editor_file_dialog.cpp widgets/elided_label.cpp widgets/field_filter_edit.cpp widgets/find_line_edit.cpp diff --git a/ui/qt/Makefile.am b/ui/qt/Makefile.am index a5d0add4be..29b56aec0f 100644 --- a/ui/qt/Makefile.am +++ b/ui/qt/Makefile.am @@ -159,6 +159,7 @@ MOC_WIDGET_HDRS = \ widgets/clickable_label.h \ widgets/display_filter_combo.h \ widgets/display_filter_edit.h \ + widgets/editor_file_dialog.h \ widgets/elided_label.h \ widgets/field_filter_edit.h \ widgets/find_line_edit.h \ @@ -471,6 +472,7 @@ WIRESHARK_QT_WIDGET_SRC = \ widgets/clickable_label.cpp \ widgets/display_filter_combo.cpp \ widgets/display_filter_edit.cpp \ + widgets/editor_file_dialog.cpp \ widgets/elided_label.cpp \ widgets/field_filter_edit.cpp \ widgets/find_line_edit.cpp \ diff --git a/ui/qt/models/uat_delegate.cpp b/ui/qt/models/uat_delegate.cpp index ee149a2fc8..9d77337aef 100644 --- a/ui/qt/models/uat_delegate.cpp +++ b/ui/qt/models/uat_delegate.cpp @@ -31,6 +31,7 @@ #include #include +#include UatDelegate::UatDelegate(QObject *parent) : QStyledItemDelegate(parent) { @@ -49,11 +50,39 @@ QWidget *UatDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem & switch (field->mode) { case PT_TXTMOD_DIRECTORYNAME: - case PT_TXTMOD_FILENAME: - // TODO tab navigation from this field is broken. - // Do not create editor, a dialog will be opened in editorEvent - return 0; + { + if (index.isValid()) { + QString filename_old = index.model()->data(index, Qt::EditRole).toString(); + EditorFileDialog* fileDialog = new EditorFileDialog(index, parent, QString(field->title), filename_old); + fileDialog->setFileMode(QFileDialog::DirectoryOnly); + + //Use signals to accept data from cell + connect(fileDialog, SIGNAL(acceptEdit(const QModelIndex &)), this, SLOT(applyDirectory(const QModelIndex&))); + return fileDialog; + } + + //shouldn't happen + return 0; + } + + case PT_TXTMOD_FILENAME: + { + if (index.isValid()) { + QString filename_old = index.model()->data(index, Qt::EditRole).toString(); + EditorFileDialog* fileDialog = new EditorFileDialog(index, parent, QString(field->title), filename_old); + + fileDialog->setFileMode(QFileDialog::ExistingFile); + fileDialog->setOption(QFileDialog::DontConfirmOverwrite); + + //Use signals to accept data from cell + connect(fileDialog, SIGNAL(acceptEdit(const QModelIndex &)), this, SLOT(applyFilename(const QModelIndex &))); + return fileDialog; + } + + //shouldn't happen + return 0; + } case PT_TXTMOD_ENUM: { // Note: the string repr. is written, not the integer value. @@ -143,64 +172,60 @@ void UatDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, model->setData(index, data, Qt::EditRole); break; } - + case PT_TXTMOD_DIRECTORYNAME: + case PT_TXTMOD_FILENAME: + //do nothing, dialog signals will update table + break; default: QStyledItemDelegate::setModelData(editor, model, index); } } -#if 0 -// Qt docs suggest overriding updateEditorGeometry, but the defaults seem sane. void UatDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const -{ - QStyledItemDelegate::updateEditorGeometry(editor, option, index); -} -#endif - -bool UatDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) { uat_field_t *field = indexToField(index); switch (field->mode) { case PT_TXTMOD_DIRECTORYNAME: + { + QRect rect = option.rect; + rect.setBottom(rect.width()); + editor->setGeometry(rect); + break; + } case PT_TXTMOD_FILENAME: - if (event && (event->type() == QEvent::MouseButtonRelease || - event->type() == QEvent::MouseButtonDblClick)) { - // Ignore these mouse events, only handle MouseButtonPress. - return false; - } - if (index.isValid()) { - QString filename_old = model->data(index, Qt::EditRole).toString(); - QString filename = openFileDialog(field, filename_old); - // TODO should this overwrite only when !filename.isEmpty()? - model->setData(index, filename, Qt::EditRole); - } - // returns false to ensure that QAbstractItemView::edit does not assume - // the editing state. This causes the view's currentIndex to be changed - // to the cell where this delegate was "created", as desired. - return false; - + { + QRect rect = option.rect; + rect.setWidth(600); + rect.setHeight(600); + editor->setGeometry(rect); + break; + } default: - return QStyledItemDelegate::editorEvent(event, model, option, index); + //the defaults for other editors seem sane. + QStyledItemDelegate::updateEditorGeometry(editor, option, index); } } -QString UatDelegate::openFileDialog(uat_field_t *field, const QString &cur_path) const +void UatDelegate::applyFilename(const QModelIndex& index) { - // Note: file dialogs have their parent widget set to NULL because we do not - // have an editor nor the view that would attach us. - switch (field->mode) { - case PT_TXTMOD_DIRECTORYNAME: - return QFileDialog::getExistingDirectory(NULL, field->title, cur_path); + if (index.isValid()) { + EditorFileDialog* fileDialog = static_cast(sender()); - case PT_TXTMOD_FILENAME: - return QFileDialog::getOpenFileName(NULL, field->title, cur_path, - QString(), NULL, QFileDialog::DontConfirmOverwrite); + QStringList files = fileDialog->selectedFiles(); + if (files.size() > 0) { + ((QAbstractItemModel *)index.model())->setData(index, files[0], Qt::EditRole); + } + } +} - default: - g_assert_not_reached(); - return 0; +void UatDelegate::applyDirectory(const QModelIndex& index) +{ + if (index.isValid()) { + EditorFileDialog* fileDialog = static_cast(sender()); + const QString &data = fileDialog->directory().absolutePath(); + ((QAbstractItemModel *)index.model())->setData(index, data, Qt::EditRole); } } diff --git a/ui/qt/models/uat_delegate.h b/ui/qt/models/uat_delegate.h index 795161a686..81ad108864 100644 --- a/ui/qt/models/uat_delegate.h +++ b/ui/qt/models/uat_delegate.h @@ -28,7 +28,9 @@ #include #include #include + #include +#include class UatDelegate : public QStyledItemDelegate { @@ -43,15 +45,14 @@ public: void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const; -#if 0 void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const; -#endif - bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index); +private slots: + void applyDirectory(const QModelIndex& index); + void applyFilename(const QModelIndex& index); private: uat_field_t *indexToField(const QModelIndex &index) const; - QString openFileDialog(uat_field_t *field, const QString &cur_path) const; }; #endif // UAT_DELEGATE_H diff --git a/ui/qt/widgets/editor_file_dialog.cpp b/ui/qt/widgets/editor_file_dialog.cpp new file mode 100644 index 0000000000..9f699ecf2d --- /dev/null +++ b/ui/qt/widgets/editor_file_dialog.cpp @@ -0,0 +1,55 @@ +/* editor_file_dialog.h + * + * File dialog that can be used as an "inline editor" in a table + * + * Wireshark - Network traffic analyzer + * By Gerald Combs + * 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 + +EditorFileDialog::EditorFileDialog(const QModelIndex& index, QWidget* parent, Qt::WindowFlags flags) + : QFileDialog(parent, flags) + , index_(index) +{ +} + +EditorFileDialog::EditorFileDialog(const QModelIndex& index, QWidget* parent, const QString& caption, const QString& directory, const QString& filter) + : QFileDialog(parent, caption, directory, filter) + , index_(index) +{ +} + +void EditorFileDialog::accept() +{ + emit acceptEdit(index_); + QFileDialog::accept(); +} + +/* + * 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/editor_file_dialog.h b/ui/qt/widgets/editor_file_dialog.h new file mode 100644 index 0000000000..9922fff201 --- /dev/null +++ b/ui/qt/widgets/editor_file_dialog.h @@ -0,0 +1,59 @@ +/* editor_file_dialog.h + * + * File dialog that can be used as an "inline editor" in a table + * + * Wireshark - Network traffic analyzer + * By Gerald Combs + * 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. + */ + +#ifndef EDITOR_FILE_DIALOG_H_ +#define EDITOR_FILE_DIALOG_H_ + +#include +#include + +class EditorFileDialog : public QFileDialog +{ + Q_OBJECT +public: + explicit EditorFileDialog(const QModelIndex& index, QWidget* parent, Qt::WindowFlags flags); + explicit EditorFileDialog(const QModelIndex& index, QWidget* parent = 0, const QString & caption = QString(), const QString & directory = QString(), const QString & filter = QString()); + + void accept(); + +signals: + void acceptEdit(const QModelIndex& index); + +protected: + const QModelIndex index_; //saved index of table cell +}; + +#endif /* EDITOR_FILE_DIALOG_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: + */