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