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_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");
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()));
41 //m_properties.insert("out", QString::number(out));
43 if (m_name.isEmpty()) m_name = url.fileName();
45 if (!url.isEmpty() && QFile::exists(url.path())) {
46 m_thumbProd = new KThumb(clipManager, url);
47 if (m_clipType == AV || m_clipType == AUDIO) slotCreateAudioTimer();
49 //kDebug() << "type is video" << (m_clipType == AV) << " " << m_clipType;
54 DocClipBase::DocClipBase(const DocClipBase& clip) {
56 m_clipType = clip.clipType();
58 m_duration = clip.duration();
59 m_audioThumbCreated = clip.audioThumbCreated();
60 m_properties = clip.properties();
63 DocClipBase & DocClipBase::operator=(const DocClipBase & clip) {
64 DocClipBase::operator=(clip);
66 m_clipType = clip.clipType();
68 m_duration = clip.duration();
69 m_audioThumbCreated = clip.audioThumbCreated();
70 m_properties = clip.properties();
74 DocClipBase::~DocClipBase() {
75 if (m_thumbProd) delete m_thumbProd;
78 void DocClipBase::slotCreateAudioTimer() {
79 connect(m_thumbProd, SIGNAL(audioThumbReady(QMap <int, QMap <int, QByteArray> >)), this , SLOT(updateAudioThumbnail(QMap <int, QMap <int, QByteArray> >)));
80 connect(this, SIGNAL(getAudioThumbs()), this , SLOT(slotGetAudioThumbs()));
81 m_audioTimer = new QTimer(this);
82 connect(m_audioTimer, SIGNAL(timeout()), this, SLOT(slotGetAudioThumbs()));
85 void DocClipBase::slotRequestAudioThumbs() {
86 emit getAudioThumbs();
89 void DocClipBase::slotClearAudioCache() {
90 if (m_thumbProd) m_thumbProd->stopAudioThumbs();
91 if (m_audioTimer != NULL) m_audioTimer->stop();
92 audioFrameChache.clear();
93 m_audioThumbCreated = false;
96 KThumb *DocClipBase::thumbProducer() {
100 bool DocClipBase::audioThumbCreated() const {
101 return m_audioThumbCreated;
104 void DocClipBase::setName(const QString name) {
108 const QString & DocClipBase::name() const {
113 uint DocClipBase::getId() const {
117 void DocClipBase::setId(const uint &newId) {
121 const CLIPTYPE & DocClipBase::clipType() const {
125 void DocClipBase::setClipType(CLIPTYPE type) {
127 m_properties.insert("type", QString::number((int) type));
128 if (m_thumbProd && m_audioTimer == NULL && (m_clipType == AV || m_clipType == AUDIO))
129 slotCreateAudioTimer();
132 KUrl DocClipBase::fileURL() const {
133 QString res = m_properties.value("resource");
134 if (m_clipType != COLOR && !res.isEmpty()) return KUrl(res);
138 void DocClipBase::setProjectThumbFrame(const uint &ix) {
139 m_projectThumbFrame = ix;
142 uint DocClipBase::getProjectThumbFrame() const {
143 return m_projectThumbFrame;
146 const QString DocClipBase::description() const {
147 return m_properties.value("description");
150 const QString DocClipBase::getProperty(const QString prop) const {
151 return m_properties.value(prop);
154 void DocClipBase::setDuration(GenTime dur) {
158 const GenTime &DocClipBase::duration() const {
162 const GenTime &DocClipBase::maxDuration() const {
163 if (m_clipType == COLOR || m_clipType == IMAGE || m_clipType == TEXT || (m_clipType == SLIDESHOW && m_properties.value("loop") == "1")) {
164 GenTime dur(10000, KdenliveSettings::project_fps());
170 bool DocClipBase::hasFileSize() const {
176 QDomElement DocClipBase::toXML() const {
179 QDomElement clip = doc.createElement("producer");
181 QMapIterator<QString, QString> i(m_properties);
182 while (i.hasNext()) {
184 if (!i.value().isEmpty()) clip.setAttribute(i.key(), i.value());
186 //doc.appendChild(clip);
187 //kDebug()<<"/// CLIP XML: "<<doc.toString();
191 DocClipBase *DocClipBase::
192 createClip(KdenliveDoc *doc, const QDomElement & element) {
193 DocClipBase *clip = 0;
195 QDomNode node = element;
197 if (element.tagName() != "kdenliveclip") {
199 "DocClipBase::createClip() element has unknown tagName : " << element.tagName();
203 QDomNode n = element.firstChild();
205 while (!n.isNull()) {
206 QDomElement e = n.toElement();
208 QString tagName = e.tagName();
209 if (e.tagName() == "avfile") {
210 // clip = DocClipAVFile::createClip(e);
211 } else if (e.tagName() == "DocTrackBaseList") {
212 // clip = DocClipProject::createClip(doc, e);
215 QDomText text = n.toText();
216 if (!text.isNull()) {
217 description = text.nodeValue();
224 kWarning() << "DocClipBase::createClip() unable to create clip";
226 // setup DocClipBase specifics of the clip.
227 QMap <QString, QString> props;
228 props.insert("description", description);
229 clip->setProperties(props);
230 clip->setAudioThumbCreated(false);
235 void DocClipBase::setAudioThumbCreated(bool isDone) {
236 m_audioThumbCreated = isDone;
240 QDomDocument DocClipBase::generateSceneList(bool, bool) const {
243 void DocClipBase::setThumbnail(const QPixmap & pixmap) {
244 m_thumbnail = pixmap;
247 const QPixmap & DocClipBase::thumbnail() const {
251 void DocClipBase::updateAudioThumbnail(QMap<int, QMap<int, QByteArray> > data) {
252 kDebug() << "CLIPBASE RECIEDVED AUDIO DATA*********************************************";
253 audioFrameChache = data;
254 m_audioThumbCreated = true;
258 QList < GenTime > DocClipBase::snapMarkers() const {
259 QList < GenTime > markers;
261 for (uint count = 0; count < m_snapMarkers.count(); ++count) {
262 markers.append(m_snapMarkers[count].time());
268 QList < CommentedTime > DocClipBase::commentedSnapMarkers() const {
269 return m_snapMarkers;
272 void DocClipBase::setSnapMarkers(QList < CommentedTime > markers) {
273 m_snapMarkers = markers;
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)
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";
287 CommentedTime t(time, comment);
288 m_snapMarkers.insert(it, t);
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)
299 if (it != m_snapMarkers.end()) {
300 (*it).setComment(comment);
302 kError() << "trying to edit Snap Marker that does not already exists";
306 QString DocClipBase::deleteSnapMarker(const GenTime & time) {
307 QString result = i18n("Marker");
308 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
310 while (itt != m_snapMarkers.end()) {
311 if ((*itt).time() == time)
316 if ((itt != m_snapMarkers.end()) && ((*itt).time() == time)) {
317 result = (*itt).comment();
318 m_snapMarkers.erase(itt);
324 GenTime DocClipBase::hasSnapMarkers(const GenTime & time) {
325 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
327 while (itt != m_snapMarkers.end()) {
328 if ((*itt).time() == time)
336 GenTime DocClipBase::findPreviousSnapMarker(const GenTime & currTime) {
338 for (it = 0; it < m_snapMarkers.count(); it++) {
339 if (m_snapMarkers[it].time() >= currTime)
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();
348 GenTime DocClipBase::findNextSnapMarker(const GenTime & currTime) {
350 for (it = 0; it < m_snapMarkers.count(); it++) {
351 if (m_snapMarkers[it].time() > currTime)
354 if (it < m_snapMarkers.count() && m_snapMarkers[it].time() > currTime) return m_snapMarkers[it].time();
358 QString DocClipBase::markerComment(GenTime t) {
359 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
361 while (itt != m_snapMarkers.end()) {
362 if ((*itt).time() == t)
363 return (*itt).comment();
366 return QString::null;
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()) {
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()));
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()));
387 QMap <QString, QString> DocClipBase::properties() const {
391 void DocClipBase::slotGetAudioThumbs() {
392 if (m_thumbProd == NULL) return;
393 if (m_audioThumbCreated) {
394 if (m_audioTimer != NULL)
395 m_audioTimer->stop();
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);