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();
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 audioFrameChache.clear();
91 m_audioThumbCreated = false;
94 KThumb *DocClipBase::thumbProducer() {
98 bool DocClipBase::audioThumbCreated() const {
99 return m_audioThumbCreated;
102 void DocClipBase::setName(const QString name) {
106 const QString & DocClipBase::name() const {
111 uint DocClipBase::getId() const {
115 void DocClipBase::setId(const uint &newId) {
119 const CLIPTYPE & DocClipBase::clipType() const {
123 void DocClipBase::setClipType(CLIPTYPE type) {
125 if (m_audioTimer == NULL && (m_clipType == AV || m_clipType == AUDIO))
126 slotCreateAudioTimer();
129 KUrl DocClipBase::fileURL() const {
130 QString res = m_properties.value("resource");
131 if (m_clipType != COLOR && !res.isEmpty()) return KUrl(res);
135 void DocClipBase::setProjectThumbFrame(const uint &ix) {
136 m_projectThumbFrame = ix;
139 uint DocClipBase::getProjectThumbFrame() const {
140 return m_projectThumbFrame;
143 const QString DocClipBase::description() const {
144 return m_properties.value("description");
147 const QString DocClipBase::getProperty(const QString prop) const {
148 return m_properties.value(prop);
151 void DocClipBase::setDuration(GenTime dur) {
155 const GenTime &DocClipBase::duration() const {
159 const GenTime &DocClipBase::maxDuration() const {
160 if (m_clipType == COLOR || m_clipType == IMAGE || m_clipType == TEXT || (m_clipType == SLIDESHOW && m_properties.value("loop") == "1")) {
161 GenTime dur(10000, KdenliveSettings::project_fps());
167 bool DocClipBase::hasFileSize() const {
173 QDomElement DocClipBase::toXML() const {
176 QDomElement clip = doc.createElement("producer");
178 QMapIterator<QString, QString> i(m_properties);
179 while (i.hasNext()) {
181 if (!i.value().isEmpty()) clip.setAttribute(i.key(), i.value());
183 //doc.appendChild(clip);
184 //kDebug()<<"/// CLIP XML: "<<doc.toString();
188 DocClipBase *DocClipBase::
189 createClip(KdenliveDoc *doc, const QDomElement & element) {
190 DocClipBase *clip = 0;
192 QDomNode node = element;
194 if (element.tagName() != "kdenliveclip") {
196 "DocClipBase::createClip() element has unknown tagName : " <<
197 element.tagName() << endl;
201 QDomNode n = element.firstChild();
203 while (!n.isNull()) {
204 QDomElement e = n.toElement();
206 QString tagName = e.tagName();
207 if (e.tagName() == "avfile") {
208 // clip = DocClipAVFile::createClip(e);
209 } else if (e.tagName() == "DocTrackBaseList") {
210 // clip = DocClipProject::createClip(doc, e);
213 QDomText text = n.toText();
214 if (!text.isNull()) {
215 description = text.nodeValue();
222 kWarning() << "DocClipBase::createClip() unable to create clip" <<
225 // setup DocClipBase specifics of the clip.
226 QMap <QString, QString> props;
227 props.insert("description", description);
228 clip->setProperties(props);
229 clip->setAudioThumbCreated(false);
234 void DocClipBase::setAudioThumbCreated(bool isDone) {
235 m_audioThumbCreated = isDone;
239 QDomDocument DocClipBase::generateSceneList(bool, bool) const {
242 void DocClipBase::setThumbnail(const QPixmap & pixmap) {
243 m_thumbnail = pixmap;
246 const QPixmap & DocClipBase::thumbnail() const {
250 void DocClipBase::updateAudioThumbnail(QMap<int, QMap<int, QByteArray> > data) {
251 kDebug() << "CLIPBASE RECIEDVED AUDIO DATA*********************************************";
252 audioFrameChache = data;
253 m_audioThumbCreated = true;
257 QList < GenTime > DocClipBase::snapMarkers() const {
258 QList < GenTime > markers;
260 for (uint count = 0; count < m_snapMarkers.count(); ++count) {
261 markers.append(m_snapMarkers[count].time());
267 QList < CommentedTime > DocClipBase::commentedSnapMarkers() const {
268 return m_snapMarkers;
271 void DocClipBase::setSnapMarkers(QList < CommentedTime > markers) {
272 m_snapMarkers = markers;
275 void DocClipBase::addSnapMarker(const GenTime & time, QString comment) {
276 QList < CommentedTime >::Iterator it = m_snapMarkers.begin();
277 for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
278 if ((*it).time() >= time)
282 if ((it != m_snapMarkers.end()) && ((*it).time() == time)) {
284 "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);
303 "trying to edit Snap Marker that does not already exists" << endl;
307 QString DocClipBase::deleteSnapMarker(const GenTime & time) {
308 QString result = i18n("Marker");
309 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
311 while (itt != m_snapMarkers.end()) {
312 if ((*itt).time() == time)
317 if ((itt != m_snapMarkers.end()) && ((*itt).time() == time)) {
318 result = (*itt).comment();
319 m_snapMarkers.erase(itt);
325 GenTime DocClipBase::hasSnapMarkers(const GenTime & time) {
326 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
328 while (itt != m_snapMarkers.end()) {
329 if ((*itt).time() == time)
337 GenTime DocClipBase::findPreviousSnapMarker(const GenTime & currTime) {
339 for (it = 0; it < m_snapMarkers.count(); it++) {
340 if (m_snapMarkers[it].time() >= currTime)
343 if (it == 0) return GenTime();
344 else if (it == m_snapMarkers.count() - 1 && m_snapMarkers[it].time() < currTime)
345 return m_snapMarkers[it].time();
346 else return m_snapMarkers[it-1].time();
349 GenTime DocClipBase::findNextSnapMarker(const GenTime & currTime) {
351 for (it = 0; it < m_snapMarkers.count(); it++) {
352 if (m_snapMarkers[it].time() > currTime)
355 if (it < m_snapMarkers.count() && m_snapMarkers[it].time() > currTime) return m_snapMarkers[it].time();
359 QString DocClipBase::markerComment(GenTime t) {
360 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
362 while (itt != m_snapMarkers.end()) {
363 if ((*itt).time() == t)
364 return (*itt).comment();
367 return QString::null;
370 void DocClipBase::setProperties(QMap <QString, QString> properties) {
371 QMapIterator<QString, QString> i(properties);
372 while (i.hasNext()) {
374 m_properties.insert(i.key(), i.value());
375 if (i.key() == "resource") m_thumbProd->updateClipUrl(KUrl(i.value()));
376 else if (i.key() == "out") setDuration(GenTime(i.value().toInt(), KdenliveSettings::project_fps()));
380 void DocClipBase::setProperty(QString key, QString value) {
381 m_properties.insert(key, value);
382 if (key == "resource") m_thumbProd->updateClipUrl(KUrl(value));
383 else if (key == "out") setDuration(GenTime(value.toInt(), KdenliveSettings::project_fps()));
386 QMap <QString, QString> DocClipBase::properties() const {
390 void DocClipBase::slotGetAudioThumbs() {
392 if (m_audioThumbCreated) {
393 if (m_audioTimer != NULL)
394 m_audioTimer->stop();
396 if (m_audioTimer != NULL)
397 m_audioTimer->start(2000);
398 double lengthInFrames = duration().frames(/*framesPerSecond()*/25);
399 m_thumbProd->getAudioThumbs(2, 0, lengthInFrames /*must be number of frames*/, 20);