1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
14 **   * Redistributions of source code must retain the above copyright
15 **     notice, this list of conditions and the following disclaimer.
16 **   * Redistributions in binary form must reproduce the above copyright
17 **     notice, this list of conditions and the following disclaimer in
18 **     the documentation and/or other materials provided with the
19 **     distribution.
20 **   * Neither the name of The Qt Company Ltd nor the names of its
21 **     contributors may be used to endorse or promote products derived
22 **     from this software without specific prior written permission.
23 **
24 **
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40 
41 #include "fademessage.h"
42 
43 #include <QtGui>
44 
FadeMessage(QWidget * parent)45 FadeMessage::FadeMessage(QWidget *parent): QGraphicsView(parent)
46 {
47     setScene(&m_scene);
48     setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
49     setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
50 
51     setupScene();
52 
53     m_animation = new QPropertyAnimation(m_effect, "strength", this);
54     m_animation->setDuration(500);
55     m_animation->setEasingCurve(QEasingCurve::InOutSine);
56     m_animation->setStartValue(0);
57     m_animation->setEndValue(1);
58 
59     setRenderHint(QPainter::Antialiasing, true);
60     setFrameStyle(QFrame::NoFrame);
61 }
62 
togglePopup()63 void FadeMessage::togglePopup()
64 {
65     if (m_message->isVisible()) {
66         m_message->setVisible(false);
67         m_animation->setDirection(QAbstractAnimation::Backward);
68     } else {
69         m_message->setVisible(true);
70         m_animation->setDirection(QAbstractAnimation::Forward);
71     }
72     m_animation->start();
73 }
74 
setupScene()75 void FadeMessage::setupScene()
76 {
77     QGraphicsRectItem *parent = m_scene.addRect(0, 0, 800, 600);
78     parent->setPen(Qt::NoPen);
79     parent->setZValue(0);
80 
81     QGraphicsPixmapItem *bg = m_scene.addPixmap(QPixmap(":/background.jpg"));
82     bg->setParentItem(parent);
83     bg->setZValue(-1);
84 
85     for (int i = 1; i < 5; ++i)
86         for (int j = 2; j < 5; ++j) {
87             QGraphicsRectItem *item = m_scene.addRect(i * 50, (j - 1) * 50, 38, 38);
88             item->setParentItem(parent);
89             item->setZValue(1);
90             int hue = 12 * (i * 5 + j);
91             item->setBrush(QColor::fromHsv(hue, 128, 128));
92         }
93 
94     QFont font;
95     font.setPointSize(font.pointSize() * 2);
96     font.setBold(true);
97     QFontMetrics fontMetrics(font);
98     int fh = fontMetrics.height();
99 
100     QString sceneText = "Qt Everywhere!";
101     int sceneTextWidth = fontMetrics.width(sceneText);
102 
103     QGraphicsRectItem *block = m_scene.addRect(50, 300, sceneTextWidth, fh + 3);
104     block->setPen(Qt::NoPen);
105     block->setBrush(QColor(102, 153, 51));
106 
107     QGraphicsTextItem *text = m_scene.addText(sceneText, font);
108     text->setDefaultTextColor(Qt::white);
109     text->setPos(50, 300);
110     block->setZValue(2);
111     block->hide();
112 
113     text->setParentItem(block);
114     m_message = block;
115 
116     m_effect = new QGraphicsColorizeEffect;
117     m_effect->setColor(QColor(122, 193, 66));
118     m_effect->setStrength(0);
119     m_effect->setEnabled(true);
120     parent->setGraphicsEffect(m_effect);
121 
122     QPushButton *press = new QPushButton;
123     press->setText(tr("Press me"));
124     connect(press, SIGNAL(clicked()), SLOT(togglePopup()));
125     m_scene.addWidget(press);
126 
127 #if defined(Q_WS_S60) || defined(Q_WS_MAEMO_5)
128     press->move(200, 210);
129 #else
130     press->move(300, 500);
131 #endif
132 }
133