]> git.sesse.net Git - kdenlive/blob - src/projectlist.cpp
026e5b29d1ac91c657edf701166148fe31a0b434
[kdenlive] / src / projectlist.cpp
1 /***************************************************************************
2  *   Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
3  *                                                                         *
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.                                   *
8  *                                                                         *
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.                          *
13  *                                                                         *
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  ***************************************************************************/
19
20
21 #include <QMouseEvent>
22 #include <QStylePainter>
23 #include <QPixmap>
24 #include <QIcon>
25 #include <QDialog>
26
27 #include <KDebug>
28 #include <KAction>
29 #include <KLocale>
30 #include <KFileDialog>
31 #include <KInputDialog>
32 #include <kio/netaccess.h>
33 #include <KMessageBox>
34
35 #include <nepomuk/global.h>
36 #include <nepomuk/resource.h>
37 #include <nepomuk/tag.h>
38
39 #include "projectlist.h"
40 #include "projectitem.h"
41 #include "kdenlivesettings.h"
42 #include "ui_colorclip_ui.h"
43
44 #include "addclipcommand.h"
45 #include "definitions.h"
46
47 #include <QtGui>
48
49 ProjectList::ProjectList(QWidget *parent)
50     : QWidget(parent), m_render(NULL), m_fps(-1), m_commandStack(NULL)
51 {
52
53   QWidget *vbox = new QWidget;
54   listView = new ProjectListView(this);;
55   QVBoxLayout *layout = new QVBoxLayout;
56   m_clipIdCounter = 0;
57
58   // setup toolbar
59   searchView = new KTreeWidgetSearchLine (this);
60   m_toolbar = new QToolBar("projectToolBar", this);
61   m_toolbar->addWidget (searchView);
62
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);
68
69   QAction *addClipButton = addMenu->addAction (KIcon("document-new"), i18n("Add Clip"));
70   connect(addClipButton, SIGNAL(triggered()), this, SLOT(slotAddClip()));
71
72   QAction *addColorClip = addMenu->addAction (KIcon("document-new"), i18n("Add Color Clip"));
73   connect(addColorClip, SIGNAL(triggered()), this, SLOT(slotAddColorClip()));
74
75   m_deleteAction = m_toolbar->addAction (KIcon("edit-delete"), i18n("Delete Clip"));
76   connect(m_deleteAction, SIGNAL(triggered()), this, SLOT(slotRemoveClip()));
77
78   m_editAction = m_toolbar->addAction (KIcon("document-properties"), i18n("Edit Clip"));
79   connect(m_editAction, SIGNAL(triggered()), this, SLOT(slotEditClip()));
80
81   QAction *addFolderButton = addMenu->addAction (KIcon("folder-new"), i18n("Create Folder"));
82   connect(addFolderButton, SIGNAL(triggered()), this, SLOT(slotAddFolder()));
83
84   addButton->setDefaultAction( addClipButton );
85
86   layout->addWidget( m_toolbar );
87   layout->addWidget( listView );
88   setLayout( layout );
89   m_toolbar->setEnabled(false);
90
91   searchView->setTreeWidget(listView);
92   listView->setColumnCount(3);
93   QStringList headers;
94   headers<<i18n("Thumbnail")<<i18n("Filename")<<i18n("Description");
95   listView->setHeaderLabels(headers);
96   listView->sortByColumn(1, Qt::AscendingOrder);
97
98   m_menu = new QMenu(); 
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);
105
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 )));
111
112   m_listViewDelegate = new ItemDelegate(listView);
113   listView->setItemDelegate(m_listViewDelegate);
114   listView->setIconSize(QSize(60, 40));
115   listView->setSortingEnabled (true);
116 }
117
118 ProjectList::~ProjectList()
119 {
120   delete m_menu;
121   delete m_toolbar;
122 }
123
124 void ProjectList::setRenderer(Render *projectRender)
125 {
126   m_render = projectRender;
127 }
128
129 void ProjectList::slotClipSelected()
130 {
131   ProjectItem *item = (ProjectItem*) listView->currentItem();
132   if (item && !item->isGroup()) emit clipSelected(item->toXml());
133 }
134
135 void ProjectList::slotUpdateItemDescription( QTreeWidgetItem *item, int column)
136 {
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);
145   }
146 }
147
148 void ProjectList::slotEditClip()
149 {
150   kDebug()<<"////////////////////////////////////////   DBL CLK";
151 }
152
153
154 void ProjectList::slotEditClip(QTreeWidgetItem *item, int column)
155 {
156   kDebug()<<"////////////////////////////////////////   DBL CLK";
157 }
158
159 void ProjectList::slotContextMenu( const QPoint &pos, QTreeWidgetItem *item )
160 {
161   bool enable = false;
162   if (item) {
163     QFrame *w = new QFrame;
164     w->setFrameShape(QFrame::StyledPanel);
165     w->setLineWidth(2);
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());
174     enable = true;
175   }
176   m_editAction->setEnabled(enable);
177   m_deleteAction->setEnabled(enable);
178
179   m_menu->popup(pos);
180 }
181
182 void ProjectList::slotRemoveClip()
183 {
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);
191 }
192
193 void ProjectList::selectItemById(const int clipId)
194 {
195   ProjectItem *item = getItemById(clipId);
196   if (item) listView->setCurrentItem(item);
197 }
198
199 void ProjectList::addClip(const QStringList &name, const QDomElement &elem, const int clipId, const KUrl &url, const QString &group, int parentId)
200 {
201   kDebug()<<"/////////  ADDING VCLIP=: "<<name;
202   ProjectItem *item;
203   ProjectItem *groupItem = NULL;
204   QString groupName;
205   if (group.isEmpty()) groupName = elem.attribute("group", QString::null);
206   else groupName = group;
207   if (elem.isNull() && url.isEmpty()) {
208     // this is a folder
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);
213     }
214     return;
215   }
216
217   if (parentId != -1) {
218     groupItem = getItemById(parentId);
219   }
220   else if (!groupName.isEmpty()) {
221     // Clip is in a group
222     QList<QTreeWidgetItem *> groupList = listView->findItems(groupName, Qt::MatchExactly, 1);
223
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++);
229     }
230     else groupItem = (ProjectItem *) groupList.first();
231   }
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")) {
242         QString tmpfile;
243         QDomDocument doc;
244         if (KIO::NetAccess::download(url, tmpfile, 0)) {
245             QFile file(tmpfile);
246             if (file.open(QIODevice::ReadOnly)) {
247               doc.setContent(&file, false);
248               file.close();
249             }
250             KIO::NetAccess::removeTempFile(tmpfile);
251
252             QDomNodeList subProds = doc.elementsByTagName("producer");
253             int ct = subProds.count();
254             for (int i = 0; i <  ct ; i++)
255             {
256               QDomElement e = subProds.item(i).toElement();
257               if (!e.isNull()) {
258                 addProducer(e, clipId);
259               }
260             }  
261           }
262     }
263
264   }
265
266   if (elem.isNull() ) {
267     QDomDocument doc;
268     QDomElement element = doc.createElement("producer");
269     element.setAttribute("resource", url.path());
270     emit getFileProperties(element, clipId);
271   }
272   else emit getFileProperties(elem, clipId);
273   selectItemById(clipId);
274 }
275
276 void ProjectList::deleteClip(const int clipId)
277 {
278   ProjectItem *item = getItemById(clipId);
279   if (item) delete item;
280 }
281
282 void ProjectList::slotAddFolder()
283 {
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);
291 }
292
293 void ProjectList::slotAddClip(QUrl givenUrl, const QString &group)
294 {
295   if (!m_commandStack) kDebug()<<"!!!!!!!!!!!!!!!!  NO CMD STK";
296   KUrl::List list;
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;
302   KUrl url;
303 //  ProjectItem *item;
304
305   for (it = list.begin(); it != list.end(); it++) {
306       QStringList itemEntry;
307       QDomDocument doc;
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));
317       }
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());
322   }
323   //listView->setCurrentItem(item);
324 }
325
326 void ProjectList::slotAddColorClip()
327 {
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)
335   {
336     QDomDocument doc;
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);
356     
357   }
358   delete dia_ui;
359   delete dia;
360 }
361
362 void ProjectList::setDocument(KdenliveDoc *doc)
363 {
364   m_fps = doc->fps();
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();
370   listView->clear();
371   for (int i = 0; i <  ct ; i++)
372   {
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);
376   }
377   QTreeWidgetItem *first = listView->topLevelItem(0);
378   if (first) listView->setCurrentItem(first);
379   m_toolbar->setEnabled(true);
380 }
381
382 QDomElement ProjectList::producersList()
383 {
384   QDomDocument doc;
385   QDomElement prods = doc.createElement("producerlist");
386   doc.appendChild(prods);
387   kDebug()<<"////////////  PRO LIST BUILD PRDSLIST ";
388     QTreeWidgetItemIterator it(listView);
389      while (*it) {
390          if (!((ProjectItem *)(*it))->isGroup())
391           prods.appendChild(doc.importNode(((ProjectItem *)(*it))->toXml(), true));
392          ++it;
393      }
394   return prods;
395 }
396
397
398 void ProjectList::slotReplyGetFileProperties(int clipId, const QMap < QString, QString > &properties, const QMap < QString, QString > &metadata)
399 {
400   ProjectItem *item = getItemById(clipId);
401   if (item) {
402     item->setProperties(properties, metadata);
403     emit receivedClipDuration(clipId, item->clipMaxDuration());
404   }
405 }
406
407
408
409 void ProjectList::slotReplyGetImage(int clipId, int pos, const QPixmap &pix, int w, int h)
410 {
411   ProjectItem *item = getItemById(clipId);
412   if (item) item->setIcon(0, pix);
413 }
414
415 ProjectItem *ProjectList::getItemById(int id)
416 {
417     QTreeWidgetItemIterator it(listView);
418      while (*it) {
419          if (((ProjectItem *)(*it))->clipId() == id)
420           break;
421          ++it;
422      }
423   return ((ProjectItem *)(*it));
424 }
425
426
427 void ProjectList::addProducer(QDomElement producer, int parentId)
428 {
429   if (!m_commandStack) kDebug()<<"!!!!!!!!!!!!!!!!  NO CMD STK";
430   CLIPTYPE type = (CLIPTYPE) producer.attribute("type").toInt();
431
432     /*QDomDocument doc;
433     QDomElement prods = doc.createElement("list");
434     doc.appendChild(prods);
435     prods.appendChild(doc.importNode(producer, true));*/
436     
437
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++;
443
444   if (parentId != -1) {
445     // item is a westley playlist, adjust subproducers ids 
446     id = (parentId + 1) * 10000 + id;
447   }
448   if (type == AUDIO || type == VIDEO || type == AV || type == IMAGE  || type == PLAYLIST)
449   {
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);*/
458
459
460       /*ProjectItem *item = new ProjectItem(listView, itemEntry, producer);
461       item->setData(1, FullPathRole, resource.path());
462       item->setData(1, ClipTypeRole, (int) type);*/
463       
464     }
465   }
466   else if (type == COLOR) {
467     QString colour = producer.attribute("colour");
468     QPixmap pix(60, 40);
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);*/
480   }
481       
482 }
483
484 #include "projectlist.moc"