OSDN Git Service

48461da581206e730ef60670e5c5d87a1679db24
[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         //Start thread
97         QApplication::processEvents();
98         thread->start();
99         QApplication::processEvents();
100
101         //Init taskbar
102         WinSevenTaskbar::setTaskbarState(splashScreen, WinSevenTaskbar::WinSevenTaskbarIndeterminateState);
103
104         //Fade in
105         for(int i = 0; i <= 100; i++)
106         {
107                 opacity = 0.01 * static_cast<double>(i);
108                 splashScreen->setWindowOpacity(opacity);
109                 QApplication::processEvents();
110                 Sleep(FADE_DELAY);
111         }
112
113         //Loop while thread is running
114         while(thread->isRunning())
115         {
116                 QTimer::singleShot(15000, loop, SLOT(quit()));
117                 loop->exec();
118                 if(thread->isRunning()) qWarning("Potential deadlock in Init thread!");
119         }
120         
121         //Fade out
122         for(int i = 100; i >= 0; i--)
123         {
124                 opacity = 0.01 * static_cast<double>(i);
125                 splashScreen->setWindowOpacity(opacity);
126                 QApplication::processEvents();
127                 Sleep(FADE_DELAY);
128         }
129
130         //Restore taskbar
131         WinSevenTaskbar::setTaskbarState(splashScreen, WinSevenTaskbar::WinSevenTaskbarNoState);
132
133         //Hide splash
134         splashScreen->m_canClose = true;
135         splashScreen->close();
136
137         //Free
138         LAMEXP_DELETE(loop);
139         LAMEXP_DELETE(splashScreen);
140 }
141
142 ////////////////////////////////////////////////////////////
143 // EVENTS
144 ////////////////////////////////////////////////////////////
145
146 void SplashScreen::keyPressEvent(QKeyEvent *event)
147 {
148         event->ignore();
149 }
150
151 void SplashScreen::keyReleaseEvent(QKeyEvent *event)
152 {
153         event->ignore();
154 }
155
156 void SplashScreen::closeEvent(QCloseEvent *event)
157 {
158         if(!m_canClose) event->ignore();
159 }
160
161 bool SplashScreen::winEvent(MSG *message, long *result)
162 {
163         return WinSevenTaskbar::handleWinEvent(message, result);
164 }