]> git.sesse.net Git - kdenlive/blob - src/projectlist.cpp
08c1582fb37fc99a3098ddc12da4f37df4cf9525
[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 #include "titlewidget.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         QAction *addTitleClip = addMenu->addAction (KIcon("document-new"), i18n("Add Title Clip"));
76         connect(addTitleClip, SIGNAL(triggered()), this, SLOT(slotAddTitleClip()));
77
78   m_deleteAction = m_toolbar->addAction (KIcon("edit-delete"), i18n("Delete Clip"));
79   connect(m_deleteAction, SIGNAL(triggered()), this, SLOT(slotRemoveClip()));
80
81   m_editAction = m_toolbar->addAction (KIcon("document-properties"), i18n("Edit Clip"));
82   connect(m_editAction, SIGNAL(triggered()), this, SLOT(slotEditClip()));
83
84   QAction *addFolderButton = addMenu->addAction (KIcon("folder-new"), i18n("Create Folder"));
85   connect(addFolderButton, SIGNAL(triggered()), this, SLOT(slotAddFolder()));
86
87   addButton->setDefaultAction( addClipButton );
88
89   layout->addWidget( m_toolbar );
90   layout->addWidget( listView );
91   setLayout( layout );
92   //m_toolbar->setEnabled(false);
93
94   searchView->setTreeWidget(listView);
95   listView->setColumnCount(3);
96   QStringList headers;
97   headers<<i18n("Thumbnail")<<i18n("Filename")<<i18n("Description");
98   listView->setHeaderLabels(headers);
99   listView->sortByColumn(1, Qt::AscendingOrder);
100
101   m_menu = new QMenu(); 
102   m_menu->addAction(addClipButton);
103   m_menu->addAction(addColorClip);
104   m_menu->addAction(addTitleClip);
105   m_menu->addAction(m_editAction);
106   m_menu->addAction(m_deleteAction);
107   m_menu->addAction(addFolderButton);
108   m_menu->insertSeparator(m_deleteAction);
109
110   connect(listView, SIGNAL(itemSelectionChanged()), this, SLOT(slotClipSelected()));
111   connect(listView, SIGNAL(requestMenu ( const QPoint &, QTreeWidgetItem * )), this, SLOT(slotContextMenu(const QPoint &, QTreeWidgetItem *)));
112   connect(listView, SIGNAL(addClip ()), this, SLOT(slotAddClip()));
113   connect(listView, SIGNAL(addClip (QUrl, const QString &)), this, SLOT(slotAddClip(QUrl, const QString &)));
114   connect(listView, SIGNAL (itemChanged ( QTreeWidgetItem *, int )), this, SLOT(slotUpdateItemDescription(QTreeWidgetItem *, int )));
115
116   m_listViewDelegate = new ItemDelegate(listView);
117   listView->setItemDelegate(m_listViewDelegate);
118   listView->setIconSize(QSize(60, 40));
119   listView->setSortingEnabled (true);
120 }
121
122 ProjectList::~ProjectList()
123 {
124   delete m_menu;
125   delete m_toolbar;
126 }
127
128 void ProjectList::setRenderer(Render *projectRender)
129 {
130   m_render = projectRender;
131 }
132
133 void ProjectList::slotClipSelected()
134 {
135   ProjectItem *item = (ProjectItem*) listView->currentItem();
136   if (item && !item->isGroup()) emit clipSelected(item->toXml());
137 }
138
139 void ProjectList::slotUpdateItemDescription( QTreeWidgetItem *item, int column)
140 {
141   if (column != 2) return;
142   ProjectItem *clip = (ProjectItem*) item;
143   CLIPTYPE type = clip->clipType(); 
144   if (type == AUDIO || type == VIDEO || type == AV || type == IMAGE || type == PLAYLIST) {
145     // Use Nepomuk system to store clip description
146     Nepomuk::Resource f( clip->clipUrl().path() );
147     f.setDescription(item->text(2));
148     kDebug()<<"NEPOMUK, SETTING CLIP: "<<clip->clipUrl().path()<<", TO TEXT: "<<item->text(2);
149   }
150 }
151
152 void ProjectList::slotEditClip()
153 {
154   kDebug()<<"////////////////////////////////////////   DBL CLK";
155 }
156
157
158 void ProjectList::slotEditClip(QTreeWidgetItem *item, int column)
159 {
160   kDebug()<<"////////////////////////////////////////   DBL CLK";
161 }
162
163 void ProjectList::slotContextMenu( const QPoint &pos, QTreeWidgetItem *item )
164 {
165   bool enable = false;
166   if (item) {
167     QFrame *w = new QFrame;
168     w->setFrameShape(QFrame::StyledPanel);
169     w->setLineWidth(2);
170     w->setAutoFillBackground(true);
171     QHBoxLayout *layout = new QHBoxLayout;
172     layout->addWidget( new QLabel(i18n("Color:")));
173     layout->addWidget( new KColorButton());
174     layout->addWidget( new QLabel(i18n("Duration:")));
175     layout->addWidget( new KRestrictedLine());
176     w->setLayout( layout );
177     m_listViewDelegate->extendItem(w, listView->currentIndex());
178     enable = true;
179   }
180   m_editAction->setEnabled(enable);
181   m_deleteAction->setEnabled(enable);
182
183   m_menu->popup(pos);
184 }
185
186 void ProjectList::slotRemoveClip()
187 {
188
189   if (!m_commandStack) kDebug()<<"!!!!!!!!!!!!!!!!  NO CMD STK";
190   if (!listView->currentItem()) return;
191   ProjectItem *item = ((ProjectItem *)listView->currentItem());
192   if (item->numReferences() > 0) {
193     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;
194   }
195   m_doc->deleteProjectClip(item->clipId());
196 }
197
198 void ProjectList::selectItemById(const int clipId)
199 {
200   ProjectItem *item = getItemById(clipId);
201   if (item) listView->setCurrentItem(item);
202 }
203
204 void ProjectList::addClip(const QStringList &name, const QDomElement &elem, const int clipId, const KUrl &url, const QString &group, int parentId)
205 {
206   kDebug()<<"/////////  ADDING VCLIP=: "<<name;
207   ProjectItem *item;
208   ProjectItem *groupItem = NULL;
209   QString groupName;
210   if (group.isEmpty()) groupName = elem.attribute("group", QString::null);
211   else groupName = group;
212   if (elem.isNull() && url.isEmpty()) {
213     // this is a folder
214     groupName = name.at(1);
215     QList<QTreeWidgetItem *> groupList = listView->findItems(groupName, Qt::MatchExactly, 1);
216     if (groupList.isEmpty())  {
217         (void) new ProjectItem(listView, name, clipId);
218     }
219     return;
220   }
221
222   if (parentId != -1) {
223     groupItem = getItemById(parentId);
224   }
225   else if (!groupName.isEmpty()) {
226     // Clip is in a group
227     QList<QTreeWidgetItem *> groupList = listView->findItems(groupName, Qt::MatchExactly, 1);
228
229     if (groupList.isEmpty())  {
230         QStringList itemName;
231         itemName<<QString::null<<groupName;
232         kDebug()<<"-------  CREATING NEW GRP: "<<itemName;
233         groupItem = new ProjectItem(listView, itemName, m_clipIdCounter++);
234     }
235     else groupItem = (ProjectItem *) groupList.first();
236   }
237   if (groupItem) item = new ProjectItem(groupItem, name, elem, clipId);
238   else item = new ProjectItem(listView, name, elem, clipId);
239   if (!url.isEmpty()) {
240     // if file has Nepomuk comment, use it
241     Nepomuk::Resource f( url.path() );
242     QString annotation = f.description();
243     if (!annotation.isEmpty()) item->setText(2, annotation);
244     QString resource = url.path();
245     if (resource.endsWith("westley") || resource.endsWith("kdenlive")) {
246         QString tmpfile;
247         QDomDocument doc;
248         if (KIO::NetAccess::download(url, tmpfile, 0)) {
249             QFile file(tmpfile);
250             if (file.open(QIODevice::ReadOnly)) {
251               doc.setContent(&file, false);
252               file.close();
253             }
254             KIO::NetAccess::removeTempFile(tmpfile);
255
256             QDomNodeList subProds = doc.elementsByTagName("producer");
257             int ct = subProds.count();
258             for (int i = 0; i <  ct ; i++)
259             {
260               QDomElement e = subProds.item(i).toElement();
261               if (!e.isNull()) {
262                 addProducer(e, clipId);
263               }
264             }  
265           }
266     }
267
268   }
269
270   if (elem.isNull() ) {
271     QDomDocument doc;
272     QDomElement element = doc.createElement("producer");
273     element.setAttribute("resource", url.path());
274     emit getFileProperties(element, clipId);
275   }
276   else emit getFileProperties(elem, clipId);
277   selectItemById(clipId);
278 }
279
280 void ProjectList::slotDeleteClip( int clipId)
281 {
282   ProjectItem *item = getItemById(clipId);
283   if (item) delete item;
284 }
285
286 void ProjectList::slotAddFolder()
287 {
288 /*
289   QString folderName = KInputDialog::getText(i18n("New Folder"), i18n("Enter new folder name: "));
290   if (folderName.isEmpty()) return;
291   QStringList itemEntry;
292   itemEntry.append(QString::null);
293   itemEntry.append(folderName);
294   AddClipCommand *command = new AddClipCommand(this, itemEntry, QDomElement(), m_clipIdCounter++, KUrl(), folderName, true);
295   m_commandStack->push(command);*/
296 }
297
298 void ProjectList::slotAddClip(DocClipBase *clip)
299 {
300   ProjectItem *item = new ProjectItem(listView, clip);
301   listView->setCurrentItem(item);
302   emit getFileProperties(clip->toXML(), clip->getId());
303 }
304
305 void ProjectList::slotUpdateClip(int id)
306 {
307   ProjectItem *item = getItemById(id);
308   item->setData(1, UsageRole, QString::number(item->numReferences()));
309 }
310
311 void ProjectList::slotAddClip(QUrl givenUrl, const QString &group)
312 {
313   if (!m_commandStack) kDebug()<<"!!!!!!!!!!!!!!!!  NO CMD STK";
314   KUrl::List list;
315   if (givenUrl.isEmpty())
316           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/mpeg 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");
317   else list.append(givenUrl);
318   if (list.isEmpty()) return;
319   KUrl::List::Iterator it;
320
321   for (it = list.begin(); it != list.end(); it++) {
322       m_doc->slotAddClipFile(*it, group);
323   }
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     QString color = dia_ui->clip_color->color().name();
337     color = color.replace(0, 1, "0x") + "ff";
338     m_doc->slotAddColorClipFile(dia_ui->clip_name->text(), color, dia_ui->clip_duration->text(), QString::null);
339   }
340   delete dia_ui;
341   delete dia;
342 }
343
344 void ProjectList::slotAddTitleClip()
345 {
346
347         if (!m_commandStack) kDebug()<<"!!!!!!!!!!!!!!!!  NO CMD STK";
348         //QDialog *dia = new QDialog;
349         
350         TitleWidget *dia_ui = new TitleWidget();
351         //dia_ui->setupUi(dia);
352         //dia_ui->clip_name->setText(i18n("Title Clip"));
353         //dia_ui->clip_duration->setText(KdenliveSettings::color_duration());
354         if (dia_ui->exec() == QDialog::Accepted)
355         {
356                 //QString color = dia_ui->clip_color->color().name();
357                 //color = color.replace(0, 1, "0x") + "ff";
358                 //m_doc->slotAddColorClipFile(dia_ui->clip_name->text(), color, dia_ui->clip_duration->text(), QString::null);
359         }
360         delete dia_ui;
361         //delete dia;
362 }
363 void ProjectList::setDocument(KdenliveDoc *doc)
364 {
365   m_fps = doc->fps();
366   m_timecode = doc->timecode();
367   m_commandStack = doc->commandStack();
368   m_doc = doc;
369   QDomNodeList prods = doc->producersList();
370   int ct = prods.count();
371   kDebug()<<"////////////  SETTING DOC, FOUND CLIPS: "<<prods.count();
372   listView->clear();
373   for (int i = 0; i <  ct ; i++)
374   {
375     QDomElement e = prods.item(i).toElement();
376     kDebug()<<"// IMPORT: "<<i<<", :"<<e.attribute("id", "non")<<", NAME: "<<e.attribute("name", "non");
377     if (!e.isNull()) addProducer(e);
378   }
379   QTreeWidgetItem *first = listView->topLevelItem(0);
380   if (first) listView->setCurrentItem(first);
381   m_toolbar->setEnabled(true);
382 }
383
384 QDomElement ProjectList::producersList()
385 {
386   QDomDocument doc;
387   QDomElement prods = doc.createElement("producerlist");
388   doc.appendChild(prods);
389   kDebug()<<"////////////  PRO LIST BUILD PRDSLIST ";
390     QTreeWidgetItemIterator it(listView);
391      while (*it) {
392          if (!((ProjectItem *)(*it))->isGroup())
393           prods.appendChild(doc.importNode(((ProjectItem *)(*it))->toXml(), true));
394          ++it;
395      }
396   return prods;
397 }
398
399
400 void ProjectList::slotReplyGetFileProperties(int clipId, const QMap < QString, QString > &properties, const QMap < QString, QString > &metadata)
401 {
402   ProjectItem *item = getItemById(clipId);
403   if (item) {
404     item->setProperties(properties, metadata);
405     emit receivedClipDuration(clipId, item->clipMaxDuration());
406   }
407 }
408
409
410
411 void ProjectList::slotReplyGetImage(int clipId, int pos, const QPixmap &pix, int w, int h)
412 {
413   ProjectItem *item = getItemById(clipId);
414   if (item) item->setIcon(0, pix);
415 }
416
417 ProjectItem *ProjectList::getItemById(int id)
418 {
419     QTreeWidgetItemIterator it(listView);
420      while (*it) {
421          if (((ProjectItem *)(*it))->clipId() == id)
422           break;
423          ++it;
424      }
425   return ((ProjectItem *)(*it));
426 }
427
428
429 void ProjectList::addProducer(QDomElement producer, int parentId)
430 {
431   if (!m_commandStack) kDebug()<<"!!!!!!!!!!!!!!!!  NO CMD STK";
432   CLIPTYPE type = (CLIPTYPE) producer.attribute("type").toInt();
433
434     /*QDomDocument doc;
435     QDomElement prods = doc.createElement("list");
436     doc.appendChild(prods);
437     prods.appendChild(doc.importNode(producer, true));*/
438     
439
440   //kDebug()<<"//////  ADDING PRODUCER:\n "<<doc.toString()<<"\n+++++++++++++++++";
441   int id = producer.attribute("id").toInt();
442   QString groupName = producer.attribute("group");
443   if (id >= m_clipIdCounter) m_clipIdCounter = id + 1;
444   else if (id == 0) id = m_clipIdCounter++;
445
446   if (parentId != -1) {
447     // item is a westley playlist, adjust subproducers ids 
448     id = (parentId + 1) * 10000 + id;
449   }
450   if (type == AUDIO || type == VIDEO || type == AV || type == IMAGE  || type == PLAYLIST)
451   {
452     KUrl resource = KUrl(producer.attribute("resource"));
453     if (!resource.isEmpty()) {
454       QStringList itemEntry;
455       itemEntry.append(QString::null);
456       itemEntry.append(resource.fileName());
457       addClip(itemEntry, producer, id, resource, groupName, parentId);  
458     }
459   }
460   else if (type == COLOR) {
461     QString colour = producer.attribute("colour");
462     QPixmap pix(60, 40);
463     colour = colour.replace(0, 2, "#");
464     pix.fill(QColor(colour.left(7)));
465     QStringList itemEntry;
466     itemEntry.append(QString::null);
467     itemEntry.append(producer.attribute("name", i18n("Color clip")));
468     addClip(itemEntry, producer, id, KUrl(), groupName, parentId);
469   }
470       
471 }
472
473 #include "projectlist.moc"