Added support for saving and deleting custom templates.

This commit is contained in:
LoRd_MuldeR 2012-01-31 22:30:21 +01:00
parent 932610e822
commit 18a3411e6e
4 changed files with 218 additions and 9 deletions

View File

@ -21,6 +21,10 @@
#include "model_options.h"
#include <QDesktopServices>
#include <QSettings>
#include <QStringList>
OptionsModel::OptionsModel(void)
{
m_rcMode = RCMode_CRF;
@ -72,3 +76,91 @@ bool OptionsModel::equals(OptionsModel *model)
return equal;
}
bool OptionsModel::saveTemplate(OptionsModel *model, const QString &name)
{
const QString templateName = name.simplified();
const QString appDir = QDesktopServices::storageLocation(QDesktopServices::DataLocation);
if(templateName.startsWith("<") || templateName.endsWith(">") || templateName.contains("\\") || templateName.contains("/"))
{
return false;
}
QSettings settings(QString("%1/templates.ini").arg(appDir), QSettings::IniFormat);
settings.beginGroup(templateName);
settings.setValue("rate_control_mode", model->m_rcMode);
settings.setValue("target_bitrate", model->m_bitrate);
settings.setValue("target_quantizer", model->m_quantizer);
settings.setValue("preset_name", model->m_preset);
settings.setValue("tuning_name", model->m_tune);
settings.setValue("profile_name", model->m_profile);
settings.setValue("custom_params", model->m_custom);
settings.endGroup();
settings.sync();
return true;
}
QMap<QString, OptionsModel*> OptionsModel::loadAllTemplates(void)
{
QMap<QString, OptionsModel*> list;
const QString appDir = QDesktopServices::storageLocation(QDesktopServices::DataLocation);
QSettings settings(QString("%1/templates.ini").arg(appDir), QSettings::IniFormat);
QStringList allTemplates = settings.childGroups();
while(!allTemplates.isEmpty())
{
settings.beginGroup(allTemplates.takeFirst());
bool complete = true;
if(!settings.contains("rate_control_mode")) complete = false;
if(!settings.contains("target_bitrate")) complete = false;
if(!settings.contains("target_quantizer")) complete = false;
if(!settings.contains("preset_name")) complete = false;
if(!settings.contains("tuning_name")) complete = false;
if(!settings.contains("profile_name")) complete = false;
if(!settings.contains("custom_params")) complete = false;
if(complete)
{
OptionsModel *options = new OptionsModel();
options->setRCMode(static_cast<OptionsModel::RCMode>(settings.value("rate_control_mode", options->m_rcMode).toInt()));
options->setBitrate(settings.value("target_bitrate", options->m_bitrate).toUInt());
options->setQuantizer(settings.value("target_quantizer", options->m_quantizer).toUInt());
options->setPreset(settings.value("preset_name", options->m_preset).toString());
options->setTune(settings.value("tuning_name", options->m_tune).toString());
options->setProfile(settings.value("profile_name", options->m_profile).toString());
options->setCustom(settings.value("custom_params", options->m_custom).toString());
list.insert(settings.group(), options);
}
settings.endGroup();
}
return list;
}
bool OptionsModel::templateExists(const QString &name)
{
const QString appDir = QDesktopServices::storageLocation(QDesktopServices::DataLocation);
QSettings settings(QString("%1/templates.ini").arg(appDir), QSettings::IniFormat);
QStringList allGroups = settings.childGroups();
return allGroups.contains(name, Qt::CaseInsensitive);
}
bool OptionsModel::deleteTemplate(const QString &name)
{
const QString appDir = QDesktopServices::storageLocation(QDesktopServices::DataLocation);
QSettings settings(QString("%1/templates.ini").arg(appDir), QSettings::IniFormat);
if(settings.childGroups().contains(name, Qt::CaseInsensitive))
{
settings.remove(name);
return true;
}
return false;
}

View File

@ -23,6 +23,7 @@
#include <QObject>
#include <QString>
#include <QMap>
class OptionsModel
{
@ -59,8 +60,12 @@ public:
//Stuff
bool equals(OptionsModel *model);
//Helper
//Static functions
static QString rcMode2String(RCMode mode);
static bool saveTemplate(OptionsModel *model, const QString &name);
static QMap<QString, OptionsModel*> loadAllTemplates(void);
static bool templateExists(const QString &name);
static bool deleteTemplate(const QString &name);
protected:
RCMode m_rcMode;

View File

@ -32,6 +32,7 @@
#include <QDesktopServices>
#include <QValidator>
#include <QDir>
#include <QInputDialog>
static const struct
{
@ -129,6 +130,8 @@ AddJobDialog::AddJobDialog(QWidget *parent, OptionsModel *options)
//Activate buttons
connect(buttonBrowseSource, SIGNAL(clicked()), this, SLOT(browseButtonClicked()));
connect(buttonBrowseOutput, SIGNAL(clicked()), this, SLOT(browseButtonClicked()));
connect(buttonSaveTemplate, SIGNAL(clicked()), this, SLOT(saveTemplateButtonClicked()));
connect(buttonDeleteTemplate, SIGNAL(clicked()), this, SLOT(deleteTemplateButtonClicked()));
//Setup validator
editCustomParams->installEventFilter(this);
@ -147,19 +150,24 @@ AddJobDialog::AddJobDialog(QWidget *parent, OptionsModel *options)
connect(cbxProfile, SIGNAL(currentIndexChanged(int)), this, SLOT(configurationChanged()));
connect(editCustomParams, SIGNAL(textChanged(QString)), this, SLOT(configurationChanged()));
//Monitor template
cbxTemplate->insertItem(0, tr("<Default>"), QVariant::fromValue<void*>(m_defaults));
cbxTemplate->setCurrentIndex(0);
if(!m_options->equals(m_defaults))
{
cbxTemplate->insertItem(1, tr("<Recently Used>"), QVariant::fromValue<void*>(m_options));
cbxTemplate->setCurrentIndex(1);
}
//Setup template selector
loadTemplateList();
connect(cbxTemplate, SIGNAL(currentIndexChanged(int)), this, SLOT(templateSelected()));
}
AddJobDialog::~AddJobDialog(void)
{
for(int i = 0; i < cbxTemplate->model()->rowCount(); i++)
{
if(cbxTemplate->itemText(i).startsWith("<") || cbxTemplate->itemText(i).endsWith(">"))
{
continue;
}
OptionsModel *item = reinterpret_cast<OptionsModel*>(cbxTemplate->itemData(i).value<void*>());
cbxTemplate->setItemData(i, QVariant::fromValue<void*>(NULL));
X264_DELETE(item);
}
X264_DELETE(m_defaults);
}
@ -316,6 +324,80 @@ void AddJobDialog::templateSelected(void)
modeIndexChanged(cbxRateControlMode->currentIndex());
}
void AddJobDialog::saveTemplateButtonClicked(void)
{
qDebug("Saving template");
QString name = tr("New Template");
forever
{
bool ok = false;
name = QInputDialog::getText(this, tr("Save Template"), tr("Please enter the name of the template:"), QLineEdit::Normal, name, &ok).simplified();
if(!ok) return;
if(name.startsWith("<") || name.endsWith(">"))
{
QMessageBox::warning (this, tr("Invalid Name"), tr("Sorry, the name you have entered is invalid!"));
while(name.startsWith("<")) name = name.mid(1).trimmed();
while(name.endsWith(">")) name = name.left(name.size() - 1).trimmed();
continue;
}
if(OptionsModel::templateExists(name))
{
QMessageBox::warning (this, tr("Already Exists"), tr("Sorry, a template of that name already exists!"));
continue;
}
break;
}
OptionsModel *options = new OptionsModel();
saveOptions(options);
if(options->equals(m_defaults))
{
QMessageBox::warning (this, tr("Default"), tr("It makes no sense to save the defaults!"));
X264_DELETE(options);
return;
}
if(!OptionsModel::saveTemplate(options, name))
{
QMessageBox::warning (this, tr("Save Failed"), tr("Sorry, the template could not be used!"));
X264_DELETE(options);
return;
}
int index = cbxTemplate->model()->rowCount();
cbxTemplate->blockSignals(true);
cbxTemplate->insertItem(index, name, QVariant::fromValue<void*>(options));
cbxTemplate->setCurrentIndex(index);
for(int i = 0; i < cbxTemplate->model()->rowCount(); i++)
{
OptionsModel* temp = reinterpret_cast<OptionsModel*>(cbxTemplate->itemData(i).value<void*>());
if(temp == NULL)
{
cbxTemplate->removeItem(i);
break;
}
}
cbxTemplate->blockSignals(false);
}
void AddJobDialog::deleteTemplateButtonClicked(void)
{
const int index = cbxTemplate->currentIndex();
QString name = cbxTemplate->itemText(index);
if(name.startsWith("<") || name.endsWith(">"))
{
QMessageBox::warning (this, tr("Invalid Item"), tr("Sorry, the selected item cannot be deleted!"));
return;
}
OptionsModel::deleteTemplate(name);
OptionsModel *item = reinterpret_cast<OptionsModel*>(cbxTemplate->itemData(index).value<void*>());
cbxTemplate->removeItem(index);
X264_DELETE(item);
}
///////////////////////////////////////////////////////////////////////////////
// Public functions
///////////////////////////////////////////////////////////////////////////////
@ -334,6 +416,33 @@ QString AddJobDialog::outputFile(void)
// Private functions
///////////////////////////////////////////////////////////////////////////////
void AddJobDialog::loadTemplateList(void)
{
cbxTemplate->addItem(tr("<Default>"), QVariant::fromValue<void*>(m_defaults));
cbxTemplate->setCurrentIndex(0);
QMap<QString, OptionsModel*> templates = OptionsModel::loadAllTemplates();
QStringList templateNames = templates.keys();
templateNames.sort();
while(!templateNames.isEmpty())
{
QString current = templateNames.takeFirst();
cbxTemplate->addItem(current, QVariant::fromValue<void*>(templates.value(current)));
if(templates.value(current)->equals(m_options))
{
cbxTemplate->setCurrentIndex(cbxTemplate->count() - 1);
}
}
if((cbxTemplate->currentIndex() == 0) && (!m_options->equals(m_defaults)))
{
cbxTemplate->insertItem(1, tr("<Recently Used>"), QVariant::fromValue<void*>(m_options));
cbxTemplate->setCurrentIndex(1);
}
}
void AddJobDialog::updateComboBox(QComboBox *cbox, const QString &text)
{
for(int i = 0; i < cbox->model()->rowCount(); i++)

View File

@ -54,10 +54,13 @@ private slots:
void browseButtonClicked(void);
void configurationChanged(void);
void templateSelected(void);
void saveTemplateButtonClicked(void);
void deleteTemplateButtonClicked(void);
virtual void accept(void);
private:
void loadTemplateList(void);
void restoreOptions(OptionsModel *options);
void saveOptions(OptionsModel *options);
void updateComboBox(QComboBox *cbox, const QString &text);