1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2013 LoRd_MuldeR <MuldeR2@GMX.de>
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.
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"
25 #include "WinSevenTaskbar.h"
30 #include <QFontMetrics>
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 ////////////////////////////////////////////////////////////
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
53 m_working
= new QMovie(":/images/Busy.gif");
54 m_working
->setSpeed(50);
55 labelWorking
->setMovie(m_working
);
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());
65 QFont font
= m_progress
->font();
67 m_progress
->setFont(font
);
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
);
76 setCursor(Qt::WaitCursor
);
79 ////////////////////////////////////////////////////////////
81 ////////////////////////////////////////////////////////////
83 WorkingBanner::~WorkingBanner(void)
92 LAMEXP_DELETE(m_progress
);
93 LAMEXP_DELETE(m_metrics
);
96 ////////////////////////////////////////////////////////////
98 ////////////////////////////////////////////////////////////
100 void WorkingBanner::show(const QString
&text
)
106 setFixedSize(size());
109 m_progress
->setText(QString());
111 QApplication::processEvents();
114 bool WorkingBanner::close(void)
118 return QDialog::close();
121 void WorkingBanner::show(const QString
&text
, QThread
*thread
)
127 QEventLoop
*loop
= new QEventLoop(this);
128 connect(thread
, SIGNAL(finished()), loop
, SLOT(quit()));
129 connect(thread
, SIGNAL(terminated()), loop
, SLOT(quit()));
132 WinSevenTaskbar::setOverlayIcon(dynamic_cast<QWidget
*>(this->parent()), &QIcon(":/icons/hourglass.png"));
133 WinSevenTaskbar::setTaskbarState(dynamic_cast<QWidget
*>(this->parent()), WinSevenTaskbar::WinSevenTaskbarIndeterminateState
);
138 //Loop while thread is still running
139 while(THREAD_RUNNING(thread
))
145 WinSevenTaskbar::setTaskbarState(dynamic_cast<QWidget
*>(this->parent()), WinSevenTaskbar::WinSevenTaskbarNoState
);
146 WinSevenTaskbar::setOverlayIcon(dynamic_cast<QWidget
*>(this->parent()), NULL
);
155 void WorkingBanner::show(const QString
&text
, QEventLoop
*loop
)
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
);
168 WinSevenTaskbar::setTaskbarState(dynamic_cast<QWidget
*>(this->parent()), WinSevenTaskbar::WinSevenTaskbarNoState
);
169 WinSevenTaskbar::setOverlayIcon(dynamic_cast<QWidget
*>(this->parent()), NULL
);
175 ////////////////////////////////////////////////////////////
177 ////////////////////////////////////////////////////////////
179 void WorkingBanner::keyPressEvent(QKeyEvent
*event
)
181 if(event
->key() == Qt::Key_Escape
)
183 qDebug("QT::KEY_ESCAPE pressed!");
190 void WorkingBanner::keyReleaseEvent(QKeyEvent
*event
)
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 ////////////////////////////////////////////////////////////
207 ////////////////////////////////////////////////////////////
209 void WorkingBanner::setText(const QString
&text
)
213 m_metrics
= new QFontMetrics(labelStatus
->font());
216 if(m_metrics
->width(text
) <= labelStatus
->width())
218 labelStatus
->setText(text
);
222 QString choppedText
= text
.simplified().append("...");
223 while((m_metrics
->width(choppedText
) > labelStatus
->width()) && (choppedText
.length() > 8))
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
)
243 void WorkingBanner::setProgressVal(unsigned int val
)
249 ////////////////////////////////////////////////////////////
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();