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