Updated Ukrainian translation.
[LameXP.git] / src / Dialog_SplashScreen.cpp
blob60daea9364e1ffa2a0eac586e2999382fe722a87
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.
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)
42 ////////////////////////////////////////////////////////////
43 // Constructor
44 ////////////////////////////////////////////////////////////
46 SplashScreen::SplashScreen(QWidget *parent)
48 QFrame(parent, Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint)
50 //Init the dialog, from the .ui file
51 setupUi(this);
53 //Start animation
54 m_working = new QMovie(":/images/Loading.gif");
55 labelLoading->setMovie(m_working);
56 m_working->start();
58 //Set wait cursor
59 setCursor(Qt::WaitCursor);
61 //Prevent close
62 m_canClose = false;
65 ////////////////////////////////////////////////////////////
66 // Destructor
67 ////////////////////////////////////////////////////////////
69 SplashScreen::~SplashScreen(void)
71 if(m_working)
73 m_working->stop();
74 delete m_working;
75 m_working = NULL;
79 ////////////////////////////////////////////////////////////
80 // PUBLIC FUNCTIONS
81 ////////////////////////////////////////////////////////////
83 void SplashScreen::showSplash(QThread *thread)
85 double opacity = OPACITY_DELTA;
86 const int opacitySteps = qRound(1.0 / OPACITY_DELTA);
87 SplashScreen *splashScreen = new SplashScreen();
89 //Show splash
90 splashScreen->m_canClose = false;
91 splashScreen->setWindowOpacity(opacity);
92 splashScreen->setFixedSize(splashScreen->size());
93 splashScreen->show();
95 //Wait for window to show
96 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
97 splashScreen->repaint();
98 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
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);
105 //Create timer
106 QTimer *timer = new QTimer();
107 connect(timer, SIGNAL(timeout()), loop, SLOT(quit()));
109 //Start thread
110 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
111 thread->start();
112 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
114 //Init taskbar
115 WinSevenTaskbar::setTaskbarState(splashScreen, WinSevenTaskbar::WinSevenTaskbarIndeterminateState);
117 //Fade in
118 for(int i = 1; i <= opacitySteps; i++)
120 opacity = OPACITY_DELTA * static_cast<double>(i);
121 splashScreen->setWindowOpacity(opacity);
122 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents, FADE_DELAY);
123 Sleep(FADE_DELAY);
126 //Start the timer
127 timer->start(30720);
129 //Loop while thread is still running
130 if(bool bIsRunning = THREAD_RUNNING(thread))
132 while(bIsRunning)
134 loop->exec();
135 if(bIsRunning = THREAD_RUNNING(thread))
137 qWarning("Potential deadlock in initialization thread!");
142 //Stop the timer
143 timer->stop();
145 //Fade out
146 for(int i = opacitySteps; i >= 0; i--)
148 opacity = OPACITY_DELTA * static_cast<double>(i);
149 splashScreen->setWindowOpacity(opacity);
150 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents, FADE_DELAY);
151 Sleep(FADE_DELAY);
154 //Restore taskbar
155 WinSevenTaskbar::setTaskbarState(splashScreen, WinSevenTaskbar::WinSevenTaskbarNoState);
157 //Hide splash
158 splashScreen->m_canClose = true;
159 splashScreen->close();
161 //Free
162 LAMEXP_DELETE(loop);
163 LAMEXP_DELETE(timer);
164 LAMEXP_DELETE(splashScreen);
167 ////////////////////////////////////////////////////////////
168 // EVENTS
169 ////////////////////////////////////////////////////////////
171 void SplashScreen::keyPressEvent(QKeyEvent *event)
173 event->ignore();
176 void SplashScreen::keyReleaseEvent(QKeyEvent *event)
178 event->ignore();
181 void SplashScreen::closeEvent(QCloseEvent *event)
183 if(!m_canClose) event->ignore();
186 bool SplashScreen::winEvent(MSG *message, long *result)
188 return WinSevenTaskbar::handleWinEvent(message, result);