1 /***************************************************************************
2 * Copyright (C) 2008 by Jean-Baptiste Mardelle (jb@kdenlive.org) *
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. *
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. *
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 ***************************************************************************/
22 #include <KStandardDirs>
26 #include "kdenlivesettings.h"
27 #include "clipproperties.h"
29 #include "markerdialog.h"
31 static const int VIDEOTAB = 0;
32 static const int AUDIOTAB = 1;
33 static const int COLORTAB = 2;
34 static const int SLIDETAB = 3;
35 static const int IMAGETAB = 4;
36 static const int MARKERTAB = 5;
37 static const int ADVANCEDTAB = 6;
39 static const int TYPE_JPEG = 0;
40 static const int TYPE_PNG = 1;
41 static const int TYPE_BMP = 2;
42 static const int TYPE_GIF = 3;
44 ClipProperties::ClipProperties(DocClipBase *clip, Timecode tc, double fps, QWidget * parent): QDialog(parent), m_tc(tc), m_clip(clip), m_fps(fps), m_clipNeedsRefresh(false), m_count(0) {
45 setFont(KGlobalSettings::toolBarFont());
47 KUrl url = m_clip->fileURL();
48 m_view.clip_path->setText(url.path());
49 m_view.clip_description->setText(m_clip->description());
50 QMap <QString, QString> props = m_clip->properties();
52 if (props.contains("force_aspect_ratio") && props.value("force_aspect_ratio").toDouble() > 0) {
53 m_view.clip_force_ar->setChecked(true);
54 m_view.clip_ar->setEnabled(true);
55 m_view.clip_ar->setValue(props.value("force_aspect_ratio").toDouble());
58 if (props.contains("threads") && props.value("threads").toInt() != 1) {
59 m_view.clip_force_threads->setChecked(true);
60 m_view.clip_threads->setEnabled(true);
61 m_view.clip_threads->setValue(props.value("threads").toInt());
64 if (props.contains("video_index") && props.value("video_index").toInt() != 0) {
65 m_view.clip_force_vindex->setChecked(true);
66 m_view.clip_vindex->setEnabled(true);
67 m_view.clip_vindex->setValue(props.value("video_index").toInt());
70 if (props.contains("audio_index") && props.value("audio_index").toInt() != 0) {
71 m_view.clip_force_aindex->setChecked(true);
72 m_view.clip_aindex->setEnabled(true);
73 m_view.clip_aindex->setValue(props.value("audio_index").toInt());
76 if (props.contains("audio_max")) {
77 m_view.clip_aindex->setMaximum(props.value("audio_max").toInt());
80 if (props.contains("video_max")) {
81 m_view.clip_vindex->setMaximum(props.value("video_max").toInt());
84 connect(m_view.clip_force_ar, SIGNAL(toggled(bool)), m_view.clip_ar, SLOT(setEnabled(bool)));
85 connect(m_view.clip_force_threads, SIGNAL(toggled(bool)), m_view.clip_threads, SLOT(setEnabled(bool)));
86 connect(m_view.clip_force_vindex, SIGNAL(toggled(bool)), m_view.clip_vindex, SLOT(setEnabled(bool)));
87 connect(m_view.clip_force_aindex, SIGNAL(toggled(bool)), m_view.clip_aindex, SLOT(setEnabled(bool)));
89 if (props.contains("audiocodec"))
90 m_view.clip_acodec->setText(props.value("audiocodec"));
91 if (props.contains("frequency"))
92 m_view.clip_frequency->setText(props.value("frequency"));
93 if (props.contains("channels"))
94 m_view.clip_channels->setText(props.value("channels"));
96 CLIPTYPE t = m_clip->clipType();
97 if (t != AUDIO && t != AV) {
98 m_view.clip_force_aindex->setEnabled(false);
101 if (t != VIDEO && t != AV) {
102 m_view.clip_force_vindex->setEnabled(false);
106 m_view.tabWidget->removeTab(SLIDETAB);
107 m_view.tabWidget->removeTab(COLORTAB);
108 m_view.tabWidget->removeTab(AUDIOTAB);
109 m_view.tabWidget->removeTab(VIDEOTAB);
110 if (props.contains("frame_size"))
111 m_view.image_size->setText(props.value("frame_size"));
112 if (props.contains("transparency"))
113 m_view.image_transparency->setChecked(props.value("transparency").toInt());
114 } else if (t == COLOR) {
115 m_view.clip_path->setEnabled(false);
116 m_view.tabWidget->removeTab(IMAGETAB);
117 m_view.tabWidget->removeTab(SLIDETAB);
118 m_view.tabWidget->removeTab(AUDIOTAB);
119 m_view.tabWidget->removeTab(VIDEOTAB);
120 m_view.clip_thumb->setHidden(true);
121 m_view.clip_color->setColor(QColor("#" + props.value("colour").right(8).left(6)));
122 } else if (t == SLIDESHOW) {
123 m_view.clip_path->setText(url.directory());
124 m_view.tabWidget->removeTab(IMAGETAB);
125 m_view.tabWidget->removeTab(COLORTAB);
126 m_view.tabWidget->removeTab(AUDIOTAB);
127 m_view.tabWidget->removeTab(VIDEOTAB);
129 types << "JPG" << "PNG" << "BMP" << "GIF";
130 m_view.image_type->addItems(types);
131 m_view.slide_loop->setChecked(props.value("loop").toInt());
132 m_view.slide_fade->setChecked(props.value("fade").toInt());
133 m_view.luma_softness->setValue(props.value("softness").toInt());
134 QString path = props.value("resource");
135 if (path.endsWith("png")) m_view.image_type->setCurrentIndex(TYPE_PNG);
136 else if (path.endsWith("bmp")) m_view.image_type->setCurrentIndex(TYPE_BMP);
137 else if (path.endsWith("gif")) m_view.image_type->setCurrentIndex(TYPE_GIF);
138 m_view.slide_duration->setText(tc.getTimecodeFromFrames(props.value("ttl").toInt()));
141 m_view.luma_duration->setText(tc.getTimecodeFromFrames(props.value("luma_duration").toInt()));
142 QString lumaFile = props.value("luma_file");
144 // Check for Kdenlive installed luma files
146 filters << "*.pgm" << "*.png";
148 QStringList customLumas = KGlobal::dirs()->findDirs("appdata", "lumas");
149 foreach(const QString &folder, customLumas) {
150 QStringList filesnames = QDir(folder).entryList(filters, QDir::Files);
151 foreach(const QString &fname, filesnames) {
152 m_view.luma_file->addItem(KIcon(folder + '/' + fname), fname, folder + '/' + fname);
156 // Check for MLT lumas
157 QString profilePath = KdenliveSettings::mltpath();
158 QString folder = profilePath.section('/', 0, -3);
159 folder.append("/lumas/PAL"); // TODO: cleanup the PAL / NTSC mess in luma files
160 QDir lumafolder(folder);
161 QStringList filesnames = lumafolder.entryList(filters, QDir::Files);
162 foreach(const QString &fname, filesnames) {
163 m_view.luma_file->addItem(KIcon(folder + '/' + fname), fname, folder + '/' + fname);
166 slotEnableLuma(m_view.slide_fade->isChecked());
167 slotEnableLumaFile(m_view.slide_luma->isChecked());
169 if (!lumaFile.isEmpty()) {
170 m_view.slide_luma->setChecked(true);
171 m_view.luma_file->setCurrentIndex(m_view.luma_file->findData(lumaFile));
172 } else m_view.luma_file->setEnabled(false);
173 connect(m_view.slide_fade, SIGNAL(stateChanged(int)), this, SLOT(slotEnableLuma(int)));
174 connect(m_view.slide_luma, SIGNAL(stateChanged(int)), this, SLOT(slotEnableLumaFile(int)));
176 connect(m_view.image_type, SIGNAL(currentIndexChanged(int)), this, SLOT(parseFolder()));
177 } else if (t != AUDIO) {
178 m_view.tabWidget->removeTab(IMAGETAB);
179 m_view.tabWidget->removeTab(SLIDETAB);
180 m_view.tabWidget->removeTab(COLORTAB);
181 if (props.contains("frame_size"))
182 m_view.clip_size->setText(props.value("frame_size"));
183 if (props.contains("videocodec"))
184 m_view.clip_vcodec->setText(props.value("videocodec"));
185 if (props.contains("fps"))
186 m_view.clip_fps->setText(props.value("fps"));
187 if (props.contains("aspect_ratio"))
188 m_view.clip_ratio->setText(props.value("aspect_ratio"));
190 QPixmap pix = m_clip->thumbProducer()->getImage(url, m_clip->getClipThumbFrame(), 240, 180);
191 m_view.clip_thumb->setPixmap(pix);
192 if (t == IMAGE || t == VIDEO) m_view.tabWidget->removeTab(AUDIOTAB);
194 m_view.tabWidget->removeTab(IMAGETAB);
195 m_view.tabWidget->removeTab(SLIDETAB);
196 m_view.tabWidget->removeTab(COLORTAB);
197 m_view.tabWidget->removeTab(VIDEOTAB);
198 m_view.clip_thumb->setHidden(true);
201 KFileItem f(KFileItem::Unknown, KFileItem::Unknown, url, true);
202 m_view.clip_filesize->setText(KIO::convertSize(f.size()));
203 m_view.clip_duration->setText(tc.getTimecode(m_clip->duration(), m_fps));
204 if (t != IMAGE && t != COLOR && t != TEXT) m_view.clip_duration->setReadOnly(true);
205 else connect(m_view.clip_duration, SIGNAL(editingFinished()), this, SLOT(slotCheckMaxLength()));
208 m_view.marker_new->setIcon(KIcon("document-new"));
209 m_view.marker_new->setToolTip(i18n("Add marker"));
210 m_view.marker_edit->setIcon(KIcon("document-properties"));
211 m_view.marker_edit->setToolTip(i18n("Edit marker"));
212 m_view.marker_delete->setIcon(KIcon("trash-empty"));
213 m_view.marker_delete->setToolTip(i18n("Delete marker"));
215 slotFillMarkersList();
216 connect(m_view.marker_new, SIGNAL(clicked()), this, SLOT(slotAddMarker()));
217 connect(m_view.marker_edit, SIGNAL(clicked()), this, SLOT(slotEditMarker()));
218 connect(m_view.marker_delete, SIGNAL(clicked()), this, SLOT(slotDeleteMarker()));
219 connect(m_view.markers_list, SIGNAL(doubleClicked(const QModelIndex &)), this, SLOT(slotEditMarker()));
224 void ClipProperties::slotEnableLuma(int state) {
226 if (state == Qt::Checked) enable = true;
227 m_view.luma_duration->setEnabled(enable);
228 m_view.slide_luma->setEnabled(enable);
230 m_view.luma_file->setEnabled(m_view.slide_luma->isChecked());
231 } else m_view.luma_file->setEnabled(false);
232 m_view.label_softness->setEnabled(m_view.slide_luma->isChecked() && enable);
233 m_view.luma_softness->setEnabled(m_view.label_softness->isEnabled());
236 void ClipProperties::slotEnableLumaFile(int state) {
238 if (state == Qt::Checked) enable = true;
239 m_view.luma_file->setEnabled(enable);
240 m_view.luma_softness->setEnabled(enable);
241 m_view.label_softness->setEnabled(enable);
244 void ClipProperties::slotFillMarkersList() {
245 m_view.markers_list->clear();
246 QList < CommentedTime > marks = m_clip->commentedSnapMarkers();
247 for (uint count = 0; count < marks.count(); ++count) {
248 QString time = m_tc.getTimecode(marks[count].time(), m_tc.fps());
249 QStringList itemtext;
250 itemtext << time << marks[count].comment();
251 (void) new QTreeWidgetItem(m_view.markers_list, itemtext);
255 void ClipProperties::slotAddMarker() {
256 CommentedTime marker(GenTime(), i18n("Marker"));
257 MarkerDialog d(m_clip, marker, m_tc, i18n("Add Marker"), this);
258 if (d.exec() == QDialog::Accepted) {
259 emit addMarker(m_clip->getId(), d.newMarker().time(), d.newMarker().comment());
261 QTimer::singleShot(500, this, SLOT(slotFillMarkersList()));
264 void ClipProperties::slotEditMarker() {
265 QList < CommentedTime > marks = m_clip->commentedSnapMarkers();
266 int pos = m_view.markers_list->currentIndex().row();
267 if (pos < 0 || pos > marks.count() - 1) return;
268 MarkerDialog d(m_clip, marks.at(pos), m_tc, i18n("Edit Marker"), this);
269 if (d.exec() == QDialog::Accepted) {
270 emit addMarker(m_clip->getId(), d.newMarker().time(), d.newMarker().comment());
272 QTimer::singleShot(500, this, SLOT(slotFillMarkersList()));
275 void ClipProperties::slotDeleteMarker() {
276 QList < CommentedTime > marks = m_clip->commentedSnapMarkers();
277 int pos = m_view.markers_list->currentIndex().row();
278 if (pos < 0 || pos > marks.count() - 1) return;
279 emit addMarker(m_clip->getId(), marks.at(pos).time(), QString());
281 QTimer::singleShot(500, this, SLOT(slotFillMarkersList()));
284 const QString &ClipProperties::clipId() const {
285 return m_clip->getId();
289 QMap <QString, QString> ClipProperties::properties() {
290 QMap <QString, QString> props;
291 CLIPTYPE t = m_clip->clipType();
292 QMap <QString, QString> old_props = m_clip->properties();
294 if (old_props.value("description") != m_view.clip_description->text())
295 props["description"] = m_view.clip_description->text();
297 double aspect = m_view.clip_ar->value();
298 if (m_view.clip_force_ar->isChecked()) {
299 if (aspect != old_props.value("force_aspect_ratio").toDouble()) {
300 props["force_aspect_ratio"] = QString::number(aspect);
301 m_clipNeedsRefresh = true;
303 } else if (old_props.contains("force_aspect_ratio")) {
304 props["force_aspect_ratio"] = QString();
305 m_clipNeedsRefresh = true;
308 int threads = m_view.clip_threads->value();
309 if (m_view.clip_force_threads->isChecked()) {
310 if (threads != old_props.value("threads").toInt()) {
311 props["threads"] = QString::number(threads);
313 } else if (old_props.contains("threads")) {
314 props["threads"] = QString();
317 int vindex = m_view.clip_vindex->value();
318 if (m_view.clip_force_vindex->isChecked()) {
319 if (vindex != old_props.value("video_index").toInt()) {
320 props["video_index"] = QString::number(vindex);
322 } else if (old_props.contains("video_index")) {
323 props["video_index"] = QString();
326 int aindex = m_view.clip_aindex->value();
327 if (m_view.clip_force_aindex->isChecked()) {
328 if (aindex != old_props.value("audio_index").toInt()) {
329 props["audio_index"] = QString::number(aindex);
331 } else if (old_props.contains("audio_index")) {
332 props["audio_index"] = QString();
336 QString new_color = m_view.clip_color->color().name();
337 if (new_color != QString("#" + old_props.value("colour").right(8).left(6))) {
338 m_clipNeedsRefresh = true;
339 props["colour"] = "0x" + new_color.right(6) + "ff";
341 int duration = m_tc.getFrameCount(m_view.clip_duration->text(), m_fps);
342 if (duration != m_clip->duration().frames(m_fps)) {
343 props["out"] = QString::number(duration);
345 } else if (t == IMAGE) {
346 if ((int) m_view.image_transparency->isChecked() != old_props.value("transparency").toInt()) {
347 props["transparency"] = QString::number((int)m_view.image_transparency->isChecked());
348 m_clipNeedsRefresh = true;
350 int duration = m_tc.getFrameCount(m_view.clip_duration->text(), m_fps);
351 if (duration != m_clip->duration().frames(m_fps)) {
352 props["out"] = QString::number(duration);
354 } else if (t == SLIDESHOW) {
355 QString value = QString::number((int) m_view.slide_loop->isChecked());
356 if (old_props.value("loop") != value) props["loop"] = value;
357 value = QString::number((int) m_view.slide_fade->isChecked());
358 if (old_props.value("fade") != value) props["fade"] = value;
359 value = QString::number((int) m_view.luma_softness->value());
360 if (old_props.value("softness") != value) props["softness"] = value;
363 switch (m_view.image_type->currentIndex()) {
365 extension = "/.all.png";
368 extension = "/.all.bmp";
371 extension = "/.all.gif";
374 extension = "/.all.jpg";
377 QString new_path = m_view.clip_path->text() + extension;
378 if (new_path != old_props.value("resource")) {
379 m_clipNeedsRefresh = true;
380 props["resource"] = new_path;
381 kDebug() << "//// SLIDE EDIT, NEW:" << new_path << ", OLD; " << old_props.value("resource");
383 int duration = m_tc.getFrameCount(m_view.slide_duration->text(), m_fps);
384 if (duration != old_props.value("ttl").toInt()) {
385 m_clipNeedsRefresh = true;
386 props["ttl"] = QString::number(duration);
387 props["out"] = QString::number(duration * m_count);
389 if (duration * m_count != old_props.value("out").toInt()) {
390 m_clipNeedsRefresh = true;
391 props["out"] = QString::number(duration * m_count);
393 if (m_view.slide_fade->isChecked()) {
394 int luma_duration = m_tc.getFrameCount(m_view.luma_duration->text(), m_fps);
395 if (luma_duration != old_props.value("luma_duration").toInt()) {
396 m_clipNeedsRefresh = true;
397 props["luma_duration"] = QString::number(luma_duration);
400 if (m_view.slide_luma->isChecked())
401 lumaFile = m_view.luma_file->itemData(m_view.luma_file->currentIndex()).toString();
402 if (lumaFile != old_props.value("luma_file")) {
403 m_clipNeedsRefresh = true;
404 props["luma_file"] = lumaFile;
407 if (old_props.value("luma_file") != QString()) {
408 props["luma_file"] = QString();
416 bool ClipProperties::needsTimelineRefresh() const {
417 return m_clipNeedsRefresh;
420 void ClipProperties::parseFolder() {
422 QDir dir(m_view.clip_path->text());
425 switch (m_view.image_type->currentIndex()) {
428 extension = "/.all.png";
432 extension = "/.all.bmp";
436 extension = "/.all.gif";
440 // TODO: improve jpeg image detection with extension like jpeg, requires change in MLT image producers
442 extension = "/.all.jpg";
446 dir.setNameFilters(filters);
447 QStringList result = dir.entryList(QDir::Files);
448 m_count = result.count();
449 m_view.slide_info->setText(i18n("%1 images found", m_count));
450 QDomElement xml = m_clip->toXML();
451 xml.setAttribute("resource", m_view.clip_path->text() + extension);
452 QPixmap pix = m_clip->thumbProducer()->getImage(KUrl(m_view.clip_path->text() + extension), 1, 240, 180);
453 QMap <QString, QString> props = m_clip->properties();
454 m_view.clip_duration->setText(m_tc.getTimecodeFromFrames(props.value("ttl").toInt() * m_count));
455 m_view.clip_thumb->setPixmap(pix);
458 void ClipProperties::slotCheckMaxLength() {
459 int duration = m_tc.getFrameCount(m_view.clip_duration->text(), m_fps);
460 if (duration > m_clip->maxDuration().frames(m_fps)) {
461 m_view.clip_duration->setText(m_tc.getTimecode(m_clip->maxDuration(), m_fps));
465 #include "clipproperties.moc"