2012-01-28 16:40:14 +01:00
///////////////////////////////////////////////////////////////////////////////
// Simple x264 Launcher
// Copyright (C) 2004-2012 LoRd_MuldeR <MuldeR2@GMX.de>
//
// 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 "win_main.h"
2012-01-28 18:55:40 +01:00
2012-01-28 16:40:14 +01:00
# include "global.h"
2012-01-28 18:55:40 +01:00
# include "model_jobList.h"
2012-01-29 04:06:07 +01:00
# include "win_addJob.h"
2012-01-28 18:55:40 +01:00
# include <QDate>
2012-01-28 19:59:04 +01:00
# include <QTimer>
2012-01-28 23:24:41 +01:00
# include <QCloseEvent>
# include <QMessageBox>
2012-01-29 15:57:23 +01:00
# include <QDesktopServices>
# include <QUrl>
const char * home_url = " http://mulder.brhack.net/ " ;
2012-01-28 16:40:14 +01:00
///////////////////////////////////////////////////////////////////////////////
// Constructor & Destructor
///////////////////////////////////////////////////////////////////////////////
2012-01-29 01:19:50 +01:00
MainWindow : : MainWindow ( bool x64supported )
:
m_x64supported ( x64supported )
2012-01-28 16:40:14 +01:00
{
//Init the dialog, from the .ui file
setupUi ( this ) ;
setWindowFlags ( windowFlags ( ) ^ Qt : : WindowMaximizeButtonHint ) ;
2012-01-28 18:55:40 +01:00
//Register meta types
qRegisterMetaType < QUuid > ( " QUuid " ) ;
qRegisterMetaType < EncodeThread : : JobStatus > ( " EncodeThread::JobStatus " ) ;
//Freeze minimum size
setMinimumSize ( size ( ) ) ;
2012-01-29 01:19:50 +01:00
//Update title
2012-01-28 18:55:40 +01:00
setWindowTitle ( QString ( " %1 [%2] " ) . arg ( windowTitle ( ) , x264_version_date ( ) . toString ( Qt : : ISODate ) ) ) ;
2012-01-29 01:19:50 +01:00
if ( m_x64supported ) setWindowTitle ( QString ( " %1 (x64) " ) . arg ( windowTitle ( ) ) ) ;
2012-01-28 18:55:40 +01:00
//Create model
m_jobList = new JobListModel ( ) ;
2012-01-28 23:24:41 +01:00
connect ( m_jobList , SIGNAL ( dataChanged ( QModelIndex , QModelIndex ) ) , this , SLOT ( jobChangedData ( QModelIndex , QModelIndex ) ) ) ;
2012-01-28 18:55:40 +01:00
jobsView - > setModel ( m_jobList ) ;
//Setup view
2012-01-28 23:24:41 +01:00
jobsView - > horizontalHeader ( ) - > setSectionHidden ( 3 , true ) ;
2012-01-28 18:55:40 +01:00
jobsView - > horizontalHeader ( ) - > setResizeMode ( 0 , QHeaderView : : Stretch ) ;
jobsView - > horizontalHeader ( ) - > setResizeMode ( 1 , QHeaderView : : Fixed ) ;
jobsView - > horizontalHeader ( ) - > setResizeMode ( 2 , QHeaderView : : Fixed ) ;
jobsView - > horizontalHeader ( ) - > resizeSection ( 1 , 150 ) ;
jobsView - > horizontalHeader ( ) - > resizeSection ( 2 , 90 ) ;
jobsView - > verticalHeader ( ) - > setResizeMode ( QHeaderView : : ResizeToContents ) ;
2012-01-28 19:59:04 +01:00
connect ( jobsView - > selectionModel ( ) , SIGNAL ( currentChanged ( QModelIndex , QModelIndex ) ) , this , SLOT ( jobSelected ( QModelIndex , QModelIndex ) ) ) ;
2012-01-28 18:55:40 +01:00
//Enable buttons
connect ( buttonAddJob , SIGNAL ( clicked ( ) ) , this , SLOT ( addButtonPressed ( ) ) ) ;
2012-01-28 23:24:41 +01:00
connect ( buttonStartJob , SIGNAL ( clicked ( ) ) , this , SLOT ( startButtonPressed ( ) ) ) ;
connect ( buttonAbortJob , SIGNAL ( clicked ( ) ) , this , SLOT ( abortButtonPressed ( ) ) ) ;
//Enable menu
connect ( actionAbout , SIGNAL ( triggered ( ) ) , this , SLOT ( showAbout ( ) ) ) ;
2012-01-29 15:57:23 +01:00
connect ( actionWebMulder , SIGNAL ( triggered ( ) ) , this , SLOT ( showWebLink ( ) ) ) ;
connect ( actionWebX264 , SIGNAL ( triggered ( ) ) , this , SLOT ( showWebLink ( ) ) ) ;
connect ( actionWebKomisar , SIGNAL ( triggered ( ) ) , this , SLOT ( showWebLink ( ) ) ) ;
connect ( actionWebJarod , SIGNAL ( triggered ( ) ) , this , SLOT ( showWebLink ( ) ) ) ;
connect ( actionWebWiki , SIGNAL ( triggered ( ) ) , this , SLOT ( showWebLink ( ) ) ) ;
2012-01-28 16:40:14 +01:00
}
MainWindow : : ~ MainWindow ( void )
{
2012-01-28 23:24:41 +01:00
X264_DELETE ( m_jobList ) ;
2012-01-28 16:40:14 +01:00
}
2012-01-28 18:55:40 +01:00
///////////////////////////////////////////////////////////////////////////////
// Slots
///////////////////////////////////////////////////////////////////////////////
void MainWindow : : addButtonPressed ( void )
{
2012-01-29 04:06:07 +01:00
AddJobDialog * addDialog = new AddJobDialog ( this ) ;
2012-01-29 15:57:23 +01:00
addDialog - > setRunImmediately ( ! havePendingJobs ( ) ) ;
2012-01-29 04:06:07 +01:00
int result = addDialog - > exec ( ) ;
if ( result = = QDialog : : Accepted )
{
2012-01-29 15:57:23 +01:00
EncodeThread * thrd = new EncodeThread
(
addDialog - > sourceFile ( ) ,
addDialog - > outputFile ( )
) ;
2012-01-29 04:06:07 +01:00
QModelIndex newIndex = m_jobList - > insertJob ( thrd ) ;
2012-01-29 15:57:23 +01:00
if ( addDialog - > runImmediately ( ) )
{
jobsView - > selectRow ( newIndex . row ( ) ) ;
QApplication : : processEvents ( QEventLoop : : ExcludeUserInputEvents ) ;
m_jobList - > startJob ( newIndex ) ;
}
2012-01-29 04:06:07 +01:00
}
X264_DELETE ( addDialog ) ;
2012-01-28 23:24:41 +01:00
}
2012-01-28 19:59:04 +01:00
2012-01-28 23:24:41 +01:00
void MainWindow : : startButtonPressed ( void )
{
m_jobList - > startJob ( jobsView - > currentIndex ( ) ) ;
2012-01-28 19:59:04 +01:00
}
2012-01-28 23:24:41 +01:00
void MainWindow : : abortButtonPressed ( void )
{
m_jobList - > abortJob ( jobsView - > currentIndex ( ) ) ;
}
2012-01-29 00:57:47 +01:00
2012-01-28 19:59:04 +01:00
void MainWindow : : jobSelected ( const QModelIndex & current , const QModelIndex & previous )
{
qDebug ( " Job selected: %d " , current . row ( ) ) ;
2012-01-28 23:24:41 +01:00
if ( logView - > model ( ) )
{
disconnect ( logView - > model ( ) , SIGNAL ( rowsInserted ( QModelIndex , int , int ) ) , this , SLOT ( jobLogExtended ( QModelIndex , int , int ) ) ) ;
}
2012-01-28 19:59:04 +01:00
logView - > setModel ( m_jobList - > getLogFile ( current ) ) ;
2012-01-28 23:24:41 +01:00
connect ( logView - > model ( ) , SIGNAL ( rowsInserted ( QModelIndex , int , int ) ) , this , SLOT ( jobLogExtended ( QModelIndex , int , int ) ) ) ;
QTimer : : singleShot ( 0 , logView , SLOT ( scrollToBottom ( ) ) ) ;
progressBar - > setValue ( m_jobList - > getJobProgress ( current ) ) ;
editDetails - > setText ( m_jobList - > data ( m_jobList - > index ( current . row ( ) , 3 , QModelIndex ( ) ) , Qt : : DisplayRole ) . toString ( ) ) ;
updateButtons ( m_jobList - > getJobStatus ( current ) ) ;
}
void MainWindow : : jobChangedData ( const QModelIndex & topLeft , const QModelIndex & bottomRight )
{
int selected = jobsView - > currentIndex ( ) . row ( ) ;
if ( topLeft . column ( ) < = 1 & & bottomRight . column ( ) > = 1 )
{
for ( int i = topLeft . row ( ) ; i < = bottomRight . row ( ) ; i + + )
{
2012-01-29 00:57:47 +01:00
EncodeThread : : JobStatus status = m_jobList - > getJobStatus ( m_jobList - > index ( i , 0 , QModelIndex ( ) ) ) ;
2012-01-28 23:24:41 +01:00
if ( i = = selected )
{
qDebug ( " Current job changed status! " ) ;
2012-01-29 00:57:47 +01:00
updateButtons ( status ) ;
}
if ( status = = EncodeThread : : JobStatus_Completed )
{
QTimer : : singleShot ( 0 , this , SLOT ( launchNextJob ( ) ) ) ;
2012-01-28 23:24:41 +01:00
}
}
}
else if ( topLeft . column ( ) < = 2 & & bottomRight . column ( ) > = 2 )
{
for ( int i = topLeft . row ( ) ; i < = bottomRight . row ( ) ; i + + )
{
if ( i = = selected )
{
progressBar - > setValue ( m_jobList - > getJobProgress ( m_jobList - > index ( i , 0 , QModelIndex ( ) ) ) ) ;
break ;
}
}
}
else if ( topLeft . column ( ) < = 3 & & bottomRight . column ( ) > = 3 )
{
for ( int i = topLeft . row ( ) ; i < = bottomRight . row ( ) ; i + + )
{
if ( i = = selected )
{
editDetails - > setText ( m_jobList - > data ( m_jobList - > index ( i , 3 , QModelIndex ( ) ) , Qt : : DisplayRole ) . toString ( ) ) ;
break ;
}
}
}
}
void MainWindow : : jobLogExtended ( const QModelIndex & parent , int start , int end )
{
QTimer : : singleShot ( 0 , logView , SLOT ( scrollToBottom ( ) ) ) ;
}
void MainWindow : : showAbout ( void )
{
QString text ;
2012-01-29 00:57:47 +01:00
text + = QString ( ) . sprintf ( " <nobr><tt>Simple x264 Launcher v%u.%02u - use 64-Bit x264 with 32-Bit Avisynth<br> " , x264_version_major ( ) , x264_version_minor ( ) ) ;
text + = QString ( ) . sprintf ( " Copyright (c) 2004-%04d LoRd_MuldeR <mulder2@gmx.de>. Some rights reserved.<br> " , qMax ( x264_version_date ( ) . year ( ) , QDate : : currentDate ( ) . year ( ) ) ) ;
text + = QString ( ) . sprintf ( " Built on %s at %s with %s for Win-%s.<br><br> " , x264_version_date ( ) . toString ( Qt : : ISODate ) . toLatin1 ( ) . constData ( ) , x264_version_time ( ) , x264_version_compiler ( ) , x264_version_arch ( ) ) ;
2012-01-28 23:24:41 +01:00
text + = QString ( ) . sprintf ( " This program is free software: you can redistribute it and/or modify<br> " ) ;
text + = QString ( ) . sprintf ( " it under the terms of the GNU General Public License <http://www.gnu.org/>.<br> " ) ;
text + = QString ( ) . sprintf ( " Note that this program is distributed with ABSOLUTELY NO WARRANTY.<br><br> " ) ;
2012-01-29 15:57:23 +01:00
text + = QString ( ) . sprintf ( " Please check the web-site at <a href= \" %s \" >%s</a> for updates !!!<br></tt></nobr> " , home_url , home_url ) ;
2012-01-29 00:57:47 +01:00
QMessageBox : : information ( this , tr ( " About... " ) , text . replace ( " - " , " − " ) ) ;
}
2012-01-29 15:57:23 +01:00
void MainWindow : : showWebLink ( void )
{
if ( QObject : : sender ( ) = = actionWebMulder ) QDesktopServices : : openUrl ( QUrl ( home_url ) ) ;
if ( QObject : : sender ( ) = = actionWebX264 ) QDesktopServices : : openUrl ( QUrl ( " http://www.x264.com/ " ) ) ;
if ( QObject : : sender ( ) = = actionWebKomisar ) QDesktopServices : : openUrl ( QUrl ( " http://komisar.gin.by/ " ) ) ;
if ( QObject : : sender ( ) = = actionWebJarod ) QDesktopServices : : openUrl ( QUrl ( " http://www.x264.nl/ " ) ) ;
if ( QObject : : sender ( ) = = actionWebWiki ) QDesktopServices : : openUrl ( QUrl ( " http://mewiki.project357.com/wiki/X264_Settings " ) ) ;
}
2012-01-29 00:57:47 +01:00
void MainWindow : : launchNextJob ( void )
{
const int rows = m_jobList - > rowCount ( QModelIndex ( ) ) ;
for ( int i = 0 ; i < rows ; i + + )
{
EncodeThread : : JobStatus status = m_jobList - > getJobStatus ( m_jobList - > index ( i , 0 , QModelIndex ( ) ) ) ;
if ( status = = EncodeThread : : JobStatus_Running | | status = = EncodeThread : : JobStatus_Running_Pass1 | | status = = EncodeThread : : JobStatus_Running_Pass2 )
{
qWarning ( " Still have a job running, won't launch next yet! " ) ;
return ;
}
}
for ( int i = 0 ; i < rows ; i + + )
{
EncodeThread : : JobStatus status = m_jobList - > getJobStatus ( m_jobList - > index ( i , 0 , QModelIndex ( ) ) ) ;
if ( status = = EncodeThread : : JobStatus_Enqueued )
{
m_jobList - > startJob ( m_jobList - > index ( i , 0 , QModelIndex ( ) ) ) ;
return ;
}
}
2012-01-28 23:24:41 +01:00
2012-01-29 00:57:47 +01:00
qWarning ( " No enqueued jobs left! " ) ;
2012-01-28 23:24:41 +01:00
}
///////////////////////////////////////////////////////////////////////////////
// Event functions
///////////////////////////////////////////////////////////////////////////////
void MainWindow : : closeEvent ( QCloseEvent * e )
2012-01-29 15:57:23 +01:00
{
if ( havePendingJobs ( ) )
{
e - > ignore ( ) ;
MessageBeep ( MB_ICONWARNING ) ;
return ;
}
QMainWindow : : closeEvent ( e ) ;
}
///////////////////////////////////////////////////////////////////////////////
// Private functions
///////////////////////////////////////////////////////////////////////////////
bool MainWindow : : havePendingJobs ( void )
2012-01-28 23:24:41 +01:00
{
const int rows = m_jobList - > rowCount ( QModelIndex ( ) ) ;
2012-01-29 15:57:23 +01:00
2012-01-28 23:24:41 +01:00
for ( int i = 0 ; i < rows ; i + + )
{
EncodeThread : : JobStatus status = m_jobList - > getJobStatus ( m_jobList - > index ( i , 0 , QModelIndex ( ) ) ) ;
if ( status ! = EncodeThread : : JobStatus_Completed & & status ! = EncodeThread : : JobStatus_Aborted & & status ! = EncodeThread : : JobStatus_Failed )
{
2012-01-29 15:57:23 +01:00
return true ;
2012-01-28 23:24:41 +01:00
}
}
2012-01-29 15:57:23 +01:00
return false ;
}
2012-01-28 23:24:41 +01:00
void MainWindow : : updateButtons ( EncodeThread : : JobStatus status )
{
qDebug ( " MainWindow::updateButtons(void) " ) ;
buttonStartJob - > setEnabled ( status = = EncodeThread : : JobStatus_Enqueued ) ;
2012-01-29 00:57:47 +01:00
buttonAbortJob - > setEnabled ( status = = EncodeThread : : JobStatus_Indexing | | status = = EncodeThread : : JobStatus_Running | |
status = = EncodeThread : : JobStatus_Running_Pass1 | | status = = EncodeThread : : JobStatus_Running_Pass2 ) ;
2012-01-28 18:55:40 +01:00
}