Bump version.
[LameXP.git] / src / Dialog_SplashScreen.cpp
blob9620cc4748bfe17abfb87eb4d6dfa8cd00d69041
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 4
34 #define THREAD_RUNNING(THRD) (((THRD)->isRunning()) ? (!((THRD)->wait(16))) : false)
36 ////////////////////////////////////////////////////////////
37 // Constructor
38 ////////////////////////////////////////////////////////////
40 SplashScreen::SplashScreen(QWidget *parent)
42 QFrame(parent, Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint)
44 //Init the dialog, from the .ui file
45 setupUi(this);
47 //Start animation
48 m_working = new QMovie(":/images/Loading.gif");
49 labelLoading->setMovie(m_working);
50 m_working->start();
52 //Set wait cursor
53 setCursor(Qt::WaitCursor);
55 //Prevent close
56 m_canClose = false;
59 ////////////////////////////////////////////////////////////
60 // Destructor
61 ////////////////////////////////////////////////////////////
63 SplashScreen::~SplashScreen(void)
65 if(m_working)
67 m_working->stop();
68 delete m_working;
69 m_working = NULL;
73 ////////////////////////////////////////////////////////////
74 // PUBLIC FUNCTIONS
75 ////////////////////////////////////////////////////////////
77 void SplashScreen::showSplash(QThread *thread)
79 double opacity = 0.0;
80 SplashScreen *splashScreen = new SplashScreen();
82 //Show splash
83 splashScreen->m_canClose = false;
84 splashScreen->setWindowOpacity(opacity);
85 splashScreen->show();
87 //Wait for window to show
88 QApplication::processEvents();
89 Sleep(100);
90 QApplication::processEvents();
92 //Setup the event loop
93 QEventLoop *loop = new QEventLoop(splashScreen);
94 connect(thread, SIGNAL(terminated()), loop, SLOT(quit()), Qt::QueuedConnection);
95 connect(thread, SIGNAL(finished()), loop, SLOT(quit()), Qt::QueuedConnection);
97 //Create timer
98 QTimer *timer = new QTimer();
99 connect(timer, SIGNAL(timeout()), loop, SLOT(quit()));
101 //Start thread
102 QApplication::processEvents();
103 thread->start();
104 QApplication::processEvents();
106 //Init taskbar
107 WinSevenTaskbar::setTaskbarState(splashScreen, WinSevenTaskbar::WinSevenTaskbarIndeterminateState);
109 //Fade in
110 for(int i = 0; i <= 100; i++)
112 opacity = 0.01 * static_cast<double>(i);
113 splashScreen->setWindowOpacity(opacity);
114 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents, FADE_DELAY);
115 Sleep(FADE_DELAY);
118 //Start the timer
119 timer->start(30720);
121 //Loop while thread is still running
122 if(bool bIsRunning = THREAD_RUNNING(thread))
124 while(bIsRunning)
126 loop->exec();
127 if(bIsRunning = THREAD_RUNNING(thread))
129 qWarning("Potential deadlock in initialization thread!");
134 //Stop the timer
135 timer->stop();
137 //Fade out
138 for(int i = 100; i >= 0; i--)
140 opacity = 0.01 * static_cast<double>(i);
141 splashScreen->setWindowOpacity(opacity);
142 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents, FADE_DELAY);
143 Sleep(FADE_DELAY);
146 //Restore taskbar
147 WinSevenTaskbar::setTaskbarState(splashScreen, WinSevenTaskbar::WinSevenTaskbarNoState);
149 //Hide splash
150 splashScreen->m_canClose = true;
151 splashScreen->close();
153 //Free
154 LAMEXP_DELETE(loop);
155 LAMEXP_DELETE(timer);
156 LAMEXP_DELETE(splashScreen);
159 ////////////////////////////////////////////////////////////
160 // EVENTS
161 ////////////////////////////////////////////////////////////
163 void SplashScreen::keyPressEvent(QKeyEvent *event)
165 event->ignore();
168 void SplashScreen::keyReleaseEvent(QKeyEvent *event)
170 event->ignore();
173 void SplashScreen::closeEvent(QCloseEvent *event)
175 if(!m_canClose) event->ignore();
178 bool SplashScreen::winEvent(MSG *message, long *result)
180 return WinSevenTaskbar::handleWinEvent(message, result);