]> git.sesse.net Git - kdenlive/blob - src/clipproperties.cpp
* internal rework: switch clip id's from integer to string
[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 #include <QDir>
21
22 #include <KStandardDirs>
23 #include <KDebug>
24 #include <KFileItem>
25
26 #include "kdenlivesettings.h"
27 #include "clipproperties.h"
28 #include "kthumb.h"
29 #include "markerdialog.h"
30
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;
38
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;
43
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());
46     m_view.setupUi(this);
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();
51
52     if (props.contains("audiocodec"))
53         m_view.clip_acodec->setText(props.value("audiocodec"));
54     if (props.contains("frequency"))
55         m_view.clip_frequency->setText(props.value("frequency"));
56     if (props.contains("channels"))
57         m_view.clip_channels->setText(props.value("channels"));
58
59     CLIPTYPE t = m_clip->clipType();
60     if (t == IMAGE) {
61         m_view.tabWidget->removeTab(SLIDETAB);
62         m_view.tabWidget->removeTab(COLORTAB);
63         m_view.tabWidget->removeTab(AUDIOTAB);
64         m_view.tabWidget->removeTab(VIDEOTAB);
65         if (props.contains("frame_size"))
66             m_view.image_size->setText(props.value("frame_size"));
67         if (props.contains("transparency"))
68             m_view.image_transparency->setChecked(props.value("transparency").toInt());
69     } else if (t == COLOR) {
70         m_view.clip_path->setEnabled(false);
71         m_view.tabWidget->removeTab(IMAGETAB);
72         m_view.tabWidget->removeTab(SLIDETAB);
73         m_view.tabWidget->removeTab(AUDIOTAB);
74         m_view.tabWidget->removeTab(VIDEOTAB);
75         m_view.clip_thumb->setHidden(true);
76         m_view.clip_color->setColor(QColor("#" + props.value("colour").right(8).left(6)));
77     } else if (t == SLIDESHOW) {
78         m_view.clip_path->setText(url.directory());
79         m_view.tabWidget->removeTab(IMAGETAB);
80         m_view.tabWidget->removeTab(COLORTAB);
81         m_view.tabWidget->removeTab(AUDIOTAB);
82         m_view.tabWidget->removeTab(VIDEOTAB);
83         QStringList types;
84         types << "JPG" << "PNG" << "BMP" << "GIF";
85         m_view.image_type->addItems(types);
86         m_view.slide_loop->setChecked(props.value("loop").toInt());
87         m_view.slide_fade->setChecked(props.value("fade").toInt());
88         m_view.luma_softness->setValue(props.value("softness").toInt());
89         QString path = props.value("resource");
90         if (path.endsWith("png")) m_view.image_type->setCurrentIndex(TYPE_PNG);
91         else if (path.endsWith("bmp")) m_view.image_type->setCurrentIndex(TYPE_BMP);
92         else if (path.endsWith("gif")) m_view.image_type->setCurrentIndex(TYPE_GIF);
93         m_view.slide_duration->setText(tc.getTimecodeFromFrames(props.value("ttl").toInt()));
94         parseFolder();
95
96         m_view.luma_duration->setText(tc.getTimecodeFromFrames(props.value("luma_duration").toInt()));
97         QString lumaFile = props.value("luma_file");
98         QString profilePath = KdenliveSettings::mltpath();
99         profilePath = profilePath.section('/', 0, -3);
100         profilePath += "/lumas/PAL/";
101
102         QDir dir(profilePath);
103         QStringList filter;
104         filter << "*.pgm";
105         const QStringList result = dir.entryList(filter, QDir::Files);
106         QStringList imagefiles;
107         QStringList imagenamelist;
108         int current;
109         foreach(const QString file, result) {
110             m_view.luma_file->addItem(KIcon(profilePath + file), file, profilePath + file);
111             if (!lumaFile.isEmpty() && lumaFile == QString(profilePath + file))
112                 current = m_view.luma_file->count() - 1;
113         }
114
115         if (!lumaFile.isEmpty()) {
116             m_view.slide_luma->setChecked(true);
117             m_view.luma_file->setCurrentIndex(current);
118         }
119
120         connect(m_view.image_type, SIGNAL(currentIndexChanged(int)), this, SLOT(parseFolder()));
121     } else if (t != AUDIO) {
122         m_view.tabWidget->removeTab(IMAGETAB);
123         m_view.tabWidget->removeTab(SLIDETAB);
124         m_view.tabWidget->removeTab(COLORTAB);
125         if (props.contains("frame_size"))
126             m_view.clip_size->setText(props.value("frame_size"));
127         if (props.contains("videocodec"))
128             m_view.clip_vcodec->setText(props.value("videocodec"));
129         if (props.contains("fps"))
130             m_view.clip_fps->setText(props.value("fps"));
131         if (props.contains("aspect_ratio"))
132             m_view.clip_ratio->setText(props.value("aspect_ratio"));
133
134         QPixmap pix = m_clip->thumbProducer()->getImage(url, m_clip->getClipThumbFrame(), 240, 180);
135         m_view.clip_thumb->setPixmap(pix);
136         if (t == IMAGE || t == VIDEO) m_view.tabWidget->removeTab(AUDIOTAB);
137     } else {
138         m_view.tabWidget->removeTab(IMAGETAB);
139         m_view.tabWidget->removeTab(SLIDETAB);
140         m_view.tabWidget->removeTab(COLORTAB);
141         m_view.tabWidget->removeTab(VIDEOTAB);
142         m_view.clip_thumb->setHidden(true);
143     }
144     if (t != IMAGE && t != COLOR && t != TEXT) m_view.clip_duration->setReadOnly(true);
145
146     KFileItem f(KFileItem::Unknown, KFileItem::Unknown, url, true);
147     m_view.clip_filesize->setText(KIO::convertSize(f.size()));
148     m_view.clip_duration->setText(tc.getTimecode(m_clip->duration(), m_fps));
149
150     // markers
151     m_view.marker_new->setIcon(KIcon("document-new"));
152     m_view.marker_new->setToolTip(i18n("Add marker"));
153     m_view.marker_edit->setIcon(KIcon("document-properties"));
154     m_view.marker_edit->setToolTip(i18n("Edit marker"));
155     m_view.marker_delete->setIcon(KIcon("trash-empty"));
156     m_view.marker_delete->setToolTip(i18n("Delete marker"));
157
158     slotFillMarkersList();
159     connect(m_view.marker_new, SIGNAL(clicked()), this, SLOT(slotAddMarker()));
160     connect(m_view.marker_edit, SIGNAL(clicked()), this, SLOT(slotEditMarker()));
161     connect(m_view.marker_delete, SIGNAL(clicked()), this, SLOT(slotDeleteMarker()));
162     connect(m_view.markers_list, SIGNAL(doubleClicked(const QModelIndex &)), this, SLOT(slotEditMarker()));
163
164     adjustSize();
165 }
166
167 void ClipProperties::slotFillMarkersList() {
168     m_view.markers_list->clear();
169     QList < CommentedTime > marks = m_clip->commentedSnapMarkers();
170     for (uint count = 0; count < marks.count(); ++count) {
171         QString time = m_tc.getTimecode(marks[count].time(), m_tc.fps());
172         QStringList itemtext;
173         itemtext << time << marks[count].comment();
174         (void) new QTreeWidgetItem(m_view.markers_list, itemtext);
175     }
176 }
177
178 void ClipProperties::slotAddMarker() {
179     CommentedTime marker(GenTime(), i18n("Marker"));
180     MarkerDialog d(m_clip, marker, m_tc, this);
181     if (d.exec() == QDialog::Accepted) {
182         emit addMarker(m_clip->getId(), d.newMarker().time(), d.newMarker().comment());
183     }
184     QTimer::singleShot(500, this, SLOT(slotFillMarkersList()));
185 }
186
187 void ClipProperties::slotEditMarker() {
188     QList < CommentedTime > marks = m_clip->commentedSnapMarkers();
189     int pos = m_view.markers_list->currentIndex().row();
190     if (pos < 0 || pos > marks.count() - 1) return;
191     MarkerDialog d(m_clip, marks.at(pos), m_tc, this);
192     if (d.exec() == QDialog::Accepted) {
193         emit addMarker(m_clip->getId(), d.newMarker().time(), d.newMarker().comment());
194     }
195     QTimer::singleShot(500, this, SLOT(slotFillMarkersList()));
196 }
197
198 void ClipProperties::slotDeleteMarker() {
199     QList < CommentedTime > marks = m_clip->commentedSnapMarkers();
200     int pos = m_view.markers_list->currentIndex().row();
201     if (pos < 0 || pos > marks.count() - 1) return;
202     emit addMarker(m_clip->getId(), marks.at(pos).time(), QString());
203
204     QTimer::singleShot(500, this, SLOT(slotFillMarkersList()));
205 }
206
207 const QString &ClipProperties::clipId() const {
208     return m_clip->getId();
209 }
210
211
212 QMap <QString, QString> ClipProperties::properties() {
213     QMap <QString, QString> props;
214     props["description"] = m_view.clip_description->text();
215     CLIPTYPE t = m_clip->clipType();
216     if (t == COLOR) {
217         QMap <QString, QString> old_props = m_clip->properties();
218         QString new_color = m_view.clip_color->color().name();
219         if (new_color != QString("#" + old_props.value("colour").right(8).left(6))) {
220             m_clipNeedsRefresh = true;
221             props["colour"] = "0x" + new_color.right(6) + "ff";
222         }
223     } else if (t == IMAGE) {
224         QMap <QString, QString> old_props = m_clip->properties();
225         if ((int) m_view.image_transparency->isChecked() != old_props.value("transparency").toInt()) {
226             props["transparency"] = QString::number((int)m_view.image_transparency->isChecked());
227             m_clipNeedsRefresh = true;
228         }
229     } else if (t == SLIDESHOW) {
230         QMap <QString, QString> old_props = m_clip->properties();
231         QString value = QString::number((int) m_view.slide_loop->isChecked());
232         if (old_props.value("loop") != value) props["loop"] = value;
233         value = QString::number((int) m_view.slide_fade->isChecked());
234         if (old_props.value("fade") != value) props["fade"] = value;
235         value = QString::number((int) m_view.luma_softness->value());
236         if (old_props.value("softness") != value) props["softness"] = value;
237
238         QString extension;
239         switch (m_view.image_type->currentIndex()) {
240         case TYPE_PNG:
241             extension = "/.all.png";
242             break;
243         case TYPE_BMP:
244             extension = "/.all.bmp";
245             break;
246         case TYPE_GIF:
247             extension = "/.all.gif";
248             break;
249         default:
250             extension = "/.all.jpg";
251             break;
252         }
253         QString new_path = m_view.clip_path->text() + extension;
254         if (new_path != old_props.value("resource")) {
255             m_clipNeedsRefresh = true;
256             props["resource"] = new_path;
257             kDebug() << "////  SLIDE EDIT, NEW:" << new_path << ", OLD; " << old_props.value("resource");
258         }
259         int duration = m_tc.getFrameCount(m_view.slide_duration->text(), m_fps);
260         if (duration != old_props.value("ttl").toInt()) {
261             m_clipNeedsRefresh = true;
262             props["ttl"] = QString::number(duration);
263             props["out"] = QString::number(duration * m_count);
264         }
265         if (duration * m_count != old_props.value("out").toInt()) {
266             m_clipNeedsRefresh = true;
267             props["out"] = QString::number(duration * m_count);
268         }
269         if (m_view.slide_fade->isChecked()) {
270             int luma_duration = m_tc.getFrameCount(m_view.luma_duration->text(), m_fps);
271             if (luma_duration != old_props.value("luma_duration").toInt()) {
272                 m_clipNeedsRefresh = true;
273                 props["luma_duration"] = QString::number(luma_duration);
274             }
275             QString lumaFile;
276             if (m_view.slide_luma->isChecked())
277                 lumaFile = m_view.luma_file->itemData(m_view.luma_file->currentIndex()).toString();
278             if (lumaFile != old_props.value("luma_file")) {
279                 m_clipNeedsRefresh = true;
280                 props["luma_file"] = lumaFile;
281             }
282         } else {
283             if (old_props.value("luma_file") != QString()) {
284                 props["luma_file"] = QString();
285             }
286         }
287
288     }
289     return props;
290 }
291
292 bool ClipProperties::needsTimelineRefresh() const {
293     return m_clipNeedsRefresh;
294 }
295
296 void ClipProperties::parseFolder() {
297
298     QDir dir(m_view.clip_path->text());
299     QStringList filters;
300     QString extension;
301     switch (m_view.image_type->currentIndex()) {
302     case TYPE_PNG:
303         filters << "*.png";
304         extension = "/.all.png";
305         break;
306     case TYPE_BMP:
307         filters << "*.bmp";
308         extension = "/.all.bmp";
309         break;
310     case TYPE_GIF:
311         filters << "*.gif";
312         extension = "/.all.gif";
313         break;
314     default:
315         filters << "*.jpg" << "*.jpeg";
316         extension = "/.all.jpg";
317         break;
318     }
319
320     dir.setNameFilters(filters);
321     QStringList result = dir.entryList(QDir::Files);
322     m_count = result.count();
323     m_view.slide_info->setText(i18n("%1 images found", m_count));
324     QDomElement xml = m_clip->toXML();
325     xml.setAttribute("resource", m_view.clip_path->text() + extension);
326     QPixmap pix = m_clip->thumbProducer()->getImage(KUrl(m_view.clip_path->text() + extension), 1, 240, 180);
327     QMap <QString, QString> props = m_clip->properties();
328     m_view.clip_duration->setText(m_tc.getTimecodeFromFrames(props.value("ttl").toInt() * m_count));
329     m_view.clip_thumb->setPixmap(pix);
330 }
331
332 #include "clipproperties.moc"
333
334