ColumnGroupProxyModel.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 "ColumnGroupProxyModel.h"
  10. // Editor
  11. #include "Util/ColumnSortProxyModel.h"
  12. #include "Util/AbstractSortModel.h"
  13. ColumnGroupProxyModel::ColumnGroupProxyModel(QObject* parent)
  14. : AbstractGroupProxyModel(parent)
  15. , m_sortModel(new ColumnSortProxyModel(this))
  16. , m_freeSortColumn(-1)
  17. {
  18. AbstractGroupProxyModel::setSourceModel(m_sortModel);
  19. connect(m_sortModel, &ColumnSortProxyModel::SortChanged, this, &ColumnGroupProxyModel::SortChanged);
  20. }
  21. void ColumnGroupProxyModel::sort(int column, Qt::SortOrder order)
  22. {
  23. if (m_freeSortColumn != -1)
  24. {
  25. m_sortModel->RemoveColumnWithoutSorting(m_freeSortColumn);
  26. m_freeSortColumn = -1;
  27. }
  28. if (!m_groups.contains(column))
  29. {
  30. m_freeSortColumn = column;
  31. }
  32. m_sortModel->sort(column, order);
  33. }
  34. void ColumnGroupProxyModel::setSourceModel(QAbstractItemModel* sourceModel)
  35. {
  36. Q_ASSERT(qobject_cast<AbstractSortModel*>(sourceModel));
  37. m_sortModel->setSourceModel(sourceModel);
  38. RebuildTree();
  39. }
  40. void ColumnGroupProxyModel::AddGroup(int column)
  41. {
  42. if (!m_groups.contains(column))
  43. {
  44. m_groups.push_back(column);
  45. sort(column);
  46. Q_EMIT GroupsChanged();
  47. }
  48. }
  49. void ColumnGroupProxyModel::RemoveGroup(int column)
  50. {
  51. if (m_groups.contains(column))
  52. {
  53. m_groups.remove(m_groups.indexOf(column));
  54. m_sortModel->RemoveColumn(column);
  55. Q_EMIT GroupsChanged();
  56. }
  57. }
  58. void ColumnGroupProxyModel::SetGroups(const QVector<int>& columns)
  59. {
  60. m_groups.clear();
  61. foreach(int col, columns)
  62. {
  63. m_groups.push_back(col);
  64. m_sortModel->AddColumnWithoutSorting(col);
  65. }
  66. m_sortModel->SortModel();
  67. Q_EMIT GroupsChanged();
  68. }
  69. void ColumnGroupProxyModel::ClearGroups()
  70. {
  71. m_groups.clear();
  72. m_sortModel->ClearColumns();
  73. Q_EMIT GroupsChanged();
  74. }
  75. QVector<int> ColumnGroupProxyModel::Groups() const
  76. {
  77. return m_groups;
  78. }
  79. bool ColumnGroupProxyModel::IsColumnSorted(int col) const
  80. {
  81. return m_sortModel->IsColumnSorted(col);
  82. }
  83. Qt::SortOrder ColumnGroupProxyModel::SortOrder(int col) const
  84. {
  85. return m_sortModel->SortOrder(col);
  86. }
  87. QStringList ColumnGroupProxyModel::GroupForSourceIndex(const QModelIndex& sourceIndex) const
  88. {
  89. QStringList group;
  90. foreach(int column, m_groups)
  91. group.push_back(QString::fromLatin1("%1: %2").arg(headerData(column, Qt::Horizontal).toString(), sourceIndex.sibling(sourceIndex.row(), column).data().toString()));
  92. return group;
  93. }
  94. #include <Util/moc_ColumnGroupProxyModel.cpp>