File indexing completed on 2024-05-12 16:25:51

0001 /*
0002    SPDX-FileCopyrightText: 2021 David Faure <faure@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 #pragma once
0007 
0008 #include "libruqolacore_export.h"
0009 #include "roommodel.h"
0010 #include <QAbstractProxyModel>
0011 
0012 #include <array>
0013 #include <vector>
0014 
0015 class LIBRUQOLACORE_EXPORT RoomListHeadingsProxyModel final : public QAbstractProxyModel
0016 {
0017     Q_OBJECT
0018 
0019 public:
0020     explicit RoomListHeadingsProxyModel(QObject *parent = nullptr);
0021 
0022     static constexpr uint sectionCount = uint(RoomModel::Section::NSections);
0023 
0024     // QAbstractItemModel interface
0025     QVariant data(const QModelIndex &index, int role) const override;
0026     QModelIndex index(int row, int column, const QModelIndex &parent) const override;
0027     QModelIndex parent(const QModelIndex &child) const override;
0028     int rowCount(const QModelIndex &parent) const override;
0029     int columnCount(const QModelIndex &parent) const override;
0030     Qt::ItemFlags flags(const QModelIndex &index) const override;
0031     bool hasChildren(const QModelIndex &index) const override;
0032 
0033     // QAbstractProxyModel interface
0034     void setSourceModel(QAbstractItemModel *sourceModel) override;
0035     QModelIndex mapToSource(const QModelIndex &proxyIndex) const override;
0036     QModelIndex mapFromSource(const QModelIndex &sourceIndex) const override;
0037 
0038 private:
0039     LIBRUQOLACORE_NO_EXPORT void onRowsInserted(const QModelIndex &parent, int first, int last);
0040     LIBRUQOLACORE_NO_EXPORT void onRowsAboutToBeRemoved(const QModelIndex &parent, int first, int last);
0041     LIBRUQOLACORE_NO_EXPORT void onDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles);
0042     LIBRUQOLACORE_NO_EXPORT void rebuildSections();
0043 
0044     // each section vector is kept sorted for performance reasons
0045     std::array<std::vector<QPersistentModelIndex>, sectionCount> mSections;
0046 
0047     enum class IndexType {
0048         Root,
0049         Section,
0050         Channel,
0051     };
0052     [[nodiscard]] LIBRUQOLACORE_NO_EXPORT IndexType type(const QModelIndex &index) const;
0053 };