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, but always including the *additional*
9 // restrictions defined in the "License.txt" file.
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.
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.
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
23 #include "Dialog_WorkingBanner.h"
26 #include "WinSevenTaskbar.h"
31 #include <QFontMetrics>
35 /* 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. */
36 /* 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. */
37 /* 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. */
38 #define THREAD_RUNNING(THRD) (((THRD)->isRunning()) ? (!((THRD)->wait(1))) : false)
40 ////////////////////////////////////////////////////////////
42 ////////////////////////////////////////////////////////////
44 WorkingBanner::WorkingBanner(QWidget
*parent
)
46 QDialog(parent
, Qt::CustomizeWindowHint
| Qt::WindowStaysOnTopHint
),
47 m_progressMax(0), m_progressVal(0), m_progressInt(0), m_metrics(NULL
)
49 //Init the dialog, from the .ui file
54 m_working
= new QMovie(":/images/Busy.gif");
55 m_working
->setSpeed(50);
56 labelWorking
->setMovie(m_working
);
59 //Create progress indicator
60 m_progress
= new QLabel(labelWorking
);
61 m_progress
->setAlignment(Qt::AlignHCenter
| Qt::AlignVCenter
);
62 m_progress
->move(0, 0);
63 m_progress
->resize(labelWorking
->size());
66 QFont font
= m_progress
->font();
68 m_progress
->setFont(font
);
71 QPalette color
= m_progress
->palette();
72 color
.setColor(QPalette::Text
, QColor::fromRgb(0x33, 0x33, 0x33));
73 color
.setColor(QPalette::WindowText
, QColor::fromRgb(0x33, 0x33, 0x33));
74 m_progress
->setPalette(color
);
77 this->setWindowOpacity(0.9);
80 setCursor(Qt::WaitCursor
);
83 ////////////////////////////////////////////////////////////
85 ////////////////////////////////////////////////////////////
87 WorkingBanner::~WorkingBanner(void)
96 LAMEXP_DELETE(m_progress
);
97 LAMEXP_DELETE(m_metrics
);
100 ////////////////////////////////////////////////////////////
102 ////////////////////////////////////////////////////////////
104 void WorkingBanner::show(const QString
&text
)
110 setFixedSize(size());
113 m_progress
->setText(QString());
115 QApplication::processEvents();
118 bool WorkingBanner::close(void)
122 return QDialog::close();
125 void WorkingBanner::show(const QString
&text
, QThread
*thread
)
131 QEventLoop
*loop
= new QEventLoop(this);
132 connect(thread
, SIGNAL(finished()), loop
, SLOT(quit()));
133 connect(thread
, SIGNAL(terminated()), loop
, SLOT(quit()));
136 WinSevenTaskbar::setOverlayIcon(dynamic_cast<QWidget
*>(this->parent()), &QIcon(":/icons/hourglass.png"));
137 WinSevenTaskbar::setTaskbarState(dynamic_cast<QWidget
*>(this->parent()), WinSevenTaskbar::WinSevenTaskbarIndeterminateState
);
142 //Loop while thread is still running
143 while(THREAD_RUNNING(thread
))
149 WinSevenTaskbar::setTaskbarState(dynamic_cast<QWidget
*>(this->parent()), WinSevenTaskbar::WinSevenTaskbarNoState
);
150 WinSevenTaskbar::setOverlayIcon(dynamic_cast<QWidget
*>(this->parent()), NULL
);
159 void WorkingBanner::show(const QString
&text
, QEventLoop
*loop
)
165 WinSevenTaskbar::setOverlayIcon(dynamic_cast<QWidget
*>(this->parent()), &QIcon(":/icons/hourglass.png"));
166 WinSevenTaskbar::setTaskbarState(dynamic_cast<QWidget
*>(this->parent()), WinSevenTaskbar::WinSevenTaskbarIndeterminateState
);
168 //Loop while thread is running
169 loop
->exec(QEventLoop::ExcludeUserInputEvents
);
172 WinSevenTaskbar::setTaskbarState(dynamic_cast<QWidget
*>(this->parent()), WinSevenTaskbar::WinSevenTaskbarNoState
);
173 WinSevenTaskbar::setOverlayIcon(dynamic_cast<QWidget
*>(this->parent()), NULL
);
179 ////////////////////////////////////////////////////////////
181 ////////////////////////////////////////////////////////////
183 void WorkingBanner::keyPressEvent(QKeyEvent
*event
)
185 if(event
->key() == Qt::Key_Escape
)
187 qDebug("QT::KEY_ESCAPE pressed!");
194 void WorkingBanner::keyReleaseEvent(QKeyEvent
*event
)
199 void WorkingBanner::closeEvent(QCloseEvent
*event
)
201 if(!m_canClose
) event
->ignore();
204 bool WorkingBanner::winEvent(MSG
*message
, long *result
)
206 return WinSevenTaskbar::handleWinEvent(message
, result
);
209 ////////////////////////////////////////////////////////////
211 ////////////////////////////////////////////////////////////
213 void WorkingBanner::setText(const QString
&text
)
217 m_metrics
= new QFontMetrics(labelStatus
->font());
220 if(m_metrics
->width(text
) <= labelStatus
->width())
222 labelStatus
->setText(text
);
226 QString choppedText
= text
.simplified().append("...");
227 while((m_metrics
->width(choppedText
) > labelStatus
->width()) && (choppedText
.length() > 8))
230 choppedText
= choppedText
.trimmed();
231 choppedText
.append("...");
233 labelStatus
->setText(choppedText
);
235 if(this->isVisible())
237 labelStatus
->repaint();
241 void WorkingBanner::setProgressMax(unsigned int max
)
247 void WorkingBanner::setProgressVal(unsigned int val
)
253 ////////////////////////////////////////////////////////////
255 ////////////////////////////////////////////////////////////
257 void WorkingBanner::updateProgress(void)
259 if(m_progressMax
> 0)
261 int newProgress
= qRound(qBound(0.0, static_cast<double>(m_progressVal
) / static_cast<double>(m_progressMax
), 1.0) * 100.0);
262 if(m_progressInt
!= newProgress
)
264 m_progressInt
= newProgress
;
265 m_progress
->setText(QString::number(m_progressInt
));
266 if(this->isVisible())
268 labelStatus
->repaint();