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 splashScreen
->update();
136 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents
, FADE_DELAY
);
137 SET_TASKBAR_STATE(true);
138 lamexp_sleep(FADE_DELAY
);
144 //Loop while thread is still running
145 if(bool bIsRunning
= THREAD_RUNNING(thread
))
147 int deadlockCounter
= 0;
151 if(bIsRunning
= THREAD_RUNNING(thread
))
153 qWarning("Potential deadlock in initialization thread!");
154 if(++deadlockCounter
>= 10) qFatal("Deadlock in initialization thread!");
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
);
173 SET_TASKBAR_STATE(false);
176 splashScreen
->m_canClose
= true;
177 splashScreen
->close();
181 LAMEXP_DELETE(timer
);
182 LAMEXP_DELETE(splashScreen
);
185 ////////////////////////////////////////////////////////////
187 ////////////////////////////////////////////////////////////
189 void SplashScreen::keyPressEvent(QKeyEvent
*event
)
194 void SplashScreen::keyReleaseEvent(QKeyEvent
*event
)
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
);