]> git.sesse.net Git - kdenlive/blob - src/docclipbase.cpp
edit and delete markers
[kdenlive] / src / docclipbase.cpp
1 /**************************1*************************************************
2                           DocClipBase.cpp  -  description
3                              -------------------
4     begin                : Fri Apr 12 2002
5     copyright            : (C) 2002 by Jason Wood
6     email                : jasonwood@blueyonder.co.uk
7  ***************************************************************************/
8
9 /***************************************************************************
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  ***************************************************************************/
17
18 #include <KDebug>
19
20 #include "kdenlivesettings.h"
21 #include "docclipbase.h"
22 #include "kthumb.h"
23 #include "clipmanager.h"
24
25 DocClipBase::DocClipBase(ClipManager *clipManager, QDomElement xml, uint id):
26         m_id(id), m_description(QString()), m_refcount(0), m_projectThumbFrame(0), m_audioThumbCreated(false), m_duration(GenTime()), m_thumbProd(NULL), m_audioTimer(NULL) {
27     int type = xml.attribute("type").toInt();
28     m_clipType = (CLIPTYPE) type;
29     m_name = xml.attribute("name");
30     m_id = id;
31
32     QDomNamedNodeMap attributes = xml.attributes();
33     for (unsigned int i = 0; i < attributes.count(); i++) {
34         m_properties.insert(attributes.item(i).nodeName(), attributes.item(i).nodeValue());
35     }
36
37     KUrl url = KUrl(xml.attribute("resource"));
38     int out = xml.attribute("out").toInt();
39     if (out != 0) {
40         setDuration(GenTime(out, KdenliveSettings::project_fps()));
41         //m_properties.insert("out", QString::number(out));
42     }
43     if (m_name.isEmpty()) m_name = url.fileName();
44
45     if (!url.isEmpty() && QFile::exists(url.path())) {
46         m_thumbProd = new KThumb(clipManager, url);
47         if (m_clipType == AV || m_clipType == AUDIO) slotCreateAudioTimer();
48     }
49     //kDebug() << "type is video" << (m_clipType == AV) << " " << m_clipType;
50 }
51
52
53
54 DocClipBase::DocClipBase(const DocClipBase& clip) {
55     m_id = clip.getId();
56     m_clipType = clip.clipType();
57     m_name = clip.name();
58     m_duration = clip.duration();
59     m_audioThumbCreated = clip.audioThumbCreated();
60     m_properties = clip.properties();
61 }
62
63 DocClipBase & DocClipBase::operator=(const DocClipBase & clip) {
64     DocClipBase::operator=(clip);
65     m_id = clip.getId();
66     m_clipType = clip.clipType();
67     m_name = clip.name();
68     m_duration = clip.duration();
69     m_audioThumbCreated = clip.audioThumbCreated();
70     m_properties = clip.properties();
71     return *this;
72 }
73
74 DocClipBase::~DocClipBase() {
75     if (m_thumbProd) delete m_thumbProd;
76 }
77
78 void DocClipBase::slotCreateAudioTimer() {
79     connect(m_thumbProd, SIGNAL(audioThumbReady(QMap <int, QMap <int, QByteArray> >)), this , SLOT(updateAudioThumbnail(QMap <int, QMap <int, QByteArray> >)));
80     connect(this, SIGNAL(getAudioThumbs()), this , SLOT(slotGetAudioThumbs()));
81     m_audioTimer = new QTimer(this);
82     connect(m_audioTimer, SIGNAL(timeout()), this, SLOT(slotGetAudioThumbs()));
83 }
84
85 void DocClipBase::slotRequestAudioThumbs() {
86     emit getAudioThumbs();
87 }
88
89 void DocClipBase::slotClearAudioCache() {
90     if (m_thumbProd) m_thumbProd->stopAudioThumbs();
91     if (m_audioTimer != NULL) m_audioTimer->stop();
92     audioFrameChache.clear();
93     m_audioThumbCreated = false;
94 }
95
96 KThumb *DocClipBase::thumbProducer() {
97     return m_thumbProd;
98 }
99
100 bool DocClipBase::audioThumbCreated() const {
101     return m_audioThumbCreated;
102 }
103
104 void DocClipBase::setName(const QString name) {
105     m_name = name;
106 }
107
108 const QString & DocClipBase::name() const {
109
110     return m_name;
111 }
112
113 uint DocClipBase::getId() const {
114     return m_id;
115 }
116
117 void DocClipBase::setId(const uint &newId) {
118     m_id = newId;
119 }
120
121 const CLIPTYPE & DocClipBase::clipType() const {
122     return m_clipType;
123 }
124
125 void DocClipBase::setClipType(CLIPTYPE type) {
126     m_clipType = type;
127     if (m_thumbProd && m_audioTimer == NULL && (m_clipType == AV || m_clipType == AUDIO))
128         slotCreateAudioTimer();
129 }
130
131 KUrl DocClipBase::fileURL() const {
132     QString res = m_properties.value("resource");
133     if (m_clipType != COLOR && !res.isEmpty()) return KUrl(res);
134     return KUrl();
135 }
136
137 void DocClipBase::setProjectThumbFrame(const uint &ix) {
138     m_projectThumbFrame = ix;
139 }
140
141 uint DocClipBase::getProjectThumbFrame() const {
142     return m_projectThumbFrame;
143 }
144
145 const QString DocClipBase::description() const {
146     return m_properties.value("description");
147 }
148
149 const QString DocClipBase::getProperty(const QString prop) const {
150     return m_properties.value(prop);
151 }
152
153 void DocClipBase::setDuration(GenTime dur) {
154     m_duration = dur;
155 }
156
157 const GenTime &DocClipBase::duration() const {
158     return m_duration;
159 }
160
161 const GenTime &DocClipBase::maxDuration() const {
162     if (m_clipType == COLOR || m_clipType == IMAGE || m_clipType == TEXT || (m_clipType == SLIDESHOW &&  m_properties.value("loop") == "1")) {
163         GenTime dur(10000, KdenliveSettings::project_fps());
164         return dur;
165     }
166     return m_duration;
167 }
168
169 bool DocClipBase::hasFileSize() const {
170     return true;
171 }
172
173
174 // virtual
175 QDomElement DocClipBase::toXML() const {
176     QDomDocument doc;
177
178     QDomElement clip = doc.createElement("producer");
179
180     QMapIterator<QString, QString> i(m_properties);
181     while (i.hasNext()) {
182         i.next();
183         if (!i.value().isEmpty()) clip.setAttribute(i.key(), i.value());
184     }
185     //doc.appendChild(clip);
186     //kDebug()<<"/// CLIP XML: "<<doc.toString();
187     return clip;
188 }
189
190 DocClipBase *DocClipBase::
191 createClip(KdenliveDoc *doc, const QDomElement & element) {
192     DocClipBase *clip = 0;
193     QString description;
194     QDomNode node = element;
195     node.normalize();
196     if (element.tagName() != "kdenliveclip") {
197         kWarning() <<
198         "DocClipBase::createClip() element has unknown tagName : " << element.tagName();
199         return 0;
200     }
201
202     QDomNode n = element.firstChild();
203
204     while (!n.isNull()) {
205         QDomElement e = n.toElement();
206         if (!e.isNull()) {
207             QString tagName = e.tagName();
208             if (e.tagName() == "avfile") {
209                 // clip = DocClipAVFile::createClip(e);
210             } else if (e.tagName() == "DocTrackBaseList") {
211                 // clip = DocClipProject::createClip(doc, e);
212             }
213         } else {
214             QDomText text = n.toText();
215             if (!text.isNull()) {
216                 description = text.nodeValue();
217             }
218         }
219
220         n = n.nextSibling();
221     }
222     if (clip == 0) {
223         kWarning() << "DocClipBase::createClip() unable to create clip";
224     } else {
225         // setup DocClipBase specifics of the clip.
226         QMap <QString, QString> props;
227         props.insert("description", description);
228         clip->setProperties(props);
229         clip->setAudioThumbCreated(false);
230     }
231     return clip;
232 }
233
234 void DocClipBase::setAudioThumbCreated(bool isDone) {
235     m_audioThumbCreated = isDone;
236 }
237
238
239 QDomDocument DocClipBase::generateSceneList(bool, bool) const {
240 }
241
242 void DocClipBase::setThumbnail(const QPixmap & pixmap) {
243     m_thumbnail = pixmap;
244 }
245
246 const QPixmap & DocClipBase::thumbnail() const {
247     return m_thumbnail;
248 }
249
250 void DocClipBase::updateAudioThumbnail(QMap<int, QMap<int, QByteArray> > data) {
251     kDebug() << "CLIPBASE RECIEDVED AUDIO DATA*********************************************";
252     audioFrameChache = data;
253     m_audioThumbCreated = true;
254     emit gotAudioData();
255 }
256
257 QList < GenTime > DocClipBase::snapMarkers() const {
258     QList < GenTime > markers;
259
260     for (uint count = 0; count < m_snapMarkers.count(); ++count) {
261         markers.append(m_snapMarkers[count].time());
262     }
263
264     return markers;
265 }
266
267 QList < CommentedTime > DocClipBase::commentedSnapMarkers() const {
268     return m_snapMarkers;
269 }
270
271 void DocClipBase::setSnapMarkers(QList < CommentedTime > markers) {
272     m_snapMarkers = markers;
273 }
274
275 void DocClipBase::addSnapMarker(const GenTime & time, QString comment) {
276     QList < CommentedTime >::Iterator it = m_snapMarkers.begin();
277     for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
278         if ((*it).time() >= time)
279             break;
280     }
281
282     if ((it != m_snapMarkers.end()) && ((*it).time() == time)) {
283         (*it).setComment(comment);
284         //kError() << "trying to add Snap Marker that already exists, this will cause inconsistancies with undo/redo";
285     } else {
286         CommentedTime t(time, comment);
287         m_snapMarkers.insert(it, t);
288     }
289
290 }
291
292 void DocClipBase::editSnapMarker(const GenTime & time, QString comment) {
293     QList < CommentedTime >::Iterator it;
294     for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
295         if ((*it).time() == time)
296             break;
297     }
298     if (it != m_snapMarkers.end()) {
299         (*it).setComment(comment);
300     } else {
301         kError() << "trying to edit Snap Marker that does not already exists";
302     }
303 }
304
305 QString DocClipBase::deleteSnapMarker(const GenTime & time) {
306     QString result = i18n("Marker");
307     QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
308
309     while (itt != m_snapMarkers.end()) {
310         if ((*itt).time() == time)
311             break;
312         ++itt;
313     }
314
315     if ((itt != m_snapMarkers.end()) && ((*itt).time() == time)) {
316         result = (*itt).comment();
317         m_snapMarkers.erase(itt);
318     }
319     return result;
320 }
321
322
323 GenTime DocClipBase::hasSnapMarkers(const GenTime & time) {
324     QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
325
326     while (itt != m_snapMarkers.end()) {
327         if ((*itt).time() == time)
328             return time;
329         ++itt;
330     }
331
332     return GenTime(0.0);
333 }
334
335 GenTime DocClipBase::findPreviousSnapMarker(const GenTime & currTime) {
336     int it;
337     for (it = 0; it < m_snapMarkers.count(); it++) {
338         if (m_snapMarkers[it].time() >= currTime)
339             break;
340     }
341     if (it == 0) return GenTime();
342     else if (it == m_snapMarkers.count() - 1 && m_snapMarkers[it].time() < currTime)
343         return m_snapMarkers[it].time();
344     else return m_snapMarkers[it-1].time();
345 }
346
347 GenTime DocClipBase::findNextSnapMarker(const GenTime & currTime) {
348     int it;
349     for (it = 0; it < m_snapMarkers.count(); it++) {
350         if (m_snapMarkers[it].time() > currTime)
351             break;
352     }
353     if (it < m_snapMarkers.count() && m_snapMarkers[it].time() > currTime) return m_snapMarkers[it].time();
354     return duration();
355 }
356
357 QString DocClipBase::markerComment(GenTime t) {
358     QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
359
360     while (itt != m_snapMarkers.end()) {
361         if ((*itt).time() == t)
362             return (*itt).comment();
363         ++itt;
364     }
365     return QString::null;
366 }
367
368 void DocClipBase::setProperties(QMap <QString, QString> properties) {
369     // changing clip type is not allowed
370     properties.remove("type");
371     QMapIterator<QString, QString> i(properties);
372     while (i.hasNext()) {
373         i.next();
374         m_properties.insert(i.key(), i.value());
375         if (i.key() == "resource") m_thumbProd->updateClipUrl(KUrl(i.value()));
376         else if (i.key() == "out") setDuration(GenTime(i.value().toInt(), KdenliveSettings::project_fps()));
377     }
378 }
379
380 void DocClipBase::setProperty(QString key, QString value) {
381     m_properties.insert(key, value);
382     if (key == "resource") m_thumbProd->updateClipUrl(KUrl(value));
383     else if (key == "out") setDuration(GenTime(value.toInt(), KdenliveSettings::project_fps()));
384 }
385
386 QMap <QString, QString> DocClipBase::properties() const {
387     return m_properties;
388 }
389
390 void DocClipBase::slotGetAudioThumbs() {
391     if (m_thumbProd == NULL) return;
392     if (m_audioThumbCreated) {
393         if (m_audioTimer != NULL)
394             m_audioTimer->stop();
395     } else {
396         if (m_audioTimer != NULL)
397             m_audioTimer->start(2000);
398         double lengthInFrames = duration().frames(/*framesPerSecond()*/25);
399         m_thumbProd->getAudioThumbs(2, 0, lengthInFrames /*must be number of frames*/, 20);
400     }
401 }
402
403
404