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