File indexing completed on 2025-01-05 04:55:05

0001 /****************************************************************************
0002 **
0003 ** Copyright (C) 2016 The Qt Company Ltd.
0004 ** Contact: https://www.qt.io/licensing/
0005 **
0006 ** This file is part of the Qt Quick Controls module of the Qt Toolkit.
0007 **
0008 ** $QT_BEGIN_LICENSE:LGPL$
0009 ** Commercial License Usage
0010 ** Licensees holding valid commercial Qt licenses may use this file in
0011 ** accordance with the commercial license agreement provided with the
0012 ** Software or, alternatively, in accordance with the terms contained in
0013 ** a written agreement between you and The Qt Company. For licensing terms
0014 ** and conditions see https://www.qt.io/terms-conditions. For further
0015 ** information use the contact form at https://www.qt.io/contact-us.
0016 **
0017 ** GNU Lesser General Public License Usage
0018 ** Alternatively, this file may be used under the terms of the GNU Lesser
0019 ** General Public License version 3 as published by the Free Software
0020 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
0021 ** packaging of this file. Please review the following information to
0022 ** ensure the GNU Lesser General Public License version 3 requirements
0023 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
0024 **
0025 ** GNU General Public License Usage
0026 ** Alternatively, this file may be used under the terms of the GNU
0027 ** General Public License version 2.0 or (at your option) the GNU General
0028 ** Public license version 3 or any later version approved by the KDE Free
0029 ** Qt Foundation. The licenses are as published by the Free Software
0030 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
0031 ** included in the packaging of this file. Please review the following
0032 ** information to ensure the GNU General Public License requirements will
0033 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
0034 ** https://www.gnu.org/licenses/gpl-3.0.html.
0035 **
0036 ** $QT_END_LICENSE$
0037 **
0038 ****************************************************************************/
0039 
0040 #ifndef QQUICKTREEMODELADAPTOR_H
0041 #define QQUICKTREEMODELADAPTOR_H
0042 
0043 //
0044 //  W A R N I N G
0045 //  -------------
0046 //
0047 // This file is not part of the Qt API.  It exists purely as an
0048 // implementation detail.  This header file may change from version to
0049 // version without notice, or even be removed.
0050 //
0051 // We mean it.
0052 //
0053 
0054 #include "kube_export.h"
0055 
0056 #include <QSet>
0057 #include <QPointer>
0058 #include <QAbstractItemModel>
0059 #include <QItemSelectionModel>
0060 
0061 class QAbstractItemModel;
0062 
0063 class KUBE_EXPORT QQuickTreeModelAdaptor1 : public QAbstractListModel
0064 {
0065     Q_OBJECT
0066     Q_PROPERTY(QAbstractItemModel *model READ model WRITE setModel NOTIFY modelChanged)
0067     Q_PROPERTY(QModelIndex rootIndex READ rootIndex WRITE setRootIndex RESET resetRootIndex NOTIFY rootIndexChanged)
0068 
0069     struct TreeItem;
0070 
0071 public:
0072     explicit QQuickTreeModelAdaptor1(QObject *parent = 0);
0073 
0074     QAbstractItemModel *model() const;
0075     const QModelIndex &rootIndex() const;
0076     void setRootIndex(const QModelIndex &idx);
0077     void resetRootIndex();
0078 
0079     enum {
0080         DepthRole = Qt::UserRole - 5,
0081         ExpandedRole,
0082         HasChildrenRole,
0083         HasSiblingRole,
0084         ModelIndexRole
0085     };
0086 
0087     QHash<int, QByteArray> roleNames() const;
0088     int rowCount(const QModelIndex &parent = QModelIndex()) const;
0089     QVariant data(const QModelIndex &, int role) const;
0090     bool setData(const QModelIndex &index, const QVariant &value, int role);
0091 
0092     void clearModelData();
0093 
0094     bool isVisible(const QModelIndex &index);
0095     bool childrenVisible(const QModelIndex &index);
0096 
0097     const QModelIndex &mapToModel(const QModelIndex &index) const;
0098     Q_INVOKABLE QModelIndex mapRowToModelIndex(int row) const;
0099 
0100     Q_INVOKABLE QItemSelection selectionForRowRange(const QModelIndex &fromIndex, const QModelIndex &toIndex) const;
0101 
0102     void showModelTopLevelItems(bool doInsertRows = true);
0103     void showModelChildItems(const TreeItem &parent, int start, int end, bool doInsertRows = true, bool doExpandPendingRows = true);
0104 
0105     int itemIndex(const QModelIndex &index) const;
0106     void expandPendingRows(bool doInsertRows = true);
0107     int lastChildIndex(const QModelIndex &index);
0108     void removeVisibleRows(int startIndex, int endIndex, bool doRemoveRows = true);
0109 
0110     void expandRow(int n);
0111     void collapseRow(int n);
0112     bool isExpanded(int row) const;
0113 
0114     Q_INVOKABLE bool isExpanded(const QModelIndex &) const;
0115 
0116     void dump() const;
0117     bool testConsistency(bool dumpOnFail = false) const;
0118 
0119 signals:
0120     void modelChanged(QAbstractItemModel *model);
0121     void rootIndexChanged();
0122     void expanded(const QModelIndex &index);
0123     void collapsed(const QModelIndex &index);
0124 
0125 public slots:
0126     void expand(const QModelIndex &);
0127     void collapse(const QModelIndex &);
0128 
0129     void setModel(QAbstractItemModel *model);
0130 
0131 private slots:
0132     void modelHasBeenDestroyed();
0133     void modelHasBeenReset();
0134     void modelDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRigth, const QVector<int> &roles);
0135     void modelLayoutAboutToBeChanged(const QList<QPersistentModelIndex> &parents, QAbstractItemModel::LayoutChangeHint hint);
0136     void modelLayoutChanged(const QList<QPersistentModelIndex> &parents, QAbstractItemModel::LayoutChangeHint hint);
0137     void modelRowsAboutToBeInserted(const QModelIndex & parent, int start, int end);
0138     void modelRowsAboutToBeMoved(const QModelIndex & sourceParent, int sourceStart, int sourceEnd, const QModelIndex & destinationParent, int destinationRow);
0139     void modelRowsAboutToBeRemoved(const QModelIndex & parent, int start, int end);
0140     void modelRowsInserted(const QModelIndex & parent, int start, int end);
0141     void modelRowsMoved(const QModelIndex & sourceParent, int sourceStart, int sourceEnd, const QModelIndex & destinationParent, int destinationRow);
0142     void modelRowsRemoved(const QModelIndex & parent, int start, int end);
0143 
0144 private:
0145     struct TreeItem {
0146         QPersistentModelIndex index;
0147         int depth;
0148         bool expanded;
0149 
0150         explicit TreeItem(const QModelIndex &idx = QModelIndex(), int d = 0, int e = false)
0151             : index(idx), depth(d), expanded(e)
0152         { }
0153 
0154         inline bool operator== (const TreeItem &other) const
0155         {
0156             return this->index == other.index;
0157         }
0158     };
0159 
0160     QPointer<QAbstractItemModel> m_model;
0161     QPersistentModelIndex m_rootIndex;
0162     QList<TreeItem> m_items;
0163     QSet<QPersistentModelIndex> m_expandedItems;
0164     QList<TreeItem *> m_itemsToExpand;
0165     mutable int m_lastItemIndex;
0166 };
0167 
0168 #endif // QQUICKTREEMODELADAPTOR_H