OSDN Git Service

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