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 &)));
111 m_listViewDelegate = new ItemDelegate(listView);
112 listView->setItemDelegate(m_listViewDelegate);
113 listView->setIconSize(QSize(60, 40));
114 listView->setSortingEnabled (true);
117 ProjectList::~ProjectList()
123 void ProjectList::setRenderer(Render *projectRender)
125 m_render = projectRender;
128 void ProjectList::slotClipSelected()
130 ProjectItem *item = (ProjectItem*) listView->currentItem();
131 if (item && !item->isGroup()) emit clipSelected(item->toXml());
134 void ProjectList::slotEditClip()
136 kDebug()<<"//////////////////////////////////////// DBL CLK";
140 void ProjectList::slotEditClip(QTreeWidgetItem *item, int column)
142 kDebug()<<"//////////////////////////////////////// DBL CLK";
145 void ProjectList::slotContextMenu( const QPoint &pos, QTreeWidgetItem *item )
149 QFrame *w = new QFrame;
150 w->setFrameShape(QFrame::StyledPanel);
152 w->setAutoFillBackground(true);
153 QHBoxLayout *layout = new QHBoxLayout;
154 layout->addWidget( new QLabel(i18n("Color:")));
155 layout->addWidget( new KColorButton());
156 layout->addWidget( new QLabel(i18n("Duration:")));
157 layout->addWidget( new KRestrictedLine());
158 w->setLayout( layout );
159 m_listViewDelegate->extendItem(w, listView->currentIndex());
162 m_editAction->setEnabled(enable);
163 m_deleteAction->setEnabled(enable);
168 void ProjectList::slotRemoveClip()
170 if (!m_commandStack) kDebug()<<"!!!!!!!!!!!!!!!! NO CMD STK";
171 if (!listView->currentItem()) return;
172 ProjectItem *item = ((ProjectItem *)listView->currentItem());
173 if (!item) kDebug()<<"/////////////// ERROR NOT FOUND";
174 if (KMessageBox::questionYesNo(this, i18n("Delete clip <b>%1</b> ?").arg(item->names().at(1)), i18n("Delete Clip")) != KMessageBox::Yes) return;
175 AddClipCommand *command = new AddClipCommand(this, item->names(), item->toXml(), item->clipId(), item->clipUrl(), item->groupName(), false);
176 m_commandStack->push(command);
179 void ProjectList::selectItemById(const int clipId)
181 ProjectItem *item = getItemById(clipId);
182 if (item) listView->setCurrentItem(item);
185 void ProjectList::addClip(const QStringList &name, const QDomElement &elem, const int clipId, const KUrl &url, const QString &group, int parentId)
187 kDebug()<<"///////// ADDING VCLIP=: "<<name;
189 ProjectItem *groupItem = NULL;
191 if (group.isEmpty()) groupName = elem.attribute("group", QString::null);
192 else groupName = group;
193 if (elem.isNull() && url.isEmpty()) {
195 groupName = name.at(1);
196 QList<QTreeWidgetItem *> groupList = listView->findItems(groupName, Qt::MatchExactly, 1);
197 if (groupList.isEmpty()) {
198 (void) new ProjectItem(listView, name, clipId);
203 if (parentId != -1) {
204 groupItem = getItemById(parentId);
206 else if (!groupName.isEmpty()) {
207 // Clip is in a group
208 QList<QTreeWidgetItem *> groupList = listView->findItems(groupName, Qt::MatchExactly, 1);
210 if (groupList.isEmpty()) {
211 QStringList itemName;
212 itemName<<QString::null<<groupName;
213 kDebug()<<"------- CREATING NEW GRP: "<<itemName;
214 groupItem = new ProjectItem(listView, itemName, m_clipIdCounter++);
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 item->setData(1, FullPathRole, url.path());
222 // if file has Nepomuk comment, use it
223 Nepomuk::Resource f( url.path() );
224 QString annotation = f.description();
225 if (!annotation.isEmpty()) item->setText(2, annotation);
226 QString resource = url.path();
227 if (resource.endsWith("westley") || resource.endsWith("kdenlive")) {
230 if (KIO::NetAccess::download(url, tmpfile, 0)) {
232 if (file.open(QIODevice::ReadOnly)) {
233 doc.setContent(&file, false);
236 KIO::NetAccess::removeTempFile(tmpfile);
238 QDomNodeList subProds = doc.elementsByTagName("producer");
239 int ct = subProds.count();
240 for (int i = 0; i < ct ; i++)
242 QDomElement e = subProds.item(i).toElement();
244 addProducer(e, clipId);
252 if (elem.isNull() ) {
254 QDomElement element = doc.createElement("producer");
255 element.setAttribute("resource", url.path());
256 emit getFileProperties(element, clipId);
258 else emit getFileProperties(elem, clipId);
259 selectItemById(clipId);
262 void ProjectList::deleteClip(const int clipId)
264 ProjectItem *item = getItemById(clipId);
265 if (item) delete item;
268 void ProjectList::slotAddFolder()
270 QString folderName = KInputDialog::getText(i18n("New Folder"), i18n("Enter new folder name: "));
271 if (folderName.isEmpty()) return;
272 QStringList itemEntry;
273 itemEntry.append(QString::null);
274 itemEntry.append(folderName);
275 AddClipCommand *command = new AddClipCommand(this, itemEntry, QDomElement(), m_clipIdCounter++, KUrl(), folderName, true);
276 m_commandStack->push(command);
279 void ProjectList::slotAddClip(QUrl givenUrl, const QString &group)
281 if (!m_commandStack) kDebug()<<"!!!!!!!!!!!!!!!! NO CMD STK";
283 if (givenUrl.isEmpty())
284 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");
285 else list.append(givenUrl);
286 if (list.isEmpty()) return;
287 KUrl::List::Iterator it;
289 // ProjectItem *item;
291 for (it = list.begin(); it != list.end(); it++) {
292 QStringList itemEntry;
294 QDomElement prod = doc.createElement("producer");
295 prod.setAttribute("resource", (*it).path());
296 itemEntry.append(QString::null);
297 itemEntry.append((*it).fileName());
298 KMimeType::Ptr type = KMimeType::findByUrl(*it);
299 if (type->name().startsWith("image/")) {
300 prod.setAttribute("type", (int) IMAGE);
301 prod.setAttribute("in", "0");
302 prod.setAttribute("out", m_timecode.getFrameCount(KdenliveSettings::image_duration(), m_fps));
304 AddClipCommand *command = new AddClipCommand(this, itemEntry, prod, m_clipIdCounter++, *it, group, true);
305 m_commandStack->push(command);
306 //item = new ProjectItem(listView, itemEntry, QDomElement());
307 //item->setData(1, FullPathRole, (*it).path());
309 //listView->setCurrentItem(item);
312 void ProjectList::slotAddColorClip()
314 if (!m_commandStack) kDebug()<<"!!!!!!!!!!!!!!!! NO CMD STK";
315 QDialog *dia = new QDialog;
316 Ui::ColorClip_UI *dia_ui = new Ui::ColorClip_UI();
317 dia_ui->setupUi(dia);
318 dia_ui->clip_name->setText(i18n("Color Clip"));
319 dia_ui->clip_duration->setText(KdenliveSettings::color_duration());
320 if (dia->exec() == QDialog::Accepted)
323 QDomElement element = doc.createElement("producer");
324 element.setAttribute("mlt_service", "colour");
325 QString color = dia_ui->clip_color->color().name();
326 color = color.replace(0, 1, "0x") + "ff";
327 element.setAttribute("colour", color);
328 element.setAttribute("type", (int) COLOR);
329 element.setAttribute("in", "0");
330 element.setAttribute("out", m_timecode.getFrameCount(dia_ui->clip_duration->text(), m_fps));
331 element.setAttribute("name", dia_ui->clip_name->text());
332 QStringList itemEntry;
333 itemEntry.append(QString::null);
334 itemEntry.append(dia_ui->clip_name->text());
335 AddClipCommand *command = new AddClipCommand(this, itemEntry, element, m_clipIdCounter++, KUrl(), QString::null, true);
336 m_commandStack->push(command);
337 // ProjectItem *item = new ProjectItem(listView, itemEntry, element, m_clipIdCounter++);
338 /*QPixmap pix(60, 40);
339 pix.fill(dia_ui->clip_color->color());
340 item->setIcon(0, QIcon(pix));*/
341 //listView->setCurrentItem(item);
348 void ProjectList::setDocument(KdenliveDoc *doc)
351 m_timecode = doc->timecode();
352 m_commandStack = doc->commandStack();
353 QDomNodeList prods = doc->producersList();
354 int ct = prods.count();
355 kDebug()<<"//////////// SETTING DOC, FOUND CLIPS: "<<prods.count();
357 for (int i = 0; i < ct ; i++)
359 QDomElement e = prods.item(i).toElement();
360 kDebug()<<"// IMPORT: "<<i<<", :"<<e.attribute("id", "non")<<", NAME: "<<e.attribute("name", "non");
361 if (!e.isNull()) addProducer(e);
363 QTreeWidgetItem *first = listView->topLevelItem(0);
364 if (first) listView->setCurrentItem(first);
365 m_toolbar->setEnabled(true);
368 QDomElement ProjectList::producersList()
371 QDomElement prods = doc.createElement("producerlist");
372 doc.appendChild(prods);
373 kDebug()<<"//////////// PRO LIST BUILD PRDSLIST ";
374 QTreeWidgetItemIterator it(listView);
376 if (!((ProjectItem *)(*it))->isGroup())
377 prods.appendChild(doc.importNode(((ProjectItem *)(*it))->toXml(), true));
384 void ProjectList::slotReplyGetFileProperties(int clipId, const QMap < QString, QString > &properties, const QMap < QString, QString > &metadata)
386 ProjectItem *item = getItemById(clipId);
388 item->setProperties(properties, metadata);
389 emit receivedClipDuration(clipId, item->clipMaxDuration());
395 void ProjectList::slotReplyGetImage(int clipId, int pos, const QPixmap &pix, int w, int h)
397 ProjectItem *item = getItemById(clipId);
398 if (item) item->setIcon(0, pix);
401 ProjectItem *ProjectList::getItemById(int id)
403 QTreeWidgetItemIterator it(listView);
405 if (((ProjectItem *)(*it))->clipId() == id)
409 return ((ProjectItem *)(*it));
413 void ProjectList::addProducer(QDomElement producer, int parentId)
415 if (!m_commandStack) kDebug()<<"!!!!!!!!!!!!!!!! NO CMD STK";
416 CLIPTYPE type = (CLIPTYPE) producer.attribute("type").toInt();
419 QDomElement prods = doc.createElement("list");
420 doc.appendChild(prods);
421 prods.appendChild(doc.importNode(producer, true));*/
424 //kDebug()<<"////// ADDING PRODUCER:\n "<<doc.toString()<<"\n+++++++++++++++++";
425 int id = producer.attribute("id").toInt();
426 QString groupName = producer.attribute("group");
427 if (id >= m_clipIdCounter) m_clipIdCounter = id + 1;
428 else if (id == 0) id = m_clipIdCounter++;
430 if (parentId != -1) {
431 // item is a westley playlist, adjust subproducers ids
432 id = (parentId + 1) * 10000 + id;
434 if (type == AUDIO || type == VIDEO || type == AV || type == IMAGE || type == PLAYLIST)
436 KUrl resource = KUrl(producer.attribute("resource"));
437 if (!resource.isEmpty()) {
438 QStringList itemEntry;
439 itemEntry.append(QString::null);
440 itemEntry.append(resource.fileName());
441 addClip(itemEntry, producer, id, resource, groupName, parentId);
442 /*AddClipCommand *command = new AddClipCommand(this, itemEntry, producer, id, resource, true);
443 m_commandStack->push(command);*/
446 /*ProjectItem *item = new ProjectItem(listView, itemEntry, producer);
447 item->setData(1, FullPathRole, resource.path());
448 item->setData(1, ClipTypeRole, (int) type);*/
452 else if (type == COLOR) {
453 QString colour = producer.attribute("colour");
455 colour = colour.replace(0, 2, "#");
456 pix.fill(QColor(colour.left(7)));
457 QStringList itemEntry;
458 itemEntry.append(QString::null);
459 itemEntry.append(producer.attribute("name", i18n("Color clip")));
460 addClip(itemEntry, producer, id, KUrl(), groupName, parentId);
461 /*AddClipCommand *command = new AddClipCommand(this, itemEntry, producer, id, KUrl(), true);
462 m_commandStack->push(command);*/
463 //ProjectItem *item = new ProjectItem(listView, itemEntry, producer);
464 /*item->setIcon(0, QIcon(pix));
465 item->setData(1, ClipTypeRole, (int) type);*/
470 #include "projectlist.moc"