ColumnSortProxyModel.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 "ColumnSortProxyModel.h"
  10. // Editor
  11. #include "Util/AbstractSortModel.h"
  12. class ColumnSortProxyModelLessThan
  13. {
  14. public:
  15. inline ColumnSortProxyModelLessThan(int column, const AbstractSortModel* source)
  16. : sort_column(column)
  17. , source_model(source) {}
  18. inline bool operator()(int r1, int r2) const
  19. {
  20. QModelIndex i1 = source_model->index(r1, sort_column);
  21. QModelIndex i2 = source_model->index(r2, sort_column);
  22. return source_model->LessThan(i1, i2);
  23. }
  24. private:
  25. int sort_column;
  26. const AbstractSortModel* source_model;
  27. };
  28. class ColumnSortProxyModelGreaterThan
  29. {
  30. public:
  31. inline ColumnSortProxyModelGreaterThan(int column, const AbstractSortModel* source)
  32. : sort_column(column)
  33. , source_model(source) {}
  34. inline bool operator()(int r1, int r2) const
  35. {
  36. QModelIndex i1 = source_model->index(r1, sort_column);
  37. QModelIndex i2 = source_model->index(r2, sort_column);
  38. return source_model->LessThan(i2, i1);
  39. }
  40. private:
  41. int sort_column;
  42. const AbstractSortModel* source_model;
  43. };
  44. ColumnSortProxyModel::ColumnSortProxyModel(QObject* parent)
  45. : QAbstractProxyModel(parent)
  46. {
  47. }
  48. ColumnSortProxyModel::~ColumnSortProxyModel()
  49. {
  50. }
  51. QVariant ColumnSortProxyModel::data(const QModelIndex& index, int role) const
  52. {
  53. Q_ASSERT(index.isValid() && index.model() == this);
  54. return sourceModel()->data(mapToSource(index), role);
  55. }
  56. QVariant ColumnSortProxyModel::headerData(int section, Qt::Orientation orientation, int role) const
  57. {
  58. return sourceModel()->headerData(section, orientation, role);
  59. }
  60. int ColumnSortProxyModel::rowCount(const QModelIndex& index) const
  61. {
  62. if (!sourceModel() || index.isValid())
  63. {
  64. return 0;
  65. }
  66. return sourceModel()->rowCount(index);
  67. }
  68. int ColumnSortProxyModel::columnCount(const QModelIndex& index) const
  69. {
  70. if (!sourceModel() || index.isValid())
  71. {
  72. return 0;
  73. }
  74. return sourceModel()->columnCount(index);
  75. }
  76. QModelIndex ColumnSortProxyModel::index(int row, int column, const QModelIndex& parent) const
  77. {
  78. Q_UNUSED(parent);
  79. Q_ASSERT(!parent.isValid());
  80. return createIndex(row, column);
  81. }
  82. QModelIndex ColumnSortProxyModel::parent(const QModelIndex& index) const
  83. {
  84. Q_UNUSED(index);
  85. return QModelIndex();
  86. }
  87. QModelIndex ColumnSortProxyModel::mapFromSource(const QModelIndex& sourceIndex) const
  88. {
  89. Q_ASSERT(!sourceIndex.isValid() || sourceIndex.model() == sourceModel());
  90. if (!sourceIndex.isValid())
  91. {
  92. return QModelIndex();
  93. }
  94. int row = m_mappingToSource.indexOf(sourceIndex.row());
  95. return createIndex(row, sourceIndex.column());
  96. }
  97. QModelIndex ColumnSortProxyModel::mapToSource(const QModelIndex& proxyIndex) const
  98. {
  99. Q_ASSERT(!proxyIndex.isValid() || proxyIndex.model() == this);
  100. if (!proxyIndex.isValid())
  101. {
  102. return QModelIndex();
  103. }
  104. int row = m_mappingToSource.at(proxyIndex.row());
  105. return sourceModel()->index(row, proxyIndex.column());
  106. }
  107. void ColumnSortProxyModel::setSourceModel(QAbstractItemModel* sourceModel)
  108. {
  109. Q_ASSERT(qobject_cast<AbstractSortModel*>(sourceModel));
  110. QAbstractProxyModel::setSourceModel(sourceModel);
  111. connect(sourceModel, &QAbstractItemModel::rowsInserted, this, &ColumnSortProxyModel::SortModel);
  112. connect(sourceModel, &QAbstractItemModel::rowsRemoved, this, &ColumnSortProxyModel::SortModel);
  113. connect(sourceModel, &QAbstractItemModel::modelAboutToBeReset, this, &ColumnSortProxyModel::beginResetModel);
  114. connect(sourceModel, &QAbstractItemModel::modelReset, this, [=]()
  115. {
  116. { QSignalBlocker sb(this);
  117. SortModel();
  118. } endResetModel();
  119. });
  120. connect(sourceModel, &QAbstractItemModel::layoutChanged, this, &ColumnSortProxyModel::SortModel);
  121. connect(sourceModel, &QAbstractItemModel::dataChanged, this, &ColumnSortProxyModel::SourceDataChanged);
  122. SortModel();
  123. }
  124. void ColumnSortProxyModel::sort(int column, Qt::SortOrder order)
  125. {
  126. int id = ColumnContains(column);
  127. if (id == -1)
  128. {
  129. AddColumn(column, order);
  130. }
  131. else
  132. {
  133. if (m_columns.at(id).sortOrder != order)
  134. {
  135. m_columns[id].sortOrder = order;
  136. SortModel();
  137. }
  138. }
  139. }
  140. void ColumnSortProxyModel::AddColumn(int column, Qt::SortOrder order)
  141. {
  142. if (ColumnContains(column) == -1)
  143. {
  144. m_columns.push_front({ column, order });
  145. SortModel();
  146. }
  147. }
  148. void ColumnSortProxyModel::AddColumnWithoutSorting(int column, Qt::SortOrder order)
  149. {
  150. if (ColumnContains(column) == -1)
  151. {
  152. m_columns.push_front({ column, order });
  153. }
  154. }
  155. void ColumnSortProxyModel::RemoveColumn(int column)
  156. {
  157. int id = ColumnContains(column);
  158. if (id != -1)
  159. {
  160. m_columns.remove(id);
  161. SortModel();
  162. }
  163. }
  164. void ColumnSortProxyModel::RemoveColumnWithoutSorting(int column)
  165. {
  166. int id = ColumnContains(column);
  167. if (id != -1)
  168. {
  169. m_columns.remove(id);
  170. }
  171. }
  172. void ColumnSortProxyModel::SetColumns(const QVector<int>& columns)
  173. {
  174. m_columns.clear();
  175. foreach(int col, columns)
  176. {
  177. m_columns.push_back({ col, Qt::AscendingOrder });
  178. }
  179. SortModel();
  180. }
  181. void ColumnSortProxyModel::ClearColumns()
  182. {
  183. SortModel();
  184. }
  185. bool ColumnSortProxyModel::IsColumnSorted(int col) const
  186. {
  187. int id = ColumnContains(col);
  188. return (id != -1);
  189. }
  190. Qt::SortOrder ColumnSortProxyModel::SortOrder(int col) const
  191. {
  192. int id = ColumnContains(col);
  193. if (id != -1)
  194. {
  195. return m_columns.at(id).sortOrder;
  196. }
  197. return Qt::AscendingOrder;
  198. }
  199. void ColumnSortProxyModel::SortModel()
  200. {
  201. emit layoutAboutToBeChanged();
  202. int size = sourceModel() ? sourceModel()->rowCount() : 0;
  203. m_mappingToSource.resize(size);
  204. for (int i = 0; i < size; ++i)
  205. {
  206. m_mappingToSource[i] = i;
  207. }
  208. foreach(Column col, m_columns)
  209. {
  210. if (col.sortOrder == Qt::AscendingOrder)
  211. {
  212. ColumnSortProxyModelLessThan lt(col.column, qobject_cast<AbstractSortModel*>(sourceModel()));
  213. std::stable_sort(m_mappingToSource.begin(), m_mappingToSource.end(), lt);
  214. }
  215. else
  216. {
  217. ColumnSortProxyModelGreaterThan gt(col.column, qobject_cast<AbstractSortModel*>(sourceModel()));
  218. std::stable_sort(m_mappingToSource.begin(), m_mappingToSource.end(), gt);
  219. }
  220. }
  221. emit layoutChanged();
  222. emit SortChanged();
  223. }
  224. void ColumnSortProxyModel::SourceDataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight)
  225. {
  226. for (int col = topLeft.column(); col <= bottomRight.column(); ++col)
  227. {
  228. if (ColumnContains(col) != -1)
  229. {
  230. SortModel();
  231. return;
  232. }
  233. }
  234. }
  235. int ColumnSortProxyModel::ColumnContains(int col) const
  236. {
  237. for (int i = 0; i < m_columns.count(); ++i)
  238. {
  239. if (m_columns.at(i).column == col)
  240. {
  241. return i;
  242. }
  243. }
  244. return -1;
  245. }
  246. #include <Util/moc_ColumnSortProxyModel.cpp>