]> git.sesse.net Git - kdenlive/blob - src/projectlist.cpp
Some work on project tree view
[kdenlive] / src / projectlist.cpp
1
2 #include <QMouseEvent>
3 #include <QStylePainter>
4 #include <QPixmap>
5 #include <QIcon>
6 #include <QToolBar>
7 #include <QDialog>
8
9 #include <KDebug>
10 #include <KAction>
11 #include <KLocale>
12 #include <KFileDialog>
13 #include <klistwidgetsearchline.h>
14
15 #include "projectlist.h"
16 #include "projectitem.h"
17 #include "ui_colorclip_ui.h"
18
19 #include <QtGui>
20
21   const int NameRole = Qt::UserRole;
22   const int DurationRole = NameRole + 1;
23   const int FullPathRole = NameRole + 2;
24   const int ClipTypeRole = NameRole + 3;
25
26 class ItemDelegate: public QItemDelegate
27 {
28   public:
29     ItemDelegate(QObject* parent = 0): QItemDelegate(parent)
30     {
31     }
32
33 void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
34 {
35   if (index.column() == 1)
36   {
37     QFont font = painter->font();
38     font.setPointSize(font.pointSize() - 2 );
39     QRect r1 = option.rect;
40     r1.setBottom(r1.y() + (r1.height() *2 / 3 ));
41     QRect r2 = option.rect;
42     r2.setTop(r2.y() + (r2.height() *2 / 3 ));
43     painter->drawText(r1, Qt::AlignLeft | Qt::AlignVCenter , index.data().toString());
44     painter->setPen(Qt::green);
45     painter->drawText(r2, Qt::AlignLeft | Qt::AlignTop , index.data(DurationRole).toString());
46     painter->setPen(Qt::black);
47   }
48   else
49   {
50     QItemDelegate::paint(painter, option, index);
51   }
52 }
53 };
54
55
56 ProjectList::ProjectList(Render *projectRender, QWidget *parent)
57     : QWidget(parent), m_render(projectRender)
58 {
59
60   QWidget *vbox = new QWidget;
61   listView = new QTreeWidget(this);;
62   QVBoxLayout *layout = new QVBoxLayout;
63
64   // setup toolbar
65   searchView = new KTreeWidgetSearchLine (this);
66   QToolBar *bar = new QToolBar("projectToolBar", this);
67   bar->addWidget (searchView);
68
69   QToolButton *addButton = new QToolButton( bar );
70   QMenu *addMenu = new QMenu(this);
71   addButton->setMenu( addMenu );
72   addButton->setPopupMode(QToolButton::MenuButtonPopup);
73   bar->addWidget (addButton);
74   
75   QAction *addClip = addMenu->addAction (KIcon("document-new"), i18n("Add Clip"));
76   connect(addClip, SIGNAL(triggered()), this, SLOT(slotAddClip()));
77
78   QAction *addColorClip = addMenu->addAction (KIcon("document-new"), i18n("Add Color Clip"));
79   connect(addColorClip, SIGNAL(triggered()), this, SLOT(slotAddColorClip()));
80
81   QAction *deleteClip = bar->addAction (KIcon("edit-delete"), i18n("Delete Clip"));
82   connect(deleteClip, SIGNAL(triggered()), this, SLOT(slotRemoveClip()));
83
84   QAction *editClip = bar->addAction (KIcon("document-properties"), i18n("Edit Clip"));
85   connect(editClip, SIGNAL(triggered()), this, SLOT(slotEditClip()));
86
87   addButton->setDefaultAction( addClip );
88
89   layout->addWidget( bar );
90   layout->addWidget( listView );
91   setLayout( layout );
92
93   searchView->setTreeWidget(listView);
94   listView->setColumnCount(3);
95   QStringList headers;
96   headers<<i18n("Thumbnail")<<i18n("Filename")<<i18n("Description");
97   listView->setHeaderLabels(headers);
98
99   QStringList itemEntry;
100   itemEntry.append(QString::null);
101   itemEntry.append("coucou");
102   new ProjectItem(listView, itemEntry);
103
104   connect(listView, SIGNAL(itemSelectionChanged()), this, SLOT(slotClipSelected()));
105   //connect(listView, SIGNAL(itemDoubleClicked ( QTreeWidgetItem *, int )), this, SLOT(slotEditClip(QTreeWidgetItem *, int)));
106
107
108   listView->setItemDelegate(new ItemDelegate(listView));
109   listView->setIconSize(QSize(60, 40));
110   listView->setSortingEnabled (true);
111
112 }
113
114 void ProjectList::setRenderer(Render *projectRender)
115 {
116   m_render = projectRender;
117 }
118
119 void ProjectList::slotDoubleClicked(QListWidgetItem *item, const QPoint &pos)
120 {
121   kDebug()<<" / / / DBL CLICK";
122   if (item) {
123     KUrl url = KFileDialog::getOpenUrl( KUrl(), "video/mpeg");
124   }
125 }
126
127 void ProjectList::slotClipSelected()
128 {
129   ProjectItem *item = (ProjectItem*) listView->currentItem();
130   if (item) emit clipSelected(item->toXml());
131 }
132
133 void ProjectList::slotEditClip()
134 {
135
136 }
137
138 void ProjectList::slotRemoveClip()
139 {
140   kDebug()<<"//////////  SLOT REMOVE";
141   if (!listView->currentItem()) return;
142   delete listView->currentItem();
143 }
144
145 void ProjectList::slotAddClip()
146 {
147    
148   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");
149
150   KUrl::List::Iterator it;
151   KUrl url;
152         
153   for (it = list.begin(); it != list.end(); it++) {
154       QStringList itemEntry;
155       itemEntry.append(QString::null);
156       itemEntry.append((*it).fileName());
157       ProjectItem *item = new ProjectItem(listView, itemEntry, QDomElement());
158       item->setData(1, FullPathRole, (*it).path());
159       emit getFileProperties((*it), 0);
160   }
161 }
162
163 void ProjectList::slotAddColorClip()
164 {
165   QDialog *dia = new QDialog;
166   Ui::ColorClip_UI *dia_ui = new Ui::ColorClip_UI();
167   dia_ui->setupUi(dia);
168   dia_ui->clip_name->setText(i18n("Color Clip"));
169   if (dia->exec() == QDialog::Accepted)
170   {
171     QDomDocument doc;
172     QDomElement element = doc.createElement("producer");
173     element.setAttribute("mlt_service", "colour");
174     QString color = dia_ui->clip_color->color().name();
175     color = color.replace(0, 1, "0x") + "ff";
176     element.setAttribute("colour", color);
177     element.setAttribute("type", (int) DocClipBase::COLOR);
178     QStringList itemEntry;
179     itemEntry.append(QString::null);
180     itemEntry.append(dia_ui->clip_name->text());
181     ProjectItem *item = new ProjectItem(listView, itemEntry, element);
182     QPixmap pix(60, 40);
183     pix.fill(dia_ui->clip_color->color());
184     item->setIcon(0, QIcon(pix));
185
186     
187   }
188   delete dia_ui;
189   delete dia;
190   /*for (it = list.begin(); it != list.end(); it++) {
191       QStringList itemEntry;
192       itemEntry.append(QString::null);
193       itemEntry.append((*it).fileName());
194       ProjectItem *item = new ProjectItem(listView, itemEntry, QDomElement());
195       item->setData(1, FullPathRole, (*it).path());
196       emit getFileProperties((*it), 0);
197   }*/
198 }
199
200 void ProjectList::populate(QDomNodeList prods)
201 {
202   listView->clear();
203   for (int i = 0; i <  prods.count () ; i++)
204   {
205     addProducer(prods.item(i).toElement());
206   }
207 }
208
209 void ProjectList::slotReplyGetFileProperties(const QMap < QString, QString > &properties, const QMap < QString, QString > &metadata)
210 {
211   QTreeWidgetItem *parent = 0;
212   int count =
213     parent ? parent->childCount() : listView->topLevelItemCount();
214
215   for (int i = 0; i < count; i++)
216   {
217     QTreeWidgetItem *item =
218       parent ? parent->child(i) : listView->topLevelItem(i);
219
220     if (item->data(1, FullPathRole).toString() == properties["filename"]) {
221       ((ProjectItem *) item)->setProperties(properties, metadata);
222       break;
223     }
224   }
225 }
226
227
228 void ProjectList::slotReplyGetImage(const KUrl &url, int pos, const QPixmap &pix, int w, int h)
229 {
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
239     if (item->data(1, FullPathRole).toString() == url.path()) {
240       item->setIcon(0,pix);
241       break;
242     }
243   }
244
245 }
246
247
248 void ProjectList::addProducer(QDomElement producer)
249 {
250   DocClipBase::CLIPTYPE type = (DocClipBase::CLIPTYPE) producer.attribute("type").toInt();
251   
252   if (type == DocClipBase::AUDIO || type == DocClipBase::VIDEO || type == DocClipBase::AV)
253   {
254     KUrl resource = KUrl(producer.attribute("resource"));
255     if (!resource.isEmpty()) {
256       QStringList itemEntry;
257       itemEntry.append(QString::null);
258       itemEntry.append(resource.fileName());
259       ProjectItem *item = new ProjectItem(listView, itemEntry, producer);
260       //item->setIcon(0, Render::getVideoThumbnail(resource, 0, 60, 40));
261       item->setData(1, FullPathRole, resource.path());
262       item->setData(1, ClipTypeRole, (int) type);
263       emit getFileProperties(resource, producer.attribute("frame_thumbnail", 0).toInt());
264     }
265   }
266   else if (type == DocClipBase::COLOR) {
267     QString colour = producer.attribute("colour");
268     QPixmap pix(60, 40);
269     colour = colour.replace(0, 2, "#");
270     pix.fill(QColor(colour.left(7)));
271     QStringList itemEntry;
272     itemEntry.append(QString::null);
273     itemEntry.append(producer.attribute("name"););
274     ProjectItem *item = new ProjectItem(listView, itemEntry, producer);
275     item->setIcon(0, QIcon(pix));
276     item->setData(1, ClipTypeRole, (int) type);
277   }
278       
279 }
280
281 #include "projectlist.moc"