OSDN Git Service

Fix help display. Regression introduced when 8-Bit/10-Bit support was added.
[x264-launcher/x264-launcher.git] / src / win_addJob.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_addJob.h"
23
24 #include "global.h"
25 #include "model_options.h"
26 #include "win_help.h"
27 #include "win_editor.h"
28
29 #include <QDate>
30 #include <QTimer>
31 #include <QCloseEvent>
32 #include <QMessageBox>
33 #include <QFileDialog>
34 #include <QDesktopServices>
35 #include <QValidator>
36 #include <QDir>
37 #include <QInputDialog>
38 #include <QSettings>
39 #include <QUrl>
40 #include <QAction>
41 #include <QClipboard>
42
43 #define VALID_DIR(PATH) ((!(PATH).isEmpty()) && QFileInfo(PATH).exists() && QFileInfo(PATH).isDir())
44
45 #define REMOVE_USAFED_ITEM \
46 { \
47         for(int i = 0; i < cbxTemplate->count(); i++) \
48         { \
49                 OptionsModel* temp = reinterpret_cast<OptionsModel*>(cbxTemplate->itemData(i).value<void*>()); \
50                 if(temp == NULL) \
51                 { \
52                         cbxTemplate->blockSignals(true); \
53                         cbxTemplate->removeItem(i); \
54                         cbxTemplate->blockSignals(false); \
55                         break; \
56                 } \
57         } \
58 }
59
60 #define ADD_CONTEXTMENU_ACTION(WIDGET, ICON, TEXT, SLOTNAME) \
61 { \
62         QAction *_action = new QAction((ICON), (TEXT), this); \
63         _action->setData(QVariant::fromValue<void*>(WIDGET)); \
64         WIDGET->addAction(_action); \
65         connect(_action, SIGNAL(triggered(bool)), this, SLOT(SLOTNAME())); \
66 }
67
68 #define ADD_CONTEXTMENU_SEPARATOR(WIDGET) \
69 { \
70         QAction *_action = new QAction(this); \
71         _action->setSeparator(true); \
72         WIDGET->addAction(_action); \
73
74
75 ///////////////////////////////////////////////////////////////////////////////
76 // Validator
77 ///////////////////////////////////////////////////////////////////////////////
78
79 class StringValidator : public QValidator
80 {
81 public:
82         StringValidator(QLabel *notifier, QLabel *icon)
83         :
84                 m_notifier(notifier), m_icon(icon)
85         {
86                 m_notifier->hide();
87                 m_icon->hide();
88         }
89         
90         virtual State validate(QString &input, int &pos) const = 0;
91
92         virtual void fixup(QString &input) const
93         {
94                 input = input.simplified();
95         }
96
97 protected:
98         QLabel *const m_notifier, *const m_icon;
99
100         bool checkParam(const QString &input, const QString &param, const bool doubleMinus) const
101         {
102                 static const char c[20] = {' ', '*', '?', '<', '>', '/', '\\', '"', '\'', '!', '+', '#', '&', '%', '=', ',', ';', '.', 'ยด', '`'};
103                 const QString prefix = doubleMinus ? QLatin1String("--") : QLatin1String("-");
104                 
105                 bool flag = false;
106                 if(param.length() > 1)
107                 {
108                         flag = flag || input.endsWith(QString("%1%2").arg(prefix, param), Qt::CaseInsensitive);
109                         for(size_t i = 0; i < sizeof(c); i++)
110                         {
111                                 flag = flag || input.contains(QString("%1%2%3").arg(prefix, param, QChar::fromLatin1(c[i])), Qt::CaseInsensitive);
112                         }
113                 }
114                 else
115                 {
116                         flag = flag || input.startsWith(QString("-%1").arg(param));
117                         for(size_t i = 0; i < sizeof(c); i++)
118                         {
119                                 flag = flag || input.contains(QString("%1-%2").arg(QChar::fromLatin1(c[i]), param), Qt::CaseSensitive);
120                         }
121                 }
122                 if(flag)
123                 {
124                         if(m_notifier)
125                         {
126                                 m_notifier->setText(tr("Invalid parameter: %1").arg((param.length() > 1) ? QString("%1%2").arg(prefix, param) : QString("-%1").arg(param)));
127                                 if(m_notifier->isHidden()) m_notifier->show();
128                                 if(m_icon) { if(m_icon->isHidden()) m_icon->show(); }
129                         }
130                 }
131                 else
132                 {
133                         if(m_notifier)
134                         {
135                                 if(m_notifier->isVisible()) m_notifier->hide();
136                                 if(m_icon) { if(m_icon->isVisible()) m_icon->hide(); }
137                         }
138                 }
139                 return flag;
140         }
141 };
142
143 class StringValidatorX264 : public StringValidator
144 {
145 public:
146         StringValidatorX264(QLabel *notifier, QLabel *icon) : StringValidator(notifier, icon) {}
147
148         virtual State validate(QString &input, int &pos) const
149         {
150                 static const char* p[] = {"B", "o", "h", "p", "q", "fps", "frames", "preset", "tune", "profile",
151                         "stdin", "crf", "bitrate", "qp", "pass", "stats", "output", "help","quiet", NULL};
152
153                 bool invalid = false;
154
155                 for(size_t i = 0; p[i] && (!invalid); i++)
156                 {
157                         invalid = invalid || checkParam(input, QString::fromLatin1(p[i]), true);
158                 }
159
160                 return invalid ? QValidator::Intermediate : QValidator::Acceptable;
161         }
162 };
163
164 class StringValidatorAvs2YUV : public StringValidator
165 {
166 public:
167         StringValidatorAvs2YUV(QLabel *notifier, QLabel *icon) : StringValidator(notifier, icon) {}
168
169         virtual State validate(QString &input, int &pos) const
170         {
171                 static const char* p[] = {"o", "frames", "seek", "raw", "hfyu", "slave", NULL};
172
173                 bool invalid = false;
174
175                 for(size_t i = 0; p[i] && (!invalid); i++)
176                 {
177                         invalid = invalid || checkParam(input, QString::fromLatin1(p[i]), false);
178                 }
179
180                 return invalid ? QValidator::Intermediate : QValidator::Acceptable;
181         }
182 };
183
184 ///////////////////////////////////////////////////////////////////////////////
185 // Constructor & Destructor
186 ///////////////////////////////////////////////////////////////////////////////
187
188 AddJobDialog::AddJobDialog(QWidget *parent, OptionsModel *options, bool x64supported, bool use10BitEncoding)
189 :
190         QDialog(parent),
191         m_defaults(new OptionsModel()),
192         m_options(options),
193         m_x64supported(x64supported),
194         m_use10BitEncoding(use10BitEncoding),
195         m_initialDir_src(QDir::fromNativeSeparators(QDesktopServices::storageLocation(QDesktopServices::MoviesLocation))),
196         m_initialDir_out(QDir::fromNativeSeparators(QDesktopServices::storageLocation(QDesktopServices::MoviesLocation))),
197         m_lastFilterIndex(0)
198 {
199         //Init the dialog, from the .ui file
200         setupUi(this);
201         setWindowFlags(windowFlags() & (~Qt::WindowContextHelpButtonHint));
202         
203         //Fix dialog size
204         buttonSaveTemplate->setMaximumHeight(20);
205         buttonDeleteTemplate->setMaximumHeight(20);
206         resize(width(), minimumHeight());
207         setMinimumSize(size());
208         setMaximumHeight(height());
209
210         //Setup file type filter
211         m_types.clear();
212         m_types << tr("Matroska Files (*.mkv)");
213         m_types << tr("MPEG-4 Part 14 Container (*.mp4)");
214         m_types << tr("H.264 Elementary Stream (*.264)");
215
216         //Monitor RC mode combobox
217         connect(cbxRateControlMode, SIGNAL(currentIndexChanged(int)), this, SLOT(modeIndexChanged(int)));
218
219         //Activate buttons
220         connect(buttonBrowseSource, SIGNAL(clicked()), this, SLOT(browseButtonClicked()));
221         connect(buttonBrowseOutput, SIGNAL(clicked()), this, SLOT(browseButtonClicked()));
222         connect(buttonSaveTemplate, SIGNAL(clicked()), this, SLOT(saveTemplateButtonClicked()));
223         connect(buttonDeleteTemplate, SIGNAL(clicked()), this, SLOT(deleteTemplateButtonClicked()));
224
225         //Setup validator
226         editCustomX264Params->installEventFilter(this);
227         editCustomX264Params->setValidator(new StringValidatorX264(labelNotificationX264, iconNotificationX264));
228         editCustomX264Params->clear();
229         editCustomAvs2YUVParams->installEventFilter(this);
230         editCustomAvs2YUVParams->setValidator(new StringValidatorAvs2YUV(labelNotificationAvs2YUV, iconNotificationAvs2YUV));
231         editCustomAvs2YUVParams->clear();
232
233         //Install event filter
234         labelHelpScreenX264->installEventFilter(this);
235         labelHelpScreenAvs2YUV->installEventFilter(this);
236
237         //Monitor for options changes
238         connect(cbxRateControlMode, SIGNAL(currentIndexChanged(int)), this, SLOT(configurationChanged()));
239         connect(spinQuantizer, SIGNAL(valueChanged(double)), this, SLOT(configurationChanged()));
240         connect(spinBitrate, SIGNAL(valueChanged(int)), this, SLOT(configurationChanged()));
241         connect(cbxPreset, SIGNAL(currentIndexChanged(int)), this, SLOT(configurationChanged()));
242         connect(cbxTuning, SIGNAL(currentIndexChanged(int)), this, SLOT(configurationChanged()));
243         connect(cbxProfile, SIGNAL(currentIndexChanged(int)), this, SLOT(configurationChanged()));
244         connect(editCustomX264Params, SIGNAL(textChanged(QString)), this, SLOT(configurationChanged()));
245         connect(editCustomAvs2YUVParams, SIGNAL(textChanged(QString)), this, SLOT(configurationChanged()));
246
247         //Create context menus
248         ADD_CONTEXTMENU_ACTION(editCustomX264Params, QIcon(":/buttons/page_edit.png"), tr("Open the Text-Editor"), editorActionTriggered);
249         ADD_CONTEXTMENU_ACTION(editCustomAvs2YUVParams, QIcon(":/buttons/page_edit.png"), tr("Open the Text-Editor"), editorActionTriggered);
250         ADD_CONTEXTMENU_SEPARATOR(editCustomX264Params);
251         ADD_CONTEXTMENU_SEPARATOR(editCustomAvs2YUVParams);
252         ADD_CONTEXTMENU_ACTION(editCustomX264Params, QIcon(":/buttons/page_copy.png"), tr("Copy to Clipboard"), copyActionTriggered);
253         ADD_CONTEXTMENU_ACTION(editCustomAvs2YUVParams, QIcon(":/buttons/page_copy.png"), tr("Copy to Clipboard"), copyActionTriggered);
254         ADD_CONTEXTMENU_ACTION(editCustomX264Params, QIcon(":/buttons/page_paste.png"), tr("Paste from Clipboard"), pasteActionTriggered);
255         ADD_CONTEXTMENU_ACTION(editCustomAvs2YUVParams, QIcon(":/buttons/page_paste.png"), tr("Paste from Clipboard"), pasteActionTriggered);
256
257         //Setup template selector
258         loadTemplateList();
259         connect(cbxTemplate, SIGNAL(currentIndexChanged(int)), this, SLOT(templateSelected()));
260
261         //Load directories
262         const QString appDir = x264_data_path();
263         QSettings settings(QString("%1/last.ini").arg(appDir), QSettings::IniFormat);
264         m_initialDir_src = settings.value("path/directory_openFrom", m_initialDir_src).toString();
265         m_initialDir_out = settings.value("path/directory_saveTo", m_initialDir_out).toString();
266         m_lastFilterIndex = settings.value("path/filterIndex", m_lastFilterIndex).toInt();
267 }
268
269 AddJobDialog::~AddJobDialog(void)
270 {
271         //Free templates
272         for(int i = 0; i < cbxTemplate->model()->rowCount(); i++)
273         {
274                 if(cbxTemplate->itemText(i).startsWith("<") || cbxTemplate->itemText(i).endsWith(">"))
275                 {
276                         continue;
277                 }
278                 OptionsModel *item = reinterpret_cast<OptionsModel*>(cbxTemplate->itemData(i).value<void*>());
279                 cbxTemplate->setItemData(i, QVariant::fromValue<void*>(NULL));
280                 X264_DELETE(item);
281         }
282         
283         X264_DELETE(m_defaults);
284 }
285
286 ///////////////////////////////////////////////////////////////////////////////
287 // Events
288 ///////////////////////////////////////////////////////////////////////////////
289
290 void AddJobDialog::showEvent(QShowEvent *event)
291 {
292         QDialog::showEvent(event);
293         templateSelected();
294
295         if(!editSource->text().isEmpty()) m_initialDir_src = QFileInfo(QDir::fromNativeSeparators(editSource->text())).path();
296         if(!editOutput->text().isEmpty()) m_initialDir_out = QFileInfo(QDir::fromNativeSeparators(editOutput->text())).path();
297
298         if((!editSource->text().isEmpty()) && editOutput->text().isEmpty())
299         {
300                 generateOutputFileName(QDir::fromNativeSeparators(editSource->text()));
301                 buttonAccept->setFocus();
302         }
303
304         labelNotificationX264->hide();
305         iconNotificationX264->hide();
306         labelNotificationAvs2YUV->hide();
307         iconNotificationAvs2YUV->hide();
308 }
309
310 bool AddJobDialog::eventFilter(QObject *o, QEvent *e)
311 {
312         if((o == labelHelpScreenX264) && (e->type() == QEvent::MouseButtonPress))
313         {
314                 HelpDialog *helpScreen = new HelpDialog(this, false, m_x64supported, m_use10BitEncoding);
315                 helpScreen->exec();
316                 X264_DELETE(helpScreen);
317         }
318         else if((o == labelHelpScreenAvs2YUV) && (e->type() == QEvent::MouseButtonPress))
319         {
320                 HelpDialog *helpScreen = new HelpDialog(this, true, m_x64supported, m_use10BitEncoding);
321                 helpScreen->exec();
322                 X264_DELETE(helpScreen);
323         }
324         else if((o == editCustomX264Params) && (e->type() == QEvent::FocusOut))
325         {
326                 editCustomX264Params->setText(editCustomX264Params->text().simplified());
327         }
328         else if((o == editCustomAvs2YUVParams) && (e->type() == QEvent::FocusOut))
329         {
330                 editCustomAvs2YUVParams->setText(editCustomAvs2YUVParams->text().simplified());
331         }
332         return false;
333 }
334
335 void AddJobDialog::dragEnterEvent(QDragEnterEvent *event)
336 {
337         QStringList formats = event->mimeData()->formats();
338         
339         if(formats.contains("application/x-qt-windows-mime;value=\"FileNameW\"", Qt::CaseInsensitive) && formats.contains("text/uri-list", Qt::CaseInsensitive))
340         {
341                 event->acceptProposedAction();
342         }
343 }
344
345 void AddJobDialog::dropEvent(QDropEvent *event)
346 {
347         QString droppedFile;
348         QList<QUrl> urls = event->mimeData()->urls();
349
350         if(urls.count() > 1)
351         {
352                 QDragEnterEvent dragEvent(event->pos(), event->proposedAction(), event->mimeData(), Qt::NoButton, Qt::NoModifier);
353                 if(qApp->notify(parent(), &dragEvent))
354                 {
355                         qApp->notify(parent(), event);
356                         reject(); return;
357                 }
358         }
359
360         while((!urls.isEmpty()) && droppedFile.isEmpty())
361         {
362                 QUrl currentUrl = urls.takeFirst();
363                 QFileInfo file(currentUrl.toLocalFile());
364                 if(file.exists() && file.isFile())
365                 {
366                         qDebug("AddJobDialog::dropEvent: %s", file.canonicalFilePath().toUtf8().constData());
367                         droppedFile = file.canonicalFilePath();
368                 }
369         }
370         
371         if(!droppedFile.isEmpty())
372         {
373                 editSource->setText(QDir::toNativeSeparators(droppedFile));
374                 generateOutputFileName(droppedFile);
375         }
376 }
377
378 ///////////////////////////////////////////////////////////////////////////////
379 // Slots
380 ///////////////////////////////////////////////////////////////////////////////
381
382 void AddJobDialog::modeIndexChanged(int index)
383 {
384         spinQuantizer->setEnabled(index == 0 || index == 1);
385         spinBitrate->setEnabled(index == 2 || index == 3);
386 }
387
388 void AddJobDialog::accept(void)
389 {
390         if(editSource->text().trimmed().isEmpty())
391         {
392                 QMessageBox::warning(this, tr("Not Found!"), tr("Please select a valid source file first!"));
393                 return;
394         }
395         
396         if(editOutput->text().trimmed().isEmpty())
397         {
398                 QMessageBox::warning(this, tr("Not Selected!"), tr("<nobr>Please select a valid output file first!</nobr>"));
399                 return;
400         }
401
402         QFileInfo sourceFile = QFileInfo(editSource->text());
403         if(!(sourceFile.exists() && sourceFile.isFile()))
404         {
405                 QMessageBox::warning(this, tr("Not Found!"), tr("<nobr>The selected source file could not be found!</nobr>"));
406                 return;
407         }
408
409         QFileInfo outputDir = QFileInfo(QFileInfo(editOutput->text()).path());
410         if(!(outputDir.exists() && outputDir.isDir() && outputDir.isWritable()))
411         {
412                 QMessageBox::warning(this, tr("Not Writable!"), tr("<nobr>Output directory does not exist or is not writable!</nobr>"));
413                 return;
414         }
415
416         QFileInfo outputFile = QFileInfo(editOutput->text());
417         if(outputFile.exists() && outputFile.isFile())
418         {
419                 int ret = QMessageBox::question(this, tr("Already Exists!"), tr("<nobr>Output file already exists! Overwrite?</nobr>"), QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
420                 if(ret != QMessageBox::Yes) return;
421         }
422         if(outputFile.exists() && (!outputFile.isFile()))
423         {
424                 QMessageBox::warning(this, tr("Not a File!"), tr("<nobr>Selected output file does not appear to be a valid file!</nobr>"));
425                 return;
426         }
427         if(!editCustomX264Params->hasAcceptableInput())
428         {
429                 int ret = QMessageBox::warning(this, tr("Invalid Params"), tr("<nobr>Your custom parameters are invalid and will be discarded!</nobr>"), QMessageBox::Ignore | QMessageBox::Cancel, QMessageBox::Cancel);
430                 if(ret != QMessageBox::Ignore) return;
431         }
432
433         //Save directories
434         const QString appDir = x264_data_path();
435         QSettings settings(QString("%1/last.ini").arg(appDir), QSettings::IniFormat);
436         if(settings.isWritable())
437         {
438                 settings.setValue("path/directory_saveTo", m_initialDir_out);
439                 settings.setValue("path/directory_openFrom", m_initialDir_src);
440                 settings.setValue("path/filterIndex", m_lastFilterIndex);
441                 settings.sync();
442         }
443
444         saveOptions(m_options);
445         QDialog::accept();
446 }
447
448 void AddJobDialog::browseButtonClicked(void)
449 {
450         if(QObject::sender() == buttonBrowseSource)
451         {
452                 QString initDir = VALID_DIR(m_initialDir_src) ? m_initialDir_src : QDesktopServices::storageLocation(QDesktopServices::MoviesLocation);
453                 if(!editSource->text().isEmpty()) initDir = QString("%1/%2").arg(initDir, QFileInfo(QDir::fromNativeSeparators(editSource->text())).fileName());
454
455                 QString filePath = QFileDialog::getOpenFileName(this, tr("Open Source File"), initDir, makeFileFilter(), NULL, QFileDialog::DontUseNativeDialog);
456                 if(!(filePath.isNull() || filePath.isEmpty()))
457                 {
458                         editSource->setText(QDir::toNativeSeparators(filePath));
459                         generateOutputFileName(filePath);
460                         m_initialDir_src = QFileInfo(filePath).path();
461                 }
462         }
463         else if(QObject::sender() == buttonBrowseOutput)
464         {
465                 QString initDir = VALID_DIR(m_initialDir_out) ? m_initialDir_out : QDesktopServices::storageLocation(QDesktopServices::MoviesLocation);
466                 if(!editOutput->text().isEmpty()) initDir = QString("%1/%2").arg(initDir, QFileInfo(QDir::fromNativeSeparators(editOutput->text())).completeBaseName());
467                 int filterIdx = getFilterIndex(QFileInfo(QDir::fromNativeSeparators(editOutput->text())).suffix());
468                 QString selectedType = m_types.at((filterIdx >= 0) ? filterIdx : m_lastFilterIndex);
469
470                 QString filePath = QFileDialog::getSaveFileName(this, tr("Choose Output File"), initDir, m_types.join(";;"), &selectedType, QFileDialog::DontUseNativeDialog | QFileDialog::DontConfirmOverwrite);
471
472                 if(!(filePath.isNull() || filePath.isEmpty()))
473                 {
474                         if(getFilterIndex(QFileInfo(filePath).suffix()) < 0)
475                         {
476                                 filterIdx = m_types.indexOf(selectedType);
477                                 if(filterIdx >= 0)
478                                 {
479                                         filePath = QString("%1.%2").arg(filePath, getFilterExt(filterIdx));
480                                 }
481                         }
482                         editOutput->setText(QDir::toNativeSeparators(filePath));
483                         m_lastFilterIndex = getFilterIndex(QFileInfo(filePath).suffix());
484                         m_initialDir_out = QFileInfo(filePath).path();
485                 }
486         }
487 }
488
489 void AddJobDialog::configurationChanged(void)
490 {
491         OptionsModel* options = reinterpret_cast<OptionsModel*>(cbxTemplate->itemData(cbxTemplate->currentIndex()).value<void*>());
492         if(options)
493         {
494                 cbxTemplate->blockSignals(true);
495                 cbxTemplate->insertItem(0, tr("<Unsaved Configuration>"), QVariant::fromValue<void*>(NULL));
496                 cbxTemplate->setCurrentIndex(0);
497                 cbxTemplate->blockSignals(false);
498         }
499 }
500
501 void AddJobDialog::templateSelected(void)
502 {
503         OptionsModel* options = reinterpret_cast<OptionsModel*>(cbxTemplate->itemData(cbxTemplate->currentIndex()).value<void*>());
504         if(options)
505         {
506                 qDebug("Loading options!");
507                 REMOVE_USAFED_ITEM;
508                 restoreOptions(options);
509         }
510
511         modeIndexChanged(cbxRateControlMode->currentIndex());
512 }
513
514 void AddJobDialog::saveTemplateButtonClicked(void)
515 {
516         qDebug("Saving template");
517         QString name = tr("New Template");
518         int n = 2;
519
520         while(OptionsModel::templateExists(name))
521         {
522                 name = tr("New Template (%1)").arg(QString::number(n++));
523         }
524
525         OptionsModel *options = new OptionsModel();
526         saveOptions(options);
527
528         if(options->equals(m_defaults))
529         {
530                 QMessageBox::warning (this, tr("Oups"), tr("<nobr>It makes no sense to save the default settings!</nobr>"));
531                 cbxTemplate->blockSignals(true);
532                 cbxTemplate->setCurrentIndex(0);
533                 cbxTemplate->blockSignals(false);
534                 REMOVE_USAFED_ITEM;
535                 X264_DELETE(options);
536                 return;
537         }
538
539         for(int i = 0; i < cbxTemplate->count(); i++)
540         {
541                 const QString tempName = cbxTemplate->itemText(i);
542                 if(tempName.contains('<') || tempName.contains('>'))
543                 {
544                         continue;
545                 }
546                 OptionsModel* test = reinterpret_cast<OptionsModel*>(cbxTemplate->itemData(i).value<void*>());
547                 if(test != NULL)
548                 {
549                         if(options->equals(test))
550                         {
551                                 QMessageBox::warning (this, tr("Oups"), tr("<nobr>There already is a template for the current settings!</nobr>"));
552                                 cbxTemplate->blockSignals(true);
553                                 cbxTemplate->setCurrentIndex(i);
554                                 cbxTemplate->blockSignals(false);
555                                 REMOVE_USAFED_ITEM;
556                                 X264_DELETE(options);
557                                 return;
558                         }
559                 }
560         }
561
562         forever
563         {
564                 bool ok = false;
565                 name = QInputDialog::getText(this, tr("Save Template"), tr("Please enter the name of the template:").leftJustified(144, ' '), QLineEdit::Normal, name, &ok).simplified();
566                 if(!ok)
567                 {
568                         X264_DELETE(options);
569                         return;
570                 }
571                 if(name.contains('<') || name.contains('>') || name.contains('\\') || name.contains('/') || name.contains('"'))
572                 {
573                         QMessageBox::warning (this, tr("Invalid Name"), tr("<nobr>Sorry, the name you have entered is invalid!</nobr>"));
574                         while(name.contains('<')) name.remove('<');
575                         while(name.contains('>')) name.remove('>');
576                         while(name.contains('\\')) name.remove('\\');
577                         while(name.contains('/')) name.remove('/');
578                         while(name.contains('"')) name.remove('"');
579                         name = name.simplified();
580                         continue;
581                 }
582                 if(OptionsModel::templateExists(name))
583                 {
584                         int ret = QMessageBox::warning (this, tr("Already Exists"), tr("<nobr>A template of that name already exists! Overwrite?</nobr>"), QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
585                         if(ret != QMessageBox::Yes)
586                         {
587                                 continue;
588                         }
589                 }
590                 break;
591         }
592         
593         if(!OptionsModel::saveTemplate(options, name))
594         {
595                 QMessageBox::critical(this, tr("Save Failed"), tr("Sorry, the template could not be saved!"));
596                 X264_DELETE(options);
597                 return;
598         }
599         
600         int index = cbxTemplate->model()->rowCount();
601         cbxTemplate->blockSignals(true);
602         for(int i = 0; i < cbxTemplate->count(); i++)
603         {
604                 if(cbxTemplate->itemText(i).compare(name, Qt::CaseInsensitive) == 0)
605                 {
606                         index = -1; //Do not append new template
607                         OptionsModel *oldItem = reinterpret_cast<OptionsModel*>(cbxTemplate->itemData(i).value<void*>());
608                         cbxTemplate->setItemData(i, QVariant::fromValue<void*>(options));
609                         cbxTemplate->setCurrentIndex(i);
610                         X264_DELETE(oldItem);
611                 }
612         }
613         if(index >= 0)
614         {
615                 cbxTemplate->insertItem(index, name, QVariant::fromValue<void*>(options));
616                 cbxTemplate->setCurrentIndex(index);
617         }
618         cbxTemplate->blockSignals(false);
619
620         REMOVE_USAFED_ITEM;
621 }
622
623 void AddJobDialog::deleteTemplateButtonClicked(void)
624 {
625         const int index = cbxTemplate->currentIndex();
626         QString name = cbxTemplate->itemText(index);
627
628         if(name.contains('<') || name.contains('>') || name.contains('\\') || name.contains('/'))
629         {
630                 QMessageBox::warning (this, tr("Invalid Item"), tr("Sorry, the selected item cannot be deleted!"));
631                 return;
632         }
633
634         int ret = QMessageBox::question (this, tr("Delete Template"), tr("<nobr>Do you really want to delete the selected template?</nobr>"), QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
635         if(ret != QMessageBox::Yes)
636         {
637                 return;
638         }
639
640
641         OptionsModel::deleteTemplate(name);
642         OptionsModel *item = reinterpret_cast<OptionsModel*>(cbxTemplate->itemData(index).value<void*>());
643         cbxTemplate->removeItem(index);
644         X264_DELETE(item);
645 }
646
647 void AddJobDialog::editorActionTriggered(void)
648 {
649
650         if(QAction *action = dynamic_cast<QAction*>(QObject::sender()))
651         {
652                 QLineEdit *lineEdit = reinterpret_cast<QLineEdit*>(action->data().value<void*>());
653                 
654                 EditorDialog *editor = new EditorDialog(this);
655                 editor->setEditText(lineEdit->text());
656
657                 if(editor->exec() == QDialog::Accepted)
658                 {
659                         lineEdit->setText(editor->getEditText());
660                 }
661
662                 X264_DELETE(editor);
663         }
664 }
665
666 void AddJobDialog::copyActionTriggered(void)
667 {
668         if(QAction *action = dynamic_cast<QAction*>(QObject::sender()))
669         {
670                 QClipboard *clipboard = QApplication::clipboard();
671                 QLineEdit *lineEdit = reinterpret_cast<QLineEdit*>(action->data().value<void*>());
672                 QString text = lineEdit->hasSelectedText() ? lineEdit->selectedText() : lineEdit->text();
673                 clipboard->setText(text);
674         }
675 }
676
677 void AddJobDialog::pasteActionTriggered(void)
678 {
679         if(QAction *action = dynamic_cast<QAction*>(QObject::sender()))
680         {
681                 QClipboard *clipboard = QApplication::clipboard();
682                 QLineEdit *lineEdit = reinterpret_cast<QLineEdit*>(action->data().value<void*>());
683                 QString text = clipboard->text();
684                 if(!text.isEmpty()) lineEdit->setText(text);
685         }
686 }
687
688 ///////////////////////////////////////////////////////////////////////////////
689 // Public functions
690 ///////////////////////////////////////////////////////////////////////////////
691
692 QString AddJobDialog::sourceFile(void)
693 {
694         return QDir::fromNativeSeparators(editSource->text());
695 }
696
697 QString AddJobDialog::outputFile(void)
698 {
699         return QDir::fromNativeSeparators(editOutput->text());
700 }
701
702 ///////////////////////////////////////////////////////////////////////////////
703 // Private functions
704 ///////////////////////////////////////////////////////////////////////////////
705
706 void AddJobDialog::loadTemplateList(void)
707 {
708         cbxTemplate->addItem(tr("<Default>"), QVariant::fromValue<void*>(m_defaults));
709         cbxTemplate->setCurrentIndex(0);
710
711         QMap<QString, OptionsModel*> templates = OptionsModel::loadAllTemplates();
712         QStringList templateNames = templates.keys();
713         templateNames.sort();
714
715         while(!templateNames.isEmpty())
716         {
717                 QString current = templateNames.takeFirst();
718                 cbxTemplate->addItem(current, QVariant::fromValue<void*>(templates.value(current)));
719
720                 if(templates.value(current)->equals(m_options))
721                 {
722                         cbxTemplate->setCurrentIndex(cbxTemplate->count() - 1);
723                 }
724         }
725
726         if((cbxTemplate->currentIndex() == 0) && (!m_options->equals(m_defaults)))
727         {
728                 cbxTemplate->insertItem(1, tr("<Recently Used>"), QVariant::fromValue<void*>(m_options));
729                 cbxTemplate->setCurrentIndex(1);
730         }
731 }
732
733 void AddJobDialog::updateComboBox(QComboBox *cbox, const QString &text)
734 {
735         for(int i = 0; i < cbox->model()->rowCount(); i++)
736         {
737                 if(cbox->model()->data(cbox->model()->index(i, 0, QModelIndex())).toString().compare(text, Qt::CaseInsensitive) == 0)
738                 {
739                         cbox->setCurrentIndex(i);
740                         break;
741                 }
742         }
743 }
744
745 void AddJobDialog::restoreOptions(OptionsModel *options)
746 {
747         cbxRateControlMode->blockSignals(true);
748         spinQuantizer->blockSignals(true);
749         spinBitrate->blockSignals(true);
750         cbxPreset->blockSignals(true);
751         cbxTuning->blockSignals(true);
752         cbxProfile->blockSignals(true);
753         editCustomX264Params->blockSignals(true);
754         editCustomAvs2YUVParams->blockSignals(true);
755
756         cbxRateControlMode->setCurrentIndex(options->rcMode());
757         spinQuantizer->setValue(options->quantizer());
758         spinBitrate->setValue(options->bitrate());
759         updateComboBox(cbxPreset, options->preset());
760         updateComboBox(cbxTuning, options->tune());
761         updateComboBox(cbxProfile, options->profile());
762         editCustomX264Params->setText(options->customX264());
763         editCustomAvs2YUVParams->setText(options->customAvs2YUV());
764
765         cbxRateControlMode->blockSignals(false);
766         spinQuantizer->blockSignals(false);
767         spinBitrate->blockSignals(false);
768         cbxPreset->blockSignals(false);
769         cbxTuning->blockSignals(false);
770         cbxProfile->blockSignals(false);
771         editCustomX264Params->blockSignals(false);
772         editCustomAvs2YUVParams->blockSignals(false);
773 }
774
775 void AddJobDialog::saveOptions(OptionsModel *options)
776 {
777         options->setRCMode(static_cast<OptionsModel::RCMode>(cbxRateControlMode->currentIndex()));
778         options->setQuantizer(spinQuantizer->value());
779         options->setBitrate(spinBitrate->value());
780         options->setPreset(cbxPreset->model()->data(cbxPreset->model()->index(cbxPreset->currentIndex(), 0)).toString());
781         options->setTune(cbxTuning->model()->data(cbxTuning->model()->index(cbxTuning->currentIndex(), 0)).toString());
782         options->setProfile(cbxProfile->model()->data(cbxProfile->model()->index(cbxProfile->currentIndex(), 0)).toString());
783         options->setCustomX264(editCustomX264Params->hasAcceptableInput() ? editCustomX264Params->text().simplified() : QString());
784         options->setCustomAvs2YUV(editCustomAvs2YUVParams->hasAcceptableInput() ? editCustomAvs2YUVParams->text().simplified() : QString());
785 }
786
787 QString AddJobDialog::makeFileFilter(void)
788 {
789         static const struct
790         {
791                 const char *name;
792                 const char *fext;
793         }
794         s_filters[] =
795         {
796                 {"Avisynth Scripts", "avs"},
797                 {"Matroska Files", "mkv"},
798                 {"MPEG-4 Part 14 Container", "mp4"},
799                 {"Audio Video Interleaved", "avi"},
800                 {"Flash Video", "flv"},
801                 {"YUV4MPEG2 Stream", "y4m"},
802                 {"Uncompresses YUV Data", "yuv"},
803                 {NULL, NULL}
804         };
805
806         QString filters("All supported files (");
807
808         for(size_t index = 0; s_filters[index].name && s_filters[index].fext; index++)
809         {
810                 filters += QString((index > 0) ? " *.%1" : "*.%1").arg(QString::fromLatin1(s_filters[index].fext));
811         }
812
813         filters += QString(");;");
814
815         for(size_t index = 0; s_filters[index].name && s_filters[index].fext; index++)
816         {
817                 filters += QString("%1 (*.%2);;").arg(QString::fromLatin1(s_filters[index].name), QString::fromLatin1(s_filters[index].fext));
818         }
819                 
820         filters += QString("All files (*.*)");
821         return filters;
822 }
823
824 void AddJobDialog::generateOutputFileName(const QString &filePath)
825 {
826         QString name = QFileInfo(filePath).completeBaseName();
827         QString path = VALID_DIR(m_initialDir_out) ? m_initialDir_out : QFileInfo(filePath).path();
828         QString fext = getFilterExt(m_lastFilterIndex);
829                         
830         QString outPath = QString("%1/%2.%3").arg(path, name, fext);
831
832         if(QFileInfo(outPath).exists())
833         {
834                 int i = 2;
835                 while(QFileInfo(outPath).exists())
836                 {
837                         outPath = QString("%1/%2 (%3).%4").arg(path, name, QString::number(i++), fext);
838                 }
839         }
840
841         editOutput->setText(QDir::toNativeSeparators(outPath));
842 }
843
844 int AddJobDialog::getFilterIndex(const QString &fileExt)
845 {
846         if(!fileExt.isEmpty())
847         {
848                 QRegExp ext("\\(\\*\\.(.+)\\)");
849                 for(int i = 0; i < m_types.count(); i++)
850                 {
851                         if(ext.lastIndexIn(m_types.at(i)) >= 0)
852                         {
853                                 if(fileExt.compare(ext.cap(1), Qt::CaseInsensitive) == 0)
854                                 {
855                                         return i;
856                                 }
857                         }
858                 }
859         }
860
861         return -1;
862 }
863
864 QString AddJobDialog::getFilterExt(int filterIdx)
865 {
866         int index = qBound(0, filterIdx, m_types.count()-1);
867
868         QRegExp ext("\\(\\*\\.(.+)\\)");
869         if(ext.lastIndexIn(m_types.at(index)) >= 0)
870         {
871                 return ext.cap(1).toLower();
872         }
873
874         return QString::fromLatin1("mkv");
875 }