OSDN Git Service

2a449b935c12b6ce3945b9b679161850c4ed1ca4
[lamexp/LameXP.git] / src / Dialog_SplashScreen.cpp
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.
14 //
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.
18 //
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
21
22 #include "Dialog_SplashScreen.h"
23
24 #include "Global.h"
25
26 #include <QThread>
27 #include <QMovie>
28 #include <QKeyEvent>
29 #include <QTimer>
30
31 #include "WinSevenTaskbar.h"
32
33 #define EPS (1.0E-5)
34 #define FADE_DELAY 4
35
36 ////////////////////////////////////////////////////////////
37 // Constructor
38 ////////////////////////////////////////////////////////////
39
40 SplashScreen::SplashScreen(QWidget *parent)
41         : QFrame(parent, Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint)
42 {
43         //Init the dialog, from the .ui file
44         setupUi(this);
45
46         //Start animation
47         m_working = new QMovie(":/images/Loading.gif");
48         labelLoading->setMovie(m_working);
49         m_working->start();
50
51         //Set wait cursor
52         setCursor(Qt::WaitCursor);
53
54         //Prevent close
55         m_canClose = false;
56 }
57
58 ////////////////////////////////////////////////////////////
59 // Destructor
60 ////////////////////////////////////////////////////////////
61
62 SplashScreen::~SplashScreen(void)
63 {
64         if(m_working)
65         {
66                 m_working->stop();
67                 delete m_working;
68                 m_working = NULL;
69         }
70 }
71
72 ////////////////////////////////////////////////////////////
73 // PUBLIC FUNCTIONS
74 ////////////////////////////////////////////////////////////
75
76 void SplashScreen::showSplash(QThread *thread)
77 {
78         double opacity = 0.0;
79         SplashScreen *splashScreen = new SplashScreen();
80         
81         //Show splash
82         splashScreen->m_canClose = false;
83         splashScreen->setWindowOpacity(opacity);
84         splashScreen->show();
85
86         //Wait for window to show
87         QApplication::processEvents();
88         Sleep(100);
89         QApplication::processEvents();
90
91         //Setup the event loop
92         QEventLoop *loop = new QEventLoop(splashScreen);
93         connect(thread, SIGNAL(terminated()), loop, SLOT(quit()), Qt::QueuedConnection);
94         connect(thread, SIGNAL(finished()), loop, SLOT(quit()), Qt::QueuedConnection);
95
96         //Create timer
97         QTimer *timer = new QTimer();
98         connect(timer, SIGNAL(timeout()), loop, SLOT(quit()));
99
100         //Start thread
101         QApplication::processEvents();
102         thread->start();
103         QApplication::processEvents();
104
105         //Init taskbar
106         WinSevenTaskbar::setTaskbarState(splashScreen, WinSevenTaskbar::WinSevenTaskbarIndeterminateState);
107
108         //Fade in
109         for(int i = 0; i <= 100; i++)
110         {
111                 opacity = 0.01 * static_cast<double>(i);
112                 splashScreen->setWindowOpacity(opacity);
113                 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents, FADE_DELAY);
114                 Sleep(FADE_DELAY);
115         }
116
117         //Start the timer
118         timer->start(15360);
119
120         //Loop while thread is running
121         while(thread->isRunning())
122         {
123                 loop->exec();
124                 if(thread->isRunning())
125                 {
126                         qWarning("Potential deadlock in initialization thread!");
127                 }
128         }
129         
130         //Stop the timer
131         timer->stop();
132
133         //Fade out
134         for(int i = 100; i >= 0; i--)
135         {
136                 opacity = 0.01 * static_cast<double>(i);
137                 splashScreen->setWindowOpacity(opacity);
138                 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents, FADE_DELAY);
139                 Sleep(FADE_DELAY);
140         }
141
142         //Restore taskbar
143         WinSevenTaskbar::setTaskbarState(splashScreen, WinSevenTaskbar::WinSevenTaskbarNoState);
144
145         //Hide splash
146         splashScreen->m_canClose = true;
147         splashScreen->close();
148
149         //Free
150         LAMEXP_DELETE(loop);
151         LAMEXP_DELETE(timer);
152         LAMEXP_DELETE(splashScreen);
153 }
154
155 ////////////////////////////////////////////////////////////
156 // EVENTS
157 ////////////////////////////////////////////////////////////
158
159 void SplashScreen::keyPressEvent(QKeyEvent *event)
160 {
161         event->ignore();
162 }
163
164 void SplashScreen::keyReleaseEvent(QKeyEvent *event)
165 {
166         event->ignore();
167 }
168
169 void SplashScreen::closeEvent(QCloseEvent *event)
170 {
171         if(!m_canClose) event->ignore();
172 }
173
174 bool SplashScreen::winEvent(MSG *message, long *result)
175 {
176         return WinSevenTaskbar::handleWinEvent(message, result);
177 }