]> git.sesse.net Git - kdenlive/blob - src/clipproperties.cpp
Reformat initializer lists in all constructors.
[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_tc(tc),
49         m_clip(clip),
50         m_fps(fps),
51         m_clipNeedsRefresh(false),
52         m_count(0)
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         parseFolder();
161
162         m_view.luma_duration->setText(tc.getTimecodeFromFrames(props.value("luma_duration").toInt()));
163         QString lumaFile = props.value("luma_file");
164
165         // Check for Kdenlive installed luma files
166         QStringList filters;
167         filters << "*.pgm" << "*.png";
168
169         QStringList customLumas = KGlobal::dirs()->findDirs("appdata", "lumas");
170         foreach(const QString &folder, customLumas) {
171             QStringList filesnames = QDir(folder).entryList(filters, QDir::Files);
172             foreach(const QString &fname, filesnames) {
173                 m_view.luma_file->addItem(KIcon(folder + '/' + fname), fname, folder + '/' + fname);
174             }
175         }
176
177         // Check for MLT lumas
178         QString profilePath = KdenliveSettings::mltpath();
179         QString folder = profilePath.section('/', 0, -3);
180         folder.append("/lumas/PAL"); // TODO: cleanup the PAL / NTSC mess in luma files
181         QDir lumafolder(folder);
182         QStringList filesnames = lumafolder.entryList(filters, QDir::Files);
183         foreach(const QString &fname, filesnames) {
184             m_view.luma_file->addItem(KIcon(folder + '/' + fname), fname, folder + '/' + fname);
185         }
186
187         slotEnableLuma(m_view.slide_fade->isChecked());
188         slotEnableLumaFile(m_view.slide_luma->isChecked());
189
190         if (!lumaFile.isEmpty()) {
191             m_view.slide_luma->setChecked(true);
192             m_view.luma_file->setCurrentIndex(m_view.luma_file->findData(lumaFile));
193         } else m_view.luma_file->setEnabled(false);
194         connect(m_view.slide_fade, SIGNAL(stateChanged(int)), this, SLOT(slotEnableLuma(int)));
195         connect(m_view.slide_luma, SIGNAL(stateChanged(int)), this, SLOT(slotEnableLumaFile(int)));
196
197         connect(m_view.image_type, SIGNAL(currentIndexChanged(int)), this, SLOT(parseFolder()));
198     } else if (t != AUDIO) {
199         m_view.tabWidget->removeTab(IMAGETAB);
200         m_view.tabWidget->removeTab(SLIDETAB);
201         m_view.tabWidget->removeTab(COLORTAB);
202         if (props.contains("frame_size"))
203             m_view.clip_size->setText(props.value("frame_size"));
204         if (props.contains("videocodec"))
205             m_view.clip_vcodec->setText(props.value("videocodec"));
206         if (props.contains("fps"))
207             m_view.clip_fps->setText(props.value("fps"));
208         if (props.contains("aspect_ratio"))
209             m_view.clip_ratio->setText(props.value("aspect_ratio"));
210
211         QPixmap pix = m_clip->thumbProducer()->getImage(url, m_clip->getClipThumbFrame(), 240, 180);
212         m_view.clip_thumb->setPixmap(pix);
213         if (t == IMAGE || t == VIDEO) m_view.tabWidget->removeTab(AUDIOTAB);
214     } else {
215         m_view.tabWidget->removeTab(IMAGETAB);
216         m_view.tabWidget->removeTab(SLIDETAB);
217         m_view.tabWidget->removeTab(COLORTAB);
218         m_view.tabWidget->removeTab(VIDEOTAB);
219         m_view.clip_thumb->setHidden(true);
220     }
221
222     KFileItem f(KFileItem::Unknown, KFileItem::Unknown, url, true);
223     m_view.clip_filesize->setText(KIO::convertSize(f.size()));
224     m_view.clip_duration->setText(tc.getTimecode(m_clip->duration(), m_fps));
225     if (t != IMAGE && t != COLOR && t != TEXT) m_view.clip_duration->setReadOnly(true);
226     else connect(m_view.clip_duration, SIGNAL(editingFinished()), this, SLOT(slotCheckMaxLength()));
227
228     // markers
229     m_view.marker_new->setIcon(KIcon("document-new"));
230     m_view.marker_new->setToolTip(i18n("Add marker"));
231     m_view.marker_edit->setIcon(KIcon("document-properties"));
232     m_view.marker_edit->setToolTip(i18n("Edit marker"));
233     m_view.marker_delete->setIcon(KIcon("trash-empty"));
234     m_view.marker_delete->setToolTip(i18n("Delete marker"));
235
236     slotFillMarkersList();
237     connect(m_view.marker_new, SIGNAL(clicked()), this, SLOT(slotAddMarker()));
238     connect(m_view.marker_edit, SIGNAL(clicked()), this, SLOT(slotEditMarker()));
239     connect(m_view.marker_delete, SIGNAL(clicked()), this, SLOT(slotDeleteMarker()));
240     connect(m_view.markers_list, SIGNAL(doubleClicked(const QModelIndex &)), this, SLOT(slotEditMarker()));
241
242     adjustSize();
243 }
244
245 void ClipProperties::slotEnableLuma(int state)
246 {
247     bool enable = false;
248     if (state == Qt::Checked) enable = true;
249     m_view.luma_duration->setEnabled(enable);
250     m_view.slide_luma->setEnabled(enable);
251     if (enable) {
252         m_view.luma_file->setEnabled(m_view.slide_luma->isChecked());
253     } else m_view.luma_file->setEnabled(false);
254     m_view.label_softness->setEnabled(m_view.slide_luma->isChecked() && enable);
255     m_view.luma_softness->setEnabled(m_view.label_softness->isEnabled());
256 }
257
258 void ClipProperties::slotEnableLumaFile(int state)
259 {
260     bool enable = false;
261     if (state == Qt::Checked) enable = true;
262     m_view.luma_file->setEnabled(enable);
263     m_view.luma_softness->setEnabled(enable);
264     m_view.label_softness->setEnabled(enable);
265 }
266
267 void ClipProperties::slotFillMarkersList()
268 {
269     m_view.markers_list->clear();
270     QList < CommentedTime > marks = m_clip->commentedSnapMarkers();
271     for (uint count = 0; count < marks.count(); ++count) {
272         QString time = m_tc.getTimecode(marks[count].time(), m_tc.fps());
273         QStringList itemtext;
274         itemtext << time << marks[count].comment();
275         (void) new QTreeWidgetItem(m_view.markers_list, itemtext);
276     }
277 }
278
279 void ClipProperties::slotAddMarker()
280 {
281     CommentedTime marker(GenTime(), i18n("Marker"));
282     MarkerDialog d(m_clip, marker, m_tc, i18n("Add Marker"), this);
283     if (d.exec() == QDialog::Accepted) {
284         emit addMarker(m_clip->getId(), d.newMarker().time(), d.newMarker().comment());
285     }
286     QTimer::singleShot(500, this, SLOT(slotFillMarkersList()));
287 }
288
289 void ClipProperties::slotEditMarker()
290 {
291     QList < CommentedTime > marks = m_clip->commentedSnapMarkers();
292     int pos = m_view.markers_list->currentIndex().row();
293     if (pos < 0 || pos > marks.count() - 1) return;
294     MarkerDialog d(m_clip, marks.at(pos), m_tc, i18n("Edit Marker"), this);
295     if (d.exec() == QDialog::Accepted) {
296         emit addMarker(m_clip->getId(), d.newMarker().time(), d.newMarker().comment());
297     }
298     QTimer::singleShot(500, this, SLOT(slotFillMarkersList()));
299 }
300
301 void ClipProperties::slotDeleteMarker()
302 {
303     QList < CommentedTime > marks = m_clip->commentedSnapMarkers();
304     int pos = m_view.markers_list->currentIndex().row();
305     if (pos < 0 || pos > marks.count() - 1) return;
306     emit addMarker(m_clip->getId(), marks.at(pos).time(), QString());
307
308     QTimer::singleShot(500, this, SLOT(slotFillMarkersList()));
309 }
310
311 const QString &ClipProperties::clipId() const
312 {
313     return m_clip->getId();
314 }
315
316
317 QMap <QString, QString> ClipProperties::properties()
318 {
319     QMap <QString, QString> props;
320     CLIPTYPE t = m_clip->clipType();
321     QMap <QString, QString> old_props = m_clip->properties();
322
323     if (old_props.value("description") != m_view.clip_description->text())
324         props["description"] = m_view.clip_description->text();
325
326     double aspect = m_view.clip_ar->value();
327     if (m_view.clip_force_ar->isChecked()) {
328         if (aspect != old_props.value("force_aspect_ratio").toDouble()) {
329             props["force_aspect_ratio"] = QString::number(aspect);
330             m_clipNeedsRefresh = true;
331         }
332     } else if (old_props.contains("force_aspect_ratio")) {
333         props["force_aspect_ratio"].clear();
334         m_clipNeedsRefresh = true;
335     }
336
337     int threads = m_view.clip_threads->value();
338     if (m_view.clip_force_threads->isChecked()) {
339         if (threads != old_props.value("threads").toInt()) {
340             props["threads"] = QString::number(threads);
341         }
342     } else if (old_props.contains("threads")) {
343         props["threads"].clear();
344     }
345
346     int vindex = m_view.clip_vindex->value();
347     if (m_view.clip_force_vindex->isChecked()) {
348         if (vindex != old_props.value("video_index").toInt()) {
349             props["video_index"] = QString::number(vindex);
350         }
351     } else if (old_props.contains("video_index")) {
352         props["video_index"].clear();
353     }
354
355     int aindex = m_view.clip_aindex->value();
356     if (m_view.clip_force_aindex->isChecked()) {
357         if (aindex != old_props.value("audio_index").toInt()) {
358             props["audio_index"] = QString::number(aindex);
359         }
360     } else if (old_props.contains("audio_index")) {
361         props["audio_index"].clear();
362     }
363
364     if (t == COLOR) {
365         QString new_color = m_view.clip_color->color().name();
366         if (new_color != QString('#' + old_props.value("colour").right(8).left(6))) {
367             m_clipNeedsRefresh = true;
368             props["colour"] = "0x" + new_color.right(6) + "ff";
369         }
370         int duration = m_tc.getFrameCount(m_view.clip_duration->text(), m_fps);
371         if (duration != m_clip->duration().frames(m_fps)) {
372             props["out"] = QString::number(duration);
373         }
374     } else if (t == IMAGE) {
375         if ((int) m_view.image_transparency->isChecked() != old_props.value("transparency").toInt()) {
376             props["transparency"] = QString::number((int)m_view.image_transparency->isChecked());
377             m_clipNeedsRefresh = true;
378         }
379         int duration = m_tc.getFrameCount(m_view.clip_duration->text(), m_fps);
380         if (duration != m_clip->duration().frames(m_fps)) {
381             props["out"] = QString::number(duration);
382         }
383     } else if (t == SLIDESHOW) {
384         QString value = QString::number((int) m_view.slide_loop->isChecked());
385         if (old_props.value("loop") != value) props["loop"] = value;
386         value = QString::number((int) m_view.slide_fade->isChecked());
387         if (old_props.value("fade") != value) props["fade"] = value;
388         value = QString::number((int) m_view.luma_softness->value());
389         if (old_props.value("softness") != value) props["softness"] = value;
390
391         QString extension;
392         switch (m_view.image_type->currentIndex()) {
393         case TYPE_PNG:
394             extension = "/.all.png";
395             break;
396         case TYPE_BMP:
397             extension = "/.all.bmp";
398             break;
399         case TYPE_GIF:
400             extension = "/.all.gif";
401             break;
402         default:
403             extension = "/.all.jpg";
404             break;
405         }
406         QString new_path = m_view.clip_path->text() + extension;
407         if (new_path != old_props.value("resource")) {
408             m_clipNeedsRefresh = true;
409             props["resource"] = new_path;
410             kDebug() << "////  SLIDE EDIT, NEW:" << new_path << ", OLD; " << old_props.value("resource");
411         }
412         int duration = m_tc.getFrameCount(m_view.slide_duration->text(), m_fps);
413         if (duration != old_props.value("ttl").toInt()) {
414             m_clipNeedsRefresh = true;
415             props["ttl"] = QString::number(duration);
416             props["out"] = QString::number(duration * m_count);
417         }
418         if (duration * m_count != old_props.value("out").toInt()) {
419             m_clipNeedsRefresh = true;
420             props["out"] = QString::number(duration * m_count);
421         }
422         if (m_view.slide_fade->isChecked()) {
423             int luma_duration = m_tc.getFrameCount(m_view.luma_duration->text(), m_fps);
424             if (luma_duration != old_props.value("luma_duration").toInt()) {
425                 m_clipNeedsRefresh = true;
426                 props["luma_duration"] = QString::number(luma_duration);
427             }
428             QString lumaFile;
429             if (m_view.slide_luma->isChecked())
430                 lumaFile = m_view.luma_file->itemData(m_view.luma_file->currentIndex()).toString();
431             if (lumaFile != old_props.value("luma_file")) {
432                 m_clipNeedsRefresh = true;
433                 props["luma_file"] = lumaFile;
434             }
435         } else {
436             if (!old_props.value("luma_file").isEmpty()) {
437                 props["luma_file"].clear();
438             }
439         }
440
441     }
442     return props;
443 }
444
445 bool ClipProperties::needsTimelineRefresh() const
446 {
447     return m_clipNeedsRefresh;
448 }
449
450 void ClipProperties::parseFolder()
451 {
452
453     QDir dir(m_view.clip_path->text());
454     QStringList filters;
455     QString extension;
456     switch (m_view.image_type->currentIndex()) {
457     case TYPE_PNG:
458         filters << "*.png";
459         extension = "/.all.png";
460         break;
461     case TYPE_BMP:
462         filters << "*.bmp";
463         extension = "/.all.bmp";
464         break;
465     case TYPE_GIF:
466         filters << "*.gif";
467         extension = "/.all.gif";
468         break;
469     default:
470         filters << "*.jpg";
471         // TODO: improve jpeg image detection with extension like jpeg, requires change in MLT image producers
472         // << "*.jpeg";
473         extension = "/.all.jpg";
474         break;
475     }
476
477     dir.setNameFilters(filters);
478     QStringList result = dir.entryList(QDir::Files);
479     m_count = result.count();
480     m_view.slide_info->setText(i18n("%1 images found", m_count));
481     QDomElement xml = m_clip->toXML();
482     xml.setAttribute("resource", m_view.clip_path->text() + extension);
483     QPixmap pix = m_clip->thumbProducer()->getImage(KUrl(m_view.clip_path->text() + extension), 1, 240, 180);
484     QMap <QString, QString> props = m_clip->properties();
485     m_view.clip_duration->setText(m_tc.getTimecodeFromFrames(props.value("ttl").toInt() * m_count));
486     m_view.clip_thumb->setPixmap(pix);
487 }
488
489 void ClipProperties::slotCheckMaxLength()
490 {
491     int duration = m_tc.getFrameCount(m_view.clip_duration->text(), m_fps);
492     if (duration > m_clip->maxDuration().frames(m_fps)) {
493         m_view.clip_duration->setText(m_tc.getTimecode(m_clip->maxDuration(), m_fps));
494     }
495 }
496
497 #include "clipproperties.moc"
498
499