]> git.sesse.net Git - kdenlive/blob - src/clipproperties.cpp
8e9436aec9aca2941898badb0516dcfe517a457b
[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         QString path = props.value("resource");
75         if (path.endsWith("png")) m_view.image_type->setCurrentIndex(TYPE_PNG);
76         else if (path.endsWith("bmp")) m_view.image_type->setCurrentIndex(TYPE_BMP);
77         else if (path.endsWith("gif")) m_view.image_type->setCurrentIndex(TYPE_GIF);
78         m_view.slide_duration->setText(tc.getTimecodeFromFrames(props.value("ttl").toInt()));
79         parseFolder();
80         connect(m_view.image_type, SIGNAL(currentIndexChanged(int)), this, SLOT(parseFolder()));
81     } else if (t != AUDIO) {
82         m_view.tabWidget->removeTab(SLIDETAB);
83         m_view.tabWidget->removeTab(COLORTAB);
84         if (props.contains("frame_size"))
85             m_view.clip_size->setText(props.value("frame_size"));
86         if (props.contains("videocodec"))
87             m_view.clip_vcodec->setText(props.value("videocodec"));
88         if (props.contains("fps"))
89             m_view.clip_fps->setText(props.value("fps"));
90         if (props.contains("aspect_ratio"))
91             m_view.clip_ratio->setText(props.value("aspect_ratio"));
92
93         QPixmap pix = m_clip->thumbProducer()->getImage(url, m_clip->getClipThumbFrame(), 240, 180);
94         m_view.clip_thumb->setPixmap(pix);
95         if (t == IMAGE || t == VIDEO) m_view.tabWidget->removeTab(AUDIOTAB);
96     } else {
97         m_view.tabWidget->removeTab(SLIDETAB);
98         m_view.tabWidget->removeTab(COLORTAB);
99         m_view.tabWidget->removeTab(VIDEOTAB);
100         m_view.clip_thumb->setHidden(true);
101     }
102     if (t != IMAGE && t != COLOR && t != TEXT) m_view.clip_duration->setReadOnly(true);
103
104     KFileItem f(KFileItem::Unknown, KFileItem::Unknown, url, true);
105     m_view.clip_filesize->setText(KIO::convertSize(f.size()));
106     m_view.clip_duration->setText(tc.getTimecode(m_clip->duration(), m_fps));
107
108     // markers
109     m_view.marker_new->setIcon(KIcon("document-new"));
110     m_view.marker_new->setToolTip(i18n("Add marker"));
111     m_view.marker_edit->setIcon(KIcon("document-properties"));
112     m_view.marker_edit->setToolTip(i18n("Edit marker"));
113     m_view.marker_delete->setIcon(KIcon("trash-empty"));
114     m_view.marker_delete->setToolTip(i18n("Delete marker"));
115
116     slotFillMarkersList();
117     connect(m_view.marker_new, SIGNAL(clicked()), this, SLOT(slotAddMarker()));
118     connect(m_view.marker_edit, SIGNAL(clicked()), this, SLOT(slotEditMarker()));
119     connect(m_view.marker_delete, SIGNAL(clicked()), this, SLOT(slotDeleteMarker()));
120     connect(m_view.markers_list, SIGNAL(doubleClicked(const QModelIndex &)), this, SLOT(slotEditMarker()));
121
122     adjustSize();
123 }
124
125 void ClipProperties::slotFillMarkersList() {
126     m_view.markers_list->clear();
127     QList < CommentedTime > marks = m_clip->commentedSnapMarkers();
128     for (uint count = 0; count < marks.count(); ++count) {
129         QString time = m_tc.getTimecode(marks[count].time(), m_tc.fps());
130         QStringList itemtext;
131         itemtext << time << marks[count].comment();
132         (void) new QTreeWidgetItem(m_view.markers_list, itemtext);
133     }
134 }
135
136 void ClipProperties::slotAddMarker() {
137     CommentedTime marker(GenTime(), i18n("Marker"));
138     MarkerDialog d(m_clip, marker, m_tc, this);
139     if (d.exec() == QDialog::Accepted) {
140         int id = m_clip->getId();
141         emit addMarker(id, d.newMarker().time(), d.newMarker().comment());
142     }
143     QTimer::singleShot(500, this, SLOT(slotFillMarkersList()));
144 }
145
146 void ClipProperties::slotEditMarker() {
147     QList < CommentedTime > marks = m_clip->commentedSnapMarkers();
148     int pos = m_view.markers_list->currentIndex().row();
149     if (pos < 0 || pos > marks.count() - 1) return;
150     MarkerDialog d(m_clip, marks.at(pos), m_tc, this);
151     if (d.exec() == QDialog::Accepted) {
152         int id = m_clip->getId();
153         emit addMarker(id, d.newMarker().time(), d.newMarker().comment());
154     }
155     QTimer::singleShot(500, this, SLOT(slotFillMarkersList()));
156 }
157
158 void ClipProperties::slotDeleteMarker() {
159     QList < CommentedTime > marks = m_clip->commentedSnapMarkers();
160     int pos = m_view.markers_list->currentIndex().row();
161     if (pos < 0 || pos > marks.count() - 1) return;
162     int id = m_clip->getId();
163     emit addMarker(id, marks.at(pos).time(), QString());
164
165     QTimer::singleShot(500, this, SLOT(slotFillMarkersList()));
166 }
167
168 int ClipProperties::clipId() const {
169     return m_clip->getId();
170 }
171
172
173 QMap <QString, QString> ClipProperties::properties() {
174     QMap <QString, QString> props;
175     props["description"] = m_view.clip_description->text();
176     CLIPTYPE t = m_clip->clipType();
177     if (t == COLOR) {
178         QMap <QString, QString> old_props = m_clip->properties();
179         QString new_color = m_view.clip_color->color().name();
180         if (new_color != QString("#" + old_props.value("colour").right(8).left(6))) {
181             m_clipNeedsRefresh = true;
182             props["colour"] = "0x" + new_color.right(6) + "ff";
183         }
184     } else if (t == SLIDESHOW) {
185         props["loop"] = QString::number((int) m_view.slide_loop->isChecked());
186         QMap <QString, QString> old_props = m_clip->properties();
187         QString extension;
188         switch (m_view.image_type->currentIndex()) {
189         case TYPE_PNG:
190             extension = "/.all.png";
191             break;
192         case TYPE_BMP:
193             extension = "/.all.bmp";
194             break;
195         case TYPE_GIF:
196             extension = "/.all.gif";
197             break;
198         default:
199             extension = "/.all.jpg";
200             break;
201         }
202         QString new_path = m_view.clip_path->text() + extension;
203         if (new_path != old_props.value("resource")) {
204             m_clipNeedsRefresh = true;
205             props["resource"] = new_path;
206             kDebug() << "////  SLIDE EDIT, NEW:" << new_path << ", OLD; " << old_props.value("resource");
207         }
208         int duration = m_tc.getFrameCount(m_view.slide_duration->text(), m_fps);
209         if (duration != old_props.value("ttl").toInt()) {
210             m_clipNeedsRefresh = true;
211             props["ttl"] = QString::number(duration);
212             props["out"] = QString::number(duration * m_count);
213         }
214         if (duration * m_count != old_props.value("out").toInt()) {
215             m_clipNeedsRefresh = true;
216             props["out"] = QString::number(duration * m_count);
217         }
218
219     }
220     return props;
221 }
222
223 bool ClipProperties::needsTimelineRefresh() const {
224     return m_clipNeedsRefresh;
225 }
226
227 void ClipProperties::parseFolder() {
228
229     QDir dir(m_view.clip_path->text());
230     QStringList filters;
231     QString extension;
232     switch (m_view.image_type->currentIndex()) {
233     case TYPE_PNG:
234         filters << "*.png";
235         extension = "/.all.png";
236         break;
237     case TYPE_BMP:
238         filters << "*.bmp";
239         extension = "/.all.bmp";
240         break;
241     case TYPE_GIF:
242         filters << "*.gif";
243         extension = "/.all.gif";
244         break;
245     default:
246         filters << "*.jpg" << "*.jpeg";
247         extension = "/.all.jpg";
248         break;
249     }
250
251     dir.setNameFilters(filters);
252     QStringList result = dir.entryList(QDir::Files);
253     m_count = result.count();
254     m_view.slide_info->setText(i18n("%1 images found", m_count));
255     QDomElement xml = m_clip->toXML();
256     xml.setAttribute("resource", m_view.clip_path->text() + extension);
257     QPixmap pix = m_clip->thumbProducer()->getImage(xml, 1, 240, 180);
258     QMap <QString, QString> props = m_clip->properties();
259     m_view.clip_duration->setText(m_tc.getTimecodeFromFrames(props.value("ttl").toInt() * m_count));
260     m_view.clip_thumb->setPixmap(pix);
261 }
262
263 #include "clipproperties.moc"
264
265