]> git.sesse.net Git - kdenlive/blob - src/slideshowclip.cpp
Use KDE's thumbnailer for slideshow preview
[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
47     connect(m_view.show_thumbs, SIGNAL(stateChanged(int)), this, SLOT(slotEnableThumbs(int)));
48     connect(m_view.slide_fade, SIGNAL(stateChanged(int)), this, SLOT(slotEnableLuma(int)));
49     connect(m_view.luma_fade, SIGNAL(stateChanged(int)), this, SLOT(slotEnableLumaFile(int)));
50
51     //WARNING: keep in sync with clipproperties.cpp
52     m_view.image_type->addItem("JPG (*.jpg)", "jpg");
53     m_view.image_type->addItem("JPEG (*.jpeg)", "jpeg");
54     m_view.image_type->addItem("PNG (*.png)", "png");
55     m_view.image_type->addItem("BMP (*.bmp)", "bmp");
56     m_view.image_type->addItem("GIF (*.gif)", "gif");
57     m_view.image_type->addItem("TGA (*.tga)", "tga");
58     m_view.image_type->addItem("TIFF (*.tiff)", "tiff");
59     m_view.image_type->addItem("Open EXR (*.exr)", "exr");
60
61     m_view.clip_duration->setInputMask(m_timecode.inputMask());
62     m_view.clip_duration->setText(m_timecode.reformatSeparators(KdenliveSettings::image_duration()));
63     m_view.luma_duration->setInputMask(m_timecode.inputMask());
64     m_view.luma_duration->setText(m_timecode.getTimecodeFromFrames(int(ceil(m_timecode.fps()))));
65     m_view.folder_url->setUrl(QDir::homePath());
66
67     m_view.clip_duration_format->addItem(i18n("hh:mm:ss::ff"));
68     m_view.clip_duration_format->addItem(i18n("Frames"));
69     connect(m_view.clip_duration_format, SIGNAL(activated(int)), this, SLOT(slotUpdateDurationFormat(int)));
70     m_view.clip_duration_frames->setHidden(true);
71     m_view.luma_duration_frames->setHidden(true);
72
73     // Check for Kdenlive installed luma files
74     QStringList filters;
75     filters << "*.pgm" << "*.png";
76
77     QStringList customLumas = KGlobal::dirs()->findDirs("appdata", "lumas");
78     foreach(const QString &folder, customLumas) {
79         QStringList filesnames = QDir(folder).entryList(filters, QDir::Files);
80         foreach(const QString &fname, filesnames) {
81             QString filePath = KUrl(folder).path(KUrl::AddTrailingSlash) + fname;
82             m_view.luma_file->addItem(KIcon(filePath), fname, filePath);
83         }
84     }
85
86     // Check for MLT lumas
87     QString profilePath = KdenliveSettings::mltpath();
88     QString folder = profilePath.section('/', 0, -3);
89     folder.append("/lumas/PAL"); // TODO: cleanup the PAL / NTSC mess in luma files
90     QDir lumafolder(folder);
91     QStringList filesnames = lumafolder.entryList(filters, QDir::Files);
92     foreach(const QString &fname, filesnames) {
93         QString filePath = KUrl(folder).path(KUrl::AddTrailingSlash) + fname;
94         m_view.luma_file->addItem(KIcon(filePath), fname, filePath);
95     }
96
97     //adjustSize();
98 }
99
100 SlideshowClip::~SlideshowClip()
101 {
102     if (m_thumbJob) {
103         delete m_thumbJob;
104     }
105 }
106
107 void SlideshowClip::slotEnableLuma(int state)
108 {
109     bool enable = false;
110     if (state == Qt::Checked) enable = true;
111     m_view.luma_duration->setEnabled(enable);
112     m_view.luma_duration_frames->setEnabled(enable);
113     m_view.luma_fade->setEnabled(enable);
114     if (enable) {
115         m_view.luma_file->setEnabled(m_view.luma_fade->isChecked());
116     } else m_view.luma_file->setEnabled(false);
117     m_view.label_softness->setEnabled(m_view.luma_fade->isChecked() && enable);
118     m_view.luma_softness->setEnabled(m_view.label_softness->isEnabled());
119 }
120
121 void SlideshowClip::slotEnableThumbs(int state)
122 {
123     if (state == Qt::Checked) {
124         KdenliveSettings::setShowslideshowthumbs(true);
125         slotGenerateThumbs();
126     } else {
127         KdenliveSettings::setShowslideshowthumbs(false);
128         if (m_thumbJob) {
129             disconnect(m_thumbJob, SIGNAL(gotPreview(const KFileItem &, const QPixmap &)), this, SLOT(slotSetPixmap(const KFileItem &, const QPixmap &)));
130             m_thumbJob->kill();
131             m_thumbJob = NULL;
132         }
133     }
134
135 }
136
137 void SlideshowClip::slotEnableLumaFile(int state)
138 {
139     bool enable = false;
140     if (state == Qt::Checked) enable = true;
141     m_view.luma_file->setEnabled(enable);
142     m_view.luma_softness->setEnabled(enable);
143     m_view.label_softness->setEnabled(enable);
144 }
145
146 void SlideshowClip::parseFolder()
147 {
148     m_view.icon_list->clear();
149     QDir dir(m_view.folder_url->url().path());
150     QStringList filters;
151     QString filter = m_view.image_type->itemData(m_view.image_type->currentIndex()).toString();
152     filters << "*." + filter;
153     // TODO: improve jpeg image detection with extension like jpeg, requires change in MLT image producers
154     // << "*.jpeg";
155
156     dir.setNameFilters(filters);
157     const QStringList result = dir.entryList(QDir::Files);
158     m_count = result.count();
159     if (m_count == 0) m_view.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
160     else m_view.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
161     m_view.label_info->setText(i18np("1 image found", "%1 images found", m_count));
162     QListWidgetItem *item;
163     int i = 0;
164     KIcon unknownicon("unknown");
165     foreach(const QString &path, result) {
166         i++;
167         item = new QListWidgetItem(unknownicon, KUrl(path).fileName());
168         item->setData(Qt::UserRole, dir.filePath(path));
169         m_view.icon_list->addItem(item);
170     }
171     if (m_view.show_thumbs->isChecked()) slotGenerateThumbs();
172     m_view.icon_list->setCurrentRow(0);
173 }
174
175 void SlideshowClip::slotGenerateThumbs()
176 {
177     if (m_thumbJob) {
178         delete m_thumbJob;
179     };
180     KFileItemList fileList;
181     for (int i = 0; i < m_view.icon_list->count(); i++) {
182         QListWidgetItem* item = m_view.icon_list->item(i);
183         if (item) {
184             QString path = item->data(Qt::UserRole).toString();
185             if (!path.isEmpty()) {
186                 fileList.append(KFileItem(KFileItem::Unknown, KFileItem::Unknown, KUrl(path)));
187             }
188         }
189     }
190     m_thumbJob = new KIO::PreviewJob(fileList, 50, 0, 0, 0, true, true, 0);
191     m_thumbJob->setAutoDelete(false);
192     connect(m_thumbJob, SIGNAL(gotPreview(const KFileItem &, const QPixmap &)), this, SLOT(slotSetPixmap(const KFileItem &, const QPixmap &)));
193     m_thumbJob->start();
194 }
195
196 void SlideshowClip::slotSetPixmap(const KFileItem &fileItem, const QPixmap &pix)
197 {
198     for (int i = 0; i < m_view.icon_list->count(); i++) {
199         QListWidgetItem* item = m_view.icon_list->item(i);
200         if (item) {
201             QString path = item->data(Qt::UserRole).toString();
202             if (path == fileItem.url().path()) {
203                 item->setIcon(KIcon(pix));
204                 item->setData(Qt::UserRole, QString());
205                 break;
206             }
207         }
208     }
209 }
210
211
212 QString SlideshowClip::selectedPath() const
213 {
214     QString extension = "/.all." + m_view.image_type->itemData(m_view.image_type->currentIndex()).toString();
215     return m_view.folder_url->url().path() + extension;
216 }
217
218
219 QString SlideshowClip::clipName() const
220 {
221     return m_view.clip_name->text();
222 }
223
224 QString SlideshowClip::clipDuration() const
225 {
226     if (m_view.clip_duration_format->currentIndex() == 1) {
227         // we are in frames mode
228         return m_timecode.getTimecodeFromFrames(m_view.clip_duration_frames->value());
229     }
230     return m_view.clip_duration->text();
231 }
232
233 int SlideshowClip::imageCount() const
234 {
235     return m_count;
236 }
237
238 int SlideshowClip::softness() const
239 {
240     return m_view.luma_softness->value();
241 }
242
243 bool SlideshowClip::loop() const
244 {
245     return m_view.slide_loop->isChecked();
246 }
247
248 bool SlideshowClip::fade() const
249 {
250     return m_view.slide_fade->isChecked();
251 }
252
253 QString SlideshowClip::lumaDuration() const
254 {
255     if (m_view.clip_duration_format->currentIndex() == 1) {
256         // we are in frames mode
257         return m_timecode.getTimecodeFromFrames(m_view.luma_duration_frames->value());
258     }
259     return m_view.luma_duration->text();
260 }
261
262 QString SlideshowClip::lumaFile() const
263 {
264     if (!m_view.luma_fade->isChecked() || !m_view.luma_file->isEnabled()) return QString();
265     return m_view.luma_file->itemData(m_view.luma_file->currentIndex()).toString();
266 }
267
268 void SlideshowClip::slotUpdateDurationFormat(int ix)
269 {
270     bool framesFormat = ix == 1;
271     if (framesFormat) {
272         // switching to frames count, update widget
273         m_view.clip_duration_frames->setValue(m_timecode.getFrameCount(m_view.clip_duration->text()));
274         m_view.luma_duration_frames->setValue(m_timecode.getFrameCount(m_view.luma_duration->text()));
275     } else {
276         // switching to timecode format
277         m_view.clip_duration->setInputMask(m_timecode.inputMask());
278         m_view.clip_duration->setText(m_timecode.getTimecodeFromFrames(m_view.clip_duration_frames->value()));
279         m_view.luma_duration->setInputMask(m_timecode.inputMask());
280         m_view.luma_duration->setText(m_timecode.getTimecodeFromFrames(m_view.luma_duration_frames->value()));
281     }
282     m_view.clip_duration_frames->setHidden(!framesFormat);
283     m_view.clip_duration->setHidden(framesFormat);
284     m_view.luma_duration_frames->setHidden(!framesFormat);
285     m_view.luma_duration->setHidden(framesFormat);
286 }
287
288 #include "slideshowclip.moc"
289
290