]> git.sesse.net Git - kdenlive/blobdiff - src/profilesdialog.cpp
Try to fix timecode error:
[kdenlive] / src / profilesdialog.cpp
index 97a822e93665f6c618abf23c9eaaf004554ab329..3a8d559a53867bc95706c5679be5bffa0b2abd66 100644 (file)
 #include <QDir>
 #include <QCloseEvent>
 
-ProfilesDialog::ProfilesDialog(QWidget * parent): QDialog(parent), m_isCustomProfile(false), m_profileIsModified(false) {
+ProfilesDialog::ProfilesDialog(QWidget * parent) :
+        QDialog(parent),
+        m_profileIsModified(false),
+        m_isCustomProfile(false)
+{
     m_view.setupUi(this);
 
     QStringList profilesFilter;
@@ -61,11 +65,13 @@ ProfilesDialog::ProfilesDialog(QWidget * parent): QDialog(parent), m_isCustomPro
     connect(m_view.size_w, SIGNAL(valueChanged(int)), this, SLOT(slotProfileEdited()));
 }
 
-void ProfilesDialog::slotProfileEdited() {
+void ProfilesDialog::slotProfileEdited()
+{
     m_profileIsModified = true;
 }
 
-void ProfilesDialog::fillList(const QString selectedProfile) {
+void ProfilesDialog::fillList(const QString selectedProfile)
+{
     // List the Mlt profiles
     m_view.profiles_list->clear();
     QMap <QString, QString> profilesInfo = ProfilesDialog::getProfilesInfo();
@@ -88,11 +94,13 @@ void ProfilesDialog::fillList(const QString selectedProfile) {
     m_selectedProfileIndex = m_view.profiles_list->currentIndex();
 }
 
-void ProfilesDialog::accept() {
+void ProfilesDialog::accept()
+{
     if (askForSave()) QDialog::accept();
 }
 
-void ProfilesDialog::closeEvent(QCloseEvent *event) {
+void ProfilesDialog::closeEvent(QCloseEvent *event)
+{
     if (askForSave()) {
         event->accept();
     } else {
@@ -100,26 +108,30 @@ void ProfilesDialog::closeEvent(QCloseEvent *event) {
     }
 }
 
-bool ProfilesDialog::askForSave() {
+bool ProfilesDialog::askForSave()
+{
     if (!m_profileIsModified) return true;
     if (KMessageBox::questionYesNo(this, i18n("The custom profile was modified, do you want to save it?")) != KMessageBox::Yes) return true;
     return slotSaveProfile();
 }
 
-void ProfilesDialog::slotCreateProfile() {
+void ProfilesDialog::slotCreateProfile()
+{
     m_view.button_delete->setEnabled(false);
     m_view.button_create->setEnabled(false);
     m_view.button_save->setEnabled(true);
     m_view.properties->setEnabled(true);
 }
 
-void ProfilesDialog::slotSetDefaultProfile() {
+void ProfilesDialog::slotSetDefaultProfile()
+{
     int ix = m_view.profiles_list->currentIndex();
     QString path = m_view.profiles_list->itemData(ix).toString();
     if (!path.isEmpty()) KdenliveSettings::setDefault_profile(path);
 }
 
-bool ProfilesDialog::slotSaveProfile() {
+bool ProfilesDialog::slotSaveProfile()
+{
     const QString profileDesc = m_view.description->text();
     int ix = m_view.profiles_list->findText(profileDesc);
     if (ix != -1) {
@@ -147,7 +159,8 @@ bool ProfilesDialog::slotSaveProfile() {
     return true;
 }
 
-void ProfilesDialog::saveProfile(const QString path) {
+void ProfilesDialog::saveProfile(const QString path)
+{
     QFile file(path);
     if (!file.open(QIODevice::WriteOnly)) {
         KMessageBox::sorry(this, i18n("Cannot write to file %1", path));
@@ -155,10 +168,14 @@ void ProfilesDialog::saveProfile(const QString path) {
     }
     QTextStream out(&file);
     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";
+    if (file.error() != QFile::NoError) {
+        KMessageBox::error(this, i18n("Cannot write to file %1", path));
+    }
     file.close();
 }
 
-void ProfilesDialog::slotDeleteProfile() {
+void ProfilesDialog::slotDeleteProfile()
+{
     const QString path = m_view.profiles_list->itemData(m_view.profiles_list->currentIndex()).toString();
     if (path.contains('/')) {
         KIO::NetAccess::del(KUrl(path), this);
@@ -167,7 +184,8 @@ void ProfilesDialog::slotDeleteProfile() {
 }
 
 // static
-MltVideoProfile ProfilesDialog::getVideoProfile(QString name) {
+MltVideoProfile ProfilesDialog::getVideoProfile(QString name)
+{
     MltVideoProfile result;
     QStringList profilesNames;
     QStringList profilesFiles;
@@ -180,9 +198,9 @@ MltVideoProfile ProfilesDialog::getVideoProfile(QString name) {
     if (!isCustom) {
         // List the Mlt profiles
         profilesFiles = QDir(KdenliveSettings::mltpath()).entryList(profilesFilter, QDir::Files);
-        if (profilesFiles.contains(name)) path = KdenliveSettings::mltpath() + '/' + name;
+        if (profilesFiles.contains(name)) path = KdenliveSettings::mltpath() + name;
     }
-    if (isCustom  || path.isEmpty()) {
+    if (isCustom || path.isEmpty()) {
         path = name;
     }
 
@@ -192,8 +210,8 @@ MltVideoProfile ProfilesDialog::getVideoProfile(QString name) {
             return result;
         }
         if (name == KdenliveSettings::default_profile()) KdenliveSettings::setDefault_profile("dv_pal");
-        kDebug() << "// WARNING, COULD NOT FIND PROFILE " << name;
-        return getVideoProfile("dv_pal");
+        kDebug() << "// WARNING, COULD NOT FIND PROFILE " << name;
+        return result;
     }
     KConfig confFile(path, KConfig::SimpleConfig);
     result.path = name;
@@ -211,34 +229,92 @@ MltVideoProfile ProfilesDialog::getVideoProfile(QString name) {
 }
 
 // static
-QString ProfilesDialog::getProfileDescription(QString name) {
-    QStringList profilesNames;
-    QStringList profilesFiles;
+double ProfilesDialog::getStringEval(const MltVideoProfile &profile, QString eval)
+{
+    double result;
+    eval.replace("%width", QString::number(profile.width));
+    eval.replace("%height", QString::number(profile.height));
+    if (eval.contains('/')) result = (double) eval.section('/', 0, 0).toInt() / eval.section('/', 1, 1).toInt();
+    else if (eval.contains('*')) result = (double) eval.section('*', 0, 0).toInt() * eval.section('*', 1, 1).toInt();
+    else if (eval.contains('+')) result = (double) eval.section('+', 0, 0).toInt() + eval.section('+', 1, 1).toInt();
+    else if (eval.contains('-')) result = (double) eval.section('-', 0, 0).toInt() - eval.section('-', 1, 1).toInt();
+    else result = eval.toDouble();
+    return result;
+}
+
+
+// static
+bool ProfilesDialog::existingProfileDescription(const QString &desc)
+{
     QStringList profilesFilter;
     profilesFilter << "*";
 
     // List the Mlt profiles
-    profilesFiles = QDir(KdenliveSettings::mltpath()).entryList(profilesFilter, QDir::Files);
-    if (profilesFiles.contains(name)) {
-        KConfig confFile(KdenliveSettings::mltpath() + '/' + name, KConfig::SimpleConfig);
-        return confFile.entryMap().value("description");
+    QStringList profilesFiles = QDir(KdenliveSettings::mltpath()).entryList(profilesFilter, QDir::Files);
+    for (int i = 0; i < profilesFiles.size(); ++i) {
+        KConfig confFile(KdenliveSettings::mltpath() + profilesFiles.at(i), KConfig::SimpleConfig);
+        if (desc == confFile.entryMap().value("description")) return true;
     }
 
     // List custom profiles
     QStringList customProfiles = KGlobal::dirs()->findDirs("appdata", "profiles");
     for (int i = 0; i < customProfiles.size(); ++i) {
         profilesFiles = QDir(customProfiles.at(i)).entryList(profilesFilter, QDir::Files);
-        if (profilesFiles.contains(name)) {
-            KConfig confFile(customProfiles.at(i) + '/' + name, KConfig::SimpleConfig);
-            return confFile.entryMap().value("description");
+        for (int j = 0; j < profilesFiles.size(); ++j) {
+            KConfig confFile(customProfiles.at(i) + profilesFiles.at(j), KConfig::SimpleConfig);
+            if (desc == confFile.entryMap().value("description")) return true;
         }
     }
+    return false;
+}
+
+// static
+QString ProfilesDialog::existingProfile(MltVideoProfile profile)
+{
+    // Check if the profile has a matching entry in existing ones
+    QStringList profilesFilter;
+    profilesFilter << "*";
+
+    // Check the Mlt profiles
+    QStringList profilesFiles = QDir(KdenliveSettings::mltpath()).entryList(profilesFilter, QDir::Files);
+    for (int i = 0; i < profilesFiles.size(); ++i) {
+        KConfig confFile(KdenliveSettings::mltpath() + profilesFiles.at(i), KConfig::SimpleConfig);
+        if (profile.display_aspect_den != confFile.entryMap().value("display_aspect_den").toInt()) continue;
+        if (profile.display_aspect_num != confFile.entryMap().value("display_aspect_num").toInt()) continue;
+        if (profile.sample_aspect_den != confFile.entryMap().value("sample_aspect_den").toInt()) continue;
+        if (profile.sample_aspect_num != confFile.entryMap().value("sample_aspect_num").toInt()) continue;
+        if (profile.width != confFile.entryMap().value("width").toInt()) continue;
+        if (profile.height != confFile.entryMap().value("height").toInt()) continue;
+        if (profile.frame_rate_den != confFile.entryMap().value("frame_rate_den").toInt()) continue;
+        if (profile.frame_rate_num != confFile.entryMap().value("frame_rate_num").toInt()) continue;
+        if (profile.progressive != confFile.entryMap().value("progressive").toInt()) continue;
+        return profilesFiles.at(i);
+    }
 
+    // Check custom profiles
+    QStringList customProfiles = KGlobal::dirs()->findDirs("appdata", "profiles");
+    for (int i = 0; i < customProfiles.size(); ++i) {
+        profilesFiles = QDir(customProfiles.at(i)).entryList(profilesFilter, QDir::Files);
+        for (int j = 0; j < profilesFiles.size(); ++j) {
+            KConfig confFile(customProfiles.at(i) + profilesFiles.at(j), KConfig::SimpleConfig);
+            if (profile.display_aspect_den != confFile.entryMap().value("display_aspect_den").toInt()) continue;
+            if (profile.display_aspect_num != confFile.entryMap().value("display_aspect_num").toInt()) continue;
+            if (profile.sample_aspect_den != confFile.entryMap().value("sample_aspect_den").toInt()) continue;
+            if (profile.sample_aspect_num != confFile.entryMap().value("sample_aspect_num").toInt()) continue;
+            if (profile.width != confFile.entryMap().value("width").toInt()) continue;
+            if (profile.height != confFile.entryMap().value("height").toInt()) continue;
+            if (profile.frame_rate_den != confFile.entryMap().value("frame_rate_den").toInt()) continue;
+            if (profile.frame_rate_num != confFile.entryMap().value("frame_rate_num").toInt()) continue;
+            if (profile.progressive != confFile.entryMap().value("progressive").toInt()) continue;
+            return customProfiles.at(i) + profilesFiles.at(j);
+        }
+    }
     return QString();
 }
 
 // static
-QMap <QString, QString> ProfilesDialog::getProfilesInfo() {
+QMap <QString, QString> ProfilesDialog::getProfilesInfo()
+{
     QMap <QString, QString> result;
     QStringList profilesFilter;
     profilesFilter << "*";
@@ -246,7 +322,7 @@ QMap <QString, QString> ProfilesDialog::getProfilesInfo() {
     // List the Mlt profiles
     QStringList profilesFiles = QDir(KdenliveSettings::mltpath()).entryList(profilesFilter, QDir::Files);
     for (int i = 0; i < profilesFiles.size(); ++i) {
-        KConfig confFile(KdenliveSettings::mltpath() + '/' + profilesFiles.at(i), KConfig::SimpleConfig);
+        KConfig confFile(KdenliveSettings::mltpath() + profilesFiles.at(i), KConfig::SimpleConfig);
         QString desc = confFile.entryMap().value("description");
         if (!desc.isEmpty()) result.insert(desc, profilesFiles.at(i));
     }
@@ -256,16 +332,17 @@ QMap <QString, QString> ProfilesDialog::getProfilesInfo() {
     for (int i = 0; i < customProfiles.size(); ++i) {
         profilesFiles = QDir(customProfiles.at(i)).entryList(profilesFilter, QDir::Files);
         for (int j = 0; j < profilesFiles.size(); ++j) {
-            KConfig confFile(customProfiles.at(i) + '/' + profilesFiles.at(j), KConfig::SimpleConfig);
+            KConfig confFile(customProfiles.at(i) + profilesFiles.at(j), KConfig::SimpleConfig);
             QString desc = confFile.entryMap().value("description");
-            if (!desc.isEmpty()) result.insert(desc, customProfiles.at(i) + '/' + profilesFiles.at(j));
+            if (!desc.isEmpty()) result.insert(desc, customProfiles.at(i) + profilesFiles.at(j));
         }
     }
     return result;
 }
 
 // static
-QMap< QString, QString > ProfilesDialog::getSettingsFromFile(const QString path) {
+QMap< QString, QString > ProfilesDialog::getSettingsFromFile(const QString path)
+{
     QStringList profilesNames;
     QStringList profilesFiles;
     QStringList profilesFilter;
@@ -273,7 +350,7 @@ QMap< QString, QString > ProfilesDialog::getSettingsFromFile(const QString path)
 
     if (!path.contains('/')) {
         // This is an MLT profile
-        KConfig confFile(KdenliveSettings::mltpath() + '/' + path, KConfig::SimpleConfig);
+        KConfig confFile(KdenliveSettings::mltpath() + path, KConfig::SimpleConfig);
         return confFile.entryMap();
     } else {
         // This is a custom profile
@@ -283,7 +360,8 @@ QMap< QString, QString > ProfilesDialog::getSettingsFromFile(const QString path)
 }
 
 // static
-QMap< QString, QString > ProfilesDialog::getSettingsForProfile(const QString profileName) {
+QMap< QString, QString > ProfilesDialog::getSettingsForProfile(const QString profileName)
+{
     QStringList profilesNames;
     QStringList profilesFiles;
     QStringList profilesFilter;
@@ -292,7 +370,7 @@ QMap< QString, QString > ProfilesDialog::getSettingsForProfile(const QString pro
     // List the Mlt profiles
     profilesFiles = QDir(KdenliveSettings::mltpath()).entryList(profilesFilter, QDir::Files);
     for (int i = 0; i < profilesFiles.size(); ++i) {
-        KConfig confFile(KdenliveSettings::mltpath() + '/' + profilesFiles.at(i), KConfig::SimpleConfig);
+        KConfig confFile(KdenliveSettings::mltpath() + profilesFiles.at(i), KConfig::SimpleConfig);
         QMap< QString, QString > values = confFile.entryMap();
         if (values.value("description") == profileName) {
             values.insert("path", profilesFiles.at(i));
@@ -305,10 +383,10 @@ QMap< QString, QString > ProfilesDialog::getSettingsForProfile(const QString pro
     for (int i = 0; i < customProfiles.size(); ++i) {
         QStringList profiles = QDir(customProfiles.at(i)).entryList(profilesFilter, QDir::Files);
         for (int i = 0; i < profiles.size(); ++i) {
-            KConfig confFile(customProfiles.at(i) + '/' + profiles.at(i), KConfig::SimpleConfig);
+            KConfig confFile(customProfiles.at(i) + profiles.at(i), KConfig::SimpleConfig);
             QMap< QString, QString > values = confFile.entryMap();
             if (values.value("description") == profileName) {
-                values.insert("path", customProfiles.at(i) + '/' + profiles.at(i));
+                values.insert("path", customProfiles.at(i) + profiles.at(i));
                 return values;
             }
         }
@@ -317,7 +395,8 @@ QMap< QString, QString > ProfilesDialog::getSettingsForProfile(const QString pro
 }
 
 // static
-QString ProfilesDialog::getPathFromDescription(const QString profileDesc) {
+QString ProfilesDialog::getPathFromDescription(const QString profileDesc)
+{
     QStringList profilesNames;
     QStringList profilesFiles;
     QStringList profilesFilter;
@@ -326,7 +405,7 @@ QString ProfilesDialog::getPathFromDescription(const QString profileDesc) {
     // List the Mlt profiles
     profilesFiles = QDir(KdenliveSettings::mltpath()).entryList(profilesFilter, QDir::Files);
     for (int i = 0; i < profilesFiles.size(); ++i) {
-        KConfig confFile(KdenliveSettings::mltpath() + '/' + profilesFiles.at(i), KConfig::SimpleConfig);
+        KConfig confFile(KdenliveSettings::mltpath() + profilesFiles.at(i), KConfig::SimpleConfig);
         QMap< QString, QString > values = confFile.entryMap();
         if (values.value("description") == profileDesc) return profilesFiles.at(i);
     }
@@ -336,16 +415,42 @@ QString ProfilesDialog::getPathFromDescription(const QString profileDesc) {
     for (int i = 0; i < customProfiles.size(); ++i) {
         QStringList profiles = QDir(customProfiles.at(i)).entryList(profilesFilter, QDir::Files);
         for (int i = 0; i < profiles.size(); ++i) {
-            KConfig confFile(customProfiles.at(i) + '/' + profiles.at(i), KConfig::SimpleConfig);
+            KConfig confFile(customProfiles.at(i) + profiles.at(i), KConfig::SimpleConfig);
             QMap< QString, QString > values = confFile.entryMap();
-            if (values.value("description") == profileDesc) return customProfiles.at(i) + '/' + profiles.at(i);
+            if (values.value("description") == profileDesc) return customProfiles.at(i) + profiles.at(i);
         }
     }
     return QString();
 }
 
+// static
+void ProfilesDialog::saveProfile(MltVideoProfile &profile)
+{
+    int i = 0;
+    QString customName = "profiles/customprofile";
+    QString profilePath = KStandardDirs::locateLocal("appdata", customName + QString::number(i));
+    kDebug() << " TYING PROFILE FILE: " << profilePath;
+    while (KIO::NetAccess::exists(KUrl(profilePath), KIO::NetAccess::SourceSide, 0)) {
+        i++;
+        profilePath = KStandardDirs::locateLocal("appdata", customName + QString::number(i));
+    }
+    QFile file(profilePath);
+    if (!file.open(QIODevice::WriteOnly)) {
+        KMessageBox::sorry(0, i18n("Cannot write to file %1", profilePath));
+        return;
+    }
+    QTextStream out(&file);
+    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";
+    if (file.error() != QFile::NoError) {
+        KMessageBox::error(0, i18n("Cannot write to file %1", profilePath));
+    }
+    file.close();
+    profile.path = profilePath;
+}
 
-void ProfilesDialog::slotUpdateDisplay() {
+
+void ProfilesDialog::slotUpdateDisplay()
+{
     if (askForSave() == false) {
         m_view.profiles_list->blockSignals(true);
         m_view.profiles_list->setCurrentIndex(m_selectedProfileIndex);
@@ -356,6 +461,7 @@ void ProfilesDialog::slotUpdateDisplay() {
     m_selectedProfileIndex = m_view.profiles_list->currentIndex();
     QString currentProfile = m_view.profiles_list->itemData(m_view.profiles_list->currentIndex()).toString();
     m_isCustomProfile = currentProfile.contains('/');
+    m_view.button_create->setEnabled(true);
     m_view.button_delete->setEnabled(m_isCustomProfile);
     m_view.properties->setEnabled(m_isCustomProfile);
     m_view.button_save->setEnabled(m_isCustomProfile);