Properly detect Windows 8, now that Qt supports it officially.
[LameXP.git] / src / Dialog_SplashScreen.cpp
blob6c361d0b30c4a291cea7115b412cdfe6db8f4b94
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.
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.
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
22 #include "Dialog_SplashScreen.h"
24 #include "Global.h"
26 #include <QThread>
27 #include <QMovie>
28 #include <QKeyEvent>
29 #include <QTimer>
31 #include "WinSevenTaskbar.h"
33 #define FADE_DELAY 16
34 #define OPACITY_DELTA 0.02
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)
41 #define SET_TASKBAR_STATE(FLAG) do \
42 { \
43 if(FLAG) \
44 { \
45 if(!bTaskBar) bTaskBar = WinSevenTaskbar::setTaskbarState(splashScreen, WinSevenTaskbar::WinSevenTaskbarIndeterminateState); \
46 } \
47 else \
48 { \
49 if(bTaskBar) bTaskBar = (!WinSevenTaskbar::setTaskbarState(splashScreen, WinSevenTaskbar::WinSevenTaskbarNoState)); \
50 } \
51 } \
52 while(0)
54 ////////////////////////////////////////////////////////////
55 // Constructor
56 ////////////////////////////////////////////////////////////
58 SplashScreen::SplashScreen(QWidget *parent)
60 QFrame(parent, Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint)
62 //Init the dialog, from the .ui file
63 setupUi(this);
65 //Start animation
66 m_working = new QMovie(":/images/Loading.gif");
67 labelLoading->setMovie(m_working);
68 m_working->start();
70 //Set wait cursor
71 setCursor(Qt::WaitCursor);
73 //Prevent close
74 m_canClose = false;
77 ////////////////////////////////////////////////////////////
78 // Destructor
79 ////////////////////////////////////////////////////////////
81 SplashScreen::~SplashScreen(void)
83 if(m_working)
85 m_working->stop();
86 delete m_working;
87 m_working = NULL;
91 ////////////////////////////////////////////////////////////
92 // PUBLIC FUNCTIONS
93 ////////////////////////////////////////////////////////////
95 void SplashScreen::showSplash(QThread *thread)
97 double opacity = OPACITY_DELTA;
98 const int opacitySteps = qRound(1.0 / OPACITY_DELTA);
99 SplashScreen *splashScreen = new SplashScreen();
100 bool bTaskBar = false;
102 //Show splash
103 splashScreen->m_canClose = false;
104 splashScreen->setWindowOpacity(opacity);
105 splashScreen->setFixedSize(splashScreen->size());
106 splashScreen->show();
108 //Wait for window to show
109 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
110 splashScreen->repaint();
111 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
113 //Setup the event loop
114 QEventLoop *loop = new QEventLoop(splashScreen);
115 connect(thread, SIGNAL(terminated()), loop, SLOT(quit()), Qt::QueuedConnection);
116 connect(thread, SIGNAL(finished()), loop, SLOT(quit()), Qt::QueuedConnection);
118 //Create timer
119 QTimer *timer = new QTimer();
120 connect(timer, SIGNAL(timeout()), loop, SLOT(quit()));
122 //Start thread
123 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
124 thread->start();
125 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
127 //Init taskbar
128 SET_TASKBAR_STATE(true);
130 //Fade in
131 for(int i = 1; i <= opacitySteps; i++)
133 opacity = (i < opacitySteps) ? (OPACITY_DELTA * static_cast<double>(i)) : 1.0;
134 splashScreen->setWindowOpacity(opacity);
135 splashScreen->update();
136 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents, FADE_DELAY);
137 SET_TASKBAR_STATE(true);
138 lamexp_sleep(FADE_DELAY);
141 //Start the timer
142 timer->start(30720);
144 //Loop while thread is still running
145 if(bool bIsRunning = THREAD_RUNNING(thread))
147 int deadlockCounter = 0;
148 while(bIsRunning)
150 loop->exec();
151 if(bIsRunning = THREAD_RUNNING(thread))
153 qWarning("Potential deadlock in initialization thread!");
154 if(++deadlockCounter >= 10) qFatal("Deadlock in initialization thread!");
159 //Stop the timer
160 timer->stop();
162 //Fade out
163 for(int i = opacitySteps; i >= 0; i--)
165 opacity = OPACITY_DELTA * static_cast<double>(i);
166 splashScreen->setWindowOpacity(opacity);
167 splashScreen->update();
168 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents, FADE_DELAY);
169 lamexp_sleep(FADE_DELAY);
172 //Restore taskbar
173 SET_TASKBAR_STATE(false);
175 //Hide splash
176 splashScreen->m_canClose = true;
177 splashScreen->close();
179 //Free
180 LAMEXP_DELETE(loop);
181 LAMEXP_DELETE(timer);
182 LAMEXP_DELETE(splashScreen);
185 ////////////////////////////////////////////////////////////
186 // EVENTS
187 ////////////////////////////////////////////////////////////
189 void SplashScreen::keyPressEvent(QKeyEvent *event)
191 event->ignore();
194 void SplashScreen::keyReleaseEvent(QKeyEvent *event)
196 event->ignore();
199 void SplashScreen::closeEvent(QCloseEvent *event)
201 if(!m_canClose) event->ignore();
204 bool SplashScreen::winEvent(MSG *message, long *result)
206 return WinSevenTaskbar::handleWinEvent(message, result);