diff --git a/ui/qt/proto_tree.cpp b/ui/qt/proto_tree.cpp index 8b53332039..15e78bed6b 100644 --- a/ui/qt/proto_tree.cpp +++ b/ui/qt/proto_tree.cpp @@ -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; } diff --git a/ui/qt/proto_tree.h b/ui/qt/proto_tree.h index 6ed2e6ebb5..4988343386 100644 --- a/ui/qt/proto_tree.h +++ b/ui/qt/proto_tree.h @@ -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_;