Some code refactoring regarding the QWaitCondition/QMutex in FileAnalyzer_Task.
[LameXP.git] / src / Dialog_WorkingBanner.cpp
blobf854788ed7165ed63d02ef25bca3e4ceea107f21
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_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 QDialog::show();
104 setFixedSize(size());
105 setText(text);
107 m_progress->setText(QString());
109 QApplication::processEvents();
112 bool WorkingBanner::close(void)
114 m_canClose = true;
115 emit userAbort();
116 return QDialog::close();
119 void WorkingBanner::show(const QString &text, QThread *thread)
121 //Show splash
122 this->show(text);
124 //Create event loop
125 QEventLoop *loop = new QEventLoop(this);
126 connect(thread, SIGNAL(finished()), loop, SLOT(quit()));
127 connect(thread, SIGNAL(terminated()), loop, SLOT(quit()));
129 //Set taskbar state
130 WinSevenTaskbar::setOverlayIcon(dynamic_cast<QWidget*>(this->parent()), &QIcon(":/icons/hourglass.png"));
131 WinSevenTaskbar::setTaskbarState(dynamic_cast<QWidget*>(this->parent()), WinSevenTaskbar::WinSevenTaskbarIndeterminateState);
133 //Start the thread
134 thread->start();
136 //Loop while thread is still running
137 while(THREAD_RUNNING(thread))
139 loop->exec();
142 //Set taskbar state
143 WinSevenTaskbar::setTaskbarState(dynamic_cast<QWidget*>(this->parent()), WinSevenTaskbar::WinSevenTaskbarNoState);
144 WinSevenTaskbar::setOverlayIcon(dynamic_cast<QWidget*>(this->parent()), NULL);
146 //Free memory
147 LAMEXP_DELETE(loop);
149 //Hide splash
150 this->close();
153 void WorkingBanner::show(const QString &text, QEventLoop *loop)
155 //Show splash
156 this->show(text);
158 //Set taskbar state
159 WinSevenTaskbar::setOverlayIcon(dynamic_cast<QWidget*>(this->parent()), &QIcon(":/icons/hourglass.png"));
160 WinSevenTaskbar::setTaskbarState(dynamic_cast<QWidget*>(this->parent()), WinSevenTaskbar::WinSevenTaskbarIndeterminateState);
162 //Loop while thread is running
163 loop->exec(QEventLoop::ExcludeUserInputEvents);
165 //Set taskbar state
166 WinSevenTaskbar::setTaskbarState(dynamic_cast<QWidget*>(this->parent()), WinSevenTaskbar::WinSevenTaskbarNoState);
167 WinSevenTaskbar::setOverlayIcon(dynamic_cast<QWidget*>(this->parent()), NULL);
169 //Hide splash
170 this->close();
173 ////////////////////////////////////////////////////////////
174 // EVENTS
175 ////////////////////////////////////////////////////////////
177 void WorkingBanner::keyPressEvent(QKeyEvent *event)
179 if(event->key() == Qt::Key_Escape)
181 qDebug("QT::KEY_ESCAPE pressed!");
182 emit userAbort();
185 event->ignore();
188 void WorkingBanner::keyReleaseEvent(QKeyEvent *event)
190 event->ignore();
193 void WorkingBanner::closeEvent(QCloseEvent *event)
195 if(!m_canClose) event->ignore();
198 bool WorkingBanner::winEvent(MSG *message, long *result)
200 return WinSevenTaskbar::handleWinEvent(message, result);
203 ////////////////////////////////////////////////////////////
204 // SLOTS
205 ////////////////////////////////////////////////////////////
207 void WorkingBanner::setText(const QString &text)
209 if(!m_metrics)
211 m_metrics = new QFontMetrics(labelStatus->font());
214 if(m_metrics->width(text) <= labelStatus->width())
216 labelStatus->setText(text);
218 else
220 QString choppedText = text.simplified().append("...");
221 while((m_metrics->width(choppedText) > labelStatus->width()) && (choppedText.length() > 8))
223 choppedText.chop(4);
224 choppedText = choppedText.trimmed();
225 choppedText.append("...");
227 labelStatus->setText(choppedText);
230 if(this->isVisible())
232 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
237 void WorkingBanner::setProgressMax(unsigned int max)
239 m_progressMax = max;
242 void WorkingBanner::setProgressVal(unsigned int val)
244 m_progressVal = val;
245 if(m_progressMax > 0)
247 int progress = qRound(qBound(0.0, static_cast<double>(m_progressVal) / static_cast<double>(m_progressMax), 1.0) * 100.0);
248 m_progress->setText(QString::number(progress));