]> git.sesse.net Git - kdenlive/blob - src/projectlist.cpp
adapt to latest MLT changes
[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
111   m_listViewDelegate = new ItemDelegate(listView);
112   listView->setItemDelegate(m_listViewDelegate);
113   listView->setIconSize(QSize(60, 40));
114   listView->setSortingEnabled (true);
115 }
116
117 ProjectList::~ProjectList()
118 {
119   delete m_menu;
120   delete m_toolbar;
121 }
122
123 void ProjectList::setRenderer(Render *projectRender)
124 {
125   m_render = projectRender;
126 }
127
128 void ProjectList::slotClipSelected()
129 {
130   ProjectItem *item = (ProjectItem*) listView->currentItem();
131   if (item && !item->isGroup()) emit clipSelected(item->toXml());
132 }
133
134 void ProjectList::slotEditClip()
135 {
136   kDebug()<<"////////////////////////////////////////   DBL CLK";
137 }
138
139
140 void ProjectList::slotEditClip(QTreeWidgetItem *item, int column)
141 {
142   kDebug()<<"////////////////////////////////////////   DBL CLK";
143 }
144
145 void ProjectList::slotContextMenu( const QPoint &pos, QTreeWidgetItem *item )
146 {
147   bool enable = false;
148   if (item) {
149     QFrame *w = new QFrame;
150     w->setFrameShape(QFrame::StyledPanel);
151     w->setLineWidth(2);
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());
160     enable = true;
161   }
162   m_editAction->setEnabled(enable);
163   m_deleteAction->setEnabled(enable);
164
165   m_menu->popup(pos);
166 }
167
168 void ProjectList::slotRemoveClip()
169 {
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);
177 }
178
179 void ProjectList::selectItemById(const int clipId)
180 {
181   ProjectItem *item = getItemById(clipId);
182   if (item) listView->setCurrentItem(item);
183 }
184
185 void ProjectList::addClip(const QStringList &name, const QDomElement &elem, const int clipId, const KUrl &url, const QString &group, int parentId)
186 {
187   kDebug()<<"/////////  ADDING VCLIP=: "<<name;
188   ProjectItem *item;
189   ProjectItem *groupItem = NULL;
190   QString groupName;
191   if (group.isEmpty()) groupName = elem.attribute("group", QString::null);
192   else groupName = group;
193   if (elem.isNull() && url.isEmpty()) {
194     // this is a folder
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);
199     }
200     return;
201   }
202
203   if (parentId != -1) {
204     groupItem = getItemById(parentId);
205   }
206   else if (!groupName.isEmpty()) {
207     // Clip is in a group
208     QList<QTreeWidgetItem *> groupList = listView->findItems(groupName, Qt::MatchExactly, 1);
209
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++);
215     }
216     else groupItem = (ProjectItem *) groupList.first();
217   }
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")) {
228         QString tmpfile;
229         QDomDocument doc;
230         if (KIO::NetAccess::download(url, tmpfile, 0)) {
231             QFile file(tmpfile);
232             if (file.open(QIODevice::ReadOnly)) {
233               doc.setContent(&file, false);
234               file.close();
235             }
236             KIO::NetAccess::removeTempFile(tmpfile);
237
238             QDomNodeList subProds = doc.elementsByTagName("producer");
239             int ct = subProds.count();
240             for (int i = 0; i <  ct ; i++)
241             {
242               QDomElement e = subProds.item(i).toElement();
243               if (!e.isNull()) {
244                 addProducer(e, clipId);
245               }
246             }  
247           }
248     }
249
250   }
251
252   if (elem.isNull() ) {
253     QDomDocument doc;
254     QDomElement element = doc.createElement("producer");
255     element.setAttribute("resource", url.path());
256     emit getFileProperties(element, clipId);
257   }
258   else emit getFileProperties(elem, clipId);
259   selectItemById(clipId);
260 }
261
262 void ProjectList::deleteClip(const int clipId)
263 {
264   ProjectItem *item = getItemById(clipId);
265   if (item) delete item;
266 }
267
268 void ProjectList::slotAddFolder()
269 {
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);
277 }
278
279 void ProjectList::slotAddClip(QUrl givenUrl, const QString &group)
280 {
281   if (!m_commandStack) kDebug()<<"!!!!!!!!!!!!!!!!  NO CMD STK";
282   KUrl::List list;
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;
288   KUrl url;
289 //  ProjectItem *item;
290
291   for (it = list.begin(); it != list.end(); it++) {
292       QStringList itemEntry;
293       QDomDocument doc;
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));
303       }
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());
308   }
309   //listView->setCurrentItem(item);
310 }
311
312 void ProjectList::slotAddColorClip()
313 {
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)
321   {
322     QDomDocument doc;
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);
342     
343   }
344   delete dia_ui;
345   delete dia;
346 }
347
348 void ProjectList::setDocument(KdenliveDoc *doc)
349 {
350   m_fps = doc->fps();
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();
356   listView->clear();
357   for (int i = 0; i <  ct ; i++)
358   {
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);
362   }
363   QTreeWidgetItem *first = listView->topLevelItem(0);
364   if (first) listView->setCurrentItem(first);
365   m_toolbar->setEnabled(true);
366 }
367
368 QDomElement ProjectList::producersList()
369 {
370   QDomDocument doc;
371   QDomElement prods = doc.createElement("producerlist");
372   doc.appendChild(prods);
373   kDebug()<<"////////////  PRO LIST BUILD PRDSLIST ";
374     QTreeWidgetItemIterator it(listView);
375      while (*it) {
376          if (!((ProjectItem *)(*it))->isGroup())
377           prods.appendChild(doc.importNode(((ProjectItem *)(*it))->toXml(), true));
378          ++it;
379      }
380   return prods;
381 }
382
383
384 void ProjectList::slotReplyGetFileProperties(int clipId, const QMap < QString, QString > &properties, const QMap < QString, QString > &metadata)
385 {
386   ProjectItem *item = getItemById(clipId);
387   if (item) {
388     item->setProperties(properties, metadata);
389     emit receivedClipDuration(clipId, item->clipMaxDuration());
390   }
391 }
392
393
394
395 void ProjectList::slotReplyGetImage(int clipId, int pos, const QPixmap &pix, int w, int h)
396 {
397   ProjectItem *item = getItemById(clipId);
398   if (item) item->setIcon(0, pix);
399 }
400
401 ProjectItem *ProjectList::getItemById(int id)
402 {
403     QTreeWidgetItemIterator it(listView);
404      while (*it) {
405          if (((ProjectItem *)(*it))->clipId() == id)
406           break;
407          ++it;
408      }
409   return ((ProjectItem *)(*it));
410 }
411
412
413 void ProjectList::addProducer(QDomElement producer, int parentId)
414 {
415   if (!m_commandStack) kDebug()<<"!!!!!!!!!!!!!!!!  NO CMD STK";
416   CLIPTYPE type = (CLIPTYPE) producer.attribute("type").toInt();
417
418     /*QDomDocument doc;
419     QDomElement prods = doc.createElement("list");
420     doc.appendChild(prods);
421     prods.appendChild(doc.importNode(producer, true));*/
422     
423
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++;
429
430   if (parentId != -1) {
431     // item is a westley playlist, adjust subproducers ids 
432     id = (parentId + 1) * 10000 + id;
433   }
434   if (type == AUDIO || type == VIDEO || type == AV || type == IMAGE  || type == PLAYLIST)
435   {
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);*/
444
445
446       /*ProjectItem *item = new ProjectItem(listView, itemEntry, producer);
447       item->setData(1, FullPathRole, resource.path());
448       item->setData(1, ClipTypeRole, (int) type);*/
449       
450     }
451   }
452   else if (type == COLOR) {
453     QString colour = producer.attribute("colour");
454     QPixmap pix(60, 40);
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);*/
466   }
467       
468 }
469
470 #include "projectlist.moc"