dynamictreemodel.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2009 Stephen Kelly <steveire@gmail.com>
  4. ** All rights reserved.
  5. ** Contact: Nokia Corporation (qt-info@nokia.com)
  6. **
  7. ** This file is part of the test suite of the Qt Toolkit.
  8. **
  9. ** This file is free software: you can redistribute it and/or modify
  10. ** it under the terms of the GNU General Public License as published by
  11. ** the Free Software Foundation, either version 3 of the License, or
  12. ** (at your option) any later version.
  13. **
  14. ** $QT_BEGIN_LICENSE:LGPL$
  15. ** GNU Lesser General Public License Usage
  16. ** This file may be used under the terms of the GNU Lesser General Public
  17. ** License version 2.1 as published by the Free Software Foundation and
  18. ** appearing in the file LICENSE.LGPL included in the packaging of this
  19. ** file. Please review the following information to ensure the GNU Lesser
  20. ** General Public License version 2.1 requirements will be met:
  21. ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
  22. **
  23. ** In addition, as a special exception, Nokia gives you certain additional
  24. ** rights. These rights are described in the Nokia Qt LGPL Exception
  25. ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
  26. **
  27. ** GNU General Public License Usage
  28. ** Alternatively, this file may be used under the terms of the GNU General
  29. ** Public License version 3.0 as published by the Free Software Foundation
  30. ** and appearing in the file LICENSE.GPL included in the packaging of this
  31. ** file. Please review the following information to ensure the GNU General
  32. ** Public License version 3.0 requirements will be met:
  33. ** http://www.gnu.org/copyleft/gpl.html.
  34. **
  35. ** Other Usage
  36. ** Alternatively, this file may be used in accordance with the terms and
  37. ** conditions contained in a signed written agreement between you and Nokia.
  38. **
  39. **
  40. **
  41. **
  42. **
  43. ** $QT_END_LICENSE$
  44. **
  45. ****************************************************************************/
  46. #ifndef DYNAMICTREEMODEL_H
  47. #define DYNAMICTREEMODEL_H
  48. #include <QtCore/QAbstractItemModel>
  49. #include <QtCore/QHash>
  50. #include <QtCore/QList>
  51. class DynamicTreeModel : public QAbstractItemModel
  52. {
  53. Q_OBJECT
  54. public:
  55. DynamicTreeModel( QObject *parent = 0 );
  56. QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const;
  57. QModelIndex parent( const QModelIndex &index ) const;
  58. int rowCount( const QModelIndex &index = QModelIndex() ) const;
  59. int columnCount( const QModelIndex &index = QModelIndex() ) const;
  60. QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const;
  61. void clear();
  62. protected slots:
  63. /**
  64. Finds the parent id of the string with id @p searchId.
  65. Returns -1 if not found.
  66. */
  67. qint64 findParentId( qint64 searchId ) const;
  68. private:
  69. QHash<qint64, QString> m_items;
  70. QHash<qint64, QList<QList<qint64>>> m_childItems;
  71. qint64 nextId;
  72. qint64 newId()
  73. {
  74. return nextId++;
  75. };
  76. QModelIndex m_nextParentIndex;
  77. int m_nextRow;
  78. int m_depth;
  79. int maxDepth;
  80. friend class ModelInsertCommand;
  81. friend class ModelMoveCommand;
  82. friend class ModelResetCommand;
  83. friend class ModelResetCommandFixed;
  84. };
  85. class ModelChangeCommand : public QObject
  86. {
  87. Q_OBJECT
  88. public:
  89. ModelChangeCommand( DynamicTreeModel *model, QObject *parent = 0 );
  90. virtual ~ModelChangeCommand() {}
  91. void setAncestorRowNumbers( QList<int> rowNumbers )
  92. {
  93. m_rowNumbers = rowNumbers;
  94. }
  95. QModelIndex findIndex( QList<int> rows );
  96. void setStartRow( int row )
  97. {
  98. m_startRow = row;
  99. }
  100. void setEndRow( int row )
  101. {
  102. m_endRow = row;
  103. }
  104. void setNumCols( int cols )
  105. {
  106. m_numCols = cols;
  107. }
  108. virtual void doCommand() = 0;
  109. protected:
  110. DynamicTreeModel *m_model;
  111. QList<int> m_rowNumbers;
  112. int m_numCols;
  113. int m_startRow;
  114. int m_endRow;
  115. };
  116. typedef QList<ModelChangeCommand *> ModelChangeCommandList;
  117. class ModelInsertCommand : public ModelChangeCommand
  118. {
  119. Q_OBJECT
  120. public:
  121. ModelInsertCommand( DynamicTreeModel *model, QObject *parent = 0 );
  122. virtual ~ModelInsertCommand() {}
  123. virtual void doCommand();
  124. };
  125. class ModelMoveCommand : public ModelChangeCommand
  126. {
  127. Q_OBJECT
  128. public:
  129. ModelMoveCommand( DynamicTreeModel *model, QObject *parent );
  130. virtual ~ModelMoveCommand() {}
  131. virtual bool emitPreSignal( const QModelIndex &srcParent, int srcStart, int srcEnd, const QModelIndex &destParent, int destRow );
  132. virtual void doCommand();
  133. virtual void emitPostSignal();
  134. void setDestAncestors( QList<int> rows )
  135. {
  136. m_destRowNumbers = rows;
  137. }
  138. void setDestRow( int row )
  139. {
  140. m_destRow = row;
  141. }
  142. protected:
  143. QList<int> m_destRowNumbers;
  144. int m_destRow;
  145. };
  146. /**
  147. A command which does a move and emits a reset signal.
  148. */
  149. class ModelResetCommand : public ModelMoveCommand
  150. {
  151. Q_OBJECT
  152. public:
  153. ModelResetCommand( DynamicTreeModel *model, QObject *parent = 0 );
  154. virtual ~ModelResetCommand();
  155. virtual bool emitPreSignal( const QModelIndex &srcParent, int srcStart, int srcEnd, const QModelIndex &destParent, int destRow );
  156. virtual void emitPostSignal();
  157. };
  158. /**
  159. A command which does a move and emits a beginResetModel and endResetModel signals.
  160. */
  161. class ModelResetCommandFixed : public ModelMoveCommand
  162. {
  163. Q_OBJECT
  164. public:
  165. ModelResetCommandFixed( DynamicTreeModel *model, QObject *parent = 0 );
  166. virtual ~ModelResetCommandFixed();
  167. virtual bool emitPreSignal( const QModelIndex &srcParent, int srcStart, int srcEnd, const QModelIndex &destParent, int destRow );
  168. virtual void emitPostSignal();
  169. };
  170. #endif