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