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