tests/auto/qgraphicslayout/tst_qgraphicslayout.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the test suite of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    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.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    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 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 
       
    43 #include <QtTest/QtTest>
       
    44 #include <QtGui>
       
    45 #include <math.h>
       
    46 
       
    47 //TESTED_CLASS=QGraphicsLayout
       
    48 //TESTED_FILES=
       
    49 
       
    50 class tst_QGraphicsLayout : public QObject
       
    51 {
       
    52 Q_OBJECT
       
    53 
       
    54 public:
       
    55     tst_QGraphicsLayout();
       
    56     virtual ~tst_QGraphicsLayout();
       
    57 
       
    58 private slots:
       
    59     void sizeHints();
       
    60     void compressLayoutRequest();
       
    61     void automaticReparenting();
       
    62     void verifyActivate();
       
    63     void constructors();
       
    64     void alternativeLayoutItems();
       
    65     void ownership();
       
    66 };
       
    67 
       
    68 tst_QGraphicsLayout::tst_QGraphicsLayout()
       
    69 {
       
    70 }
       
    71 
       
    72 tst_QGraphicsLayout::~tst_QGraphicsLayout()
       
    73 {
       
    74 }
       
    75 
       
    76 void tst_QGraphicsLayout::sizeHints()
       
    77 {
       
    78 
       
    79     QGraphicsView view;
       
    80     QGraphicsScene scene;
       
    81     QGraphicsWidget *window = new QGraphicsWidget();
       
    82     scene.addItem(window);
       
    83     QGraphicsLinearLayout *lout = new QGraphicsLinearLayout(window);
       
    84     lout->setContentsMargins(0,0,0,0);
       
    85     QGraphicsWidget *gw = new QGraphicsWidget(window);
       
    86     gw->setMinimumSize(QSizeF(10,10));
       
    87     gw->setPreferredSize(QSizeF(100,100));
       
    88     gw->setMaximumSize(QSizeF(500,500));
       
    89     lout->addItem(gw);
       
    90     QCOMPARE(lout->effectiveSizeHint(Qt::MinimumSize), gw->effectiveSizeHint(Qt::MinimumSize));
       
    91     QCOMPARE(lout->effectiveSizeHint(Qt::PreferredSize), gw->effectiveSizeHint(Qt::PreferredSize));
       
    92     QCOMPARE(lout->effectiveSizeHint(Qt::MaximumSize), gw->effectiveSizeHint(Qt::MaximumSize));
       
    93 
       
    94 }
       
    95 
       
    96 class TestGraphicsWidget : public QGraphicsWidget {
       
    97 public:
       
    98     TestGraphicsWidget(QGraphicsWidget *parent = 0) : QGraphicsWidget(parent)
       
    99     { }
       
   100 
       
   101     bool event(QEvent *e) {
       
   102         ++(m_eventCount[int(e->type())]);
       
   103         return QGraphicsWidget::event(e);
       
   104     }
       
   105 
       
   106     int eventCount(QEvent::Type type) {
       
   107         return m_eventCount.value(int(type));
       
   108     }
       
   109     void clearEventCount() {
       
   110         m_eventCount.clear();
       
   111     }
       
   112 private:
       
   113     QMap<int, int> m_eventCount;
       
   114 };
       
   115 
       
   116 void tst_QGraphicsLayout::compressLayoutRequest()
       
   117 {
       
   118     QGraphicsView view;
       
   119     QGraphicsScene scene;
       
   120     TestGraphicsWidget *tw = new TestGraphicsWidget();
       
   121     scene.addItem(tw);
       
   122     view.show();
       
   123     QGraphicsLinearLayout *lout = new QGraphicsLinearLayout(tw);
       
   124     for (int i = 0; i < 4; ++i) {
       
   125         QGraphicsWidget *gw = new QGraphicsWidget(tw);
       
   126         gw->setPreferredSize(QSizeF(50, 50));
       
   127         lout->addItem(gw);
       
   128     }
       
   129     QApplication::processEvents();
       
   130     QCOMPARE(tw->eventCount(QEvent::LayoutRequest), 1);
       
   131 }
       
   132 
       
   133 void tst_QGraphicsLayout::automaticReparenting()
       
   134 {
       
   135     QGraphicsView view;
       
   136     QGraphicsScene scene;
       
   137     {
       
   138         QGraphicsWidget *w = new QGraphicsWidget();
       
   139         QGraphicsLinearLayout *l = new QGraphicsLinearLayout(w);
       
   140         QGraphicsWidget *w1 = new QGraphicsWidget;
       
   141         l->addItem(w1);
       
   142         scene.addItem(w);
       
   143         QCOMPARE(w1->parentWidget(), w);
       
   144         delete w;
       
   145     }
       
   146     {
       
   147         QGraphicsWidget *w = new QGraphicsWidget();
       
   148         QGraphicsLinearLayout *l = new QGraphicsLinearLayout(w);
       
   149         QGraphicsWidget *w1 = new QGraphicsWidget;
       
   150         l->addItem(w1);
       
   151         scene.addItem(w);
       
   152         QCOMPARE(w1->parentWidget(), w);
       
   153 
       
   154         QGraphicsWidget *ww = new QGraphicsWidget();
       
   155         QGraphicsLinearLayout *l1 = new QGraphicsLinearLayout(ww);
       
   156 #if !defined(Q_OS_MAC) && defined(QT_DEBUG)
       
   157         QTest::ignoreMessage(QtWarningMsg, "QGraphicsLayout::addChildLayoutItem: QGraphicsWidget \"\""
       
   158                              " in wrong parent; moved to correct parent");
       
   159 #endif
       
   160         l1->addItem(w1);
       
   161         QCOMPARE(w1->parentWidget(), ww);
       
   162         delete w;
       
   163     }
       
   164 
       
   165     QGraphicsWidget *window = new QGraphicsWidget();
       
   166     scene.addItem(window);
       
   167     view.show();
       
   168     QGraphicsLinearLayout *l1 = new QGraphicsLinearLayout();
       
   169     QGraphicsWidget *w1 = new QGraphicsWidget();
       
   170     l1->addItem(w1);
       
   171     QGraphicsWidget *w2 = new QGraphicsWidget();
       
   172     l1->addItem(w2);
       
   173     QCOMPARE(w1->parentItem(), static_cast<QGraphicsItem*>(0));
       
   174     QCOMPARE(w2->parentItem(), static_cast<QGraphicsItem*>(0));
       
   175     scene.addItem(w1);
       
   176     QCOMPARE(w1->parentItem(), static_cast<QGraphicsItem*>(0));
       
   177     window->setLayout(l1);
       
   178     QCOMPARE(w1->parentItem(), static_cast<QGraphicsItem*>(window));
       
   179     QCOMPARE(w2->parentItem(), static_cast<QGraphicsItem*>(window));
       
   180 
       
   181     // Sublayouts
       
   182     QGraphicsLinearLayout *l2 = new QGraphicsLinearLayout();
       
   183     QGraphicsWidget *w3 = new QGraphicsWidget();
       
   184     l2->addItem(w3);
       
   185     QGraphicsWidget *w4 = new QGraphicsWidget();
       
   186     l2->addItem(w4);
       
   187     QGraphicsLinearLayout *l3 = new QGraphicsLinearLayout();
       
   188     l2->addItem(l3);
       
   189     QGraphicsWidget *window2 = new QGraphicsWidget();
       
   190     scene.addItem(window2);
       
   191     window2->setLayout(l2);
       
   192 
       
   193     QCOMPARE(w3->parentItem(), static_cast<QGraphicsItem*>(window2));
       
   194     QCOMPARE(w4->parentItem(), static_cast<QGraphicsItem*>(window2));
       
   195 
       
   196     // graphics item with another parent
       
   197     QGraphicsLinearLayout *l5 = new QGraphicsLinearLayout();
       
   198     l5->addItem(w1);
       
   199     l5->addItem(w2);
       
   200     QCOMPARE(w1->parentItem(), static_cast<QGraphicsItem*>(window));
       
   201     QCOMPARE(w2->parentItem(), static_cast<QGraphicsItem*>(window));
       
   202     QGraphicsLinearLayout *l4 = new QGraphicsLinearLayout();
       
   203     l4->addItem(l5);
       
   204     QGraphicsWidget *window3 = new QGraphicsWidget();
       
   205     scene.addItem(window3);
       
   206     window3->setLayout(l4);
       
   207 
       
   208     QCOMPARE(w1->parentItem(), static_cast<QGraphicsItem*>(window3));
       
   209     QCOMPARE(w2->parentItem(), static_cast<QGraphicsItem*>(window3));
       
   210 }
       
   211 
       
   212 class TestLayout : public QGraphicsLinearLayout
       
   213 {
       
   214     public:
       
   215     TestLayout(QGraphicsLayoutItem *parent = 0)
       
   216         : QGraphicsLinearLayout(parent)
       
   217     {
       
   218         m_count = 0;
       
   219     }
       
   220 
       
   221     void setGeometry(const QRectF &rect) {
       
   222 
       
   223         ++m_count;
       
   224         QGraphicsLinearLayout::setGeometry(rect);
       
   225     }
       
   226 
       
   227 
       
   228     int m_count;
       
   229 };
       
   230 
       
   231 void tst_QGraphicsLayout::verifyActivate()
       
   232 {
       
   233     QGraphicsScene scene;
       
   234     QGraphicsView view(&scene);
       
   235 
       
   236     QGraphicsWidget *window = new QGraphicsWidget();
       
   237     scene.addItem(window);
       
   238     TestLayout *lout = new TestLayout(window);
       
   239     QGraphicsWidget *w = new QGraphicsWidget();
       
   240     lout->addItem(w);
       
   241     window->setLayout(lout);
       
   242 
       
   243     QCOMPARE(lout->m_count, 0);
       
   244     window->setVisible(false);
       
   245     QCOMPARE(lout->m_count, 0);
       
   246     window->setVisible(true);
       
   247     // on polish or the first time a widget is shown, the widget is resized.
       
   248     QCOMPARE(lout->m_count, 1);
       
   249 
       
   250 }
       
   251 
       
   252 class Layout : public QGraphicsLayout
       
   253 {
       
   254 public:
       
   255     Layout(QGraphicsLayoutItem *parentItem = 0) : QGraphicsLayout(parentItem) {}
       
   256 
       
   257     void setGeometry(const QRectF &rect)
       
   258     {
       
   259         QGraphicsLayout::setGeometry(rect);
       
   260     }
       
   261 
       
   262     int count() const {
       
   263         return 0;
       
   264     }
       
   265 
       
   266     QGraphicsLayoutItem *itemAt(int index) const {
       
   267         Q_UNUSED(index);
       
   268         return 0;
       
   269     }
       
   270 
       
   271     void removeAt(int index)
       
   272     {
       
   273         Q_UNUSED(index);
       
   274     }
       
   275 
       
   276 protected:
       
   277     QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const
       
   278     {
       
   279         Q_UNUSED(constraint);
       
   280         Q_UNUSED(which);
       
   281         return QSizeF(100,100);
       
   282     }
       
   283 
       
   284 };
       
   285 
       
   286 void tst_QGraphicsLayout::constructors()
       
   287 {
       
   288     // Strange test, but see the fix that was with this submit
       
   289     QVector<Layout*> layouts;
       
   290     for (int pass = 0; pass < 5; ++pass) {
       
   291         Layout *lay = new Layout();
       
   292         layouts << lay;
       
   293         qreal left, top, right, bottom;
       
   294         lay->getContentsMargins(&left, &top, &right, &bottom);
       
   295         // Test if the style defaults are sane (should always be ints)
       
   296         double intpart;
       
   297         QVERIFY(modf(left, &intpart) == 0.0);
       
   298         QVERIFY(modf(top, &intpart) == 0.0);
       
   299         QVERIFY(modf(right, &intpart) == 0.0);
       
   300         QVERIFY(modf(bottom, &intpart) == 0.0);
       
   301 
       
   302         lay->setContentsMargins(1, 2, 4, 8);
       
   303         lay->getContentsMargins(&left, &top, &right, &bottom);
       
   304 
       
   305         QCOMPARE(int(left), 1);
       
   306         QCOMPARE(int(top), 2);
       
   307         QCOMPARE(int(right), 4);
       
   308         QCOMPARE(int(bottom), 8);
       
   309     }
       
   310 
       
   311     qDeleteAll(layouts);
       
   312 }
       
   313 
       
   314 class AnimatedLayoutItem : public QGraphicsLayoutItem {
       
   315 public:
       
   316     AnimatedLayoutItem(QGraphicsRectItem *item)
       
   317     : QGraphicsLayoutItem()
       
   318     {
       
   319         setGraphicsItem(item);
       
   320     }
       
   321 
       
   322     void setGeometry(const QRectF &geom);
       
   323 
       
   324     QSizeF sizeHint(Qt::SizeHint which, const QSizeF & constraint = QSizeF() ) const;
       
   325 
       
   326     inline QGraphicsRectItem *rectItem() {
       
   327         return static_cast<QGraphicsRectItem *>(graphicsItem());
       
   328     }
       
   329 
       
   330     QRectF m_geom;
       
   331 private:
       
   332     AnimatedLayoutItem() {}
       
   333 };
       
   334 
       
   335 void AnimatedLayoutItem::setGeometry(const QRectF &geom)
       
   336 {
       
   337     QGraphicsLayoutItem::setGeometry(geom);
       
   338 }
       
   339 
       
   340 QSizeF AnimatedLayoutItem::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
       
   341 {
       
   342     switch (which) {
       
   343     case Qt::MinimumSize:
       
   344         return QSizeF(32,32);
       
   345     case Qt::PreferredSize:
       
   346         return QSizeF(160,90);
       
   347     case Qt::MaximumSize:
       
   348         return QSizeF(1000,1000);
       
   349     default:
       
   350         return QSizeF(300, 300);
       
   351     }
       
   352 }
       
   353 
       
   354 class AnimatedLayout : public QObject, public QGraphicsLinearLayout {
       
   355     Q_OBJECT
       
   356 public:
       
   357     AnimatedLayout(QGraphicsWidget *widget) : QGraphicsLinearLayout(widget), m_timeline(500, this)
       
   358     {
       
   359         connect(&m_timeline, SIGNAL(valueChanged(qreal)), this, SLOT(valueChanged(qreal)));
       
   360     }
       
   361 
       
   362     void setGeometry(const QRectF &geom) {
       
   363         fromGeoms.clear();
       
   364         toGeoms.clear();
       
   365         for (int i = 0; i < count(); ++i) {
       
   366             fromGeoms << itemAt(i)->geometry();
       
   367         }
       
   368 
       
   369         QGraphicsLinearLayout::setGeometry(geom);
       
   370 
       
   371         for (int i = 0; i < count(); ++i) {
       
   372             toGeoms << itemAt(i)->geometry();
       
   373         }
       
   374         m_timeline.start();
       
   375     }
       
   376 
       
   377 private slots:
       
   378     void valueChanged(qreal value) {
       
   379         for (int i = 0; i < fromGeoms.count(); ++i) {
       
   380             QGraphicsLayoutItem *li = itemAt(i);
       
   381             QRectF from = fromGeoms.at(i);
       
   382             QRectF to = toGeoms.at(i);
       
   383 
       
   384             QRectF geom(from.topLeft() + (to.topLeft() - from.topLeft()) * value,
       
   385                         from.size() + (to.size() - from.size()) * value);
       
   386             static_cast<QGraphicsRectItem*>(li->graphicsItem())->setRect(geom);
       
   387         }
       
   388     }
       
   389 private:
       
   390     QTimeLine m_timeline;
       
   391     QVector<QRectF> fromGeoms;
       
   392     QVector<QRectF> toGeoms;
       
   393 };
       
   394 
       
   395 
       
   396 void tst_QGraphicsLayout::alternativeLayoutItems()
       
   397 {
       
   398     QGraphicsScene scene;
       
   399     QGraphicsView view(&scene);
       
   400 
       
   401     QGraphicsWidget *window = new QGraphicsWidget;
       
   402     scene.addItem(window);
       
   403     AnimatedLayout *lout = new AnimatedLayout(window);
       
   404     lout->setContentsMargins(0, 0, 0, 0);
       
   405     lout->setSpacing(0);
       
   406 
       
   407     QGraphicsRectItem *item1 = new QGraphicsRectItem;
       
   408     AnimatedLayoutItem *li1 = new AnimatedLayoutItem(item1);
       
   409     lout->addItem(li1);
       
   410 
       
   411     QGraphicsRectItem *item2 = new QGraphicsRectItem;
       
   412     AnimatedLayoutItem *li2 = new AnimatedLayoutItem(item2);
       
   413     lout->addItem(li2);
       
   414 
       
   415     QGraphicsRectItem *item3 = new QGraphicsRectItem;
       
   416     AnimatedLayoutItem *li3 = new AnimatedLayoutItem(item3);
       
   417     lout->addItem(li3);
       
   418 
       
   419     window->setLayout(lout);
       
   420 
       
   421     window->setGeometry(0, 0, 99, 99);
       
   422     view.setSceneRect(QRectF(-10, -10, 110, 110));
       
   423     view.resize(150, 150);
       
   424     view.show();
       
   425 
       
   426     QApplication::processEvents();
       
   427     QTest::qWait(750);
       
   428     QApplication::processEvents();
       
   429 
       
   430     QCOMPARE(static_cast<QGraphicsRectItem*>(li1->graphicsItem())->rect(), QRectF( 0, 0, 33, 99));
       
   431     QCOMPARE(static_cast<QGraphicsRectItem*>(li2->graphicsItem())->rect(), QRectF(33, 0, 33, 99));
       
   432     QCOMPARE(static_cast<QGraphicsRectItem*>(li3->graphicsItem())->rect(), QRectF(66, 0, 33, 99));
       
   433 
       
   434     lout->setOrientation(Qt::Vertical);
       
   435 
       
   436     QApplication::processEvents();
       
   437     QTest::qWait(750);
       
   438     QApplication::processEvents();
       
   439     QCOMPARE(static_cast<QGraphicsRectItem*>(li1->graphicsItem())->rect(), QRectF(0, 0,  99, 33));
       
   440     QCOMPARE(static_cast<QGraphicsRectItem*>(li2->graphicsItem())->rect(), QRectF(0, 33, 99, 33));
       
   441     QCOMPARE(static_cast<QGraphicsRectItem*>(li3->graphicsItem())->rect(), QRectF(0, 66, 99, 33));
       
   442 
       
   443 }
       
   444 
       
   445 class CustomLayoutItem : public QGraphicsLayoutItem {
       
   446 public:
       
   447     CustomLayoutItem(QSet<QGraphicsLayoutItem*> *destructedSet)
       
   448     : QGraphicsLayoutItem()
       
   449     {
       
   450         m_destructedSet = destructedSet;
       
   451         setOwnedByLayout(true);
       
   452     }
       
   453 
       
   454     QSizeF sizeHint(Qt::SizeHint which, const QSizeF & constraint = QSizeF() ) const;
       
   455 
       
   456     ~CustomLayoutItem() {
       
   457         m_destructedSet->insert(this);
       
   458     }
       
   459 private:
       
   460     QSet<QGraphicsLayoutItem*> *m_destructedSet;
       
   461 };
       
   462 
       
   463 QSizeF CustomLayoutItem::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
       
   464 {
       
   465     switch (which) {
       
   466     case Qt::MinimumSize:
       
   467         return QSizeF(32,32);
       
   468     case Qt::PreferredSize:
       
   469         return QSizeF(160,90);
       
   470     case Qt::MaximumSize:
       
   471         return QSizeF(1000,1000);
       
   472     default:
       
   473         return QSizeF(300, 300);
       
   474     }
       
   475 }
       
   476 
       
   477 class CustomGraphicsWidget : public QGraphicsWidget {
       
   478 public:
       
   479     CustomGraphicsWidget(QSet<QGraphicsLayoutItem*> *destructedSet = 0)
       
   480     : QGraphicsWidget()
       
   481     {
       
   482         m_destructedSet = destructedSet;
       
   483     }
       
   484 
       
   485     QSizeF sizeHint(Qt::SizeHint which, const QSizeF & constraint = QSizeF() ) const;
       
   486 
       
   487     void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget * = 0)
       
   488     {
       
   489         const QRect r = option->rect.adjusted(0, 0, -1, -1);
       
   490         painter->drawLine(r.topLeft(), r.bottomRight());
       
   491         painter->drawLine(r.bottomLeft(), r.topRight());
       
   492         painter->drawRect(r);
       
   493     }
       
   494 
       
   495     ~CustomGraphicsWidget() {
       
   496         if (m_destructedSet)
       
   497             m_destructedSet->insert(this);
       
   498     }
       
   499 private:
       
   500     QSet<QGraphicsLayoutItem*> *m_destructedSet;
       
   501 };
       
   502 
       
   503 QSizeF CustomGraphicsWidget::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
       
   504 {
       
   505     switch (which) {
       
   506     case Qt::MinimumSize:
       
   507         return QSizeF(32,32);
       
   508     case Qt::PreferredSize:
       
   509         return QSizeF(160,90);
       
   510     case Qt::MaximumSize:
       
   511         return QSizeF(1000,1000);
       
   512     default:
       
   513         return QSizeF(300, 300);
       
   514     }
       
   515 }
       
   516 
       
   517 static bool compareSets(const QSet<QGraphicsLayoutItem*> &actual, const QSet<QGraphicsLayoutItem*> &expected)
       
   518 {
       
   519     if (actual != expected) {
       
   520         qDebug() << "actual:" << actual << "expected:" << expected;
       
   521         return false;
       
   522     }
       
   523     return true;
       
   524 }
       
   525 
       
   526 class CustomLayout : public QGraphicsLayout
       
   527 {
       
   528 public :
       
   529 CustomLayout(QGraphicsLayoutItem *parent)
       
   530     : QGraphicsLayout(parent)
       
   531 {
       
   532 }
       
   533 
       
   534 
       
   535 ~CustomLayout()
       
   536 {
       
   537 }
       
   538 
       
   539 int count() const
       
   540 {
       
   541     return items.count();
       
   542 }
       
   543 
       
   544 QGraphicsLayoutItem* itemAt(int index) const
       
   545 {
       
   546     return items.at(index);
       
   547 }
       
   548 
       
   549 
       
   550 void removeAt(int index)
       
   551 {
       
   552     items.removeAt(index);
       
   553 }
       
   554 
       
   555 void addItem(QGraphicsLayoutItem *item)
       
   556 {
       
   557     insertItem(items.count(), item);
       
   558 }
       
   559 
       
   560 void insertItem(int index, QGraphicsLayoutItem *item)
       
   561 {
       
   562     index = qBound(0, index, items.count());
       
   563 
       
   564     item->setParentLayoutItem(this);
       
   565 
       
   566     QGraphicsWidget *widget = static_cast<QGraphicsWidget *>(item);
       
   567     updateParentWidget(widget);
       
   568 
       
   569 
       
   570     if (index == items.count()) {
       
   571         items.append(item);
       
   572     } else {
       
   573         items.insert(index, item);
       
   574     }
       
   575 
       
   576     updateGeometry();
       
   577     activate();
       
   578 }
       
   579 
       
   580 void updateParentWidget(QGraphicsWidget *item)
       
   581 {
       
   582     QGraphicsLayoutItem *parentItem = parentLayoutItem();
       
   583     while (parentItem && parentItem->isLayout()) {
       
   584         parentItem = parentItem->parentLayoutItem();
       
   585     }
       
   586 
       
   587     if (parentItem) {
       
   588         item->setParentItem(static_cast<QGraphicsWidget*>(parentItem));
       
   589     }
       
   590 }
       
   591 
       
   592 QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
       
   593 {
       
   594     return QSizeF(50,50);
       
   595 }
       
   596 
       
   597 QList<QGraphicsLayoutItem*> items;
       
   598 
       
   599 };
       
   600 
       
   601 void tst_QGraphicsLayout::ownership()
       
   602 {
       
   603     QGraphicsScene scene;
       
   604     QGraphicsView view(&scene);
       
   605 
       
   606     {
       
   607         QGraphicsLinearLayout *lay = new QGraphicsLinearLayout;
       
   608         QSet<QGraphicsLayoutItem*> destructedSet;
       
   609         CustomLayoutItem *li1 = new CustomLayoutItem(&destructedSet);
       
   610         lay->addItem(li1);
       
   611         CustomLayoutItem *li2 = new CustomLayoutItem(&destructedSet);
       
   612         lay->addItem(li2);
       
   613         CustomLayoutItem *li3 = new CustomLayoutItem(&destructedSet);
       
   614         lay->addItem(li3);
       
   615         destructedSet.clear();
       
   616 
       
   617         delete lay;
       
   618         QSet<QGraphicsLayoutItem*> expected;
       
   619         expected << li1 << li2 << li3;
       
   620         QVERIFY(compareSets(destructedSet, expected));
       
   621     }
       
   622 
       
   623     {
       
   624         QGraphicsWidget *window = new QGraphicsWidget;
       
   625         QGraphicsLinearLayout *lay = new QGraphicsLinearLayout;
       
   626         QSet<QGraphicsLayoutItem*> destructedSet;
       
   627         CustomGraphicsWidget *li1 = new CustomGraphicsWidget(&destructedSet);
       
   628         lay->addItem(li1);
       
   629         CustomGraphicsWidget *li2 = new CustomGraphicsWidget(&destructedSet);
       
   630         lay->addItem(li2);
       
   631         CustomGraphicsWidget *li3 = new CustomGraphicsWidget(&destructedSet);
       
   632         lay->addItem(li3);
       
   633         window->setLayout(lay);
       
   634         scene.addItem(window);
       
   635 
       
   636         destructedSet.clear();
       
   637         window->setLayout(0);
       
   638         QVERIFY(destructedSet.count() == 0);
       
   639         delete window;
       
   640     }
       
   641 
       
   642     {
       
   643         QGraphicsWidget *window = new QGraphicsWidget(0, Qt::Window);
       
   644         QGraphicsLinearLayout *lay = new QGraphicsLinearLayout;
       
   645 
       
   646         CustomGraphicsWidget *li1 = new CustomGraphicsWidget;
       
   647         lay->addItem(li1);
       
   648 
       
   649         QGraphicsLinearLayout *li2 = new QGraphicsLinearLayout;
       
   650         CustomGraphicsWidget *li2_1 = new CustomGraphicsWidget;
       
   651         li2->addItem(li2_1);
       
   652         CustomGraphicsWidget *li2_2 = new CustomGraphicsWidget;
       
   653         li2->addItem(li2_2);
       
   654         CustomGraphicsWidget *li2_3 = new CustomGraphicsWidget;
       
   655         li2->addItem(li2_3);
       
   656         lay->addItem(li2);
       
   657 
       
   658         CustomGraphicsWidget *li3 = new CustomGraphicsWidget;
       
   659         lay->addItem(li3);
       
   660 
       
   661         window->setLayout(lay);
       
   662         scene.addItem(window);
       
   663         view.resize(500, 200);
       
   664         view.show();
       
   665 
       
   666         for (int i = li2->count(); i > 0; --i) {
       
   667             QCOMPARE(li2->count(), i);
       
   668             delete li2->itemAt(0);
       
   669         }
       
   670 
       
   671         for (int i = lay->count(); i > 0; --i) {
       
   672             QCOMPARE(lay->count(), i);
       
   673             delete lay->itemAt(0);
       
   674         }
       
   675 
       
   676         delete window;
       
   677     }
       
   678 
       
   679     {
       
   680         QGraphicsWidget *top = new QGraphicsWidget;
       
   681         QGraphicsWidget *w = new QGraphicsWidget;
       
   682         QGraphicsWidget *w2 = new QGraphicsWidget;
       
   683         CustomLayout *layout = new CustomLayout(top);
       
   684         layout->addItem(w);
       
   685         layout->addItem(w2);
       
   686         top->setLayout(layout);
       
   687         delete top;
       
   688         //don't crash after that.
       
   689     }
       
   690 }
       
   691 
       
   692 QTEST_MAIN(tst_QGraphicsLayout)
       
   693 #include "tst_qgraphicslayout.moc"