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)
47 //Init the dialog, from the .ui file
52 m_working = new QMovie(":/images/Busy.gif");
53 m_working->setSpeed(50);
54 labelWorking->setMovie(m_working);
58 setCursor(Qt::WaitCursor);
61 ////////////////////////////////////////////////////////////
63 ////////////////////////////////////////////////////////////
65 WorkingBanner::~WorkingBanner(void)
75 ////////////////////////////////////////////////////////////
77 ////////////////////////////////////////////////////////////
79 void WorkingBanner::show(const QString &text)
85 QApplication::processEvents();
88 bool WorkingBanner::close(void)
92 return QDialog::close();
95 void WorkingBanner::show(const QString &text, QThread *thread)
101 QEventLoop *loop = new QEventLoop(this);
102 connect(thread, SIGNAL(finished()), loop, SLOT(quit()));
103 connect(thread, SIGNAL(terminated()), loop, SLOT(quit()));
106 WinSevenTaskbar::setOverlayIcon(dynamic_cast<QWidget*>(this->parent()), &QIcon(":/icons/hourglass.png"));
107 WinSevenTaskbar::setTaskbarState(dynamic_cast<QWidget*>(this->parent()), WinSevenTaskbar::WinSevenTaskbarIndeterminateState);
112 //Loop while thread is still running
113 while(THREAD_RUNNING(thread))
119 WinSevenTaskbar::setTaskbarState(dynamic_cast<QWidget*>(this->parent()), WinSevenTaskbar::WinSevenTaskbarNoState);
120 WinSevenTaskbar::setOverlayIcon(dynamic_cast<QWidget*>(this->parent()), NULL);
129 void WorkingBanner::show(const QString &text, QEventLoop *loop)
135 WinSevenTaskbar::setOverlayIcon(dynamic_cast<QWidget*>(this->parent()), &QIcon(":/icons/hourglass.png"));
136 WinSevenTaskbar::setTaskbarState(dynamic_cast<QWidget*>(this->parent()), WinSevenTaskbar::WinSevenTaskbarIndeterminateState);
138 //Loop while thread is running
139 loop->exec(QEventLoop::ExcludeUserInputEvents);
142 WinSevenTaskbar::setTaskbarState(dynamic_cast<QWidget*>(this->parent()), WinSevenTaskbar::WinSevenTaskbarNoState);
143 WinSevenTaskbar::setOverlayIcon(dynamic_cast<QWidget*>(this->parent()), NULL);
149 ////////////////////////////////////////////////////////////
151 ////////////////////////////////////////////////////////////
153 void WorkingBanner::keyPressEvent(QKeyEvent *event)
155 if(event->key() == Qt::Key_Escape)
157 qDebug("QT::KEY_ESCAPE pressed!");
164 void WorkingBanner::keyReleaseEvent(QKeyEvent *event)
169 void WorkingBanner::closeEvent(QCloseEvent *event)
171 if(!m_canClose) event->ignore();
174 bool WorkingBanner::winEvent(MSG *message, long *result)
176 return WinSevenTaskbar::handleWinEvent(message, result);
179 ////////////////////////////////////////////////////////////
181 ////////////////////////////////////////////////////////////
183 void WorkingBanner::setText(const QString &text)
185 QFontMetrics metrics(labelStatus->font());
186 if(metrics.width(text) <= labelStatus->width())
188 labelStatus->setText(text);
192 QString choppedText = text.simplified().append("...");
193 while(metrics.width(choppedText) > labelStatus->width() && choppedText.length() > 8)
196 choppedText = choppedText.trimmed();
197 choppedText.append("...");
199 labelStatus->setText(choppedText);
202 if(this->isVisible())
204 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);