]> git.sesse.net Git - kdenlive/blob - src/slideshowclip.cpp
Fix label
[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(const 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(QString)), this, SLOT(parseFolder()));
46     connect(m_view.image_type, SIGNAL(currentIndexChanged(int)), this, SLOT(parseFolder()));
47     connect(m_view.pattern_url, SIGNAL(textChanged(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(m_timecode.mask());
72     m_view.luma_duration->setInputMask(m_timecode.mask());
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     delete m_thumbJob;
116 }
117
118 void SlideshowClip::slotEnableLuma(int state)
119 {
120     bool enable = false;
121     if (state == Qt::Checked) enable = true;
122     m_view.luma_duration->setEnabled(enable);
123     m_view.luma_duration_frames->setEnabled(enable);
124     m_view.luma_fade->setEnabled(enable);
125     if (enable) {
126         m_view.luma_file->setEnabled(m_view.luma_fade->isChecked());
127     } else m_view.luma_file->setEnabled(false);
128     m_view.label_softness->setEnabled(m_view.luma_fade->isChecked() && enable);
129     m_view.luma_softness->setEnabled(m_view.label_softness->isEnabled());
130 }
131
132 void SlideshowClip::slotEnableThumbs(int state)
133 {
134     if (state == Qt::Checked) {
135         KdenliveSettings::setShowslideshowthumbs(true);
136         slotGenerateThumbs();
137     } else {
138         KdenliveSettings::setShowslideshowthumbs(false);
139         if (m_thumbJob) {
140             disconnect(m_thumbJob, SIGNAL(gotPreview(KFileItem,QPixmap)), this, SLOT(slotSetPixmap(KFileItem,QPixmap)));
141             m_thumbJob->kill();
142             m_thumbJob = NULL;
143         }
144     }
145
146 }
147
148 void SlideshowClip::slotEnableLumaFile(int state)
149 {
150     bool enable = false;
151     if (state == Qt::Checked) enable = true;
152     m_view.luma_file->setEnabled(enable);
153     m_view.luma_softness->setEnabled(enable);
154     m_view.label_softness->setEnabled(enable);
155 }
156
157 void SlideshowClip::parseFolder()
158 {
159     m_view.icon_list->clear();
160     bool isMime = m_view.method_mime->isChecked();
161     QString path = isMime ? m_view.folder_url->url().path() : m_view.pattern_url->url().directory();
162     QDir dir(path);
163     if (path.isEmpty() || !dir.exists()) {
164         m_count = 0;
165         m_view.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
166         m_view.label_info->setText(QString());
167         return;
168     }
169
170     KIcon unknownicon("unknown");
171     QStringList result;
172     QStringList filters;
173     QString filter;
174     if (isMime) {
175         // TODO: improve jpeg image detection with extension like jpeg, requires change in MLT image producers
176         filter = m_view.image_type->itemData(m_view.image_type->currentIndex()).toString();
177         filters << "*." + filter;
178         dir.setNameFilters(filters);
179         result = dir.entryList(QDir::Files);
180     } else {
181         // find pattern
182         filter = m_view.pattern_url->url().fileName();
183         QString ext = '.' + filter.section('.', -1);
184         filter = filter.section('.', 0, -2);
185         int fullSize = filter.size();
186         while (filter.at(filter.size() - 1).isDigit()) {
187             filter.chop(1);
188         }
189         int precision = fullSize - filter.size();
190         int firstFrame = m_view.pattern_url->url().fileName().section('.', 0, -2).right(precision).toInt();
191         QString path;
192         int gap = 0;
193         for (int i = firstFrame; gap < 100; ++i) {
194             path = filter + QString::number(i).rightJustified(precision, '0', false) + ext;
195             if (dir.exists(path)) {
196                 result.append(path);
197                 gap = 0;
198             } else {
199                 gap++;
200             }
201         }
202     }
203     QListWidgetItem *item;
204     foreach(const QString & path, result) {
205         item = new QListWidgetItem(unknownicon, KUrl(path).fileName());
206         item->setData(Qt::UserRole, dir.filePath(path));
207         m_view.icon_list->addItem(item);
208     }
209     m_count = m_view.icon_list->count();
210     m_view.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(m_count > 0);
211     m_view.label_info->setText(i18np("1 image found", "%1 images found", m_count));
212     if (m_view.show_thumbs->isChecked()) slotGenerateThumbs();
213     m_view.icon_list->setCurrentRow(0);
214 }
215
216 void SlideshowClip::slotGenerateThumbs()
217 {
218     if (m_thumbJob) {
219         delete m_thumbJob;
220     };
221     KFileItemList fileList;
222     for (int i = 0; i < m_view.icon_list->count(); ++i) {
223         QListWidgetItem* item = m_view.icon_list->item(i);
224         if (item) {
225             QString path = item->data(Qt::UserRole).toString();
226             if (!path.isEmpty()) {
227                 fileList.append(KFileItem(KFileItem::Unknown, KFileItem::Unknown, KUrl(path)));
228             }
229         }
230     }
231 #if KDE_IS_VERSION(4,7,0)
232     m_thumbJob = new KIO::PreviewJob(fileList, QSize(50, 50));
233     m_thumbJob->setScaleType(KIO::PreviewJob::Scaled);
234 #else
235     m_thumbJob = new KIO::PreviewJob(fileList, 50, 0, 0, 0, true, false, 0);
236 #endif
237
238     m_thumbJob->setAutoDelete(false);
239     connect(m_thumbJob, SIGNAL(gotPreview(KFileItem,QPixmap)), this, SLOT(slotSetPixmap(KFileItem,QPixmap)));
240     m_thumbJob->start();
241 }
242
243 void SlideshowClip::slotSetPixmap(const KFileItem &fileItem, const QPixmap &pix)
244 {
245     for (int i = 0; i < m_view.icon_list->count(); ++i) {
246         QListWidgetItem* item = m_view.icon_list->item(i);
247         if (item) {
248             QString path = item->data(Qt::UserRole).toString();
249             if (path == fileItem.url().path()) {
250                 item->setIcon(KIcon(pix));
251                 item->setData(Qt::UserRole, QString());
252                 break;
253             }
254         }
255     }
256 }
257
258
259 QString SlideshowClip::selectedPath()
260 {
261     QStringList list;
262     KUrl url;
263     if (m_view.method_mime->isChecked()) url = m_view.folder_url->url();
264     else url = m_view.pattern_url->url();
265     QString path = selectedPath(url, m_view.method_mime->isChecked(), ".all." + m_view.image_type->itemData(m_view.image_type->currentIndex()).toString(), &list);
266     m_count = list.count();
267     kDebug()<<"// SELECTED PATH: "<<path;
268     return path;
269 }
270
271 // static
272 int SlideshowClip::getFrameNumberFromPath(const KUrl &path)
273 {
274     QString filter = path.fileName();
275     filter = filter.section('.', 0, -2);
276     int ix = filter.size() - 1;
277     while (filter.at(ix).isDigit()) {
278         ix--;
279     }
280     return filter.remove(0, ix + 1).toInt();
281 }
282
283 // static
284 QString SlideshowClip::selectedPath(const KUrl &url, bool isMime, QString extension, QStringList *list)
285 {
286     QString folder;
287     if (isMime) {
288         folder = url.path(KUrl::AddTrailingSlash);
289         // Check how many files we have
290         QDir dir(folder);
291         QStringList filters;
292         filters << "*." + extension.section('.', -1);
293         dir.setNameFilters(filters);
294         *list = dir.entryList(QDir::Files);
295     } else {
296         folder = url.directory(KUrl::AppendTrailingSlash);
297         QString filter = url.fileName();
298         QString ext = '.' + filter.section('.', -1);
299         filter = filter.section('.', 0, -2);
300         int fullSize = filter.size();
301         QString firstFrameData = filter;
302
303         while (filter.at(filter.size() - 1).isDigit()) {
304             filter.chop(1);
305         }
306
307         // Find number of digits in sequence
308         int precision = fullSize - filter.size();
309         int firstFrame = firstFrameData.right(precision).toInt();
310
311         // Check how many files we have
312         QDir dir(folder);
313         QString path;
314         int gap = 0;
315         for (int i = firstFrame; gap < 100; ++i) {
316             path = filter + QString::number(i).rightJustified(precision, '0', false) + ext;
317             if (dir.exists(path)) {
318                 (*list).append(folder + path);
319                 gap = 0;
320             } else {
321                 gap++;
322             }
323         }
324         extension = filter + "%0" + QString::number(precision) + 'd' + ext;
325         if (firstFrame > 0) extension.append(QString("?begin:%1").arg(firstFrame));
326     }
327     kDebug() << "// FOUND " << (*list).count() << " items for " << url.path();
328     return  folder + extension;
329 }
330
331
332 QString SlideshowClip::clipName() const
333 {
334     return m_view.clip_name->text();
335 }
336
337 QString SlideshowClip::clipDuration() const
338 {
339     if (m_view.clip_duration_format->currentIndex() == 1) {
340         // we are in frames mode
341         return m_timecode.getTimecodeFromFrames(m_view.clip_duration_frames->value());
342     }
343     return m_view.clip_duration->text();
344 }
345
346 int SlideshowClip::imageCount() const
347 {
348     return m_count;
349 }
350
351 int SlideshowClip::softness() const
352 {
353     return m_view.luma_softness->value();
354 }
355
356 bool SlideshowClip::loop() const
357 {
358     return m_view.slide_loop->isChecked();
359 }
360
361 bool SlideshowClip::crop() const
362 {
363     return m_view.slide_crop->isChecked();
364 }
365
366 bool SlideshowClip::fade() const
367 {
368     return m_view.slide_fade->isChecked();
369 }
370
371 QString SlideshowClip::lumaDuration() const
372 {
373     if (m_view.clip_duration_format->currentIndex() == 1) {
374         // we are in frames mode
375         return m_timecode.getTimecodeFromFrames(m_view.luma_duration_frames->value());
376     }
377     return m_view.luma_duration->text();
378 }
379
380 QString SlideshowClip::lumaFile() const
381 {
382     if (!m_view.luma_fade->isChecked() || !m_view.luma_file->isEnabled()) return QString();
383     return m_view.luma_file->itemData(m_view.luma_file->currentIndex()).toString();
384 }
385
386 QString SlideshowClip::animation() const
387 {
388     if (m_view.animation->itemData(m_view.animation->currentIndex()).isNull()) return QString();
389     return m_view.animation->itemData(m_view.animation->currentIndex()).toString();
390 }
391
392 void SlideshowClip::slotUpdateDurationFormat(int ix)
393 {
394     bool framesFormat = ix == 1;
395     if (framesFormat) {
396         // switching to frames count, update widget
397         m_view.clip_duration_frames->setValue(m_timecode.getFrameCount(m_view.clip_duration->text()));
398         m_view.luma_duration_frames->setValue(m_timecode.getFrameCount(m_view.luma_duration->text()));
399     } else {
400         // switching to timecode format
401         m_view.clip_duration->setText(m_timecode.getTimecodeFromFrames(m_view.clip_duration_frames->value()));
402         m_view.luma_duration->setText(m_timecode.getTimecodeFromFrames(m_view.luma_duration_frames->value()));
403     }
404     m_view.clip_duration_frames->setHidden(!framesFormat);
405     m_view.clip_duration->setHidden(framesFormat);
406     m_view.luma_duration_frames->setHidden(!framesFormat);
407     m_view.luma_duration->setHidden(framesFormat);
408 }
409
410 void SlideshowClip::slotMethodChanged(bool active)
411 {
412     if (active) {
413         // User wants mimetype image sequence
414         m_view.clip_duration->setText(m_timecode.reformatSeparators(KdenliveSettings::image_duration()));
415         m_view.stackedWidget->setCurrentIndex(0);
416         KdenliveSettings::setSlideshowbymime(true);
417     } else {
418         // User wants pattern image sequence
419         m_view.clip_duration->setText(m_timecode.reformatSeparators(KdenliveSettings::sequence_duration()));
420         m_view.stackedWidget->setCurrentIndex(1);
421         KdenliveSettings::setSlideshowbymime(false);
422     }
423     parseFolder();
424 }
425
426 // static
427 QString SlideshowClip::animationToGeometry(const QString &animation, int &ttl)
428 {
429     QString geometry;
430     if (animation.startsWith("Pan and zoom")) {
431         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%%",
432                                      ttl - 1, ttl, ttl * 2 - 1, ttl * 2, ttl * 3 - 1);
433         ttl *= 3;
434     } else if (animation.startsWith("Pan")) {
435         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%%",
436                                      ttl - 1, ttl, ttl * 2 - 1, ttl * 2, ttl * 3 - 1, ttl * 3, ttl * 4 - 1);
437         ttl *= 4;
438     } else if (animation.startsWith("Zoom")) {
439         geometry = QString().sprintf("0=0/0:100%%x100%%;%d=-14%%/-14%%:120%%x120%%", ttl - 1);
440     }
441     return geometry;
442 }
443
444
445 #include "slideshowclip.moc"
446
447