Updated Ukrainian translation.
[LameXP.git] / src / Dialog_WorkingBanner.cpp
blob25caea1f93a5e114234dfa4a1fd9dc8418f320c1
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2013 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, 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"
25 #include "Global.h"
26 #include "WinSevenTaskbar.h"
28 #include <QThread>
29 #include <QMovie>
30 #include <QKeyEvent>
31 #include <QFontMetrics>
33 #define EPS (1.0E-5)
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 ////////////////////////////////////////////////////////////
41 // Constructor
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
50 setupUi(this);
51 setModal(true);
53 //Start animation
54 m_working = new QMovie(":/images/Busy.gif");
55 m_working->setSpeed(50);
56 labelWorking->setMovie(m_working);
57 m_working->start();
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());
65 //Set font size
66 QFont font = m_progress->font();
67 font.setPointSize(6);
68 m_progress->setFont(font);
70 //Set font color
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);
76 //Set Opacity
77 this->setWindowOpacity(0.9);
79 //Set wait cursor
80 setCursor(Qt::WaitCursor);
83 ////////////////////////////////////////////////////////////
84 // Destructor
85 ////////////////////////////////////////////////////////////
87 WorkingBanner::~WorkingBanner(void)
89 if(m_working)
91 m_working->stop();
92 delete m_working;
93 m_working = NULL;
96 LAMEXP_DELETE(m_progress);
97 LAMEXP_DELETE(m_metrics);
100 ////////////////////////////////////////////////////////////
101 // PUBLIC FUNCTIONS
102 ////////////////////////////////////////////////////////////
104 void WorkingBanner::show(const QString &text)
106 m_canClose = false;
107 m_progressInt = -1;
109 QDialog::show();
110 setFixedSize(size());
111 setText(text);
113 m_progress->setText(QString());
115 QApplication::processEvents();
118 bool WorkingBanner::close(void)
120 m_canClose = true;
121 emit userAbort();
122 return QDialog::close();
125 void WorkingBanner::show(const QString &text, QThread *thread)
127 //Show splash
128 this->show(text);
130 //Create event loop
131 QEventLoop *loop = new QEventLoop(this);
132 connect(thread, SIGNAL(finished()), loop, SLOT(quit()));
133 connect(thread, SIGNAL(terminated()), loop, SLOT(quit()));
135 //Set taskbar state
136 WinSevenTaskbar::setOverlayIcon(dynamic_cast<QWidget*>(this->parent()), &QIcon(":/icons/hourglass.png"));
137 WinSevenTaskbar::setTaskbarState(dynamic_cast<QWidget*>(this->parent()), WinSevenTaskbar::WinSevenTaskbarIndeterminateState);
139 //Start the thread
140 thread->start();
142 //Loop while thread is still running
143 while(THREAD_RUNNING(thread))
145 loop->exec();
148 //Set taskbar state
149 WinSevenTaskbar::setTaskbarState(dynamic_cast<QWidget*>(this->parent()), WinSevenTaskbar::WinSevenTaskbarNoState);
150 WinSevenTaskbar::setOverlayIcon(dynamic_cast<QWidget*>(this->parent()), NULL);
152 //Free memory
153 LAMEXP_DELETE(loop);
155 //Hide splash
156 this->close();
159 void WorkingBanner::show(const QString &text, QEventLoop *loop)
161 //Show splash
162 this->show(text);
164 //Set taskbar state
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);
171 //Set taskbar state
172 WinSevenTaskbar::setTaskbarState(dynamic_cast<QWidget*>(this->parent()), WinSevenTaskbar::WinSevenTaskbarNoState);
173 WinSevenTaskbar::setOverlayIcon(dynamic_cast<QWidget*>(this->parent()), NULL);
175 //Hide splash
176 this->close();
179 ////////////////////////////////////////////////////////////
180 // EVENTS
181 ////////////////////////////////////////////////////////////
183 void WorkingBanner::keyPressEvent(QKeyEvent *event)
185 if(event->key() == Qt::Key_Escape)
187 qDebug("QT::KEY_ESCAPE pressed!");
188 emit userAbort();
191 event->ignore();
194 void WorkingBanner::keyReleaseEvent(QKeyEvent *event)
196 event->ignore();
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 ////////////////////////////////////////////////////////////
210 // SLOTS
211 ////////////////////////////////////////////////////////////
213 void WorkingBanner::setText(const QString &text)
215 if(!m_metrics)
217 m_metrics = new QFontMetrics(labelStatus->font());
220 if(m_metrics->width(text) <= labelStatus->width())
222 labelStatus->setText(text);
224 else
226 QString choppedText = text.simplified().append("...");
227 while((m_metrics->width(choppedText) > labelStatus->width()) && (choppedText.length() > 8))
229 choppedText.chop(4);
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)
243 m_progressMax = max;
244 updateProgress();
247 void WorkingBanner::setProgressVal(unsigned int val)
249 m_progressVal = val;
250 updateProgress();
253 ////////////////////////////////////////////////////////////
254 // Private
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();