OSDN Git Service

do not send move and resize events to the widget from qFadeEffect()
[kde/Katie.git] / src / gui / widgets / qeffects.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Copyright (C) 2016 Ivailo Monev
5 **
6 ** This file is part of the QtGui module of the Katie Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 **
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser
12 ** General Public License version 2.1 as published by the Free Software
13 ** Foundation and appearing in the file LICENSE.LGPL included in the
14 ** packaging of this file.  Please review the following information to
15 ** ensure the GNU Lesser General Public License version 2.1 requirements
16 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** $QT_END_LICENSE$
19 **
20 ****************************************************************************/
21
22 #include "qapplication.h"
23 #ifndef QT_NO_EFFECTS
24 #include "qdesktopwidget.h"
25 #include "qeffects_p.h"
26 #include "qevent.h"
27 #include "qimage.h"
28 #include "qpainter.h"
29 #include "qpixmap.h"
30 #include "qpointer.h"
31 #include "qtimer.h"
32 #include "qelapsedtimer.h"
33 #include "qdebug.h"
34
35 QT_BEGIN_NAMESPACE
36
37 static const int s_duration = 150;
38
39 /*!
40     \internal
41
42     The QOpacityEffect object sets the widget opacity
43 */
44 class QOpacityEffect: public QObject
45 {
46     Q_OBJECT
47 public:
48     QOpacityEffect(QWidget* w);
49     ~QOpacityEffect();
50
51 private slots:
52     void fade();
53     void cancel();
54
55 private:
56     QPointer<QWidget> widget;
57     QTimer anim;
58     QElapsedTimer checkTime;
59 };
60
61 static QOpacityEffect* q_opacity = nullptr;
62
63 QOpacityEffect::QOpacityEffect(QWidget* w)
64     : QObject(w),
65     widget(w)
66 {
67     connect(widget, SIGNAL(destroyed()), this, SLOT(cancel()));
68
69     checkTime.start();
70     widget->setWindowOpacity(0.0);
71     widget->show();
72     connect(&anim, SIGNAL(timeout()), this, SLOT(fade()));
73     anim.start(1);
74 }
75
76 QOpacityEffect::~QOpacityEffect()
77 {
78     // Restore the opacity value
79     if (widget) {
80         widget->setWindowOpacity(1);
81     }
82 }
83
84 /*
85     Sets the widget opacity for the time elapsed
86 */
87 void QOpacityEffect::fade()
88 {
89     const int tempel = checkTime.elapsed();
90     const double alpha = tempel / double(s_duration);
91
92     if (alpha >= 1.0 || !widget) {
93         anim.stop();
94         if (widget) {
95             widget->setWindowOpacity(1.0);
96         }
97         q_opacity = nullptr;
98         deleteLater();
99     } else {
100         widget->setWindowOpacity(alpha);
101     }
102 }
103
104 void QOpacityEffect::cancel()
105 {
106     anim.stop();
107     q_opacity = nullptr;
108     deleteLater();
109 }
110
111 /*!
112     Fade in widget \a w.
113 */
114 void qFadeEffect(QWidget* w)
115 {
116     if (q_opacity) {
117         q_opacity = nullptr;
118         q_opacity->deleteLater();
119     }
120
121     if (!w) {
122         return;
123     }
124
125     q_opacity = new QOpacityEffect(w);
126 }
127
128 QT_END_NAMESPACE
129
130 #include "moc_qeffects.cpp"
131
132 #endif //QT_NO_EFFECTS