~ubuntu-branches/ubuntu/wily/qtbase-opensource-src/wily

« back to all changes in this revision

Viewing changes to src/gui/itemmodels/qstandarditemmodel.cpp

  • Committer: Package Import Robot
  • Author(s): Timo Jyrinki
  • Date: 2013-02-05 12:46:17 UTC
  • Revision ID: package-import@ubuntu.com-20130205124617-c8jouts182j002fx
Tags: upstream-5.0.1+dfsg
ImportĀ upstreamĀ versionĀ 5.0.1+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
 
4
** Contact: http://www.qt-project.org/legal
 
5
**
 
6
** This file is part of the QtGui module of the Qt Toolkit.
 
7
**
 
8
** $QT_BEGIN_LICENSE:LGPL$
 
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 Digia.  For licensing terms and
 
14
** conditions see http://qt.digia.com/licensing.  For further information
 
15
** use the contact form at http://qt.digia.com/contact-us.
 
16
**
 
17
** GNU Lesser General Public License Usage
 
18
** Alternatively, this file may be used under the terms of the GNU Lesser
 
19
** General Public License version 2.1 as published by the Free Software
 
20
** Foundation and appearing in the file LICENSE.LGPL included in the
 
21
** packaging of this file.  Please review the following information to
 
22
** ensure the GNU Lesser General Public License version 2.1 requirements
 
23
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 
24
**
 
25
** In addition, as a special exception, Digia gives you certain additional
 
26
** rights.  These rights are described in the Digia Qt LGPL Exception
 
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
 
28
**
 
29
** GNU General Public License Usage
 
30
** Alternatively, this file may be used under the terms of the GNU
 
31
** General Public License version 3.0 as published by the Free Software
 
32
** Foundation and appearing in the file LICENSE.GPL included in the
 
33
** packaging of this file.  Please review the following information to
 
34
** ensure the GNU General Public License version 3.0 requirements will be
 
35
** met: http://www.gnu.org/copyleft/gpl.html.
 
36
**
 
37
**
 
38
** $QT_END_LICENSE$
 
39
**
 
40
****************************************************************************/
 
41
 
 
42
#include "qstandarditemmodel.h"
 
43
 
 
44
#ifndef QT_NO_STANDARDITEMMODEL
 
45
 
 
46
#include <QtCore/qdatetime.h>
 
47
#include <QtCore/qlist.h>
 
48
#include <QtCore/qmap.h>
 
49
#include <QtCore/qpair.h>
 
50
#include <QtCore/qvariant.h>
 
51
#include <QtCore/qvector.h>
 
52
#include <QtCore/qstringlist.h>
 
53
#include <QtCore/qbitarray.h>
 
54
#include <QtCore/qmimedata.h>
 
55
 
 
56
#include <private/qstandarditemmodel_p.h>
 
57
#include <qdebug.h>
 
58
 
 
59
QT_BEGIN_NAMESPACE
 
60
 
 
61
class QStandardItemModelLessThan
 
62
{
 
63
public:
 
64
    inline QStandardItemModelLessThan()
 
65
        { }
 
66
 
 
67
    inline bool operator()(const QPair<QStandardItem*, int> &l,
 
68
                           const QPair<QStandardItem*, int> &r) const
 
69
    {
 
70
        return *(l.first) < *(r.first);
 
71
    }
 
72
};
 
73
 
 
74
class QStandardItemModelGreaterThan
 
75
{
 
76
public:
 
77
    inline QStandardItemModelGreaterThan()
 
78
        { }
 
79
 
 
80
    inline bool operator()(const QPair<QStandardItem*, int> &l,
 
81
                           const QPair<QStandardItem*, int> &r) const
 
82
    {
 
83
        return *(r.first) < *(l.first);
 
84
    }
 
85
};
 
86
 
 
87
/*!
 
88
  \internal
 
89
*/
 
90
QStandardItemPrivate::~QStandardItemPrivate()
 
91
{
 
92
    QVector<QStandardItem*>::const_iterator it;
 
93
    for (it = children.constBegin(); it != children.constEnd(); ++it) {
 
94
        QStandardItem *child = *it;
 
95
        if (child)
 
96
            child->d_func()->setModel(0);
 
97
        delete child;
 
98
    }
 
99
    children.clear();
 
100
    if (parent && model)
 
101
        parent->d_func()->childDeleted(q_func());
 
102
}
 
103
 
 
104
/*!
 
105
  \internal
 
106
*/
 
107
QPair<int, int> QStandardItemPrivate::position() const
 
108
{
 
109
    if (QStandardItem *par = parent) {
 
110
        int idx = par->d_func()->childIndex(q_func());
 
111
        if (idx == -1)
 
112
            return QPair<int, int>(-1, -1);
 
113
        return QPair<int, int>(idx / par->columnCount(), idx % par->columnCount());
 
114
    }
 
115
    // ### support header items?
 
116
    return QPair<int, int>(-1, -1);
 
117
}
 
118
 
 
119
/*!
 
120
  \internal
 
121
*/
 
122
void QStandardItemPrivate::setChild(int row, int column, QStandardItem *item,
 
123
                                    bool emitChanged)
 
124
{
 
125
    Q_Q(QStandardItem);
 
126
    if (item == q) {
 
127
        qWarning("QStandardItem::setChild: Can't make an item a child of itself %p",
 
128
                 item);
 
129
        return;
 
130
    }
 
131
    if ((row < 0) || (column < 0))
 
132
        return;
 
133
    if (rows <= row)
 
134
        q->setRowCount(row + 1);
 
135
    if (columns <= column)
 
136
        q->setColumnCount(column + 1);
 
137
    int index = childIndex(row, column);
 
138
    Q_ASSERT(index != -1);
 
139
    QStandardItem *oldItem = children.at(index);
 
140
    if (item == oldItem)
 
141
        return;
 
142
 
 
143
    if (model && emitChanged) {
 
144
        emit model->layoutAboutToBeChanged();
 
145
    }
 
146
 
 
147
    if (item) {
 
148
        if (item->d_func()->parent == 0) {
 
149
            item->d_func()->setParentAndModel(q, model);
 
150
        } else {
 
151
            qWarning("QStandardItem::setChild: Ignoring duplicate insertion of item %p",
 
152
                     item);
 
153
            return;
 
154
        }
 
155
    }
 
156
    if (oldItem)
 
157
        oldItem->d_func()->setModel(0);
 
158
    delete oldItem;
 
159
    children.replace(index, item);
 
160
 
 
161
    if (model && emitChanged)
 
162
        emit model->layoutChanged();
 
163
 
 
164
    if (emitChanged && model)
 
165
        model->d_func()->itemChanged(item);
 
166
}
 
167
 
 
168
 
 
169
/*!
 
170
  \internal
 
171
*/
 
172
void QStandardItemPrivate::changeFlags(bool enable, Qt::ItemFlags f)
 
173
{
 
174
    Q_Q(QStandardItem);
 
175
    Qt::ItemFlags flags = q->flags();
 
176
    if (enable)
 
177
        flags |= f;
 
178
    else
 
179
        flags &= ~f;
 
180
    q->setFlags(flags);
 
181
}
 
182
 
 
183
/*!
 
184
  \internal
 
185
*/
 
186
void QStandardItemPrivate::childDeleted(QStandardItem *child)
 
187
{
 
188
    int index = childIndex(child);
 
189
    Q_ASSERT(index != -1);
 
190
    children.replace(index, 0);
 
191
}
 
192
 
 
193
/*!
 
194
  \internal
 
195
*/
 
196
void QStandardItemPrivate::setItemData(const QMap<int, QVariant> &roles)
 
197
{
 
198
    Q_Q(QStandardItem);
 
199
 
 
200
    //let's build the vector of new values
 
201
    QVector<QStandardItemData> newValues;
 
202
    QMap<int, QVariant>::const_iterator it;
 
203
    for (it = roles.begin(); it != roles.end(); ++it) {
 
204
        QVariant value = it.value();
 
205
        if (value.isValid()) {
 
206
            int role = it.key();
 
207
            role = (role == Qt::EditRole) ? Qt::DisplayRole : role;
 
208
            QStandardItemData wid(role,it.value());
 
209
            newValues.append(wid);
 
210
        }
 
211
    }
 
212
 
 
213
    if (values!=newValues) {
 
214
        values=newValues;
 
215
        if (model)
 
216
            model->d_func()->itemChanged(q);
 
217
    }
 
218
}
 
219
 
 
220
/*!
 
221
  \internal
 
222
*/
 
223
const QMap<int, QVariant> QStandardItemPrivate::itemData() const
 
224
{
 
225
    QMap<int, QVariant> result;
 
226
    QVector<QStandardItemData>::const_iterator it;
 
227
    for (it = values.begin(); it != values.end(); ++it)
 
228
        result.insert((*it).role, (*it).value);
 
229
    return result;
 
230
}
 
231
 
 
232
/*!
 
233
  \internal
 
234
*/
 
235
void QStandardItemPrivate::sortChildren(int column, Qt::SortOrder order)
 
236
{
 
237
    Q_Q(QStandardItem);
 
238
    if (column >= columnCount())
 
239
        return;
 
240
 
 
241
    QVector<QPair<QStandardItem*, int> > sortable;
 
242
    QVector<int> unsortable;
 
243
 
 
244
    sortable.reserve(rowCount());
 
245
    unsortable.reserve(rowCount());
 
246
 
 
247
    for (int row = 0; row < rowCount(); ++row) {
 
248
        QStandardItem *itm = q->child(row, column);
 
249
        if (itm)
 
250
            sortable.append(QPair<QStandardItem*,int>(itm, row));
 
251
        else
 
252
            unsortable.append(row);
 
253
    }
 
254
 
 
255
    if (order == Qt::AscendingOrder) {
 
256
        QStandardItemModelLessThan lt;
 
257
        qStableSort(sortable.begin(), sortable.end(), lt);
 
258
    } else {
 
259
        QStandardItemModelGreaterThan gt;
 
260
        qStableSort(sortable.begin(), sortable.end(), gt);
 
261
    }
 
262
 
 
263
    QModelIndexList changedPersistentIndexesFrom, changedPersistentIndexesTo;
 
264
    QVector<QStandardItem*> sorted_children(children.count());
 
265
    for (int i = 0; i < rowCount(); ++i) {
 
266
        int r = (i < sortable.count()
 
267
                 ? sortable.at(i).second
 
268
                 : unsortable.at(i - sortable.count()));
 
269
        for (int c = 0; c < columnCount(); ++c) {
 
270
            QStandardItem *itm = q->child(r, c);
 
271
            sorted_children[childIndex(i, c)] = itm;
 
272
            if (model) {
 
273
                QModelIndex from = model->createIndex(r, c, q);
 
274
                if (model->d_func()->persistent.indexes.contains(from)) {
 
275
                    QModelIndex to = model->createIndex(i, c, q);
 
276
                    changedPersistentIndexesFrom.append(from);
 
277
                    changedPersistentIndexesTo.append(to);
 
278
                }
 
279
            }
 
280
        }
 
281
    }
 
282
 
 
283
    children = sorted_children;
 
284
 
 
285
    if (model) {
 
286
        model->changePersistentIndexList(changedPersistentIndexesFrom, changedPersistentIndexesTo);
 
287
    }
 
288
 
 
289
    QVector<QStandardItem*>::iterator it;
 
290
    for (it = children.begin(); it != children.end(); ++it) {
 
291
        if (*it)
 
292
            (*it)->d_func()->sortChildren(column, order);
 
293
    }
 
294
}
 
295
 
 
296
/*!
 
297
  \internal
 
298
  set the model of this item and all its children
 
299
  */
 
300
void QStandardItemPrivate::setModel(QStandardItemModel *mod)
 
301
{
 
302
    if (children.isEmpty()) {
 
303
        if (model)
 
304
            model->d_func()->invalidatePersistentIndex(model->indexFromItem(q_ptr));
 
305
        model = mod;
 
306
    } else {
 
307
        QStack<QStandardItem*> stack;
 
308
        stack.push(q_ptr);
 
309
        while (!stack.isEmpty()) {
 
310
            QStandardItem *itm = stack.pop();
 
311
            if (itm->d_func()->model) {
 
312
                itm->d_func()->model->d_func()->invalidatePersistentIndex(itm->d_func()->model->indexFromItem(itm));
 
313
            }
 
314
            itm->d_func()->model = mod;
 
315
            const QVector<QStandardItem*> &childList = itm->d_func()->children;
 
316
            for (int i = 0; i < childList.count(); ++i) {
 
317
                QStandardItem *chi = childList.at(i);
 
318
                if (chi)
 
319
                    stack.push(chi);
 
320
            }
 
321
        }
 
322
    }
 
323
}
 
324
 
 
325
/*!
 
326
  \internal
 
327
*/
 
328
QStandardItemModelPrivate::QStandardItemModelPrivate()
 
329
    : root(new QStandardItem),
 
330
      itemPrototype(0),
 
331
      sortRole(Qt::DisplayRole)
 
332
{
 
333
    root->setFlags(Qt::ItemIsDropEnabled);
 
334
}
 
335
 
 
336
/*!
 
337
  \internal
 
338
*/
 
339
QStandardItemModelPrivate::~QStandardItemModelPrivate()
 
340
{
 
341
    delete itemPrototype;
 
342
    qDeleteAll(columnHeaderItems);
 
343
    qDeleteAll(rowHeaderItems);
 
344
}
 
345
 
 
346
/*!
 
347
  \internal
 
348
*/
 
349
void QStandardItemModelPrivate::init()
 
350
{
 
351
    Q_Q(QStandardItemModel);
 
352
    QObject::connect(q, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
 
353
                     q, SLOT(_q_emitItemChanged(QModelIndex,QModelIndex)));
 
354
}
 
355
 
 
356
/*!
 
357
    \internal
 
358
*/
 
359
void QStandardItemModelPrivate::_q_emitItemChanged(const QModelIndex &topLeft,
 
360
                                                   const QModelIndex &bottomRight)
 
361
{
 
362
    Q_Q(QStandardItemModel);
 
363
    QModelIndex parent = topLeft.parent();
 
364
    for (int row = topLeft.row(); row <= bottomRight.row(); ++row) {
 
365
        for (int column = topLeft.column(); column <= bottomRight.column(); ++column) {
 
366
            QModelIndex index = q->index(row, column, parent);
 
367
            if (QStandardItem *item = itemFromIndex(index))
 
368
                emit q->itemChanged(item);
 
369
        }
 
370
    }
 
371
}
 
372
 
 
373
/*!
 
374
    \internal
 
375
*/
 
376
bool QStandardItemPrivate::insertRows(int row, const QList<QStandardItem*> &items)
 
377
{
 
378
    Q_Q(QStandardItem);
 
379
    if ((row < 0) || (row > rowCount()))
 
380
        return false;
 
381
    int count = items.count();
 
382
    if (model)
 
383
        model->d_func()->rowsAboutToBeInserted(q, row, row + count - 1);
 
384
    if (rowCount() == 0) {
 
385
        if (columnCount() == 0)
 
386
            q->setColumnCount(1);
 
387
        children.resize(columnCount() * count);
 
388
        rows = count;
 
389
    } else {
 
390
        rows += count;
 
391
        int index = childIndex(row, 0);
 
392
        if (index != -1)
 
393
            children.insert(index, columnCount() * count, 0);
 
394
    }
 
395
    for (int i = 0; i < items.count(); ++i) {
 
396
        QStandardItem *item = items.at(i);
 
397
        item->d_func()->model = model;
 
398
        item->d_func()->parent = q;
 
399
        int index = childIndex(i + row, 0);
 
400
        children.replace(index, item);
 
401
    }
 
402
    if (model)
 
403
        model->d_func()->rowsInserted(q, row, count);
 
404
    return true;
 
405
}
 
406
 
 
407
bool QStandardItemPrivate::insertRows(int row, int count, const QList<QStandardItem*> &items)
 
408
{
 
409
    Q_Q(QStandardItem);
 
410
    if ((count < 1) || (row < 0) || (row > rowCount()))
 
411
        return false;
 
412
    if (model)
 
413
        model->d_func()->rowsAboutToBeInserted(q, row, row + count - 1);
 
414
    if (rowCount() == 0) {
 
415
        children.resize(columnCount() * count);
 
416
        rows = count;
 
417
    } else {
 
418
        rows += count;
 
419
        int index = childIndex(row, 0);
 
420
        if (index != -1)
 
421
            children.insert(index, columnCount() * count, 0);
 
422
    }
 
423
    if (!items.isEmpty()) {
 
424
        int index = childIndex(row, 0);
 
425
        int limit = qMin(items.count(), columnCount() * count);
 
426
        for (int i = 0; i < limit; ++i) {
 
427
            QStandardItem *item = items.at(i);
 
428
            if (item) {
 
429
                if (item->d_func()->parent == 0) {
 
430
                    item->d_func()->setParentAndModel(q, model);
 
431
                } else {
 
432
                    qWarning("QStandardItem::insertRows: Ignoring duplicate insertion of item %p",
 
433
                             item);
 
434
                    item = 0;
 
435
                }
 
436
            }
 
437
            children.replace(index, item);
 
438
            ++index;
 
439
        }
 
440
    }
 
441
    if (model)
 
442
        model->d_func()->rowsInserted(q, row, count);
 
443
    return true;
 
444
}
 
445
 
 
446
/*!
 
447
    \internal
 
448
*/
 
449
bool QStandardItemPrivate::insertColumns(int column, int count, const QList<QStandardItem*> &items)
 
450
{
 
451
    Q_Q(QStandardItem);
 
452
    if ((count < 1) || (column < 0) || (column > columnCount()))
 
453
        return false;
 
454
    if (model)
 
455
        model->d_func()->columnsAboutToBeInserted(q, column, column + count - 1);
 
456
    if (columnCount() == 0) {
 
457
        children.resize(rowCount() * count);
 
458
        columns = count;
 
459
    } else {
 
460
        columns += count;
 
461
        int index = childIndex(0, column);
 
462
        for (int row = 0; row < rowCount(); ++row) {
 
463
            children.insert(index, count, 0);
 
464
            index += columnCount();
 
465
        }
 
466
    }
 
467
    if (!items.isEmpty()) {
 
468
        int limit = qMin(items.count(), rowCount() * count);
 
469
        for (int i = 0; i < limit; ++i) {
 
470
            QStandardItem *item = items.at(i);
 
471
            if (item) {
 
472
                if (item->d_func()->parent == 0) {
 
473
                    item->d_func()->setParentAndModel(q, model);
 
474
                } else {
 
475
                    qWarning("QStandardItem::insertColumns: Ignoring duplicate insertion of item %p",
 
476
                             item);
 
477
                    item = 0;
 
478
                }
 
479
            }
 
480
            int r = i / count;
 
481
            int c = column + (i % count);
 
482
            int index = childIndex(r, c);
 
483
            children.replace(index, item);
 
484
        }
 
485
    }
 
486
    if (model)
 
487
        model->d_func()->columnsInserted(q, column, count);
 
488
    return true;
 
489
}
 
490
 
 
491
/*!
 
492
  \internal
 
493
*/
 
494
void QStandardItemModelPrivate::itemChanged(QStandardItem *item)
 
495
{
 
496
    Q_Q(QStandardItemModel);
 
497
    if (item->d_func()->parent == 0) {
 
498
        // Header item
 
499
        int idx = columnHeaderItems.indexOf(item);
 
500
        if (idx != -1) {
 
501
            emit q->headerDataChanged(Qt::Horizontal, idx, idx);
 
502
        } else {
 
503
            idx = rowHeaderItems.indexOf(item);
 
504
            if (idx != -1)
 
505
                emit q->headerDataChanged(Qt::Vertical, idx, idx);
 
506
        }
 
507
    } else {
 
508
        // Normal item
 
509
        QModelIndex index = q->indexFromItem(item);
 
510
        emit q->dataChanged(index, index);
 
511
    }
 
512
}
 
513
 
 
514
/*!
 
515
  \internal
 
516
*/
 
517
void QStandardItemModelPrivate::rowsAboutToBeInserted(QStandardItem *parent,
 
518
                                                      int start, int end)
 
519
{
 
520
    Q_Q(QStandardItemModel);
 
521
    QModelIndex index = q->indexFromItem(parent);
 
522
    q->beginInsertRows(index, start, end);
 
523
}
 
524
 
 
525
/*!
 
526
  \internal
 
527
*/
 
528
void QStandardItemModelPrivate::columnsAboutToBeInserted(QStandardItem *parent,
 
529
                                                         int start, int end)
 
530
{
 
531
    Q_Q(QStandardItemModel);
 
532
    QModelIndex index = q->indexFromItem(parent);
 
533
    q->beginInsertColumns(index, start, end);
 
534
}
 
535
 
 
536
/*!
 
537
  \internal
 
538
*/
 
539
void QStandardItemModelPrivate::rowsAboutToBeRemoved(QStandardItem *parent,
 
540
                                                     int start, int end)
 
541
{
 
542
    Q_Q(QStandardItemModel);
 
543
    QModelIndex index = q->indexFromItem(parent);
 
544
    q->beginRemoveRows(index, start, end);
 
545
}
 
546
 
 
547
/*!
 
548
  \internal
 
549
*/
 
550
void QStandardItemModelPrivate::columnsAboutToBeRemoved(QStandardItem *parent,
 
551
                                                        int start, int end)
 
552
{
 
553
    Q_Q(QStandardItemModel);
 
554
    QModelIndex index = q->indexFromItem(parent);
 
555
    q->beginRemoveColumns(index, start, end);
 
556
}
 
557
 
 
558
/*!
 
559
  \internal
 
560
*/
 
561
void QStandardItemModelPrivate::rowsInserted(QStandardItem *parent,
 
562
                                             int row, int count)
 
563
{
 
564
    Q_Q(QStandardItemModel);
 
565
    if (parent == root.data())
 
566
        rowHeaderItems.insert(row, count, 0);
 
567
    q->endInsertRows();
 
568
}
 
569
 
 
570
/*!
 
571
  \internal
 
572
*/
 
573
void QStandardItemModelPrivate::columnsInserted(QStandardItem *parent,
 
574
                                                int column, int count)
 
575
{
 
576
    Q_Q(QStandardItemModel);
 
577
    if (parent == root.data())
 
578
        columnHeaderItems.insert(column, count, 0);
 
579
    q->endInsertColumns();
 
580
}
 
581
 
 
582
/*!
 
583
  \internal
 
584
*/
 
585
void QStandardItemModelPrivate::rowsRemoved(QStandardItem *parent,
 
586
                                            int row, int count)
 
587
{
 
588
    Q_Q(QStandardItemModel);
 
589
    if (parent == root.data()) {
 
590
        for (int i = row; i < row + count; ++i) {
 
591
            QStandardItem *oldItem = rowHeaderItems.at(i);
 
592
            if (oldItem)
 
593
                oldItem->d_func()->setModel(0);
 
594
            delete oldItem;
 
595
        }
 
596
        rowHeaderItems.remove(row, count);
 
597
    }
 
598
    q->endRemoveRows();
 
599
}
 
600
 
 
601
/*!
 
602
  \internal
 
603
*/
 
604
void QStandardItemModelPrivate::columnsRemoved(QStandardItem *parent,
 
605
                                               int column, int count)
 
606
{
 
607
    Q_Q(QStandardItemModel);
 
608
    if (parent == root.data()) {
 
609
        for (int i = column; i < column + count; ++i) {
 
610
            QStandardItem *oldItem = columnHeaderItems.at(i);
 
611
            if (oldItem)
 
612
                oldItem->d_func()->setModel(0);
 
613
            delete oldItem;
 
614
        }
 
615
        columnHeaderItems.remove(column, count);
 
616
    }
 
617
    q->endRemoveColumns();
 
618
}
 
619
 
 
620
/*!
 
621
    \class QStandardItem
 
622
    \brief The QStandardItem class provides an item for use with the
 
623
    QStandardItemModel class.
 
624
    \since 4.2
 
625
    \ingroup model-view
 
626
    \inmodule QtGui
 
627
 
 
628
    Items usually contain text, icons, or checkboxes.
 
629
 
 
630
    Each item can have its own background brush which is set with the
 
631
    setBackground() function. The current background brush can be found with
 
632
    background().  The text label for each item can be rendered with its own
 
633
    font and brush. These are specified with the setFont() and setForeground()
 
634
    functions, and read with font() and foreground().
 
635
 
 
636
    By default, items are enabled, editable, selectable, checkable, and can be
 
637
    used both as the source of a drag and drop operation and as a drop target.
 
638
    Each item's flags can be changed by calling setFlags(). Checkable items
 
639
    can be checked and unchecked with the setCheckState() function. The
 
640
    corresponding checkState() function indicates whether the item is
 
641
    currently checked.
 
642
 
 
643
    You can store application-specific data in an item by calling setData().
 
644
 
 
645
    Each item can have a two-dimensional table of child items. This makes it
 
646
    possible to build hierarchies of items. The typical hierarchy is the tree,
 
647
    in which case the child table is a table with a single column (a list).
 
648
 
 
649
    The dimensions of the child table can be set with setRowCount() and
 
650
    setColumnCount(). Items can be positioned in the child table with
 
651
    setChild(). Get a pointer to a child item with child(). New rows and
 
652
    columns of children can also be inserted with insertRow() and
 
653
    insertColumn(), or appended with appendRow() and appendColumn(). When
 
654
    using the append and insert functions, the dimensions of the child table
 
655
    will grow as needed.
 
656
 
 
657
    An existing row of children can be removed with removeRow() or takeRow();
 
658
    correspondingly, a column can be removed with removeColumn() or
 
659
    takeColumn().
 
660
 
 
661
    An item's children can be sorted by calling sortChildren().
 
662
 
 
663
    \section1 Subclassing
 
664
 
 
665
    When subclassing QStandardItem to provide custom items, it is possible to
 
666
    define new types for them so that they can be distinguished from the base
 
667
    class. The type() function should be reimplemented to return a new type
 
668
    value equal to or greater than \l UserType.
 
669
 
 
670
    Reimplement data() and setData() if you want to perform custom handling of
 
671
    data queries and/or control how an item's data is represented.
 
672
 
 
673
    Reimplement clone() if you want QStandardItemModel to be able to create
 
674
    instances of your custom item class on demand (see
 
675
    QStandardItemModel::setItemPrototype()).
 
676
 
 
677
    Reimplement read() and write() if you want to control how items are
 
678
    represented in their serialized form.
 
679
 
 
680
    Reimplement \l{operator<()} if you want to control the semantics of item
 
681
    comparison. \l{operator<()} determines the sorted order when sorting items
 
682
    with sortChildren() or with QStandardItemModel::sort().
 
683
 
 
684
    \sa QStandardItemModel, {Item View Convenience Classes}, {Model/View Programming}
 
685
*/
 
686
 
 
687
/*!
 
688
    \enum QStandardItem::ItemType
 
689
 
 
690
    This enum describes the types that are used to describe standard items.
 
691
 
 
692
    \value Type     The default type for standard items.
 
693
    \value UserType The minimum value for custom types. Values below UserType are
 
694
                    reserved by Qt.
 
695
 
 
696
    You can define new user types in QStandardItem subclasses to ensure that
 
697
    custom items are treated specially; for example, when items are sorted.
 
698
 
 
699
    \sa type()
 
700
*/
 
701
 
 
702
/*!
 
703
    Constructs an item.
 
704
*/
 
705
QStandardItem::QStandardItem()
 
706
    : d_ptr(new QStandardItemPrivate)
 
707
{
 
708
    Q_D(QStandardItem);
 
709
    d->q_ptr = this;
 
710
}
 
711
 
 
712
/*!
 
713
    Constructs an item with the given \a text.
 
714
*/
 
715
QStandardItem::QStandardItem(const QString &text)
 
716
    : d_ptr(new QStandardItemPrivate)
 
717
{
 
718
    Q_D(QStandardItem);
 
719
    d->q_ptr = this;
 
720
    setText(text);
 
721
}
 
722
 
 
723
/*!
 
724
    Constructs an item with the given \a icon and \a text.
 
725
*/
 
726
QStandardItem::QStandardItem(const QIcon &icon, const QString &text)
 
727
    : d_ptr(new QStandardItemPrivate)
 
728
{
 
729
    Q_D(QStandardItem);
 
730
    d->q_ptr = this;
 
731
    setIcon(icon);
 
732
    setText(text);
 
733
}
 
734
 
 
735
/*!
 
736
   Constructs an item with \a rows rows and \a columns columns of child items.
 
737
*/
 
738
QStandardItem::QStandardItem(int rows, int columns)
 
739
    : d_ptr(new QStandardItemPrivate)
 
740
{
 
741
    Q_D(QStandardItem);
 
742
    d->q_ptr = this;
 
743
    setRowCount(rows);
 
744
    setColumnCount(columns);
 
745
}
 
746
 
 
747
/*!
 
748
  \internal
 
749
*/
 
750
QStandardItem::QStandardItem(QStandardItemPrivate &dd)
 
751
    : d_ptr(&dd)
 
752
{
 
753
    Q_D(QStandardItem);
 
754
    d->q_ptr = this;
 
755
}
 
756
 
 
757
/*!
 
758
  Constructs a copy of \a other. Note that model() is
 
759
  not copied.
 
760
 
 
761
  This function is useful when reimplementing clone().
 
762
*/
 
763
QStandardItem::QStandardItem(const QStandardItem &other)
 
764
    : d_ptr(new QStandardItemPrivate)
 
765
{
 
766
    Q_D(QStandardItem);
 
767
    d->q_ptr = this;
 
768
    operator=(other);
 
769
}
 
770
 
 
771
/*!
 
772
  Assigns \a other's data and flags to this item. Note that
 
773
  type() and model() are not copied.
 
774
 
 
775
  This function is useful when reimplementing clone().
 
776
*/
 
777
QStandardItem &QStandardItem::operator=(const QStandardItem &other)
 
778
{
 
779
    Q_D(QStandardItem);
 
780
    d->values = other.d_func()->values;
 
781
    return *this;
 
782
}
 
783
 
 
784
/*!
 
785
  Destructs the item.
 
786
  This causes the item's children to be destructed as well.
 
787
*/
 
788
QStandardItem::~QStandardItem()
 
789
{
 
790
}
 
791
 
 
792
/*!
 
793
  Returns the item's parent item, or 0 if the item has no parent.
 
794
  \note For toplevel items parent() returns 0. To receive toplevel
 
795
  item's parent use QStandardItemModel::invisibleRootItem() instead.
 
796
 
 
797
  \sa child(), QStandardItemModel::invisibleRootItem()
 
798
*/
 
799
QStandardItem *QStandardItem::parent() const
 
800
{
 
801
    Q_D(const QStandardItem);
 
802
    if (!d->model || (d->model->d_func()->root.data() != d->parent))
 
803
        return d->parent;
 
804
    return 0;
 
805
}
 
806
 
 
807
/*!
 
808
    Sets the item's data for the given \a role to the specified \a value.
 
809
 
 
810
    If you subclass QStandardItem and reimplement this function, your
 
811
    reimplementation should call emitDataChanged() if you do not call
 
812
    the base implementation of setData(). This will ensure that e.g.
 
813
    views using the model are notified of the changes.
 
814
 
 
815
    \note The default implementation treats Qt::EditRole and Qt::DisplayRole
 
816
    as referring to the same data.
 
817
 
 
818
    \sa Qt::ItemDataRole, data(), setFlags()
 
819
*/
 
820
void QStandardItem::setData(const QVariant &value, int role)
 
821
{
 
822
    Q_D(QStandardItem);
 
823
    role = (role == Qt::EditRole) ? Qt::DisplayRole : role;
 
824
    QVector<QStandardItemData>::iterator it;
 
825
    for (it = d->values.begin(); it != d->values.end(); ++it) {
 
826
        if ((*it).role == role) {
 
827
            if (value.isValid()) {
 
828
                if ((*it).value.type() == value.type() && (*it).value == value)
 
829
                    return;
 
830
                (*it).value = value;
 
831
            } else {
 
832
                d->values.erase(it);
 
833
            }
 
834
            if (d->model)
 
835
                d->model->d_func()->itemChanged(this);
 
836
            return;
 
837
        }
 
838
    }
 
839
    d->values.append(QStandardItemData(role, value));
 
840
    if (d->model)
 
841
        d->model->d_func()->itemChanged(this);
 
842
}
 
843
 
 
844
/*!
 
845
    Returns the item's data for the given \a role, or an invalid
 
846
    QVariant if there is no data for the role.
 
847
 
 
848
    \note The default implementation treats Qt::EditRole and Qt::DisplayRole
 
849
    as referring to the same data.
 
850
*/
 
851
QVariant QStandardItem::data(int role) const
 
852
{
 
853
    Q_D(const QStandardItem);
 
854
    role = (role == Qt::EditRole) ? Qt::DisplayRole : role;
 
855
    QVector<QStandardItemData>::const_iterator it;
 
856
    for (it = d->values.begin(); it != d->values.end(); ++it) {
 
857
        if ((*it).role == role)
 
858
            return (*it).value;
 
859
    }
 
860
    return QVariant();
 
861
}
 
862
 
 
863
/*!
 
864
  \since 4.4
 
865
 
 
866
  Causes the model associated with this item to emit a
 
867
  \l{QAbstractItemModel::dataChanged()}{dataChanged}() signal for this
 
868
  item.
 
869
 
 
870
  You normally only need to call this function if you have subclassed
 
871
  QStandardItem and reimplemented data() and/or setData().
 
872
 
 
873
  \sa setData()
 
874
*/
 
875
void QStandardItem::emitDataChanged()
 
876
{
 
877
    Q_D(QStandardItem);
 
878
    if (d->model)
 
879
        d->model->d_func()->itemChanged(this);
 
880
}
 
881
 
 
882
/*!
 
883
  Sets the item flags for the item to \a flags.
 
884
 
 
885
  The item flags determine how the user can interact with the item.
 
886
  This is often used to disable an item.
 
887
 
 
888
  \sa flags(), setData()
 
889
*/
 
890
void QStandardItem::setFlags(Qt::ItemFlags flags)
 
891
{
 
892
    setData((int)flags, Qt::UserRole - 1);
 
893
}
 
894
 
 
895
/*!
 
896
  Returns the item flags for the item.
 
897
 
 
898
  The item flags determine how the user can interact with the item.
 
899
 
 
900
  By default, items are enabled, editable, selectable, checkable, and can be
 
901
  used both as the source of a drag and drop operation and as a drop target.
 
902
 
 
903
  \sa setFlags()
 
904
*/
 
905
Qt::ItemFlags QStandardItem::flags() const
 
906
{
 
907
    QVariant v = data(Qt::UserRole - 1);
 
908
    if (!v.isValid())
 
909
        return (Qt::ItemIsSelectable|Qt::ItemIsEnabled|Qt::ItemIsEditable
 
910
                |Qt::ItemIsDragEnabled|Qt::ItemIsDropEnabled);
 
911
    return Qt::ItemFlags(v.toInt());
 
912
}
 
913
 
 
914
/*!
 
915
    \fn QString QStandardItem::text() const
 
916
 
 
917
    Returns the item's text. This is the text that's presented to the user
 
918
    in a view.
 
919
 
 
920
    \sa setText()
 
921
*/
 
922
 
 
923
/*!
 
924
    \fn void QStandardItem::setText(const QString &text)
 
925
 
 
926
    Sets the item's text to the \a text specified.
 
927
 
 
928
    \sa text(), setFont(), setForeground()
 
929
*/
 
930
 
 
931
/*!
 
932
    \fn QIcon QStandardItem::icon() const
 
933
 
 
934
    Returns the item's icon.
 
935
 
 
936
    \sa setIcon(), {QAbstractItemView::iconSize}{iconSize}
 
937
*/
 
938
 
 
939
/*!
 
940
    \fn void QStandardItem::setIcon(const QIcon &icon)
 
941
 
 
942
    Sets the item's icon to the \a icon specified.
 
943
*/
 
944
 
 
945
/*!
 
946
    \fn QString QStandardItem::statusTip() const
 
947
 
 
948
    Returns the item's status tip.
 
949
 
 
950
    \sa setStatusTip(), toolTip(), whatsThis()
 
951
*/
 
952
 
 
953
/*!
 
954
    \fn void QStandardItem::setStatusTip(const QString &statusTip)
 
955
 
 
956
    Sets the item's status tip to the string specified by \a statusTip.
 
957
 
 
958
    \sa statusTip(), setToolTip(), setWhatsThis()
 
959
*/
 
960
 
 
961
/*!
 
962
    \fn QString QStandardItem::toolTip() const
 
963
 
 
964
    Returns the item's tooltip.
 
965
 
 
966
    \sa setToolTip(), statusTip(), whatsThis()
 
967
*/
 
968
 
 
969
/*!
 
970
    \fn void QStandardItem::setToolTip(const QString &toolTip)
 
971
 
 
972
    Sets the item's tooltip to the string specified by \a toolTip.
 
973
 
 
974
    \sa toolTip(), setStatusTip(), setWhatsThis()
 
975
*/
 
976
 
 
977
/*!
 
978
    \fn QString QStandardItem::whatsThis() const
 
979
 
 
980
    Returns the item's "What's This?" help.
 
981
 
 
982
    \sa setWhatsThis(), toolTip(), statusTip()
 
983
*/
 
984
 
 
985
/*!
 
986
    \fn void QStandardItem::setWhatsThis(const QString &whatsThis)
 
987
 
 
988
    Sets the item's "What's This?" help to the string specified by \a whatsThis.
 
989
 
 
990
    \sa whatsThis(), setStatusTip(), setToolTip()
 
991
*/
 
992
 
 
993
/*!
 
994
    \fn QFont QStandardItem::font() const
 
995
 
 
996
    Returns the font used to render the item's text.
 
997
 
 
998
    \sa setFont()
 
999
*/
 
1000
 
 
1001
/*!
 
1002
    \fn void QStandardItem::setFont(const QFont &font)
 
1003
 
 
1004
    Sets the font used to display the item's text to the given \a font.
 
1005
 
 
1006
    \sa font(), setText(), setForeground()
 
1007
*/
 
1008
 
 
1009
/*!
 
1010
    \fn QBrush QStandardItem::background() const
 
1011
 
 
1012
    Returns the brush used to render the item's background.
 
1013
 
 
1014
    \sa foreground(), setBackground()
 
1015
*/
 
1016
 
 
1017
/*!
 
1018
    \fn void QStandardItem::setBackground(const QBrush &brush)
 
1019
 
 
1020
    Sets the item's background brush to the specified \a brush.
 
1021
 
 
1022
    \sa background(), setForeground()
 
1023
*/
 
1024
 
 
1025
/*!
 
1026
    \fn QBrush QStandardItem::foreground() const
 
1027
 
 
1028
    Returns the brush used to render the item's foreground (e.g. text).
 
1029
 
 
1030
    \sa setForeground(), background()
 
1031
*/
 
1032
 
 
1033
/*!
 
1034
    \fn void QStandardItem::setForeground(const QBrush &brush)
 
1035
 
 
1036
    Sets the brush used to display the item's foreground (e.g. text) to the
 
1037
    given \a brush.
 
1038
 
 
1039
    \sa foreground(), setBackground(), setFont()
 
1040
*/
 
1041
 
 
1042
/*!
 
1043
    \fn int QStandardItem::textAlignment() const
 
1044
 
 
1045
    Returns the text alignment for the item's text.
 
1046
*/
 
1047
 
 
1048
/*!
 
1049
    \fn void QStandardItem::setTextAlignment(Qt::Alignment alignment)
 
1050
 
 
1051
    Sets the text alignment for the item's text to the \a alignment
 
1052
    specified.
 
1053
 
 
1054
    \sa textAlignment()
 
1055
*/
 
1056
 
 
1057
/*!
 
1058
    \fn QSize QStandardItem::sizeHint() const
 
1059
 
 
1060
    Returns the size hint set for the item, or an invalid QSize if no
 
1061
    size hint has been set.
 
1062
 
 
1063
    If no size hint has been set, the item delegate will compute the
 
1064
    size hint based on the item data.
 
1065
 
 
1066
    \sa setSizeHint()
 
1067
*/
 
1068
 
 
1069
/*!
 
1070
    \fn void QStandardItem::setSizeHint(const QSize &size)
 
1071
 
 
1072
    Sets the size hint for the item to be \a size.
 
1073
    If no size hint is set, the item delegate will compute the
 
1074
    size hint based on the item data.
 
1075
 
 
1076
    \sa sizeHint()
 
1077
*/
 
1078
 
 
1079
/*!
 
1080
    \fn Qt::CheckState QStandardItem::checkState() const
 
1081
 
 
1082
    Returns the checked state of the item.
 
1083
 
 
1084
    \sa setCheckState(), isCheckable()
 
1085
*/
 
1086
 
 
1087
/*!
 
1088
    \fn void QStandardItem::setCheckState(Qt::CheckState state)
 
1089
 
 
1090
    Sets the check state of the item to be \a state.
 
1091
 
 
1092
    \sa checkState(), setCheckable()
 
1093
*/
 
1094
 
 
1095
/*!
 
1096
    \fn QString QStandardItem::accessibleText() const
 
1097
 
 
1098
    Returns the item's accessible text.
 
1099
 
 
1100
    The accessible text is used by assistive technologies (i.e. for users who
 
1101
    cannot use conventional means of interaction).
 
1102
 
 
1103
    \sa setAccessibleText(), accessibleDescription()
 
1104
*/
 
1105
 
 
1106
/*!
 
1107
    \fn void QStandardItem::setAccessibleText(const QString &accessibleText)
 
1108
 
 
1109
    Sets the item's accessible text to the string specified by \a accessibleText.
 
1110
 
 
1111
    The accessible text is used by assistive technologies (i.e. for users who
 
1112
    cannot use conventional means of interaction).
 
1113
 
 
1114
    \sa accessibleText(), setAccessibleDescription()
 
1115
*/
 
1116
 
 
1117
/*!
 
1118
    \fn QString QStandardItem::accessibleDescription() const
 
1119
 
 
1120
    Returns the item's accessible description.
 
1121
 
 
1122
    The accessible description is used by assistive technologies (i.e. for
 
1123
    users who cannot use conventional means of interaction).
 
1124
 
 
1125
    \sa setAccessibleDescription(), accessibleText()
 
1126
*/
 
1127
 
 
1128
/*!
 
1129
    \fn void QStandardItem::setAccessibleDescription(const QString &accessibleDescription)
 
1130
 
 
1131
    Sets the item's accessible description to the string specified by \a
 
1132
    accessibleDescription.
 
1133
 
 
1134
    The accessible description is used by assistive technologies (i.e. for
 
1135
    users who cannot use conventional means of interaction).
 
1136
 
 
1137
    \sa accessibleDescription(), setAccessibleText()
 
1138
*/
 
1139
 
 
1140
/*!
 
1141
  Sets whether the item is enabled. If \a enabled is true, the item is enabled,
 
1142
  meaning that the user can interact with the item; if \a enabled is false, the
 
1143
  user cannot interact with the item.
 
1144
 
 
1145
  This flag takes precedence over the other item flags; e.g. if an item is not
 
1146
  enabled, it cannot be selected by the user, even if the Qt::ItemIsSelectable
 
1147
  flag has been set.
 
1148
 
 
1149
  \sa isEnabled(), Qt::ItemIsEnabled, setFlags()
 
1150
*/
 
1151
void QStandardItem::setEnabled(bool enabled)
 
1152
{
 
1153
    Q_D(QStandardItem);
 
1154
    d->changeFlags(enabled, Qt::ItemIsEnabled);
 
1155
}
 
1156
 
 
1157
/*!
 
1158
  \fn bool QStandardItem::isEnabled() const
 
1159
 
 
1160
  Returns whether the item is enabled.
 
1161
 
 
1162
  When an item is enabled, the user can interact with it. The possible
 
1163
  types of interaction are specified by the other item flags, such as
 
1164
  isEditable() and isSelectable().
 
1165
 
 
1166
  The default value is true.
 
1167
 
 
1168
  \sa setEnabled(), flags()
 
1169
*/
 
1170
 
 
1171
/*!
 
1172
  Sets whether the item is editable. If \a editable is true, the item can be
 
1173
  edited by the user; otherwise, the user cannot edit the item.
 
1174
 
 
1175
  How the user can edit items in a view is determined by the view's edit
 
1176
  triggers; see QAbstractItemView::editTriggers.
 
1177
 
 
1178
  \sa isEditable(), setFlags()
 
1179
*/
 
1180
void QStandardItem::setEditable(bool editable)
 
1181
{
 
1182
    Q_D(QStandardItem);
 
1183
    d->changeFlags(editable, Qt::ItemIsEditable);
 
1184
}
 
1185
 
 
1186
/*!
 
1187
  \fn bool QStandardItem::isEditable() const
 
1188
 
 
1189
  Returns whether the item can be edited by the user.
 
1190
 
 
1191
  When an item is editable (and enabled), the user can edit the item by
 
1192
  invoking one of the view's edit triggers; see
 
1193
  QAbstractItemView::editTriggers.
 
1194
 
 
1195
  The default value is true.
 
1196
 
 
1197
  \sa setEditable(), flags()
 
1198
*/
 
1199
 
 
1200
/*!
 
1201
  Sets whether the item is selectable. If \a selectable is true, the item
 
1202
  can be selected by the user; otherwise, the user cannot select the item.
 
1203
 
 
1204
  You can control the selection behavior and mode by manipulating their
 
1205
  view properties; see QAbstractItemView::selectionMode and
 
1206
  QAbstractItemView::selectionBehavior.
 
1207
 
 
1208
  \sa isSelectable(), setFlags()
 
1209
*/
 
1210
void QStandardItem::setSelectable(bool selectable)
 
1211
{
 
1212
    Q_D(QStandardItem);
 
1213
    d->changeFlags(selectable, Qt::ItemIsSelectable);
 
1214
}
 
1215
 
 
1216
/*!
 
1217
  \fn bool QStandardItem::isSelectable() const
 
1218
 
 
1219
  Returns whether the item is selectable by the user.
 
1220
 
 
1221
  The default value is true.
 
1222
 
 
1223
  \sa setSelectable(), flags()
 
1224
*/
 
1225
 
 
1226
/*!
 
1227
  Sets whether the item is user-checkable. If \a checkable is true, the
 
1228
  item can be checked by the user; otherwise, the user cannot check
 
1229
  the item.
 
1230
 
 
1231
  The item delegate will render a checkable item with a check box next to the
 
1232
  item's text.
 
1233
 
 
1234
  \sa isCheckable(), setCheckState(), setTristate()
 
1235
*/
 
1236
void QStandardItem::setCheckable(bool checkable)
 
1237
{
 
1238
    Q_D(QStandardItem);
 
1239
    if (checkable && !isCheckable()) {
 
1240
        // make sure there's data for the checkstate role
 
1241
        if (!data(Qt::CheckStateRole).isValid())
 
1242
            setData(Qt::Unchecked, Qt::CheckStateRole);
 
1243
    }
 
1244
    d->changeFlags(checkable, Qt::ItemIsUserCheckable);
 
1245
}
 
1246
 
 
1247
/*!
 
1248
  \fn bool QStandardItem::isCheckable() const
 
1249
 
 
1250
  Returns whether the item is user-checkable.
 
1251
 
 
1252
  The default value is false.
 
1253
 
 
1254
  \sa setCheckable(), checkState(), isTristate()
 
1255
*/
 
1256
 
 
1257
/*!
 
1258
  Sets whether the item is tristate. If \a tristate is true, the
 
1259
  item is checkable with three separate states; otherwise, the item
 
1260
  is checkable with two states. (Note that this also requires that
 
1261
  the item is checkable; see isCheckable().)
 
1262
 
 
1263
  \sa isTristate(), setCheckable(), setCheckState()
 
1264
*/
 
1265
void QStandardItem::setTristate(bool tristate)
 
1266
{
 
1267
    Q_D(QStandardItem);
 
1268
    d->changeFlags(tristate, Qt::ItemIsTristate);
 
1269
}
 
1270
 
 
1271
/*!
 
1272
  \fn bool QStandardItem::isTristate() const
 
1273
 
 
1274
  Returns whether the item is tristate; that is, if it's checkable with three
 
1275
  separate states.
 
1276
 
 
1277
  The default value is false.
 
1278
 
 
1279
  \sa setTristate(), isCheckable(), checkState()
 
1280
*/
 
1281
 
 
1282
#ifndef QT_NO_DRAGANDDROP
 
1283
 
 
1284
/*!
 
1285
  Sets whether the item is drag enabled. If \a dragEnabled is true, the item
 
1286
  can be dragged by the user; otherwise, the user cannot drag the item.
 
1287
 
 
1288
  Note that you also need to ensure that item dragging is enabled in the view;
 
1289
  see QAbstractItemView::dragEnabled.
 
1290
 
 
1291
  \sa isDragEnabled(), setDropEnabled(), setFlags()
 
1292
*/
 
1293
void QStandardItem::setDragEnabled(bool dragEnabled)
 
1294
{
 
1295
    Q_D(QStandardItem);
 
1296
    d->changeFlags(dragEnabled, Qt::ItemIsDragEnabled);
 
1297
}
 
1298
 
 
1299
/*!
 
1300
  \fn bool QStandardItem::isDragEnabled() const
 
1301
 
 
1302
  Returns whether the item is drag enabled. An item that is drag enabled can
 
1303
  be dragged by the user.
 
1304
 
 
1305
  The default value is true.
 
1306
 
 
1307
  Note that item dragging must be enabled in the view for dragging to work;
 
1308
  see QAbstractItemView::dragEnabled.
 
1309
 
 
1310
  \sa setDragEnabled(), isDropEnabled(), flags()
 
1311
*/
 
1312
 
 
1313
/*!
 
1314
  Sets whether the item is drop enabled. If \a dropEnabled is true, the item
 
1315
  can be used as a drop target; otherwise, it cannot.
 
1316
 
 
1317
  Note that you also need to ensure that drops are enabled in the view; see
 
1318
  QWidget::acceptDrops(); and that the model supports the desired drop actions;
 
1319
  see QAbstractItemModel::supportedDropActions().
 
1320
 
 
1321
  \sa isDropEnabled(), setDragEnabled(), setFlags()
 
1322
*/
 
1323
void QStandardItem::setDropEnabled(bool dropEnabled)
 
1324
{
 
1325
    Q_D(QStandardItem);
 
1326
    d->changeFlags(dropEnabled, Qt::ItemIsDropEnabled);
 
1327
}
 
1328
 
 
1329
/*!
 
1330
  \fn bool QStandardItem::isDropEnabled() const
 
1331
 
 
1332
  Returns whether the item is drop enabled. When an item is drop enabled, it
 
1333
  can be used as a drop target.
 
1334
 
 
1335
  The default value is true.
 
1336
 
 
1337
  \sa setDropEnabled(), isDragEnabled(), flags()
 
1338
*/
 
1339
 
 
1340
#endif // QT_NO_DRAGANDDROP
 
1341
 
 
1342
/*!
 
1343
  Returns the row where the item is located in its parent's child table, or
 
1344
  -1 if the item has no parent.
 
1345
 
 
1346
  \sa column(), parent()
 
1347
*/
 
1348
int QStandardItem::row() const
 
1349
{
 
1350
    Q_D(const QStandardItem);
 
1351
    QPair<int, int> pos = d->position();
 
1352
    return pos.first;
 
1353
}
 
1354
 
 
1355
/*!
 
1356
  Returns the column where the item is located in its parent's child table,
 
1357
  or -1 if the item has no parent.
 
1358
 
 
1359
  \sa row(), parent()
 
1360
*/
 
1361
int QStandardItem::column() const
 
1362
{
 
1363
    Q_D(const QStandardItem);
 
1364
    QPair<int, int> pos = d->position();
 
1365
    return pos.second;
 
1366
}
 
1367
 
 
1368
/*!
 
1369
  Returns the QModelIndex associated with this item.
 
1370
 
 
1371
  When you need to invoke item functionality in a QModelIndex-based API (e.g.
 
1372
  QAbstractItemView), you can call this function to obtain an index that
 
1373
  corresponds to the item's location in the model.
 
1374
 
 
1375
  If the item is not associated with a model, an invalid QModelIndex is
 
1376
  returned.
 
1377
 
 
1378
  \sa model(), QStandardItemModel::itemFromIndex()
 
1379
*/
 
1380
QModelIndex QStandardItem::index() const
 
1381
{
 
1382
    Q_D(const QStandardItem);
 
1383
    return d->model ? d->model->indexFromItem(this) : QModelIndex();
 
1384
}
 
1385
 
 
1386
/*!
 
1387
  Returns the QStandardItemModel that this item belongs to.
 
1388
 
 
1389
  If the item is not a child of another item that belongs to the model, this
 
1390
  function returns 0.
 
1391
 
 
1392
  \sa index()
 
1393
*/
 
1394
QStandardItemModel *QStandardItem::model() const
 
1395
{
 
1396
    Q_D(const QStandardItem);
 
1397
    return d->model;
 
1398
}
 
1399
 
 
1400
/*!
 
1401
    Sets the number of child item rows to \a rows. If this is less than
 
1402
    rowCount(), the data in the unwanted rows is discarded.
 
1403
 
 
1404
    \sa rowCount(), setColumnCount()
 
1405
*/
 
1406
void QStandardItem::setRowCount(int rows)
 
1407
{
 
1408
    int rc = rowCount();
 
1409
    if (rc == rows)
 
1410
        return;
 
1411
    if (rc < rows)
 
1412
        insertRows(qMax(rc, 0), rows - rc);
 
1413
    else
 
1414
        removeRows(qMax(rows, 0), rc - rows);
 
1415
}
 
1416
 
 
1417
/*!
 
1418
    Returns the number of child item rows that the item has.
 
1419
 
 
1420
    \sa setRowCount(), columnCount()
 
1421
*/
 
1422
int QStandardItem::rowCount() const
 
1423
{
 
1424
    Q_D(const QStandardItem);
 
1425
    return d->rowCount();
 
1426
}
 
1427
 
 
1428
/*!
 
1429
    Sets the number of child item columns to \a columns. If this is less than
 
1430
    columnCount(), the data in the unwanted columns is discarded.
 
1431
 
 
1432
    \sa columnCount(), setRowCount()
 
1433
*/
 
1434
void QStandardItem::setColumnCount(int columns)
 
1435
{
 
1436
    int cc = columnCount();
 
1437
    if (cc == columns)
 
1438
        return;
 
1439
    if (cc < columns)
 
1440
        insertColumns(qMax(cc, 0), columns - cc);
 
1441
    else
 
1442
        removeColumns(qMax(columns, 0), cc - columns);
 
1443
}
 
1444
 
 
1445
/*!
 
1446
    Returns the number of child item columns that the item has.
 
1447
 
 
1448
    \sa setColumnCount(), rowCount()
 
1449
*/
 
1450
int QStandardItem::columnCount() const
 
1451
{
 
1452
    Q_D(const QStandardItem);
 
1453
    return d->columnCount();
 
1454
}
 
1455
 
 
1456
/*!
 
1457
    Inserts a row at \a row containing \a items. If necessary, the column
 
1458
    count is increased to the size of \a items.
 
1459
 
 
1460
    \sa insertRows(), insertColumn()
 
1461
*/
 
1462
void QStandardItem::insertRow(int row, const QList<QStandardItem*> &items)
 
1463
{
 
1464
    Q_D(QStandardItem);
 
1465
    if (row < 0)
 
1466
        return;
 
1467
    if (columnCount() < items.count())
 
1468
        setColumnCount(items.count());
 
1469
    d->insertRows(row, 1, items);
 
1470
}
 
1471
 
 
1472
/*!
 
1473
    Inserts \a items at \a row. The column count wont be changed.
 
1474
 
 
1475
    \sa insertRow(), insertColumn()
 
1476
*/
 
1477
void QStandardItem::insertRows(int row, const QList<QStandardItem*> &items)
 
1478
{
 
1479
    Q_D(QStandardItem);
 
1480
    if (row < 0)
 
1481
        return;
 
1482
    d->insertRows(row, items);
 
1483
}
 
1484
 
 
1485
/*!
 
1486
    Inserts a column at \a column containing \a items. If necessary,
 
1487
    the row count is increased to the size of \a items.
 
1488
 
 
1489
    \sa insertColumns(), insertRow()
 
1490
*/
 
1491
void QStandardItem::insertColumn(int column, const QList<QStandardItem*> &items)
 
1492
{
 
1493
    Q_D(QStandardItem);
 
1494
    if (column < 0)
 
1495
        return;
 
1496
    if (rowCount() < items.count())
 
1497
        setRowCount(items.count());
 
1498
    d->insertColumns(column, 1, items);
 
1499
}
 
1500
 
 
1501
/*!
 
1502
    Inserts \a count rows of child items at row \a row.
 
1503
 
 
1504
    \sa insertRow(), insertColumns()
 
1505
*/
 
1506
void QStandardItem::insertRows(int row, int count)
 
1507
{
 
1508
    Q_D(QStandardItem);
 
1509
    if (rowCount() < row) {
 
1510
        count += row - rowCount();
 
1511
        row = rowCount();
 
1512
    }
 
1513
    d->insertRows(row, count, QList<QStandardItem*>());
 
1514
}
 
1515
 
 
1516
/*!
 
1517
    Inserts \a count columns of child items at column \a column.
 
1518
 
 
1519
    \sa insertColumn(), insertRows()
 
1520
*/
 
1521
void QStandardItem::insertColumns(int column, int count)
 
1522
{
 
1523
    Q_D(QStandardItem);
 
1524
    if (columnCount() < column) {
 
1525
        count += column - columnCount();
 
1526
        column = columnCount();
 
1527
    }
 
1528
    d->insertColumns(column, count, QList<QStandardItem*>());
 
1529
}
 
1530
 
 
1531
/*!
 
1532
    \fn void QStandardItem::appendRow(const QList<QStandardItem*> &items)
 
1533
 
 
1534
    Appends a row containing \a items. If necessary, the column count is
 
1535
    increased to the size of \a items.
 
1536
 
 
1537
    \sa insertRow()
 
1538
*/
 
1539
 
 
1540
/*!
 
1541
    \fn void QStandardItem::appendRows(const QList<QStandardItem*> &items)
 
1542
 
 
1543
    Appends rows containing \a items.  The column count will not change.
 
1544
 
 
1545
    \sa insertRow()
 
1546
*/
 
1547
 
 
1548
/*!
 
1549
    \fn void QStandardItem::appendColumn(const QList<QStandardItem*> &items)
 
1550
 
 
1551
    Appends a column containing \a items. If necessary, the row count is
 
1552
    increased to the size of \a items.
 
1553
 
 
1554
    \sa insertColumn()
 
1555
*/
 
1556
 
 
1557
/*!
 
1558
    \fn bool QStandardItemModel::insertRow(int row, const QModelIndex &parent)
 
1559
 
 
1560
    Inserts a single row before the given \a row in the child items of the
 
1561
    \a parent specified. Returns true if the row is inserted; otherwise
 
1562
    returns false.
 
1563
 
 
1564
    \sa insertRows(), insertColumn(), removeRow()
 
1565
*/
 
1566
 
 
1567
/*!
 
1568
    \fn bool QStandardItemModel::insertColumn(int column, const QModelIndex &parent)
 
1569
 
 
1570
    Inserts a single column before the given \a column in the child items of
 
1571
    the \a parent specified. Returns true if the column is inserted; otherwise
 
1572
    returns false.
 
1573
 
 
1574
    \sa insertColumns(), insertRow(), removeColumn()
 
1575
*/
 
1576
 
 
1577
/*!
 
1578
    \fn QStandardItem::insertRow(int row, QStandardItem *item)
 
1579
    \overload
 
1580
 
 
1581
    Inserts a row at \a row containing \a item.
 
1582
 
 
1583
    When building a list or a tree that has only one column, this function
 
1584
    provides a convenient way to insert a single new item.
 
1585
*/
 
1586
 
 
1587
/*!
 
1588
    \fn QStandardItem::appendRow(QStandardItem *item)
 
1589
    \overload
 
1590
 
 
1591
    Appends a row containing \a item.
 
1592
 
 
1593
    When building a list or a tree that has only one column, this function
 
1594
    provides a convenient way to append a single new item.
 
1595
*/
 
1596
 
 
1597
/*!
 
1598
    Removes the given \a row. The items that were in the row are deleted.
 
1599
 
 
1600
    \sa takeRow(), removeRows(), removeColumn()
 
1601
*/
 
1602
void QStandardItem::removeRow(int row)
 
1603
{
 
1604
    removeRows(row, 1);
 
1605
}
 
1606
 
 
1607
/*!
 
1608
    Removes the given \a column. The items that were in the
 
1609
    column are deleted.
 
1610
 
 
1611
    \sa takeColumn(), removeColumns(), removeRow()
 
1612
*/
 
1613
void QStandardItem::removeColumn(int column)
 
1614
{
 
1615
    removeColumns(column, 1);
 
1616
}
 
1617
 
 
1618
/*!
 
1619
    Removes \a count rows at row \a row. The items that were in those rows are
 
1620
    deleted.
 
1621
 
 
1622
    \sa removeRow(), removeColumn()
 
1623
*/
 
1624
void QStandardItem::removeRows(int row, int count)
 
1625
{
 
1626
    Q_D(QStandardItem);
 
1627
    if ((count < 1) || (row < 0) || ((row + count) > rowCount()))
 
1628
        return;
 
1629
    if (d->model)
 
1630
        d->model->d_func()->rowsAboutToBeRemoved(this, row, row + count - 1);
 
1631
    int i = d->childIndex(row, 0);
 
1632
    int n = count * d->columnCount();
 
1633
    for (int j = i; j < n+i; ++j) {
 
1634
        QStandardItem *oldItem = d->children.at(j);
 
1635
        if (oldItem)
 
1636
            oldItem->d_func()->setModel(0);
 
1637
        delete oldItem;
 
1638
    }
 
1639
    d->children.remove(qMax(i, 0), n);
 
1640
    d->rows -= count;
 
1641
    if (d->model)
 
1642
        d->model->d_func()->rowsRemoved(this, row, count);
 
1643
}
 
1644
 
 
1645
/*!
 
1646
    Removes \a count columns at column \a column. The items that were in those
 
1647
    columns are deleted.
 
1648
 
 
1649
    \sa removeColumn(), removeRows()
 
1650
*/
 
1651
void QStandardItem::removeColumns(int column, int count)
 
1652
{
 
1653
    Q_D(QStandardItem);
 
1654
    if ((count < 1) || (column < 0) || ((column + count) > columnCount()))
 
1655
        return;
 
1656
    if (d->model)
 
1657
        d->model->d_func()->columnsAboutToBeRemoved(this, column, column + count - 1);
 
1658
    for (int row = d->rowCount() - 1; row >= 0; --row) {
 
1659
        int i = d->childIndex(row, column);
 
1660
        for (int j=i; j<i+count; ++j) {
 
1661
            QStandardItem *oldItem = d->children.at(j);
 
1662
            if (oldItem)
 
1663
                oldItem->d_func()->setModel(0);
 
1664
            delete oldItem;
 
1665
        }
 
1666
        d->children.remove(i, count);
 
1667
    }
 
1668
    d->columns -= count;
 
1669
    if (d->model)
 
1670
        d->model->d_func()->columnsRemoved(this, column, count);
 
1671
}
 
1672
 
 
1673
/*!
 
1674
    Returns true if this item has any children; otherwise returns false.
 
1675
 
 
1676
    \sa rowCount(), columnCount(), child()
 
1677
*/
 
1678
bool QStandardItem::hasChildren() const
 
1679
{
 
1680
    return (rowCount() > 0) && (columnCount() > 0);
 
1681
}
 
1682
 
 
1683
/*!
 
1684
    Sets the child item at (\a row, \a column) to \a item. This item (the parent
 
1685
    item) takes ownership of \a item. If necessary, the row count and column
 
1686
    count are increased to fit the item.
 
1687
 
 
1688
    \sa child()
 
1689
*/
 
1690
void QStandardItem::setChild(int row, int column, QStandardItem *item)
 
1691
{
 
1692
    Q_D(QStandardItem);
 
1693
    d->setChild(row, column, item, true);
 
1694
}
 
1695
 
 
1696
/*!
 
1697
    \fn QStandardItem::setChild(int row, QStandardItem *item)
 
1698
    \overload
 
1699
 
 
1700
    Sets the child at \a row to \a item.
 
1701
*/
 
1702
 
 
1703
/*!
 
1704
    Returns the child item at (\a row, \a column) if one has been set; otherwise
 
1705
    returns 0.
 
1706
 
 
1707
    \sa setChild(), takeChild(), parent()
 
1708
*/
 
1709
QStandardItem *QStandardItem::child(int row, int column) const
 
1710
{
 
1711
    Q_D(const QStandardItem);
 
1712
    int index = d->childIndex(row, column);
 
1713
    if (index == -1)
 
1714
        return 0;
 
1715
    return d->children.at(index);
 
1716
}
 
1717
 
 
1718
/*!
 
1719
    Removes the child item at (\a row, \a column) without deleting it, and returns
 
1720
    a pointer to the item. If there was no child at the given location, then
 
1721
    this function returns 0.
 
1722
 
 
1723
    Note that this function, unlike takeRow() and takeColumn(), does not affect
 
1724
    the dimensions of the child table.
 
1725
 
 
1726
    \sa child(), takeRow(), takeColumn()
 
1727
*/
 
1728
QStandardItem *QStandardItem::takeChild(int row, int column)
 
1729
{
 
1730
    Q_D(QStandardItem);
 
1731
    QStandardItem *item = 0;
 
1732
    int index = d->childIndex(row, column);
 
1733
    if (index != -1) {
 
1734
        item = d->children.at(index);
 
1735
        if (item)
 
1736
            item->d_func()->setParentAndModel(0, 0);
 
1737
        d->children.replace(index, 0);
 
1738
    }
 
1739
    return item;
 
1740
}
 
1741
 
 
1742
/*!
 
1743
    Removes \a row without deleting the row items, and returns a list of
 
1744
    pointers to the removed items. For items in the row that have not been
 
1745
    set, the corresponding pointers in the list will be 0.
 
1746
 
 
1747
    \sa removeRow(), insertRow(), takeColumn()
 
1748
*/
 
1749
QList<QStandardItem*> QStandardItem::takeRow(int row)
 
1750
{
 
1751
    Q_D(QStandardItem);
 
1752
    if ((row < 0) || (row >= rowCount()))
 
1753
        return QList<QStandardItem*>();
 
1754
    if (d->model)
 
1755
        d->model->d_func()->rowsAboutToBeRemoved(this, row, row);
 
1756
    QList<QStandardItem*> items;
 
1757
    int index = d->childIndex(row, 0);  // Will return -1 if there are no columns
 
1758
    if (index != -1) {
 
1759
        int col_count = d->columnCount();
 
1760
        for (int column = 0; column < col_count; ++column) {
 
1761
            QStandardItem *ch = d->children.at(index + column);
 
1762
            if (ch)
 
1763
                ch->d_func()->setParentAndModel(0, 0);
 
1764
            items.append(ch);
 
1765
        }
 
1766
        d->children.remove(index, col_count);
 
1767
    }
 
1768
    d->rows--;
 
1769
    if (d->model)
 
1770
        d->model->d_func()->rowsRemoved(this, row, 1);
 
1771
    return items;
 
1772
}
 
1773
 
 
1774
/*!
 
1775
    Removes \a column without deleting the column items, and returns a list of
 
1776
    pointers to the removed items. For items in the column that have not been
 
1777
    set, the corresponding pointers in the list will be 0.
 
1778
 
 
1779
    \sa removeColumn(), insertColumn(), takeRow()
 
1780
*/
 
1781
QList<QStandardItem*> QStandardItem::takeColumn(int column)
 
1782
{
 
1783
    Q_D(QStandardItem);
 
1784
    if ((column < 0) || (column >= columnCount()))
 
1785
        return QList<QStandardItem*>();
 
1786
    if (d->model)
 
1787
        d->model->d_func()->columnsAboutToBeRemoved(this, column, column);
 
1788
    QList<QStandardItem*> items;
 
1789
 
 
1790
    for (int row = d->rowCount() - 1; row >= 0; --row) {
 
1791
        int index = d->childIndex(row, column);
 
1792
        QStandardItem *ch = d->children.at(index);
 
1793
        if (ch)
 
1794
            ch->d_func()->setParentAndModel(0, 0);
 
1795
        d->children.remove(index);
 
1796
        items.prepend(ch);
 
1797
    }
 
1798
    d->columns--;
 
1799
    if (d->model)
 
1800
        d->model->d_func()->columnsRemoved(this, column, 1);
 
1801
    return items;
 
1802
}
 
1803
 
 
1804
/*!
 
1805
    Returns true if this item is less than \a other; otherwise returns false.
 
1806
 
 
1807
    The default implementation uses the data for the item's sort role (see
 
1808
    QStandardItemModel::sortRole) to perform the comparison if the item
 
1809
    belongs to a model; otherwise, the data for the item's Qt::DisplayRole
 
1810
    (text()) is used to perform the comparison.
 
1811
 
 
1812
    sortChildren() and QStandardItemModel::sort() use this function when
 
1813
    sorting items. If you want custom sorting, you can subclass QStandardItem
 
1814
    and reimplement this function.
 
1815
*/
 
1816
bool QStandardItem::operator<(const QStandardItem &other) const
 
1817
{
 
1818
    const int role = model() ? model()->sortRole() : Qt::DisplayRole;
 
1819
    const QVariant l = data(role), r = other.data(role);
 
1820
    // this code is copied from QSortFilterProxyModel::lessThan()
 
1821
    switch (l.userType()) {
 
1822
    case QVariant::Invalid:
 
1823
        return (r.type() == QVariant::Invalid);
 
1824
    case QVariant::Int:
 
1825
        return l.toInt() < r.toInt();
 
1826
    case QVariant::UInt:
 
1827
        return l.toUInt() < r.toUInt();
 
1828
    case QVariant::LongLong:
 
1829
        return l.toLongLong() < r.toLongLong();
 
1830
    case QVariant::ULongLong:
 
1831
        return l.toULongLong() < r.toULongLong();
 
1832
    case QMetaType::Float:
 
1833
        return l.toFloat() < r.toFloat();
 
1834
    case QVariant::Double:
 
1835
        return l.toDouble() < r.toDouble();
 
1836
    case QVariant::Char:
 
1837
        return l.toChar() < r.toChar();
 
1838
    case QVariant::Date:
 
1839
        return l.toDate() < r.toDate();
 
1840
    case QVariant::Time:
 
1841
        return l.toTime() < r.toTime();
 
1842
    case QVariant::DateTime:
 
1843
        return l.toDateTime() < r.toDateTime();
 
1844
    case QVariant::String:
 
1845
    default:
 
1846
        return l.toString().compare(r.toString()) < 0;
 
1847
    }
 
1848
}
 
1849
 
 
1850
/*!
 
1851
    Sorts the children of the item using the given \a order, by the values in
 
1852
    the given \a column.
 
1853
 
 
1854
    \note This function is recursive, therefore it sorts the children of the
 
1855
    item, its grandchildren, etc.
 
1856
 
 
1857
    \sa {operator<()}
 
1858
*/
 
1859
void QStandardItem::sortChildren(int column, Qt::SortOrder order)
 
1860
{
 
1861
    Q_D(QStandardItem);
 
1862
    if ((column < 0) || (rowCount() == 0))
 
1863
        return;
 
1864
 
 
1865
    QList<QPersistentModelIndex> parents;
 
1866
    if (d->model) {
 
1867
        parents << index();
 
1868
        emit d->model->layoutAboutToBeChanged(parents, QAbstractItemModel::VerticalSortHint);
 
1869
    }
 
1870
    d->sortChildren(column, order);
 
1871
    if (d->model)
 
1872
        emit d->model->layoutChanged(parents, QAbstractItemModel::VerticalSortHint);
 
1873
}
 
1874
 
 
1875
/*!
 
1876
    Returns a copy of this item. The item's children are not copied.
 
1877
 
 
1878
    When subclassing QStandardItem, you can reimplement this function
 
1879
    to provide QStandardItemModel with a factory that it can use to
 
1880
    create new items on demand.
 
1881
 
 
1882
    \sa QStandardItemModel::setItemPrototype(), operator=()
 
1883
*/
 
1884
QStandardItem *QStandardItem::clone() const
 
1885
{
 
1886
    return new QStandardItem(*this);
 
1887
}
 
1888
 
 
1889
/*!
 
1890
    Returns the type of this item. The type is used to distinguish custom
 
1891
    items from the base class. When subclassing QStandardItem, you should
 
1892
    reimplement this function and return a new value greater than or equal
 
1893
    to \l UserType.
 
1894
 
 
1895
    \sa QStandardItem::Type
 
1896
*/
 
1897
int QStandardItem::type() const
 
1898
{
 
1899
    return Type;
 
1900
}
 
1901
 
 
1902
#ifndef QT_NO_DATASTREAM
 
1903
 
 
1904
/*!
 
1905
    Reads the item from stream \a in. Only the data and flags of the item are
 
1906
    read, not the child items.
 
1907
 
 
1908
    \sa write()
 
1909
*/
 
1910
void QStandardItem::read(QDataStream &in)
 
1911
{
 
1912
    Q_D(QStandardItem);
 
1913
    in >> d->values;
 
1914
    qint32 flags;
 
1915
    in >> flags;
 
1916
    setFlags(Qt::ItemFlags(flags));
 
1917
}
 
1918
 
 
1919
/*!
 
1920
    Writes the item to stream \a out. Only the data and flags of the item
 
1921
    are written, not the child items.
 
1922
 
 
1923
    \sa read()
 
1924
*/
 
1925
void QStandardItem::write(QDataStream &out) const
 
1926
{
 
1927
    Q_D(const QStandardItem);
 
1928
    out << d->values;
 
1929
    out << flags();
 
1930
}
 
1931
 
 
1932
/*!
 
1933
    \relates QStandardItem
 
1934
    \since 4.2
 
1935
 
 
1936
    Reads a QStandardItem from stream \a in into \a item.
 
1937
 
 
1938
    This operator uses QStandardItem::read().
 
1939
 
 
1940
    \sa {Serializing Qt Data Types}
 
1941
*/
 
1942
QDataStream &operator>>(QDataStream &in, QStandardItem &item)
 
1943
{
 
1944
    item.read(in);
 
1945
    return in;
 
1946
}
 
1947
 
 
1948
/*!
 
1949
    \relates QStandardItem
 
1950
    \since 4.2
 
1951
 
 
1952
    Writes the QStandardItem \a item to stream \a out.
 
1953
 
 
1954
    This operator uses QStandardItem::write().
 
1955
 
 
1956
    \sa {Serializing Qt Data Types}
 
1957
*/
 
1958
QDataStream &operator<<(QDataStream &out, const QStandardItem &item)
 
1959
{
 
1960
    item.write(out);
 
1961
    return out;
 
1962
}
 
1963
 
 
1964
#endif // QT_NO_DATASTREAM
 
1965
 
 
1966
/*!
 
1967
    \class QStandardItemModel
 
1968
    \brief The QStandardItemModel class provides a generic model for storing custom data.
 
1969
    \ingroup model-view
 
1970
    \inmodule QtGui
 
1971
 
 
1972
    QStandardItemModel can be used as a repository for standard Qt
 
1973
    data types. It is one of the \l {Model/View Classes} and is part
 
1974
    of Qt's \l {Model/View Programming}{model/view} framework.
 
1975
 
 
1976
    QStandardItemModel provides a classic item-based approach to working with
 
1977
    the model.  The items in a QStandardItemModel are provided by
 
1978
    QStandardItem.
 
1979
 
 
1980
    QStandardItemModel implements the QAbstractItemModel interface, which
 
1981
    means that the model can be used to provide data in any view that supports
 
1982
    that interface (such as QListView, QTableView and QTreeView, and your own
 
1983
    custom views). For performance and flexibility, you may want to subclass
 
1984
    QAbstractItemModel to provide support for different kinds of data
 
1985
    repositories. For example, the QDirModel provides a model interface to the
 
1986
    underlying file system.
 
1987
 
 
1988
    When you want a list or tree, you typically create an empty
 
1989
    QStandardItemModel and use appendRow() to add items to the model, and
 
1990
    item() to access an item.  If your model represents a table, you typically
 
1991
    pass the dimensions of the table to the QStandardItemModel constructor and
 
1992
    use setItem() to position items into the table. You can also use setRowCount()
 
1993
    and setColumnCount() to alter the dimensions of the model. To insert items,
 
1994
    use insertRow() or insertColumn(), and to remove items, use removeRow() or
 
1995
    removeColumn().
 
1996
 
 
1997
    You can set the header labels of your model with setHorizontalHeaderLabels()
 
1998
    and setVerticalHeaderLabels().
 
1999
 
 
2000
    You can search for items in the model with findItems(), and sort the model by
 
2001
    calling sort().
 
2002
 
 
2003
    Call clear() to remove all items from the model.
 
2004
 
 
2005
    An example usage of QStandardItemModel to create a table:
 
2006
 
 
2007
    \snippet code/src_gui_itemviews_qstandarditemmodel.cpp 0
 
2008
 
 
2009
    An example usage of QStandardItemModel to create a tree:
 
2010
 
 
2011
    \snippet code/src_gui_itemviews_qstandarditemmodel.cpp 1
 
2012
 
 
2013
    After setting the model on a view, you typically want to react to user
 
2014
    actions, such as an item being clicked. Since a QAbstractItemView provides
 
2015
    QModelIndex-based signals and functions, you need a way to obtain the
 
2016
    QStandardItem that corresponds to a given QModelIndex, and vice
 
2017
    versa. itemFromIndex() and indexFromItem() provide this mapping. Typical
 
2018
    usage of itemFromIndex() includes obtaining the item at the current index
 
2019
    in a view, and obtaining the item that corresponds to an index carried by
 
2020
    a QAbstractItemView signal, such as QAbstractItemView::clicked(). First
 
2021
    you connect the view's signal to a slot in your class:
 
2022
 
 
2023
    \snippet code/src_gui_itemviews_qstandarditemmodel.cpp 2
 
2024
 
 
2025
    When you receive the signal, you call itemFromIndex() on the given model
 
2026
    index to get a pointer to the item:
 
2027
 
 
2028
    \snippet code/src_gui_itemviews_qstandarditemmodel.cpp 3
 
2029
 
 
2030
    Conversely, you must obtain the QModelIndex of an item when you want to
 
2031
    invoke a model/view function that takes an index as argument. You can
 
2032
    obtain the index either by using the model's indexFromItem() function, or,
 
2033
    equivalently, by calling QStandardItem::index():
 
2034
 
 
2035
    \snippet code/src_gui_itemviews_qstandarditemmodel.cpp 4
 
2036
 
 
2037
    You are, of course, not required to use the item-based approach; you could
 
2038
    instead rely entirely on the QAbstractItemModel interface when working with
 
2039
    the model, or use a combination of the two as appropriate.
 
2040
 
 
2041
    \sa QStandardItem, {Model/View Programming}, QAbstractItemModel,
 
2042
    {itemviews/simpletreemodel}{Simple Tree Model example},
 
2043
    {Item View Convenience Classes}
 
2044
*/
 
2045
 
 
2046
/*!
 
2047
    \fn void QStandardItemModel::itemChanged(QStandardItem *item)
 
2048
    \since 4.2
 
2049
 
 
2050
    This signal is emitted whenever the data of \a item has changed.
 
2051
*/
 
2052
 
 
2053
/*!
 
2054
    Constructs a new item model with the given \a parent.
 
2055
*/
 
2056
QStandardItemModel::QStandardItemModel(QObject *parent)
 
2057
    : QAbstractItemModel(*new QStandardItemModelPrivate, parent)
 
2058
{
 
2059
    Q_D(QStandardItemModel);
 
2060
    d->init();
 
2061
    d->root->d_func()->setModel(this);
 
2062
}
 
2063
 
 
2064
/*!
 
2065
    Constructs a new item model that initially has \a rows rows and \a columns
 
2066
    columns, and that has the given \a parent.
 
2067
*/
 
2068
QStandardItemModel::QStandardItemModel(int rows, int columns, QObject *parent)
 
2069
    : QAbstractItemModel(*new QStandardItemModelPrivate, parent)
 
2070
{
 
2071
    Q_D(QStandardItemModel);
 
2072
    d->init();
 
2073
    d->root->insertColumns(0, columns);
 
2074
    d->columnHeaderItems.insert(0, columns, 0);
 
2075
    d->root->insertRows(0, rows);
 
2076
    d->rowHeaderItems.insert(0, rows, 0);
 
2077
    d->root->d_func()->setModel(this);
 
2078
}
 
2079
 
 
2080
/*!
 
2081
  \internal
 
2082
*/
 
2083
QStandardItemModel::QStandardItemModel(QStandardItemModelPrivate &dd, QObject *parent)
 
2084
    : QAbstractItemModel(dd, parent)
 
2085
{
 
2086
    Q_D(QStandardItemModel);
 
2087
    d->init();
 
2088
}
 
2089
 
 
2090
/*!
 
2091
    Destructs the model. The model destroys all its items.
 
2092
*/
 
2093
QStandardItemModel::~QStandardItemModel()
 
2094
{
 
2095
}
 
2096
 
 
2097
/*!
 
2098
    Sets the item role names to \a roleNames.
 
2099
*/
 
2100
void QStandardItemModel::setItemRoleNames(const QHash<int,QByteArray> &roleNames)
 
2101
{
 
2102
    Q_D(QStandardItemModel);
 
2103
    d->roleNames = roleNames;
 
2104
}
 
2105
 
 
2106
/*!
 
2107
    Removes all items (including header items) from the model and sets the
 
2108
    number of rows and columns to zero.
 
2109
 
 
2110
    \sa removeColumns(), removeRows()
 
2111
*/
 
2112
void QStandardItemModel::clear()
 
2113
{
 
2114
    Q_D(QStandardItemModel);
 
2115
    beginResetModel();
 
2116
    d->root.reset(new QStandardItem);
 
2117
    d->root->d_func()->setModel(this);
 
2118
    qDeleteAll(d->columnHeaderItems);
 
2119
    d->columnHeaderItems.clear();
 
2120
    qDeleteAll(d->rowHeaderItems);
 
2121
    d->rowHeaderItems.clear();
 
2122
    endResetModel();
 
2123
}
 
2124
 
 
2125
/*!
 
2126
    \since 4.2
 
2127
 
 
2128
    Returns a pointer to the QStandardItem associated with the given \a index.
 
2129
 
 
2130
    Calling this function is typically the initial step when processing
 
2131
    QModelIndex-based signals from a view, such as
 
2132
    QAbstractItemView::activated(). In your slot, you call itemFromIndex(),
 
2133
    with the QModelIndex carried by the signal as argument, to obtain a
 
2134
    pointer to the corresponding QStandardItem.
 
2135
 
 
2136
    Note that this function will lazily create an item for the index (using
 
2137
    itemPrototype()), and set it in the parent item's child table, if no item
 
2138
    already exists at that index.
 
2139
 
 
2140
    If \a index is an invalid index, this function returns 0.
 
2141
 
 
2142
    \sa indexFromItem()
 
2143
*/
 
2144
QStandardItem *QStandardItemModel::itemFromIndex(const QModelIndex &index) const
 
2145
{
 
2146
    Q_D(const QStandardItemModel);
 
2147
    if ((index.row() < 0) || (index.column() < 0) || (index.model() != this))
 
2148
        return 0;
 
2149
    QStandardItem *parent = static_cast<QStandardItem*>(index.internalPointer());
 
2150
    if (parent == 0)
 
2151
        return 0;
 
2152
    QStandardItem *item = parent->child(index.row(), index.column());
 
2153
    // lazy part
 
2154
    if (item == 0) {
 
2155
        item = d->createItem();
 
2156
        parent->d_func()->setChild(index.row(), index.column(), item);
 
2157
    }
 
2158
    return item;
 
2159
}
 
2160
 
 
2161
/*!
 
2162
    \since 4.2
 
2163
 
 
2164
    Returns the QModelIndex associated with the given \a item.
 
2165
 
 
2166
    Use this function when you want to perform an operation that requires the
 
2167
    QModelIndex of the item, such as
 
2168
    QAbstractItemView::scrollTo(). QStandardItem::index() is provided as
 
2169
    convenience; it is equivalent to calling this function.
 
2170
 
 
2171
    \sa itemFromIndex(), QStandardItem::index()
 
2172
*/
 
2173
QModelIndex QStandardItemModel::indexFromItem(const QStandardItem *item) const
 
2174
{
 
2175
    if (item && item->d_func()->parent) {
 
2176
        QPair<int, int> pos = item->d_func()->position();
 
2177
        return createIndex(pos.first, pos.second, item->d_func()->parent);
 
2178
    }
 
2179
    return QModelIndex();
 
2180
}
 
2181
 
 
2182
/*!
 
2183
    \since 4.2
 
2184
 
 
2185
    Sets the number of rows in this model to \a rows. If
 
2186
    this is less than rowCount(), the data in the unwanted rows
 
2187
    is discarded.
 
2188
 
 
2189
    \sa setColumnCount()
 
2190
*/
 
2191
void QStandardItemModel::setRowCount(int rows)
 
2192
{
 
2193
    Q_D(QStandardItemModel);
 
2194
    d->root->setRowCount(rows);
 
2195
}
 
2196
 
 
2197
/*!
 
2198
    \since 4.2
 
2199
 
 
2200
    Sets the number of columns in this model to \a columns. If
 
2201
    this is less than columnCount(), the data in the unwanted columns
 
2202
    is discarded.
 
2203
 
 
2204
    \sa setRowCount()
 
2205
*/
 
2206
void QStandardItemModel::setColumnCount(int columns)
 
2207
{
 
2208
    Q_D(QStandardItemModel);
 
2209
    d->root->setColumnCount(columns);
 
2210
}
 
2211
 
 
2212
/*!
 
2213
    \since 4.2
 
2214
 
 
2215
    Sets the item for the given \a row and \a column to \a item. The model
 
2216
    takes ownership of the item. If necessary, the row count and column count
 
2217
    are increased to fit the item. The previous item at the given location (if
 
2218
    there was one) is deleted.
 
2219
 
 
2220
    \sa item()
 
2221
*/
 
2222
void QStandardItemModel::setItem(int row, int column, QStandardItem *item)
 
2223
{
 
2224
    Q_D(QStandardItemModel);
 
2225
    d->root->d_func()->setChild(row, column, item, true);
 
2226
}
 
2227
 
 
2228
/*!
 
2229
  \fn QStandardItemModel::setItem(int row, QStandardItem *item)
 
2230
  \overload
 
2231
*/
 
2232
 
 
2233
/*!
 
2234
    \since 4.2
 
2235
 
 
2236
    Returns the item for the given \a row and \a column if one has been set;
 
2237
    otherwise returns 0.
 
2238
 
 
2239
    \sa setItem(), takeItem(), itemFromIndex()
 
2240
*/
 
2241
QStandardItem *QStandardItemModel::item(int row, int column) const
 
2242
{
 
2243
    Q_D(const QStandardItemModel);
 
2244
    return d->root->child(row, column);
 
2245
}
 
2246
 
 
2247
/*!
 
2248
    \since 4.2
 
2249
 
 
2250
    Returns the model's invisible root item.
 
2251
 
 
2252
    The invisible root item provides access to the model's top-level items
 
2253
    through the QStandardItem API, making it possible to write functions that
 
2254
    can treat top-level items and their children in a uniform way; for
 
2255
    example, recursive functions involving a tree model.
 
2256
 
 
2257
    \note Calling \l{QAbstractItemModel::index()}{index()} on the QStandardItem object
 
2258
    retrieved from this function is not valid.
 
2259
*/
 
2260
QStandardItem *QStandardItemModel::invisibleRootItem() const
 
2261
{
 
2262
    Q_D(const QStandardItemModel);
 
2263
    return d->root.data();
 
2264
}
 
2265
 
 
2266
/*!
 
2267
    \since 4.2
 
2268
 
 
2269
    Sets the horizontal header item for \a column to \a item.  The model takes
 
2270
    ownership of the item. If necessary, the column count is increased to fit
 
2271
    the item. The previous header item (if there was one) is deleted.
 
2272
 
 
2273
    \sa horizontalHeaderItem(), setHorizontalHeaderLabels(),
 
2274
    setVerticalHeaderItem()
 
2275
*/
 
2276
void QStandardItemModel::setHorizontalHeaderItem(int column, QStandardItem *item)
 
2277
{
 
2278
    Q_D(QStandardItemModel);
 
2279
    if (column < 0)
 
2280
        return;
 
2281
    if (columnCount() <= column)
 
2282
        setColumnCount(column + 1);
 
2283
 
 
2284
    QStandardItem *oldItem = d->columnHeaderItems.at(column);
 
2285
    if (item == oldItem)
 
2286
        return;
 
2287
 
 
2288
    if (item) {
 
2289
        if (item->model() == 0) {
 
2290
            item->d_func()->setModel(this);
 
2291
        } else {
 
2292
            qWarning("QStandardItem::setHorizontalHeaderItem: Ignoring duplicate insertion of item %p",
 
2293
                     item);
 
2294
            return;
 
2295
        }
 
2296
    }
 
2297
 
 
2298
    if (oldItem)
 
2299
        oldItem->d_func()->setModel(0);
 
2300
    delete oldItem;
 
2301
 
 
2302
    d->columnHeaderItems.replace(column, item);
 
2303
    emit headerDataChanged(Qt::Horizontal, column, column);
 
2304
}
 
2305
 
 
2306
/*!
 
2307
    \since 4.2
 
2308
 
 
2309
    Returns the horizontal header item for \a column if one has been set;
 
2310
    otherwise returns 0.
 
2311
 
 
2312
    \sa setHorizontalHeaderItem(), verticalHeaderItem()
 
2313
*/
 
2314
QStandardItem *QStandardItemModel::horizontalHeaderItem(int column) const
 
2315
{
 
2316
    Q_D(const QStandardItemModel);
 
2317
    if ((column < 0) || (column >= columnCount()))
 
2318
        return 0;
 
2319
    return d->columnHeaderItems.at(column);
 
2320
}
 
2321
 
 
2322
/*!
 
2323
    \since 4.2
 
2324
 
 
2325
    Sets the vertical header item for \a row to \a item.  The model takes
 
2326
    ownership of the item. If necessary, the row count is increased to fit the
 
2327
    item. The previous header item (if there was one) is deleted.
 
2328
 
 
2329
    \sa verticalHeaderItem(), setVerticalHeaderLabels(),
 
2330
    setHorizontalHeaderItem()
 
2331
*/
 
2332
void QStandardItemModel::setVerticalHeaderItem(int row, QStandardItem *item)
 
2333
{
 
2334
    Q_D(QStandardItemModel);
 
2335
    if (row < 0)
 
2336
        return;
 
2337
    if (rowCount() <= row)
 
2338
        setRowCount(row + 1);
 
2339
 
 
2340
    QStandardItem *oldItem = d->rowHeaderItems.at(row);
 
2341
    if (item == oldItem)
 
2342
        return;
 
2343
 
 
2344
    if (item) {
 
2345
        if (item->model() == 0) {
 
2346
            item->d_func()->setModel(this);
 
2347
        } else {
 
2348
            qWarning("QStandardItem::setVerticalHeaderItem: Ignoring duplicate insertion of item %p",
 
2349
                     item);
 
2350
            return;
 
2351
        }
 
2352
    }
 
2353
 
 
2354
    if (oldItem)
 
2355
        oldItem->d_func()->setModel(0);
 
2356
    delete oldItem;
 
2357
 
 
2358
    d->rowHeaderItems.replace(row, item);
 
2359
    emit headerDataChanged(Qt::Vertical, row, row);
 
2360
}
 
2361
 
 
2362
/*!
 
2363
    \since 4.2
 
2364
 
 
2365
    Returns the vertical header item for row \a row if one has been set;
 
2366
    otherwise returns 0.
 
2367
 
 
2368
    \sa setVerticalHeaderItem(), horizontalHeaderItem()
 
2369
*/
 
2370
QStandardItem *QStandardItemModel::verticalHeaderItem(int row) const
 
2371
{
 
2372
    Q_D(const QStandardItemModel);
 
2373
    if ((row < 0) || (row >= rowCount()))
 
2374
        return 0;
 
2375
    return d->rowHeaderItems.at(row);
 
2376
}
 
2377
 
 
2378
/*!
 
2379
    \since 4.2
 
2380
 
 
2381
    Sets the horizontal header labels using \a labels. If necessary, the
 
2382
    column count is increased to the size of \a labels.
 
2383
 
 
2384
    \sa setHorizontalHeaderItem()
 
2385
*/
 
2386
void QStandardItemModel::setHorizontalHeaderLabels(const QStringList &labels)
 
2387
{
 
2388
    Q_D(QStandardItemModel);
 
2389
    if (columnCount() < labels.count())
 
2390
        setColumnCount(labels.count());
 
2391
    for (int i = 0; i < labels.count(); ++i) {
 
2392
        QStandardItem *item = horizontalHeaderItem(i);
 
2393
        if (!item) {
 
2394
            item = d->createItem();
 
2395
            setHorizontalHeaderItem(i, item);
 
2396
        }
 
2397
        item->setText(labels.at(i));
 
2398
    }
 
2399
}
 
2400
 
 
2401
/*!
 
2402
    \since 4.2
 
2403
 
 
2404
    Sets the vertical header labels using \a labels. If necessary, the row
 
2405
    count is increased to the size of \a labels.
 
2406
 
 
2407
    \sa setVerticalHeaderItem()
 
2408
*/
 
2409
void QStandardItemModel::setVerticalHeaderLabels(const QStringList &labels)
 
2410
{
 
2411
    Q_D(QStandardItemModel);
 
2412
    if (rowCount() < labels.count())
 
2413
        setRowCount(labels.count());
 
2414
    for (int i = 0; i < labels.count(); ++i) {
 
2415
        QStandardItem *item = verticalHeaderItem(i);
 
2416
        if (!item) {
 
2417
            item = d->createItem();
 
2418
            setVerticalHeaderItem(i, item);
 
2419
        }
 
2420
        item->setText(labels.at(i));
 
2421
    }
 
2422
}
 
2423
 
 
2424
/*!
 
2425
    \since 4.2
 
2426
 
 
2427
    Sets the item prototype for the model to the specified \a item. The model
 
2428
    takes ownership of the prototype.
 
2429
 
 
2430
    The item prototype acts as a QStandardItem factory, by relying on the
 
2431
    QStandardItem::clone() function.  To provide your own prototype, subclass
 
2432
    QStandardItem, reimplement QStandardItem::clone() and set the prototype to
 
2433
    be an instance of your custom class. Whenever QStandardItemModel needs to
 
2434
    create an item on demand (for instance, when a view or item delegate calls
 
2435
    setData())), the new items will be instances of your custom class.
 
2436
 
 
2437
    \sa itemPrototype(), QStandardItem::clone()
 
2438
*/
 
2439
void QStandardItemModel::setItemPrototype(const QStandardItem *item)
 
2440
{
 
2441
    Q_D(QStandardItemModel);
 
2442
    if (d->itemPrototype != item) {
 
2443
        delete d->itemPrototype;
 
2444
        d->itemPrototype = item;
 
2445
    }
 
2446
}
 
2447
 
 
2448
/*!
 
2449
    \since 4.2
 
2450
 
 
2451
    Returns the item prototype used by the model. The model uses the item
 
2452
    prototype as an item factory when it needs to construct new items on
 
2453
    demand (for instance, when a view or item delegate calls setData()).
 
2454
 
 
2455
    \sa setItemPrototype()
 
2456
*/
 
2457
const QStandardItem *QStandardItemModel::itemPrototype() const
 
2458
{
 
2459
    Q_D(const QStandardItemModel);
 
2460
    return d->itemPrototype;
 
2461
}
 
2462
 
 
2463
/*!
 
2464
    \since 4.2
 
2465
 
 
2466
    Returns a list of items that match the given \a text, using the given \a
 
2467
    flags, in the given \a column.
 
2468
*/
 
2469
QList<QStandardItem*> QStandardItemModel::findItems(const QString &text,
 
2470
                                                    Qt::MatchFlags flags, int column) const
 
2471
{
 
2472
    QModelIndexList indexes = match(index(0, column, QModelIndex()),
 
2473
                                    Qt::DisplayRole, text, -1, flags);
 
2474
    QList<QStandardItem*> items;
 
2475
    for (int i = 0; i < indexes.size(); ++i)
 
2476
        items.append(itemFromIndex(indexes.at(i)));
 
2477
    return items;
 
2478
}
 
2479
 
 
2480
/*!
 
2481
    \since 4.2
 
2482
 
 
2483
    Appends a row containing \a items. If necessary, the column count is
 
2484
    increased to the size of \a items.
 
2485
 
 
2486
    \sa insertRow(), appendColumn()
 
2487
*/
 
2488
void QStandardItemModel::appendRow(const QList<QStandardItem*> &items)
 
2489
{
 
2490
    invisibleRootItem()->appendRow(items);
 
2491
}
 
2492
 
 
2493
/*!
 
2494
    \since 4.2
 
2495
 
 
2496
    Appends a column containing \a items. If necessary, the row count is
 
2497
    increased to the size of \a items.
 
2498
 
 
2499
    \sa insertColumn(), appendRow()
 
2500
*/
 
2501
void QStandardItemModel::appendColumn(const QList<QStandardItem*> &items)
 
2502
{
 
2503
    invisibleRootItem()->appendColumn(items);
 
2504
}
 
2505
 
 
2506
/*!
 
2507
    \since 4.2
 
2508
    \fn QStandardItemModel::appendRow(QStandardItem *item)
 
2509
    \overload
 
2510
 
 
2511
    When building a list or a tree that has only one column, this function
 
2512
    provides a convenient way to append a single new \a item.
 
2513
*/
 
2514
 
 
2515
/*!
 
2516
    \since 4.2
 
2517
 
 
2518
    Inserts a row at \a row containing \a items. If necessary, the column
 
2519
    count is increased to the size of \a items.
 
2520
 
 
2521
    \sa takeRow(), appendRow(), insertColumn()
 
2522
*/
 
2523
void QStandardItemModel::insertRow(int row, const QList<QStandardItem*> &items)
 
2524
{
 
2525
    invisibleRootItem()->insertRow(row, items);
 
2526
}
 
2527
 
 
2528
/*!
 
2529
    \since 4.2
 
2530
 
 
2531
    \fn void QStandardItemModel::insertRow(int row, QStandardItem *item)
 
2532
    \overload
 
2533
 
 
2534
    Inserts a row at \a row containing \a item.
 
2535
 
 
2536
    When building a list or a tree that has only one column, this function
 
2537
    provides a convenient way to append a single new item.
 
2538
*/
 
2539
 
 
2540
/*!
 
2541
    \since 4.2
 
2542
 
 
2543
    Inserts a column at \a column containing \a items. If necessary, the row
 
2544
    count is increased to the size of \a items.
 
2545
 
 
2546
    \sa takeColumn(), appendColumn(), insertRow()
 
2547
*/
 
2548
void QStandardItemModel::insertColumn(int column, const QList<QStandardItem*> &items)
 
2549
{
 
2550
    invisibleRootItem()->insertColumn(column, items);
 
2551
}
 
2552
 
 
2553
/*!
 
2554
    \since 4.2
 
2555
 
 
2556
    Removes the item at (\a row, \a column) without deleting it. The model
 
2557
    releases ownership of the item.
 
2558
 
 
2559
    \sa item(), takeRow(), takeColumn()
 
2560
*/
 
2561
QStandardItem *QStandardItemModel::takeItem(int row, int column)
 
2562
{
 
2563
    Q_D(QStandardItemModel);
 
2564
    return d->root->takeChild(row, column);
 
2565
}
 
2566
 
 
2567
/*!
 
2568
    \since 4.2
 
2569
 
 
2570
    Removes the given \a row without deleting the row items, and returns a
 
2571
    list of pointers to the removed items. The model releases ownership of the
 
2572
    items. For items in the row that have not been set, the corresponding
 
2573
    pointers in the list will be 0.
 
2574
 
 
2575
    \sa takeColumn()
 
2576
*/
 
2577
QList<QStandardItem*> QStandardItemModel::takeRow(int row)
 
2578
{
 
2579
    Q_D(QStandardItemModel);
 
2580
    return d->root->takeRow(row);
 
2581
}
 
2582
 
 
2583
/*!
 
2584
    \since 4.2
 
2585
 
 
2586
    Removes the given \a column without deleting the column items, and returns
 
2587
    a list of pointers to the removed items. The model releases ownership of
 
2588
    the items. For items in the column that have not been set, the
 
2589
    corresponding pointers in the list will be 0.
 
2590
 
 
2591
    \sa takeRow()
 
2592
*/
 
2593
QList<QStandardItem*> QStandardItemModel::takeColumn(int column)
 
2594
{
 
2595
    Q_D(QStandardItemModel);
 
2596
    return d->root->takeColumn(column);
 
2597
}
 
2598
 
 
2599
/*!
 
2600
    \since 4.2
 
2601
 
 
2602
    Removes the horizontal header item at \a column from the header without
 
2603
    deleting it, and returns a pointer to the item. The model releases
 
2604
    ownership of the item.
 
2605
 
 
2606
    \sa horizontalHeaderItem(), takeVerticalHeaderItem()
 
2607
*/
 
2608
QStandardItem *QStandardItemModel::takeHorizontalHeaderItem(int column)
 
2609
{
 
2610
    Q_D(QStandardItemModel);
 
2611
    if ((column < 0) || (column >= columnCount()))
 
2612
        return 0;
 
2613
    QStandardItem *headerItem = d->columnHeaderItems.at(column);
 
2614
    if (headerItem) {
 
2615
        headerItem->d_func()->setParentAndModel(0, 0);
 
2616
        d->columnHeaderItems.replace(column, 0);
 
2617
    }
 
2618
    return headerItem;
 
2619
}
 
2620
 
 
2621
/*!
 
2622
    \since 4.2
 
2623
 
 
2624
    Removes the vertical header item at \a row from the header without
 
2625
    deleting it, and returns a pointer to the item. The model releases
 
2626
    ownership of the item.
 
2627
 
 
2628
    \sa verticalHeaderItem(), takeHorizontalHeaderItem()
 
2629
*/
 
2630
QStandardItem *QStandardItemModel::takeVerticalHeaderItem(int row)
 
2631
{
 
2632
    Q_D(QStandardItemModel);
 
2633
    if ((row < 0) || (row >= rowCount()))
 
2634
        return 0;
 
2635
    QStandardItem *headerItem = d->rowHeaderItems.at(row);
 
2636
    if (headerItem) {
 
2637
        headerItem->d_func()->setParentAndModel(0, 0);
 
2638
        d->rowHeaderItems.replace(row, 0);
 
2639
    }
 
2640
    return headerItem;
 
2641
}
 
2642
 
 
2643
/*!
 
2644
    \since 4.2
 
2645
    \property QStandardItemModel::sortRole
 
2646
    \brief the item role that is used to query the model's data when sorting items
 
2647
 
 
2648
    The default value is Qt::DisplayRole.
 
2649
 
 
2650
    \sa sort(), QStandardItem::sortChildren()
 
2651
*/
 
2652
int QStandardItemModel::sortRole() const
 
2653
{
 
2654
    Q_D(const QStandardItemModel);
 
2655
    return d->sortRole;
 
2656
}
 
2657
 
 
2658
void QStandardItemModel::setSortRole(int role)
 
2659
{
 
2660
    Q_D(QStandardItemModel);
 
2661
    d->sortRole = role;
 
2662
}
 
2663
 
 
2664
/*!
 
2665
  \reimp
 
2666
*/
 
2667
int QStandardItemModel::columnCount(const QModelIndex &parent) const
 
2668
{
 
2669
    Q_D(const QStandardItemModel);
 
2670
    QStandardItem *item = d->itemFromIndex(parent);
 
2671
    return item ? item->columnCount() : 0;
 
2672
}
 
2673
 
 
2674
/*!
 
2675
  \reimp
 
2676
*/
 
2677
QVariant QStandardItemModel::data(const QModelIndex &index, int role) const
 
2678
{
 
2679
    Q_D(const QStandardItemModel);
 
2680
    QStandardItem *item = d->itemFromIndex(index);
 
2681
    return item ? item->data(role) : QVariant();
 
2682
}
 
2683
 
 
2684
/*!
 
2685
  \reimp
 
2686
*/
 
2687
Qt::ItemFlags QStandardItemModel::flags(const QModelIndex &index) const
 
2688
{
 
2689
    Q_D(const QStandardItemModel);
 
2690
    if (!d->indexValid(index))
 
2691
        return d->root->flags();
 
2692
    QStandardItem *item = d->itemFromIndex(index);
 
2693
    if (item)
 
2694
        return item->flags();
 
2695
    return Qt::ItemIsSelectable
 
2696
        |Qt::ItemIsEnabled
 
2697
        |Qt::ItemIsEditable
 
2698
        |Qt::ItemIsDragEnabled
 
2699
        |Qt::ItemIsDropEnabled;
 
2700
}
 
2701
 
 
2702
/*!
 
2703
  \reimp
 
2704
*/
 
2705
bool QStandardItemModel::hasChildren(const QModelIndex &parent) const
 
2706
{
 
2707
    Q_D(const QStandardItemModel);
 
2708
    QStandardItem *item = d->itemFromIndex(parent);
 
2709
    return item ? item->hasChildren() : false;
 
2710
}
 
2711
 
 
2712
/*!
 
2713
  \reimp
 
2714
*/
 
2715
QModelIndex QStandardItemModel::sibling(int row, int column, const QModelIndex &idx) const
 
2716
{
 
2717
    return createIndex(row, column, idx.internalPointer());
 
2718
}
 
2719
 
 
2720
/*!
 
2721
  \reimp
 
2722
*/
 
2723
QVariant QStandardItemModel::headerData(int section, Qt::Orientation orientation, int role) const
 
2724
{
 
2725
    Q_D(const QStandardItemModel);
 
2726
    if ((section < 0)
 
2727
        || ((orientation == Qt::Horizontal) && (section >= columnCount()))
 
2728
        || ((orientation == Qt::Vertical) && (section >= rowCount()))) {
 
2729
        return QVariant();
 
2730
    }
 
2731
    QStandardItem *headerItem = 0;
 
2732
    if (orientation == Qt::Horizontal)
 
2733
        headerItem = d->columnHeaderItems.at(section);
 
2734
    else if (orientation == Qt::Vertical)
 
2735
        headerItem = d->rowHeaderItems.at(section);
 
2736
    return headerItem ? headerItem->data(role)
 
2737
        : QAbstractItemModel::headerData(section, orientation, role);
 
2738
}
 
2739
 
 
2740
/*!
 
2741
    \reimp
 
2742
 
 
2743
    QStandardItemModel supports both copy and move.
 
2744
*/
 
2745
Qt::DropActions QStandardItemModel::supportedDropActions () const
 
2746
{
 
2747
    return Qt::CopyAction | Qt::MoveAction;
 
2748
}
 
2749
 
 
2750
/*!
 
2751
  \reimp
 
2752
*/
 
2753
QModelIndex QStandardItemModel::index(int row, int column, const QModelIndex &parent) const
 
2754
{
 
2755
    Q_D(const QStandardItemModel);
 
2756
    QStandardItem *parentItem = d->itemFromIndex(parent);
 
2757
    if ((parentItem == 0)
 
2758
        || (row < 0)
 
2759
        || (column < 0)
 
2760
        || (row >= parentItem->rowCount())
 
2761
        || (column >= parentItem->columnCount())) {
 
2762
        return QModelIndex();
 
2763
    }
 
2764
    return createIndex(row, column, parentItem);
 
2765
}
 
2766
 
 
2767
/*!
 
2768
  \reimp
 
2769
*/
 
2770
bool QStandardItemModel::insertColumns(int column, int count, const QModelIndex &parent)
 
2771
{
 
2772
    Q_D(QStandardItemModel);
 
2773
    QStandardItem *item = parent.isValid() ? itemFromIndex(parent) : d->root.data();
 
2774
    if (item == 0)
 
2775
        return false;
 
2776
    return item->d_func()->insertColumns(column, count, QList<QStandardItem*>());
 
2777
}
 
2778
 
 
2779
/*!
 
2780
  \reimp
 
2781
*/
 
2782
bool QStandardItemModel::insertRows(int row, int count, const QModelIndex &parent)
 
2783
{
 
2784
    Q_D(QStandardItemModel);
 
2785
    QStandardItem *item = parent.isValid() ? itemFromIndex(parent) : d->root.data();
 
2786
    if (item == 0)
 
2787
        return false;
 
2788
    return item->d_func()->insertRows(row, count, QList<QStandardItem*>());
 
2789
}
 
2790
 
 
2791
/*!
 
2792
  \reimp
 
2793
*/
 
2794
QMap<int, QVariant> QStandardItemModel::itemData(const QModelIndex &index) const
 
2795
{
 
2796
    Q_D(const QStandardItemModel);
 
2797
    QStandardItem *item = d->itemFromIndex(index);
 
2798
    return item ? item->d_func()->itemData() : QMap<int, QVariant>();
 
2799
}
 
2800
 
 
2801
/*!
 
2802
  \reimp
 
2803
*/
 
2804
QModelIndex QStandardItemModel::parent(const QModelIndex &child) const
 
2805
{
 
2806
    Q_D(const QStandardItemModel);
 
2807
    if (!d->indexValid(child))
 
2808
        return QModelIndex();
 
2809
    QStandardItem *parentItem = static_cast<QStandardItem*>(child.internalPointer());
 
2810
    return indexFromItem(parentItem);
 
2811
}
 
2812
 
 
2813
/*!
 
2814
  \reimp
 
2815
*/
 
2816
bool QStandardItemModel::removeColumns(int column, int count, const QModelIndex &parent)
 
2817
{
 
2818
    Q_D(QStandardItemModel);
 
2819
    QStandardItem *item = d->itemFromIndex(parent);
 
2820
    if ((item == 0) || (count < 1) || (column < 0) || ((column + count) > item->columnCount()))
 
2821
        return false;
 
2822
    item->removeColumns(column, count);
 
2823
    return true;
 
2824
}
 
2825
 
 
2826
/*!
 
2827
  \reimp
 
2828
*/
 
2829
bool QStandardItemModel::removeRows(int row, int count, const QModelIndex &parent)
 
2830
{
 
2831
    Q_D(QStandardItemModel);
 
2832
    QStandardItem *item = d->itemFromIndex(parent);
 
2833
    if ((item == 0) || (count < 1) || (row < 0) || ((row + count) > item->rowCount()))
 
2834
        return false;
 
2835
    item->removeRows(row, count);
 
2836
    return true;
 
2837
}
 
2838
 
 
2839
/*!
 
2840
  \reimp
 
2841
*/
 
2842
int QStandardItemModel::rowCount(const QModelIndex &parent) const
 
2843
{
 
2844
    Q_D(const QStandardItemModel);
 
2845
    QStandardItem *item = d->itemFromIndex(parent);
 
2846
    return item ? item->rowCount() : 0;
 
2847
}
 
2848
 
 
2849
/*!
 
2850
  \reimp
 
2851
*/
 
2852
bool QStandardItemModel::setData(const QModelIndex &index, const QVariant &value, int role)
 
2853
{
 
2854
    if (!index.isValid())
 
2855
        return false;
 
2856
    QStandardItem *item = itemFromIndex(index);
 
2857
    if (item == 0)
 
2858
        return false;
 
2859
    item->setData(value, role);
 
2860
    return true;
 
2861
}
 
2862
 
 
2863
/*!
 
2864
  \reimp
 
2865
*/
 
2866
bool QStandardItemModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role)
 
2867
{
 
2868
    Q_D(QStandardItemModel);
 
2869
    if ((section < 0)
 
2870
        || ((orientation == Qt::Horizontal) && (section >= columnCount()))
 
2871
        || ((orientation == Qt::Vertical) && (section >= rowCount()))) {
 
2872
        return false;
 
2873
    }
 
2874
    QStandardItem *headerItem = 0;
 
2875
    if (orientation == Qt::Horizontal) {
 
2876
        headerItem = d->columnHeaderItems.at(section);
 
2877
        if (headerItem == 0) {
 
2878
            headerItem = d->createItem();
 
2879
            headerItem->d_func()->setModel(this);
 
2880
            d->columnHeaderItems.replace(section, headerItem);
 
2881
        }
 
2882
    } else if (orientation == Qt::Vertical) {
 
2883
        headerItem = d->rowHeaderItems.at(section);
 
2884
        if (headerItem == 0) {
 
2885
            headerItem = d->createItem();
 
2886
            headerItem->d_func()->setModel(this);
 
2887
            d->rowHeaderItems.replace(section, headerItem);
 
2888
        }
 
2889
    }
 
2890
    if (headerItem) {
 
2891
        headerItem->setData(value, role);
 
2892
        return true;
 
2893
    }
 
2894
    return false;
 
2895
}
 
2896
 
 
2897
/*!
 
2898
  \reimp
 
2899
*/
 
2900
bool QStandardItemModel::setItemData(const QModelIndex &index, const QMap<int, QVariant> &roles)
 
2901
{
 
2902
    QStandardItem *item = itemFromIndex(index);
 
2903
    if (item == 0)
 
2904
        return false;
 
2905
    item->d_func()->setItemData(roles);
 
2906
    return true;
 
2907
}
 
2908
 
 
2909
/*!
 
2910
  \reimp
 
2911
*/
 
2912
void QStandardItemModel::sort(int column, Qt::SortOrder order)
 
2913
{
 
2914
    Q_D(QStandardItemModel);
 
2915
    d->root->sortChildren(column, order);
 
2916
}
 
2917
 
 
2918
/*!
 
2919
  \fn QObject *QStandardItemModel::parent() const
 
2920
  \internal
 
2921
*/
 
2922
 
 
2923
 
 
2924
/*!
 
2925
  \reimp
 
2926
*/
 
2927
QStringList QStandardItemModel::mimeTypes() const
 
2928
{
 
2929
    return QAbstractItemModel::mimeTypes() <<  QLatin1String("application/x-qstandarditemmodeldatalist");
 
2930
}
 
2931
 
 
2932
/*!
 
2933
  \reimp
 
2934
*/
 
2935
QMimeData *QStandardItemModel::mimeData(const QModelIndexList &indexes) const
 
2936
{
 
2937
    QMimeData *data = QAbstractItemModel::mimeData(indexes);
 
2938
    if(!data)
 
2939
        return 0;
 
2940
 
 
2941
    QString format = QLatin1String("application/x-qstandarditemmodeldatalist");
 
2942
    if (!mimeTypes().contains(format))
 
2943
        return data;
 
2944
    QByteArray encoded;
 
2945
    QDataStream stream(&encoded, QIODevice::WriteOnly);
 
2946
 
 
2947
    QSet<QStandardItem*> itemsSet;
 
2948
    QStack<QStandardItem*> stack;
 
2949
    itemsSet.reserve(indexes.count());
 
2950
    stack.reserve(indexes.count());
 
2951
    for (int i = 0; i < indexes.count(); ++i) {
 
2952
        QStandardItem *item = itemFromIndex(indexes.at(i));
 
2953
        itemsSet << item;
 
2954
        stack.push(item);
 
2955
    }
 
2956
 
 
2957
    //remove duplicates childrens
 
2958
    {
 
2959
        QSet<QStandardItem *> seen;
 
2960
        while (!stack.isEmpty()) {
 
2961
            QStandardItem *itm = stack.pop();
 
2962
            if (seen.contains(itm))
 
2963
                continue;
 
2964
            seen.insert(itm);
 
2965
 
 
2966
            const QVector<QStandardItem*> &childList = itm->d_func()->children;
 
2967
            for (int i = 0; i < childList.count(); ++i) {
 
2968
                QStandardItem *chi = childList.at(i);
 
2969
                if (chi) {
 
2970
                    QSet<QStandardItem *>::iterator it = itemsSet.find(chi);
 
2971
                    if (it != itemsSet.end()) {
 
2972
                        itemsSet.erase(it);
 
2973
                    }
 
2974
                    stack.push(chi);
 
2975
                }
 
2976
            }
 
2977
        }
 
2978
    }
 
2979
 
 
2980
    stack.reserve(itemsSet.count());
 
2981
    foreach (QStandardItem *item, itemsSet) {
 
2982
        stack.push(item);
 
2983
    }
 
2984
 
 
2985
    //stream everything recursively
 
2986
    while (!stack.isEmpty()) {
 
2987
        QStandardItem *item = stack.pop();
 
2988
        if(itemsSet.contains(item)) { //if the item is selection 'top-level', strem its position
 
2989
            stream << item->row() << item->column();
 
2990
        }
 
2991
        if(item) {
 
2992
            stream << *item << item->columnCount() << item->d_ptr->children.count();
 
2993
            stack += item->d_ptr->children;
 
2994
        } else {
 
2995
            QStandardItem dummy;
 
2996
            stream << dummy << 0 << 0;
 
2997
        }
 
2998
    }
 
2999
 
 
3000
    data->setData(format, encoded);
 
3001
    return data;
 
3002
}
 
3003
 
 
3004
 
 
3005
/* \internal
 
3006
    Used by QStandardItemModel::dropMimeData
 
3007
    stream out an item and his children
 
3008
 */
 
3009
void QStandardItemModelPrivate::decodeDataRecursive(QDataStream &stream, QStandardItem *item)
 
3010
{
 
3011
    int colCount, childCount;
 
3012
    stream >> *item;
 
3013
    stream >> colCount >> childCount;
 
3014
    item->setColumnCount(colCount);
 
3015
 
 
3016
    int childPos = childCount;
 
3017
 
 
3018
    while(childPos > 0) {
 
3019
        childPos--;
 
3020
        QStandardItem *child = createItem();
 
3021
        decodeDataRecursive(stream, child);
 
3022
        item->setChild( childPos / colCount, childPos % colCount, child);
 
3023
    }
 
3024
}
 
3025
 
 
3026
 
 
3027
/*!
 
3028
  \reimp
 
3029
*/
 
3030
bool QStandardItemModel::dropMimeData(const QMimeData *data, Qt::DropAction action,
 
3031
                                      int row, int column, const QModelIndex &parent)
 
3032
{
 
3033
    Q_D(QStandardItemModel);
 
3034
    // check if the action is supported
 
3035
    if (!data || !(action == Qt::CopyAction || action == Qt::MoveAction))
 
3036
        return false;
 
3037
    // check if the format is supported
 
3038
    QString format = QLatin1String("application/x-qstandarditemmodeldatalist");
 
3039
    if (!data->hasFormat(format))
 
3040
        return QAbstractItemModel::dropMimeData(data, action, row, column, parent);
 
3041
 
 
3042
    if (row > rowCount(parent))
 
3043
        row = rowCount(parent);
 
3044
    if (row == -1)
 
3045
        row = rowCount(parent);
 
3046
    if (column == -1)
 
3047
        column = 0;
 
3048
 
 
3049
    // decode and insert
 
3050
    QByteArray encoded = data->data(format);
 
3051
    QDataStream stream(&encoded, QIODevice::ReadOnly);
 
3052
 
 
3053
 
 
3054
    //code based on QAbstractItemModel::decodeData
 
3055
    // adapted to work with QStandardItem
 
3056
    int top = INT_MAX;
 
3057
    int left = INT_MAX;
 
3058
    int bottom = 0;
 
3059
    int right = 0;
 
3060
    QVector<int> rows, columns;
 
3061
    QVector<QStandardItem *> items;
 
3062
 
 
3063
    while (!stream.atEnd()) {
 
3064
        int r, c;
 
3065
        QStandardItem *item = d->createItem();
 
3066
        stream >> r >> c;
 
3067
        d->decodeDataRecursive(stream, item);
 
3068
 
 
3069
        rows.append(r);
 
3070
        columns.append(c);
 
3071
        items.append(item);
 
3072
        top = qMin(r, top);
 
3073
        left = qMin(c, left);
 
3074
        bottom = qMax(r, bottom);
 
3075
        right = qMax(c, right);
 
3076
    }
 
3077
 
 
3078
    // insert the dragged items into the table, use a bit array to avoid overwriting items,
 
3079
    // since items from different tables can have the same row and column
 
3080
    int dragRowCount = 0;
 
3081
    int dragColumnCount = right - left + 1;
 
3082
 
 
3083
    // Compute the number of continuous rows upon insertion and modify the rows to match
 
3084
    QVector<int> rowsToInsert(bottom + 1);
 
3085
    for (int i = 0; i < rows.count(); ++i)
 
3086
        rowsToInsert[rows.at(i)] = 1;
 
3087
    for (int i = 0; i < rowsToInsert.count(); ++i) {
 
3088
        if (rowsToInsert[i] == 1){
 
3089
            rowsToInsert[i] = dragRowCount;
 
3090
            ++dragRowCount;
 
3091
        }
 
3092
    }
 
3093
    for (int i = 0; i < rows.count(); ++i)
 
3094
        rows[i] = top + rowsToInsert[rows[i]];
 
3095
 
 
3096
    QBitArray isWrittenTo(dragRowCount * dragColumnCount);
 
3097
 
 
3098
    // make space in the table for the dropped data
 
3099
    int colCount = columnCount(parent);
 
3100
    if (colCount < dragColumnCount + column) {
 
3101
        insertColumns(colCount, dragColumnCount + column - colCount, parent);
 
3102
        colCount = columnCount(parent);
 
3103
    }
 
3104
    insertRows(row, dragRowCount, parent);
 
3105
 
 
3106
    row = qMax(0, row);
 
3107
    column = qMax(0, column);
 
3108
 
 
3109
    QStandardItem *parentItem = itemFromIndex (parent);
 
3110
    if (!parentItem)
 
3111
        parentItem = invisibleRootItem();
 
3112
 
 
3113
    QVector<QPersistentModelIndex> newIndexes(items.size());
 
3114
    // set the data in the table
 
3115
    for (int j = 0; j < items.size(); ++j) {
 
3116
        int relativeRow = rows.at(j) - top;
 
3117
        int relativeColumn = columns.at(j) - left;
 
3118
        int destinationRow = relativeRow + row;
 
3119
        int destinationColumn = relativeColumn + column;
 
3120
        int flat = (relativeRow * dragColumnCount) + relativeColumn;
 
3121
        // if the item was already written to, or we just can't fit it in the table, create a new row
 
3122
        if (destinationColumn >= colCount || isWrittenTo.testBit(flat)) {
 
3123
            destinationColumn = qBound(column, destinationColumn, colCount - 1);
 
3124
            destinationRow = row + dragRowCount;
 
3125
            insertRows(row + dragRowCount, 1, parent);
 
3126
            flat = (dragRowCount * dragColumnCount) + relativeColumn;
 
3127
            isWrittenTo.resize(++dragRowCount * dragColumnCount);
 
3128
        }
 
3129
        if (!isWrittenTo.testBit(flat)) {
 
3130
            newIndexes[j] = index(destinationRow, destinationColumn, parentItem->index());
 
3131
            isWrittenTo.setBit(flat);
 
3132
        }
 
3133
    }
 
3134
 
 
3135
    for(int k = 0; k < newIndexes.size(); k++) {
 
3136
        if (newIndexes.at(k).isValid()) {
 
3137
            parentItem->setChild(newIndexes.at(k).row(), newIndexes.at(k).column(), items.at(k));
 
3138
        } else {
 
3139
            delete items.at(k);
 
3140
        }
 
3141
    }
 
3142
 
 
3143
    return true;
 
3144
}
 
3145
 
 
3146
QT_END_NAMESPACE
 
3147
 
 
3148
#include "moc_qstandarditemmodel.cpp"
 
3149
 
 
3150
#endif // QT_NO_STANDARDITEMMODEL