Qt:Fix ProtoTree Copy

There was an issue, where siblings where not copied correctly.

Bug:14355
Change-Id: I31611a6731f3f4de6b204c7ee708e42f0b7b170c
Reviewed-on: https://code.wireshark.org/review/27802
Petri-Dish: Roland Knall <rknall@gmail.com>
Tested-by: Petri Dish Buildbot
Reviewed-by: Kenneth Soerensen <knnthsrnsn@gmail.com>
Reviewed-by: Roland Knall <rknall@gmail.com>
This commit is contained in:
Roland Knall 2018-05-25 15:19:46 +02:00 committed by Roland Knall
parent c5b5c4a2ed
commit 9e4f7dc50d
2 changed files with 34 additions and 25 deletions

View File

@ -488,33 +488,40 @@ void ProtoTree::restoreSelectedField()
autoScrollTo(cur_index);
}
const QString ProtoTree::toString(const QModelIndex &start_idx) const
QString ProtoTree::traverseTree(const QModelIndex & travTree, int identLevel) const
{
QModelIndex cur_idx = start_idx.isValid() ? start_idx : proto_tree_model_->index(0, 0);
QModelIndex stop_idx = proto_tree_model_->index(cur_idx.row() + 1, 0, cur_idx.parent());
QString tree_string;
int indent_level = 0;
QString result = "";
do {
tree_string.append(QString(" ").repeated(indent_level));
tree_string.append(cur_idx.data().toString());
tree_string.append("\n");
// Next child
if (isExpanded(cur_idx)) {
cur_idx = proto_tree_model_->index(0, 0, cur_idx);
indent_level++;
continue;
if ( travTree.isValid() )
{
result.append(QString(" ").repeated(identLevel));
result.append(travTree.data().toString());
result.append("\n");
/* if the element is expanded, we traverse one level down */
if ( isExpanded(travTree) )
{
int children = proto_tree_model_->rowCount(travTree);
identLevel++;
for ( int child = 0; child < children; child++ )
result += traverseTree(proto_tree_model_->index(child, 0, travTree), identLevel);
}
// Next sibling
QModelIndex sibling = proto_tree_model_->index(cur_idx.row() + 1, 0, cur_idx.parent());
if (sibling.isValid()) {
cur_idx = sibling;
continue;
}
// Next parent
cur_idx = proto_tree_model_->index(cur_idx.parent().row() + 1, 0, cur_idx.parent().parent());
indent_level--;
} while (cur_idx.isValid() && cur_idx.internalPointer() != stop_idx.internalPointer() && indent_level >= 0);
}
return result;
}
QString ProtoTree::toString(const QModelIndex &start_idx) const
{
QString tree_string = "";
if ( start_idx.isValid() )
tree_string = traverseTree(start_idx, 0);
else
{
int children = proto_tree_model_->rowCount();
for ( int child = 0; child < children; child++ )
tree_string += traverseTree(proto_tree_model_->index(child, 0, QModelIndex()), 0);
}
return tree_string;
}

View File

@ -38,7 +38,7 @@ public:
void clear();
void closeContextMenu();
void restoreSelectedField();
const QString toString(const QModelIndex &start_idx = QModelIndex()) const;
QString toString(const QModelIndex &start_idx = QModelIndex()) const;
protected:
virtual void contextMenuEvent(QContextMenuEvent *event);
@ -47,6 +47,8 @@ protected:
virtual bool eventFilter(QObject * obj, QEvent * ev);
virtual QModelIndex moveCursor(CursorAction cursorAction, Qt::KeyboardModifiers modifiers);
QString traverseTree(const QModelIndex & rootNode, int identLevel = 0) const;
private:
ProtoTreeModel *proto_tree_model_;
QMenu ctx_menu_;