tests/benchmarks/qgraphicsview/benchapps/chipTest/view.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 #include "view.h"
       
    43 
       
    44 #include <QtGui>
       
    45 #include "valgrind/callgrind.h"
       
    46 #ifndef QT_NO_OPENGL
       
    47 #include <QtOpenGL>
       
    48 #endif
       
    49 
       
    50 #include <qmath.h>
       
    51 
       
    52 class CountView : public QGraphicsView
       
    53 {
       
    54 protected:
       
    55     void paintEvent(QPaintEvent *event)
       
    56     {
       
    57         static int n = 0;
       
    58         if (n)
       
    59             CALLGRIND_START_INSTRUMENTATION
       
    60         QGraphicsView::paintEvent(event);
       
    61         if (n)
       
    62             CALLGRIND_STOP_INSTRUMENTATION
       
    63         if (++n == 500)
       
    64             qApp->quit();
       
    65     }
       
    66 };
       
    67 
       
    68 View::View(const QString &name, QWidget *parent)
       
    69     : QFrame(parent)
       
    70 {
       
    71     setFrameStyle(Sunken | StyledPanel);
       
    72     graphicsView = new CountView;
       
    73     graphicsView->setRenderHint(QPainter::Antialiasing, false);
       
    74     graphicsView->setDragMode(QGraphicsView::RubberBandDrag);
       
    75     graphicsView->setViewportUpdateMode(QGraphicsView::SmartViewportUpdate);
       
    76 
       
    77     int size = style()->pixelMetric(QStyle::PM_ToolBarIconSize);
       
    78     QSize iconSize(size, size);
       
    79 
       
    80     QToolButton *zoomInIcon = new QToolButton;
       
    81     zoomInIcon->setAutoRepeat(true);
       
    82     zoomInIcon->setAutoRepeatInterval(33);
       
    83     zoomInIcon->setAutoRepeatDelay(0);
       
    84     zoomInIcon->setIcon(QPixmap(":/zoomin.png"));
       
    85     zoomInIcon->setIconSize(iconSize);
       
    86     QToolButton *zoomOutIcon = new QToolButton;
       
    87     zoomOutIcon->setAutoRepeat(true);
       
    88     zoomOutIcon->setAutoRepeatInterval(33);
       
    89     zoomOutIcon->setAutoRepeatDelay(0);
       
    90     zoomOutIcon->setIcon(QPixmap(":/zoomout.png"));
       
    91     zoomOutIcon->setIconSize(iconSize);
       
    92     zoomSlider = new QSlider;
       
    93     zoomSlider->setMinimum(0);
       
    94     zoomSlider->setMaximum(500);
       
    95     zoomSlider->setValue(250);
       
    96     zoomSlider->setTickPosition(QSlider::TicksRight);
       
    97 
       
    98     // Zoom slider layout
       
    99     QVBoxLayout *zoomSliderLayout = new QVBoxLayout;
       
   100     zoomSliderLayout->addWidget(zoomInIcon);
       
   101     zoomSliderLayout->addWidget(zoomSlider);
       
   102     zoomSliderLayout->addWidget(zoomOutIcon);
       
   103 
       
   104     QToolButton *rotateLeftIcon = new QToolButton;
       
   105     rotateLeftIcon->setIcon(QPixmap(":/rotateleft.png"));
       
   106     rotateLeftIcon->setIconSize(iconSize);
       
   107     QToolButton *rotateRightIcon = new QToolButton;
       
   108     rotateRightIcon->setIcon(QPixmap(":/rotateright.png"));
       
   109     rotateRightIcon->setIconSize(iconSize);
       
   110     rotateSlider = new QSlider;
       
   111     rotateSlider->setOrientation(Qt::Horizontal);
       
   112     rotateSlider->setMinimum(-360);
       
   113     rotateSlider->setMaximum(360);
       
   114     rotateSlider->setValue(0);
       
   115     rotateSlider->setTickPosition(QSlider::TicksBelow);
       
   116 
       
   117     // Rotate slider layout
       
   118     QHBoxLayout *rotateSliderLayout = new QHBoxLayout;
       
   119     rotateSliderLayout->addWidget(rotateLeftIcon);
       
   120     rotateSliderLayout->addWidget(rotateSlider);
       
   121     rotateSliderLayout->addWidget(rotateRightIcon);
       
   122 
       
   123     resetButton = new QToolButton;
       
   124     resetButton->setText(tr("0"));
       
   125     resetButton->setEnabled(false);
       
   126 
       
   127     // Label layout
       
   128     QHBoxLayout *labelLayout = new QHBoxLayout;
       
   129     label = new QLabel(name);
       
   130     antialiasButton = new QToolButton;
       
   131     antialiasButton->setText(tr("Antialiasing"));
       
   132     antialiasButton->setCheckable(true);
       
   133     antialiasButton->setChecked(false);
       
   134     openGlButton = new QToolButton;
       
   135     openGlButton->setText(tr("OpenGL"));
       
   136     openGlButton->setCheckable(true);
       
   137 #ifndef QT_NO_OPENGL
       
   138     openGlButton->setEnabled(QGLFormat::hasOpenGL());
       
   139 #else
       
   140     openGlButton->setEnabled(false);
       
   141 #endif
       
   142     printButton = new QToolButton;
       
   143     printButton->setIcon(QIcon(QPixmap(":/fileprint.png")));
       
   144 
       
   145     labelLayout->addWidget(label);
       
   146     labelLayout->addStretch();
       
   147     labelLayout->addWidget(antialiasButton);
       
   148     labelLayout->addWidget(openGlButton);
       
   149     labelLayout->addWidget(printButton);
       
   150 
       
   151     QGridLayout *topLayout = new QGridLayout;
       
   152     topLayout->addLayout(labelLayout, 0, 0);
       
   153     topLayout->addWidget(graphicsView, 1, 0);
       
   154     topLayout->addLayout(zoomSliderLayout, 1, 1);
       
   155     topLayout->addLayout(rotateSliderLayout, 2, 0);
       
   156     topLayout->addWidget(resetButton, 2, 1);
       
   157     setLayout(topLayout);
       
   158 
       
   159     connect(resetButton, SIGNAL(clicked()), this, SLOT(resetView()));
       
   160     connect(zoomSlider, SIGNAL(valueChanged(int)), this, SLOT(setupMatrix()));
       
   161     connect(rotateSlider, SIGNAL(valueChanged(int)), this, SLOT(setupMatrix()));
       
   162     connect(graphicsView->verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(setResetButtonEnabled()));
       
   163     connect(graphicsView->horizontalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(setResetButtonEnabled()));
       
   164     connect(antialiasButton, SIGNAL(toggled(bool)), this, SLOT(toggleAntialiasing()));
       
   165     connect(openGlButton, SIGNAL(toggled(bool)), this, SLOT(toggleOpenGL()));
       
   166     connect(rotateLeftIcon, SIGNAL(clicked()), this, SLOT(rotateLeft()));
       
   167     connect(rotateRightIcon, SIGNAL(clicked()), this, SLOT(rotateRight()));
       
   168     connect(zoomInIcon, SIGNAL(clicked()), this, SLOT(zoomIn()));
       
   169     connect(zoomOutIcon, SIGNAL(clicked()), this, SLOT(zoomOut()));
       
   170     connect(printButton, SIGNAL(clicked()), this, SLOT(print()));
       
   171 
       
   172     setupMatrix();
       
   173 
       
   174     startTimer(0);
       
   175 }
       
   176 
       
   177 QGraphicsView *View::view() const
       
   178 {
       
   179     return graphicsView;
       
   180 }
       
   181 
       
   182 void View::resetView()
       
   183 {
       
   184     zoomSlider->setValue(250);
       
   185     rotateSlider->setValue(0);
       
   186     setupMatrix();
       
   187     graphicsView->ensureVisible(QRectF(0, 0, 0, 0));
       
   188 
       
   189     resetButton->setEnabled(false);
       
   190 }
       
   191 
       
   192 void View::setResetButtonEnabled()
       
   193 {
       
   194     resetButton->setEnabled(true);
       
   195 }
       
   196 
       
   197 void View::setupMatrix()
       
   198 {
       
   199     qreal scale = qPow(qreal(2), (zoomSlider->value() - 250) / qreal(50));
       
   200 
       
   201     QMatrix matrix;
       
   202     matrix.scale(scale, scale);
       
   203     matrix.rotate(rotateSlider->value());
       
   204 
       
   205     graphicsView->setMatrix(matrix);
       
   206     setResetButtonEnabled();
       
   207 }
       
   208 
       
   209 void View::toggleOpenGL()
       
   210 {
       
   211 #ifndef QT_NO_OPENGL
       
   212     graphicsView->setViewport(openGlButton->isChecked() ? new QGLWidget(QGLFormat(QGL::SampleBuffers)) : new QWidget);
       
   213 #endif
       
   214 }
       
   215 
       
   216 void View::toggleAntialiasing()
       
   217 {
       
   218     graphicsView->setRenderHint(QPainter::Antialiasing, antialiasButton->isChecked());
       
   219 }
       
   220 
       
   221 void View::print()
       
   222 {
       
   223 #ifndef QT_NO_PRINTER
       
   224     QPrinter printer;
       
   225     QPrintDialog dialog(&printer, this);
       
   226     if (dialog.exec() == QDialog::Accepted) {
       
   227         QPainter painter(&printer);
       
   228         graphicsView->render(&painter);
       
   229     }
       
   230 #endif
       
   231 }
       
   232 
       
   233 void View::zoomIn()
       
   234 {
       
   235     zoomSlider->setValue(zoomSlider->value() + 1);
       
   236 }
       
   237 
       
   238 void View::zoomOut()
       
   239 {
       
   240     zoomSlider->setValue(zoomSlider->value() - 1);
       
   241 }
       
   242 
       
   243 void View::rotateLeft()
       
   244 {
       
   245     rotateSlider->setValue(rotateSlider->value() - 10);
       
   246 }
       
   247 
       
   248 void View::rotateRight()
       
   249 {
       
   250     rotateSlider->setValue(rotateSlider->value() + 10);
       
   251 }
       
   252 
       
   253 void View::timerEvent(QTimerEvent *)
       
   254 {
       
   255     graphicsView->horizontalScrollBar()->setValue(graphicsView->horizontalScrollBar()->value() + 1);
       
   256 }
       
   257