]> git.sesse.net Git - kdenlive/blob - src/slideshowclip.cpp
Fix thumbnail creation in slideshow dialog:
[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 #include <QtConcurrentRun>
29 #include <QFutureWatcher>
30
31
32 SlideshowClip::SlideshowClip(Timecode tc, QWidget * parent) :
33         QDialog(parent),
34         m_count(0),
35         m_timecode(tc)
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
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
62     m_view.clip_duration->setInputMask(m_timecode.inputMask());
63     m_view.clip_duration->setText(m_timecode.reformatSeparators(KdenliveSettings::image_duration()));
64     m_view.luma_duration->setInputMask(m_timecode.inputMask());
65     m_view.luma_duration->setText(m_timecode.getTimecodeFromFrames(int(ceil(m_timecode.fps()))));
66     m_view.folder_url->setUrl(QDir::homePath());
67
68     m_view.clip_duration_format->addItem(i18n("hh:mm:ss::ff"));
69     m_view.clip_duration_format->addItem(i18n("Frames"));
70     connect(m_view.clip_duration_format, SIGNAL(activated(int)), this, SLOT(slotUpdateDurationFormat(int)));
71     m_view.clip_duration_frames->setHidden(true);
72     m_view.luma_duration_frames->setHidden(true);
73
74     // Check for Kdenlive installed luma files
75     QStringList filters;
76     filters << "*.pgm" << "*.png";
77
78     QStringList customLumas = KGlobal::dirs()->findDirs("appdata", "lumas");
79     foreach(const QString &folder, customLumas) {
80         QStringList filesnames = QDir(folder).entryList(filters, QDir::Files);
81         foreach(const QString &fname, filesnames) {
82             QString filePath = KUrl(folder).path(KUrl::AddTrailingSlash) + fname;
83             m_view.luma_file->addItem(KIcon(filePath), fname, filePath);
84         }
85     }
86
87     // Check for MLT lumas
88     QString profilePath = KdenliveSettings::mltpath();
89     QString folder = profilePath.section('/', 0, -3);
90     folder.append("/lumas/PAL"); // TODO: cleanup the PAL / NTSC mess in luma files
91     QDir lumafolder(folder);
92     QStringList filesnames = lumafolder.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     //adjustSize();
99 }
100
101 void SlideshowClip::slotEnableLuma(int state)
102 {
103     bool enable = false;
104     if (state == Qt::Checked) enable = true;
105     m_view.luma_duration->setEnabled(enable);
106     m_view.luma_duration_frames->setEnabled(enable);
107     m_view.luma_fade->setEnabled(enable);
108     if (enable) {
109         m_view.luma_file->setEnabled(m_view.luma_fade->isChecked());
110     } else m_view.luma_file->setEnabled(false);
111     m_view.label_softness->setEnabled(m_view.luma_fade->isChecked() && enable);
112     m_view.luma_softness->setEnabled(m_view.label_softness->isEnabled());
113 }
114
115 void SlideshowClip::slotEnableThumbs(int state)
116 {
117     if (state == Qt::Checked) {
118         KdenliveSettings::setShowslideshowthumbs(true);
119         slotGenerateThumbs();
120     } else {
121         KdenliveSettings::setShowslideshowthumbs(false);
122     }
123
124 }
125
126 void SlideshowClip::slotEnableLumaFile(int state)
127 {
128     bool enable = false;
129     if (state == Qt::Checked) enable = true;
130     m_view.luma_file->setEnabled(enable);
131     m_view.luma_softness->setEnabled(enable);
132     m_view.label_softness->setEnabled(enable);
133 }
134
135 void SlideshowClip::parseFolder()
136 {
137     m_view.icon_list->clear();
138     QDir dir(m_view.folder_url->url().path());
139
140     QStringList filters;
141     QString filter = m_view.image_type->itemData(m_view.image_type->currentIndex()).toString();
142     filters << "*." + filter;
143     // TODO: improve jpeg image detection with extension like jpeg, requires change in MLT image producers
144     // << "*.jpeg";
145
146     dir.setNameFilters(filters);
147     const QStringList result = dir.entryList(QDir::Files);
148     m_count = result.count();
149     if (m_count == 0) m_view.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
150     else m_view.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
151     m_view.label_info->setText(i18np("1 image found", "%1 images found", m_count));
152     QListWidgetItem *item;
153     int i = 0;
154     KIcon unknownicon("unknown");
155     foreach(const QString &path, result) {
156         i++;
157         item = new QListWidgetItem(unknownicon, KUrl(path).fileName());
158         item->setData(Qt::UserRole, dir.filePath(path));
159         m_view.icon_list->addItem(item);
160     }
161     if (m_view.show_thumbs->isChecked()) slotGenerateThumbs();
162     m_view.icon_list->setCurrentRow(0);
163 }
164
165 void SlideshowClip::slotGenerateThumbs()
166 {
167     if (!m_future.isRunning()) {
168         connect(&m_watcher, SIGNAL(finished()), this, SLOT(slotCheckGenerateThumbs()));
169         m_future = QtConcurrent::run(this, &SlideshowClip::doGetThumbs);
170         m_watcher.setFuture(m_future);
171     }
172 }
173
174 void SlideshowClip::slotCheckGenerateThumbs()
175 {
176     QListWidgetItem* item = m_view.icon_list->item(m_view.icon_list->count() - 1);
177     if (!item || item->data(Qt::UserRole).toString().isEmpty() || m_view.show_thumbs->isChecked() == false) return;
178     QTimer::singleShot(300, this, SLOT(slotGenerateThumbs()));
179 }
180
181 void SlideshowClip::doGetThumbs()
182 {
183     for (int i = 0; i < m_view.icon_list->count(); i++) {
184         QListWidgetItem* item = m_view.icon_list->item(i);
185         if (item && m_view.show_thumbs->isChecked()) {
186             QString path = item->data(Qt::UserRole).toString();
187             if (path.isEmpty()) continue;
188             else {
189                 item->setIcon(KIcon(path));
190                 item->setData(Qt::UserRole, QString());
191                 break;
192             }
193         }
194     }
195 }
196
197 QString SlideshowClip::selectedPath() const
198 {
199     QString extension = "/.all." + m_view.image_type->itemData(m_view.image_type->currentIndex()).toString();
200     return m_view.folder_url->url().path() + extension;
201 }
202
203
204 QString SlideshowClip::clipName() const
205 {
206     return m_view.clip_name->text();
207 }
208
209 QString SlideshowClip::clipDuration() const
210 {
211     if (m_view.clip_duration_format->currentIndex() == 1) {
212         // we are in frames mode
213         return m_timecode.getTimecodeFromFrames(m_view.clip_duration_frames->value());
214     }
215     return m_view.clip_duration->text();
216 }
217
218 int SlideshowClip::imageCount() const
219 {
220     return m_count;
221 }
222
223 int SlideshowClip::softness() const
224 {
225     return m_view.luma_softness->value();
226 }
227
228 bool SlideshowClip::loop() const
229 {
230     return m_view.slide_loop->isChecked();
231 }
232
233 bool SlideshowClip::fade() const
234 {
235     return m_view.slide_fade->isChecked();
236 }
237
238 QString SlideshowClip::lumaDuration() const
239 {
240     if (m_view.clip_duration_format->currentIndex() == 1) {
241         // we are in frames mode
242         return m_timecode.getTimecodeFromFrames(m_view.luma_duration_frames->value());
243     }
244     return m_view.luma_duration->text();
245 }
246
247 QString SlideshowClip::lumaFile() const
248 {
249     if (!m_view.luma_fade->isChecked() || !m_view.luma_file->isEnabled()) return QString();
250     return m_view.luma_file->itemData(m_view.luma_file->currentIndex()).toString();
251 }
252
253 void SlideshowClip::slotUpdateDurationFormat(int ix)
254 {
255     bool framesFormat = ix == 1;
256     if (framesFormat) {
257         // switching to frames count, update widget
258         m_view.clip_duration_frames->setValue(m_timecode.getFrameCount(m_view.clip_duration->text()));
259         m_view.luma_duration_frames->setValue(m_timecode.getFrameCount(m_view.luma_duration->text()));
260     } else {
261         // switching to timecode format
262         m_view.clip_duration->setInputMask(m_timecode.inputMask());
263         m_view.clip_duration->setText(m_timecode.getTimecodeFromFrames(m_view.clip_duration_frames->value()));
264         m_view.luma_duration->setInputMask(m_timecode.inputMask());
265         m_view.luma_duration->setText(m_timecode.getTimecodeFromFrames(m_view.luma_duration_frames->value()));
266     }
267     m_view.clip_duration_frames->setHidden(!framesFormat);
268     m_view.clip_duration->setHidden(framesFormat);
269     m_view.luma_duration_frames->setHidden(!framesFormat);
270     m_view.luma_duration->setHidden(framesFormat);
271 }
272
273 #include "slideshowclip.moc"
274
275