1 /***************************************************************************
2 * Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org) *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
21 #include <QMouseEvent>
22 #include <QStylePainter>
30 #include <KFileDialog>
31 #include <KInputDialog>
32 #include <kio/netaccess.h>
33 #include <KMessageBox>
35 #include <nepomuk/global.h>
36 #include <nepomuk/resource.h>
37 #include <nepomuk/tag.h>
39 #include "projectlist.h"
40 #include "projectitem.h"
41 #include "kdenlivesettings.h"
42 #include "ui_colorclip_ui.h"
44 #include "definitions.h"
45 #include "clipmanager.h"
46 #include "docclipbase.h"
47 #include "kdenlivedoc.h"
49 #include "projectlistview.h"
52 ProjectList::ProjectList(QWidget *parent)
53 : QWidget(parent), m_render(NULL), m_fps(-1), m_commandStack(NULL) {
55 QWidget *vbox = new QWidget;
56 listView = new ProjectListView(this);;
57 QVBoxLayout *layout = new QVBoxLayout;
61 searchView = new KTreeWidgetSearchLine(this);
62 m_toolbar = new QToolBar("projectToolBar", this);
63 m_toolbar->addWidget(searchView);
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);
71 QAction *addClipButton = addMenu->addAction(KIcon("document-new"), i18n("Add Clip"));
72 connect(addClipButton, SIGNAL(triggered()), this, SLOT(slotAddClip()));
74 QAction *addColorClip = addMenu->addAction(KIcon("document-new"), i18n("Add Color Clip"));
75 connect(addColorClip, SIGNAL(triggered()), this, SLOT(slotAddColorClip()));
77 QAction *addTitleClip = addMenu->addAction(KIcon("document-new"), i18n("Add Title Clip"));
78 connect(addTitleClip, SIGNAL(triggered()), this, SLOT(slotAddTitleClip()));
80 m_deleteAction = m_toolbar->addAction(KIcon("edit-delete"), i18n("Delete Clip"));
81 connect(m_deleteAction, SIGNAL(triggered()), this, SLOT(slotRemoveClip()));
83 m_editAction = m_toolbar->addAction(KIcon("document-properties"), i18n("Edit Clip"));
84 connect(m_editAction, SIGNAL(triggered()), this, SLOT(slotEditClip()));
86 QAction *addFolderButton = addMenu->addAction(KIcon("folder-new"), i18n("Create Folder"));
87 connect(addFolderButton, SIGNAL(triggered()), this, SLOT(slotAddFolder()));
89 addButton->setDefaultAction(addClipButton);
91 layout->addWidget(m_toolbar);
92 layout->addWidget(listView);
94 //m_toolbar->setEnabled(false);
96 searchView->setTreeWidget(listView);
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);
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 *)));
114 m_listViewDelegate = new ItemDelegate(listView);
115 listView->setItemDelegate(m_listViewDelegate);
118 ProjectList::~ProjectList() {
125 void ProjectList::setRenderer(Render *projectRender) {
126 m_render = projectRender;
129 void ProjectList::slotClipSelected() {
130 ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
131 if (item && !item->isGroup()) emit clipSelected(item->toXml());
134 void ProjectList::slotUpdateClipProperties(int id, QMap <QString, QString> properties) {
135 ProjectItem *item = getItemById(id);
136 if (item) slotUpdateClipProperties(item, properties);
139 void ProjectList::slotUpdateClipProperties(ProjectItem *clip, QMap <QString, QString> properties) {
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"));
153 void ProjectList::slotItemEdited(QTreeWidgetItem *item, int column) {
154 ProjectItem *clip = static_cast <ProjectItem*>(item);
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());
164 void ProjectList::slotContextMenu(const QPoint &pos, QTreeWidgetItem *item) {
169 m_editAction->setEnabled(enable);
170 m_deleteAction->setEnabled(enable);
175 void ProjectList::slotRemoveClip() {
176 if (!listView->currentItem()) return;
177 ProjectItem *item = static_cast <ProjectItem *>(listView->currentItem());
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();
192 if (!ids.isEmpty()) m_doc->deleteProjectClip(ids);
193 if (!folderids.isEmpty()) m_doc->deleteProjectFolder(folderids);
196 void ProjectList::selectItemById(const int clipId) {
197 ProjectItem *item = getItemById(clipId);
198 if (item) listView->setCurrentItem(item);
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;
204 ProjectItem *groupItem = NULL;
206 if (group.isEmpty()) groupName = elem.attribute("groupname", QString::null);
207 else groupName = group;
208 if (elem.isNull() && url.isEmpty()) {
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());
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);
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();
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());
237 if (f.isValid()) annotation = f.description();
239 if (!annotation.isEmpty()) item->setText(2, annotation);
240 QString resource = url.path();
241 if (resource.endsWith("westley") || resource.endsWith("kdenlive")) {
244 if (KIO::NetAccess::download(url, tmpfile, 0)) {
246 if (file.open(QIODevice::ReadOnly)) {
247 doc.setContent(&file, false);
250 KIO::NetAccess::removeTempFile(tmpfile);
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();
257 addProducer(e, clipId);
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);
274 void ProjectList::slotDeleteClip(int clipId) {
275 ProjectItem *item = getItemById(clipId);
276 QTreeWidgetItem *p = item->parent();
278 kDebug() << "/////// DELETEED CLIP HAS A PARENT... " << p->indexOfChild(item);
279 QTreeWidgetItem *clone = p->takeChild(p->indexOfChild(item));
280 } else if (item) delete item;
283 void ProjectList::slotAddFolder() {
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);
290 void ProjectList::slotAddFolder(const QString foldername, int clipId, bool remove, bool edit) {
293 QTreeWidgetItemIterator it(listView);
295 item = static_cast <ProjectItem *>(*it);
296 if (item->clipType() == FOLDER && item->clipId() == clipId) {
304 disconnect(listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotUpdateItemDescription(QTreeWidgetItem *, int)));
306 QTreeWidgetItemIterator it(listView);
308 item = static_cast <ProjectItem *>(*it);
309 if (item->clipType() == FOLDER && item->clipId() == clipId) {
310 item->setText(1, foldername);
315 connect(listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotUpdateItemDescription(QTreeWidgetItem *, int)));
318 text << QString() << foldername;
319 (void) new ProjectItem(listView, text, clipId);
324 void ProjectList::slotAddClip(DocClipBase *clip) {
325 const int parent = clip->toXML().attribute("groupid").toInt();
326 ProjectItem *item = NULL;
328 ProjectItem *parentitem = getItemById(parent);
329 if (parentitem) item = new ProjectItem(parentitem, clip);
331 if (item == NULL) item = new ProjectItem(listView, clip);
333 KUrl url = clip->fileURL();
334 if (!url.isEmpty()) {
335 // if file has Nepomuk comment, use it
336 Nepomuk::Resource f(url.path());
339 annotation = f.description();
341 Nepomuk::Tag tag("test");
343 } else kDebug() << "--- CANNOT CONTACT NEPOMUK";
344 if (!annotation.isEmpty()) item->setText(2, annotation);
346 emit getFileProperties(clip->toXML(), clip->getId());
349 void ProjectList::slotUpdateClip(int id) {
350 ProjectItem *item = getItemById(id);
351 item->setData(1, UsageRole, QString::number(item->numReferences()));
354 void ProjectList::slotAddClip(QUrl givenUrl, QString group) {
355 if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
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;
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;
371 if (item && item->clipType() == FOLDER) {
372 group = item->groupName();
373 groupId = item->clipId();
376 for (it = list.begin(); it != list.end(); it++) {
377 m_doc->slotAddClipFile(*it, group, groupId);
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";
392 QString group = QString();
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;
401 if (item && item->clipType() == FOLDER) {
402 group = item->groupName();
403 groupId = item->clipId();
406 m_doc->slotAddColorClipFile(dia_ui->clip_name->text(), color, dia_ui->clip_duration->text(), group, groupId);
412 void ProjectList::slotAddTitleClip() {
413 QString group = QString();
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;
422 if (item && item->clipType() == FOLDER) {
423 group = item->groupName();
424 groupId = item->clipId();
427 m_doc->slotCreateTextClip(group, groupId);
429 void ProjectList::setDocument(KdenliveDoc *doc) {
431 QList <DocClipBase*> list = doc->clipManager()->documentClipList();
432 for (int i = 0; i < list.count(); i++) {
433 slotAddClip(list.at(i));
437 m_timecode = doc->timecode();
438 m_commandStack = doc->commandStack();
440 /* QDomNodeList prods = doc->producersList();
441 int ct = prods.count();
442 kDebug() << "//////////// SETTING DOC, FOUND CLIPS: " << prods.count();
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);
449 QTreeWidgetItem *first = listView->topLevelItem(0);
450 if (first) listView->setCurrentItem(first);
451 m_toolbar->setEnabled(true);
454 QDomElement ProjectList::producersList() {
456 QDomElement prods = doc.createElement("producerlist");
457 doc.appendChild(prods);
458 kDebug() << "//////////// PRO LIST BUILD PRDSLIST ";
459 QTreeWidgetItemIterator it(listView);
461 if (!((ProjectItem *)(*it))->isGroup())
462 prods.appendChild(doc.importNode(((ProjectItem *)(*it))->toXml(), true));
469 void ProjectList::slotReplyGetFileProperties(int clipId, const QMap < QString, QString > &properties, const QMap < QString, QString > &metadata) {
470 ProjectItem *item = getItemById(clipId);
472 item->setProperties(properties, metadata);
473 listView->setCurrentItem(item);
474 emit receivedClipDuration(clipId, item->clipMaxDuration());
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);
485 ProjectItem *ProjectList::getItemById(int id) {
486 QTreeWidgetItemIterator it(listView);
488 if (((ProjectItem *)(*it))->clipId() == id)
492 if (*it) return ((ProjectItem *)(*it));
497 void ProjectList::addProducer(QDomElement producer, int parentId) {
498 if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
499 CLIPTYPE type = (CLIPTYPE) producer.attribute("type").toInt();
502 QDomElement prods = doc.createElement("list");
503 doc.appendChild(prods);
504 prods.appendChild(doc.importNode(producer, true));*/
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++;
513 if (parentId != -1) {
514 // item is a westley playlist, adjust subproducers ids
515 id = (parentId + 1) * 10000 + id;
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);
525 } else if (type == COLOR) {
526 QString colour = producer.attribute("colour");
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);
538 #include "projectlist.moc"