OSDN Git Service

Added "Pause" support.
[x264-launcher/x264-launcher.git] / src / win_main.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2012 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
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.
9 //
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.
14 //
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.
18 //
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
21
22 #include "win_main.h"
23
24 #include "global.h"
25 #include "model_jobList.h"
26 #include "model_options.h"
27 #include "win_addJob.h"
28
29 #include <QDate>
30 #include <QTimer>
31 #include <QCloseEvent>
32 #include <QMessageBox>
33 #include <QDesktopServices>
34 #include <QUrl>
35 #include <QDir>
36 #include <QLibrary>
37
38 const char *home_url = "http://mulder.brhack.net/";
39
40 #define PRE_RELEASE (0)
41
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); }
44
45 ///////////////////////////////////////////////////////////////////////////////
46 // Constructor & Destructor
47 ///////////////////////////////////////////////////////////////////////////////
48
49 MainWindow::MainWindow(bool x64supported)
50 :
51         m_x64supported(x64supported),
52         m_appDir(QApplication::applicationDirPath()),
53         m_firstShow(true)
54 {
55         //Init the dialog, from the .ui file
56         setupUi(this);
57         setWindowFlags(windowFlags() & (~Qt::WindowMaximizeButtonHint));
58
59         //Register meta types
60         qRegisterMetaType<QUuid>("QUuid");
61         qRegisterMetaType<EncodeThread::JobStatus>("EncodeThread::JobStatus");
62
63         //Freeze minimum size
64         setMinimumSize(size());
65         splitter->setSizes(QList<int>() << 16 << 196);
66
67         //Update title
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()));
72
73
74         //Create model
75         m_jobList = new JobListModel();
76         connect(m_jobList, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this, SLOT(jobChangedData(QModelIndex, QModelIndex)));
77         jobsView->setModel(m_jobList);
78         
79         //Setup view
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)));
88
89         //Create context menu
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());
94
95         //Enable buttons
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)));
100
101         //Enable menu
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()));
109
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()));
120         updateLabel();
121
122         //Create options object
123         m_options = new OptionsModel();
124 }
125
126 MainWindow::~MainWindow(void)
127 {
128         X264_DELETE(m_jobList);
129         X264_DELETE(m_options);
130         X264_DELETE(m_label);
131
132         while(!m_toolsList.isEmpty())
133         {
134                 QFile *temp = m_toolsList.takeFirst();
135                 X264_DELETE(temp);
136         }
137 }
138
139 ///////////////////////////////////////////////////////////////////////////////
140 // Slots
141 ///////////////////////////////////////////////////////////////////////////////
142
143 void MainWindow::addButtonPressed(void)
144 {
145         AddJobDialog *addDialog = new AddJobDialog(this, m_options);
146         addDialog->setRunImmediately(!havePendingJobs());
147         int result = addDialog->exec();
148         
149         if(result == QDialog::Accepted)
150         {
151                 
152                 EncodeThread *thrd = new EncodeThread
153                 (
154                         addDialog->sourceFile(),
155                         addDialog->outputFile(),
156                         m_options,
157                         QString("%1/toolset").arg(m_appDir),
158                         m_x64supported
159                 );
160
161                 QModelIndex newIndex = m_jobList->insertJob(thrd);
162
163                 if(addDialog->runImmediately())
164                 {
165                         jobsView->selectRow(newIndex.row());
166                         QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
167                         m_jobList->startJob(newIndex);
168                 }
169
170                 m_label->setVisible(false);
171         }
172
173         X264_DELETE(addDialog);
174 }
175
176 void MainWindow::startButtonPressed(void)
177 {
178         m_jobList->startJob(jobsView->currentIndex());
179 }
180
181 void MainWindow::abortButtonPressed(void)
182 {
183         m_jobList->abortJob(jobsView->currentIndex());
184 }
185
186 void MainWindow::pauseButtonPressed(bool checked)
187 {
188         if(checked)
189         {
190                 m_jobList->pauseJob(jobsView->currentIndex());
191         }
192         else
193         {
194                 m_jobList->resumeJob(jobsView->currentIndex());
195         }
196 }
197
198 void MainWindow::jobSelected(const QModelIndex & current, const QModelIndex & previous)
199 {
200         qDebug("Job selected: %d", current.row());
201         
202         if(logView->model())
203         {
204                 disconnect(logView->model(), SIGNAL(rowsInserted(QModelIndex, int, int)), this, SLOT(jobLogExtended(QModelIndex, int, int)));
205         }
206         
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()));
210         
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));
214
215         progressBar->repaint();
216 }
217
218 void MainWindow::jobChangedData(const QModelIndex &topLeft, const  QModelIndex &bottomRight)
219 {
220         int selected = jobsView->currentIndex().row();
221         
222         if(topLeft.column() <= 1 && bottomRight.column() >= 1)
223         {
224                 for(int i = topLeft.row(); i <= bottomRight.row(); i++)
225                 {
226                         EncodeThread::JobStatus status =  m_jobList->getJobStatus(m_jobList->index(i, 0, QModelIndex()));
227                         if(i == selected)
228                         {
229                                 qDebug("Current job changed status!");
230                                 updateButtons(status);
231                         }
232                         if(status == EncodeThread::JobStatus_Completed)
233                         {
234                                 QTimer::singleShot(0, this, SLOT(launchNextJob()));
235                         }
236                 }
237         }
238         else if(topLeft.column() <= 2 && bottomRight.column() >= 2)
239         {
240                 for(int i = topLeft.row(); i <= bottomRight.row(); i++)
241                 {
242                         if(i == selected)
243                         {
244                                 progressBar->setValue(m_jobList->getJobProgress(m_jobList->index(i, 0, QModelIndex())));
245                                 break;
246                         }
247                 }
248         }
249         else if(topLeft.column() <= 3 && bottomRight.column() >= 3)
250         {
251                 for(int i = topLeft.row(); i <= bottomRight.row(); i++)
252                 {
253                         if(i == selected)
254                         {
255                                 editDetails->setText(m_jobList->data(m_jobList->index(i, 3, QModelIndex()), Qt::DisplayRole).toString());
256                                 break;
257                         }
258                 }
259         }
260 }
261
262 void MainWindow::jobLogExtended(const QModelIndex & parent, int start, int end)
263 {
264         QTimer::singleShot(0, logView, SLOT(scrollToBottom()));
265 }
266
267 void MainWindow::showAbout(void)
268 {
269         QString text;
270
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 &lt;mulder2@gmx.de&gt;. 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 &lt;http://www.gnu.org/&gt;.<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);
278
279         QMessageBox::information(this, tr("About..."), text.replace("-", "&minus;"), tr("Close"));
280 }
281
282 void MainWindow::showWebLink(void)
283 {
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/"));
290 }
291
292 void MainWindow::launchNextJob(void)
293 {
294         const int rows = m_jobList->rowCount(QModelIndex());
295
296         for(int i = 0; i < rows; i++)
297         {
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)
300                 {
301                         qWarning("Still have a job running, won't launch next yet!");
302                         return;
303                 }
304         }
305
306         for(int i = 0; i < rows; i++)
307         {
308                 EncodeThread::JobStatus status = m_jobList->getJobStatus(m_jobList->index(i, 0, QModelIndex()));
309                 if(status == EncodeThread::JobStatus_Enqueued)
310                 {
311                         m_jobList->startJob(m_jobList->index(i, 0, QModelIndex()));
312                         return;
313                 }
314         }
315
316         qWarning("No enqueued jobs left!");
317 }
318
319 void MainWindow::init(void)
320 {
321         static const char *binFiles = "x264.exe:x264_x64.exe:avs2yuv.exe";
322         QStringList binaries = QString::fromLatin1(binFiles).split(":", QString::SkipEmptyParts);
323
324         updateLabel();
325
326         //Check all binaries
327         while(!binaries.isEmpty())
328         {
329                 QString current = binaries.takeFirst();
330                 QFile *file = new QFile(QString("%1/toolset/%2").arg(m_appDir, current));
331                 if(file->open(QIODevice::ReadOnly))
332                 {
333                         m_toolsList << file;
334                 }
335                 else
336                 {
337                         X264_DELETE(file);
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("-", "&minus;"));
339                         qFatal(QString("Binary not found: %1/toolset/%2").arg(m_appDir, current).toLatin1().constData());
340                         close(); qApp->exit(-1); return;
341                 }
342         }
343
344         //Pre-release popup
345         if(PRE_RELEASE)
346         {
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; }
350         }
351
352         //Check for Avisynth support
353         bool avsAvailable = false;
354         QLibrary *avsLib = new QLibrary("avisynth.dll");
355         if(avsLib->load())
356         {
357                 avsAvailable = (avsLib->resolve("avs_create_script_environment") != NULL);
358         }
359         if(!avsAvailable)
360         {
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("-", "&minus;"), tr("Quit"), tr("Ignore"));
363                 if(val != 1) { close(); qApp->exit(-1); return; }
364         }
365
366         //Check for expiration
367         if(x264_version_date().addMonths(6) < QDate::currentDate())
368         {
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("-", "&minus;").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()));
380                 msgBox.exec();
381         }
382 }
383
384 void MainWindow::updateLabel(void)
385 {
386         m_label->setGeometry(0, 0, jobsView->width(), jobsView->height());
387 }
388
389 void MainWindow::copyLogToClipboard(bool checked)
390 {
391         qDebug("copyLogToClipboard");
392         
393         if(LogFileModel *log = dynamic_cast<LogFileModel*>(logView->model()))
394         {
395                 log->copyToClipboard();
396                 MessageBeep(MB_ICONINFORMATION);
397         }
398 }
399
400 ///////////////////////////////////////////////////////////////////////////////
401 // Event functions
402 ///////////////////////////////////////////////////////////////////////////////
403
404 void MainWindow::showEvent(QShowEvent *e)
405 {
406         QMainWindow::showEvent(e);
407
408         if(m_firstShow)
409         {
410                 m_firstShow = false;
411                 QTimer::singleShot(0, this, SLOT(init()));
412         }
413 }
414
415 void MainWindow::closeEvent(QCloseEvent *e)
416 {
417         if(havePendingJobs())
418         {
419                 e->ignore();
420                 MessageBeep(MB_ICONWARNING);
421                 return;
422         }
423
424         QMainWindow::closeEvent(e);
425 }
426
427 void MainWindow::resizeEvent(QResizeEvent *e)
428 {
429         QMainWindow::resizeEvent(e);
430         updateLabel();
431 }
432
433 bool MainWindow::eventFilter(QObject *o, QEvent *e)
434 {
435         if((o == labelBuildDate) && (e->type() == QEvent::MouseButtonPress))
436         {
437                 QTimer::singleShot(0, this, SLOT(showAbout()));
438                 return true;
439         }
440         return false;
441 }
442
443 ///////////////////////////////////////////////////////////////////////////////
444 // Private functions
445 ///////////////////////////////////////////////////////////////////////////////
446
447 bool MainWindow::havePendingJobs(void)
448 {
449         const int rows = m_jobList->rowCount(QModelIndex());
450
451         for(int i = 0; i < rows; i++)
452         {
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)
455                 {
456                         return true;
457                 }
458         }
459
460         return false;
461 }
462
463 void MainWindow::updateButtons(EncodeThread::JobStatus status)
464 {
465         qDebug("MainWindow::updateButtons(void)");
466
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);
471
472         actionJob_Start->setEnabled(buttonStartJob->isEnabled());
473         actionJob_Abort->setEnabled(buttonAbortJob->isEnabled());
474         actionJob_Pause->setEnabled(buttonPauseJob->isEnabled());
475         actionJob_Pause->setChecked(buttonPauseJob->isChecked());
476
477         editDetails->setEnabled(status != EncodeThread::JobStatus_Paused);
478 }