ColumnSortProxyModel.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. #ifndef COLUMNSORTPROXYMODEL_H
  9. #define COLUMNSORTPROXYMODEL_H
  10. #if !defined(Q_MOC_RUN)
  11. #include <QAbstractProxyModel>
  12. #include <QVector>
  13. #endif
  14. class ColumnGroupProxyModel;
  15. /*!
  16. \brief Proxy model used to sort on multiple columns
  17. It's using a stable sort to sort on multiple columns.
  18. Every time a value is changed (on one of the sorted columns), it will resort everything.
  19. THis model does not work with tree models!
  20. */
  21. class ColumnSortProxyModel
  22. : public QAbstractProxyModel
  23. {
  24. Q_OBJECT
  25. public:
  26. ColumnSortProxyModel(QObject* parent = nullptr);
  27. ~ColumnSortProxyModel();
  28. QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
  29. QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
  30. int rowCount(const QModelIndex& index = QModelIndex()) const override;
  31. int columnCount(const QModelIndex& index = QModelIndex()) const override;
  32. QModelIndex index(int row, int column, const QModelIndex& parent = QModelIndex()) const override;
  33. QModelIndex parent(const QModelIndex& index) const override;
  34. QModelIndex mapFromSource(const QModelIndex& sourceIndex) const override;
  35. QModelIndex mapToSource(const QModelIndex& proxyIndex) const override;
  36. void setSourceModel(QAbstractItemModel* sourceModel) override;
  37. void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override;
  38. void AddColumn(int column, Qt::SortOrder order = Qt::AscendingOrder);
  39. void RemoveColumn(int column);
  40. void SetColumns(const QVector<int>& columns);
  41. void ClearColumns();
  42. bool IsColumnSorted(int col) const;
  43. Qt::SortOrder SortOrder(int col) const;
  44. signals:
  45. void SortChanged();
  46. private:
  47. friend class ColumnGroupProxyModel;
  48. void AddColumnWithoutSorting(int column, Qt::SortOrder order = Qt::AscendingOrder);
  49. void RemoveColumnWithoutSorting(int column);
  50. private slots:
  51. void SortModel();
  52. void SourceDataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight);
  53. private:
  54. struct Column
  55. {
  56. int column;
  57. Qt::SortOrder sortOrder;
  58. };
  59. int ColumnContains(int col) const;
  60. QVector<Column> m_columns;
  61. QVector<int> m_mappingToSource;
  62. };
  63. #endif //COLUMNSORTPROXYMODEL_H