OSDN Git Service

Updated Russian translation. Thanks to Иван Митин <bardak@inbox.ru>.
[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->setFixedSize(splashScreen->size());
89         splashScreen->show();
90
91         //Wait for window to show
92         QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
93         splashScreen->repaint();
94         QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
95
96         //Setup the event loop
97         QEventLoop *loop = new QEventLoop(splashScreen);
98         connect(thread, SIGNAL(terminated()), loop, SLOT(quit()), Qt::QueuedConnection);
99         connect(thread, SIGNAL(finished()), loop, SLOT(quit()), Qt::QueuedConnection);
100
101         //Create timer
102         QTimer *timer = new QTimer();
103         connect(timer, SIGNAL(timeout()), loop, SLOT(quit()));
104
105         //Start thread
106         QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
107         thread->start();
108         QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
109
110         //Init taskbar
111         WinSevenTaskbar::setTaskbarState(splashScreen, WinSevenTaskbar::WinSevenTaskbarIndeterminateState);
112
113         //Fade in
114         for(int i = 1; i <= opacitySteps; i++)
115         {
116                 opacity = OPACITY_DELTA * static_cast<double>(i);
117                 splashScreen->setWindowOpacity(opacity);
118                 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents, FADE_DELAY);
119                 Sleep(FADE_DELAY);
120         }
121
122         //Start the timer
123         timer->start(30720);
124
125         //Loop while thread is still running
126         if(bool bIsRunning = THREAD_RUNNING(thread))
127         {
128                 while(bIsRunning)
129                 {
130                         loop->exec();
131                         if(bIsRunning = THREAD_RUNNING(thread))
132                         {
133                                 qWarning("Potential deadlock in initialization thread!");
134                         }
135                 }
136         }
137
138         //Stop the timer
139         timer->stop();
140
141         //Fade out
142         for(int i = opacitySteps; i >= 0; i--)
143         {
144                 opacity = OPACITY_DELTA * static_cast<double>(i);
145                 splashScreen->setWindowOpacity(opacity);
146                 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents, FADE_DELAY);
147                 Sleep(FADE_DELAY);
148         }
149
150         //Restore taskbar
151         WinSevenTaskbar::setTaskbarState(splashScreen, WinSevenTaskbar::WinSevenTaskbarNoState);
152
153         //Hide splash
154         splashScreen->m_canClose = true;
155         splashScreen->close();
156
157         //Free
158         LAMEXP_DELETE(loop);
159         LAMEXP_DELETE(timer);
160         LAMEXP_DELETE(splashScreen);
161 }
162
163 ////////////////////////////////////////////////////////////
164 // EVENTS
165 ////////////////////////////////////////////////////////////
166
167 void SplashScreen::keyPressEvent(QKeyEvent *event)
168 {
169         event->ignore();
170 }
171
172 void SplashScreen::keyReleaseEvent(QKeyEvent *event)
173 {
174         event->ignore();
175 }
176
177 void SplashScreen::closeEvent(QCloseEvent *event)
178 {
179         if(!m_canClose) event->ignore();
180 }
181
182 bool SplashScreen::winEvent(MSG *message, long *result)
183 {
184         return WinSevenTaskbar::handleWinEvent(message, result);
185 }