Qt:Fix ProtoTree Copy
authorRoland Knall <roland.knall@br-automation.com>
Fri, 25 May 2018 13:19:46 +0000 (15:19 +0200)
committerRoland Knall <rknall@gmail.com>
Wed, 30 May 2018 12:35:30 +0000 (12:35 +0000)
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>
ui/qt/proto_tree.cpp
ui/qt/proto_tree.h

index 8b533320390e3f48f6ef48f37cd4812ec45c6224..15e78bed6ba4885ef2e5b979bc97b75fd5153f85 100644 (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;
-
-    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;
-        }
-        // Next sibling
-        QModelIndex sibling = proto_tree_model_->index(cur_idx.row() + 1, 0, cur_idx.parent());
-        if (sibling.isValid()) {
-            cur_idx = sibling;
-            continue;
+    QString result = "";
+
+    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 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;
 }
index 6ed2e6ebb50fc2a5164b7d9831bc00b7d73c7492..49883433865b38621a14bfc7d2a4ee8ee2dd3ed8 100644 (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_;