]> git.sesse.net Git - kdenlive/blob - src/docclipbase.cpp
b96cdf58359c510063226a96398b15a9cb8df76d
[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, 25));
41         //m_properties.insert("out", QString::number(out));
42     }
43     if (m_name.isEmpty()) m_name = url.fileName();
44
45     if (!url.isEmpty()) {
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     audioFrameChache.clear();
91     m_audioThumbCreated = false;
92 }
93
94 KThumb *DocClipBase::thumbProducer() {
95     return m_thumbProd;
96 }
97
98 bool DocClipBase::audioThumbCreated() const {
99     return m_audioThumbCreated;
100 }
101
102 void DocClipBase::setName(const QString name) {
103     m_name = name;
104 }
105
106 const QString & DocClipBase::name() const {
107
108     return m_name;
109 }
110
111 uint DocClipBase::getId() const {
112     return m_id;
113 }
114
115 void DocClipBase::setId(const uint &newId) {
116     m_id = newId;
117 }
118
119 const CLIPTYPE & DocClipBase::clipType() const {
120     return m_clipType;
121 }
122
123 void DocClipBase::setClipType(CLIPTYPE type) {
124     m_clipType = type;
125     if (m_audioTimer == NULL && (m_clipType == AV || m_clipType == AUDIO))
126         slotCreateAudioTimer();
127 }
128
129 KUrl DocClipBase::fileURL() const {
130     QString res = m_properties.value("resource");
131     if (m_clipType != COLOR && !res.isEmpty()) return KUrl(res);
132     return KUrl();
133 }
134
135 void DocClipBase::setProjectThumbFrame(const uint &ix) {
136     m_projectThumbFrame = ix;
137 }
138
139 uint DocClipBase::getProjectThumbFrame() const {
140     return m_projectThumbFrame;
141 }
142
143 const QString DocClipBase::description() const {
144     return m_properties.value("description");
145 }
146
147 void DocClipBase::setDuration(GenTime dur) {
148     m_duration = dur;
149 }
150
151 const GenTime &DocClipBase::duration() const {
152     return m_duration;
153 }
154
155 bool DocClipBase::hasFileSize() const {
156     return true;
157 }
158
159
160 // virtual
161 QDomElement DocClipBase::toXML() const {
162     QDomDocument doc;
163
164     QDomElement clip = doc.createElement("producer");
165
166     QMapIterator<QString, QString> i(m_properties);
167     while (i.hasNext()) {
168         i.next();
169         if (!i.value().isEmpty()) clip.setAttribute(i.key(), i.value());
170     }
171     //doc.appendChild(clip);
172     //kDebug()<<"/// CLIP XML: "<<doc.toString();
173     return clip;
174 }
175
176 DocClipBase *DocClipBase::
177 createClip(KdenliveDoc *doc, const QDomElement & element) {
178     DocClipBase *clip = 0;
179     QString description;
180     QDomNode node = element;
181     node.normalize();
182     if (element.tagName() != "kdenliveclip") {
183         kWarning() <<
184         "DocClipBase::createClip() element has unknown tagName : " <<
185         element.tagName() << endl;
186         return 0;
187     }
188
189     QDomNode n = element.firstChild();
190
191     while (!n.isNull()) {
192         QDomElement e = n.toElement();
193         if (!e.isNull()) {
194             QString tagName = e.tagName();
195             if (e.tagName() == "avfile") {
196                 // clip = DocClipAVFile::createClip(e);
197             } else if (e.tagName() == "DocTrackBaseList") {
198                 // clip = DocClipProject::createClip(doc, e);
199             }
200         } else {
201             QDomText text = n.toText();
202             if (!text.isNull()) {
203                 description = text.nodeValue();
204             }
205         }
206
207         n = n.nextSibling();
208     }
209     if (clip == 0) {
210         kWarning() << "DocClipBase::createClip() unable to create clip" <<
211         endl;
212     } else {
213         // setup DocClipBase specifics of the clip.
214         QMap <QString, QString> props;
215         props.insert("description", description);
216         clip->setProperties(props);
217         clip->setAudioThumbCreated(false);
218     }
219     return clip;
220 }
221
222 void DocClipBase::setAudioThumbCreated(bool isDone) {
223     m_audioThumbCreated = isDone;
224 }
225
226
227 QDomDocument DocClipBase::generateSceneList(bool, bool) const {
228 }
229
230 void DocClipBase::setThumbnail(const QPixmap & pixmap) {
231     m_thumbnail = pixmap;
232 }
233
234 const QPixmap & DocClipBase::thumbnail() const {
235     return m_thumbnail;
236 }
237
238 void DocClipBase::updateAudioThumbnail(QMap<int, QMap<int, QByteArray> > data) {
239     kDebug() << "CLIPBASE RECIEDVED AUDIO DATA*********************************************";
240     audioFrameChache = data;
241     m_audioThumbCreated = true;
242     emit gotAudioData();
243 }
244
245 QList < GenTime > DocClipBase::snapMarkers() const {
246     QList < GenTime > markers;
247
248     for (uint count = 0; count < m_snapMarkers.count(); ++count) {
249         markers.append(m_snapMarkers[count].time());
250     }
251
252     return markers;
253 }
254
255 QList < CommentedTime > DocClipBase::commentedSnapMarkers() const {
256     return m_snapMarkers;
257 }
258
259 void DocClipBase::setSnapMarkers(QList < CommentedTime > markers) {
260     m_snapMarkers = markers;
261 }
262
263 void DocClipBase::addSnapMarker(const GenTime & time, QString comment) {
264     QList < CommentedTime >::Iterator it = m_snapMarkers.begin();
265     for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
266         if ((*it).time() >= time)
267             break;
268     }
269
270     if ((it != m_snapMarkers.end()) && ((*it).time() == time)) {
271         kError() <<
272         "trying to add Snap Marker that already exists, this will cause inconsistancies with undo/redo"
273         << endl;
274     } else {
275         CommentedTime t(time, comment);
276         m_snapMarkers.insert(it, t);
277     }
278
279 }
280
281 void DocClipBase::editSnapMarker(const GenTime & time, QString comment) {
282     QList < CommentedTime >::Iterator it;
283     for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
284         if ((*it).time() == time)
285             break;
286     }
287     if (it != m_snapMarkers.end()) {
288         (*it).setComment(comment);
289     } else {
290         kError() <<
291         "trying to edit Snap Marker that does not already exists"  << endl;
292     }
293 }
294
295 QString DocClipBase::deleteSnapMarker(const GenTime & time) {
296     QString result = i18n("Marker");
297     QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
298
299     while (itt != m_snapMarkers.end()) {
300         if ((*itt).time() == time)
301             break;
302         ++itt;
303     }
304
305     if ((itt != m_snapMarkers.end()) && ((*itt).time() == time)) {
306         result = (*itt).comment();
307         m_snapMarkers.erase(itt);
308     }
309     return result;
310 }
311
312
313 GenTime DocClipBase::hasSnapMarkers(const GenTime & time) {
314     QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
315
316     while (itt != m_snapMarkers.end()) {
317         if ((*itt).time() == time)
318             return time;
319         ++itt;
320     }
321
322     return GenTime(0.0);
323 }
324
325 GenTime DocClipBase::findPreviousSnapMarker(const GenTime & currTime) {
326     int it;
327     for (it = 0; it < m_snapMarkers.count(); it++) {
328         if (m_snapMarkers[it].time() >= currTime)
329             break;
330     }
331     if (it == 0) return GenTime();
332     else if (it == m_snapMarkers.count() - 1 && m_snapMarkers[it].time() < currTime)
333         return m_snapMarkers[it].time();
334     else return m_snapMarkers[it-1].time();
335 }
336
337 GenTime DocClipBase::findNextSnapMarker(const GenTime & currTime) {
338     int it;
339     for (it = 0; it < m_snapMarkers.count(); it++) {
340         if (m_snapMarkers[it].time() > currTime)
341             break;
342     }
343     if (it < m_snapMarkers.count() && m_snapMarkers[it].time() > currTime) return m_snapMarkers[it].time();
344     return duration();
345 }
346
347 QString DocClipBase::markerComment(GenTime t) {
348     QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
349
350     while (itt != m_snapMarkers.end()) {
351         if ((*itt).time() == t)
352             return (*itt).comment();
353         ++itt;
354     }
355     return QString::null;
356 }
357
358 void DocClipBase::setProperties(QMap <QString, QString> properties) {
359     QMapIterator<QString, QString> i(properties);
360     while (i.hasNext()) {
361         i.next();
362         m_properties.insert(i.key(), i.value());
363     }
364 }
365
366 QMap <QString, QString> DocClipBase::properties() const {
367     return m_properties;
368 }
369
370
371 void DocClipBase::slotGetAudioThumbs() {
372
373     if (m_audioThumbCreated) {
374         if (m_audioTimer != NULL)
375             m_audioTimer->stop();
376     } else {
377         if (m_audioTimer != NULL)
378             m_audioTimer->start(2000);
379         double lengthInFrames = duration().frames(/*framesPerSecond()*/25);
380         m_thumbProd->getAudioThumbs(2, 0, lengthInFrames /*must be number of frames*/, 20);
381     }
382 }
383
384
385