tests/manual/gestures/graphicsview/main.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
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 <QtGui>
       
    43 
       
    44 #include "imageitem.h"
       
    45 #include "gestures.h"
       
    46 #include "mousepangesturerecognizer.h"
       
    47 
       
    48 class GraphicsView : public QGraphicsView
       
    49 {
       
    50 public:
       
    51     GraphicsView(QGraphicsScene *scene, QWidget *parent = 0)
       
    52         : QGraphicsView(scene, parent)
       
    53     {
       
    54     }
       
    55 protected:
       
    56     bool viewportEvent(QEvent *event)
       
    57     {
       
    58         if (event->type() == QEvent::Gesture) {
       
    59             QGestureEvent *ge = static_cast<QGestureEvent *>(event);
       
    60             if (QPanGesture *pan = static_cast<QPanGesture *>(ge->gesture(Qt::PanGesture))) {
       
    61                 switch (pan->state()) {
       
    62                 case Qt::GestureStarted: qDebug("view: Pan: started"); break;
       
    63                 case Qt::GestureFinished: qDebug("view: Pan: finished"); break;
       
    64                 case Qt::GestureCanceled: qDebug("view: Pan: canceled"); break;
       
    65                 case Qt::GestureUpdated: break;
       
    66                 default: qDebug("view: Pan: <unknown state>"); break;
       
    67                 }
       
    68 
       
    69                 const QPointF offset = pan->offset();
       
    70                 QScrollBar *vbar = verticalScrollBar();
       
    71                 QScrollBar *hbar = horizontalScrollBar();
       
    72                 vbar->setValue(vbar->value() - offset.y());
       
    73                 hbar->setValue(hbar->value() - offset.x());
       
    74                 ge->accept(pan);
       
    75                 return true;
       
    76             }
       
    77         }
       
    78         return QGraphicsView::viewportEvent(event);
       
    79     }
       
    80 };
       
    81 
       
    82 class StandardGestures : public QWidget
       
    83 {
       
    84 public:
       
    85     StandardGestures(QWidget *parent = 0)
       
    86             : QWidget(parent)
       
    87     {
       
    88         scene = new QGraphicsScene(this);
       
    89         scene->setSceneRect(-2000, -2000, 4000, 4000);
       
    90         view = new QGraphicsView(scene, 0);
       
    91         QVBoxLayout *l = new QVBoxLayout(this);
       
    92         l->addWidget(view);
       
    93     }
       
    94 
       
    95     QGraphicsScene *scene;
       
    96     QGraphicsView *view;
       
    97 };
       
    98 
       
    99 class GlobalViewGestures : public QWidget
       
   100 {
       
   101     Q_OBJECT
       
   102 public:
       
   103     GlobalViewGestures(QWidget *parent = 0)
       
   104             : QWidget(parent)
       
   105     {
       
   106         scene = new QGraphicsScene(this);
       
   107         scene->setSceneRect(-2000, -2000, 4000, 4000);
       
   108         view = new GraphicsView(scene, 0);
       
   109         view->viewport()->grabGesture(Qt::PanGesture);
       
   110         view->viewport()->grabGesture(ThreeFingerSlideGesture::Type);
       
   111         QVBoxLayout *l = new QVBoxLayout(this);
       
   112         l->addWidget(view);
       
   113     }
       
   114 
       
   115     QGraphicsScene *scene;
       
   116     QGraphicsView *view;
       
   117 };
       
   118 
       
   119 class GraphicsItemGestures : public QWidget
       
   120 {
       
   121     Q_OBJECT
       
   122 public:
       
   123     GraphicsItemGestures(QWidget *parent = 0)
       
   124             : QWidget(parent)
       
   125     {
       
   126         scene = new QGraphicsScene(this);
       
   127         scene->setSceneRect(-2000, -2000, 4000, 4000);
       
   128         view = new QGraphicsView(scene, 0);
       
   129         view->viewport()->grabGesture(Qt::PanGesture);
       
   130         view->viewport()->grabGesture(ThreeFingerSlideGesture::Type);
       
   131         QVBoxLayout *l = new QVBoxLayout(this);
       
   132         l->addWidget(view);
       
   133     }
       
   134 
       
   135     QGraphicsScene *scene;
       
   136     QGraphicsView *view;
       
   137 };
       
   138 
       
   139 class MainWindow : public QMainWindow
       
   140 {
       
   141 public:
       
   142     MainWindow();
       
   143 
       
   144     void setDirectory(const QString &path);
       
   145 
       
   146 private:
       
   147     QTabWidget *tabWidget;
       
   148     StandardGestures *standardGestures;
       
   149     GlobalViewGestures *globalViewGestures;
       
   150     GraphicsItemGestures *graphicsItemGestures;
       
   151 };
       
   152 
       
   153 MainWindow::MainWindow()
       
   154 {
       
   155     (void)qApp->registerGestureRecognizer(new MousePanGestureRecognizer);
       
   156     ThreeFingerSlideGesture::Type = qApp->registerGestureRecognizer(new ThreeFingerSlideGestureRecognizer);
       
   157 
       
   158     tabWidget = new QTabWidget;
       
   159 
       
   160     standardGestures = new StandardGestures;
       
   161     tabWidget->addTab(standardGestures, "Standard gestures");
       
   162 
       
   163     globalViewGestures = new GlobalViewGestures;
       
   164     tabWidget->addTab(globalViewGestures , "Global gestures");
       
   165 
       
   166     graphicsItemGestures = new GraphicsItemGestures;
       
   167     tabWidget->addTab(graphicsItemGestures, "Graphics item gestures");
       
   168 
       
   169     setCentralWidget(tabWidget);
       
   170 }
       
   171 
       
   172 void MainWindow::setDirectory(const QString &path)
       
   173 {
       
   174     QDir dir(path);
       
   175     QStringList files = dir.entryList(QDir::Files | QDir::Readable | QDir::NoDotAndDotDot);
       
   176     foreach(const QString &file, files) {
       
   177         QImageReader img(path + QLatin1String("/")+file);
       
   178         QImage image = img.read();
       
   179         if (!image.isNull()) {
       
   180             {
       
   181                 ImageItem *item = new ImageItem(image);
       
   182                 item->setPos(0, 0);
       
   183                 item->setFlags(QGraphicsItem::ItemIsMovable);
       
   184                 standardGestures->scene->addItem(item);
       
   185             }
       
   186             {
       
   187                 ImageItem *item = new ImageItem(image);
       
   188                 item->setPos(0, 0);
       
   189                 item->setFlags(QGraphicsItem::ItemIsMovable);
       
   190                 globalViewGestures->scene->addItem(item);
       
   191             }
       
   192             {
       
   193                 GestureImageItem *item = new GestureImageItem(image);
       
   194                 item->setPos(0, 0);
       
   195                 item->setFlags(QGraphicsItem::ItemIsMovable);
       
   196                 graphicsItemGestures->scene->addItem(item);
       
   197             }
       
   198         }
       
   199     }
       
   200 
       
   201     {
       
   202         QList<QGraphicsItem*> items = standardGestures->scene->items();
       
   203         if (!items.isEmpty())
       
   204             standardGestures->view->ensureVisible(items.at(0));
       
   205     }
       
   206     {
       
   207         QList<QGraphicsItem*> items = globalViewGestures->scene->items();
       
   208         if (!items.isEmpty())
       
   209             globalViewGestures->view->ensureVisible(items.at(0));
       
   210     }
       
   211     {
       
   212         QList<QGraphicsItem*> items = graphicsItemGestures->scene->items();
       
   213         if (!items.isEmpty())
       
   214             graphicsItemGestures->view->ensureVisible(items.at(0));
       
   215     }
       
   216 }
       
   217 
       
   218 int main(int argc, char **argv)
       
   219 {
       
   220     QApplication app(argc, argv);
       
   221     MainWindow window;
       
   222     if (QApplication::arguments().size() > 1)
       
   223         window.setDirectory(QApplication::arguments().at(1));
       
   224     else
       
   225         window.setDirectory(QFileDialog::getExistingDirectory(0, "Select image folder"));
       
   226     window.show();
       
   227     return app.exec();
       
   228 }
       
   229 
       
   230 #include "main.moc"