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