]> git.sesse.net Git - kdenlive/blob - src/projectlist.cpp
Fix project view thumbnails
[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(KUndoStack *commandStack, QWidget *parent)
71     : QWidget(parent), m_render(NULL), m_fps(-1), m_commandStack(commandStack)
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::addClip(const QStringList &name, const QDomElement &elem, const int clipId, const KUrl &url)
154 {
155   ProjectItem *item = new ProjectItem(listView, name, elem, clipId);
156   if (!url.isEmpty()) {
157     item->setData(1, FullPathRole, url.path());
158   }
159   if (elem.isNull() ) {
160     QDomDocument doc;
161     QDomElement element = doc.createElement("producer");
162     element.setAttribute("resource", url.path());
163     emit getFileProperties(element, clipId);
164   }
165   else emit getFileProperties(elem, clipId);
166 }
167
168 void ProjectList::deleteClip(const int clipId)
169 {
170   QTreeWidgetItem *parent = 0;
171   int count =
172     parent ? parent->childCount() : listView->topLevelItemCount();
173
174   for (int i = 0; i < count; i++)
175   {
176     QTreeWidgetItem *item =
177       parent ? parent->child(i) : listView->topLevelItem(i);
178     if (((ProjectItem *)item)->clipId() == clipId) {
179       delete item;
180       break;
181     }
182   }
183 }
184
185 void ProjectList::slotAddClip()
186 {
187    
188   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");
189   if (list.isEmpty()) return;
190   KUrl::List::Iterator it;
191   KUrl url;
192 //  ProjectItem *item;
193
194   for (it = list.begin(); it != list.end(); it++) {
195       QStringList itemEntry;
196       itemEntry.append(QString::null);
197       itemEntry.append((*it).fileName());
198       AddClipCommand *command = new AddClipCommand(this, itemEntry, QDomElement(), m_clipIdCounter++, *it, true);
199       m_commandStack->push(command);
200       //item = new ProjectItem(listView, itemEntry, QDomElement());
201       //item->setData(1, FullPathRole, (*it).path());
202   }
203   //listView->setCurrentItem(item);
204 }
205
206 void ProjectList::slotAddColorClip()
207 {
208   QDialog *dia = new QDialog;
209   Ui::ColorClip_UI *dia_ui = new Ui::ColorClip_UI();
210   dia_ui->setupUi(dia);
211   dia_ui->clip_name->setText(i18n("Color Clip"));
212   dia_ui->clip_duration->setText(KdenliveSettings::color_duration());
213   if (dia->exec() == QDialog::Accepted)
214   {
215     QDomDocument doc;
216     QDomElement element = doc.createElement("producer");
217     element.setAttribute("mlt_service", "colour");
218     QString color = dia_ui->clip_color->color().name();
219     color = color.replace(0, 1, "0x") + "ff";
220     element.setAttribute("colour", color);
221     element.setAttribute("type", (int) DocClipBase::COLOR);
222     element.setAttribute("in", "0");
223     element.setAttribute("out", m_timecode.getFrameCount(dia_ui->clip_duration->text(), m_fps));
224     element.setAttribute("name", dia_ui->clip_name->text());
225     QStringList itemEntry;
226     itemEntry.append(QString::null);
227     itemEntry.append(dia_ui->clip_name->text());
228     AddClipCommand *command = new AddClipCommand(this, itemEntry, element, m_clipIdCounter++, KUrl(), true);
229     m_commandStack->push(command);
230     //ProjectItem *item = new ProjectItem(listView, itemEntry, element);
231     /*QPixmap pix(60, 40);
232     pix.fill(dia_ui->clip_color->color());
233     item->setIcon(0, QIcon(pix));*/
234     //listView->setCurrentItem(item);
235     
236   }
237   delete dia_ui;
238   delete dia;
239   /*for (it = list.begin(); it != list.end(); it++) {
240       QStringList itemEntry;
241       itemEntry.append(QString::null);
242       itemEntry.append((*it).fileName());
243       ProjectItem *item = new ProjectItem(listView, itemEntry, QDomElement());
244       item->setData(1, FullPathRole, (*it).path());
245       emit getFileProperties((*it), 0);
246   }*/
247 }
248
249 void ProjectList::setDocument(KdenliveDoc *doc)
250 {
251   m_fps = doc->fps();
252   m_timecode = doc->timecode();
253
254   QDomNodeList prods = doc->producersList();
255   listView->clear();
256   for (int i = 0; i <  prods.count () ; i++)
257   {
258     addProducer(prods.item(i).toElement());
259   }
260   QTreeWidgetItem *first = listView->topLevelItem(0);
261   if (first) listView->setCurrentItem(first);
262   m_toolbar->setEnabled(true);
263 }
264
265 QDomElement ProjectList::producersList()
266 {
267   QDomDocument doc;
268   QDomElement prods = doc.createElement("producerlist");
269   doc.appendChild(prods);
270   QTreeWidgetItem *parent = 0;
271   int count =
272     parent ? parent->childCount() : listView->topLevelItemCount();
273
274   for (int i = 0; i < count; i++)
275   {
276     QTreeWidgetItem *item =
277       parent ? parent->child(i) : listView->topLevelItem(i);
278     prods.appendChild(doc.importNode(((ProjectItem *)item)->toXml(), true));
279   }
280   //kDebug()<<"PRODUCERS: \n"<<doc.toString();
281   return prods;
282 }
283
284
285 void ProjectList::slotReplyGetFileProperties(int clipId, const QMap < QString, QString > &properties, const QMap < QString, QString > &metadata)
286 {
287   QTreeWidgetItem *parent = 0;
288   int count =
289     parent ? parent->childCount() : listView->topLevelItemCount();
290
291   for (int i = 0; i < count; i++)
292   {
293     QTreeWidgetItem *item =
294       parent ? parent->child(i) : listView->topLevelItem(i);
295
296     if (((ProjectItem *)item)->clipId() == clipId) {
297       ((ProjectItem *) item)->setProperties(properties, metadata);
298       break;
299     }
300   }
301 }
302
303
304
305 void ProjectList::slotReplyGetImage(int clipId, int pos, const QPixmap &pix, int w, int h)
306 {
307   QTreeWidgetItem *parent = 0;
308   int count =
309     parent ? parent->childCount() : listView->topLevelItemCount();
310
311   for (int i = 0; i < count; i++)
312   {
313     QTreeWidgetItem *item =
314       parent ? parent->child(i) : listView->topLevelItem(i);
315
316       if (((ProjectItem *)item)->clipId() == clipId) {
317         item->setIcon(0, pix);
318       break;
319     }
320   }
321 }
322
323
324 void ProjectList::addProducer(QDomElement producer)
325 {
326   DocClipBase::CLIPTYPE type = (DocClipBase::CLIPTYPE) producer.attribute("type").toInt();
327   
328   if (type == DocClipBase::AUDIO || type == DocClipBase::VIDEO || type == DocClipBase::AV)
329   {
330     KUrl resource = KUrl(producer.attribute("resource"));
331     if (!resource.isEmpty()) {
332       QStringList itemEntry;
333       itemEntry.append(QString::null);
334       itemEntry.append(resource.fileName());
335
336       AddClipCommand *command = new AddClipCommand(this, itemEntry, producer, producer.attribute("id").toInt(), resource, true);
337       m_commandStack->push(command);
338
339       /*ProjectItem *item = new ProjectItem(listView, itemEntry, producer);
340       item->setData(1, FullPathRole, resource.path());
341       item->setData(1, ClipTypeRole, (int) type);*/
342       
343     }
344   }
345   else if (type == DocClipBase::COLOR) {
346     QString colour = producer.attribute("colour");
347     QPixmap pix(60, 40);
348     colour = colour.replace(0, 2, "#");
349     pix.fill(QColor(colour.left(7)));
350     QStringList itemEntry;
351     itemEntry.append(QString::null);
352     itemEntry.append(producer.attribute("name"));
353
354     AddClipCommand *command = new AddClipCommand(this, itemEntry, producer, producer.attribute("id").toInt(), KUrl(), true);
355     m_commandStack->push(command);
356     //ProjectItem *item = new ProjectItem(listView, itemEntry, producer);
357     /*item->setIcon(0, QIcon(pix));
358     item->setData(1, ClipTypeRole, (int) type);*/
359   }
360       
361 }
362
363 #include "projectlist.moc"