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, 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 \
45 if(!bTaskBar) bTaskBar = WinSevenTaskbar::setTaskbarState(splashScreen, WinSevenTaskbar::WinSevenTaskbarIndeterminateState); \
49 if(bTaskBar) bTaskBar = (!WinSevenTaskbar::setTaskbarState(splashScreen, WinSevenTaskbar::WinSevenTaskbarNoState)); \
54 ////////////////////////////////////////////////////////////
56 ////////////////////////////////////////////////////////////
58 SplashScreen::SplashScreen(QWidget *parent)
60 QFrame(parent, Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint)
62 //Init the dialog, from the .ui file
66 m_working = new QMovie(":/images/Loading.gif");
67 labelLoading->setMovie(m_working);
71 setCursor(Qt::WaitCursor);
77 ////////////////////////////////////////////////////////////
79 ////////////////////////////////////////////////////////////
81 SplashScreen::~SplashScreen(void)
91 ////////////////////////////////////////////////////////////
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;
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);
119 QTimer *timer = new QTimer();
120 connect(timer, SIGNAL(timeout()), loop, SLOT(quit()));
123 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
125 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
128 SET_TASKBAR_STATE(true);
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 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents, FADE_DELAY);
136 SET_TASKBAR_STATE(true);
143 //Loop while thread is still running
144 if(bool bIsRunning = THREAD_RUNNING(thread))
149 if(bIsRunning = THREAD_RUNNING(thread))
151 qWarning("Potential deadlock in initialization thread!");
160 for(int i = opacitySteps; i >= 0; i--)
162 opacity = OPACITY_DELTA * static_cast<double>(i);
163 splashScreen->setWindowOpacity(opacity);
164 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents, FADE_DELAY);
169 SET_TASKBAR_STATE(false);
172 splashScreen->m_canClose = true;
173 splashScreen->close();
177 LAMEXP_DELETE(timer);
178 LAMEXP_DELETE(splashScreen);
181 ////////////////////////////////////////////////////////////
183 ////////////////////////////////////////////////////////////
185 void SplashScreen::keyPressEvent(QKeyEvent *event)
190 void SplashScreen::keyReleaseEvent(QKeyEvent *event)
195 void SplashScreen::closeEvent(QCloseEvent *event)
197 if(!m_canClose) event->ignore();
200 bool SplashScreen::winEvent(MSG *message, long *result)
202 return WinSevenTaskbar::handleWinEvent(message, result);