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