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 "titlewidget.h"
46 #include "clipmanager.h"
47 #include "docclipbase.h"
48 #include "kdenlivedoc.h"
50 #include "projectlistview.h"
53 ProjectList::ProjectList(QWidget *parent)
54 : QWidget(parent), m_render(NULL), m_fps(-1), m_commandStack(NULL) {
56 QWidget *vbox = new QWidget;
57 listView = new ProjectListView(this);;
58 QVBoxLayout *layout = new QVBoxLayout;
62 searchView = new KTreeWidgetSearchLine(this);
63 m_toolbar = new QToolBar("projectToolBar", this);
64 m_toolbar->addWidget(searchView);
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);
72 QAction *addClipButton = addMenu->addAction(KIcon("document-new"), i18n("Add Clip"));
73 connect(addClipButton, SIGNAL(triggered()), this, SLOT(slotAddClip()));
75 QAction *addColorClip = addMenu->addAction(KIcon("document-new"), i18n("Add Color Clip"));
76 connect(addColorClip, SIGNAL(triggered()), this, SLOT(slotAddColorClip()));
78 QAction *addTitleClip = addMenu->addAction(KIcon("document-new"), i18n("Add Title Clip"));
79 connect(addTitleClip, SIGNAL(triggered()), this, SLOT(slotAddTitleClip()));
81 m_deleteAction = m_toolbar->addAction(KIcon("edit-delete"), i18n("Delete Clip"));
82 connect(m_deleteAction, SIGNAL(triggered()), this, SLOT(slotRemoveClip()));
84 m_editAction = m_toolbar->addAction(KIcon("document-properties"), i18n("Edit Clip"));
85 connect(m_editAction, SIGNAL(triggered()), this, SLOT(slotEditClip()));
87 QAction *addFolderButton = addMenu->addAction(KIcon("folder-new"), i18n("Create Folder"));
88 connect(addFolderButton, SIGNAL(triggered()), this, SLOT(slotAddFolder()));
90 addButton->setDefaultAction(addClipButton);
92 layout->addWidget(m_toolbar);
93 layout->addWidget(listView);
95 //m_toolbar->setEnabled(false);
97 searchView->setTreeWidget(listView);
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);
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 *)));
115 m_listViewDelegate = new ItemDelegate(listView);
116 listView->setItemDelegate(m_listViewDelegate);
119 ProjectList::~ProjectList() {
126 void ProjectList::setRenderer(Render *projectRender) {
127 m_render = projectRender;
130 void ProjectList::slotClipSelected() {
131 ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
132 if (item && !item->isGroup()) emit clipSelected(item->toXml());
135 void ProjectList::slotUpdateItemDescription(QTreeWidgetItem *item, int column) {
136 ProjectItem *clip = static_cast <ProjectItem*>(item);
137 CLIPTYPE type = clip->clipType();
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);
146 } else if (column == 1 && type == FOLDER) {
147 m_doc->slotEditFolder(item->text(1), clip->groupName(), clip->clipId());
151 void ProjectList::slotContextMenu(const QPoint &pos, QTreeWidgetItem *item) {
156 m_editAction->setEnabled(enable);
157 m_deleteAction->setEnabled(enable);
162 void ProjectList::slotRemoveClip() {
163 if (!listView->currentItem()) return;
164 ProjectItem *item = static_cast <ProjectItem *>(listView->currentItem());
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();
179 if (!ids.isEmpty()) m_doc->deleteProjectClip(ids);
180 if (!folderids.isEmpty()) m_doc->deleteProjectFolder(folderids);
183 void ProjectList::selectItemById(const int clipId) {
184 ProjectItem *item = getItemById(clipId);
185 if (item) listView->setCurrentItem(item);
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;
191 ProjectItem *groupItem = NULL;
193 if (group.isEmpty()) groupName = elem.attribute("groupname", QString::null);
194 else groupName = group;
195 if (elem.isNull() && url.isEmpty()) {
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());
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);
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();
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());
224 if (f.isValid()) annotation = f.description();
226 if (!annotation.isEmpty()) item->setText(2, annotation);
227 QString resource = url.path();
228 if (resource.endsWith("westley") || resource.endsWith("kdenlive")) {
231 if (KIO::NetAccess::download(url, tmpfile, 0)) {
233 if (file.open(QIODevice::ReadOnly)) {
234 doc.setContent(&file, false);
237 KIO::NetAccess::removeTempFile(tmpfile);
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();
244 addProducer(e, clipId);
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);
261 void ProjectList::slotDeleteClip(int clipId) {
262 ProjectItem *item = getItemById(clipId);
263 QTreeWidgetItem *p = item->parent();
265 kDebug() << "/////// DELETEED CLIP HAS A PARENT... " << p->indexOfChild(item);
266 QTreeWidgetItem *clone = p->takeChild(p->indexOfChild(item));
267 } else if (item) delete item;
270 void ProjectList::slotAddFolder() {
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);
277 void ProjectList::slotAddFolder(const QString foldername, int clipId, bool remove, bool edit) {
280 QTreeWidgetItemIterator it(listView);
282 item = static_cast <ProjectItem *>(*it);
283 if (item->clipType() == FOLDER && item->clipId() == clipId) {
291 disconnect(listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotUpdateItemDescription(QTreeWidgetItem *, int)));
293 QTreeWidgetItemIterator it(listView);
295 item = static_cast <ProjectItem *>(*it);
296 if (item->clipType() == FOLDER && item->clipId() == clipId) {
297 item->setText(1, foldername);
302 connect(listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotUpdateItemDescription(QTreeWidgetItem *, int)));
305 text << QString() << foldername;
306 (void) new ProjectItem(listView, text, clipId);
311 void ProjectList::slotAddClip(DocClipBase *clip) {
312 const int parent = clip->toXML().attribute("groupid").toInt();
313 ProjectItem *item = NULL;
315 ProjectItem *parentitem = getItemById(parent);
316 if (parentitem) item = new ProjectItem(parentitem, clip);
318 if (item == NULL) item = new ProjectItem(listView, clip);
320 KUrl url = clip->fileURL();
321 if (!url.isEmpty()) {
322 // if file has Nepomuk comment, use it
323 Nepomuk::Resource f(url.path());
326 annotation = f.description();
328 Nepomuk::Tag tag("test");
330 } else kDebug() << "--- CANNOT CONTACT NEPOMUK";
331 if (!annotation.isEmpty()) item->setText(2, annotation);
333 emit getFileProperties(clip->toXML(), clip->getId());
336 void ProjectList::slotUpdateClip(int id) {
337 ProjectItem *item = getItemById(id);
338 item->setData(1, UsageRole, QString::number(item->numReferences()));
341 void ProjectList::slotAddClip(QUrl givenUrl, QString group) {
342 if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
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;
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;
358 if (item && item->clipType() == FOLDER) {
359 group = item->groupName();
360 groupId = item->clipId();
363 for (it = list.begin(); it != list.end(); it++) {
364 m_doc->slotAddClipFile(*it, group, groupId);
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";
379 QString group = QString();
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;
388 if (item && item->clipType() == FOLDER) {
389 group = item->groupName();
390 groupId = item->clipId();
393 m_doc->slotAddColorClipFile(dia_ui->clip_name->text(), color, dia_ui->clip_duration->text(), group, groupId);
399 void ProjectList::slotAddTitleClip() {
401 if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
402 //QDialog *dia = new QDialog;
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);
416 void ProjectList::setDocument(KdenliveDoc *doc) {
418 QList <DocClipBase*> list = doc->clipManager()->documentClipList();
419 for (int i = 0; i < list.count(); i++) {
420 slotAddClip(list.at(i));
424 m_timecode = doc->timecode();
425 m_commandStack = doc->commandStack();
427 /* QDomNodeList prods = doc->producersList();
428 int ct = prods.count();
429 kDebug() << "//////////// SETTING DOC, FOUND CLIPS: " << prods.count();
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);
436 QTreeWidgetItem *first = listView->topLevelItem(0);
437 if (first) listView->setCurrentItem(first);
438 m_toolbar->setEnabled(true);
441 QDomElement ProjectList::producersList() {
443 QDomElement prods = doc.createElement("producerlist");
444 doc.appendChild(prods);
445 kDebug() << "//////////// PRO LIST BUILD PRDSLIST ";
446 QTreeWidgetItemIterator it(listView);
448 if (!((ProjectItem *)(*it))->isGroup())
449 prods.appendChild(doc.importNode(((ProjectItem *)(*it))->toXml(), true));
456 void ProjectList::slotReplyGetFileProperties(int clipId, const QMap < QString, QString > &properties, const QMap < QString, QString > &metadata) {
457 ProjectItem *item = getItemById(clipId);
459 item->setProperties(properties, metadata);
460 listView->setCurrentItem(item);
461 emit receivedClipDuration(clipId, item->clipMaxDuration());
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);
472 ProjectItem *ProjectList::getItemById(int id) {
473 QTreeWidgetItemIterator it(listView);
475 if (((ProjectItem *)(*it))->clipId() == id)
479 if (*it) return ((ProjectItem *)(*it));
484 void ProjectList::addProducer(QDomElement producer, int parentId) {
485 if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
486 CLIPTYPE type = (CLIPTYPE) producer.attribute("type").toInt();
489 QDomElement prods = doc.createElement("list");
490 doc.appendChild(prods);
491 prods.appendChild(doc.importNode(producer, true));*/
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++;
500 if (parentId != -1) {
501 // item is a westley playlist, adjust subproducers ids
502 id = (parentId + 1) * 10000 + id;
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);
512 } else if (type == COLOR) {
513 QString colour = producer.attribute("colour");
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);
525 #include "projectlist.moc"