]> git.sesse.net Git - kdenlive/blob - src/profilesdialog.cpp
Ask to save profile before closing profiles dialog, default to pal dv when 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 <QDir>
21 #include <QCloseEvent>
22
23 #include <KStandardDirs>
24 #include <KDebug>
25 #include <KMessageBox>
26 #include <KIO/NetAccess>
27
28 #include "kdenlivesettings.h"
29 #include "profilesdialog.h"
30
31 ProfilesDialog::ProfilesDialog(QWidget * parent): QDialog(parent), m_isCustomProfile(false), m_profileIsModified(false) {
32     m_view.setupUi(this);
33
34     QStringList profilesFilter;
35     profilesFilter << "*";
36
37     m_view.button_delete->setIcon(KIcon("trash-empty"));
38     m_view.button_delete->setToolTip(i18n("Delete profile"));
39     m_view.button_save->setIcon(KIcon("document-save"));
40     m_view.button_save->setToolTip(i18n("Save profile"));
41     m_view.button_create->setIcon(KIcon("document-new"));
42     m_view.button_create->setToolTip(i18n("Create new profile"));
43
44     fillList();
45     slotUpdateDisplay();
46     connect(m_view.profiles_list, SIGNAL(currentIndexChanged(int)), this, SLOT(slotUpdateDisplay()));
47     connect(m_view.button_create, SIGNAL(clicked()), this, SLOT(slotCreateProfile()));
48     connect(m_view.button_save, SIGNAL(clicked()), this, SLOT(slotSaveProfile()));
49     connect(m_view.button_delete, SIGNAL(clicked()), this, SLOT(slotDeleteProfile()));
50     connect(m_view.button_default, SIGNAL(clicked()), this, SLOT(slotSetDefaultProfile()));
51
52     connect(m_view.description, SIGNAL(textChanged(const QString &)), this, SLOT(slotProfileEdited()));
53     connect(m_view.frame_num, SIGNAL(valueChanged(int)), this, SLOT(slotProfileEdited()));
54     connect(m_view.frame_den, SIGNAL(valueChanged(int)), this, SLOT(slotProfileEdited()));
55     connect(m_view.aspect_num, SIGNAL(valueChanged(int)), this, SLOT(slotProfileEdited()));
56     connect(m_view.aspect_den, SIGNAL(valueChanged(int)), this, SLOT(slotProfileEdited()));
57     connect(m_view.display_num, SIGNAL(valueChanged(int)), this, SLOT(slotProfileEdited()));
58     connect(m_view.display_den, SIGNAL(valueChanged(int)), this, SLOT(slotProfileEdited()));
59     connect(m_view.progressive, SIGNAL(stateChanged(int)), this, SLOT(slotProfileEdited()));
60     connect(m_view.size_h, SIGNAL(valueChanged(int)), this, SLOT(slotProfileEdited()));
61     connect(m_view.size_w, SIGNAL(valueChanged(int)), this, SLOT(slotProfileEdited()));
62 }
63
64 void ProfilesDialog::slotProfileEdited() {
65     m_profileIsModified = true;
66 }
67
68 void ProfilesDialog::fillList(const QString selectedProfile) {
69     // List the Mlt profiles
70     m_view.profiles_list->clear();
71     QMap <QString, QString> profilesInfo = ProfilesDialog::getProfilesInfo();
72     QMapIterator<QString, QString> i(profilesInfo);
73     while (i.hasNext()) {
74         i.next();
75         m_view.profiles_list->addItem(i.key(), i.value());
76     }
77
78     if (!KdenliveSettings::default_profile().isEmpty()) {
79         for (int i = 0; i < m_view.profiles_list->count(); i++) {
80             if (m_view.profiles_list->itemData(i).toString() == KdenliveSettings::default_profile()) {
81                 m_view.profiles_list->setCurrentIndex(i);
82                 break;
83             }
84         }
85     }
86     int ix = m_view.profiles_list->findText(selectedProfile);
87     if (ix != -1) m_view.profiles_list->setCurrentIndex(ix);
88     m_selectedProfileIndex = m_view.profiles_list->currentIndex();
89 }
90
91 void ProfilesDialog::accept() {
92     if (askForSave()) QDialog::accept();
93 }
94
95 void ProfilesDialog::closeEvent(QCloseEvent *event) {
96     if (askForSave()) {
97         event->accept();
98     } else {
99         event->ignore();
100     }
101 }
102
103 bool ProfilesDialog::askForSave() {
104     if (!m_profileIsModified) return true;
105     if (KMessageBox::questionYesNo(this, i18n("The custom profile was modified, do you want to save it ?")) != KMessageBox::Yes) return true;
106     return slotSaveProfile();
107 }
108
109 void ProfilesDialog::slotCreateProfile() {
110     m_view.button_delete->setEnabled(false);
111     m_view.button_create->setEnabled(false);
112     m_view.button_save->setEnabled(true);
113     m_view.properties->setEnabled(true);
114 }
115
116 void ProfilesDialog::slotSetDefaultProfile() {
117     int ix = m_view.profiles_list->currentIndex();
118     QString path = m_view.profiles_list->itemData(ix).toString();
119     if (!path.isEmpty()) KdenliveSettings::setDefault_profile(path);
120 }
121
122 bool ProfilesDialog::slotSaveProfile() {
123     const QString profileDesc = m_view.description->text();
124     int ix = m_view.profiles_list->findText(profileDesc);
125     if (ix != -1) {
126         // this profile name already exists
127         const QString path = m_view.profiles_list->itemData(ix).toString();
128         if (!path.contains("/")) {
129             KMessageBox::sorry(this, i18n("A profile with same name already exists in MLT's default profiles, please choose another description for your custom profile."));
130             return false;
131         }
132         saveProfile(path);
133     } else {
134         int i = 0;
135         QString customName = "profiles/customprofile";
136         QString profilePath = KStandardDirs::locateLocal("appdata", customName + QString::number(i));
137         kDebug() << " TYING PROFILE FILE: " << profilePath;
138         while (KIO::NetAccess::exists(KUrl(profilePath), KIO::NetAccess::SourceSide, this)) {
139             i++;
140             profilePath = KStandardDirs::locateLocal("appdata", customName + QString::number(i));
141         }
142         saveProfile(profilePath);
143     }
144     m_profileIsModified = false;
145     fillList(profileDesc);
146     m_view.button_create->setEnabled(true);
147     return true;
148 }
149
150 void ProfilesDialog::saveProfile(const QString path) {
151     QFile file(path);
152     if (!file.open(QIODevice::WriteOnly)) {
153         KMessageBox::sorry(this, i18n("Cannot write to file %1", path));
154         return;
155     }
156     QTextStream out(&file);
157     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";
158     file.close();
159 }
160
161 void ProfilesDialog::slotDeleteProfile() {
162     const QString path = m_view.profiles_list->itemData(m_view.profiles_list->currentIndex()).toString();
163     if (path.contains("/")) {
164         KIO::NetAccess::del(KUrl(path), this);
165         fillList();
166     } else kDebug() << "//// Cannot delete profile " << path << ", does not seem to be custom one";
167 }
168
169 // static
170 MltVideoProfile ProfilesDialog::getVideoProfile(QString name) {
171     MltVideoProfile result;
172     QStringList profilesNames;
173     QStringList profilesFiles;
174     QStringList profilesFilter;
175     profilesFilter << "*";
176     QString path;
177     bool isCustom = false;
178     if (name.contains('/')) isCustom = true;
179
180     if (!isCustom) {
181         // List the Mlt profiles
182         profilesFiles = QDir(KdenliveSettings::mltpath()).entryList(profilesFilter, QDir::Files);
183         if (profilesFiles.contains(name)) path = KdenliveSettings::mltpath() + "/" + name;
184     }
185     if (isCustom  || path.isEmpty()) {
186         path = name;
187     }
188
189     if (path.isEmpty() || !QFile::exists(path)) {
190         if (name == "dv_pal") {
191             kDebug() << "!!! WARNING, COULD NOT FIND DEFAULT MLT PROFILE";
192             return result;
193         }
194         if (name == KdenliveSettings::default_profile()) KdenliveSettings::setDefault_profile("dv_pal");
195         kDebug() << "// WARNING, COULD NOT FIND PROFILE " << name;
196         return getVideoProfile("dv_pal");
197     }
198     KConfig confFile(path, KConfig::SimpleConfig);
199     result.path = name;
200     result.description = confFile.entryMap().value("description");
201     result.frame_rate_num = confFile.entryMap().value("frame_rate_num").toInt();
202     result.frame_rate_den = confFile.entryMap().value("frame_rate_den").toInt();
203     result.width = confFile.entryMap().value("width").toInt();
204     result.height = confFile.entryMap().value("height").toInt();
205     result.progressive = confFile.entryMap().value("progressive").toInt();
206     result.sample_aspect_num = confFile.entryMap().value("sample_aspect_num").toInt();
207     result.sample_aspect_den = confFile.entryMap().value("sample_aspect_den").toInt();
208     result.display_aspect_num = confFile.entryMap().value("display_aspect_num").toInt();
209     result.display_aspect_den = confFile.entryMap().value("display_aspect_den").toInt();
210     return result;
211 }
212
213 // static
214 QString ProfilesDialog::getProfileDescription(QString name) {
215     QStringList profilesNames;
216     QStringList profilesFiles;
217     QStringList profilesFilter;
218     profilesFilter << "*";
219
220     // List the Mlt profiles
221     profilesFiles = QDir(KdenliveSettings::mltpath()).entryList(profilesFilter, QDir::Files);
222     if (profilesFiles.contains(name)) {
223         KConfig confFile(KdenliveSettings::mltpath() + "/" + name, KConfig::SimpleConfig);
224         return confFile.entryMap().value("description");
225     }
226
227     // List custom profiles
228     QStringList customProfiles = KGlobal::dirs()->findDirs("appdata", "profiles");
229     for (int i = 0; i < customProfiles.size(); ++i) {
230         profilesFiles = QDir(customProfiles.at(i)).entryList(profilesFilter, QDir::Files);
231         if (profilesFiles.contains(name)) {
232             KConfig confFile(customProfiles.at(i) + "/" + name, KConfig::SimpleConfig);
233             return confFile.entryMap().value("description");
234         }
235     }
236
237     return QString();
238 }
239
240 // static
241 QMap <QString, QString> ProfilesDialog::getProfilesInfo() {
242     QMap <QString, QString> result;
243     QStringList profilesFilter;
244     profilesFilter << "*";
245
246     // List the Mlt profiles
247     QStringList profilesFiles = QDir(KdenliveSettings::mltpath()).entryList(profilesFilter, QDir::Files);
248     for (int i = 0; i < profilesFiles.size(); ++i) {
249         KConfig confFile(KdenliveSettings::mltpath() + "/" + profilesFiles.at(i), KConfig::SimpleConfig);
250         QString desc = confFile.entryMap().value("description");
251         if (!desc.isEmpty()) result.insert(desc, profilesFiles.at(i));
252     }
253
254     // List custom profiles
255     QStringList customProfiles = KGlobal::dirs()->findDirs("appdata", "profiles");
256     for (int i = 0; i < customProfiles.size(); ++i) {
257         profilesFiles = QDir(customProfiles.at(i)).entryList(profilesFilter, QDir::Files);
258         for (int j = 0; j < profilesFiles.size(); ++j) {
259             KConfig confFile(customProfiles.at(i) + "/" + profilesFiles.at(j), KConfig::SimpleConfig);
260             QString desc = confFile.entryMap().value("description");
261             if (!desc.isEmpty()) result.insert(desc, customProfiles.at(i) + "/" + profilesFiles.at(j));
262         }
263     }
264     return result;
265 }
266
267 // static
268 QMap< QString, QString > ProfilesDialog::getSettingsFromFile(const QString path) {
269     QStringList profilesNames;
270     QStringList profilesFiles;
271     QStringList profilesFilter;
272     profilesFilter << "*";
273
274     if (!path.contains("/")) {
275         // This is an MLT profile
276         KConfig confFile(KdenliveSettings::mltpath() + "/" + path, KConfig::SimpleConfig);
277         return confFile.entryMap();
278     } else {
279         // This is a custom profile
280         KConfig confFile(path, KConfig::SimpleConfig);
281         return confFile.entryMap();
282     }
283 }
284
285 // static
286 QMap< QString, QString > ProfilesDialog::getSettingsForProfile(const QString profileName) {
287     QStringList profilesNames;
288     QStringList profilesFiles;
289     QStringList profilesFilter;
290     profilesFilter << "*";
291
292     // List the Mlt profiles
293     profilesFiles = QDir(KdenliveSettings::mltpath()).entryList(profilesFilter, QDir::Files);
294     for (int i = 0; i < profilesFiles.size(); ++i) {
295         KConfig confFile(KdenliveSettings::mltpath() + "/" + profilesFiles.at(i), KConfig::SimpleConfig);
296         QMap< QString, QString > values = confFile.entryMap();
297         if (values.value("description") == profileName) {
298             values.insert("path", profilesFiles.at(i));
299             return values;
300         }
301     }
302
303     // List custom profiles
304     QStringList customProfiles = KGlobal::dirs()->findDirs("appdata", "profiles");
305     for (int i = 0; i < customProfiles.size(); ++i) {
306         QStringList profiles = QDir(customProfiles.at(i)).entryList(profilesFilter, QDir::Files);
307         for (int i = 0; i < profiles.size(); ++i) {
308             KConfig confFile(customProfiles.at(i) + "/" + profiles.at(i), KConfig::SimpleConfig);
309             QMap< QString, QString > values = confFile.entryMap();
310             if (values.value("description") == profileName) {
311                 values.insert("path", customProfiles.at(i) + "/" + profiles.at(i));
312                 return values;
313             }
314         }
315     }
316     return QMap< QString, QString >();
317 }
318
319 // static
320 QString ProfilesDialog::getPathFromDescription(const QString profileDesc) {
321     QStringList profilesNames;
322     QStringList profilesFiles;
323     QStringList profilesFilter;
324     profilesFilter << "*";
325
326     // List the Mlt profiles
327     profilesFiles = QDir(KdenliveSettings::mltpath()).entryList(profilesFilter, QDir::Files);
328     for (int i = 0; i < profilesFiles.size(); ++i) {
329         KConfig confFile(KdenliveSettings::mltpath() + "/" + profilesFiles.at(i), KConfig::SimpleConfig);
330         QMap< QString, QString > values = confFile.entryMap();
331         if (values.value("description") == profileDesc) return profilesFiles.at(i);
332     }
333
334     // List custom profiles
335     QStringList customProfiles = KGlobal::dirs()->findDirs("appdata", "profiles");
336     for (int i = 0; i < customProfiles.size(); ++i) {
337         QStringList profiles = QDir(customProfiles.at(i)).entryList(profilesFilter, QDir::Files);
338         for (int i = 0; i < profiles.size(); ++i) {
339             KConfig confFile(customProfiles.at(i) + "/" + profiles.at(i), KConfig::SimpleConfig);
340             QMap< QString, QString > values = confFile.entryMap();
341             if (values.value("description") == profileDesc) return customProfiles.at(i) + "/" + profiles.at(i);
342         }
343     }
344     return QString();
345 }
346
347
348 void ProfilesDialog::slotUpdateDisplay() {
349     if (askForSave() == false) {
350         m_view.profiles_list->blockSignals(true);
351         m_view.profiles_list->setCurrentIndex(m_selectedProfileIndex);
352         m_view.profiles_list->blockSignals(false);
353         return;
354     }
355
356     m_selectedProfileIndex = m_view.profiles_list->currentIndex();
357     QString currentProfile = m_view.profiles_list->itemData(m_view.profiles_list->currentIndex()).toString();
358     m_isCustomProfile = currentProfile.contains("/");
359     m_view.button_delete->setEnabled(m_isCustomProfile);
360     m_view.properties->setEnabled(m_isCustomProfile);
361     m_view.button_save->setEnabled(m_isCustomProfile);
362     QMap< QString, QString > values = ProfilesDialog::getSettingsFromFile(currentProfile);
363     m_view.description->setText(values.value("description"));
364     m_view.size_w->setValue(values.value("width").toInt());
365     m_view.size_h->setValue(values.value("height").toInt());
366     m_view.aspect_num->setValue(values.value("sample_aspect_num").toInt());
367     m_view.aspect_den->setValue(values.value("sample_aspect_den").toInt());
368     m_view.display_num->setValue(values.value("display_aspect_num").toInt());
369     m_view.display_den->setValue(values.value("display_aspect_den").toInt());
370     m_view.frame_num->setValue(values.value("frame_rate_num").toInt());
371     m_view.frame_den->setValue(values.value("frame_rate_den").toInt());
372     m_view.progressive->setChecked(values.value("progressive").toInt());
373     m_profileIsModified = false;
374 }
375
376
377 #include "profilesdialog.moc"
378
379