OSDN Git Service

Tweaked the number of extractor threads. Also added a simple benchmark function ...
[lamexp/LameXP.git] / src / Dialog_SplashScreen.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2013 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, but always including the *additional*
9 // restrictions defined in the "License.txt" file.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License along
17 // with this program; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 //
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
22
23 #include "Dialog_SplashScreen.h"
24
25 #include "Global.h"
26
27 #include <QThread>
28 #include <QMovie>
29 #include <QKeyEvent>
30 #include <QTimer>
31
32 #include "WinSevenTaskbar.h"
33
34 #define FADE_DELAY 16
35 #define OPACITY_DELTA 0.04
36
37 //Setup taskbar indicator
38 #define SET_TASKBAR_STATE(WIDGET,VAR,FLAG) do \
39 { \
40         if(FLAG) \
41         { \
42                 if(!(VAR)) (VAR) = WinSevenTaskbar::setTaskbarState((WIDGET), WinSevenTaskbar::WinSevenTaskbarIndeterminateState); \
43         } \
44         else \
45         { \
46                 if((VAR)) (VAR) = (!WinSevenTaskbar::setTaskbarState((WIDGET), WinSevenTaskbar::WinSevenTaskbarNoState)); \
47         } \
48 } \
49 while(0)
50
51 ////////////////////////////////////////////////////////////
52 // Constructor
53 ////////////////////////////////////////////////////////////
54
55 SplashScreen::SplashScreen(QWidget *parent)
56 :
57         m_opacitySteps(qRound(1.0 / OPACITY_DELTA)),
58         QFrame(parent, Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint)
59 {
60         //Init the dialog, from the .ui file
61         setupUi(this);
62
63         //Make size fixed
64         setFixedSize(this->size());
65
66         //Create event loop
67         m_loop = new QEventLoop(this);
68
69         //Create timer
70         m_timer = new QTimer(this);
71         m_timer->setInterval(FADE_DELAY);
72         m_timer->setSingleShot(false);
73
74         //Connect timer to slot
75         connect(m_timer, SIGNAL(timeout()), this, SLOT(updateHandler()));
76
77         //Start animation
78         m_working = new QMovie(":/images/Loading.gif");
79         labelLoading->setMovie(m_working);
80         m_working->start();
81
82         //Set wait cursor
83         setCursor(Qt::WaitCursor);
84
85         //Init status
86         m_canClose = false;
87         m_status = STATUS_FADE_IN;
88         m_fadeValue = 0;
89         m_taskBarInit = false;
90 }
91
92 ////////////////////////////////////////////////////////////
93 // Destructor
94 ////////////////////////////////////////////////////////////
95
96 SplashScreen::~SplashScreen(void)
97 {
98         if(m_working)
99         {
100                 m_working->stop();
101         }
102
103         LAMEXP_DELETE(m_working);
104         LAMEXP_DELETE(m_loop);
105         LAMEXP_DELETE(m_timer);
106 }
107
108 ////////////////////////////////////////////////////////////
109 // PUBLIC FUNCTIONS
110 ////////////////////////////////////////////////////////////
111
112 void SplashScreen::showSplash(QThread *thread)
113 {
114         SplashScreen *splashScreen = new SplashScreen();
115         
116         //Show splash
117         splashScreen->setWindowOpacity(OPACITY_DELTA);
118         splashScreen->show();
119
120         //Wait for window to show
121         QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
122         splashScreen->repaint();
123         QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
124
125         //Connect thread signals
126         connect(thread, SIGNAL(terminated()), splashScreen, SLOT(threadComplete()), Qt::QueuedConnection);
127         connect(thread, SIGNAL(finished()),   splashScreen, SLOT(threadComplete()), Qt::QueuedConnection);
128
129         //Init taskbar
130         SET_TASKBAR_STATE(splashScreen, splashScreen->m_taskBarInit, true);
131
132         //Start the thread
133         splashScreen->m_timer->start(FADE_DELAY);
134         QTimer::singleShot(8*60*1000, splashScreen->m_loop, SLOT(quit()));
135         QTimer::singleShot(0, thread, SLOT(start()));
136
137         //Start event handling!
138         const int ret = splashScreen->m_loop->exec(QEventLoop::ExcludeUserInputEvents);
139
140         //Check for timeout
141         if(ret != 42)
142         {
143                 thread->terminate();
144                 qFatal("Deadlock in initialization thread encountered!");
145         }
146
147         //Restore taskbar
148         SET_TASKBAR_STATE(splashScreen, splashScreen->m_taskBarInit, false);
149
150         //Hide splash
151         splashScreen->m_canClose = true;
152         splashScreen->close();
153
154         //Free
155         LAMEXP_DELETE(splashScreen);
156 }
157
158 ////////////////////////////////////////////////////////////
159 // SLOTS
160 ////////////////////////////////////////////////////////////
161
162 void SplashScreen::updateHandler(void)
163 {
164         if(m_status == STATUS_FADE_IN)
165         {
166                 if(m_fadeValue < m_opacitySteps)
167                 {
168                         setWindowOpacity(OPACITY_DELTA * static_cast<double>(++m_fadeValue));
169                         SET_TASKBAR_STATE(this, m_taskBarInit, true);
170                 }
171                 else
172                 {
173                         setWindowOpacity(1.0);
174                         m_timer->stop();
175                         m_status = STATUS_WAIT;
176                 }
177         }
178         else if(m_status == STATUS_FADE_OUT)
179         {
180                 if(m_fadeValue > 0)
181                 {
182                         setWindowOpacity(OPACITY_DELTA * static_cast<double>(--m_fadeValue));
183                         SET_TASKBAR_STATE(this, m_taskBarInit, true);
184                 }
185                 else
186                 {
187                         setWindowOpacity(0.0);
188                         m_timer->stop();
189                         m_status = STATUS_DONE;
190                         m_loop->exit(42);
191                 }
192         }
193 }
194
195 void SplashScreen::threadComplete(void)
196 {
197         m_status = STATUS_FADE_OUT;
198         if(!m_timer->isActive())
199         {
200                 m_timer->start(FADE_DELAY);
201         }
202 }
203
204 ////////////////////////////////////////////////////////////
205 // EVENTS
206 ////////////////////////////////////////////////////////////
207
208 void SplashScreen::keyPressEvent(QKeyEvent *event)
209 {
210         event->ignore();
211 }
212
213 void SplashScreen::keyReleaseEvent(QKeyEvent *event)
214 {
215         event->ignore();
216 }
217
218 void SplashScreen::closeEvent(QCloseEvent *event)
219 {
220         if(!m_canClose) event->ignore();
221 }
222
223 bool SplashScreen::winEvent(MSG *message, long *result)
224 {
225         return WinSevenTaskbar::handleWinEvent(message, result);
226 }