Qt: refactor UatTreeView into something reusable

UatTreeView had two functions:
 1. Saner navigation functionality when pressing tab.
 2. Start editing when the currently selected item changes.

Since this tab navigation functionality is desired in more places,
extract this functionality. Add more documentation while at it and use
an alternative, declarative style to connect signals.

Move the second functionality to the caller since not all views need it.

Change-Id: Ibe886f2c2763dbe024614203a44b72173fbbce06
Reviewed-on: https://code.wireshark.org/review/22639
Petri-Dish: Michael Mann <mmann78@netscape.net>
Reviewed-by: Roland Knall <rknall@gmail.com>
Tested-by: Petri Dish Buildbot <buildbot-no-reply@wireshark.org>
Reviewed-by: Michael Mann <mmann78@netscape.net>
This commit is contained in:
Peter Wu 2017-07-16 12:32:40 +02:00 committed by Michael Mann
parent 96ac279d98
commit 4beb7430d4
11 changed files with 62 additions and 34 deletions

View File

@ -37,6 +37,7 @@ set(WIRESHARK_WIDGET_HEADERS
widgets/label_stack.h
widgets/overlay_scroll_bar.h
widgets/syntax_line_edit.h
widgets/tabnav_tree_view.h
widgets/drag_drop_toolbar.h
)
@ -171,7 +172,6 @@ set(WIRESHARK_QT_HEADERS
uat_dialog.h
uat_frame.h
uat_model.h
uat_tree_view.h
voip_calls_dialog.h
voip_calls_info_model.h
wireless_frame.h
@ -223,6 +223,7 @@ set(WIRESHARK_WIDGET_SRCS
widgets/label_stack.cpp
widgets/overlay_scroll_bar.cpp
widgets/syntax_line_edit.cpp
widgets/tabnav_tree_view.cpp
widgets/drag_drop_toolbar.cpp
)
@ -351,7 +352,6 @@ set(WIRESHARK_QT_SRC
uat_dialog.cpp
uat_frame.cpp
uat_model.cpp
uat_tree_view.cpp
voip_calls_dialog.cpp
voip_calls_info_model.cpp
wireless_frame.cpp

View File

@ -166,6 +166,7 @@ MOC_WIDGET_HDRS = \
widgets/label_stack.h \
widgets/overlay_scroll_bar.h \
widgets/syntax_line_edit.h \
widgets/tabnav_tree_view.h \
widgets/drag_drop_toolbar.h
#
@ -302,7 +303,6 @@ MOC_HDRS = \
uat_dialog.h \
uat_frame.h \
uat_model.h \
uat_tree_view.h \
voip_calls_dialog.h \
voip_calls_info_model.h \
wireless_frame.h \
@ -466,6 +466,7 @@ WIRESHARK_QT_WIDGET_SRC = \
widgets/label_stack.cpp \
widgets/overlay_scroll_bar.cpp \
widgets/syntax_line_edit.cpp \
widgets/tabnav_tree_view.cpp \
widgets/drag_drop_toolbar.cpp
@ -596,7 +597,6 @@ WIRESHARK_QT_SRC = \
uat_dialog.cpp \
uat_frame.cpp \
uat_model.cpp \
uat_tree_view.cpp \
voip_calls_dialog.cpp \
voip_calls_info_model.cpp \
wireless_frame.cpp \

View File

@ -24,7 +24,7 @@
#include "tabnav_tree_widget.h"
// Copy on UatTreeView, modified to use QTreeWidget instead of QTreeView.
// Copy of TabnavTreeView, modified to use QTreeWidget instead of QTreeView.
TabnavTreeWidget::TabnavTreeWidget(QWidget *parent) : QTreeWidget(parent)
{

View File

@ -73,6 +73,10 @@ UatDialog::UatDialog(QWidget *parent, epan_uat *uat) :
ui->uatTreeView->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
#endif
// start editing as soon as the field is selected or when typing starts
ui->uatTreeView->setEditTriggers(ui->uatTreeView->editTriggers() |
QAbstractItemView::CurrentChanged | QAbstractItemView::AnyKeyPressed);
// Need to add uat_move or uat_insert to the UAT API.
ui->uatTreeView->setDragEnabled(false);
qDebug() << "FIX Add drag reordering to UAT dialog";
@ -115,8 +119,6 @@ void UatDialog::setUat(epan_uat *uat)
this, SLOT(modelDataChanged(QModelIndex)));
connect(uat_model_, SIGNAL(rowsRemoved(QModelIndex, int, int)),
this, SLOT(modelRowsRemoved()));
connect(ui->uatTreeView, SIGNAL(currentItemChanged(QModelIndex,QModelIndex)),
this, SLOT(viewCurrentChanged(QModelIndex,QModelIndex)));
ok_button_->setEnabled(!uat_model_->hasErrors());
if (uat_->help && strlen(uat_->help) > 0) {
@ -147,7 +149,7 @@ void UatDialog::modelRowsRemoved()
// Invoked when a different field is selected. Note: when selecting a different
// field after editing, this event is triggered after modelDataChanged.
void UatDialog::viewCurrentChanged(const QModelIndex &current, const QModelIndex &previous)
void UatDialog::on_uatTreeView_currentItemChanged(const QModelIndex &current, const QModelIndex &previous)
{
if (current.isValid()) {
ui->deleteToolButton->setEnabled(true);

View File

@ -52,7 +52,7 @@ public:
private slots:
void modelDataChanged(const QModelIndex &topLeft);
void modelRowsRemoved();
void viewCurrentChanged(const QModelIndex &current, const QModelIndex &previous);
void on_uatTreeView_currentItemChanged(const QModelIndex &current, const QModelIndex &previous);
void acceptChanges();
void rejectChanges();
void on_newToolButton_clicked();

View File

@ -12,7 +12,7 @@
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="UatTreeView" name="uatTreeView">
<widget class="TabnavTreeView" name="uatTreeView">
<column>
<property name="text">
<string notr="true">1</string>
@ -130,9 +130,9 @@
<header>widgets/elided_label.h</header>
</customwidget>
<customwidget>
<class>UatTreeView</class>
<class>TabnavTreeView</class>
<extends>QTreeView</extends>
<header>uat_tree_view.h</header>
<header>widgets/tabnav_tree_view.h</header>
</customwidget>
</customwidgets>
<resources>

View File

@ -66,6 +66,10 @@ UatFrame::UatFrame(QWidget *parent) :
ui->uatTreeView->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
#endif
// start editing as soon as the field is selected or when typing starts
ui->uatTreeView->setEditTriggers(ui->uatTreeView->editTriggers() |
QAbstractItemView::CurrentChanged | QAbstractItemView::AnyKeyPressed);
// XXX - Need to add uat_move or uat_insert to the UAT API for drag/drop
}
@ -105,8 +109,6 @@ void UatFrame::setUat(epan_uat *uat)
this, SLOT(modelDataChanged(QModelIndex)));
connect(uat_model_, SIGNAL(rowsRemoved(QModelIndex, int, int)),
this, SLOT(modelRowsRemoved()));
connect(ui->uatTreeView, SIGNAL(currentItemChanged(QModelIndex,QModelIndex)),
this, SLOT(viewCurrentChanged(QModelIndex,QModelIndex)));
}
setWindowTitle(title);
@ -175,7 +177,7 @@ void UatFrame::addRecord(bool copy_from_current)
// Invoked when a different field is selected. Note: when selecting a different
// field after editing, this event is triggered after modelDataChanged.
void UatFrame::viewCurrentChanged(const QModelIndex &current, const QModelIndex &previous)
void UatFrame::on_uatTreeView_currentItemChanged(const QModelIndex &current, const QModelIndex &previous)
{
if (current.isValid()) {
ui->deleteToolButton->setEnabled(true);

View File

@ -59,7 +59,7 @@ private:
private slots:
void modelDataChanged(const QModelIndex &topLeft);
void modelRowsRemoved();
void viewCurrentChanged(const QModelIndex &current, const QModelIndex &previous);
void on_uatTreeView_currentItemChanged(const QModelIndex &current, const QModelIndex &previous);
void on_newToolButton_clicked();
void on_deleteToolButton_clicked();
void on_copyToolButton_clicked();

View File

@ -18,7 +18,7 @@
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="UatTreeView" name="uatTreeView">
<widget class="TabnavTreeView" name="uatTreeView">
<column>
<property name="text">
<string notr="true">1</string>
@ -138,9 +138,9 @@
<header>widgets/elided_label.h</header>
</customwidget>
<customwidget>
<class>UatTreeView</class>
<class>TabnavTreeView</class>
<extends>QTreeView</extends>
<header>uat_tree_view.h</header>
<header>widgets/tabnav_tree_view.h</header>
</customwidget>
</customwidgets>
<resources>

View File

@ -1,5 +1,5 @@
/* uat_tree_view.cpp
* Tree view of UAT data.
/* tabnav_tree_view.cpp
* Tree view with saner tab navigation functionality.
*
* Copyright 2016 Peter Wu <peter@lekensteyn.nl>
*
@ -22,18 +22,16 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "uat_tree_view.h"
#include "tabnav_tree_view.h"
UatTreeView::UatTreeView(QWidget *parent) : QTreeView(parent)
TabnavTreeView::TabnavTreeView(QWidget *parent) : QTreeView(parent)
{
// start editing as soon as the field is selected or when typing starts
setEditTriggers(editTriggers() | CurrentChanged | AnyKeyPressed);
}
// Note: if a QTableView is used, then this is not needed anymore since Tab
// works as "expected" (move to next cell instead of row).
// Note 2: this does not help with fields with no widget (like filename).
QModelIndex UatTreeView::moveCursor(CursorAction cursorAction, Qt::KeyboardModifiers modifiers)
QModelIndex TabnavTreeView::moveCursor(CursorAction cursorAction, Qt::KeyboardModifiers modifiers)
{
QModelIndex current = currentIndex();
// If an item is currently selected, interpret Next/Previous. Otherwise,
@ -55,7 +53,16 @@ QModelIndex UatTreeView::moveCursor(CursorAction cursorAction, Qt::KeyboardModif
return QTreeView::moveCursor(cursorAction, modifiers);
}
void UatTreeView::currentChanged(const QModelIndex &current, const QModelIndex &previous)
/*!
\fn void TabnavTreeView::currentItemChanged(QModelIndex *current, QModelIndex *previous)
This signal is emitted whenever the current item changes.
\a previous is the item that previously had the focus; \a current is the
new current item.
*/
void TabnavTreeView::currentChanged(const QModelIndex &current, const QModelIndex &previous)
{
QTreeView::currentChanged(current, previous);
emit currentItemChanged(current, previous);

View File

@ -1,5 +1,5 @@
/* uat_tree_view.h
* Tree view of UAT data.
/* tabnav_tree_view.h
* Tree view with saner tab navigation functionality.
*
* Copyright 2016 Peter Wu <peter@lekensteyn.nl>
*
@ -22,17 +22,22 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef UAT_TREE_VIEW_H
#define UAT_TREE_VIEW_H
#ifndef TABNAV_TREE_VIEW_H
#define TABNAV_TREE_VIEW_H
#include <config.h>
#include <QTreeView>
class UatTreeView : public QTreeView
/**
* Like QTreeView, but instead of changing to the next row (same column) when
* pressing Tab while editing, change to the next column (same row).
*/
class TabnavTreeView : public QTreeView
{
Q_OBJECT
public:
UatTreeView(QWidget *parent = 0);
TabnavTreeView(QWidget *parent = 0);
QModelIndex moveCursor(CursorAction cursorAction, Qt::KeyboardModifiers modifiers);
protected slots:
@ -41,4 +46,16 @@ protected slots:
signals:
void currentItemChanged(const QModelIndex &current, const QModelIndex &previous);
};
#endif // UAT_TREE_VIEW_H
#endif // TABNAV_TREE_VIEW_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:
*/