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, bool use10BitEncoding)
191 m_defaults(new OptionsModel()),
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))),
199 //Init the dialog, from the .ui file
201 setWindowFlags(windowFlags() & (~Qt::WindowContextHelpButtonHint));
204 buttonSaveTemplate->setMaximumHeight(20);
205 buttonDeleteTemplate->setMaximumHeight(20);
206 resize(width(), minimumHeight());
207 setMinimumSize(size());
208 setMaximumHeight(height());
210 //Setup file type filter
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)");
216 //Monitor RC mode combobox
217 connect(cbxRateControlMode, SIGNAL(currentIndexChanged(int)), this, SLOT(modeIndexChanged(int)));
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()));
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();
233 //Install event filter
234 labelHelpScreenX264->installEventFilter(this);
235 labelHelpScreenAvs2YUV->installEventFilter(this);
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()));
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);
257 //Setup template selector
259 connect(cbxTemplate, SIGNAL(currentIndexChanged(int)), this, SLOT(templateSelected()));
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();
269 AddJobDialog::~AddJobDialog(void)
272 for(int i = 0; i < cbxTemplate->model()->rowCount(); i++)
274 if(cbxTemplate->itemText(i).startsWith("<") || cbxTemplate->itemText(i).endsWith(">"))
278 OptionsModel *item = reinterpret_cast<OptionsModel*>(cbxTemplate->itemData(i).value<void*>());
279 cbxTemplate->setItemData(i, QVariant::fromValue<void*>(NULL));
283 X264_DELETE(m_defaults);
286 ///////////////////////////////////////////////////////////////////////////////
288 ///////////////////////////////////////////////////////////////////////////////
290 void AddJobDialog::showEvent(QShowEvent *event)
292 QDialog::showEvent(event);
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();
298 if((!editSource->text().isEmpty()) && editOutput->text().isEmpty())
300 generateOutputFileName(QDir::fromNativeSeparators(editSource->text()));
301 buttonAccept->setFocus();
304 labelNotificationX264->hide();
305 iconNotificationX264->hide();
306 labelNotificationAvs2YUV->hide();
307 iconNotificationAvs2YUV->hide();
310 bool AddJobDialog::eventFilter(QObject *o, QEvent *e)
312 if((o == labelHelpScreenX264) && (e->type() == QEvent::MouseButtonPress))
314 HelpDialog *helpScreen = new HelpDialog(this, false, m_x64supported, m_use10BitEncoding);
316 X264_DELETE(helpScreen);
318 else if((o == labelHelpScreenAvs2YUV) && (e->type() == QEvent::MouseButtonPress))
320 HelpDialog *helpScreen = new HelpDialog(this, true, m_x64supported, m_use10BitEncoding);
322 X264_DELETE(helpScreen);
324 else if((o == editCustomX264Params) && (e->type() == QEvent::FocusOut))
326 editCustomX264Params->setText(editCustomX264Params->text().simplified());
328 else if((o == editCustomAvs2YUVParams) && (e->type() == QEvent::FocusOut))
330 editCustomAvs2YUVParams->setText(editCustomAvs2YUVParams->text().simplified());
335 void AddJobDialog::dragEnterEvent(QDragEnterEvent *event)
337 QStringList formats = event->mimeData()->formats();
339 if(formats.contains("application/x-qt-windows-mime;value=\"FileNameW\"", Qt::CaseInsensitive) && formats.contains("text/uri-list", Qt::CaseInsensitive))
341 event->acceptProposedAction();
345 void AddJobDialog::dropEvent(QDropEvent *event)
348 QList<QUrl> urls = event->mimeData()->urls();
352 QDragEnterEvent dragEvent(event->pos(), event->proposedAction(), event->mimeData(), Qt::NoButton, Qt::NoModifier);
353 if(qApp->notify(parent(), &dragEvent))
355 qApp->notify(parent(), event);
360 while((!urls.isEmpty()) && droppedFile.isEmpty())
362 QUrl currentUrl = urls.takeFirst();
363 QFileInfo file(currentUrl.toLocalFile());
364 if(file.exists() && file.isFile())
366 qDebug("AddJobDialog::dropEvent: %s", file.canonicalFilePath().toUtf8().constData());
367 droppedFile = file.canonicalFilePath();
371 if(!droppedFile.isEmpty())
373 editSource->setText(QDir::toNativeSeparators(droppedFile));
374 generateOutputFileName(droppedFile);
378 ///////////////////////////////////////////////////////////////////////////////
380 ///////////////////////////////////////////////////////////////////////////////
382 void AddJobDialog::modeIndexChanged(int index)
384 spinQuantizer->setEnabled(index == 0 || index == 1);
385 spinBitrate->setEnabled(index == 2 || index == 3);
388 void AddJobDialog::accept(void)
390 if(editSource->text().trimmed().isEmpty())
392 QMessageBox::warning(this, tr("Not Found!"), tr("Please select a valid source file first!"));
396 if(editOutput->text().trimmed().isEmpty())
398 QMessageBox::warning(this, tr("Not Selected!"), tr("<nobr>Please select a valid output file first!</nobr>"));
402 QFileInfo sourceFile = QFileInfo(editSource->text());
403 if(!(sourceFile.exists() && sourceFile.isFile()))
405 QMessageBox::warning(this, tr("Not Found!"), tr("<nobr>The selected source file could not be found!</nobr>"));
409 QFileInfo outputDir = QFileInfo(QFileInfo(editOutput->text()).path());
410 if(!(outputDir.exists() && outputDir.isDir() && outputDir.isWritable()))
412 QMessageBox::warning(this, tr("Not Writable!"), tr("<nobr>Output directory does not exist or is not writable!</nobr>"));
416 QFileInfo outputFile = QFileInfo(editOutput->text());
417 if(outputFile.exists() && outputFile.isFile())
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;
422 if(outputFile.exists() && (!outputFile.isFile()))
424 QMessageBox::warning(this, tr("Not a File!"), tr("<nobr>Selected output file does not appear to be a valid file!</nobr>"));
427 if(!editCustomX264Params->hasAcceptableInput())
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;
434 const QString appDir = x264_data_path();
435 QSettings settings(QString("%1/last.ini").arg(appDir), QSettings::IniFormat);
436 if(settings.isWritable())
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);
444 saveOptions(m_options);
448 void AddJobDialog::browseButtonClicked(void)
450 if(QObject::sender() == buttonBrowseSource)
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());
455 QString filePath = QFileDialog::getOpenFileName(this, tr("Open Source File"), initDir, makeFileFilter(), NULL, QFileDialog::DontUseNativeDialog);
456 if(!(filePath.isNull() || filePath.isEmpty()))
458 editSource->setText(QDir::toNativeSeparators(filePath));
459 generateOutputFileName(filePath);
460 m_initialDir_src = QFileInfo(filePath).path();
463 else if(QObject::sender() == buttonBrowseOutput)
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);
470 QString filePath = QFileDialog::getSaveFileName(this, tr("Choose Output File"), initDir, m_types.join(";;"), &selectedType, QFileDialog::DontUseNativeDialog | QFileDialog::DontConfirmOverwrite);
472 if(!(filePath.isNull() || filePath.isEmpty()))
474 if(getFilterIndex(QFileInfo(filePath).suffix()) < 0)
476 filterIdx = m_types.indexOf(selectedType);
479 filePath = QString("%1.%2").arg(filePath, getFilterExt(filterIdx));
482 editOutput->setText(QDir::toNativeSeparators(filePath));
483 m_lastFilterIndex = getFilterIndex(QFileInfo(filePath).suffix());
484 m_initialDir_out = QFileInfo(filePath).path();
489 void AddJobDialog::configurationChanged(void)
491 OptionsModel* options = reinterpret_cast<OptionsModel*>(cbxTemplate->itemData(cbxTemplate->currentIndex()).value<void*>());
494 cbxTemplate->blockSignals(true);
495 cbxTemplate->insertItem(0, tr("<Unsaved Configuration>"), QVariant::fromValue<void*>(NULL));
496 cbxTemplate->setCurrentIndex(0);
497 cbxTemplate->blockSignals(false);
501 void AddJobDialog::templateSelected(void)
503 OptionsModel* options = reinterpret_cast<OptionsModel*>(cbxTemplate->itemData(cbxTemplate->currentIndex()).value<void*>());
506 qDebug("Loading options!");
508 restoreOptions(options);
511 modeIndexChanged(cbxRateControlMode->currentIndex());
514 void AddJobDialog::saveTemplateButtonClicked(void)
516 qDebug("Saving template");
517 QString name = tr("New Template");
520 while(OptionsModel::templateExists(name))
522 name = tr("New Template (%1)").arg(QString::number(n++));
525 OptionsModel *options = new OptionsModel();
526 saveOptions(options);
528 if(options->equals(m_defaults))
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);
535 X264_DELETE(options);
539 for(int i = 0; i < cbxTemplate->count(); i++)
541 const QString tempName = cbxTemplate->itemText(i);
542 if(tempName.contains('<') || tempName.contains('>'))
546 OptionsModel* test = reinterpret_cast<OptionsModel*>(cbxTemplate->itemData(i).value<void*>());
549 if(options->equals(test))
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);
556 X264_DELETE(options);
565 name = QInputDialog::getText(this, tr("Save Template"), tr("Please enter the name of the template:").leftJustified(144, ' '), QLineEdit::Normal, name, &ok).simplified();
568 X264_DELETE(options);
571 if(name.contains('<') || name.contains('>') || name.contains('\\') || name.contains('/') || name.contains('"'))
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();
582 if(OptionsModel::templateExists(name))
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)
593 if(!OptionsModel::saveTemplate(options, name))
595 QMessageBox::critical(this, tr("Save Failed"), tr("Sorry, the template could not be saved!"));
596 X264_DELETE(options);
600 int index = cbxTemplate->model()->rowCount();
601 cbxTemplate->blockSignals(true);
602 for(int i = 0; i < cbxTemplate->count(); i++)
604 if(cbxTemplate->itemText(i).compare(name, Qt::CaseInsensitive) == 0)
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);
615 cbxTemplate->insertItem(index, name, QVariant::fromValue<void*>(options));
616 cbxTemplate->setCurrentIndex(index);
618 cbxTemplate->blockSignals(false);
623 void AddJobDialog::deleteTemplateButtonClicked(void)
625 const int index = cbxTemplate->currentIndex();
626 QString name = cbxTemplate->itemText(index);
628 if(name.contains('<') || name.contains('>') || name.contains('\\') || name.contains('/'))
630 QMessageBox::warning (this, tr("Invalid Item"), tr("Sorry, the selected item cannot be deleted!"));
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)
641 OptionsModel::deleteTemplate(name);
642 OptionsModel *item = reinterpret_cast<OptionsModel*>(cbxTemplate->itemData(index).value<void*>());
643 cbxTemplate->removeItem(index);
647 void AddJobDialog::editorActionTriggered(void)
650 if(QAction *action = dynamic_cast<QAction*>(QObject::sender()))
652 QLineEdit *lineEdit = reinterpret_cast<QLineEdit*>(action->data().value<void*>());
654 EditorDialog *editor = new EditorDialog(this);
655 editor->setEditText(lineEdit->text());
657 if(editor->exec() == QDialog::Accepted)
659 lineEdit->setText(editor->getEditText());
666 void AddJobDialog::copyActionTriggered(void)
668 if(QAction *action = dynamic_cast<QAction*>(QObject::sender()))
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);
677 void AddJobDialog::pasteActionTriggered(void)
679 if(QAction *action = dynamic_cast<QAction*>(QObject::sender()))
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);
688 ///////////////////////////////////////////////////////////////////////////////
690 ///////////////////////////////////////////////////////////////////////////////
692 QString AddJobDialog::sourceFile(void)
694 return QDir::fromNativeSeparators(editSource->text());
697 QString AddJobDialog::outputFile(void)
699 return QDir::fromNativeSeparators(editOutput->text());
702 ///////////////////////////////////////////////////////////////////////////////
704 ///////////////////////////////////////////////////////////////////////////////
706 void AddJobDialog::loadTemplateList(void)
708 cbxTemplate->addItem(tr("<Default>"), QVariant::fromValue<void*>(m_defaults));
709 cbxTemplate->setCurrentIndex(0);
711 QMap<QString, OptionsModel*> templates = OptionsModel::loadAllTemplates();
712 QStringList templateNames = templates.keys();
713 templateNames.sort();
715 while(!templateNames.isEmpty())
717 QString current = templateNames.takeFirst();
718 cbxTemplate->addItem(current, QVariant::fromValue<void*>(templates.value(current)));
720 if(templates.value(current)->equals(m_options))
722 cbxTemplate->setCurrentIndex(cbxTemplate->count() - 1);
726 if((cbxTemplate->currentIndex() == 0) && (!m_options->equals(m_defaults)))
728 cbxTemplate->insertItem(1, tr("<Recently Used>"), QVariant::fromValue<void*>(m_options));
729 cbxTemplate->setCurrentIndex(1);
733 void AddJobDialog::updateComboBox(QComboBox *cbox, const QString &text)
735 for(int i = 0; i < cbox->model()->rowCount(); i++)
737 if(cbox->model()->data(cbox->model()->index(i, 0, QModelIndex())).toString().compare(text, Qt::CaseInsensitive) == 0)
739 cbox->setCurrentIndex(i);
745 void AddJobDialog::restoreOptions(OptionsModel *options)
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);
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());
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);
775 void AddJobDialog::saveOptions(OptionsModel *options)
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());
787 QString AddJobDialog::makeFileFilter(void)
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"},
806 QString filters("All supported files (");
808 for(size_t index = 0; s_filters[index].name && s_filters[index].fext; index++)
810 filters += QString((index > 0) ? " *.%1" : "*.%1").arg(QString::fromLatin1(s_filters[index].fext));
813 filters += QString(");;");
815 for(size_t index = 0; s_filters[index].name && s_filters[index].fext; index++)
817 filters += QString("%1 (*.%2);;").arg(QString::fromLatin1(s_filters[index].name), QString::fromLatin1(s_filters[index].fext));
820 filters += QString("All files (*.*)");
824 void AddJobDialog::generateOutputFileName(const QString &filePath)
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);
830 QString outPath = QString("%1/%2.%3").arg(path, name, fext);
832 if(QFileInfo(outPath).exists())
835 while(QFileInfo(outPath).exists())
837 outPath = QString("%1/%2 (%3).%4").arg(path, name, QString::number(i++), fext);
841 editOutput->setText(QDir::toNativeSeparators(outPath));
844 int AddJobDialog::getFilterIndex(const QString &fileExt)
846 if(!fileExt.isEmpty())
848 QRegExp ext("\\(\\*\\.(.+)\\)");
849 for(int i = 0; i < m_types.count(); i++)
851 if(ext.lastIndexIn(m_types.at(i)) >= 0)
853 if(fileExt.compare(ext.cap(1), Qt::CaseInsensitive) == 0)
864 QString AddJobDialog::getFilterExt(int filterIdx)
866 int index = qBound(0, filterIdx, m_types.count()-1);
868 QRegExp ext("\\(\\*\\.(.+)\\)");
869 if(ext.lastIndexIn(m_types.at(index)) >= 0)
871 return ext.cap(1).toLower();
874 return QString::fromLatin1("mkv");