2011-04-27 10:05:43 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
|
|
|
** Copyright (C) 2009 Stephen Kelly <steveire@gmail.com>
|
2016-01-15 12:36:27 +00:00
|
|
|
** Contact: https://www.qt.io/licensing/
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** This file is part of the test suite of the Qt Toolkit.
|
|
|
|
**
|
2016-01-15 12:36:27 +00:00
|
|
|
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
|
2012-09-19 12:28:29 +00:00
|
|
|
** Commercial License Usage
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2015-01-28 08:44:43 +00:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
2016-01-15 12:36:27 +00:00
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2012-09-19 12:28:29 +00:00
|
|
|
**
|
2016-01-15 12:36:27 +00:00
|
|
|
** GNU General Public License Usage
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef DYNAMICTREEMODEL_H
|
|
|
|
#define DYNAMICTREEMODEL_H
|
|
|
|
|
|
|
|
#include <QtCore/QAbstractItemModel>
|
|
|
|
|
|
|
|
#include <QtCore/QHash>
|
|
|
|
#include <QtCore/QList>
|
|
|
|
|
|
|
|
class DynamicTreeModel : public QAbstractItemModel
|
|
|
|
{
|
2017-01-16 15:17:26 +00:00
|
|
|
Q_OBJECT
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
public:
|
2017-01-16 15:17:26 +00:00
|
|
|
DynamicTreeModel(QObject *parent = 0);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2017-01-16 15:17:26 +00:00
|
|
|
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
|
|
|
|
QModelIndex parent(const QModelIndex &index) const;
|
|
|
|
int rowCount(const QModelIndex &index = QModelIndex()) const;
|
|
|
|
int columnCount(const QModelIndex &index = QModelIndex()) const;
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2017-01-16 15:17:26 +00:00
|
|
|
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2017-01-16 15:17:26 +00:00
|
|
|
void clear();
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
protected slots:
|
|
|
|
|
2017-01-16 15:17:26 +00:00
|
|
|
/**
|
|
|
|
Finds the parent id of the string with id @p searchId.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2017-01-16 15:17:26 +00:00
|
|
|
Returns -1 if not found.
|
|
|
|
*/
|
|
|
|
qint64 findParentId(qint64 searchId) const;
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
private:
|
2017-01-16 15:17:26 +00:00
|
|
|
QHash<qint64, QString> m_items;
|
|
|
|
QHash<qint64, QList<QList<qint64> > > m_childItems;
|
|
|
|
qint64 nextId;
|
|
|
|
qint64 newId()
|
|
|
|
{
|
|
|
|
return nextId++;
|
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex m_nextParentIndex;
|
|
|
|
int m_nextRow;
|
|
|
|
|
|
|
|
int m_depth;
|
|
|
|
int maxDepth;
|
|
|
|
|
|
|
|
friend class ModelInsertCommand;
|
|
|
|
friend class ModelMoveCommand;
|
|
|
|
friend class ModelResetCommand;
|
|
|
|
friend class ModelResetCommandFixed;
|
|
|
|
friend class ModelChangeChildrenLayoutsCommand;
|
2011-04-27 10:05:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class ModelChangeCommand : public QObject
|
|
|
|
{
|
2017-01-16 15:17:26 +00:00
|
|
|
Q_OBJECT
|
2011-04-27 10:05:43 +00:00
|
|
|
public:
|
|
|
|
|
2017-01-16 15:17:26 +00:00
|
|
|
ModelChangeCommand(DynamicTreeModel *model, QObject *parent = 0);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2017-01-16 15:17:26 +00:00
|
|
|
virtual ~ModelChangeCommand()
|
|
|
|
{
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2017-01-16 15:17:26 +00:00
|
|
|
void setAncestorRowNumbers(QList<int> rowNumbers)
|
|
|
|
{
|
|
|
|
m_rowNumbers = rowNumbers;
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2017-01-16 15:17:26 +00:00
|
|
|
QModelIndex findIndex(QList<int> rows);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2017-01-16 15:17:26 +00:00
|
|
|
void setStartRow(int row)
|
|
|
|
{
|
|
|
|
m_startRow = row;
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2017-01-16 15:17:26 +00:00
|
|
|
void setEndRow(int row)
|
|
|
|
{
|
|
|
|
m_endRow = row;
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2017-01-16 15:17:26 +00:00
|
|
|
void setNumCols(int cols)
|
|
|
|
{
|
|
|
|
m_numCols = cols;
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2017-01-16 15:17:26 +00:00
|
|
|
virtual void doCommand() = 0;
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
protected:
|
2017-01-16 15:17:26 +00:00
|
|
|
DynamicTreeModel *m_model;
|
|
|
|
QList<int> m_rowNumbers;
|
|
|
|
int m_numCols;
|
|
|
|
int m_startRow;
|
|
|
|
int m_endRow;
|
2011-04-27 10:05:43 +00:00
|
|
|
};
|
|
|
|
|
2017-01-16 15:17:26 +00:00
|
|
|
typedef QList<ModelChangeCommand *> ModelChangeCommandList;
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
class ModelInsertCommand : public ModelChangeCommand
|
|
|
|
{
|
2017-01-16 15:17:26 +00:00
|
|
|
Q_OBJECT
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2017-01-16 15:17:26 +00:00
|
|
|
ModelInsertCommand(DynamicTreeModel *model, QObject *parent = 0);
|
|
|
|
virtual ~ModelInsertCommand()
|
|
|
|
{
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2017-01-16 15:17:26 +00:00
|
|
|
virtual void doCommand();
|
2011-04-27 10:05:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class ModelMoveCommand : public ModelChangeCommand
|
|
|
|
{
|
2017-01-16 15:17:26 +00:00
|
|
|
Q_OBJECT
|
2011-04-27 10:05:43 +00:00
|
|
|
public:
|
2017-01-16 15:17:26 +00:00
|
|
|
ModelMoveCommand(DynamicTreeModel *model, QObject *parent);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2017-01-16 15:17:26 +00:00
|
|
|
virtual ~ModelMoveCommand()
|
|
|
|
{
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2017-01-16 15:17:26 +00:00
|
|
|
virtual bool emitPreSignal(const QModelIndex &srcParent, int srcStart, int srcEnd,
|
|
|
|
const QModelIndex &destParent, int destRow);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2017-01-16 15:17:26 +00:00
|
|
|
virtual void doCommand();
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2017-01-16 15:17:26 +00:00
|
|
|
virtual void emitPostSignal();
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2017-01-16 15:17:26 +00:00
|
|
|
void setDestAncestors(QList<int> rows)
|
|
|
|
{
|
|
|
|
m_destRowNumbers = rows;
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2017-01-16 15:17:26 +00:00
|
|
|
void setDestRow(int row)
|
|
|
|
{
|
|
|
|
m_destRow = row;
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
protected:
|
2017-01-16 15:17:26 +00:00
|
|
|
QList<int> m_destRowNumbers;
|
|
|
|
int m_destRow;
|
2011-04-27 10:05:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
A command which does a move and emits a reset signal.
|
|
|
|
*/
|
|
|
|
class ModelResetCommand : public ModelMoveCommand
|
|
|
|
{
|
2017-01-16 15:17:26 +00:00
|
|
|
Q_OBJECT
|
2011-04-27 10:05:43 +00:00
|
|
|
public:
|
2017-01-16 15:17:26 +00:00
|
|
|
ModelResetCommand(DynamicTreeModel *model, QObject *parent = 0);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2017-01-16 15:17:26 +00:00
|
|
|
virtual ~ModelResetCommand();
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2017-01-16 15:17:26 +00:00
|
|
|
virtual bool emitPreSignal(const QModelIndex &srcParent, int srcStart, int srcEnd,
|
|
|
|
const QModelIndex &destParent, int destRow);
|
|
|
|
virtual void emitPostSignal();
|
2011-04-27 10:05:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
A command which does a move and emits a beginResetModel and endResetModel signals.
|
|
|
|
*/
|
|
|
|
class ModelResetCommandFixed : public ModelMoveCommand
|
|
|
|
{
|
2017-01-16 15:17:26 +00:00
|
|
|
Q_OBJECT
|
2011-04-27 10:05:43 +00:00
|
|
|
public:
|
2017-01-16 15:17:26 +00:00
|
|
|
ModelResetCommandFixed(DynamicTreeModel *model, QObject *parent = 0);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2017-01-16 15:17:26 +00:00
|
|
|
virtual ~ModelResetCommandFixed();
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2017-01-16 15:17:26 +00:00
|
|
|
virtual bool emitPreSignal(const QModelIndex &srcParent, int srcStart, int srcEnd,
|
|
|
|
const QModelIndex &destParent, int destRow);
|
|
|
|
virtual void emitPostSignal();
|
2011-04-27 10:05:43 +00:00
|
|
|
};
|
|
|
|
|
2011-11-23 19:50:55 +00:00
|
|
|
class ModelChangeChildrenLayoutsCommand : public ModelChangeCommand
|
|
|
|
{
|
2017-01-16 15:17:26 +00:00
|
|
|
Q_OBJECT
|
2011-11-23 19:50:55 +00:00
|
|
|
public:
|
2017-01-16 15:17:26 +00:00
|
|
|
ModelChangeChildrenLayoutsCommand(DynamicTreeModel *model, QObject *parent);
|
2011-11-23 19:50:55 +00:00
|
|
|
|
2017-01-16 15:17:26 +00:00
|
|
|
virtual ~ModelChangeChildrenLayoutsCommand()
|
|
|
|
{
|
|
|
|
}
|
2011-11-23 19:50:55 +00:00
|
|
|
|
2017-01-16 15:17:26 +00:00
|
|
|
virtual void doCommand();
|
2011-11-23 19:50:55 +00:00
|
|
|
|
2017-01-16 15:17:26 +00:00
|
|
|
void setSecondAncestorRowNumbers(QList<int> rows)
|
|
|
|
{
|
|
|
|
m_secondRowNumbers = rows;
|
|
|
|
}
|
2011-11-23 19:50:55 +00:00
|
|
|
|
|
|
|
protected:
|
2017-01-16 15:17:26 +00:00
|
|
|
QList<int> m_secondRowNumbers;
|
|
|
|
int m_destRow;
|
2011-11-23 19:50:55 +00:00
|
|
|
};
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
#endif
|