]> git.sesse.net Git - kdenlive/blob - src/projectlist.cpp
add context menu
[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
149
150   listView->setItemDelegate(new ItemDelegate(listView));
151   listView->setIconSize(QSize(60, 40));
152   listView->setSortingEnabled (true);
153
154 }
155
156 ProjectList::~ProjectList()
157 {
158   delete m_menu;
159   delete m_toolbar;
160 }
161
162 void ProjectList::setRenderer(Render *projectRender)
163 {
164   m_render = projectRender;
165 }
166
167 void ProjectList::slotClipSelected()
168 {
169   ProjectItem *item = (ProjectItem*) listView->currentItem();
170   if (item && !item->isGroup()) emit clipSelected(item->toXml());
171 }
172
173 void ProjectList::slotEditClip()
174 {
175   kDebug()<<"////////////////////////////////////////   DBL CLK";
176 }
177
178
179 void ProjectList::slotEditClip(QTreeWidgetItem *item, int column)
180 {
181   kDebug()<<"////////////////////////////////////////   DBL CLK";
182 }
183
184 void ProjectList::slotContextMenu( const QPoint &pos, QTreeWidgetItem *item )
185 {
186   bool enable = false;
187   if (item) {
188     enable = true;
189   }
190   m_editAction->setEnabled(enable);
191   m_deleteAction->setEnabled(enable);
192
193   m_menu->popup(pos);
194 }
195
196 void ProjectList::slotRemoveClip()
197 {
198   if (!m_commandStack) kDebug()<<"!!!!!!!!!!!!!!!!  NO CMD STK";
199   if (!listView->currentItem()) return;
200   ProjectItem *item = ((ProjectItem *)listView->currentItem());
201   AddClipCommand *command = new AddClipCommand(this, item->names(), item->toXml(), item->clipId(), KUrl(item->data(1, FullPathRole).toString()), false);
202   m_commandStack->push(command);
203
204 }
205
206 void ProjectList::selectItemById(const int clipId)
207 {
208   ProjectItem *item = getItemById(clipId);
209   if (item) listView->setCurrentItem(item);
210 }
211
212 void ProjectList::addClip(const QStringList &name, const QDomElement &elem, const int clipId, const KUrl &url, int parentId)
213 {
214   kDebug()<<"/////////  ADDING VCLIP=: "<<name;
215   ProjectItem *item;
216   ProjectItem *groupItem = NULL;
217   QString group = elem.attribute("group", QString::null);
218   if (parentId != -1) {
219     groupItem = getItemById(parentId);
220   }
221   else if (!group.isEmpty()) {
222     // Clip is in a group
223     QList<QTreeWidgetItem *> groupList = listView->findItems(group, Qt::MatchExactly, 1);
224
225     if (groupList.isEmpty())  {
226         QStringList groupName;
227         groupName<<QString::null<<group;
228         kDebug()<<"-------  CREATING NEW GRP: "<<groupName;
229         groupItem = new ProjectItem(listView, groupName);
230     }
231     else groupItem = (ProjectItem *) groupList.first();
232   }
233   if (groupItem) item = new ProjectItem(groupItem, name, elem, clipId);
234   else item = new ProjectItem(listView, name, elem, clipId);
235   if (!url.isEmpty()) {
236     item->setData(1, FullPathRole, url.path());
237 /*    Nepomuk::File f( url.path() );
238     QString annotation = f.getAnnotation();
239     if (!annotation.isEmpty()) item->setText(2, annotation);*/
240     QString resource = url.path();
241     if (resource.endsWith("westley") || resource.endsWith("kdenlive")) {
242         QString tmpfile;
243         QDomDocument doc;
244         if (KIO::NetAccess::download(url, tmpfile, 0)) {
245             QFile file(tmpfile);
246             if (file.open(QIODevice::ReadOnly)) {
247               doc.setContent(&file, false);
248               file.close();
249             }
250             KIO::NetAccess::removeTempFile(tmpfile);
251
252             QDomNodeList subProds = doc.elementsByTagName("producer");
253             int ct = subProds.count();
254             for (int i = 0; i <  ct ; i++)
255             {
256               QDomElement e = subProds.item(i).toElement();
257               if (!e.isNull()) {
258                 addProducer(e, clipId);
259               }
260             }  
261           }
262     }
263
264   }
265
266   if (elem.isNull() ) {
267     QDomDocument doc;
268     QDomElement element = doc.createElement("producer");
269     element.setAttribute("resource", url.path());
270     emit getFileProperties(element, clipId);
271   }
272   else emit getFileProperties(elem, clipId);
273   selectItemById(clipId);
274 }
275
276 void ProjectList::deleteClip(const int clipId)
277 {
278   ProjectItem *item = getItemById(clipId);
279   if (item) delete item;
280 }
281
282 void ProjectList::slotAddClip()
283 {
284   if (!m_commandStack) kDebug()<<"!!!!!!!!!!!!!!!!  NO CMD STK";
285   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");
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       itemEntry.append(QString::null);
294       itemEntry.append((*it).fileName());
295       AddClipCommand *command = new AddClipCommand(this, itemEntry, QDomElement(), m_clipIdCounter++, *it, true);
296       m_commandStack->push(command);
297       //item = new ProjectItem(listView, itemEntry, QDomElement());
298       //item->setData(1, FullPathRole, (*it).path());
299   }
300   //listView->setCurrentItem(item);
301 }
302
303 void ProjectList::slotAddColorClip()
304 {
305   if (!m_commandStack) kDebug()<<"!!!!!!!!!!!!!!!!  NO CMD STK";
306   QDialog *dia = new QDialog;
307   Ui::ColorClip_UI *dia_ui = new Ui::ColorClip_UI();
308   dia_ui->setupUi(dia);
309   dia_ui->clip_name->setText(i18n("Color Clip"));
310   dia_ui->clip_duration->setText(KdenliveSettings::color_duration());
311   if (dia->exec() == QDialog::Accepted)
312   {
313     QDomDocument doc;
314     QDomElement element = doc.createElement("producer");
315     element.setAttribute("mlt_service", "colour");
316     QString color = dia_ui->clip_color->color().name();
317     color = color.replace(0, 1, "0x") + "ff";
318     element.setAttribute("colour", color);
319     element.setAttribute("type", (int) DocClipBase::COLOR);
320     element.setAttribute("in", "0");
321     element.setAttribute("out", m_timecode.getFrameCount(dia_ui->clip_duration->text(), m_fps));
322     element.setAttribute("name", dia_ui->clip_name->text());
323     QStringList itemEntry;
324     itemEntry.append(QString::null);
325     itemEntry.append(dia_ui->clip_name->text());
326     AddClipCommand *command = new AddClipCommand(this, itemEntry, element, m_clipIdCounter++, KUrl(), true);
327     m_commandStack->push(command);
328     // ProjectItem *item = new ProjectItem(listView, itemEntry, element, m_clipIdCounter++);
329     /*QPixmap pix(60, 40);
330     pix.fill(dia_ui->clip_color->color());
331     item->setIcon(0, QIcon(pix));*/
332     //listView->setCurrentItem(item);
333     
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   QDomNodeList prods = doc->producersList();
345   int ct = prods.count();
346   kDebug()<<"////////////  SETTING DOC, FOUND CLIPS: "<<prods.count();
347   listView->clear();
348   for (int i = 0; i <  ct ; i++)
349   {
350     QDomElement e = prods.item(i).toElement();
351     kDebug()<<"// IMPORT: "<<i<<", :"<<e.attribute("id", "non")<<", NAME: "<<e.attribute("name", "non");
352     if (!e.isNull()) addProducer(e);
353   }
354   QTreeWidgetItem *first = listView->topLevelItem(0);
355   if (first) listView->setCurrentItem(first);
356   m_toolbar->setEnabled(true);
357 }
358
359 QDomElement ProjectList::producersList()
360 {
361   QDomDocument doc;
362   QDomElement prods = doc.createElement("producerlist");
363   doc.appendChild(prods);
364   kDebug()<<"////////////  PRO LIST BUILD PRDSLIST ";
365     QTreeWidgetItemIterator it(listView);
366      while (*it) {
367          if (!((ProjectItem *)(*it))->isGroup())
368           prods.appendChild(doc.importNode(((ProjectItem *)(*it))->toXml(), true));
369          ++it;
370      }
371   return prods;
372 }
373
374
375 void ProjectList::slotReplyGetFileProperties(int clipId, const QMap < QString, QString > &properties, const QMap < QString, QString > &metadata)
376 {
377   ProjectItem *item = getItemById(clipId);
378   if (item) item->setProperties(properties, metadata);
379 }
380
381
382
383 void ProjectList::slotReplyGetImage(int clipId, int pos, const QPixmap &pix, int w, int h)
384 {
385   ProjectItem *item = getItemById(clipId);
386   if (item) item->setIcon(0, pix);
387 }
388
389 ProjectItem *ProjectList::getItemById(int id)
390 {
391     QTreeWidgetItemIterator it(listView);
392      while (*it) {
393          if (((ProjectItem *)(*it))->clipId() == id)
394           break;
395          ++it;
396      }
397   return ((ProjectItem *)(*it));
398 }
399
400
401 void ProjectList::addProducer(QDomElement producer, int parentId)
402 {
403   if (!m_commandStack) kDebug()<<"!!!!!!!!!!!!!!!!  NO CMD STK";
404   DocClipBase::CLIPTYPE type = (DocClipBase::CLIPTYPE) producer.attribute("type").toInt();
405
406     /*QDomDocument doc;
407     QDomElement prods = doc.createElement("list");
408     doc.appendChild(prods);
409     prods.appendChild(doc.importNode(producer, true));*/
410     
411
412   //kDebug()<<"//////  ADDING PRODUCER:\n "<<doc.toString()<<"\n+++++++++++++++++";
413   int id = producer.attribute("id").toInt();
414   if (id >= m_clipIdCounter) m_clipIdCounter = id + 1;
415   else if (id == 0) id = m_clipIdCounter++;
416
417   if (type == DocClipBase::AUDIO || type == DocClipBase::VIDEO || type == DocClipBase::AV || type == DocClipBase::IMAGE  || type == DocClipBase::PLAYLIST)
418   {
419     KUrl resource = KUrl(producer.attribute("resource"));
420     if (!resource.isEmpty()) {
421       QStringList itemEntry;
422       itemEntry.append(QString::null);
423       itemEntry.append(resource.fileName());
424       addClip(itemEntry, producer, id, resource, parentId);
425       /*AddClipCommand *command = new AddClipCommand(this, itemEntry, producer, id, resource, true);
426       m_commandStack->push(command);*/
427
428
429       /*ProjectItem *item = new ProjectItem(listView, itemEntry, producer);
430       item->setData(1, FullPathRole, resource.path());
431       item->setData(1, ClipTypeRole, (int) type);*/
432       
433     }
434   }
435   else if (type == DocClipBase::COLOR) {
436     QString colour = producer.attribute("colour");
437     QPixmap pix(60, 40);
438     colour = colour.replace(0, 2, "#");
439     pix.fill(QColor(colour.left(7)));
440     QStringList itemEntry;
441     itemEntry.append(QString::null);
442     itemEntry.append(producer.attribute("name", i18n("Color clip")));
443     addClip(itemEntry, producer, id, KUrl(), parentId);
444     /*AddClipCommand *command = new AddClipCommand(this, itemEntry, producer, id, KUrl(), true);
445     m_commandStack->push(command);*/
446     //ProjectItem *item = new ProjectItem(listView, itemEntry, producer);
447     /*item->setIcon(0, QIcon(pix));
448     item->setData(1, ClipTypeRole, (int) type);*/
449   }
450       
451 }
452
453 #include "projectlist.moc"