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"
29 #include <QCloseEvent>
30 #include <QMessageBox>
31 #include <QFileDialog>
32 #include <QDesktopServices>
35 #include <QInputDialog>
44 {"Avisynth Scripts", "avs"},
45 {"Matroska Files", "mkv"},
46 {"MPEG-4 Part 14 Container", "mp4"},
47 {"Audio Video Interleaved", "avi"},
48 {"Flash Video", "flv"},
52 ///////////////////////////////////////////////////////////////////////////////
54 ///////////////////////////////////////////////////////////////////////////////
56 class StringValidator : public QValidator
59 StringValidator(QLabel *notifier) : m_notifier(notifier) { m_notifier->hide(); }
61 virtual State validate(QString &input, int &pos) const
65 invalid = invalid || (input.contains(" -B") || input.startsWith("-B"));
66 invalid = invalid || (input.contains(" -o") || input.startsWith("-o"));
67 invalid = invalid || (input.contains(" -h") || input.startsWith("-h"));
68 invalid = invalid || (input.contains(" -p") || input.startsWith("-p"));
70 invalid = invalid || input.contains("--fps", Qt::CaseInsensitive);
71 invalid = invalid || input.contains("--frames", Qt::CaseInsensitive);
72 invalid = invalid || input.contains("--preset", Qt::CaseInsensitive);
73 invalid = invalid || input.contains("--tune", Qt::CaseInsensitive);
74 invalid = invalid || input.contains("--profile", Qt::CaseInsensitive);
75 invalid = invalid || input.contains("--stdin", Qt::CaseInsensitive);
76 invalid = invalid || input.contains("--crf", Qt::CaseInsensitive);
77 invalid = invalid || input.contains("--bitrate", Qt::CaseInsensitive);
78 invalid = invalid || input.contains("--qp", Qt::CaseInsensitive);
79 invalid = invalid || input.contains("--pass", Qt::CaseInsensitive);
80 invalid = invalid || input.contains("--stats", Qt::CaseInsensitive);
81 invalid = invalid || input.contains("--output", Qt::CaseInsensitive);
82 invalid = invalid || input.contains("--help", Qt::CaseInsensitive);
86 MessageBeep(MB_ICONWARNING);
87 if(m_notifier->isHidden())
90 QTimer::singleShot(1000, m_notifier, SLOT(hide()));
94 return invalid ? QValidator::Invalid : QValidator::Acceptable;
97 virtual void fixup(QString &input) const
99 input = input.simplified();
103 QLabel *const m_notifier;
106 ///////////////////////////////////////////////////////////////////////////////
107 // Constructor & Destructor
108 ///////////////////////////////////////////////////////////////////////////////
110 AddJobDialog::AddJobDialog(QWidget *parent, OptionsModel *options)
113 m_defaults(new OptionsModel()),
116 //Init the dialog, from the .ui file
118 setWindowFlags(windowFlags() & (~Qt::WindowContextHelpButtonHint));
121 buttonSaveTemplate->setMaximumHeight(20);
122 buttonDeleteTemplate->setMaximumHeight(20);
123 resize(width(), minimumHeight());
124 setMinimumSize(size());
125 setMaximumHeight(height());
127 //Monitor RC mode combobox
128 connect(cbxRateControlMode, SIGNAL(currentIndexChanged(int)), this, SLOT(modeIndexChanged(int)));
131 connect(buttonBrowseSource, SIGNAL(clicked()), this, SLOT(browseButtonClicked()));
132 connect(buttonBrowseOutput, SIGNAL(clicked()), this, SLOT(browseButtonClicked()));
133 connect(buttonSaveTemplate, SIGNAL(clicked()), this, SLOT(saveTemplateButtonClicked()));
134 connect(buttonDeleteTemplate, SIGNAL(clicked()), this, SLOT(deleteTemplateButtonClicked()));
137 editCustomParams->installEventFilter(this);
138 editCustomParams->setValidator(new StringValidator(labelNotification));
139 editCustomParams->clear();
141 //Install event filter
142 labelHelpScreen->installEventFilter(this);
144 //Monitor for options changes
145 connect(cbxRateControlMode, SIGNAL(currentIndexChanged(int)), this, SLOT(configurationChanged()));
146 connect(spinQuantizer, SIGNAL(valueChanged(int)), this, SLOT(configurationChanged()));
147 connect(spinBitrate, SIGNAL(valueChanged(int)), this, SLOT(configurationChanged()));
148 connect(cbxPreset, SIGNAL(currentIndexChanged(int)), this, SLOT(configurationChanged()));
149 connect(cbxTuning, SIGNAL(currentIndexChanged(int)), this, SLOT(configurationChanged()));
150 connect(cbxProfile, SIGNAL(currentIndexChanged(int)), this, SLOT(configurationChanged()));
151 connect(editCustomParams, SIGNAL(textChanged(QString)), this, SLOT(configurationChanged()));
153 //Setup template selector
155 connect(cbxTemplate, SIGNAL(currentIndexChanged(int)), this, SLOT(templateSelected()));
158 AddJobDialog::~AddJobDialog(void)
160 for(int i = 0; i < cbxTemplate->model()->rowCount(); i++)
162 if(cbxTemplate->itemText(i).startsWith("<") || cbxTemplate->itemText(i).endsWith(">"))
166 OptionsModel *item = reinterpret_cast<OptionsModel*>(cbxTemplate->itemData(i).value<void*>());
167 cbxTemplate->setItemData(i, QVariant::fromValue<void*>(NULL));
171 X264_DELETE(m_defaults);
174 ///////////////////////////////////////////////////////////////////////////////
176 ///////////////////////////////////////////////////////////////////////////////
178 void AddJobDialog::showEvent(QShowEvent *event)
180 QDialog::showEvent(event);
184 bool AddJobDialog::eventFilter(QObject *o, QEvent *e)
186 if((o == labelHelpScreen) && (e->type() == QEvent::MouseButtonPress))
188 QMessageBox::information(this, tr("Not yet"), tr("Not implemented yet. Please use the '?' menu for now!"));
190 else if((o == editCustomParams) && (e->type() == QEvent::FocusOut))
192 editCustomParams->setText(editCustomParams->text().simplified());
197 ///////////////////////////////////////////////////////////////////////////////
199 ///////////////////////////////////////////////////////////////////////////////
201 void AddJobDialog::modeIndexChanged(int index)
203 spinQuantizer->setEnabled(index == 0 || index == 1);
204 spinBitrate->setEnabled(index == 2 || index == 3);
207 void AddJobDialog::accept(void)
209 if(editSource->text().trimmed().isEmpty())
211 QMessageBox::warning(this, tr("Not Found!"), tr("Please select a valid source file first!"));
215 QFileInfo sourceFile = QFileInfo(editSource->text());
216 if(!(sourceFile.exists() && sourceFile.isFile()))
218 QMessageBox::warning(this, tr("Not Found!"), tr("The selected source file could not be found!"));
222 QFileInfo outputDir = QFileInfo(QFileInfo(editOutput->text()).path());
223 if(!(outputDir.exists() && outputDir.isDir() && outputDir.isWritable()))
225 QMessageBox::warning(this, tr("Not Writable!"), tr("Output directory does not exist or is not writable!"));
229 QFileInfo outputFile = QFileInfo(editOutput->text());
230 if(outputFile.exists() && outputFile.isFile())
232 if(QMessageBox::question(this, tr("Already Exists!"), tr("Output file already exists! Overwrite?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::No) != QMessageBox::Yes)
237 if(outputFile.exists() && (!outputFile.isFile()))
239 QMessageBox::warning(this, tr("Not a File!"), tr("Selected output files does not appear to be a file!"));
243 saveOptions(m_options);
247 void AddJobDialog::browseButtonClicked(void)
249 QString initialDir = QDesktopServices::storageLocation(QDesktopServices::MoviesLocation);
251 if(QObject::sender() == buttonBrowseSource)
253 QString filePath = QFileDialog::getOpenFileName(this, tr("Open Source File"), initialDir, makeFileFilter());
255 if(!(filePath.isNull() || filePath.isEmpty()))
257 editSource->setText(QDir::toNativeSeparators(filePath));
259 QString path = QFileInfo(filePath).path();
260 QString name = QFileInfo(filePath).completeBaseName();
262 QString outPath = QString("%1/%2.mkv").arg(path, name);
264 if(QFileInfo(outPath).exists())
267 while(QFileInfo(outPath).exists())
269 outPath = QString("%1/%2 (%3).mkv").arg(path, name, QString::number(i++));
273 editOutput->setText(QDir::toNativeSeparators(outPath));
276 else if(QObject::sender() == buttonBrowseOutput)
279 filters += tr("Matroska Files (*.mkv)").append(";;");
280 filters += tr("MPEG-4 Part 14 Container (*.mp4)").append(";;");
281 filters += tr("H.264 Elementary Stream (*.264)");
283 QString filePath = QFileDialog::getSaveFileName(this, tr("Choose Output File"), initialDir, filters);
285 if(!(filePath.isNull() || filePath.isEmpty()))
287 editOutput->setText(QDir::toNativeSeparators(filePath));
292 void AddJobDialog::configurationChanged(void)
294 OptionsModel* options = reinterpret_cast<OptionsModel*>(cbxTemplate->itemData(cbxTemplate->currentIndex()).value<void*>());
297 cbxTemplate->blockSignals(true);
298 cbxTemplate->insertItem(0, tr("<Unsaved Configuration>"), QVariant::fromValue<void*>(NULL));
299 cbxTemplate->setCurrentIndex(0);
300 cbxTemplate->blockSignals(false);
304 void AddJobDialog::templateSelected(void)
306 OptionsModel* options = reinterpret_cast<OptionsModel*>(cbxTemplate->itemData(cbxTemplate->currentIndex()).value<void*>());
309 qDebug("Loading options!");
310 for(int i = 0; i < cbxTemplate->model()->rowCount(); i++)
312 OptionsModel* temp = reinterpret_cast<OptionsModel*>(cbxTemplate->itemData(i).value<void*>());
315 cbxTemplate->blockSignals(true);
316 cbxTemplate->removeItem(i);
317 cbxTemplate->blockSignals(false);
321 restoreOptions(options);
324 modeIndexChanged(cbxRateControlMode->currentIndex());
327 void AddJobDialog::saveTemplateButtonClicked(void)
329 qDebug("Saving template");
330 QString name = tr("New Template");
335 name = QInputDialog::getText(this, tr("Save Template"), tr("Please enter the name of the template:"), QLineEdit::Normal, name, &ok).simplified();
337 if(name.startsWith("<") || name.endsWith(">"))
339 QMessageBox::warning (this, tr("Invalid Name"), tr("Sorry, the name you have entered is invalid!"));
340 while(name.startsWith("<")) name = name.mid(1).trimmed();
341 while(name.endsWith(">")) name = name.left(name.size() - 1).trimmed();
344 if(OptionsModel::templateExists(name))
346 QMessageBox::warning (this, tr("Already Exists"), tr("Sorry, a template of that name already exists!"));
352 OptionsModel *options = new OptionsModel();
353 saveOptions(options);
355 if(options->equals(m_defaults))
357 QMessageBox::warning (this, tr("Default"), tr("It makes no sense to save the defaults!"));
358 X264_DELETE(options);
361 if(!OptionsModel::saveTemplate(options, name))
363 QMessageBox::warning (this, tr("Save Failed"), tr("Sorry, the template could not be used!"));
364 X264_DELETE(options);
368 int index = cbxTemplate->model()->rowCount();
369 cbxTemplate->blockSignals(true);
370 cbxTemplate->insertItem(index, name, QVariant::fromValue<void*>(options));
371 cbxTemplate->setCurrentIndex(index);
372 for(int i = 0; i < cbxTemplate->model()->rowCount(); i++)
374 OptionsModel* temp = reinterpret_cast<OptionsModel*>(cbxTemplate->itemData(i).value<void*>());
377 cbxTemplate->removeItem(i);
381 cbxTemplate->blockSignals(false);
384 void AddJobDialog::deleteTemplateButtonClicked(void)
386 const int index = cbxTemplate->currentIndex();
387 QString name = cbxTemplate->itemText(index);
389 if(name.startsWith("<") || name.endsWith(">"))
391 QMessageBox::warning (this, tr("Invalid Item"), tr("Sorry, the selected item cannot be deleted!"));
395 OptionsModel::deleteTemplate(name);
396 OptionsModel *item = reinterpret_cast<OptionsModel*>(cbxTemplate->itemData(index).value<void*>());
397 cbxTemplate->removeItem(index);
401 ///////////////////////////////////////////////////////////////////////////////
403 ///////////////////////////////////////////////////////////////////////////////
405 QString AddJobDialog::sourceFile(void)
407 return QDir::fromNativeSeparators(editSource->text());
410 QString AddJobDialog::outputFile(void)
412 return QDir::fromNativeSeparators(editOutput->text());
415 ///////////////////////////////////////////////////////////////////////////////
417 ///////////////////////////////////////////////////////////////////////////////
419 void AddJobDialog::loadTemplateList(void)
421 cbxTemplate->addItem(tr("<Default>"), QVariant::fromValue<void*>(m_defaults));
422 cbxTemplate->setCurrentIndex(0);
424 QMap<QString, OptionsModel*> templates = OptionsModel::loadAllTemplates();
425 QStringList templateNames = templates.keys();
426 templateNames.sort();
428 while(!templateNames.isEmpty())
430 QString current = templateNames.takeFirst();
431 cbxTemplate->addItem(current, QVariant::fromValue<void*>(templates.value(current)));
433 if(templates.value(current)->equals(m_options))
435 cbxTemplate->setCurrentIndex(cbxTemplate->count() - 1);
439 if((cbxTemplate->currentIndex() == 0) && (!m_options->equals(m_defaults)))
441 cbxTemplate->insertItem(1, tr("<Recently Used>"), QVariant::fromValue<void*>(m_options));
442 cbxTemplate->setCurrentIndex(1);
446 void AddJobDialog::updateComboBox(QComboBox *cbox, const QString &text)
448 for(int i = 0; i < cbox->model()->rowCount(); i++)
450 if(cbox->model()->data(cbox->model()->index(i, 0, QModelIndex())).toString().compare(text, Qt::CaseInsensitive) == 0)
452 cbox->setCurrentIndex(i);
458 void AddJobDialog::restoreOptions(OptionsModel *options)
460 cbxRateControlMode->blockSignals(true);
461 spinQuantizer->blockSignals(true);
462 spinBitrate->blockSignals(true);
463 cbxPreset->blockSignals(true);
464 cbxTuning->blockSignals(true);
465 cbxProfile->blockSignals(true);
466 editCustomParams->blockSignals(true);
468 cbxRateControlMode->setCurrentIndex(options->rcMode());
469 spinQuantizer->setValue(options->quantizer());
470 spinBitrate->setValue(options->bitrate());
471 updateComboBox(cbxPreset, options->preset());
472 updateComboBox(cbxTuning, options->tune());
473 updateComboBox(cbxProfile, options->profile());
474 editCustomParams->setText(options->custom());
476 cbxRateControlMode->blockSignals(false);
477 spinQuantizer->blockSignals(false);
478 spinBitrate->blockSignals(false);
479 cbxPreset->blockSignals(false);
480 cbxTuning->blockSignals(false);
481 cbxProfile->blockSignals(false);
482 editCustomParams->blockSignals(false);
485 void AddJobDialog::saveOptions(OptionsModel *options)
487 options->setRCMode(static_cast<OptionsModel::RCMode>(cbxRateControlMode->currentIndex()));
488 options->setQuantizer(spinQuantizer->value());
489 options->setBitrate(spinBitrate->value());
490 options->setPreset(cbxPreset->model()->data(cbxPreset->model()->index(cbxPreset->currentIndex(), 0)).toString());
491 options->setTune(cbxTuning->model()->data(cbxTuning->model()->index(cbxTuning->currentIndex(), 0)).toString());
492 options->setProfile(cbxProfile->model()->data(cbxProfile->model()->index(cbxProfile->currentIndex(), 0)).toString());
493 options->setCustom(editCustomParams->text().simplified());
496 QString AddJobDialog::makeFileFilter(void)
498 QString filters("All supported files (");
500 for(size_t index = 0; g_filters[index].name && g_filters[index].fext; index++)
502 filters += QString((index > 0) ? " *.%1" : "*.%1").arg(QString::fromLatin1(g_filters[index].fext));
505 filters += QString(");;");
507 for(size_t index = 0; g_filters[index].name && g_filters[index].fext; index++)
509 filters += QString("%1 (*.%2);;").arg(QString::fromLatin1(g_filters[index].name), QString::fromLatin1(g_filters[index].fext));
512 filters += QString("All files (*.*)");