OSDN Git Service

Version v4.04 is released!
[lamexp/LameXP.git] / src / Dialog_SplashScreen.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2012 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, then we will wait() for at most 50 ms. */
38 /* If, after 50 ms, the wait() function returns with FALSE, then the thread probably is still running and we return TRUE. Otherwise we can return FALSE. */
39 #define THREAD_RUNNING(THRD) (((THRD)->isRunning()) ? (!((THRD)->wait(50))) : false)
40
41
42 ////////////////////////////////////////////////////////////
43 // Constructor
44 ////////////////////////////////////////////////////////////
45
46 SplashScreen::SplashScreen(QWidget *parent)
47 :
48         QFrame(parent, Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint)
49 {
50         //Init the dialog, from the .ui file
51         setupUi(this);
52
53         //Start animation
54         m_working = new QMovie(":/images/Loading.gif");
55         labelLoading->setMovie(m_working);
56         m_working->start();
57
58         //Set wait cursor
59         setCursor(Qt::WaitCursor);
60
61         //Prevent close
62         m_canClose = false;
63 }
64
65 ////////////////////////////////////////////////////////////
66 // Destructor
67 ////////////////////////////////////////////////////////////
68
69 SplashScreen::~SplashScreen(void)
70 {
71         if(m_working)
72         {
73                 m_working->stop();
74                 delete m_working;
75                 m_working = NULL;
76         }
77 }
78
79 ////////////////////////////////////////////////////////////
80 // PUBLIC FUNCTIONS
81 ////////////////////////////////////////////////////////////
82
83 void SplashScreen::showSplash(QThread *thread)
84 {
85         double opacity = OPACITY_DELTA;
86         const int opacitySteps = qRound(1.0 / OPACITY_DELTA);
87         SplashScreen *splashScreen = new SplashScreen();
88         
89         //Show splash
90         splashScreen->m_canClose = false;
91         splashScreen->setWindowOpacity(opacity);
92         splashScreen->setFixedSize(splashScreen->size());
93         splashScreen->show();
94
95         //Wait for window to show
96         QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
97         splashScreen->repaint();
98         QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
99
100         //Setup the event loop
101         QEventLoop *loop = new QEventLoop(splashScreen);
102         connect(thread, SIGNAL(terminated()), loop, SLOT(quit()), Qt::QueuedConnection);
103         connect(thread, SIGNAL(finished()), loop, SLOT(quit()), Qt::QueuedConnection);
104
105         //Create timer
106         QTimer *timer = new QTimer();
107         connect(timer, SIGNAL(timeout()), loop, SLOT(quit()));
108
109         //Start thread
110         QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
111         thread->start();
112         QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
113
114         //Init taskbar
115         WinSevenTaskbar::setTaskbarState(splashScreen, WinSevenTaskbar::WinSevenTaskbarIndeterminateState);
116
117         //Fade in
118         for(int i = 1; i <= opacitySteps; i++)
119         {
120                 opacity = OPACITY_DELTA * static_cast<double>(i);
121                 splashScreen->setWindowOpacity(opacity);
122                 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents, FADE_DELAY);
123                 Sleep(FADE_DELAY);
124         }
125
126         //Start the timer
127         timer->start(30720);
128
129         //Loop while thread is still running
130         if(bool bIsRunning = THREAD_RUNNING(thread))
131         {
132                 while(bIsRunning)
133                 {
134                         loop->exec();
135                         if(bIsRunning = THREAD_RUNNING(thread))
136                         {
137                                 qWarning("Potential deadlock in initialization thread!");
138                         }
139                 }
140         }
141
142         //Stop the timer
143         timer->stop();
144
145         //Fade out
146         for(int i = opacitySteps; i >= 0; i--)
147         {
148                 opacity = OPACITY_DELTA * static_cast<double>(i);
149                 splashScreen->setWindowOpacity(opacity);
150                 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents, FADE_DELAY);
151                 Sleep(FADE_DELAY);
152         }
153
154         //Restore taskbar
155         WinSevenTaskbar::setTaskbarState(splashScreen, WinSevenTaskbar::WinSevenTaskbarNoState);
156
157         //Hide splash
158         splashScreen->m_canClose = true;
159         splashScreen->close();
160
161         //Free
162         LAMEXP_DELETE(loop);
163         LAMEXP_DELETE(timer);
164         LAMEXP_DELETE(splashScreen);
165 }
166
167 ////////////////////////////////////////////////////////////
168 // EVENTS
169 ////////////////////////////////////////////////////////////
170
171 void SplashScreen::keyPressEvent(QKeyEvent *event)
172 {
173         event->ignore();
174 }
175
176 void SplashScreen::keyReleaseEvent(QKeyEvent *event)
177 {
178         event->ignore();
179 }
180
181 void SplashScreen::closeEvent(QCloseEvent *event)
182 {
183         if(!m_canClose) event->ignore();
184 }
185
186 bool SplashScreen::winEvent(MSG *message, long *result)
187 {
188         return WinSevenTaskbar::handleWinEvent(message, result);
189 }