File indexing completed on 2024-04-21 03:56:05

0001 /*
0002     SPDX-FileCopyrightText: 2009 Stephen Kelly <steveire@gmail.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #ifndef MODELSPY_H
0008 #define MODELSPY_H
0009 
0010 #include <QModelIndex>
0011 #include <QObject>
0012 #include <QVariantList>
0013 
0014 #include "persistentchangelist.h"
0015 #include <QItemSelectionRange>
0016 
0017 #include "proxymodeltestsuite_export.h"
0018 
0019 enum SignalType {
0020     NoSignal,
0021     RowsAboutToBeInserted,
0022     RowsInserted,
0023     RowsAboutToBeRemoved,
0024     RowsRemoved,
0025     RowsAboutToBeMoved,
0026     RowsMoved,
0027     DataChanged,
0028     LayoutAboutToBeChanged,
0029     LayoutChanged,
0030     ModelAboutToBeReset,
0031     ModelReset,
0032 };
0033 
0034 class PROXYMODELTESTSUITE_EXPORT ModelSpy : public QObject, public QList<QVariantList>
0035 {
0036     Q_OBJECT
0037 public:
0038     ModelSpy(QObject *parent);
0039 
0040     void setModel(QAbstractItemModel *model);
0041     bool useLazyPersistence() const
0042     {
0043         return m_lazyPersist;
0044     }
0045     void setLazyPersistence(bool lazy)
0046     {
0047         m_lazyPersist = lazy;
0048     }
0049 
0050     void preTestPersistIndexes(const PersistentChangeList &changeList);
0051     QModelIndexList getUnchangedIndexes() const
0052     {
0053         return m_unchangedIndexes;
0054     }
0055     QList<QPersistentModelIndex> getUnchangedPersistentIndexes() const
0056     {
0057         return m_unchangedPersistentIndexes;
0058     }
0059     PersistentChangeList getChangeList() const
0060     {
0061         return m_changeList;
0062     }
0063 
0064     void startSpying();
0065     void stopSpying();
0066     bool isSpying()
0067     {
0068         return m_isSpying;
0069     }
0070 
0071     void clearTestData();
0072 
0073 protected Q_SLOTS:
0074     void rowsAboutToBeInserted(const QModelIndex &parent, int start, int end);
0075     void rowsInserted(const QModelIndex &parent, int start, int end);
0076     void rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end);
0077     void rowsRemoved(const QModelIndex &parent, int start, int end);
0078     void rowsAboutToBeMoved(const QModelIndex &srcParent, int start, int end, const QModelIndex &destParent, int destStart);
0079     void rowsMoved(const QModelIndex &srcParent, int start, int end, const QModelIndex &destParent, int destStart);
0080 
0081     void layoutAboutToBeChanged();
0082     void layoutChanged();
0083 
0084     void modelAboutToBeReset();
0085     void modelReset();
0086 
0087     void modelDestroyed();
0088 
0089     void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight);
0090 
0091 private:
0092     void doPersist();
0093     QModelIndexList getUnchangedIndexes(const QModelIndex &parent, const QList<QItemSelectionRange> &ignoredRanges);
0094     QModelIndexList getDescendantIndexes(const QModelIndex &index);
0095     QList<QPersistentModelIndex> toPersistent(const QModelIndexList &list);
0096 
0097 private:
0098     QAbstractItemModel *m_model;
0099     bool m_isSpying;
0100     bool m_lazyPersist;
0101     PersistentChangeList m_changeList;
0102     QModelIndexList m_unchangedIndexes;
0103     QList<QPersistentModelIndex> m_unchangedPersistentIndexes;
0104 };
0105 
0106 PROXYMODELTESTSUITE_EXPORT uint qHash(const QVariant &var);
0107 
0108 PROXYMODELTESTSUITE_EXPORT QDebug operator<<(QDebug d, ModelSpy *modelSpy);
0109 
0110 #endif