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