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
61QT_BEGIN_NAMESPACE
62
63QT_REQUIRE_CONFIG(itemmodel);
64
65class QPersistentModelIndexData
66{
67public:
68 QPersistentModelIndexData() {}
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
76class Q_CORE_EXPORT QAbstractItemModelPrivate : public QObjectPrivate
77{
78 Q_DECLARE_PUBLIC(QAbstractItemModel)
79
80public:
81 QAbstractItemModelPrivate();
82 ~QAbstractItemModelPrivate();
83
84 void removePersistentIndexData(QPersistentModelIndexData *data);
85 void movePersistentIndexes(const QList<QPersistentModelIndexData *> &indexes, int change, const QModelIndex &parent,
86 Qt::Orientation orientation);
87 void rowsAboutToBeInserted(const QModelIndex &parent, int first, int last);
88 void rowsInserted(const QModelIndex &parent, int first, int last);
89 void rowsAboutToBeRemoved(const QModelIndex &parent, int first, int last);
90 void rowsRemoved(const QModelIndex &parent, int first, int last);
91 void columnsAboutToBeInserted(const QModelIndex &parent, int first, int last);
92 void columnsInserted(const QModelIndex &parent, int first, int last);
93 void columnsAboutToBeRemoved(const QModelIndex &parent, int first, int last);
94 void columnsRemoved(const QModelIndex &parent, int first, int last);
95 static QAbstractItemModel *staticEmptyModel();
96 static bool variantLessThan(const QVariant &v1, const QVariant &v2);
97
98 void itemsAboutToBeMoved(const QModelIndex &srcParent, int srcFirst, int srcLast, const QModelIndex &destinationParent, int destinationChild, Qt::Orientation);
99 void itemsMoved(const QModelIndex &srcParent, int srcFirst, int srcLast, const QModelIndex &destinationParent, int destinationChild, Qt::Orientation orientation);
100 bool allowMove(const QModelIndex &srcParent, int srcFirst, int srcLast, const QModelIndex &destinationParent, int destinationChild, Qt::Orientation orientation);
101
102 inline QModelIndex createIndex(int row, int column, void *data = nullptr) const {
103 return q_func()->createIndex(row, column, data);
104 }
105
106 inline QModelIndex createIndex(int row, int column, int id) const {
107 return q_func()->createIndex(row, column, id);
108 }
109
110 inline bool indexValid(const QModelIndex &index) const {
111 return (index.row() >= 0) && (index.column() >= 0) && (index.model() == q_func());
112 }
113
114 void invalidatePersistentIndexes();
115 void invalidatePersistentIndex(const QModelIndex &index);
116
117 struct Change {
118 constexpr Change() : parent(), first(-1), last(-1), needsAdjust(false) {}
119 constexpr Change(const QModelIndex &p, int f, int l) : parent(p), first(f), last(l), needsAdjust(false) {}
120
121 QModelIndex parent;
122 int first, last;
123
124
125 // In cases such as this:
126 // - A
127 // - B
128 // - C
129 // - - D
130 // - - E
131 // - - F
132 //
133 // If B is moved to above E, C is the source parent in the signal and its row is 2. When the move is
134 // completed however, C is at row 1 and there is no row 2 at the same level in the model at all.
135 // The QModelIndex is adjusted to correct that in those cases before reporting it though the
136 // rowsMoved signal.
137 bool needsAdjust;
138
139 constexpr bool isValid() const { return first >= 0 && last >= 0; }
140 };
141 QStack<Change> changes;
142
143 struct Persistent {
144 Persistent() {}
145 QMultiHash<QModelIndex, QPersistentModelIndexData *> indexes;
146 QStack<QList<QPersistentModelIndexData *>> moved;
147 QStack<QList<QPersistentModelIndexData *>> invalidated;
148 void insertMultiAtEnd(const QModelIndex& key, QPersistentModelIndexData *data);
149 } persistent;
150
151 static const QHash<int,QByteArray> &defaultRoleNames();
152 static bool isVariantLessThan(const QVariant &left, const QVariant &right,
153 Qt::CaseSensitivity cs = Qt::CaseSensitive, bool isLocaleAware = false);
154};
155Q_DECLARE_TYPEINFO(QAbstractItemModelPrivate::Change, Q_MOVABLE_TYPE);
156
157QT_END_NAMESPACE
158
159#endif // QABSTRACTITEMMODEL_P_H
160