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

« back to all changes in this revision

Viewing changes to src/gui/itemviews/qheaderview.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
****************************************************************************/
62
62
 
63
63
#ifndef QT_NO_DATASTREAM
64
64
#include <qdatastream.h>
 
65
#endif
65
66
 
66
67
QT_BEGIN_NAMESPACE
67
68
 
 
69
#ifndef QT_NO_DATASTREAM
68
70
QDataStream &operator<<(QDataStream &out, const QHeaderViewPrivate::SectionSpan &span)
69
71
{
70
72
    span.write(out);
76
78
    span.read(in);
77
79
    return in;
78
80
}
79
 
#endif
 
81
#endif // QT_NO_DATASTREAM
80
82
 
81
83
 
82
84
/*!
86
88
    item views.
87
89
 
88
90
    \ingroup model-view
89
 
    \mainclass
 
91
 
90
92
 
91
93
    A QHeaderView displays the headers used in item views such as the
92
94
    QTableView and QTreeView classes. It takes the place of Qt3's \c QHeader
520
522
QSize QHeaderView::sizeHint() const
521
523
{
522
524
    Q_D(const QHeaderView);
523
 
    if (count() < 1)
524
 
        return QSize(0, 0);
525
525
    if (d->cachedSizeHint.isValid())
526
526
        return d->cachedSizeHint;
527
 
    int width = 0;
528
 
    int height = 0;
 
527
    d->cachedSizeHint = QSize(0, 0); //reinitialize the cached size hint
 
528
    const int sectionCount = count();
 
529
 
529
530
    // get size hint for the first n sections
530
 
    int c = qMin(count(), 100);
531
 
    for (int i = 0; i < c; ++i) {
 
531
    int i = 0;
 
532
    for (int checked = 0; checked < 100 && i < sectionCount; ++i) {
532
533
        if (isSectionHidden(i))
533
534
            continue;
 
535
        checked++;
534
536
        QSize hint = sectionSizeFromContents(i);
535
 
        width = qMax(hint.width(), width);
536
 
        height = qMax(hint.height(), height);
 
537
        d->cachedSizeHint = d->cachedSizeHint.expandedTo(hint);
537
538
    }
538
539
    // get size hint for the last n sections
539
 
    c = qMax(count() - 100, c);
540
 
    for (int j = count() - 1; j >= c; --j) {
 
540
    i = qMax(i, sectionCount - 100 );
 
541
    for (int j = sectionCount - 1, checked = 0; j >= i && checked < 100; --j) {
541
542
        if (isSectionHidden(j))
542
543
            continue;
 
544
        checked++;
543
545
        QSize hint = sectionSizeFromContents(j);
544
 
        width = qMax(hint.width(), width);
545
 
        height = qMax(hint.height(), height);
 
546
        d->cachedSizeHint = d->cachedSizeHint.expandedTo(hint);
546
547
    }
547
 
    d->cachedSizeHint = QSize(width, height);
548
548
    return d->cachedSizeHint;
549
549
}
550
550
 
1047
1047
 
1048
1048
/*!
1049
1049
    Returns the logicalIndex for the section at the given \a visualIndex
1050
 
    position, or -1 otherwise.
 
1050
    position, or -1 if visualIndex < 0 or visualIndex >= QHeaderView::count().
 
1051
 
 
1052
    Note that the visualIndex is not affected by hidden sections.
1051
1053
 
1052
1054
    \sa visualIndex(), sectionPosition()
1053
1055
*/
1151
1153
    \overload
1152
1154
 
1153
1155
    Sets the constraints on how the section specified by \a logicalIndex in
1154
 
    the header can be resized to those described by the given \a mode.
 
1156
    the header can be resized to those described by the given \a mode. The logical
 
1157
    index should exist at the time this function is called.
1155
1158
 
1156
1159
    \note This setting will be ignored for the last section if the stretchLastSection
1157
1160
    property is set to true. This is the default for the horizontal headers provided
1194
1197
{
1195
1198
    Q_D(const QHeaderView);
1196
1199
    int visual = visualIndex(logicalIndex);
1197
 
    Q_ASSERT(visual != -1);
1198
 
    return d->visualIndexResizeMode(visual);
 
1200
    if (visual == -1)
 
1201
        return Fixed; //the default value
 
1202
    return d->headerSectionResizeMode(visual);
1199
1203
}
1200
1204
 
1201
1205
/*!
1234
1238
    if (sortIndicatorSection() < 0 || sortIndicatorSection() > count())
1235
1239
        return;
1236
1240
 
1237
 
    if (d->visualIndexResizeMode(sortIndicatorSection()) == ResizeToContents)
 
1241
    if (d->headerSectionResizeMode(sortIndicatorSection()) == ResizeToContents)
1238
1242
        resizeSections();
1239
1243
 
1240
1244
    d->viewport->update();
1535
1539
    }
1536
1540
    return false;
1537
1541
}
1538
 
#endif
 
1542
#endif // QT_NO_DATASTREAM
1539
1543
 
1540
1544
/*!
1541
1545
  \reimp
1969
1973
 
1970
1974
    if (d->orientation == Qt::Horizontal && current.column() != old.column()) {
1971
1975
        if (old.isValid() && old.parent() == d->root)
1972
 
            d->setDirtyRegion(QRect(sectionViewportPosition(old.column()), 0,
 
1976
            d->viewport->update(QRect(sectionViewportPosition(old.column()), 0,
1973
1977
                                    sectionSize(old.column()), d->viewport->height()));
1974
1978
        if (current.isValid() && current.parent() == d->root)
1975
 
            d->setDirtyRegion(QRect(sectionViewportPosition(current.column()), 0,
 
1979
            d->viewport->update(QRect(sectionViewportPosition(current.column()), 0,
1976
1980
                                    sectionSize(current.column()), d->viewport->height()));
1977
1981
    } else if (d->orientation == Qt::Vertical && current.row() != old.row()) {
1978
1982
        if (old.isValid() && old.parent() == d->root)
1979
 
            d->setDirtyRegion(QRect(0, sectionViewportPosition(old.row()),
 
1983
            d->viewport->update(QRect(0, sectionViewportPosition(old.row()),
1980
1984
                                    d->viewport->width(), sectionSize(old.row())));
1981
1985
        if (current.isValid() && current.parent() == d->root)
1982
 
            d->setDirtyRegion(QRect(0, sectionViewportPosition(current.row()),
 
1986
            d->viewport->update(QRect(0, sectionViewportPosition(current.row()),
1983
1987
                                    d->viewport->width(), sectionSize(current.row())));
1984
1988
    }
1985
 
    d->updateDirtyRegion();
1986
1989
}
1987
1990
 
1988
1991
 
2538
2541
    if (opt.icon.isNull())
2539
2542
        opt.icon = qvariant_cast<QPixmap>(variant);
2540
2543
    QSize size = style()->sizeFromContents(QStyle::CT_HeaderSection, &opt, QSize(), this);
2541
 
    if (isSortIndicatorShown() && sortIndicatorSection() == logicalIndex) {
 
2544
    if (isSortIndicatorShown()) {
2542
2545
        int margin = style()->pixelMetric(QStyle::PM_HeaderMargin, &opt, this);
2543
2546
        if (d->orientation == Qt::Horizontal)
2544
2547
            size.rwidth() += size.height() + margin;
2615
2618
        int first = orientation() == Qt::Horizontal ? topLeft.column() : topLeft.row();
2616
2619
        int last = orientation() == Qt::Horizontal ? bottomRight.column() : bottomRight.row();
2617
2620
        for (int i = first; i <= last && !resizeRequired; ++i)
2618
 
            resizeRequired = (resizeRequired && resizeMode(i));
 
2621
            resizeRequired = (resizeMode(i) == ResizeToContents);
2619
2622
        if (resizeRequired)
2620
2623
            d->doDelayedResizeSections();
2621
2624
    }
2821
2824
        sectionIndicator = new QLabel(viewport);
2822
2825
    }
2823
2826
 
2824
 
    int x, y, w, h;
 
2827
    int w, h;
2825
2828
    int p = q->sectionViewportPosition(section);
2826
2829
    if (orientation == Qt::Horizontal) {
2827
 
        x = p;
2828
 
        y = 0;
2829
2830
        w = q->sectionSize(section);
2830
2831
        h = viewport->height();
2831
2832
    } else {
2832
 
        x = 0;
2833
 
        y = p;
2834
2833
        w = viewport->width();
2835
2834
        h = q->sectionSize(section);
2836
2835
    }
2936
2935
void QHeaderViewPrivate::resizeSections(QHeaderView::ResizeMode globalMode, bool useGlobalMode)
2937
2936
{
2938
2937
    Q_Q(QHeaderView);
 
2938
    //stop the timer in case it is delayed
 
2939
    delayedResize.stop();
2939
2940
 
2940
2941
    executePostedLayout();
2941
2942
    if (sectionCount == 0)
2942
2943
        return;
 
2944
 
 
2945
    if (resizeRecursionBlock)
 
2946
        return;
 
2947
    resizeRecursionBlock = true;
 
2948
 
2943
2949
    invalidateCachedSizeHint();
2944
2950
 
 
2951
    const int lastVisibleSection = lastVisibleVisualIndex();
 
2952
 
2945
2953
    // find stretchLastSection if we have it
2946
2954
    int stretchSection = -1;
2947
 
    if (stretchLastSection && !useGlobalMode) {
2948
 
        for (int i = sectionCount - 1; i >= 0; --i) {
2949
 
            if (!isVisualIndexHidden(i)) {
2950
 
                stretchSection = i;
2951
 
                break;
2952
 
            }
2953
 
        }
2954
 
    }
 
2955
    if (stretchLastSection && !useGlobalMode)
 
2956
        stretchSection = lastVisibleVisualIndex();
2955
2957
 
2956
2958
    // count up the number of strected sections and how much space left for them
2957
2959
    int lengthToStrech = (orientation == Qt::Horizontal ? viewport->width() : viewport->height());
2965
2967
        if (useGlobalMode && (i != stretchSection))
2966
2968
            resizeMode = globalMode;
2967
2969
        else
2968
 
            resizeMode = (i == stretchSection ? QHeaderView::Stretch : visualIndexResizeMode(i));
 
2970
            resizeMode = (i == stretchSection ? QHeaderView::Stretch : headerSectionResizeMode(i));
2969
2971
 
2970
2972
        if (resizeMode == QHeaderView::Stretch) {
2971
2973
            ++numberOfStretchedSections;
2997
2999
 
2998
3000
    int spanStartSection = 0;
2999
3001
    int previousSectionLength = 0;
3000
 
    const int lastVisibleSection = lastVisibleVisualIndex();
3001
3002
 
3002
3003
    QHeaderView::ResizeMode previousSectionResizeMode = QHeaderView::Interactive;
3003
3004
 
3016
3017
            else
3017
3018
                resizeMode = (i == stretchSection
3018
3019
                              ? QHeaderView::Stretch
3019
 
                              : visualIndexResizeMode(i));
 
3020
                              : newSectionResizeMode);
3020
3021
            if (resizeMode == QHeaderView::Stretch && stretchSectionLength != -1) {
3021
3022
                if (i == lastVisibleSection)
3022
3023
                    newSectionLength = qMax(stretchSectionLength, lastSectionSize);
3053
3054
                      (sectionCount - spanStartSection) * previousSectionLength,
3054
3055
                      previousSectionResizeMode);
3055
3056
    //Q_ASSERT(headerLength() == length);
3056
 
 
 
3057
    resizeRecursionBlock = false;
3057
3058
    viewport->update();
3058
3059
}
3059
3060
 
3470
3471
 
3471
3472
int QHeaderViewPrivate::viewSectionSizeHint(int logical) const
3472
3473
{
3473
 
    Q_Q(const QHeaderView);
3474
 
    if (QAbstractItemView *parent = qobject_cast<QAbstractItemView*>(q->parent())) {
 
3474
    if (QAbstractItemView *view = qobject_cast<QAbstractItemView*>(parent)) {
3475
3475
        return (orientation == Qt::Horizontal
3476
 
                ? parent->sizeHintForColumn(logical)
3477
 
                : parent->sizeHintForRow(logical));
 
3476
                ? view->sizeHintForColumn(logical)
 
3477
                : view->sizeHintForRow(logical));
3478
3478
    }
3479
3479
    return 0;
3480
3480
}
3560
3560
    in >> minimumSectionSize;
3561
3561
 
3562
3562
    in >> align;
3563
 
    defaultAlignment = (Qt::Alignment)align;
 
3563
    defaultAlignment = Qt::Alignment(align);
3564
3564
 
3565
3565
    in >> global;
3566
3566
    globalResizeMode = (QHeaderView::ResizeMode)global;
3570
3570
    return true;
3571
3571
}
3572
3572
 
 
3573
#endif // QT_NO_DATASTREAM
 
3574
 
3573
3575
QT_END_NAMESPACE
3574
3576
 
3575
 
#endif // QT_NO_DATASTREAEM
3576
 
 
3577
3577
#endif // QT_NO_ITEMVIEWS
3578
3578
 
3579
3579
#include "moc_qheaderview.cpp"