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