1 /****************************************************************************
2 **
3 ** Copyright (C) 2009 Stephen Kelly <steveire@gmail.com>
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21 ** included in the packaging of this file. Please review the following
22 ** information to ensure the GNU General Public License requirements will
23 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 **
25 ** $QT_END_LICENSE$
26 **
27 ****************************************************************************/
28 #include "dynamictreemodel.h"
29 #include <QtCore/QHash>
30 #include <QtCore/QList>
31 #include <QtCore/QTimer>
32 #include <QtCore/QDebug>
DynamicTreeModel(QObject * parent)33 DynamicTreeModel::DynamicTreeModel( QObject *parent ) :
34   QAbstractItemModel( parent ),
35   nextId( 1 )
36 {
37 }
index(int row,int column,const QModelIndex & parent) const38 QModelIndex DynamicTreeModel::index( int row, int column, const QModelIndex &parent ) const
39 {
40 //   if (column != 0)
41 //     return QModelIndex();
42   if ( column < 0 || row < 0 )
43     return QModelIndex();
44   QList<QList<qint64> > childIdColumns = m_childItems.value( parent.internalId() );
45   const qint64 grandParent = findParentId( parent.internalId() );
46   if ( grandParent >= 0 )
47   {
48     QList<QList<qint64> > parentTable = m_childItems.value( grandParent );
49     if ( parent.column() >= parentTable.size() )
50       qFatal( "%s: parent.column() must be less than parentTable.size()", Q_FUNC_INFO );
51     QList<qint64> parentSiblings = parentTable.at( parent.column() );
52     if ( parent.row() >= parentSiblings.size() )
53       qFatal( "%s: parent.row() must be less than parentSiblings.size()", Q_FUNC_INFO );
54   }
55   if ( childIdColumns.size() == 0 )
56     return QModelIndex();
57   if ( column >= childIdColumns.size() )
58     return QModelIndex();
59   QList<qint64> rowIds = childIdColumns.at( column );
60   if ( row >= rowIds.size() )
61     return QModelIndex();
62   qint64 id = rowIds.at( row );
63   return createIndex( row, column, reinterpret_cast<void *>( id ) );
64 }
findParentId(qint64 searchId) const65 qint64 DynamicTreeModel::findParentId( qint64 searchId ) const
66 {
67   if ( searchId <= 0 )
68     return -1;
69   for ( auto i = m_childItems.cbegin(), end = m_childItems.cend(); i != end; ++i )
70   {
71     for ( const auto &list : i.value() )
72     {
73       if ( list.contains( searchId ) )
74         return i.key();
75     }
76   }
77   return -1;
78 }
parent(const QModelIndex & index) const79 QModelIndex DynamicTreeModel::parent( const QModelIndex &index ) const
80 {
81   if ( !index.isValid() )
82     return QModelIndex();
83   qint64 searchId = index.internalId();
84   qint64 parentId = findParentId( searchId );
85   // Will never happen for valid index, but what the hey...
86   if ( parentId <= 0 )
87     return QModelIndex();
88   qint64 grandParentId = findParentId( parentId );
89   if ( grandParentId < 0 )
90     grandParentId = 0;
91   int column = 0;
92   QList<qint64> childList = m_childItems.value( grandParentId ).at( column );
93   int row = childList.indexOf( parentId );
94   return createIndex( row, column, reinterpret_cast<void *>( parentId ) );
95 }
rowCount(const QModelIndex & index) const96 int DynamicTreeModel::rowCount( const QModelIndex &index ) const
97 {
98   QList<QList<qint64> > cols = m_childItems.value( index.internalId() );
99   if ( cols.size() == 0 )
100     return 0;
101   if ( index.column() > 0 )
102     return 0;
103   return cols.at( 0 ).size();
104 }
columnCount(const QModelIndex & index) const105 int DynamicTreeModel::columnCount( const QModelIndex &index ) const
106 {
107 //   Q_UNUSED(index);
108   return m_childItems.value( index.internalId() ).size();
109 }
data(const QModelIndex & index,int role) const110 QVariant DynamicTreeModel::data( const QModelIndex &index, int role ) const
111 {
112   if ( !index.isValid() )
113     return QVariant();
114   if ( Qt::DisplayRole == role )
115     return m_items.value( index.internalId() );
116   return QVariant();
117 }
clear()118 void DynamicTreeModel::clear()
119 {
120   beginResetModel();
121   m_items.clear();
122   m_childItems.clear();
123   nextId = 1;
124   endResetModel();
125 }
ModelChangeCommand(DynamicTreeModel * model,QObject * parent)126 ModelChangeCommand::ModelChangeCommand( DynamicTreeModel *model, QObject *parent ) :
127   QObject( parent ),
128   m_model( model ),
129   m_numCols( 1 ),
130   m_startRow( -1 ),
131   m_endRow( -1 )
132 {
133 }
findIndex(const QList<int> & rows) const134 QModelIndex ModelChangeCommand::findIndex( const QList<int> &rows ) const
135 {
136   const int col = 0;
137   QModelIndex parent = QModelIndex();
138   for ( int row : rows )
139   {
140     parent = m_model->index( row, col, parent );
141     if ( !parent.isValid() )
142       qFatal( "%s: parent must be valid", Q_FUNC_INFO );
143   }
144   return parent;
145 }
ModelInsertCommand(DynamicTreeModel * model,QObject * parent)146 ModelInsertCommand::ModelInsertCommand( DynamicTreeModel *model, QObject *parent ) :
147   ModelChangeCommand( model, parent )
148 {
149 }
doCommand()150 void ModelInsertCommand::doCommand()
151 {
152   QModelIndex parent = findIndex( m_rowNumbers );
153   m_model->beginInsertRows( parent, m_startRow, m_endRow );
154   qint64 parentId = parent.internalId();
155   for ( int row = m_startRow; row <= m_endRow; row++ )
156   {
157     for ( int col = 0; col < m_numCols; col++ )
158     {
159       if ( m_model->m_childItems[parentId].size() <= col )
160         m_model->m_childItems[parentId].append( QList<qint64>() );
161 //       QString name = QUuid::createUuid().toString();
162       qint64 id = m_model->newId();
163       QString name = QString::number( id );
164       m_model->m_items.insert( id, name );
165       m_model->m_childItems[parentId][col].insert( row, id );
166     }
167   }
168   m_model->endInsertRows();
169 }
ModelMoveCommand(DynamicTreeModel * model,QObject * parent)170 ModelMoveCommand::ModelMoveCommand( DynamicTreeModel *model, QObject *parent ) :
171   ModelChangeCommand( model, parent )
172 {
173 }
emitPreSignal(const QModelIndex & srcParent,int srcStart,int srcEnd,const QModelIndex & destParent,int destRow)174 bool ModelMoveCommand::emitPreSignal( const QModelIndex &srcParent, int srcStart, int srcEnd,
175                                       const QModelIndex &destParent, int destRow )
176 {
177   return m_model->beginMoveRows( srcParent, srcStart, srcEnd, destParent, destRow );
178 }
doCommand()179 void ModelMoveCommand::doCommand()
180 {
181   QModelIndex srcParent = findIndex( m_rowNumbers );
182   QModelIndex destParent = findIndex( m_destRowNumbers );
183   if ( !emitPreSignal( srcParent, m_startRow, m_endRow, destParent, m_destRow ) )
184     return;
185   for ( int column = 0; column < m_numCols; ++column )
186   {
187     const QList<qint64> l = m_model->m_childItems.value( srcParent.internalId() )[column].mid(
188                               m_startRow, m_endRow - m_startRow + 1 );
189     for ( int i = m_startRow; i <= m_endRow; i++ )
190       m_model->m_childItems[srcParent.internalId()][column].removeAt( m_startRow );
191     int d;
192     if ( m_destRow < m_startRow )
193     {
194       d = m_destRow;
195     }
196     else
197     {
198       if ( srcParent == destParent )
199         d = m_destRow - ( m_endRow - m_startRow + 1 );
200       else
201         d = m_destRow - ( m_endRow - m_startRow ) + 1;
202     }
203     for ( qint64 id : l )
204       m_model->m_childItems[destParent.internalId()][column].insert( d++, id );
205   }
206   emitPostSignal();
207 }
emitPostSignal()208 void ModelMoveCommand::emitPostSignal()
209 {
210   m_model->endMoveRows();
211 }
ModelResetCommand(DynamicTreeModel * model,QObject * parent)212 ModelResetCommand::ModelResetCommand( DynamicTreeModel *model, QObject *parent ) :
213   ModelMoveCommand( model, parent )
214 {
215 }
emitPreSignal(const QModelIndex & srcParent,int srcStart,int srcEnd,const QModelIndex & destParent,int destRow)216 bool ModelResetCommand::emitPreSignal( const QModelIndex &srcParent, int srcStart, int srcEnd,
217                                        const QModelIndex &destParent, int destRow )
218 {
219   Q_UNUSED( srcParent );
220   Q_UNUSED( srcStart );
221   Q_UNUSED( srcEnd );
222   Q_UNUSED( destParent );
223   Q_UNUSED( destRow );
224   return true;
225 }
emitPostSignal()226 void ModelResetCommand::emitPostSignal()
227 {
228   m_model->beginResetModel();
229   m_model->endResetModel();
230 }
ModelResetCommandFixed(DynamicTreeModel * model,QObject * parent)231 ModelResetCommandFixed::ModelResetCommandFixed( DynamicTreeModel *model, QObject *parent ) :
232   ModelMoveCommand( model, parent )
233 {
234 }
emitPreSignal(const QModelIndex & srcParent,int srcStart,int srcEnd,const QModelIndex & destParent,int destRow)235 bool ModelResetCommandFixed::emitPreSignal( const QModelIndex &srcParent, int srcStart, int srcEnd,
236     const QModelIndex &destParent, int destRow )
237 {
238   Q_UNUSED( srcParent );
239   Q_UNUSED( srcStart );
240   Q_UNUSED( srcEnd );
241   Q_UNUSED( destParent );
242   Q_UNUSED( destRow );
243   m_model->beginResetModel();
244   return true;
245 }
emitPostSignal()246 void ModelResetCommandFixed::emitPostSignal()
247 {
248   m_model->endResetModel();
249 }
ModelChangeChildrenLayoutsCommand(DynamicTreeModel * model,QObject * parent)250 ModelChangeChildrenLayoutsCommand::ModelChangeChildrenLayoutsCommand( DynamicTreeModel *model,
251     QObject *parent ) :
252   ModelChangeCommand( model, parent )
253 {
254 }
doCommand()255 void ModelChangeChildrenLayoutsCommand::doCommand()
256 {
257   const QPersistentModelIndex parent1 = findIndex( m_rowNumbers );
258   const QPersistentModelIndex parent2 = findIndex( m_secondRowNumbers );
259   QList<QPersistentModelIndex> parents;
260   parents << parent1;
261   parents << parent2;
262   emit m_model->layoutAboutToBeChanged( parents );
263   int rowSize1 = -1;
264   int rowSize2 = -1;
265   for ( int column = 0; column < m_numCols; ++column )
266   {
267     {
268       QList<qint64> &l = m_model->m_childItems[parent1.internalId()][column];
269       rowSize1 = l.size();
270       l.prepend( l.takeLast() );
271     }
272     {
273       QList<qint64> &l = m_model->m_childItems[parent2.internalId()][column];
274       rowSize2 = l.size();
275       l.append( l.takeFirst() );
276     }
277   }
278   // If we're changing one of the parent indexes, we need to ensure that we do that before
279   // changing any children of that parent. The reason is that we're keeping parent1 and parent2
280   // around as QPersistentModelIndex instances, and we query idx.parent() in the loop.
281   QModelIndexList persistent = m_model->persistentIndexList();
282   for ( const QPersistentModelIndex &parent : parents )
283   {
284     int idx = persistent.indexOf( parent );
285     if ( idx != -1 )
286       persistent.move( idx, 0 );
287   }
288   for ( const QModelIndex &idx : persistent )
289   {
290     if ( idx.parent() == parent1 )
291     {
292       if ( idx.row() == rowSize1 - 1 )
293       {
294         m_model->changePersistentIndex( idx,
295                                         m_model->createIndex( 0, idx.column(),
296                                             idx.internalPointer() ) );
297       }
298       else
299       {
300         m_model->changePersistentIndex( idx,
301                                         m_model->createIndex( idx.row() + 1, idx.column(),
302                                             idx.internalPointer() ) );
303       }
304     }
305     else if ( idx.parent() == parent2 )
306     {
307       if ( idx.row() == 0 )
308       {
309         m_model->changePersistentIndex( idx,
310                                         m_model->createIndex( rowSize2 - 1, idx.column(),
311                                             idx.internalPointer() ) );
312       }
313       else
314       {
315         m_model->changePersistentIndex( idx,
316                                         m_model->createIndex( idx.row() - 1, idx.column(),
317                                             idx.internalPointer() ) );
318       }
319     }
320   }
321   emit m_model->layoutChanged( parents );
322 }
323