1 /***************************************************************************
2 * Copyright (C) 2008 by Jean-Baptiste Mardelle (jb@kdenlive.org) *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
20 #include "profilesdialog.h"
21 #include "kdenlivesettings.h"
23 #include <KStandardDirs>
25 #include <KMessageBox>
26 #include <KIO/NetAccess>
29 #include <QCloseEvent>
31 ProfilesDialog::ProfilesDialog(QWidget * parent) :
33 m_profileIsModified(false),
34 m_isCustomProfile(false)
38 QStringList profilesFilter;
39 profilesFilter << "*";
41 m_view.button_delete->setIcon(KIcon("trash-empty"));
42 m_view.button_delete->setToolTip(i18n("Delete profile"));
43 m_view.button_save->setIcon(KIcon("document-save"));
44 m_view.button_save->setToolTip(i18n("Save profile"));
45 m_view.button_create->setIcon(KIcon("document-new"));
46 m_view.button_create->setToolTip(i18n("Create new profile"));
50 connect(m_view.profiles_list, SIGNAL(currentIndexChanged(int)), this, SLOT(slotUpdateDisplay()));
51 connect(m_view.button_create, SIGNAL(clicked()), this, SLOT(slotCreateProfile()));
52 connect(m_view.button_save, SIGNAL(clicked()), this, SLOT(slotSaveProfile()));
53 connect(m_view.button_delete, SIGNAL(clicked()), this, SLOT(slotDeleteProfile()));
54 connect(m_view.button_default, SIGNAL(clicked()), this, SLOT(slotSetDefaultProfile()));
56 connect(m_view.description, SIGNAL(textChanged(const QString &)), this, SLOT(slotProfileEdited()));
57 connect(m_view.frame_num, SIGNAL(valueChanged(int)), this, SLOT(slotProfileEdited()));
58 connect(m_view.frame_den, SIGNAL(valueChanged(int)), this, SLOT(slotProfileEdited()));
59 connect(m_view.aspect_num, SIGNAL(valueChanged(int)), this, SLOT(slotProfileEdited()));
60 connect(m_view.aspect_den, SIGNAL(valueChanged(int)), this, SLOT(slotProfileEdited()));
61 connect(m_view.display_num, SIGNAL(valueChanged(int)), this, SLOT(slotProfileEdited()));
62 connect(m_view.display_den, SIGNAL(valueChanged(int)), this, SLOT(slotProfileEdited()));
63 connect(m_view.progressive, SIGNAL(stateChanged(int)), this, SLOT(slotProfileEdited()));
64 connect(m_view.size_h, SIGNAL(valueChanged(int)), this, SLOT(slotProfileEdited()));
65 connect(m_view.size_w, SIGNAL(valueChanged(int)), this, SLOT(slotProfileEdited()));
68 void ProfilesDialog::slotProfileEdited()
70 m_profileIsModified = true;
73 void ProfilesDialog::fillList(const QString selectedProfile)
75 // List the Mlt profiles
76 m_view.profiles_list->clear();
77 QMap <QString, QString> profilesInfo = ProfilesDialog::getProfilesInfo();
78 QMapIterator<QString, QString> i(profilesInfo);
81 m_view.profiles_list->addItem(i.key(), i.value());
84 if (!KdenliveSettings::default_profile().isEmpty()) {
85 for (int i = 0; i < m_view.profiles_list->count(); i++) {
86 if (m_view.profiles_list->itemData(i).toString() == KdenliveSettings::default_profile()) {
87 m_view.profiles_list->setCurrentIndex(i);
92 int ix = m_view.profiles_list->findText(selectedProfile);
93 if (ix != -1) m_view.profiles_list->setCurrentIndex(ix);
94 m_selectedProfileIndex = m_view.profiles_list->currentIndex();
97 void ProfilesDialog::accept()
99 if (askForSave()) QDialog::accept();
102 void ProfilesDialog::closeEvent(QCloseEvent *event)
111 bool ProfilesDialog::askForSave()
113 if (!m_profileIsModified) return true;
114 if (KMessageBox::questionYesNo(this, i18n("The custom profile was modified, do you want to save it?")) != KMessageBox::Yes) return true;
115 return slotSaveProfile();
118 void ProfilesDialog::slotCreateProfile()
120 m_view.button_delete->setEnabled(false);
121 m_view.button_create->setEnabled(false);
122 m_view.button_save->setEnabled(true);
123 m_view.properties->setEnabled(true);
126 void ProfilesDialog::slotSetDefaultProfile()
128 int ix = m_view.profiles_list->currentIndex();
129 QString path = m_view.profiles_list->itemData(ix).toString();
130 if (!path.isEmpty()) KdenliveSettings::setDefault_profile(path);
133 bool ProfilesDialog::slotSaveProfile()
135 const QString profileDesc = m_view.description->text();
136 int ix = m_view.profiles_list->findText(profileDesc);
138 // this profile name already exists
139 const QString path = m_view.profiles_list->itemData(ix).toString();
140 if (!path.contains('/')) {
141 KMessageBox::sorry(this, i18n("A profile with same name already exists in MLT's default profiles, please choose another description for your custom profile."));
147 QString customName = "profiles/customprofile";
148 QString profilePath = KStandardDirs::locateLocal("appdata", customName + QString::number(i));
149 kDebug() << " TYING PROFILE FILE: " << profilePath;
150 while (KIO::NetAccess::exists(KUrl(profilePath), KIO::NetAccess::SourceSide, this)) {
152 profilePath = KStandardDirs::locateLocal("appdata", customName + QString::number(i));
154 saveProfile(profilePath);
156 m_profileIsModified = false;
157 fillList(profileDesc);
158 m_view.button_create->setEnabled(true);
162 void ProfilesDialog::saveProfile(const QString path)
165 if (!file.open(QIODevice::WriteOnly)) {
166 KMessageBox::sorry(this, i18n("Cannot write to file %1", path));
169 QTextStream out(&file);
170 out << "description=" << m_view.description->text() << "\n" << "frame_rate_num=" << m_view.frame_num->value() << "\n" << "frame_rate_den=" << m_view.frame_den->value() << "\n" << "width=" << m_view.size_w->value() << "\n" << "height=" << m_view.size_h->value() << "\n" << "progressive=" << m_view.progressive->isChecked() << "\n" << "sample_aspect_num=" << m_view.aspect_num->value() << "\n" << "sample_aspect_den=" << m_view.aspect_den->value() << "\n" << "display_aspect_num=" << m_view.display_num->value() << "\n" << "display_aspect_den=" << m_view.display_den->value() << "\n";
171 if (file.error() != QFile::NoError) {
172 KMessageBox::error(this, i18n("Cannot write to file %1", path));
177 void ProfilesDialog::slotDeleteProfile()
179 const QString path = m_view.profiles_list->itemData(m_view.profiles_list->currentIndex()).toString();
180 if (path.contains('/')) {
181 KIO::NetAccess::del(KUrl(path), this);
183 } else kDebug() << "//// Cannot delete profile " << path << ", does not seem to be custom one";
187 MltVideoProfile ProfilesDialog::getVideoProfile(QString name)
189 MltVideoProfile result;
190 QStringList profilesNames;
191 QStringList profilesFiles;
192 QStringList profilesFilter;
193 profilesFilter << "*";
195 bool isCustom = false;
196 if (name.contains('/')) isCustom = true;
199 // List the Mlt profiles
200 profilesFiles = QDir(KdenliveSettings::mltpath()).entryList(profilesFilter, QDir::Files);
201 if (profilesFiles.contains(name)) path = KdenliveSettings::mltpath() + name;
203 if (isCustom || path.isEmpty()) {
207 if (path.isEmpty() || !QFile::exists(path)) {
208 if (name == "dv_pal") {
209 kDebug() << "!!! WARNING, COULD NOT FIND DEFAULT MLT PROFILE";
212 if (name == KdenliveSettings::default_profile()) KdenliveSettings::setDefault_profile("dv_pal");
213 kDebug() << "// WARNING, COULD NOT FIND PROFILE " << name;
216 KConfig confFile(path, KConfig::SimpleConfig);
218 result.description = confFile.entryMap().value("description");
219 result.frame_rate_num = confFile.entryMap().value("frame_rate_num").toInt();
220 result.frame_rate_den = confFile.entryMap().value("frame_rate_den").toInt();
221 result.width = confFile.entryMap().value("width").toInt();
222 result.height = confFile.entryMap().value("height").toInt();
223 result.progressive = confFile.entryMap().value("progressive").toInt();
224 result.sample_aspect_num = confFile.entryMap().value("sample_aspect_num").toInt();
225 result.sample_aspect_den = confFile.entryMap().value("sample_aspect_den").toInt();
226 result.display_aspect_num = confFile.entryMap().value("display_aspect_num").toInt();
227 result.display_aspect_den = confFile.entryMap().value("display_aspect_den").toInt();
233 bool ProfilesDialog::existingProfileDescription(const QString &desc)
235 QStringList profilesFilter;
236 profilesFilter << "*";
238 // List the Mlt profiles
239 QStringList profilesFiles = QDir(KdenliveSettings::mltpath()).entryList(profilesFilter, QDir::Files);
240 for (int i = 0; i < profilesFiles.size(); ++i) {
241 KConfig confFile(KdenliveSettings::mltpath() + profilesFiles.at(i), KConfig::SimpleConfig);
242 if (desc == confFile.entryMap().value("description")) return true;
245 // List custom profiles
246 QStringList customProfiles = KGlobal::dirs()->findDirs("appdata", "profiles");
247 for (int i = 0; i < customProfiles.size(); ++i) {
248 profilesFiles = QDir(customProfiles.at(i)).entryList(profilesFilter, QDir::Files);
249 for (int j = 0; j < profilesFiles.size(); ++j) {
250 KConfig confFile(customProfiles.at(i) + profilesFiles.at(j), KConfig::SimpleConfig);
251 if (desc == confFile.entryMap().value("description")) return true;
258 QString ProfilesDialog::existingProfile(MltVideoProfile profile)
260 // Check if the profile has a matching entry in existing ones
261 QStringList profilesFilter;
262 profilesFilter << "*";
264 // Check the Mlt profiles
265 QStringList profilesFiles = QDir(KdenliveSettings::mltpath()).entryList(profilesFilter, QDir::Files);
266 for (int i = 0; i < profilesFiles.size(); ++i) {
267 KConfig confFile(KdenliveSettings::mltpath() + profilesFiles.at(i), KConfig::SimpleConfig);
268 if (profile.display_aspect_den != confFile.entryMap().value("display_aspect_den").toInt()) continue;
269 if (profile.display_aspect_num != confFile.entryMap().value("display_aspect_num").toInt()) continue;
270 if (profile.sample_aspect_den != confFile.entryMap().value("sample_aspect_den").toInt()) continue;
271 if (profile.sample_aspect_num != confFile.entryMap().value("sample_aspect_num").toInt()) continue;
272 if (profile.width != confFile.entryMap().value("width").toInt()) continue;
273 if (profile.height != confFile.entryMap().value("height").toInt()) continue;
274 if (profile.frame_rate_den != confFile.entryMap().value("frame_rate_den").toInt()) continue;
275 if (profile.frame_rate_num != confFile.entryMap().value("frame_rate_num").toInt()) continue;
276 if (profile.progressive != confFile.entryMap().value("progressive").toInt()) continue;
277 return profilesFiles.at(i);
280 // Check custom profiles
281 QStringList customProfiles = KGlobal::dirs()->findDirs("appdata", "profiles");
282 for (int i = 0; i < customProfiles.size(); ++i) {
283 profilesFiles = QDir(customProfiles.at(i)).entryList(profilesFilter, QDir::Files);
284 for (int j = 0; j < profilesFiles.size(); ++j) {
285 KConfig confFile(customProfiles.at(i) + profilesFiles.at(j), KConfig::SimpleConfig);
286 if (profile.display_aspect_den != confFile.entryMap().value("display_aspect_den").toInt()) continue;
287 if (profile.display_aspect_num != confFile.entryMap().value("display_aspect_num").toInt()) continue;
288 if (profile.sample_aspect_den != confFile.entryMap().value("sample_aspect_den").toInt()) continue;
289 if (profile.sample_aspect_num != confFile.entryMap().value("sample_aspect_num").toInt()) continue;
290 if (profile.width != confFile.entryMap().value("width").toInt()) continue;
291 if (profile.height != confFile.entryMap().value("height").toInt()) continue;
292 if (profile.frame_rate_den != confFile.entryMap().value("frame_rate_den").toInt()) continue;
293 if (profile.frame_rate_num != confFile.entryMap().value("frame_rate_num").toInt()) continue;
294 if (profile.progressive != confFile.entryMap().value("progressive").toInt()) continue;
295 return customProfiles.at(i) + profilesFiles.at(j);
302 QMap <QString, QString> ProfilesDialog::getProfilesInfo()
304 QMap <QString, QString> result;
305 QStringList profilesFilter;
306 profilesFilter << "*";
308 // List the Mlt profiles
309 QStringList profilesFiles = QDir(KdenliveSettings::mltpath()).entryList(profilesFilter, QDir::Files);
310 for (int i = 0; i < profilesFiles.size(); ++i) {
311 KConfig confFile(KdenliveSettings::mltpath() + profilesFiles.at(i), KConfig::SimpleConfig);
312 QString desc = confFile.entryMap().value("description");
313 if (!desc.isEmpty()) result.insert(desc, profilesFiles.at(i));
316 // List custom profiles
317 QStringList customProfiles = KGlobal::dirs()->findDirs("appdata", "profiles");
318 for (int i = 0; i < customProfiles.size(); ++i) {
319 profilesFiles = QDir(customProfiles.at(i)).entryList(profilesFilter, QDir::Files);
320 for (int j = 0; j < profilesFiles.size(); ++j) {
321 KConfig confFile(customProfiles.at(i) + profilesFiles.at(j), KConfig::SimpleConfig);
322 QString desc = confFile.entryMap().value("description");
323 if (!desc.isEmpty()) result.insert(desc, customProfiles.at(i) + profilesFiles.at(j));
330 QMap< QString, QString > ProfilesDialog::getSettingsFromFile(const QString path)
332 QStringList profilesNames;
333 QStringList profilesFiles;
334 QStringList profilesFilter;
335 profilesFilter << "*";
337 if (!path.contains('/')) {
338 // This is an MLT profile
339 KConfig confFile(KdenliveSettings::mltpath() + path, KConfig::SimpleConfig);
340 return confFile.entryMap();
342 // This is a custom profile
343 KConfig confFile(path, KConfig::SimpleConfig);
344 return confFile.entryMap();
349 QMap< QString, QString > ProfilesDialog::getSettingsForProfile(const QString profileName)
351 QStringList profilesNames;
352 QStringList profilesFiles;
353 QStringList profilesFilter;
354 profilesFilter << "*";
356 // List the Mlt profiles
357 profilesFiles = QDir(KdenliveSettings::mltpath()).entryList(profilesFilter, QDir::Files);
358 for (int i = 0; i < profilesFiles.size(); ++i) {
359 KConfig confFile(KdenliveSettings::mltpath() + profilesFiles.at(i), KConfig::SimpleConfig);
360 QMap< QString, QString > values = confFile.entryMap();
361 if (values.value("description") == profileName) {
362 values.insert("path", profilesFiles.at(i));
367 // List custom profiles
368 QStringList customProfiles = KGlobal::dirs()->findDirs("appdata", "profiles");
369 for (int i = 0; i < customProfiles.size(); ++i) {
370 QStringList profiles = QDir(customProfiles.at(i)).entryList(profilesFilter, QDir::Files);
371 for (int i = 0; i < profiles.size(); ++i) {
372 KConfig confFile(customProfiles.at(i) + profiles.at(i), KConfig::SimpleConfig);
373 QMap< QString, QString > values = confFile.entryMap();
374 if (values.value("description") == profileName) {
375 values.insert("path", customProfiles.at(i) + profiles.at(i));
380 return QMap< QString, QString >();
384 QString ProfilesDialog::getPathFromDescription(const QString profileDesc)
386 QStringList profilesNames;
387 QStringList profilesFiles;
388 QStringList profilesFilter;
389 profilesFilter << "*";
391 // List the Mlt profiles
392 profilesFiles = QDir(KdenliveSettings::mltpath()).entryList(profilesFilter, QDir::Files);
393 for (int i = 0; i < profilesFiles.size(); ++i) {
394 KConfig confFile(KdenliveSettings::mltpath() + profilesFiles.at(i), KConfig::SimpleConfig);
395 QMap< QString, QString > values = confFile.entryMap();
396 if (values.value("description") == profileDesc) return profilesFiles.at(i);
399 // List custom profiles
400 QStringList customProfiles = KGlobal::dirs()->findDirs("appdata", "profiles");
401 for (int i = 0; i < customProfiles.size(); ++i) {
402 QStringList profiles = QDir(customProfiles.at(i)).entryList(profilesFilter, QDir::Files);
403 for (int i = 0; i < profiles.size(); ++i) {
404 KConfig confFile(customProfiles.at(i) + profiles.at(i), KConfig::SimpleConfig);
405 QMap< QString, QString > values = confFile.entryMap();
406 if (values.value("description") == profileDesc) return customProfiles.at(i) + profiles.at(i);
413 void ProfilesDialog::saveProfile(MltVideoProfile &profile)
416 QString customName = "profiles/customprofile";
417 QString profilePath = KStandardDirs::locateLocal("appdata", customName + QString::number(i));
418 kDebug() << " TYING PROFILE FILE: " << profilePath;
419 while (KIO::NetAccess::exists(KUrl(profilePath), KIO::NetAccess::SourceSide, 0)) {
421 profilePath = KStandardDirs::locateLocal("appdata", customName + QString::number(i));
423 QFile file(profilePath);
424 if (!file.open(QIODevice::WriteOnly)) {
425 KMessageBox::sorry(0, i18n("Cannot write to file %1", profilePath));
428 QTextStream out(&file);
429 out << "description=" << profile.description << "\n" << "frame_rate_num=" << profile.frame_rate_num << "\n" << "frame_rate_den=" << profile.frame_rate_den << "\n" << "width=" << profile.width << "\n" << "height=" << profile.height << "\n" << "progressive=" << profile.progressive << "\n" << "sample_aspect_num=" << profile.sample_aspect_num << "\n" << "sample_aspect_den=" << profile.sample_aspect_den << "\n" << "display_aspect_num=" << profile.display_aspect_num << "\n" << "display_aspect_den=" << profile.display_aspect_den << "\n";
430 if (file.error() != QFile::NoError) {
431 KMessageBox::error(0, i18n("Cannot write to file %1", profilePath));
434 profile.path = profilePath;
438 void ProfilesDialog::slotUpdateDisplay()
440 if (askForSave() == false) {
441 m_view.profiles_list->blockSignals(true);
442 m_view.profiles_list->setCurrentIndex(m_selectedProfileIndex);
443 m_view.profiles_list->blockSignals(false);
447 m_selectedProfileIndex = m_view.profiles_list->currentIndex();
448 QString currentProfile = m_view.profiles_list->itemData(m_view.profiles_list->currentIndex()).toString();
449 m_isCustomProfile = currentProfile.contains('/');
450 m_view.button_delete->setEnabled(m_isCustomProfile);
451 m_view.properties->setEnabled(m_isCustomProfile);
452 m_view.button_save->setEnabled(m_isCustomProfile);
453 QMap< QString, QString > values = ProfilesDialog::getSettingsFromFile(currentProfile);
454 m_view.description->setText(values.value("description"));
455 m_view.size_w->setValue(values.value("width").toInt());
456 m_view.size_h->setValue(values.value("height").toInt());
457 m_view.aspect_num->setValue(values.value("sample_aspect_num").toInt());
458 m_view.aspect_den->setValue(values.value("sample_aspect_den").toInt());
459 m_view.display_num->setValue(values.value("display_aspect_num").toInt());
460 m_view.display_den->setValue(values.value("display_aspect_den").toInt());
461 m_view.frame_num->setValue(values.value("frame_rate_num").toInt());
462 m_view.frame_den->setValue(values.value("frame_rate_den").toInt());
463 m_view.progressive->setChecked(values.value("progressive").toInt());
464 m_profileIsModified = false;
468 #include "profilesdialog.moc"