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