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 "addclipcommand.h"
45 #include "definitions.h"
49 ProjectList::ProjectList(QWidget *parent)
50 : QWidget(parent), m_render(NULL), m_fps(-1), m_commandStack(NULL)
53 QWidget *vbox = new QWidget;
54 listView = new ProjectListView(this);;
55 QVBoxLayout *layout = new QVBoxLayout;
59 searchView = new KTreeWidgetSearchLine (this);
60 m_toolbar = new QToolBar("projectToolBar", this);
61 m_toolbar->addWidget (searchView);
63 QToolButton *addButton = new QToolButton( m_toolbar );
64 QMenu *addMenu = new QMenu(this);
65 addButton->setMenu( addMenu );
66 addButton->setPopupMode(QToolButton::MenuButtonPopup);
67 m_toolbar->addWidget (addButton);
69 QAction *addClipButton = addMenu->addAction (KIcon("document-new"), i18n("Add Clip"));
70 connect(addClipButton, SIGNAL(triggered()), this, SLOT(slotAddClip()));
72 QAction *addColorClip = addMenu->addAction (KIcon("document-new"), i18n("Add Color Clip"));
73 connect(addColorClip, SIGNAL(triggered()), this, SLOT(slotAddColorClip()));
75 m_deleteAction = m_toolbar->addAction (KIcon("edit-delete"), i18n("Delete Clip"));
76 connect(m_deleteAction, SIGNAL(triggered()), this, SLOT(slotRemoveClip()));
78 m_editAction = m_toolbar->addAction (KIcon("document-properties"), i18n("Edit Clip"));
79 connect(m_editAction, SIGNAL(triggered()), this, SLOT(slotEditClip()));
81 QAction *addFolderButton = addMenu->addAction (KIcon("folder-new"), i18n("Create Folder"));
82 connect(addFolderButton, SIGNAL(triggered()), this, SLOT(slotAddFolder()));
84 addButton->setDefaultAction( addClipButton );
86 layout->addWidget( m_toolbar );
87 layout->addWidget( listView );
89 m_toolbar->setEnabled(false);
91 searchView->setTreeWidget(listView);
92 listView->setColumnCount(3);
94 headers<<i18n("Thumbnail")<<i18n("Filename")<<i18n("Description");
95 listView->setHeaderLabels(headers);
96 listView->sortByColumn(1, Qt::AscendingOrder);
99 m_menu->addAction(addClipButton);
100 m_menu->addAction(addColorClip);
101 m_menu->addAction(m_editAction);
102 m_menu->addAction(m_deleteAction);
103 m_menu->addAction(addFolderButton);
104 m_menu->insertSeparator(m_deleteAction);
106 connect(listView, SIGNAL(itemSelectionChanged()), this, SLOT(slotClipSelected()));
107 connect(listView, SIGNAL(requestMenu ( const QPoint &, QTreeWidgetItem * )), this, SLOT(slotContextMenu(const QPoint &, QTreeWidgetItem *)));
108 connect(listView, SIGNAL(addClip ()), this, SLOT(slotAddClip()));
109 connect(listView, SIGNAL(addClip (QUrl, const QString &)), this, SLOT(slotAddClip(QUrl, const QString &)));
110 connect(listView, SIGNAL (itemChanged ( QTreeWidgetItem *, int )), this, SLOT(slotUpdateItemDescription(QTreeWidgetItem *, int )));
112 m_listViewDelegate = new ItemDelegate(listView);
113 listView->setItemDelegate(m_listViewDelegate);
114 listView->setIconSize(QSize(60, 40));
115 listView->setSortingEnabled (true);
118 ProjectList::~ProjectList()
124 void ProjectList::setRenderer(Render *projectRender)
126 m_render = projectRender;
129 void ProjectList::slotClipSelected()
131 ProjectItem *item = (ProjectItem*) listView->currentItem();
132 if (item && !item->isGroup()) emit clipSelected(item->toXml());
135 void ProjectList::slotUpdateItemDescription( QTreeWidgetItem *item, int column)
137 if (column != 2) return;
138 ProjectItem *clip = (ProjectItem*) item;
139 CLIPTYPE type = clip->clipType();
140 if (type == AUDIO || type == VIDEO || type == AV || type == IMAGE || type == PLAYLIST) {
141 // Use Nepomuk system to store clip description
142 Nepomuk::Resource f( clip->clipUrl().path() );
143 f.setDescription(item->text(2));
144 kDebug()<<"NEPOMUK, SETTING CLIP: "<<clip->clipUrl().path()<<", TO TEXT: "<<item->text(2);
148 void ProjectList::slotEditClip()
150 kDebug()<<"//////////////////////////////////////// DBL CLK";
154 void ProjectList::slotEditClip(QTreeWidgetItem *item, int column)
156 kDebug()<<"//////////////////////////////////////// DBL CLK";
159 void ProjectList::slotContextMenu( const QPoint &pos, QTreeWidgetItem *item )
163 QFrame *w = new QFrame;
164 w->setFrameShape(QFrame::StyledPanel);
166 w->setAutoFillBackground(true);
167 QHBoxLayout *layout = new QHBoxLayout;
168 layout->addWidget( new QLabel(i18n("Color:")));
169 layout->addWidget( new KColorButton());
170 layout->addWidget( new QLabel(i18n("Duration:")));
171 layout->addWidget( new KRestrictedLine());
172 w->setLayout( layout );
173 m_listViewDelegate->extendItem(w, listView->currentIndex());
176 m_editAction->setEnabled(enable);
177 m_deleteAction->setEnabled(enable);
182 void ProjectList::slotRemoveClip()
184 if (!m_commandStack) kDebug()<<"!!!!!!!!!!!!!!!! NO CMD STK";
185 if (!listView->currentItem()) return;
186 ProjectItem *item = ((ProjectItem *)listView->currentItem());
187 if (!item) kDebug()<<"/////////////// ERROR NOT FOUND";
188 if (KMessageBox::questionYesNo(this, i18n("Delete clip <b>%1</b> ?").arg(item->names().at(1)), i18n("Delete Clip")) != KMessageBox::Yes) return;
189 AddClipCommand *command = new AddClipCommand(this, item->names(), item->toXml(), item->clipId(), item->clipUrl(), item->groupName(), false);
190 m_commandStack->push(command);
193 void ProjectList::selectItemById(const int clipId)
195 ProjectItem *item = getItemById(clipId);
196 if (item) listView->setCurrentItem(item);
199 void ProjectList::addClip(const QStringList &name, const QDomElement &elem, const int clipId, const KUrl &url, const QString &group, int parentId)
201 kDebug()<<"///////// ADDING VCLIP=: "<<name;
203 ProjectItem *groupItem = NULL;
205 if (group.isEmpty()) groupName = elem.attribute("group", QString::null);
206 else groupName = group;
207 if (elem.isNull() && url.isEmpty()) {
209 groupName = name.at(1);
210 QList<QTreeWidgetItem *> groupList = listView->findItems(groupName, Qt::MatchExactly, 1);
211 if (groupList.isEmpty()) {
212 (void) new ProjectItem(listView, name, clipId);
217 if (parentId != -1) {
218 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_clipIdCounter++);
230 else groupItem = (ProjectItem *) groupList.first();
232 if (groupItem) item = new ProjectItem(groupItem, name, elem, clipId);
233 else item = new ProjectItem(listView, name, elem, clipId);
234 if (!url.isEmpty()) {
235 item->setData(1, FullPathRole, url.path());
236 // if file has Nepomuk comment, use it
237 Nepomuk::Resource f( url.path() );
238 QString 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++)
256 QDomElement e = subProds.item(i).toElement();
258 addProducer(e, clipId);
266 if (elem.isNull() ) {
268 QDomElement element = doc.createElement("producer");
269 element.setAttribute("resource", url.path());
270 emit getFileProperties(element, clipId);
272 else emit getFileProperties(elem, clipId);
273 selectItemById(clipId);
276 void ProjectList::deleteClip(const int clipId)
278 ProjectItem *item = getItemById(clipId);
279 if (item) delete item;
282 void ProjectList::slotAddFolder()
284 QString folderName = KInputDialog::getText(i18n("New Folder"), i18n("Enter new folder name: "));
285 if (folderName.isEmpty()) return;
286 QStringList itemEntry;
287 itemEntry.append(QString::null);
288 itemEntry.append(folderName);
289 AddClipCommand *command = new AddClipCommand(this, itemEntry, QDomElement(), m_clipIdCounter++, KUrl(), folderName, true);
290 m_commandStack->push(command);
293 void ProjectList::slotAddClip(QUrl givenUrl, const QString &group)
295 if (!m_commandStack) kDebug()<<"!!!!!!!!!!!!!!!! NO CMD STK";
297 if (givenUrl.isEmpty())
298 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/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");
299 else list.append(givenUrl);
300 if (list.isEmpty()) return;
301 KUrl::List::Iterator it;
303 // ProjectItem *item;
305 for (it = list.begin(); it != list.end(); it++) {
306 QStringList itemEntry;
308 QDomElement prod = doc.createElement("producer");
309 prod.setAttribute("resource", (*it).path());
310 itemEntry.append(QString::null);
311 itemEntry.append((*it).fileName());
312 KMimeType::Ptr type = KMimeType::findByUrl(*it);
313 if (type->name().startsWith("image/")) {
314 prod.setAttribute("type", (int) IMAGE);
315 prod.setAttribute("in", "0");
316 prod.setAttribute("out", m_timecode.getFrameCount(KdenliveSettings::image_duration(), m_fps));
318 AddClipCommand *command = new AddClipCommand(this, itemEntry, prod, m_clipIdCounter++, *it, group, true);
319 m_commandStack->push(command);
320 //item = new ProjectItem(listView, itemEntry, QDomElement());
321 //item->setData(1, FullPathRole, (*it).path());
323 //listView->setCurrentItem(item);
326 void ProjectList::slotAddColorClip()
328 if (!m_commandStack) kDebug()<<"!!!!!!!!!!!!!!!! NO CMD STK";
329 QDialog *dia = new QDialog;
330 Ui::ColorClip_UI *dia_ui = new Ui::ColorClip_UI();
331 dia_ui->setupUi(dia);
332 dia_ui->clip_name->setText(i18n("Color Clip"));
333 dia_ui->clip_duration->setText(KdenliveSettings::color_duration());
334 if (dia->exec() == QDialog::Accepted)
337 QDomElement element = doc.createElement("producer");
338 element.setAttribute("mlt_service", "colour");
339 QString color = dia_ui->clip_color->color().name();
340 color = color.replace(0, 1, "0x") + "ff";
341 element.setAttribute("colour", color);
342 element.setAttribute("type", (int) COLOR);
343 element.setAttribute("in", "0");
344 element.setAttribute("out", m_timecode.getFrameCount(dia_ui->clip_duration->text(), m_fps));
345 element.setAttribute("name", dia_ui->clip_name->text());
346 QStringList itemEntry;
347 itemEntry.append(QString::null);
348 itemEntry.append(dia_ui->clip_name->text());
349 AddClipCommand *command = new AddClipCommand(this, itemEntry, element, m_clipIdCounter++, KUrl(), QString::null, true);
350 m_commandStack->push(command);
351 // ProjectItem *item = new ProjectItem(listView, itemEntry, element, m_clipIdCounter++);
352 /*QPixmap pix(60, 40);
353 pix.fill(dia_ui->clip_color->color());
354 item->setIcon(0, QIcon(pix));*/
355 //listView->setCurrentItem(item);
362 void ProjectList::setDocument(KdenliveDoc *doc)
365 m_timecode = doc->timecode();
366 m_commandStack = doc->commandStack();
367 QDomNodeList prods = doc->producersList();
368 int ct = prods.count();
369 kDebug()<<"//////////// SETTING DOC, FOUND CLIPS: "<<prods.count();
371 for (int i = 0; i < ct ; i++)
373 QDomElement e = prods.item(i).toElement();
374 kDebug()<<"// IMPORT: "<<i<<", :"<<e.attribute("id", "non")<<", NAME: "<<e.attribute("name", "non");
375 if (!e.isNull()) addProducer(e);
377 QTreeWidgetItem *first = listView->topLevelItem(0);
378 if (first) listView->setCurrentItem(first);
379 m_toolbar->setEnabled(true);
382 QDomElement ProjectList::producersList()
385 QDomElement prods = doc.createElement("producerlist");
386 doc.appendChild(prods);
387 kDebug()<<"//////////// PRO LIST BUILD PRDSLIST ";
388 QTreeWidgetItemIterator it(listView);
390 if (!((ProjectItem *)(*it))->isGroup())
391 prods.appendChild(doc.importNode(((ProjectItem *)(*it))->toXml(), true));
398 void ProjectList::slotReplyGetFileProperties(int clipId, const QMap < QString, QString > &properties, const QMap < QString, QString > &metadata)
400 ProjectItem *item = getItemById(clipId);
402 item->setProperties(properties, metadata);
403 emit receivedClipDuration(clipId, item->clipMaxDuration());
409 void ProjectList::slotReplyGetImage(int clipId, int pos, const QPixmap &pix, int w, int h)
411 ProjectItem *item = getItemById(clipId);
412 if (item) item->setIcon(0, pix);
415 ProjectItem *ProjectList::getItemById(int id)
417 QTreeWidgetItemIterator it(listView);
419 if (((ProjectItem *)(*it))->clipId() == id)
423 return ((ProjectItem *)(*it));
427 void ProjectList::addProducer(QDomElement producer, int parentId)
429 if (!m_commandStack) kDebug()<<"!!!!!!!!!!!!!!!! NO CMD STK";
430 CLIPTYPE type = (CLIPTYPE) producer.attribute("type").toInt();
433 QDomElement prods = doc.createElement("list");
434 doc.appendChild(prods);
435 prods.appendChild(doc.importNode(producer, true));*/
438 //kDebug()<<"////// ADDING PRODUCER:\n "<<doc.toString()<<"\n+++++++++++++++++";
439 int id = producer.attribute("id").toInt();
440 QString groupName = producer.attribute("group");
441 if (id >= m_clipIdCounter) m_clipIdCounter = id + 1;
442 else if (id == 0) id = m_clipIdCounter++;
444 if (parentId != -1) {
445 // item is a westley playlist, adjust subproducers ids
446 id = (parentId + 1) * 10000 + id;
448 if (type == AUDIO || type == VIDEO || type == AV || type == IMAGE || type == PLAYLIST)
450 KUrl resource = KUrl(producer.attribute("resource"));
451 if (!resource.isEmpty()) {
452 QStringList itemEntry;
453 itemEntry.append(QString::null);
454 itemEntry.append(resource.fileName());
455 addClip(itemEntry, producer, id, resource, groupName, parentId);
456 /*AddClipCommand *command = new AddClipCommand(this, itemEntry, producer, id, resource, true);
457 m_commandStack->push(command);*/
460 /*ProjectItem *item = new ProjectItem(listView, itemEntry, producer);
461 item->setData(1, FullPathRole, resource.path());
462 item->setData(1, ClipTypeRole, (int) type);*/
466 else if (type == COLOR) {
467 QString colour = producer.attribute("colour");
469 colour = colour.replace(0, 2, "#");
470 pix.fill(QColor(colour.left(7)));
471 QStringList itemEntry;
472 itemEntry.append(QString::null);
473 itemEntry.append(producer.attribute("name", i18n("Color clip")));
474 addClip(itemEntry, producer, id, KUrl(), groupName, parentId);
475 /*AddClipCommand *command = new AddClipCommand(this, itemEntry, producer, id, KUrl(), true);
476 m_commandStack->push(command);*/
477 //ProjectItem *item = new ProjectItem(listView, itemEntry, producer);
478 /*item->setIcon(0, QIcon(pix));
479 item->setData(1, ClipTypeRole, (int) type);*/
484 #include "projectlist.moc"