1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2012 LoRd_MuldeR <MuldeR2@GMX.de>
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.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License along
16 // with this program; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
25 #include "model_jobList.h"
26 #include "model_options.h"
27 #include "win_addJob.h"
31 #include <QCloseEvent>
32 #include <QMessageBox>
33 #include <QDesktopServices>
38 const char *home_url = "http://mulder.brhack.net/";
40 #define PRE_RELEASE (0)
42 #define SET_FONT_BOLD(WIDGET,BOLD) { QFont _font = WIDGET->font(); _font.setBold(BOLD); WIDGET->setFont(_font); }
43 #define SET_TEXT_COLOR(WIDGET,COLOR) { QPalette _palette = WIDGET->palette(); _palette.setColor(QPalette::WindowText, (COLOR)); _palette.setColor(QPalette::Text, (COLOR)); WIDGET->setPalette(_palette); }
45 ///////////////////////////////////////////////////////////////////////////////
46 // Constructor & Destructor
47 ///////////////////////////////////////////////////////////////////////////////
49 MainWindow::MainWindow(bool x64supported)
51 m_x64supported(x64supported),
52 m_appDir(QApplication::applicationDirPath()),
55 //Init the dialog, from the .ui file
57 setWindowFlags(windowFlags() & (~Qt::WindowMaximizeButtonHint));
60 qRegisterMetaType<QUuid>("QUuid");
61 qRegisterMetaType<EncodeThread::JobStatus>("EncodeThread::JobStatus");
64 setMinimumSize(size());
65 splitter->setSizes(QList<int>() << 16 << 196);
68 labelBuildDate->setText(tr("Built on %1 at %2").arg(x264_version_date().toString(Qt::ISODate), QString::fromLatin1(x264_version_time())));
69 labelBuildDate->installEventFilter(this);
70 setWindowTitle(QString("%1 (%2 Mode)").arg(windowTitle(), m_x64supported ? "64-Bit" : "32-Bit"));
71 if(PRE_RELEASE) setWindowTitle(QString("%1 | PRE-RELEASE VERSION").arg(windowTitle()));
75 m_jobList = new JobListModel();
76 connect(m_jobList, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this, SLOT(jobChangedData(QModelIndex, QModelIndex)));
77 jobsView->setModel(m_jobList);
80 jobsView->horizontalHeader()->setSectionHidden(3, true);
81 jobsView->horizontalHeader()->setResizeMode(0, QHeaderView::Stretch);
82 jobsView->horizontalHeader()->setResizeMode(1, QHeaderView::Fixed);
83 jobsView->horizontalHeader()->setResizeMode(2, QHeaderView::Fixed);
84 jobsView->horizontalHeader()->resizeSection(1, 150);
85 jobsView->horizontalHeader()->resizeSection(2, 90);
86 jobsView->verticalHeader()->setResizeMode(QHeaderView::ResizeToContents);
87 connect(jobsView->selectionModel(), SIGNAL(currentChanged(QModelIndex, QModelIndex)), this, SLOT(jobSelected(QModelIndex, QModelIndex)));
90 QAction *actionClipboard = new QAction(QIcon(":/buttons/page_paste.png"), tr("Copy to Clipboard"), logView);
91 logView->addAction(actionClipboard);
92 connect(actionClipboard, SIGNAL(triggered(bool)), this, SLOT(copyLogToClipboard(bool)));
93 jobsView->addActions(menuJob->actions());
96 connect(buttonAddJob, SIGNAL(clicked()), this, SLOT(addButtonPressed()));
97 connect(buttonStartJob, SIGNAL(clicked()), this, SLOT(startButtonPressed()));
98 connect(buttonAbortJob, SIGNAL(clicked()), this, SLOT(abortButtonPressed()));
99 connect(buttonPauseJob, SIGNAL(toggled(bool)), this, SLOT(pauseButtonPressed(bool)));
102 connect(actionAbout, SIGNAL(triggered()), this, SLOT(showAbout()));
103 connect(actionWebMulder, SIGNAL(triggered()), this, SLOT(showWebLink()));
104 connect(actionWebX264, SIGNAL(triggered()), this, SLOT(showWebLink()));
105 connect(actionWebKomisar, SIGNAL(triggered()), this, SLOT(showWebLink()));
106 connect(actionWebJarod, SIGNAL(triggered()), this, SLOT(showWebLink()));
107 connect(actionWebWiki, SIGNAL(triggered()), this, SLOT(showWebLink()));
108 connect(actionWebBluRay, SIGNAL(triggered()), this, SLOT(showWebLink()));
110 //Create floating label
111 m_label = new QLabel(jobsView);
112 m_label->setText(tr("No job created yet. Please click the 'Add New Job' button!"));
113 m_label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
114 SET_TEXT_COLOR(m_label, Qt::darkGray);
115 SET_FONT_BOLD(m_label, true);
116 m_label->setVisible(true);
117 m_label->setContextMenuPolicy(Qt::ActionsContextMenu);
118 m_label->addActions(jobsView->actions());
119 connect(splitter, SIGNAL(splitterMoved(int, int)), this, SLOT(updateLabel()));
122 //Create options object
123 m_options = new OptionsModel();
126 MainWindow::~MainWindow(void)
128 X264_DELETE(m_jobList);
129 X264_DELETE(m_options);
130 X264_DELETE(m_label);
132 while(!m_toolsList.isEmpty())
134 QFile *temp = m_toolsList.takeFirst();
139 ///////////////////////////////////////////////////////////////////////////////
141 ///////////////////////////////////////////////////////////////////////////////
143 void MainWindow::addButtonPressed(void)
145 AddJobDialog *addDialog = new AddJobDialog(this, m_options);
146 addDialog->setRunImmediately(!havePendingJobs());
147 int result = addDialog->exec();
149 if(result == QDialog::Accepted)
152 EncodeThread *thrd = new EncodeThread
154 addDialog->sourceFile(),
155 addDialog->outputFile(),
157 QString("%1/toolset").arg(m_appDir),
161 QModelIndex newIndex = m_jobList->insertJob(thrd);
163 if(addDialog->runImmediately())
165 jobsView->selectRow(newIndex.row());
166 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
167 m_jobList->startJob(newIndex);
170 m_label->setVisible(false);
173 X264_DELETE(addDialog);
176 void MainWindow::startButtonPressed(void)
178 m_jobList->startJob(jobsView->currentIndex());
181 void MainWindow::abortButtonPressed(void)
183 m_jobList->abortJob(jobsView->currentIndex());
186 void MainWindow::pauseButtonPressed(bool checked)
190 m_jobList->pauseJob(jobsView->currentIndex());
194 m_jobList->resumeJob(jobsView->currentIndex());
198 void MainWindow::jobSelected(const QModelIndex & current, const QModelIndex & previous)
200 qDebug("Job selected: %d", current.row());
204 disconnect(logView->model(), SIGNAL(rowsInserted(QModelIndex, int, int)), this, SLOT(jobLogExtended(QModelIndex, int, int)));
207 logView->setModel(m_jobList->getLogFile(current));
208 connect(logView->model(), SIGNAL(rowsInserted(QModelIndex, int, int)), this, SLOT(jobLogExtended(QModelIndex, int, int)));
209 QTimer::singleShot(0, logView, SLOT(scrollToBottom()));
211 progressBar->setValue(m_jobList->getJobProgress(current));
212 editDetails->setText(m_jobList->data(m_jobList->index(current.row(), 3, QModelIndex()), Qt::DisplayRole).toString());
213 updateButtons(m_jobList->getJobStatus(current));
215 progressBar->repaint();
218 void MainWindow::jobChangedData(const QModelIndex &topLeft, const QModelIndex &bottomRight)
220 int selected = jobsView->currentIndex().row();
222 if(topLeft.column() <= 1 && bottomRight.column() >= 1)
224 for(int i = topLeft.row(); i <= bottomRight.row(); i++)
226 EncodeThread::JobStatus status = m_jobList->getJobStatus(m_jobList->index(i, 0, QModelIndex()));
229 qDebug("Current job changed status!");
230 updateButtons(status);
232 if(status == EncodeThread::JobStatus_Completed)
234 QTimer::singleShot(0, this, SLOT(launchNextJob()));
238 else if(topLeft.column() <= 2 && bottomRight.column() >= 2)
240 for(int i = topLeft.row(); i <= bottomRight.row(); i++)
244 progressBar->setValue(m_jobList->getJobProgress(m_jobList->index(i, 0, QModelIndex())));
249 else if(topLeft.column() <= 3 && bottomRight.column() >= 3)
251 for(int i = topLeft.row(); i <= bottomRight.row(); i++)
255 editDetails->setText(m_jobList->data(m_jobList->index(i, 3, QModelIndex()), Qt::DisplayRole).toString());
262 void MainWindow::jobLogExtended(const QModelIndex & parent, int start, int end)
264 QTimer::singleShot(0, logView, SLOT(scrollToBottom()));
267 void MainWindow::showAbout(void)
271 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());
272 text += QString().sprintf("Copyright (c) 2004-%04d LoRd_MuldeR <mulder2@gmx.de>. Some rights reserved.<br>", qMax(x264_version_date().year(),QDate::currentDate().year()));
273 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());
274 text += QString().sprintf("This program is free software: you can redistribute it and/or modify<br>");
275 text += QString().sprintf("it under the terms of the GNU General Public License <http://www.gnu.org/>.<br>");
276 text += QString().sprintf("Note that this program is distributed with ABSOLUTELY NO WARRANTY.<br><br>");
277 text += QString().sprintf("Please check the web-site at <a href=\"%s\">%s</a> for updates !!!<br></tt></nobr>", home_url, home_url);
279 QMessageBox::information(this, tr("About..."), text.replace("-", "−"), tr("Close"));
282 void MainWindow::showWebLink(void)
284 if(QObject::sender() == actionWebMulder) QDesktopServices::openUrl(QUrl(home_url));
285 if(QObject::sender() == actionWebX264) QDesktopServices::openUrl(QUrl("http://www.x264.com/"));
286 if(QObject::sender() == actionWebKomisar) QDesktopServices::openUrl(QUrl("http://komisar.gin.by/"));
287 if(QObject::sender() == actionWebJarod) QDesktopServices::openUrl(QUrl("http://www.x264.nl/"));
288 if(QObject::sender() == actionWebWiki) QDesktopServices::openUrl(QUrl("http://mewiki.project357.com/wiki/X264_Settings"));
289 if(QObject::sender() == actionWebBluRay) QDesktopServices::openUrl(QUrl("http://www.x264bluray.com/"));
292 void MainWindow::launchNextJob(void)
294 const int rows = m_jobList->rowCount(QModelIndex());
296 for(int i = 0; i < rows; i++)
298 EncodeThread::JobStatus status = m_jobList->getJobStatus(m_jobList->index(i, 0, QModelIndex()));
299 if(status == EncodeThread::JobStatus_Running || status == EncodeThread::JobStatus_Running_Pass1 || status == EncodeThread::JobStatus_Running_Pass2)
301 qWarning("Still have a job running, won't launch next yet!");
306 for(int i = 0; i < rows; i++)
308 EncodeThread::JobStatus status = m_jobList->getJobStatus(m_jobList->index(i, 0, QModelIndex()));
309 if(status == EncodeThread::JobStatus_Enqueued)
311 m_jobList->startJob(m_jobList->index(i, 0, QModelIndex()));
316 qWarning("No enqueued jobs left!");
319 void MainWindow::init(void)
321 static const char *binFiles = "x264.exe:x264_x64.exe:avs2yuv.exe";
322 QStringList binaries = QString::fromLatin1(binFiles).split(":", QString::SkipEmptyParts);
327 while(!binaries.isEmpty())
329 QString current = binaries.takeFirst();
330 QFile *file = new QFile(QString("%1/toolset/%2").arg(m_appDir, current));
331 if(file->open(QIODevice::ReadOnly))
338 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("-", "−"));
339 qFatal(QString("Binary not found: %1/toolset/%2").arg(m_appDir, current).toLatin1().constData());
340 close(); qApp->exit(-1); return;
347 qsrand(time(NULL)); int rnd = qrand() % 3;
348 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);
349 if(rnd != val) { close(); qApp->exit(-1); return; }
352 //Check for Avisynth support
353 bool avsAvailable = false;
354 QLibrary *avsLib = new QLibrary("avisynth.dll");
357 avsAvailable = (avsLib->resolve("avs_create_script_environment") != NULL);
361 avsLib->unload(); X264_DELETE(avsLib);
362 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("-", "−"), tr("Quit"), tr("Ignore"));
363 if(val != 1) { close(); qApp->exit(-1); return; }
366 //Check for expiration
367 if(x264_version_date().addMonths(6) < QDate::currentDate())
369 QMessageBox msgBox(this);
370 msgBox.setIcon(QMessageBox::Information);
371 msgBox.setWindowTitle(tr("Update Notification"));
372 msgBox.setWindowFlags(Qt::Window | Qt::WindowTitleHint | Qt::CustomizeWindowHint);
373 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("-", "−").arg(home_url));
374 QPushButton *btn1 = msgBox.addButton(tr("Discard"), QMessageBox::NoRole);
375 QPushButton *btn2 = msgBox.addButton(tr("Discard"), QMessageBox::AcceptRole);
376 btn1->setEnabled(false);
377 btn2->setVisible(false);
378 QTimer::singleShot(5000, btn1, SLOT(hide()));
379 QTimer::singleShot(5000, btn2, SLOT(show()));
384 void MainWindow::updateLabel(void)
386 m_label->setGeometry(0, 0, jobsView->width(), jobsView->height());
389 void MainWindow::copyLogToClipboard(bool checked)
391 qDebug("copyLogToClipboard");
393 if(LogFileModel *log = dynamic_cast<LogFileModel*>(logView->model()))
395 log->copyToClipboard();
396 MessageBeep(MB_ICONINFORMATION);
400 ///////////////////////////////////////////////////////////////////////////////
402 ///////////////////////////////////////////////////////////////////////////////
404 void MainWindow::showEvent(QShowEvent *e)
406 QMainWindow::showEvent(e);
411 QTimer::singleShot(0, this, SLOT(init()));
415 void MainWindow::closeEvent(QCloseEvent *e)
417 if(havePendingJobs())
420 MessageBeep(MB_ICONWARNING);
424 QMainWindow::closeEvent(e);
427 void MainWindow::resizeEvent(QResizeEvent *e)
429 QMainWindow::resizeEvent(e);
433 bool MainWindow::eventFilter(QObject *o, QEvent *e)
435 if((o == labelBuildDate) && (e->type() == QEvent::MouseButtonPress))
437 QTimer::singleShot(0, this, SLOT(showAbout()));
443 ///////////////////////////////////////////////////////////////////////////////
445 ///////////////////////////////////////////////////////////////////////////////
447 bool MainWindow::havePendingJobs(void)
449 const int rows = m_jobList->rowCount(QModelIndex());
451 for(int i = 0; i < rows; i++)
453 EncodeThread::JobStatus status = m_jobList->getJobStatus(m_jobList->index(i, 0, QModelIndex()));
454 if(status != EncodeThread::JobStatus_Completed && status != EncodeThread::JobStatus_Aborted && status != EncodeThread::JobStatus_Failed)
463 void MainWindow::updateButtons(EncodeThread::JobStatus status)
465 qDebug("MainWindow::updateButtons(void)");
467 buttonStartJob->setEnabled(status == EncodeThread::JobStatus_Enqueued);
468 buttonAbortJob->setEnabled(status == EncodeThread::JobStatus_Indexing || status == EncodeThread::JobStatus_Running || status == EncodeThread::JobStatus_Running_Pass1 || status == EncodeThread::JobStatus_Running_Pass2 || status == EncodeThread::JobStatus_Paused);
469 buttonPauseJob->setEnabled(status == EncodeThread::JobStatus_Indexing || status == EncodeThread::JobStatus_Running || status == EncodeThread::JobStatus_Paused || status == EncodeThread::JobStatus_Running_Pass1 || status == EncodeThread::JobStatus_Running_Pass2);
470 buttonPauseJob->setChecked(status == EncodeThread::JobStatus_Paused || status == EncodeThread::JobStatus_Pausing);
472 actionJob_Start->setEnabled(buttonStartJob->isEnabled());
473 actionJob_Abort->setEnabled(buttonAbortJob->isEnabled());
474 actionJob_Pause->setEnabled(buttonPauseJob->isEnabled());
475 actionJob_Pause->setChecked(buttonPauseJob->isChecked());
477 editDetails->setEnabled(status != EncodeThread::JobStatus_Paused);