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