OSDN Git Service

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