1 /**************************1*************************************************
2 DocClipBase.cpp - description
4 begin : Fri Apr 12 2002
5 copyright : (C) 2002 by Jason Wood
6 email : jasonwood@blueyonder.co.uk
7 ***************************************************************************/
9 /***************************************************************************
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. *
16 ***************************************************************************/
20 #include "kdenlivesettings.h"
21 #include "docclipbase.h"
23 #include "clipmanager.h"
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");
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());
37 KUrl url = KUrl(xml.attribute("resource"));
38 int out = xml.attribute("out").toInt();
40 setDuration(GenTime(out, KdenliveSettings::project_fps()));
42 out = xml.attribute("duration").toInt();
43 if (out != 0) setDuration(GenTime(out, KdenliveSettings::project_fps()));
45 if (m_name.isEmpty()) m_name = url.fileName();
47 if (!url.isEmpty() && QFile::exists(url.path())) {
48 m_thumbProd = new KThumb(clipManager, url);
49 if (m_clipType == AV || m_clipType == AUDIO) slotCreateAudioTimer();
51 //kDebug() << "type is video" << (m_clipType == AV) << " " << m_clipType;
56 DocClipBase::DocClipBase(const DocClipBase& clip) {
58 m_clipType = clip.clipType();
60 m_duration = clip.duration();
61 m_audioThumbCreated = clip.audioThumbCreated();
62 m_properties = clip.properties();
65 DocClipBase & DocClipBase::operator=(const DocClipBase & clip) {
66 DocClipBase::operator=(clip);
68 m_clipType = clip.clipType();
70 m_duration = clip.duration();
71 m_audioThumbCreated = clip.audioThumbCreated();
72 m_properties = clip.properties();
76 DocClipBase::~DocClipBase() {
77 if (m_thumbProd) delete m_thumbProd;
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()));
87 void DocClipBase::slotRequestAudioThumbs() {
88 emit getAudioThumbs();
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;
98 KThumb *DocClipBase::thumbProducer() {
102 bool DocClipBase::audioThumbCreated() const {
103 return m_audioThumbCreated;
106 void DocClipBase::setName(const QString name) {
110 const QString & DocClipBase::name() const {
115 uint DocClipBase::getId() const {
119 void DocClipBase::setId(const uint &newId) {
123 const CLIPTYPE & DocClipBase::clipType() const {
127 void DocClipBase::setClipType(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();
134 KUrl DocClipBase::fileURL() const {
135 QString res = m_properties.value("resource");
136 if (m_clipType != COLOR && !res.isEmpty()) return KUrl(res);
140 void DocClipBase::setClipThumbFrame(const uint &ix) {
141 m_properties.insert("thumbnail", QString::number((int) ix));
144 uint DocClipBase::getClipThumbFrame() const {
145 return (uint) m_properties.value("thumbnail").toInt();
148 const QString DocClipBase::description() const {
149 return m_properties.value("description");
152 const QString DocClipBase::getProperty(const QString prop) const {
153 return m_properties.value(prop);
156 void DocClipBase::setDuration(GenTime dur) {
160 const GenTime &DocClipBase::duration() const {
164 const GenTime &DocClipBase::maxDuration() const {
165 if (m_clipType == COLOR || m_clipType == IMAGE || m_clipType == TEXT || (m_clipType == SLIDESHOW && m_properties.value("loop") == "1")) {
166 const GenTime dur(10000, KdenliveSettings::project_fps());
172 bool DocClipBase::hasFileSize() const {
178 QDomElement DocClipBase::toXML() const {
181 QDomElement clip = doc.createElement("producer");
183 QMapIterator<QString, QString> i(m_properties);
184 while (i.hasNext()) {
186 if (!i.value().isEmpty()) clip.setAttribute(i.key(), i.value());
188 //doc.appendChild(clip);
189 //kDebug()<<"/// CLIP XML: "<<doc.toString();
193 DocClipBase *DocClipBase::
194 createClip(KdenliveDoc *doc, const QDomElement & element) {
195 DocClipBase *clip = 0;
197 QDomNode node = element;
199 if (element.tagName() != "kdenliveclip") {
201 "DocClipBase::createClip() element has unknown tagName : " << element.tagName();
205 QDomNode n = element.firstChild();
207 while (!n.isNull()) {
208 QDomElement e = n.toElement();
210 QString tagName = e.tagName();
211 if (e.tagName() == "avfile") {
212 // clip = DocClipAVFile::createClip(e);
213 } else if (e.tagName() == "DocTrackBaseList") {
214 // clip = DocClipProject::createClip(doc, e);
217 QDomText text = n.toText();
218 if (!text.isNull()) {
219 description = text.nodeValue();
226 kWarning() << "DocClipBase::createClip() unable to create clip";
228 // setup DocClipBase specifics of the clip.
229 QMap <QString, QString> props;
230 props.insert("description", description);
231 clip->setProperties(props);
232 clip->setAudioThumbCreated(false);
237 void DocClipBase::setAudioThumbCreated(bool isDone) {
238 m_audioThumbCreated = isDone;
242 QDomDocument DocClipBase::generateSceneList(bool, bool) const {
245 void DocClipBase::setThumbnail(const QPixmap & pixmap) {
246 m_thumbnail = pixmap;
249 const QPixmap & DocClipBase::thumbnail() const {
253 void DocClipBase::updateAudioThumbnail(QMap<int, QMap<int, QByteArray> > data) {
254 kDebug() << "CLIPBASE RECIEDVED AUDIO DATA*********************************************";
255 audioFrameChache = data;
256 m_audioThumbCreated = true;
260 QList < GenTime > DocClipBase::snapMarkers() const {
261 QList < GenTime > markers;
263 for (uint count = 0; count < m_snapMarkers.count(); ++count) {
264 markers.append(m_snapMarkers[count].time());
270 QList < CommentedTime > DocClipBase::commentedSnapMarkers() const {
271 return m_snapMarkers;
274 void DocClipBase::setSnapMarkers(QList < CommentedTime > markers) {
275 m_snapMarkers = markers;
278 void DocClipBase::addSnapMarker(const GenTime & time, QString comment) {
279 QList < CommentedTime >::Iterator it = m_snapMarkers.begin();
280 for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
281 if ((*it).time() >= time)
285 if ((it != m_snapMarkers.end()) && ((*it).time() == time)) {
286 (*it).setComment(comment);
287 //kError() << "trying to add Snap Marker that already exists, this will cause inconsistancies with undo/redo";
289 CommentedTime t(time, comment);
290 m_snapMarkers.insert(it, t);
295 void DocClipBase::editSnapMarker(const GenTime & time, QString comment) {
296 QList < CommentedTime >::Iterator it;
297 for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
298 if ((*it).time() == time)
301 if (it != m_snapMarkers.end()) {
302 (*it).setComment(comment);
304 kError() << "trying to edit Snap Marker that does not already exists";
308 QString DocClipBase::deleteSnapMarker(const GenTime & time) {
309 QString result = i18n("Marker");
310 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
312 while (itt != m_snapMarkers.end()) {
313 if ((*itt).time() == time)
318 if ((itt != m_snapMarkers.end()) && ((*itt).time() == time)) {
319 result = (*itt).comment();
320 m_snapMarkers.erase(itt);
326 GenTime DocClipBase::hasSnapMarkers(const GenTime & time) {
327 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
329 while (itt != m_snapMarkers.end()) {
330 if ((*itt).time() == time)
338 GenTime DocClipBase::findPreviousSnapMarker(const GenTime & currTime) {
340 for (it = 0; it < m_snapMarkers.count(); it++) {
341 if (m_snapMarkers[it].time() >= currTime)
344 if (it == 0) return GenTime();
345 else if (it == m_snapMarkers.count() - 1 && m_snapMarkers[it].time() < currTime)
346 return m_snapMarkers[it].time();
347 else return m_snapMarkers[it-1].time();
350 GenTime DocClipBase::findNextSnapMarker(const GenTime & currTime) {
352 for (it = 0; it < m_snapMarkers.count(); it++) {
353 if (m_snapMarkers[it].time() > currTime)
356 if (it < m_snapMarkers.count() && m_snapMarkers[it].time() > currTime) return m_snapMarkers[it].time();
360 QString DocClipBase::markerComment(GenTime t) {
361 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
363 while (itt != m_snapMarkers.end()) {
364 if ((*itt).time() == t)
365 return (*itt).comment();
368 return QString::null;
371 void DocClipBase::setProperties(QMap <QString, QString> properties) {
372 // changing clip type is not allowed
373 properties.remove("type");
374 QMapIterator<QString, QString> i(properties);
375 while (i.hasNext()) {
377 m_properties.insert(i.key(), i.value());
378 if (i.key() == "resource") m_thumbProd->updateClipUrl(KUrl(i.value()));
379 else if (i.key() == "out") setDuration(GenTime(i.value().toInt(), KdenliveSettings::project_fps()));
383 void DocClipBase::setProperty(QString key, QString value) {
384 m_properties.insert(key, value);
385 if (key == "resource") m_thumbProd->updateClipUrl(KUrl(value));
386 else if (key == "out") setDuration(GenTime(value.toInt(), KdenliveSettings::project_fps()));
389 QMap <QString, QString> DocClipBase::properties() const {
393 void DocClipBase::slotGetAudioThumbs() {
394 if (m_thumbProd == NULL) return;
395 if (m_audioThumbCreated) {
396 if (m_audioTimer != NULL)
397 m_audioTimer->stop();
399 if (m_audioTimer != NULL)
400 m_audioTimer->start(2000);
401 double lengthInFrames = duration().frames(/*framesPerSecond()*/25);
402 m_thumbProd->getAudioThumbs(2, 0, lengthInFrames /*must be number of frames*/, 20);