1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2012 LoRd_MuldeR <MuldeR2@GMX.de>
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License along
16 // with this program; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
22 #include "win_addJob.h"
25 #include "model_options.h"
27 #include "win_editor.h"
31 #include <QCloseEvent>
32 #include <QMessageBox>
33 #include <QFileDialog>
34 #include <QDesktopServices>
37 #include <QInputDialog>
43 #define VALID_DIR(PATH) ((!(PATH).isEmpty()) && QFileInfo(PATH).exists() && QFileInfo(PATH).isDir())
45 #define REMOVE_USAFED_ITEM \
47 for(int i = 0; i < cbxTemplate->count(); i++) \
49 OptionsModel* temp = reinterpret_cast<OptionsModel*>(cbxTemplate->itemData(i).value<void*>()); \
52 cbxTemplate->blockSignals(true); \
53 cbxTemplate->removeItem(i); \
54 cbxTemplate->blockSignals(false); \
60 #define ADD_CONTEXTMENU_ACTION(WIDGET, ICON, TEXT, SLOTNAME) \
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())); \
68 #define ADD_CONTEXTMENU_SEPARATOR(WIDGET) \
70 QAction *_action = new QAction(this); \
71 _action->setSeparator(true); \
72 WIDGET->addAction(_action); \
75 ///////////////////////////////////////////////////////////////////////////////
77 ///////////////////////////////////////////////////////////////////////////////
79 class StringValidator : public QValidator
82 StringValidator(QLabel *notifier, QLabel *icon)
84 m_notifier(notifier), m_icon(icon)
90 virtual State validate(QString &input, int &pos) const = 0;
92 virtual void fixup(QString &input) const
94 input = input.simplified();
98 QLabel *const m_notifier, *const m_icon;
100 bool checkParam(const QString &input, const QString ¶m, const bool doubleMinus) const
102 static const char c[20] = {' ', '*', '?', '<', '>', '/', '\\', '"', '\'', '!', '+', '#', '&', '%', '=', ',', ';', '.', 'ยด', '`'};
103 const QString prefix = doubleMinus ? QLatin1String("--") : QLatin1String("-");
106 if(param.length() > 1)
108 flag = flag || input.endsWith(QString("%1%2").arg(prefix, param), Qt::CaseInsensitive);
109 for(size_t i = 0; i < sizeof(c); i++)
111 flag = flag || input.contains(QString("%1%2%3").arg(prefix, param, QChar::fromLatin1(c[i])), Qt::CaseInsensitive);
116 flag = flag || input.startsWith(QString("-%1").arg(param));
117 for(size_t i = 0; i < sizeof(c); i++)
119 flag = flag || input.contains(QString("%1-%2").arg(QChar::fromLatin1(c[i]), param), Qt::CaseSensitive);
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(); }
135 if(m_notifier->isVisible()) m_notifier->hide();
136 if(m_icon) { if(m_icon->isVisible()) m_icon->hide(); }
143 class StringValidatorX264 : public StringValidator
146 StringValidatorX264(QLabel *notifier, QLabel *icon) : StringValidator(notifier, icon) {}
148 virtual State validate(QString &input, int &pos) const
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};
153 bool invalid = false;
155 for(size_t i = 0; p[i] && (!invalid); i++)
157 invalid = invalid || checkParam(input, QString::fromLatin1(p[i]), true);
160 return invalid ? QValidator::Intermediate : QValidator::Acceptable;
164 class StringValidatorAvs2YUV : public StringValidator
167 StringValidatorAvs2YUV(QLabel *notifier, QLabel *icon) : StringValidator(notifier, icon) {}
169 virtual State validate(QString &input, int &pos) const
171 static const char* p[] = {"o", "frames", "seek", "raw", "hfyu", "slave", NULL};
173 bool invalid = false;
175 for(size_t i = 0; p[i] && (!invalid); i++)
177 invalid = invalid || checkParam(input, QString::fromLatin1(p[i]), false);
180 return invalid ? QValidator::Intermediate : QValidator::Acceptable;
184 ///////////////////////////////////////////////////////////////////////////////
185 // Constructor & Destructor
186 ///////////////////////////////////////////////////////////////////////////////
188 AddJobDialog::AddJobDialog(QWidget *parent, OptionsModel *options, bool x64supported)
191 m_defaults(new OptionsModel()),
193 m_x64supported(x64supported),
194 m_initialDir_src(QDir::fromNativeSeparators(QDesktopServices::storageLocation(QDesktopServices::MoviesLocation))),
195 m_initialDir_out(QDir::fromNativeSeparators(QDesktopServices::storageLocation(QDesktopServices::MoviesLocation))),
198 //Init the dialog, from the .ui file
200 setWindowFlags(windowFlags() & (~Qt::WindowContextHelpButtonHint));
203 buttonSaveTemplate->setMaximumHeight(20);
204 buttonDeleteTemplate->setMaximumHeight(20);
205 resize(width(), minimumHeight());
206 setMinimumSize(size());
207 setMaximumHeight(height());
209 //Setup file type filter
211 m_types << tr("Matroska Files (*.mkv)");
212 m_types << tr("MPEG-4 Part 14 Container (*.mp4)");
213 m_types << tr("H.264 Elementary Stream (*.264)");
215 //Monitor RC mode combobox
216 connect(cbxRateControlMode, SIGNAL(currentIndexChanged(int)), this, SLOT(modeIndexChanged(int)));
219 connect(buttonBrowseSource, SIGNAL(clicked()), this, SLOT(browseButtonClicked()));
220 connect(buttonBrowseOutput, SIGNAL(clicked()), this, SLOT(browseButtonClicked()));
221 connect(buttonSaveTemplate, SIGNAL(clicked()), this, SLOT(saveTemplateButtonClicked()));
222 connect(buttonDeleteTemplate, SIGNAL(clicked()), this, SLOT(deleteTemplateButtonClicked()));
225 editCustomX264Params->installEventFilter(this);
226 editCustomX264Params->setValidator(new StringValidatorX264(labelNotificationX264, iconNotificationX264));
227 editCustomX264Params->clear();
228 editCustomAvs2YUVParams->installEventFilter(this);
229 editCustomAvs2YUVParams->setValidator(new StringValidatorAvs2YUV(labelNotificationAvs2YUV, iconNotificationAvs2YUV));
230 editCustomAvs2YUVParams->clear();
232 //Install event filter
233 labelHelpScreenX264->installEventFilter(this);
234 labelHelpScreenAvs2YUV->installEventFilter(this);
236 //Monitor for options changes
237 connect(cbxRateControlMode, SIGNAL(currentIndexChanged(int)), this, SLOT(configurationChanged()));
238 connect(spinQuantizer, SIGNAL(valueChanged(double)), this, SLOT(configurationChanged()));
239 connect(spinBitrate, SIGNAL(valueChanged(int)), this, SLOT(configurationChanged()));
240 connect(cbxPreset, SIGNAL(currentIndexChanged(int)), this, SLOT(configurationChanged()));
241 connect(cbxTuning, SIGNAL(currentIndexChanged(int)), this, SLOT(configurationChanged()));
242 connect(cbxProfile, SIGNAL(currentIndexChanged(int)), this, SLOT(configurationChanged()));
243 connect(editCustomX264Params, SIGNAL(textChanged(QString)), this, SLOT(configurationChanged()));
244 connect(editCustomAvs2YUVParams, SIGNAL(textChanged(QString)), this, SLOT(configurationChanged()));
246 //Create context menus
247 ADD_CONTEXTMENU_ACTION(editCustomX264Params, QIcon(":/buttons/page_edit.png"), tr("Open the Text-Editor"), editorActionTriggered);
248 ADD_CONTEXTMENU_ACTION(editCustomAvs2YUVParams, QIcon(":/buttons/page_edit.png"), tr("Open the Text-Editor"), editorActionTriggered);
249 ADD_CONTEXTMENU_SEPARATOR(editCustomX264Params);
250 ADD_CONTEXTMENU_SEPARATOR(editCustomAvs2YUVParams);
251 ADD_CONTEXTMENU_ACTION(editCustomX264Params, QIcon(":/buttons/page_copy.png"), tr("Copy to Clipboard"), copyActionTriggered);
252 ADD_CONTEXTMENU_ACTION(editCustomAvs2YUVParams, QIcon(":/buttons/page_copy.png"), tr("Copy to Clipboard"), copyActionTriggered);
253 ADD_CONTEXTMENU_ACTION(editCustomX264Params, QIcon(":/buttons/page_paste.png"), tr("Paste from Clipboard"), pasteActionTriggered);
254 ADD_CONTEXTMENU_ACTION(editCustomAvs2YUVParams, QIcon(":/buttons/page_paste.png"), tr("Paste from Clipboard"), pasteActionTriggered);
256 //Setup template selector
258 connect(cbxTemplate, SIGNAL(currentIndexChanged(int)), this, SLOT(templateSelected()));
261 const QString appDir = x264_data_path();
262 QSettings settings(QString("%1/last.ini").arg(appDir), QSettings::IniFormat);
263 m_initialDir_src = settings.value("path/directory_openFrom", m_initialDir_src).toString();
264 m_initialDir_out = settings.value("path/directory_saveTo", m_initialDir_out).toString();
265 m_lastFilterIndex = settings.value("path/filterIndex", m_lastFilterIndex).toInt();
268 AddJobDialog::~AddJobDialog(void)
271 for(int i = 0; i < cbxTemplate->model()->rowCount(); i++)
273 if(cbxTemplate->itemText(i).startsWith("<") || cbxTemplate->itemText(i).endsWith(">"))
277 OptionsModel *item = reinterpret_cast<OptionsModel*>(cbxTemplate->itemData(i).value<void*>());
278 cbxTemplate->setItemData(i, QVariant::fromValue<void*>(NULL));
282 X264_DELETE(m_defaults);
285 ///////////////////////////////////////////////////////////////////////////////
287 ///////////////////////////////////////////////////////////////////////////////
289 void AddJobDialog::showEvent(QShowEvent *event)
291 QDialog::showEvent(event);
294 if(!editSource->text().isEmpty()) m_initialDir_src = QFileInfo(QDir::fromNativeSeparators(editSource->text())).path();
295 if(!editOutput->text().isEmpty()) m_initialDir_out = QFileInfo(QDir::fromNativeSeparators(editOutput->text())).path();
297 if((!editSource->text().isEmpty()) && editOutput->text().isEmpty())
299 generateOutputFileName(QDir::fromNativeSeparators(editSource->text()));
300 buttonAccept->setFocus();
303 labelNotificationX264->hide();
304 iconNotificationX264->hide();
305 labelNotificationAvs2YUV->hide();
306 iconNotificationAvs2YUV->hide();
309 bool AddJobDialog::eventFilter(QObject *o, QEvent *e)
311 if((o == labelHelpScreenX264) && (e->type() == QEvent::MouseButtonPress))
313 HelpDialog *helpScreen = new HelpDialog(this, false, m_x64supported);
315 X264_DELETE(helpScreen);
317 else if((o == labelHelpScreenAvs2YUV) && (e->type() == QEvent::MouseButtonPress))
319 HelpDialog *helpScreen = new HelpDialog(this, true, m_x64supported);
321 X264_DELETE(helpScreen);
323 else if((o == editCustomX264Params) && (e->type() == QEvent::FocusOut))
325 editCustomX264Params->setText(editCustomX264Params->text().simplified());
327 else if((o == editCustomAvs2YUVParams) && (e->type() == QEvent::FocusOut))
329 editCustomAvs2YUVParams->setText(editCustomAvs2YUVParams->text().simplified());
334 void AddJobDialog::dragEnterEvent(QDragEnterEvent *event)
336 QStringList formats = event->mimeData()->formats();
338 if(formats.contains("application/x-qt-windows-mime;value=\"FileNameW\"", Qt::CaseInsensitive) && formats.contains("text/uri-list", Qt::CaseInsensitive))
340 event->acceptProposedAction();
344 void AddJobDialog::dropEvent(QDropEvent *event)
347 QList<QUrl> urls = event->mimeData()->urls();
351 QDragEnterEvent dragEvent(event->pos(), event->proposedAction(), event->mimeData(), Qt::NoButton, Qt::NoModifier);
352 if(qApp->notify(parent(), &dragEvent))
354 qApp->notify(parent(), event);
359 while((!urls.isEmpty()) && droppedFile.isEmpty())
361 QUrl currentUrl = urls.takeFirst();
362 QFileInfo file(currentUrl.toLocalFile());
363 if(file.exists() && file.isFile())
365 qDebug("AddJobDialog::dropEvent: %s", file.canonicalFilePath().toUtf8().constData());
366 droppedFile = file.canonicalFilePath();
370 if(!droppedFile.isEmpty())
372 editSource->setText(QDir::toNativeSeparators(droppedFile));
373 generateOutputFileName(droppedFile);
377 ///////////////////////////////////////////////////////////////////////////////
379 ///////////////////////////////////////////////////////////////////////////////
381 void AddJobDialog::modeIndexChanged(int index)
383 spinQuantizer->setEnabled(index == 0 || index == 1);
384 spinBitrate->setEnabled(index == 2 || index == 3);
387 void AddJobDialog::accept(void)
389 if(editSource->text().trimmed().isEmpty())
391 QMessageBox::warning(this, tr("Not Found!"), tr("Please select a valid source file first!"));
395 if(editOutput->text().trimmed().isEmpty())
397 QMessageBox::warning(this, tr("Not Selected!"), tr("<nobr>Please select a valid output file first!</nobr>"));
401 QFileInfo sourceFile = QFileInfo(editSource->text());
402 if(!(sourceFile.exists() && sourceFile.isFile()))
404 QMessageBox::warning(this, tr("Not Found!"), tr("<nobr>The selected source file could not be found!</nobr>"));
408 QFileInfo outputDir = QFileInfo(QFileInfo(editOutput->text()).path());
409 if(!(outputDir.exists() && outputDir.isDir() && outputDir.isWritable()))
411 QMessageBox::warning(this, tr("Not Writable!"), tr("<nobr>Output directory does not exist or is not writable!</nobr>"));
415 QFileInfo outputFile = QFileInfo(editOutput->text());
416 if(outputFile.exists() && outputFile.isFile())
418 int ret = QMessageBox::question(this, tr("Already Exists!"), tr("<nobr>Output file already exists! Overwrite?</nobr>"), QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
419 if(ret != QMessageBox::Yes) return;
421 if(outputFile.exists() && (!outputFile.isFile()))
423 QMessageBox::warning(this, tr("Not a File!"), tr("<nobr>Selected output file does not appear to be a valid file!</nobr>"));
426 if(!editCustomX264Params->hasAcceptableInput())
428 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);
429 if(ret != QMessageBox::Ignore) return;
433 const QString appDir = x264_data_path();
434 QSettings settings(QString("%1/last.ini").arg(appDir), QSettings::IniFormat);
435 if(settings.isWritable())
437 settings.setValue("path/directory_saveTo", m_initialDir_out);
438 settings.setValue("path/directory_openFrom", m_initialDir_src);
439 settings.setValue("path/filterIndex", m_lastFilterIndex);
443 saveOptions(m_options);
447 void AddJobDialog::browseButtonClicked(void)
449 if(QObject::sender() == buttonBrowseSource)
451 QString initDir = VALID_DIR(m_initialDir_src) ? m_initialDir_src : QDesktopServices::storageLocation(QDesktopServices::MoviesLocation);
452 if(!editSource->text().isEmpty()) initDir = QString("%1/%2").arg(initDir, QFileInfo(QDir::fromNativeSeparators(editSource->text())).fileName());
454 QString filePath = QFileDialog::getOpenFileName(this, tr("Open Source File"), initDir, makeFileFilter(), NULL, QFileDialog::DontUseNativeDialog);
455 if(!(filePath.isNull() || filePath.isEmpty()))
457 editSource->setText(QDir::toNativeSeparators(filePath));
458 generateOutputFileName(filePath);
459 m_initialDir_src = QFileInfo(filePath).path();
462 else if(QObject::sender() == buttonBrowseOutput)
464 QString initDir = VALID_DIR(m_initialDir_out) ? m_initialDir_out : QDesktopServices::storageLocation(QDesktopServices::MoviesLocation);
465 if(!editOutput->text().isEmpty()) initDir = QString("%1/%2").arg(initDir, QFileInfo(QDir::fromNativeSeparators(editOutput->text())).completeBaseName());
466 int filterIdx = getFilterIndex(QFileInfo(QDir::fromNativeSeparators(editOutput->text())).suffix());
467 QString selectedType = m_types.at((filterIdx >= 0) ? filterIdx : m_lastFilterIndex);
469 QString filePath = QFileDialog::getSaveFileName(this, tr("Choose Output File"), initDir, m_types.join(";;"), &selectedType, QFileDialog::DontUseNativeDialog | QFileDialog::DontConfirmOverwrite);
471 if(!(filePath.isNull() || filePath.isEmpty()))
473 if(getFilterIndex(QFileInfo(filePath).suffix()) < 0)
475 filterIdx = m_types.indexOf(selectedType);
478 filePath = QString("%1.%2").arg(filePath, getFilterExt(filterIdx));
481 editOutput->setText(QDir::toNativeSeparators(filePath));
482 m_lastFilterIndex = getFilterIndex(QFileInfo(filePath).suffix());
483 m_initialDir_out = QFileInfo(filePath).path();
488 void AddJobDialog::configurationChanged(void)
490 OptionsModel* options = reinterpret_cast<OptionsModel*>(cbxTemplate->itemData(cbxTemplate->currentIndex()).value<void*>());
493 cbxTemplate->blockSignals(true);
494 cbxTemplate->insertItem(0, tr("<Unsaved Configuration>"), QVariant::fromValue<void*>(NULL));
495 cbxTemplate->setCurrentIndex(0);
496 cbxTemplate->blockSignals(false);
500 void AddJobDialog::templateSelected(void)
502 OptionsModel* options = reinterpret_cast<OptionsModel*>(cbxTemplate->itemData(cbxTemplate->currentIndex()).value<void*>());
505 qDebug("Loading options!");
507 restoreOptions(options);
510 modeIndexChanged(cbxRateControlMode->currentIndex());
513 void AddJobDialog::saveTemplateButtonClicked(void)
515 qDebug("Saving template");
516 QString name = tr("New Template");
519 while(OptionsModel::templateExists(name))
521 name = tr("New Template (%1)").arg(QString::number(n++));
524 OptionsModel *options = new OptionsModel();
525 saveOptions(options);
527 if(options->equals(m_defaults))
529 QMessageBox::warning (this, tr("Oups"), tr("<nobr>It makes no sense to save the default settings!</nobr>"));
530 cbxTemplate->blockSignals(true);
531 cbxTemplate->setCurrentIndex(0);
532 cbxTemplate->blockSignals(false);
534 X264_DELETE(options);
538 for(int i = 0; i < cbxTemplate->count(); i++)
540 const QString tempName = cbxTemplate->itemText(i);
541 if(tempName.contains('<') || tempName.contains('>'))
545 OptionsModel* test = reinterpret_cast<OptionsModel*>(cbxTemplate->itemData(i).value<void*>());
548 if(options->equals(test))
550 QMessageBox::warning (this, tr("Oups"), tr("<nobr>There already is a template for the current settings!</nobr>"));
551 cbxTemplate->blockSignals(true);
552 cbxTemplate->setCurrentIndex(i);
553 cbxTemplate->blockSignals(false);
555 X264_DELETE(options);
564 name = QInputDialog::getText(this, tr("Save Template"), tr("Please enter the name of the template:").leftJustified(144, ' '), QLineEdit::Normal, name, &ok).simplified();
567 X264_DELETE(options);
570 if(name.contains('<') || name.contains('>') || name.contains('\\') || name.contains('/') || name.contains('"'))
572 QMessageBox::warning (this, tr("Invalid Name"), tr("<nobr>Sorry, the name you have entered is invalid!</nobr>"));
573 while(name.contains('<')) name.remove('<');
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 name = name.simplified();
581 if(OptionsModel::templateExists(name))
583 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);
584 if(ret != QMessageBox::Yes)
592 if(!OptionsModel::saveTemplate(options, name))
594 QMessageBox::critical(this, tr("Save Failed"), tr("Sorry, the template could not be saved!"));
595 X264_DELETE(options);
599 int index = cbxTemplate->model()->rowCount();
600 cbxTemplate->blockSignals(true);
601 for(int i = 0; i < cbxTemplate->count(); i++)
603 if(cbxTemplate->itemText(i).compare(name, Qt::CaseInsensitive) == 0)
605 index = -1; //Do not append new template
606 OptionsModel *oldItem = reinterpret_cast<OptionsModel*>(cbxTemplate->itemData(i).value<void*>());
607 cbxTemplate->setItemData(i, QVariant::fromValue<void*>(options));
608 cbxTemplate->setCurrentIndex(i);
609 X264_DELETE(oldItem);
614 cbxTemplate->insertItem(index, name, QVariant::fromValue<void*>(options));
615 cbxTemplate->setCurrentIndex(index);
617 cbxTemplate->blockSignals(false);
622 void AddJobDialog::deleteTemplateButtonClicked(void)
624 const int index = cbxTemplate->currentIndex();
625 QString name = cbxTemplate->itemText(index);
627 if(name.contains('<') || name.contains('>') || name.contains('\\') || name.contains('/'))
629 QMessageBox::warning (this, tr("Invalid Item"), tr("Sorry, the selected item cannot be deleted!"));
633 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);
634 if(ret != QMessageBox::Yes)
640 OptionsModel::deleteTemplate(name);
641 OptionsModel *item = reinterpret_cast<OptionsModel*>(cbxTemplate->itemData(index).value<void*>());
642 cbxTemplate->removeItem(index);
646 void AddJobDialog::editorActionTriggered(void)
649 if(QAction *action = dynamic_cast<QAction*>(QObject::sender()))
651 QLineEdit *lineEdit = reinterpret_cast<QLineEdit*>(action->data().value<void*>());
653 EditorDialog *editor = new EditorDialog(this);
654 editor->setEditText(lineEdit->text());
656 if(editor->exec() == QDialog::Accepted)
658 lineEdit->setText(editor->getEditText());
665 void AddJobDialog::copyActionTriggered(void)
667 if(QAction *action = dynamic_cast<QAction*>(QObject::sender()))
669 QClipboard *clipboard = QApplication::clipboard();
670 QLineEdit *lineEdit = reinterpret_cast<QLineEdit*>(action->data().value<void*>());
671 QString text = lineEdit->hasSelectedText() ? lineEdit->selectedText() : lineEdit->text();
672 clipboard->setText(text);
676 void AddJobDialog::pasteActionTriggered(void)
678 if(QAction *action = dynamic_cast<QAction*>(QObject::sender()))
680 QClipboard *clipboard = QApplication::clipboard();
681 QLineEdit *lineEdit = reinterpret_cast<QLineEdit*>(action->data().value<void*>());
682 QString text = clipboard->text();
683 if(!text.isEmpty()) lineEdit->setText(text);
687 ///////////////////////////////////////////////////////////////////////////////
689 ///////////////////////////////////////////////////////////////////////////////
691 QString AddJobDialog::sourceFile(void)
693 return QDir::fromNativeSeparators(editSource->text());
696 QString AddJobDialog::outputFile(void)
698 return QDir::fromNativeSeparators(editOutput->text());
701 ///////////////////////////////////////////////////////////////////////////////
703 ///////////////////////////////////////////////////////////////////////////////
705 void AddJobDialog::loadTemplateList(void)
707 cbxTemplate->addItem(tr("<Default>"), QVariant::fromValue<void*>(m_defaults));
708 cbxTemplate->setCurrentIndex(0);
710 QMap<QString, OptionsModel*> templates = OptionsModel::loadAllTemplates();
711 QStringList templateNames = templates.keys();
712 templateNames.sort();
714 while(!templateNames.isEmpty())
716 QString current = templateNames.takeFirst();
717 cbxTemplate->addItem(current, QVariant::fromValue<void*>(templates.value(current)));
719 if(templates.value(current)->equals(m_options))
721 cbxTemplate->setCurrentIndex(cbxTemplate->count() - 1);
725 if((cbxTemplate->currentIndex() == 0) && (!m_options->equals(m_defaults)))
727 cbxTemplate->insertItem(1, tr("<Recently Used>"), QVariant::fromValue<void*>(m_options));
728 cbxTemplate->setCurrentIndex(1);
732 void AddJobDialog::updateComboBox(QComboBox *cbox, const QString &text)
734 for(int i = 0; i < cbox->model()->rowCount(); i++)
736 if(cbox->model()->data(cbox->model()->index(i, 0, QModelIndex())).toString().compare(text, Qt::CaseInsensitive) == 0)
738 cbox->setCurrentIndex(i);
744 void AddJobDialog::restoreOptions(OptionsModel *options)
746 cbxRateControlMode->blockSignals(true);
747 spinQuantizer->blockSignals(true);
748 spinBitrate->blockSignals(true);
749 cbxPreset->blockSignals(true);
750 cbxTuning->blockSignals(true);
751 cbxProfile->blockSignals(true);
752 editCustomX264Params->blockSignals(true);
753 editCustomAvs2YUVParams->blockSignals(true);
755 cbxRateControlMode->setCurrentIndex(options->rcMode());
756 spinQuantizer->setValue(options->quantizer());
757 spinBitrate->setValue(options->bitrate());
758 updateComboBox(cbxPreset, options->preset());
759 updateComboBox(cbxTuning, options->tune());
760 updateComboBox(cbxProfile, options->profile());
761 editCustomX264Params->setText(options->customX264());
762 editCustomAvs2YUVParams->setText(options->customAvs2YUV());
764 cbxRateControlMode->blockSignals(false);
765 spinQuantizer->blockSignals(false);
766 spinBitrate->blockSignals(false);
767 cbxPreset->blockSignals(false);
768 cbxTuning->blockSignals(false);
769 cbxProfile->blockSignals(false);
770 editCustomX264Params->blockSignals(false);
771 editCustomAvs2YUVParams->blockSignals(false);
774 void AddJobDialog::saveOptions(OptionsModel *options)
776 options->setRCMode(static_cast<OptionsModel::RCMode>(cbxRateControlMode->currentIndex()));
777 options->setQuantizer(spinQuantizer->value());
778 options->setBitrate(spinBitrate->value());
779 options->setPreset(cbxPreset->model()->data(cbxPreset->model()->index(cbxPreset->currentIndex(), 0)).toString());
780 options->setTune(cbxTuning->model()->data(cbxTuning->model()->index(cbxTuning->currentIndex(), 0)).toString());
781 options->setProfile(cbxProfile->model()->data(cbxProfile->model()->index(cbxProfile->currentIndex(), 0)).toString());
782 options->setCustomX264(editCustomX264Params->hasAcceptableInput() ? editCustomX264Params->text().simplified() : QString());
783 options->setCustomAvs2YUV(editCustomAvs2YUVParams->hasAcceptableInput() ? editCustomAvs2YUVParams->text().simplified() : QString());
786 QString AddJobDialog::makeFileFilter(void)
795 {"Avisynth Scripts", "avs"},
796 {"Matroska Files", "mkv"},
797 {"MPEG-4 Part 14 Container", "mp4"},
798 {"Audio Video Interleaved", "avi"},
799 {"Flash Video", "flv"},
800 {"YUV4MPEG2 Stream", "y4m"},
801 {"Uncompresses YUV Data", "yuv"},
805 QString filters("All supported files (");
807 for(size_t index = 0; s_filters[index].name && s_filters[index].fext; index++)
809 filters += QString((index > 0) ? " *.%1" : "*.%1").arg(QString::fromLatin1(s_filters[index].fext));
812 filters += QString(");;");
814 for(size_t index = 0; s_filters[index].name && s_filters[index].fext; index++)
816 filters += QString("%1 (*.%2);;").arg(QString::fromLatin1(s_filters[index].name), QString::fromLatin1(s_filters[index].fext));
819 filters += QString("All files (*.*)");
823 void AddJobDialog::generateOutputFileName(const QString &filePath)
825 QString name = QFileInfo(filePath).completeBaseName();
826 QString path = VALID_DIR(m_initialDir_out) ? m_initialDir_out : QFileInfo(filePath).path();
827 QString fext = getFilterExt(m_lastFilterIndex);
829 QString outPath = QString("%1/%2.%3").arg(path, name, fext);
831 if(QFileInfo(outPath).exists())
834 while(QFileInfo(outPath).exists())
836 outPath = QString("%1/%2 (%3).%4").arg(path, name, QString::number(i++), fext);
840 editOutput->setText(QDir::toNativeSeparators(outPath));
843 int AddJobDialog::getFilterIndex(const QString &fileExt)
845 if(!fileExt.isEmpty())
847 QRegExp ext("\\(\\*\\.(.+)\\)");
848 for(int i = 0; i < m_types.count(); i++)
850 if(ext.lastIndexIn(m_types.at(i)) >= 0)
852 if(fileExt.compare(ext.cap(1), Qt::CaseInsensitive) == 0)
863 QString AddJobDialog::getFilterExt(int filterIdx)
865 int index = qBound(0, filterIdx, m_types.count()-1);
867 QRegExp ext("\\(\\*\\.(.+)\\)");
868 if(ext.lastIndexIn(m_types.at(index)) >= 0)
870 return ext.cap(1).toLower();
873 return QString::fromLatin1("mkv");