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