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