1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2012 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
)
104 setFixedSize(size());
107 m_progress
->setText(QString());
109 QApplication::processEvents();
112 bool WorkingBanner::close(void)
116 return QDialog::close();
119 void WorkingBanner::show(const QString
&text
, QThread
*thread
)
125 QEventLoop
*loop
= new QEventLoop(this);
126 connect(thread
, SIGNAL(finished()), loop
, SLOT(quit()));
127 connect(thread
, SIGNAL(terminated()), loop
, SLOT(quit()));
130 WinSevenTaskbar::setOverlayIcon(dynamic_cast<QWidget
*>(this->parent()), &QIcon(":/icons/hourglass.png"));
131 WinSevenTaskbar::setTaskbarState(dynamic_cast<QWidget
*>(this->parent()), WinSevenTaskbar::WinSevenTaskbarIndeterminateState
);
136 //Loop while thread is still running
137 while(THREAD_RUNNING(thread
))
143 WinSevenTaskbar::setTaskbarState(dynamic_cast<QWidget
*>(this->parent()), WinSevenTaskbar::WinSevenTaskbarNoState
);
144 WinSevenTaskbar::setOverlayIcon(dynamic_cast<QWidget
*>(this->parent()), NULL
);
153 void WorkingBanner::show(const QString
&text
, QEventLoop
*loop
)
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
);
166 WinSevenTaskbar::setTaskbarState(dynamic_cast<QWidget
*>(this->parent()), WinSevenTaskbar::WinSevenTaskbarNoState
);
167 WinSevenTaskbar::setOverlayIcon(dynamic_cast<QWidget
*>(this->parent()), NULL
);
173 ////////////////////////////////////////////////////////////
175 ////////////////////////////////////////////////////////////
177 void WorkingBanner::keyPressEvent(QKeyEvent
*event
)
179 if(event
->key() == Qt::Key_Escape
)
181 qDebug("QT::KEY_ESCAPE pressed!");
188 void WorkingBanner::keyReleaseEvent(QKeyEvent
*event
)
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 ////////////////////////////////////////////////////////////
205 ////////////////////////////////////////////////////////////
207 void WorkingBanner::setText(const QString
&text
)
211 m_metrics
= new QFontMetrics(labelStatus
->font());
214 if(m_metrics
->width(text
) <= labelStatus
->width())
216 labelStatus
->setText(text
);
220 QString choppedText
= text
.simplified().append("...");
221 while((m_metrics
->width(choppedText
) > labelStatus
->width()) && (choppedText
.length() > 8))
224 choppedText
= choppedText
.trimmed();
225 choppedText
.append("...");
227 labelStatus
->setText(choppedText
);
229 if(this->isVisible())
231 labelStatus
->repaint();
235 void WorkingBanner::setProgressMax(unsigned int max
)
241 void WorkingBanner::setProgressVal(unsigned int val
)
247 ////////////////////////////////////////////////////////////
249 ////////////////////////////////////////////////////////////
251 void WorkingBanner::updateProgress(void)
253 if(m_progressMax
> 0)
255 int newProgress
= qRound(qBound(0.0, static_cast<double>(m_progressVal
) / static_cast<double>(m_progressMax
), 1.0) * 100.0);
256 if(m_progressInt
!= newProgress
)
258 m_progressInt
= newProgress
;
259 m_progress
->setText(QString::number(m_progressInt
));
260 if(this->isVisible())
262 labelStatus
->repaint();