examples/graphicsview/padnavigator/panel.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 examples 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 "panel.h"
       
    43 #include "roundrectitem.h"
       
    44 #include "splashitem.h"
       
    45 #include "ui_backside.h"
       
    46 
       
    47 #ifndef QT_NO_OPENGL
       
    48 #include <QtOpenGL/QtOpenGL>
       
    49 #else
       
    50 #endif
       
    51 #include <QtGui/QtGui>
       
    52 
       
    53 #include <math.h>
       
    54 
       
    55 Panel::Panel(int width, int height)
       
    56     : selectedX(0),
       
    57       selectedY(0),
       
    58       width(width),
       
    59       height(height),
       
    60       flipped(false),
       
    61       flipLeft(true)
       
    62 {
       
    63     setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
       
    64     setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
       
    65     setCacheMode(CacheBackground);
       
    66     setViewportUpdateMode(FullViewportUpdate);
       
    67     setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform
       
    68                    | QPainter::TextAntialiasing);
       
    69     setBackgroundBrush(QPixmap(":/images/blue_angle_swirl.jpg"));
       
    70 #ifndef QT_NO_OPENGL
       
    71     setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers)));
       
    72 #endif
       
    73     setMinimumSize(50, 50);
       
    74 
       
    75     selectionTimeLine = new QTimeLine(150, this);
       
    76     flipTimeLine = new QTimeLine(500, this);
       
    77 
       
    78     QRectF bounds((-width / 2.0) * 150, (-height / 2.0) * 150, width * 150, height * 150);
       
    79 
       
    80     scene = new QGraphicsScene(bounds, this);
       
    81     scene->setItemIndexMethod(QGraphicsScene::NoIndex);
       
    82     setScene(scene);
       
    83 
       
    84     baseItem = new RoundRectItem(bounds, QColor(226, 255, 92, 64));
       
    85     scene->addItem(baseItem);
       
    86 
       
    87     QWidget *embed = new QWidget;
       
    88     ui = new Ui_BackSide;
       
    89     ui->setupUi(embed);
       
    90     ui->hostName->setFocus();
       
    91 
       
    92     backItem = new RoundRectItem(bounds, embed->palette().window(), embed);
       
    93     backItem->setTransform(QTransform().rotate(180, Qt::YAxis));
       
    94     backItem->setParentItem(baseItem);
       
    95         
       
    96     selectionItem = new RoundRectItem(QRectF(-60, -60, 120, 120), Qt::gray);
       
    97     selectionItem->setParentItem(baseItem);
       
    98     selectionItem->setZValue(-1);
       
    99     selectionItem->setPos(posForLocation(0, 0));
       
   100     startPos = selectionItem->pos();
       
   101 
       
   102     grid = new QGraphicsItem **[height];
       
   103         
       
   104     for (int y = 0; y < height; ++y) {
       
   105         grid[y] = new QGraphicsItem *[width];
       
   106 
       
   107         for (int x = 0; x < width; ++x) {
       
   108             RoundRectItem *item = new RoundRectItem(QRectF(-54, -54, 108, 108),
       
   109                                                     QColor(214, 240, 110, 128));
       
   110             item->setPos(posForLocation(x, y));
       
   111                 
       
   112             item->setParentItem(baseItem);
       
   113             item->setFlag(QGraphicsItem::ItemIsFocusable);
       
   114             grid[y][x] = item;
       
   115 
       
   116             switch (qrand() % 9) {
       
   117             case 0: item->setPixmap(QPixmap(":/images/kontact_contacts.png")); break;
       
   118             case 1: item->setPixmap(QPixmap(":/images/kontact_journal.png")); break;
       
   119             case 2: item->setPixmap(QPixmap(":/images/kontact_notes.png")); break;
       
   120             case 3: item->setPixmap(QPixmap(":/images/kopeteavailable.png")); break;
       
   121             case 4: item->setPixmap(QPixmap(":/images/metacontact_online.png")); break;
       
   122             case 5: item->setPixmap(QPixmap(":/images/minitools.png")); break;
       
   123             case 6: item->setPixmap(QPixmap(":/images/kontact_journal.png")); break;
       
   124             case 7: item->setPixmap(QPixmap(":/images/kontact_contacts.png")); break;
       
   125             case 8: item->setPixmap(QPixmap(":/images/kopeteavailable.png")); break;
       
   126             default:
       
   127                 break;
       
   128             }
       
   129 
       
   130             connect(item, SIGNAL(activated()), this, SLOT(flip()));
       
   131         }
       
   132     }
       
   133 
       
   134     grid[0][0]->setFocus();
       
   135 
       
   136     connect(backItem, SIGNAL(activated()),
       
   137             this, SLOT(flip()));
       
   138     connect(selectionTimeLine, SIGNAL(valueChanged(qreal)),
       
   139             this, SLOT(updateSelectionStep(qreal)));
       
   140     connect(flipTimeLine, SIGNAL(valueChanged(qreal)),
       
   141             this, SLOT(updateFlipStep(qreal)));
       
   142 
       
   143     splash = new SplashItem;
       
   144     splash->setZValue(5);
       
   145     splash->setPos(-splash->rect().width() / 2, scene->sceneRect().top());
       
   146     scene->addItem(splash);
       
   147 
       
   148     splash->grabKeyboard();
       
   149     
       
   150     updateSelectionStep(0);
       
   151 
       
   152     setWindowTitle(tr("Pad Navigator Example"));
       
   153 }
       
   154 
       
   155 Panel::~Panel()
       
   156 {
       
   157     for (int y = 0; y < height; ++y)
       
   158         delete [] grid[y];
       
   159     delete [] grid;
       
   160 }
       
   161     
       
   162 void Panel::keyPressEvent(QKeyEvent *event)
       
   163 {
       
   164     if (splash->isVisible() || event->key() == Qt::Key_Return || flipped) {
       
   165         QGraphicsView::keyPressEvent(event);
       
   166         return;
       
   167     }
       
   168 
       
   169     selectedX = (selectedX + width + (event->key() == Qt::Key_Right) - (event->key() == Qt::Key_Left)) % width;
       
   170     selectedY = (selectedY + height + (event->key() == Qt::Key_Down) - (event->key() == Qt::Key_Up)) % height;
       
   171     grid[selectedY][selectedX]->setFocus();
       
   172     
       
   173     selectionTimeLine->stop();
       
   174     startPos = selectionItem->pos();
       
   175     endPos = posForLocation(selectedX, selectedY);
       
   176     selectionTimeLine->start();
       
   177 }
       
   178 
       
   179 void Panel::resizeEvent(QResizeEvent *event)
       
   180 {
       
   181     QGraphicsView::resizeEvent(event);
       
   182     fitInView(scene->sceneRect(), Qt::KeepAspectRatio);
       
   183 }
       
   184 
       
   185 void Panel::updateSelectionStep(qreal val)
       
   186 {
       
   187     QPointF newPos(startPos.x() + (endPos - startPos).x() * val,
       
   188                    startPos.y() + (endPos - startPos).y() * val);
       
   189     selectionItem->setPos(newPos);
       
   190     
       
   191     QTransform transform;
       
   192     yrot = newPos.x() / 6.0;
       
   193     xrot = newPos.y() / 6.0;
       
   194     transform.rotate(newPos.x() / 6.0, Qt::YAxis);
       
   195     transform.rotate(newPos.y() / 6.0, Qt::XAxis);
       
   196     baseItem->setTransform(transform);
       
   197 }
       
   198 
       
   199 void Panel::updateFlipStep(qreal val)
       
   200 {
       
   201     qreal finalxrot = xrot - xrot * val;
       
   202     qreal finalyrot;
       
   203     if (flipLeft)
       
   204         finalyrot = yrot - yrot * val - 180 * val;
       
   205     else
       
   206         finalyrot = yrot - yrot * val + 180 * val;
       
   207     QTransform transform;
       
   208     transform.rotate(finalyrot, Qt::YAxis);
       
   209     transform.rotate(finalxrot, Qt::XAxis);
       
   210     qreal scale = 1 - sin(3.14 * val) * 0.3;
       
   211     transform.scale(scale, scale);
       
   212     baseItem->setTransform(transform);
       
   213     if (val == 0)
       
   214         grid[selectedY][selectedX]->setFocus();
       
   215 }
       
   216 
       
   217 void Panel::flip()
       
   218 {
       
   219     if (flipTimeLine->state() == QTimeLine::Running)
       
   220         return;
       
   221 
       
   222     if (flipTimeLine->currentValue() == 0) {
       
   223         flipTimeLine->setDirection(QTimeLine::Forward);
       
   224         flipTimeLine->start();
       
   225         flipped = true;
       
   226         flipLeft = selectionItem->pos().x() < 0;
       
   227     } else {
       
   228         flipTimeLine->setDirection(QTimeLine::Backward);
       
   229         flipTimeLine->start();
       
   230         flipped = false;
       
   231     }
       
   232 }
       
   233 
       
   234 QPointF Panel::posForLocation(int x, int y) const
       
   235 {
       
   236     return QPointF(x * 150, y * 150)
       
   237         - QPointF((width - 1) * 75, (height - 1) * 75);
       
   238 }