ColumnGroupTreeView.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include "EditorDefs.h"
  9. #include "ColumnGroupTreeView.h"
  10. // Editor
  11. #include "Util/ColumnGroupHeaderView.h"
  12. #include "Util/ColumnGroupProxyModel.h"
  13. #include "Util/ColumnGroupItemDelegate.h"
  14. ColumnGroupTreeView::ColumnGroupTreeView(QWidget* parent)
  15. : QTreeView(parent)
  16. , m_header(new ColumnGroupHeaderView)
  17. , m_groupModel(new ColumnGroupProxyModel(this))
  18. {
  19. setSortingEnabled(true);
  20. setHeader(m_header);
  21. setItemDelegate(new ColumnGroupItemDelegate(this));
  22. setSelectionMode(QAbstractItemView::ExtendedSelection);
  23. setSelectionBehavior(QAbstractItemView::SelectRows);
  24. QTreeView::setModel(m_groupModel);
  25. connect(m_groupModel, &QAbstractItemModel::modelAboutToBeReset, this, &ColumnGroupTreeView::SaveOpenState);
  26. connect(m_groupModel, &QAbstractItemModel::modelReset, this, &ColumnGroupTreeView::RestoreOpenState);
  27. connect(m_groupModel, SIGNAL(GroupUpdated()), this, SLOT(SpanGroups()));
  28. connect(m_groupModel, &ColumnGroupProxyModel::GroupsChanged, this, &ColumnGroupTreeView::expandAll);
  29. }
  30. void ColumnGroupTreeView::setModel(QAbstractItemModel* model)
  31. {
  32. m_groupModel->setSourceModel(model);
  33. if (model)
  34. {
  35. connect(model, &QAbstractItemModel::modelReset, this, &QTreeView::expandAll, Qt::QueuedConnection);
  36. }
  37. }
  38. bool ColumnGroupTreeView::IsGroupsShown() const
  39. {
  40. return m_header->IsGroupsShown();
  41. }
  42. void ColumnGroupTreeView::ShowGroups(bool showGroups)
  43. {
  44. m_header->ShowGroups(showGroups);
  45. }
  46. QSet<QString> GetOpenNodes(QTreeView* tree, const QModelIndex& parent)
  47. {
  48. int rows = tree->model()->rowCount(parent);
  49. QSet<QString> results;
  50. for (int row = 0; row < rows; ++row)
  51. {
  52. auto index = tree->model()->index(row, 0, parent);
  53. if (tree->isExpanded(index))
  54. {
  55. results.insert(index.data().toString());
  56. }
  57. results.unite(GetOpenNodes(tree, index));
  58. }
  59. return results;
  60. }
  61. void ColumnGroupTreeView::SaveOpenState()
  62. {
  63. m_openNodes = GetOpenNodes(this, QModelIndex());
  64. }
  65. void RestoreOpenNodes(QTreeView* tree, const QSet<QString> openNodes, const QModelIndex& parent)
  66. {
  67. int rows = tree->model()->rowCount(parent);
  68. QSet<QString> results;
  69. for (int row = 0; row < rows; ++row)
  70. {
  71. auto index = tree->model()->index(row, 0, parent);
  72. auto text = index.data().toString();
  73. if (openNodes.contains(text))
  74. {
  75. tree->expand(index);
  76. }
  77. RestoreOpenNodes(tree, openNodes, index);
  78. }
  79. }
  80. void ColumnGroupTreeView::RestoreOpenState()
  81. {
  82. RestoreOpenNodes(this, m_openNodes, QModelIndex());
  83. }
  84. void ColumnGroupTreeView::Sort(int column, Qt::SortOrder order)
  85. {
  86. m_groupModel->sort(column, order);
  87. m_header->setSortIndicator(column, order);
  88. }
  89. void ColumnGroupTreeView::ToggleSortOrder(int column)
  90. {
  91. auto sortOrder = m_groupModel->SortOrder(column) == Qt::AscendingOrder ? Qt::DescendingOrder : Qt::AscendingOrder;
  92. m_groupModel->sort(column, sortOrder);
  93. }
  94. void ColumnGroupTreeView::AddGroup(int column)
  95. {
  96. m_groupModel->AddGroup(column);
  97. }
  98. void ColumnGroupTreeView::RemoveGroup(int column)
  99. {
  100. m_groupModel->RemoveGroup(column);
  101. }
  102. void ColumnGroupTreeView::SetGroups(const QVector<int>& columns)
  103. {
  104. m_groupModel->SetGroups(columns);
  105. }
  106. void ColumnGroupTreeView::ClearGroups()
  107. {
  108. m_groupModel->ClearGroups();
  109. }
  110. QVector<int> ColumnGroupTreeView::Groups() const
  111. {
  112. return m_groupModel->Groups();
  113. }
  114. void ColumnGroupTreeView::SpanGroups(const QModelIndex& index)
  115. {
  116. int childrenCount = m_groupModel->rowCount(index);
  117. for (int row = 0; row < childrenCount; ++row)
  118. {
  119. auto childIndex = m_groupModel->index(row, 0, index);
  120. bool hasChildren = m_groupModel->hasChildren(childIndex);
  121. if (hasChildren)
  122. {
  123. setFirstColumnSpanned(row, index, true);
  124. SpanGroups(childIndex);
  125. }
  126. }
  127. }
  128. QModelIndex ColumnGroupTreeView::mapToSource(const QModelIndex& proxyIndex) const
  129. {
  130. auto sortProxy = qobject_cast<QAbstractProxyModel*>(m_groupModel->sourceModel());
  131. return sortProxy->mapToSource(m_groupModel->mapToSource(proxyIndex));
  132. }
  133. QModelIndex ColumnGroupTreeView::mapFromSource(const QModelIndex& sourceModel) const
  134. {
  135. auto sortProxy = qobject_cast<QAbstractProxyModel*>(m_groupModel->sourceModel());
  136. return m_groupModel->mapFromSource(sortProxy->mapFromSource(sourceModel));
  137. }
  138. #include <Util/moc_ColumnGroupTreeView.cpp>