]> git.sesse.net Git - kdenlive/blob - src/projectlist.cpp
Fix undo/redo delete clip
[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 "definitions.h"
45
46 #include <QtGui>
47
48 ProjectList::ProjectList(QWidget *parent)
49     : QWidget(parent), m_render(NULL), m_fps(-1), m_commandStack(NULL)
50 {
51
52   QWidget *vbox = new QWidget;
53   listView = new ProjectListView(this);;
54   QVBoxLayout *layout = new QVBoxLayout;
55   m_clipIdCounter = 0;
56
57   // setup toolbar
58   searchView = new KTreeWidgetSearchLine (this);
59   m_toolbar = new QToolBar("projectToolBar", this);
60   m_toolbar->addWidget (searchView);
61
62   QToolButton *addButton = new QToolButton( m_toolbar );
63   QMenu *addMenu = new QMenu(this);
64   addButton->setMenu( addMenu );
65   addButton->setPopupMode(QToolButton::MenuButtonPopup);
66   m_toolbar->addWidget (addButton);
67
68   QAction *addClipButton = addMenu->addAction (KIcon("document-new"), i18n("Add Clip"));
69   connect(addClipButton, SIGNAL(triggered()), this, SLOT(slotAddClip()));
70
71   QAction *addColorClip = addMenu->addAction (KIcon("document-new"), i18n("Add Color Clip"));
72   connect(addColorClip, SIGNAL(triggered()), this, SLOT(slotAddColorClip()));
73
74   m_deleteAction = m_toolbar->addAction (KIcon("edit-delete"), i18n("Delete Clip"));
75   connect(m_deleteAction, SIGNAL(triggered()), this, SLOT(slotRemoveClip()));
76
77   m_editAction = m_toolbar->addAction (KIcon("document-properties"), i18n("Edit Clip"));
78   connect(m_editAction, SIGNAL(triggered()), this, SLOT(slotEditClip()));
79
80   QAction *addFolderButton = addMenu->addAction (KIcon("folder-new"), i18n("Create Folder"));
81   connect(addFolderButton, SIGNAL(triggered()), this, SLOT(slotAddFolder()));
82
83   addButton->setDefaultAction( addClipButton );
84
85   layout->addWidget( m_toolbar );
86   layout->addWidget( listView );
87   setLayout( layout );
88   //m_toolbar->setEnabled(false);
89
90   searchView->setTreeWidget(listView);
91   listView->setColumnCount(3);
92   QStringList headers;
93   headers<<i18n("Thumbnail")<<i18n("Filename")<<i18n("Description");
94   listView->setHeaderLabels(headers);
95   listView->sortByColumn(1, Qt::AscendingOrder);
96
97   m_menu = new QMenu(); 
98   m_menu->addAction(addClipButton);
99   m_menu->addAction(addColorClip);
100   m_menu->addAction(m_editAction);
101   m_menu->addAction(m_deleteAction);
102   m_menu->addAction(addFolderButton);
103   m_menu->insertSeparator(m_deleteAction);
104
105   connect(listView, SIGNAL(itemSelectionChanged()), this, SLOT(slotClipSelected()));
106   connect(listView, SIGNAL(requestMenu ( const QPoint &, QTreeWidgetItem * )), this, SLOT(slotContextMenu(const QPoint &, QTreeWidgetItem *)));
107   connect(listView, SIGNAL(addClip ()), this, SLOT(slotAddClip()));
108   connect(listView, SIGNAL(addClip (QUrl, const QString &)), this, SLOT(slotAddClip(QUrl, const QString &)));
109   connect(listView, SIGNAL (itemChanged ( QTreeWidgetItem *, int )), this, SLOT(slotUpdateItemDescription(QTreeWidgetItem *, int )));
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::slotUpdateItemDescription( QTreeWidgetItem *item, int column)
135 {
136   if (column != 2) return;
137   ProjectItem *clip = (ProjectItem*) item;
138   CLIPTYPE type = clip->clipType(); 
139   if (type == AUDIO || type == VIDEO || type == AV || type == IMAGE || type == PLAYLIST) {
140     // Use Nepomuk system to store clip description
141     Nepomuk::Resource f( clip->clipUrl().path() );
142     f.setDescription(item->text(2));
143     kDebug()<<"NEPOMUK, SETTING CLIP: "<<clip->clipUrl().path()<<", TO TEXT: "<<item->text(2);
144   }
145 }
146
147 void ProjectList::slotEditClip()
148 {
149   kDebug()<<"////////////////////////////////////////   DBL CLK";
150 }
151
152
153 void ProjectList::slotEditClip(QTreeWidgetItem *item, int column)
154 {
155   kDebug()<<"////////////////////////////////////////   DBL CLK";
156 }
157
158 void ProjectList::slotContextMenu( const QPoint &pos, QTreeWidgetItem *item )
159 {
160   bool enable = false;
161   if (item) {
162     QFrame *w = new QFrame;
163     w->setFrameShape(QFrame::StyledPanel);
164     w->setLineWidth(2);
165     w->setAutoFillBackground(true);
166     QHBoxLayout *layout = new QHBoxLayout;
167     layout->addWidget( new QLabel(i18n("Color:")));
168     layout->addWidget( new KColorButton());
169     layout->addWidget( new QLabel(i18n("Duration:")));
170     layout->addWidget( new KRestrictedLine());
171     w->setLayout( layout );
172     m_listViewDelegate->extendItem(w, listView->currentIndex());
173     enable = true;
174   }
175   m_editAction->setEnabled(enable);
176   m_deleteAction->setEnabled(enable);
177
178   m_menu->popup(pos);
179 }
180
181 void ProjectList::slotRemoveClip()
182 {
183
184   if (!m_commandStack) kDebug()<<"!!!!!!!!!!!!!!!!  NO CMD STK";
185   if (!listView->currentItem()) return;
186   ProjectItem *item = ((ProjectItem *)listView->currentItem());
187   if (item->numReferences() > 0) {
188     if (KMessageBox::questionYesNo(this, i18n("Delete clip <b>%1</b> ?<br>This will also remove its %2 clips in timeline").arg(item->names().at(1)).arg(item->numReferences()), i18n("Delete Clip")) != KMessageBox::Yes) return;
189   }
190   m_doc->deleteProjectClip(item->clipId());
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     // if file has Nepomuk comment, use it
236     Nepomuk::Resource f( url.path() );
237     QString annotation = f.description();
238     if (!annotation.isEmpty()) item->setText(2, annotation);
239     QString resource = url.path();
240     if (resource.endsWith("westley") || resource.endsWith("kdenlive")) {
241         QString tmpfile;
242         QDomDocument doc;
243         if (KIO::NetAccess::download(url, tmpfile, 0)) {
244             QFile file(tmpfile);
245             if (file.open(QIODevice::ReadOnly)) {
246               doc.setContent(&file, false);
247               file.close();
248             }
249             KIO::NetAccess::removeTempFile(tmpfile);
250
251             QDomNodeList subProds = doc.elementsByTagName("producer");
252             int ct = subProds.count();
253             for (int i = 0; i <  ct ; i++)
254             {
255               QDomElement e = subProds.item(i).toElement();
256               if (!e.isNull()) {
257                 addProducer(e, clipId);
258               }
259             }  
260           }
261     }
262
263   }
264
265   if (elem.isNull() ) {
266     QDomDocument doc;
267     QDomElement element = doc.createElement("producer");
268     element.setAttribute("resource", url.path());
269     emit getFileProperties(element, clipId);
270   }
271   else emit getFileProperties(elem, clipId);
272   selectItemById(clipId);
273 }
274
275 void ProjectList::slotDeleteClip( int clipId)
276 {
277   ProjectItem *item = getItemById(clipId);
278   if (item) delete item;
279 }
280
281 void ProjectList::slotAddFolder()
282 {
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(DocClipBase *clip)
294 {
295   ProjectItem *item = new ProjectItem(listView, clip);
296   listView->setCurrentItem(item);
297   emit getFileProperties(clip->toXML(), clip->getId());
298 }
299
300 void ProjectList::slotUpdateClip(int id)
301 {
302   ProjectItem *item = getItemById(id);
303   item->setData(1, UsageRole, QString::number(item->numReferences()));
304 }
305
306 void ProjectList::slotAddClip(QUrl givenUrl, const QString &group)
307 {
308   if (!m_commandStack) kDebug()<<"!!!!!!!!!!!!!!!!  NO CMD STK";
309   KUrl::List list;
310   if (givenUrl.isEmpty())
311     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");
312   else list.append(givenUrl);
313   if (list.isEmpty()) return;
314   KUrl::List::Iterator it;
315
316   for (it = list.begin(); it != list.end(); it++) {
317       m_doc->slotAddClipFile(*it, group);
318   }
319 }
320
321 void ProjectList::slotAddColorClip()
322 {
323   if (!m_commandStack) kDebug()<<"!!!!!!!!!!!!!!!!  NO CMD STK";
324   QDialog *dia = new QDialog;
325   Ui::ColorClip_UI *dia_ui = new Ui::ColorClip_UI();
326   dia_ui->setupUi(dia);
327   dia_ui->clip_name->setText(i18n("Color Clip"));
328   dia_ui->clip_duration->setText(KdenliveSettings::color_duration());
329   if (dia->exec() == QDialog::Accepted)
330   {
331     QString color = dia_ui->clip_color->color().name();
332     color = color.replace(0, 1, "0x") + "ff";
333     m_doc->slotAddColorClipFile(dia_ui->clip_name->text(), color, dia_ui->clip_duration->text(), QString::null);
334   }
335   delete dia_ui;
336   delete dia;
337 }
338
339 void ProjectList::setDocument(KdenliveDoc *doc)
340 {
341   m_fps = doc->fps();
342   m_timecode = doc->timecode();
343   m_commandStack = doc->commandStack();
344   m_doc = doc;
345   QDomNodeList prods = doc->producersList();
346   int ct = prods.count();
347   kDebug()<<"////////////  SETTING DOC, FOUND CLIPS: "<<prods.count();
348   listView->clear();
349   for (int i = 0; i <  ct ; i++)
350   {
351     QDomElement e = prods.item(i).toElement();
352     kDebug()<<"// IMPORT: "<<i<<", :"<<e.attribute("id", "non")<<", NAME: "<<e.attribute("name", "non");
353     if (!e.isNull()) addProducer(e);
354   }
355   QTreeWidgetItem *first = listView->topLevelItem(0);
356   if (first) listView->setCurrentItem(first);
357   m_toolbar->setEnabled(true);
358 }
359
360 QDomElement ProjectList::producersList()
361 {
362   QDomDocument doc;
363   QDomElement prods = doc.createElement("producerlist");
364   doc.appendChild(prods);
365   kDebug()<<"////////////  PRO LIST BUILD PRDSLIST ";
366     QTreeWidgetItemIterator it(listView);
367      while (*it) {
368          if (!((ProjectItem *)(*it))->isGroup())
369           prods.appendChild(doc.importNode(((ProjectItem *)(*it))->toXml(), true));
370          ++it;
371      }
372   return prods;
373 }
374
375
376 void ProjectList::slotReplyGetFileProperties(int clipId, const QMap < QString, QString > &properties, const QMap < QString, QString > &metadata)
377 {
378   ProjectItem *item = getItemById(clipId);
379   if (item) {
380     item->setProperties(properties, metadata);
381     emit receivedClipDuration(clipId, item->clipMaxDuration());
382   }
383 }
384
385
386
387 void ProjectList::slotReplyGetImage(int clipId, int pos, const QPixmap &pix, int w, int h)
388 {
389   ProjectItem *item = getItemById(clipId);
390   if (item) item->setIcon(0, pix);
391 }
392
393 ProjectItem *ProjectList::getItemById(int id)
394 {
395     QTreeWidgetItemIterator it(listView);
396      while (*it) {
397          if (((ProjectItem *)(*it))->clipId() == id)
398           break;
399          ++it;
400      }
401   return ((ProjectItem *)(*it));
402 }
403
404
405 void ProjectList::addProducer(QDomElement producer, int parentId)
406 {
407   if (!m_commandStack) kDebug()<<"!!!!!!!!!!!!!!!!  NO CMD STK";
408   CLIPTYPE type = (CLIPTYPE) producer.attribute("type").toInt();
409
410     /*QDomDocument doc;
411     QDomElement prods = doc.createElement("list");
412     doc.appendChild(prods);
413     prods.appendChild(doc.importNode(producer, true));*/
414     
415
416   //kDebug()<<"//////  ADDING PRODUCER:\n "<<doc.toString()<<"\n+++++++++++++++++";
417   int id = producer.attribute("id").toInt();
418   QString groupName = producer.attribute("group");
419   if (id >= m_clipIdCounter) m_clipIdCounter = id + 1;
420   else if (id == 0) id = m_clipIdCounter++;
421
422   if (parentId != -1) {
423     // item is a westley playlist, adjust subproducers ids 
424     id = (parentId + 1) * 10000 + id;
425   }
426   if (type == AUDIO || type == VIDEO || type == AV || type == IMAGE  || type == PLAYLIST)
427   {
428     KUrl resource = KUrl(producer.attribute("resource"));
429     if (!resource.isEmpty()) {
430       QStringList itemEntry;
431       itemEntry.append(QString::null);
432       itemEntry.append(resource.fileName());
433       addClip(itemEntry, producer, id, resource, groupName, parentId);  
434     }
435   }
436   else if (type == COLOR) {
437     QString colour = producer.attribute("colour");
438     QPixmap pix(60, 40);
439     colour = colour.replace(0, 2, "#");
440     pix.fill(QColor(colour.left(7)));
441     QStringList itemEntry;
442     itemEntry.append(QString::null);
443     itemEntry.append(producer.attribute("name", i18n("Color clip")));
444     addClip(itemEntry, producer, id, KUrl(), groupName, parentId);
445   }
446       
447 }
448
449 #include "projectlist.moc"