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