examples/effects/fademessage/fademessage.cpp
changeset 3 41300fa6a67c
parent 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
2:56cd8111b7f7 3:41300fa6a67c
    41 
    41 
    42 #include "fademessage.h"
    42 #include "fademessage.h"
    43 
    43 
    44 #include <QtGui>
    44 #include <QtGui>
    45 
    45 
    46 FadeMessage::FadeMessage(QWidget *parent): QGraphicsView(parent), m_index(0.0)
    46 FadeMessage::FadeMessage(QWidget *parent): QGraphicsView(parent)
    47 {
    47 {
    48     setScene(&m_scene);
    48     setScene(&m_scene);
    49     setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    49     setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    50     setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    50     setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    51 
    51 
    52     setupScene();
    52     setupScene();
    53 
    53 
    54     m_timeLine = new QTimeLine(500, this);
    54     m_animation = new QPropertyAnimation(m_effect, "strength", this);
    55     m_timeLine->setCurveShape(QTimeLine::EaseInOutCurve);
    55     m_animation->setDuration(500);
    56     connect(m_timeLine, SIGNAL(valueChanged(qreal)), m_effect, SLOT(setStrength(qreal)));
    56     m_animation->setEasingCurve(QEasingCurve::InOutSine);
       
    57     m_animation->setStartValue(0);
       
    58     m_animation->setEndValue(1);
       
    59 
    57 
    60 
    58     setRenderHint(QPainter::Antialiasing, true);
    61     setRenderHint(QPainter::Antialiasing, true);
    59     setFrameStyle(QFrame::NoFrame);
    62     setFrameStyle(QFrame::NoFrame);
    60 }
    63 }
    61 
    64 
    62 void FadeMessage::togglePopup()
    65 void FadeMessage::togglePopup()
    63 {
    66 {
    64     if (m_message->isVisible()) {
    67     if (m_message->isVisible()) {
    65         m_message->setVisible(false);
    68         m_message->setVisible(false);
    66         m_timeLine->setDirection(QTimeLine::Backward);
    69         m_animation->setDirection(QAbstractAnimation::Backward);
    67         m_timeLine->start();
       
    68     } else {
    70     } else {
    69         m_message->setVisible(true);
    71         m_message->setVisible(true);
    70         m_timeLine->setDirection(QTimeLine::Forward);
    72         m_animation->setDirection(QAbstractAnimation::Forward);
    71         m_timeLine->start();
       
    72     }
    73     }
       
    74     m_animation->start();
    73 }
    75 }
    74 
    76 
    75 void FadeMessage::setupScene()
    77 void FadeMessage::setupScene()
    76 {
    78 {
    77     QGraphicsRectItem *parent = m_scene.addRect(0, 0, 400, 600);
    79     QGraphicsRectItem *parent = m_scene.addRect(0, 0, 400, 600);
    92         }
    94         }
    93 
    95 
    94     QFont font;
    96     QFont font;
    95     font.setPointSize(font.pointSize() * 2);
    97     font.setPointSize(font.pointSize() * 2);
    96     font.setBold(true);
    98     font.setBold(true);
    97     int fh = QFontMetrics(font).height();
    99     QFontMetrics fontMetrics(font);
       
   100     int fh = fontMetrics.height();
    98 
   101 
    99     QGraphicsRectItem *block = m_scene.addRect(50, 300, 300, fh + 3);
   102     QString sceneText = "Qt Everywhere!";
       
   103     int sceneTextWidth = fontMetrics.width(sceneText);
       
   104 
       
   105     QGraphicsRectItem *block = m_scene.addRect(50, 300, sceneTextWidth, fh + 3);
   100     block->setPen(Qt::NoPen);
   106     block->setPen(Qt::NoPen);
   101     block->setBrush(QColor(102, 153, 51));
   107     block->setBrush(QColor(102, 153, 51));
   102 
   108 
   103     QGraphicsTextItem *text = m_scene.addText("Qt Everywhere!", font);
   109     QGraphicsTextItem *text = m_scene.addText(sceneText, font);
   104     text->setDefaultTextColor(Qt::white);
   110     text->setDefaultTextColor(Qt::white);
   105     text->setPos(50, 300);
   111     text->setPos(50, 300);
   106     block->setZValue(2);
   112     block->setZValue(2);
   107     block->hide();
   113     block->hide();
   108 
   114