]> git.sesse.net Git - kdenlive/blob - src/clipproperties.cpp
14a7748a92b243eee8959502bdb3653c1870e372
[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 #include "profilesdialog.h"
26
27 #include <KStandardDirs>
28 #include <KDebug>
29 #include <KFileItem>
30
31 #include <QDir>
32
33 static const int VIDEOTAB = 0;
34 static const int AUDIOTAB = 1;
35 static const int COLORTAB = 2;
36 static const int SLIDETAB = 3;
37 static const int IMAGETAB = 4;
38 static const int MARKERTAB = 5;
39 static const int METATAB = 6;
40 static const int ADVANCEDTAB = 7;
41
42
43 ClipProperties::ClipProperties(DocClipBase *clip, Timecode tc, double fps, QWidget * parent) :
44     QDialog(parent),
45     m_clip(clip),
46     m_tc(tc),
47     m_fps(fps),
48     m_count(0),
49     m_clipNeedsRefresh(false),
50     m_clipNeedsReLoad(false)
51 {
52     setAttribute(Qt::WA_DeleteOnClose, true);
53     setFont(KGlobalSettings::toolBarFont());
54     m_view.setupUi(this);
55     KUrl url = m_clip->fileURL();
56     m_view.clip_path->setText(url.path());
57     m_view.clip_description->setText(m_clip->description());
58     connect(m_view.clip_description, SIGNAL(textChanged(QString)), this, SLOT(slotModified()));
59
60     QMap <QString, QString> props = m_clip->properties();
61     m_view.clip_force_out->setHidden(true);
62     m_view.clip_out->setHidden(true);
63
64     // New display aspect ratio support
65     if (props.contains("force_aspect_num") && props.value("force_aspect_num").toInt() > 0 &&
66         props.contains("force_aspect_den") && props.value("force_aspect_den").toInt() > 0) {
67         m_view.clip_force_ar->setChecked(true);
68         m_view.clip_ar_num->setEnabled(true);
69         m_view.clip_ar_den->setEnabled(true);
70         m_view.clip_ar_num->setValue(props.value("force_aspect_num").toInt());
71         m_view.clip_ar_den->setValue(props.value("force_aspect_den").toInt());
72     }
73     // Legacy support for pixel aspect ratio
74     else if (props.contains("force_aspect_ratio") && props.value("force_aspect_ratio").toDouble() > 0) {
75         m_view.clip_force_ar->setChecked(true);
76         m_view.clip_ar_num->setEnabled(true);
77         m_view.clip_ar_den->setEnabled(true);
78         if (props.contains("frame_size")) {
79             int width = props.value("force_aspect_ratio").toDouble() * props.value("frame_size").section('x', 0, 0).toInt();
80             int height = props.value("frame_size").section('x', 1, 1).toInt();
81             if (width > 0 && height > 0) {
82                 if ((width / height * 100) == 133) {
83                     width = 4;
84                     height = 3;
85                 }
86                 else if (int(width / height * 100) == 177) {
87                     width = 16;
88                     height = 9;
89                 }
90                 m_view.clip_ar_num->setValue(width);
91                 m_view.clip_ar_den->setValue(height);
92             }
93         }
94     }
95     connect(m_view.clip_force_ar, SIGNAL(toggled(bool)), this, SLOT(slotModified()));
96     connect(m_view.clip_ar_num, SIGNAL(valueChanged(int)), this, SLOT(slotModified()));
97     connect(m_view.clip_ar_den, SIGNAL(valueChanged(int)), this, SLOT(slotModified()));
98
99     if (props.contains("force_fps") && props.value("force_fps").toDouble() > 0) {
100         m_view.clip_force_framerate->setChecked(true);
101         m_view.clip_framerate->setEnabled(true);
102         m_view.clip_framerate->setValue(props.value("force_fps").toDouble());
103     }
104     connect(m_view.clip_force_framerate, SIGNAL(toggled(bool)), this, SLOT(slotModified()));
105     connect(m_view.clip_framerate, SIGNAL(valueChanged(double)), this, SLOT(slotModified()));
106
107     if (props.contains("force_progressive")) {
108         m_view.clip_force_progressive->setChecked(true);
109         m_view.clip_progressive->setEnabled(true);
110         m_view.clip_progressive->setValue(props.value("force_progressive").toInt());
111     }
112     connect(m_view.clip_force_progressive, SIGNAL(toggled(bool)), this, SLOT(slotModified()));
113     connect(m_view.clip_progressive, SIGNAL(valueChanged(int)), this, SLOT(slotModified()));
114
115     m_view.clip_fieldorder->addItem(i18n("Bottom first"), 0);
116     m_view.clip_fieldorder->addItem(i18n("Top first"), 1);
117     if (props.contains("force_tff")) {
118         m_view.clip_force_fieldorder->setChecked(true);
119         m_view.clip_fieldorder->setEnabled(true);
120         m_view.clip_fieldorder->setCurrentIndex(props.value("force_tff").toInt());
121     }
122     connect(m_view.clip_force_fieldorder, SIGNAL(toggled(bool)), this, SLOT(slotModified()));
123     connect(m_view.clip_fieldorder, SIGNAL(currentIndexChanged(int)), this, SLOT(slotModified()));
124     
125     if (props.contains("threads") && props.value("threads").toInt() != 1) {
126         m_view.clip_force_threads->setChecked(true);
127         m_view.clip_threads->setEnabled(true);
128         m_view.clip_threads->setValue(props.value("threads").toInt());
129     }
130     connect(m_view.clip_force_threads, SIGNAL(toggled(bool)), this, SLOT(slotModified()));
131     connect(m_view.clip_threads, SIGNAL(valueChanged(int)), this, SLOT(slotModified()));
132
133     if (props.contains("video_index") && props.value("video_index").toInt() != 0) {
134         m_view.clip_force_vindex->setChecked(true);
135         m_view.clip_vindex->setEnabled(true);
136         m_view.clip_vindex->setValue(props.value("video_index").toInt());
137     }
138     connect(m_view.clip_force_vindex, SIGNAL(toggled(bool)), this, SLOT(slotModified()));
139     connect(m_view.clip_vindex, SIGNAL(valueChanged(int)), this, SLOT(slotModified()));
140
141     if (props.contains("audio_index") && props.value("audio_index").toInt() != 0) {
142         m_view.clip_force_aindex->setChecked(true);
143         m_view.clip_aindex->setEnabled(true);
144         m_view.clip_aindex->setValue(props.value("audio_index").toInt());
145     }
146     connect(m_view.clip_force_aindex, SIGNAL(toggled(bool)), this, SLOT(slotModified()));
147     connect(m_view.clip_aindex, SIGNAL(valueChanged(int)), this, SLOT(slotModified()));
148
149     if (props.contains("audio_max")) {
150         m_view.clip_aindex->setMaximum(props.value("audio_max").toInt());
151     }
152
153     if (props.contains("video_max")) {
154         m_view.clip_vindex->setMaximum(props.value("video_max").toInt());
155     }
156     
157     m_view.clip_colorspace->addItem(ProfilesDialog::getColorspaceDescription(601), 601);
158     m_view.clip_colorspace->addItem(ProfilesDialog::getColorspaceDescription(709), 709);
159     m_view.clip_colorspace->addItem(ProfilesDialog::getColorspaceDescription(240), 240);
160     if (props.contains("force_colorspace")) {
161         m_view.clip_force_colorspace->setChecked(true);
162         m_view.clip_colorspace->setEnabled(true);
163         m_view.clip_colorspace->setCurrentIndex(m_view.clip_colorspace->findData(props.value("force_colorspace").toInt()));
164     } else if (props.contains("colorspace")) {
165         m_view.clip_colorspace->setCurrentIndex(m_view.clip_colorspace->findData(props.value("colorspace").toInt()));
166     }
167     connect(m_view.clip_force_colorspace, SIGNAL(toggled(bool)), this, SLOT(slotModified()));
168     connect(m_view.clip_colorspace, SIGNAL(currentIndexChanged(int)), this, SLOT(slotModified()));
169     
170     if (props.contains("full_luma")) {
171         m_view.clip_full_luma->setChecked(true);
172     }
173     connect(m_view.clip_full_luma, SIGNAL(toggled(bool)), this, SLOT(slotModified()));
174
175     // Check for Metadata
176     QMap<QString, QString> meta = m_clip->metadata();
177     QMap<QString, QString>::const_iterator i = meta.constBegin();
178     while (i != meta.constEnd()) {
179         QTreeWidgetItem *metaitem = new QTreeWidgetItem(m_view.metadata_list);
180         metaitem->setText(0, i.key()); //i18n(i.key().section('.', 2, 3).toUtf8().data()));
181         metaitem->setText(1, i.value());
182         ++i;
183     }
184
185     connect(m_view.clip_force_ar, SIGNAL(toggled(bool)), m_view.clip_ar_num, SLOT(setEnabled(bool)));
186     connect(m_view.clip_force_ar, SIGNAL(toggled(bool)), m_view.clip_ar_den, SLOT(setEnabled(bool)));
187     connect(m_view.clip_force_framerate, SIGNAL(toggled(bool)), m_view.clip_framerate, SLOT(setEnabled(bool)));
188     connect(m_view.clip_force_progressive, SIGNAL(toggled(bool)), m_view.clip_progressive, SLOT(setEnabled(bool)));
189     connect(m_view.clip_force_fieldorder, SIGNAL(toggled(bool)), m_view.clip_fieldorder, SLOT(setEnabled(bool)));
190     connect(m_view.clip_force_threads, SIGNAL(toggled(bool)), m_view.clip_threads, SLOT(setEnabled(bool)));
191     connect(m_view.clip_force_vindex, SIGNAL(toggled(bool)), m_view.clip_vindex, SLOT(setEnabled(bool)));
192     connect(m_view.clip_force_aindex, SIGNAL(toggled(bool)), m_view.clip_aindex, SLOT(setEnabled(bool)));
193     connect(m_view.clip_force_colorspace, SIGNAL(toggled(bool)), m_view.clip_colorspace, SLOT(setEnabled(bool)));
194
195     if (props.contains("audiocodec"))
196         QTreeWidgetItem *item = new QTreeWidgetItem(m_view.clip_aproperties, QStringList() << i18n("Audio codec") << props.value("audiocodec"));
197
198     if (props.contains("channels"))
199         QTreeWidgetItem *item = new QTreeWidgetItem(m_view.clip_aproperties, QStringList() << i18n("Channels") << props.value("channels"));
200
201     if (props.contains("frequency"))
202         QTreeWidgetItem *item = new QTreeWidgetItem(m_view.clip_aproperties, QStringList() << i18n("Frequency") << props.value("frequency"));
203
204     CLIPTYPE t = m_clip->clipType();
205     if (t != AUDIO && t != AV) {
206         m_view.clip_force_aindex->setEnabled(false);
207     }
208
209     if (t != VIDEO && t != AV) {
210         m_view.clip_force_vindex->setEnabled(false);
211     }
212
213     if (t == IMAGE) {
214         m_view.tabWidget->removeTab(SLIDETAB);
215         m_view.tabWidget->removeTab(COLORTAB);
216         m_view.tabWidget->removeTab(AUDIOTAB);
217         m_view.tabWidget->removeTab(VIDEOTAB);
218         if (props.contains("frame_size"))
219             m_view.image_size->setText(props.value("frame_size"));
220         if (props.contains("transparency"))
221             m_view.image_transparency->setChecked(props.value("transparency").toInt());
222         connect(m_view.image_transparency, SIGNAL(toggled(bool)), this, SLOT(slotModified()));
223         int width = 180.0 * KdenliveSettings::project_display_ratio();
224         if (width % 2 == 1) width++;
225         m_view.clip_thumb->setPixmap(QPixmap(url.path()).scaled(QSize(width, 180), Qt::KeepAspectRatio));
226     } else if (t == COLOR) {
227         m_view.clip_path->setEnabled(false);
228         m_view.tabWidget->removeTab(METATAB);
229         m_view.tabWidget->removeTab(IMAGETAB);
230         m_view.tabWidget->removeTab(SLIDETAB);
231         m_view.tabWidget->removeTab(AUDIOTAB);
232         m_view.tabWidget->removeTab(VIDEOTAB);
233         m_view.clip_thumb->setHidden(true);
234         m_view.clip_color->setColor(QColor('#' + props.value("colour").right(8).left(6)));
235         connect(m_view.clip_color, SIGNAL(changed(QColor)), this, SLOT(slotModified()));
236     } else if (t == SLIDESHOW) {
237         bool isMime = true;
238         if (url.fileName().startsWith(".all.")) {
239             // the image sequence is defined by mimetype
240             m_view.clip_path->setText(url.directory());
241         } else {
242             // the image sequence is defined by pattern
243             m_view.slide_type_label->setHidden(true);
244             m_view.image_type->setHidden(true);
245             m_view.clip_path->setText(url.path());
246             isMime = false;
247         }
248
249         m_view.tabWidget->removeTab(METATAB);
250         m_view.tabWidget->removeTab(IMAGETAB);
251         m_view.tabWidget->removeTab(COLORTAB);
252         m_view.tabWidget->removeTab(AUDIOTAB);
253         m_view.tabWidget->removeTab(VIDEOTAB);
254
255         //WARNING: Keep in sync with slideshowclip.cpp
256         m_view.image_type->addItem("JPG (*.jpg)", "jpg");
257         m_view.image_type->addItem("JPEG (*.jpeg)", "jpeg");
258         m_view.image_type->addItem("PNG (*.png)", "png");
259         m_view.image_type->addItem("BMP (*.bmp)", "bmp");
260         m_view.image_type->addItem("GIF (*.gif)", "gif");
261         m_view.image_type->addItem("TGA (*.tga)", "tga");
262         m_view.image_type->addItem("TIF (*.tif)", "tif");
263         m_view.image_type->addItem("TIFF (*.tiff)", "tiff");
264         m_view.image_type->addItem("Open EXR (*.exr)", "exr");
265         m_view.animation->addItem(i18n("None"), QString());
266         m_view.animation->addItem(i18n("Pan"), "Pan");
267         m_view.animation->addItem(i18n("Pan, low-pass"), "Pan, low-pass");
268         m_view.animation->addItem(i18n("Pan and zoom"), "Pan and zoom");
269         m_view.animation->addItem(i18n("Pan and zoom, low-pass"), "Pan and zoom, low-pass");
270         m_view.animation->addItem(i18n("Zoom"), "Zoom");
271         m_view.animation->addItem(i18n("Zoom, low-pass"), "Zoom, low-pass");
272
273         m_view.slide_loop->setChecked(props.value("loop").toInt());
274         m_view.slide_crop->setChecked(props.value("crop").toInt());
275         m_view.slide_fade->setChecked(props.value("fade").toInt());
276         m_view.luma_softness->setValue(props.value("softness").toInt());
277         if (!props.value("animation").isEmpty())
278             m_view.animation->setCurrentItem(props.value("animation"));
279         else
280             m_view.animation->setCurrentIndex(0);
281         QString path = props.value("resource");
282         QString ext = path.section('.', -1);
283         for (int i = 0; i < m_view.image_type->count(); i++) {
284             if (m_view.image_type->itemData(i).toString() == ext) {
285                 m_view.image_type->setCurrentIndex(i);
286                 break;
287             }
288         }
289         m_view.slide_duration->setText(tc.getTimecodeFromFrames(props.value("ttl").toInt()));
290
291         m_view.slide_duration_format->addItem(i18n("hh:mm:ss:ff"));
292         m_view.slide_duration_format->addItem(i18n("Frames"));
293         connect(m_view.slide_duration_format, SIGNAL(activated(int)), this, SLOT(slotUpdateDurationFormat(int)));
294         m_view.slide_duration_frames->setHidden(true);
295         m_view.luma_duration_frames->setHidden(true);
296
297         parseFolder();
298
299         m_view.luma_duration->setText(tc.getTimecodeFromFrames(props.value("luma_duration").toInt()));
300         QString lumaFile = props.value("luma_file");
301
302         // Check for Kdenlive installed luma files
303         QStringList filters;
304         filters << "*.pgm" << "*.png";
305
306         QStringList customLumas = KGlobal::dirs()->findDirs("appdata", "lumas");
307         foreach(const QString & folder, customLumas) {
308             QStringList filesnames = QDir(folder).entryList(filters, QDir::Files);
309             foreach(const QString & fname, filesnames) {
310                 QString filePath = KUrl(folder).path(KUrl::AddTrailingSlash) + fname;
311                 m_view.luma_file->addItem(KIcon(filePath), fname, filePath);
312             }
313         }
314
315         // Check for MLT lumas
316         QString profilePath = KdenliveSettings::mltpath();
317         QString folder = profilePath.section('/', 0, -3);
318         folder.append("/lumas/PAL"); // TODO: cleanup the PAL / NTSC mess in luma files
319         QDir lumafolder(folder);
320         QStringList filesnames = lumafolder.entryList(filters, QDir::Files);
321         foreach(const QString & fname, filesnames) {
322             QString filePath = KUrl(folder).path(KUrl::AddTrailingSlash) + fname;
323             m_view.luma_file->addItem(KIcon(filePath), fname, filePath);
324         }
325
326         if (!lumaFile.isEmpty()) {
327             m_view.slide_luma->setChecked(true);
328             m_view.luma_file->setCurrentIndex(m_view.luma_file->findData(lumaFile));
329         } else m_view.luma_file->setEnabled(false);
330         slotEnableLuma(m_view.slide_fade->checkState());
331         slotEnableLumaFile(m_view.slide_luma->checkState());
332
333         connect(m_view.slide_fade, SIGNAL(toggled(bool)), this, SLOT(slotModified()));
334         connect(m_view.slide_luma, SIGNAL(toggled(bool)), this, SLOT(slotModified()));
335         connect(m_view.slide_loop, SIGNAL(toggled(bool)), this, SLOT(slotModified()));
336         connect(m_view.slide_crop, SIGNAL(toggled(bool)), this, SLOT(slotModified()));
337         connect(m_view.slide_duration, SIGNAL(textChanged(QString)), this, SLOT(slotModified()));
338         connect(m_view.slide_duration_frames, SIGNAL(valueChanged(int)), this, SLOT(slotModified()));
339         connect(m_view.luma_duration, SIGNAL(textChanged(QString)), this, SLOT(slotModified()));
340         connect(m_view.luma_softness, SIGNAL(valueChanged(int)), this, SLOT(slotModified()));
341         connect(m_view.luma_file, SIGNAL(currentIndexChanged(int)), this, SLOT(slotModified()));
342         connect(m_view.animation, SIGNAL(currentIndexChanged(int)), this, SLOT(slotModified()));
343
344
345         connect(m_view.slide_fade, SIGNAL(stateChanged(int)), this, SLOT(slotEnableLuma(int)));
346         connect(m_view.slide_luma, SIGNAL(stateChanged(int)), this, SLOT(slotEnableLumaFile(int)));
347         connect(m_view.image_type, SIGNAL(currentIndexChanged(int)), this, SLOT(parseFolder()));
348
349     } else if (t != AUDIO) {
350         m_view.tabWidget->removeTab(IMAGETAB);
351         m_view.tabWidget->removeTab(SLIDETAB);
352         m_view.tabWidget->removeTab(COLORTAB);
353
354         PropertiesViewDelegate *del1 = new PropertiesViewDelegate(this);
355         PropertiesViewDelegate *del2 = new PropertiesViewDelegate(this);
356         m_view.clip_vproperties->setItemDelegate(del1);
357         m_view.clip_aproperties->setItemDelegate(del2);
358         m_view.clip_aproperties->setStyleSheet(QString("QTreeWidget { background-color: transparent;}"));
359         m_view.clip_vproperties->setStyleSheet(QString("QTreeWidget { background-color: transparent;}"));
360
361         if (props.contains("videocodec"))
362             QTreeWidgetItem *item = new QTreeWidgetItem(m_view.clip_vproperties, QStringList() << i18n("Video codec") << props.value("videocodec"));
363
364         if (props.contains("frame_size"))
365             new QTreeWidgetItem(m_view.clip_vproperties, QStringList() << i18n("Frame size") << props.value("frame_size"));
366
367         if (props.contains("fps")) {
368             new QTreeWidgetItem(m_view.clip_vproperties, QStringList() << i18n("Frame rate") << props.value("fps"));
369             if (!m_view.clip_framerate->isEnabled()) m_view.clip_framerate->setValue(props.value("fps").toDouble());
370         }
371         if (props.contains("aspect_ratio"))
372             new QTreeWidgetItem(m_view.clip_vproperties, QStringList() << i18n("Pixel aspect ratio") << props.value("aspect_ratio"));
373
374         if (props.contains("pix_fmt"))
375             new QTreeWidgetItem(m_view.clip_vproperties, QStringList() << i18n("Pixel format") << props.value("pix_fmt"));
376
377         if (props.contains("colorspace"))
378             new QTreeWidgetItem(m_view.clip_vproperties, QStringList() << i18n("Colorspace") << ProfilesDialog::getColorspaceDescription(props.value("colorspace").toInt()));
379
380         int width = 180.0 * KdenliveSettings::project_display_ratio();
381         if (width % 2 == 1) width++;
382         QPixmap pix = m_clip->thumbProducer()->getImage(url, m_clip->getClipThumbFrame(), width, 180);
383         m_view.clip_thumb->setPixmap(pix);
384         if (t == IMAGE || t == VIDEO) m_view.tabWidget->removeTab(AUDIOTAB);
385     } else {
386         m_view.tabWidget->removeTab(IMAGETAB);
387         m_view.tabWidget->removeTab(SLIDETAB);
388         m_view.tabWidget->removeTab(COLORTAB);
389         m_view.tabWidget->removeTab(VIDEOTAB);
390         m_view.clip_thumb->setHidden(true);
391     }
392
393     if (t != SLIDESHOW && t != COLOR) {
394         KFileItem f(KFileItem::Unknown, KFileItem::Unknown, url, true);
395         m_view.clip_filesize->setText(KIO::convertSize(f.size()));
396     } else {
397         m_view.clip_filesize->setHidden(true);
398         m_view.label_size->setHidden(true);
399     }
400     m_view.clip_duration->setInputMask("");
401     m_view.clip_duration->setValidator(tc.validator());
402     m_view.clip_duration->setText(tc.getTimecode(m_clip->duration()));
403     if (t != IMAGE && t != COLOR && t != TEXT) m_view.clip_duration->setReadOnly(true);
404     else connect(m_view.clip_duration, SIGNAL(editingFinished()), this, SLOT(slotCheckMaxLength()));
405
406     // markers
407     m_view.marker_new->setIcon(KIcon("document-new"));
408     m_view.marker_new->setToolTip(i18n("Add marker"));
409     m_view.marker_edit->setIcon(KIcon("document-properties"));
410     m_view.marker_edit->setToolTip(i18n("Edit marker"));
411     m_view.marker_delete->setIcon(KIcon("trash-empty"));
412     m_view.marker_delete->setToolTip(i18n("Delete marker"));
413
414     slotFillMarkersList();
415     connect(m_view.marker_new, SIGNAL(clicked()), this, SLOT(slotAddMarker()));
416     connect(m_view.marker_edit, SIGNAL(clicked()), this, SLOT(slotEditMarker()));
417     connect(m_view.marker_delete, SIGNAL(clicked()), this, SLOT(slotDeleteMarker()));
418     connect(m_view.markers_list, SIGNAL(doubleClicked(const QModelIndex &)), this, SLOT(slotEditMarker()));
419
420     connect(this, SIGNAL(accepted()), this, SLOT(slotApplyProperties()));
421     connect(m_view.buttonBox->button(QDialogButtonBox::Apply), SIGNAL(clicked()), this, SLOT(slotApplyProperties()));
422     m_view.buttonBox->button(QDialogButtonBox::Apply)->setEnabled(false);
423     //adjustSize();
424 }
425
426
427 // Used for multiple clips editing
428 ClipProperties::ClipProperties(QList <DocClipBase *>cliplist, Timecode tc, QMap <QString, QString> commonproperties, QWidget * parent) :
429     QDialog(parent),
430     m_clip(NULL),
431     m_tc(tc),
432     m_fps(0),
433     m_count(0),
434     m_clipNeedsRefresh(false),
435     m_clipNeedsReLoad(false)
436 {
437     setFont(KGlobalSettings::toolBarFont());
438     m_view.setupUi(this);
439     QMap <QString, QString> props = cliplist.at(0)->properties();
440     m_old_props = commonproperties;
441
442     if (commonproperties.contains("force_aspect_num") && !commonproperties.value("force_aspect_num").isEmpty() && commonproperties.value("force_aspect_den").toInt() > 0) {
443         m_view.clip_force_ar->setChecked(true);
444         m_view.clip_ar_num->setEnabled(true);
445         m_view.clip_ar_den->setEnabled(true);
446         m_view.clip_ar_num->setValue(commonproperties.value("force_aspect_num").toInt());
447         m_view.clip_ar_den->setValue(commonproperties.value("force_aspect_den").toInt());
448     }
449
450     if (commonproperties.contains("force_fps") && !commonproperties.value("force_fps").isEmpty() && commonproperties.value("force_fps").toDouble() > 0) {
451         m_view.clip_force_framerate->setChecked(true);
452         m_view.clip_framerate->setEnabled(true);
453         m_view.clip_framerate->setValue(commonproperties.value("force_fps").toDouble());
454     }
455
456     if (commonproperties.contains("force_progressive") && !commonproperties.value("force_progressive").isEmpty()) {
457         m_view.clip_force_progressive->setChecked(true);
458         m_view.clip_progressive->setEnabled(true);
459         m_view.clip_progressive->setValue(commonproperties.value("force_progressive").toInt());
460     }
461
462     if (commonproperties.contains("force_tff") && !commonproperties.value("force_tff").isEmpty()) {
463         m_view.clip_force_fieldorder->setChecked(true);
464         m_view.clip_fieldorder->setEnabled(true);
465         m_view.clip_fieldorder->setCurrentIndex(commonproperties.value("force_tff").toInt());
466     }
467     
468     if (commonproperties.contains("threads") && !commonproperties.value("threads").isEmpty() && commonproperties.value("threads").toInt() != 1) {
469         m_view.clip_force_threads->setChecked(true);
470         m_view.clip_threads->setEnabled(true);
471         m_view.clip_threads->setValue(commonproperties.value("threads").toInt());
472     }
473
474     if (commonproperties.contains("video_index") && !commonproperties.value("video_index").isEmpty() && commonproperties.value("video_index").toInt() != 0) {
475         m_view.clip_force_vindex->setChecked(true);
476         m_view.clip_vindex->setEnabled(true);
477         m_view.clip_vindex->setValue(commonproperties.value("video_index").toInt());
478     }
479
480     if (commonproperties.contains("audio_index") && !commonproperties.value("audio_index").isEmpty() && commonproperties.value("audio_index").toInt() != 0) {
481         m_view.clip_force_aindex->setChecked(true);
482         m_view.clip_aindex->setEnabled(true);
483         m_view.clip_aindex->setValue(commonproperties.value("audio_index").toInt());
484     }
485
486     if (props.contains("audio_max")) {
487         m_view.clip_aindex->setMaximum(props.value("audio_max").toInt());
488     }
489
490     if (props.contains("video_max")) {
491         m_view.clip_vindex->setMaximum(props.value("video_max").toInt());
492     }
493     
494     m_view.clip_colorspace->addItem(ProfilesDialog::getColorspaceDescription(601), 601);
495     m_view.clip_colorspace->addItem(ProfilesDialog::getColorspaceDescription(709), 709);
496     m_view.clip_colorspace->addItem(ProfilesDialog::getColorspaceDescription(240), 240);
497     
498     if (commonproperties.contains("force_colorspace") && !commonproperties.value("force_colorspace").isEmpty() && commonproperties.value("force_colorspace").toInt() != 0) {
499         m_view.clip_force_colorspace->setChecked(true);
500         m_view.clip_colorspace->setEnabled(true);
501         m_view.clip_colorspace->setCurrentIndex(m_view.clip_colorspace->findData(commonproperties.value("force_colorspace").toInt()));
502     }
503     
504     if (commonproperties.contains("full_luma") && !commonproperties.value("full_luma").isEmpty()) {
505         m_view.clip_full_luma->setChecked(true);
506     }
507
508     connect(m_view.clip_force_ar, SIGNAL(toggled(bool)), m_view.clip_ar_num, SLOT(setEnabled(bool)));
509     connect(m_view.clip_force_ar, SIGNAL(toggled(bool)), m_view.clip_ar_den, SLOT(setEnabled(bool)));
510     connect(m_view.clip_force_progressive, SIGNAL(toggled(bool)), m_view.clip_progressive, SLOT(setEnabled(bool)));
511     connect(m_view.clip_force_threads, SIGNAL(toggled(bool)), m_view.clip_threads, SLOT(setEnabled(bool)));
512     connect(m_view.clip_force_vindex, SIGNAL(toggled(bool)), m_view.clip_vindex, SLOT(setEnabled(bool)));
513     connect(m_view.clip_force_aindex, SIGNAL(toggled(bool)), m_view.clip_aindex, SLOT(setEnabled(bool)));
514     connect(m_view.clip_force_out, SIGNAL(toggled(bool)), m_view.clip_out, SLOT(setEnabled(bool)));
515     connect(m_view.clip_force_colorspace, SIGNAL(toggled(bool)), m_view.clip_colorspace, SLOT(setEnabled(bool)));
516
517     m_view.tabWidget->removeTab(METATAB);
518     m_view.tabWidget->removeTab(MARKERTAB);
519     m_view.tabWidget->removeTab(IMAGETAB);
520     m_view.tabWidget->removeTab(SLIDETAB);
521     m_view.tabWidget->removeTab(COLORTAB);
522     m_view.tabWidget->removeTab(AUDIOTAB);
523     m_view.tabWidget->removeTab(VIDEOTAB);
524
525     m_view.clip_path->setHidden(true);
526     m_view.label_path->setHidden(true);
527     m_view.label_description->setHidden(true);
528     m_view.label_size->setHidden(true);
529     m_view.clip_filesize->setHidden(true);
530     m_view.clip_filesize->setHidden(true);
531     m_view.clip_path->setHidden(true);
532     m_view.clip_description->setHidden(true);
533     m_view.clip_thumb->setHidden(true);
534     m_view.label_duration->setHidden(true);
535     m_view.clip_duration->setHidden(true);
536
537     if (commonproperties.contains("out")) {
538         if (commonproperties.value("out").toInt() > 0) {
539             m_view.clip_force_out->setChecked(true);
540             m_view.clip_out->setText(m_tc.getTimecodeFromFrames(commonproperties.value("out").toInt()));
541         } else m_view.clip_out->setText(KdenliveSettings::image_duration());
542     } else {
543         m_view.clip_force_out->setHidden(true);
544         m_view.clip_out->setHidden(true);
545     }
546 }
547
548 ClipProperties::~ClipProperties()
549 {
550     QAbstractItemDelegate *del1 = m_view.clip_vproperties->itemDelegate();
551     if (del1) delete del1;
552     QAbstractItemDelegate *del2 = m_view.clip_aproperties->itemDelegate();
553     if (del2) delete del2;
554 }
555
556 void ClipProperties::slotApplyProperties()
557 {
558     if (m_clip != NULL)
559         emit applyNewClipProperties(m_clip->getId(), m_clip->properties(), properties(), needsTimelineRefresh(), needsTimelineReload());
560     m_view.buttonBox->button(QDialogButtonBox::Apply)->setEnabled(false);
561 }
562
563 void ClipProperties::disableClipId(const QString &id)
564 {
565     if (m_clip && m_view.buttonBox->button(QDialogButtonBox::Ok)->isEnabled()) {
566         if (m_clip->getId() == id) {
567             // clip was removed from project, close this properties dialog
568             close();
569         }
570     }
571 }
572
573 void ClipProperties::slotModified()
574 {
575     m_view.buttonBox->button(QDialogButtonBox::Apply)->setEnabled(true);
576 }
577
578
579 void ClipProperties::slotEnableLuma(int state)
580 {
581     bool enable = false;
582     if (state == Qt::Checked) enable = true;
583     m_view.luma_duration->setEnabled(enable);
584     m_view.luma_duration_frames->setEnabled(enable);
585     m_view.slide_luma->setEnabled(enable);
586     if (enable) {
587         m_view.luma_file->setEnabled(m_view.slide_luma->isChecked());
588     } else m_view.luma_file->setEnabled(false);
589     m_view.label_softness->setEnabled(m_view.slide_luma->isChecked() && enable);
590     m_view.luma_softness->setEnabled(m_view.label_softness->isEnabled());
591 }
592
593 void ClipProperties::slotEnableLumaFile(int state)
594 {
595     bool enable = false;
596     if (state == Qt::Checked) enable = true;
597     m_view.luma_file->setEnabled(enable);
598     m_view.luma_softness->setEnabled(enable);
599     m_view.label_softness->setEnabled(enable);
600 }
601
602 void ClipProperties::slotFillMarkersList()
603 {
604     m_view.markers_list->clear();
605     QList < CommentedTime > marks = m_clip->commentedSnapMarkers();
606     for (int count = 0; count < marks.count(); ++count) {
607         QString time = m_tc.getTimecode(marks[count].time());
608         QStringList itemtext;
609         itemtext << time << marks[count].comment();
610         (void) new QTreeWidgetItem(m_view.markers_list, itemtext);
611     }
612 }
613
614 void ClipProperties::slotAddMarker()
615 {
616     CommentedTime marker(GenTime(), i18n("Marker"));
617     MarkerDialog d(m_clip, marker, m_tc, i18n("Add Marker"), this);
618     if (d.exec() == QDialog::Accepted) {
619         emit addMarker(m_clip->getId(), d.newMarker().time(), d.newMarker().comment());
620     }
621     QTimer::singleShot(500, this, SLOT(slotFillMarkersList()));
622 }
623
624 void ClipProperties::slotEditMarker()
625 {
626     QList < CommentedTime > marks = m_clip->commentedSnapMarkers();
627     int pos = m_view.markers_list->currentIndex().row();
628     if (pos < 0 || pos > marks.count() - 1) return;
629     MarkerDialog d(m_clip, marks.at(pos), m_tc, i18n("Edit Marker"), this);
630     if (d.exec() == QDialog::Accepted) {
631         emit addMarker(m_clip->getId(), d.newMarker().time(), d.newMarker().comment());
632     }
633     QTimer::singleShot(500, this, SLOT(slotFillMarkersList()));
634 }
635
636 void ClipProperties::slotDeleteMarker()
637 {
638     QList < CommentedTime > marks = m_clip->commentedSnapMarkers();
639     int pos = m_view.markers_list->currentIndex().row();
640     if (pos < 0 || pos > marks.count() - 1) return;
641     emit addMarker(m_clip->getId(), marks.at(pos).time(), QString());
642
643     QTimer::singleShot(500, this, SLOT(slotFillMarkersList()));
644 }
645
646 const QString &ClipProperties::clipId() const
647 {
648     return m_clip->getId();
649 }
650
651
652 QMap <QString, QString> ClipProperties::properties()
653 {
654     QMap <QString, QString> props;
655     CLIPTYPE t = UNKNOWN;
656     if (m_clip != NULL) {
657         t = m_clip->clipType();
658         m_old_props = m_clip->properties();
659     }
660
661     int aspectNumerator = m_view.clip_ar_num->value();
662     int aspectDenominator = m_view.clip_ar_den->value();
663     if (m_view.clip_force_ar->isChecked()) {
664         if (aspectNumerator != m_old_props.value("force_aspect_num").toInt() ||
665             aspectDenominator != m_old_props.value("force_aspect_den").toInt()) {
666             props["force_aspect_num"] = QString::number(aspectNumerator);
667             props["force_aspect_den"] = QString::number(aspectDenominator);
668             props["force_aspect_ratio"].clear();
669             m_clipNeedsRefresh = true;
670         }
671     } else {
672         if (m_old_props.contains("force_aspect_num")) {
673             props["force_aspect_num"].clear();
674             m_clipNeedsRefresh = true;
675         }
676         if (m_old_props.contains("force_aspect_den")) {
677             props["force_aspect_den"].clear();
678             m_clipNeedsRefresh = true;
679         }
680     }
681
682     double fps = m_view.clip_framerate->value();
683     if (m_view.clip_force_framerate->isChecked()) {
684         if (fps != m_old_props.value("force_fps").toDouble()) {
685             props["force_fps"] = QString::number(fps);
686             m_clipNeedsRefresh = true;
687         }
688     } else if (m_old_props.contains("force_fps")) {
689         props["force_fps"].clear();
690         m_clipNeedsRefresh = true;
691     }
692
693     int progressive = m_view.clip_progressive->value();
694     if (m_view.clip_force_progressive->isChecked()) {
695         if (progressive != m_old_props.value("force_progressive").toInt()) {
696             props["force_progressive"] = QString::number(progressive);
697         }
698     } else if (m_old_props.contains("force_progressive")) {
699         props["force_progressive"].clear();
700     }
701
702     int fieldOrder = m_view.clip_fieldorder->currentIndex();
703     if (m_view.clip_force_fieldorder->isChecked()) {
704         if (fieldOrder != m_old_props.value("force_tff").toInt()) {
705             props["force_tff"] = QString::number(fieldOrder);
706         }
707     } else if (m_old_props.contains("force_tff")) {
708         props["force_tff"].clear();
709     }
710
711     int threads = m_view.clip_threads->value();
712     if (m_view.clip_force_threads->isChecked()) {
713         if (threads != m_old_props.value("threads").toInt()) {
714             props["threads"] = QString::number(threads);
715         }
716     } else if (m_old_props.contains("threads")) {
717         props["threads"].clear();
718     }
719
720     int vindex = m_view.clip_vindex->value();
721     if (m_view.clip_force_vindex->isChecked()) {
722         if (vindex != m_old_props.value("video_index").toInt()) {
723             props["video_index"] = QString::number(vindex);
724         }
725     } else if (m_old_props.contains("video_index")) {
726         props["video_index"].clear();
727     }
728
729     int aindex = m_view.clip_aindex->value();
730     if (m_view.clip_force_aindex->isChecked()) {
731         if (aindex != m_old_props.value("audio_index").toInt()) {
732             props["audio_index"] = QString::number(aindex);
733         }
734     } else if (m_old_props.contains("audio_index")) {
735         props["audio_index"].clear();
736     }
737     
738     int colorspace = m_view.clip_colorspace->itemData(m_view.clip_colorspace->currentIndex()).toInt();
739     if (m_view.clip_force_colorspace->isChecked()) {
740         if (colorspace != m_old_props.value("force_colorspace").toInt()) {
741             props["force_colorspace"] = QString::number(colorspace);
742             m_clipNeedsRefresh = true;
743         }
744     } else if (m_old_props.contains("force_colorspace")) {
745         props["force_colorspace"].clear();
746         m_clipNeedsRefresh = true;
747     }
748
749     if (m_view.clip_full_luma->isChecked()) {
750         props["full_luma"] = QString::number(1);
751         m_clipNeedsRefresh = true;
752     } else if (m_old_props.contains("full_luma")) {
753         props["full_luma"].clear();
754         m_clipNeedsRefresh = true;
755     }
756
757     // If we adjust several clips, return now
758     if (m_clip == NULL) {
759         if (m_view.clip_out->isEnabled()) {
760             int duration = m_tc.getFrameCount(m_view.clip_out->text());
761             if (duration != m_old_props.value("out").toInt()) {
762                 props["out"] = QString::number(duration - 1);
763             }
764         }
765         return props;
766     }
767
768     if (m_old_props.value("description") != m_view.clip_description->text())
769         props["description"] = m_view.clip_description->text();
770
771     if (t == COLOR) {
772         QString new_color = m_view.clip_color->color().name();
773         if (new_color != QString('#' + m_old_props.value("colour").right(8).left(6))) {
774             m_clipNeedsRefresh = true;
775             props["colour"] = "0x" + new_color.right(6) + "ff";
776         }
777         int duration = m_tc.getFrameCount(m_view.clip_duration->text());
778         if (duration != m_clip->duration().frames(m_fps)) {
779             props["out"] = QString::number(duration - 1);
780         }
781     } else if (t == IMAGE) {
782         if ((int) m_view.image_transparency->isChecked() != m_old_props.value("transparency").toInt()) {
783             props["transparency"] = QString::number((int)m_view.image_transparency->isChecked());
784             //m_clipNeedsRefresh = true;
785         }
786         int duration = m_tc.getFrameCount(m_view.clip_duration->text());
787         if (duration != m_clip->duration().frames(m_fps)) {
788             props["out"] = QString::number(duration - 1);
789         }
790     } else if (t == SLIDESHOW) {
791         QString value = QString::number((int) m_view.slide_loop->isChecked());
792         if (m_old_props.value("loop") != value) props["loop"] = value;
793         value = QString::number((int) m_view.slide_crop->isChecked());
794         if (m_old_props.value("crop") != value) props["crop"] = value;
795         value = QString::number((int) m_view.slide_fade->isChecked());
796         if (m_old_props.value("fade") != value) props["fade"] = value;
797         value = QString::number((int) m_view.luma_softness->value());
798         if (m_old_props.value("softness") != value) props["softness"] = value;
799
800         bool isMime = !(m_view.clip_path->text().contains('%'));
801         if (isMime) {
802             QString extension = "/.all." + m_view.image_type->itemData(m_view.image_type->currentIndex()).toString();
803             QString new_path = m_view.clip_path->text() + extension;
804             if (new_path != m_old_props.value("resource")) {
805                 m_clipNeedsReLoad = true;
806                 props["resource"] = new_path;
807                 kDebug() << "////  SLIDE EDIT, NEW:" << new_path << ", OLD; " << m_old_props.value("resource");
808             }
809         }
810         int duration;
811         if (m_view.slide_duration_format->currentIndex() == 1) {
812             // we are in frames mode
813             duration = m_view.slide_duration_frames->value();
814         } else duration = m_tc.getFrameCount(m_view.slide_duration->text());
815         if (duration != m_old_props.value("ttl").toInt()) {
816             m_clipNeedsRefresh = true;
817             props["ttl"] = QString::number(duration);
818             props["length"] = QString::number(duration * m_count);
819         }
820
821         if (duration * m_count - 1 != m_old_props.value("out").toInt()) {
822             m_clipNeedsRefresh = true;
823             props["out"] = QString::number(duration * m_count - 1);
824         }
825         if (m_view.slide_fade->isChecked()) {
826             int luma_duration;
827             if (m_view.slide_duration_format->currentIndex() == 1) {
828                 // we are in frames mode
829                 luma_duration = m_view.luma_duration_frames->value();
830             } else luma_duration = m_tc.getFrameCount(m_view.luma_duration->text());
831             if (luma_duration != m_old_props.value("luma_duration").toInt()) {
832                 m_clipNeedsRefresh = true;
833                 props["luma_duration"] = QString::number(luma_duration);
834             }
835             QString lumaFile;
836             if (m_view.slide_luma->isChecked())
837                 lumaFile = m_view.luma_file->itemData(m_view.luma_file->currentIndex()).toString();
838             if (lumaFile != m_old_props.value("luma_file")) {
839                 m_clipNeedsRefresh = true;
840                 props["luma_file"] = lumaFile;
841             }
842         } else {
843             if (!m_old_props.value("luma_file").isEmpty()) {
844                 props["luma_file"].clear();
845             }
846         }
847
848         QString animation = m_view.animation->itemData(m_view.animation->currentIndex()).toString();
849         if (animation != m_old_props.value("animation")) {
850             if (animation.isEmpty()) {
851                 props["animation"].clear();
852             } else {
853                 props["animation"] = animation;
854             }
855             m_clipNeedsRefresh = true;
856         }
857     }
858     return props;
859 }
860
861 bool ClipProperties::needsTimelineRefresh() const
862 {
863     return m_clipNeedsRefresh;
864 }
865
866 bool ClipProperties::needsTimelineReload() const
867 {
868     return m_clipNeedsReLoad;
869 }
870
871
872 void ClipProperties::parseFolder()
873 {
874     QString path = m_view.clip_path->text();
875     bool isMime = !(path.contains('%'));
876     if (!isMime) path = KUrl(path).directory();
877     QDir dir(path);
878
879     QStringList filters;
880     QString extension;
881
882     if (isMime) {
883         // TODO: improve jpeg image detection with extension like jpeg, requires change in MLT image producers
884         filters << "*." + m_view.image_type->itemData(m_view.image_type->currentIndex()).toString();
885         extension = "/.all." + m_view.image_type->itemData(m_view.image_type->currentIndex()).toString();
886         dir.setNameFilters(filters);
887     }
888
889     QStringList result = dir.entryList(QDir::Files);
890
891     if (!isMime) {
892         // find pattern
893         QString filter = KUrl(m_view.clip_path->text()).fileName();
894         QString ext = filter.section('.', -1);
895         filter = filter.section('%', 0, -2);
896         QString regexp = "^" + filter + "\\d+\\." + ext + "$";
897         QRegExp rx(regexp);
898         QStringList entries;
899         foreach(const QString & path, result) {
900             if (rx.exactMatch(path)) entries << path;
901         }
902         result = entries;
903     }
904
905     m_count = result.count();
906     m_view.buttonBox->button(QDialogButtonBox::Apply)->setEnabled(m_count > 0);
907     if (m_count == 0) {
908         // no images, do not accept that
909         m_view.slide_info->setText(i18n("No image found"));
910         m_view.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
911         return;
912     }
913     m_view.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
914     m_view.slide_info->setText(i18np("1 image found", "%1 images found", m_count));
915     QDomElement xml = m_clip->toXML();
916     xml.setAttribute("resource", m_view.clip_path->text() + extension);
917     int width = 180.0 * KdenliveSettings::project_display_ratio();
918     if (width % 2 == 1) width++;
919     QString filePath = m_view.clip_path->text();
920     if (isMime) filePath.append(extension);
921     QPixmap pix = m_clip->thumbProducer()->getImage(KUrl(filePath), 1, width, 180);
922     QMap <QString, QString> props = m_clip->properties();
923     m_view.clip_duration->setText(m_tc.getTimecodeFromFrames(props.value("ttl").toInt() * m_count));
924     m_view.clip_thumb->setPixmap(pix);
925 }
926
927 void ClipProperties::slotCheckMaxLength()
928 {
929     if (m_clip->maxDuration() == GenTime()) return;
930     int duration = m_tc.getFrameCount(m_view.clip_duration->text());
931     if (duration > m_clip->maxDuration().frames(m_fps)) {
932         m_view.clip_duration->setText(m_tc.getTimecode(m_clip->maxDuration()));
933     }
934 }
935
936 void ClipProperties::slotUpdateDurationFormat(int ix)
937 {
938     bool framesFormat = ix == 1;
939     if (framesFormat) {
940         // switching to frames count, update widget
941         m_view.slide_duration_frames->setValue(m_tc.getFrameCount(m_view.slide_duration->text()));
942         m_view.luma_duration_frames->setValue(m_tc.getFrameCount(m_view.luma_duration->text()));
943         m_view.slide_duration->setHidden(true);
944         m_view.luma_duration->setHidden(true);
945         m_view.slide_duration_frames->setHidden(false);
946         m_view.luma_duration_frames->setHidden(false);
947     } else {
948         // switching to timecode format
949         m_view.slide_duration->setText(m_tc.getTimecodeFromFrames(m_view.slide_duration_frames->value()));
950         m_view.luma_duration->setText(m_tc.getTimecodeFromFrames(m_view.luma_duration_frames->value()));
951         m_view.slide_duration_frames->setHidden(true);
952         m_view.luma_duration_frames->setHidden(true);
953         m_view.slide_duration->setHidden(false);
954         m_view.luma_duration->setHidden(false);
955     }
956 }
957
958 #include "clipproperties.moc"
959
960