]> git.sesse.net Git - kdenlive/blob - src/profilesdialog.cpp
Allow to manually edit video4linux capture profile
[kdenlive] / src / profilesdialog.cpp
1 /***************************************************************************
2  *   Copyright (C) 2008 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
3  *                                                                         *
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.                                   *
8  *                                                                         *
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.                          *
13  *                                                                         *
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  ***************************************************************************/
19
20 #include "profilesdialog.h"
21 #include "kdenlivesettings.h"
22
23 #include <KStandardDirs>
24 #include <KDebug>
25 #include <KMessageBox>
26 #include <KIO/NetAccess>
27
28 #include <QDir>
29 #include <QCloseEvent>
30
31 ProfilesDialog::ProfilesDialog(QWidget * parent) :
32     QDialog(parent),
33     m_profileIsModified(false),
34     m_isCustomProfile(false)
35 {
36     m_view.setupUi(this);
37
38     // Fill colorspace list (see mlt_profile.h)
39     m_view.colorspace->addItem(getColorspaceDescription(601), 601);
40     m_view.colorspace->addItem(getColorspaceDescription(709), 709);
41     m_view.colorspace->addItem(getColorspaceDescription(240), 240);
42     m_view.colorspace->addItem(getColorspaceDescription(0), 0);
43
44     QStringList profilesFilter;
45     profilesFilter << "*";
46
47     m_view.button_delete->setIcon(KIcon("trash-empty"));
48     m_view.button_delete->setToolTip(i18n("Delete profile"));
49     m_view.button_save->setIcon(KIcon("document-save"));
50     m_view.button_save->setToolTip(i18n("Save profile"));
51     m_view.button_create->setIcon(KIcon("document-new"));
52     m_view.button_create->setToolTip(i18n("Create new profile"));
53
54     fillList();
55     slotUpdateDisplay();
56     connect(m_view.profiles_list, SIGNAL(currentIndexChanged(int)), this, SLOT(slotUpdateDisplay()));
57     connect(m_view.button_create, SIGNAL(clicked()), this, SLOT(slotCreateProfile()));
58     connect(m_view.button_save, SIGNAL(clicked()), this, SLOT(slotSaveProfile()));
59     connect(m_view.button_delete, SIGNAL(clicked()), this, SLOT(slotDeleteProfile()));
60     connect(m_view.button_default, SIGNAL(clicked()), this, SLOT(slotSetDefaultProfile()));
61
62     connect(m_view.description, SIGNAL(textChanged(const QString &)), this, SLOT(slotProfileEdited()));
63     connect(m_view.frame_num, SIGNAL(valueChanged(int)), this, SLOT(slotProfileEdited()));
64     connect(m_view.frame_den, SIGNAL(valueChanged(int)), this, SLOT(slotProfileEdited()));
65     connect(m_view.aspect_num, SIGNAL(valueChanged(int)), this, SLOT(slotProfileEdited()));
66     connect(m_view.aspect_den, SIGNAL(valueChanged(int)), this, SLOT(slotProfileEdited()));
67     connect(m_view.display_num, SIGNAL(valueChanged(int)), this, SLOT(slotProfileEdited()));
68     connect(m_view.display_den, SIGNAL(valueChanged(int)), this, SLOT(slotProfileEdited()));
69     connect(m_view.progressive, SIGNAL(stateChanged(int)), this, SLOT(slotProfileEdited()));
70     connect(m_view.size_h, SIGNAL(valueChanged(int)), this, SLOT(slotProfileEdited()));
71     connect(m_view.size_w, SIGNAL(valueChanged(int)), this, SLOT(slotProfileEdited()));
72 }
73
74
75 ProfilesDialog::ProfilesDialog(QString profilePath, QWidget * parent) :
76     QDialog(parent),
77     m_profileIsModified(false),
78     m_isCustomProfile(true),
79     m_customProfilePath(profilePath)
80 {
81     m_view.setupUi(this);
82
83     // Fill colorspace list (see mlt_profile.h)
84     m_view.colorspace->addItem(getColorspaceDescription(601), 601);
85     m_view.colorspace->addItem(getColorspaceDescription(709), 709);
86     m_view.colorspace->addItem(getColorspaceDescription(240), 240);
87     m_view.colorspace->addItem(getColorspaceDescription(0), 0);
88
89     QStringList profilesFilter;
90     profilesFilter << "*";
91
92     m_view.button_save->setIcon(KIcon("document-save"));
93     m_view.button_save->setToolTip(i18n("Save profile"));
94     m_view.button_create->setHidden(true);
95     m_view.profiles_list->setHidden(true);
96     m_view.button_delete->setHidden(true);
97     m_view.button_default->setHidden(true);
98     m_view.description->setEnabled(false);
99
100     slotUpdateDisplay(profilePath);
101     connect(m_view.button_save, SIGNAL(clicked()), this, SLOT(slotSaveProfile()));
102
103     connect(m_view.description, SIGNAL(textChanged(const QString &)), this, SLOT(slotProfileEdited()));
104     connect(m_view.frame_num, SIGNAL(valueChanged(int)), this, SLOT(slotProfileEdited()));
105     connect(m_view.frame_den, SIGNAL(valueChanged(int)), this, SLOT(slotProfileEdited()));
106     connect(m_view.aspect_num, SIGNAL(valueChanged(int)), this, SLOT(slotProfileEdited()));
107     connect(m_view.aspect_den, SIGNAL(valueChanged(int)), this, SLOT(slotProfileEdited()));
108     connect(m_view.display_num, SIGNAL(valueChanged(int)), this, SLOT(slotProfileEdited()));
109     connect(m_view.display_den, SIGNAL(valueChanged(int)), this, SLOT(slotProfileEdited()));
110     connect(m_view.progressive, SIGNAL(stateChanged(int)), this, SLOT(slotProfileEdited()));
111     connect(m_view.size_h, SIGNAL(valueChanged(int)), this, SLOT(slotProfileEdited()));
112     connect(m_view.size_w, SIGNAL(valueChanged(int)), this, SLOT(slotProfileEdited()));
113 }
114
115 void ProfilesDialog::slotProfileEdited()
116 {
117     m_profileIsModified = true;
118 }
119
120 void ProfilesDialog::fillList(const QString selectedProfile)
121 {
122     // List the Mlt profiles
123     m_view.profiles_list->clear();
124     QMap <QString, QString> profilesInfo = ProfilesDialog::getProfilesInfo();
125     QMapIterator<QString, QString> i(profilesInfo);
126     while (i.hasNext()) {
127         i.next();
128         m_view.profiles_list->addItem(i.key(), i.value());
129     }
130
131     if (!KdenliveSettings::default_profile().isEmpty()) {
132         for (int i = 0; i < m_view.profiles_list->count(); i++) {
133             if (m_view.profiles_list->itemData(i).toString() == KdenliveSettings::default_profile()) {
134                 m_view.profiles_list->setCurrentIndex(i);
135                 break;
136             }
137         }
138     }
139     int ix = m_view.profiles_list->findText(selectedProfile);
140     if (ix != -1) m_view.profiles_list->setCurrentIndex(ix);
141     m_selectedProfileIndex = m_view.profiles_list->currentIndex();
142 }
143
144 void ProfilesDialog::accept()
145 {
146     if (askForSave()) QDialog::accept();
147 }
148
149 void ProfilesDialog::closeEvent(QCloseEvent *event)
150 {
151     if (askForSave()) {
152         event->accept();
153     } else {
154         event->ignore();
155     }
156 }
157
158 bool ProfilesDialog::askForSave()
159 {
160     if (!m_profileIsModified) return true;
161     if (KMessageBox::questionYesNo(this, i18n("The custom profile was modified, do you want to save it?")) != KMessageBox::Yes) return true;
162     return slotSaveProfile();
163 }
164
165 void ProfilesDialog::slotCreateProfile()
166 {
167     m_view.button_delete->setEnabled(false);
168     m_view.button_create->setEnabled(false);
169     m_view.button_save->setEnabled(true);
170     m_view.properties->setEnabled(true);
171 }
172
173 void ProfilesDialog::slotSetDefaultProfile()
174 {
175     int ix = m_view.profiles_list->currentIndex();
176     QString path = m_view.profiles_list->itemData(ix).toString();
177     if (!path.isEmpty()) KdenliveSettings::setDefault_profile(path);
178 }
179
180 bool ProfilesDialog::slotSaveProfile()
181 {
182     if (!m_customProfilePath.isEmpty()) {
183         saveProfile(m_customProfilePath);
184         return true;
185     }
186     const QString profileDesc = m_view.description->text();
187     int ix = m_view.profiles_list->findText(profileDesc);
188     if (ix != -1) {
189         // this profile name already exists
190         const QString path = m_view.profiles_list->itemData(ix).toString();
191         if (!path.contains('/')) {
192             KMessageBox::sorry(this, i18n("A profile with same name already exists in MLT's default profiles, please choose another description for your custom profile."));
193             return false;
194         }
195         saveProfile(path);
196     } else {
197         int i = 0;
198         QString customName = "profiles/customprofile";
199         QString profilePath = KStandardDirs::locateLocal("appdata", customName + QString::number(i));
200         kDebug() << " TYING PROFILE FILE: " << profilePath;
201         while (KIO::NetAccess::exists(KUrl(profilePath), KIO::NetAccess::SourceSide, this)) {
202             i++;
203             profilePath = KStandardDirs::locateLocal("appdata", customName + QString::number(i));
204         }
205         saveProfile(profilePath);
206     }
207     m_profileIsModified = false;
208     fillList(profileDesc);
209     m_view.button_create->setEnabled(true);
210     return true;
211 }
212
213 void ProfilesDialog::saveProfile(const QString path)
214 {
215     QFile file(path);
216     if (!file.open(QIODevice::WriteOnly)) {
217         KMessageBox::sorry(this, i18n("Cannot write to file %1", path));
218         return;
219     }
220     QTextStream out(&file);
221     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" << "colorspace=" << m_view.colorspace->itemData(m_view.colorspace->currentIndex()).toInt() << "\n";
222     if (file.error() != QFile::NoError) {
223         KMessageBox::error(this, i18n("Cannot write to file %1", path));
224     }
225     file.close();
226 }
227
228 void ProfilesDialog::slotDeleteProfile()
229 {
230     const QString path = m_view.profiles_list->itemData(m_view.profiles_list->currentIndex()).toString();
231     if (path.contains('/')) {
232         KIO::NetAccess::del(KUrl(path), this);
233         fillList();
234     } else kDebug() << "//// Cannot delete profile " << path << ", does not seem to be custom one";
235 }
236
237 // static
238 MltVideoProfile ProfilesDialog::getVideoProfile(QString name)
239 {
240     MltVideoProfile result;
241     QStringList profilesNames;
242     QStringList profilesFiles;
243     QStringList profilesFilter;
244     profilesFilter << "*";
245     QString path;
246     bool isCustom = false;
247     if (name.contains('/')) isCustom = true;
248
249     if (!isCustom) {
250         // List the Mlt profiles
251         profilesFiles = QDir(KdenliveSettings::mltpath()).entryList(profilesFilter, QDir::Files);
252         if (profilesFiles.contains(name)) path = KdenliveSettings::mltpath() + name;
253     }
254     if (isCustom || path.isEmpty()) {
255         path = name;
256     }
257
258     if (path.isEmpty() || !QFile::exists(path)) {
259         if (name == "dv_pal") {
260             kDebug() << "!!! WARNING, COULD NOT FIND DEFAULT MLT PROFILE";
261             return result;
262         }
263         if (name == KdenliveSettings::default_profile()) KdenliveSettings::setDefault_profile("dv_pal");
264         kDebug() << "// WARNING, COULD NOT FIND PROFILE " << name;
265         return result;
266     }
267     KConfig confFile(path, KConfig::SimpleConfig);
268     result.path = name;
269     result.description = confFile.entryMap().value("description");
270     result.frame_rate_num = confFile.entryMap().value("frame_rate_num").toInt();
271     result.frame_rate_den = confFile.entryMap().value("frame_rate_den").toInt();
272     result.width = confFile.entryMap().value("width").toInt();
273     result.height = confFile.entryMap().value("height").toInt();
274     result.progressive = confFile.entryMap().value("progressive").toInt();
275     result.sample_aspect_num = confFile.entryMap().value("sample_aspect_num").toInt();
276     result.sample_aspect_den = confFile.entryMap().value("sample_aspect_den").toInt();
277     result.display_aspect_num = confFile.entryMap().value("display_aspect_num").toInt();
278     result.display_aspect_den = confFile.entryMap().value("display_aspect_den").toInt();
279     result.colorspace = confFile.entryMap().value("colorspace").toInt();
280     return result;
281 }
282
283 // static
284 double ProfilesDialog::getStringEval(const MltVideoProfile &profile, QString eval)
285 {
286     double result;
287     eval.replace("%width", QString::number(profile.width));
288     eval.replace("%height", QString::number(profile.height));
289     if (eval.contains('/')) result = (double) eval.section('/', 0, 0).toInt() / eval.section('/', 1, 1).toInt();
290     else if (eval.contains('*')) result = (double) eval.section('*', 0, 0).toInt() * eval.section('*', 1, 1).toInt();
291     else if (eval.contains('+')) result = (double) eval.section('+', 0, 0).toInt() + eval.section('+', 1, 1).toInt();
292     else if (eval.contains('-')) result = (double) eval.section('-', 0, 0).toInt() - eval.section('-', 1, 1).toInt();
293     else result = eval.toDouble();
294     return result;
295 }
296
297
298 // static
299 bool ProfilesDialog::existingProfileDescription(const QString &desc)
300 {
301     QStringList profilesFilter;
302     profilesFilter << "*";
303
304     // List the Mlt profiles
305     QStringList profilesFiles = QDir(KdenliveSettings::mltpath()).entryList(profilesFilter, QDir::Files);
306     for (int i = 0; i < profilesFiles.size(); ++i) {
307         KConfig confFile(KdenliveSettings::mltpath() + profilesFiles.at(i), KConfig::SimpleConfig);
308         if (desc == confFile.entryMap().value("description")) return true;
309     }
310
311     // List custom profiles
312     QStringList customProfiles = KGlobal::dirs()->findDirs("appdata", "profiles");
313     for (int i = 0; i < customProfiles.size(); ++i) {
314         profilesFiles = QDir(customProfiles.at(i)).entryList(profilesFilter, QDir::Files);
315         for (int j = 0; j < profilesFiles.size(); ++j) {
316             KConfig confFile(customProfiles.at(i) + profilesFiles.at(j), KConfig::SimpleConfig);
317             if (desc == confFile.entryMap().value("description")) return true;
318         }
319     }
320     return false;
321 }
322
323 // static
324 QString ProfilesDialog::existingProfile(MltVideoProfile profile)
325 {
326     // Check if the profile has a matching entry in existing ones
327     QStringList profilesFilter;
328     profilesFilter << "*";
329
330     // Check the Mlt profiles
331     QStringList profilesFiles = QDir(KdenliveSettings::mltpath()).entryList(profilesFilter, QDir::Files);
332     for (int i = 0; i < profilesFiles.size(); ++i) {
333         KConfig confFile(KdenliveSettings::mltpath() + profilesFiles.at(i), KConfig::SimpleConfig);
334         if (profile.display_aspect_den != confFile.entryMap().value("display_aspect_den").toInt()) continue;
335         if (profile.display_aspect_num != confFile.entryMap().value("display_aspect_num").toInt()) continue;
336         if (profile.sample_aspect_den != confFile.entryMap().value("sample_aspect_den").toInt()) continue;
337         if (profile.sample_aspect_num != confFile.entryMap().value("sample_aspect_num").toInt()) continue;
338         if (profile.width != confFile.entryMap().value("width").toInt()) continue;
339         if (profile.height != confFile.entryMap().value("height").toInt()) continue;
340         if (profile.frame_rate_den != confFile.entryMap().value("frame_rate_den").toInt()) continue;
341         if (profile.frame_rate_num != confFile.entryMap().value("frame_rate_num").toInt()) continue;
342         if (profile.progressive != confFile.entryMap().value("progressive").toInt()) continue;
343         if (profile.colorspace != confFile.entryMap().value("colorspace").toInt()) continue;
344         return profilesFiles.at(i);
345     }
346
347     // Check custom profiles
348     QStringList customProfiles = KGlobal::dirs()->findDirs("appdata", "profiles");
349     for (int i = 0; i < customProfiles.size(); ++i) {
350         profilesFiles = QDir(customProfiles.at(i)).entryList(profilesFilter, QDir::Files);
351         for (int j = 0; j < profilesFiles.size(); ++j) {
352             KConfig confFile(customProfiles.at(i) + profilesFiles.at(j), KConfig::SimpleConfig);
353             if (profile.display_aspect_den != confFile.entryMap().value("display_aspect_den").toInt()) continue;
354             if (profile.display_aspect_num != confFile.entryMap().value("display_aspect_num").toInt()) continue;
355             if (profile.sample_aspect_den != confFile.entryMap().value("sample_aspect_den").toInt()) continue;
356             if (profile.sample_aspect_num != confFile.entryMap().value("sample_aspect_num").toInt()) continue;
357             if (profile.width != confFile.entryMap().value("width").toInt()) continue;
358             if (profile.height != confFile.entryMap().value("height").toInt()) continue;
359             if (profile.frame_rate_den != confFile.entryMap().value("frame_rate_den").toInt()) continue;
360             if (profile.frame_rate_num != confFile.entryMap().value("frame_rate_num").toInt()) continue;
361             if (profile.progressive != confFile.entryMap().value("progressive").toInt()) continue;
362             if (profile.colorspace != confFile.entryMap().value("colorspace").toInt()) continue;
363             return customProfiles.at(i) + profilesFiles.at(j);
364         }
365     }
366     return QString();
367 }
368
369 // static
370 QMap <QString, QString> ProfilesDialog::getProfilesInfo()
371 {
372     QMap <QString, QString> result;
373     QStringList profilesFilter;
374     profilesFilter << "*";
375
376     // List the Mlt profiles
377     QStringList profilesFiles = QDir(KdenliveSettings::mltpath()).entryList(profilesFilter, QDir::Files);
378     for (int i = 0; i < profilesFiles.size(); ++i) {
379         KConfig confFile(KdenliveSettings::mltpath() + profilesFiles.at(i), KConfig::SimpleConfig);
380         QString desc = confFile.entryMap().value("description");
381         if (!desc.isEmpty()) result.insert(desc, profilesFiles.at(i));
382     }
383
384     // List custom profiles
385     QStringList customProfiles = KGlobal::dirs()->findDirs("appdata", "profiles");
386     for (int i = 0; i < customProfiles.size(); ++i) {
387         profilesFiles = QDir(customProfiles.at(i)).entryList(profilesFilter, QDir::Files);
388         for (int j = 0; j < profilesFiles.size(); ++j) {
389             KConfig confFile(customProfiles.at(i) + profilesFiles.at(j), KConfig::SimpleConfig);
390             QString desc = confFile.entryMap().value("description");
391             if (!desc.isEmpty()) result.insert(desc, customProfiles.at(i) + profilesFiles.at(j));
392         }
393     }
394     return result;
395 }
396
397 // static
398 QMap< QString, QString > ProfilesDialog::getSettingsFromFile(const QString path)
399 {
400     QStringList profilesNames;
401     QStringList profilesFiles;
402     QStringList profilesFilter;
403     profilesFilter << "*";
404
405     if (!path.contains('/')) {
406         // This is an MLT profile
407         KConfig confFile(KdenliveSettings::mltpath() + path, KConfig::SimpleConfig);
408         return confFile.entryMap();
409     } else {
410         // This is a custom profile
411         KConfig confFile(path, KConfig::SimpleConfig);
412         return confFile.entryMap();
413     }
414 }
415
416 // static
417 QMap< QString, QString > ProfilesDialog::getSettingsForProfile(const QString profileName)
418 {
419     QStringList profilesNames;
420     QStringList profilesFiles;
421     QStringList profilesFilter;
422     profilesFilter << "*";
423
424     // List the Mlt profiles
425     profilesFiles = QDir(KdenliveSettings::mltpath()).entryList(profilesFilter, QDir::Files);
426     for (int i = 0; i < profilesFiles.size(); ++i) {
427         KConfig confFile(KdenliveSettings::mltpath() + profilesFiles.at(i), KConfig::SimpleConfig);
428         QMap< QString, QString > values = confFile.entryMap();
429         if (values.value("description") == profileName) {
430             values.insert("path", profilesFiles.at(i));
431             return values;
432         }
433     }
434
435     // List custom profiles
436     QStringList customProfiles = KGlobal::dirs()->findDirs("appdata", "profiles");
437     for (int i = 0; i < customProfiles.size(); ++i) {
438         QStringList profiles = QDir(customProfiles.at(i)).entryList(profilesFilter, QDir::Files);
439         for (int j = 0; j < profiles.size(); ++j) {
440             KConfig confFile(customProfiles.at(i) + profiles.at(j), KConfig::SimpleConfig);
441             QMap< QString, QString > values = confFile.entryMap();
442             if (values.value("description") == profileName) {
443                 values.insert("path", customProfiles.at(i) + profiles.at(j));
444                 return values;
445             }
446         }
447     }
448     return QMap< QString, QString >();
449 }
450
451 // static
452 bool ProfilesDialog::matchProfile(int width, int height, double fps, double par, bool isImage, MltVideoProfile profile)
453 {
454     int profileWidth;
455     if (isImage) {
456         // when using image, compare with display width
457         profileWidth = profile.height * profile.display_aspect_num / profile.display_aspect_den + 0.5;
458     } else profileWidth = profile.width;
459     if (width != profileWidth || height != profile.height || (fps > 0 && qAbs((double) profile.frame_rate_num / profile.frame_rate_den - fps) > 0.4) || (!isImage && par > 0 && qAbs((double) profile.sample_aspect_num / profile.sample_aspect_den - par) > 0.1)) return false;
460     return true;
461 }
462
463 // static
464 QMap <QString, QString> ProfilesDialog::getProfilesFromProperties(int width, int height, double fps, double par, bool useDisplayWidth)
465 {
466     QStringList profilesNames;
467     QStringList profilesFiles;
468     QStringList profilesFilter;
469     QMap <QString, QString> result;
470     profilesFilter << "*";
471     // List the Mlt profiles
472     profilesFiles = QDir(KdenliveSettings::mltpath()).entryList(profilesFilter, QDir::Files);
473     for (int i = 0; i < profilesFiles.size(); ++i) {
474         KConfig confFile(KdenliveSettings::mltpath() + profilesFiles.at(i), KConfig::SimpleConfig);
475         QMap< QString, QString > values = confFile.entryMap();
476         int profileWidth;
477         if (useDisplayWidth) profileWidth = values.value("height").toInt() * values.value("display_aspect_num").toInt() / values.value("display_aspect_den").toInt() + 0.5;
478         else profileWidth = values.value("width").toInt();
479         if (profileWidth == width && values.value("height").toInt() == height) {
480             double profile_fps = values.value("frame_rate_num").toDouble() / values.value("frame_rate_den").toDouble();
481             double profile_par = values.value("sample_aspect_num").toDouble() / values.value("sample_aspect_den").toDouble();
482             if ((fps <= 0 || qAbs(profile_fps - fps) < 0.5) && (par <= 0 || qAbs(profile_par - par) < 0.1))
483                 result.insert(profilesFiles.at(i), values.value("description"));
484         }
485     }
486
487     // List custom profiles
488     QStringList customProfiles = KGlobal::dirs()->findDirs("appdata", "profiles");
489     for (int i = 0; i < customProfiles.size(); ++i) {
490         QStringList profiles = QDir(customProfiles.at(i)).entryList(profilesFilter, QDir::Files);
491         for (int j = 0; j < profiles.size(); j++) {
492             KConfig confFile(customProfiles.at(i) + profiles.at(j), KConfig::SimpleConfig);
493             QMap< QString, QString > values = confFile.entryMap();
494             int profileWidth;
495             if (useDisplayWidth) profileWidth = values.value("height").toInt() * values.value("display_aspect_num").toInt() / values.value("display_aspect_den").toInt() + 0.5;
496             else profileWidth = values.value("width").toInt();
497             if (profileWidth == width && values.value("height").toInt() == height) {
498                 double profile_fps = values.value("frame_rate_num").toDouble() / values.value("frame_rate_den").toDouble();
499                 double profile_par = values.value("sample_aspect_num").toDouble() / values.value("sample_aspect_den").toDouble();
500                 if ((fps <= 0 || qAbs(profile_fps - fps) < 0.5) && (par <= 0 || qAbs(profile_par - par) < 0.1))
501                     result.insert(customProfiles.at(i) + profiles.at(j), values.value("description"));
502             }
503         }
504     }
505     return result;
506 }
507
508 // static
509 QString ProfilesDialog::getPathFromDescription(const QString profileDesc)
510 {
511     QStringList profilesNames;
512     QStringList profilesFiles;
513     QStringList profilesFilter;
514     profilesFilter << "*";
515
516     // List the Mlt profiles
517     profilesFiles = QDir(KdenliveSettings::mltpath()).entryList(profilesFilter, QDir::Files);
518     for (int i = 0; i < profilesFiles.size(); ++i) {
519         KConfig confFile(KdenliveSettings::mltpath() + profilesFiles.at(i), KConfig::SimpleConfig);
520         QMap< QString, QString > values = confFile.entryMap();
521         if (values.value("description") == profileDesc) return profilesFiles.at(i);
522     }
523
524     // List custom profiles
525     QStringList customProfiles = KGlobal::dirs()->findDirs("appdata", "profiles");
526     for (int i = 0; i < customProfiles.size(); ++i) {
527         QStringList profiles = QDir(customProfiles.at(i)).entryList(profilesFilter, QDir::Files);
528         for (int j = 0; j < profiles.size(); ++j) {
529             KConfig confFile(customProfiles.at(i) + profiles.at(j), KConfig::SimpleConfig);
530             QMap< QString, QString > values = confFile.entryMap();
531             if (values.value("description") == profileDesc) return customProfiles.at(i) + profiles.at(j);
532         }
533     }
534     return QString();
535 }
536
537 // static
538 void ProfilesDialog::saveProfile(MltVideoProfile &profile, QString profilePath)
539 {
540     if (profilePath.isEmpty()) {
541         int i = 0;
542         QString customName = "profiles/customprofile";
543         profilePath = KStandardDirs::locateLocal("appdata", customName + QString::number(i));
544         kDebug() << " TYING PROFILE FILE: " << profilePath;
545         while (KIO::NetAccess::exists(KUrl(profilePath), KIO::NetAccess::SourceSide, 0)) {
546             i++;
547             profilePath = KStandardDirs::locateLocal("appdata", customName + QString::number(i));
548         }
549     }
550     QFile file(profilePath);
551     if (!file.open(QIODevice::WriteOnly)) {
552         KMessageBox::sorry(0, i18n("Cannot write to file %1", profilePath));
553         return;
554     }
555     QTextStream out(&file);
556     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" << "colorspace=" << profile.colorspace << "\n";
557     if (file.error() != QFile::NoError) {
558         KMessageBox::error(0, i18n("Cannot write to file %1", profilePath));
559     }
560     file.close();
561     profile.path = profilePath;
562 }
563
564
565 void ProfilesDialog::slotUpdateDisplay(QString currentProfile)
566 {
567     if (askForSave() == false) {
568         m_view.profiles_list->blockSignals(true);
569         m_view.profiles_list->setCurrentIndex(m_selectedProfileIndex);
570         m_view.profiles_list->blockSignals(false);
571         return;
572     }
573
574     m_selectedProfileIndex = m_view.profiles_list->currentIndex();
575     if (currentProfile.isEmpty()) currentProfile = m_view.profiles_list->itemData(m_view.profiles_list->currentIndex()).toString();
576     m_isCustomProfile = currentProfile.contains('/');
577     m_view.button_create->setEnabled(true);
578     m_view.button_delete->setEnabled(m_isCustomProfile);
579     m_view.properties->setEnabled(m_isCustomProfile);
580     m_view.button_save->setEnabled(m_isCustomProfile);
581     QMap< QString, QString > values = ProfilesDialog::getSettingsFromFile(currentProfile);
582     m_view.description->setText(values.value("description"));
583     m_view.size_w->setValue(values.value("width").toInt());
584     m_view.size_h->setValue(values.value("height").toInt());
585     m_view.aspect_num->setValue(values.value("sample_aspect_num").toInt());
586     m_view.aspect_den->setValue(values.value("sample_aspect_den").toInt());
587     m_view.display_num->setValue(values.value("display_aspect_num").toInt());
588     m_view.display_den->setValue(values.value("display_aspect_den").toInt());
589     m_view.frame_num->setValue(values.value("frame_rate_num").toInt());
590     m_view.frame_den->setValue(values.value("frame_rate_den").toInt());
591     m_view.progressive->setChecked(values.value("progressive").toInt());
592     if (values.value("progressive").toInt()) {
593         m_view.fields->setText(QString::number((double) values.value("frame_rate_num").toInt() / values.value("frame_rate_den").toInt(), 'f', 2));
594     } else {
595         m_view.fields->setText(QString::number((double) 2 * values.value("frame_rate_num").toInt() / values.value("frame_rate_den").toInt(), 'f', 2));
596     }
597
598     int colorix = m_view.colorspace->findData(values.value("colorspace").toInt());
599     if (colorix > -1) m_view.colorspace->setCurrentIndex(colorix);
600     m_profileIsModified = false;
601 }
602
603 //static
604 QString ProfilesDialog::getColorspaceDescription(int colorspace)
605 {
606     //TODO: should the descriptions be translated?
607     switch (colorspace) {
608     case 601:
609         return QString("ITU-R 601");
610     case 709:
611         return QString("ITU-R 709");
612     case 240:
613         return QString("SMPTE240M");
614     default:
615         return i18n("Unknown");
616     }
617 }
618
619 //static
620 int ProfilesDialog::getColorspaceFromDescription(const QString &description)
621 {
622     //TODO: should the descriptions be translated?
623     if (description == "SMPTE240M") return 240;
624     if (description == "ITU-R 709") return 709;
625     return 601;
626 }
627
628 #include "profilesdialog.moc"
629
630