OSDN Git Service

Added "Pause" support.
[x264-launcher/x264-launcher.git] / src / model_options.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2012 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 "model_options.h"
23
24 #include <QDesktopServices>
25 #include <QSettings>
26 #include <QStringList>
27
28 OptionsModel::OptionsModel(void)
29 {
30         m_rcMode = RCMode_CRF;
31         m_bitrate = 1200;
32         m_quantizer = 22;
33         m_preset = "Medium";
34         m_tune = "None";
35         m_profile = "Auto";
36         m_custom = "";
37 }
38
39 OptionsModel::~OptionsModel(void)
40 {
41 }
42
43 QString OptionsModel::rcMode2String(RCMode mode)
44 {
45         switch(mode)
46         {
47         case RCMode_CRF:
48                 return QObject::tr("CRF");
49                 break;
50         case RCMode_CQ:
51                 return QObject::tr("CQ");
52                 break;
53         case RCMode_2Pass:
54                 return QObject::tr("2-Pass");
55                 break;
56         case RCMode_ABR:
57                 return QObject::tr("ABR");
58                 break;
59         default:
60                 return QString();
61                 break;
62         }
63 }
64
65 bool OptionsModel::equals(OptionsModel *model)
66 {
67         bool equal = true;
68         
69         if(this->m_rcMode != model->m_rcMode) equal = false;
70         if(this->m_bitrate!= model->m_bitrate) equal = false;
71         if(this->m_quantizer != model->m_quantizer) equal = false;
72         if(this->m_preset.compare(model->m_preset, Qt::CaseInsensitive)) equal = false;
73         if(this->m_tune.compare(model->m_tune, Qt::CaseInsensitive)) equal = false;
74         if(this->m_profile.compare(model->m_profile, Qt::CaseInsensitive)) equal = false;
75         if(this->m_custom.compare(model->m_custom, Qt::CaseInsensitive)) equal = false;
76
77         return equal;
78 }
79
80 bool OptionsModel::saveTemplate(OptionsModel *model, const QString &name)
81 {
82         const QString templateName = name.simplified();
83         const QString appDir = QDesktopServices::storageLocation(QDesktopServices::DataLocation);
84
85         if(templateName.startsWith("<") || templateName.endsWith(">") || templateName.contains("\\") || templateName.contains("/"))
86         {
87                 return false;
88         }
89
90         QSettings settings(QString("%1/templates.ini").arg(appDir), QSettings::IniFormat);
91         settings.beginGroup(templateName);
92         
93         settings.setValue("rate_control_mode", model->m_rcMode);
94         settings.setValue("target_bitrate", model->m_bitrate);
95         settings.setValue("target_quantizer", model->m_quantizer);
96         settings.setValue("preset_name", model->m_preset);
97         settings.setValue("tuning_name", model->m_tune);
98         settings.setValue("profile_name", model->m_profile);
99         settings.setValue("custom_params", model->m_custom);
100         
101         settings.endGroup();
102         settings.sync();
103         
104         return true;
105 }
106
107 QMap<QString, OptionsModel*> OptionsModel::loadAllTemplates(void)
108 {
109         QMap<QString, OptionsModel*> list;
110         const QString appDir = QDesktopServices::storageLocation(QDesktopServices::DataLocation);
111         QSettings settings(QString("%1/templates.ini").arg(appDir), QSettings::IniFormat);
112         QStringList allTemplates = settings.childGroups();
113
114         while(!allTemplates.isEmpty())
115         {
116                 settings.beginGroup(allTemplates.takeFirst());
117
118                 bool complete = true;
119                 if(!settings.contains("rate_control_mode")) complete = false;
120                 if(!settings.contains("target_bitrate")) complete = false;
121                 if(!settings.contains("target_quantizer")) complete = false;
122                 if(!settings.contains("preset_name")) complete = false;
123                 if(!settings.contains("tuning_name")) complete = false;
124                 if(!settings.contains("profile_name")) complete = false;
125                 if(!settings.contains("custom_params")) complete = false;
126
127                 if(complete)
128                 {
129                         OptionsModel *options = new OptionsModel();
130                         options->setRCMode(static_cast<OptionsModel::RCMode>(settings.value("rate_control_mode", options->m_rcMode).toInt()));
131                         options->setBitrate(settings.value("target_bitrate", options->m_bitrate).toUInt());
132                         options->setQuantizer(settings.value("target_quantizer", options->m_quantizer).toUInt());
133                         options->setPreset(settings.value("preset_name", options->m_preset).toString());
134                         options->setTune(settings.value("tuning_name", options->m_tune).toString());
135                         options->setProfile(settings.value("profile_name", options->m_profile).toString());
136                         options->setCustom(settings.value("custom_params", options->m_custom).toString());
137                         list.insert(settings.group(), options);
138                 }
139
140                 settings.endGroup();
141         }
142
143         return list;
144 }
145
146 bool OptionsModel::templateExists(const QString &name)
147 {
148         const QString appDir = QDesktopServices::storageLocation(QDesktopServices::DataLocation);
149         QSettings settings(QString("%1/templates.ini").arg(appDir), QSettings::IniFormat);
150         QStringList allGroups = settings.childGroups();
151         return allGroups.contains(name, Qt::CaseInsensitive);
152 }
153
154 bool OptionsModel::deleteTemplate(const QString &name)
155 {
156         const QString appDir = QDesktopServices::storageLocation(QDesktopServices::DataLocation);
157         QSettings settings(QString("%1/templates.ini").arg(appDir), QSettings::IniFormat);
158
159         if(settings.childGroups().contains(name, Qt::CaseInsensitive))
160         {
161                 settings.remove(name);
162                 return true;
163         }
164
165         return false;
166 }