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