OSDN Git Service

Prevent some more dialogs from blocking a quick system shutdown.
[lamexp/LameXP.git] / src / Dialog_SplashScreen.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2011 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
30 #include "WinSevenTaskbar.h"
31
32 #define EPS (1.0E-5)
33 #define FADE_DELAY 4
34
35 ////////////////////////////////////////////////////////////
36 // Constructor
37 ////////////////////////////////////////////////////////////
38
39 SplashScreen::SplashScreen(QWidget *parent)
40         : QFrame(parent, Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint)
41 {
42         //Init the dialog, from the .ui file
43         setupUi(this);
44
45         //Start animation
46         m_working = new QMovie(":/images/Loading.gif");
47         labelLoading->setMovie(m_working);
48         m_working->start();
49
50         //Set wait cursor
51         setCursor(Qt::WaitCursor);
52
53         //Prevent close
54         m_canClose = false;
55 }
56
57 ////////////////////////////////////////////////////////////
58 // Destructor
59 ////////////////////////////////////////////////////////////
60
61 SplashScreen::~SplashScreen(void)
62 {
63         if(m_working)
64         {
65                 m_working->stop();
66                 delete m_working;
67                 m_working = NULL;
68         }
69 }
70
71 ////////////////////////////////////////////////////////////
72 // PUBLIC FUNCTIONS
73 ////////////////////////////////////////////////////////////
74
75 void SplashScreen::showSplash(QThread *thread)
76 {
77         double opacity = 0.0;
78         SplashScreen *splashScreen = new SplashScreen();
79         
80         //Show splash
81         splashScreen->m_canClose = false;
82         splashScreen->setWindowOpacity(opacity);
83         splashScreen->show();
84
85         //Wait for window to show
86         QApplication::processEvents();
87         Sleep(100);
88         QApplication::processEvents();
89
90         //Setup the event loop
91         QEventLoop *loop = new QEventLoop(splashScreen);
92         connect(thread, SIGNAL(terminated()), loop, SLOT(quit()), Qt::QueuedConnection);
93         connect(thread, SIGNAL(finished()), loop, SLOT(quit()), Qt::QueuedConnection);
94
95         //Start thread
96         QApplication::processEvents();
97         thread->start();
98         QApplication::processEvents();
99
100         //Init taskbar
101         WinSevenTaskbar::setTaskbarState(splashScreen, WinSevenTaskbar::WinSevenTaskbarIndeterminateState);
102
103         //Fade in
104         for(int i = 0; i <= 100; i++)
105         {
106                 opacity = 0.01 * static_cast<double>(i);
107                 splashScreen->setWindowOpacity(opacity);
108                 QApplication::processEvents();
109                 Sleep(FADE_DELAY);
110         }
111
112         //Loop while thread is running
113         loop->exec();
114         
115         //Fade out
116         for(int i = 100; i >= 0; i--)
117         {
118                 opacity = 0.01 * static_cast<double>(i);
119                 splashScreen->setWindowOpacity(opacity);
120                 QApplication::processEvents();
121                 Sleep(FADE_DELAY);
122         }
123
124         //Restore taskbar
125         WinSevenTaskbar::setTaskbarState(splashScreen, WinSevenTaskbar::WinSevenTaskbarNoState);
126
127         //Hide splash
128         splashScreen->m_canClose = true;
129         splashScreen->close();
130
131         //Free
132         LAMEXP_DELETE(loop);
133         LAMEXP_DELETE(splashScreen);
134 }
135
136 ////////////////////////////////////////////////////////////
137 // EVENTS
138 ////////////////////////////////////////////////////////////
139
140 void SplashScreen::keyPressEvent(QKeyEvent *event)
141 {
142         event->ignore();
143 }
144
145 void SplashScreen::keyReleaseEvent(QKeyEvent *event)
146 {
147         event->ignore();
148 }
149
150 void SplashScreen::closeEvent(QCloseEvent *event)
151 {
152         if(!m_canClose && !lamexp_session_ending()) event->ignore();
153 }
154
155 bool SplashScreen::winEvent(MSG *message, long *result)
156 {
157         return WinSevenTaskbar::handleWinEvent(message, result);
158 }