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