OSDN Git Service

Introduce QUTF8 macro and replace ".toUtf8().constData()" everywhere.
[lamexp/LameXP.git] / src / Dialog_SplashScreen.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2013 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License along
16 // with this program; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 //
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
21
22 #include "Dialog_SplashScreen.h"
23
24 #include "Global.h"
25
26 #include <QThread>
27 #include <QMovie>
28 #include <QKeyEvent>
29 #include <QTimer>
30
31 #include "WinSevenTaskbar.h"
32
33 #define FADE_DELAY 16
34 #define OPACITY_DELTA 0.02
35
36 /* It can happen that the QThread has just terminated and already emitted the 'terminated' signal, but did NOT change the 'isRunning' flag to FALSE yet. */
37 /* For this reason the macro will first check the 'isRunning' flag. If (and only if) the flag still returns TRUE, we will call the wait() on the thread. */
38 #define THREAD_RUNNING(THRD) (((THRD)->isRunning()) ? (!((THRD)->wait(1))) : false)
39
40 #define SET_TASKBAR_STATE(FLAG) do \
41 { \
42         if(FLAG) \
43         { \
44                 if(!bTaskBar) bTaskBar = WinSevenTaskbar::setTaskbarState(splashScreen, WinSevenTaskbar::WinSevenTaskbarIndeterminateState); \
45         } \
46         else \
47         { \
48                 if(bTaskBar) bTaskBar = (!WinSevenTaskbar::setTaskbarState(splashScreen, WinSevenTaskbar::WinSevenTaskbarNoState)); \
49         } \
50 } \
51 while(0)
52
53 #define ASYNC_WAIT(LOOP, DELAY) do \
54 { \
55         QTimer::singleShot((DELAY), (LOOP), SLOT(quit())); \
56         (LOOP)->exec(QEventLoop::ExcludeUserInputEvents); \
57 } \
58 while(0)
59
60 ////////////////////////////////////////////////////////////
61 // Constructor
62 ////////////////////////////////////////////////////////////
63
64 SplashScreen::SplashScreen(QWidget *parent)
65 :
66         QFrame(parent, Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint)
67 {
68         //Init the dialog, from the .ui file
69         setupUi(this);
70
71         //Start animation
72         m_working = new QMovie(":/images/Loading.gif");
73         labelLoading->setMovie(m_working);
74         m_working->start();
75
76         //Set wait cursor
77         setCursor(Qt::WaitCursor);
78
79         //Prevent close
80         m_canClose = false;
81 }
82
83 ////////////////////////////////////////////////////////////
84 // Destructor
85 ////////////////////////////////////////////////////////////
86
87 SplashScreen::~SplashScreen(void)
88 {
89         if(m_working)
90         {
91                 m_working->stop();
92                 delete m_working;
93                 m_working = NULL;
94         }
95 }
96
97 ////////////////////////////////////////////////////////////
98 // PUBLIC FUNCTIONS
99 ////////////////////////////////////////////////////////////
100
101 void SplashScreen::showSplash(QThread *thread)
102 {
103         const int opacitySteps = qRound(1.0 / OPACITY_DELTA);
104         bool bTaskBar = false;
105         unsigned int deadlockCounter = 0;
106         SplashScreen *splashScreen = new SplashScreen();
107         
108         //Show splash
109         splashScreen->m_canClose = false;
110         splashScreen->setWindowOpacity(OPACITY_DELTA);
111         splashScreen->setFixedSize(splashScreen->size());
112         splashScreen->show();
113
114         //Wait for window to show
115         QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
116         splashScreen->repaint();
117         QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
118
119         //Setup the event loop
120         QEventLoop *loop = new QEventLoop(splashScreen);
121         connect(thread, SIGNAL(terminated()), loop, SLOT(quit()), Qt::QueuedConnection);
122         connect(thread, SIGNAL(finished()), loop, SLOT(quit()), Qt::QueuedConnection);
123
124         //Create timer
125         QTimer *timer = new QTimer();
126         connect(timer, SIGNAL(timeout()), loop, SLOT(quit()));
127
128         //Start the thread
129         thread->start();
130
131         //Init taskbar
132         SET_TASKBAR_STATE(true);
133
134         //Fade in
135         for(int i = 1; i <= opacitySteps; i++)
136         {
137                 const double opacity = (i < opacitySteps) ? (OPACITY_DELTA * static_cast<double>(i)) : 1.0;
138                 splashScreen->setWindowOpacity(opacity);
139                 splashScreen->update();
140                 ASYNC_WAIT(loop, FADE_DELAY);
141                 SET_TASKBAR_STATE(true);
142         }
143
144         //Start the timer
145         timer->start(30720);
146
147         //Loop while thread is still running
148         while(THREAD_RUNNING(thread))
149         {
150                 if((++deadlockCounter) > 60)
151                 {
152                         qFatal("Deadlock in initialization thread detected!");
153                 }
154                 ASYNC_WAIT(loop, 5000);
155         }
156
157         //Stop the timer
158         timer->stop();
159
160         //Fade out
161         for(int i = opacitySteps; i >= 0; i--)
162         {
163                 const double opacity = OPACITY_DELTA * static_cast<double>(i);
164                 splashScreen->setWindowOpacity(opacity);
165                 splashScreen->update();
166                 ASYNC_WAIT(loop, FADE_DELAY);
167         }
168
169         //Restore taskbar
170         SET_TASKBAR_STATE(false);
171
172         //Hide splash
173         splashScreen->m_canClose = true;
174         splashScreen->close();
175
176         //Free
177         LAMEXP_DELETE(loop);
178         LAMEXP_DELETE(timer);
179         LAMEXP_DELETE(splashScreen);
180 }
181
182 ////////////////////////////////////////////////////////////
183 // EVENTS
184 ////////////////////////////////////////////////////////////
185
186 void SplashScreen::keyPressEvent(QKeyEvent *event)
187 {
188         event->ignore();
189 }
190
191 void SplashScreen::keyReleaseEvent(QKeyEvent *event)
192 {
193         event->ignore();
194 }
195
196 void SplashScreen::closeEvent(QCloseEvent *event)
197 {
198         if(!m_canClose) event->ignore();
199 }
200
201 bool SplashScreen::winEvent(MSG *message, long *result)
202 {
203         return WinSevenTaskbar::handleWinEvent(message, result);
204 }