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