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