examples/effects/blurpicker/blurpicker.cpp
changeset 3 41300fa6a67c
parent 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
2:56cd8111b7f7 3:41300fa6a67c
    47 
    47 
    48 #ifndef M_PI
    48 #ifndef M_PI
    49 #define M_PI 3.14159265358979323846
    49 #define M_PI 3.14159265358979323846
    50 #endif
    50 #endif
    51 
    51 
    52 BlurPicker::BlurPicker(QWidget *parent): QGraphicsView(parent), m_index(0.0)
    52 BlurPicker::BlurPicker(QWidget *parent): QGraphicsView(parent), m_index(0.0), m_animation(this, "index")
    53 {
    53 {
    54     setBackgroundBrush(QPixmap(":/images/background.jpg"));
    54     setBackgroundBrush(QPixmap(":/images/background.jpg"));
    55     setScene(&m_scene);
    55     setScene(new QGraphicsScene(this));
    56 
    56 
    57     setupScene();
    57     setupScene();
    58     updateIconPositions();
    58     setIndex(0);
    59 
    59 
    60     connect(&m_timeLine, SIGNAL(valueChanged(qreal)), SLOT(updateIconPositions()));
    60     m_animation.setDuration(400);
    61     m_timeLine.setDuration(400);
    61     m_animation.setEasingCurve(QEasingCurve::InOutSine);
    62 
    62 
    63     setRenderHint(QPainter::Antialiasing, true);
    63     setRenderHint(QPainter::Antialiasing, true);
    64     setFrameStyle(QFrame::NoFrame);
    64     setFrameStyle(QFrame::NoFrame);
    65 }
    65 }
    66 
    66 
    67 void BlurPicker::updateIconPositions()
    67 qreal BlurPicker::index() const
    68 {
    68 {
    69     m_index = m_timeLine.currentFrame() / 1000.0;
    69     return m_index;
       
    70 }
       
    71 
       
    72 void BlurPicker::setIndex(qreal index)
       
    73 {
       
    74     m_index = index;
    70 
    75 
    71     qreal baseline = 0;
    76     qreal baseline = 0;
    72     for (int i = 0; i < m_icons.count(); ++i) {
    77     for (int i = 0; i < m_icons.count(); ++i) {
    73         QGraphicsItem *icon = m_icons[i];
    78         QGraphicsItem *icon = m_icons[i];
    74         qreal a = ((i + m_index) * 2 * M_PI) / m_icons.count();
    79         qreal a = ((i + m_index) * 2 * M_PI) / m_icons.count();
    80         icon->setPos(pos);
    85         icon->setPos(pos);
    81         baseline = qMax(baseline, ys);
    86         baseline = qMax(baseline, ys);
    82         static_cast<BlurEffect *>(icon->graphicsEffect())->setBaseLine(baseline);
    87         static_cast<BlurEffect *>(icon->graphicsEffect())->setBaseLine(baseline);
    83     }
    88     }
    84 
    89 
    85     m_scene.update();
    90     scene()->update();
    86 }
    91 }
    87 
    92 
    88 void BlurPicker::setupScene()
    93 void BlurPicker::setupScene()
    89 {
    94 {
    90     m_scene.setSceneRect(-200, -120, 400, 240);
    95     scene()->setSceneRect(-200, -120, 400, 240);
    91 
    96 
    92     QStringList names;
    97     QStringList names;
    93     names << ":/images/accessories-calculator.png";
    98     names << ":/images/accessories-calculator.png";
    94     names << ":/images/accessories-text-editor.png";
    99     names << ":/images/accessories-text-editor.png";
    95     names << ":/images/help-browser.png";
   100     names << ":/images/help-browser.png";
    99     names << ":/images/office-calendar.png";
   104     names << ":/images/office-calendar.png";
   100     names << ":/images/system-users.png";
   105     names << ":/images/system-users.png";
   101 
   106 
   102     for (int i = 0; i < names.count(); i++) {
   107     for (int i = 0; i < names.count(); i++) {
   103         QPixmap pixmap(names[i]);
   108         QPixmap pixmap(names[i]);
   104         QGraphicsPixmapItem *icon = m_scene.addPixmap(pixmap);
   109         QGraphicsPixmapItem *icon = scene()->addPixmap(pixmap);
   105         icon->setZValue(1);
   110         icon->setZValue(1);
   106         icon->setGraphicsEffect(new BlurEffect(icon));
   111         icon->setGraphicsEffect(new BlurEffect(icon));
   107         m_icons << icon;
   112         m_icons << icon;
   108     }
   113     }
   109 
   114 
   110     QGraphicsPixmapItem *bg = m_scene.addPixmap(QPixmap(":/images/background.jpg"));
   115     QGraphicsPixmapItem *bg = scene()->addPixmap(QPixmap(":/images/background.jpg"));
   111     bg->setZValue(0);
   116     bg->setZValue(0);
   112     bg->setPos(-200, -150);
   117     bg->setPos(-200, -150);
   113 }
   118 }
   114 
   119 
   115 void BlurPicker::keyPressEvent(QKeyEvent *event)
   120 void BlurPicker::keyPressEvent(QKeyEvent *event)
   116 {
   121 {
   117     if (event->key() == Qt::Key_Left) {
   122     int delta = 0;
   118         if (m_timeLine.state() == QTimeLine::NotRunning) {
   123     switch (event->key())
   119             m_timeLine.setFrameRange(m_index * 1000, m_index * 1000 - 1000);
   124     {
   120             m_timeLine.start();
   125     case Qt::Key_Left:
       
   126         delta = -1;
       
   127         break;
       
   128     case  Qt::Key_Right:
       
   129         delta = 1;
       
   130         break;
       
   131     default:
       
   132         break;
       
   133     }
       
   134     if (m_animation.state() == QAbstractAnimation::Stopped && delta) {
       
   135             m_animation.setEndValue(m_index + delta);
       
   136             m_animation.start();
   121             event->accept();
   137             event->accept();
   122         }
       
   123     }
       
   124 
       
   125     if (event->key() == Qt::Key_Right) {
       
   126         if (m_timeLine.state() == QTimeLine::NotRunning) {
       
   127             m_timeLine.setFrameRange(m_index * 1000, m_index * 1000 + 1000);
       
   128             m_timeLine.start();
       
   129             event->accept();
       
   130         }
       
   131     }
   138     }
   132 }
   139 }