dynamictreemodel.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. #include "dynamictreemodel.h"
  47. #include <QtCore/QHash>
  48. #include <QtCore/QList>
  49. #include <QtCore/QTimer>
  50. DynamicTreeModel::DynamicTreeModel( QObject *parent )
  51. : QAbstractItemModel( parent ), nextId( 1 )
  52. {
  53. }
  54. QModelIndex DynamicTreeModel::index( int row, int column, const QModelIndex &parent ) const
  55. {
  56. // if (column != 0)
  57. // return QModelIndex();
  58. if ( column < 0 || row < 0 )
  59. return QModelIndex();
  60. QList<QList<qint64>> childIdColumns = m_childItems.value( parent.internalId() );
  61. const qint64 grandParent = findParentId( parent.internalId() );
  62. if ( grandParent >= 0 )
  63. {
  64. QList<QList<qint64>> parentTable = m_childItems.value( grandParent );
  65. Q_ASSERT( parent.column() < parentTable.size() );
  66. QList<qint64> parentSiblings = parentTable.at( parent.column() );
  67. Q_ASSERT( parent.row() < parentSiblings.size() );
  68. }
  69. if ( childIdColumns.size() == 0 )
  70. return QModelIndex();
  71. if ( column >= childIdColumns.size() )
  72. return QModelIndex();
  73. QList<qint64> rowIds = childIdColumns.at( column );
  74. if ( row >= rowIds.size() )
  75. return QModelIndex();
  76. qint64 id = rowIds.at( row );
  77. return createIndex( row, column, reinterpret_cast<void *>( id ) );
  78. }
  79. qint64 DynamicTreeModel::findParentId( qint64 searchId ) const
  80. {
  81. if ( searchId <= 0 )
  82. return -1;
  83. QHashIterator<qint64, QList<QList<qint64>>> i( m_childItems );
  84. while ( i.hasNext() )
  85. {
  86. i.next();
  87. QListIterator<QList<qint64>> j( i.value() );
  88. while ( j.hasNext() )
  89. {
  90. QList<qint64> l = j.next();
  91. if ( l.contains( searchId ) )
  92. {
  93. return i.key();
  94. }
  95. }
  96. }
  97. return -1;
  98. }
  99. QModelIndex DynamicTreeModel::parent( const QModelIndex &index ) const
  100. {
  101. if ( !index.isValid() )
  102. return QModelIndex();
  103. qint64 searchId = index.internalId();
  104. qint64 parentId = findParentId( searchId );
  105. // Will never happen for valid index, but what the hey...
  106. if ( parentId <= 0 )
  107. return QModelIndex();
  108. qint64 grandParentId = findParentId( parentId );
  109. if ( grandParentId < 0 )
  110. grandParentId = 0;
  111. int column = 0;
  112. QList<qint64> childList = m_childItems.value( grandParentId ).at( column );
  113. int row = childList.indexOf( parentId );
  114. return createIndex( row, column, reinterpret_cast<void *>( parentId ) );
  115. }
  116. int DynamicTreeModel::rowCount( const QModelIndex &index ) const
  117. {
  118. QList<QList<qint64>> cols = m_childItems.value( index.internalId() );
  119. if ( cols.size() == 0 )
  120. return 0;
  121. if ( index.column() > 0 )
  122. return 0;
  123. return cols.at( 0 ).size();
  124. }
  125. int DynamicTreeModel::columnCount( const QModelIndex &index ) const
  126. {
  127. // Q_UNUSED(index);
  128. return m_childItems.value( index.internalId() ).size();
  129. }
  130. QVariant DynamicTreeModel::data( const QModelIndex &index, int role ) const
  131. {
  132. if ( !index.isValid() )
  133. return QVariant();
  134. if ( Qt::DisplayRole == role )
  135. {
  136. return m_items.value( index.internalId() );
  137. }
  138. return QVariant();
  139. }
  140. void DynamicTreeModel::clear()
  141. {
  142. beginResetModel();
  143. m_items.clear();
  144. m_childItems.clear();
  145. nextId = 1;
  146. endResetModel();
  147. }
  148. ModelChangeCommand::ModelChangeCommand( DynamicTreeModel *model, QObject *parent )
  149. : QObject( parent ), m_model( model ), m_numCols( 1 ), m_startRow( -1 ), m_endRow( -1 )
  150. {
  151. }
  152. QModelIndex ModelChangeCommand::findIndex( QList<int> rows )
  153. {
  154. const int col = 0;
  155. QModelIndex parent = QModelIndex();
  156. QListIterator<int> i( rows );
  157. while ( i.hasNext() )
  158. {
  159. parent = m_model->index( i.next(), col, parent );
  160. Q_ASSERT( parent.isValid() );
  161. }
  162. return parent;
  163. }
  164. ModelInsertCommand::ModelInsertCommand( DynamicTreeModel *model, QObject *parent )
  165. : ModelChangeCommand( model, parent )
  166. {
  167. }
  168. void ModelInsertCommand::doCommand()
  169. {
  170. QModelIndex parent = findIndex( m_rowNumbers );
  171. m_model->beginInsertRows( parent, m_startRow, m_endRow );
  172. qint64 parentId = parent.internalId();
  173. for ( int row = m_startRow; row <= m_endRow; row++ )
  174. {
  175. for ( int col = 0; col < m_numCols; col++ )
  176. {
  177. if ( m_model->m_childItems[parentId].size() <= col )
  178. {
  179. m_model->m_childItems[parentId].append( QList<qint64>() );
  180. }
  181. // QString name = QUuid::createUuid().toString();
  182. qint64 id = m_model->newId();
  183. QString name = QString::number( id );
  184. m_model->m_items.insert( id, name );
  185. m_model->m_childItems[parentId][col].insert( row, id );
  186. }
  187. }
  188. m_model->endInsertRows();
  189. }
  190. ModelMoveCommand::ModelMoveCommand( DynamicTreeModel *model, QObject *parent )
  191. : ModelChangeCommand( model, parent )
  192. {
  193. }
  194. bool ModelMoveCommand::emitPreSignal( const QModelIndex &srcParent, int srcStart, int srcEnd, const QModelIndex &destParent, int destRow )
  195. {
  196. return m_model->beginMoveRows( srcParent, srcStart, srcEnd, destParent, destRow );
  197. }
  198. void ModelMoveCommand::doCommand()
  199. {
  200. QModelIndex srcParent = findIndex( m_rowNumbers );
  201. QModelIndex destParent = findIndex( m_destRowNumbers );
  202. if ( !emitPreSignal( srcParent, m_startRow, m_endRow, destParent, m_destRow ) )
  203. {
  204. return;
  205. }
  206. for ( int column = 0; column < m_numCols; ++column )
  207. {
  208. QList<qint64> l = m_model->m_childItems.value( srcParent.internalId() )[column].mid( m_startRow, m_endRow - m_startRow + 1 );
  209. for ( int i = m_startRow; i <= m_endRow; i++ )
  210. {
  211. m_model->m_childItems[srcParent.internalId()][column].removeAt( m_startRow );
  212. }
  213. int d;
  214. if ( m_destRow < m_startRow )
  215. d = m_destRow;
  216. else
  217. {
  218. if ( srcParent == destParent )
  219. d = m_destRow - ( m_endRow - m_startRow + 1 );
  220. else
  221. d = m_destRow - ( m_endRow - m_startRow ) + 1;
  222. }
  223. foreach ( const qint64 id, l )
  224. {
  225. m_model->m_childItems[destParent.internalId()][column].insert( d++, id );
  226. }
  227. }
  228. emitPostSignal();
  229. }
  230. void ModelMoveCommand::emitPostSignal()
  231. {
  232. m_model->endMoveRows();
  233. }
  234. ModelResetCommand::ModelResetCommand( DynamicTreeModel *model, QObject *parent )
  235. : ModelMoveCommand( model, parent )
  236. {
  237. }
  238. ModelResetCommand::~ModelResetCommand()
  239. {
  240. }
  241. bool ModelResetCommand::emitPreSignal( const QModelIndex &srcParent, int srcStart, int srcEnd, const QModelIndex &destParent, int destRow )
  242. {
  243. Q_UNUSED( srcParent );
  244. Q_UNUSED( srcStart );
  245. Q_UNUSED( srcEnd );
  246. Q_UNUSED( destParent );
  247. Q_UNUSED( destRow );
  248. return true;
  249. }
  250. void ModelResetCommand::emitPostSignal()
  251. {
  252. m_model->reset();
  253. }
  254. ModelResetCommandFixed::ModelResetCommandFixed( DynamicTreeModel *model, QObject *parent )
  255. : ModelMoveCommand( model, parent )
  256. {
  257. }
  258. ModelResetCommandFixed::~ModelResetCommandFixed()
  259. {
  260. }
  261. bool ModelResetCommandFixed::emitPreSignal( const QModelIndex &srcParent, int srcStart, int srcEnd, const QModelIndex &destParent, int destRow )
  262. {
  263. Q_UNUSED( srcParent );
  264. Q_UNUSED( srcStart );
  265. Q_UNUSED( srcEnd );
  266. Q_UNUSED( destParent );
  267. Q_UNUSED( destRow );
  268. m_model->beginResetModel();
  269. return true;
  270. }
  271. void ModelResetCommandFixed::emitPostSignal()
  272. {
  273. m_model->endResetModel();
  274. }