| 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 test suite of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
| 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 General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
| 21 | ** included in the packaging of this file. Please review the following |
| 22 | ** information to ensure the GNU General Public License requirements will |
| 23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 24 | ** |
| 25 | ** $QT_END_LICENSE$ |
| 26 | ** |
| 27 | ****************************************************************************/ |
| 28 | |
| 29 | #ifndef TESTTYPES_H |
| 30 | #define TESTTYPES_H |
| 31 | |
| 32 | #include <QtCore/qabstractitemmodel.h> |
| 33 | #include <QtCore/qitemselectionmodel.h> |
| 34 | #include "qdebug.h" |
| 35 | |
| 36 | class ItemModelsTest : public QObject |
| 37 | { |
| 38 | Q_OBJECT |
| 39 | |
| 40 | Q_PROPERTY(QAbstractItemModel *model READ model WRITE setModel NOTIFY modelChanged) |
| 41 | Q_PROPERTY(QModelIndex modelIndex READ modelIndex WRITE setModelIndex NOTIFY changed) |
| 42 | Q_PROPERTY(QPersistentModelIndex persistentModelIndex READ persistentModelIndex WRITE setPersistentModelIndex NOTIFY changed) |
| 43 | Q_PROPERTY(QModelIndexList modelIndexList READ modelIndexList WRITE setModelIndexList NOTIFY changed) |
| 44 | Q_PROPERTY(QItemSelection itemSelection READ itemSelection WRITE setItemSelection NOTIFY changed) |
| 45 | |
| 46 | public: |
| 47 | ItemModelsTest(QObject *parent = 0) |
| 48 | : QObject(parent) |
| 49 | , m_model(0) |
| 50 | { |
| 51 | } |
| 52 | |
| 53 | QModelIndex modelIndex() const |
| 54 | { |
| 55 | return m_modelIndex; |
| 56 | } |
| 57 | |
| 58 | QPersistentModelIndex persistentModelIndex() const |
| 59 | { |
| 60 | return m_persistentModelIndex; |
| 61 | } |
| 62 | |
| 63 | QModelIndexList modelIndexList() |
| 64 | { |
| 65 | static bool firstTime = true; |
| 66 | if (firstTime && m_model && m_modelIndexList.isEmpty()) { |
| 67 | firstTime = false; |
| 68 | for (int i = 0; i < m_model->rowCount(); i++) |
| 69 | m_modelIndexList << m_model->index(row: i, column: 0); |
| 70 | } |
| 71 | return m_modelIndexList; |
| 72 | } |
| 73 | |
| 74 | Q_INVOKABLE QModelIndexList someModelIndexList() const |
| 75 | { |
| 76 | QModelIndexList list; |
| 77 | if (m_model) |
| 78 | for (int i = 0; i < m_model->rowCount(); i++) |
| 79 | list << m_model->index(row: i, column: 0); |
| 80 | return list; |
| 81 | } |
| 82 | |
| 83 | QItemSelection itemSelection() const |
| 84 | { |
| 85 | return m_itemSelection; |
| 86 | } |
| 87 | |
| 88 | void emitChanged() |
| 89 | { |
| 90 | emit changed(); |
| 91 | } |
| 92 | |
| 93 | void emitSignalWithModelIndex(const QModelIndex &index) |
| 94 | { |
| 95 | emit signalWithModelIndex(index); |
| 96 | } |
| 97 | |
| 98 | void emitSignalWithPersistentModelIndex(const QPersistentModelIndex &index) |
| 99 | { |
| 100 | emit signalWithPersistentModelIndex(index); |
| 101 | } |
| 102 | |
| 103 | QAbstractItemModel * model() const |
| 104 | { |
| 105 | return m_model; |
| 106 | } |
| 107 | |
| 108 | Q_INVOKABLE QModelIndex invalidModelIndex() const |
| 109 | { |
| 110 | return QModelIndex(); |
| 111 | } |
| 112 | |
| 113 | Q_INVOKABLE QItemSelectionRange createItemSelectionRange(const QModelIndex &tl, const QModelIndex &br) const |
| 114 | { |
| 115 | return QItemSelectionRange(tl, br); |
| 116 | } |
| 117 | |
| 118 | Q_INVOKABLE QItemSelection createItemSelection() |
| 119 | { |
| 120 | return QItemSelection(); |
| 121 | } |
| 122 | |
| 123 | Q_INVOKABLE QPersistentModelIndex createPersistentModelIndex(const QModelIndex &index) |
| 124 | { |
| 125 | return QPersistentModelIndex(index); |
| 126 | } |
| 127 | |
| 128 | public slots: |
| 129 | void setModelIndex(const QModelIndex &arg) |
| 130 | { |
| 131 | if (m_modelIndex == arg) |
| 132 | return; |
| 133 | |
| 134 | m_modelIndex = arg; |
| 135 | emit changed(); |
| 136 | } |
| 137 | |
| 138 | void setPersistentModelIndex(const QPersistentModelIndex &arg) |
| 139 | { |
| 140 | if (m_persistentModelIndex == arg) |
| 141 | return; |
| 142 | |
| 143 | m_persistentModelIndex = arg; |
| 144 | emit changed(); |
| 145 | } |
| 146 | |
| 147 | void setModel(QAbstractItemModel *arg) |
| 148 | { |
| 149 | if (m_model == arg) |
| 150 | return; |
| 151 | |
| 152 | m_model = arg; |
| 153 | emit modelChanged(arg); |
| 154 | } |
| 155 | |
| 156 | void setModelIndexList(QModelIndexList arg) |
| 157 | { |
| 158 | if (m_modelIndexList == arg) |
| 159 | return; |
| 160 | |
| 161 | m_modelIndexList = arg; |
| 162 | emit changed(); |
| 163 | } |
| 164 | |
| 165 | void setItemSelection(QItemSelection arg) |
| 166 | { |
| 167 | if (m_itemSelection == arg) |
| 168 | return; |
| 169 | |
| 170 | m_itemSelection = arg; |
| 171 | emit changed(); |
| 172 | } |
| 173 | |
| 174 | signals: |
| 175 | void changed(); |
| 176 | |
| 177 | void signalWithModelIndex(QModelIndex index); |
| 178 | void signalWithPersistentModelIndex(QPersistentModelIndex index); |
| 179 | |
| 180 | void modelChanged(QAbstractItemModel * arg); |
| 181 | |
| 182 | private: |
| 183 | QModelIndex m_modelIndex; |
| 184 | QPersistentModelIndex m_persistentModelIndex; |
| 185 | QAbstractItemModel *m_model; |
| 186 | QModelIndexList m_modelIndexList; |
| 187 | QItemSelection m_itemSelection; |
| 188 | }; |
| 189 | |
| 190 | #endif // TESTTYPES_H |
| 191 | |
| 192 |
