OSDN Git Service

Added "Pause" support.
[x264-launcher/x264-launcher.git] / src / win_main.cpp
index a110f87..d52a9d4 100644 (file)
 #include <QMessageBox>
 #include <QDesktopServices>
 #include <QUrl>
+#include <QDir>
+#include <QLibrary>
 
 const char *home_url = "http://mulder.brhack.net/";
 
+#define PRE_RELEASE (0)
+
+#define SET_FONT_BOLD(WIDGET,BOLD) { QFont _font = WIDGET->font(); _font.setBold(BOLD); WIDGET->setFont(_font); }
+#define SET_TEXT_COLOR(WIDGET,COLOR) { QPalette _palette = WIDGET->palette(); _palette.setColor(QPalette::WindowText, (COLOR)); _palette.setColor(QPalette::Text, (COLOR)); WIDGET->setPalette(_palette); }
+
 ///////////////////////////////////////////////////////////////////////////////
 // Constructor & Destructor
 ///////////////////////////////////////////////////////////////////////////////
 
 MainWindow::MainWindow(bool x64supported)
 :
-       m_x64supported(x64supported)
+       m_x64supported(x64supported),
+       m_appDir(QApplication::applicationDirPath()),
+       m_firstShow(true)
 {
        //Init the dialog, from the .ui file
        setupUi(this);
@@ -53,16 +62,20 @@ MainWindow::MainWindow(bool x64supported)
 
        //Freeze minimum size
        setMinimumSize(size());
+       splitter->setSizes(QList<int>() << 16 << 196);
 
        //Update title
-       setWindowTitle(QString("%1 [%2]").arg(windowTitle(), x264_version_date().toString(Qt::ISODate)));
-       if(m_x64supported) setWindowTitle(QString("%1 (x64)").arg(windowTitle()));
+       labelBuildDate->setText(tr("Built on %1 at %2").arg(x264_version_date().toString(Qt::ISODate), QString::fromLatin1(x264_version_time())));
+       labelBuildDate->installEventFilter(this);
+       setWindowTitle(QString("%1 (%2 Mode)").arg(windowTitle(), m_x64supported ? "64-Bit" : "32-Bit"));
+       if(PRE_RELEASE) setWindowTitle(QString("%1 | PRE-RELEASE VERSION").arg(windowTitle()));
+
 
        //Create model
        m_jobList = new JobListModel();
        connect(m_jobList, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this, SLOT(jobChangedData(QModelIndex, QModelIndex)));
        jobsView->setModel(m_jobList);
-
+       
        //Setup view
        jobsView->horizontalHeader()->setSectionHidden(3, true);
        jobsView->horizontalHeader()->setResizeMode(0, QHeaderView::Stretch);
@@ -73,10 +86,17 @@ MainWindow::MainWindow(bool x64supported)
        jobsView->verticalHeader()->setResizeMode(QHeaderView::ResizeToContents);
        connect(jobsView->selectionModel(), SIGNAL(currentChanged(QModelIndex, QModelIndex)), this, SLOT(jobSelected(QModelIndex, QModelIndex)));
 
+       //Create context menu
+       QAction *actionClipboard = new QAction(QIcon(":/buttons/page_paste.png"), tr("Copy to Clipboard"), logView);
+       logView->addAction(actionClipboard);
+       connect(actionClipboard, SIGNAL(triggered(bool)), this, SLOT(copyLogToClipboard(bool)));
+       jobsView->addActions(menuJob->actions());
+
        //Enable buttons
        connect(buttonAddJob, SIGNAL(clicked()), this, SLOT(addButtonPressed()));
        connect(buttonStartJob, SIGNAL(clicked()), this, SLOT(startButtonPressed()));
        connect(buttonAbortJob, SIGNAL(clicked()), this, SLOT(abortButtonPressed()));
+       connect(buttonPauseJob, SIGNAL(toggled(bool)), this, SLOT(pauseButtonPressed(bool)));
 
        //Enable menu
        connect(actionAbout, SIGNAL(triggered()), this, SLOT(showAbout()));
@@ -85,6 +105,19 @@ MainWindow::MainWindow(bool x64supported)
        connect(actionWebKomisar, SIGNAL(triggered()), this, SLOT(showWebLink()));
        connect(actionWebJarod, SIGNAL(triggered()), this, SLOT(showWebLink()));
        connect(actionWebWiki, SIGNAL(triggered()), this, SLOT(showWebLink()));
+       connect(actionWebBluRay, SIGNAL(triggered()), this, SLOT(showWebLink()));
+
+       //Create floating label
+       m_label = new QLabel(jobsView);
+       m_label->setText(tr("No job created yet. Please click the 'Add New Job' button!"));
+       m_label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
+       SET_TEXT_COLOR(m_label, Qt::darkGray);
+       SET_FONT_BOLD(m_label, true);
+       m_label->setVisible(true);
+       m_label->setContextMenuPolicy(Qt::ActionsContextMenu);
+       m_label->addActions(jobsView->actions());
+       connect(splitter, SIGNAL(splitterMoved(int, int)), this, SLOT(updateLabel()));
+       updateLabel();
 
        //Create options object
        m_options = new OptionsModel();
@@ -94,6 +127,13 @@ MainWindow::~MainWindow(void)
 {
        X264_DELETE(m_jobList);
        X264_DELETE(m_options);
+       X264_DELETE(m_label);
+
+       while(!m_toolsList.isEmpty())
+       {
+               QFile *temp = m_toolsList.takeFirst();
+               X264_DELETE(temp);
+       }
 }
 
 ///////////////////////////////////////////////////////////////////////////////
@@ -108,11 +148,14 @@ void MainWindow::addButtonPressed(void)
        
        if(result == QDialog::Accepted)
        {
+               
                EncodeThread *thrd = new EncodeThread
                (
                        addDialog->sourceFile(),
                        addDialog->outputFile(),
-                       m_options
+                       m_options,
+                       QString("%1/toolset").arg(m_appDir),
+                       m_x64supported
                );
 
                QModelIndex newIndex = m_jobList->insertJob(thrd);
@@ -123,6 +166,8 @@ void MainWindow::addButtonPressed(void)
                        QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
                        m_jobList->startJob(newIndex);
                }
+
+               m_label->setVisible(false);
        }
 
        X264_DELETE(addDialog);
@@ -138,6 +183,18 @@ void MainWindow::abortButtonPressed(void)
        m_jobList->abortJob(jobsView->currentIndex());
 }
 
+void MainWindow::pauseButtonPressed(bool checked)
+{
+       if(checked)
+       {
+               m_jobList->pauseJob(jobsView->currentIndex());
+       }
+       else
+       {
+               m_jobList->resumeJob(jobsView->currentIndex());
+       }
+}
+
 void MainWindow::jobSelected(const QModelIndex & current, const QModelIndex & previous)
 {
        qDebug("Job selected: %d", current.row());
@@ -154,6 +211,8 @@ void MainWindow::jobSelected(const QModelIndex & current, const QModelIndex & pr
        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));
+
+       progressBar->repaint();
 }
 
 void MainWindow::jobChangedData(const QModelIndex &topLeft, const  QModelIndex &bottomRight)
@@ -217,7 +276,7 @@ void MainWindow::showAbout(void)
        text += QString().sprintf("Note that this program is distributed with ABSOLUTELY NO WARRANTY.<br><br>");
        text += QString().sprintf("Please check the web-site at <a href=\"%s\">%s</a> for updates !!!<br></tt></nobr>", home_url, home_url);
 
-       QMessageBox::information(this, tr("About..."), text.replace("-", "&minus;"));
+       QMessageBox::information(this, tr("About..."), text.replace("-", "&minus;"), tr("Close"));
 }
 
 void MainWindow::showWebLink(void)
@@ -227,6 +286,7 @@ void MainWindow::showWebLink(void)
        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"));
+       if(QObject::sender() == actionWebBluRay) QDesktopServices::openUrl(QUrl("http://www.x264bluray.com/"));
 }
 
 void MainWindow::launchNextJob(void)
@@ -256,10 +316,102 @@ void MainWindow::launchNextJob(void)
        qWarning("No enqueued jobs left!");
 }
 
+void MainWindow::init(void)
+{
+       static const char *binFiles = "x264.exe:x264_x64.exe:avs2yuv.exe";
+       QStringList binaries = QString::fromLatin1(binFiles).split(":", QString::SkipEmptyParts);
+
+       updateLabel();
+
+       //Check all binaries
+       while(!binaries.isEmpty())
+       {
+               QString current = binaries.takeFirst();
+               QFile *file = new QFile(QString("%1/toolset/%2").arg(m_appDir, current));
+               if(file->open(QIODevice::ReadOnly))
+               {
+                       m_toolsList << file;
+               }
+               else
+               {
+                       X264_DELETE(file);
+                       QMessageBox::critical(this, tr("File Not Found!"), tr("<nobr>At least on required tool could not be found:<br>%1<br><br>Please re-install the program in order to fix the problem!</nobr>").arg(QDir::toNativeSeparators(QString("%1/toolset/%2").arg(m_appDir, current))).replace("-", "&minus;"));
+                       qFatal(QString("Binary not found: %1/toolset/%2").arg(m_appDir, current).toLatin1().constData());
+                       close(); qApp->exit(-1); return;
+               }
+       }
+
+       //Pre-release popup
+       if(PRE_RELEASE)
+       {
+               qsrand(time(NULL)); int rnd = qrand() % 3;
+               int val = QMessageBox::information(this, tr("Pre-Release Version"), tr("Note: This is a pre-release version. Please do NOT use for production!<br>Click the button #%1 in order to continue...<br><br>(There will be no such message box in the final version of this application)").arg(QString::number(rnd + 1)), tr("(1)"), tr("(2)"), tr("(3)"), qrand() % 3);
+               if(rnd != val) { close(); qApp->exit(-1); return; }
+       }
+
+       //Check for Avisynth support
+       bool avsAvailable = false;
+       QLibrary *avsLib = new QLibrary("avisynth.dll");
+       if(avsLib->load())
+       {
+               avsAvailable = (avsLib->resolve("avs_create_script_environment") != NULL);
+       }
+       if(!avsAvailable)
+       {
+               avsLib->unload(); X264_DELETE(avsLib);
+               int val = QMessageBox::warning(this, tr("Avisynth Missing"), tr("<nobr>It appears that Avisynth is not currently installed on your computer.<br>Thus Avisynth input will not be working at all!<br><br>Please download and install Avisynth:<br><a href=\"http://sourceforge.net/projects/avisynth2/files/AviSynth%202.5/\">http://sourceforge.net/projects/avisynth2/files/AviSynth 2.5/</a></nobr>").replace("-", "&minus;"), tr("Quit"), tr("Ignore"));
+               if(val != 1) { close(); qApp->exit(-1); return; }
+       }
+
+       //Check for expiration
+       if(x264_version_date().addMonths(6) < QDate::currentDate())
+       {
+               QMessageBox msgBox(this);
+               msgBox.setIcon(QMessageBox::Information);
+               msgBox.setWindowTitle(tr("Update Notification"));
+               msgBox.setWindowFlags(Qt::Window | Qt::WindowTitleHint | Qt::CustomizeWindowHint);
+               msgBox.setText(tr("<nobr><tt>Oups, this version of 'Simple x264 Launcher' is more than 6 months old.<br><br>Please check the official web-site at <a href=\"%1\">%1</a> for updates!<br></tt></nobr>").replace("-", "&minus;").arg(home_url));
+               QPushButton *btn1 = msgBox.addButton(tr("Discard"), QMessageBox::NoRole);
+               QPushButton *btn2 = msgBox.addButton(tr("Discard"), QMessageBox::AcceptRole);
+               btn1->setEnabled(false);
+               btn2->setVisible(false);
+               QTimer::singleShot(5000, btn1, SLOT(hide()));
+               QTimer::singleShot(5000, btn2, SLOT(show()));
+               msgBox.exec();
+       }
+}
+
+void MainWindow::updateLabel(void)
+{
+       m_label->setGeometry(0, 0, jobsView->width(), jobsView->height());
+}
+
+void MainWindow::copyLogToClipboard(bool checked)
+{
+       qDebug("copyLogToClipboard");
+       
+       if(LogFileModel *log = dynamic_cast<LogFileModel*>(logView->model()))
+       {
+               log->copyToClipboard();
+               MessageBeep(MB_ICONINFORMATION);
+       }
+}
+
 ///////////////////////////////////////////////////////////////////////////////
 // Event functions
 ///////////////////////////////////////////////////////////////////////////////
 
+void MainWindow::showEvent(QShowEvent *e)
+{
+       QMainWindow::showEvent(e);
+
+       if(m_firstShow)
+       {
+               m_firstShow = false;
+               QTimer::singleShot(0, this, SLOT(init()));
+       }
+}
+
 void MainWindow::closeEvent(QCloseEvent *e)
 {
        if(havePendingJobs())
@@ -272,6 +424,22 @@ void MainWindow::closeEvent(QCloseEvent *e)
        QMainWindow::closeEvent(e);
 }
 
+void MainWindow::resizeEvent(QResizeEvent *e)
+{
+       QMainWindow::resizeEvent(e);
+       updateLabel();
+}
+
+bool MainWindow::eventFilter(QObject *o, QEvent *e)
+{
+       if((o == labelBuildDate) && (e->type() == QEvent::MouseButtonPress))
+       {
+               QTimer::singleShot(0, this, SLOT(showAbout()));
+               return true;
+       }
+       return false;
+}
+
 ///////////////////////////////////////////////////////////////////////////////
 // Private functions
 ///////////////////////////////////////////////////////////////////////////////
@@ -297,6 +465,14 @@ void MainWindow::updateButtons(EncodeThread::JobStatus status)
        qDebug("MainWindow::updateButtons(void)");
 
        buttonStartJob->setEnabled(status == EncodeThread::JobStatus_Enqueued);
-       buttonAbortJob->setEnabled(status == EncodeThread::JobStatus_Indexing || status == EncodeThread::JobStatus_Running ||
-               status == EncodeThread::JobStatus_Running_Pass1 || status == EncodeThread::JobStatus_Running_Pass2 );
+       buttonAbortJob->setEnabled(status == EncodeThread::JobStatus_Indexing || status == EncodeThread::JobStatus_Running || status == EncodeThread::JobStatus_Running_Pass1 || status == EncodeThread::JobStatus_Running_Pass2 || status == EncodeThread::JobStatus_Paused);
+       buttonPauseJob->setEnabled(status == EncodeThread::JobStatus_Indexing || status == EncodeThread::JobStatus_Running || status == EncodeThread::JobStatus_Paused || status == EncodeThread::JobStatus_Running_Pass1 || status == EncodeThread::JobStatus_Running_Pass2);
+       buttonPauseJob->setChecked(status == EncodeThread::JobStatus_Paused || status == EncodeThread::JobStatus_Pausing);
+
+       actionJob_Start->setEnabled(buttonStartJob->isEnabled());
+       actionJob_Abort->setEnabled(buttonAbortJob->isEnabled());
+       actionJob_Pause->setEnabled(buttonPauseJob->isEnabled());
+       actionJob_Pause->setChecked(buttonPauseJob->isChecked());
+
+       editDetails->setEnabled(status != EncodeThread::JobStatus_Paused);
 }