wireshark/ui/qt/widgets/tabnav_tree_view.cpp
Peter Wu 4beb7430d4 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>
2017-07-16 14:47:01 +00:00

82 lines
2.7 KiB
C++

/* tabnav_tree_view.cpp
* Tree view with saner tab navigation functionality.
*
* Copyright 2016 Peter Wu <peter@lekensteyn.nl>
*
* 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 "tabnav_tree_view.h"
TabnavTreeView::TabnavTreeView(QWidget *parent) : QTreeView(parent)
{
}
// 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 TabnavTreeView::moveCursor(CursorAction cursorAction, Qt::KeyboardModifiers modifiers)
{
QModelIndex current = currentIndex();
// If an item is currently selected, interpret Next/Previous. Otherwise,
// fallback to the default selection (e.g. first row for Next).
if (current.isValid()) {
if (cursorAction == MoveNext) {
if (current.column() < model()->columnCount()) {
return current.sibling(current.row(), current.column() + 1);
}
return current;
} else if (cursorAction == MovePrevious) {
if (current.column() > 0) {
return current.sibling(current.row(), current.column() - 1);
}
return current;
}
}
return QTreeView::moveCursor(cursorAction, modifiers);
}
/*!
\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);
}
/* * 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:
*/