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