1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #ifndef QABSTRACTITEMMODEL_P_H
41 #define QABSTRACTITEMMODEL_P_H
42 
43 //
44 //  W A R N I N G
45 //  -------------
46 //
47 // This file is not part of the Qt API.  It exists for the convenience
48 // of QAbstractItemModel*.  This header file may change from version
49 // to version without notice, or even be removed.
50 //
51 // We mean it.
52 //
53 //
54 
55 #include "QtCore/qabstractitemmodel.h"
56 #include "QtCore/private/qobject_p.h"
57 #include "QtCore/qstack.h"
58 #include "QtCore/qset.h"
59 #include "QtCore/qhash.h"
60 
61 QT_BEGIN_NAMESPACE
62 
63 QT_REQUIRE_CONFIG(itemmodel);
64 
65 class QPersistentModelIndexData
66 {
67 public:
QPersistentModelIndexData()68     QPersistentModelIndexData() {}
QPersistentModelIndexData(const QModelIndex & idx)69     QPersistentModelIndexData(const QModelIndex &idx) : index(idx) {}
70     QModelIndex index;
71     QAtomicInt ref;
72     static QPersistentModelIndexData *create(const QModelIndex &index);
73     static void destroy(QPersistentModelIndexData *data);
74 };
75 
76 class Q_CORE_EXPORT QAbstractItemModelPrivate : public QObjectPrivate
77 {
78     Q_DECLARE_PUBLIC(QAbstractItemModel)
79 
80 public:
81     QAbstractItemModelPrivate();
82     ~QAbstractItemModelPrivate();
83 
84     void removePersistentIndexData(QPersistentModelIndexData *data);
85     void movePersistentIndexes(const QVector<QPersistentModelIndexData *> &indexes, int change, const QModelIndex &parent, Qt::Orientation orientation);
86     void rowsAboutToBeInserted(const QModelIndex &parent, int first, int last);
87     void rowsInserted(const QModelIndex &parent, int first, int last);
88     void rowsAboutToBeRemoved(const QModelIndex &parent, int first, int last);
89     void rowsRemoved(const QModelIndex &parent, int first, int last);
90     void columnsAboutToBeInserted(const QModelIndex &parent, int first, int last);
91     void columnsInserted(const QModelIndex &parent, int first, int last);
92     void columnsAboutToBeRemoved(const QModelIndex &parent, int first, int last);
93     void columnsRemoved(const QModelIndex &parent, int first, int last);
94     static QAbstractItemModel *staticEmptyModel();
95     static bool variantLessThan(const QVariant &v1, const QVariant &v2);
96 
97     void itemsAboutToBeMoved(const QModelIndex &srcParent, int srcFirst, int srcLast, const QModelIndex &destinationParent, int destinationChild, Qt::Orientation);
98     void itemsMoved(const QModelIndex &srcParent, int srcFirst, int srcLast, const QModelIndex &destinationParent, int destinationChild, Qt::Orientation orientation);
99     bool allowMove(const QModelIndex &srcParent, int srcFirst, int srcLast, const QModelIndex &destinationParent, int destinationChild, Qt::Orientation orientation);
100 
101     inline QModelIndex createIndex(int row, int column, void *data = nullptr) const {
102         return q_func()->createIndex(row, column, data);
103     }
104 
createIndex(int row,int column,int id)105     inline QModelIndex createIndex(int row, int column, int id) const {
106         return q_func()->createIndex(row, column, id);
107     }
108 
indexValid(const QModelIndex & index)109     inline bool indexValid(const QModelIndex &index) const {
110          return (index.row() >= 0) && (index.column() >= 0) && (index.model() == q_func());
111     }
112 
113     void invalidatePersistentIndexes();
114     void invalidatePersistentIndex(const QModelIndex &index);
115 
116     struct Change {
ChangeChange117         Q_DECL_CONSTEXPR Change() : parent(), first(-1), last(-1), needsAdjust(false) {}
ChangeChange118         Q_DECL_CONSTEXPR Change(const QModelIndex &p, int f, int l) : parent(p), first(f), last(l), needsAdjust(false) {}
119 
120         QModelIndex parent;
121         int first, last;
122 
123 
124         // In cases such as this:
125         // - A
126         // - B
127         // - C
128         // - - D
129         // - - E
130         // - - F
131         //
132         // If B is moved to above E, C is the source parent in the signal and its row is 2. When the move is
133         // completed however, C is at row 1 and there is no row 2 at the same level in the model at all.
134         // The QModelIndex is adjusted to correct that in those cases before reporting it though the
135         // rowsMoved signal.
136         bool needsAdjust;
137 
isValidChange138         Q_DECL_CONSTEXPR bool isValid() const { return first >= 0 && last >= 0; }
139     };
140     QStack<Change> changes;
141 
142     struct Persistent {
PersistentPersistent143         Persistent() {}
144         QMultiHash<QModelIndex, QPersistentModelIndexData *> indexes;
145         QStack<QVector<QPersistentModelIndexData *> > moved;
146         QStack<QVector<QPersistentModelIndexData *> > invalidated;
147         void insertMultiAtEnd(const QModelIndex& key, QPersistentModelIndexData *data);
148     } persistent;
149 
150     Qt::DropActions supportedDragActions;
151 
152     QHash<int,QByteArray> roleNames;
153     static const QHash<int,QByteArray> &defaultRoleNames();
154     static bool isVariantLessThan(const QVariant &left, const QVariant &right,
155                                   Qt::CaseSensitivity cs = Qt::CaseSensitive, bool isLocaleAware = false);
156 };
157 Q_DECLARE_TYPEINFO(QAbstractItemModelPrivate::Change, Q_MOVABLE_TYPE);
158 
159 QT_END_NAMESPACE
160 
161 #endif // QABSTRACTITEMMODEL_P_H
162