]> git.sesse.net Git - kdenlive/blob - src/projectlist.cpp
project tree view: Add drag and drop support
[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 <nepomuk/resourcemanager.h>
33 #include <kio/netaccess.h>
34
35 #include "projectlist.h"
36 #include "projectitem.h"
37 #include "kdenlivesettings.h"
38 #include "ui_colorclip_ui.h"
39
40 #include "addclipcommand.h"
41
42 #include <QtGui>
43
44   const int NameRole = Qt::UserRole;
45   const int DurationRole = NameRole + 1;
46   const int FullPathRole = NameRole + 2;
47   const int ClipTypeRole = NameRole + 3;
48
49 class ItemDelegate: public QItemDelegate
50 {
51   public:
52     ItemDelegate(QObject* parent = 0): QItemDelegate(parent)
53     {
54     }
55
56 void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
57 {
58   if (index.column() == 1)
59   {
60     const bool hover = option.state & (QStyle::State_Selected|QStyle::State_MouseOver|QStyle::State_HasFocus);
61     QRect r1 = option.rect;
62     painter->save();
63     if (hover) {
64         painter->setPen(option.palette.color(QPalette::HighlightedText));
65         QColor backgroundColor = option.palette.color(QPalette::Highlight);
66         painter->setBrush(QBrush(backgroundColor));
67         painter->fillRect(r1, QBrush(backgroundColor));
68     }
69     QFont font = painter->font();
70     font.setPointSize(font.pointSize() - 1 );
71     font.setBold(true);
72     painter->setFont(font);
73     int mid = (int) ((r1.height() / 2 ));
74     r1.setBottom(r1.y() + mid);
75     QRect r2 = option.rect;
76     r2.setTop(r2.y() + mid);
77     painter->drawText(r1, Qt::AlignLeft | Qt::AlignBottom , index.data().toString());
78     //painter->setPen(Qt::green);
79     font.setBold(false);
80     painter->setFont(font);
81     painter->drawText(r2, Qt::AlignLeft | Qt::AlignVCenter , index.data(DurationRole).toString());
82     painter->restore();
83   }
84   else
85   {
86     QItemDelegate::paint(painter, option, index);
87   }
88 }
89 };
90
91
92 ProjectList::ProjectList(QWidget *parent)
93     : QWidget(parent), m_render(NULL), m_fps(-1), m_commandStack(NULL)
94 {
95
96   QWidget *vbox = new QWidget;
97   listView = new ProjectListView(this);;
98   QVBoxLayout *layout = new QVBoxLayout;
99   m_clipIdCounter = 0;
100
101   // setup toolbar
102   searchView = new KTreeWidgetSearchLine (this);
103   m_toolbar = new QToolBar("projectToolBar", this);
104   m_toolbar->addWidget (searchView);
105
106   QToolButton *addButton = new QToolButton( m_toolbar );
107   QMenu *addMenu = new QMenu(this);
108   addButton->setMenu( addMenu );
109   addButton->setPopupMode(QToolButton::MenuButtonPopup);
110   m_toolbar->addWidget (addButton);
111
112   QAction *addClipButton = addMenu->addAction (KIcon("document-new"), i18n("Add Clip"));
113   connect(addClipButton, SIGNAL(triggered()), this, SLOT(slotAddClip()));
114
115   QAction *addColorClip = addMenu->addAction (KIcon("document-new"), i18n("Add Color Clip"));
116   connect(addColorClip, SIGNAL(triggered()), this, SLOT(slotAddColorClip()));
117
118   m_deleteAction = m_toolbar->addAction (KIcon("edit-delete"), i18n("Delete Clip"));
119   connect(m_deleteAction, SIGNAL(triggered()), this, SLOT(slotRemoveClip()));
120
121   m_editAction = m_toolbar->addAction (KIcon("document-properties"), i18n("Edit Clip"));
122   connect(m_editAction, SIGNAL(triggered()), this, SLOT(slotEditClip()));
123
124   QAction *addFolderButton = addMenu->addAction (KIcon("folder-new"), i18n("Create Folder"));
125   connect(addFolderButton, SIGNAL(triggered()), this, SLOT(slotAddFolder()));
126
127   addButton->setDefaultAction( addClipButton );
128
129   layout->addWidget( m_toolbar );
130   layout->addWidget( listView );
131   setLayout( layout );
132   m_toolbar->setEnabled(false);
133
134   searchView->setTreeWidget(listView);
135   listView->setColumnCount(3);
136   QStringList headers;
137   headers<<i18n("Thumbnail")<<i18n("Filename")<<i18n("Description");
138   listView->setHeaderLabels(headers);
139   listView->sortByColumn(1, Qt::AscendingOrder);
140
141   m_menu = new QMenu(); 
142   m_menu->addAction(addClipButton);
143   m_menu->addAction(addColorClip);
144   m_menu->addAction(m_editAction);
145   m_menu->addAction(m_deleteAction);
146   m_menu->addAction(addFolderButton);
147   m_menu->insertSeparator(m_deleteAction);
148
149   connect(listView, SIGNAL(itemSelectionChanged()), this, SLOT(slotClipSelected()));
150   connect(listView, SIGNAL(requestMenu ( const QPoint &, QTreeWidgetItem * )), this, SLOT(slotContextMenu(const QPoint &, QTreeWidgetItem *)));
151   connect(listView, SIGNAL(addClip ()), this, SLOT(slotAddClip()));
152   connect(listView, SIGNAL(addClip (QUrl, const QString &)), this, SLOT(slotAddClip(QUrl, const QString &)));
153
154
155   listView->setItemDelegate(new ItemDelegate(listView));
156   listView->setIconSize(QSize(60, 40));
157   listView->setSortingEnabled (true);
158
159 }
160
161 ProjectList::~ProjectList()
162 {
163   delete m_menu;
164   delete m_toolbar;
165 }
166
167 void ProjectList::setRenderer(Render *projectRender)
168 {
169   m_render = projectRender;
170 }
171
172 void ProjectList::slotClipSelected()
173 {
174   ProjectItem *item = (ProjectItem*) listView->currentItem();
175   if (item && !item->isGroup()) emit clipSelected(item->toXml());
176 }
177
178 void ProjectList::slotEditClip()
179 {
180   kDebug()<<"////////////////////////////////////////   DBL CLK";
181 }
182
183
184 void ProjectList::slotEditClip(QTreeWidgetItem *item, int column)
185 {
186   kDebug()<<"////////////////////////////////////////   DBL CLK";
187 }
188
189 void ProjectList::slotContextMenu( const QPoint &pos, QTreeWidgetItem *item )
190 {
191   bool enable = false;
192   if (item) {
193     enable = true;
194   }
195   m_editAction->setEnabled(enable);
196   m_deleteAction->setEnabled(enable);
197
198   m_menu->popup(pos);
199 }
200
201 void ProjectList::slotRemoveClip()
202 {
203   if (!m_commandStack) kDebug()<<"!!!!!!!!!!!!!!!!  NO CMD STK";
204   if (!listView->currentItem()) return;
205   ProjectItem *item = ((ProjectItem *)listView->currentItem());
206   AddClipCommand *command = new AddClipCommand(this, item->names(), item->toXml(), item->clipId(), KUrl(item->data(1, FullPathRole).toString()), item->groupName(), false);
207   m_commandStack->push(command);
208
209 }
210
211 void ProjectList::selectItemById(const int clipId)
212 {
213   ProjectItem *item = getItemById(clipId);
214   if (item) listView->setCurrentItem(item);
215 }
216
217 void ProjectList::addClip(const QStringList &name, const QDomElement &elem, const int clipId, const KUrl &url, const QString &group, int parentId)
218 {
219   kDebug()<<"/////////  ADDING VCLIP=: "<<name;
220   ProjectItem *item;
221   ProjectItem *groupItem = NULL;
222   QString groupName;
223   if (group.isEmpty()) groupName = elem.attribute("group", QString::null);
224   else groupName = group;
225   if (elem.isNull() && url.isEmpty()) {
226     // this is a folder
227     groupName = name.at(1);
228     QList<QTreeWidgetItem *> groupList = listView->findItems(groupName, Qt::MatchExactly, 1);
229     if (groupList.isEmpty())  {
230         (void) new ProjectItem(listView, name, clipId);
231     }
232     return;
233   }
234
235   if (parentId != -1) {
236     groupItem = getItemById(parentId);
237   }
238   else if (!groupName.isEmpty()) {
239     // Clip is in a group
240     QList<QTreeWidgetItem *> groupList = listView->findItems(groupName, Qt::MatchExactly, 1);
241
242     if (groupList.isEmpty())  {
243         QStringList itemName;
244         itemName<<QString::null<<groupName;
245         kDebug()<<"-------  CREATING NEW GRP: "<<itemName;
246         groupItem = new ProjectItem(listView, itemName, m_clipIdCounter++);
247     }
248     else groupItem = (ProjectItem *) groupList.first();
249   }
250   if (groupItem) item = new ProjectItem(groupItem, name, elem, clipId);
251   else item = new ProjectItem(listView, name, elem, clipId);
252   if (!url.isEmpty()) {
253     item->setData(1, FullPathRole, url.path());
254 /*    Nepomuk::File f( url.path() );
255     QString annotation = f.getAnnotation();
256     if (!annotation.isEmpty()) item->setText(2, annotation);*/
257     QString resource = url.path();
258     if (resource.endsWith("westley") || resource.endsWith("kdenlive")) {
259         QString tmpfile;
260         QDomDocument doc;
261         if (KIO::NetAccess::download(url, tmpfile, 0)) {
262             QFile file(tmpfile);
263             if (file.open(QIODevice::ReadOnly)) {
264               doc.setContent(&file, false);
265               file.close();
266             }
267             KIO::NetAccess::removeTempFile(tmpfile);
268
269             QDomNodeList subProds = doc.elementsByTagName("producer");
270             int ct = subProds.count();
271             for (int i = 0; i <  ct ; i++)
272             {
273               QDomElement e = subProds.item(i).toElement();
274               if (!e.isNull()) {
275                 addProducer(e, clipId);
276               }
277             }  
278           }
279     }
280
281   }
282
283   if (elem.isNull() ) {
284     QDomDocument doc;
285     QDomElement element = doc.createElement("producer");
286     element.setAttribute("resource", url.path());
287     emit getFileProperties(element, clipId);
288   }
289   else emit getFileProperties(elem, clipId);
290   selectItemById(clipId);
291 }
292
293 void ProjectList::deleteClip(const int clipId)
294 {
295   ProjectItem *item = getItemById(clipId);
296   if (item) delete item;
297 }
298
299 void ProjectList::slotAddFolder()
300 {
301   QString folderName = KInputDialog::getText(i18n("New Folder"), i18n("Enter new folder name: "));
302   if (folderName.isEmpty()) return;
303   QStringList itemEntry;
304   itemEntry.append(QString::null);
305   itemEntry.append(folderName);
306   AddClipCommand *command = new AddClipCommand(this, itemEntry, QDomElement(), m_clipIdCounter++, KUrl(), folderName, true);
307   m_commandStack->push(command);
308 }
309
310 void ProjectList::slotAddClip(QUrl givenUrl, const QString &group)
311 {
312   if (!m_commandStack) kDebug()<<"!!!!!!!!!!!!!!!!  NO CMD STK";
313   KUrl::List list;
314   if (givenUrl.isEmpty())
315     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");
316   else list.append(givenUrl);
317   if (list.isEmpty()) return;
318   KUrl::List::Iterator it;
319   KUrl url;
320 //  ProjectItem *item;
321
322   for (it = list.begin(); it != list.end(); it++) {
323       QStringList itemEntry;
324       itemEntry.append(QString::null);
325       itemEntry.append((*it).fileName());
326       AddClipCommand *command = new AddClipCommand(this, itemEntry, QDomElement(), m_clipIdCounter++, *it, group, true);
327       m_commandStack->push(command);
328       //item = new ProjectItem(listView, itemEntry, QDomElement());
329       //item->setData(1, FullPathRole, (*it).path());
330   }
331   //listView->setCurrentItem(item);
332 }
333
334 void ProjectList::slotAddColorClip()
335 {
336   if (!m_commandStack) kDebug()<<"!!!!!!!!!!!!!!!!  NO CMD STK";
337   QDialog *dia = new QDialog;
338   Ui::ColorClip_UI *dia_ui = new Ui::ColorClip_UI();
339   dia_ui->setupUi(dia);
340   dia_ui->clip_name->setText(i18n("Color Clip"));
341   dia_ui->clip_duration->setText(KdenliveSettings::color_duration());
342   if (dia->exec() == QDialog::Accepted)
343   {
344     QDomDocument doc;
345     QDomElement element = doc.createElement("producer");
346     element.setAttribute("mlt_service", "colour");
347     QString color = dia_ui->clip_color->color().name();
348     color = color.replace(0, 1, "0x") + "ff";
349     element.setAttribute("colour", color);
350     element.setAttribute("type", (int) DocClipBase::COLOR);
351     element.setAttribute("in", "0");
352     element.setAttribute("out", m_timecode.getFrameCount(dia_ui->clip_duration->text(), m_fps));
353     element.setAttribute("name", dia_ui->clip_name->text());
354     QStringList itemEntry;
355     itemEntry.append(QString::null);
356     itemEntry.append(dia_ui->clip_name->text());
357     AddClipCommand *command = new AddClipCommand(this, itemEntry, element, m_clipIdCounter++, KUrl(), QString::null, true);
358     m_commandStack->push(command);
359     // ProjectItem *item = new ProjectItem(listView, itemEntry, element, m_clipIdCounter++);
360     /*QPixmap pix(60, 40);
361     pix.fill(dia_ui->clip_color->color());
362     item->setIcon(0, QIcon(pix));*/
363     //listView->setCurrentItem(item);
364     
365   }
366   delete dia_ui;
367   delete dia;
368 }
369
370 void ProjectList::setDocument(KdenliveDoc *doc)
371 {
372   m_fps = doc->fps();
373   m_timecode = doc->timecode();
374   m_commandStack = doc->commandStack();
375   QDomNodeList prods = doc->producersList();
376   int ct = prods.count();
377   kDebug()<<"////////////  SETTING DOC, FOUND CLIPS: "<<prods.count();
378   listView->clear();
379   for (int i = 0; i <  ct ; i++)
380   {
381     QDomElement e = prods.item(i).toElement();
382     kDebug()<<"// IMPORT: "<<i<<", :"<<e.attribute("id", "non")<<", NAME: "<<e.attribute("name", "non");
383     if (!e.isNull()) addProducer(e);
384   }
385   QTreeWidgetItem *first = listView->topLevelItem(0);
386   if (first) listView->setCurrentItem(first);
387   m_toolbar->setEnabled(true);
388 }
389
390 QDomElement ProjectList::producersList()
391 {
392   QDomDocument doc;
393   QDomElement prods = doc.createElement("producerlist");
394   doc.appendChild(prods);
395   kDebug()<<"////////////  PRO LIST BUILD PRDSLIST ";
396     QTreeWidgetItemIterator it(listView);
397      while (*it) {
398          if (!((ProjectItem *)(*it))->isGroup())
399           prods.appendChild(doc.importNode(((ProjectItem *)(*it))->toXml(), true));
400          ++it;
401      }
402   return prods;
403 }
404
405
406 void ProjectList::slotReplyGetFileProperties(int clipId, const QMap < QString, QString > &properties, const QMap < QString, QString > &metadata)
407 {
408   ProjectItem *item = getItemById(clipId);
409   if (item) item->setProperties(properties, metadata);
410 }
411
412
413
414 void ProjectList::slotReplyGetImage(int clipId, int pos, const QPixmap &pix, int w, int h)
415 {
416   ProjectItem *item = getItemById(clipId);
417   if (item) item->setIcon(0, pix);
418 }
419
420 ProjectItem *ProjectList::getItemById(int id)
421 {
422     QTreeWidgetItemIterator it(listView);
423      while (*it) {
424          if (((ProjectItem *)(*it))->clipId() == id)
425           break;
426          ++it;
427      }
428   return ((ProjectItem *)(*it));
429 }
430
431
432 void ProjectList::addProducer(QDomElement producer, int parentId)
433 {
434   if (!m_commandStack) kDebug()<<"!!!!!!!!!!!!!!!!  NO CMD STK";
435   DocClipBase::CLIPTYPE type = (DocClipBase::CLIPTYPE) producer.attribute("type").toInt();
436
437     /*QDomDocument doc;
438     QDomElement prods = doc.createElement("list");
439     doc.appendChild(prods);
440     prods.appendChild(doc.importNode(producer, true));*/
441     
442
443   //kDebug()<<"//////  ADDING PRODUCER:\n "<<doc.toString()<<"\n+++++++++++++++++";
444   int id = producer.attribute("id").toInt();
445   QString groupName = producer.attribute("group");
446   if (id >= m_clipIdCounter) m_clipIdCounter = id + 1;
447   else if (id == 0) id = m_clipIdCounter++;
448
449   if (type == DocClipBase::AUDIO || type == DocClipBase::VIDEO || type == DocClipBase::AV || type == DocClipBase::IMAGE  || type == DocClipBase::PLAYLIST)
450   {
451     KUrl resource = KUrl(producer.attribute("resource"));
452     if (!resource.isEmpty()) {
453       QStringList itemEntry;
454       itemEntry.append(QString::null);
455       itemEntry.append(resource.fileName());
456       addClip(itemEntry, producer, id, resource, groupName, parentId);
457       /*AddClipCommand *command = new AddClipCommand(this, itemEntry, producer, id, resource, true);
458       m_commandStack->push(command);*/
459
460
461       /*ProjectItem *item = new ProjectItem(listView, itemEntry, producer);
462       item->setData(1, FullPathRole, resource.path());
463       item->setData(1, ClipTypeRole, (int) type);*/
464       
465     }
466   }
467   else if (type == DocClipBase::COLOR) {
468     QString colour = producer.attribute("colour");
469     QPixmap pix(60, 40);
470     colour = colour.replace(0, 2, "#");
471     pix.fill(QColor(colour.left(7)));
472     QStringList itemEntry;
473     itemEntry.append(QString::null);
474     itemEntry.append(producer.attribute("name", i18n("Color clip")));
475     addClip(itemEntry, producer, id, KUrl(), groupName, parentId);
476     /*AddClipCommand *command = new AddClipCommand(this, itemEntry, producer, id, KUrl(), true);
477     m_commandStack->push(command);*/
478     //ProjectItem *item = new ProjectItem(listView, itemEntry, producer);
479     /*item->setIcon(0, QIcon(pix));
480     item->setData(1, ClipTypeRole, (int) type);*/
481   }
482       
483 }
484
485 #include "projectlist.moc"