2010-11-06 23:04:47 +01:00
///////////////////////////////////////////////////////////////////////////////
// LameXP - Audio Encoder Front-End
2012-01-02 00:52:27 +01:00
// Copyright (C) 2004-2012 LoRd_MuldeR <MuldeR2@GMX.de>
2010-11-06 23:04:47 +01:00
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// http://www.gnu.org/licenses/gpl-2.0.txt
///////////////////////////////////////////////////////////////////////////////
# include "Dialog_WorkingBanner.h"
# include "Global.h"
2010-12-05 23:11:03 +01:00
# include "WinSevenTaskbar.h"
2010-11-06 23:04:47 +01:00
# include <QThread>
# include <QMovie>
# include <QKeyEvent>
# include <QFontMetrics>
# define EPS (1.0E-5)
2012-05-04 16:38:05 +02:00
/* 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. */
/* 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. */
/* 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. */
# define THREAD_RUNNING(THRD) (((THRD)->isRunning()) ? (!((THRD)->wait(50))) : false)
2010-11-06 23:04:47 +01:00
////////////////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////////////////
WorkingBanner : : WorkingBanner ( QWidget * parent )
2010-12-05 23:11:03 +01:00
:
2012-05-06 04:57:00 +02:00
QDialog ( parent , Qt : : CustomizeWindowHint | Qt : : WindowStaysOnTopHint ) ,
m_progressMax ( 0 ) , m_progressVal ( 0 )
2010-11-06 23:04:47 +01:00
{
//Init the dialog, from the .ui file
setupUi ( this ) ;
setModal ( true ) ;
//Start animation
m_working = new QMovie ( " :/images/Busy.gif " ) ;
2012-05-05 21:56:14 +02:00
m_working - > setSpeed ( 50 ) ;
2010-11-06 23:04:47 +01:00
labelWorking - > setMovie ( m_working ) ;
m_working - > start ( ) ;
2012-05-06 04:57:00 +02:00
//Create progress indicator
m_progress = new QLabel ( labelWorking ) ;
m_progress - > setAlignment ( Qt : : AlignHCenter | Qt : : AlignVCenter ) ;
m_progress - > move ( 0 , 0 ) ;
m_progress - > resize ( labelWorking - > size ( ) ) ;
//Set font size
QFont font = m_progress - > font ( ) ;
2012-05-06 23:58:18 +02:00
font . setPointSize ( 6 ) ;
2012-05-06 04:57:00 +02:00
m_progress - > setFont ( font ) ;
//Set font color
QPalette color = m_progress - > palette ( ) ;
2012-05-06 23:58:18 +02:00
color . setColor ( QPalette : : Text , QColor : : fromRgb ( 0x33 , 0x33 , 0x33 ) ) ;
color . setColor ( QPalette : : WindowText , QColor : : fromRgb ( 0x33 , 0x33 , 0x33 ) ) ;
2012-05-06 04:57:00 +02:00
m_progress - > setPalette ( color ) ;
2010-11-06 23:04:47 +01:00
//Set wait cursor
setCursor ( Qt : : WaitCursor ) ;
}
////////////////////////////////////////////////////////////
// Destructor
////////////////////////////////////////////////////////////
WorkingBanner : : ~ WorkingBanner ( void )
{
if ( m_working )
{
m_working - > stop ( ) ;
delete m_working ;
m_working = NULL ;
}
2012-05-06 04:57:00 +02:00
LAMEXP_DELETE ( m_progress ) ;
2010-11-06 23:04:47 +01:00
}
////////////////////////////////////////////////////////////
// PUBLIC FUNCTIONS
////////////////////////////////////////////////////////////
void WorkingBanner : : show ( const QString & text )
{
m_canClose = false ;
QDialog : : show ( ) ;
2012-04-11 00:44:40 +02:00
setFixedSize ( size ( ) ) ;
2010-11-06 23:04:47 +01:00
setText ( text ) ;
2012-05-06 04:57:00 +02:00
m_progress - > setText ( QString ( ) ) ;
2010-11-06 23:04:47 +01:00
QApplication : : processEvents ( ) ;
}
2011-12-29 14:42:20 +01:00
bool WorkingBanner : : close ( void )
2010-11-06 23:04:47 +01:00
{
m_canClose = true ;
2011-12-29 14:42:20 +01:00
emit userAbort ( ) ;
return QDialog : : close ( ) ;
2010-11-06 23:04:47 +01:00
}
void WorkingBanner : : show ( const QString & text , QThread * thread )
{
//Show splash
this - > show ( text ) ;
2012-05-04 16:38:05 +02:00
//Create event loop
QEventLoop * loop = new QEventLoop ( this ) ;
connect ( thread , SIGNAL ( finished ( ) ) , loop , SLOT ( quit ( ) ) ) ;
connect ( thread , SIGNAL ( terminated ( ) ) , loop , SLOT ( quit ( ) ) ) ;
2010-11-06 23:04:47 +01:00
2010-12-05 23:11:03 +01:00
//Set taskbar state
WinSevenTaskbar : : setOverlayIcon ( dynamic_cast < QWidget * > ( this - > parent ( ) ) , & QIcon ( " :/icons/hourglass.png " ) ) ;
WinSevenTaskbar : : setTaskbarState ( dynamic_cast < QWidget * > ( this - > parent ( ) ) , WinSevenTaskbar : : WinSevenTaskbarIndeterminateState ) ;
2012-05-04 16:38:05 +02:00
//Start the thread
thread - > start ( ) ;
//Loop while thread is still running
while ( THREAD_RUNNING ( thread ) )
2010-11-06 23:04:47 +01:00
{
2012-05-04 16:38:05 +02:00
loop - > exec ( ) ;
2010-11-06 23:04:47 +01:00
}
2010-12-05 23:11:03 +01:00
//Set taskbar state
WinSevenTaskbar : : setTaskbarState ( dynamic_cast < QWidget * > ( this - > parent ( ) ) , WinSevenTaskbar : : WinSevenTaskbarNoState ) ;
WinSevenTaskbar : : setOverlayIcon ( dynamic_cast < QWidget * > ( this - > parent ( ) ) , NULL ) ;
2012-05-04 16:38:05 +02:00
//Free memory
LAMEXP_DELETE ( loop ) ;
2010-11-06 23:04:47 +01:00
//Hide splash
this - > close ( ) ;
}
2010-12-12 01:49:07 +01:00
void WorkingBanner : : show ( const QString & text , QEventLoop * loop )
{
//Show splash
this - > show ( text ) ;
//Set taskbar state
WinSevenTaskbar : : setOverlayIcon ( dynamic_cast < QWidget * > ( this - > parent ( ) ) , & QIcon ( " :/icons/hourglass.png " ) ) ;
WinSevenTaskbar : : setTaskbarState ( dynamic_cast < QWidget * > ( this - > parent ( ) ) , WinSevenTaskbar : : WinSevenTaskbarIndeterminateState ) ;
//Loop while thread is running
loop - > exec ( QEventLoop : : ExcludeUserInputEvents ) ;
//Set taskbar state
WinSevenTaskbar : : setTaskbarState ( dynamic_cast < QWidget * > ( this - > parent ( ) ) , WinSevenTaskbar : : WinSevenTaskbarNoState ) ;
WinSevenTaskbar : : setOverlayIcon ( dynamic_cast < QWidget * > ( this - > parent ( ) ) , NULL ) ;
//Hide splash
this - > close ( ) ;
}
2010-11-06 23:04:47 +01:00
////////////////////////////////////////////////////////////
// EVENTS
////////////////////////////////////////////////////////////
void WorkingBanner : : keyPressEvent ( QKeyEvent * event )
{
2011-05-16 18:05:50 +02:00
if ( event - > key ( ) = = Qt : : Key_Escape )
{
2011-05-16 21:02:24 +02:00
qDebug ( " QT::KEY_ESCAPE pressed! " ) ;
2011-05-16 18:05:50 +02:00
emit userAbort ( ) ;
}
2010-11-06 23:04:47 +01:00
event - > ignore ( ) ;
}
void WorkingBanner : : keyReleaseEvent ( QKeyEvent * event )
{
event - > ignore ( ) ;
}
void WorkingBanner : : closeEvent ( QCloseEvent * event )
{
2011-12-29 14:42:20 +01:00
if ( ! m_canClose ) event - > ignore ( ) ;
2010-11-06 23:04:47 +01:00
}
2011-11-07 17:13:41 +01:00
bool WorkingBanner : : winEvent ( MSG * message , long * result )
{
return WinSevenTaskbar : : handleWinEvent ( message , result ) ;
}
2010-11-06 23:04:47 +01:00
////////////////////////////////////////////////////////////
// SLOTS
////////////////////////////////////////////////////////////
void WorkingBanner : : setText ( const QString & text )
{
QFontMetrics metrics ( labelStatus - > font ( ) ) ;
if ( metrics . width ( text ) < = labelStatus - > width ( ) )
{
labelStatus - > setText ( text ) ;
}
else
{
QString choppedText = text . simplified ( ) . append ( " ... " ) ;
while ( metrics . width ( choppedText ) > labelStatus - > width ( ) & & choppedText . length ( ) > 8 )
{
choppedText . chop ( 4 ) ;
choppedText = choppedText . trimmed ( ) ;
choppedText . append ( " ... " ) ;
}
labelStatus - > setText ( choppedText ) ;
}
2012-05-04 16:38:05 +02:00
/*
2010-11-17 00:49:38 +01:00
if ( this - > isVisible ( ) )
{
QApplication : : processEvents ( QEventLoop : : ExcludeUserInputEvents ) ;
}
2012-05-04 16:38:05 +02:00
*/
2010-11-06 23:04:47 +01:00
}
2012-05-06 04:57:00 +02:00
void WorkingBanner : : setProgressMax ( unsigned int max )
{
m_progressMax = max ;
}
void WorkingBanner : : setProgressVal ( unsigned int val )
{
m_progressVal = val ;
if ( m_progressMax > 0 )
{
int progress = qRound ( qBound ( 0.0 , static_cast < double > ( m_progressVal ) / static_cast < double > ( m_progressMax ) , 1.0 ) * 100.0 ) ;
m_progress - > setText ( QString : : number ( progress ) ) ;
}
}