]> git.sesse.net Git - kdenlive/blob - src/projectlist.cpp
folders in project view
[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 "titlewidget.h"
46 #include "clipmanager.h"
47 #include "docclipbase.h"
48 #include "kdenlivedoc.h"
49 #include "renderer.h"
50 #include "projectlistview.h"
51 #include <QtGui>
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     listView->setColumnCount(3);
99     QStringList headers;
100     headers << i18n("Thumbnail") << i18n("Filename") << i18n("Description");
101     listView->setHeaderLabels(headers);
102     listView->sortByColumn(1, Qt::AscendingOrder);
103
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);
112
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(slotUpdateItemDescription(QTreeWidgetItem *, int)));
118
119     m_listViewDelegate = new ItemDelegate(listView);
120     listView->setItemDelegate(m_listViewDelegate);
121     listView->setIconSize(QSize(60, 40));
122     listView->setSortingEnabled(true);
123 }
124
125 ProjectList::~ProjectList() {
126     delete m_menu;
127     delete m_toolbar;
128 }
129
130 void ProjectList::setRenderer(Render *projectRender) {
131     m_render = projectRender;
132 }
133
134 void ProjectList::slotClipSelected() {
135     ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
136     if (item && !item->isGroup()) emit clipSelected(item->toXml());
137 }
138
139 void ProjectList::slotUpdateItemDescription(QTreeWidgetItem *item, int column) {
140     ProjectItem *clip = static_cast <ProjectItem*>(item);
141     CLIPTYPE type = clip->clipType();
142     if (column == 2) {
143         if (type == AUDIO || type == VIDEO || type == AV || type == IMAGE || type == PLAYLIST) {
144             // Use Nepomuk system to store clip description
145             Nepomuk::Resource f(clip->clipUrl().path());
146             f.setDescription(item->text(2));
147             kDebug() << "NEPOMUK, SETTING CLIP: " << clip->clipUrl().path() << ", TO TEXT: " << item->text(2);
148         }
149     } else if (column == 1 && type == FOLDER) {
150         m_doc->slotEditFolder(item->text(1), clip->groupName(), clip->clipId());
151     }
152 }
153
154 void ProjectList::slotEditClip() {
155     kDebug() << "////////////////////////////////////////   DBL CLK";
156 }
157
158
159 void ProjectList::slotEditClip(QTreeWidgetItem *item, int column) {
160     kDebug() << "////////////////////////////////////////   DBL CLK";
161 }
162
163 void ProjectList::slotContextMenu(const QPoint &pos, QTreeWidgetItem *item) {
164     bool enable = false;
165     if (item) {
166         QFrame *w = new QFrame;
167         w->setFrameShape(QFrame::StyledPanel);
168         w->setLineWidth(2);
169         w->setAutoFillBackground(true);
170         QHBoxLayout *layout = new QHBoxLayout;
171         layout->addWidget(new QLabel(i18n("Color:")));
172         layout->addWidget(new KColorButton());
173         layout->addWidget(new QLabel(i18n("Duration:")));
174         layout->addWidget(new KRestrictedLine());
175         w->setLayout(layout);
176         m_listViewDelegate->extendItem(w, listView->currentIndex());
177         enable = true;
178     }
179     m_editAction->setEnabled(enable);
180     m_deleteAction->setEnabled(enable);
181
182     m_menu->popup(pos);
183 }
184
185 void ProjectList::slotRemoveClip() {
186     if (!listView->currentItem()) return;
187     ProjectItem *item = static_cast <ProjectItem *>(listView->currentItem());
188     QList <int> ids;
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();
200         }
201     }
202     if (!ids.isEmpty()) m_doc->deleteProjectClip(ids);
203     if (!folderids.isEmpty()) m_doc->deleteProjectFolder(folderids);
204 }
205
206 void ProjectList::selectItemById(const int clipId) {
207     ProjectItem *item = getItemById(clipId);
208     if (item) listView->setCurrentItem(item);
209 }
210
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;
213     ProjectItem *item;
214     ProjectItem *groupItem = NULL;
215     QString groupName;
216     if (group.isEmpty()) groupName = elem.attribute("groupname", QString::null);
217     else groupName = group;
218     if (elem.isNull() && url.isEmpty()) {
219         // this is a folder
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());
224         }
225         return;
226     }
227
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);
233
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();
240     }
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());
246         QString annotation = f.description();
247         if (!annotation.isEmpty()) item->setText(2, annotation);
248         QString resource = url.path();
249         if (resource.endsWith("westley") || resource.endsWith("kdenlive")) {
250             QString tmpfile;
251             QDomDocument doc;
252             if (KIO::NetAccess::download(url, tmpfile, 0)) {
253                 QFile file(tmpfile);
254                 if (file.open(QIODevice::ReadOnly)) {
255                     doc.setContent(&file, false);
256                     file.close();
257                 }
258                 KIO::NetAccess::removeTempFile(tmpfile);
259
260                 QDomNodeList subProds = doc.elementsByTagName("producer");
261                 int ct = subProds.count();
262                 for (int i = 0; i <  ct ; i++) {
263                     QDomElement e = subProds.item(i).toElement();
264                     if (!e.isNull()) {
265                         addProducer(e, clipId);
266                     }
267                 }
268             }
269         }
270
271     }
272
273     if (elem.isNull()) {
274         QDomDocument doc;
275         QDomElement element = doc.createElement("producer");
276         element.setAttribute("resource", url.path());
277         emit getFileProperties(element, clipId);
278     } else emit getFileProperties(elem, clipId);
279     selectItemById(clipId);
280 }
281
282 void ProjectList::slotDeleteClip(int clipId) {
283     kDebug() << "///////  DELETE CLIP: " << clipId;
284     ProjectItem *item = getItemById(clipId);
285     if (item) delete item;
286 }
287
288 void ProjectList::slotAddFolder() {
289
290     // QString folderName = KInputDialog::getText(i18n("New Folder"), i18n("Enter new folder name: "));
291     // if (folderName.isEmpty()) return;
292     m_doc->slotAddFolder(i18n("Folder")); //folderName);
293 }
294
295 void ProjectList::slotAddFolder(const QString foldername, int clipId, bool remove, bool edit) {
296     if (remove) {
297         ProjectItem *item;
298         QTreeWidgetItemIterator it(listView);
299         while (*it) {
300             item = static_cast <ProjectItem *>(*it);
301             if (item->clipType() == FOLDER && item->clipId() == clipId) {
302                 delete item;
303                 break;
304             }
305             ++it;
306         }
307     } else {
308         if (edit) {
309             disconnect(listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotUpdateItemDescription(QTreeWidgetItem *, int)));
310             ProjectItem *item;
311             QTreeWidgetItemIterator it(listView);
312             while (*it) {
313                 item = static_cast <ProjectItem *>(*it);
314                 if (item->clipType() == FOLDER && item->clipId() == clipId) {
315                     item->setText(1, foldername);
316                     break;
317                 }
318                 ++it;
319             }
320             connect(listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotUpdateItemDescription(QTreeWidgetItem *, int)));
321         } else {
322             QStringList text;
323             text << QString() << foldername;
324             (void) new ProjectItem(listView, text, clipId);
325         }
326     }
327 }
328
329 void ProjectList::slotAddClip(DocClipBase *clip) {
330     const int parent = clip->toXML().attribute("groupid").toInt();
331     if (parent != 0) {
332         ProjectItem *parentitem = getItemById(parent);
333         if (parentitem)(void) new ProjectItem(parentitem, clip);
334         else (void) new ProjectItem(listView, clip);
335     } else (void) new ProjectItem(listView, clip);
336     emit getFileProperties(clip->toXML(), clip->getId());
337 }
338
339 void ProjectList::slotUpdateClip(int id) {
340     ProjectItem *item = getItemById(id);
341     item->setData(1, UsageRole, QString::number(item->numReferences()));
342 }
343
344 void ProjectList::slotAddClip(QUrl givenUrl, QString group) {
345     if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!!  NO CMD STK";
346     KUrl::List list;
347     if (givenUrl.isEmpty())
348         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");
349     else list.append(givenUrl);
350     if (list.isEmpty()) return;
351     KUrl::List::Iterator it;
352     int groupId = -1;
353     if (group.isEmpty()) {
354         ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
355         if (item && item->clipType() != FOLDER) {
356             while (item->parent()) {
357                 item = static_cast <ProjectItem*>(item->parent());
358                 if (item->clipType() == FOLDER) break;
359             }
360         }
361         if (item && item->clipType() == FOLDER) {
362             group = item->groupName();
363             groupId = item->clipId();
364         }
365     }
366     for (it = list.begin(); it != list.end(); it++) {
367         m_doc->slotAddClipFile(*it, group, groupId);
368     }
369 }
370
371 void ProjectList::slotAddColorClip() {
372     if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!!  NO CMD STK";
373     QDialog *dia = new QDialog;
374     Ui::ColorClip_UI *dia_ui = new Ui::ColorClip_UI();
375     dia_ui->setupUi(dia);
376     dia_ui->clip_name->setText(i18n("Color Clip"));
377     dia_ui->clip_duration->setText(KdenliveSettings::color_duration());
378     if (dia->exec() == QDialog::Accepted) {
379         QString color = dia_ui->clip_color->color().name();
380         color = color.replace(0, 1, "0x") + "ff";
381
382         QString group = QString();
383         int groupId = -1;
384         ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
385         if (item && item->clipType() != FOLDER) {
386             while (item->parent()) {
387                 item = static_cast <ProjectItem*>(item->parent());
388                 if (item->clipType() == FOLDER) break;
389             }
390         }
391         if (item && item->clipType() == FOLDER) {
392             group = item->groupName();
393             groupId = item->clipId();
394         }
395
396         m_doc->slotAddColorClipFile(dia_ui->clip_name->text(), color, dia_ui->clip_duration->text(), group, groupId);
397     }
398     delete dia_ui;
399     delete dia;
400 }
401
402 void ProjectList::slotAddTitleClip() {
403
404     if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!!  NO CMD STK";
405     //QDialog *dia = new QDialog;
406
407     TitleWidget *dia_ui = new TitleWidget();
408     //dia_ui->setupUi(dia);
409     //dia_ui->clip_name->setText(i18n("Title Clip"));
410     //dia_ui->clip_duration->setText(KdenliveSettings::color_duration());
411     if (dia_ui->exec() == QDialog::Accepted) {
412         //QString color = dia_ui->clip_color->color().name();
413         //color = color.replace(0, 1, "0x") + "ff";
414         //m_doc->slotAddColorClipFile(dia_ui->clip_name->text(), color, dia_ui->clip_duration->text(), QString::null);
415     }
416     delete dia_ui;
417     //delete dia;
418 }
419 void ProjectList::setDocument(KdenliveDoc *doc) {
420     listView->clear();
421     QList <DocClipBase*> list = doc->clipManager()->documentClipList();
422     for (int i = 0; i < list.count(); i++) {
423         slotAddClip(list.at(i));
424     }
425
426     m_fps = doc->fps();
427     m_timecode = doc->timecode();
428     m_commandStack = doc->commandStack();
429     m_doc = doc;
430     /*    QDomNodeList prods = doc->producersList();
431         int ct = prods.count();
432         kDebug() << "////////////  SETTING DOC, FOUND CLIPS: " << prods.count();
433         listView->clear();
434         for (int i = 0; i <  ct ; i++) {
435             QDomElement e = prods.item(i).toElement();
436             kDebug() << "// IMPORT: " << i << ", :" << e.attribute("id", "non") << ", NAME: " << e.attribute("name", "non");
437             if (!e.isNull()) addProducer(e);
438         }*/
439     QTreeWidgetItem *first = listView->topLevelItem(0);
440     if (first) listView->setCurrentItem(first);
441     m_toolbar->setEnabled(true);
442 }
443
444 QDomElement ProjectList::producersList() {
445     QDomDocument doc;
446     QDomElement prods = doc.createElement("producerlist");
447     doc.appendChild(prods);
448     kDebug() << "////////////  PRO LIST BUILD PRDSLIST ";
449     QTreeWidgetItemIterator it(listView);
450     while (*it) {
451         if (!((ProjectItem *)(*it))->isGroup())
452             prods.appendChild(doc.importNode(((ProjectItem *)(*it))->toXml(), true));
453         ++it;
454     }
455     return prods;
456 }
457
458
459 void ProjectList::slotReplyGetFileProperties(int clipId, const QMap < QString, QString > &properties, const QMap < QString, QString > &metadata) {
460     ProjectItem *item = getItemById(clipId);
461     if (item) {
462         item->setProperties(properties, metadata);
463         listView->setCurrentItem(item);
464         emit receivedClipDuration(clipId, item->clipMaxDuration());
465     }
466 }
467
468
469
470 void ProjectList::slotReplyGetImage(int clipId, int pos, const QPixmap &pix, int w, int h) {
471     ProjectItem *item = getItemById(clipId);
472     if (item) item->setIcon(0, pix);
473 }
474
475 ProjectItem *ProjectList::getItemById(int id) {
476     QTreeWidgetItemIterator it(listView);
477     while (*it) {
478         if (((ProjectItem *)(*it))->clipId() == id)
479             break;
480         ++it;
481     }
482     if (*it) return ((ProjectItem *)(*it));
483     return NULL;
484 }
485
486
487 void ProjectList::addProducer(QDomElement producer, int parentId) {
488     if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!!  NO CMD STK";
489     CLIPTYPE type = (CLIPTYPE) producer.attribute("type").toInt();
490
491     /*QDomDocument doc;
492     QDomElement prods = doc.createElement("list");
493     doc.appendChild(prods);
494     prods.appendChild(doc.importNode(producer, true));*/
495
496
497     //kDebug()<<"//////  ADDING PRODUCER:\n "<<doc.toString()<<"\n+++++++++++++++++";
498     int id = producer.attribute("id").toInt();
499     QString groupName = producer.attribute("groupname");
500     if (id >= m_clipIdCounter) m_clipIdCounter = id + 1;
501     else if (id == 0) id = m_clipIdCounter++;
502
503     if (parentId != -1) {
504         // item is a westley playlist, adjust subproducers ids
505         id = (parentId + 1) * 10000 + id;
506     }
507     if (type == AUDIO || type == VIDEO || type == AV || type == IMAGE  || type == PLAYLIST) {
508         KUrl resource = KUrl(producer.attribute("resource"));
509         if (!resource.isEmpty()) {
510             QStringList itemEntry;
511             itemEntry.append(QString::null);
512             itemEntry.append(resource.fileName());
513             addClip(itemEntry, producer, id, resource, groupName, parentId);
514         }
515     } else if (type == COLOR) {
516         QString colour = producer.attribute("colour");
517         QPixmap pix(60, 40);
518         colour = colour.replace(0, 2, "#");
519         pix.fill(QColor(colour.left(7)));
520         QStringList itemEntry;
521         itemEntry.append(QString::null);
522         itemEntry.append(producer.attribute("name", i18n("Color clip")));
523         addClip(itemEntry, producer, id, KUrl(), groupName, parentId);
524     }
525
526 }
527
528 #include "projectlist.moc"