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