]> git.sesse.net Git - kdenlive/blob - src/clipproperties.cpp
Allow changing duration of color / image clips:
[kdenlive] / src / clipproperties.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 <QDir>
21
22 #include <KStandardDirs>
23 #include <KDebug>
24 #include <KFileItem>
25
26 #include "kdenlivesettings.h"
27 #include "clipproperties.h"
28 #include "kthumb.h"
29 #include "markerdialog.h"
30
31 static const int VIDEOTAB = 0;
32 static const int AUDIOTAB = 1;
33 static const int COLORTAB = 2;
34 static const int SLIDETAB = 3;
35 static const int IMAGETAB = 4;
36 static const int MARKERTAB = 5;
37 static const int ADVANCEDTAB = 6;
38
39 static const int TYPE_JPEG = 0;
40 static const int TYPE_PNG = 1;
41 static const int TYPE_BMP = 2;
42 static const int TYPE_GIF = 3;
43
44 ClipProperties::ClipProperties(DocClipBase *clip, Timecode tc, double fps, QWidget * parent): QDialog(parent), m_tc(tc), m_clip(clip), m_fps(fps), m_clipNeedsRefresh(false), m_count(0) {
45     setFont(KGlobalSettings::toolBarFont());
46     m_view.setupUi(this);
47     KUrl url = m_clip->fileURL();
48     m_view.clip_path->setText(url.path());
49     m_view.clip_description->setText(m_clip->description());
50     QMap <QString, QString> props = m_clip->properties();
51
52     if (props.contains("force_aspect_ratio") && props.value("force_aspect_ratio").toDouble() > 0) {
53         m_view.clip_force_ar->setChecked(true);
54         m_view.clip_ar->setEnabled(true);
55         m_view.clip_ar->setValue(props.value("force_aspect_ratio").toDouble());
56     }
57
58     if (props.contains("threads") && props.value("threads").toInt() != 1) {
59         m_view.clip_force_threads->setChecked(true);
60         m_view.clip_threads->setEnabled(true);
61         m_view.clip_threads->setValue(props.value("threads").toInt());
62     }
63
64     if (props.contains("video_index") && props.value("video_index").toInt() != 0) {
65         m_view.clip_force_vindex->setChecked(true);
66         m_view.clip_vindex->setEnabled(true);
67         m_view.clip_vindex->setValue(props.value("video_index").toInt());
68     }
69
70     if (props.contains("audio_index") && props.value("audio_index").toInt() != 0) {
71         m_view.clip_force_aindex->setChecked(true);
72         m_view.clip_aindex->setEnabled(true);
73         m_view.clip_aindex->setValue(props.value("audio_index").toInt());
74     }
75
76     if (props.contains("audio_max")) {
77         m_view.clip_aindex->setMaximum(props.value("audio_max").toInt());
78     }
79
80     if (props.contains("video_max")) {
81         m_view.clip_vindex->setMaximum(props.value("video_max").toInt());
82     }
83
84     connect(m_view.clip_force_ar, SIGNAL(toggled(bool)), m_view.clip_ar, SLOT(setEnabled(bool)));
85     connect(m_view.clip_force_threads, SIGNAL(toggled(bool)), m_view.clip_threads, SLOT(setEnabled(bool)));
86     connect(m_view.clip_force_vindex, SIGNAL(toggled(bool)), m_view.clip_vindex, SLOT(setEnabled(bool)));
87     connect(m_view.clip_force_aindex, SIGNAL(toggled(bool)), m_view.clip_aindex, SLOT(setEnabled(bool)));
88
89     if (props.contains("audiocodec"))
90         m_view.clip_acodec->setText(props.value("audiocodec"));
91     if (props.contains("frequency"))
92         m_view.clip_frequency->setText(props.value("frequency"));
93     if (props.contains("channels"))
94         m_view.clip_channels->setText(props.value("channels"));
95
96     CLIPTYPE t = m_clip->clipType();
97     if (t != AUDIO && t != AV) {
98         m_view.clip_force_aindex->setEnabled(false);
99     }
100
101     if (t != VIDEO && t != AV) {
102         m_view.clip_force_vindex->setEnabled(false);
103     }
104
105     if (t == IMAGE) {
106         m_view.tabWidget->removeTab(SLIDETAB);
107         m_view.tabWidget->removeTab(COLORTAB);
108         m_view.tabWidget->removeTab(AUDIOTAB);
109         m_view.tabWidget->removeTab(VIDEOTAB);
110         if (props.contains("frame_size"))
111             m_view.image_size->setText(props.value("frame_size"));
112         if (props.contains("transparency"))
113             m_view.image_transparency->setChecked(props.value("transparency").toInt());
114     } else if (t == COLOR) {
115         m_view.clip_path->setEnabled(false);
116         m_view.tabWidget->removeTab(IMAGETAB);
117         m_view.tabWidget->removeTab(SLIDETAB);
118         m_view.tabWidget->removeTab(AUDIOTAB);
119         m_view.tabWidget->removeTab(VIDEOTAB);
120         m_view.clip_thumb->setHidden(true);
121         m_view.clip_color->setColor(QColor("#" + props.value("colour").right(8).left(6)));
122     } else if (t == SLIDESHOW) {
123         m_view.clip_path->setText(url.directory());
124         m_view.tabWidget->removeTab(IMAGETAB);
125         m_view.tabWidget->removeTab(COLORTAB);
126         m_view.tabWidget->removeTab(AUDIOTAB);
127         m_view.tabWidget->removeTab(VIDEOTAB);
128         QStringList types;
129         types << "JPG" << "PNG" << "BMP" << "GIF";
130         m_view.image_type->addItems(types);
131         m_view.slide_loop->setChecked(props.value("loop").toInt());
132         m_view.slide_fade->setChecked(props.value("fade").toInt());
133         m_view.luma_softness->setValue(props.value("softness").toInt());
134         QString path = props.value("resource");
135         if (path.endsWith("png")) m_view.image_type->setCurrentIndex(TYPE_PNG);
136         else if (path.endsWith("bmp")) m_view.image_type->setCurrentIndex(TYPE_BMP);
137         else if (path.endsWith("gif")) m_view.image_type->setCurrentIndex(TYPE_GIF);
138         m_view.slide_duration->setText(tc.getTimecodeFromFrames(props.value("ttl").toInt()));
139         parseFolder();
140
141         m_view.luma_duration->setText(tc.getTimecodeFromFrames(props.value("luma_duration").toInt()));
142         QString lumaFile = props.value("luma_file");
143         QString profilePath = KdenliveSettings::mltpath();
144         profilePath = profilePath.section('/', 0, -3);
145         profilePath += "/lumas/PAL/";
146
147         QDir dir(profilePath);
148         QStringList filter;
149         filter << "*.pgm";
150         const QStringList result = dir.entryList(filter, QDir::Files);
151         QStringList imagefiles;
152         QStringList imagenamelist;
153         int current;
154         foreach(const QString file, result) {
155             m_view.luma_file->addItem(KIcon(profilePath + file), file, profilePath + file);
156             if (!lumaFile.isEmpty() && lumaFile == QString(profilePath + file))
157                 current = m_view.luma_file->count() - 1;
158         }
159
160         if (!lumaFile.isEmpty()) {
161             m_view.slide_luma->setChecked(true);
162             m_view.luma_file->setCurrentIndex(current);
163         }
164
165         connect(m_view.image_type, SIGNAL(currentIndexChanged(int)), this, SLOT(parseFolder()));
166     } else if (t != AUDIO) {
167         m_view.tabWidget->removeTab(IMAGETAB);
168         m_view.tabWidget->removeTab(SLIDETAB);
169         m_view.tabWidget->removeTab(COLORTAB);
170         if (props.contains("frame_size"))
171             m_view.clip_size->setText(props.value("frame_size"));
172         if (props.contains("videocodec"))
173             m_view.clip_vcodec->setText(props.value("videocodec"));
174         if (props.contains("fps"))
175             m_view.clip_fps->setText(props.value("fps"));
176         if (props.contains("aspect_ratio"))
177             m_view.clip_ratio->setText(props.value("aspect_ratio"));
178
179         QPixmap pix = m_clip->thumbProducer()->getImage(url, m_clip->getClipThumbFrame(), 240, 180);
180         m_view.clip_thumb->setPixmap(pix);
181         if (t == IMAGE || t == VIDEO) m_view.tabWidget->removeTab(AUDIOTAB);
182     } else {
183         m_view.tabWidget->removeTab(IMAGETAB);
184         m_view.tabWidget->removeTab(SLIDETAB);
185         m_view.tabWidget->removeTab(COLORTAB);
186         m_view.tabWidget->removeTab(VIDEOTAB);
187         m_view.clip_thumb->setHidden(true);
188     }
189     if (t != IMAGE && t != COLOR && t != TEXT) m_view.clip_duration->setReadOnly(true);
190
191     KFileItem f(KFileItem::Unknown, KFileItem::Unknown, url, true);
192     m_view.clip_filesize->setText(KIO::convertSize(f.size()));
193     m_view.clip_duration->setText(tc.getTimecode(m_clip->duration(), m_fps));
194
195     // markers
196     m_view.marker_new->setIcon(KIcon("document-new"));
197     m_view.marker_new->setToolTip(i18n("Add marker"));
198     m_view.marker_edit->setIcon(KIcon("document-properties"));
199     m_view.marker_edit->setToolTip(i18n("Edit marker"));
200     m_view.marker_delete->setIcon(KIcon("trash-empty"));
201     m_view.marker_delete->setToolTip(i18n("Delete marker"));
202
203     slotFillMarkersList();
204     connect(m_view.marker_new, SIGNAL(clicked()), this, SLOT(slotAddMarker()));
205     connect(m_view.marker_edit, SIGNAL(clicked()), this, SLOT(slotEditMarker()));
206     connect(m_view.marker_delete, SIGNAL(clicked()), this, SLOT(slotDeleteMarker()));
207     connect(m_view.markers_list, SIGNAL(doubleClicked(const QModelIndex &)), this, SLOT(slotEditMarker()));
208
209     adjustSize();
210 }
211
212 void ClipProperties::slotFillMarkersList() {
213     m_view.markers_list->clear();
214     QList < CommentedTime > marks = m_clip->commentedSnapMarkers();
215     for (uint count = 0; count < marks.count(); ++count) {
216         QString time = m_tc.getTimecode(marks[count].time(), m_tc.fps());
217         QStringList itemtext;
218         itemtext << time << marks[count].comment();
219         (void) new QTreeWidgetItem(m_view.markers_list, itemtext);
220     }
221 }
222
223 void ClipProperties::slotAddMarker() {
224     CommentedTime marker(GenTime(), i18n("Marker"));
225     MarkerDialog d(m_clip, marker, m_tc, i18n("Add Marker"), this);
226     if (d.exec() == QDialog::Accepted) {
227         emit addMarker(m_clip->getId(), d.newMarker().time(), d.newMarker().comment());
228     }
229     QTimer::singleShot(500, this, SLOT(slotFillMarkersList()));
230 }
231
232 void ClipProperties::slotEditMarker() {
233     QList < CommentedTime > marks = m_clip->commentedSnapMarkers();
234     int pos = m_view.markers_list->currentIndex().row();
235     if (pos < 0 || pos > marks.count() - 1) return;
236     MarkerDialog d(m_clip, marks.at(pos), m_tc, i18n("Edit Marker"), this);
237     if (d.exec() == QDialog::Accepted) {
238         emit addMarker(m_clip->getId(), d.newMarker().time(), d.newMarker().comment());
239     }
240     QTimer::singleShot(500, this, SLOT(slotFillMarkersList()));
241 }
242
243 void ClipProperties::slotDeleteMarker() {
244     QList < CommentedTime > marks = m_clip->commentedSnapMarkers();
245     int pos = m_view.markers_list->currentIndex().row();
246     if (pos < 0 || pos > marks.count() - 1) return;
247     emit addMarker(m_clip->getId(), marks.at(pos).time(), QString());
248
249     QTimer::singleShot(500, this, SLOT(slotFillMarkersList()));
250 }
251
252 const QString &ClipProperties::clipId() const {
253     return m_clip->getId();
254 }
255
256
257 QMap <QString, QString> ClipProperties::properties() {
258     QMap <QString, QString> props;
259     CLIPTYPE t = m_clip->clipType();
260     QMap <QString, QString> old_props = m_clip->properties();
261
262     if (old_props.value("description") != m_view.clip_description->text())
263         props["description"] = m_view.clip_description->text();
264
265     double aspect = m_view.clip_ar->value();
266     if (m_view.clip_force_ar->isChecked()) {
267         if (aspect != old_props.value("force_aspect_ratio").toDouble()) {
268             props["force_aspect_ratio"] = QString::number(aspect);
269             m_clipNeedsRefresh = true;
270         }
271     } else if (old_props.contains("force_aspect_ratio")) {
272         props["force_aspect_ratio"] = QString();
273         m_clipNeedsRefresh = true;
274     }
275
276     int threads = m_view.clip_threads->value();
277     if (m_view.clip_force_threads->isChecked()) {
278         if (threads != old_props.value("threads").toInt()) {
279             props["threads"] = QString::number(threads);
280         }
281     } else if (old_props.contains("threads")) {
282         props["threads"] = QString();
283     }
284
285     int vindex = m_view.clip_vindex->value();
286     if (m_view.clip_force_vindex->isChecked()) {
287         if (vindex != old_props.value("video_index").toInt()) {
288             props["video_index"] = QString::number(vindex);
289         }
290     } else if (old_props.contains("video_index")) {
291         props["video_index"] = QString();
292     }
293
294     int aindex = m_view.clip_aindex->value();
295     if (m_view.clip_force_aindex->isChecked()) {
296         if (aindex != old_props.value("audio_index").toInt()) {
297             props["audio_index"] = QString::number(aindex);
298         }
299     } else if (old_props.contains("audio_index")) {
300         props["audio_index"] = QString();
301     }
302
303     if (t == COLOR) {
304         QString new_color = m_view.clip_color->color().name();
305         if (new_color != QString("#" + old_props.value("colour").right(8).left(6))) {
306             m_clipNeedsRefresh = true;
307             props["colour"] = "0x" + new_color.right(6) + "ff";
308         }
309         int duration = m_tc.getFrameCount(m_view.clip_duration->text(), m_fps);
310         if (duration != m_clip->duration().frames(m_fps)) {
311             props["out"] = QString::number(duration);
312         }
313     } else if (t == IMAGE) {
314         if ((int) m_view.image_transparency->isChecked() != old_props.value("transparency").toInt()) {
315             props["transparency"] = QString::number((int)m_view.image_transparency->isChecked());
316             m_clipNeedsRefresh = true;
317         }
318         int duration = m_tc.getFrameCount(m_view.clip_duration->text(), m_fps);
319         if (duration != m_clip->duration().frames(m_fps)) {
320             props["out"] = QString::number(duration);
321         }
322     } else if (t == SLIDESHOW) {
323         QString value = QString::number((int) m_view.slide_loop->isChecked());
324         if (old_props.value("loop") != value) props["loop"] = value;
325         value = QString::number((int) m_view.slide_fade->isChecked());
326         if (old_props.value("fade") != value) props["fade"] = value;
327         value = QString::number((int) m_view.luma_softness->value());
328         if (old_props.value("softness") != value) props["softness"] = value;
329
330         QString extension;
331         switch (m_view.image_type->currentIndex()) {
332         case TYPE_PNG:
333             extension = "/.all.png";
334             break;
335         case TYPE_BMP:
336             extension = "/.all.bmp";
337             break;
338         case TYPE_GIF:
339             extension = "/.all.gif";
340             break;
341         default:
342             extension = "/.all.jpg";
343             break;
344         }
345         QString new_path = m_view.clip_path->text() + extension;
346         if (new_path != old_props.value("resource")) {
347             m_clipNeedsRefresh = true;
348             props["resource"] = new_path;
349             kDebug() << "////  SLIDE EDIT, NEW:" << new_path << ", OLD; " << old_props.value("resource");
350         }
351         int duration = m_tc.getFrameCount(m_view.slide_duration->text(), m_fps);
352         if (duration != old_props.value("ttl").toInt()) {
353             m_clipNeedsRefresh = true;
354             props["ttl"] = QString::number(duration);
355             props["out"] = QString::number(duration * m_count);
356         }
357         if (duration * m_count != old_props.value("out").toInt()) {
358             m_clipNeedsRefresh = true;
359             props["out"] = QString::number(duration * m_count);
360         }
361         if (m_view.slide_fade->isChecked()) {
362             int luma_duration = m_tc.getFrameCount(m_view.luma_duration->text(), m_fps);
363             if (luma_duration != old_props.value("luma_duration").toInt()) {
364                 m_clipNeedsRefresh = true;
365                 props["luma_duration"] = QString::number(luma_duration);
366             }
367             QString lumaFile;
368             if (m_view.slide_luma->isChecked())
369                 lumaFile = m_view.luma_file->itemData(m_view.luma_file->currentIndex()).toString();
370             if (lumaFile != old_props.value("luma_file")) {
371                 m_clipNeedsRefresh = true;
372                 props["luma_file"] = lumaFile;
373             }
374         } else {
375             if (old_props.value("luma_file") != QString()) {
376                 props["luma_file"] = QString();
377             }
378         }
379
380     }
381     return props;
382 }
383
384 bool ClipProperties::needsTimelineRefresh() const {
385     return m_clipNeedsRefresh;
386 }
387
388 void ClipProperties::parseFolder() {
389
390     QDir dir(m_view.clip_path->text());
391     QStringList filters;
392     QString extension;
393     switch (m_view.image_type->currentIndex()) {
394     case TYPE_PNG:
395         filters << "*.png";
396         extension = "/.all.png";
397         break;
398     case TYPE_BMP:
399         filters << "*.bmp";
400         extension = "/.all.bmp";
401         break;
402     case TYPE_GIF:
403         filters << "*.gif";
404         extension = "/.all.gif";
405         break;
406     default:
407         filters << "*.jpg";
408         // TODO: improve jpeg image detection with extension like jpeg, requires change in MLT image producers
409         // << "*.jpeg";
410         extension = "/.all.jpg";
411         break;
412     }
413
414     dir.setNameFilters(filters);
415     QStringList result = dir.entryList(QDir::Files);
416     m_count = result.count();
417     m_view.slide_info->setText(i18n("%1 images found", m_count));
418     QDomElement xml = m_clip->toXML();
419     xml.setAttribute("resource", m_view.clip_path->text() + extension);
420     QPixmap pix = m_clip->thumbProducer()->getImage(KUrl(m_view.clip_path->text() + extension), 1, 240, 180);
421     QMap <QString, QString> props = m_clip->properties();
422     m_view.clip_duration->setText(m_tc.getTimecodeFromFrames(props.value("ttl").toInt() * m_count));
423     m_view.clip_thumb->setPixmap(pix);
424 }
425
426 #include "clipproperties.moc"
427
428