1 /***************************************************************************
2 * Copyright (C) 2008 by Jean-Baptiste Mardelle (jb@kdenlive.org) *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
20 #include <KFileDialog>
21 #include <kio/netaccess.h>
23 #include "addclipcommand.h"
24 #include "kdenlivesettings.h"
25 #include "clipmanager.h"
26 #include "docclipbase.h"
27 #include "kdenlivedoc.h"
29 #include <mlt++/Mlt.h>
31 ClipManager::ClipManager(KdenliveDoc *doc): m_doc(doc), m_audioThumbsEnabled(false), m_audioThumbsQueue(QList <QString> ()), m_generatingAudioId(QString()) {
33 m_folderIdCounter = 1;
36 ClipManager::~ClipManager() {
37 qDeleteAll(m_clipList);
40 void ClipManager::checkAudioThumbs() {
41 if (m_audioThumbsEnabled == KdenliveSettings::audiothumbnails()) return;
42 m_audioThumbsEnabled = KdenliveSettings::audiothumbnails();
43 for (int i = 0; i < m_clipList.count(); i++) {
44 if (m_audioThumbsEnabled) m_audioThumbsQueue.append(m_clipList.at(i)->getId());
45 else m_clipList.at(i)->slotClearAudioCache();
47 if (m_audioThumbsEnabled) {
48 if (m_generatingAudioId.isEmpty()) startAudioThumbsGeneration();
50 m_audioThumbsQueue.clear();
51 m_generatingAudioId = QString();
55 void ClipManager::askForAudioThumb(const QString &id) {
56 DocClipBase *clip = getClipById(id);
57 if (clip && KdenliveSettings::audiothumbnails()) {
58 m_audioThumbsQueue.append(id);
59 if (m_generatingAudioId.isEmpty()) startAudioThumbsGeneration();
63 void ClipManager::startAudioThumbsGeneration() {
64 if (!KdenliveSettings::audiothumbnails()) {
65 m_audioThumbsQueue.clear();
66 m_generatingAudioId = QString();
69 if (!m_audioThumbsQueue.isEmpty()) {
70 m_generatingAudioId = m_audioThumbsQueue.takeFirst();
71 DocClipBase *clip = getClipById(m_generatingAudioId);
72 if (!clip || !clip->slotGetAudioThumbs())
73 endAudioThumbsGeneration(m_generatingAudioId);
75 m_generatingAudioId = QString();
79 void ClipManager::endAudioThumbsGeneration(const QString &requestedId) {
80 if (!KdenliveSettings::audiothumbnails()) {
81 m_audioThumbsQueue.clear();
82 m_generatingAudioId = QString();
85 if (!m_audioThumbsQueue.isEmpty()) {
86 if (m_generatingAudioId == requestedId) {
87 startAudioThumbsGeneration();
90 m_generatingAudioId = QString();
94 void ClipManager::setThumbsProgress(const QString &message, int progress) {
95 m_doc->setThumbsProgress(message, progress);
98 QList <DocClipBase*> ClipManager::documentClipList() {
102 void ClipManager::addClip(DocClipBase *clip) {
103 m_clipList.append(clip);
104 const QString id = clip->getId();
105 if (id.toInt() >= m_clipIdCounter) m_clipIdCounter = id.toInt() + 1;
106 const QString gid = clip->getProperty("groupid");
107 if (!gid.isEmpty() && gid.toInt() >= m_folderIdCounter) m_folderIdCounter = gid.toInt() + 1;
110 void ClipManager::slotDeleteClip(const QString &clipId) {
111 for (int i = 0; i < m_clipList.count(); i++) {
112 if (m_clipList.at(i)->getId() == clipId) {
113 AddClipCommand *command = new AddClipCommand(m_doc, m_clipList.at(i)->toXML(), clipId, false);
114 m_doc->commandStack()->push(command);
120 void ClipManager::deleteClip(const QString &clipId) {
121 for (int i = 0; i < m_clipList.count(); i++) {
122 if (m_clipList.at(i)->getId() == clipId) {
123 DocClipBase *clip = m_clipList.takeAt(i);
131 DocClipBase *ClipManager::getClipAt(int pos) {
132 return m_clipList.at(pos);
135 DocClipBase *ClipManager::getClipById(QString clipId) {
136 //kDebug() << "++++ CLIP MAN, LOOKING FOR CLIP ID: " << clipId;
137 clipId = clipId.section('_', 0, 0);
138 for (int i = 0; i < m_clipList.count(); i++) {
139 if (m_clipList.at(i)->getId() == clipId) {
140 //kDebug() << "++++ CLIP MAN, FOUND FOR CLIP ID: " << clipId;
141 return m_clipList.at(i);
147 DocClipBase *ClipManager::getClipByResource(QString resource) {
148 for (int i = 0; i < m_clipList.count(); i++) {
149 if (m_clipList.at(i)->getProperty("resource") == resource) {
150 return m_clipList.at(i);
156 void ClipManager::updatePreviewSettings() {
157 for (int i = 0; i < m_clipList.count(); i++) {
158 if (m_clipList.at(i)->clipType() == AV || m_clipList.at(i)->clipType() == VIDEO) {
159 if (m_clipList.at(i)->producerProperty("meta.media.0.codec.name") && strcmp(m_clipList.at(i)->producerProperty("meta.media.0.codec.name"), "h264") == 0) {
160 if (KdenliveSettings::dropbframes()) {
161 m_clipList[i]->setProducerProperty("skip_loop_filter", "all");
162 m_clipList[i]->setProducerProperty("skip_frame", "bidir");
164 m_clipList[i]->setProducerProperty("skip_loop_filter", "");
165 m_clipList[i]->setProducerProperty("skip_frame", "");
172 void ClipManager::resetProducersList(QList <Mlt::Producer *> prods) {
173 for (int i = 0; i < m_clipList.count(); i++) {
174 if (m_clipList.at(i)->numReferences() > 0) {
175 m_clipList.at(i)->deleteProducers();
179 for (int i = 0; i < prods.count(); i++) {
180 id = prods.at(i)->get("id");
181 if (id.contains('_')) id = id.section('_', 0, 0);
182 DocClipBase *clip = getClipById(id);
184 clip->setProducer(prods.at(i));
185 kDebug() << "// // // REPLACE CLIP: " << id;
190 void ClipManager::slotAddClipList(const KUrl::List urls, const QString group, const QString &groupId) {
191 QUndoCommand *addClips = new QUndoCommand();
192 addClips->setText(i18n("Add clips"));
194 foreach(const KUrl file, urls) {
195 if (KIO::NetAccess::exists(file, KIO::NetAccess::SourceSide, NULL)) {
197 QDomElement prod = doc.createElement("producer");
198 if (!group.isEmpty()) {
199 prod.setAttribute("groupname", group);
200 prod.setAttribute("groupid", groupId);
202 prod.setAttribute("resource", file.path());
203 uint id = m_clipIdCounter++;
204 prod.setAttribute("id", QString::number(id));
205 KMimeType::Ptr type = KMimeType::findByUrl(file);
206 if (type->name().startsWith("image/")) {
207 prod.setAttribute("type", (int) IMAGE);
208 prod.setAttribute("in", "0");
209 prod.setAttribute("out", m_doc->getFramePos(KdenliveSettings::image_duration()) - 1);
211 new AddClipCommand(m_doc, prod, QString::number(id), true, addClips);
214 m_doc->commandStack()->push(addClips);
217 void ClipManager::slotAddClipFile(const KUrl url, const QString group, const QString &groupId) {
218 kDebug() << "///// CLIP MANAGER, ADDING CLIP: " << url;
220 QDomElement prod = doc.createElement("producer");
221 prod.setAttribute("resource", url.path());
222 uint id = m_clipIdCounter++;
223 prod.setAttribute("id", QString::number(id));
224 if (!group.isEmpty()) {
225 prod.setAttribute("groupname", group);
226 prod.setAttribute("groupid", groupId);
228 KMimeType::Ptr type = KMimeType::findByUrl(url);
229 if (type->name().startsWith("image/")) {
230 prod.setAttribute("type", (int) IMAGE);
231 prod.setAttribute("in", "0");
232 prod.setAttribute("out", m_doc->getFramePos(KdenliveSettings::image_duration()) - 1);
234 AddClipCommand *command = new AddClipCommand(m_doc, prod, QString::number(id), true);
235 m_doc->commandStack()->push(command);
238 void ClipManager::slotAddColorClipFile(const QString name, const QString color, QString duration, const QString group, const QString &groupId) {
240 QDomElement prod = doc.createElement("producer");
241 prod.setAttribute("mlt_service", "colour");
242 prod.setAttribute("colour", color);
243 prod.setAttribute("type", (int) COLOR);
244 uint id = m_clipIdCounter++;
245 prod.setAttribute("id", QString::number(id));
246 prod.setAttribute("in", "0");
247 prod.setAttribute("out", m_doc->getFramePos(duration) - 1);
248 prod.setAttribute("name", name);
249 if (!group.isEmpty()) {
250 prod.setAttribute("groupname", group);
251 prod.setAttribute("groupid", groupId);
253 AddClipCommand *command = new AddClipCommand(m_doc, prod, QString::number(id), true);
254 m_doc->commandStack()->push(command);
257 void ClipManager::slotAddSlideshowClipFile(const QString name, const QString path, int count, const QString duration, const bool loop, const bool fade, const QString &luma_duration, const QString &luma_file, const int softness, QString group, const QString &groupId) {
259 QDomElement prod = doc.createElement("producer");
260 prod.setAttribute("resource", path);
261 prod.setAttribute("type", (int) SLIDESHOW);
262 uint id = m_clipIdCounter++;
263 prod.setAttribute("id", QString::number(id));
264 prod.setAttribute("in", "0");
265 prod.setAttribute("out", m_doc->getFramePos(duration) * count - 1);
266 prod.setAttribute("ttl", m_doc->getFramePos(duration));
267 prod.setAttribute("luma_duration", m_doc->getFramePos(luma_duration));
268 prod.setAttribute("name", name);
269 prod.setAttribute("loop", loop);
270 prod.setAttribute("fade", fade);
271 prod.setAttribute("softness", QString::number(softness));
272 prod.setAttribute("luma_file", luma_file);
273 if (!group.isEmpty()) {
274 prod.setAttribute("groupname", group);
275 prod.setAttribute("groupid", groupId);
277 AddClipCommand *command = new AddClipCommand(m_doc, prod, QString::number(id), true);
278 m_doc->commandStack()->push(command);
283 void ClipManager::slotAddTextClipFile(const QString titleName, const QString imagePath, const QString xml, const QString group, const QString &groupId) {
285 QDomElement prod = doc.createElement("producer");
286 prod.setAttribute("resource", imagePath);
287 prod.setAttribute("titlename", titleName);
288 prod.setAttribute("xmldata", xml);
289 uint id = m_clipIdCounter++;
290 prod.setAttribute("id", QString::number(id));
291 if (!group.isEmpty()) {
292 prod.setAttribute("groupname", group);
293 prod.setAttribute("groupid", groupId);
295 prod.setAttribute("type", (int) TEXT);
296 prod.setAttribute("transparency", "1");
297 prod.setAttribute("in", "0");
298 prod.setAttribute("out", m_doc->getFramePos(KdenliveSettings::image_duration()) - 1);
299 AddClipCommand *command = new AddClipCommand(m_doc, prod, QString::number(id), true);
300 m_doc->commandStack()->push(command);
303 int ClipManager::getFreeClipId() {
304 return m_clipIdCounter++;
307 int ClipManager::getFreeFolderId() {
308 return m_folderIdCounter++;
311 int ClipManager::lastClipId() const {
312 return m_clipIdCounter - 1;
315 QString ClipManager::projectFolder() const {
316 return m_doc->projectFolder().path();