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

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghersi
  • Date: 2009-11-02 18:30:08 UTC
  • mfrom: (1.2.2 upstream)
  • mto: (15.2.5 experimental)
  • mto: This revision was merged to the branch mainline in revision 88.
  • Revision ID: james.westby@ubuntu.com-20091102183008-b6a4gcs128mvfb3m
Tags: upstream-4.6.0~beta1
ImportĀ upstreamĀ versionĀ 4.6.0~beta1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/****************************************************************************
2
2
**
3
3
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
 
4
** All rights reserved.
4
5
** Contact: Nokia Corporation (qt-info@nokia.com)
5
6
**
6
7
** This file is part of the QtGui module of the Qt Toolkit.
7
8
**
8
9
** $QT_BEGIN_LICENSE:LGPL$
9
 
** Commercial Usage
10
 
** Licensees holding valid Qt Commercial licenses may use this file in
11
 
** accordance with the Qt Commercial License Agreement provided with the
12
 
** Software or, alternatively, in accordance with the terms contained in
13
 
** a written agreement between you and Nokia.
 
10
** No Commercial Usage
 
11
** This file contains pre-release code and may not be distributed.
 
12
** You may use this file in accordance with the terms and conditions
 
13
** contained in the Technology Preview License Agreement accompanying
 
14
** this package.
14
15
**
15
16
** GNU Lesser General Public License Usage
16
17
** Alternatively, this file may be used under the terms of the GNU Lesser
20
21
** ensure the GNU Lesser General Public License version 2.1 requirements
21
22
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22
23
**
23
 
** In addition, as a special exception, Nokia gives you certain
24
 
** additional rights. These rights are described in the Nokia Qt LGPL
25
 
** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26
 
** package.
27
 
**
28
 
** GNU General Public License Usage
29
 
** Alternatively, this file may be used under the terms of the GNU
30
 
** General Public License version 3.0 as published by the Free Software
31
 
** Foundation and appearing in the file LICENSE.GPL included in the
32
 
** packaging of this file.  Please review the following information to
33
 
** ensure the GNU General Public License version 3.0 requirements will be
34
 
** met: http://www.gnu.org/copyleft/gpl.html.
35
 
**
36
 
** If you are unsure which license is appropriate for your use, please
37
 
** contact the sales department at http://www.qtsoftware.com/contact.
 
24
** In addition, as a special exception, Nokia gives you certain additional
 
25
** rights.  These rights are described in the Nokia Qt LGPL Exception
 
26
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
 
27
**
 
28
** If you have questions regarding the use of this file, please contact
 
29
** Nokia at qt-info@nokia.com.
 
30
**
 
31
**
 
32
**
 
33
**
 
34
**
 
35
**
 
36
**
 
37
**
38
38
** $QT_END_LICENSE$
39
39
**
40
40
****************************************************************************/
329
329
*/
330
330
QStandardItemModelPrivate::~QStandardItemModelPrivate()
331
331
{
332
 
    delete root;
333
332
    delete itemPrototype;
334
333
    qDeleteAll(columnHeaderItems);
335
334
    qDeleteAll(rowHeaderItems);
554
553
                                             int row, int count)
555
554
{
556
555
    Q_Q(QStandardItemModel);
557
 
    if (parent == root)
 
556
    if (parent == root.data())
558
557
        rowHeaderItems.insert(row, count, 0);
559
558
    q->endInsertRows();
560
559
}
566
565
                                                int column, int count)
567
566
{
568
567
    Q_Q(QStandardItemModel);
569
 
    if (parent == root)
 
568
    if (parent == root.data())
570
569
        columnHeaderItems.insert(column, count, 0);
571
570
    q->endInsertColumns();
572
571
}
578
577
                                            int row, int count)
579
578
{
580
579
    Q_Q(QStandardItemModel);
581
 
    if (parent == root) {
 
580
    if (parent == root.data()) {
582
581
        for (int i = row; i < row + count; ++i) {
583
582
            QStandardItem *oldItem = rowHeaderItems.at(i);
584
583
            if (oldItem)
597
596
                                               int column, int count)
598
597
{
599
598
    Q_Q(QStandardItemModel);
600
 
    if (parent == root) {
 
599
    if (parent == root.data()) {
601
600
        for (int i = column; i < column + count; ++i) {
602
601
            QStandardItem *oldItem = columnHeaderItems.at(i);
603
602
            if (oldItem)
778
777
*/
779
778
QStandardItem::~QStandardItem()
780
779
{
781
 
    Q_D(QStandardItem);
782
 
    delete d;
783
780
}
784
781
 
785
782
/*!
790
787
QStandardItem *QStandardItem::parent() const
791
788
{
792
789
    Q_D(const QStandardItem);
793
 
    if (!d->model || (d->model->d_func()->root != d->parent))
 
790
    if (!d->model || (d->model->d_func()->root.data() != d->parent))
794
791
        return d->parent;
795
792
    return 0;
796
793
}
899
896
    if (!v.isValid())
900
897
        return (Qt::ItemIsSelectable|Qt::ItemIsEnabled|Qt::ItemIsEditable
901
898
                |Qt::ItemIsDragEnabled|Qt::ItemIsDropEnabled);
902
 
    return ((Qt::ItemFlags)(v.toInt()));
 
899
    return Qt::ItemFlags(v.toInt());
903
900
}
904
901
 
905
902
/*!
1742
1739
    Q_D(QStandardItem);
1743
1740
    if ((row < 0) || (row >= rowCount()))
1744
1741
        return QList<QStandardItem*>();
 
1742
    if (d->model)
 
1743
        d->model->d_func()->rowsAboutToBeRemoved(this, row, row);
1745
1744
    QList<QStandardItem*> items;
1746
 
    int index = d->childIndex(row, 0);
1747
 
    for (int column = 0; column < d->columnCount(); ++column) {
1748
 
        QStandardItem *ch = d->children.at(index);
1749
 
        if (ch) {
1750
 
            ch->d_func()->setParentAndModel(0, 0);
1751
 
            d->children.replace(index, 0);
 
1745
    int index = d->childIndex(row, 0);  // Will return -1 if there are no columns
 
1746
    if (index != -1) {
 
1747
        int col_count = d->columnCount();
 
1748
        for (int column = 0; column < col_count; ++column) {
 
1749
            QStandardItem *ch = d->children.at(index + column);
 
1750
            if (ch)
 
1751
                ch->d_func()->setParentAndModel(0, 0);
 
1752
            items.append(ch);
1752
1753
        }
1753
 
        items.append(ch);
1754
 
        ++index;
 
1754
        d->children.remove(index, col_count);
1755
1755
    }
1756
 
    removeRow(row);
 
1756
    d->rows--;
 
1757
    if (d->model)
 
1758
        d->model->d_func()->rowsRemoved(this, row, 1);
1757
1759
    return items;
1758
1760
}
1759
1761
 
1769
1771
    Q_D(QStandardItem);
1770
1772
    if ((column < 0) || (column >= columnCount()))
1771
1773
        return QList<QStandardItem*>();
 
1774
    if (d->model)
 
1775
        d->model->d_func()->columnsAboutToBeRemoved(this, column, column);
1772
1776
    QList<QStandardItem*> items;
1773
 
    int index = d->childIndex(0, column);
1774
 
    for (int row = 0; row < d->rowCount(); ++row) {
 
1777
 
 
1778
    for (int row = d->rowCount() - 1; row >= 0; --row) {
 
1779
        int index = d->childIndex(row, column);
1775
1780
        QStandardItem *ch = d->children.at(index);
1776
 
        if (ch) {
 
1781
        if (ch)
1777
1782
            ch->d_func()->setParentAndModel(0, 0);
1778
 
            d->children.replace(index, 0);
1779
 
        }
1780
 
        items.append(ch);
1781
 
        index += d->columnCount();
 
1783
        d->children.remove(index);
 
1784
        items.prepend(ch);
1782
1785
    }
1783
 
    removeColumn(column);
 
1786
    d->columns--;
 
1787
    if (d->model)
 
1788
        d->model->d_func()->columnsRemoved(this, column, 1);
1784
1789
    return items;
1785
1790
}
1786
1791
 
1801
1806
    const int role = model() ? model()->sortRole() : Qt::DisplayRole;
1802
1807
    const QVariant l = data(role), r = other.data(role);
1803
1808
    // this code is copied from QSortFilterProxyModel::lessThan()
1804
 
    switch (l.type()) {
 
1809
    switch (l.userType()) {
1805
1810
    case QVariant::Invalid:
1806
1811
        return (r.type() == QVariant::Invalid);
1807
1812
    case QVariant::Int:
1812
1817
        return l.toLongLong() < r.toLongLong();
1813
1818
    case QVariant::ULongLong:
1814
1819
        return l.toULongLong() < r.toULongLong();
 
1820
    case QMetaType::Float:
 
1821
        return l.toFloat() < r.toFloat();
1815
1822
    case QVariant::Double:
1816
1823
        return l.toDouble() < r.toDouble();
1817
1824
    case QVariant::Char:
1890
1897
    in >> d->values;
1891
1898
    qint32 flags;
1892
1899
    in >> flags;
1893
 
    setFlags((Qt::ItemFlags)flags);
 
1900
    setFlags(Qt::ItemFlags(flags));
1894
1901
}
1895
1902
 
1896
1903
/*!
1938
1945
    return out;
1939
1946
}
1940
1947
 
1941
 
#endif // !QT_NO_DATASTREAM
 
1948
#endif // QT_NO_DATASTREAM
1942
1949
 
1943
1950
/*!
1944
1951
    \class QStandardItemModel
2079
2086
void QStandardItemModel::clear()
2080
2087
{
2081
2088
    Q_D(QStandardItemModel);
2082
 
    delete d->root;
2083
 
    d->root = new QStandardItem;
 
2089
    d->root.reset(new QStandardItem);
2084
2090
    d->root->d_func()->setModel(this);
2085
2091
    qDeleteAll(d->columnHeaderItems);
2086
2092
    d->columnHeaderItems.clear();
2227
2233
QStandardItem *QStandardItemModel::invisibleRootItem() const
2228
2234
{
2229
2235
    Q_D(const QStandardItemModel);
2230
 
    return d->root;
 
2236
    return d->root.data();
2231
2237
}
2232
2238
 
2233
2239
/*!
2729
2735
bool QStandardItemModel::insertColumns(int column, int count, const QModelIndex &parent)
2730
2736
{
2731
2737
    Q_D(QStandardItemModel);
2732
 
    QStandardItem *item = parent.isValid() ? itemFromIndex(parent) : d->root;
 
2738
    QStandardItem *item = parent.isValid() ? itemFromIndex(parent) : d->root.data();
2733
2739
    if (item == 0)
2734
2740
        return false;
2735
2741
    return item->d_func()->insertColumns(column, count, QList<QStandardItem*>());
2741
2747
bool QStandardItemModel::insertRows(int row, int count, const QModelIndex &parent)
2742
2748
{
2743
2749
    Q_D(QStandardItemModel);
2744
 
    QStandardItem *item = parent.isValid() ? itemFromIndex(parent) : d->root;
 
2750
    QStandardItem *item = parent.isValid() ? itemFromIndex(parent) : d->root.data();
2745
2751
    if (item == 0)
2746
2752
        return false;
2747
2753
    return item->d_func()->insertRows(row, count, QList<QStandardItem*>());
2907
2913
    QStack<QStandardItem*> stack;
2908
2914
    itemsSet.reserve(indexes.count());
2909
2915
    stack.reserve(indexes.count());
2910
 
    foreach (const QModelIndex &index, indexes) {
2911
 
        QStandardItem *item = itemFromIndex(index);
 
2916
    for (int i = 0; i < indexes.count(); ++i) {
 
2917
        QStandardItem *item = itemFromIndex(indexes.at(i));
2912
2918
        itemsSet << item;
2913
2919
        stack.push(item);
2914
2920
    }
2965
2971
    Used by QStandardItemModel::dropMimeData
2966
2972
    stream out an item and his children 
2967
2973
 */
2968
 
static void decodeDataRecursive(QDataStream &stream, QStandardItem *item)
 
2974
void QStandardItemModelPrivate::decodeDataRecursive(QDataStream &stream, QStandardItem *item)
2969
2975
{
2970
2976
    int colCount, childCount;
2971
2977
    stream >> *item;
2976
2982
    
2977
2983
    while(childPos > 0) {
2978
2984
        childPos--;
2979
 
        QStandardItem *child = new QStandardItem;
 
2985
        QStandardItem *child = createItem();
2980
2986
        decodeDataRecursive(stream, child);
2981
2987
        item->setChild( childPos / colCount, childPos % colCount, child);
2982
2988
    }
2989
2995
bool QStandardItemModel::dropMimeData(const QMimeData *data, Qt::DropAction action,
2990
2996
                                      int row, int column, const QModelIndex &parent)
2991
2997
{
 
2998
    Q_D(QStandardItemModel);
2992
2999
    // check if the action is supported
2993
3000
    if (!data || !(action == Qt::CopyAction || action == Qt::MoveAction))
2994
3001
        return false;
3020
3027
 
3021
3028
    while (!stream.atEnd()) {
3022
3029
        int r, c;
3023
 
        QStandardItem *item = new QStandardItem;
 
3030
        QStandardItem *item = d->createItem();
3024
3031
        stream >> r >> c;
3025
 
        decodeDataRecursive(stream, item);
 
3032
        d->decodeDataRecursive(stream, item);
3026
3033
 
3027
3034
        rows.append(r);
3028
3035
        columns.append(c);