]> git.sesse.net Git - kdenlive/blob - src/projectlist.cpp
cee7dbd01f773532314cb3a421936e536929c623
[kdenlive] / src / projectlist.cpp
1 /***************************************************************************
2  *   Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
3  *                                                                         *
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.                                   *
8  *                                                                         *
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.                          *
13  *                                                                         *
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  ***************************************************************************/
19
20
21 #include <QMouseEvent>
22 #include <QStylePainter>
23 #include <QPixmap>
24 #include <QIcon>
25 #include <QDialog>
26
27 #include <KDebug>
28 #include <KAction>
29 #include <KLocale>
30 #include <KFileDialog>
31 #include <KInputDialog>
32 #include <kio/netaccess.h>
33 #include <KMessageBox>
34
35 #include <nepomuk/global.h>
36 #include <nepomuk/resource.h>
37 #include <nepomuk/tag.h>
38
39 #include "projectlist.h"
40 #include "projectitem.h"
41 #include "kdenlivesettings.h"
42 #include "ui_colorclip_ui.h"
43
44 #include "definitions.h"
45 #include "clipmanager.h"
46 #include "docclipbase.h"
47 #include "kdenlivedoc.h"
48 #include "renderer.h"
49 #include "projectlistview.h"
50 #include <QtGui>
51
52 ProjectList::ProjectList(QWidget *parent)
53         : QWidget(parent), m_render(NULL), m_fps(-1), m_commandStack(NULL) {
54
55     QWidget *vbox = new QWidget;
56     listView = new ProjectListView(this);;
57     QVBoxLayout *layout = new QVBoxLayout;
58     m_clipIdCounter = 0;
59
60     // setup toolbar
61     searchView = new KTreeWidgetSearchLine(this);
62     m_toolbar = new QToolBar("projectToolBar", this);
63     m_toolbar->addWidget(searchView);
64
65     QToolButton *addButton = new QToolButton(m_toolbar);
66     QMenu *addMenu = new QMenu(this);
67     addButton->setMenu(addMenu);
68     addButton->setPopupMode(QToolButton::MenuButtonPopup);
69     m_toolbar->addWidget(addButton);
70
71     QAction *addClipButton = addMenu->addAction(KIcon("document-new"), i18n("Add Clip"));
72     connect(addClipButton, SIGNAL(triggered()), this, SLOT(slotAddClip()));
73
74     QAction *addColorClip = addMenu->addAction(KIcon("document-new"), i18n("Add Color Clip"));
75     connect(addColorClip, SIGNAL(triggered()), this, SLOT(slotAddColorClip()));
76
77     QAction *addTitleClip = addMenu->addAction(KIcon("document-new"), i18n("Add Title Clip"));
78     connect(addTitleClip, SIGNAL(triggered()), this, SLOT(slotAddTitleClip()));
79
80     m_deleteAction = m_toolbar->addAction(KIcon("edit-delete"), i18n("Delete Clip"));
81     connect(m_deleteAction, SIGNAL(triggered()), this, SLOT(slotRemoveClip()));
82
83     m_editAction = m_toolbar->addAction(KIcon("document-properties"), i18n("Edit Clip"));
84     connect(m_editAction, SIGNAL(triggered()), this, SLOT(slotEditClip()));
85
86     QAction *addFolderButton = addMenu->addAction(KIcon("folder-new"), i18n("Create Folder"));
87     connect(addFolderButton, SIGNAL(triggered()), this, SLOT(slotAddFolder()));
88
89     addButton->setDefaultAction(addClipButton);
90
91     layout->addWidget(m_toolbar);
92     layout->addWidget(listView);
93     setLayout(layout);
94     //m_toolbar->setEnabled(false);
95
96     searchView->setTreeWidget(listView);
97
98     m_menu = new QMenu();
99     m_menu->addAction(addClipButton);
100     m_menu->addAction(addColorClip);
101     m_menu->addAction(addTitleClip);
102     m_menu->addAction(m_editAction);
103     m_menu->addAction(m_deleteAction);
104     m_menu->addAction(addFolderButton);
105     m_menu->insertSeparator(m_deleteAction);
106
107     connect(listView, SIGNAL(itemSelectionChanged()), this, SLOT(slotClipSelected()));
108     connect(listView, SIGNAL(requestMenu(const QPoint &, QTreeWidgetItem *)), this, SLOT(slotContextMenu(const QPoint &, QTreeWidgetItem *)));
109     connect(listView, SIGNAL(addClip()), this, SLOT(slotAddClip()));
110     connect(listView, SIGNAL(addClip(QUrl, const QString &)), this, SLOT(slotAddClip(QUrl, const QString &)));
111     connect(listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotItemEdited(QTreeWidgetItem *, int)));
112     connect(listView, SIGNAL(showProperties(DocClipBase *)), this, SIGNAL(showClipProperties(DocClipBase *)));
113
114     m_listViewDelegate = new ItemDelegate(listView);
115     listView->setItemDelegate(m_listViewDelegate);
116 }
117
118 ProjectList::~ProjectList() {
119     delete m_menu;
120     delete m_toolbar;
121 }
122
123
124
125 void ProjectList::setRenderer(Render *projectRender) {
126     m_render = projectRender;
127 }
128
129 void ProjectList::slotClipSelected() {
130     ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
131     if (item && !item->isGroup()) emit clipSelected(item->toXml());
132 }
133
134 void ProjectList::slotUpdateClipProperties(int id, QMap <QString, QString> properties) {
135     ProjectItem *item = getItemById(id);
136     if (item) slotUpdateClipProperties(item, properties);
137 }
138
139 void ProjectList::slotUpdateClipProperties(ProjectItem *clip, QMap <QString, QString> properties) {
140     if (!clip) return;
141     clip->setProperties(properties);
142     if (properties.contains("description")) {
143         CLIPTYPE type = clip->clipType();
144         clip->setText(2, properties.value("description"));
145         if (type == AUDIO || type == VIDEO || type == AV || type == IMAGE || type == PLAYLIST) {
146             // Use Nepomuk system to store clip description
147             Nepomuk::Resource f(clip->clipUrl().path());
148             if (f.isValid()) f.setDescription(properties.value("description"));
149         }
150     }
151 }
152
153 void ProjectList::slotItemEdited(QTreeWidgetItem *item, int column) {
154     ProjectItem *clip = static_cast <ProjectItem*>(item);
155     if (column == 2) {
156         QMap <QString, QString> props;
157         props["description"] = item->text(2);
158         slotUpdateClipProperties(clip, props);
159     } else if (column == 1 && clip->clipType() == FOLDER) {
160         m_doc->slotEditFolder(item->text(1), clip->groupName(), clip->clipId());
161     }
162 }
163
164 void ProjectList::slotContextMenu(const QPoint &pos, QTreeWidgetItem *item) {
165     bool enable = false;
166     if (item) {
167         enable = true;
168     }
169     m_editAction->setEnabled(enable);
170     m_deleteAction->setEnabled(enable);
171
172     m_menu->popup(pos);
173 }
174
175 void ProjectList::slotRemoveClip() {
176     if (!listView->currentItem()) return;
177     ProjectItem *item = static_cast <ProjectItem *>(listView->currentItem());
178     QList <int> ids;
179     QMap <QString, int> folderids;
180     if (item->clipType() == FOLDER) folderids[item->groupName()] = item->clipId();
181     else ids << item->clipId();
182     if (item->numReferences() > 0) {
183         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;
184     } else if (item->clipType() == FOLDER && item->childCount() > 0) {
185         int children = item->childCount();
186         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;
187         for (int i = 0; i < children; ++i) {
188             ProjectItem *child = static_cast <ProjectItem *>(item->child(i));
189             ids << child->clipId();
190         }
191     }
192     if (!ids.isEmpty()) m_doc->deleteProjectClip(ids);
193     if (!folderids.isEmpty()) m_doc->deleteProjectFolder(folderids);
194 }
195
196 void ProjectList::selectItemById(const int clipId) {
197     ProjectItem *item = getItemById(clipId);
198     if (item) listView->setCurrentItem(item);
199 }
200
201 void ProjectList::addClip(const QStringList &name, const QDomElement &elem, const int clipId, const KUrl &url, const QString &group, int parentId) {
202     kDebug() << "/////////  ADDING VCLIP=: " << name;
203     ProjectItem *item;
204     ProjectItem *groupItem = NULL;
205     QString groupName;
206     if (group.isEmpty()) groupName = elem.attribute("groupname", QString::null);
207     else groupName = group;
208     if (elem.isNull() && url.isEmpty()) {
209         // this is a folder
210         groupName = name.at(1);
211         QList<QTreeWidgetItem *> groupList = listView->findItems(groupName, Qt::MatchExactly, 1);
212         if (groupList.isEmpty())  {
213             (void) new ProjectItem(listView, name, m_doc->getFreeClipId());
214         }
215         return;
216     }
217
218     if (parentId != -1) {
219         groupItem = getItemById(parentId);
220     } else if (!groupName.isEmpty()) {
221         // Clip is in a group
222         QList<QTreeWidgetItem *> groupList = listView->findItems(groupName, Qt::MatchExactly, 1);
223
224         if (groupList.isEmpty())  {
225             QStringList itemName;
226             itemName << QString::null << groupName;
227             kDebug() << "-------  CREATING NEW GRP: " << itemName;
228             groupItem = new ProjectItem(listView, itemName, m_doc->getFreeClipId());
229         } else groupItem = (ProjectItem *) groupList.first();
230     }
231     if (groupItem) item = new ProjectItem(groupItem, name, elem, clipId);
232     else item = new ProjectItem(listView, name, elem, clipId);
233     if (!url.isEmpty()) {
234         // if file has Nepomuk comment, use it
235         Nepomuk::Resource f(url.path());
236         QString annotation;
237         if (f.isValid()) annotation = f.description();
238
239         if (!annotation.isEmpty()) item->setText(2, annotation);
240         QString resource = url.path();
241         if (resource.endsWith("westley") || resource.endsWith("kdenlive")) {
242             QString tmpfile;
243             QDomDocument doc;
244             if (KIO::NetAccess::download(url, tmpfile, 0)) {
245                 QFile file(tmpfile);
246                 if (file.open(QIODevice::ReadOnly)) {
247                     doc.setContent(&file, false);
248                     file.close();
249                 }
250                 KIO::NetAccess::removeTempFile(tmpfile);
251
252                 QDomNodeList subProds = doc.elementsByTagName("producer");
253                 int ct = subProds.count();
254                 for (int i = 0; i <  ct ; i++) {
255                     QDomElement e = subProds.item(i).toElement();
256                     if (!e.isNull()) {
257                         addProducer(e, clipId);
258                     }
259                 }
260             }
261         }
262
263     }
264
265     if (elem.isNull()) {
266         QDomDocument doc;
267         QDomElement element = doc.createElement("producer");
268         element.setAttribute("resource", url.path());
269         emit getFileProperties(element, clipId);
270     } else emit getFileProperties(elem, clipId);
271     selectItemById(clipId);
272 }
273
274 void ProjectList::slotDeleteClip(int clipId) {
275     ProjectItem *item = getItemById(clipId);
276     QTreeWidgetItem *p = item->parent();
277     if (p) {
278         kDebug() << "///////  DELETEED CLIP HAS A PARENT... " << p->indexOfChild(item);
279         QTreeWidgetItem *clone = p->takeChild(p->indexOfChild(item));
280     } else if (item) delete item;
281 }
282
283 void ProjectList::slotAddFolder() {
284
285     // QString folderName = KInputDialog::getText(i18n("New Folder"), i18n("Enter new folder name: "));
286     // if (folderName.isEmpty()) return;
287     m_doc->slotAddFolder(i18n("Folder")); //folderName);
288 }
289
290 void ProjectList::slotAddFolder(const QString foldername, int clipId, bool remove, bool edit) {
291     if (remove) {
292         ProjectItem *item;
293         QTreeWidgetItemIterator it(listView);
294         while (*it) {
295             item = static_cast <ProjectItem *>(*it);
296             if (item->clipType() == FOLDER && item->clipId() == clipId) {
297                 delete item;
298                 break;
299             }
300             ++it;
301         }
302     } else {
303         if (edit) {
304             disconnect(listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotUpdateItemDescription(QTreeWidgetItem *, int)));
305             ProjectItem *item;
306             QTreeWidgetItemIterator it(listView);
307             while (*it) {
308                 item = static_cast <ProjectItem *>(*it);
309                 if (item->clipType() == FOLDER && item->clipId() == clipId) {
310                     item->setText(1, foldername);
311                     break;
312                 }
313                 ++it;
314             }
315             connect(listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotUpdateItemDescription(QTreeWidgetItem *, int)));
316         } else {
317             QStringList text;
318             text << QString() << foldername;
319             (void) new ProjectItem(listView, text, clipId);
320         }
321     }
322 }
323
324 void ProjectList::slotAddClip(DocClipBase *clip) {
325     const int parent = clip->toXML().attribute("groupid").toInt();
326     ProjectItem *item = NULL;
327     if (parent != 0) {
328         ProjectItem *parentitem = getItemById(parent);
329         if (parentitem) item = new ProjectItem(parentitem, clip);
330     }
331     if (item == NULL) item = new ProjectItem(listView, clip);
332
333     KUrl url = clip->fileURL();
334     if (!url.isEmpty()) {
335         // if file has Nepomuk comment, use it
336         Nepomuk::Resource f(url.path());
337         QString annotation;
338         if (f.isValid()) {
339             annotation = f.description();
340             /*
341             Nepomuk::Tag tag("test");
342             f.addTag(tag);*/
343         } else kDebug() << "---  CANNOT CONTACT NEPOMUK";
344         if (!annotation.isEmpty()) item->setText(2, annotation);
345     }
346     emit getFileProperties(clip->toXML(), clip->getId());
347 }
348
349 void ProjectList::slotUpdateClip(int id) {
350     ProjectItem *item = getItemById(id);
351     item->setData(1, UsageRole, QString::number(item->numReferences()));
352 }
353
354 void ProjectList::slotAddClip(QUrl givenUrl, QString group) {
355     if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!!  NO CMD STK";
356     KUrl::List list;
357     if (givenUrl.isEmpty())
358         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");
359     else list.append(givenUrl);
360     if (list.isEmpty()) return;
361     KUrl::List::Iterator it;
362     int groupId = -1;
363     if (group.isEmpty()) {
364         ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
365         if (item && item->clipType() != FOLDER) {
366             while (item->parent()) {
367                 item = static_cast <ProjectItem*>(item->parent());
368                 if (item->clipType() == FOLDER) break;
369             }
370         }
371         if (item && item->clipType() == FOLDER) {
372             group = item->groupName();
373             groupId = item->clipId();
374         }
375     }
376     for (it = list.begin(); it != list.end(); it++) {
377         m_doc->slotAddClipFile(*it, group, groupId);
378     }
379 }
380
381 void ProjectList::slotAddColorClip() {
382     if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!!  NO CMD STK";
383     QDialog *dia = new QDialog(this);
384     Ui::ColorClip_UI *dia_ui = new Ui::ColorClip_UI();
385     dia_ui->setupUi(dia);
386     dia_ui->clip_name->setText(i18n("Color Clip"));
387     dia_ui->clip_duration->setText(KdenliveSettings::color_duration());
388     if (dia->exec() == QDialog::Accepted) {
389         QString color = dia_ui->clip_color->color().name();
390         color = color.replace(0, 1, "0x") + "ff";
391
392         QString group = QString();
393         int groupId = -1;
394         ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
395         if (item && item->clipType() != FOLDER) {
396             while (item->parent()) {
397                 item = static_cast <ProjectItem*>(item->parent());
398                 if (item->clipType() == FOLDER) break;
399             }
400         }
401         if (item && item->clipType() == FOLDER) {
402             group = item->groupName();
403             groupId = item->clipId();
404         }
405
406         m_doc->slotAddColorClipFile(dia_ui->clip_name->text(), color, dia_ui->clip_duration->text(), group, groupId);
407     }
408     delete dia_ui;
409     delete dia;
410 }
411
412 void ProjectList::slotAddTitleClip() {
413     QString group = QString();
414     int groupId = -1;
415     ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
416     if (item && item->clipType() != FOLDER) {
417         while (item->parent()) {
418             item = static_cast <ProjectItem*>(item->parent());
419             if (item->clipType() == FOLDER) break;
420         }
421     }
422     if (item && item->clipType() == FOLDER) {
423         group = item->groupName();
424         groupId = item->clipId();
425     }
426
427     m_doc->slotCreateTextClip(group, groupId);
428 }
429 void ProjectList::setDocument(KdenliveDoc *doc) {
430     listView->clear();
431     QList <DocClipBase*> list = doc->clipManager()->documentClipList();
432     for (int i = 0; i < list.count(); i++) {
433         slotAddClip(list.at(i));
434     }
435
436     m_fps = doc->fps();
437     m_timecode = doc->timecode();
438     m_commandStack = doc->commandStack();
439     m_doc = doc;
440     /*    QDomNodeList prods = doc->producersList();
441         int ct = prods.count();
442         kDebug() << "////////////  SETTING DOC, FOUND CLIPS: " << prods.count();
443         listView->clear();
444         for (int i = 0; i <  ct ; i++) {
445             QDomElement e = prods.item(i).toElement();
446             kDebug() << "// IMPORT: " << i << ", :" << e.attribute("id", "non") << ", NAME: " << e.attribute("name", "non");
447             if (!e.isNull()) addProducer(e);
448         }*/
449     QTreeWidgetItem *first = listView->topLevelItem(0);
450     if (first) listView->setCurrentItem(first);
451     m_toolbar->setEnabled(true);
452 }
453
454 QDomElement ProjectList::producersList() {
455     QDomDocument doc;
456     QDomElement prods = doc.createElement("producerlist");
457     doc.appendChild(prods);
458     kDebug() << "////////////  PRO LIST BUILD PRDSLIST ";
459     QTreeWidgetItemIterator it(listView);
460     while (*it) {
461         if (!((ProjectItem *)(*it))->isGroup())
462             prods.appendChild(doc.importNode(((ProjectItem *)(*it))->toXml(), true));
463         ++it;
464     }
465     return prods;
466 }
467
468
469 void ProjectList::slotReplyGetFileProperties(int clipId, const QMap < QString, QString > &properties, const QMap < QString, QString > &metadata) {
470     ProjectItem *item = getItemById(clipId);
471     if (item) {
472         item->setProperties(properties, metadata);
473         listView->setCurrentItem(item);
474         emit receivedClipDuration(clipId, item->clipMaxDuration());
475     }
476 }
477
478
479
480 void ProjectList::slotReplyGetImage(int clipId, int pos, const QPixmap &pix, int w, int h) {
481     ProjectItem *item = getItemById(clipId);
482     if (item) item->setIcon(0, pix);
483 }
484
485 ProjectItem *ProjectList::getItemById(int id) {
486     QTreeWidgetItemIterator it(listView);
487     while (*it) {
488         if (((ProjectItem *)(*it))->clipId() == id)
489             break;
490         ++it;
491     }
492     if (*it) return ((ProjectItem *)(*it));
493     return NULL;
494 }
495
496
497 void ProjectList::addProducer(QDomElement producer, int parentId) {
498     if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!!  NO CMD STK";
499     CLIPTYPE type = (CLIPTYPE) producer.attribute("type").toInt();
500
501     /*QDomDocument doc;
502     QDomElement prods = doc.createElement("list");
503     doc.appendChild(prods);
504     prods.appendChild(doc.importNode(producer, true));*/
505
506
507     //kDebug()<<"//////  ADDING PRODUCER:\n "<<doc.toString()<<"\n+++++++++++++++++";
508     int id = producer.attribute("id").toInt();
509     QString groupName = producer.attribute("groupname");
510     if (id >= m_clipIdCounter) m_clipIdCounter = id + 1;
511     else if (id == 0) id = m_clipIdCounter++;
512
513     if (parentId != -1) {
514         // item is a westley playlist, adjust subproducers ids
515         id = (parentId + 1) * 10000 + id;
516     }
517     if (type == AUDIO || type == VIDEO || type == AV || type == IMAGE  || type == PLAYLIST) {
518         KUrl resource = KUrl(producer.attribute("resource"));
519         if (!resource.isEmpty()) {
520             QStringList itemEntry;
521             itemEntry.append(QString::null);
522             itemEntry.append(resource.fileName());
523             addClip(itemEntry, producer, id, resource, groupName, parentId);
524         }
525     } else if (type == COLOR) {
526         QString colour = producer.attribute("colour");
527         QPixmap pix(60, 40);
528         colour = colour.replace(0, 2, "#");
529         pix.fill(QColor(colour.left(7)));
530         QStringList itemEntry;
531         itemEntry.append(QString::null);
532         itemEntry.append(producer.attribute("name", i18n("Color clip")));
533         addClip(itemEntry, producer, id, KUrl(), groupName, parentId);
534     }
535
536 }
537
538 #include "projectlist.moc"