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() {
129 void ProjectList::slotEditClip()
131 ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
132 if (item && !item->isGroup()) emit clipSelected(item->toXml());
133 emit showClipProperties(item->referencedClip());
138 void ProjectList::setRenderer(Render *projectRender) {
139 m_render = projectRender;
142 void ProjectList::slotClipSelected() {
143 ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
144 if (item && !item->isGroup()) emit clipSelected(item->toXml());
147 void ProjectList::slotUpdateClipProperties(int id, QMap <QString, QString> properties) {
148 ProjectItem *item = getItemById(id);
150 slotUpdateClipProperties(item, properties);
151 if (properties.contains("colour") || properties.contains("resource")) slotRefreshClipThumbnail(item);
152 if (properties.contains("out")) item->changeDuration(properties.value("out").toInt());
156 void ProjectList::slotUpdateClipProperties(ProjectItem *clip, QMap <QString, QString> properties) {
158 clip->setProperties(properties);
159 if (properties.contains("description")) {
160 CLIPTYPE type = clip->clipType();
161 clip->setText(2, properties.value("description"));
162 if (type == AUDIO || type == VIDEO || type == AV || type == IMAGE || type == PLAYLIST) {
163 // Use Nepomuk system to store clip description
164 Nepomuk::Resource f(clip->clipUrl().path());
165 if (f.isValid()) f.setDescription(properties.value("description"));
170 void ProjectList::slotItemEdited(QTreeWidgetItem *item, int column) {
171 ProjectItem *clip = static_cast <ProjectItem*>(item);
173 QMap <QString, QString> props;
174 props["description"] = item->text(2);
175 slotUpdateClipProperties(clip, props);
176 } else if (column == 1 && clip->clipType() == FOLDER) {
177 m_doc->slotEditFolder(item->text(1), clip->groupName(), clip->clipId());
181 void ProjectList::slotContextMenu(const QPoint &pos, QTreeWidgetItem *item) {
186 m_editAction->setEnabled(enable);
187 m_deleteAction->setEnabled(enable);
192 void ProjectList::slotRemoveClip() {
193 if (!listView->currentItem()) return;
194 ProjectItem *item = static_cast <ProjectItem *>(listView->currentItem());
196 QMap <QString, int> folderids;
197 if (item->clipType() == FOLDER) folderids[item->groupName()] = item->clipId();
198 else ids << item->clipId();
199 if (item->numReferences() > 0) {
200 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;
201 } else if (item->clipType() == FOLDER && item->childCount() > 0) {
202 int children = item->childCount();
203 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;
204 for (int i = 0; i < children; ++i) {
205 ProjectItem *child = static_cast <ProjectItem *>(item->child(i));
206 ids << child->clipId();
209 if (!ids.isEmpty()) m_doc->deleteProjectClip(ids);
210 if (!folderids.isEmpty()) m_doc->deleteProjectFolder(folderids);
213 void ProjectList::selectItemById(const int clipId) {
214 ProjectItem *item = getItemById(clipId);
215 if (item) listView->setCurrentItem(item);
218 void ProjectList::addClip(const QStringList &name, const QDomElement &elem, const int clipId, const KUrl &url, const QString &group, int parentId) {
219 kDebug() << "///////// ADDING VCLIP=: " << name;
221 ProjectItem *groupItem = NULL;
223 if (group.isEmpty()) groupName = elem.attribute("groupname", QString::null);
224 else groupName = group;
225 if (elem.isNull() && url.isEmpty()) {
227 groupName = name.at(1);
228 QList<QTreeWidgetItem *> groupList = listView->findItems(groupName, Qt::MatchExactly, 1);
229 if (groupList.isEmpty()) {
230 (void) new ProjectItem(listView, name, m_doc->getFreeClipId());
235 if (parentId != -1) {
236 groupItem = getItemById(parentId);
237 } else if (!groupName.isEmpty()) {
238 // Clip is in a group
239 QList<QTreeWidgetItem *> groupList = listView->findItems(groupName, Qt::MatchExactly, 1);
241 if (groupList.isEmpty()) {
242 QStringList itemName;
243 itemName << QString::null << groupName;
244 kDebug() << "------- CREATING NEW GRP: " << itemName;
245 groupItem = new ProjectItem(listView, itemName, m_doc->getFreeClipId());
246 } else groupItem = (ProjectItem *) groupList.first();
248 if (groupItem) item = new ProjectItem(groupItem, name, elem, clipId);
249 else item = new ProjectItem(listView, name, elem, clipId);
250 if (!url.isEmpty()) {
251 // if file has Nepomuk comment, use it
252 Nepomuk::Resource f(url.path());
254 if (f.isValid()) annotation = f.description();
256 if (!annotation.isEmpty()) item->setText(2, annotation);
257 QString resource = url.path();
258 if (resource.endsWith("westley") || resource.endsWith("kdenlive")) {
261 if (KIO::NetAccess::download(url, tmpfile, 0)) {
263 if (file.open(QIODevice::ReadOnly)) {
264 doc.setContent(&file, false);
267 KIO::NetAccess::removeTempFile(tmpfile);
269 QDomNodeList subProds = doc.elementsByTagName("producer");
270 int ct = subProds.count();
271 for (int i = 0; i < ct ; i++) {
272 QDomElement e = subProds.item(i).toElement();
274 addProducer(e, clipId);
284 QDomElement element = doc.createElement("producer");
285 element.setAttribute("resource", url.path());
286 emit getFileProperties(element, clipId);
287 } else emit getFileProperties(elem, clipId);
288 selectItemById(clipId);
291 void ProjectList::slotDeleteClip(int clipId) {
292 ProjectItem *item = getItemById(clipId);
293 QTreeWidgetItem *p = item->parent();
295 kDebug() << "/////// DELETEED CLIP HAS A PARENT... " << p->indexOfChild(item);
296 QTreeWidgetItem *clone = p->takeChild(p->indexOfChild(item));
297 } else if (item) delete item;
300 void ProjectList::slotAddFolder() {
302 // QString folderName = KInputDialog::getText(i18n("New Folder"), i18n("Enter new folder name: "));
303 // if (folderName.isEmpty()) return;
304 m_doc->slotAddFolder(i18n("Folder")); //folderName);
307 void ProjectList::slotAddFolder(const QString foldername, int clipId, bool remove, bool edit) {
310 QTreeWidgetItemIterator it(listView);
312 item = static_cast <ProjectItem *>(*it);
313 if (item->clipType() == FOLDER && item->clipId() == clipId) {
321 disconnect(listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotUpdateItemDescription(QTreeWidgetItem *, int)));
323 QTreeWidgetItemIterator it(listView);
325 item = static_cast <ProjectItem *>(*it);
326 if (item->clipType() == FOLDER && item->clipId() == clipId) {
327 item->setText(1, foldername);
332 connect(listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotUpdateItemDescription(QTreeWidgetItem *, int)));
335 text << QString() << foldername;
336 (void) new ProjectItem(listView, text, clipId);
341 void ProjectList::slotAddClip(DocClipBase *clip) {
342 const int parent = clip->toXML().attribute("groupid").toInt();
343 ProjectItem *item = NULL;
345 ProjectItem *parentitem = getItemById(parent);
346 if (parentitem) item = new ProjectItem(parentitem, clip);
348 if (item == NULL) item = new ProjectItem(listView, clip);
350 KUrl url = clip->fileURL();
351 if (!url.isEmpty()) {
352 // if file has Nepomuk comment, use it
353 Nepomuk::Resource f(url.path());
356 annotation = f.description();
358 Nepomuk::Tag tag("test");
360 } else kDebug() << "--- CANNOT CONTACT NEPOMUK";
361 if (!annotation.isEmpty()) item->setText(2, annotation);
363 emit getFileProperties(clip->toXML(), clip->getId());
366 void ProjectList::slotUpdateClip(int id) {
367 ProjectItem *item = getItemById(id);
368 item->setData(1, UsageRole, QString::number(item->numReferences()));
371 void ProjectList::slotAddClip(QUrl givenUrl, QString group) {
372 if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
374 if (givenUrl.isEmpty())
375 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");
376 else list.append(givenUrl);
377 if (list.isEmpty()) return;
378 KUrl::List::Iterator it;
380 if (group.isEmpty()) {
381 ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
382 if (item && item->clipType() != FOLDER) {
383 while (item->parent()) {
384 item = static_cast <ProjectItem*>(item->parent());
385 if (item->clipType() == FOLDER) break;
388 if (item && item->clipType() == FOLDER) {
389 group = item->groupName();
390 groupId = item->clipId();
393 for (it = list.begin(); it != list.end(); it++) {
394 m_doc->slotAddClipFile(*it, group, groupId);
398 void ProjectList::slotAddColorClip() {
399 if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
400 QDialog *dia = new QDialog(this);
401 Ui::ColorClip_UI *dia_ui = new Ui::ColorClip_UI();
402 dia_ui->setupUi(dia);
403 dia_ui->clip_name->setText(i18n("Color Clip"));
404 dia_ui->clip_duration->setText(KdenliveSettings::color_duration());
405 if (dia->exec() == QDialog::Accepted) {
406 QString color = dia_ui->clip_color->color().name();
407 color = color.replace(0, 1, "0x") + "ff";
409 QString group = QString();
411 ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
412 if (item && item->clipType() != FOLDER) {
413 while (item->parent()) {
414 item = static_cast <ProjectItem*>(item->parent());
415 if (item->clipType() == FOLDER) break;
418 if (item && item->clipType() == FOLDER) {
419 group = item->groupName();
420 groupId = item->clipId();
423 m_doc->slotAddColorClipFile(dia_ui->clip_name->text(), color, dia_ui->clip_duration->text(), group, groupId);
430 void ProjectList::slotAddSlideshowClip() {
431 if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
432 SlideshowClip *dia = new SlideshowClip(this);
434 if (dia->exec() == QDialog::Accepted) {
436 QString group = QString();
438 ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
439 if (item && item->clipType() != FOLDER) {
440 while (item->parent()) {
441 item = static_cast <ProjectItem*>(item->parent());
442 if (item->clipType() == FOLDER) break;
445 if (item && item->clipType() == FOLDER) {
446 group = item->groupName();
447 groupId = item->clipId();
450 m_doc->slotAddSlideshowClipFile(dia->clipName(), dia->selectedPath(), dia->imageCount(), dia->clipDuration(), dia->loop(), group, groupId);
454 void ProjectList::slotAddTitleClip() {
455 QString group = QString();
457 ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
458 if (item && item->clipType() != FOLDER) {
459 while (item->parent()) {
460 item = static_cast <ProjectItem*>(item->parent());
461 if (item->clipType() == FOLDER) break;
464 if (item && item->clipType() == FOLDER) {
465 group = item->groupName();
466 groupId = item->clipId();
469 m_doc->slotCreateTextClip(group, groupId);
472 void ProjectList::setDocument(KdenliveDoc *doc) {
474 QList <DocClipBase*> list = doc->clipManager()->documentClipList();
475 for (int i = 0; i < list.count(); i++) {
476 slotAddClip(list.at(i));
480 m_timecode = doc->timecode();
481 m_commandStack = doc->commandStack();
483 /* QDomNodeList prods = doc->producersList();
484 int ct = prods.count();
485 kDebug() << "//////////// SETTING DOC, FOUND CLIPS: " << prods.count();
487 for (int i = 0; i < ct ; i++) {
488 QDomElement e = prods.item(i).toElement();
489 kDebug() << "// IMPORT: " << i << ", :" << e.attribute("id", "non") << ", NAME: " << e.attribute("name", "non");
490 if (!e.isNull()) addProducer(e);
492 QTreeWidgetItem *first = listView->topLevelItem(0);
493 if (first) listView->setCurrentItem(first);
494 m_toolbar->setEnabled(true);
497 QDomElement ProjectList::producersList() {
499 QDomElement prods = doc.createElement("producerlist");
500 doc.appendChild(prods);
501 kDebug() << "//////////// PRO LIST BUILD PRDSLIST ";
502 QTreeWidgetItemIterator it(listView);
504 if (!((ProjectItem *)(*it))->isGroup())
505 prods.appendChild(doc.importNode(((ProjectItem *)(*it))->toXml(), true));
511 void ProjectList::slotRefreshClipThumbnail(int clipId) {
512 ProjectItem *item = getItemById(clipId);
513 if (item) slotRefreshClipThumbnail(item);
516 void ProjectList::slotRefreshClipThumbnail(ProjectItem *item) {
519 int width = (int)(height * (double) m_render->renderWidth() / m_render->renderHeight());
520 QPixmap pix = KThumb::getImage(item->toXml(), item->referencedClip()->getProjectThumbFrame(), width, height);
521 //QPixmap pix = KThumb::getFrame(item->toXml()), 0, width, height);
522 item->setIcon(0, pix);
526 void ProjectList::slotReplyGetFileProperties(int clipId, const QMap < QString, QString > &properties, const QMap < QString, QString > &metadata) {
527 ProjectItem *item = getItemById(clipId);
529 item->setProperties(properties, metadata);
530 listView->setCurrentItem(item);
531 emit receivedClipDuration(clipId, item->clipMaxDuration());
535 void ProjectList::slotReplyGetImage(int clipId, int pos, const QPixmap &pix, int w, int h) {
536 ProjectItem *item = getItemById(clipId);
537 if (item) item->setIcon(0, pix);
540 ProjectItem *ProjectList::getItemById(int id) {
541 QTreeWidgetItemIterator it(listView);
543 if (((ProjectItem *)(*it))->clipId() == id)
547 if (*it) return ((ProjectItem *)(*it));
552 void ProjectList::addProducer(QDomElement producer, int parentId) {
553 if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
554 CLIPTYPE type = (CLIPTYPE) producer.attribute("type").toInt();
557 QDomElement prods = doc.createElement("list");
558 doc.appendChild(prods);
559 prods.appendChild(doc.importNode(producer, true));*/
562 //kDebug()<<"////// ADDING PRODUCER:\n "<<doc.toString()<<"\n+++++++++++++++++";
563 int id = producer.attribute("id").toInt();
564 QString groupName = producer.attribute("groupname");
565 if (id >= m_clipIdCounter) m_clipIdCounter = id + 1;
566 else if (id == 0) id = m_clipIdCounter++;
568 if (parentId != -1) {
569 // item is a westley playlist, adjust subproducers ids
570 id = (parentId + 1) * 10000 + id;
572 if (type == AUDIO || type == VIDEO || type == AV || type == IMAGE || type == PLAYLIST) {
573 KUrl resource = KUrl(producer.attribute("resource"));
574 if (!resource.isEmpty()) {
575 QStringList itemEntry;
576 itemEntry.append(QString::null);
577 itemEntry.append(resource.fileName());
578 addClip(itemEntry, producer, id, resource, groupName, parentId);
580 } else if (type == COLOR) {
581 QString colour = producer.attribute("colour");
583 colour = colour.replace(0, 2, "#");
584 pix.fill(QColor(colour.left(7)));
585 QStringList itemEntry;
586 itemEntry.append(QString::null);
587 itemEntry.append(producer.attribute("name", i18n("Color clip")));
588 addClip(itemEntry, producer, id, KUrl(), groupName, parentId);
593 #include "projectlist.moc"