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