OSDN Git Service

Implemented a method to disables update signals from the FileList model. This will...
[lamexp/LameXP.git] / src / Dialog_WorkingBanner.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_WorkingBanner.h"
23
24 #include "Global.h"
25 #include "WinSevenTaskbar.h"
26
27 #include <QThread>
28 #include <QMovie>
29 #include <QKeyEvent>
30 #include <QFontMetrics>
31
32 #define EPS (1.0E-5)
33
34 /* It can happen that the QThread has just terminated and already emitted the 'terminated' signal, but did NOT change the 'isRunning' flag to FALSE yet. */
35 /* For this reason the macro will first check the 'isRunning' flag. If (and only if) the flag still returns TRUE, then we will wait() for at most 50 ms. */
36 /* If, after 50 ms, the wait() function returns with FALSE, then the thread probably is still running and we return TRUE. Otherwise we can return FALSE. */
37 #define THREAD_RUNNING(THRD) (((THRD)->isRunning()) ? (!((THRD)->wait(50))) : false)
38
39 ////////////////////////////////////////////////////////////
40 // Constructor
41 ////////////////////////////////////////////////////////////
42
43 WorkingBanner::WorkingBanner(QWidget *parent)
44 :
45         QDialog(parent, Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint)
46 {
47         //Init the dialog, from the .ui file
48         setupUi(this);
49         setModal(true);
50
51         //Start animation
52         m_working = new QMovie(":/images/Busy.gif");
53         m_working->setSpeed(50);
54         labelWorking->setMovie(m_working);
55         m_working->start();
56
57         //Set wait cursor
58         setCursor(Qt::WaitCursor);
59 }
60
61 ////////////////////////////////////////////////////////////
62 // Destructor
63 ////////////////////////////////////////////////////////////
64
65 WorkingBanner::~WorkingBanner(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 WorkingBanner::show(const QString &text)
80 {
81         m_canClose = false;
82         QDialog::show();
83         setFixedSize(size());
84         setText(text);
85         QApplication::processEvents();
86 }
87
88 bool WorkingBanner::close(void)
89 {
90         m_canClose = true;
91         emit userAbort();
92         return QDialog::close();
93 }
94
95 void WorkingBanner::show(const QString &text, QThread *thread)
96 {
97         //Show splash
98         this->show(text);
99
100         //Create event loop
101         QEventLoop *loop = new QEventLoop(this);
102         connect(thread, SIGNAL(finished()), loop, SLOT(quit()));
103         connect(thread, SIGNAL(terminated()), loop, SLOT(quit()));
104
105         //Set taskbar state
106         WinSevenTaskbar::setOverlayIcon(dynamic_cast<QWidget*>(this->parent()), &QIcon(":/icons/hourglass.png"));
107         WinSevenTaskbar::setTaskbarState(dynamic_cast<QWidget*>(this->parent()), WinSevenTaskbar::WinSevenTaskbarIndeterminateState);
108
109         //Start the thread
110         thread->start();
111
112         //Loop while thread is still running
113         while(THREAD_RUNNING(thread))
114         {
115                 loop->exec();
116         }
117
118         //Set taskbar state
119         WinSevenTaskbar::setTaskbarState(dynamic_cast<QWidget*>(this->parent()), WinSevenTaskbar::WinSevenTaskbarNoState);
120         WinSevenTaskbar::setOverlayIcon(dynamic_cast<QWidget*>(this->parent()), NULL);
121
122         //Free memory
123         LAMEXP_DELETE(loop);
124
125         //Hide splash
126         this->close();
127 }
128
129 void WorkingBanner::show(const QString &text, QEventLoop *loop)
130 {
131         //Show splash
132         this->show(text);
133
134         //Set taskbar state
135         WinSevenTaskbar::setOverlayIcon(dynamic_cast<QWidget*>(this->parent()), &QIcon(":/icons/hourglass.png"));
136         WinSevenTaskbar::setTaskbarState(dynamic_cast<QWidget*>(this->parent()), WinSevenTaskbar::WinSevenTaskbarIndeterminateState);
137
138         //Loop while thread is running
139         loop->exec(QEventLoop::ExcludeUserInputEvents);
140
141         //Set taskbar state
142         WinSevenTaskbar::setTaskbarState(dynamic_cast<QWidget*>(this->parent()), WinSevenTaskbar::WinSevenTaskbarNoState);
143         WinSevenTaskbar::setOverlayIcon(dynamic_cast<QWidget*>(this->parent()), NULL);
144
145         //Hide splash
146         this->close();
147 }
148
149 ////////////////////////////////////////////////////////////
150 // EVENTS
151 ////////////////////////////////////////////////////////////
152
153 void WorkingBanner::keyPressEvent(QKeyEvent *event)
154 {
155         if(event->key() == Qt::Key_Escape)
156         {
157                 qDebug("QT::KEY_ESCAPE pressed!");
158                 emit userAbort();
159         }
160         
161         event->ignore();
162 }
163
164 void WorkingBanner::keyReleaseEvent(QKeyEvent *event)
165 {
166         event->ignore();
167 }
168
169 void WorkingBanner::closeEvent(QCloseEvent *event)
170 {
171         if(!m_canClose) event->ignore();
172 }
173
174 bool WorkingBanner::winEvent(MSG *message, long *result)
175 {
176         return WinSevenTaskbar::handleWinEvent(message, result);
177 }
178
179 ////////////////////////////////////////////////////////////
180 // SLOTS
181 ////////////////////////////////////////////////////////////
182
183 void WorkingBanner::setText(const QString &text)
184 {
185         QFontMetrics metrics(labelStatus->font());
186         if(metrics.width(text) <= labelStatus->width())
187         {
188                 labelStatus->setText(text);
189         }
190         else
191         {
192                 QString choppedText = text.simplified().append("...");
193                 while(metrics.width(choppedText) > labelStatus->width() && choppedText.length() > 8)
194                 {
195                         choppedText.chop(4);
196                         choppedText = choppedText.trimmed();
197                         choppedText.append("...");
198                 }
199                 labelStatus->setText(choppedText);
200         }
201         /*
202         if(this->isVisible())
203         {
204                 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
205         }
206         */
207 }