]> git.sesse.net Git - kdenlive/blob - src/projectsettings.cpp
List images included in titles
[kdenlive] / src / projectsettings.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 "projectsettings.h"
21 #include "kdenlivesettings.h"
22 #include "profilesdialog.h"
23 #include "docclipbase.h"
24 #include "titlewidget.h"
25
26 #include <KStandardDirs>
27 #include <KMessageBox>
28 #include <KDebug>
29 #include <kio/directorysizejob.h>
30 #include <KIO/NetAccess>
31
32 #include <QDir>
33 #include <kmessagebox.h>
34
35 ProjectSettings::ProjectSettings(ProjectList *projectlist, int videotracks, int audiotracks, const QString projectPath, bool readOnlyTracks, bool savedProject, QWidget * parent) :
36         QDialog(parent), m_savedProject(savedProject), m_projectList(projectlist)
37 {
38     setupUi(this);
39     
40     list_search->setListWidget(files_list);
41
42     QMap <QString, QString> profilesInfo = ProfilesDialog::getProfilesInfo();
43     QMapIterator<QString, QString> i(profilesInfo);
44     while (i.hasNext()) {
45         i.next();
46         profiles_list->addItem(i.key(), i.value());
47     }
48     project_folder->setMode(KFile::Directory);
49     project_folder->setUrl(KUrl(projectPath));
50     QString currentProf = KdenliveSettings::current_profile();
51
52     for (int i = 0; i < profiles_list->count(); i++) {
53         if (profiles_list->itemData(i).toString() == currentProf) {
54             profiles_list->setCurrentIndex(i);
55             break;
56         }
57     }
58
59     m_buttonOk = buttonBox->button(QDialogButtonBox::Ok);
60     //buttonOk->setEnabled(false);
61     audio_thumbs->setChecked(KdenliveSettings::audiothumbnails());
62     video_thumbs->setChecked(KdenliveSettings::videothumbnails());
63     audio_tracks->setValue(audiotracks);
64     video_tracks->setValue(videotracks);
65     if (readOnlyTracks) {
66         video_tracks->setEnabled(false);
67         audio_tracks->setEnabled(false);
68     }
69     slotUpdateDisplay();
70     if (m_projectList != NULL) {
71         slotUpdateFiles();
72         connect(clear_cache, SIGNAL(clicked()), this, SLOT(slotClearCache()));
73         connect(delete_unused, SIGNAL(clicked()), this, SLOT(slotDeleteUnused()));
74     } else tabWidget->widget(1)->setEnabled(false);
75     connect(profiles_list, SIGNAL(currentIndexChanged(int)), this, SLOT(slotUpdateDisplay()));
76     connect(project_folder, SIGNAL(textChanged(const QString &)), this, SLOT(slotUpdateButton(const QString &)));
77 }
78
79 void ProjectSettings::slotDeleteUnused()
80 {
81     QStringList toDelete;
82     QList <DocClipBase*> list = m_projectList->documentClipList();
83     for (int i = 0; i < list.count(); i++) {
84         DocClipBase *clip = list.at(i);
85         if (clip->numReferences() == 0 && clip->clipType() != SLIDESHOW) {
86             KUrl url = clip->fileURL();
87             if (!url.isEmpty()) toDelete << url.path();
88         }
89     }
90     toDelete.removeDuplicates();
91
92     // make sure our urls are not used in another clip
93     for (int i = 0; i < list.count(); i++) {
94         DocClipBase *clip = list.at(i);
95         if (clip->numReferences() > 0) {
96             KUrl url = clip->fileURL();
97             if (!url.isEmpty() && toDelete.contains(url.path())) toDelete.removeAll(url.path());
98         }
99     }
100
101     if (toDelete.count() == 0) {
102         // No physical url to delete, we only remove unused clips from project (color clips for example have no physical url)
103         if (KMessageBox::warningContinueCancel(this, i18n("This will remove all unused clips from your project."), i18n("Clean up project")) == KMessageBox::Cancel) return;
104         m_projectList->cleanup();
105         slotUpdateFiles();
106         return;
107     }
108     if (KMessageBox::warningYesNoList(this, i18n("This will remove the following files from your hard drive.\nThis action cannot be undone, only use if you know what you are doing.\nAre you sure you want to continue?"), toDelete, i18n("Delete unused clips")) != KMessageBox::Yes) return;
109     m_projectList->trashUnusedClips();
110     slotUpdateFiles();
111 }
112
113 void ProjectSettings::slotClearCache()
114 {
115     buttonBox->setEnabled(false);
116     KIO::NetAccess::del(KUrl(project_folder->url().path(KUrl::AddTrailingSlash) + "thumbs/"), this);
117     KStandardDirs::makeDir(project_folder->url().path(KUrl::AddTrailingSlash) + "thumbs/");
118     buttonBox->setEnabled(true);
119     slotUpdateFiles(true);
120 }
121
122 void ProjectSettings::slotUpdateFiles(bool cacheOnly)
123 {
124     KIO::DirectorySizeJob * job = KIO::directorySize(project_folder->url().path(KUrl::AddTrailingSlash) + "thumbs/");
125     job->exec();
126     thumbs_count->setText(QString::number(job->totalFiles()));
127     thumbs_size->setText(KIO::convertSize(job->totalSize()));
128     delete job;
129     if (cacheOnly) return;
130     int unused = 0;
131     int used = 0;
132     KIO::filesize_t usedSize = 0;
133     KIO::filesize_t unUsedSize = 0;
134     QList <DocClipBase*> list = m_projectList->documentClipList();
135     files_list->clear();
136     
137     // List all files that are used in the project. That also means:
138     // images included in slideshow, titles
139     // TODO: images used in luma transitions, files used in playlist clips, files used for LADSPA effects
140     
141     QStringList allFiles;
142     for (int i = 0; i < list.count(); i++) {
143         DocClipBase *clip = list.at(i);
144         if (clip->clipType() == SLIDESHOW) {
145             // special case, list all images
146             QString path = clip->fileURL().directory();
147             QString ext = clip->fileURL().path().section('.', -1);
148             QDir dir(path);
149             QStringList filters;
150             filters << "*." + ext;
151             dir.setNameFilters(filters);
152             QStringList result = dir.entryList(QDir::Files);
153             for (int j = 0; j < result.count(); j++) {
154                 allFiles.append(path + result.at(j));
155             }
156         }
157         else if (!clip->fileURL().isEmpty()) allFiles.append(clip->fileURL().path());
158         if (clip->clipType() == TEXT) {
159             QStringList images = TitleWidget::extractImageList(clip->getProperty("xmldata"));
160             allFiles << images;
161         }
162         
163         if (clip->numReferences() == 0) {
164             unused++;
165             unUsedSize += clip->fileSize();
166         } else {
167             used++;
168             usedSize += clip->fileSize();
169         }
170     }
171     allFiles.removeDuplicates();
172     files_count->setText(QString::number(allFiles.count()));
173     files_list->addItems(allFiles);
174     used_count->setText(QString::number(used));
175     used_size->setText(KIO::convertSize(usedSize));
176     unused_count->setText(QString::number(unused));
177     unused_size->setText(KIO::convertSize(unUsedSize));
178     delete_unused->setEnabled(unused > 0);
179 }
180
181 void ProjectSettings::accept()
182 {
183     if (!m_savedProject && selectedProfile() != KdenliveSettings::current_profile())
184         if (KMessageBox::warningContinueCancel(this, i18n("Changing the profile of your project cannot be undone.\nIt is recommended to save your project before attempting this operation that might cause some corruption in transitions.\n Are you sure you want to proceed?"), i18n("Confirm profile change")) == KMessageBox::Cancel) return;
185     QDialog::accept();
186 }
187
188 void ProjectSettings::slotUpdateDisplay()
189 {
190     QString currentProfile = profiles_list->itemData(profiles_list->currentIndex()).toString();
191     QMap< QString, QString > values = ProfilesDialog::getSettingsFromFile(currentProfile);
192     p_size->setText(values.value("width") + 'x' + values.value("height"));
193     p_fps->setText(values.value("frame_rate_num") + '/' + values.value("frame_rate_den"));
194     p_aspect->setText(values.value("sample_aspect_num") + '/' + values.value("sample_aspect_den"));
195     p_display->setText(values.value("display_aspect_num") + '/' + values.value("display_aspect_den"));
196     if (values.value("progressive").toInt() == 0) p_progressive->setText(i18n("Interlaced"));
197     else p_progressive->setText(i18n("Progressive"));
198 }
199
200 void ProjectSettings::slotUpdateButton(const QString &path)
201 {
202     if (path.isEmpty()) m_buttonOk->setEnabled(false);
203     else {
204         m_buttonOk->setEnabled(true);
205         slotUpdateFiles(true);
206     }
207 }
208
209 QString ProjectSettings::selectedProfile() const
210 {
211     return profiles_list->itemData(profiles_list->currentIndex()).toString();
212 }
213
214 KUrl ProjectSettings::selectedFolder() const
215 {
216     return project_folder->url();
217 }
218
219 QPoint ProjectSettings::tracks()
220 {
221     QPoint p;
222     p.setX(video_tracks->value());
223     p.setY(audio_tracks->value());
224     return p;
225 }
226
227 bool ProjectSettings::enableVideoThumbs() const
228 {
229     return video_thumbs->isChecked();
230 }
231
232 bool ProjectSettings::enableAudioThumbs() const
233 {
234     return audio_thumbs->isChecked();
235 }
236
237 #include "projectsettings.moc"
238
239