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 ///////////////////////////////////////////////////////////////////////////////
24 #include "model_jobList.h"
25 #include "model_options.h"
26 #include "win_addJob.h"
27 #include "win_preferences.h"
30 #include "avisynth_c.h"
34 #include <QCloseEvent>
35 #include <QMessageBox>
36 #include <QDesktopServices>
41 #include <QProgressDialog>
43 #include <QTextStream>
47 const char *home_url = "http://mulder.brhack.net/";
48 const char *update_url = "http://code.google.com/p/mulder/downloads/list";
49 const char *tpl_last = "<LAST_USED>";
51 #define SET_FONT_BOLD(WIDGET,BOLD) { QFont _font = WIDGET->font(); _font.setBold(BOLD); WIDGET->setFont(_font); }
52 #define SET_TEXT_COLOR(WIDGET,COLOR) { QPalette _palette = WIDGET->palette(); _palette.setColor(QPalette::WindowText, (COLOR)); _palette.setColor(QPalette::Text, (COLOR)); WIDGET->setPalette(_palette); }
54 ///////////////////////////////////////////////////////////////////////////////
55 // Constructor & Destructor
56 ///////////////////////////////////////////////////////////////////////////////
61 MainWindow::MainWindow(const x264_cpu_t *const cpuFeatures)
63 m_cpuFeatures(cpuFeatures),
64 m_appDir(QApplication::applicationDirPath()),
71 //Init the dialog, from the .ui file
73 setWindowFlags(windowFlags() & (~Qt::WindowMaximizeButtonHint));
76 qRegisterMetaType<QUuid>("QUuid");
77 qRegisterMetaType<QUuid>("DWORD");
78 qRegisterMetaType<EncodeThread::JobStatus>("EncodeThread::JobStatus");
81 PreferencesDialog::initPreferences(&m_preferences);
82 PreferencesDialog::loadPreferences(&m_preferences);
84 //Create options object
85 m_options = new OptionsModel();
86 OptionsModel::loadTemplate(m_options, QString::fromLatin1(tpl_last));
88 //Create IPC thread object
89 m_ipcThread = new IPCThread();
90 connect(m_ipcThread, SIGNAL(instanceCreated(DWORD)), this, SLOT(instanceCreated(DWORD)), Qt::QueuedConnection);
93 setMinimumSize(size());
94 splitter->setSizes(QList<int>() << 16 << 196);
97 labelBuildDate->setText(tr("Built on %1 at %2").arg(x264_version_date().toString(Qt::ISODate), QString::fromLatin1(x264_version_time())));
98 labelBuildDate->installEventFilter(this);
99 setWindowTitle(QString("%1 (%2 Mode)").arg(windowTitle(), m_cpuFeatures->x64 ? "64-Bit" : "32-Bit"));
102 setWindowTitle(QString("%1 | !!! DEBUG VERSION !!!").arg(windowTitle()));
103 setStyleSheet("QMenuBar, QMainWindow { background-color: yellow }");
105 else if(x264_is_prerelease())
107 setWindowTitle(QString("%1 | PRE-RELEASE VERSION").arg(windowTitle()));
111 m_jobList = new JobListModel();
112 connect(m_jobList, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this, SLOT(jobChangedData(QModelIndex, QModelIndex)));
113 jobsView->setModel(m_jobList);
116 jobsView->horizontalHeader()->setSectionHidden(3, true);
117 jobsView->horizontalHeader()->setResizeMode(0, QHeaderView::Stretch);
118 jobsView->horizontalHeader()->setResizeMode(1, QHeaderView::Fixed);
119 jobsView->horizontalHeader()->setResizeMode(2, QHeaderView::Fixed);
120 jobsView->horizontalHeader()->resizeSection(1, 150);
121 jobsView->horizontalHeader()->resizeSection(2, 90);
122 jobsView->verticalHeader()->setResizeMode(QHeaderView::ResizeToContents);
123 connect(jobsView->selectionModel(), SIGNAL(currentChanged(QModelIndex, QModelIndex)), this, SLOT(jobSelected(QModelIndex, QModelIndex)));
125 //Create context menu
126 QAction *actionClipboard = new QAction(QIcon(":/buttons/page_paste.png"), tr("Copy to Clipboard"), logView);
127 actionClipboard->setEnabled(false);
128 logView->addAction(actionClipboard);
129 connect(actionClipboard, SIGNAL(triggered(bool)), this, SLOT(copyLogToClipboard(bool)));
130 jobsView->addActions(menuJob->actions());
133 connect(buttonAddJob, SIGNAL(clicked()), this, SLOT(addButtonPressed()));
134 connect(buttonStartJob, SIGNAL(clicked()), this, SLOT(startButtonPressed()));
135 connect(buttonAbortJob, SIGNAL(clicked()), this, SLOT(abortButtonPressed()));
136 connect(buttonPauseJob, SIGNAL(toggled(bool)), this, SLOT(pauseButtonPressed(bool)));
137 connect(actionJob_Delete, SIGNAL(triggered()), this, SLOT(deleteButtonPressed()));
138 connect(actionJob_Restart, SIGNAL(triggered()), this, SLOT(restartButtonPressed()));
139 connect(actionJob_Browse, SIGNAL(triggered()), this, SLOT(browseButtonPressed()));
142 connect(actionAbout, SIGNAL(triggered()), this, SLOT(showAbout()));
143 connect(actionWebMulder, SIGNAL(triggered()), this, SLOT(showWebLink()));
144 connect(actionWebX264, SIGNAL(triggered()), this, SLOT(showWebLink()));
145 connect(actionWebKomisar, SIGNAL(triggered()), this, SLOT(showWebLink()));
146 connect(actionWebJarod, SIGNAL(triggered()), this, SLOT(showWebLink()));
147 connect(actionWebJEEB, SIGNAL(triggered()), this, SLOT(showWebLink()));
148 connect(actionWebAvisynth32, SIGNAL(triggered()), this, SLOT(showWebLink()));
149 connect(actionWebAvisynth64, SIGNAL(triggered()), this, SLOT(showWebLink()));
150 connect(actionWebWiki, SIGNAL(triggered()), this, SLOT(showWebLink()));
151 connect(actionWebBluRay, SIGNAL(triggered()), this, SLOT(showWebLink()));
152 connect(actionWebAvsWiki, SIGNAL(triggered()), this, SLOT(showWebLink()));
153 connect(actionWebSecret, SIGNAL(triggered()), this, SLOT(showWebLink()));
154 connect(actionWebSupport, SIGNAL(triggered()), this, SLOT(showWebLink()));
155 connect(actionPreferences, SIGNAL(triggered()), this, SLOT(showPreferences()));
157 //Create floating label
158 m_label = new QLabel(jobsView->viewport());
159 m_label->setText(tr("No job created yet. Please click the 'Add New Job' button!"));
160 m_label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
161 SET_TEXT_COLOR(m_label, Qt::darkGray);
162 SET_FONT_BOLD(m_label, true);
163 m_label->setVisible(true);
164 m_label->setContextMenuPolicy(Qt::ActionsContextMenu);
165 m_label->addActions(jobsView->actions());
166 connect(splitter, SIGNAL(splitterMoved(int, int)), this, SLOT(updateLabelPos()));
173 MainWindow::~MainWindow(void)
175 OptionsModel::saveTemplate(m_options, QString::fromLatin1(tpl_last));
177 X264_DELETE(m_jobList);
178 X264_DELETE(m_options);
179 X264_DELETE(m_droppedFiles);
180 X264_DELETE(m_label);
182 while(!m_toolsList.isEmpty())
184 QFile *temp = m_toolsList.takeFirst();
188 if(m_ipcThread->isRunning())
190 m_ipcThread->setAbort();
191 if(!m_ipcThread->wait(5000))
193 m_ipcThread->terminate();
198 X264_DELETE(m_ipcThread);
203 X264_DELETE(m_avsLib);
207 ///////////////////////////////////////////////////////////////////////////////
209 ///////////////////////////////////////////////////////////////////////////////
212 * The "add" button was clicked
214 void MainWindow::addButtonPressed(const QString &filePathIn, const QString &filePathOut, OptionsModel *options, int fileNo, int fileTotal, bool *ok)
216 qDebug("MainWindow::addButtonPressed");
220 AddJobDialog *addDialog = new AddJobDialog(this, options ? options : m_options, m_cpuFeatures->x64, m_preferences.use10BitEncoding);
221 addDialog->setRunImmediately(countRunningJobs() < (m_preferences.autoRunNextJob ? m_preferences.maxRunningJobCount : 1));
223 if(options) addDialog->setWindowTitle(tr("Restart Job"));
224 if((fileNo >= 0) && (fileTotal > 1)) addDialog->setWindowTitle(addDialog->windowTitle().append(tr(" (File %1 of %2)").arg(QString::number(fileNo+1), QString::number(fileTotal))));
225 if(!filePathIn.isEmpty()) addDialog->setSourceFile(filePathIn);
226 if(!filePathOut.isEmpty()) addDialog->setOutputFile(filePathOut);
228 int result = addDialog->exec();
229 if(result == QDialog::Accepted)
231 EncodeThread *thrd = new EncodeThread
233 addDialog->sourceFile(),
234 addDialog->outputFile(),
235 options ? options : m_options,
236 QString("%1/toolset").arg(m_appDir),
238 m_preferences.use10BitEncoding,
239 m_cpuFeatures->x64 && m_preferences.useAvisyth64Bit
242 QModelIndex newIndex = m_jobList->insertJob(thrd);
244 if(newIndex.isValid())
246 if(addDialog->runImmediately())
248 jobsView->selectRow(newIndex.row());
249 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
250 m_jobList->startJob(newIndex);
256 m_label->setVisible(m_jobList->rowCount(QModelIndex()) == 0);
259 X264_DELETE(addDialog);
263 * The "start" button was clicked
265 void MainWindow::startButtonPressed(void)
267 m_jobList->startJob(jobsView->currentIndex());
271 * The "abort" button was clicked
273 void MainWindow::abortButtonPressed(void)
275 m_jobList->abortJob(jobsView->currentIndex());
279 * The "delete" button was clicked
281 void MainWindow::deleteButtonPressed(void)
283 m_jobList->deleteJob(jobsView->currentIndex());
284 m_label->setVisible(m_jobList->rowCount(QModelIndex()) == 0);
288 * The "browse" button was clicked
290 void MainWindow::browseButtonPressed(void)
292 QString outputFile = m_jobList->getJobOutputFile(jobsView->currentIndex());
293 if((!outputFile.isEmpty()) && QFileInfo(outputFile).exists() && QFileInfo(outputFile).isFile())
295 QProcess::startDetached(QString::fromLatin1("explorer.exe"), QStringList() << QString::fromLatin1("/select,") << QDir::toNativeSeparators(outputFile), QFileInfo(outputFile).path());
299 QMessageBox::warning(this, tr("Not Found"), tr("Sorry, the output file could not be found!"));
304 * The "pause" button was clicked
306 void MainWindow::pauseButtonPressed(bool checked)
310 m_jobList->pauseJob(jobsView->currentIndex());
314 m_jobList->resumeJob(jobsView->currentIndex());
319 * The "restart" button was clicked
321 void MainWindow::restartButtonPressed(void)
323 const QModelIndex index = jobsView->currentIndex();
325 const QString &source = m_jobList->getJobSourceFile(index);
326 const QString &output = m_jobList->getJobOutputFile(index);
327 const OptionsModel *options = m_jobList->getJobOptions(index);
329 if((options) && (!source.isEmpty()) && (!output.isEmpty()))
331 OptionsModel *tempOptions = new OptionsModel(*options);
332 addButtonPressed(source, output, tempOptions);
333 X264_DELETE(tempOptions);
338 * Job item selected by user
340 void MainWindow::jobSelected(const QModelIndex & current, const QModelIndex & previous)
342 qDebug("Job selected: %d", current.row());
346 disconnect(logView->model(), SIGNAL(rowsInserted(QModelIndex, int, int)), this, SLOT(jobLogExtended(QModelIndex, int, int)));
349 if(current.isValid())
351 logView->setModel(m_jobList->getLogFile(current));
352 connect(logView->model(), SIGNAL(rowsInserted(QModelIndex, int, int)), this, SLOT(jobLogExtended(QModelIndex, int, int)));
353 logView->actions().first()->setEnabled(true);
354 QTimer::singleShot(0, logView, SLOT(scrollToBottom()));
356 progressBar->setValue(m_jobList->getJobProgress(current));
357 editDetails->setText(m_jobList->data(m_jobList->index(current.row(), 3, QModelIndex()), Qt::DisplayRole).toString());
358 updateButtons(m_jobList->getJobStatus(current));
359 updateTaskbar(m_jobList->getJobStatus(current), m_jobList->data(m_jobList->index(current.row(), 0, QModelIndex()), Qt::DecorationRole).value<QIcon>());
363 logView->setModel(NULL);
364 logView->actions().first()->setEnabled(false);
365 progressBar->setValue(0);
366 editDetails->clear();
367 updateButtons(EncodeThread::JobStatus_Undefined);
368 updateTaskbar(EncodeThread::JobStatus_Undefined, QIcon());
371 progressBar->repaint();
375 * Handle update of job info (status, progress, details, etc)
377 void MainWindow::jobChangedData(const QModelIndex &topLeft, const QModelIndex &bottomRight)
379 int selected = jobsView->currentIndex().row();
381 if(topLeft.column() <= 1 && bottomRight.column() >= 1) /*STATUS*/
383 for(int i = topLeft.row(); i <= bottomRight.row(); i++)
385 EncodeThread::JobStatus status = m_jobList->getJobStatus(m_jobList->index(i, 0, QModelIndex()));
388 qDebug("Current job changed status!");
389 updateButtons(status);
390 updateTaskbar(status, m_jobList->data(m_jobList->index(i, 0, QModelIndex()), Qt::DecorationRole).value<QIcon>());
392 if((status == EncodeThread::JobStatus_Completed) || (status == EncodeThread::JobStatus_Failed))
394 if(m_preferences.autoRunNextJob) QTimer::singleShot(0, this, SLOT(launchNextJob()));
395 if(m_preferences.shutdownComputer) QTimer::singleShot(0, this, SLOT(shutdownComputer()));
396 if(m_preferences.saveLogFiles) saveLogFile(m_jobList->index(i, 1, QModelIndex()));
400 if(topLeft.column() <= 2 && bottomRight.column() >= 2) /*PROGRESS*/
402 for(int i = topLeft.row(); i <= bottomRight.row(); i++)
406 progressBar->setValue(m_jobList->getJobProgress(m_jobList->index(i, 0, QModelIndex())));
407 WinSevenTaskbar::setTaskbarProgress(this, progressBar->value(), progressBar->maximum());
412 if(topLeft.column() <= 3 && bottomRight.column() >= 3) /*DETAILS*/
414 for(int i = topLeft.row(); i <= bottomRight.row(); i++)
418 editDetails->setText(m_jobList->data(m_jobList->index(i, 3, QModelIndex()), Qt::DisplayRole).toString());
426 * Handle new log file content
428 void MainWindow::jobLogExtended(const QModelIndex & parent, int start, int end)
430 QTimer::singleShot(0, logView, SLOT(scrollToBottom()));
436 void MainWindow::showAbout(void)
440 text += QString().sprintf("<nobr><tt>Simple x264 Launcher v%u.%02u.%u - use 64-Bit x264 with 32-Bit Avisynth<br>", x264_version_major(), x264_version_minor(), x264_version_build());
441 text += QString().sprintf("Copyright (c) 2004-%04d LoRd_MuldeR <mulder2@gmx.de>. Some rights reserved.<br>", qMax(x264_version_date().year(),QDate::currentDate().year()));
442 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());
443 text += QString().sprintf("This program is free software: you can redistribute it and/or modify<br>");
444 text += QString().sprintf("it under the terms of the GNU General Public License <http://www.gnu.org/>.<br>");
445 text += QString().sprintf("Note that this program is distributed with ABSOLUTELY NO WARRANTY.<br><br>");
446 text += QString().sprintf("Please check the web-site at <a href=\"%s\">%s</a> for updates !!!<br></tt></nobr>", home_url, home_url);
448 QMessageBox aboutBox(this);
449 aboutBox.setIconPixmap(QIcon(":/images/movie.png").pixmap(64,64));
450 aboutBox.setWindowTitle(tr("About..."));
451 aboutBox.setText(text.replace("-", "−"));
452 aboutBox.addButton(tr("About x264"), QMessageBox::NoRole);
453 aboutBox.addButton(tr("About AVS"), QMessageBox::NoRole);
454 aboutBox.addButton(tr("About Qt"), QMessageBox::NoRole);
455 aboutBox.setEscapeButton(aboutBox.addButton(tr("Close"), QMessageBox::NoRole));
459 MessageBeep(MB_ICONINFORMATION);
460 switch(aboutBox.exec())
465 text2 += tr("<nobr><tt>x264 - the best H.264/AVC encoder. Copyright (c) 2003-2012 x264 project.<br>");
466 text2 += tr("Free software library for encoding video streams into the H.264/MPEG-4 AVC format.<br>");
467 text2 += tr("Released under the terms of the GNU General Public License.<br><br>");
468 text2 += tr("Please visit <a href=\"%1\">%1</a> for obtaining a commercial x264 license.<br>").arg("http://x264licensing.com/");
469 text2 += tr("Read the <a href=\"%1\">user's manual</a> to get started and use the <a href=\"%2\">support forum</a> for help!<br></tt></nobr>").arg("http://mewiki.project357.com/wiki/X264_Settings", "http://forum.doom9.org/forumdisplay.php?f=77");
471 QMessageBox x264Box(this);
472 x264Box.setIconPixmap(QIcon(":/images/x264.png").pixmap(48,48));
473 x264Box.setWindowTitle(tr("About x264"));
474 x264Box.setText(text2.replace("-", "−"));
475 x264Box.setEscapeButton(x264Box.addButton(tr("Close"), QMessageBox::NoRole));
476 MessageBeep(MB_ICONINFORMATION);
483 text2 += tr("<nobr><tt>Avisynth - powerful video processing scripting language.<br>");
484 text2 += tr("Copyright (c) 2000 Ben Rudiak-Gould and all subsequent developers.<br>");
485 text2 += tr("Released under the terms of the GNU General Public License.<br><br>");
486 text2 += tr("Please visit the web-site <a href=\"%1\">%1</a> for more information.<br>").arg("http://avisynth.org/");
487 text2 += tr("Read the <a href=\"%1\">guide</a> to get started and use the <a href=\"%2\">support forum</a> for help!<br></tt></nobr>").arg("http://avisynth.org/mediawiki/First_script", "http://forum.doom9.org/forumdisplay.php?f=33");
489 QMessageBox x264Box(this);
490 x264Box.setIconPixmap(QIcon(":/images/avisynth.png").pixmap(48,67));
491 x264Box.setWindowTitle(tr("About Avisynth"));
492 x264Box.setText(text2.replace("-", "−"));
493 x264Box.setEscapeButton(x264Box.addButton(tr("Close"), QMessageBox::NoRole));
494 MessageBeep(MB_ICONINFORMATION);
499 QMessageBox::aboutQt(this);
510 void MainWindow::showWebLink(void)
512 if(QObject::sender() == actionWebMulder) QDesktopServices::openUrl(QUrl(home_url));
513 if(QObject::sender() == actionWebX264) QDesktopServices::openUrl(QUrl("http://www.x264.com/"));
514 if(QObject::sender() == actionWebKomisar) QDesktopServices::openUrl(QUrl("http://komisar.gin.by/"));
515 if(QObject::sender() == actionWebJarod) QDesktopServices::openUrl(QUrl("http://www.x264.nl/"));
516 if(QObject::sender() == actionWebJEEB) QDesktopServices::openUrl(QUrl("http://x264.fushizen.eu/"));
517 if(QObject::sender() == actionWebAvisynth32) QDesktopServices::openUrl(QUrl("http://sourceforge.net/projects/avisynth2/files/AviSynth%202.5/"));
518 if(QObject::sender() == actionWebAvisynth64) QDesktopServices::openUrl(QUrl("http://code.google.com/p/avisynth64/downloads/list"));
519 if(QObject::sender() == actionWebWiki) QDesktopServices::openUrl(QUrl("http://mewiki.project357.com/wiki/X264_Settings"));
520 if(QObject::sender() == actionWebBluRay) QDesktopServices::openUrl(QUrl("http://www.x264bluray.com/"));
521 if(QObject::sender() == actionWebAvsWiki) QDesktopServices::openUrl(QUrl("http://avisynth.org/mediawiki/Main_Page#Usage"));
522 if(QObject::sender() == actionWebSupport) QDesktopServices::openUrl(QUrl("http://forum.doom9.org/showthread.php?t=144140"));
523 if(QObject::sender() == actionWebSecret) QDesktopServices::openUrl(QUrl("http://www.youtube.com/watch_popup?v=AXIeHY-OYNI"));
527 * Pereferences dialog
529 void MainWindow::showPreferences(void)
531 PreferencesDialog *preferences = new PreferencesDialog(this, &m_preferences, m_cpuFeatures->x64);
533 X264_DELETE(preferences);
537 * Launch next job, after running job has finished
539 void MainWindow::launchNextJob(void)
541 qDebug("launchNextJob(void)");
543 const int rows = m_jobList->rowCount(QModelIndex());
545 if(countRunningJobs() >= m_preferences.maxRunningJobCount)
547 qDebug("Still have too many jobs running, won't launch next one yet!");
551 int startIdx= jobsView->currentIndex().isValid() ? qBound(0, jobsView->currentIndex().row(), rows-1) : 0;
553 for(int i = 0; i < rows; i++)
555 int currentIdx = (i + startIdx) % rows;
556 EncodeThread::JobStatus status = m_jobList->getJobStatus(m_jobList->index(currentIdx, 0, QModelIndex()));
557 if(status == EncodeThread::JobStatus_Enqueued)
559 if(m_jobList->startJob(m_jobList->index(currentIdx, 0, QModelIndex())))
561 jobsView->selectRow(currentIdx);
567 qWarning("No enqueued jobs left!");
571 * Save log to text file
573 void MainWindow::saveLogFile(const QModelIndex &index)
577 if(LogFileModel *log = m_jobList->getLogFile(index))
579 QDir(QString("%1/logs").arg(x264_data_path())).mkpath(".");
580 QString logFilePath = QString("%1/logs/LOG.%2.%3.txt").arg(x264_data_path(), QDate::currentDate().toString(Qt::ISODate), QTime::currentTime().toString(Qt::ISODate).replace(':', "-"));
581 QFile outFile(logFilePath);
582 if(outFile.open(QIODevice::WriteOnly))
584 QTextStream outStream(&outFile);
585 outStream.setCodec("UTF-8");
586 outStream.setGenerateByteOrderMark(true);
588 const int rows = log->rowCount(QModelIndex());
589 for(int i = 0; i < rows; i++)
591 outStream << log->data(log->index(i, 0, QModelIndex()), Qt::DisplayRole).toString() << QLatin1String("\r\n");
597 qWarning("Failed to open log file for writing:\n%s", logFilePath.toUtf8().constData());
604 * Shut down the computer (with countdown)
606 void MainWindow::shutdownComputer(void)
608 qDebug("shutdownComputer(void)");
610 if(countPendingJobs() > 0)
612 qDebug("Still have pending jobs, won't shutdown yet!");
616 const int iTimeout = 30;
617 const Qt::WindowFlags flags = Qt::WindowStaysOnTopHint | Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::MSWindowsFixedSizeDialogHint | Qt::WindowSystemMenuHint;
618 const QString text = QString("%1%2%1").arg(QString().fill(' ', 18), tr("Warning: Computer will shutdown in %1 seconds..."));
620 qWarning("Initiating shutdown sequence!");
622 QProgressDialog progressDialog(text.arg(iTimeout), tr("Cancel Shutdown"), 0, iTimeout + 1, this, flags);
623 QPushButton *cancelButton = new QPushButton(tr("Cancel Shutdown"), &progressDialog);
624 cancelButton->setIcon(QIcon(":/buttons/power_on.png"));
625 progressDialog.setModal(true);
626 progressDialog.setAutoClose(false);
627 progressDialog.setAutoReset(false);
628 progressDialog.setWindowIcon(QIcon(":/buttons/power_off.png"));
629 progressDialog.setWindowTitle(windowTitle());
630 progressDialog.setCancelButton(cancelButton);
631 progressDialog.show();
633 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
634 QApplication::setOverrideCursor(Qt::WaitCursor);
635 PlaySound(MAKEINTRESOURCE(IDR_WAVE1), GetModuleHandle(NULL), SND_RESOURCE | SND_SYNC);
636 QApplication::restoreOverrideCursor();
639 timer.setInterval(1000);
642 QEventLoop eventLoop(this);
643 connect(&timer, SIGNAL(timeout()), &eventLoop, SLOT(quit()));
644 connect(&progressDialog, SIGNAL(canceled()), &eventLoop, SLOT(quit()));
646 for(int i = 1; i <= iTimeout; i++)
649 if(progressDialog.wasCanceled())
651 progressDialog.close();
654 progressDialog.setValue(i+1);
655 progressDialog.setLabelText(text.arg(iTimeout-i));
656 if(iTimeout-i == 3) progressDialog.setCancelButton(NULL);
657 QApplication::processEvents();
658 PlaySound(MAKEINTRESOURCE((i < iTimeout) ? IDR_WAVE2 : IDR_WAVE3), GetModuleHandle(NULL), SND_RESOURCE | SND_SYNC);
661 qWarning("Shutting down !!!");
663 if(x264_shutdown_computer("Simple x264 Launcher: All jobs completed, shutting down!", 10, true))
665 qApp->closeAllWindows();
670 * Main initialization function (called only once!)
672 void MainWindow::init(void)
674 static const char *binFiles = "x264_8bit_x86.exe:x264_8bit_x64.exe:x264_10bit_x86.exe:x264_10bit_x64.exe:avs2yuv_x86.exe:avs2yuv_x64.exe";
675 QStringList binaries = QString::fromLatin1(binFiles).split(":", QString::SkipEmptyParts);
679 //Check for a running instance
680 bool firstInstance = false;
681 if(m_ipcThread->initialize(&firstInstance))
683 m_ipcThread->start();
686 if(!m_ipcThread->wait(5000))
688 QMessageBox::warning(this, tr("Not Responding"), tr("<nobr>Another instance of this application is already running, but did not respond in time.<br>If the problem persists, please kill the running instance from the task manager!</nobr>"), tr("Quit"));
689 m_ipcThread->terminate();
692 close(); qApp->exit(-1); return;
697 while(!binaries.isEmpty())
699 qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
700 QString current = binaries.takeFirst();
701 QFile *file = new QFile(QString("%1/toolset/%2").arg(m_appDir, current));
702 if(file->open(QIODevice::ReadOnly))
704 bool binaryTypeOkay = false;
706 if(GetBinaryType(QWCHAR(file->fileName()), &binaryType))
708 binaryTypeOkay = (binaryType == SCS_32BIT_BINARY || binaryType == SCS_64BIT_BINARY);
712 QMessageBox::critical(this, tr("Invalid File!"), tr("<nobr>At least on required tool is not a valid Win32 or Win64 binary:<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("-", "−"));
713 qFatal(QString("Binary is invalid: %1/toolset/%2").arg(m_appDir, current).toLatin1().constData());
714 close(); qApp->exit(-1); return;
721 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("-", "−"));
722 qFatal(QString("Binary not found: %1/toolset/%2").arg(m_appDir, current).toLatin1().constData());
723 close(); qApp->exit(-1); return;
727 //Check for portable mode
731 static const char *data = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
732 QFile writeTest(QString("%1/%2").arg(x264_data_path(), QUuid::createUuid().toString()));
733 if(writeTest.open(QIODevice::WriteOnly))
735 ok = (writeTest.write(data) == strlen(data));
740 int val = QMessageBox::warning(this, tr("Write Test Failed"), tr("<nobr>The application was launched in portable mode, but the program path is <b>not</b> writable!</nobr>"), tr("Quit"), tr("Ignore"));
741 if(val != 1) { close(); qApp->exit(-1); return; }
746 if(x264_is_prerelease())
748 qsrand(time(NULL)); int rnd = qrand() % 3;
749 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);
750 if(rnd != val) { close(); qApp->exit(-1); return; }
753 //Make sure this CPU can run x264 (requires MMX + MMXEXT/iSSE to run x264 with ASM enabled, additionally requires SSE1 for most x264 builds)
754 if(!(m_cpuFeatures->mmx && m_cpuFeatures->mmx2))
756 QMessageBox::critical(this, tr("Unsupported CPU"), tr("<nobr>Sorry, but this machine is <b>not</b> physically capable of running x264 (with assembly).<br>Please get a CPU that supports at least the MMX and MMXEXT instruction sets!</nobr>"), tr("Quit"));
757 qFatal("System does not support MMX and MMXEXT, x264 will not work !!!");
758 close(); qApp->exit(-1); return;
760 else if(!(m_cpuFeatures->mmx && m_cpuFeatures->sse))
762 qWarning("WARNING: System does not support SSE1, most x264 builds will not work !!!\n");
763 int val = QMessageBox::warning(this, tr("Unsupported CPU"), tr("<nobr>It appears that this machine does <b>not</b> support the SSE1 instruction set.<br>Thus most builds of x264 will <b>not</b> run on this computer at all.<br><br>Please get a CPU that supports the MMX and SSE1 instruction sets!</nobr>"), tr("Quit"), tr("Ignore"));
764 if(val != 1) { close(); qApp->exit(-1); return; }
767 //Check for Avisynth support
768 if(!qApp->arguments().contains("--skip-avisynth-check", Qt::CaseInsensitive))
770 qDebug("[Check for Avisynth support]");
771 double avisynthVersion = 0.0;
774 m_avsLib = new QLibrary("avisynth.dll");
778 avisynthVersion = detectAvisynthVersion(m_avsLib);
779 if(avisynthVersion < 0.0)
781 int val = QMessageBox::critical(this, tr("Avisynth Error"), tr("<nobr>A critical error was encountered while checking your Avisynth version!</nobr>").replace("-", "−"), tr("Quit"), tr("Ignore"));
782 if(val != 1) { close(); qApp->exit(-1); return; }
787 qWarning("Failed to load avisynth.dll libraray!");
789 if(avisynthVersion < 2.5)
791 int val = QMessageBox::warning(this, tr("Avisynth Missing"), tr("<nobr>It appears that Avisynth is <b>not</b> currently installed on your computer.<br>Therefore Avisynth (.avs) input will <b>not</b> 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"));
792 m_avsLib->unload(); X264_DELETE(m_avsLib);
793 if(val != 1) { close(); qApp->exit(-1); return; }
798 //Check for expiration
799 if(x264_version_date().addMonths(6) < QDate::currentDate())
801 QMessageBox msgBox(this);
802 msgBox.setIconPixmap(QIcon(":/images/update.png").pixmap(56,56));
803 msgBox.setWindowTitle(tr("Update Notification"));
804 msgBox.setWindowFlags(Qt::Window | Qt::WindowTitleHint | Qt::CustomizeWindowHint);
805 msgBox.setText(tr("<nobr><tt>Your version of 'Simple x264 Launcher' is more than 6 months old!<br><br>Please download the most recent version from the official web-site at:<br><a href=\"%1\">%1</a><br></tt></nobr>").replace("-", "−").arg(update_url));
806 QPushButton *btn1 = msgBox.addButton(tr("Discard"), QMessageBox::NoRole);
807 QPushButton *btn2 = msgBox.addButton(tr("Discard"), QMessageBox::AcceptRole);
808 btn1->setEnabled(false);
809 btn2->setVisible(false);
810 QTimer::singleShot(5000, btn1, SLOT(hide()));
811 QTimer::singleShot(5000, btn2, SLOT(show()));
815 //Add files from command-line
816 bool bAddFile = false;
817 QStringList files, args = qApp->arguments();
818 while(!args.isEmpty())
820 QString current = args.takeFirst();
823 bAddFile = (current.compare("--add", Qt::CaseInsensitive) == 0);
826 if((!current.startsWith("--")) && QFileInfo(current).exists() && QFileInfo(current).isFile())
828 files << QFileInfo(current).canonicalFilePath();
831 if(int totalFiles = files.count())
833 bool ok = true; int n = 0;
834 while((!files.isEmpty()) && ok)
836 QString currentFile = files.takeFirst();
837 qDebug("Adding file: %s", currentFile.toUtf8().constData());
838 addButtonPressed(currentFile, QString(), NULL, n++, totalFiles, &ok);
844 * Update the label position
846 void MainWindow::updateLabelPos(void)
848 const QWidget *const viewPort = jobsView->viewport();
849 m_label->setGeometry(0, 0, viewPort->width(), viewPort->height());
853 * Copy the complete log to the clipboard
855 void MainWindow::copyLogToClipboard(bool checked)
857 qDebug("copyLogToClipboard");
859 if(LogFileModel *log = dynamic_cast<LogFileModel*>(logView->model()))
861 log->copyToClipboard();
862 MessageBeep(MB_ICONINFORMATION);
867 * Process the dropped files
869 void MainWindow::handleDroppedFiles(void)
871 qDebug("MainWindow::handleDroppedFiles");
874 QStringList droppedFiles(*m_droppedFiles);
875 m_droppedFiles->clear();
876 int totalFiles = droppedFiles.count();
877 bool ok = true; int n = 0;
878 while((!droppedFiles.isEmpty()) && ok)
880 QString currentFile = droppedFiles.takeFirst();
881 qDebug("Adding file: %s", currentFile.toUtf8().constData());
882 addButtonPressed(currentFile, QString(), NULL, n++, totalFiles, &ok);
885 qDebug("Leave from MainWindow::handleDroppedFiles!");
888 void MainWindow::instanceCreated(DWORD pid)
890 qDebug("Notification from other instance (PID=0x%X) received!", pid);
892 FLASHWINFO flashWinInfo;
893 memset(&flashWinInfo, 0, sizeof(FLASHWINFO));
894 flashWinInfo.cbSize = sizeof(FLASHWINFO);
895 flashWinInfo.hwnd = this->winId();
896 flashWinInfo.dwFlags = FLASHW_ALL;
897 flashWinInfo.dwTimeout = 125;
898 flashWinInfo.uCount = 5;
900 SwitchToThisWindow(this->winId(), TRUE);
901 SetForegroundWindow(this->winId());
902 qApp->processEvents();
903 FlashWindowEx(&flashWinInfo);
906 ///////////////////////////////////////////////////////////////////////////////
908 ///////////////////////////////////////////////////////////////////////////////
913 void MainWindow::showEvent(QShowEvent *e)
915 QMainWindow::showEvent(e);
920 QTimer::singleShot(0, this, SLOT(init()));
927 void MainWindow::closeEvent(QCloseEvent *e)
929 if(countRunningJobs() > 0)
932 QMessageBox::warning(this, tr("Jobs Are Running"), tr("Sorry, can not exit while there still are running jobs!"));
936 if(countPendingJobs() > 0)
938 int ret = QMessageBox::question(this, tr("Jobs Are Pending"), tr("Do you really want to quit and discard the pending jobs?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
939 if(ret != QMessageBox::Yes)
946 while(m_jobList->rowCount(QModelIndex()) > 0)
948 qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
949 if(!m_jobList->deleteJob(m_jobList->index(0, 0, QModelIndex())))
952 QMessageBox::warning(this, tr("Failed To Exit"), tr("Sorry, at least one job could not be deleted!"));
957 qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
958 QMainWindow::closeEvent(e);
962 * Window resize event
964 void MainWindow::resizeEvent(QResizeEvent *e)
966 QMainWindow::resizeEvent(e);
973 bool MainWindow::eventFilter(QObject *o, QEvent *e)
975 if((o == labelBuildDate) && (e->type() == QEvent::MouseButtonPress))
977 QTimer::singleShot(0, this, SLOT(showAbout()));
984 * Win32 message filter
986 bool MainWindow::winEvent(MSG *message, long *result)
988 return WinSevenTaskbar::handleWinEvent(message, result);
992 * File dragged over window
994 void MainWindow::dragEnterEvent(QDragEnterEvent *event)
996 QStringList formats = event->mimeData()->formats();
998 if(formats.contains("application/x-qt-windows-mime;value=\"FileNameW\"", Qt::CaseInsensitive) && formats.contains("text/uri-list", Qt::CaseInsensitive))
1000 event->acceptProposedAction();
1005 * File dropped onto window
1007 void MainWindow::dropEvent(QDropEvent *event)
1009 QStringList droppedFiles;
1010 QList<QUrl> urls = event->mimeData()->urls();
1012 while(!urls.isEmpty())
1014 QUrl currentUrl = urls.takeFirst();
1015 QFileInfo file(currentUrl.toLocalFile());
1016 if(file.exists() && file.isFile())
1018 qDebug("MainWindow::dropEvent: %s", file.canonicalFilePath().toUtf8().constData());
1019 droppedFiles << file.canonicalFilePath();
1023 if(droppedFiles.count() > 0)
1027 m_droppedFiles = new QStringList();
1029 m_droppedFiles->append(droppedFiles);
1030 m_droppedFiles->sort();
1031 QTimer::singleShot(0, this, SLOT(handleDroppedFiles()));
1035 ///////////////////////////////////////////////////////////////////////////////
1036 // Private functions
1037 ///////////////////////////////////////////////////////////////////////////////
1040 * Jobs that are not completed (or failed, or aborted) yet
1042 unsigned int MainWindow::countPendingJobs(void)
1044 unsigned int count = 0;
1045 const int rows = m_jobList->rowCount(QModelIndex());
1047 for(int i = 0; i < rows; i++)
1049 EncodeThread::JobStatus status = m_jobList->getJobStatus(m_jobList->index(i, 0, QModelIndex()));
1050 if(status != EncodeThread::JobStatus_Completed && status != EncodeThread::JobStatus_Aborted && status != EncodeThread::JobStatus_Failed)
1060 * Jobs that are still active, i.e. not terminated or enqueued
1062 unsigned int MainWindow::countRunningJobs(void)
1064 unsigned int count = 0;
1065 const int rows = m_jobList->rowCount(QModelIndex());
1067 for(int i = 0; i < rows; i++)
1069 EncodeThread::JobStatus status = m_jobList->getJobStatus(m_jobList->index(i, 0, QModelIndex()));
1070 if(status != EncodeThread::JobStatus_Completed && status != EncodeThread::JobStatus_Aborted && status != EncodeThread::JobStatus_Failed && status != EncodeThread::JobStatus_Enqueued)
1080 * Update all buttons with respect to current job status
1082 void MainWindow::updateButtons(EncodeThread::JobStatus status)
1084 qDebug("MainWindow::updateButtons(void)");
1086 buttonStartJob->setEnabled(status == EncodeThread::JobStatus_Enqueued);
1087 buttonAbortJob->setEnabled(status == EncodeThread::JobStatus_Indexing || status == EncodeThread::JobStatus_Running || status == EncodeThread::JobStatus_Running_Pass1 || status == EncodeThread::JobStatus_Running_Pass2 || status == EncodeThread::JobStatus_Paused);
1088 buttonPauseJob->setEnabled(status == EncodeThread::JobStatus_Indexing || status == EncodeThread::JobStatus_Running || status == EncodeThread::JobStatus_Paused || status == EncodeThread::JobStatus_Running_Pass1 || status == EncodeThread::JobStatus_Running_Pass2);
1089 buttonPauseJob->setChecked(status == EncodeThread::JobStatus_Paused || status == EncodeThread::JobStatus_Pausing);
1091 actionJob_Delete->setEnabled(status == EncodeThread::JobStatus_Completed || status == EncodeThread::JobStatus_Aborted || status == EncodeThread::JobStatus_Failed || status == EncodeThread::JobStatus_Enqueued);
1092 actionJob_Restart->setEnabled(status == EncodeThread::JobStatus_Completed || status == EncodeThread::JobStatus_Aborted || status == EncodeThread::JobStatus_Failed || status == EncodeThread::JobStatus_Enqueued);
1093 actionJob_Browse->setEnabled(status == EncodeThread::JobStatus_Completed);
1095 actionJob_Start->setEnabled(buttonStartJob->isEnabled());
1096 actionJob_Abort->setEnabled(buttonAbortJob->isEnabled());
1097 actionJob_Pause->setEnabled(buttonPauseJob->isEnabled());
1098 actionJob_Pause->setChecked(buttonPauseJob->isChecked());
1100 editDetails->setEnabled(status != EncodeThread::JobStatus_Paused);
1104 * Update the taskbar with current job status
1106 void MainWindow::updateTaskbar(EncodeThread::JobStatus status, const QIcon &icon)
1108 qDebug("MainWindow::updateTaskbar(void)");
1112 case EncodeThread::JobStatus_Undefined:
1113 WinSevenTaskbar::setTaskbarState(this, WinSevenTaskbar::WinSevenTaskbarNoState);
1115 case EncodeThread::JobStatus_Aborting:
1116 case EncodeThread::JobStatus_Starting:
1117 case EncodeThread::JobStatus_Pausing:
1118 case EncodeThread::JobStatus_Resuming:
1119 WinSevenTaskbar::setTaskbarState(this, WinSevenTaskbar::WinSevenTaskbarIndeterminateState);
1121 case EncodeThread::JobStatus_Aborted:
1122 case EncodeThread::JobStatus_Failed:
1123 WinSevenTaskbar::setTaskbarState(this, WinSevenTaskbar::WinSevenTaskbarErrorState);
1125 case EncodeThread::JobStatus_Paused:
1126 WinSevenTaskbar::setTaskbarState(this, WinSevenTaskbar::WinSevenTaskbarPausedState);
1129 WinSevenTaskbar::setTaskbarState(this, WinSevenTaskbar::WinSevenTaskbarNormalState);
1135 case EncodeThread::JobStatus_Aborting:
1136 case EncodeThread::JobStatus_Starting:
1137 case EncodeThread::JobStatus_Pausing:
1138 case EncodeThread::JobStatus_Resuming:
1141 WinSevenTaskbar::setTaskbarProgress(this, progressBar->value(), progressBar->maximum());
1145 WinSevenTaskbar::setOverlayIcon(this, icon.isNull() ? NULL : &icon);
1149 * Detect Avisynth version
1151 double MainWindow::detectAvisynthVersion(QLibrary *avsLib)
1153 qDebug("detectAvisynthVersion(QLibrary *avsLib)");
1154 double version_number = 0.0;
1158 avs_create_script_environment_func avs_create_script_environment_ptr = (avs_create_script_environment_func) avsLib->resolve("avs_create_script_environment");
1159 avs_invoke_func avs_invoke_ptr = (avs_invoke_func) avsLib->resolve("avs_invoke");
1160 avs_function_exists_func avs_function_exists_ptr = (avs_function_exists_func) avsLib->resolve("avs_function_exists");
1161 avs_delete_script_environment_func avs_delete_script_environment_ptr = (avs_delete_script_environment_func) avsLib->resolve("avs_delete_script_environment");
1162 avs_release_value_func avs_release_value_ptr = (avs_release_value_func) avsLib->resolve("avs_release_value");
1164 if((avs_create_script_environment_ptr != NULL) && (avs_invoke_ptr != NULL) && (avs_function_exists_ptr != NULL))
1166 qDebug("avs_create_script_environment_ptr(AVS_INTERFACE_25)");
1167 AVS_ScriptEnvironment* avs_env = avs_create_script_environment_ptr(AVS_INTERFACE_25);
1170 qDebug("avs_function_exists_ptr(avs_env, \"VersionNumber\")");
1171 if(avs_function_exists_ptr(avs_env, "VersionNumber"))
1173 qDebug("avs_invoke_ptr(avs_env, \"VersionNumber\", avs_new_value_array(NULL, 0), NULL)");
1174 AVS_Value avs_version = avs_invoke_ptr(avs_env, "VersionNumber", avs_new_value_array(NULL, 0), NULL);
1175 if(!avs_is_error(avs_version))
1177 if(avs_is_float(avs_version))
1179 qDebug("Avisynth version: v%.2f", avs_as_float(avs_version));
1180 version_number = avs_as_float(avs_version);
1181 if(avs_release_value_ptr) avs_release_value_ptr(avs_version);
1185 qWarning("Failed to determine version number, Avisynth didn't return a float!");
1190 qWarning("Failed to determine version number, Avisynth returned an error!");
1195 qWarning("The 'VersionNumber' function does not exist in your Avisynth DLL, can't determine version!");
1197 if(avs_delete_script_environment_ptr != NULL)
1199 avs_delete_script_environment_ptr(avs_env);
1205 qWarning("The Avisynth DLL failed to create the script environment!");
1210 qWarning("It seems the Avisynth DLL is missing required API functions!");
1215 qWarning("Exception in Avisynth initialization code!");
1216 version_number = -1.0;
1219 return version_number;