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