]> git.sesse.net Git - kdenlive/blob - src/projectlist.cpp
1f17abc0d8b8942fb91773a4698744158feac329
[kdenlive] / src / projectlist.cpp
1 /***************************************************************************
2  *   Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
3  *                                                                         *
4  *   This program is free software; you can redistribute it and/or modify  *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; either version 2 of the License, or     *
7  *   (at your option) any later version.                                   *
8  *                                                                         *
9  *   This program is distributed in the hope that it will be useful,       *
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12  *   GNU General Public License for more details.                          *
13  *                                                                         *
14  *   You should have received a copy of the GNU General Public License     *
15  *   along with this program; if not, write to the                         *
16  *   Free Software Foundation, Inc.,                                       *
17  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
18  ***************************************************************************/
19
20
21 #include <QMouseEvent>
22 #include <QStylePainter>
23 #include <QPixmap>
24 #include <QIcon>
25 #include <QDialog>
26
27 #include <KDebug>
28 #include <KAction>
29 #include <KLocale>
30 #include <KFileDialog>
31 #include <KInputDialog>
32 #include <kio/netaccess.h>
33 #include <KMessageBox>
34
35 #include <nepomuk/global.h>
36 #include <nepomuk/resource.h>
37 #include <nepomuk/tag.h>
38
39 #include "projectlist.h"
40 #include "projectitem.h"
41 #include "kdenlivesettings.h"
42 #include "ui_colorclip_ui.h"
43
44 #include "definitions.h"
45 #include "titlewidget.h"
46 #include "clipmanager.h"
47 #include "docclipbase.h"
48 #include "kdenlivedoc.h"
49 #include "renderer.h"
50 #include "projectlistview.h"
51 #include <QtGui>
52
53 ProjectList::ProjectList(QWidget *parent)
54         : QWidget(parent), m_render(NULL), m_fps(-1), m_commandStack(NULL) {
55
56     QWidget *vbox = new QWidget;
57     listView = new ProjectListView(this);;
58     QVBoxLayout *layout = new QVBoxLayout;
59     m_clipIdCounter = 0;
60
61     // setup toolbar
62     searchView = new KTreeWidgetSearchLine(this);
63     m_toolbar = new QToolBar("projectToolBar", this);
64     m_toolbar->addWidget(searchView);
65
66     QToolButton *addButton = new QToolButton(m_toolbar);
67     QMenu *addMenu = new QMenu(this);
68     addButton->setMenu(addMenu);
69     addButton->setPopupMode(QToolButton::MenuButtonPopup);
70     m_toolbar->addWidget(addButton);
71
72     QAction *addClipButton = addMenu->addAction(KIcon("document-new"), i18n("Add Clip"));
73     connect(addClipButton, SIGNAL(triggered()), this, SLOT(slotAddClip()));
74
75     QAction *addColorClip = addMenu->addAction(KIcon("document-new"), i18n("Add Color Clip"));
76     connect(addColorClip, SIGNAL(triggered()), this, SLOT(slotAddColorClip()));
77
78     QAction *addTitleClip = addMenu->addAction(KIcon("document-new"), i18n("Add Title Clip"));
79     connect(addTitleClip, SIGNAL(triggered()), this, SLOT(slotAddTitleClip()));
80
81     m_deleteAction = m_toolbar->addAction(KIcon("edit-delete"), i18n("Delete Clip"));
82     connect(m_deleteAction, SIGNAL(triggered()), this, SLOT(slotRemoveClip()));
83
84     m_editAction = m_toolbar->addAction(KIcon("document-properties"), i18n("Edit Clip"));
85     connect(m_editAction, SIGNAL(triggered()), this, SLOT(slotEditClip()));
86
87     QAction *addFolderButton = addMenu->addAction(KIcon("folder-new"), i18n("Create Folder"));
88     connect(addFolderButton, SIGNAL(triggered()), this, SLOT(slotAddFolder()));
89
90     addButton->setDefaultAction(addClipButton);
91
92     layout->addWidget(m_toolbar);
93     layout->addWidget(listView);
94     setLayout(layout);
95     //m_toolbar->setEnabled(false);
96
97     searchView->setTreeWidget(listView);
98     listView->setColumnCount(3);
99     QStringList headers;
100     headers << i18n("Thumbnail") << i18n("Filename") << i18n("Description");
101     listView->setHeaderLabels(headers);
102     listView->sortByColumn(1, Qt::AscendingOrder);
103
104     m_menu = new QMenu();
105     m_menu->addAction(addClipButton);
106     m_menu->addAction(addColorClip);
107     m_menu->addAction(addTitleClip);
108     m_menu->addAction(m_editAction);
109     m_menu->addAction(m_deleteAction);
110     m_menu->addAction(addFolderButton);
111     m_menu->insertSeparator(m_deleteAction);
112
113     connect(listView, SIGNAL(itemSelectionChanged()), this, SLOT(slotClipSelected()));
114     connect(listView, SIGNAL(requestMenu(const QPoint &, QTreeWidgetItem *)), this, SLOT(slotContextMenu(const QPoint &, QTreeWidgetItem *)));
115     connect(listView, SIGNAL(addClip()), this, SLOT(slotAddClip()));
116     connect(listView, SIGNAL(addClip(QUrl, const QString &)), this, SLOT(slotAddClip(QUrl, const QString &)));
117     connect(listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotUpdateItemDescription(QTreeWidgetItem *, int)));
118
119     m_listViewDelegate = new ItemDelegate(listView);
120     listView->setItemDelegate(m_listViewDelegate);
121     listView->setIconSize(QSize(60, 40));
122     listView->setSortingEnabled(true);
123 }
124
125 ProjectList::~ProjectList() {
126     delete m_menu;
127     delete m_toolbar;
128 }
129
130 void ProjectList::setRenderer(Render *projectRender) {
131     m_render = projectRender;
132 }
133
134 void ProjectList::slotClipSelected() {
135     ProjectItem *item = (ProjectItem*) listView->currentItem();
136     if (item && !item->isGroup()) emit clipSelected(item->toXml());
137 }
138
139 void ProjectList::slotUpdateItemDescription(QTreeWidgetItem *item, int column) {
140     if (column != 2) return;
141     ProjectItem *clip = (ProjectItem*) item;
142     CLIPTYPE type = clip->clipType();
143     if (type == AUDIO || type == VIDEO || type == AV || type == IMAGE || type == PLAYLIST) {
144         // Use Nepomuk system to store clip description
145         Nepomuk::Resource f(clip->clipUrl().path());
146         f.setDescription(item->text(2));
147         kDebug() << "NEPOMUK, SETTING CLIP: " << clip->clipUrl().path() << ", TO TEXT: " << item->text(2);
148     }
149 }
150
151 void ProjectList::slotEditClip() {
152     kDebug() << "////////////////////////////////////////   DBL CLK";
153 }
154
155
156 void ProjectList::slotEditClip(QTreeWidgetItem *item, int column) {
157     kDebug() << "////////////////////////////////////////   DBL CLK";
158 }
159
160 void ProjectList::slotContextMenu(const QPoint &pos, QTreeWidgetItem *item) {
161     bool enable = false;
162     if (item) {
163         QFrame *w = new QFrame;
164         w->setFrameShape(QFrame::StyledPanel);
165         w->setLineWidth(2);
166         w->setAutoFillBackground(true);
167         QHBoxLayout *layout = new QHBoxLayout;
168         layout->addWidget(new QLabel(i18n("Color:")));
169         layout->addWidget(new KColorButton());
170         layout->addWidget(new QLabel(i18n("Duration:")));
171         layout->addWidget(new KRestrictedLine());
172         w->setLayout(layout);
173         m_listViewDelegate->extendItem(w, listView->currentIndex());
174         enable = true;
175     }
176     m_editAction->setEnabled(enable);
177     m_deleteAction->setEnabled(enable);
178
179     m_menu->popup(pos);
180 }
181
182 void ProjectList::slotRemoveClip() {
183
184     if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!!  NO CMD STK";
185     if (!listView->currentItem()) return;
186     ProjectItem *item = ((ProjectItem *)listView->currentItem());
187     if (item->numReferences() > 0) {
188         if (KMessageBox::questionYesNo(this, i18np("Delete clip <b>%2</b> ?<br>This will also remove the clip in timeline", "Delete clip <b>%2</b> ?<br>This will also remove its %1 clips in timeline", item->numReferences(), item->names().at(1)), i18n("Delete Clip")) != KMessageBox::Yes) return;
189     }
190     m_doc->deleteProjectClip(item->clipId());
191 }
192
193 void ProjectList::selectItemById(const int clipId) {
194     ProjectItem *item = getItemById(clipId);
195     if (item) listView->setCurrentItem(item);
196 }
197
198 void ProjectList::addClip(const QStringList &name, const QDomElement &elem, const int clipId, const KUrl &url, const QString &group, int parentId) {
199     kDebug() << "/////////  ADDING VCLIP=: " << name;
200     ProjectItem *item;
201     ProjectItem *groupItem = NULL;
202     QString groupName;
203     if (group.isEmpty()) groupName = elem.attribute("group", QString::null);
204     else groupName = group;
205     if (elem.isNull() && url.isEmpty()) {
206         // this is a folder
207         groupName = name.at(1);
208         QList<QTreeWidgetItem *> groupList = listView->findItems(groupName, Qt::MatchExactly, 1);
209         if (groupList.isEmpty())  {
210             (void) new ProjectItem(listView, name, clipId);
211         }
212         return;
213     }
214
215     if (parentId != -1) {
216         groupItem = getItemById(parentId);
217     } else if (!groupName.isEmpty()) {
218         // Clip is in a group
219         QList<QTreeWidgetItem *> groupList = listView->findItems(groupName, Qt::MatchExactly, 1);
220
221         if (groupList.isEmpty())  {
222             QStringList itemName;
223             itemName << QString::null << groupName;
224             kDebug() << "-------  CREATING NEW GRP: " << itemName;
225             groupItem = new ProjectItem(listView, itemName, m_clipIdCounter++);
226         } else groupItem = (ProjectItem *) groupList.first();
227     }
228     if (groupItem) item = new ProjectItem(groupItem, name, elem, clipId);
229     else item = new ProjectItem(listView, name, elem, clipId);
230     if (!url.isEmpty()) {
231         // if file has Nepomuk comment, use it
232         Nepomuk::Resource f(url.path());
233         QString annotation = f.description();
234         if (!annotation.isEmpty()) item->setText(2, annotation);
235         QString resource = url.path();
236         if (resource.endsWith("westley") || resource.endsWith("kdenlive")) {
237             QString tmpfile;
238             QDomDocument doc;
239             if (KIO::NetAccess::download(url, tmpfile, 0)) {
240                 QFile file(tmpfile);
241                 if (file.open(QIODevice::ReadOnly)) {
242                     doc.setContent(&file, false);
243                     file.close();
244                 }
245                 KIO::NetAccess::removeTempFile(tmpfile);
246
247                 QDomNodeList subProds = doc.elementsByTagName("producer");
248                 int ct = subProds.count();
249                 for (int i = 0; i <  ct ; i++) {
250                     QDomElement e = subProds.item(i).toElement();
251                     if (!e.isNull()) {
252                         addProducer(e, clipId);
253                     }
254                 }
255             }
256         }
257
258     }
259
260     if (elem.isNull()) {
261         QDomDocument doc;
262         QDomElement element = doc.createElement("producer");
263         element.setAttribute("resource", url.path());
264         emit getFileProperties(element, clipId);
265     } else emit getFileProperties(elem, clipId);
266     selectItemById(clipId);
267 }
268
269 void ProjectList::slotDeleteClip(int clipId) {
270     ProjectItem *item = getItemById(clipId);
271     if (item) delete item;
272 }
273
274 void ProjectList::slotAddFolder() {
275     /*
276       QString folderName = KInputDialog::getText(i18n("New Folder"), i18n("Enter new folder name: "));
277       if (folderName.isEmpty()) return;
278       QStringList itemEntry;
279       itemEntry.append(QString::null);
280       itemEntry.append(folderName);
281       AddClipCommand *command = new AddClipCommand(this, itemEntry, QDomElement(), m_clipIdCounter++, KUrl(), folderName, true);
282       m_commandStack->push(command);*/
283 }
284
285 void ProjectList::slotAddClip(DocClipBase *clip) {
286     ProjectItem *item = new ProjectItem(listView, clip);
287     emit getFileProperties(clip->toXML(), clip->getId());
288 }
289
290 void ProjectList::slotUpdateClip(int id) {
291     ProjectItem *item = getItemById(id);
292     item->setData(1, UsageRole, QString::number(item->numReferences()));
293 }
294
295 void ProjectList::slotAddClip(QUrl givenUrl, const QString &group) {
296     if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!!  NO CMD STK";
297     KUrl::List list;
298     if (givenUrl.isEmpty())
299         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/mpeg 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");
300     else list.append(givenUrl);
301     if (list.isEmpty()) return;
302     KUrl::List::Iterator it;
303
304     for (it = list.begin(); it != list.end(); it++) {
305         m_doc->slotAddClipFile(*it, group);
306     }
307 }
308
309 void ProjectList::slotAddColorClip() {
310     if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!!  NO CMD STK";
311     QDialog *dia = new QDialog;
312     Ui::ColorClip_UI *dia_ui = new Ui::ColorClip_UI();
313     dia_ui->setupUi(dia);
314     dia_ui->clip_name->setText(i18n("Color Clip"));
315     dia_ui->clip_duration->setText(KdenliveSettings::color_duration());
316     if (dia->exec() == QDialog::Accepted) {
317         QString color = dia_ui->clip_color->color().name();
318         color = color.replace(0, 1, "0x") + "ff";
319         m_doc->slotAddColorClipFile(dia_ui->clip_name->text(), color, dia_ui->clip_duration->text(), QString::null);
320     }
321     delete dia_ui;
322     delete dia;
323 }
324
325 void ProjectList::slotAddTitleClip() {
326
327     if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!!  NO CMD STK";
328     //QDialog *dia = new QDialog;
329
330     TitleWidget *dia_ui = new TitleWidget();
331     //dia_ui->setupUi(dia);
332     //dia_ui->clip_name->setText(i18n("Title Clip"));
333     //dia_ui->clip_duration->setText(KdenliveSettings::color_duration());
334     if (dia_ui->exec() == QDialog::Accepted) {
335         //QString color = dia_ui->clip_color->color().name();
336         //color = color.replace(0, 1, "0x") + "ff";
337         //m_doc->slotAddColorClipFile(dia_ui->clip_name->text(), color, dia_ui->clip_duration->text(), QString::null);
338     }
339     delete dia_ui;
340     //delete dia;
341 }
342 void ProjectList::setDocument(KdenliveDoc *doc) {
343     listView->clear();
344     QList <DocClipBase*> list = doc->clipManager()->documentClipList();
345     for (int i = 0; i < list.count(); i++) {
346         slotAddClip(list.at(i));
347     }
348
349     m_fps = doc->fps();
350     m_timecode = doc->timecode();
351     m_commandStack = doc->commandStack();
352     m_doc = doc;
353     /*    QDomNodeList prods = doc->producersList();
354         int ct = prods.count();
355         kDebug() << "////////////  SETTING DOC, FOUND CLIPS: " << prods.count();
356         listView->clear();
357         for (int i = 0; i <  ct ; i++) {
358             QDomElement e = prods.item(i).toElement();
359             kDebug() << "// IMPORT: " << i << ", :" << e.attribute("id", "non") << ", NAME: " << e.attribute("name", "non");
360             if (!e.isNull()) addProducer(e);
361         }*/
362     QTreeWidgetItem *first = listView->topLevelItem(0);
363     if (first) listView->setCurrentItem(first);
364     m_toolbar->setEnabled(true);
365 }
366
367 QDomElement ProjectList::producersList() {
368     QDomDocument doc;
369     QDomElement prods = doc.createElement("producerlist");
370     doc.appendChild(prods);
371     kDebug() << "////////////  PRO LIST BUILD PRDSLIST ";
372     QTreeWidgetItemIterator it(listView);
373     while (*it) {
374         if (!((ProjectItem *)(*it))->isGroup())
375             prods.appendChild(doc.importNode(((ProjectItem *)(*it))->toXml(), true));
376         ++it;
377     }
378     return prods;
379 }
380
381
382 void ProjectList::slotReplyGetFileProperties(int clipId, const QMap < QString, QString > &properties, const QMap < QString, QString > &metadata) {
383     ProjectItem *item = getItemById(clipId);
384     if (item) {
385         item->setProperties(properties, metadata);
386         listView->setCurrentItem(item);
387         emit receivedClipDuration(clipId, item->clipMaxDuration());
388     }
389 }
390
391
392
393 void ProjectList::slotReplyGetImage(int clipId, int pos, const QPixmap &pix, int w, int h) {
394     ProjectItem *item = getItemById(clipId);
395     if (item) item->setIcon(0, pix);
396 }
397
398 ProjectItem *ProjectList::getItemById(int id) {
399     QTreeWidgetItemIterator it(listView);
400     while (*it) {
401         if (((ProjectItem *)(*it))->clipId() == id)
402             break;
403         ++it;
404     }
405     return ((ProjectItem *)(*it));
406 }
407
408
409 void ProjectList::addProducer(QDomElement producer, int parentId) {
410     if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!!  NO CMD STK";
411     CLIPTYPE type = (CLIPTYPE) producer.attribute("type").toInt();
412
413     /*QDomDocument doc;
414     QDomElement prods = doc.createElement("list");
415     doc.appendChild(prods);
416     prods.appendChild(doc.importNode(producer, true));*/
417
418
419     //kDebug()<<"//////  ADDING PRODUCER:\n "<<doc.toString()<<"\n+++++++++++++++++";
420     int id = producer.attribute("id").toInt();
421     QString groupName = producer.attribute("group");
422     if (id >= m_clipIdCounter) m_clipIdCounter = id + 1;
423     else if (id == 0) id = m_clipIdCounter++;
424
425     if (parentId != -1) {
426         // item is a westley playlist, adjust subproducers ids
427         id = (parentId + 1) * 10000 + id;
428     }
429     if (type == AUDIO || type == VIDEO || type == AV || type == IMAGE  || type == PLAYLIST) {
430         KUrl resource = KUrl(producer.attribute("resource"));
431         if (!resource.isEmpty()) {
432             QStringList itemEntry;
433             itemEntry.append(QString::null);
434             itemEntry.append(resource.fileName());
435             addClip(itemEntry, producer, id, resource, groupName, parentId);
436         }
437     } else if (type == COLOR) {
438         QString colour = producer.attribute("colour");
439         QPixmap pix(60, 40);
440         colour = colour.replace(0, 2, "#");
441         pix.fill(QColor(colour.left(7)));
442         QStringList itemEntry;
443         itemEntry.append(QString::null);
444         itemEntry.append(producer.attribute("name", i18n("Color clip")));
445         addClip(itemEntry, producer, id, KUrl(), groupName, parentId);
446     }
447
448 }
449
450 #include "projectlist.moc"