]> git.sesse.net Git - kdenlive/blob - src/projectlist.cpp
a20b953351b68218a54e247f43014fc0baa4fdd0
[kdenlive] / src / projectlist.cpp
1
2 #include <QMouseEvent>
3 #include <QStylePainter>
4 #include <QPixmap>
5 #include <QIcon>
6 #include <QDialog>
7
8 #include <KDebug>
9 #include <KAction>
10 #include <KLocale>
11 #include <KFileDialog>
12
13 #include "projectlist.h"
14 #include "projectitem.h"
15 #include "kdenlivesettings.h"
16 #include "ui_colorclip_ui.h"
17
18 #include "addclipcommand.h"
19
20 #include <QtGui>
21
22   const int NameRole = Qt::UserRole;
23   const int DurationRole = NameRole + 1;
24   const int FullPathRole = NameRole + 2;
25   const int ClipTypeRole = NameRole + 3;
26
27 class ItemDelegate: public QItemDelegate
28 {
29   public:
30     ItemDelegate(QObject* parent = 0): QItemDelegate(parent)
31     {
32     }
33
34 void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
35 {
36   if (index.column() == 1)
37   {
38     const bool hover = option.state & (QStyle::State_Selected|QStyle::State_MouseOver|QStyle::State_HasFocus);
39     QRect r1 = option.rect;
40     painter->save();
41     if (hover) {
42         painter->setPen(option.palette.color(QPalette::HighlightedText));
43         QColor backgroundColor = option.palette.color(QPalette::Highlight);
44         painter->setBrush(QBrush(backgroundColor));
45         painter->fillRect(r1, QBrush(backgroundColor));
46     }
47     QFont font = painter->font();
48     font.setPointSize(font.pointSize() - 1 );
49     font.setBold(true);
50     painter->setFont(font);
51     int mid = (int) ((r1.height() / 2 ));
52     r1.setBottom(r1.y() + mid);
53     QRect r2 = option.rect;
54     r2.setTop(r2.y() + mid);
55     painter->drawText(r1, Qt::AlignLeft | Qt::AlignBottom , index.data().toString());
56     //painter->setPen(Qt::green);
57     font.setBold(false);
58     painter->setFont(font);
59     painter->drawText(r2, Qt::AlignLeft | Qt::AlignVCenter , index.data(DurationRole).toString());
60     painter->restore();
61   }
62   else
63   {
64     QItemDelegate::paint(painter, option, index);
65   }
66 }
67 };
68
69
70 ProjectList::ProjectList(QWidget *parent)
71     : QWidget(parent), m_render(NULL), m_fps(-1), m_commandStack(NULL)
72 {
73
74   QWidget *vbox = new QWidget;
75   listView = new QTreeWidget(this);;
76   QVBoxLayout *layout = new QVBoxLayout;
77   m_clipIdCounter = 0;
78
79   // setup toolbar
80   searchView = new KTreeWidgetSearchLine (this);
81   m_toolbar = new QToolBar("projectToolBar", this);
82   m_toolbar->addWidget (searchView);
83
84   QToolButton *addButton = new QToolButton( m_toolbar );
85   QMenu *addMenu = new QMenu(this);
86   addButton->setMenu( addMenu );
87   addButton->setPopupMode(QToolButton::MenuButtonPopup);
88   m_toolbar->addWidget (addButton);
89   
90   QAction *addClip = addMenu->addAction (KIcon("document-new"), i18n("Add Clip"));
91   connect(addClip, SIGNAL(triggered()), this, SLOT(slotAddClip()));
92
93   QAction *addColorClip = addMenu->addAction (KIcon("document-new"), i18n("Add Color Clip"));
94   connect(addColorClip, SIGNAL(triggered()), this, SLOT(slotAddColorClip()));
95
96   QAction *deleteClip = m_toolbar->addAction (KIcon("edit-delete"), i18n("Delete Clip"));
97   connect(deleteClip, SIGNAL(triggered()), this, SLOT(slotRemoveClip()));
98
99   QAction *editClip = m_toolbar->addAction (KIcon("document-properties"), i18n("Edit Clip"));
100   connect(editClip, SIGNAL(triggered()), this, SLOT(slotEditClip()));
101
102   addButton->setDefaultAction( addClip );
103
104   layout->addWidget( m_toolbar );
105   layout->addWidget( listView );
106   setLayout( layout );
107   m_toolbar->setEnabled(false);
108
109   searchView->setTreeWidget(listView);
110   listView->setColumnCount(3);
111   listView->setDragEnabled(true);
112   listView->setDragDropMode(QAbstractItemView::DragOnly);
113   QStringList headers;
114   headers<<i18n("Thumbnail")<<i18n("Filename")<<i18n("Description");
115   listView->setHeaderLabels(headers);
116   listView->sortByColumn(1, Qt::AscendingOrder);
117
118   connect(listView, SIGNAL(itemSelectionChanged()), this, SLOT(slotClipSelected()));
119   //connect(listView, SIGNAL(itemDoubleClicked ( QTreeWidgetItem *, int )), this, SLOT(slotEditClip(QTreeWidgetItem *, int)));
120
121
122   listView->setItemDelegate(new ItemDelegate(listView));
123   listView->setIconSize(QSize(60, 40));
124   listView->setSortingEnabled (true);
125
126 }
127
128 void ProjectList::setRenderer(Render *projectRender)
129 {
130   m_render = projectRender;
131 }
132
133 void ProjectList::slotClipSelected()
134 {
135   ProjectItem *item = (ProjectItem*) listView->currentItem();
136   if (item) emit clipSelected(item->toXml());
137 }
138
139 void ProjectList::slotEditClip()
140 {
141
142 }
143
144 void ProjectList::slotRemoveClip()
145 {
146   if (!listView->currentItem()) return;
147   ProjectItem *item = ((ProjectItem *)listView->currentItem());
148   AddClipCommand *command = new AddClipCommand(this, item->names(), item->toXml(), item->clipId(), KUrl(item->data(1, FullPathRole).toString()), false);
149   m_commandStack->push(command);
150
151 }
152
153 void ProjectList::selectItemById(const int clipId)
154 {
155   QTreeWidgetItem *parent = 0;
156   int count =
157     parent ? parent->childCount() : listView->topLevelItemCount();
158
159   for (int i = 0; i < count; i++)
160   {
161     QTreeWidgetItem *item =
162       parent ? parent->child(i) : listView->topLevelItem(i);
163     if (((ProjectItem *)item)->clipId() == clipId) {
164       listView->setCurrentItem(item);
165       break;
166     }
167   }
168 }
169
170 void ProjectList::addClip(const QStringList &name, const QDomElement &elem, const int clipId, const KUrl &url)
171 {
172   kDebug()<<"/////////  ADDING VCLIP=: "<<name;
173   ProjectItem *item = new ProjectItem(listView, name, elem, clipId);
174   if (!url.isEmpty()) {
175     item->setData(1, FullPathRole, url.path());
176   }
177   if (elem.isNull() ) {
178     QDomDocument doc;
179     QDomElement element = doc.createElement("producer");
180     element.setAttribute("resource", url.path());
181     emit getFileProperties(element, clipId);
182   }
183   else emit getFileProperties(elem, clipId);
184   selectItemById(clipId);
185 }
186
187 void ProjectList::deleteClip(const int clipId)
188 {
189   QTreeWidgetItem *parent = 0;
190   int count =
191     parent ? parent->childCount() : listView->topLevelItemCount();
192
193   for (int i = 0; i < count; i++)
194   {
195     QTreeWidgetItem *item =
196       parent ? parent->child(i) : listView->topLevelItem(i);
197     if (((ProjectItem *)item)->clipId() == clipId) {
198       delete item;
199       break;
200     }
201   }
202 }
203
204 void ProjectList::slotAddClip()
205 {
206    
207   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");
208   if (list.isEmpty()) return;
209   KUrl::List::Iterator it;
210   KUrl url;
211 //  ProjectItem *item;
212
213   for (it = list.begin(); it != list.end(); it++) {
214       QStringList itemEntry;
215       itemEntry.append(QString::null);
216       itemEntry.append((*it).fileName());
217       AddClipCommand *command = new AddClipCommand(this, itemEntry, QDomElement(), m_clipIdCounter++, *it, true);
218       m_commandStack->push(command);
219       //item = new ProjectItem(listView, itemEntry, QDomElement());
220       //item->setData(1, FullPathRole, (*it).path());
221   }
222   //listView->setCurrentItem(item);
223 }
224
225 void ProjectList::slotAddColorClip()
226 {
227   QDialog *dia = new QDialog;
228   Ui::ColorClip_UI *dia_ui = new Ui::ColorClip_UI();
229   dia_ui->setupUi(dia);
230   dia_ui->clip_name->setText(i18n("Color Clip"));
231   dia_ui->clip_duration->setText(KdenliveSettings::color_duration());
232   if (dia->exec() == QDialog::Accepted)
233   {
234     QDomDocument doc;
235     QDomElement element = doc.createElement("producer");
236     element.setAttribute("mlt_service", "colour");
237     QString color = dia_ui->clip_color->color().name();
238     color = color.replace(0, 1, "0x") + "ff";
239     element.setAttribute("colour", color);
240     element.setAttribute("type", (int) DocClipBase::COLOR);
241     element.setAttribute("in", "0");
242     element.setAttribute("out", m_timecode.getFrameCount(dia_ui->clip_duration->text(), m_fps));
243     element.setAttribute("name", dia_ui->clip_name->text());
244     QStringList itemEntry;
245     itemEntry.append(QString::null);
246     itemEntry.append(dia_ui->clip_name->text());
247     AddClipCommand *command = new AddClipCommand(this, itemEntry, element, m_clipIdCounter++, KUrl(), true);
248     m_commandStack->push(command);
249     //ProjectItem *item = new ProjectItem(listView, itemEntry, element);
250     /*QPixmap pix(60, 40);
251     pix.fill(dia_ui->clip_color->color());
252     item->setIcon(0, QIcon(pix));*/
253     //listView->setCurrentItem(item);
254     
255   }
256   delete dia_ui;
257   delete dia;
258   /*for (it = list.begin(); it != list.end(); it++) {
259       QStringList itemEntry;
260       itemEntry.append(QString::null);
261       itemEntry.append((*it).fileName());
262       ProjectItem *item = new ProjectItem(listView, itemEntry, QDomElement());
263       item->setData(1, FullPathRole, (*it).path());
264       emit getFileProperties((*it), 0);
265   }*/
266 }
267
268 void ProjectList::setDocument(KdenliveDoc *doc)
269 {
270   m_fps = doc->fps();
271   m_timecode = doc->timecode();
272   m_commandStack = doc->commandStack();
273   QDomNodeList prods = doc->producersList();
274   int ct = prods.count();
275   kDebug()<<"////////////  SETTING DOC, FOUND CLIPS: "<<prods.count();
276   listView->clear();
277   for (int i = 0; i <  ct ; i++)
278   {
279     QDomElement e = prods.item(i).cloneNode().toElement();
280     kDebug()<<"// IMPORT: "<<i<<", :"<<e.attribute("id", "non")<<", NAME: "<<e.attribute("name", "non");
281     if (!e.isNull()) addProducer(e);
282   }
283   QTreeWidgetItem *first = listView->topLevelItem(0);
284   if (first) listView->setCurrentItem(first);
285   m_toolbar->setEnabled(true);
286 }
287
288 QDomElement ProjectList::producersList()
289 {
290   QDomDocument doc;
291   QDomElement prods = doc.createElement("producerlist");
292   doc.appendChild(prods);
293   QTreeWidgetItem *parent = 0;
294   int count =
295     parent ? parent->childCount() : listView->topLevelItemCount();
296
297   for (int i = 0; i < count; i++)
298   {
299     QTreeWidgetItem *item =
300       parent ? parent->child(i) : listView->topLevelItem(i);
301       prods.appendChild(doc.importNode(((ProjectItem *)item)->toXml(), true));
302   }
303
304   return prods;
305 }
306
307
308 void ProjectList::slotReplyGetFileProperties(int clipId, const QMap < QString, QString > &properties, const QMap < QString, QString > &metadata)
309 {
310   QTreeWidgetItem *parent = 0;
311   int count =
312     parent ? parent->childCount() : listView->topLevelItemCount();
313
314   for (int i = 0; i < count; i++)
315   {
316     QTreeWidgetItem *item =
317       parent ? parent->child(i) : listView->topLevelItem(i);
318
319     if (((ProjectItem *)item)->clipId() == clipId) {
320       ((ProjectItem *) item)->setProperties(properties, metadata);
321       break;
322     }
323   }
324 }
325
326
327
328 void ProjectList::slotReplyGetImage(int clipId, int pos, const QPixmap &pix, int w, int h)
329 {
330   QTreeWidgetItem *parent = 0;
331   int count =
332     parent ? parent->childCount() : listView->topLevelItemCount();
333
334   for (int i = 0; i < count; i++)
335   {
336     QTreeWidgetItem *item =
337       parent ? parent->child(i) : listView->topLevelItem(i);
338
339       if (((ProjectItem *)item)->clipId() == clipId) {
340         item->setIcon(0, pix);
341       break;
342     }
343   }
344 }
345
346
347 void ProjectList::addProducer(QDomElement producer)
348 {
349   DocClipBase::CLIPTYPE type = (DocClipBase::CLIPTYPE) producer.attribute("type").toInt();
350
351     /*QDomDocument doc;
352     QDomElement prods = doc.createElement("list");
353     doc.appendChild(prods);
354     prods.appendChild(doc.importNode(producer, true));*/
355     
356
357   //kDebug()<<"//////  ADDING PRODUCER:\n "<<doc.toString()<<"\n+++++++++++++++++";
358   int id = producer.attribute("id").toInt();
359   if (id >= m_clipIdCounter) m_clipIdCounter = id + 1;
360
361   if (type == DocClipBase::AUDIO || type == DocClipBase::VIDEO || type == DocClipBase::AV)
362   {
363     KUrl resource = KUrl(producer.attribute("resource"));
364     if (!resource.isEmpty()) {
365       QStringList itemEntry;
366       itemEntry.append(QString::null);
367       itemEntry.append(resource.fileName());
368       addClip(itemEntry, producer, id, resource);
369       /*AddClipCommand *command = new AddClipCommand(this, itemEntry, producer, id, resource, true);
370       m_commandStack->push(command);*/
371
372
373       /*ProjectItem *item = new ProjectItem(listView, itemEntry, producer);
374       item->setData(1, FullPathRole, resource.path());
375       item->setData(1, ClipTypeRole, (int) type);*/
376       
377     }
378   }
379   else if (type == DocClipBase::COLOR) {
380     QString colour = producer.attribute("colour");
381     QPixmap pix(60, 40);
382     colour = colour.replace(0, 2, "#");
383     pix.fill(QColor(colour.left(7)));
384     QStringList itemEntry;
385     itemEntry.append(QString::null);
386     itemEntry.append(producer.attribute("name"));
387     addClip(itemEntry, producer, id, KUrl());
388     /*AddClipCommand *command = new AddClipCommand(this, itemEntry, producer, id, KUrl(), true);
389     m_commandStack->push(command);*/
390     //ProjectItem *item = new ProjectItem(listView, itemEntry, producer);
391     /*item->setIcon(0, QIcon(pix));
392     item->setData(1, ClipTypeRole, (int) type);*/
393   }
394       
395 }
396
397 #include "projectlist.moc"