File: pGenericTableModel.cpp

package info (click to toggle)
monkeystudio 1.9.0.4%2Bgit20161218-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 41,500 kB
  • ctags: 22,118
  • sloc: cpp: 144,671; ansic: 33,969; python: 2,922; makefile: 127; sh: 122; php: 73; cs: 69
file content (438 lines) | stat: -rw-r--r-- 12,891 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/****************************************************************************
    Copyright (C) 2005 - 2011  Filipe AZEVEDO & The Monkey Studio Team
    http://monkeystudio.org licensing under the GNU GPL.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
****************************************************************************/
#include "pGenericTableModel.h"

#include <QStringList>
#include <QDebug>

bool operator<( const QPoint& left, const QPoint& right )
{
    return qMakePair( left.x(), left.y() ) < qMakePair( right.x(), right.y() );
}

pGenericTableModel::pGenericTableModel( QObject* parent )
    : QAbstractTableModel( parent )
{
    mColumnCount = 0;
    mRowCount = 0;
}

pGenericTableModel::~pGenericTableModel()
{
}

int pGenericTableModel::columnCount( const QModelIndex& parent ) const
{
    return parent == QModelIndex() ? mColumnCount : 0;
}

int pGenericTableModel::rowCount( const QModelIndex& parent ) const
{
    return parent == QModelIndex() ? mRowCount : 0;
}

QVariant pGenericTableModel::data( const QModelIndex& index, int role ) const
{
    switch ( role ) {
        case pGenericTableModel::ItemFlagsRole:
            return QVariant::fromValue( flags( index ) );
        default: {
            QVariant value = indexInternalData( index ).value( role );
            
            if ( value.isNull() && role == Qt::DisplayRole ) {
                value = indexInternalData( index ).value( Qt::EditRole );
            }
            
            return value;
        }
    }
}

QVariant pGenericTableModel::headerData( int section, Qt::Orientation orientation, int role ) const
{
    return headerInternalData( section, orientation ).value( role );
}

bool pGenericTableModel::setData( const QModelIndex& index, const QVariant& value, int role )
{
    pGenericTableModel::IntVariantMap* map = indexInternalData( index );
    
    if ( !map ) {
        return false;
    }
    
    if ( value.isNull() ) {
        if ( !map->contains( role ) ) {
            return true;
        }
        
        map->remove( role );
    }
    else {
        if ( map->value( role ) == value ) {
            return true;
        }
        
        (*map)[ role ] = value;
    }
    
    emit dataChanged( index, index );
    return true;
}

bool pGenericTableModel::setHeaderData( int section, Qt::Orientation orientation, const QVariant& value, int role )
{
    pGenericTableModel::IntVariantMap* map = headerInternalData( section, orientation );
    
    if ( !map ) {
        return false;
    }
    
    (*map)[ role ] = value;
    emit headerDataChanged( orientation, section, section );
    return true;
}

Qt::ItemFlags pGenericTableModel::flags( const QModelIndex& index ) const
{
    const QVariant variant = indexInternalData( index ).value( pGenericTableModel::ItemFlagsRole );
    return variant.isNull() ? QAbstractTableModel::flags( index ) : variant.value<Qt::ItemFlags>();
}

void pGenericTableModel::sort( int column, Qt::SortOrder order )
{
    Q_UNUSED( column );
    Q_UNUSED( order );
    qWarning() << Q_FUNC_INFO << "Not yet implemented";
}

bool pGenericTableModel::insertColumns( int column, int count, const QModelIndex& parent )
{
    Q_UNUSED( column );
    Q_UNUSED( count );
    Q_UNUSED( parent );
    qWarning() << Q_FUNC_INFO << "Not yet implemented";
    return false;
}

bool pGenericTableModel::removeColumns( int column, int count, const QModelIndex& parent )
{
    Q_UNUSED( column );
    Q_UNUSED( count );
    Q_UNUSED( parent );
    qWarning() << Q_FUNC_INFO << "Not yet implemented";
    return false;
}

bool pGenericTableModel::insertRows( int row, int count, const QModelIndex& parent )
{
    if ( parent != QModelIndex() || row < 0 || row > mRowCount || count <= 0 || mColumnCount == 0 ) {
        return false;
    }
    
    beginInsertRows( parent, row, row +count -1 );
    mRowCount += count;
    foreach ( const QPoint& point, mData.keys() ) {
        if ( point.y() >= row ) {
            mData[ QPoint( point.x(), point.y() +count ) ] = mData.take( point );
        }
    }
    endInsertRows();
    return true;
}

bool pGenericTableModel::removeRows( int row, int count, const QModelIndex& parent )
{
    if ( parent != QModelIndex() || row < 0 || row >= mRowCount || row +count -1 >= mRowCount ) {
        return false;
    }
    
    beginRemoveRows( parent, row, row +count -1 );
    mRowCount -= count;
    foreach ( const QPoint& point, mData.keys() ) {
        if ( point.y() >= row && point.y() < row +count ) {
            mData.remove( point );
        }
        else if ( point.y() >= row +count ) {
            mData[ QPoint( point.x(), point.y() -count ) ] = mData.take( point );
        }
    }
    endRemoveRows();
    return true;
}

void pGenericTableModel::setColumnCount( int count )
{
    if ( count == mColumnCount ) {
        return;
    }
    
    if ( count > mColumnCount ) {
        const int diff = count -mColumnCount;
        beginInsertColumns( QModelIndex(), mColumnCount, diff -1 );
        mColumnCount = count;
        endInsertColumns();
    }
    else {
        beginRemoveColumns( QModelIndex(), count, mColumnCount -1 );
        mColumnCount = count;
        foreach ( const QPoint& point, mData.keys() ) {
            if ( point.x() >= mColumnCount ) {
                mData.remove( point );
            }
        }
        foreach ( const QPoint& point, mHeaderData.keys() ) {
            if ( point.x() == Qt::Horizontal && point.y() >= mColumnCount ) {
                mHeaderData.remove( point );
            }
        }
        endRemoveColumns();
    }
}

void pGenericTableModel::setRowCount( int count )
{
    if ( count == mRowCount ) {
        return;
    }
    
    if ( count > mRowCount ) {
        beginInsertRows( QModelIndex(), mRowCount, count -1 );
        mRowCount = count;
        endInsertRows();
    }
    else {
        beginRemoveRows( QModelIndex(), count, mRowCount -1 );
        mRowCount = count;
        foreach ( const QPoint& point, mData.keys() ) {
            if ( point.y() >= mRowCount ) {
                mData.remove( point );
            }
        }
        foreach ( const QPoint& point, mHeaderData.keys() ) {
            if ( point.x() == Qt::Vertical && point.y() >= mRowCount ) {
                mHeaderData.remove( point );
            }
        }
        endRemoveRows();
    }
}

bool pGenericTableModel::swapColumns( int fromColumn, int toColumn )
{
    Q_UNUSED( fromColumn );
    Q_UNUSED( toColumn );
    qWarning() << Q_FUNC_INFO << "Not yet implemented";
    return false;
}

bool pGenericTableModel::swapRows( int fromRow, int toRow )
{
    if ( fromRow < 0 || fromRow >= mRowCount || toRow < 0 || toRow >= mRowCount ) {
        return false;
    }
    
    if ( fromRow == toRow ) {
        return true;
    }
    
    emit layoutAboutToBeChanged();
    
    const QModelIndexList oldIndexes = persistentIndexList();
    QModelIndexList newIndexes;
    pGenericTableModel::PointIntVariantMap fromData;
    pGenericTableModel::PointIntVariantMap toData;
    
    // keep backup data
    foreach( const QPoint& point, mData.keys() ) {
        if ( point.y() == fromRow ) {
            fromData[ point ] = mData.take( point );
        }
        else if ( point.y() == toRow ) {
            toData[ point ] = mData.take( point );
        }
    }
    
    // move from data
    foreach( const QPoint& point, fromData.keys() ) {
        mData[ QPoint( point.x(), toRow ) ] = fromData.take( point );
    }
    
    // move to data
    foreach( const QPoint& point, toData.keys() ) {
        mData[ QPoint( point.x(), fromRow ) ] = toData.take( point );
    }
    
    // update persistent indexes
    foreach ( const QModelIndex& index, oldIndexes ) {
        if ( index.row() == fromRow ) {
            newIndexes << this->index( toRow, index.column(), index.parent() );
        }
        else if ( index.row() == toRow ) {
            newIndexes << this->index( fromRow, index.column(), index.parent() );
        }
        else {
            newIndexes << index;
        }
    }
    
    changePersistentIndexList( oldIndexes, newIndexes );
    
    emit layoutChanged();
    
    return true;
}

pGenericTableModel::IntVariantMap* pGenericTableModel::indexInternalData( const QModelIndex& index )
{
    return index.isValid() ? &mData[ QPoint( index.column(), index.row() ) ] : 0;
}

pGenericTableModel::IntVariantMap pGenericTableModel::indexInternalData( const QModelIndex& index ) const
{
    return mData.value( QPoint( index.column(), index.row() ) );
}

pGenericTableModel::IntVariantMap* pGenericTableModel::headerInternalData( int section, Qt::Orientation orientation )
{
    switch ( orientation ) {
        case Qt::Horizontal:
            return section >= 0 && section < mColumnCount ? &mHeaderData[ QPoint( (int)orientation, section ) ] : 0;
        case Qt::Vertical:
            return section >= 0 && section < mRowCount ? &mHeaderData[ QPoint( (int)orientation, section ) ] : 0;
    }
    
    return 0;
}

pGenericTableModel::IntVariantMap pGenericTableModel::headerInternalData( int section, Qt::Orientation orientation ) const
{
    return mHeaderData.value( QPoint( (int)orientation, section ) );
}

void pGenericTableModel::clear( bool onlyData )
{
    if ( rowCount() == 0 && columnCount() == 0 ) {
        return;
    }
    
    if ( onlyData ) {
        beginResetModel();
        mData.clear();
        endResetModel();
    }
    else {
        beginRemoveColumns( QModelIndex(), 0, mColumnCount -1 );
        mColumnCount = 0;
        mHeaderData.clear();
        endRemoveColumns();
        
        beginRemoveRows( QModelIndex(), 0, mRowCount -1 );
        mRowCount = 0;
        mData.clear();
        endRemoveRows();
    }
}

QModelIndexList pGenericTableModel::checkedIndexes( int column ) const
{
    QModelIndexList indexes;
    
    foreach ( const QPoint& point, mData.keys() ) {
        if ( column != -1 && column != point.x() ) {
            continue;
        }
        
        const pGenericTableModel::IntVariantMap& map = mData[ point ];
        const QVariant value = map.value( Qt::CheckStateRole );
        const Qt::CheckState state = value.isNull() ? Qt::Unchecked : Qt::CheckState( value.toInt() );
        
        if ( state == Qt::Checked ) {
            indexes << index( point.y(), point.x() );
        }
    }
    
    return indexes;
}

QList<int> pGenericTableModel::checkedRows( int column ) const
{
    QList<int> rows;
    
    foreach ( const QPoint& point, mData.keys() ) {
        if ( column != -1 && column != point.x() ) {
            continue;
        }
        
        const pGenericTableModel::IntVariantMap& map = mData[ point ];
        const QVariant value = map.value( Qt::CheckStateRole );
        const Qt::CheckState state = value.isNull() ? Qt::Unchecked : Qt::CheckState( value.toInt() );
        
        if ( state == Qt::Checked ) {
            rows << point.y();
        }
    }
    
    return rows;
}

QStringList pGenericTableModel::checkedStringList( int column ) const
{
    QStringList strings;
    
    foreach ( const QPoint& point, mData.keys() ) {
        if ( column != -1 && column != point.x() ) {
            continue;
        }
        
        const pGenericTableModel::IntVariantMap& map = mData[ point ];
        const QVariant value = map.value( Qt::CheckStateRole );
        const Qt::CheckState state = value.isNull() ? Qt::Unchecked : Qt::CheckState( value.toInt() );
        
        if ( state == Qt::Checked ) {
            QVariant value = map.value( Qt::DisplayRole );
            
            if ( value.isNull() ) {
                value = map.value( Qt::EditRole );
            }
            
            strings << value.toString();
        }
    }
    
    return strings;
}

void pGenericTableModel::clearCheckStates( int column )
{
    if ( rowCount() == 0 ) {
        return;
    }
    
    foreach ( const QPoint& point, mData.keys() ) {
        if ( column != -1 && column != point.x() ) {
            continue;
        }
        
        pGenericTableModel::IntVariantMap& map = mData[ point ];
        map.remove( Qt::CheckStateRole );
    }
    
    emit dataChanged( index( 0, 0 ), index( rowCount() -1, columnCount() -1 ) );
}