/* * All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or * its licensors. * * For complete copyright and license terms please see the LICENSE at the root of this * distribution (the "License"). All use of this software is governed by the License, * or, if provided, by the license below or the license accompanying this file. Do not * remove or modify any license notices. This file is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * */ #ifndef COLUMNSORTPROXYMODEL_H #define COLUMNSORTPROXYMODEL_H #include #include class ColumnGroupProxyModel; /*! \brief Proxy model used to sort on multiple columns It's using a stable sort to sort on multiple columns. Every time a value is changed (on one of the sorted columns), it will resort everything. THis model does not work with tree models! */ class ColumnSortProxyModel : public QAbstractProxyModel { Q_OBJECT public: ColumnSortProxyModel(QObject* parent = nullptr); ~ColumnSortProxyModel(); QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override; QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override; int rowCount(const QModelIndex& index = QModelIndex()) const override; int columnCount(const QModelIndex& index = QModelIndex()) const override; QModelIndex index(int row, int column, const QModelIndex& parent = QModelIndex()) const override; QModelIndex parent(const QModelIndex& index) const override; QModelIndex mapFromSource(const QModelIndex& sourceIndex) const override; QModelIndex mapToSource(const QModelIndex& proxyIndex) const override; void setSourceModel(QAbstractItemModel* sourceModel) override; void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override; void AddColumn(int column, Qt::SortOrder order = Qt::AscendingOrder); void RemoveColumn(int column); void SetColumns(const QVector& columns); void ClearColumns(); bool IsColumnSorted(int col) const; Qt::SortOrder SortOrder(int col) const; signals: void SortChanged(); private: friend class ColumnGroupProxyModel; void AddColumnWithoutSorting(int column, Qt::SortOrder order = Qt::AscendingOrder); void RemoveColumnWithoutSorting(int column); private slots: void SortModel(); void SourceDataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight); private: struct Column { int column; Qt::SortOrder sortOrder; }; int ColumnContains(int col) const; QVector m_columns; QVector m_mappingToSource; }; #endif //COLUMNSORTPROXYMODEL_H