TreeViewExpandState.hpp
1 #pragma once
2 #include <score/model/tree/TreeNode.hpp>
3 #include <score/model/tree/TreeNodeItemModel.hpp>
4 
5 #include <QAbstractProxyModel>
6 #include <QTreeView>
7 
8 #include <vector>
9 
10 namespace score
11 {
12 template <typename Node, typename NodePath>
14 {
15  void save(QAbstractProxyModel* m, QTreeView* v)
16  {
17  auto de = static_cast<TreeModel*>(m->sourceModel());
18 
19  m_expandedIndices.clear();
20  de->iterate(v->rootIndex(), [this, m, v](const QModelIndex& index) {
21  if(v->isExpanded(m->mapFromSource(index)))
22  {
23  if(auto item = static_cast<Node*>(index.internalPointer()))
24  {
25  m_expandedIndices.push_back(NodePath{*item});
26  }
27  }
28  });
29  }
30 
31  void restore(QAbstractProxyModel* m, QTreeView* v)
32  {
33  auto de = static_cast<TreeModel*>(m->sourceModel());
34 
35  v->setUpdatesEnabled(false);
36  v->collapseAll();
37  for(auto& path : m_expandedIndices)
38  {
39  auto idx = de->convertPathToIndex(path);
40  if(idx.isValid())
41  {
42  v->expand(m->mapFromSource(idx));
43  }
44  }
45  v->setUpdatesEnabled(true);
46  v->update();
47  }
48 
49  std::vector<NodePath> m_expandedIndices;
50 };
51 }
Definition: TreeNodeItemModel.hpp:15
Base toolkit upon which the software is built.
Definition: Application.cpp:90
Definition: TreeViewExpandState.hpp:14