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