File indexing completed on 2024-05-19 16:37:09

0001 /*
0002     SPDX-FileCopyrightText: 2009 Stephen Kelly <steveire@gmail.com>
0003     SPDX-FileCopyrightText: 2009 Nokia Corporation and /or its subsidiary(-ies) <qt-info@nokia.com>
0004 
0005     This file is part of the test suite of the Qt Toolkit.
0006 
0007     SPDX-License-Identifier: LGPL-2.1-only WITH Qt-LGPL-exception-1.1
0008 */
0009 
0010 #ifndef DYNAMICTREEMODEL_H
0011 #define DYNAMICTREEMODEL_H
0012 
0013 #include <QAbstractItemModel>
0014 
0015 #include <QHash>
0016 #include <QList>
0017 
0018 class DynamicTreeModel : public QAbstractItemModel
0019 {
0020     Q_OBJECT
0021 
0022 public:
0023     DynamicTreeModel(QObject *parent = nullptr);
0024 
0025     QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
0026     QModelIndex parent(const QModelIndex &index) const;
0027     int rowCount(const QModelIndex &index = QModelIndex()) const;
0028     int columnCount(const QModelIndex &index = QModelIndex()) const;
0029 
0030     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
0031 
0032     void clear();
0033 
0034 protected Q_SLOTS:
0035 
0036     /**
0037     Finds the parent id of the string with id @p searchId.
0038 
0039     Returns -1 if not found.
0040     */
0041     qint64 findParentId(qint64 searchId) const;
0042 
0043 private:
0044     QHash<qint64, QString> m_items;
0045     QHash<qint64, QList<QList<qint64>>> m_childItems;
0046     qint64 nextId;
0047     qint64 newId()
0048     {
0049         return nextId++;
0050     };
0051 
0052     QModelIndex m_nextParentIndex;
0053     int m_nextRow;
0054 
0055     int m_depth;
0056     int maxDepth;
0057 
0058     friend class ModelInsertCommand;
0059     friend class ModelMoveCommand;
0060     friend class ModelResetCommand;
0061     friend class ModelResetCommandFixed;
0062 };
0063 
0064 class ModelChangeCommand : public QObject
0065 {
0066     Q_OBJECT
0067 public:
0068     explicit ModelChangeCommand(DynamicTreeModel *model, QObject *parent = nullptr);
0069 
0070     virtual ~ModelChangeCommand()
0071     {
0072     }
0073 
0074     void setAncestorRowNumbers(QList<int> rowNumbers)
0075     {
0076         m_rowNumbers = rowNumbers;
0077     }
0078 
0079     QModelIndex findIndex(QList<int> rows);
0080 
0081     void setStartRow(int row)
0082     {
0083         m_startRow = row;
0084     }
0085 
0086     void setEndRow(int row)
0087     {
0088         m_endRow = row;
0089     }
0090 
0091     void setNumCols(int cols)
0092     {
0093         m_numCols = cols;
0094     }
0095 
0096     virtual void doCommand() = 0;
0097 
0098 protected:
0099     DynamicTreeModel *m_model;
0100     QList<int> m_rowNumbers;
0101     int m_numCols;
0102     int m_startRow;
0103     int m_endRow;
0104 };
0105 
0106 typedef QList<ModelChangeCommand *> ModelChangeCommandList;
0107 
0108 class ModelInsertCommand : public ModelChangeCommand
0109 {
0110     Q_OBJECT
0111 
0112 public:
0113     explicit ModelInsertCommand(DynamicTreeModel *model, QObject *parent = nullptr);
0114     virtual ~ModelInsertCommand()
0115     {
0116     }
0117 
0118     void doCommand() override;
0119 };
0120 
0121 class ModelMoveCommand : public ModelChangeCommand
0122 {
0123     Q_OBJECT
0124 public:
0125     ModelMoveCommand(DynamicTreeModel *model, QObject *parent);
0126 
0127     virtual ~ModelMoveCommand()
0128     {
0129     }
0130 
0131     virtual bool emitPreSignal(const QModelIndex &srcParent, int srcStart, int srcEnd, const QModelIndex &destParent, int destRow);
0132 
0133     void doCommand() override;
0134 
0135     virtual void emitPostSignal();
0136 
0137     void setDestAncestors(QList<int> rows)
0138     {
0139         m_destRowNumbers = rows;
0140     }
0141 
0142     void setDestRow(int row)
0143     {
0144         m_destRow = row;
0145     }
0146 
0147 protected:
0148     QList<int> m_destRowNumbers;
0149     int m_destRow;
0150 };
0151 
0152 /**
0153   A command which does a move and emits a reset signal.
0154 */
0155 class ModelResetCommand : public ModelMoveCommand
0156 {
0157     Q_OBJECT
0158 public:
0159     explicit ModelResetCommand(DynamicTreeModel *model, QObject *parent = nullptr);
0160 
0161     virtual ~ModelResetCommand();
0162 
0163     bool emitPreSignal(const QModelIndex &srcParent, int srcStart, int srcEnd, const QModelIndex &destParent, int destRow) override;
0164     void emitPostSignal() override;
0165 };
0166 
0167 /**
0168   A command which does a move and emits a beginResetModel and endResetModel signals.
0169 */
0170 class ModelResetCommandFixed : public ModelMoveCommand
0171 {
0172     Q_OBJECT
0173 public:
0174     explicit ModelResetCommandFixed(DynamicTreeModel *model, QObject *parent = nullptr);
0175 
0176     virtual ~ModelResetCommandFixed();
0177 
0178     bool emitPreSignal(const QModelIndex &srcParent, int srcStart, int srcEnd, const QModelIndex &destParent, int destRow) override;
0179     void emitPostSignal() override;
0180 };
0181 
0182 #endif