]> git.sesse.net Git - kdenlive/blob - src/slideshowclip.cpp
Progress on stopmotion widget (make it possible to switch between HDMI and V4L)
[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.luma_duration->setInputMask("");
72     m_view.luma_duration->setValidator(m_timecode.validator());
73     m_view.luma_duration->setText(m_timecode.getTimecodeFromFrames(int(ceil(m_timecode.fps()))));
74     m_view.folder_url->setUrl(QDir::homePath());
75
76     m_view.clip_duration_format->addItem(i18n("hh:mm:ss::ff"));
77     m_view.clip_duration_format->addItem(i18n("Frames"));
78     connect(m_view.clip_duration_format, SIGNAL(activated(int)), this, SLOT(slotUpdateDurationFormat(int)));
79     m_view.clip_duration_frames->setHidden(true);
80     m_view.luma_duration_frames->setHidden(true);
81     m_view.method_mime->setChecked(KdenliveSettings::slideshowbymime());
82     connect(m_view.method_mime, SIGNAL(toggled(bool)), this, SLOT(slotMethodChanged(bool)));
83     slotMethodChanged(m_view.method_mime->isChecked());
84
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     int count = 0;
164     QString filter = file.fileName();
165     QString ext = filter.section('.', -1);
166     filter = filter.section('.', 0, -2);
167     int fullSize = filter.size();
168     bool hasDigit = false;
169     while (filter.at(filter.size() - 1).isDigit()) {
170         hasDigit = true;
171         filter.remove(filter.size() - 1, 1);
172     }
173     if (!hasDigit) return 0;
174
175
176     // Find number of digits in sequence
177     int precision = fullSize - filter.size();
178     QString folder = file.directory(KUrl::AppendTrailingSlash);
179     // Check how many files we have
180     QDir dir(folder);
181     QString path;
182     int gap = 0;
183     for (int i = 0; gap < 100; i++) {
184         path = filter + QString::number(i).rightJustified(precision, '0', false) + ext;
185         if (dir.exists(path)) {
186             count ++;
187             gap = 0;
188         } else {
189             gap++;
190         }
191     }
192     return count;
193 }
194
195 void SlideshowClip::parseFolder()
196 {
197     m_view.icon_list->clear();
198     bool isMime = m_view.method_mime->isChecked();
199     QString path = isMime ? m_view.folder_url->url().path() : m_view.pattern_url->url().directory();
200     QDir dir(path);
201     if (path.isEmpty() || !dir.exists()) return;
202
203     KIcon unknownicon("unknown");
204     QStringList result;
205     QStringList filters;
206     QString filter;
207     if (isMime) {
208         // TODO: improve jpeg image detection with extension like jpeg, requires change in MLT image producers
209         filter = m_view.image_type->itemData(m_view.image_type->currentIndex()).toString();
210         filters << "*." + filter;
211         dir.setNameFilters(filters);
212         result = dir.entryList(QDir::Files);
213     } else {
214         // find pattern
215         filter = m_view.pattern_url->url().fileName();
216         QString ext = filter.section('.', -1);
217         filter = filter.section('.', 0, -2);
218         int fullSize = filter.size();
219         while (filter.at(filter.size() - 1).isDigit()) {
220             filter.remove(filter.size() - 1, 1);
221         }
222         int precision = fullSize - filter.size();
223         QString path;
224         int gap = 0;
225         for (int i = 0; gap < 100; i++) {
226             path = filter + QString::number(i).rightJustified(precision, '0', false) + ext;
227             if (dir.exists(path)) {
228                 result.append(path);
229                 gap = 0;
230             } else {
231                 gap++;
232             }
233         }
234     }
235     QListWidgetItem *item;
236     foreach(const QString & path, result) {
237         item = new QListWidgetItem(unknownicon, KUrl(path).fileName());
238         item->setData(Qt::UserRole, dir.filePath(path));
239         m_view.icon_list->addItem(item);
240     }
241     m_count = result.count();
242     if (m_count == 0) m_view.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
243     else m_view.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
244     m_view.label_info->setText(i18np("1 image found", "%1 images found", m_count));
245     if (m_view.show_thumbs->isChecked()) slotGenerateThumbs();
246     m_view.icon_list->setCurrentRow(0);
247 }
248
249 void SlideshowClip::slotGenerateThumbs()
250 {
251     if (m_thumbJob) {
252         delete m_thumbJob;
253     };
254     KFileItemList fileList;
255     for (int i = 0; i < m_view.icon_list->count(); i++) {
256         QListWidgetItem* item = m_view.icon_list->item(i);
257         if (item) {
258             QString path = item->data(Qt::UserRole).toString();
259             if (!path.isEmpty()) {
260                 fileList.append(KFileItem(KFileItem::Unknown, KFileItem::Unknown, KUrl(path)));
261             }
262         }
263     }
264     m_thumbJob = new KIO::PreviewJob(fileList, 50, 0, 0, 0, true, true, 0);
265     m_thumbJob->setAutoDelete(false);
266     connect(m_thumbJob, SIGNAL(gotPreview(const KFileItem &, const QPixmap &)), this, SLOT(slotSetPixmap(const KFileItem &, const QPixmap &)));
267     m_thumbJob->start();
268 }
269
270 void SlideshowClip::slotSetPixmap(const KFileItem &fileItem, const QPixmap &pix)
271 {
272     for (int i = 0; i < m_view.icon_list->count(); i++) {
273         QListWidgetItem* item = m_view.icon_list->item(i);
274         if (item) {
275             QString path = item->data(Qt::UserRole).toString();
276             if (path == fileItem.url().path()) {
277                 item->setIcon(KIcon(pix));
278                 item->setData(Qt::UserRole, QString());
279                 break;
280             }
281         }
282     }
283 }
284
285
286 QString SlideshowClip::selectedPath()
287 {
288     QStringList list;
289     QString path = selectedPath(m_view.folder_url->url(), m_view.method_mime->isChecked(), ".all." + m_view.image_type->itemData(m_view.image_type->currentIndex()).toString(), &list);
290     m_count = list.count();
291     return path;
292
293
294 }
295
296 // static
297 int SlideshowClip::getFrameNumberFromPath(KUrl path)
298 {
299     QString filter = path.fileName();
300     filter = filter.section('.', 0, -2);
301     int ix = filter.size() - 1;
302     while (filter.at(ix).isDigit()) {
303         ix--;
304     }
305     return filter.remove(0, ix + 1).toInt();
306 }
307
308 // static
309 QString SlideshowClip::selectedPath(KUrl url, bool isMime, QString extension, QStringList *list)
310 {
311     QString folder;
312     if (isMime) {
313         folder = url.path(KUrl::AddTrailingSlash);
314     } else {
315         folder = url.directory(KUrl::AppendTrailingSlash);
316         QString filter = url.fileName();
317         QString ext = '.' + filter.section('.', -1);
318         filter = filter.section('.', 0, -2);
319         int fullSize = filter.size();
320
321         while (filter.at(filter.size() - 1).isDigit()) {
322             filter.chop(1);
323         }
324
325         // Find number of digits in sequence
326         int precision = fullSize - filter.size();
327
328         // Check how many files we have
329         QDir dir(folder);
330         QString path;
331         int gap = 0;
332         for (int i = 0; gap < 100; i++) {
333             path = filter + QString::number(i).rightJustified(precision, '0', false) + ext;
334             if (dir.exists(path)) {
335                 (*list).append(folder + path);
336                 gap = 0;
337             } else {
338                 gap++;
339             }
340         }
341         extension = filter + "%." + QString::number(precision) + "d" + ext;
342     }
343     kDebug() << "// FOUND " << (*list).count() << " items for " << url.path();
344     return  folder + extension;
345 }
346
347
348 QString SlideshowClip::clipName() const
349 {
350     return m_view.clip_name->text();
351 }
352
353 QString SlideshowClip::clipDuration() const
354 {
355     if (m_view.clip_duration_format->currentIndex() == 1) {
356         // we are in frames mode
357         return m_timecode.getTimecodeFromFrames(m_view.clip_duration_frames->value());
358     }
359     return m_view.clip_duration->text();
360 }
361
362 int SlideshowClip::imageCount() const
363 {
364     return m_count;
365 }
366
367 int SlideshowClip::softness() const
368 {
369     return m_view.luma_softness->value();
370 }
371
372 bool SlideshowClip::loop() const
373 {
374     return m_view.slide_loop->isChecked();
375 }
376
377 bool SlideshowClip::crop() const
378 {
379     return m_view.slide_crop->isChecked();
380 }
381
382 bool SlideshowClip::fade() const
383 {
384     return m_view.slide_fade->isChecked();
385 }
386
387 QString SlideshowClip::lumaDuration() const
388 {
389     if (m_view.clip_duration_format->currentIndex() == 1) {
390         // we are in frames mode
391         return m_timecode.getTimecodeFromFrames(m_view.luma_duration_frames->value());
392     }
393     return m_view.luma_duration->text();
394 }
395
396 QString SlideshowClip::lumaFile() const
397 {
398     if (!m_view.luma_fade->isChecked() || !m_view.luma_file->isEnabled()) return QString();
399     return m_view.luma_file->itemData(m_view.luma_file->currentIndex()).toString();
400 }
401
402 QString SlideshowClip::animation() const
403 {
404     if (m_view.animation->itemData(m_view.animation->currentIndex()).isNull()) return QString();
405     return m_view.animation->itemData(m_view.animation->currentIndex()).toString();
406 }
407
408 void SlideshowClip::slotUpdateDurationFormat(int ix)
409 {
410     bool framesFormat = ix == 1;
411     if (framesFormat) {
412         // switching to frames count, update widget
413         m_view.clip_duration_frames->setValue(m_timecode.getFrameCount(m_view.clip_duration->text()));
414         m_view.luma_duration_frames->setValue(m_timecode.getFrameCount(m_view.luma_duration->text()));
415     } else {
416         // switching to timecode format
417         m_view.clip_duration->setInputMask("");
418         m_view.clip_duration->setValidator(m_timecode.validator());
419         m_view.clip_duration->setText(m_timecode.getTimecodeFromFrames(m_view.clip_duration_frames->value()));
420         m_view.luma_duration->setInputMask("");
421         m_view.luma_duration->setValidator(m_timecode.validator());
422         m_view.luma_duration->setText(m_timecode.getTimecodeFromFrames(m_view.luma_duration_frames->value()));
423     }
424     m_view.clip_duration_frames->setHidden(!framesFormat);
425     m_view.clip_duration->setHidden(framesFormat);
426     m_view.luma_duration_frames->setHidden(!framesFormat);
427     m_view.luma_duration->setHidden(framesFormat);
428 }
429
430 void SlideshowClip::slotMethodChanged(bool active)
431 {
432     if (active) {
433         // User wants mimetype image sequence
434         if (m_view.clip_duration->text().isEmpty()) {
435             m_view.clip_duration->setText(m_timecode.reformatSeparators(KdenliveSettings::image_duration()));
436         }
437         m_view.stackedWidget->setCurrentIndex(0);
438         KdenliveSettings::setSlideshowbymime(true);
439     } else {
440         // User wants pattern image sequence
441         if (m_view.clip_duration->text().isEmpty()) {
442             m_view.clip_duration->setText(m_timecode.reformatSeparators(KdenliveSettings::sequence_duration()));
443         }
444         m_view.stackedWidget->setCurrentIndex(1);
445         KdenliveSettings::setSlideshowbymime(false);
446     }
447     parseFolder();
448 }
449
450 // static
451 QString SlideshowClip::animationToGeometry(const QString &animation, int &ttl)
452 {
453     QString geometry;
454     if (animation.startsWith("Pan and zoom")) {
455         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%%",
456                                      ttl - 1, ttl, ttl * 2 - 1, ttl * 2, ttl * 3 - 1);
457         ttl *= 3;
458     } else if (animation.startsWith("Pan")) {
459         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%%",
460                                      ttl - 1, ttl, ttl * 2 - 1, ttl * 2, ttl * 3 - 1, ttl * 3, ttl * 4 - 1);
461         ttl *= 4;
462     } else if (animation.startsWith("Zoom")) {
463         geometry = QString().sprintf("0=0,0:100%%x100%%;%d=-14%%,-14%%:120%%x120%%", ttl - 1);
464     }
465     return geometry;
466 }
467
468 #include "slideshowclip.moc"
469
470