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