Changed the method to synchronize the analyzer threads: We now use QSet to maintain...
[LameXP.git] / src / Dialog_WorkingBanner.cpp
blobc60ede8ba0cc7c79d4d635cb0143e225eb51e1fe
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.
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.
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
22 #include "Dialog_WorkingBanner.h"
24 #include "Global.h"
25 #include "WinSevenTaskbar.h"
27 #include <QThread>
28 #include <QMovie>
29 #include <QKeyEvent>
30 #include <QFontMetrics>
32 #define EPS (1.0E-5)
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)
39 ////////////////////////////////////////////////////////////
40 // Constructor
41 ////////////////////////////////////////////////////////////
43 WorkingBanner::WorkingBanner(QWidget *parent)
45 QDialog(parent, Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint),
46 m_progressMax(0), m_progressVal(0), m_progressInt(0), m_metrics(NULL)
48 //Init the dialog, from the .ui file
49 setupUi(this);
50 setModal(true);
52 //Start animation
53 m_working = new QMovie(":/images/Busy.gif");
54 m_working->setSpeed(50);
55 labelWorking->setMovie(m_working);
56 m_working->start();
58 //Create progress indicator
59 m_progress = new QLabel(labelWorking);
60 m_progress->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
61 m_progress->move(0, 0);
62 m_progress->resize(labelWorking->size());
64 //Set font size
65 QFont font = m_progress->font();
66 font.setPointSize(6);
67 m_progress->setFont(font);
69 //Set font color
70 QPalette color = m_progress->palette();
71 color.setColor(QPalette::Text, QColor::fromRgb(0x33, 0x33, 0x33));
72 color.setColor(QPalette::WindowText, QColor::fromRgb(0x33, 0x33, 0x33));
73 m_progress->setPalette(color);
75 //Set wait cursor
76 setCursor(Qt::WaitCursor);
79 ////////////////////////////////////////////////////////////
80 // Destructor
81 ////////////////////////////////////////////////////////////
83 WorkingBanner::~WorkingBanner(void)
85 if(m_working)
87 m_working->stop();
88 delete m_working;
89 m_working = NULL;
92 LAMEXP_DELETE(m_progress);
93 LAMEXP_DELETE(m_metrics);
96 ////////////////////////////////////////////////////////////
97 // PUBLIC FUNCTIONS
98 ////////////////////////////////////////////////////////////
100 void WorkingBanner::show(const QString &text)
102 m_canClose = false;
103 m_progressInt = -1;
105 QDialog::show();
106 setFixedSize(size());
107 setText(text);
109 m_progress->setText(QString());
111 QApplication::processEvents();
114 bool WorkingBanner::close(void)
116 m_canClose = true;
117 emit userAbort();
118 return QDialog::close();
121 void WorkingBanner::show(const QString &text, QThread *thread)
123 //Show splash
124 this->show(text);
126 //Create event loop
127 QEventLoop *loop = new QEventLoop(this);
128 connect(thread, SIGNAL(finished()), loop, SLOT(quit()));
129 connect(thread, SIGNAL(terminated()), loop, SLOT(quit()));
131 //Set taskbar state
132 WinSevenTaskbar::setOverlayIcon(dynamic_cast<QWidget*>(this->parent()), &QIcon(":/icons/hourglass.png"));
133 WinSevenTaskbar::setTaskbarState(dynamic_cast<QWidget*>(this->parent()), WinSevenTaskbar::WinSevenTaskbarIndeterminateState);
135 //Start the thread
136 thread->start();
138 //Loop while thread is still running
139 while(THREAD_RUNNING(thread))
141 loop->exec();
144 //Set taskbar state
145 WinSevenTaskbar::setTaskbarState(dynamic_cast<QWidget*>(this->parent()), WinSevenTaskbar::WinSevenTaskbarNoState);
146 WinSevenTaskbar::setOverlayIcon(dynamic_cast<QWidget*>(this->parent()), NULL);
148 //Free memory
149 LAMEXP_DELETE(loop);
151 //Hide splash
152 this->close();
155 void WorkingBanner::show(const QString &text, QEventLoop *loop)
157 //Show splash
158 this->show(text);
160 //Set taskbar state
161 WinSevenTaskbar::setOverlayIcon(dynamic_cast<QWidget*>(this->parent()), &QIcon(":/icons/hourglass.png"));
162 WinSevenTaskbar::setTaskbarState(dynamic_cast<QWidget*>(this->parent()), WinSevenTaskbar::WinSevenTaskbarIndeterminateState);
164 //Loop while thread is running
165 loop->exec(QEventLoop::ExcludeUserInputEvents);
167 //Set taskbar state
168 WinSevenTaskbar::setTaskbarState(dynamic_cast<QWidget*>(this->parent()), WinSevenTaskbar::WinSevenTaskbarNoState);
169 WinSevenTaskbar::setOverlayIcon(dynamic_cast<QWidget*>(this->parent()), NULL);
171 //Hide splash
172 this->close();
175 ////////////////////////////////////////////////////////////
176 // EVENTS
177 ////////////////////////////////////////////////////////////
179 void WorkingBanner::keyPressEvent(QKeyEvent *event)
181 if(event->key() == Qt::Key_Escape)
183 qDebug("QT::KEY_ESCAPE pressed!");
184 emit userAbort();
187 event->ignore();
190 void WorkingBanner::keyReleaseEvent(QKeyEvent *event)
192 event->ignore();
195 void WorkingBanner::closeEvent(QCloseEvent *event)
197 if(!m_canClose) event->ignore();
200 bool WorkingBanner::winEvent(MSG *message, long *result)
202 return WinSevenTaskbar::handleWinEvent(message, result);
205 ////////////////////////////////////////////////////////////
206 // SLOTS
207 ////////////////////////////////////////////////////////////
209 void WorkingBanner::setText(const QString &text)
211 if(!m_metrics)
213 m_metrics = new QFontMetrics(labelStatus->font());
216 if(m_metrics->width(text) <= labelStatus->width())
218 labelStatus->setText(text);
220 else
222 QString choppedText = text.simplified().append("...");
223 while((m_metrics->width(choppedText) > labelStatus->width()) && (choppedText.length() > 8))
225 choppedText.chop(4);
226 choppedText = choppedText.trimmed();
227 choppedText.append("...");
229 labelStatus->setText(choppedText);
231 if(this->isVisible())
233 labelStatus->repaint();
237 void WorkingBanner::setProgressMax(unsigned int max)
239 m_progressMax = max;
240 updateProgress();
243 void WorkingBanner::setProgressVal(unsigned int val)
245 m_progressVal = val;
246 updateProgress();
249 ////////////////////////////////////////////////////////////
250 // Private
251 ////////////////////////////////////////////////////////////
253 void WorkingBanner::updateProgress(void)
255 if(m_progressMax > 0)
257 int newProgress = qRound(qBound(0.0, static_cast<double>(m_progressVal) / static_cast<double>(m_progressMax), 1.0) * 100.0);
258 if(m_progressInt != newProgress)
260 m_progressInt = newProgress;
261 m_progress->setText(QString::number(m_progressInt));
262 if(this->isVisible())
264 labelStatus->repaint();