1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2013 LoRd_MuldeR <MuldeR2@GMX.de>
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.
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"
31 #include "WinSevenTaskbar.h"
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, we will call the wait() on the thread. */
38 #define THREAD_RUNNING(THRD) (((THRD)->isRunning()) ? (!((THRD)->wait(1))) : false)
40 #define SET_TASKBAR_STATE(FLAG) do \
44 if(!bTaskBar) bTaskBar = WinSevenTaskbar::setTaskbarState(splashScreen, WinSevenTaskbar::WinSevenTaskbarIndeterminateState); \
48 if(bTaskBar) bTaskBar = (!WinSevenTaskbar::setTaskbarState(splashScreen, WinSevenTaskbar::WinSevenTaskbarNoState)); \
53 #define ASYNC_WAIT(LOOP, DELAY) do \
55 QTimer::singleShot((DELAY), (LOOP), SLOT(quit())); \
56 (LOOP)->exec(QEventLoop::ExcludeUserInputEvents); \
60 ////////////////////////////////////////////////////////////
62 ////////////////////////////////////////////////////////////
64 SplashScreen::SplashScreen(QWidget *parent)
66 QFrame(parent, Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint)
68 //Init the dialog, from the .ui file
72 m_working = new QMovie(":/images/Loading.gif");
73 labelLoading->setMovie(m_working);
77 setCursor(Qt::WaitCursor);
83 ////////////////////////////////////////////////////////////
85 ////////////////////////////////////////////////////////////
87 SplashScreen::~SplashScreen(void)
97 ////////////////////////////////////////////////////////////
99 ////////////////////////////////////////////////////////////
101 void SplashScreen::showSplash(QThread *thread)
103 const int opacitySteps = qRound(1.0 / OPACITY_DELTA);
104 bool bTaskBar = false;
105 unsigned int deadlockCounter = 0;
106 SplashScreen *splashScreen = new SplashScreen();
109 splashScreen->m_canClose = false;
110 splashScreen->setWindowOpacity(OPACITY_DELTA);
111 splashScreen->setFixedSize(splashScreen->size());
112 splashScreen->show();
114 //Wait for window to show
115 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
116 splashScreen->repaint();
117 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
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);
125 QTimer *timer = new QTimer();
126 connect(timer, SIGNAL(timeout()), loop, SLOT(quit()));
132 SET_TASKBAR_STATE(true);
135 for(int i = 1; i <= opacitySteps; i++)
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);
147 //Loop while thread is still running
148 while(THREAD_RUNNING(thread))
150 if((++deadlockCounter) > 60)
152 qFatal("Deadlock in initialization thread detected!");
154 ASYNC_WAIT(loop, 5000);
161 for(int i = opacitySteps; i >= 0; i--)
163 const double opacity = OPACITY_DELTA * static_cast<double>(i);
164 splashScreen->setWindowOpacity(opacity);
165 splashScreen->update();
166 ASYNC_WAIT(loop, FADE_DELAY);
170 SET_TASKBAR_STATE(false);
173 splashScreen->m_canClose = true;
174 splashScreen->close();
178 LAMEXP_DELETE(timer);
179 LAMEXP_DELETE(splashScreen);
182 ////////////////////////////////////////////////////////////
184 ////////////////////////////////////////////////////////////
186 void SplashScreen::keyPressEvent(QKeyEvent *event)
191 void SplashScreen::keyReleaseEvent(QKeyEvent *event)
196 void SplashScreen::closeEvent(QCloseEvent *event)
198 if(!m_canClose) event->ignore();
201 bool SplashScreen::winEvent(MSG *message, long *result)
203 return WinSevenTaskbar::handleWinEvent(message, result);