summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/gui/graphicsview/qgraphicslayout.cpp4
-rw-r--r--src/gui/graphicsview/qgraphicslinearlayout.cpp3
-rw-r--r--src/gui/graphicsview/qgridlayoutengine.cpp22
-rw-r--r--src/gui/graphicsview/qgridlayoutengine_p.h4
-rw-r--r--tests/auto/qgraphicslinearlayout/tst_qgraphicslinearlayout.cpp25
5 files changed, 50 insertions, 8 deletions
diff --git a/src/gui/graphicsview/qgraphicslayout.cpp b/src/gui/graphicsview/qgraphicslayout.cpp
index 58c174c..df6be90 100644
--- a/src/gui/graphicsview/qgraphicslayout.cpp
+++ b/src/gui/graphicsview/qgraphicslayout.cpp
@@ -390,8 +390,10 @@ void QGraphicsLayout::widgetEvent(QEvent *e)
QGraphicsLayout to return a pointer to the item at index \a i. The
reimplementation can assume that \a i is valid (i.e., it respects the
value of count()).
+ Together with count(), it is provided as a means of iterating over all items in a layout.
- The subclass is free to decide how to store the items.
+ The subclass is free to decide how to store the items, and the visual arrangement
+ does not have to be reflected through this function.
\sa count(), removeAt()
*/
diff --git a/src/gui/graphicsview/qgraphicslinearlayout.cpp b/src/gui/graphicsview/qgraphicslinearlayout.cpp
index 185780a..1b271e5 100644
--- a/src/gui/graphicsview/qgraphicslinearlayout.cpp
+++ b/src/gui/graphicsview/qgraphicslinearlayout.cpp
@@ -277,7 +277,7 @@ void QGraphicsLinearLayout::insertItem(int index, QGraphicsLayoutItem *item)
Q_ASSERT(item);
d->fixIndex(&index);
d->engine.insertRow(index, d->orientation);
- new QGridLayoutItem(&d->engine, item, d->gridRow(index), d->gridColumn(index));
+ new QGridLayoutItem(&d->engine, item, d->gridRow(index), d->gridColumn(index), 1, 1, 0, index);
invalidate();
}
@@ -471,6 +471,7 @@ int QGraphicsLinearLayout::count() const
/*!
\reimp
+ When iterating from 0 and up, it will return the items in the visual arranged order.
*/
QGraphicsLayoutItem *QGraphicsLinearLayout::itemAt(int index) const
{
diff --git a/src/gui/graphicsview/qgridlayoutengine.cpp b/src/gui/graphicsview/qgridlayoutengine.cpp
index 5ad6ac9..beb9cb0 100644
--- a/src/gui/graphicsview/qgridlayoutengine.cpp
+++ b/src/gui/graphicsview/qgridlayoutengine.cpp
@@ -461,7 +461,7 @@ void QGridLayoutRowData::dump(int indent) const
QGridLayoutItem::QGridLayoutItem(QGridLayoutEngine *engine, QGraphicsLayoutItem *layoutItem,
int row, int column, int rowSpan, int columnSpan,
- Qt::Alignment alignment)
+ Qt::Alignment alignment, int itemAtIndex)
: q_engine(engine), q_layoutItem(layoutItem), q_alignment(alignment)
{
q_firstRows[Hor] = column;
@@ -471,7 +471,7 @@ QGridLayoutItem::QGridLayoutItem(QGridLayoutEngine *engine, QGraphicsLayoutItem
q_stretches[Hor] = -1;
q_stretches[Ver] = -1;
- q_engine->addItem(this);
+ q_engine->insertItem(this, itemAtIndex);
}
int QGridLayoutItem::firstRow(Qt::Orientation orientation) const
@@ -937,11 +937,20 @@ Qt::Alignment QGridLayoutEngine::effectiveAlignment(const QGridLayoutItem *layou
return align;
}
-void QGridLayoutEngine::addItem(QGridLayoutItem *item)
+/*!
+ \internal
+ The \a index is only used by QGraphicsLinearLayout to ensure that itemAt() reflects the order
+ of visual arrangement. Strictly speaking it does not have to, but most people expect it to.
+ (And if it didn't we would have to add itemArrangedAt(int index) or something..)
+ */
+void QGridLayoutEngine::insertItem(QGridLayoutItem *item, int index)
{
maybeExpandGrid(item->lastRow(), item->lastColumn());
- q_items.append(item);
+ if (index == -1)
+ q_items.append(item);
+ else
+ q_items.insert(index, item);
for (int i = item->firstRow(); i <= item->lastRow(); ++i) {
for (int j = item->firstColumn(); j <= item->lastColumn(); ++j) {
@@ -952,6 +961,11 @@ void QGridLayoutEngine::addItem(QGridLayoutItem *item)
}
}
+void QGridLayoutEngine::addItem(QGridLayoutItem *item)
+{
+ insertItem(item, -1);
+}
+
void QGridLayoutEngine::removeItem(QGridLayoutItem *item)
{
Q_ASSERT(q_items.contains(item));
diff --git a/src/gui/graphicsview/qgridlayoutengine_p.h b/src/gui/graphicsview/qgridlayoutengine_p.h
index 8f3eb13..aab695a 100644
--- a/src/gui/graphicsview/qgridlayoutengine_p.h
+++ b/src/gui/graphicsview/qgridlayoutengine_p.h
@@ -267,7 +267,8 @@ class QGridLayoutItem
{
public:
QGridLayoutItem(QGridLayoutEngine *engine, QGraphicsLayoutItem *layoutItem, int row, int column,
- int rowSpan = 1, int columnSpan = 1, Qt::Alignment alignment = 0);
+ int rowSpan = 1, int columnSpan = 1, Qt::Alignment alignment = 0,
+ int itemAtIndex = -1);
inline int firstRow() const { return q_firstRows[Ver]; }
inline int firstColumn() const { return q_firstRows[Hor]; }
@@ -378,6 +379,7 @@ public:
Qt::Alignment effectiveAlignment(const QGridLayoutItem *layoutItem) const;
+ void insertItem(QGridLayoutItem *item, int index);
void addItem(QGridLayoutItem *item);
void removeItem(QGridLayoutItem *item);
QGridLayoutItem *findLayoutItem(QGraphicsLayoutItem *layoutItem) const;
diff --git a/tests/auto/qgraphicslinearlayout/tst_qgraphicslinearlayout.cpp b/tests/auto/qgraphicslinearlayout/tst_qgraphicslinearlayout.cpp
index 1d81ac8..51c96b1 100644
--- a/tests/auto/qgraphicslinearlayout/tst_qgraphicslinearlayout.cpp
+++ b/tests/auto/qgraphicslinearlayout/tst_qgraphicslinearlayout.cpp
@@ -76,6 +76,7 @@ private slots:
void invalidate();
void itemAt_data();
void itemAt();
+ void itemAt_visualOrder();
void orientation_data();
void orientation();
void removeAt_data();
@@ -537,7 +538,7 @@ void tst_QGraphicsLinearLayout::insertItem()
QCOMPARE(layout.count(), itemCount + layoutCount + 1);
if (insertItemAt >= 0 && (itemCount + layoutCount >= 0)) {
- QCOMPARE(layout.itemAt(itemCount + layoutCount), item);
+ QCOMPARE(layout.itemAt(insertItemAt), item);
}
layout.activate();
@@ -686,6 +687,28 @@ void tst_QGraphicsLinearLayout::itemAt()
QVERIFY(layout.itemAt(index) != 0);
}
+void tst_QGraphicsLinearLayout::itemAt_visualOrder()
+{
+ QGraphicsLinearLayout *l = new QGraphicsLinearLayout;
+
+ QGraphicsWidget *w1 = new QGraphicsWidget;
+ l->addItem(w1);
+
+ QGraphicsWidget *w3 = new QGraphicsWidget;
+ l->addItem(w3);
+
+ QGraphicsWidget *w0 = new QGraphicsWidget;
+ l->insertItem(0, w0);
+
+ QGraphicsWidget *w2 = new QGraphicsWidget;
+ l->insertItem(2, w2);
+
+ QCOMPARE(l->itemAt(0), w0);
+ QCOMPARE(l->itemAt(1), w1);
+ QCOMPARE(l->itemAt(2), w2);
+ QCOMPARE(l->itemAt(3), w3);
+}
+
void tst_QGraphicsLinearLayout::orientation_data()
{
QTest::addColumn<Qt::Orientation>("orientation");