1 /***************************************************************************
2 * Copyright (C) 2008 by Jean-Baptiste Mardelle (jb@kdenlive.org) *
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. *
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. *
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 ***************************************************************************/
20 #include "slideshowclip.h"
21 #include "kdenlivesettings.h"
23 #include <KStandardDirs>
26 #include <kdeversion.h>
31 SlideshowClip::SlideshowClip(Timecode tc, QWidget * parent) :
37 setFont(KGlobalSettings::toolBarFont());
38 setWindowTitle(i18n("Add Slideshow Clip"));
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());
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()));
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)));
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");
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());
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());
86 // Check for Kdenlive installed luma files
88 filters << "*.pgm" << "*.png";
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);
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);
113 SlideshowClip::~SlideshowClip()
120 void SlideshowClip::slotEnableLuma(int state)
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);
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());
134 void SlideshowClip::slotEnableThumbs(int state)
136 if (state == Qt::Checked) {
137 KdenliveSettings::setShowslideshowthumbs(true);
138 slotGenerateThumbs();
140 KdenliveSettings::setShowslideshowthumbs(false);
142 disconnect(m_thumbJob, SIGNAL(gotPreview(const KFileItem &, const QPixmap &)), this, SLOT(slotSetPixmap(const KFileItem &, const QPixmap &)));
150 void SlideshowClip::slotEnableLumaFile(int state)
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);
160 //TODO: sequence begin
161 int SlideshowClip::sequenceCount(KUrl file)
165 QString filter = file.fileName();
166 QString ext = filter.section('.', -1);
167 filter = filter.section('.', 0, -2);
168 int fullSize = filter.size();
169 bool hasDigit = false;
170 while (filter.at(filter.size() - 1).isDigit()) {
172 filter.remove(filter.size() - 1, 1);
174 if (!hasDigit) return 0;
177 // Find number of digits in sequence
178 int precision = fullSize - filter.size();
179 int firstFrame = file.fileName().section('.', 0, -2).right(precision).toInt();
180 QString folder = file.directory(KUrl::AppendTrailingSlash);
181 // Check how many files we have
185 for (int i = firstFrame; gap < 100; i++) {
186 path = filter + QString::number(i).rightJustified(precision, '0', false) + ext;
187 if (dir.exists(path)) {
197 void SlideshowClip::parseFolder()
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();
203 if (path.isEmpty() || !dir.exists()) {
205 m_view.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
206 m_view.label_info->setText(QString());
210 KIcon unknownicon("unknown");
215 // TODO: improve jpeg image detection with extension like jpeg, requires change in MLT image producers
216 filter = m_view.image_type->itemData(m_view.image_type->currentIndex()).toString();
217 filters << "*." + filter;
218 dir.setNameFilters(filters);
219 result = dir.entryList(QDir::Files);
222 filter = m_view.pattern_url->url().fileName();
223 QString ext = '.' + filter.section('.', -1);
224 filter = filter.section('.', 0, -2);
225 int fullSize = filter.size();
226 while (filter.at(filter.size() - 1).isDigit()) {
229 int precision = fullSize - filter.size();
230 int firstFrame = m_view.pattern_url->url().fileName().section('.', 0, -2).right(precision).toInt();
233 for (int i = firstFrame; gap < 100; i++) {
234 path = filter + QString::number(i).rightJustified(precision, '0', false) + ext;
235 if (dir.exists(path)) {
243 QListWidgetItem *item;
244 foreach(const QString & path, result) {
245 item = new QListWidgetItem(unknownicon, KUrl(path).fileName());
246 item->setData(Qt::UserRole, dir.filePath(path));
247 m_view.icon_list->addItem(item);
249 m_count = m_view.icon_list->count();
250 m_view.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(m_count > 0);
251 m_view.label_info->setText(i18np("1 image found", "%1 images found", m_count));
252 if (m_view.show_thumbs->isChecked()) slotGenerateThumbs();
253 m_view.icon_list->setCurrentRow(0);
256 void SlideshowClip::slotGenerateThumbs()
261 KFileItemList fileList;
262 for (int i = 0; i < m_view.icon_list->count(); i++) {
263 QListWidgetItem* item = m_view.icon_list->item(i);
265 QString path = item->data(Qt::UserRole).toString();
266 if (!path.isEmpty()) {
267 fileList.append(KFileItem(KFileItem::Unknown, KFileItem::Unknown, KUrl(path)));
271 #if KDE_IS_VERSION(4,7,0)
272 m_thumbJob = new KIO::PreviewJob(fileList, QSize(50, 50));
273 m_thumbJob->setScaleType(KIO::PreviewJob::Scaled);
275 m_thumbJob = new KIO::PreviewJob(fileList, 50, 0, 0, 0, true, false, 0);
278 m_thumbJob->setAutoDelete(false);
279 connect(m_thumbJob, SIGNAL(gotPreview(const KFileItem &, const QPixmap &)), this, SLOT(slotSetPixmap(const KFileItem &, const QPixmap &)));
283 void SlideshowClip::slotSetPixmap(const KFileItem &fileItem, const QPixmap &pix)
285 for (int i = 0; i < m_view.icon_list->count(); i++) {
286 QListWidgetItem* item = m_view.icon_list->item(i);
288 QString path = item->data(Qt::UserRole).toString();
289 if (path == fileItem.url().path()) {
290 item->setIcon(KIcon(pix));
291 item->setData(Qt::UserRole, QString());
299 QString SlideshowClip::selectedPath()
303 if (m_view.method_mime->isChecked()) url = m_view.folder_url->url();
304 else url = m_view.pattern_url->url();
305 QString path = selectedPath(url, m_view.method_mime->isChecked(), ".all." + m_view.image_type->itemData(m_view.image_type->currentIndex()).toString(), &list);
306 m_count = list.count();
307 kDebug()<<"// SELECTED PATH: "<<path;
312 int SlideshowClip::getFrameNumberFromPath(KUrl path)
314 QString filter = path.fileName();
315 filter = filter.section('.', 0, -2);
316 int ix = filter.size() - 1;
317 while (filter.at(ix).isDigit()) {
320 return filter.remove(0, ix + 1).toInt();
324 QString SlideshowClip::selectedPath(KUrl url, bool isMime, QString extension, QStringList *list)
328 folder = url.path(KUrl::AddTrailingSlash);
329 // Check how many files we have
332 filters << "*." + extension.section('.', -1);
333 dir.setNameFilters(filters);
334 *list = dir.entryList(QDir::Files);
336 folder = url.directory(KUrl::AppendTrailingSlash);
337 QString filter = url.fileName();
338 QString ext = '.' + filter.section('.', -1);
339 filter = filter.section('.', 0, -2);
340 int fullSize = filter.size();
341 QString firstFrameData = filter;
343 while (filter.at(filter.size() - 1).isDigit()) {
347 // Find number of digits in sequence
348 int precision = fullSize - filter.size();
349 int firstFrame = firstFrameData.right(precision).toInt();
351 // Workaround bug in MLT image sequence detection
352 if (firstFrame < 3) firstFrame = 0;
354 // Check how many files we have
358 for (int i = firstFrame; gap < 100; i++) {
359 path = filter + QString::number(i).rightJustified(precision, '0', false) + ext;
360 if (dir.exists(path)) {
361 (*list).append(folder + path);
367 if (firstFrame > 0) extension = filter + '%' + QString::number(firstFrame).rightJustified(precision, '0', false) + 'd' + ext;
368 else extension = filter + "%0" + QString::number(precision) + 'd' + ext;
370 kDebug() << "// FOUND " << (*list).count() << " items for " << url.path();
371 return folder + extension;
375 QString SlideshowClip::clipName() const
377 return m_view.clip_name->text();
380 QString SlideshowClip::clipDuration() const
382 if (m_view.clip_duration_format->currentIndex() == 1) {
383 // we are in frames mode
384 return m_timecode.getTimecodeFromFrames(m_view.clip_duration_frames->value());
386 return m_view.clip_duration->text();
389 int SlideshowClip::imageCount() const
394 int SlideshowClip::softness() const
396 return m_view.luma_softness->value();
399 bool SlideshowClip::loop() const
401 return m_view.slide_loop->isChecked();
404 bool SlideshowClip::crop() const
406 return m_view.slide_crop->isChecked();
409 bool SlideshowClip::fade() const
411 return m_view.slide_fade->isChecked();
414 QString SlideshowClip::lumaDuration() const
416 if (m_view.clip_duration_format->currentIndex() == 1) {
417 // we are in frames mode
418 return m_timecode.getTimecodeFromFrames(m_view.luma_duration_frames->value());
420 return m_view.luma_duration->text();
423 QString SlideshowClip::lumaFile() const
425 if (!m_view.luma_fade->isChecked() || !m_view.luma_file->isEnabled()) return QString();
426 return m_view.luma_file->itemData(m_view.luma_file->currentIndex()).toString();
429 QString SlideshowClip::animation() const
431 if (m_view.animation->itemData(m_view.animation->currentIndex()).isNull()) return QString();
432 return m_view.animation->itemData(m_view.animation->currentIndex()).toString();
435 void SlideshowClip::slotUpdateDurationFormat(int ix)
437 bool framesFormat = ix == 1;
439 // switching to frames count, update widget
440 m_view.clip_duration_frames->setValue(m_timecode.getFrameCount(m_view.clip_duration->text()));
441 m_view.luma_duration_frames->setValue(m_timecode.getFrameCount(m_view.luma_duration->text()));
443 // switching to timecode format
444 m_view.clip_duration->setText(m_timecode.getTimecodeFromFrames(m_view.clip_duration_frames->value()));
445 m_view.luma_duration->setText(m_timecode.getTimecodeFromFrames(m_view.luma_duration_frames->value()));
447 m_view.clip_duration_frames->setHidden(!framesFormat);
448 m_view.clip_duration->setHidden(framesFormat);
449 m_view.luma_duration_frames->setHidden(!framesFormat);
450 m_view.luma_duration->setHidden(framesFormat);
453 void SlideshowClip::slotMethodChanged(bool active)
456 // User wants mimetype image sequence
457 m_view.clip_duration->setText(m_timecode.reformatSeparators(KdenliveSettings::image_duration()));
458 m_view.stackedWidget->setCurrentIndex(0);
459 KdenliveSettings::setSlideshowbymime(true);
461 // User wants pattern image sequence
462 m_view.clip_duration->setText(m_timecode.reformatSeparators(KdenliveSettings::sequence_duration()));
463 m_view.stackedWidget->setCurrentIndex(1);
464 KdenliveSettings::setSlideshowbymime(false);
470 QString SlideshowClip::animationToGeometry(const QString &animation, int &ttl)
473 if (animation.startsWith("Pan and zoom")) {
474 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%%",
475 ttl - 1, ttl, ttl * 2 - 1, ttl * 2, ttl * 3 - 1);
477 } else if (animation.startsWith("Pan")) {
478 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%%",
479 ttl - 1, ttl, ttl * 2 - 1, ttl * 2, ttl * 3 - 1, ttl * 3, ttl * 4 - 1);
481 } else if (animation.startsWith("Zoom")) {
482 geometry = QString().sprintf("0=0/0:100%%x100%%;%d=-14%%/-14%%:120%%x120%%", ttl - 1);
488 #include "slideshowclip.moc"