OSDN Git Service

Added preliminary NVEncC encoding support.
[x264-launcher/x264-launcher.git] / src / win_addJob.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2016 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 #include "UIC_win_addJob.h"
24
25 //Internal
26 #include "global.h"
27 #include "model_options.h"
28 #include "model_preferences.h"
29 #include "model_sysinfo.h"
30 #include "model_recently.h"
31 #include "encoder_factory.h"
32 #include "mediainfo.h"
33 #include "win_help.h"
34 #include "win_editor.h"
35
36 //MUtils
37 #include <MUtils/Global.h>
38 #include <MUtils/OSSupport.h>
39 #include <MUtils/Exception.h>
40
41 //Qt
42 #include <QDate>
43 #include <QTimer>
44 #include <QCloseEvent>
45 #include <QMessageBox>
46 #include <QFileDialog>
47 #include <QDesktopServices>
48 #include <QValidator>
49 #include <QDir>
50 #include <QInputDialog>
51 #include <QSettings>
52 #include <QUrl>
53 #include <QAction>
54 #include <QClipboard>
55 #include <QToolTip>
56
57 #include <memory>
58
59 #define ARRAY_SIZE(ARRAY) (sizeof((ARRAY))/sizeof((ARRAY[0])))
60 #define VALID_DIR(PATH) ((!(PATH).isEmpty()) && QFileInfo(PATH).exists() && QFileInfo(PATH).isDir())
61 #define SHFL(X) ((*reinterpret_cast<int*>(&(X))) <<= 1)
62
63 #define REMOVE_USAFED_ITEM \
64 { \
65         for(int i = 0; i < ui->cbxTemplate->count(); i++) \
66         { \
67                 const OptionsModel* temp = reinterpret_cast<const OptionsModel*>(ui->cbxTemplate->itemData(i).value<const void*>()); \
68                 if(temp == NULL) \
69                 { \
70                         ui->cbxTemplate->blockSignals(true); \
71                         ui->cbxTemplate->removeItem(i); \
72                         ui->cbxTemplate->blockSignals(false); \
73                         break; \
74                 } \
75         } \
76 }
77
78 #define ADD_CONTEXTMENU_ACTION(WIDGET, ICON, TEXT, SLOTNAME) \
79 { \
80         QAction *_action = new QAction((ICON), (TEXT), this); \
81         _action->setData(QVariant::fromValue<void*>(WIDGET)); \
82         WIDGET->addAction(_action); \
83         connect(_action, SIGNAL(triggered(bool)), this, SLOT(SLOTNAME())); \
84 }
85
86 #define ADD_CONTEXTMENU_SEPARATOR(WIDGET) \
87 { \
88         QAction *_action = new QAction(this); \
89         _action->setSeparator(true); \
90         WIDGET->addAction(_action); \
91
92
93 Q_DECLARE_METATYPE(const void*)
94
95 ///////////////////////////////////////////////////////////////////////////////
96 // Disable Monitoring RAII
97 ///////////////////////////////////////////////////////////////////////////////
98
99 class DisableHelperRAII
100 {
101 public:
102         DisableHelperRAII(bool *const flag) : m_flag(flag)
103         {
104                 *m_flag = false;
105         }
106
107         ~DisableHelperRAII(void)
108         {
109                 *m_flag = true;
110         }
111
112 private:
113         bool *const m_flag;
114 };
115
116 ///////////////////////////////////////////////////////////////////////////////
117 // Validator
118 ///////////////////////////////////////////////////////////////////////////////
119
120 class StringValidator : public QValidator
121 {
122 public:
123         StringValidator(QLabel *notifier, QLabel *icon)
124         :
125                 m_notifier(notifier), m_icon(icon)
126         {
127                 m_notifier->hide();
128                 m_icon->hide();
129         }
130         
131         virtual State validate(QString &input, int &pos) const = 0;
132
133         virtual void fixup(QString &input) const
134         {
135                 input = input.simplified();
136         }
137
138 protected:
139         QLabel *const m_notifier, *const m_icon;
140
141         bool checkParam(const QStringList &input, const char *const params[], const bool &doubleMinus) const
142         {
143                 for(QStringList::ConstIterator iter = input.constBegin(); iter != input.constEnd(); iter++)
144                 {
145                         for(size_t k = 0; params[k]; k++)
146                         {
147                                 const QString param = QLatin1String(params[k]);
148                                 const QString prefix = ((param.length() > 1) && doubleMinus) ? QLatin1String("--") : QLatin1String("-");
149                                 if(iter->compare(QString("%1%2").arg(prefix, param), Qt::CaseInsensitive) == 0)
150                                 {
151                                         if(m_notifier)
152                                         {
153                                                 m_notifier->setText(tr("Forbidden parameter: %1").arg(*iter));
154                                         }
155                                         return true;
156                                 }
157                         }
158                         if(iter->startsWith("--", Qt::CaseInsensitive))
159                         {
160                                 for(int i = 2; i < iter->length(); i++)
161                                 {
162                                         if((iter->at(i) == QLatin1Char('=')) && (i > 2) && (i + 1 < iter->length()))
163                                         {
164                                                 break; /*to allow "--param=value" format*/
165                                         }
166                                         if((!iter->at(i).isLetter()) && ((iter->at(i) != QLatin1Char('-')) || (i < 3)))
167                                         {
168                                                 if(m_notifier)
169                                                 {
170                                                         m_notifier->setText(tr("Invalid string: %1").arg(*iter));
171                                                 }
172                                                 return true;
173                                         }
174                                 }
175                         }
176                 }
177
178                 return false;
179         }
180
181         bool checkPrefix(const QStringList &input, const bool &doubleMinus) const
182         {
183                 for(QStringList::ConstIterator iter = input.constBegin(); iter != input.constEnd(); iter++)
184                 {
185                         static const char *const c[3] = { "--", "-", NULL };
186                         for(size_t i = 0; c[i]; i++)
187                         {
188                                 const QString prefix = QString::fromLatin1(c[i]);
189                                 if(iter->compare(prefix, Qt::CaseInsensitive) == 0)
190                                 {
191                                         if(m_notifier)
192                                         {
193                                                 m_notifier->setText(tr("Invalid parameter: %1").arg(prefix));
194                                         }
195                                         return true;
196                                 }
197                         }
198                         if
199                         (
200                                 ((!doubleMinus) && iter->startsWith("--", Qt::CaseInsensitive)) ||
201                                 (doubleMinus && iter->startsWith("-", Qt::CaseInsensitive) && (!iter->startsWith("--", Qt::CaseInsensitive)) && (iter->length() > 2) && (!iter->at(1).isDigit())) ||
202                                 (doubleMinus && iter->startsWith("--", Qt::CaseInsensitive) && (iter->length() < 4))
203                         )
204                         {
205                                 if(m_notifier)
206                                 {
207                                         m_notifier->setText(tr("Invalid syntax: %1").arg(*iter));
208                                 }
209                                 return true;
210                         }
211                 }
212                 return false;
213         }
214
215         bool checkCharacters(const QStringList &input) const
216         {
217                 static const char c[] = {'*', '?', '<', '>', '|', NULL};
218
219                 for(QStringList::ConstIterator iter = input.constBegin(); iter != input.constEnd(); iter++)
220                 {
221                         for(size_t i = 0; c[i]; i++)
222                         {
223                                 if(iter->indexOf(QLatin1Char(c[i])) >= 0)
224                                 {
225                                         if(m_notifier)
226                                         {
227                                                 m_notifier->setText(tr("Invalid character: '%1'").arg(QLatin1Char(c[i])));
228                                         }
229                                         return true;
230                                 }
231                         }
232                 }
233                 return false;
234         }
235
236         const bool &setStatus(const bool &flag, const QString &toolName) const
237         {
238                 if(flag)
239                 {
240                         if(m_notifier)
241                         {
242                                 if(m_notifier->isHidden()) m_notifier->show();
243                                 if(m_icon) { if(m_icon->isHidden()) m_icon->show(); }
244                                 if(QWidget *w = m_notifier->topLevelWidget()->focusWidget())
245                                 {
246                                         QToolTip::showText(static_cast<QWidget*>(w->parent())->mapToGlobal(w->pos()), tr("<b>Warning:</b> You entered a parameter that is forbidden. Please note that the GUI will automatically set <i>this</i> parameter for you (if required)."), m_notifier, QRect());
247                                 }
248                         }
249                 }
250                 else
251                 {
252                         if(m_notifier)
253                         {
254                                 if(m_notifier->isVisible()) m_notifier->hide();
255                                 if(m_icon) { if(m_icon->isVisible()) m_icon->hide(); }
256                                 QToolTip::hideText();
257                         }
258                 }
259                 return flag;
260         }
261 };
262
263 class StringValidatorX264 : public StringValidator
264 {
265 public:
266         StringValidatorX264(QLabel *notifier, QLabel *icon) : StringValidator(notifier, icon) {}
267
268         virtual State validate(QString &input, int &pos) const
269         {
270                 static const char *const params[] = {"B", "o", "h", "p", "q", /*"fps", "frames",*/ "preset", "tune", "profile",
271                         "stdin", "crf", "bitrate", "qp", "pass", "stats", "output", "help", "quiet", NULL};
272
273                 const QString commandLine = input.trimmed();
274                 const QStringList tokens =  commandLine.isEmpty() ? QStringList() : MUtils::OS::crack_command_line(commandLine);
275
276                 const bool invalid = checkCharacters(tokens) || checkPrefix(tokens, true) || checkParam(tokens, params, true);
277                 return setStatus(invalid, "encoder") ? QValidator::Intermediate : QValidator::Acceptable;
278         }
279 };
280
281 class StringValidatorAvs2YUV : public StringValidator
282 {
283 public:
284         StringValidatorAvs2YUV(QLabel *notifier, QLabel *icon) : StringValidator(notifier, icon) {}
285
286         virtual State validate(QString &input, int &pos) const
287         {
288                 static const char *const params[] = {"o", "frames", "seek", "raw", "hfyu", "slave", NULL};
289
290                 const QString commandLine = input.trimmed();
291                 const QStringList tokens =  commandLine.isEmpty() ? QStringList() : MUtils::OS::crack_command_line(commandLine);
292
293                 const bool invalid = checkCharacters(tokens) || checkPrefix(tokens, false) || checkParam(tokens, params, false);
294                 return setStatus(invalid, "Avs2YUV") ? QValidator::Intermediate : QValidator::Acceptable;
295         }
296 };
297
298 ///////////////////////////////////////////////////////////////////////////////
299 // Constructor & Destructor
300 ///////////////////////////////////////////////////////////////////////////////
301
302 AddJobDialog::AddJobDialog(QWidget *parent, OptionsModel *const options, RecentlyUsed *const recentlyUsed, const SysinfoModel *const sysinfo, const PreferencesModel *const preferences)
303 :
304         QDialog(parent),
305         m_options(options),
306         m_recentlyUsed(recentlyUsed),
307         m_sysinfo(sysinfo),
308         m_preferences(preferences),
309         m_defaults(new OptionsModel(sysinfo)),
310         ui(new Ui::AddJobDialog()),
311         m_monitorConfigChanges(false)
312 {
313         //Init the dialog, from the .ui file
314         ui->setupUi(this);
315         setWindowFlags(windowFlags() & (~Qt::WindowContextHelpButtonHint));
316
317         //Fix dialog size
318         ui->buttonSaveTemplate->setMaximumHeight(20);
319         ui->buttonDeleteTemplate->setMaximumHeight(20);
320         resize(width(), minimumHeight());
321         setMinimumSize(size());
322         setMaximumHeight(height());
323
324         //Init encoder combobox
325         ui->cbxEncoderType->addItem(tr("x264 (AVC)"),  OptionsModel::EncType_X264);
326         ui->cbxEncoderType->addItem(tr("x265 (HEVC)"), OptionsModel::EncType_X265);
327         ui->cbxEncoderType->addItem(tr("NVEncC"),      OptionsModel::EncType_NVEnc);
328
329         //Init arch combobox
330         ui->cbxEncoderArch->addItem(tr("32-Bit"), OptionsModel::EncArch_x86_32);
331         ui->cbxEncoderArch->addItem(tr("64-Bit"), OptionsModel::EncArch_x86_64);
332
333         //Init rc-mode combobox
334         ui->cbxRateControlMode->addItem(tr("CRF"),    OptionsModel::RCMode_CRF);
335         ui->cbxRateControlMode->addItem(tr("CQ"),     OptionsModel::RCMode_CQ);
336         ui->cbxRateControlMode->addItem(tr("2-Pass"), OptionsModel::RCMode_2Pass);
337         ui->cbxRateControlMode->addItem(tr("ABR"),    OptionsModel::RCMode_ABR);
338
339         //Init combobox items
340         ui->cbxTuning ->addItem(QString::fromLatin1(OptionsModel::SETTING_UNSPECIFIED));
341         ui->cbxProfile->addItem(QString::fromLatin1(OptionsModel::PROFILE_UNRESTRICTED));
342
343         //Hide optional controls
344         ui->checkBoxApplyToAll->setVisible(false);
345
346         //Monitor combobox changes
347         connect(ui->cbxEncoderType,     SIGNAL(currentIndexChanged(int)), this, SLOT(encoderIndexChanged(int)));
348         connect(ui->cbxEncoderVariant,  SIGNAL(currentIndexChanged(int)), this, SLOT(variantIndexChanged(int)));
349         connect(ui->cbxRateControlMode, SIGNAL(currentIndexChanged(int)), this, SLOT(modeIndexChanged(int)));
350
351         //Activate buttons
352         connect(ui->buttonBrowseSource,   SIGNAL(clicked()), this, SLOT(browseButtonClicked()));
353         connect(ui->buttonBrowseOutput,   SIGNAL(clicked()), this, SLOT(browseButtonClicked()));
354         connect(ui->buttonSaveTemplate,   SIGNAL(clicked()), this, SLOT(saveTemplateButtonClicked()));
355         connect(ui->buttonDeleteTemplate, SIGNAL(clicked()), this, SLOT(deleteTemplateButtonClicked()));
356
357         //Setup validator
358         ui->editCustomX264Params->installEventFilter(this);
359         ui->editCustomX264Params->setValidator(new StringValidatorX264(ui->labelNotificationX264, ui->iconNotificationX264));
360         ui->editCustomX264Params->clear();
361         ui->editCustomAvs2YUVParams->installEventFilter(this);
362         ui->editCustomAvs2YUVParams->setValidator(new StringValidatorAvs2YUV(ui->labelNotificationAvs2YUV, ui->iconNotificationAvs2YUV));
363         ui->editCustomAvs2YUVParams->clear();
364
365         //Install event filter
366         ui->labelHelpScreenX264->installEventFilter(this);
367         ui->labelHelpScreenAvs2YUV->installEventFilter(this);
368
369         //Monitor for options changes
370         connect(ui->cbxEncoderType, SIGNAL(currentIndexChanged(int)), this, SLOT(configurationChanged()));
371         connect(ui->cbxEncoderArch, SIGNAL(currentIndexChanged(int)), this, SLOT(configurationChanged()));
372         connect(ui->cbxEncoderVariant, SIGNAL(currentIndexChanged(int)), this, SLOT(configurationChanged()));
373         connect(ui->cbxRateControlMode, SIGNAL(currentIndexChanged(int)), this, SLOT(configurationChanged()));
374         connect(ui->spinQuantizer, SIGNAL(valueChanged(double)), this, SLOT(configurationChanged()));
375         connect(ui->spinBitrate, SIGNAL(valueChanged(int)), this, SLOT(configurationChanged()));
376         connect(ui->cbxPreset, SIGNAL(currentIndexChanged(int)), this, SLOT(configurationChanged()));
377         connect(ui->cbxTuning, SIGNAL(currentIndexChanged(int)), this, SLOT(configurationChanged()));
378         connect(ui->cbxProfile, SIGNAL(currentIndexChanged(int)), this, SLOT(configurationChanged()));
379         connect(ui->editCustomX264Params, SIGNAL(textChanged(QString)), this, SLOT(configurationChanged()));
380         connect(ui->editCustomAvs2YUVParams, SIGNAL(textChanged(QString)), this, SLOT(configurationChanged()));
381
382         //Create context menus
383         ADD_CONTEXTMENU_ACTION(ui->editCustomX264Params, QIcon(":/buttons/page_edit.png"), tr("Open the Text-Editor"), editorActionTriggered);
384         ADD_CONTEXTMENU_ACTION(ui->editCustomAvs2YUVParams, QIcon(":/buttons/page_edit.png"), tr("Open the Text-Editor"), editorActionTriggered);
385         ADD_CONTEXTMENU_SEPARATOR(ui->editCustomX264Params);
386         ADD_CONTEXTMENU_SEPARATOR(ui->editCustomAvs2YUVParams);
387         ADD_CONTEXTMENU_ACTION(ui->editCustomX264Params, QIcon(":/buttons/page_copy.png"), tr("Copy to Clipboard"), copyActionTriggered);
388         ADD_CONTEXTMENU_ACTION(ui->editCustomAvs2YUVParams, QIcon(":/buttons/page_copy.png"), tr("Copy to Clipboard"), copyActionTriggered);
389         ADD_CONTEXTMENU_ACTION(ui->editCustomX264Params, QIcon(":/buttons/page_paste.png"), tr("Paste from Clipboard"), pasteActionTriggered);
390         ADD_CONTEXTMENU_ACTION(ui->editCustomAvs2YUVParams, QIcon(":/buttons/page_paste.png"), tr("Paste from Clipboard"), pasteActionTriggered);
391
392         //Setup template selector
393         loadTemplateList();
394         connect(ui->cbxTemplate, SIGNAL(currentIndexChanged(int)), this, SLOT(templateSelected()));
395
396         //Force initial UI update
397         encoderIndexChanged(ui->cbxEncoderType->currentIndex());
398         m_monitorConfigChanges = true;
399 }
400
401 AddJobDialog::~AddJobDialog(void)
402 {
403         //Free templates
404         for(int i = 0; i < ui->cbxTemplate->model()->rowCount(); i++)
405         {
406                 if(ui->cbxTemplate->itemText(i).startsWith("<") || ui->cbxTemplate->itemText(i).endsWith(">"))
407                 {
408                         continue;
409                 }
410                 const OptionsModel *item = reinterpret_cast<const OptionsModel*>(ui->cbxTemplate->itemData(i).value<const void*>());
411                 ui->cbxTemplate->setItemData(i, QVariant::fromValue<const void*>(NULL));
412                 MUTILS_DELETE(item);
413         }
414
415         //Free validators
416         if(const QValidator *tmp = ui->editCustomX264Params->validator())
417         {
418                 ui->editCustomX264Params->setValidator(NULL);
419                 MUTILS_DELETE(tmp);
420         }
421         if(const QValidator *tmp = ui->editCustomAvs2YUVParams->validator())
422         {
423                 ui->editCustomAvs2YUVParams->setValidator(NULL);
424                 MUTILS_DELETE(tmp);
425         }
426
427         MUTILS_DELETE(m_defaults);
428         delete ui;
429 }
430
431 ///////////////////////////////////////////////////////////////////////////////
432 // Events
433 ///////////////////////////////////////////////////////////////////////////////
434
435 void AddJobDialog::showEvent(QShowEvent *event)
436 {
437         QDialog::showEvent(event);
438         templateSelected();
439
440         if((!ui->editSource->text().isEmpty()) && ui->editOutput->text().isEmpty())
441         {
442                 QString outPath = generateOutputFileName(QDir::fromNativeSeparators(ui->editSource->text()), m_recentlyUsed->outputDirectory(), m_recentlyUsed->filterIndex(), m_preferences->getSaveToSourcePath());
443                 ui->editOutput->setText(QDir::toNativeSeparators(outPath));
444                 ui->buttonAccept->setFocus();
445         }
446
447         ui->labelNotificationX264->hide();
448         ui->iconNotificationX264->hide();
449         ui->labelNotificationAvs2YUV->hide();
450         ui->iconNotificationAvs2YUV->hide();
451
452         //Enable drag&drop support for this window, required for Qt v4.8.4+
453         setAcceptDrops(true);
454 }
455
456 bool AddJobDialog::eventFilter(QObject *o, QEvent *e)
457 {
458         if((o == ui->labelHelpScreenX264) && (e->type() == QEvent::MouseButtonPress))
459         {
460                 OptionsModel options(m_sysinfo); saveOptions(&options);
461                 QScopedPointer<HelpDialog> helpScreen(new HelpDialog(this, false, m_sysinfo, &options, m_preferences));
462                 helpScreen->exec();
463         }
464         else if((o == ui->labelHelpScreenAvs2YUV) && (e->type() == QEvent::MouseButtonPress))
465         {
466                 OptionsModel options(m_sysinfo); saveOptions(&options);
467                 QScopedPointer<HelpDialog> helpScreen(new HelpDialog(this, true, m_sysinfo, &options, m_preferences));
468                 helpScreen->exec();
469         }
470         else if((o == ui->editCustomX264Params) && (e->type() == QEvent::FocusOut))
471         {
472                 ui->editCustomX264Params->setText(ui->editCustomX264Params->text().simplified());
473         }
474         else if((o == ui->editCustomAvs2YUVParams) && (e->type() == QEvent::FocusOut))
475         {
476                 ui->editCustomAvs2YUVParams->setText(ui->editCustomAvs2YUVParams->text().simplified());
477         }
478         return false;
479 }
480
481 void AddJobDialog::dragEnterEvent(QDragEnterEvent *event)
482 {
483         bool accept[2] = {false, false};
484
485         foreach(const QString &fmt, event->mimeData()->formats())
486         {
487                 accept[0] = accept[0] || fmt.contains("text/uri-list", Qt::CaseInsensitive);
488                 accept[1] = accept[1] || fmt.contains("FileNameW", Qt::CaseInsensitive);
489         }
490
491         if(accept[0] && accept[1])
492         {
493                 event->acceptProposedAction();
494         }
495 }
496
497 void AddJobDialog::dropEvent(QDropEvent *event)
498 {
499         QString droppedFile;
500         QList<QUrl> urls = event->mimeData()->urls();
501
502         if(urls.count() > 1)
503         {
504                 QDragEnterEvent dragEvent(event->pos(), event->proposedAction(), event->mimeData(), Qt::NoButton, Qt::NoModifier);
505                 if(qApp->notify(parent(), &dragEvent))
506                 {
507                         qApp->notify(parent(), event);
508                         reject(); return;
509                 }
510         }
511
512         while((!urls.isEmpty()) && droppedFile.isEmpty())
513         {
514                 QUrl currentUrl = urls.takeFirst();
515                 QFileInfo file(currentUrl.toLocalFile());
516                 if(file.exists() && file.isFile())
517                 {
518                         qDebug("AddJobDialog::dropEvent: %s", file.canonicalFilePath().toUtf8().constData());
519                         droppedFile = file.canonicalFilePath();
520                 }
521         }
522         
523         if(!droppedFile.isEmpty())
524         {
525                 const QString outFileName = generateOutputFileName(droppedFile, currentOutputPath(), currentOutputIndx(), m_preferences->getSaveToSourcePath());
526                 ui->editSource->setText(QDir::toNativeSeparators(droppedFile));
527                 ui->editOutput->setText(QDir::toNativeSeparators(outFileName));
528         }
529 }
530
531 ///////////////////////////////////////////////////////////////////////////////
532 // Slots
533 ///////////////////////////////////////////////////////////////////////////////
534
535 void AddJobDialog::encoderIndexChanged(int index)
536 {
537         const OptionsModel::EncType encType = static_cast<OptionsModel::EncType>(ui->cbxEncoderType->itemData(ui->cbxEncoderType->currentIndex()).toInt());
538         const AbstractEncoderInfo &encoderInfo = EncoderFactory::getEncoderInfo(encType);
539
540         //Update encoder variants
541         const QFlags<OptionsModel::EncVariant> variants = encoderInfo.getVariants();
542         ui->cbxEncoderVariant->clear();
543         for(OptionsModel::EncVariant varnt = OptionsModel::EncVariant_MIN; varnt <= OptionsModel::EncVariant_MAX; SHFL(varnt))
544         {
545                 if(variants.testFlag(varnt))
546                 {
547                         QString varntText;
548                         switch(varnt)
549                         {
550                                 case OptionsModel::EncVariant_8Bit:  varntText = tr("8-Bit");  break;
551                                 case OptionsModel::EncVariant_10Bit: varntText = tr("10-Bit"); break;
552                                 case OptionsModel::EncVariant_12Bit: varntText = tr("12-Bit"); break;
553                                 default: MUTILS_THROW("Bad encoder variant!");
554                         }
555                         ui->cbxEncoderVariant->addItem(varntText, QVariant(varnt));
556                 }
557         }
558
559         //Update presets
560         const QStringList presets = encoderInfo.getPresets();
561         if(presets.empty())
562         {
563                 ui->cbxPreset->setEnabled(false);
564                 ui->cbxPreset->setCurrentIndex(0);
565         }
566         else
567         {
568                 ui->cbxPreset->setEnabled(true);
569                 ui->cbxPreset->clear();
570                 ui->cbxPreset->addItem(QString::fromLatin1(OptionsModel::SETTING_UNSPECIFIED));
571                 ui->cbxPreset->addItems(presets);
572         }
573
574         //Update tunings
575         const QStringList tunings = encoderInfo.getTunings();
576         if(tunings.empty())
577         {
578                 ui->cbxTuning->setEnabled(false);
579                 ui->cbxTuning->setCurrentIndex(0);
580         }
581         else
582         {
583                 ui->cbxTuning->setEnabled(true);
584                 ui->cbxTuning->clear();
585                 ui->cbxTuning->addItem(QString::fromLatin1(OptionsModel::SETTING_UNSPECIFIED));
586                 ui->cbxTuning->addItems(tunings);
587         }
588
589         variantIndexChanged(ui->cbxEncoderVariant->currentIndex());
590 }
591
592 void AddJobDialog::variantIndexChanged(int index)
593 {
594         const OptionsModel::EncType encType = static_cast<OptionsModel::EncType>(ui->cbxEncoderType->itemData(ui->cbxEncoderType->currentIndex()).toInt());
595         const AbstractEncoderInfo &encoderInfo = EncoderFactory::getEncoderInfo(encType);
596
597         //Update encoder profiles
598         const QStringList profiles = encoderInfo.getProfiles(static_cast<OptionsModel::EncVariant>(ui->cbxEncoderVariant->itemData(index).toInt()));
599         if(profiles.empty())
600         {
601                 ui->cbxProfile->setEnabled(false);
602                 ui->cbxProfile->setCurrentIndex(0);
603         }
604         else
605         {
606                 ui->cbxProfile->setEnabled(true);
607                 ui->cbxProfile->clear();
608                 ui->cbxProfile->addItem(QString::fromLatin1(OptionsModel::PROFILE_UNRESTRICTED));
609                 ui->cbxProfile->addItems(profiles);
610         }
611         
612         modeIndexChanged(ui->cbxRateControlMode->currentIndex());
613 }
614
615 void AddJobDialog::modeIndexChanged(int index)
616 {
617         ui->spinQuantizer->setEnabled(index == OptionsModel::RCMode_CRF || index == OptionsModel::RCMode_CQ);
618         ui->spinBitrate  ->setEnabled(index == OptionsModel::RCMode_ABR || index == OptionsModel::RCMode_2Pass);
619 }
620
621 void AddJobDialog::accept(void)
622 {
623         //Check 64-Bit support
624         if((ui->cbxEncoderArch->currentIndex() == OptionsModel::EncArch_x86_64) && (!m_sysinfo->getCPUFeatures(SysinfoModel::CPUFeatures_X64)))
625         {
626                 QMessageBox::warning(this, tr("64-Bit unsupported!"), tr("<nobr>Sorry, this computer does <b>not</b> support 64-Bit encoders!</nobr>"));
627                 ui->cbxEncoderArch->setCurrentIndex(OptionsModel::EncArch_x86_32);
628                 return;
629         }
630         
631         //Selection complete?
632         if(ui->editSource->text().trimmed().isEmpty())
633         {
634                 QMessageBox::warning(this, tr("Not Found!"), tr("<nobr>Please select a valid source file first!<(nobr>"));
635                 return;
636         }
637         if(ui->editOutput->text().trimmed().isEmpty())
638         {
639                 QMessageBox::warning(this, tr("Not Selected!"), tr("<nobr>Please select a valid output file first!</nobr>"));
640                 return;
641         }
642
643         //Does source exist?
644         QFileInfo sourceFile = QFileInfo(this->sourceFile());
645         if(!(sourceFile.exists() && sourceFile.isFile()))
646         {
647                 QMessageBox::warning(this, tr("Not Found!"), tr("<nobr>The selected source file could not be found!</nobr>"));
648                 return;
649         }
650
651         //Get encoder info
652         const OptionsModel::EncType encType = static_cast<OptionsModel::EncType>(ui->cbxEncoderType->itemData(ui->cbxEncoderType->currentIndex()).toInt());
653         const AbstractEncoderInfo &encoderInfo = EncoderFactory::getEncoderInfo(encType);
654
655         //Is selected RC mode supported?
656         if(!encoderInfo.isRCModeSupported(static_cast<OptionsModel::RCMode>(ui->cbxRateControlMode->currentIndex())))
657         {
658                 QMessageBox::warning(this, tr("Bad RC Mode!"), tr("<nobr>The selected RC mode is not supported by the selected encoder!</nobr>"));
659                 for(int i = 0; i < ui->cbxRateControlMode->count(); i++)
660                 {
661                         if(encoderInfo.isRCModeSupported(static_cast<OptionsModel::RCMode>(i)))
662                         {
663                                 ui->cbxRateControlMode->setCurrentIndex(i);
664                                 break;
665                         }
666                 }
667                 return;
668         }
669
670         //Is the type of the source file supported?
671         const int sourceType = MediaInfo::analyze(sourceFile.canonicalFilePath());
672         if(sourceType == MediaInfo::FILETYPE_AVISYNTH)
673         {
674                 if(!m_sysinfo->hasAvisynth())
675                 {
676                         if(QMessageBox::warning(this, tr("Avisynth unsupported!"), tr("<nobr>An Avisynth script was selected as input, although Avisynth is <b>not</b> available!</nobr>"), tr("Abort"), tr("Ignore (at your own risk!)")) != 1)
677                         {
678                                 return;
679                         }
680                 }
681         }
682         else if(sourceType == MediaInfo::FILETYPE_VAPOURSYNTH)
683         {
684                 if(!m_sysinfo->hasVapourSynth())
685                 {
686                         if(QMessageBox::warning(this, tr("VapurSynth unsupported!"), tr("<nobr>A VapourSynth script was selected as input, although VapourSynth is <b>not/<b> available!</nobr>"), tr("Abort"), tr("Ignore (at your own risk!)")) != 1)
687                         {
688                                 return;
689                         }
690                 }
691         }
692         else if(!encoderInfo.isInputTypeSupported(sourceType))
693         {
694                 if(QMessageBox::warning(this, tr("Unsupported input format"), tr("<nobr>The selected encoder does <b>not</b> support the selected input format!</nobr>"), tr("Abort"), tr("Ignore (at your own risk!)")) != 1)
695                 {
696                         return;
697                 }
698         }
699         
700         //Is output file extension supported by encoder?
701         const QStringList outputFormats = encoderInfo.supportedOutputFormats();
702         QFileInfo outputFile = QFileInfo(this->outputFile());
703         if(!outputFormats.contains(outputFile.suffix(), Qt::CaseInsensitive))
704         {
705                 QMessageBox::warning(this, tr("Unsupported output format"), tr("<nobr>Sorry, the selected encoder does not support the selected output format!</nobr>"));
706                 ui->editOutput->setText(QDir::toNativeSeparators(QString("%1/%2.%3").arg(outputFile.absolutePath(), outputFile.completeBaseName(), outputFormats.first())));
707                 return;
708         }
709
710         //Does output file already exist?
711         if(outputFile.exists() && outputFile.isFile())
712         {
713                 int ret = QMessageBox::question(this, tr("Already Exists!"), tr("<nobr>Output file already exists! Overwrite?</nobr>"), QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
714                 if(ret != QMessageBox::Yes) return;
715         }
716         if(outputFile.exists() && (!outputFile.isFile()))
717         {
718                 QMessageBox::warning(this, tr("Not a File!"), tr("<nobr>Selected output file does not appear to be a valid file!</nobr>"));
719                 return;
720         }
721
722         //Is destination dir writable?
723         QFileInfo outputDir = QFileInfo(outputFile.absolutePath());
724         if(!(outputDir.exists() && outputDir.isDir() && outputDir.isWritable()))
725         {
726                 QMessageBox::warning(this, tr("Not Writable!"), tr("<nobr>Output directory does not exist or is not writable!</nobr>"));
727                 return;
728         }
729
730         //Custom parameters okay?
731         if(!ui->editCustomX264Params->hasAcceptableInput())
732         {
733                 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);
734                 if(ret != QMessageBox::Ignore) return;
735         }
736
737         //Update recently used
738         m_recentlyUsed->setFilterIndex(currentOutputIndx());
739         m_recentlyUsed->setSourceDirectory(currentSourcePath());
740         m_recentlyUsed->setOutputDirectory(currentOutputPath());
741         RecentlyUsed::saveRecentlyUsed(m_recentlyUsed);
742
743         //Save options
744         saveOptions(m_options);
745         QDialog::accept();
746 }
747
748 void AddJobDialog::browseButtonClicked(void)
749 {
750         if(QObject::sender() == ui->buttonBrowseSource)
751         {
752                 QString filePath = QFileDialog::getOpenFileName(this, tr("Open Source File"), currentSourcePath(true), getInputFilterLst(), NULL, QFileDialog::DontUseNativeDialog);
753                 if(!(filePath.isNull() || filePath.isEmpty()))
754                 {
755                         QString destFile = generateOutputFileName(filePath, currentOutputPath(), currentOutputIndx(), m_preferences->getSaveToSourcePath());
756                         ui->editSource->setText(QDir::toNativeSeparators(filePath));
757                         ui->editOutput->setText(QDir::toNativeSeparators(destFile));
758                 }
759         }
760         else if(QObject::sender() == ui->buttonBrowseOutput)
761         {
762                 QString selectedType = getFilterStr(currentOutputIndx());
763                 QString filePath = QFileDialog::getSaveFileName(this, tr("Choose Output File"), currentOutputPath(true), getFilterLst(), &selectedType, QFileDialog::DontUseNativeDialog | QFileDialog::DontConfirmOverwrite);
764
765                 if(!(filePath.isNull() || filePath.isEmpty()))
766                 {
767                         if(getFilterIdx(QFileInfo(filePath).suffix()) < 0)
768                         {
769                                 int tempIndex = -1;
770                                 QRegExp regExp("\\(\\*\\.(\\w+)\\)");
771                                 if(regExp.lastIndexIn(selectedType) >= 0)
772                                 {
773                                         tempIndex = getFilterIdx(regExp.cap(1));
774                                 }
775                                 if(tempIndex < 0)
776                                 {
777                                         tempIndex = m_recentlyUsed->filterIndex();
778                                 }
779                                 filePath = QString("%1.%2").arg(filePath, getFilterExt(tempIndex));
780                         }
781                         ui->editOutput->setText(QDir::toNativeSeparators(filePath));
782                 }
783         }
784 }
785
786 void AddJobDialog::configurationChanged(void)
787 {
788         if(!m_monitorConfigChanges)
789         {
790                 return;
791         }
792
793         const OptionsModel* options = reinterpret_cast<const OptionsModel*>(ui->cbxTemplate->itemData(ui->cbxTemplate->currentIndex()).value<const void*>());
794         if(options)
795         {
796                 ui->cbxTemplate->blockSignals(true);
797                 ui->cbxTemplate->insertItem(0, tr("<Modified Configuration>"), QVariant::fromValue<const void*>(NULL));
798                 ui->cbxTemplate->setCurrentIndex(0);
799                 ui->cbxTemplate->blockSignals(false);
800         }
801 }
802
803 void AddJobDialog::templateSelected(void)
804 {
805         const OptionsModel* options = reinterpret_cast<const OptionsModel*>(ui->cbxTemplate->itemData(ui->cbxTemplate->currentIndex()).value<const void*>());
806         if(options)
807         {
808                 qDebug("Loading options!");
809                 m_lastTemplateName = ui->cbxTemplate->itemText(ui->cbxTemplate->currentIndex());
810                 REMOVE_USAFED_ITEM;
811                 restoreOptions(options);
812         }
813 }
814
815 void AddJobDialog::saveTemplateButtonClicked(void)
816 {
817         qDebug("Saving template");
818
819         QString name = m_lastTemplateName;
820         if(name.isEmpty() || name.contains('<') || name.contains('>'))
821         {
822                 name = tr("New Template");
823                 int n = 1;
824                 while(OptionsModel::templateExists(name))
825                 {
826                         name = tr("New Template (%1)").arg(QString::number(++n));
827                 }
828         }
829
830         QScopedPointer<OptionsModel> options(new OptionsModel(m_sysinfo));
831         saveOptions(options.data());
832
833         if(options->equals(m_defaults))
834         {
835                 QMessageBox::warning (this, tr("Oups"), tr("<nobr>It makes no sense to save the default settings!</nobr>"));
836                 ui->cbxTemplate->blockSignals(true);
837                 ui->cbxTemplate->setCurrentIndex(0);
838                 ui->cbxTemplate->blockSignals(false);
839                 REMOVE_USAFED_ITEM;
840                 return;
841         }
842
843         for(int i = 0; i < ui->cbxTemplate->count(); i++)
844         {
845                 const QString tempName = ui->cbxTemplate->itemText(i);
846                 if(tempName.contains('<') || tempName.contains('>'))
847                 {
848                         continue;
849                 }
850                 const OptionsModel* test = reinterpret_cast<const OptionsModel*>(ui->cbxTemplate->itemData(i).value<const void*>());
851                 if(test != NULL)
852                 {
853                         if(options->equals(test))
854                         {
855                                 QMessageBox::information (this, tr("Oups"), tr("<nobr>The current settings are already saved as template:<br><b>%1</b></nobr>").arg(ui->cbxTemplate->itemText(i)));
856                                 ui->cbxTemplate->blockSignals(true);
857                                 ui->cbxTemplate->setCurrentIndex(i);
858                                 ui->cbxTemplate->blockSignals(false);
859                                 REMOVE_USAFED_ITEM;
860                                 return;
861                         }
862                 }
863         }
864
865         forever
866         {
867                 bool ok = false;
868                 
869                 QStringList items;
870                 items << name;
871                 for(int i = 0; i < ui->cbxTemplate->count(); i++)
872                 {
873                         const QString tempName = ui->cbxTemplate->itemText(i);
874                         if(!(tempName.contains('<') || tempName.contains('>')))
875                         {
876                                 items << tempName;
877                         }
878                 }
879                 
880                 name = QInputDialog::getItem(this, tr("Save Template"), tr("Please enter the name of the template:").leftJustified(144, ' '), items, 0, true, &ok).simplified();
881                 if(!ok)
882                 {
883                         return;
884                 }
885                 if(name.isEmpty())
886                 {
887                         continue;
888                 }
889                 if(name.contains('<') || name.contains('>') || name.contains('\\') || name.contains('/') || name.contains('"'))
890                 {
891                         QMessageBox::warning (this, tr("Invalid Name"), tr("<nobr>Sorry, the name you have entered is invalid!</nobr>"));
892                         while(name.contains('<')) name.remove('<');
893                         while(name.contains('>')) name.remove('>');
894                         while(name.contains('\\')) name.remove('\\');
895                         while(name.contains('/')) name.remove('/');
896                         while(name.contains('"')) name.remove('"');
897                         name = name.simplified();
898                         continue;
899                 }
900                 if(OptionsModel::templateExists(name))
901                 {
902                         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);
903                         if(ret != QMessageBox::Yes)
904                         {
905                                 continue;
906                         }
907                 }
908                 break;
909         }
910         
911         if(!OptionsModel::saveTemplate(options.data(), name))
912         {
913                 QMessageBox::critical(this, tr("Save Failed"), tr("Sorry, the template could not be saved!"));
914                 return;
915         }
916         
917         ui->cbxTemplate->blockSignals(true);
918         for(int i = 0; i < ui->cbxTemplate->count(); i++)
919         {
920                 if(ui->cbxTemplate->itemText(i).compare(name, Qt::CaseInsensitive) == 0)
921                 {
922                         QScopedPointer<const OptionsModel> oldItem(reinterpret_cast<const OptionsModel*>(ui->cbxTemplate->itemData(i).value<const void*>()));
923                         ui->cbxTemplate->setItemData(i, QVariant::fromValue<const void*>(options.take()));
924                         ui->cbxTemplate->setCurrentIndex(i);
925                 }
926         }
927         if(!options.isNull())
928         {
929                 const int index = ui->cbxTemplate->model()->rowCount();
930                 ui->cbxTemplate->insertItem(index, name, QVariant::fromValue<const void*>(options.take()));
931                 ui->cbxTemplate->setCurrentIndex(index);
932         }
933         ui->cbxTemplate->blockSignals(false);
934
935         m_lastTemplateName = name;
936         REMOVE_USAFED_ITEM;
937 }
938
939 void AddJobDialog::deleteTemplateButtonClicked(void)
940 {
941         const int index = ui->cbxTemplate->currentIndex();
942         QString name = ui->cbxTemplate->itemText(index);
943
944         if(name.contains('<') || name.contains('>') || name.contains('\\') || name.contains('/'))
945         {
946                 QMessageBox::warning (this, tr("Invalid Item"), tr("Sorry, the selected item cannot be deleted!"));
947                 return;
948         }
949
950         int ret = QMessageBox::question (this, tr("Delete Template"), tr("<nobr>Do you really want to delete the selected template?<br><b>%1</b></nobr>").arg(name), QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
951         if(ret != QMessageBox::Yes)
952         {
953                 return;
954         }
955         
956         OptionsModel::deleteTemplate(name);
957         const OptionsModel *item = reinterpret_cast<const OptionsModel*>(ui->cbxTemplate->itemData(index).value<const void*>());
958         ui->cbxTemplate->removeItem(index);
959         MUTILS_DELETE(item);
960 }
961
962 void AddJobDialog::editorActionTriggered(void)
963 {
964
965         if(QAction *action = dynamic_cast<QAction*>(QObject::sender()))
966         {
967                 QLineEdit *lineEdit = reinterpret_cast<QLineEdit*>(action->data().value<void*>());
968                 
969                 EditorDialog *editor = new EditorDialog(this);
970                 editor->setEditText(lineEdit->text());
971
972                 if(editor->exec() == QDialog::Accepted)
973                 {
974                         lineEdit->setText(editor->getEditText());
975                 }
976
977                 MUTILS_DELETE(editor);
978         }
979 }
980
981 void AddJobDialog::copyActionTriggered(void)
982 {
983         if(QAction *action = dynamic_cast<QAction*>(QObject::sender()))
984         {
985                 QClipboard *clipboard = QApplication::clipboard();
986                 QLineEdit *lineEdit = reinterpret_cast<QLineEdit*>(action->data().value<void*>());
987                 QString text = lineEdit->hasSelectedText() ? lineEdit->selectedText() : lineEdit->text();
988                 clipboard->setText(text);
989         }
990 }
991
992 void AddJobDialog::pasteActionTriggered(void)
993 {
994         if(QAction *action = dynamic_cast<QAction*>(QObject::sender()))
995         {
996                 QClipboard *clipboard = QApplication::clipboard();
997                 QLineEdit *lineEdit = reinterpret_cast<QLineEdit*>(action->data().value<void*>());
998                 QString text = clipboard->text();
999                 if(!text.isEmpty()) lineEdit->setText(text);
1000         }
1001 }
1002
1003 ///////////////////////////////////////////////////////////////////////////////
1004 // Public functions
1005 ///////////////////////////////////////////////////////////////////////////////
1006
1007 QString AddJobDialog::sourceFile(void)
1008 {
1009         return QDir::fromNativeSeparators(ui->editSource->text());
1010 }
1011
1012 QString AddJobDialog::outputFile(void)
1013 {
1014         return QDir::fromNativeSeparators(ui->editOutput->text());
1015 }
1016
1017 bool AddJobDialog::runImmediately(void)
1018 {
1019         return ui->checkBoxRun->isChecked();
1020 }
1021
1022 bool AddJobDialog::applyToAll(void)
1023 {
1024         return ui->checkBoxApplyToAll->isChecked();
1025 }
1026
1027 void AddJobDialog::setRunImmediately(bool run)
1028 {
1029         ui->checkBoxRun->setChecked(run);
1030 }
1031
1032 void AddJobDialog::setSourceFile(const QString &path)
1033 {
1034         ui->editSource->setText(QDir::toNativeSeparators(path));
1035 }
1036
1037 void AddJobDialog::setOutputFile(const QString &path)
1038 {
1039         ui->editOutput->setText(QDir::toNativeSeparators(path));}
1040
1041 void AddJobDialog::setSourceEditable(const bool editable)
1042 {
1043         ui->buttonBrowseSource->setEnabled(editable);
1044 }
1045
1046 void AddJobDialog::setApplyToAllVisible(const bool visible)
1047 {
1048         ui->checkBoxApplyToAll->setVisible(visible);
1049 }
1050
1051 ///////////////////////////////////////////////////////////////////////////////
1052 // Private functions
1053 ///////////////////////////////////////////////////////////////////////////////
1054
1055 void AddJobDialog::loadTemplateList(void)
1056 {
1057         ui->cbxTemplate->addItem(tr("<Default>"), QVariant::fromValue<const void*>(m_defaults));
1058         ui->cbxTemplate->setCurrentIndex(0);
1059
1060         QMap<QString, OptionsModel*> templates = OptionsModel::loadAllTemplates(m_sysinfo);
1061         QStringList templateNames = templates.keys();
1062         templateNames.sort();
1063
1064         for(QStringList::ConstIterator current = templateNames.constBegin(); current != templateNames.constEnd(); current++)
1065         {
1066                 OptionsModel *currentTemplate = templates.take(*current);
1067                 ui->cbxTemplate->addItem(*current, QVariant::fromValue<const void*>(currentTemplate));
1068                 if(currentTemplate->equals(m_options))
1069                 {
1070                         ui->cbxTemplate->setCurrentIndex(ui->cbxTemplate->count() - 1);
1071                 }
1072         }
1073
1074         if((ui->cbxTemplate->currentIndex() == 0) && (!m_options->equals(m_defaults)))
1075         {
1076                 qWarning("Not the default -> recently used!");
1077                 ui->cbxTemplate->insertItem(1, tr("<Recently Used>"), QVariant::fromValue<const void*>(m_options));
1078                 ui->cbxTemplate->setCurrentIndex(1);
1079         }
1080 }
1081
1082 void AddJobDialog::updateComboBox(QComboBox *const cbox, const QString &text)
1083 {
1084         int index = 0;
1085         if(QAbstractItemModel *model = cbox->model())
1086         {
1087                 for(int i = 0; i < cbox->model()->rowCount(); i++)
1088                 {
1089                         if(model->data(model->index(i, 0, QModelIndex())).toString().compare(text, Qt::CaseInsensitive) == 0)
1090                         {
1091                                 index = i;
1092                                 break;
1093                         }
1094                 }
1095         }
1096         cbox->setCurrentIndex(index);
1097 }
1098
1099 void AddJobDialog::updateComboBox(QComboBox *const cbox, const int &data)
1100 {
1101         int index = 0;
1102         if(QAbstractItemModel *model = cbox->model())
1103         {
1104                 for(int i = 0; i < cbox->model()->rowCount(); i++)
1105                 {
1106                         if(cbox->itemData(i).toInt() == data)
1107                         {
1108                                 index = i;
1109                                 break;
1110                         }
1111                 }
1112         }
1113         cbox->setCurrentIndex(index);
1114 }
1115
1116 void AddJobDialog::restoreOptions(const OptionsModel *options)
1117 {
1118         DisableHelperRAII disbale(&m_monitorConfigChanges);
1119
1120         updateComboBox(ui->cbxEncoderType,     options->encType());
1121         updateComboBox(ui->cbxEncoderArch,     options->encArch());
1122         updateComboBox(ui->cbxEncoderVariant,  options->encVariant());
1123         updateComboBox(ui->cbxRateControlMode, options->rcMode());
1124
1125         ui->spinQuantizer->setValue(options->quantizer());
1126         ui->spinBitrate  ->setValue(options->bitrate());
1127
1128         updateComboBox(ui->cbxPreset,  options->preset());
1129         updateComboBox(ui->cbxTuning,  options->tune());
1130         updateComboBox(ui->cbxProfile, options->profile());
1131
1132         ui->editCustomX264Params   ->setText(options->customEncParams());
1133         ui->editCustomAvs2YUVParams->setText(options->customAvs2YUV());
1134 }
1135
1136 void AddJobDialog::saveOptions(OptionsModel *options)
1137 {
1138         options->setEncType   (static_cast<OptionsModel::EncType>   (ui->cbxEncoderType    ->itemData(ui->cbxEncoderType    ->currentIndex()).toInt()));
1139         options->setEncArch   (static_cast<OptionsModel::EncArch>   (ui->cbxEncoderArch    ->itemData(ui->cbxEncoderArch    ->currentIndex()).toInt()));
1140         options->setEncVariant(static_cast<OptionsModel::EncVariant>(ui->cbxEncoderVariant ->itemData(ui->cbxEncoderVariant ->currentIndex()).toInt()));
1141         options->setRCMode    (static_cast<OptionsModel::RCMode>    (ui->cbxRateControlMode->itemData(ui->cbxRateControlMode->currentIndex()).toInt()));
1142         
1143         options->setQuantizer(ui->spinQuantizer->value());
1144         options->setBitrate(ui->spinBitrate->value());
1145         
1146         options->setPreset (ui->cbxPreset ->model()->data(ui->cbxPreset ->model()->index(ui->cbxPreset ->currentIndex(), 0)).toString());
1147         options->setTune   (ui->cbxTuning ->model()->data(ui->cbxTuning ->model()->index(ui->cbxTuning ->currentIndex(), 0)).toString());
1148         options->setProfile(ui->cbxProfile->model()->data(ui->cbxProfile->model()->index(ui->cbxProfile->currentIndex(), 0)).toString());
1149
1150         options->setCustomEncParams(ui->editCustomX264Params->hasAcceptableInput() ? ui->editCustomX264Params->text().simplified() : QString());
1151         options->setCustomAvs2YUV(ui->editCustomAvs2YUVParams->hasAcceptableInput() ? ui->editCustomAvs2YUVParams->text().simplified() : QString());
1152 }
1153
1154 QString AddJobDialog::currentSourcePath(const bool bWithName)
1155 {
1156         QString path = m_recentlyUsed->sourceDirectory();
1157         QString currentSourceFile = this->sourceFile();
1158         
1159         if(!currentSourceFile.isEmpty())
1160         {
1161                 QString currentSourceDir = QFileInfo(currentSourceFile).absolutePath();
1162                 if(VALID_DIR(currentSourceDir))
1163                 {
1164                         path = currentSourceDir;
1165                 }
1166                 if(bWithName)
1167                 {
1168                         path.append("/").append(QFileInfo(currentSourceFile).fileName());
1169                 }
1170         }
1171
1172         return path;
1173 }
1174
1175 QString AddJobDialog::currentOutputPath(const bool bWithName)
1176 {
1177         QString path = m_recentlyUsed->outputDirectory();
1178         QString currentOutputFile = this->outputFile();
1179         
1180         if(!currentOutputFile.isEmpty())
1181         {
1182                 QString currentOutputDir = QFileInfo(currentOutputFile).absolutePath();
1183                 if(VALID_DIR(currentOutputDir))
1184                 {
1185                         path = currentOutputDir;
1186                 }
1187                 if(bWithName)
1188                 {
1189                         path.append("/").append(QFileInfo(currentOutputFile).fileName());
1190                 }
1191         }
1192
1193         return path;
1194 }
1195
1196 int AddJobDialog::currentOutputIndx(void)
1197 {
1198         const OptionsModel::EncType encType = static_cast<OptionsModel::EncType>(ui->cbxEncoderType->itemData(ui->cbxEncoderType->currentIndex()).toInt());
1199         if(encType == OptionsModel::EncType_X265)
1200         {
1201                 return ARRAY_SIZE(X264_FILE_TYPE_FILTERS) - 1;
1202         }
1203         
1204         int index = m_recentlyUsed->filterIndex();
1205         const QString currentOutputFile = this->outputFile();
1206
1207         if(!currentOutputFile.isEmpty())
1208         {
1209                 const QString currentOutputExtn = QFileInfo(currentOutputFile).suffix();
1210                 const int tempIndex = getFilterIdx(currentOutputExtn);
1211                 if(tempIndex >= 0)
1212                 {
1213                         index = tempIndex;
1214                 }
1215         }
1216
1217         return index;
1218 }
1219
1220 ///////////////////////////////////////////////////////////////////////////////
1221 // Static functions
1222 ///////////////////////////////////////////////////////////////////////////////
1223
1224 QString AddJobDialog::generateOutputFileName(const QString &sourceFilePath, const QString &destinationDirectory, const int filterIndex, const bool saveToSourceDir)
1225 {
1226         QString name = QFileInfo(sourceFilePath).completeBaseName();
1227         QString path = saveToSourceDir ? QFileInfo(sourceFilePath).canonicalPath() : destinationDirectory;
1228         QString fext = getFilterExt(filterIndex);
1229         
1230         if(!VALID_DIR(path))
1231         {
1232                 RecentlyUsed defaults;
1233                 path = defaults.outputDirectory();
1234         }
1235
1236         QString outPath = QString("%1/%2.%3").arg(path, name, fext);
1237
1238         int n = 2;
1239         while(QFileInfo(outPath).exists())
1240         {
1241                 outPath = QString("%1/%2 (%3).%4").arg(path, name, QString::number(n++), fext);
1242         }
1243
1244         return outPath;
1245 }
1246
1247 /* ------------------------------------------------------------------------- */
1248
1249 QString AddJobDialog::getFilterExt(const int filterIndex)
1250 {
1251         const int count = ARRAY_SIZE(X264_FILE_TYPE_FILTERS);
1252
1253         if((filterIndex >= 0) && (filterIndex < count))
1254         {
1255                 return QString::fromLatin1(X264_FILE_TYPE_FILTERS[filterIndex].pcExt);
1256         }
1257
1258         return QString::fromLatin1(X264_FILE_TYPE_FILTERS[0].pcExt);
1259 }
1260
1261 int AddJobDialog::getFilterIdx(const QString &fileExt)
1262 {
1263         const int count = ARRAY_SIZE(X264_FILE_TYPE_FILTERS);
1264
1265         for(int i = 0; i < count; i++)
1266         {
1267                 if(fileExt.compare(QString::fromLatin1(X264_FILE_TYPE_FILTERS[i].pcExt), Qt::CaseInsensitive) == 0)
1268                 {
1269                         return i;
1270                 }
1271         }
1272
1273         return -1;
1274 }
1275
1276 QString AddJobDialog::getFilterStr(const int filterIndex)
1277 {
1278         const int count = ARRAY_SIZE(X264_FILE_TYPE_FILTERS);
1279
1280         if((filterIndex >= 0) && (filterIndex < count))
1281         {
1282                 return QString("%1 (*.%2)").arg(QString::fromLatin1(X264_FILE_TYPE_FILTERS[filterIndex].pcStr), QString::fromLatin1(X264_FILE_TYPE_FILTERS[filterIndex].pcExt));
1283         }
1284
1285         return QString("%1 (*.%2)").arg(QString::fromLatin1(X264_FILE_TYPE_FILTERS[0].pcStr), QString::fromLatin1(X264_FILE_TYPE_FILTERS[0].pcExt));
1286 }
1287
1288 QString AddJobDialog::getFilterLst(void)
1289 {
1290         QStringList filters;
1291         const int count = ARRAY_SIZE(X264_FILE_TYPE_FILTERS);
1292         
1293         for(int i = 0; i < count; i++)
1294         {
1295                 filters << QString("%1 (*.%2)").arg(QString::fromLatin1(X264_FILE_TYPE_FILTERS[i].pcStr), QString::fromLatin1(X264_FILE_TYPE_FILTERS[i].pcExt));
1296         }
1297
1298         return filters.join(";;");
1299 }
1300
1301 QString AddJobDialog::getInputFilterLst(void)
1302 {
1303         static const struct
1304         {
1305                 const char *name;
1306                 const char *fext;
1307         }
1308         s_filters[] =
1309         {
1310                 {"Avisynth Scripts", "avs"},
1311                 {"VapourSynth Scripts", "vpy"},
1312                 {"Matroska Files", "mkv"},
1313                 {"MPEG-4 Part 14 Container", "mp4"},
1314                 {"Audio Video Interleaved", "avi"},
1315                 {"Flash Video", "flv"},
1316                 {"YUV4MPEG2 Stream", "y4m"},
1317                 {"Uncompresses YUV Data", "yuv"},
1318         };
1319
1320         const int count = ARRAY_SIZE(s_filters);
1321
1322         QString allTypes;
1323         for(size_t index = 0; index < count; index++)
1324         {
1325
1326                 allTypes += QString((index > 0) ? " *.%1" : "*.%1").arg(QString::fromLatin1(s_filters[index].fext));
1327         }
1328         
1329         QStringList filters;
1330         filters << QString("All supported files (%1)").arg(allTypes);
1331
1332         for(size_t index = 0; index < count; index++)
1333         {
1334                 filters << QString("%1 (*.%2)").arg(QString::fromLatin1(s_filters[index].name), QString::fromLatin1(s_filters[index].fext));
1335         }
1336                 
1337         filters << QString("All files (*.*)");
1338         return filters.join(";;");
1339 }
1340
1341