tests/benchmarks/qgraphicsview/benchapps/scrolltest/main.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 #include <QtGui>
       
    42 #include "valgrind/callgrind.h"
       
    43 
       
    44 class ItemMover : public QObject
       
    45 {
       
    46     Q_OBJECT
       
    47 public:
       
    48     ItemMover(QGraphicsItem *item)
       
    49         : _item(item)
       
    50     {
       
    51         startTimer(0);
       
    52     }
       
    53 
       
    54 protected:
       
    55     void timerEvent(QTimerEvent *event)
       
    56     {
       
    57         _item->moveBy(-1, 0);
       
    58     }
       
    59 
       
    60 private:
       
    61     QGraphicsItem *_item;
       
    62 };
       
    63 
       
    64 class ClipItem : public QGraphicsRectItem
       
    65 {
       
    66 public:
       
    67     ClipItem(qreal x, qreal y, qreal w, qreal h, const QPen &pen, const QBrush &brush)
       
    68         : QGraphicsRectItem(x, y, w, h)
       
    69     {
       
    70         setPen(pen);
       
    71         setBrush(brush);
       
    72     }
       
    73 
       
    74     QPainterPath shape() const
       
    75     {
       
    76         QPainterPath path;
       
    77         path.addRect(rect());
       
    78         return path;
       
    79     }
       
    80 };
       
    81 
       
    82 class CountView : public QGraphicsView
       
    83 {
       
    84 protected:
       
    85     void paintEvent(QPaintEvent *event)
       
    86     {
       
    87         static int n = 0;
       
    88         if (n)
       
    89             CALLGRIND_START_INSTRUMENTATION
       
    90         QGraphicsView::paintEvent(event);
       
    91         if (n)
       
    92             CALLGRIND_STOP_INSTRUMENTATION
       
    93         if (++n == 500)
       
    94             qApp->quit();
       
    95     }
       
    96 };
       
    97 
       
    98 int main(int argc, char *argv[])
       
    99 {
       
   100     QApplication app(argc, argv);
       
   101 
       
   102     QGraphicsScene scene;
       
   103     scene.setItemIndexMethod(QGraphicsScene::NoIndex);
       
   104 
       
   105     ClipItem *clipItem = new ClipItem(0, 0, 100, 100, QPen(), QBrush(Qt::blue));
       
   106     clipItem->setFlag(QGraphicsItem::ItemClipsChildrenToShape);
       
   107     clipItem->setData(0, "clipItem");
       
   108     scene.addItem(clipItem);
       
   109 
       
   110     QGraphicsRectItem *scrollItem = scene.addRect(0, 0, 10, 10, QPen(Qt::NoPen), QBrush(Qt::NoBrush));
       
   111     scrollItem->setParentItem(clipItem);
       
   112     scrollItem->setFlag(QGraphicsItem::ItemIsMovable);
       
   113     scrollItem->setData(0, "scrollItem");
       
   114 
       
   115     for (int y = 0; y < 25; ++y) {
       
   116         for (int x = 0; x < 25; ++x) {
       
   117             ClipItem *rect = new ClipItem(0, 0, 90, 20, QPen(Qt::NoPen), QBrush(Qt::green));
       
   118             rect->setParentItem(scrollItem);
       
   119             rect->setPos(x * 95, y * 25);
       
   120             rect->setData(0, qPrintable(QString("rect %1 %2").arg(x).arg(y)));
       
   121             rect->setFlag(QGraphicsItem::ItemClipsChildrenToShape);
       
   122 
       
   123             QGraphicsEllipseItem *ellipse = new QGraphicsEllipseItem(-5, -5, 10, 10);
       
   124             ellipse->setPen(QPen(Qt::NoPen));
       
   125             ellipse->setBrush(QBrush(Qt::yellow));
       
   126             ellipse->setParentItem(rect);
       
   127             ellipse->setData(0, qPrintable(QString("ellipse %1 %2").arg(x).arg(y)));
       
   128         }
       
   129     }
       
   130 
       
   131     scrollItem->setRect(scrollItem->childrenBoundingRect());
       
   132 
       
   133 #if 0
       
   134     ItemMover mover(scrollItem);
       
   135 #endif
       
   136 
       
   137     CountView view;
       
   138     view.setScene(&scene);
       
   139     view.setSceneRect(-25, -25, 150, 150);
       
   140     view.resize(300, 300);
       
   141     view.show();
       
   142 
       
   143     return app.exec();
       
   144 }
       
   145 
       
   146 #include "main.moc"