1 /***************************************************************************
2 * Copyright (C) 2007 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 ***************************************************************************/
21 #include <QMouseEvent>
22 #include <QStylePainter>
31 #include <KFileDialog>
32 #include <KInputDialog>
33 #include <kio/netaccess.h>
34 #include <KMessageBox>
36 #include <nepomuk/global.h>
37 #include <nepomuk/resource.h>
38 #include <nepomuk/tag.h>
40 #include "projectlist.h"
41 #include "projectitem.h"
42 #include "kdenlivesettings.h"
43 #include "slideshowclip.h"
44 #include "ui_colorclip_ui.h"
47 #include "definitions.h"
48 #include "clipmanager.h"
49 #include "docclipbase.h"
50 #include "kdenlivedoc.h"
53 #include "projectlistview.h"
55 ProjectList::ProjectList(QWidget *parent)
56 : QWidget(parent), m_render(NULL), m_fps(-1), m_commandStack(NULL) {
58 QWidget *vbox = new QWidget;
59 listView = new ProjectListView(this);;
60 QVBoxLayout *layout = new QVBoxLayout;
64 searchView = new KTreeWidgetSearchLine(this);
65 m_toolbar = new QToolBar("projectToolBar", this);
66 m_toolbar->addWidget(searchView);
68 QToolButton *addButton = new QToolButton(m_toolbar);
69 QMenu *addMenu = new QMenu(this);
70 addButton->setMenu(addMenu);
71 addButton->setPopupMode(QToolButton::MenuButtonPopup);
72 m_toolbar->addWidget(addButton);
74 QAction *addClipButton = addMenu->addAction(KIcon("document-new"), i18n("Add Clip"));
75 connect(addClipButton, SIGNAL(triggered()), this, SLOT(slotAddClip()));
77 QAction *addColorClip = addMenu->addAction(KIcon("document-new"), i18n("Add Color Clip"));
78 connect(addColorClip, SIGNAL(triggered()), this, SLOT(slotAddColorClip()));
80 QAction *addSlideClip = addMenu->addAction(KIcon("document-new"), i18n("Add Slideshow Clip"));
81 connect(addSlideClip, SIGNAL(triggered()), this, SLOT(slotAddSlideshowClip()));
83 QAction *addTitleClip = addMenu->addAction(KIcon("document-new"), i18n("Add Title Clip"));
84 connect(addTitleClip, SIGNAL(triggered()), this, SLOT(slotAddTitleClip()));
86 m_deleteAction = m_toolbar->addAction(KIcon("edit-delete"), i18n("Delete Clip"));
87 connect(m_deleteAction, SIGNAL(triggered()), this, SLOT(slotRemoveClip()));
89 m_editAction = m_toolbar->addAction(KIcon("document-properties"), i18n("Edit Clip"));
90 connect(m_editAction, SIGNAL(triggered()), this, SLOT(slotEditClip()));
92 QAction *addFolderButton = addMenu->addAction(KIcon("folder-new"), i18n("Create Folder"));
93 connect(addFolderButton, SIGNAL(triggered()), this, SLOT(slotAddFolder()));
95 addButton->setDefaultAction(addClipButton);
97 layout->addWidget(m_toolbar);
98 layout->addWidget(listView);
100 //m_toolbar->setEnabled(false);
102 searchView->setTreeWidget(listView);
104 m_menu = new QMenu();
105 m_menu->addAction(addClipButton);
106 m_menu->addAction(addColorClip);
107 m_menu->addAction(addTitleClip);
108 m_menu->addAction(m_editAction);
109 m_menu->addAction(m_deleteAction);
110 m_menu->addAction(addFolderButton);
111 m_menu->insertSeparator(m_deleteAction);
113 connect(listView, SIGNAL(itemSelectionChanged()), this, SLOT(slotClipSelected()));
114 connect(listView, SIGNAL(requestMenu(const QPoint &, QTreeWidgetItem *)), this, SLOT(slotContextMenu(const QPoint &, QTreeWidgetItem *)));
115 connect(listView, SIGNAL(addClip()), this, SLOT(slotAddClip()));
116 connect(listView, SIGNAL(addClip(QUrl, const QString &)), this, SLOT(slotAddClip(QUrl, const QString &)));
117 connect(listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotItemEdited(QTreeWidgetItem *, int)));
118 connect(listView, SIGNAL(showProperties(DocClipBase *)), this, SIGNAL(showClipProperties(DocClipBase *)));
120 m_listViewDelegate = new ItemDelegate(listView);
121 listView->setItemDelegate(m_listViewDelegate);
124 ProjectList::~ProjectList() {
131 void ProjectList::setRenderer(Render *projectRender) {
132 m_render = projectRender;
135 void ProjectList::slotClipSelected() {
136 ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
137 if (item && !item->isGroup()) emit clipSelected(item->toXml());
140 void ProjectList::slotUpdateClipProperties(int id, QMap <QString, QString> properties) {
141 ProjectItem *item = getItemById(id);
143 slotUpdateClipProperties(item, properties);
144 if (properties.contains("colour") || properties.contains("resource")) slotRefreshClipThumbnail(item);
145 if (properties.contains("out")) item->changeDuration(properties.value("out").toInt());
149 void ProjectList::slotUpdateClipProperties(ProjectItem *clip, QMap <QString, QString> properties) {
151 clip->setProperties(properties);
152 if (properties.contains("description")) {
153 CLIPTYPE type = clip->clipType();
154 clip->setText(2, properties.value("description"));
155 if (type == AUDIO || type == VIDEO || type == AV || type == IMAGE || type == PLAYLIST) {
156 // Use Nepomuk system to store clip description
157 Nepomuk::Resource f(clip->clipUrl().path());
158 if (f.isValid()) f.setDescription(properties.value("description"));
163 void ProjectList::slotItemEdited(QTreeWidgetItem *item, int column) {
164 ProjectItem *clip = static_cast <ProjectItem*>(item);
166 QMap <QString, QString> props;
167 props["description"] = item->text(2);
168 slotUpdateClipProperties(clip, props);
169 } else if (column == 1 && clip->clipType() == FOLDER) {
170 m_doc->slotEditFolder(item->text(1), clip->groupName(), clip->clipId());
174 void ProjectList::slotContextMenu(const QPoint &pos, QTreeWidgetItem *item) {
179 m_editAction->setEnabled(enable);
180 m_deleteAction->setEnabled(enable);
185 void ProjectList::slotRemoveClip() {
186 if (!listView->currentItem()) return;
187 ProjectItem *item = static_cast <ProjectItem *>(listView->currentItem());
189 QMap <QString, int> folderids;
190 if (item->clipType() == FOLDER) folderids[item->groupName()] = item->clipId();
191 else ids << item->clipId();
192 if (item->numReferences() > 0) {
193 if (KMessageBox::questionYesNo(this, i18np("Delete clip <b>%2</b> ?<br>This will also remove the clip in timeline", "Delete clip <b>%2</b> ?<br>This will also remove its %1 clips in timeline", item->numReferences(), item->names().at(1)), i18n("Delete Clip")) != KMessageBox::Yes) return;
194 } else if (item->clipType() == FOLDER && item->childCount() > 0) {
195 int children = item->childCount();
196 if (KMessageBox::questionYesNo(this, i18n("Delete folder <b>%2</b> ?<br>This will also remove the %1 clips in that folder", children, item->names().at(1)), i18n("Delete Folder")) != KMessageBox::Yes) return;
197 for (int i = 0; i < children; ++i) {
198 ProjectItem *child = static_cast <ProjectItem *>(item->child(i));
199 ids << child->clipId();
202 if (!ids.isEmpty()) m_doc->deleteProjectClip(ids);
203 if (!folderids.isEmpty()) m_doc->deleteProjectFolder(folderids);
206 void ProjectList::selectItemById(const int clipId) {
207 ProjectItem *item = getItemById(clipId);
208 if (item) listView->setCurrentItem(item);
211 void ProjectList::addClip(const QStringList &name, const QDomElement &elem, const int clipId, const KUrl &url, const QString &group, int parentId) {
212 kDebug() << "///////// ADDING VCLIP=: " << name;
214 ProjectItem *groupItem = NULL;
216 if (group.isEmpty()) groupName = elem.attribute("groupname", QString::null);
217 else groupName = group;
218 if (elem.isNull() && url.isEmpty()) {
220 groupName = name.at(1);
221 QList<QTreeWidgetItem *> groupList = listView->findItems(groupName, Qt::MatchExactly, 1);
222 if (groupList.isEmpty()) {
223 (void) new ProjectItem(listView, name, m_doc->getFreeClipId());
228 if (parentId != -1) {
229 groupItem = getItemById(parentId);
230 } else if (!groupName.isEmpty()) {
231 // Clip is in a group
232 QList<QTreeWidgetItem *> groupList = listView->findItems(groupName, Qt::MatchExactly, 1);
234 if (groupList.isEmpty()) {
235 QStringList itemName;
236 itemName << QString::null << groupName;
237 kDebug() << "------- CREATING NEW GRP: " << itemName;
238 groupItem = new ProjectItem(listView, itemName, m_doc->getFreeClipId());
239 } else groupItem = (ProjectItem *) groupList.first();
241 if (groupItem) item = new ProjectItem(groupItem, name, elem, clipId);
242 else item = new ProjectItem(listView, name, elem, clipId);
243 if (!url.isEmpty()) {
244 // if file has Nepomuk comment, use it
245 Nepomuk::Resource f(url.path());
247 if (f.isValid()) annotation = f.description();
249 if (!annotation.isEmpty()) item->setText(2, annotation);
250 QString resource = url.path();
251 if (resource.endsWith("westley") || resource.endsWith("kdenlive")) {
254 if (KIO::NetAccess::download(url, tmpfile, 0)) {
256 if (file.open(QIODevice::ReadOnly)) {
257 doc.setContent(&file, false);
260 KIO::NetAccess::removeTempFile(tmpfile);
262 QDomNodeList subProds = doc.elementsByTagName("producer");
263 int ct = subProds.count();
264 for (int i = 0; i < ct ; i++) {
265 QDomElement e = subProds.item(i).toElement();
267 addProducer(e, clipId);
277 QDomElement element = doc.createElement("producer");
278 element.setAttribute("resource", url.path());
279 emit getFileProperties(element, clipId);
280 } else emit getFileProperties(elem, clipId);
281 selectItemById(clipId);
284 void ProjectList::slotDeleteClip(int clipId) {
285 ProjectItem *item = getItemById(clipId);
286 QTreeWidgetItem *p = item->parent();
288 kDebug() << "/////// DELETEED CLIP HAS A PARENT... " << p->indexOfChild(item);
289 QTreeWidgetItem *clone = p->takeChild(p->indexOfChild(item));
290 } else if (item) delete item;
293 void ProjectList::slotAddFolder() {
295 // QString folderName = KInputDialog::getText(i18n("New Folder"), i18n("Enter new folder name: "));
296 // if (folderName.isEmpty()) return;
297 m_doc->slotAddFolder(i18n("Folder")); //folderName);
300 void ProjectList::slotAddFolder(const QString foldername, int clipId, bool remove, bool edit) {
303 QTreeWidgetItemIterator it(listView);
305 item = static_cast <ProjectItem *>(*it);
306 if (item->clipType() == FOLDER && item->clipId() == clipId) {
314 disconnect(listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotUpdateItemDescription(QTreeWidgetItem *, int)));
316 QTreeWidgetItemIterator it(listView);
318 item = static_cast <ProjectItem *>(*it);
319 if (item->clipType() == FOLDER && item->clipId() == clipId) {
320 item->setText(1, foldername);
325 connect(listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotUpdateItemDescription(QTreeWidgetItem *, int)));
328 text << QString() << foldername;
329 (void) new ProjectItem(listView, text, clipId);
334 void ProjectList::slotAddClip(DocClipBase *clip) {
335 const int parent = clip->toXML().attribute("groupid").toInt();
336 ProjectItem *item = NULL;
338 ProjectItem *parentitem = getItemById(parent);
339 if (parentitem) item = new ProjectItem(parentitem, clip);
341 if (item == NULL) item = new ProjectItem(listView, clip);
343 KUrl url = clip->fileURL();
344 if (!url.isEmpty()) {
345 // if file has Nepomuk comment, use it
346 Nepomuk::Resource f(url.path());
349 annotation = f.description();
351 Nepomuk::Tag tag("test");
353 } else kDebug() << "--- CANNOT CONTACT NEPOMUK";
354 if (!annotation.isEmpty()) item->setText(2, annotation);
356 emit getFileProperties(clip->toXML(), clip->getId());
359 void ProjectList::slotUpdateClip(int id) {
360 ProjectItem *item = getItemById(id);
361 item->setData(1, UsageRole, QString::number(item->numReferences()));
364 void ProjectList::slotAddClip(QUrl givenUrl, QString group) {
365 if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
367 if (givenUrl.isEmpty())
368 list = KFileDialog::getOpenUrls(KUrl(), "application/vnd.kde.kdenlive application/vnd.westley.scenelist application/flv application/vnd.rn-realmedia video/x-dv video/x-msvideo video/mpeg video/x-ms-wmv audio/mpeg audio/x-mp3 audio/x-wav application/ogg *.m2t *.dv video/mp4 video/quicktime image/gif image/jpeg image/png image/x-bmp image/svg+xml image/tiff image/x-xcf-gimp image/x-vnd.adobe.photoshop image/x-pcx image/x-exr");
369 else list.append(givenUrl);
370 if (list.isEmpty()) return;
371 KUrl::List::Iterator it;
373 if (group.isEmpty()) {
374 ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
375 if (item && item->clipType() != FOLDER) {
376 while (item->parent()) {
377 item = static_cast <ProjectItem*>(item->parent());
378 if (item->clipType() == FOLDER) break;
381 if (item && item->clipType() == FOLDER) {
382 group = item->groupName();
383 groupId = item->clipId();
386 for (it = list.begin(); it != list.end(); it++) {
387 m_doc->slotAddClipFile(*it, group, groupId);
391 void ProjectList::slotAddColorClip() {
392 if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
393 QDialog *dia = new QDialog(this);
394 Ui::ColorClip_UI *dia_ui = new Ui::ColorClip_UI();
395 dia_ui->setupUi(dia);
396 dia_ui->clip_name->setText(i18n("Color Clip"));
397 dia_ui->clip_duration->setText(KdenliveSettings::color_duration());
398 if (dia->exec() == QDialog::Accepted) {
399 QString color = dia_ui->clip_color->color().name();
400 color = color.replace(0, 1, "0x") + "ff";
402 QString group = QString();
404 ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
405 if (item && item->clipType() != FOLDER) {
406 while (item->parent()) {
407 item = static_cast <ProjectItem*>(item->parent());
408 if (item->clipType() == FOLDER) break;
411 if (item && item->clipType() == FOLDER) {
412 group = item->groupName();
413 groupId = item->clipId();
416 m_doc->slotAddColorClipFile(dia_ui->clip_name->text(), color, dia_ui->clip_duration->text(), group, groupId);
423 void ProjectList::slotAddSlideshowClip() {
424 if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
425 SlideshowClip *dia = new SlideshowClip(this);
427 if (dia->exec() == QDialog::Accepted) {
429 QString group = QString();
431 ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
432 if (item && item->clipType() != FOLDER) {
433 while (item->parent()) {
434 item = static_cast <ProjectItem*>(item->parent());
435 if (item->clipType() == FOLDER) break;
438 if (item && item->clipType() == FOLDER) {
439 group = item->groupName();
440 groupId = item->clipId();
443 m_doc->slotAddSlideshowClipFile(dia->clipName(), dia->selectedPath(), dia->imageCount(), dia->clipDuration(), dia->loop(), group, groupId);
447 void ProjectList::slotAddTitleClip() {
448 QString group = QString();
450 ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
451 if (item && item->clipType() != FOLDER) {
452 while (item->parent()) {
453 item = static_cast <ProjectItem*>(item->parent());
454 if (item->clipType() == FOLDER) break;
457 if (item && item->clipType() == FOLDER) {
458 group = item->groupName();
459 groupId = item->clipId();
462 m_doc->slotCreateTextClip(group, groupId);
465 void ProjectList::setDocument(KdenliveDoc *doc) {
467 QList <DocClipBase*> list = doc->clipManager()->documentClipList();
468 for (int i = 0; i < list.count(); i++) {
469 slotAddClip(list.at(i));
473 m_timecode = doc->timecode();
474 m_commandStack = doc->commandStack();
476 /* QDomNodeList prods = doc->producersList();
477 int ct = prods.count();
478 kDebug() << "//////////// SETTING DOC, FOUND CLIPS: " << prods.count();
480 for (int i = 0; i < ct ; i++) {
481 QDomElement e = prods.item(i).toElement();
482 kDebug() << "// IMPORT: " << i << ", :" << e.attribute("id", "non") << ", NAME: " << e.attribute("name", "non");
483 if (!e.isNull()) addProducer(e);
485 QTreeWidgetItem *first = listView->topLevelItem(0);
486 if (first) listView->setCurrentItem(first);
487 m_toolbar->setEnabled(true);
490 QDomElement ProjectList::producersList() {
492 QDomElement prods = doc.createElement("producerlist");
493 doc.appendChild(prods);
494 kDebug() << "//////////// PRO LIST BUILD PRDSLIST ";
495 QTreeWidgetItemIterator it(listView);
497 if (!((ProjectItem *)(*it))->isGroup())
498 prods.appendChild(doc.importNode(((ProjectItem *)(*it))->toXml(), true));
504 void ProjectList::slotRefreshClipThumbnail(int clipId) {
505 ProjectItem *item = getItemById(clipId);
506 if (item) slotRefreshClipThumbnail(item);
509 void ProjectList::slotRefreshClipThumbnail(ProjectItem *item) {
512 int width = (int)(height * (double) m_render->renderWidth() / m_render->renderHeight());
513 QPixmap pix = KThumb::getImage(item->toXml(), item->referencedClip()->getProjectThumbFrame(), width, height);
514 //QPixmap pix = KThumb::getFrame(item->toXml()), 0, width, height);
515 item->setIcon(0, pix);
519 void ProjectList::slotReplyGetFileProperties(int clipId, const QMap < QString, QString > &properties, const QMap < QString, QString > &metadata) {
520 ProjectItem *item = getItemById(clipId);
522 item->setProperties(properties, metadata);
523 listView->setCurrentItem(item);
524 emit receivedClipDuration(clipId, item->clipMaxDuration());
528 void ProjectList::slotReplyGetImage(int clipId, int pos, const QPixmap &pix, int w, int h) {
529 ProjectItem *item = getItemById(clipId);
530 if (item) item->setIcon(0, pix);
533 ProjectItem *ProjectList::getItemById(int id) {
534 QTreeWidgetItemIterator it(listView);
536 if (((ProjectItem *)(*it))->clipId() == id)
540 if (*it) return ((ProjectItem *)(*it));
545 void ProjectList::addProducer(QDomElement producer, int parentId) {
546 if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
547 CLIPTYPE type = (CLIPTYPE) producer.attribute("type").toInt();
550 QDomElement prods = doc.createElement("list");
551 doc.appendChild(prods);
552 prods.appendChild(doc.importNode(producer, true));*/
555 //kDebug()<<"////// ADDING PRODUCER:\n "<<doc.toString()<<"\n+++++++++++++++++";
556 int id = producer.attribute("id").toInt();
557 QString groupName = producer.attribute("groupname");
558 if (id >= m_clipIdCounter) m_clipIdCounter = id + 1;
559 else if (id == 0) id = m_clipIdCounter++;
561 if (parentId != -1) {
562 // item is a westley playlist, adjust subproducers ids
563 id = (parentId + 1) * 10000 + id;
565 if (type == AUDIO || type == VIDEO || type == AV || type == IMAGE || type == PLAYLIST) {
566 KUrl resource = KUrl(producer.attribute("resource"));
567 if (!resource.isEmpty()) {
568 QStringList itemEntry;
569 itemEntry.append(QString::null);
570 itemEntry.append(resource.fileName());
571 addClip(itemEntry, producer, id, resource, groupName, parentId);
573 } else if (type == COLOR) {
574 QString colour = producer.attribute("colour");
576 colour = colour.replace(0, 2, "#");
577 pix.fill(QColor(colour.left(7)));
578 QStringList itemEntry;
579 itemEntry.append(QString::null);
580 itemEntry.append(producer.attribute("name", i18n("Color clip")));
581 addClip(itemEntry, producer, id, KUrl(), groupName, parentId);
586 #include "projectlist.moc"