summaryrefslogtreecommitdiffstats
path: root/src/gui/graphicsview/qgraphicslinearlayout.cpp
diff options
context:
space:
mode:
authorJan-Arve Sæther <jan-arve.saether@nokia.com>2009-06-23 09:02:11 (GMT)
committerJan-Arve Sæther <jan-arve.saether@nokia.com>2009-06-24 07:30:25 (GMT)
commit2ec56d158dc140f68efb45e2e0613f0e4026ddf6 (patch)
treefaaf3707a0b85104090872ba6c327816047892c3 /src/gui/graphicsview/qgraphicslinearlayout.cpp
parent84b5bd06dcc7c51d5b13f340aafd9797e1722c2e (diff)
downloadQt-2ec56d158dc140f68efb45e2e0613f0e4026ddf6.zip
Qt-2ec56d158dc140f68efb45e2e0613f0e4026ddf6.tar.gz
Qt-2ec56d158dc140f68efb45e2e0613f0e4026ddf6.tar.bz2
Fixed count(), itemAt() and removeAt() in QGraphicsLinearLayout.
Due to this, the behaviour of count(), itemAt() and removeAt() was different between QGraphicsLinearLayout and QGraphicsGridLayout. QGraphicsGridLayout does it right. This adds some behaviour changes: 1. QGraphicsLinearLayout::count() is no longer affected by inserted stretches (through insertStretch) This means that code like this will break: QGraphicsLinearLayout *linearLayout = new QGraphicsLinearLayout; linearLayout->addItem(new QGraphicsWidget); linearLayout->addStretch(); int count = linearLayout->count(); linearLayout->removeAt(count - 1); // before this patch it would do nothing (and it wouldn't // actually remove the stretch), with the new patch it would // remove the QGraphicsWidget. 2. count(), itemAt() and removeAt() now prints a warning for an invalid index. The documentation actually says that "The reimplementation can assume that index is valid (i.e., it respects the value of count()", but I decided that its too risky to not assume that it is valid, since it would break the following common pattern: while(layout->itemAt(0)) { layout->removeAt(0); } Cleaned up autotests (and a small codeblock) that assumed itemAt/removeAt with an invalid index to work, since we should try to follow our own advice. :-) This change is also an alignment with what QGraphicsGridLayout does (it checks the index argument and prints a warning too.)
Diffstat (limited to 'src/gui/graphicsview/qgraphicslinearlayout.cpp')
-rw-r--r--src/gui/graphicsview/qgraphicslinearlayout.cpp14
1 files changed, 11 insertions, 3 deletions
diff --git a/src/gui/graphicsview/qgraphicslinearlayout.cpp b/src/gui/graphicsview/qgraphicslinearlayout.cpp
index 2e78fc0..3b037cf 100644
--- a/src/gui/graphicsview/qgraphicslinearlayout.cpp
+++ b/src/gui/graphicsview/qgraphicslinearlayout.cpp
@@ -323,7 +323,11 @@ void QGraphicsLinearLayout::removeItem(QGraphicsLayoutItem *item)
void QGraphicsLinearLayout::removeAt(int index)
{
Q_D(QGraphicsLinearLayout);
- if (QGridLayoutItem *gridItem = d->engine.itemAt(d->gridRow(index), d->gridColumn(index))) {
+ if (index < 0 || index >= d->engine.itemCount()) {
+ qWarning("QGraphicsLinearLayout::removeAt: invalid index %d", index);
+ return;
+ }
+ if (QGridLayoutItem *gridItem = d->engine.itemAt(index)) {
if (QGraphicsLayoutItem *layoutItem = gridItem->layoutItem())
layoutItem->setParentLayoutItem(0);
d->removeGridItem(gridItem);
@@ -463,7 +467,7 @@ QSizePolicy::ControlTypes QGraphicsLinearLayout::controlTypes(LayoutSide side) c
int QGraphicsLinearLayout::count() const
{
Q_D(const QGraphicsLinearLayout);
- return d->engine.rowCount(d->orientation);
+ return d->engine.itemCount();
}
/*!
@@ -472,8 +476,12 @@ int QGraphicsLinearLayout::count() const
QGraphicsLayoutItem *QGraphicsLinearLayout::itemAt(int index) const
{
Q_D(const QGraphicsLinearLayout);
+ if (index < 0 || index >= d->engine.itemCount()) {
+ qWarning("QGraphicsLinearLayout::itemAt: invalid index %d", index);
+ return 0;
+ }
QGraphicsLayoutItem *item = 0;
- if (QGridLayoutItem *gridItem = d->engine.itemAt(d->gridRow(index), d->gridColumn(index)))
+ if (QGridLayoutItem *gridItem = d->engine.itemAt(index))
item = gridItem->layoutItem();
return item;
}