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