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>
31 #include <KFileDialog>
32 #include <KInputDialog>
33 #include <kio/netaccess.h>
34 #include <KMessageBox>
36 #include <nepomuk/global.h>
37 #include <nepomuk/resource.h>
38 #include <nepomuk/tag.h>
40 #include "projectlist.h"
41 #include "projectitem.h"
42 #include "kdenlivesettings.h"
43 #include "slideshowclip.h"
44 #include "ui_colorclip_ui.h"
47 #include "definitions.h"
48 #include "clipmanager.h"
49 #include "docclipbase.h"
50 #include "kdenlivedoc.h"
53 #include "projectlistview.h"
55 ProjectList::ProjectList(QWidget *parent)
56 : QWidget(parent), m_render(NULL), m_fps(-1), m_commandStack(NULL), m_selectedItem(NULL) {
58 QWidget *vbox = new QWidget;
59 listView = new ProjectListView(this);;
60 QVBoxLayout *layout = new QVBoxLayout;
64 searchView = new KTreeWidgetSearchLine(this);
65 m_toolbar = new QToolBar("projectToolBar", this);
66 m_toolbar->addWidget(searchView);
68 QToolButton *addButton = new QToolButton(m_toolbar);
69 QMenu *addMenu = new QMenu(this);
70 addButton->setMenu(addMenu);
71 addButton->setPopupMode(QToolButton::MenuButtonPopup);
72 m_toolbar->addWidget(addButton);
74 QAction *addClipButton = addMenu->addAction(KIcon("kdenlive-add-clip"), i18n("Add Clip"));
75 connect(addClipButton, SIGNAL(triggered()), this, SLOT(slotAddClip()));
77 QAction *addColorClip = addMenu->addAction(KIcon("kdenlive-add-color-clip"), i18n("Add Color Clip"));
78 connect(addColorClip, SIGNAL(triggered()), this, SLOT(slotAddColorClip()));
80 QAction *addSlideClip = addMenu->addAction(KIcon("kdenlive-add-slide-clip"), i18n("Add Slideshow Clip"));
81 connect(addSlideClip, SIGNAL(triggered()), this, SLOT(slotAddSlideshowClip()));
83 QAction *addTitleClip = addMenu->addAction(KIcon("kdenlive-add-text-clip"), i18n("Add Title Clip"));
84 connect(addTitleClip, SIGNAL(triggered()), this, SLOT(slotAddTitleClip()));
86 m_deleteAction = m_toolbar->addAction(KIcon("edit-delete"), i18n("Delete Clip"));
87 connect(m_deleteAction, SIGNAL(triggered()), this, SLOT(slotRemoveClip()));
89 m_editAction = m_toolbar->addAction(KIcon("document-properties"), i18n("Edit Clip"));
90 connect(m_editAction, SIGNAL(triggered()), this, SLOT(slotEditClip()));
92 QAction *addFolderButton = addMenu->addAction(KIcon("folder-new"), i18n("Create Folder"));
93 connect(addFolderButton, SIGNAL(triggered()), this, SLOT(slotAddFolder()));
95 addButton->setDefaultAction(addClipButton);
97 layout->addWidget(m_toolbar);
98 layout->addWidget(listView);
100 //m_toolbar->setEnabled(false);
102 searchView->setTreeWidget(listView);
104 m_menu = new QMenu();
105 m_menu->addAction(addClipButton);
106 m_menu->addAction(addColorClip);
107 m_menu->addAction(addSlideClip);
108 m_menu->addAction(addTitleClip);
109 m_menu->addAction(m_editAction);
110 m_menu->addAction(m_deleteAction);
111 m_menu->addAction(addFolderButton);
112 m_menu->insertSeparator(m_deleteAction);
114 connect(listView, SIGNAL(itemSelectionChanged()), this, SLOT(slotClipSelected()));
115 connect(listView, SIGNAL(focusMonitor()), this, SLOT(slotClipSelected()));
116 connect(listView, SIGNAL(requestMenu(const QPoint &, QTreeWidgetItem *)), this, SLOT(slotContextMenu(const QPoint &, QTreeWidgetItem *)));
117 connect(listView, SIGNAL(addClip()), this, SLOT(slotAddClip()));
118 connect(listView, SIGNAL(addClip(QUrl, const QString &)), this, SLOT(slotAddClip(QUrl, const QString &)));
119 connect(listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotItemEdited(QTreeWidgetItem *, int)));
120 connect(listView, SIGNAL(showProperties(DocClipBase *)), this, SIGNAL(showClipProperties(DocClipBase *)));
122 m_listViewDelegate = new ItemDelegate(listView);
123 listView->setItemDelegate(m_listViewDelegate);
126 ProjectList::~ProjectList() {
131 void ProjectList::slotEditClip() {
132 ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
133 if (item && !item->isGroup()) emit clipSelected(item->referencedClip());
134 emit showClipProperties(item->referencedClip());
139 void ProjectList::setRenderer(Render *projectRender) {
140 m_render = projectRender;
143 void ProjectList::slotClipSelected() {
144 ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
145 if (item && !item->isGroup()) {
146 if (item == m_selectedItem) {
147 // if user clicked on the active clip item, just focus monitor, don't update it.
148 emit clipSelected(NULL);
151 m_selectedItem = item;
152 emit clipSelected(item->referencedClip());
156 void ProjectList::slotUpdateClipProperties(int id, QMap <QString, QString> properties) {
157 ProjectItem *item = getItemById(id);
159 slotUpdateClipProperties(item, properties);
160 if (properties.contains("colour") || properties.contains("resource")) slotRefreshClipThumbnail(item);
161 if (properties.contains("out")) item->changeDuration(properties.value("out").toInt());
165 void ProjectList::slotUpdateClipProperties(ProjectItem *clip, QMap <QString, QString> properties) {
167 clip->setProperties(properties);
168 if (properties.contains("description")) {
169 CLIPTYPE type = clip->clipType();
170 clip->setText(2, properties.value("description"));
171 if (type == AUDIO || type == VIDEO || type == AV || type == IMAGE || type == PLAYLIST) {
172 // Use Nepomuk system to store clip description
173 Nepomuk::Resource f(clip->clipUrl().path());
174 if (f.isValid()) f.setDescription(properties.value("description"));
179 void ProjectList::slotItemEdited(QTreeWidgetItem *item, int column) {
180 ProjectItem *clip = static_cast <ProjectItem*>(item);
182 QMap <QString, QString> props;
183 props["description"] = item->text(2);
184 slotUpdateClipProperties(clip, props);
185 } else if (column == 1 && clip->clipType() == FOLDER) {
186 m_doc->slotEditFolder(item->text(1), clip->groupName(), clip->clipId());
190 void ProjectList::slotContextMenu(const QPoint &pos, QTreeWidgetItem *item) {
195 m_editAction->setEnabled(enable);
196 m_deleteAction->setEnabled(enable);
201 void ProjectList::slotRemoveClip() {
202 if (!listView->currentItem()) return;
203 ProjectItem *item = static_cast <ProjectItem *>(listView->currentItem());
205 QMap <QString, int> folderids;
206 if (item->clipType() == FOLDER) folderids[item->groupName()] = item->clipId();
207 else ids << item->clipId();
208 if (item->numReferences() > 0) {
209 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;
210 } else if (item->clipType() == FOLDER && item->childCount() > 0) {
211 int children = item->childCount();
212 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;
213 for (int i = 0; i < children; ++i) {
214 ProjectItem *child = static_cast <ProjectItem *>(item->child(i));
215 ids << child->clipId();
218 if (!ids.isEmpty()) m_doc->deleteProjectClip(ids);
219 if (!folderids.isEmpty()) m_doc->deleteProjectFolder(folderids);
222 void ProjectList::selectItemById(const int clipId) {
223 ProjectItem *item = getItemById(clipId);
224 if (item) listView->setCurrentItem(item);
228 void ProjectList::slotDeleteClip(int clipId) {
229 ProjectItem *item = getItemById(clipId);
230 QTreeWidgetItem *p = item->parent();
232 kDebug() << "/////// DELETEED CLIP HAS A PARENT... " << p->indexOfChild(item);
233 QTreeWidgetItem *clone = p->takeChild(p->indexOfChild(item));
239 void ProjectList::slotAddFolder() {
241 // QString folderName = KInputDialog::getText(i18n("New Folder"), i18n("Enter new folder name: "));
242 // if (folderName.isEmpty()) return;
243 m_doc->slotAddFolder(i18n("Folder")); //folderName);
246 void ProjectList::slotAddFolder(const QString foldername, int clipId, bool remove, bool edit) {
249 QTreeWidgetItemIterator it(listView);
251 item = static_cast <ProjectItem *>(*it);
252 if (item->clipType() == FOLDER && item->clipId() == clipId) {
260 disconnect(listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotUpdateItemDescription(QTreeWidgetItem *, int)));
262 QTreeWidgetItemIterator it(listView);
264 item = static_cast <ProjectItem *>(*it);
265 if (item->clipType() == FOLDER && item->clipId() == clipId) {
266 item->setText(1, foldername);
271 connect(listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotUpdateItemDescription(QTreeWidgetItem *, int)));
274 text << QString() << foldername;
275 (void) new ProjectItem(listView, text, clipId);
280 void ProjectList::slotAddClip(DocClipBase *clip) {
281 const int parent = clip->toXML().attribute("groupid").toInt();
282 ProjectItem *item = NULL;
284 ProjectItem *parentitem = getItemById(parent);
285 if (parentitem) item = new ProjectItem(parentitem, clip);
287 if (item == NULL) item = new ProjectItem(listView, clip);
289 KUrl url = clip->fileURL();
290 if (!url.isEmpty()) {
291 // if file has Nepomuk comment, use it
292 Nepomuk::Resource f(url.path());
295 annotation = f.description();
297 Nepomuk::Tag tag("test");
299 } else kDebug() << "--- CANNOT CONTACT NEPOMUK";
300 if (!annotation.isEmpty()) item->setText(2, annotation);
302 emit getFileProperties(clip->toXML(), clip->getId());
305 void ProjectList::slotUpdateClip(int id) {
306 ProjectItem *item = getItemById(id);
307 item->setData(1, UsageRole, QString::number(item->numReferences()));
310 void ProjectList::slotAddClip(QUrl givenUrl, QString group) {
311 if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
313 if (givenUrl.isEmpty()) {
314 list = KFileDialog::getOpenUrls(KUrl("kfiledialog:///clipfolder"), "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 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\n*.m2t *.mts|HDV video\n*.dv|DV video");
315 } else list.append(givenUrl);
316 if (list.isEmpty()) return;
317 KUrl::List::Iterator it;
319 if (group.isEmpty()) {
320 ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
321 if (item && item->clipType() != FOLDER) {
322 while (item->parent()) {
323 item = static_cast <ProjectItem*>(item->parent());
324 if (item->clipType() == FOLDER) break;
327 if (item && item->clipType() == FOLDER) {
328 group = item->groupName();
329 groupId = item->clipId();
332 for (it = list.begin(); it != list.end(); it++) {
333 m_doc->slotAddClipFile(*it, group, groupId);
337 void ProjectList::slotAddColorClip() {
338 if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
339 QDialog *dia = new QDialog(this);
340 Ui::ColorClip_UI *dia_ui = new Ui::ColorClip_UI();
341 dia_ui->setupUi(dia);
342 dia_ui->clip_name->setText(i18n("Color Clip"));
343 dia_ui->clip_duration->setText(KdenliveSettings::color_duration());
344 if (dia->exec() == QDialog::Accepted) {
345 QString color = dia_ui->clip_color->color().name();
346 color = color.replace(0, 1, "0x") + "ff";
348 QString group = QString();
350 ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
351 if (item && item->clipType() != FOLDER) {
352 while (item->parent()) {
353 item = static_cast <ProjectItem*>(item->parent());
354 if (item->clipType() == FOLDER) break;
357 if (item && item->clipType() == FOLDER) {
358 group = item->groupName();
359 groupId = item->clipId();
362 m_doc->slotAddColorClipFile(dia_ui->clip_name->text(), color, dia_ui->clip_duration->text(), group, groupId);
369 void ProjectList::slotAddSlideshowClip() {
370 if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
371 SlideshowClip *dia = new SlideshowClip(this);
373 if (dia->exec() == QDialog::Accepted) {
375 QString group = QString();
377 ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
378 if (item && item->clipType() != FOLDER) {
379 while (item->parent()) {
380 item = static_cast <ProjectItem*>(item->parent());
381 if (item->clipType() == FOLDER) break;
384 if (item && item->clipType() == FOLDER) {
385 group = item->groupName();
386 groupId = item->clipId();
389 m_doc->slotAddSlideshowClipFile(dia->clipName(), dia->selectedPath(), dia->imageCount(), dia->clipDuration(), dia->loop(), group, groupId);
394 void ProjectList::slotAddTitleClip() {
395 QString group = QString();
397 ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
398 if (item && item->clipType() != FOLDER) {
399 while (item->parent()) {
400 item = static_cast <ProjectItem*>(item->parent());
401 if (item->clipType() == FOLDER) break;
404 if (item && item->clipType() == FOLDER) {
405 group = item->groupName();
406 groupId = item->clipId();
409 m_doc->slotCreateTextClip(group, groupId);
412 void ProjectList::setDocument(KdenliveDoc *doc) {
414 QList <DocClipBase*> list = doc->clipManager()->documentClipList();
415 for (int i = 0; i < list.count(); i++) {
416 slotAddClip(list.at(i));
420 m_timecode = doc->timecode();
421 m_commandStack = doc->commandStack();
423 QTreeWidgetItem *first = listView->topLevelItem(0);
424 if (first) listView->setCurrentItem(first);
425 m_toolbar->setEnabled(true);
428 QDomElement ProjectList::producersList() {
430 QDomElement prods = doc.createElement("producerlist");
431 doc.appendChild(prods);
432 kDebug() << "//////////// PRO LIST BUILD PRDSLIST ";
433 QTreeWidgetItemIterator it(listView);
435 if (!((ProjectItem *)(*it))->isGroup())
436 prods.appendChild(doc.importNode(((ProjectItem *)(*it))->toXml(), true));
442 void ProjectList::slotRefreshClipThumbnail(int clipId) {
443 ProjectItem *item = getItemById(clipId);
444 if (item) slotRefreshClipThumbnail(item);
447 void ProjectList::slotRefreshClipThumbnail(ProjectItem *item) {
450 int width = (int)(height * m_render->dar());
451 QPixmap pix = KThumb::getImage(item->toXml(), item->referencedClip()->getClipThumbFrame(), width, height);
452 item->setIcon(0, pix);
456 void ProjectList::slotReplyGetFileProperties(int clipId, Mlt::Producer *producer, const QMap < QString, QString > &properties, const QMap < QString, QString > &metadata) {
457 ProjectItem *item = getItemById(clipId);
459 item->setProperties(properties, metadata);
460 item->referencedClip()->setProducer(producer);
461 listView->setCurrentItem(item);
462 emit receivedClipDuration(clipId, item->clipMaxDuration());
463 } else kDebug() << "//////// COULD NOT FIND CLIP TO UPDATE PRPS...";
466 void ProjectList::slotReplyGetImage(int clipId, int pos, const QPixmap &pix, int w, int h) {
467 ProjectItem *item = getItemById(clipId);
468 if (item) item->setIcon(0, pix);
471 ProjectItem *ProjectList::getItemById(int id) {
472 QTreeWidgetItemIterator it(listView);
474 if (((ProjectItem *)(*it))->clipId() == id)
478 if (*it) return ((ProjectItem *)(*it));
482 #include "projectlist.moc"