~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to src/corelib/kernel/qabstractitemmodel.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the core module of the Qt Toolkit.
 
6
**
 
7
** This file may be distributed under the terms of the Q Public License
 
8
** as defined by Trolltech AS of Norway and appearing in the file
 
9
** LICENSE.QPL included in the packaging of this file.
 
10
**
 
11
** This file may be distributed and/or modified under the terms of the
 
12
** GNU General Public License version 2 as published by the Free Software
 
13
** Foundation and appearing in the file LICENSE.GPL included in the
 
14
** packaging of this file.
 
15
**
 
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
 
17
**   information about Qt Commercial License Agreements.
 
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
 
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
#include "qabstractitemmodel.h"
 
30
#include <qdatastream.h>
 
31
#include <qstringlist.h>
 
32
#include <qsize.h>
 
33
#include <qmimedata.h>
 
34
#include <qdebug.h>
 
35
#include <qvector.h>
 
36
#include <private/qabstractitemmodel_p.h>
 
37
#include <qbitarray.h>
 
38
 
 
39
#include <limits.h>
 
40
 
 
41
QPersistentModelIndexData *QPersistentModelIndexData::create(const QModelIndex &index)
 
42
{
 
43
    Q_ASSERT(index.isValid()); // we will _never_ insert an invalid index in the list
 
44
    QPersistentModelIndexData *d = 0;
 
45
    QAbstractItemModel *model = const_cast<QAbstractItemModel*>(index.model());
 
46
    QList<QPersistentModelIndexData*> *persistentIndexes = &(model->d_func()->persistent.indexes);
 
47
    for (int i = 0; i < persistentIndexes->count(); ++i) {
 
48
        if (persistentIndexes->at(i)->index == index) {
 
49
            d = persistentIndexes->at(i);
 
50
            break;
 
51
        }
 
52
    }
 
53
    if (!d) { // not found
 
54
        d = new QPersistentModelIndexData();
 
55
        d->model = model;
 
56
        d->index = index;
 
57
        persistentIndexes->append(d);
 
58
    }
 
59
    Q_ASSERT(d);
 
60
    return d;
 
61
}
 
62
 
 
63
void QPersistentModelIndexData::destroy(QPersistentModelIndexData *data)
 
64
{
 
65
    Q_ASSERT(data);
 
66
    QAbstractItemModel *model = const_cast<QAbstractItemModel*>(data->model);
 
67
    // a valid persistent model index with a null model pointer can only happen if the model was destroyed
 
68
    if (model) {
 
69
        QAbstractItemModelPrivate *p = model->d_func();
 
70
        int position = p->persistent.indexes.indexOf(data);
 
71
        p->persistent.changed.removeAll(position);
 
72
        p->persistent.invalidated.removeAll(position);
 
73
        p->persistent.indexes.removeAll(data);
 
74
    }
 
75
    delete data;
 
76
}
 
77
 
 
78
/*!
 
79
  \class QPersistentModelIndex
 
80
 
 
81
  \brief The QPersistentModelIndex class is used to locate data in a data model.
 
82
 
 
83
  \ingroup model-view
 
84
 
 
85
  A QPersistentModelIndex is a model index that can be stored by an
 
86
  application, and later used to access information in a model.
 
87
  Unlike the QModelIndex class, it is safe to store a
 
88
  QPersistentModelIndex since the model will ensure that references
 
89
  to data will continue to be valid as long as that data exists within
 
90
  the model.
 
91
 
 
92
  It is good practice to check that persistent model indexes are valid
 
93
  before using them.
 
94
 
 
95
  \sa \link model-view-programming.html Model/View Programming\endlink QModelIndex QAbstractItemModel
 
96
*/
 
97
 
 
98
 
 
99
/*!
 
100
  \fn QPersistentModelIndex::QPersistentModelIndex()
 
101
 
 
102
  \internal
 
103
*/
 
104
 
 
105
QPersistentModelIndex::QPersistentModelIndex()
 
106
    : d(0)
 
107
{
 
108
}
 
109
 
 
110
/*!
 
111
  \fn QPersistentModelIndex::QPersistentModelIndex(const QPersistentModelIndex &other)
 
112
 
 
113
  Creates a new QPersistentModelIndex that is a copy of the \a other persistent
 
114
  model index.
 
115
*/
 
116
 
 
117
QPersistentModelIndex::QPersistentModelIndex(const QPersistentModelIndex &other)
 
118
    : d(other.d)
 
119
{
 
120
    if (d) d->ref.ref();
 
121
}
 
122
 
 
123
/*!
 
124
    Creates a new QPersistentModelIndex that is a copy of the model \a index.
 
125
*/
 
126
 
 
127
QPersistentModelIndex::QPersistentModelIndex(const QModelIndex &index)
 
128
    : d(0)
 
129
{
 
130
    if (index.isValid()) {
 
131
        d = QPersistentModelIndexData::create(index);
 
132
        d->ref.ref();
 
133
    }
 
134
}
 
135
 
 
136
/*!
 
137
    \fn QPersistentModelIndex::~QPersistentModelIndex()
 
138
 
 
139
    \internal
 
140
*/
 
141
 
 
142
QPersistentModelIndex::~QPersistentModelIndex()
 
143
{
 
144
    if (d && !d->ref.deref()) {
 
145
        QPersistentModelIndexData::destroy(d);
 
146
        d = 0;
 
147
    }
 
148
}
 
149
 
 
150
/*!
 
151
  \fn bool QPersistentModelIndex::operator==(const QPersistentModelIndex &other) const
 
152
 
 
153
  Returns true if this persistent model index is equal to the \a other
 
154
  persistent model index, otherwist returns false.
 
155
*/
 
156
 
 
157
bool QPersistentModelIndex::operator==(const QPersistentModelIndex &other) const
 
158
{
 
159
    if (d && other.d)
 
160
        return d->index == other.d->index;
 
161
    return d == other.d;
 
162
}
 
163
 
 
164
/*!
 
165
  \fn bool QPersistentModelIndex::operator<(const QPersistentModelIndex &other) const
 
166
 
 
167
  Returns true if this persistent model index is smaller than the \a other
 
168
  persistent model index; otherwise returns false.
 
169
*/
 
170
 
 
171
bool QPersistentModelIndex::operator<(const QPersistentModelIndex &other) const
 
172
{
 
173
    return d < other.d;
 
174
}
 
175
 
 
176
/*!
 
177
    Sets the persistent model index to refer to the same item in a model
 
178
    as the \a other persistent model index.
 
179
*/
 
180
 
 
181
QPersistentModelIndex &QPersistentModelIndex::operator=(const QPersistentModelIndex &other)
 
182
{
 
183
    if (d == other.d)
 
184
        return *this;
 
185
    if (d && !d->ref.deref())
 
186
        QPersistentModelIndexData::destroy(d);
 
187
    d = other.d;
 
188
    if (d) d->ref.ref();
 
189
    return *this;
 
190
}
 
191
 
 
192
/*!
 
193
    Sets the persistent model index to refer to the same item in a model
 
194
    as the \a other model index.
 
195
*/
 
196
 
 
197
QPersistentModelIndex &QPersistentModelIndex::operator=(const QModelIndex &other)
 
198
{
 
199
    if (d && !d->ref.deref())
 
200
        QPersistentModelIndexData::destroy(d);
 
201
    if (other.isValid()) {
 
202
        d = QPersistentModelIndexData::create(other);
 
203
        if (d) d->ref.ref();
 
204
    } else {
 
205
        d = 0;
 
206
    }
 
207
    return *this;
 
208
}
 
209
 
 
210
/*!
 
211
  \fn QPersistentModelIndex::operator const QModelIndex&() const
 
212
 
 
213
  Cast operator that returns a const QModelIndex&.
 
214
*/
 
215
 
 
216
QPersistentModelIndex::operator const QModelIndex&() const
 
217
{
 
218
    static const QModelIndex invalid;
 
219
    if (d)
 
220
        return d->index;
 
221
    return invalid;
 
222
}
 
223
 
 
224
/*!
 
225
    \fn bool QPersistentModelIndex::operator==(const QModelIndex &other) const
 
226
 
 
227
    Returns true if this persistent model index refers to the same location as
 
228
    the \a other model index; otherwise returns false.
 
229
*/
 
230
 
 
231
bool QPersistentModelIndex::operator==(const QModelIndex &other) const
 
232
{
 
233
    if (d)
 
234
        return d->index == other;
 
235
    return !other.isValid();
 
236
}
 
237
 
 
238
/*!
 
239
    \fn bool QPersistentModelIndex::operator!=(const QModelIndex &other) const
 
240
 
 
241
    Returns true if this persistent model index does not refer to the same
 
242
    location as the \a other model index; otherwise returns false.
 
243
*/
 
244
 
 
245
bool QPersistentModelIndex::operator!=(const QModelIndex &other) const
 
246
{
 
247
    if (d)
 
248
        return d->index != other;
 
249
    return other.isValid();
 
250
}
 
251
 
 
252
/*!
 
253
    \fn int QPersistentModelIndex::row() const
 
254
 
 
255
    Returns the row this persistent model index refers to.
 
256
*/
 
257
 
 
258
int QPersistentModelIndex::row() const
 
259
{
 
260
    if (d)
 
261
        return d->index.row();
 
262
    return -1;
 
263
}
 
264
 
 
265
/*!
 
266
    \fn int QPersistentModelIndex::column() const
 
267
 
 
268
    Returns the column this persistent model index refers to.
 
269
*/
 
270
 
 
271
int QPersistentModelIndex::column() const
 
272
{
 
273
    if (d)
 
274
        return d->index.column();
 
275
    return -1;
 
276
}
 
277
 
 
278
/*!
 
279
    \fn void *QPersistentModelIndex::internalPointer() const
 
280
 
 
281
    \internal
 
282
 
 
283
    Returns a \c{void} \c{*} pointer used by the model to associate the index with
 
284
    the internal data structure.
 
285
*/
 
286
 
 
287
void *QPersistentModelIndex::internalPointer() const
 
288
{
 
289
    if (d)
 
290
        return d->index.internalPointer();
 
291
    return 0;
 
292
}
 
293
 
 
294
/*!
 
295
    \fn void *QPersistentModelIndex::internalId() const
 
296
 
 
297
    \internal
 
298
 
 
299
    Returns a \c{qint64} used by the model to associate the index with
 
300
    the internal data structure.
 
301
*/
 
302
 
 
303
qint64 QPersistentModelIndex::internalId() const
 
304
{
 
305
    if (d)
 
306
        return d->index.internalId();
 
307
    return 0;
 
308
}
 
309
 
 
310
/*!
 
311
  Returns the parent QModelIndex for this persistent index, or
 
312
  QModelIndex() if it has no parent.
 
313
 
 
314
  \sa child() sibling() model()
 
315
*/
 
316
QModelIndex QPersistentModelIndex::parent() const
 
317
{
 
318
    if (d)
 
319
        return d->index.parent();
 
320
    return QModelIndex();
 
321
}
 
322
 
 
323
/*!
 
324
  Returns the sibling at \a row and \a column or an invalid
 
325
  QModelIndex if there is no sibling at this position.
 
326
 
 
327
  \sa parent() child()
 
328
*/
 
329
 
 
330
QModelIndex QPersistentModelIndex::sibling(int row, int column) const
 
331
{
 
332
    if (d)
 
333
        return d->index.sibling(row, column);
 
334
    return QModelIndex();
 
335
}
 
336
 
 
337
/*!
 
338
  Returns the child of the model index that is stored in the given
 
339
  \a row and \a column.
 
340
 
 
341
  \sa parent() sibling()
 
342
*/
 
343
 
 
344
QModelIndex QPersistentModelIndex::child(int row, int column) const
 
345
{
 
346
    if (d)
 
347
        return d->index.child(row, column);
 
348
    return QModelIndex();
 
349
}
 
350
 
 
351
/*!
 
352
  Returns the model that the index belongs to.
 
353
*/
 
354
const QAbstractItemModel *QPersistentModelIndex::model() const
 
355
{
 
356
    if (d)
 
357
        return d->index.model();
 
358
    return 0;
 
359
}
 
360
 
 
361
/*!
 
362
    \fn bool QPersistentModelIndex::isValid() const
 
363
 
 
364
    Returns true if this persistent model index is valid; otherwise returns
 
365
    false.
 
366
*/
 
367
 
 
368
bool QPersistentModelIndex::isValid() const
 
369
{
 
370
    return d && d->index.isValid();
 
371
}
 
372
 
 
373
#ifndef QT_NO_DEBUG_STREAM
 
374
QDebug operator<<(QDebug dbg, const QModelIndex &idx)
 
375
{
 
376
#ifndef Q_BROKEN_DEBUG_STREAM
 
377
    dbg.nospace() << "QModelIndex(" << idx.row() << "," << idx.column()
 
378
                  << "," << idx.internalId() << "," << idx.model() << ")";
 
379
    return dbg.space();
 
380
#else
 
381
    qWarning("This compiler doesn't support streaming QModelIndex to QDebug");
 
382
    return dbg;
 
383
    Q_UNUSED(idx);
 
384
#endif
 
385
}
 
386
 
 
387
QDebug operator<<(QDebug dbg, const QPersistentModelIndex &idx)
 
388
{
 
389
    if (idx.d)
 
390
        dbg << idx.d->index;
 
391
    else
 
392
        dbg << QModelIndex();
 
393
    return dbg;
 
394
}
 
395
#endif
 
396
 
 
397
QAbstractItemModelPrivate::~QAbstractItemModelPrivate()
 
398
{
 
399
    QList<QPersistentModelIndexData*>::iterator it = persistent.indexes.begin();
 
400
    for (; it != persistent.indexes.end(); ++it) {
 
401
        Q_ASSERT((*it));
 
402
        (*it)->index = QModelIndex();
 
403
        (*it)->model = 0;
 
404
    }
 
405
}
 
406
 
 
407
void QAbstractItemModelPrivate::invalidate(int position)
 
408
{
 
409
    QModelIndex parent = persistent.indexes.at(position)->index;
 
410
    for (int i = 0; i < persistent.indexes.count(); ++i) {
 
411
        if (persistent.indexes.at(i)->index.parent() == parent) {
 
412
            Q_ASSERT((persistent.indexes.at(i)));
 
413
            invalidate(i); // recursive
 
414
        }
 
415
    }
 
416
    persistent.indexes[position]->index = QModelIndex();
 
417
}
 
418
 
 
419
void QAbstractItemModelPrivate::rowsAboutToBeInserted(const QModelIndex &parent,
 
420
                                                      int first, int last)
 
421
{
 
422
    Q_UNUSED(last);
 
423
    persistent.changed.clear();
 
424
    for (int position = 0; position < persistent.indexes.count(); ++position) {
 
425
        QModelIndex index = persistent.indexes.at(position)->index;
 
426
        if (index.isValid() && index.parent() == parent && index.row() >= first)
 
427
            persistent.changed.append(position);
 
428
    }
 
429
}
 
430
 
 
431
void QAbstractItemModelPrivate::rowsInserted(const QModelIndex &parent,
 
432
                                             int first, int last)
 
433
{
 
434
    int count = (last - first) + 1;
 
435
    for (int i = 0; i < persistent.changed.count(); ++i) {
 
436
        int position = persistent.changed.at(i);
 
437
        QModelIndex old = persistent.indexes.at(position)->index;
 
438
        persistent.indexes[position]->index =
 
439
            q_func()->index(old.row() + count, old.column(), parent);
 
440
    }
 
441
    persistent.changed.clear();
 
442
}
 
443
 
 
444
void QAbstractItemModelPrivate::rowsAboutToBeRemoved(const QModelIndex &parent,
 
445
                                                        int first, int last)
 
446
{
 
447
    persistent.changed.clear();
 
448
    persistent.invalidated.clear();
 
449
    for (int position = 0; position < persistent.indexes.count(); ++position) {
 
450
        QModelIndex index = persistent.indexes.at(position)->index;
 
451
        if (index.isValid() && index.parent() == parent) {
 
452
            if (index.row() > last) // below the removed rows
 
453
                persistent.changed.append(position);
 
454
            else if (index.row() >= first) // about to be removed
 
455
                persistent.invalidated.append(position);
 
456
        }
 
457
    }
 
458
}
 
459
 
 
460
void QAbstractItemModelPrivate::rowsRemoved(const QModelIndex &parent,
 
461
                                               int first, int last)
 
462
{
 
463
    int count = (last - first) + 1;
 
464
    for (int i = 0; i < persistent.changed.count(); ++i) {
 
465
        int position = persistent.changed.at(i);
 
466
        QModelIndex old = persistent.indexes.at(position)->index;
 
467
        persistent.indexes[position]->index =
 
468
            q_func()->index(old.row() - count, old.column(), parent);
 
469
    }
 
470
    persistent.changed.clear();
 
471
    for (int j = 0; j < persistent.invalidated.count(); ++j)
 
472
        invalidate(persistent.invalidated.at(j));
 
473
    persistent.invalidated.clear();
 
474
}
 
475
 
 
476
void QAbstractItemModelPrivate::columnsAboutToBeInserted(const QModelIndex &parent,
 
477
                                                         int first, int last)
 
478
{
 
479
    Q_UNUSED(last);
 
480
    persistent.changed.clear();
 
481
    for (int position = 0; position < persistent.indexes.count(); ++position) {
 
482
        QModelIndex index = persistent.indexes.at(position)->index;
 
483
        if (index.isValid() && index.parent() == parent && index.column() >= first)
 
484
            persistent.changed.append(position);
 
485
    }
 
486
}
 
487
 
 
488
void QAbstractItemModelPrivate::columnsInserted(const QModelIndex &parent,
 
489
                                                int first, int last)
 
490
{
 
491
    int count = (last - first) + 1;
 
492
    for (int i = 0; i < persistent.changed.count(); ++i) {
 
493
        int position = persistent.changed.at(i);
 
494
        QModelIndex old = persistent.indexes.at(position)->index;
 
495
        persistent.indexes[position]->index =
 
496
            q_func()->index(old.row(), old.column() + count, parent);
 
497
    }
 
498
    persistent.changed.clear();
 
499
}
 
500
 
 
501
void QAbstractItemModelPrivate::columnsAboutToBeRemoved(const QModelIndex &parent,
 
502
                                                        int first, int last)
 
503
{
 
504
    persistent.changed.clear();
 
505
    persistent.invalidated.clear();
 
506
    for (int position = 0; position < persistent.indexes.count(); ++position) {
 
507
        QModelIndex index = persistent.indexes.at(position)->index;
 
508
        if (index.isValid() && index.parent() == parent) {
 
509
            if (index.column() > last) // after the removed columns
 
510
                persistent.changed.append(position);
 
511
            else if (index.column() >= first) // about to be removed
 
512
                persistent.invalidated.append(position);
 
513
        }
 
514
    }
 
515
}
 
516
 
 
517
void QAbstractItemModelPrivate::columnsRemoved(const QModelIndex &parent,
 
518
                                               int first, int last)
 
519
{
 
520
    int count = (last - first) + 1;
 
521
    for (int i = 0; i < persistent.changed.count(); ++i) {
 
522
        int position = persistent.changed.at(i);
 
523
        QModelIndex old = persistent.indexes.at(position)->index;
 
524
        persistent.indexes[position]->index =
 
525
            q_func()->index(old.row(), old.column() - count, parent);
 
526
    }
 
527
    persistent.changed.clear();
 
528
    for (int j = 0; j < persistent.invalidated.count(); ++j)
 
529
        invalidate(persistent.invalidated.at(j));
 
530
    persistent.invalidated.clear();
 
531
}
 
532
 
 
533
void QAbstractItemModelPrivate::reset()
 
534
{
 
535
    for (int i = 0; i < persistent.indexes.count(); ++i)
 
536
        persistent.indexes[i]->index = QModelIndex();
 
537
}
 
538
 
 
539
/*!
 
540
    \class QModelIndex qabstractitemmodel.h
 
541
 
 
542
    \brief The QModelIndex class is used to locate data in a data model.
 
543
 
 
544
    \ingroup model-view
 
545
    \mainclass
 
546
 
 
547
    This class is used as an index into item models derived from
 
548
    QAbstractItemModel. The index is used by item views, delegates, and
 
549
    selection models to locate an item in the model. QModelIndex objects are
 
550
    created by the model.
 
551
 
 
552
    Model indexes contain all the information required to specify the items
 
553
    they refer to in a model. Indexes are located in a row and a column, and
 
554
    they may have a parent index; use row(), column(), and parent() to obtain
 
555
    this information. Top-level items in a model are represented by model
 
556
    indexes that do not have a parent index - in this case, parent() will
 
557
    return an invalid model index that is equivalent to an index constructed
 
558
    with the zero argument form of the QModelIndex() constructor.
 
559
 
 
560
    To obtain a model index that refers to an item in a model, call
 
561
    QAbstractItemModel::index() with the required row and column
 
562
    values, and the model index of the parent. When referring to
 
563
    top-level items in a model, supply QModelIndex() as the parent index.
 
564
 
 
565
    The model() function returns the model that the index references as a
 
566
    QAbstractItemModel.
 
567
    The child() function is used to examine the items held beneath the index
 
568
    in the model.
 
569
    The sibling() function allows you to traverse items in the model on the
 
570
    same level as the index.
 
571
 
 
572
    Model indexes can become invalid over time so they should be used
 
573
    immediately and then discarded. If you need to keep a model index
 
574
    over time use a QPersistentModelIndex.
 
575
 
 
576
    \sa \link model-view-programming.html Model/View Programming\endlink QPersistentModelIndex QAbstractItemModel
 
577
*/
 
578
 
 
579
/*!
 
580
    \fn QModelIndex::QModelIndex()
 
581
 
 
582
    Creates a new empty model index.
 
583
    This type of model index is used to indicate
 
584
    that the position in the model is invalid.
 
585
 
 
586
    \sa isValid() QAbstractItemModel
 
587
*/
 
588
 
 
589
/*!
 
590
    \fn QModelIndex::QModelIndex(int row, int column, void *data, const QAbstractItemModel *model)
 
591
 
 
592
    \internal
 
593
 
 
594
    Creates a new model index at the given \a row and \a column,
 
595
    pointing to some \a data.
 
596
*/
 
597
 
 
598
/*!
 
599
    \fn QModelIndex::QModelIndex(const QModelIndex &other)
 
600
 
 
601
    Creates a new model index that is a copy of the \a other model
 
602
    index.
 
603
*/
 
604
 
 
605
/*!
 
606
    \fn QModelIndex::~QModelIndex()
 
607
 
 
608
    Destroys the model index.
 
609
*/
 
610
 
 
611
/*!
 
612
    \fn int QModelIndex::row() const
 
613
 
 
614
    Returns the row this model index refers to.
 
615
*/
 
616
 
 
617
 
 
618
/*!
 
619
    \fn int QModelIndex::column() const
 
620
 
 
621
    Returns the column this model index refers to.
 
622
*/
 
623
 
 
624
 
 
625
/*!
 
626
    \fn void *QModelIndex::internalPointer() const
 
627
 
 
628
    Returns a \c{void} \c{*} pointer used by the model to associate
 
629
    the index with the internal data structure.
 
630
*/
 
631
 
 
632
/*!
 
633
    \fn void *QModelIndex::internalId() const
 
634
 
 
635
    Returns a \c{qint64} used by the model to associate
 
636
    the index with the internal data structure.
 
637
*/
 
638
 
 
639
/*!
 
640
    \fn bool QModelIndex::isValid() const
 
641
 
 
642
    Returns true if this model index is valid; otherwise returns
 
643
    false.
 
644
*/
 
645
 
 
646
/*!
 
647
    \fn const QAbstractItemModel *QModelIndex::model() const
 
648
 
 
649
    Returns a pointer to the model containing the item that this index
 
650
    refers to.
 
651
*/
 
652
 
 
653
/*!
 
654
    \fn QModelIndex QModelIndex::sibling(int row, int column) const
 
655
 
 
656
    Returns the sibling at \a row and \a column or an invalid
 
657
    QModelIndex if there is no sibling at this position.
 
658
 
 
659
    \sa parent() child()
 
660
*/
 
661
 
 
662
/*!
 
663
    \fn QModelIndex QModelIndex::child(int row, int column) const
 
664
 
 
665
    Returns the child of the model index that is stored in the given
 
666
    \a row and \a column.
 
667
 
 
668
    \sa parent() sibling()
 
669
*/
 
670
 
 
671
/*!
 
672
    \fn bool QModelIndex::operator==(const QModelIndex &other) const
 
673
 
 
674
    Returns true if this model index refers to the same location as
 
675
    the \a other model index; otherwise returns false.
 
676
*/
 
677
 
 
678
 
 
679
/*!
 
680
    \fn bool QModelIndex::operator!=(const QModelIndex &other) const
 
681
 
 
682
    Returns true if this model index does not refer to the same
 
683
    location as the \a other model index; otherwise returns false.
 
684
*/
 
685
 
 
686
 
 
687
/*!
 
688
  \fn QModelIndex QModelIndex::parent() const
 
689
 
 
690
  Return the parent of the model index, or QModelIndex() if it has no
 
691
  parent.
 
692
 
 
693
  \sa child() sibling() model()
 
694
*/
 
695
 
 
696
/*!
 
697
    \class QAbstractItemModel qabstractitemmodel.h
 
698
 
 
699
    \brief The QAbstractItemModel class provides the abstract interface for
 
700
    item model classes.
 
701
 
 
702
    \ingroup model-view
 
703
    \mainclass
 
704
 
 
705
    The QAbstractItemModel class defines the standard interface that
 
706
    item models must use to be able to interoperate with other
 
707
    components in the model/view architecture. It is not supposed to
 
708
    be instantiated directly. Instead, you should subclass it to create
 
709
    new models.
 
710
 
 
711
    The QAbstractItemModel class is one of the \l{Model/View Classes}
 
712
    and is part of Qt's \l{Model/View Programming}{model/view framework}.
 
713
 
 
714
    If you need a model to use with a QListView or a QTableView, you
 
715
    should consider subclassing QAbstractListModel or QAbstractTableModel
 
716
    instead of this class.
 
717
 
 
718
    The underlying data model is exposed to views and delegates as
 
719
    a hierarchy of tables. If you don't make use of the hierarchy,
 
720
    then the model is a simple table of rows and columns. Each item
 
721
    has a unique index specified by a QModelIndex.
 
722
 
 
723
    \img modelindex-no-parent.png
 
724
 
 
725
    Every item of data that can be accessed via a model has an associated
 
726
    model index that is obtained using the index() function. Each index
 
727
    may have a sibling() index; child items have a parent() index.
 
728
 
 
729
    Each item has a number of data elements associated with it, and each
 
730
    of these can be retrieved by specifying a role (see \l Qt::ItemDataRole)
 
731
    to the model's data() function. Data for all available roles can be
 
732
    obtained at the same time using the itemData() function.
 
733
 
 
734
    Data for each role is set using a particular \l Qt::ItemDataRole.
 
735
    Data for individual roles are set individually with setData(), or they
 
736
    can be set for all roles with setItemData().
 
737
 
 
738
    Items can be queried with flags() (see \l Qt::ItemFlag) to see if they
 
739
    can be selected, dragged, or manipulated in other ways.
 
740
 
 
741
    If an item has child objects, hasChildren() returns true for the
 
742
    corresponding index.
 
743
 
 
744
    The model has a rowCount() and a columnCount() for each level of
 
745
    the hierarchy. Rows and columns can be inserted and removed with
 
746
    insertRows(), insertColumns(), removeRows(), and removeColumns().
 
747
 
 
748
    The model emits signals to indicate changes. For example,
 
749
    dataChanged() is emitted whenever items of data made available by
 
750
    the model are changed. Changes to the headers supplied by the model
 
751
    cause headerDataChanged() to be emitted. If the structure of the
 
752
    underlying data changes, the model can emit layoutChanged() to
 
753
    indicate to any attached views that they should redisplay any items
 
754
    shown, taking the new structure into account.
 
755
 
 
756
    The items available through the model can be searched for particular
 
757
    data using the match() function.
 
758
 
 
759
    If the model is sortable, it can be sorted with sort().
 
760
 
 
761
    \section1 Subclassing
 
762
 
 
763
    When subclassing QAbstractItemModel, at the very least you must
 
764
    implement index(), parent(), rowCount(), columnCount(), hasChildren(),
 
765
    and data().  To enable editing in your model, you must
 
766
    also implement setData(), and reimplement flags() to ensure that
 
767
    \c ItemIsEditable is returned.
 
768
 
 
769
    You can also reimplement headerData() and setHeaderData() to control
 
770
    the way the headers for your model are presented.
 
771
 
 
772
    Models that provide interfaces to resizable data structures can
 
773
    provide implementations of insertRows(), removeRows(), insertColumns(),
 
774
    and removeColumns(). When implementing these functions, it is
 
775
    important to notify any connected views about changes to the model's
 
776
    dimensions both \e before and \e after they occur:
 
777
 
 
778
    \list
 
779
    \o An insertRows() implementation must call beginInsertRows()
 
780
       \e before inserting new rows into the data structure, and it must
 
781
       call endInsertRows() \e{immediately afterwards}.
 
782
    \o An insertColumns() implementation must call beginInsertColumns()
 
783
       \e before inserting new columns into the data structure, and it must
 
784
       call endInsertColumns() \e{immediately afterwards}.
 
785
    \o A removeRows() implementation must call beginRemoveRows()
 
786
       \e before the rows are removed from the data structure, and it must
 
787
       call endRemoveRows() \e{immediately afterwards}.
 
788
    \o A removeColumns() implementation must call beginRemoveColumns()
 
789
       \e before the columns are removed from the data structure, and it must
 
790
       call endRemoveColumns() \e{immediately afterwards}.
 
791
    \endlist
 
792
 
 
793
    The signals that these functions emit give attached components the chance
 
794
    to take action before any data becomes unavailable. The encapsulation of
 
795
    the insert and remove operations with these begin and end functions also
 
796
    enable the model to manage
 
797
    \l{QPersistentModelIndex}{persistent model indexes} correctly.
 
798
    \bold{If you want selections to be handled properly, you must ensure that
 
799
    you call these functions.}
 
800
 
 
801
    \sa \link model-view-programming.html Model/View Programming\endlink QModelIndex QAbstractItemView
 
802
 
 
803
*/
 
804
 
 
805
/*!
 
806
    \fn QModelIndex QAbstractItemModel::index(int row, int column, const QModelIndex &parent) const = 0
 
807
 
 
808
    Returns the index of the item in the model specified by the given \a row,
 
809
    \a column and \a parent index.
 
810
*/
 
811
 
 
812
/*!
 
813
    \fn bool QAbstractItemModel::insertColumn(int column, const QModelIndex &parent)
 
814
 
 
815
    Inserts a single column before the given \a column in the child items of
 
816
    the \a parent specified. Returns true if the column is inserted; otherwise
 
817
    returns false.
 
818
 
 
819
    \sa insertColumns() insertRow() removeColumn()
 
820
*/
 
821
 
 
822
/*!
 
823
    \fn bool QAbstractItemModel::insertRow(int row, const QModelIndex &parent)
 
824
 
 
825
    Inserts a single row before the given \a row in the child items of the
 
826
    \a parent specified. Returns true if the row is inserted; otherwise
 
827
    returns false.
 
828
 
 
829
    \sa insertRows() insertColumn() removeRow()
 
830
*/
 
831
 
 
832
/*!
 
833
    \fn QModelIndex QAbstractItemModel::parent(const QModelIndex &index) const = 0
 
834
 
 
835
    Returns the parent of the model item with the given \a index.
 
836
*/
 
837
 
 
838
/*!
 
839
    \fn bool QAbstractItemModel::removeColumn(int column, const QModelIndex &parent)
 
840
 
 
841
    Removes the given \a column from the child items of the \a parent specified.
 
842
    Returns true if the column is removed; otherwise returns false.
 
843
 
 
844
    \sa removeColumns(), removeRow(), insertColumn()
 
845
*/
 
846
 
 
847
/*!
 
848
    \fn bool QAbstractItemModel::removeRow(int row, const QModelIndex &parent)
 
849
 
 
850
    Removes the given \a row from the child items of the \a parent specified.
 
851
    Returns true if the row is removed; otherwise returns false.
 
852
 
 
853
    \sa removeRows(), removeColumn(), insertRow()
 
854
*/
 
855
 
 
856
/*!
 
857
    \fn void QAbstractItemModel::headerDataChanged(Qt::Orientation orientation, int first, int last)
 
858
 
 
859
    This signal is emitted whenever a header is changed. The \a orientation
 
860
    indicates whether the horizontal or vertical header has changed. The
 
861
    sections in the header from the \a first to the \a last need to be updated.
 
862
 
 
863
    \sa headerData(), setHeaderData(), dataChanged()
 
864
*/
 
865
 
 
866
/*!
 
867
    Constructs an abstract item model with the given \a parent.
 
868
*/
 
869
QAbstractItemModel::QAbstractItemModel(QObject *parent)
 
870
    : QObject(*new QAbstractItemModelPrivate, parent)
 
871
{
 
872
}
 
873
 
 
874
/*!
 
875
  \internal
 
876
*/
 
877
QAbstractItemModel::QAbstractItemModel(QAbstractItemModelPrivate &dd, QObject *parent)
 
878
    : QObject(dd, parent)
 
879
{
 
880
}
 
881
 
 
882
/*!
 
883
    Destroys the abstract item model.
 
884
*/
 
885
QAbstractItemModel::~QAbstractItemModel()
 
886
{
 
887
}
 
888
 
 
889
/*!
 
890
    \fn QModelIndex QAbstractItemModel::sibling(int row, int column, const QModelIndex &index) const
 
891
 
 
892
    Returns the sibling at \a row and \a column for the item at \a
 
893
    index or an invalid QModelIndex if there is no sibling.
 
894
 
 
895
    \a row, \a column, and \a index.
 
896
*/
 
897
 
 
898
 
 
899
/*!
 
900
    \fn int QAbstractItemModel::rowCount(const QModelIndex &parent) const = 0
 
901
 
 
902
    Returns the number of rows under the given \a parent.
 
903
*/
 
904
 
 
905
 
 
906
/*!
 
907
    \fn int QAbstractItemModel::columnCount(const QModelIndex &parent) const = 0;
 
908
    Returns the number of columns for the given \a parent.
 
909
*/
 
910
 
 
911
/*!
 
912
    \fn void QAbstractItemModel::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
 
913
 
 
914
    This signal is emitted whenever the data in an existing item
 
915
    changes. The affected items are those between \a topLeft and \a
 
916
    bottomRight inclusive.
 
917
 
 
918
    \sa headerDataChanged(), setData(), layoutChanged()
 
919
*/
 
920
 
 
921
/*!
 
922
    \fn void QAbstractItemModel::rowsInserted(const QModelIndex &parent, int start, int end)
 
923
    \internal
 
924
 
 
925
    This signal is emitted after rows have been inserted into the
 
926
    model. The new items are those between \a start and \a end
 
927
    inclusive, under the given \a parent item.
 
928
 
 
929
    \sa insertRows()
 
930
*/
 
931
 
 
932
/*!
 
933
    \fn void QAbstractItemModel::rowsAboutToBeInserted(const QModelIndex &parent, int start, int end)
 
934
    \internal
 
935
 
 
936
    This signal is emitted just before rows are inserted into the
 
937
    model. The new items will be positioned between \a start and \a end
 
938
    inclusive, under the given \a parent item.
 
939
 
 
940
    \sa insertRows()
 
941
*/
 
942
 
 
943
/*!
 
944
    \fn void QAbstractItemModel::rowsRemoved(const QModelIndex &parent, int start, int end)
 
945
    \internal
 
946
 
 
947
    This signal is emitted after rows have been removed from the
 
948
    model. The removed items are those between \a start and \a end
 
949
    inclusive, under the given \a parent item.
 
950
 
 
951
    \sa removeRows()
 
952
*/
 
953
 
 
954
/*!
 
955
    \fn void QAbstractItemModel::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end)
 
956
    \internal
 
957
 
 
958
    This signal is emitted just before rows are removed from the
 
959
    model. The items that will be removed are those between \a start and \a end
 
960
    inclusive, under the given \a parent item.
 
961
 
 
962
    \sa removeRows()
 
963
*/
 
964
 
 
965
/*!
 
966
    \fn void QAbstractItemModel::columnsInserted(const QModelIndex &parent, int start, int end)
 
967
    \internal
 
968
 
 
969
    This signal is emitted after columns have been inserted into the
 
970
    model. The new items are those between \a start and \a end
 
971
    inclusive, under the given \a parent item.
 
972
 
 
973
    \sa insertColumns()
 
974
*/
 
975
 
 
976
/*!
 
977
    \fn void QAbstractItemModel::columnsAboutToBeInserted(const QModelIndex &parent, int start, int end)
 
978
    \internal
 
979
 
 
980
    This signal is emitted just before columns are inserted into the
 
981
    model. The new items will be positioned between \a start and \a end
 
982
    inclusive, under the given \a parent item.
 
983
 
 
984
    \sa insertColumns()
 
985
*/
 
986
 
 
987
/*!
 
988
    \fn void QAbstractItemModel::columnsRemoved(const QModelIndex &parent, int start, int end)
 
989
    \internal
 
990
 
 
991
    This signal is emitted after columns have been removed from the
 
992
    model. The removed items are those between \a start and \a end
 
993
    inclusive, under the given \a parent item.
 
994
 
 
995
    \sa removeColumns()
 
996
*/
 
997
 
 
998
/*!
 
999
    \fn void QAbstractItemModel::columnsAboutToBeRemoved(const QModelIndex &parent, int start, int end)
 
1000
    \internal
 
1001
 
 
1002
    This signal is emitted just before columns are removed
 
1003
    from the model. The items to be removed are those between \a start and
 
1004
    \a end inclusive, under the given \a parent item.
 
1005
 
 
1006
    \sa removeColumns()
 
1007
*/
 
1008
 
 
1009
/*!
 
1010
  Returns true if the model returns a valid QModelIndex for \a row and
 
1011
  \a column with \a parent, otherwise returns false.
 
1012
*/
 
1013
bool QAbstractItemModel::hasIndex(int row, int column, const QModelIndex &parent) const
 
1014
{
 
1015
    if (row < 0 || column < 0)
 
1016
        return false;
 
1017
    return row < rowCount(parent) && column < columnCount(parent);
 
1018
}
 
1019
 
 
1020
 
 
1021
/*!
 
1022
  Returns true if \a parent has any children; otherwise returns false.
 
1023
 
 
1024
  \sa parent() index()
 
1025
*/
 
1026
bool QAbstractItemModel::hasChildren(const QModelIndex &parent) const
 
1027
{
 
1028
    return (rowCount(parent) > 0) && (columnCount(parent) > 0);
 
1029
}
 
1030
 
 
1031
 
 
1032
/*!
 
1033
    Returns a map with values for all predefined roles in the model
 
1034
    for the item at the given \a index.
 
1035
 
 
1036
    This must be reimplemented if you want to extend the model with
 
1037
    custom roles.
 
1038
 
 
1039
    \sa Qt::ItemDataRole data()
 
1040
*/
 
1041
QMap<int, QVariant> QAbstractItemModel::itemData(const QModelIndex &index) const
 
1042
{
 
1043
    QMap<int, QVariant> roles;
 
1044
    for (int i = 0; i < Qt::UserRole; ++i) {
 
1045
        QVariant variantData = data(index, i);
 
1046
        if (variantData.type() != QVariant::Invalid)
 
1047
            roles.insert(i, variantData);
 
1048
    }
 
1049
    return roles;
 
1050
}
 
1051
 
 
1052
/*!
 
1053
    Sets the \a role data for the item at \a index to \a value.
 
1054
    Returns true if successful; otherwise returns false.
 
1055
 
 
1056
    The base class implementation returns false. This function and
 
1057
    data() must be reimplemented for editable models.
 
1058
 
 
1059
    \sa data() itemData()
 
1060
*/
 
1061
bool QAbstractItemModel::setData(const QModelIndex &index, const QVariant &value, int role)
 
1062
{
 
1063
    Q_UNUSED(index);
 
1064
    Q_UNUSED(value);
 
1065
    Q_UNUSED(role);
 
1066
    return false;
 
1067
}
 
1068
 
 
1069
/*!
 
1070
    \fn QVariant QAbstractItemModel::data(const QModelIndex &index, int role) const = 0
 
1071
 
 
1072
    Returns the data stored under the given \a role for the item referred to
 
1073
    by the \a index.
 
1074
 
 
1075
    \sa Qt::ItemDataRole
 
1076
*/
 
1077
 
 
1078
/*!
 
1079
    For every \c Qt::ItemDataRole in \a roles, sets the role data for the item at
 
1080
    \a index to the associated value in \a roles. Returns true if
 
1081
    successful; otherwise returns false.
 
1082
 
 
1083
    \sa setData() data() itemData()
 
1084
*/
 
1085
bool QAbstractItemModel::setItemData(const QModelIndex &index, const QMap<int, QVariant> &roles)
 
1086
{
 
1087
    bool b = true;
 
1088
    for (QMap<int, QVariant>::ConstIterator it = roles.begin(); it != roles.end(); ++it)
 
1089
        b = b && setData(index, it.value(), it.key());
 
1090
    return b;
 
1091
}
 
1092
 
 
1093
/*!
 
1094
    Returns a list of MIME types that can be used to describe a list of
 
1095
    model indexes.
 
1096
 
 
1097
    \sa mimeData()
 
1098
*/
 
1099
QStringList QAbstractItemModel::mimeTypes() const
 
1100
{
 
1101
    QStringList types;
 
1102
    types << "application/x-qabstractitemmodeldatalist";
 
1103
    return types;
 
1104
}
 
1105
 
 
1106
/*!
 
1107
    Returns an object that contains a serialized description of the specified
 
1108
    \a indexes. The format used to describe the items corresponding to the
 
1109
    indexes is obtained from the mimeTypes() function.
 
1110
 
 
1111
    If the list of indexes is empty, 0 is returned rather than a serialized
 
1112
    empty list.
 
1113
*/
 
1114
QMimeData *QAbstractItemModel::mimeData(const QModelIndexList &indexes) const
 
1115
{
 
1116
    if (indexes.count() <= 0)
 
1117
        return 0;
 
1118
    QMimeData *data = new QMimeData();
 
1119
    QString format = mimeTypes().at(0);
 
1120
    QByteArray encoded;
 
1121
    QDataStream stream(&encoded, QIODevice::WriteOnly);
 
1122
    encodeData(indexes, stream);
 
1123
    data->setData(format, encoded);
 
1124
    return data;
 
1125
}
 
1126
 
 
1127
/*!
 
1128
    Handles the \a data supplied by a drag and drop operation that ended with
 
1129
    the given \a action over the row in the model specified by the \a row,
 
1130
    \a column, and the \a parent index.
 
1131
 
 
1132
    \sa supportedDropActions()
 
1133
*/
 
1134
bool QAbstractItemModel::dropMimeData(const QMimeData *data, Qt::DropAction action,
 
1135
                                      int row, int column, const QModelIndex &parent)
 
1136
{
 
1137
    // check if the action is supported
 
1138
    if (!data || action != Qt::CopyAction)
 
1139
        return false;
 
1140
    // check if the format is supported
 
1141
    QString format = mimeTypes().at(0);
 
1142
    if (!data->hasFormat(format))
 
1143
        return false;
 
1144
    if (row > rowCount(parent))
 
1145
        row = rowCount(parent);
 
1146
    if (row == -1)
 
1147
        row = rowCount(parent);
 
1148
    if (column == -1)
 
1149
        column = 0;
 
1150
    // decode and insert
 
1151
    QByteArray encoded = data->data(format);
 
1152
    QDataStream stream(&encoded, QIODevice::ReadOnly);
 
1153
    return decodeData(row, column, parent, stream);
 
1154
}
 
1155
 
 
1156
/*!
 
1157
  Returns the drop actions supported by this model.
 
1158
 
 
1159
  \sa Qt::DropActions
 
1160
*/
 
1161
Qt::DropActions QAbstractItemModel::supportedDropActions() const
 
1162
{
 
1163
    return Qt::CopyAction;
 
1164
}
 
1165
 
 
1166
/*!
 
1167
  Inserts \a count rows into the model before the given \a row.
 
1168
  The items in the new row will be children of the item represented by the
 
1169
  \a parent model index.
 
1170
 
 
1171
  If \a row is 0, the rows are prepended to any existing rows in the parent.
 
1172
  If \a row is rowCount(), the rows are appended to any existing rows in the
 
1173
  parent.
 
1174
  If \a parent has no children, a single column with \a count rows is inserted.
 
1175
 
 
1176
  Returns true if the rows were successfully inserted; otherwise returns
 
1177
  false.
 
1178
 
 
1179
  The base class implementation does nothing and returns false.
 
1180
 
 
1181
  If you want to be able to insert rows in a subclass, you must reimplement
 
1182
  this function, calling the beginInsertRows() function \e before inserting
 
1183
  new rows into your underlying data store, and call the endInsertRows()
 
1184
  function \e afterwards. Return true to indicate success; otherwise return
 
1185
  false.
 
1186
*/
 
1187
bool QAbstractItemModel::insertRows(int, int, const QModelIndex &)
 
1188
{
 
1189
    return false;
 
1190
}
 
1191
 
 
1192
/*!
 
1193
  Inserts \a count new columns into the model before the given \a column.
 
1194
  The items in each new column will be children of the item represented by the
 
1195
  \a parent model index.
 
1196
 
 
1197
  If \a column is 0, the columns are prepended to any existing columns.
 
1198
  If \a column is columnCount(), the columns are appended to any existing
 
1199
  columns.
 
1200
  If \a parent has no children, a single row with \a count columns is inserted.
 
1201
 
 
1202
  Returns true if the columns were successfully inserted; otherwise returns
 
1203
  false.
 
1204
 
 
1205
  The base class implementation does nothing and returns false.
 
1206
 
 
1207
  If you want to be able to insert columns in a subclass, you must reimplement
 
1208
  this function, calling the beginInsertColumns() function \e before inserting
 
1209
  new columns into your underlying data store, and call the endInsertColumns()
 
1210
  function \e afterwards. Return true to indicate success; otherwise return
 
1211
  false.
 
1212
*/
 
1213
bool QAbstractItemModel::insertColumns(int, int, const QModelIndex &)
 
1214
{
 
1215
    return false;
 
1216
}
 
1217
 
 
1218
/*!
 
1219
    Removes \a count rows starting with the given \a row under parent
 
1220
    \a parent from the model. Returns true if the rows were successfully
 
1221
    removed; otherwise returns false.
 
1222
 
 
1223
    The base class implementation does nothing and returns false.
 
1224
 
 
1225
    If you want to be able to remove rows in a subclass, you must reimplement
 
1226
    this function, calling the beginRemoveRows() function \e before removing
 
1227
    new rows into your underlying data store, and call the endRemoveRows()
 
1228
    function \e afterwards. Return true to indicate success; otherwise return
 
1229
    false.
 
1230
 
 
1231
    \sa removeRow(), removeColumns(), insertColumns()
 
1232
*/
 
1233
bool QAbstractItemModel::removeRows(int, int, const QModelIndex &)
 
1234
{
 
1235
    return false;
 
1236
}
 
1237
 
 
1238
/*!
 
1239
    Removes \a count columns starting with the given \a column under
 
1240
    parent \a parent from the model. Returns true if the columns were
 
1241
    successfully removed; otherwise returns false.
 
1242
 
 
1243
    The base class implementation does nothing and returns false.
 
1244
 
 
1245
    If you want to be able to remove columns in a subclass, you must reimplement
 
1246
    this function, calling the beginRemoveColumns() function \e before removing
 
1247
    new columns into your underlying data store, and call the endRemoveColumns()
 
1248
    function \e afterwards. Return true to indicate success; otherwise return
 
1249
    false.
 
1250
 
 
1251
    \sa removeColumn(), removeRows(), insertColumns()
 
1252
*/
 
1253
bool QAbstractItemModel::removeColumns(int, int, const QModelIndex &)
 
1254
{
 
1255
    return false;
 
1256
}
 
1257
 
 
1258
/*!
 
1259
  Fetches any available data for the items with the parent specified by the
 
1260
  \a parent index.
 
1261
 
 
1262
  Reimplement this if you have incremental data.
 
1263
 
 
1264
  The default implementation does nothing.
 
1265
 
 
1266
  \sa canFetchMore()
 
1267
*/
 
1268
void QAbstractItemModel::fetchMore(const QModelIndex &)
 
1269
{
 
1270
    // do nothing
 
1271
}
 
1272
 
 
1273
/*!
 
1274
  Returns true if there is more data available for \a parent,
 
1275
  otherwise false.
 
1276
 
 
1277
  The default implementation always returns false.
 
1278
 
 
1279
  \sa fetchMore()
 
1280
*/
 
1281
bool QAbstractItemModel::canFetchMore(const QModelIndex &) const
 
1282
{
 
1283
    return false;
 
1284
}
 
1285
 
 
1286
/*!
 
1287
    Returns the item flags for the given \a index.
 
1288
 
 
1289
    The base class implementation returns a combination of flags that
 
1290
    enables the item (\c ItemIsEnabled) and allows it to be
 
1291
    selected (\c ItemIsSelectable).
 
1292
 
 
1293
    \sa Qt::ItemFlags
 
1294
*/
 
1295
Qt::ItemFlags QAbstractItemModel::flags(const QModelIndex &index) const
 
1296
{
 
1297
    Q_UNUSED(index);
 
1298
    return Qt::ItemIsSelectable|Qt::ItemIsEnabled;
 
1299
}
 
1300
 
 
1301
/*!
 
1302
    Sorts the model by \a column in the given \a order.
 
1303
 
 
1304
    The base class implementation does nothing.
 
1305
*/
 
1306
void QAbstractItemModel::sort(int column, Qt::SortOrder order)
 
1307
{
 
1308
    Q_UNUSED(column);
 
1309
    Q_UNUSED(order);
 
1310
    // do nothing
 
1311
}
 
1312
 
 
1313
/*!
 
1314
  Returns a model index for the buddy of the item represented by \a index.
 
1315
  When the user wants to edit an item, the view will call this function to
 
1316
  check whether another item in the model should be edited instead, and
 
1317
  construct a delegate using the model index returned by the buddy item.
 
1318
 
 
1319
  In the default implementation each item is its own buddy.
 
1320
*/
 
1321
QModelIndex QAbstractItemModel::buddy(const QModelIndex &index) const
 
1322
{
 
1323
    return index;
 
1324
}
 
1325
 
 
1326
/*!
 
1327
    Returns a list of indexes for the items where the data stored under
 
1328
    the given \a role matches the specified \a value. The way the search
 
1329
    is performed is defined by the \a flags given. The list that is
 
1330
    returned may be empty.
 
1331
 
 
1332
    The search starts from the \a start index, and continues until the
 
1333
    number of matching data items equals \a hits, the search reaches
 
1334
    the last row, or the search reaches \a start again, depending on
 
1335
    whether \c MatchWrap is specified in \a flags.
 
1336
*/
 
1337
QModelIndexList QAbstractItemModel::match(const QModelIndex &start, int role,
 
1338
                                          const QVariant &value, int hits,
 
1339
                                          Qt::MatchFlags flags) const
 
1340
{
 
1341
    QModelIndexList result;
 
1342
    uint matchType = flags & 0x0F;
 
1343
    bool caseSesitive = flags & Qt::MatchCaseSensitive;
 
1344
    bool recurse = flags & Qt::MatchRecursive;
 
1345
    bool wrap = flags & Qt::MatchWrap;
 
1346
    bool allHits = (hits == -1);
 
1347
    QString text; // only convert to a string if it is needed
 
1348
    QModelIndex p = parent(start);
 
1349
    int from = start.row();
 
1350
    int to = rowCount(p);
 
1351
    // iterates twice if wrapping
 
1352
    for (int i = 0; (wrap && i < 2) || (!wrap && i < 1); ++i) {
 
1353
        for (int r = from; (r < to) && (allHits || result.count() < hits); ++r) {
 
1354
            QModelIndex idx = index(r, start.column(), p);
 
1355
            QVariant v = data(idx, role);
 
1356
            // QVariant based matching
 
1357
            if (matchType == Qt::MatchExactly) {
 
1358
                if (value == v)
 
1359
                    result.append(idx);
 
1360
            } else { // QString based matching
 
1361
                if (text.isEmpty()) { // lazy conversion
 
1362
                    text = value.toString();
 
1363
                    if (!caseSesitive)
 
1364
                        text = text.toLower();
 
1365
                }
 
1366
                QString t = v.toString();
 
1367
                if (!caseSesitive)
 
1368
                    t = t.toLower();
 
1369
                switch (matchType) {
 
1370
                case Qt::MatchRegExp:
 
1371
                    if (QRegExp(text).exactMatch(t))
 
1372
                        result.append(idx);
 
1373
                    break;
 
1374
                case Qt::MatchWildcard:
 
1375
                    if (QRegExp(text, Qt::CaseSensitive, QRegExp::Wildcard).exactMatch(t))
 
1376
                        result.append(idx);
 
1377
                    break;
 
1378
                case Qt::MatchStartsWith:
 
1379
                    if (t.startsWith(text))
 
1380
                        result.append(idx);
 
1381
                    break;
 
1382
                case Qt::MatchEndsWith:
 
1383
                    if (t.endsWith(text))
 
1384
                        result.append(idx);
 
1385
                    break;
 
1386
                case Qt::MatchContains:
 
1387
                default:
 
1388
                    if (t.contains(text))
 
1389
                        result.append(idx);
 
1390
                }
 
1391
            }
 
1392
            if (recurse && hasChildren(idx)) { // search the hierarchy
 
1393
                result += match(index(0, idx.column(), idx), role,
 
1394
                                (text.isEmpty() ? value : text),
 
1395
                                (allHits ? -1 : hits - result.count()), flags);
 
1396
            }
 
1397
        }
 
1398
        // prepare for the next iteration
 
1399
        from = 0;
 
1400
        to = start.row();
 
1401
    }
 
1402
    return result;
 
1403
}
 
1404
 
 
1405
/*!
 
1406
  Returns the row and column span of the item represented by \a index.
 
1407
*/
 
1408
 
 
1409
QSize QAbstractItemModel::span(const QModelIndex &) const
 
1410
{
 
1411
    return QSize(1, 1);
 
1412
}
 
1413
 
 
1414
/*!
 
1415
  Called to let the model know that it should submit whatever it has cached
 
1416
  to the permanent storage. Typically used for row editing.
 
1417
 
 
1418
  Returns false on error, otherwise true.
 
1419
*/
 
1420
 
 
1421
bool QAbstractItemModel::submit()
 
1422
{
 
1423
    return true;
 
1424
}
 
1425
 
 
1426
/*!
 
1427
  Called to let the model know that it should discart whatever it has cached.
 
1428
  Typically used for row editing.
 
1429
*/
 
1430
 
 
1431
void QAbstractItemModel::revert()
 
1432
{
 
1433
    // do nothing
 
1434
}
 
1435
 
 
1436
/*!
 
1437
  Returns the data for the given \a role and \a section in the header
 
1438
  with the specified \a orientation.
 
1439
 
 
1440
  \sa Qt::ItemDataRole
 
1441
*/
 
1442
 
 
1443
QVariant QAbstractItemModel::headerData(int section, Qt::Orientation orientation, int role) const
 
1444
{
 
1445
    if (role == Qt::DisplayRole) {
 
1446
        if ((orientation == Qt::Horizontal && section < columnCount())
 
1447
            || (orientation == Qt::Vertical && section < rowCount()))
 
1448
            return QString::number(section + 1);
 
1449
    } else if (role == Qt::TextAlignmentRole)
 
1450
        return Qt::AlignVCenter;
 
1451
    return QVariant();
 
1452
}
 
1453
 
 
1454
/*!
 
1455
  Sets the \a role for the header \a section to \a value.
 
1456
  The \a orientation gives the orientation of the header.
 
1457
 
 
1458
  \sa headerData()
 
1459
*/
 
1460
 
 
1461
bool QAbstractItemModel::setHeaderData(int section, Qt::Orientation orientation,
 
1462
                                       const QVariant &value, int role)
 
1463
{
 
1464
    Q_UNUSED(section);
 
1465
    Q_UNUSED(orientation);
 
1466
    Q_UNUSED(value);
 
1467
    Q_UNUSED(role);
 
1468
    return false;
 
1469
}
 
1470
 
 
1471
/*!
 
1472
    \fn QModelIndex QAbstractItemModel::createIndex(int row, int column, void *ptr) const
 
1473
 
 
1474
    Creates a model index for the given \a row and \a column with the internal pointer \a ptr.
 
1475
 
 
1476
    This function provides a consistent interface that model subclasses must
 
1477
    use to create model indexes.
 
1478
*/
 
1479
 
 
1480
/*!
 
1481
    \fn QModelIndex QAbstractItemModel::createIndex(int row, int column, int id) const
 
1482
 
 
1483
    Creates a model index for the given \a row and \a column with the internal identifier \a id.
 
1484
 
 
1485
    This function provides a consistent interface that model subclasses must
 
1486
    use to create model indexes.
 
1487
*/
 
1488
 
 
1489
/*!
 
1490
  \internal
 
1491
*/
 
1492
void QAbstractItemModel::encodeData(const QModelIndexList &indexes, QDataStream &stream) const
 
1493
{
 
1494
    QModelIndexList::ConstIterator it = indexes.begin();
 
1495
    for (; it != indexes.end(); ++it) {
 
1496
        stream << (*it).row();
 
1497
        stream << (*it).column();
 
1498
        stream << itemData(*it);
 
1499
    }
 
1500
}
 
1501
 
 
1502
/*!
 
1503
  \internal
 
1504
 */
 
1505
bool QAbstractItemModel::decodeData(int row, int column, const QModelIndex &parent,
 
1506
                                    QDataStream &stream)
 
1507
{
 
1508
    int top = INT_MAX;
 
1509
    int left = INT_MAX;
 
1510
    int bottom = 0;
 
1511
    int right = 0;
 
1512
    QVector<QMap<int, QVariant> > data;
 
1513
    QVector<int> rows;
 
1514
    QVector<int> columns;
 
1515
    // get data, positions and dimensions of the dragged data (the positions are from the source table)
 
1516
    while (!stream.atEnd()) {
 
1517
        int r, c;
 
1518
        QMap<int, QVariant> d;
 
1519
 
 
1520
        stream >> r;
 
1521
        stream >> c;
 
1522
        stream >> d;
 
1523
 
 
1524
        rows.append(r);
 
1525
        columns.append(c);
 
1526
        data.append(d);
 
1527
 
 
1528
        top = qMin(r, top);
 
1529
        left = qMin(c, left);
 
1530
        bottom = qMax(r, bottom);
 
1531
        right = qMax(c, right);
 
1532
    }
 
1533
 
 
1534
    // insert the dragged items into the table, use a bit array to avoid overwriting items,
 
1535
    // since items from different tables can have the same row and column
 
1536
    int dragRowCount = bottom - top + 1 ;
 
1537
    int dragColumnCount = right - left + 1;
 
1538
    QBitArray isWrittenTo(dragRowCount * dragColumnCount);
 
1539
 
 
1540
    // make space in the table for the dropped data
 
1541
    int colCount = columnCount(parent);
 
1542
    if (colCount == 0) {
 
1543
        insertColumns(colCount, dragColumnCount - colCount, parent);
 
1544
        colCount = columnCount(parent);
 
1545
    }
 
1546
    insertRows(row, dragRowCount, parent);
 
1547
 
 
1548
    row = qMax(0, row);
 
1549
    column = qMax(0, column);
 
1550
 
 
1551
    // set the data in the table
 
1552
    for (int j = 0; j < data.size(); ++j) {
 
1553
        int relativeRow = rows.at(j) - top;
 
1554
        int relativeColumn = columns.at(j) - left;
 
1555
        int destinationRow = relativeRow + row;
 
1556
        int destinationColumn = relativeColumn + column;
 
1557
        int flat = (relativeRow * dragColumnCount) + relativeColumn;
 
1558
        // if the item was already written to, or we just can't fit it in the table, create a new row
 
1559
        if (destinationColumn >= colCount || isWrittenTo.testBit(flat)) {
 
1560
            destinationColumn = qBound(column, destinationColumn, colCount - 1);
 
1561
            destinationRow = row + dragRowCount;
 
1562
            insertRows(row + dragRowCount, 1, parent);
 
1563
            flat = (dragRowCount * dragColumnCount) + relativeColumn;
 
1564
            isWrittenTo.resize(++dragRowCount * dragColumnCount);
 
1565
        }
 
1566
        if (!isWrittenTo.testBit(flat)) {
 
1567
            QModelIndex idx = index(destinationRow, destinationColumn, parent);
 
1568
            setItemData(idx, data.at(j));
 
1569
            isWrittenTo.setBit(flat);
 
1570
        }
 
1571
    }
 
1572
 
 
1573
    return true;
 
1574
}
 
1575
 
 
1576
/*!
 
1577
    Begins a row insertion operation.
 
1578
 
 
1579
    When reimplementing insertRows() in a subclass, you must call this
 
1580
    function \e before inserting data into the model's underlying data
 
1581
    store.
 
1582
 
 
1583
    The \a parent index corresponds to the parent into which the new
 
1584
    rows are inserted; \a first and \a last are the row numbers of the new
 
1585
    rows to be inserted.
 
1586
 
 
1587
    \sa endInsertRows()
 
1588
*/
 
1589
void QAbstractItemModel::beginInsertRows(const QModelIndex &parent, int first, int last)
 
1590
{
 
1591
    Q_D(QAbstractItemModel);
 
1592
    d->change.parent = parent;
 
1593
    d->change.first = first;
 
1594
    d->change.last = last;
 
1595
    emit rowsAboutToBeInserted(parent, first, last);
 
1596
    d->rowsAboutToBeInserted(parent, first, last);
 
1597
}
 
1598
 
 
1599
/*!
 
1600
    Ends a row insertion operation.
 
1601
 
 
1602
    When reimplementing insertRows() in a subclass, you must call this
 
1603
    function \e after inserting data into the model's underlying data
 
1604
    store.
 
1605
 
 
1606
    \sa beginInsertRows()
 
1607
*/
 
1608
void QAbstractItemModel::endInsertRows()
 
1609
{
 
1610
    Q_D(QAbstractItemModel);
 
1611
    d->rowsInserted(d->change.parent, d->change.first, d->change.last);
 
1612
    emit rowsInserted(d->change.parent, d->change.first, d->change.last);
 
1613
}
 
1614
 
 
1615
/*!
 
1616
    Begins a row removal operation.
 
1617
 
 
1618
    When reimplementing removeRows() in a subclass, you must call this
 
1619
    function \e before removing data from the model's underlying data
 
1620
    store.
 
1621
 
 
1622
    The \a parent index corresponds to the parent from which the new
 
1623
    rows are removed; \a first and \a last are the row numbers of the
 
1624
    rows to be removed.
 
1625
 
 
1626
    \sa endRemoveRows()
 
1627
*/
 
1628
void QAbstractItemModel::beginRemoveRows(const QModelIndex &parent, int first, int last)
 
1629
{
 
1630
    Q_D(QAbstractItemModel);
 
1631
    d->change.parent = parent;
 
1632
    d->change.first = first;
 
1633
    d->change.last = last;
 
1634
    emit rowsAboutToBeRemoved(parent, first, last);
 
1635
    d->rowsAboutToBeRemoved(parent, first, last);
 
1636
}
 
1637
 
 
1638
/*!
 
1639
    Ends a row removal operation.
 
1640
 
 
1641
    When reimplementing removeRows() in a subclass, you must call this
 
1642
    function \e after removing data from the model's underlying data
 
1643
    store.
 
1644
 
 
1645
    \sa beginRemoveRows()
 
1646
*/
 
1647
void QAbstractItemModel::endRemoveRows()
 
1648
{
 
1649
    Q_D(QAbstractItemModel);
 
1650
    d->rowsRemoved(d->change.parent, d->change.first, d->change.last);
 
1651
    emit rowsRemoved(d->change.parent, d->change.first, d->change.last);
 
1652
}
 
1653
 
 
1654
/*!
 
1655
    Begins a column insertion operation.
 
1656
 
 
1657
    When reimplementing insertColumns() in a subclass, you must call this
 
1658
    function \e before inserting data into the model's underlying data
 
1659
    store.
 
1660
 
 
1661
    The \a parent index corresponds to the parent into which the new
 
1662
    columns are inserted; \a first and \a last are the column numbers of
 
1663
    the new columns to be inserted.
 
1664
 
 
1665
    \sa endInsertColumns()
 
1666
*/
 
1667
void QAbstractItemModel::beginInsertColumns(const QModelIndex &parent, int first, int last)
 
1668
{
 
1669
    Q_D(QAbstractItemModel);
 
1670
    d->change.parent = parent;
 
1671
    d->change.first = first;
 
1672
    d->change.last = last;
 
1673
    emit columnsAboutToBeInserted(parent, first, last);
 
1674
    d->columnsAboutToBeInserted(parent, first, last);
 
1675
}
 
1676
 
 
1677
/*!
 
1678
    Ends a column insertion operation.
 
1679
 
 
1680
    When reimplementing insertColumns() in a subclass, you must call this
 
1681
    function \e after inserting data into the model's underlying data
 
1682
    store.
 
1683
 
 
1684
    \sa beginInsertColumns()
 
1685
*/
 
1686
void QAbstractItemModel::endInsertColumns()
 
1687
{
 
1688
    Q_D(QAbstractItemModel);
 
1689
    d->columnsInserted(d->change.parent, d->change.first, d->change.last);
 
1690
    emit columnsInserted(d->change.parent, d->change.first, d->change.last);
 
1691
}
 
1692
 
 
1693
/*!
 
1694
    Begins a column removal operation.
 
1695
 
 
1696
    When reimplementing removeColumns() in a subclass, you must call this
 
1697
    function \e before removing data from the model's underlying data
 
1698
    store.
 
1699
 
 
1700
    The \a parent index corresponds to the parent from which the new
 
1701
    columns are removed; \a first and \a last are the column numbers of the
 
1702
    columns to be removed.
 
1703
 
 
1704
    \sa endRemoveColumns()
 
1705
*/
 
1706
void QAbstractItemModel::beginRemoveColumns(const QModelIndex &parent, int first, int last)
 
1707
{
 
1708
    Q_D(QAbstractItemModel);
 
1709
    d->change.parent = parent;
 
1710
    d->change.first = first;
 
1711
    d->change.last = last;
 
1712
    emit columnsAboutToBeRemoved(parent, first, last);
 
1713
    d->columnsAboutToBeRemoved(parent, first, last);
 
1714
}
 
1715
 
 
1716
/*!
 
1717
    Ends a column removal operation.
 
1718
 
 
1719
    When reimplementing removeColumns() in a subclass, you must call this
 
1720
    function \e after removing data from the model's underlying data
 
1721
    store.
 
1722
 
 
1723
    \sa beginRemoveColumns()
 
1724
*/
 
1725
void QAbstractItemModel::endRemoveColumns()
 
1726
{
 
1727
    Q_D(QAbstractItemModel);
 
1728
    d->columnsRemoved(d->change.parent, d->change.first, d->change.last);
 
1729
    emit columnsRemoved(d->change.parent, d->change.first, d->change.last);
 
1730
}
 
1731
 
 
1732
/*!
 
1733
    Resets the model to its original state in any attached views.
 
1734
 
 
1735
    When a model is reset it means that any previous data reported from
 
1736
    the model is now invalid and has to be queried for again.
 
1737
 
 
1738
    When a model radically changes its data it can sometimes be easier
 
1739
    to just emit this signal rather than dataChanged().
 
1740
*/
 
1741
void QAbstractItemModel::reset()
 
1742
{
 
1743
    Q_D(QAbstractItemModel);
 
1744
    emit modelReset();
 
1745
    d->reset();
 
1746
}
 
1747
 
 
1748
 
 
1749
/*!
 
1750
  Changes the QPersistentModelIndex that is equal to the given \a from
 
1751
  model index to the given \a to model index.
 
1752
 
 
1753
  If no persistent model index equal to the given \a from model index was
 
1754
  found, nothing is changed.
 
1755
*/
 
1756
void QAbstractItemModel::changePersistentIndex(const QModelIndex &from, const QModelIndex &to)
 
1757
{
 
1758
    // ### optimize (use QMap ?)
 
1759
    Q_D(QAbstractItemModel);
 
1760
    QList<QPersistentModelIndexData*> persistentIndexes = d->persistent.indexes;
 
1761
    for (int i = 0; i < persistentIndexes.count(); ++i)
 
1762
        if (persistentIndexes.at(i)->index == from)
 
1763
            persistentIndexes.at(i)->index = to; // there could be more pointing to the same index
 
1764
}
 
1765
 
 
1766
/*!
 
1767
    \class QAbstractTableModel
 
1768
    \brief The QAbstractTableModel class provides an abstract model that can be
 
1769
    subclassed to create table models.
 
1770
 
 
1771
    \ingroup model-view
 
1772
 
 
1773
    QAbstractTableModel provides a standard interface for models that represent
 
1774
    their data as a two-dimensional array of items. It is not used directly,
 
1775
    but must be subclassed.
 
1776
 
 
1777
    Since the model provides a more specialized interface than
 
1778
    QAbstractItemModel, it is not suitable for use with tree views, although
 
1779
    it can be used to provide data to a QListView. If you need to represent
 
1780
    a simple list of items, and only need a model to contain a single column
 
1781
    of data, subclassing the QAbstractListModel may be more appropriate.
 
1782
 
 
1783
    The rowCount() and columnCount() functions return the dimensions of the
 
1784
    table. To retrieve a model index corresponding to an item in the model, use
 
1785
    index() and provide only the row and column numbers.
 
1786
 
 
1787
    \section1 Subclassing
 
1788
 
 
1789
    When subclassing QAbstractTableModel, you must implement rowCount(),
 
1790
    columnCount(), and data(). Default implementations of the index() and
 
1791
    parent() functions are provided by QAbstractTableModel.
 
1792
    Well behaved models will also implement headerData().
 
1793
 
 
1794
    Editable models need to implement setData(), and implement flags() to
 
1795
    return a value containing
 
1796
    \l{Qt::ItemFlags}{Qt::ItemIsEditable}.
 
1797
 
 
1798
    Models that provide interfaces to resizable data structures can
 
1799
    provide implementations of insertRows(), removeRows(), insertColumns(),
 
1800
    and removeColumns(). When implementing these functions, it is
 
1801
    important to call the appropriate functions so that all connected views
 
1802
    are aware of any changes:
 
1803
 
 
1804
    \list
 
1805
    \o An insertRows() implementation must call beginInsertRows()
 
1806
       \e before inserting new rows into the data structure, and it must
 
1807
       call endInsertRows() \e{immediately afterwards}.
 
1808
    \o An insertColumns() implementation must call beginInsertColumns()
 
1809
       \e before inserting new columns into the data structure, and it must
 
1810
       call endInsertColumns() \e{immediately afterwards}.
 
1811
    \o A removeRows() implementation must call beginRemoveRows()
 
1812
       \e before the rows are removed from the data structure, and it must
 
1813
       call endRemoveRows() \e{immediately afterwards}.
 
1814
    \o A removeColumns() implementation must call beginRemoveColumns()
 
1815
       \e before the columns are removed from the data structure, and it must
 
1816
       call endRemoveColumns() \e{immediately afterwards}.
 
1817
    \endlist
 
1818
 
 
1819
    \sa \link model-view-programming.html Model/View Programming\endlink QAbstractItemModel QAbstractListModel
 
1820
 
 
1821
*/
 
1822
 
 
1823
/*!
 
1824
    Constructs an abstract table model for the given \a parent.
 
1825
*/
 
1826
 
 
1827
QAbstractTableModel::QAbstractTableModel(QObject *parent)
 
1828
    : QAbstractItemModel(parent)
 
1829
{
 
1830
 
 
1831
}
 
1832
 
 
1833
/*!
 
1834
    \internal
 
1835
 
 
1836
    Constructs an abstract table model with \a dd and the given \a parent.
 
1837
*/
 
1838
 
 
1839
QAbstractTableModel::QAbstractTableModel(QAbstractItemModelPrivate &dd, QObject *parent)
 
1840
    : QAbstractItemModel(dd, parent)
 
1841
{
 
1842
 
 
1843
}
 
1844
 
 
1845
/*!
 
1846
    Destroys the abstract table model.
 
1847
*/
 
1848
 
 
1849
QAbstractTableModel::~QAbstractTableModel()
 
1850
{
 
1851
 
 
1852
}
 
1853
 
 
1854
/*!
 
1855
    \fn QModelIndex QAbstractTableModel::index(int row, int column, const QModelIndex &parent = QModelIndex()) const
 
1856
 
 
1857
    Returns the index of the data in \a row and \a column with \a parent.
 
1858
 
 
1859
    \sa parent()
 
1860
*/
 
1861
 
 
1862
QModelIndex QAbstractTableModel::index(int row, int column, const QModelIndex &parent) const
 
1863
{
 
1864
    return hasIndex(row, column, parent) ? createIndex(row, column, 0) : QModelIndex();
 
1865
}
 
1866
 
 
1867
/*!
 
1868
    \fn QModelIndex QAbstractTableModel::parent(const QModelIndex &index) const
 
1869
 
 
1870
    Returns the parent of the model item with the given \a index.
 
1871
 
 
1872
    \sa index() hasChildren()
 
1873
*/
 
1874
 
 
1875
QModelIndex QAbstractTableModel::parent(const QModelIndex &) const
 
1876
{
 
1877
    return QModelIndex();
 
1878
}
 
1879
 
 
1880
bool QAbstractTableModel::hasChildren(const QModelIndex &parent) const
 
1881
{
 
1882
    return !parent.isValid();
 
1883
}
 
1884
 
 
1885
/*!
 
1886
    \class QAbstractListModel
 
1887
    \brief The QAbstractListModel class provides an abstract model that can be
 
1888
    subclassed to create one-dimensional list models.
 
1889
 
 
1890
    \ingroup model-view
 
1891
 
 
1892
    QAbstractListModel provides a standard interface for models that represent
 
1893
    their data as a simple non-hierarchical sequence of items. It is not used
 
1894
    directly, but must be subclassed.
 
1895
 
 
1896
    Since the model provides a more specialized interface than
 
1897
    QAbstractItemModel, it is not suitable for use with tree views; you will
 
1898
    need to subclass QAbstractItemModel if you want to provide a model for
 
1899
    that purpose. If you need to use a number of list models to manage data,
 
1900
    it may be more appropriate to subclass QAbstractTableModel class instead.
 
1901
 
 
1902
    Simple models can be created by subclassing this class and implementing
 
1903
    the minimum number of required functions. For example, we could implement
 
1904
    a simple read-only QStringList-based model that provides a list of strings
 
1905
    to a QListView widget. In such a case, we only need to implement the
 
1906
    rowCount() function to return the number of items in the list, and the
 
1907
    data() function to retrieve items from the list.
 
1908
 
 
1909
    Since the model represents a one-dimensional structure, the rowCount()
 
1910
    function returns the total number of items in the model. The columnCount()
 
1911
    function is implemented for interoperability with all kinds of views, but
 
1912
    by default informs views that the model contains only one column.
 
1913
 
 
1914
    \section1 Subclassing
 
1915
 
 
1916
    When subclassing QAbstractListModel, you must provide implementations
 
1917
    of the rowCount() and data() functions. Well behaved models also provide
 
1918
    a headerData() implementation.
 
1919
 
 
1920
    For editable list models, you must also provide an implementation of
 
1921
    setData(), implement the flags() function so that it returns a value
 
1922
    containing \l{Qt::ItemFlags}{Qt::ItemIsEditable}.
 
1923
 
 
1924
    Note that QAbstractListModel provides a default implementation of
 
1925
    columnCount() that informs views that there is only a single column
 
1926
    of items in this model.
 
1927
 
 
1928
    Models that provide interfaces to resizable list-like data structures
 
1929
    can provide implementations of insertRows() and removeRows(). When
 
1930
    implementing these functions, it is important to call the appropriate
 
1931
    functions so that all connected views are aware of any changes:
 
1932
 
 
1933
    \list
 
1934
    \o An insertRows() implementation must call beginInsertRows()
 
1935
       \e before inserting new rows into the data structure, and it must
 
1936
       call endInsertRows() \e{immediately afterwards}.
 
1937
    \o A removeRows() implementation must call beginRemoveRows()
 
1938
       \e before the rows are removed from the data structure, and it must
 
1939
       call endRemoveRows() \e{immediately afterwards}.
 
1940
    \endlist
 
1941
 
 
1942
    \sa \link model-view-programming.html Model/View Programming\endlink QAbstractItemView QAbstractTableModel
 
1943
 
 
1944
*/
 
1945
 
 
1946
/*!
 
1947
    Constructs an abstract list model with the given \a parent.
 
1948
*/
 
1949
 
 
1950
QAbstractListModel::QAbstractListModel(QObject *parent)
 
1951
    : QAbstractItemModel(parent)
 
1952
{
 
1953
 
 
1954
}
 
1955
 
 
1956
/*!
 
1957
    \internal
 
1958
 
 
1959
    Constructs an abstract list model with \a dd and the given \a parent.
 
1960
*/
 
1961
 
 
1962
QAbstractListModel::QAbstractListModel(QAbstractItemModelPrivate &dd, QObject *parent)
 
1963
    : QAbstractItemModel(dd, parent)
 
1964
{
 
1965
 
 
1966
}
 
1967
 
 
1968
/*!
 
1969
    Destroys the abstract list model.
 
1970
*/
 
1971
 
 
1972
QAbstractListModel::~QAbstractListModel()
 
1973
{
 
1974
 
 
1975
}
 
1976
 
 
1977
/*!
 
1978
    \fn QModelIndex QAbstractListModel::index(int row, int column, const QModelIndex &parent = QModelIndex()) const
 
1979
 
 
1980
    Returns the index of the data in \a row and \a column with \a parent.
 
1981
 
 
1982
    \sa parent()
 
1983
*/
 
1984
 
 
1985
QModelIndex QAbstractListModel::index(int row, int column, const QModelIndex &parent) const
 
1986
{
 
1987
    return hasIndex(row, column, parent) ? createIndex(row, column, 0) : QModelIndex();
 
1988
}
 
1989
 
 
1990
/*!
 
1991
    \fn QModelIndex QAbstractListModel::parent(const QModelIndex &index) const
 
1992
 
 
1993
    Returns the parent of the model item with the given \a index.
 
1994
 
 
1995
    \sa index() hasChildren()
 
1996
*/
 
1997
 
 
1998
QModelIndex QAbstractListModel::parent(const QModelIndex &) const
 
1999
{
 
2000
    return QModelIndex();
 
2001
}
 
2002
 
 
2003
/*!
 
2004
    \internal
 
2005
 
 
2006
    Returns the number of columns in the list with the given \a parent.
 
2007
 
 
2008
    \sa rowCount()
 
2009
*/
 
2010
 
 
2011
int QAbstractListModel::columnCount(const QModelIndex &) const
 
2012
{
 
2013
    return 1;
 
2014
}
 
2015
 
 
2016
bool QAbstractListModel::hasChildren(const QModelIndex &parent) const
 
2017
{
 
2018
    return !parent.isValid();
 
2019
}