OSDN Git Service

Updated OggEnc2 binaries to v2.87 using libvorbis v1.3.4 and aoTuV v6.03_2014 (2014...
[lamexp/LameXP.git] / src / Dialog_WorkingBanner.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2015 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, but always including the *additional*
9 // restrictions defined in the "License.txt" file.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License along
17 // with this program; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 //
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
22
23 #include "Dialog_WorkingBanner.h"
24 #include "UIC_WorkingBanner.h"
25
26 //Internal
27 #include "Global.h"
28
29 //MUtils
30 #include <MUtils/Global.h>
31 #include <MUtils/GUI.h>
32 #include <MUtils/Taskbar7.h>
33
34 //Qt
35 #include <QThread>
36 #include <QMovie>
37 #include <QKeyEvent>
38 #include <QFontMetrics>
39 #include <QPainter>
40 #include <QWindowsVistaStyle>
41 #include <QTimer>
42
43 #define EPS (1.0E-5)
44
45 /* 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. */
46 /* 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. */
47 /* 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. */
48 #define THREAD_RUNNING(THRD) (((THRD)->isRunning()) ? (!((THRD)->wait(1))) : false)
49
50 /*Update text color*/
51 static inline void SET_TEXT_COLOR(QWidget *control, const QColor &color)
52 {
53         QPalette pal = control->palette();
54         pal.setColor(QPalette::WindowText, color);
55         pal.setColor(QPalette::Text, color);
56         control->setPalette(pal);
57 }
58
59 /*Make widget translucent*/
60 static inline void MAKE_TRANSLUCENT(QWidget *control)
61 {
62         control->setAttribute(Qt::WA_TranslucentBackground);
63         control->setAttribute(Qt::WA_NoSystemBackground);
64 }
65
66 /*Update widget margins*/
67 static inline void UPDATE_MARGINS(QWidget *control, int l = 0, int r = 0, int t = 0, int b = 0)
68 {
69         if(QLayout *layout = control->layout())
70         {
71                 QMargins margins = layout->contentsMargins();
72                 margins.setLeft(margins.left() + l);
73                 margins.setRight(margins.right() + r);
74                 margins.setTop(margins.top() + t);
75                 margins.setBottom(margins.bottom() + b);
76                 layout->setContentsMargins(margins);
77         }
78 }
79
80 ////////////////////////////////////////////////////////////
81 // Constructor
82 ////////////////////////////////////////////////////////////
83
84 WorkingBanner::WorkingBanner(QWidget *parent)
85 :
86         QDialog(parent, Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint),
87         ui(new Ui::WorkingBanner()),
88         m_taskbar(new MUtils::Taskbar7(parent)),
89         m_metrics(NULL), m_working(NULL), m_style(NULL)
90 {
91         //Init the dialog, from the .ui file
92         ui->setupUi(this);
93         setModal(true);
94
95         //Enable the "sheet of glass" effect
96         if(MUtils::GUI::sheet_of_glass(this))
97         {
98                 m_style.reset(new QWindowsVistaStyle());
99                 this->setStyle(m_style.data());
100                 ui->progressBar->setStyle(m_style.data());
101                 ui->labelStatus->setStyle(m_style.data());
102                 ui->labelStatus->setStyleSheet("background-color: #FFFFFF;");
103         }
104         else
105         {
106                 UPDATE_MARGINS(this, 5);
107                 m_working.reset(new QMovie(":/images/Busy.gif"));
108                 m_working->setCacheMode(QMovie::CacheAll);
109                 ui->labelWorking->setMovie(m_working.data());
110         }
111         
112         //Set Opacity
113         this->setWindowOpacity(0.9);
114
115         //Set wait cursor
116         setCursor(Qt::WaitCursor);
117
118         //Clear label
119         ui->labelStatus->clear();
120 }
121
122 ////////////////////////////////////////////////////////////
123 // Destructor
124 ////////////////////////////////////////////////////////////
125
126 WorkingBanner::~WorkingBanner(void)
127 {
128         if(!m_working.isNull())
129         {
130                 m_working->stop();
131         }
132
133         delete ui;
134 }
135
136 ////////////////////////////////////////////////////////////
137 // PUBLIC FUNCTIONS
138 ////////////////////////////////////////////////////////////
139
140 void WorkingBanner::show(const QString &text)
141 {
142         m_canClose = false;
143
144         QDialog::show();
145         setFixedSize(size());
146         setText(text);
147
148         //Reset progress
149         ui->progressBar->setMinimum(0);
150         ui->progressBar->setMaximum(0);
151         ui->progressBar->setValue(-1);
152 }
153
154 void WorkingBanner::show(const QString &text, QThread *thread)
155 {
156         //Show splash
157         this->show(text);
158
159         //Create event loop
160         QEventLoop *loop = new QEventLoop(this);
161         connect(thread, SIGNAL(finished()), loop, SLOT(quit()));
162         connect(thread, SIGNAL(terminated()), loop, SLOT(quit()));
163
164         //Set taskbar state
165         m_taskbar->setOverlayIcon(&QIcon(":/icons/hourglass.png"));
166         m_taskbar->setTaskbarState(MUtils::Taskbar7::TASKBAR_STATE_INTERMEDIATE);
167
168         //Start the thread
169         thread->start();
170
171         //Update cursor
172         QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
173
174         //Loop while thread is still running
175         while(THREAD_RUNNING(thread))
176         {
177                 loop->exec();
178         }
179
180         //Restore cursor
181         QApplication::restoreOverrideCursor();
182
183         //Set taskbar state
184         m_taskbar->setTaskbarState(MUtils::Taskbar7::TASKBAR_STATE_NONE);
185         m_taskbar->setOverlayIcon(NULL);
186
187         //Free memory
188         MUTILS_DELETE(loop);
189
190         //Hide splash
191         this->close();
192 }
193
194 void WorkingBanner::show(const QString &text, QEventLoop *loop)
195 {
196         //Show splash
197         this->show(text);
198
199         //Set taskbar state
200         m_taskbar->setOverlayIcon(&QIcon(":/icons/hourglass.png"));
201         m_taskbar->setTaskbarState(MUtils::Taskbar7::TASKBAR_STATE_INTERMEDIATE);
202
203         //Update cursor
204         QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
205
206         //Loop while thread is running
207         loop->exec(QEventLoop::ExcludeUserInputEvents);
208
209         //Restore cursor
210         QApplication::restoreOverrideCursor();
211
212         //Set taskbar state
213         m_taskbar->setTaskbarState(MUtils::Taskbar7::TASKBAR_STATE_NONE);
214         m_taskbar->setOverlayIcon(NULL);
215
216         //Hide splash
217         this->close();
218 }
219
220 bool WorkingBanner::close(void)
221 {
222         m_canClose = true;
223         emit userAbort();
224         return QDialog::close();
225 }
226
227 ////////////////////////////////////////////////////////////
228 // EVENTS
229 ////////////////////////////////////////////////////////////
230
231 void WorkingBanner::keyPressEvent(QKeyEvent *event)
232 {
233         if(event->key() == Qt::Key_Escape)
234         {
235                 qDebug("QT::KEY_ESCAPE pressed!");
236                 emit userAbort();
237         }
238         else if(event->key() == Qt::Key_M)
239         {
240                 QTimer::singleShot(0, parent(), SLOT(showMinimized()));
241         }
242         
243         QDialog::keyPressEvent(event);
244 }
245
246 void WorkingBanner::keyReleaseEvent(QKeyEvent *event)
247 {
248         QDialog::keyReleaseEvent(event);
249 }
250
251 void WorkingBanner::closeEvent(QCloseEvent *event)
252 {
253         if(!m_canClose) event->ignore();
254 }
255
256 void WorkingBanner::showEvent(QShowEvent *event)
257 {
258         QDialog::showEvent(event);
259         if(!event->spontaneous())
260         {
261                 if(m_working)
262                 {
263                         m_working->start();
264                 }
265         }
266         QTimer::singleShot(25, this, SLOT(windowShown()));
267 }
268
269 void WorkingBanner::hideEvent(QHideEvent *event)
270 {
271         QDialog::hideEvent(event);
272         if(!event->spontaneous())
273         {
274                 if(m_working)
275                 {
276                         m_working->stop();
277                 }
278         }
279 }
280
281 ////////////////////////////////////////////////////////////
282 // SLOTS
283 ////////////////////////////////////////////////////////////
284
285 void WorkingBanner::windowShown(void)
286 {
287         MUtils::GUI::bring_to_front(this);
288 }
289
290 void WorkingBanner::setText(const QString &text)
291 {
292         if(m_metrics.isNull())
293         {
294                  m_metrics.reset(new QFontMetrics(ui->labelStatus->font()));
295         }
296
297         if(m_metrics->width(text) <= ui->labelStatus->width() - 16)
298         {
299                 ui->labelStatus->setText(text);
300         }
301         else
302         {
303                 QString choppedText = text.simplified().append("...");
304                 while((m_metrics->width(choppedText) > ui->labelStatus->width() - 16) && (choppedText.length() > 8))
305                 {
306                         choppedText.chop(4);
307                         choppedText = choppedText.trimmed();
308                         choppedText.append("...");
309                 }
310                 ui->labelStatus->setText(choppedText);
311         }
312 }
313
314 void WorkingBanner::setProgressMax(unsigned int max)
315 {
316         ui->progressBar->setMaximum(max);
317         if(ui->progressBar->maximum() > ui->progressBar->minimum())
318         {
319                 m_taskbar->setTaskbarState(MUtils::Taskbar7::TASKBAR_STATE_NONE);
320                 m_taskbar->setTaskbarProgress(ui->progressBar->value(), ui->progressBar->maximum());
321         }
322         else
323         {
324                 m_taskbar->setTaskbarState(MUtils::Taskbar7::TASKBAR_STATE_INTERMEDIATE);
325         }
326 }
327
328 void WorkingBanner::setProgressVal(unsigned int val)
329 {
330         ui->progressBar->setValue(val);
331         if(ui->progressBar->maximum() > ui->progressBar->minimum())
332         {
333                 m_taskbar->setTaskbarProgress(ui->progressBar->value(), ui->progressBar->maximum());
334         }
335 }
336
337 ////////////////////////////////////////////////////////////
338 // Private
339 ////////////////////////////////////////////////////////////
340
341 /*
342 void WorkingBanner::updateProgress(void)
343 {
344         if(m_progressMax > 0)
345         {
346                 int newProgress = qRound(qBound(0.0, static_cast<double>(m_progressVal) / static_cast<double>(m_progressMax), 1.0) * 100.0);
347                 if(m_progressInt != newProgress)
348                 {
349                         m_progressInt = newProgress;
350                         m_progress->setText(QString::number(m_progressInt));
351                         if(this->isVisible())
352                         {
353                                 labelStatus->repaint();
354                         }
355                 }
356         }
357 }
358 */