1 /*
2     SPDX-FileCopyrightText: 2009 Stephen Kelly <steveire@gmail.com>
3 
4     SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #ifndef MODELSPY_H
8 #define MODELSPY_H
9 
10 #include <QModelIndex>
11 #include <QObject>
12 #include <QVariantList>
13 
14 #include "persistentchangelist.h"
15 #include <QItemSelectionRange>
16 
17 #include "proxymodeltestsuite_export.h"
18 
19 enum SignalType {
20     NoSignal,
21     RowsAboutToBeInserted,
22     RowsInserted,
23     RowsAboutToBeRemoved,
24     RowsRemoved,
25     RowsAboutToBeMoved,
26     RowsMoved,
27     DataChanged,
28     LayoutAboutToBeChanged,
29     LayoutChanged,
30     ModelAboutToBeReset,
31     ModelReset,
32 };
33 
34 class PROXYMODELTESTSUITE_EXPORT ModelSpy : public QObject, public QList<QVariantList>
35 {
36     Q_OBJECT
37 public:
38     ModelSpy(QObject *parent);
39 
40     void setModel(QAbstractItemModel *model);
setLazyPersistence(bool lazy)41     void setLazyPersistence(bool lazy)
42     {
43         m_lazyPersist = lazy;
44     }
45 
46     void preTestPersistIndexes(const PersistentChangeList &changeList);
getUnchangedIndexes()47     QModelIndexList getUnchangedIndexes() const
48     {
49         return m_unchangedIndexes;
50     }
getUnchangedPersistentIndexes()51     QList<QPersistentModelIndex> getUnchangedPersistentIndexes() const
52     {
53         return m_unchangedPersistentIndexes;
54     }
getChangeList()55     PersistentChangeList getChangeList() const
56     {
57         return m_changeList;
58     }
59 
60     void startSpying();
61     void stopSpying();
isSpying()62     bool isSpying()
63     {
64         return m_isSpying;
65     }
66 
67     void clearTestData();
68 
69 protected Q_SLOTS:
70     void rowsAboutToBeInserted(const QModelIndex &parent, int start, int end);
71     void rowsInserted(const QModelIndex &parent, int start, int end);
72     void rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end);
73     void rowsRemoved(const QModelIndex &parent, int start, int end);
74     void rowsAboutToBeMoved(const QModelIndex &srcParent, int start, int end, const QModelIndex &destParent, int destStart);
75     void rowsMoved(const QModelIndex &srcParent, int start, int end, const QModelIndex &destParent, int destStart);
76 
77     void layoutAboutToBeChanged();
78     void layoutChanged();
79 
80     void modelAboutToBeReset();
81     void modelReset();
82 
83     void modelDestroyed();
84 
85     void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight);
86 
87 private:
88     void doPersist();
89     QModelIndexList getUnchangedIndexes(const QModelIndex &parent, const QList<QItemSelectionRange> &ignoredRanges);
90     QModelIndexList getDescendantIndexes(const QModelIndex &index);
91     QList<QPersistentModelIndex> toPersistent(const QModelIndexList &list);
92 
93 private:
94     QAbstractItemModel *m_model;
95     bool m_isSpying;
96     bool m_lazyPersist;
97     PersistentChangeList m_changeList;
98     QModelIndexList m_unchangedIndexes;
99     QList<QPersistentModelIndex> m_unchangedPersistentIndexes;
100 };
101 
102 PROXYMODELTESTSUITE_EXPORT uint qHash(const QVariant &var);
103 
104 PROXYMODELTESTSUITE_EXPORT QDebug operator<<(QDebug d, ModelSpy *modelSpy);
105 
106 #endif
107