]> git.sesse.net Git - kdenlive/blob - src/docclipbase.cpp
* don't crash when clip is missing
[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 : " <<
199         element.tagName() << endl;
200         return 0;
201     }
202
203     QDomNode n = element.firstChild();
204
205     while (!n.isNull()) {
206         QDomElement e = n.toElement();
207         if (!e.isNull()) {
208             QString tagName = e.tagName();
209             if (e.tagName() == "avfile") {
210                 // clip = DocClipAVFile::createClip(e);
211             } else if (e.tagName() == "DocTrackBaseList") {
212                 // clip = DocClipProject::createClip(doc, e);
213             }
214         } else {
215             QDomText text = n.toText();
216             if (!text.isNull()) {
217                 description = text.nodeValue();
218             }
219         }
220
221         n = n.nextSibling();
222     }
223     if (clip == 0) {
224         kWarning() << "DocClipBase::createClip() unable to create clip" <<
225         endl;
226     } else {
227         // setup DocClipBase specifics of the clip.
228         QMap <QString, QString> props;
229         props.insert("description", description);
230         clip->setProperties(props);
231         clip->setAudioThumbCreated(false);
232     }
233     return clip;
234 }
235
236 void DocClipBase::setAudioThumbCreated(bool isDone) {
237     m_audioThumbCreated = isDone;
238 }
239
240
241 QDomDocument DocClipBase::generateSceneList(bool, bool) const {
242 }
243
244 void DocClipBase::setThumbnail(const QPixmap & pixmap) {
245     m_thumbnail = pixmap;
246 }
247
248 const QPixmap & DocClipBase::thumbnail() const {
249     return m_thumbnail;
250 }
251
252 void DocClipBase::updateAudioThumbnail(QMap<int, QMap<int, QByteArray> > data) {
253     kDebug() << "CLIPBASE RECIEDVED AUDIO DATA*********************************************";
254     audioFrameChache = data;
255     m_audioThumbCreated = true;
256     emit gotAudioData();
257 }
258
259 QList < GenTime > DocClipBase::snapMarkers() const {
260     QList < GenTime > markers;
261
262     for (uint count = 0; count < m_snapMarkers.count(); ++count) {
263         markers.append(m_snapMarkers[count].time());
264     }
265
266     return markers;
267 }
268
269 QList < CommentedTime > DocClipBase::commentedSnapMarkers() const {
270     return m_snapMarkers;
271 }
272
273 void DocClipBase::setSnapMarkers(QList < CommentedTime > markers) {
274     m_snapMarkers = markers;
275 }
276
277 void DocClipBase::addSnapMarker(const GenTime & time, QString comment) {
278     QList < CommentedTime >::Iterator it = m_snapMarkers.begin();
279     for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
280         if ((*it).time() >= time)
281             break;
282     }
283
284     if ((it != m_snapMarkers.end()) && ((*it).time() == time)) {
285         kError() <<
286         "trying to add Snap Marker that already exists, this will cause inconsistancies with undo/redo"
287         << endl;
288     } else {
289         CommentedTime t(time, comment);
290         m_snapMarkers.insert(it, t);
291     }
292
293 }
294
295 void DocClipBase::editSnapMarker(const GenTime & time, QString comment) {
296     QList < CommentedTime >::Iterator it;
297     for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
298         if ((*it).time() == time)
299             break;
300     }
301     if (it != m_snapMarkers.end()) {
302         (*it).setComment(comment);
303     } else {
304         kError() <<
305         "trying to edit Snap Marker that does not already exists"  << endl;
306     }
307 }
308
309 QString DocClipBase::deleteSnapMarker(const GenTime & time) {
310     QString result = i18n("Marker");
311     QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
312
313     while (itt != m_snapMarkers.end()) {
314         if ((*itt).time() == time)
315             break;
316         ++itt;
317     }
318
319     if ((itt != m_snapMarkers.end()) && ((*itt).time() == time)) {
320         result = (*itt).comment();
321         m_snapMarkers.erase(itt);
322     }
323     return result;
324 }
325
326
327 GenTime DocClipBase::hasSnapMarkers(const GenTime & time) {
328     QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
329
330     while (itt != m_snapMarkers.end()) {
331         if ((*itt).time() == time)
332             return time;
333         ++itt;
334     }
335
336     return GenTime(0.0);
337 }
338
339 GenTime DocClipBase::findPreviousSnapMarker(const GenTime & currTime) {
340     int it;
341     for (it = 0; it < m_snapMarkers.count(); it++) {
342         if (m_snapMarkers[it].time() >= currTime)
343             break;
344     }
345     if (it == 0) return GenTime();
346     else if (it == m_snapMarkers.count() - 1 && m_snapMarkers[it].time() < currTime)
347         return m_snapMarkers[it].time();
348     else return m_snapMarkers[it-1].time();
349 }
350
351 GenTime DocClipBase::findNextSnapMarker(const GenTime & currTime) {
352     int it;
353     for (it = 0; it < m_snapMarkers.count(); it++) {
354         if (m_snapMarkers[it].time() > currTime)
355             break;
356     }
357     if (it < m_snapMarkers.count() && m_snapMarkers[it].time() > currTime) return m_snapMarkers[it].time();
358     return duration();
359 }
360
361 QString DocClipBase::markerComment(GenTime t) {
362     QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
363
364     while (itt != m_snapMarkers.end()) {
365         if ((*itt).time() == t)
366             return (*itt).comment();
367         ++itt;
368     }
369     return QString::null;
370 }
371
372 void DocClipBase::setProperties(QMap <QString, QString> properties) {
373     // changing clip type is not allowed
374     properties.remove("type");
375     QMapIterator<QString, QString> i(properties);
376     while (i.hasNext()) {
377         i.next();
378         m_properties.insert(i.key(), i.value());
379         if (i.key() == "resource") m_thumbProd->updateClipUrl(KUrl(i.value()));
380         else if (i.key() == "out") setDuration(GenTime(i.value().toInt(), KdenliveSettings::project_fps()));
381     }
382 }
383
384 void DocClipBase::setProperty(QString key, QString value) {
385     m_properties.insert(key, value);
386     if (key == "resource") m_thumbProd->updateClipUrl(KUrl(value));
387     else if (key == "out") setDuration(GenTime(value.toInt(), KdenliveSettings::project_fps()));
388 }
389
390 QMap <QString, QString> DocClipBase::properties() const {
391     return m_properties;
392 }
393
394 void DocClipBase::slotGetAudioThumbs() {
395     if (m_thumbProd == NULL) return;
396     if (m_audioThumbCreated) {
397         if (m_audioTimer != NULL)
398             m_audioTimer->stop();
399     } else {
400         if (m_audioTimer != NULL)
401             m_audioTimer->start(2000);
402         double lengthInFrames = duration().frames(/*framesPerSecond()*/25);
403         m_thumbProd->getAudioThumbs(2, 0, lengthInFrames /*must be number of frames*/, 20);
404     }
405 }
406
407
408