]> git.sesse.net Git - kdenlive/blob - src/slideshowclip.cpp
Add animation feature to Slideshow Clip.
[kdenlive] / src / slideshowclip.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 "slideshowclip.h"
21 #include "kdenlivesettings.h"
22
23 #include <KStandardDirs>
24 #include <KDebug>
25 #include <KFileItem>
26
27 #include <QDir>
28
29
30 SlideshowClip::SlideshowClip(Timecode tc, QWidget * parent) :
31         QDialog(parent),
32         m_count(0),
33         m_timecode(tc),
34         m_thumbJob(NULL)
35 {
36     setFont(KGlobalSettings::toolBarFont());
37     setWindowTitle(i18n("Add Slideshow Clip"));
38     m_view.setupUi(this);
39     m_view.clip_name->setText(i18n("Slideshow Clip"));
40     m_view.folder_url->setMode(KFile::Directory);
41     m_view.icon_list->setIconSize(QSize(50, 50));
42     m_view.show_thumbs->setChecked(KdenliveSettings::showslideshowthumbs());
43
44     connect(m_view.folder_url, SIGNAL(textChanged(const QString &)), this, SLOT(parseFolder()));
45     connect(m_view.image_type, SIGNAL(currentIndexChanged(int)), this, SLOT(parseFolder()));
46     connect(m_view.pattern_url, SIGNAL(textChanged(const QString &)), this, SLOT(parseFolder()));
47
48     connect(m_view.show_thumbs, SIGNAL(stateChanged(int)), this, SLOT(slotEnableThumbs(int)));
49     connect(m_view.slide_fade, SIGNAL(stateChanged(int)), this, SLOT(slotEnableLuma(int)));
50     connect(m_view.luma_fade, SIGNAL(stateChanged(int)), this, SLOT(slotEnableLumaFile(int)));
51
52     //WARNING: keep in sync with clipproperties.cpp
53     m_view.image_type->addItem("JPG (*.jpg)", "jpg");
54     m_view.image_type->addItem("JPEG (*.jpeg)", "jpeg");
55     m_view.image_type->addItem("PNG (*.png)", "png");
56     m_view.image_type->addItem("BMP (*.bmp)", "bmp");
57     m_view.image_type->addItem("GIF (*.gif)", "gif");
58     m_view.image_type->addItem("TGA (*.tga)", "tga");
59     m_view.image_type->addItem("TIFF (*.tiff)", "tiff");
60     m_view.image_type->addItem("Open EXR (*.exr)", "exr");
61     m_view.animation->addItem(i18n("None"), QString());
62     m_view.animation->addItem(i18n("Pan"), "Pan");
63     m_view.animation->addItem(i18n("Pan, low-pass"), "Pan, low-pass");
64     m_view.animation->addItem(i18n("Pan and zoom"), "Pan and zoom");
65     m_view.animation->addItem(i18n("Pan and zoom, low-pass"), "Pan and zoom, low-pass");
66     m_view.animation->addItem(i18n("Zoom"), "Zoom");
67     m_view.animation->addItem(i18n("Zoom, low-pass"), "Zoom, low-pass");
68
69     m_view.clip_duration->setInputMask("");
70     m_view.clip_duration->setValidator(m_timecode.validator());
71     m_view.clip_duration->setText(m_timecode.reformatSeparators(KdenliveSettings::sequence_duration()));
72     m_view.luma_duration->setInputMask("");
73     m_view.luma_duration->setValidator(m_timecode.validator());
74     m_view.luma_duration->setText(m_timecode.getTimecodeFromFrames(int(ceil(m_timecode.fps()))));
75     m_view.folder_url->setUrl(QDir::homePath());
76
77     m_view.clip_duration_format->addItem(i18n("hh:mm:ss::ff"));
78     m_view.clip_duration_format->addItem(i18n("Frames"));
79     connect(m_view.clip_duration_format, SIGNAL(activated(int)), this, SLOT(slotUpdateDurationFormat(int)));
80     m_view.clip_duration_frames->setHidden(true);
81     m_view.luma_duration_frames->setHidden(true);
82     m_view.method_mime->setChecked(KdenliveSettings::slideshowbymime());
83     connect(m_view.method_mime, SIGNAL(toggled(bool)), this, SLOT(slotMethodChanged(bool)));
84     slotMethodChanged(m_view.method_mime->isChecked());
85
86     // Check for Kdenlive installed luma files
87     QStringList filters;
88     filters << "*.pgm" << "*.png";
89
90     QStringList customLumas = KGlobal::dirs()->findDirs("appdata", "lumas");
91     foreach(const QString &folder, customLumas) {
92         QStringList filesnames = QDir(folder).entryList(filters, QDir::Files);
93         foreach(const QString &fname, filesnames) {
94             QString filePath = KUrl(folder).path(KUrl::AddTrailingSlash) + fname;
95             m_view.luma_file->addItem(KIcon(filePath), fname, filePath);
96         }
97     }
98
99     // Check for MLT lumas
100     QString profilePath = KdenliveSettings::mltpath();
101     QString folder = profilePath.section('/', 0, -3);
102     folder.append("/lumas/PAL"); // TODO: cleanup the PAL / NTSC mess in luma files
103     QDir lumafolder(folder);
104     QStringList filesnames = lumafolder.entryList(filters, QDir::Files);
105     foreach(const QString &fname, filesnames) {
106         QString filePath = KUrl(folder).path(KUrl::AddTrailingSlash) + fname;
107         m_view.luma_file->addItem(KIcon(filePath), fname, filePath);
108     }
109
110     //adjustSize();
111 }
112
113 SlideshowClip::~SlideshowClip()
114 {
115     if (m_thumbJob) {
116         delete m_thumbJob;
117     }
118 }
119
120 void SlideshowClip::slotEnableLuma(int state)
121 {
122     bool enable = false;
123     if (state == Qt::Checked) enable = true;
124     m_view.luma_duration->setEnabled(enable);
125     m_view.luma_duration_frames->setEnabled(enable);
126     m_view.luma_fade->setEnabled(enable);
127     if (enable) {
128         m_view.luma_file->setEnabled(m_view.luma_fade->isChecked());
129     } else m_view.luma_file->setEnabled(false);
130     m_view.label_softness->setEnabled(m_view.luma_fade->isChecked() && enable);
131     m_view.luma_softness->setEnabled(m_view.label_softness->isEnabled());
132 }
133
134 void SlideshowClip::slotEnableThumbs(int state)
135 {
136     if (state == Qt::Checked) {
137         KdenliveSettings::setShowslideshowthumbs(true);
138         slotGenerateThumbs();
139     } else {
140         KdenliveSettings::setShowslideshowthumbs(false);
141         if (m_thumbJob) {
142             disconnect(m_thumbJob, SIGNAL(gotPreview(const KFileItem &, const QPixmap &)), this, SLOT(slotSetPixmap(const KFileItem &, const QPixmap &)));
143             m_thumbJob->kill();
144             m_thumbJob = NULL;
145         }
146     }
147
148 }
149
150 void SlideshowClip::slotEnableLumaFile(int state)
151 {
152     bool enable = false;
153     if (state == Qt::Checked) enable = true;
154     m_view.luma_file->setEnabled(enable);
155     m_view.luma_softness->setEnabled(enable);
156     m_view.label_softness->setEnabled(enable);
157 }
158
159 // static
160 int SlideshowClip::sequenceCount(KUrl file)
161 {
162     // find pattern
163     QString filter = file.fileName();
164     QString ext = filter.section('.', -1);
165     filter = filter.section('.', 0, -2);
166     bool hasDigit = false;
167     while (filter.at(filter.size() - 1).isDigit()) {
168         hasDigit = true;
169         filter.remove(filter.size() - 1, 1);
170     }
171     if (!hasDigit) return 0;
172
173     QString regexp = "^" + filter + "\\d+\\." + ext + "$";
174     QRegExp rx(regexp);
175
176     QDir dir(file.directory());
177     QStringList result = dir.entryList(QDir::Files);
178
179     int count = 0;
180     foreach(const QString &path, result) {
181         if (rx.exactMatch(path)) count ++;
182     }
183     return count;
184 }
185
186 void SlideshowClip::parseFolder()
187 {
188     m_view.icon_list->clear();
189     bool isMime = m_view.method_mime->isChecked();
190     QString path = isMime ? m_view.folder_url->url().path() : m_view.pattern_url->url().directory();
191     QDir dir(path);
192     if (path.isEmpty() || !dir.exists()) return;
193
194     QStringList filters;
195     QString filter;
196     if (isMime) {
197         // TODO: improve jpeg image detection with extension like jpeg, requires change in MLT image producers
198         filter = m_view.image_type->itemData(m_view.image_type->currentIndex()).toString();
199         filters << "*." + filter;
200         dir.setNameFilters(filters);
201     }
202
203     QStringList result = dir.entryList(QDir::Files);
204
205     if (!isMime) {
206         // find pattern
207         filter = m_view.pattern_url->url().fileName();
208         QString ext = filter.section('.', -1);
209         filter = filter.section('.', 0, -2);
210
211         while (filter.at(filter.size() - 1).isDigit()) {
212             filter.remove(filter.size() - 1, 1);
213         }
214         QString regexp = "^" + filter + "\\d+\\." + ext + "$";
215         QRegExp rx(regexp);
216         QStringList entries;
217         foreach(const QString &path, result) {
218             if (rx.exactMatch(path)) entries << path;
219         }
220         result = entries;
221     }
222
223     m_count = result.count();
224     if (m_count == 0) m_view.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
225     else m_view.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
226     m_view.label_info->setText(i18np("1 image found", "%1 images found", m_count));
227     QListWidgetItem *item;
228     int i = 0;
229     KIcon unknownicon("unknown");
230     foreach(const QString &path, result) {
231         i++;
232         item = new QListWidgetItem(unknownicon, KUrl(path).fileName());
233         item->setData(Qt::UserRole, dir.filePath(path));
234         m_view.icon_list->addItem(item);
235     }
236     if (m_view.show_thumbs->isChecked()) slotGenerateThumbs();
237     m_view.icon_list->setCurrentRow(0);
238 }
239
240 void SlideshowClip::slotGenerateThumbs()
241 {
242     if (m_thumbJob) {
243         delete m_thumbJob;
244     };
245     KFileItemList fileList;
246     for (int i = 0; i < m_view.icon_list->count(); i++) {
247         QListWidgetItem* item = m_view.icon_list->item(i);
248         if (item) {
249             QString path = item->data(Qt::UserRole).toString();
250             if (!path.isEmpty()) {
251                 fileList.append(KFileItem(KFileItem::Unknown, KFileItem::Unknown, KUrl(path)));
252             }
253         }
254     }
255     m_thumbJob = new KIO::PreviewJob(fileList, 50, 0, 0, 0, true, true, 0);
256     m_thumbJob->setAutoDelete(false);
257     connect(m_thumbJob, SIGNAL(gotPreview(const KFileItem &, const QPixmap &)), this, SLOT(slotSetPixmap(const KFileItem &, const QPixmap &)));
258     m_thumbJob->start();
259 }
260
261 void SlideshowClip::slotSetPixmap(const KFileItem &fileItem, const QPixmap &pix)
262 {
263     for (int i = 0; i < m_view.icon_list->count(); i++) {
264         QListWidgetItem* item = m_view.icon_list->item(i);
265         if (item) {
266             QString path = item->data(Qt::UserRole).toString();
267             if (path == fileItem.url().path()) {
268                 item->setIcon(KIcon(pix));
269                 item->setData(Qt::UserRole, QString());
270                 break;
271             }
272         }
273     }
274 }
275
276
277 QString SlideshowClip::selectedPath()
278 {
279     return selectedPath(m_view.folder_url->url(), m_view.method_mime->isChecked(), ".all." + m_view.image_type->itemData(m_view.image_type->currentIndex()).toString(), &m_count);
280
281
282 }
283 // static
284 QString SlideshowClip::selectedPath(KUrl url, bool isMime, QString extension, int *count)
285 {
286     QString folder;
287
288     if (isMime) {
289         folder = url.path(KUrl::AddTrailingSlash);
290     } else {
291         folder = url.directory(KUrl::AppendTrailingSlash);
292         QString filter = url.fileName();
293         QString ext = '.' + filter.section('.', -1);
294         filter = filter.section('.', 0, -2);
295
296         while (filter.at(filter.size() - 1).isDigit()) {
297             filter.chop(1);
298         }
299         // Check that the first image exists and which format it has (image1.jpg or image001.jpg, ...)
300
301         // Find first image in sequence
302         QString regexp = "^" + filter + "\\d+" + ext + "$";
303         QRegExp rx(regexp);
304         QStringList entries;
305
306         QDir dir(folder);
307         QStringList result = dir.entryList(QDir::Files);
308         int precision = 1;
309         QString pathValue;
310         QMap <int, QString> sortedList;
311         foreach(const QString &path, result) {
312             if (rx.exactMatch(path)) {
313                 pathValue = path.section('.', 0, -2);
314                 pathValue.remove(0, filter.size());
315                 sortedList.insert(pathValue.toInt(), path);
316             }
317         }
318         *count = sortedList.size();
319         if (*count == 0) kDebug() << "No IMAGE FOUND!!!!!!!";
320         else  {
321             QMapIterator<int, QString> i(sortedList);
322             i.next();
323             QString result = i.value();
324             result.remove(0, filter.size());
325             result = result.section('.', 0, -2);
326             precision = result.size();
327         }
328         extension = filter + "%." + QString::number(precision) + "d" + ext;
329     }
330     return  folder + extension;
331 }
332
333
334 QString SlideshowClip::clipName() const
335 {
336     return m_view.clip_name->text();
337 }
338
339 QString SlideshowClip::clipDuration() const
340 {
341     if (m_view.clip_duration_format->currentIndex() == 1) {
342         // we are in frames mode
343         return m_timecode.getTimecodeFromFrames(m_view.clip_duration_frames->value());
344     }
345     return m_view.clip_duration->text();
346 }
347
348 int SlideshowClip::imageCount() const
349 {
350     return m_count;
351 }
352
353 int SlideshowClip::softness() const
354 {
355     return m_view.luma_softness->value();
356 }
357
358 bool SlideshowClip::loop() const
359 {
360     return m_view.slide_loop->isChecked();
361 }
362
363 bool SlideshowClip::crop() const
364 {
365     return m_view.slide_crop->isChecked();
366 }
367
368 bool SlideshowClip::fade() const
369 {
370     return m_view.slide_fade->isChecked();
371 }
372
373 QString SlideshowClip::lumaDuration() const
374 {
375     if (m_view.clip_duration_format->currentIndex() == 1) {
376         // we are in frames mode
377         return m_timecode.getTimecodeFromFrames(m_view.luma_duration_frames->value());
378     }
379     return m_view.luma_duration->text();
380 }
381
382 QString SlideshowClip::lumaFile() const
383 {
384     if (!m_view.luma_fade->isChecked() || !m_view.luma_file->isEnabled()) return QString();
385     return m_view.luma_file->itemData(m_view.luma_file->currentIndex()).toString();
386 }
387
388 QString SlideshowClip::animation() const
389 {
390     if (m_view.animation->itemData(m_view.animation->currentIndex()).isNull()) return QString();
391     return m_view.animation->itemData(m_view.animation->currentIndex()).toString();
392 }
393
394 void SlideshowClip::slotUpdateDurationFormat(int ix)
395 {
396     bool framesFormat = ix == 1;
397     if (framesFormat) {
398         // switching to frames count, update widget
399         m_view.clip_duration_frames->setValue(m_timecode.getFrameCount(m_view.clip_duration->text()));
400         m_view.luma_duration_frames->setValue(m_timecode.getFrameCount(m_view.luma_duration->text()));
401     } else {
402         // switching to timecode format
403         m_view.clip_duration->setInputMask("");
404         m_view.clip_duration->setValidator(m_timecode.validator());
405         m_view.clip_duration->setText(m_timecode.getTimecodeFromFrames(m_view.clip_duration_frames->value()));
406         m_view.luma_duration->setInputMask("");
407         m_view.luma_duration->setValidator(m_timecode.validator());
408         m_view.luma_duration->setText(m_timecode.getTimecodeFromFrames(m_view.luma_duration_frames->value()));
409     }
410     m_view.clip_duration_frames->setHidden(!framesFormat);
411     m_view.clip_duration->setHidden(framesFormat);
412     m_view.luma_duration_frames->setHidden(!framesFormat);
413     m_view.luma_duration->setHidden(framesFormat);
414 }
415
416 void SlideshowClip::slotMethodChanged(bool active)
417 {
418     if (active) {
419         // User wants mimetype image sequence
420         m_view.stackedWidget->setCurrentIndex(0);
421         KdenliveSettings::setSlideshowbymime(true);
422     } else {
423         // User wants pattern image sequence
424         m_view.stackedWidget->setCurrentIndex(1);
425         KdenliveSettings::setSlideshowbymime(false);
426     }
427     parseFolder();
428 }
429
430 // static
431 QString SlideshowClip::animationToGeometry(const QString &animation, int &ttl)
432 {
433     QString geometry;
434     if (animation.startsWith("Pan and zoom")) {
435         geometry = QString().sprintf("0=0,0:100%%x100%%;%d=-14%%,-14%%:120%%x120%%;%d=-5%%,-5%%:110%%x110%%;%d=0,0:110%%x110%%;%d=0,-5%%:110%%x110%%;%d=-5%%,0:110%%x110%%",
436                                      ttl-1, ttl, ttl*2 - 1, ttl*2, ttl*3 - 1 );
437         ttl *= 3;
438     } else if (animation.startsWith("Pan")) {
439         geometry = QString().sprintf("0=-5%%,-5%%:110%%x110%%;%d=0,0:110%%x110%%;%d=0,0:110%%x110%%;%d=0,-5%%:110%%x110%%;%d=0,-5%%:110%%x110%%;%d=-5%%,-5%%:110%%x110%%;%d=0,-5%%:110%%x110%%;%d=-5%%,0:110%%x110%%",
440                                      ttl-1, ttl, ttl*2 - 1, ttl*2, ttl*3 - 1, ttl*3, ttl*4 - 1 );
441         ttl *= 4;
442     } else if (animation.startsWith("Zoom")) {
443         geometry = QString().sprintf("0=0,0:100%%x100%%;%d=-14%%,-14%%:120%%x120%%",
444                                      ttl-1, ttl );
445     }
446     return geometry;
447 }
448
449 #include "slideshowclip.moc"
450
451