SortedShareModel::SortedShareModel(QObject *parent)
: QSortFilterProxyModel(parent)
{
-}
-
-void SortedShareModel::sortModel()
-{
- sort(0);
-}
-
-ShareModel *SortedShareModel::shareModel() const
-{
- return qobject_cast<ShareModel*>(sourceModel());
-}
-
-void SortedShareModel::setShareModel(ShareModel *shareModel)
-{
- const auto currentSetModel = sourceModel();
-
- if(currentSetModel) {
- disconnect(currentSetModel, &ShareModel::rowsInserted, this, &SortedShareModel::sortModel);
- disconnect(currentSetModel, &ShareModel::rowsMoved, this, &SortedShareModel::sortModel);
- disconnect(currentSetModel, &ShareModel::rowsRemoved, this, &SortedShareModel::sortModel);
- disconnect(currentSetModel, &ShareModel::dataChanged, this, &SortedShareModel::sortModel);
- disconnect(currentSetModel, &ShareModel::modelReset, this, &SortedShareModel::sortModel);
- }
-
- // Re-sort model when any changes take place
- connect(shareModel, &ShareModel::rowsInserted, this, &SortedShareModel::sortModel);
- connect(shareModel, &ShareModel::rowsMoved, this, &SortedShareModel::sortModel);
- connect(shareModel, &ShareModel::rowsRemoved, this, &SortedShareModel::sortModel);
- connect(shareModel, &ShareModel::dataChanged, this, &SortedShareModel::sortModel);
- connect(shareModel, &ShareModel::modelReset, this, &SortedShareModel::sortModel);
-
- setSourceModel(shareModel);
- sortModel();
- Q_EMIT shareModelChanged();
+ sort(0, Qt::AscendingOrder);
}
bool SortedShareModel::lessThan(const QModelIndex &sourceLeft, const QModelIndex &sourceRight) const
class SortedShareModel : public QSortFilterProxyModel
{
Q_OBJECT
- Q_PROPERTY(ShareModel* shareModel READ shareModel WRITE setShareModel NOTIFY shareModelChanged)
public:
explicit SortedShareModel(QObject *parent = nullptr);
- [[nodiscard]] ShareModel *shareModel() const;
-
-signals:
- void shareModelChanged();
-
-public slots:
- void setShareModel(OCC::ShareModel *shareModel);
-
protected:
[[nodiscard]] bool lessThan(const QModelIndex &sourceLeft, const QModelIndex &sourceRight) const override;
-private slots:
- void sortModel();
};
} // namespace OCC
SortedShareModel sortedModel;
QAbstractItemModelTester sortedModelTester(&sortedModel);
QSignalSpy sortedModelReset(&sortedModel, &SortedShareModel::modelReset);
- QSignalSpy shareModelChanged(&sortedModel, &SortedShareModel::shareModelChanged);
+ QSignalSpy shareModelChanged(&sortedModel, &SortedShareModel::sourceModelChanged);
- sortedModel.setShareModel(&model);
+ sortedModel.setSourceModel(&model);
QCOMPARE(shareModelChanged.count(), 1);
QCOMPARE(sortedModelReset.count(), 1);
QCOMPARE(sortedModel.rowCount(), model.rowCount());
- QCOMPARE(sortedModel.shareModel(), &model);
+ QCOMPARE(sortedModel.sourceModel(), &model);
}
void testCorrectSort()
QAbstractItemModelTester sortedModelTester(&sortedModel);
QSignalSpy sortedModelReset(&sortedModel, &SortedShareModel::modelReset);
- sortedModel.setShareModel(&model);
+ sortedModel.setSourceModel(&model);
QCOMPARE(sortedModelReset.count(), 1);
QCOMPARE(sortedModel.rowCount(), model.rowCount());