2010-11-06 23:04:47 +01:00
///////////////////////////////////////////////////////////////////////////////
// LameXP - Audio Encoder Front-End
2023-03-07 20:12:29 +01:00
// Copyright (C) 2004-2023 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
2020-03-28 15:31:01 +01:00
// it under the terms of the GNU GENERAL PUBLIC LICENSE as published by
2010-11-06 23:04:47 +01:00
// the Free Software Foundation; either version 2 of the License, or
2020-03-28 15:31:01 +01:00
// (at your option) any later version; always including the non-optional
// LAMEXP GNU GENERAL PUBLIC LICENSE ADDENDUM. See "License.txt" file!
2010-11-06 23:04:47 +01:00
//
// 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"
2014-11-24 18:28:53 +01:00
# include "UIC_WorkingBanner.h"
2010-11-06 23:04:47 +01:00
2014-11-25 02:14:42 +01:00
//Internal
2010-11-06 23:04:47 +01:00
# include "Global.h"
2014-11-25 02:14:42 +01:00
//MUtils
# include <MUtils/Global.h>
2014-12-08 22:06:01 +01:00
# include <MUtils/GUI.h>
2014-12-18 22:49:37 +01:00
# include <MUtils/Taskbar7.h>
2014-11-25 02:14:42 +01:00
//Qt
2010-11-06 23:04:47 +01:00
# include <QThread>
# include <QMovie>
# include <QKeyEvent>
# include <QFontMetrics>
2013-11-24 17:55:35 +01:00
# include <QPainter>
2013-11-26 23:01:37 +01:00
# include <QWindowsVistaStyle>
# include <QTimer>
2010-11-06 23:04:47 +01:00
# 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. */
2013-10-25 00:48:18 +02:00
# define THREAD_RUNNING(THRD) (((THRD)->isRunning()) ? (!((THRD)->wait(1))) : false)
2012-05-04 16:38:05 +02:00
2013-11-24 17:55:35 +01:00
/*Update text color*/
static inline void SET_TEXT_COLOR ( QWidget * control , const QColor & color )
{
QPalette pal = control - > palette ( ) ;
pal . setColor ( QPalette : : WindowText , color ) ;
pal . setColor ( QPalette : : Text , color ) ;
control - > setPalette ( pal ) ;
}
/*Make widget translucent*/
static inline void MAKE_TRANSLUCENT ( QWidget * control )
{
control - > setAttribute ( Qt : : WA_TranslucentBackground ) ;
control - > setAttribute ( Qt : : WA_NoSystemBackground ) ;
}
/*Update widget margins*/
static inline void UPDATE_MARGINS ( QWidget * control , int l = 0 , int r = 0 , int t = 0 , int b = 0 )
{
if ( QLayout * layout = control - > layout ( ) )
{
QMargins margins = layout - > contentsMargins ( ) ;
margins . setLeft ( margins . left ( ) + l ) ;
margins . setRight ( margins . right ( ) + r ) ;
margins . setTop ( margins . top ( ) + t ) ;
margins . setBottom ( margins . bottom ( ) + b ) ;
layout - > setContentsMargins ( margins ) ;
}
}
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 ) ,
2014-12-18 22:49:37 +01:00
ui ( new Ui : : WorkingBanner ( ) ) ,
2021-12-23 19:14:25 +01:00
m_iconHourglass ( new QIcon ( " :/icons/hourglass.png " ) ) ,
2014-12-18 22:49:37 +01:00
m_taskbar ( new MUtils : : Taskbar7 ( parent ) ) ,
m_metrics ( NULL ) , m_working ( NULL ) , m_style ( NULL )
2010-11-06 23:04:47 +01:00
{
//Init the dialog, from the .ui file
2013-11-25 13:58:21 +01:00
ui - > setupUi ( this ) ;
2010-11-06 23:04:47 +01:00
setModal ( true ) ;
2013-11-24 17:55:35 +01:00
//Enable the "sheet of glass" effect
2014-12-08 22:06:01 +01:00
if ( MUtils : : GUI : : sheet_of_glass ( this ) )
2013-11-24 17:55:35 +01:00
{
2014-12-18 22:49:37 +01:00
m_style . reset ( new QWindowsVistaStyle ( ) ) ;
this - > setStyle ( m_style . data ( ) ) ;
ui - > progressBar - > setStyle ( m_style . data ( ) ) ;
ui - > labelStatus - > setStyle ( m_style . data ( ) ) ;
2013-12-03 22:19:11 +01:00
ui - > labelStatus - > setStyleSheet ( " background-color: #FFFFFF; " ) ;
2013-11-24 17:55:35 +01:00
}
else
{
UPDATE_MARGINS ( this , 5 ) ;
2014-12-18 22:49:37 +01:00
m_working . reset ( new QMovie ( " :/images/Busy.gif " ) ) ;
2013-11-24 17:55:35 +01:00
m_working - > setCacheMode ( QMovie : : CacheAll ) ;
2014-12-18 22:49:37 +01:00
ui - > labelWorking - > setMovie ( m_working . data ( ) ) ;
2013-11-24 17:55:35 +01:00
}
2013-11-26 23:01:37 +01:00
2013-10-25 00:48:18 +02:00
//Set Opacity
2013-11-01 00:08:40 +01:00
this - > setWindowOpacity ( 0.9 ) ;
2013-10-25 00:48:18 +02:00
2010-11-06 23:04:47 +01:00
//Set wait cursor
setCursor ( Qt : : WaitCursor ) ;
2013-11-24 17:55:35 +01:00
//Clear label
2013-11-25 13:58:21 +01:00
ui - > labelStatus - > clear ( ) ;
2010-11-06 23:04:47 +01:00
}
////////////////////////////////////////////////////////////
// Destructor
////////////////////////////////////////////////////////////
WorkingBanner : : ~ WorkingBanner ( void )
{
2014-12-18 22:49:37 +01:00
if ( ! m_working . isNull ( ) )
2010-11-06 23:04:47 +01:00
{
m_working - > stop ( ) ;
}
2012-05-06 04:57:00 +02:00
2013-11-25 13:58:21 +01:00
delete ui ;
2010-11-06 23:04:47 +01:00
}
////////////////////////////////////////////////////////////
// PUBLIC FUNCTIONS
////////////////////////////////////////////////////////////
void WorkingBanner : : show ( const QString & text )
{
m_canClose = false ;
2012-05-14 00:50:16 +02:00
2010-11-06 23:04:47 +01:00
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
2013-11-24 17:55:35 +01:00
//Reset progress
2013-11-25 13:58:21 +01:00
ui - > progressBar - > setMinimum ( 0 ) ;
ui - > progressBar - > setMaximum ( 0 ) ;
ui - > progressBar - > setValue ( - 1 ) ;
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
2021-12-23 19:14:25 +01:00
m_taskbar - > setOverlayIcon ( m_iconHourglass . data ( ) ) ;
2014-12-18 22:49:37 +01:00
m_taskbar - > setTaskbarState ( MUtils : : Taskbar7 : : TASKBAR_STATE_INTERMEDIATE ) ;
2010-12-05 23:11:03 +01:00
2012-05-04 16:38:05 +02:00
//Start the thread
thread - > start ( ) ;
2013-11-24 17:55:35 +01:00
//Update cursor
QApplication : : setOverrideCursor ( QCursor ( Qt : : WaitCursor ) ) ;
2012-05-04 16:38:05 +02:00
//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
}
2013-11-24 17:55:35 +01:00
//Restore cursor
QApplication : : restoreOverrideCursor ( ) ;
2010-12-05 23:11:03 +01:00
//Set taskbar state
2014-12-18 22:49:37 +01:00
m_taskbar - > setTaskbarState ( MUtils : : Taskbar7 : : TASKBAR_STATE_NONE ) ;
m_taskbar - > setOverlayIcon ( NULL ) ;
2010-12-05 23:11:03 +01:00
2012-05-04 16:38:05 +02:00
//Free memory
2014-11-25 02:14:42 +01:00
MUTILS_DELETE ( loop ) ;
2012-05-04 16:38:05 +02:00
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
2021-12-23 19:14:25 +01:00
m_taskbar - > setOverlayIcon ( m_iconHourglass . data ( ) ) ;
2014-12-18 22:49:37 +01:00
m_taskbar - > setTaskbarState ( MUtils : : Taskbar7 : : TASKBAR_STATE_INTERMEDIATE ) ;
2010-12-12 01:49:07 +01:00
2013-11-24 17:55:35 +01:00
//Update cursor
QApplication : : setOverrideCursor ( QCursor ( Qt : : WaitCursor ) ) ;
2010-12-12 01:49:07 +01:00
//Loop while thread is running
loop - > exec ( QEventLoop : : ExcludeUserInputEvents ) ;
2013-11-24 17:55:35 +01:00
//Restore cursor
QApplication : : restoreOverrideCursor ( ) ;
2010-12-12 01:49:07 +01:00
//Set taskbar state
2014-12-18 22:49:37 +01:00
m_taskbar - > setTaskbarState ( MUtils : : Taskbar7 : : TASKBAR_STATE_NONE ) ;
m_taskbar - > setOverlayIcon ( NULL ) ;
2010-12-12 01:49:07 +01:00
//Hide splash
this - > close ( ) ;
}
2013-11-24 17:55:35 +01:00
bool WorkingBanner : : close ( void )
{
m_canClose = true ;
emit userAbort ( ) ;
return QDialog : : 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 ( ) ;
}
2013-11-29 01:20:59 +01:00
else if ( event - > key ( ) = = Qt : : Key_M )
{
QTimer : : singleShot ( 0 , parent ( ) , SLOT ( showMinimized ( ) ) ) ;
}
2011-05-16 18:05:50 +02:00
2013-11-30 13:10:34 +01:00
QDialog : : keyPressEvent ( event ) ;
2010-11-06 23:04:47 +01:00
}
void WorkingBanner : : keyReleaseEvent ( QKeyEvent * event )
{
2013-11-30 13:10:34 +01:00
QDialog : : keyReleaseEvent ( event ) ;
2010-11-06 23:04:47 +01:00
}
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
}
2013-11-26 23:01:37 +01:00
void WorkingBanner : : showEvent ( QShowEvent * event )
{
2013-12-03 22:19:11 +01:00
QDialog : : showEvent ( event ) ;
if ( ! event - > spontaneous ( ) )
{
if ( m_working )
{
m_working - > start ( ) ;
}
}
2013-11-29 01:20:59 +01:00
QTimer : : singleShot ( 25 , this , SLOT ( windowShown ( ) ) ) ;
2013-11-26 23:01:37 +01:00
}
2013-12-03 22:19:11 +01:00
void WorkingBanner : : hideEvent ( QHideEvent * event )
{
QDialog : : hideEvent ( event ) ;
if ( ! event - > spontaneous ( ) )
{
if ( m_working )
{
m_working - > stop ( ) ;
}
}
}
2010-11-06 23:04:47 +01:00
////////////////////////////////////////////////////////////
// SLOTS
////////////////////////////////////////////////////////////
2013-11-26 23:01:37 +01:00
void WorkingBanner : : windowShown ( void )
{
2014-12-08 22:06:01 +01:00
MUtils : : GUI : : bring_to_front ( this ) ;
2013-11-26 23:01:37 +01:00
}
2010-11-06 23:04:47 +01:00
void WorkingBanner : : setText ( const QString & text )
{
2014-12-18 22:49:37 +01:00
if ( m_metrics . isNull ( ) )
2012-05-08 01:12:26 +02:00
{
2014-12-18 22:49:37 +01:00
m_metrics . reset ( new QFontMetrics ( ui - > labelStatus - > font ( ) ) ) ;
2012-05-08 01:12:26 +02:00
}
2013-11-26 23:01:37 +01:00
if ( m_metrics - > width ( text ) < = ui - > labelStatus - > width ( ) - 16 )
2010-11-06 23:04:47 +01:00
{
2013-11-25 13:58:21 +01:00
ui - > labelStatus - > setText ( text ) ;
2010-11-06 23:04:47 +01:00
}
else
{
QString choppedText = text . simplified ( ) . append ( " ... " ) ;
2013-11-26 23:01:37 +01:00
while ( ( m_metrics - > width ( choppedText ) > ui - > labelStatus - > width ( ) - 16 ) & & ( choppedText . length ( ) > 8 ) )
2010-11-06 23:04:47 +01:00
{
choppedText . chop ( 4 ) ;
choppedText = choppedText . trimmed ( ) ;
choppedText . append ( " ... " ) ;
}
2013-11-25 13:58:21 +01:00
ui - > labelStatus - > setText ( choppedText ) ;
2010-11-06 23:04:47 +01:00
}
}
2012-05-06 04:57:00 +02:00
void WorkingBanner : : setProgressMax ( unsigned int max )
{
2013-11-25 13:58:21 +01:00
ui - > progressBar - > setMaximum ( max ) ;
if ( ui - > progressBar - > maximum ( ) > ui - > progressBar - > minimum ( ) )
{
2014-12-18 22:49:37 +01:00
m_taskbar - > setTaskbarState ( MUtils : : Taskbar7 : : TASKBAR_STATE_NONE ) ;
m_taskbar - > setTaskbarProgress ( ui - > progressBar - > value ( ) , ui - > progressBar - > maximum ( ) ) ;
2013-11-25 13:58:21 +01:00
}
else
{
2014-12-18 22:49:37 +01:00
m_taskbar - > setTaskbarState ( MUtils : : Taskbar7 : : TASKBAR_STATE_INTERMEDIATE ) ;
2013-11-25 13:58:21 +01:00
}
2012-05-06 04:57:00 +02:00
}
void WorkingBanner : : setProgressVal ( unsigned int val )
{
2013-11-25 13:58:21 +01:00
ui - > progressBar - > setValue ( val ) ;
if ( ui - > progressBar - > maximum ( ) > ui - > progressBar - > minimum ( ) )
{
2014-12-18 22:49:37 +01:00
m_taskbar - > setTaskbarProgress ( ui - > progressBar - > value ( ) , ui - > progressBar - > maximum ( ) ) ;
2013-11-25 13:58:21 +01:00
}
2012-05-12 02:51:24 +02:00
}
////////////////////////////////////////////////////////////
// Private
////////////////////////////////////////////////////////////
2013-11-24 17:55:35 +01:00
/*
2012-05-12 02:51:24 +02:00
void WorkingBanner : : updateProgress ( void )
{
2012-05-06 04:57:00 +02:00
if ( m_progressMax > 0 )
{
2012-05-12 02:51:24 +02:00
int newProgress = qRound ( qBound ( 0.0 , static_cast < double > ( m_progressVal ) / static_cast < double > ( m_progressMax ) , 1.0 ) * 100.0 ) ;
if ( m_progressInt ! = newProgress )
{
m_progressInt = newProgress ;
m_progress - > setText ( QString : : number ( m_progressInt ) ) ;
if ( this - > isVisible ( ) )
{
labelStatus - > repaint ( ) ;
}
}
2012-05-06 04:57:00 +02:00
}
}
2013-11-24 17:55:35 +01:00
*/