]> git.sesse.net Git - kdenlive/blob - src/clipproperties.cpp
Fix timeline handling of objects (move them instead of changing their bounding rect
[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 #define VIDEOTAB 0
32 #define AUDIOTAB 1
33 #define COLORTAB 2
34 #define SLIDETAB 3
35 #define IMAGETAB 4
36 #define MARKERTAB 5
37 #define ADVANCEDTAB 6
38
39 #define TYPE_JPEG 0
40 #define TYPE_PNG 1
41 #define TYPE_BMP 2
42 #define 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         int id = m_clip->getId();
183         emit addMarker(id, d.newMarker().time(), d.newMarker().comment());
184     }
185     QTimer::singleShot(500, this, SLOT(slotFillMarkersList()));
186 }
187
188 void ClipProperties::slotEditMarker() {
189     QList < CommentedTime > marks = m_clip->commentedSnapMarkers();
190     int pos = m_view.markers_list->currentIndex().row();
191     if (pos < 0 || pos > marks.count() - 1) return;
192     MarkerDialog d(m_clip, marks.at(pos), m_tc, this);
193     if (d.exec() == QDialog::Accepted) {
194         int id = m_clip->getId();
195         emit addMarker(id, d.newMarker().time(), d.newMarker().comment());
196     }
197     QTimer::singleShot(500, this, SLOT(slotFillMarkersList()));
198 }
199
200 void ClipProperties::slotDeleteMarker() {
201     QList < CommentedTime > marks = m_clip->commentedSnapMarkers();
202     int pos = m_view.markers_list->currentIndex().row();
203     if (pos < 0 || pos > marks.count() - 1) return;
204     int id = m_clip->getId();
205     emit addMarker(id, marks.at(pos).time(), QString());
206
207     QTimer::singleShot(500, this, SLOT(slotFillMarkersList()));
208 }
209
210 int ClipProperties::clipId() const {
211     return m_clip->getId();
212 }
213
214
215 QMap <QString, QString> ClipProperties::properties() {
216     QMap <QString, QString> props;
217     props["description"] = m_view.clip_description->text();
218     CLIPTYPE t = m_clip->clipType();
219     if (t == COLOR) {
220         QMap <QString, QString> old_props = m_clip->properties();
221         QString new_color = m_view.clip_color->color().name();
222         if (new_color != QString("#" + old_props.value("colour").right(8).left(6))) {
223             m_clipNeedsRefresh = true;
224             props["colour"] = "0x" + new_color.right(6) + "ff";
225         }
226     } else if (t == IMAGE) {
227         QMap <QString, QString> old_props = m_clip->properties();
228         if ((int) m_view.image_transparency->isChecked() != old_props.value("transparency").toInt()) {
229             props["transparency"] = QString::number((int)m_view.image_transparency->isChecked());
230             m_clipNeedsRefresh = true;
231         }
232     } else if (t == SLIDESHOW) {
233         QMap <QString, QString> old_props = m_clip->properties();
234         QString value = QString::number((int) m_view.slide_loop->isChecked());
235         if (old_props.value("loop") != value) props["loop"] = value;
236         value = QString::number((int) m_view.slide_fade->isChecked());
237         if (old_props.value("fade") != value) props["fade"] = value;
238         value = QString::number((int) m_view.luma_softness->value());
239         if (old_props.value("softness") != value) props["softness"] = value;
240
241         QString extension;
242         switch (m_view.image_type->currentIndex()) {
243         case TYPE_PNG:
244             extension = "/.all.png";
245             break;
246         case TYPE_BMP:
247             extension = "/.all.bmp";
248             break;
249         case TYPE_GIF:
250             extension = "/.all.gif";
251             break;
252         default:
253             extension = "/.all.jpg";
254             break;
255         }
256         QString new_path = m_view.clip_path->text() + extension;
257         if (new_path != old_props.value("resource")) {
258             m_clipNeedsRefresh = true;
259             props["resource"] = new_path;
260             kDebug() << "////  SLIDE EDIT, NEW:" << new_path << ", OLD; " << old_props.value("resource");
261         }
262         int duration = m_tc.getFrameCount(m_view.slide_duration->text(), m_fps);
263         if (duration != old_props.value("ttl").toInt()) {
264             m_clipNeedsRefresh = true;
265             props["ttl"] = QString::number(duration);
266             props["out"] = QString::number(duration * m_count);
267         }
268         if (duration * m_count != old_props.value("out").toInt()) {
269             m_clipNeedsRefresh = true;
270             props["out"] = QString::number(duration * m_count);
271         }
272         if (m_view.slide_fade->isChecked()) {
273             int luma_duration = m_tc.getFrameCount(m_view.luma_duration->text(), m_fps);
274             if (luma_duration != old_props.value("luma_duration").toInt()) {
275                 m_clipNeedsRefresh = true;
276                 props["luma_duration"] = QString::number(luma_duration);
277             }
278             QString lumaFile;
279             if (m_view.slide_luma->isChecked())
280                 lumaFile = m_view.luma_file->itemData(m_view.luma_file->currentIndex()).toString();
281             if (lumaFile != old_props.value("luma_file")) {
282                 m_clipNeedsRefresh = true;
283                 props["luma_file"] = lumaFile;
284             }
285         } else {
286             if (old_props.value("luma_file") != QString()) {
287                 props["luma_file"] = QString();
288             }
289         }
290
291     }
292     return props;
293 }
294
295 bool ClipProperties::needsTimelineRefresh() const {
296     return m_clipNeedsRefresh;
297 }
298
299 void ClipProperties::parseFolder() {
300
301     QDir dir(m_view.clip_path->text());
302     QStringList filters;
303     QString extension;
304     switch (m_view.image_type->currentIndex()) {
305     case TYPE_PNG:
306         filters << "*.png";
307         extension = "/.all.png";
308         break;
309     case TYPE_BMP:
310         filters << "*.bmp";
311         extension = "/.all.bmp";
312         break;
313     case TYPE_GIF:
314         filters << "*.gif";
315         extension = "/.all.gif";
316         break;
317     default:
318         filters << "*.jpg" << "*.jpeg";
319         extension = "/.all.jpg";
320         break;
321     }
322
323     dir.setNameFilters(filters);
324     QStringList result = dir.entryList(QDir::Files);
325     m_count = result.count();
326     m_view.slide_info->setText(i18n("%1 images found", m_count));
327     QDomElement xml = m_clip->toXML();
328     xml.setAttribute("resource", m_view.clip_path->text() + extension);
329     QPixmap pix = m_clip->thumbProducer()->getImage(KUrl(m_view.clip_path->text() + extension), 1, 240, 180);
330     QMap <QString, QString> props = m_clip->properties();
331     m_view.clip_duration->setText(m_tc.getTimecodeFromFrames(props.value("ttl").toInt() * m_count));
332     m_view.clip_thumb->setPixmap(pix);
333 }
334
335 #include "clipproperties.moc"
336
337