1 /***************************************************************************
2 * Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org) *
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. *
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. *
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 ***************************************************************************/
21 #include <QMouseEvent>
22 #include <QStylePainter>
30 #include <KFileDialog>
31 #include <KInputDialog>
32 #include <kio/netaccess.h>
33 #include <KMessageBox>
35 #include <nepomuk/global.h>
36 #include <nepomuk/resource.h>
37 #include <nepomuk/tag.h>
39 #include "projectlist.h"
40 #include "projectitem.h"
41 #include "kdenlivesettings.h"
42 #include "ui_colorclip_ui.h"
44 #include "definitions.h"
45 #include "titlewidget.h"
49 ProjectList::ProjectList(QWidget *parent)
50 : QWidget(parent), m_render(NULL), m_fps(-1), m_commandStack(NULL)
53 QWidget *vbox = new QWidget;
54 listView = new ProjectListView(this);;
55 QVBoxLayout *layout = new QVBoxLayout;
59 searchView = new KTreeWidgetSearchLine (this);
60 m_toolbar = new QToolBar("projectToolBar", this);
61 m_toolbar->addWidget (searchView);
63 QToolButton *addButton = new QToolButton( m_toolbar );
64 QMenu *addMenu = new QMenu(this);
65 addButton->setMenu( addMenu );
66 addButton->setPopupMode(QToolButton::MenuButtonPopup);
67 m_toolbar->addWidget (addButton);
69 QAction *addClipButton = addMenu->addAction (KIcon("document-new"), i18n("Add Clip"));
70 connect(addClipButton, SIGNAL(triggered()), this, SLOT(slotAddClip()));
72 QAction *addColorClip = addMenu->addAction (KIcon("document-new"), i18n("Add Color Clip"));
73 connect(addColorClip, SIGNAL(triggered()), this, SLOT(slotAddColorClip()));
75 QAction *addTitleClip = addMenu->addAction (KIcon("document-new"), i18n("Add Title Clip"));
76 connect(addTitleClip, SIGNAL(triggered()), this, SLOT(slotAddTitleClip()));
78 m_deleteAction = m_toolbar->addAction (KIcon("edit-delete"), i18n("Delete Clip"));
79 connect(m_deleteAction, SIGNAL(triggered()), this, SLOT(slotRemoveClip()));
81 m_editAction = m_toolbar->addAction (KIcon("document-properties"), i18n("Edit Clip"));
82 connect(m_editAction, SIGNAL(triggered()), this, SLOT(slotEditClip()));
84 QAction *addFolderButton = addMenu->addAction (KIcon("folder-new"), i18n("Create Folder"));
85 connect(addFolderButton, SIGNAL(triggered()), this, SLOT(slotAddFolder()));
87 addButton->setDefaultAction( addClipButton );
89 layout->addWidget( m_toolbar );
90 layout->addWidget( listView );
92 //m_toolbar->setEnabled(false);
94 searchView->setTreeWidget(listView);
95 listView->setColumnCount(3);
97 headers<<i18n("Thumbnail")<<i18n("Filename")<<i18n("Description");
98 listView->setHeaderLabels(headers);
99 listView->sortByColumn(1, Qt::AscendingOrder);
101 m_menu = new QMenu();
102 m_menu->addAction(addClipButton);
103 m_menu->addAction(addColorClip);
104 m_menu->addAction(addTitleClip);
105 m_menu->addAction(m_editAction);
106 m_menu->addAction(m_deleteAction);
107 m_menu->addAction(addFolderButton);
108 m_menu->insertSeparator(m_deleteAction);
110 connect(listView, SIGNAL(itemSelectionChanged()), this, SLOT(slotClipSelected()));
111 connect(listView, SIGNAL(requestMenu ( const QPoint &, QTreeWidgetItem * )), this, SLOT(slotContextMenu(const QPoint &, QTreeWidgetItem *)));
112 connect(listView, SIGNAL(addClip ()), this, SLOT(slotAddClip()));
113 connect(listView, SIGNAL(addClip (QUrl, const QString &)), this, SLOT(slotAddClip(QUrl, const QString &)));
114 connect(listView, SIGNAL (itemChanged ( QTreeWidgetItem *, int )), this, SLOT(slotUpdateItemDescription(QTreeWidgetItem *, int )));
116 m_listViewDelegate = new ItemDelegate(listView);
117 listView->setItemDelegate(m_listViewDelegate);
118 listView->setIconSize(QSize(60, 40));
119 listView->setSortingEnabled (true);
122 ProjectList::~ProjectList()
128 void ProjectList::setRenderer(Render *projectRender)
130 m_render = projectRender;
133 void ProjectList::slotClipSelected()
135 ProjectItem *item = (ProjectItem*) listView->currentItem();
136 if (item && !item->isGroup()) emit clipSelected(item->toXml());
139 void ProjectList::slotUpdateItemDescription( QTreeWidgetItem *item, int column)
141 if (column != 2) return;
142 ProjectItem *clip = (ProjectItem*) item;
143 CLIPTYPE type = clip->clipType();
144 if (type == AUDIO || type == VIDEO || type == AV || type == IMAGE || type == PLAYLIST) {
145 // Use Nepomuk system to store clip description
146 Nepomuk::Resource f( clip->clipUrl().path() );
147 f.setDescription(item->text(2));
148 kDebug()<<"NEPOMUK, SETTING CLIP: "<<clip->clipUrl().path()<<", TO TEXT: "<<item->text(2);
152 void ProjectList::slotEditClip()
154 kDebug()<<"//////////////////////////////////////// DBL CLK";
158 void ProjectList::slotEditClip(QTreeWidgetItem *item, int column)
160 kDebug()<<"//////////////////////////////////////// DBL CLK";
163 void ProjectList::slotContextMenu( const QPoint &pos, QTreeWidgetItem *item )
167 QFrame *w = new QFrame;
168 w->setFrameShape(QFrame::StyledPanel);
170 w->setAutoFillBackground(true);
171 QHBoxLayout *layout = new QHBoxLayout;
172 layout->addWidget( new QLabel(i18n("Color:")));
173 layout->addWidget( new KColorButton());
174 layout->addWidget( new QLabel(i18n("Duration:")));
175 layout->addWidget( new KRestrictedLine());
176 w->setLayout( layout );
177 m_listViewDelegate->extendItem(w, listView->currentIndex());
180 m_editAction->setEnabled(enable);
181 m_deleteAction->setEnabled(enable);
186 void ProjectList::slotRemoveClip()
189 if (!m_commandStack) kDebug()<<"!!!!!!!!!!!!!!!! NO CMD STK";
190 if (!listView->currentItem()) return;
191 ProjectItem *item = ((ProjectItem *)listView->currentItem());
192 if (item->numReferences() > 0) {
193 if (KMessageBox::questionYesNo(this, i18n("Delete clip <b>%1</b> ?<br>This will also remove its %2 clips in timeline").arg(item->names().at(1)).arg(item->numReferences()), i18n("Delete Clip")) != KMessageBox::Yes) return;
195 m_doc->deleteProjectClip(item->clipId());
198 void ProjectList::selectItemById(const int clipId)
200 ProjectItem *item = getItemById(clipId);
201 if (item) listView->setCurrentItem(item);
204 void ProjectList::addClip(const QStringList &name, const QDomElement &elem, const int clipId, const KUrl &url, const QString &group, int parentId)
206 kDebug()<<"///////// ADDING VCLIP=: "<<name;
208 ProjectItem *groupItem = NULL;
210 if (group.isEmpty()) groupName = elem.attribute("group", QString::null);
211 else groupName = group;
212 if (elem.isNull() && url.isEmpty()) {
214 groupName = name.at(1);
215 QList<QTreeWidgetItem *> groupList = listView->findItems(groupName, Qt::MatchExactly, 1);
216 if (groupList.isEmpty()) {
217 (void) new ProjectItem(listView, name, clipId);
222 if (parentId != -1) {
223 groupItem = getItemById(parentId);
225 else if (!groupName.isEmpty()) {
226 // Clip is in a group
227 QList<QTreeWidgetItem *> groupList = listView->findItems(groupName, Qt::MatchExactly, 1);
229 if (groupList.isEmpty()) {
230 QStringList itemName;
231 itemName<<QString::null<<groupName;
232 kDebug()<<"------- CREATING NEW GRP: "<<itemName;
233 groupItem = new ProjectItem(listView, itemName, m_clipIdCounter++);
235 else groupItem = (ProjectItem *) groupList.first();
237 if (groupItem) item = new ProjectItem(groupItem, name, elem, clipId);
238 else item = new ProjectItem(listView, name, elem, clipId);
239 if (!url.isEmpty()) {
240 // if file has Nepomuk comment, use it
241 Nepomuk::Resource f( url.path() );
242 QString annotation = f.description();
243 if (!annotation.isEmpty()) item->setText(2, annotation);
244 QString resource = url.path();
245 if (resource.endsWith("westley") || resource.endsWith("kdenlive")) {
248 if (KIO::NetAccess::download(url, tmpfile, 0)) {
250 if (file.open(QIODevice::ReadOnly)) {
251 doc.setContent(&file, false);
254 KIO::NetAccess::removeTempFile(tmpfile);
256 QDomNodeList subProds = doc.elementsByTagName("producer");
257 int ct = subProds.count();
258 for (int i = 0; i < ct ; i++)
260 QDomElement e = subProds.item(i).toElement();
262 addProducer(e, clipId);
270 if (elem.isNull() ) {
272 QDomElement element = doc.createElement("producer");
273 element.setAttribute("resource", url.path());
274 emit getFileProperties(element, clipId);
276 else emit getFileProperties(elem, clipId);
277 selectItemById(clipId);
280 void ProjectList::slotDeleteClip( int clipId)
282 ProjectItem *item = getItemById(clipId);
283 if (item) delete item;
286 void ProjectList::slotAddFolder()
289 QString folderName = KInputDialog::getText(i18n("New Folder"), i18n("Enter new folder name: "));
290 if (folderName.isEmpty()) return;
291 QStringList itemEntry;
292 itemEntry.append(QString::null);
293 itemEntry.append(folderName);
294 AddClipCommand *command = new AddClipCommand(this, itemEntry, QDomElement(), m_clipIdCounter++, KUrl(), folderName, true);
295 m_commandStack->push(command);*/
298 void ProjectList::slotAddClip(DocClipBase *clip)
300 ProjectItem *item = new ProjectItem(listView, clip);
301 listView->setCurrentItem(item);
302 emit getFileProperties(clip->toXML(), clip->getId());
305 void ProjectList::slotUpdateClip(int id)
307 ProjectItem *item = getItemById(id);
308 item->setData(1, UsageRole, QString::number(item->numReferences()));
311 void ProjectList::slotAddClip(QUrl givenUrl, const QString &group)
313 if (!m_commandStack) kDebug()<<"!!!!!!!!!!!!!!!! NO CMD STK";
315 if (givenUrl.isEmpty())
316 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");
317 else list.append(givenUrl);
318 if (list.isEmpty()) return;
319 KUrl::List::Iterator it;
321 for (it = list.begin(); it != list.end(); it++) {
322 m_doc->slotAddClipFile(*it, group);
326 void ProjectList::slotAddColorClip()
328 if (!m_commandStack) kDebug()<<"!!!!!!!!!!!!!!!! NO CMD STK";
329 QDialog *dia = new QDialog;
330 Ui::ColorClip_UI *dia_ui = new Ui::ColorClip_UI();
331 dia_ui->setupUi(dia);
332 dia_ui->clip_name->setText(i18n("Color Clip"));
333 dia_ui->clip_duration->setText(KdenliveSettings::color_duration());
334 if (dia->exec() == QDialog::Accepted)
336 QString color = dia_ui->clip_color->color().name();
337 color = color.replace(0, 1, "0x") + "ff";
338 m_doc->slotAddColorClipFile(dia_ui->clip_name->text(), color, dia_ui->clip_duration->text(), QString::null);
344 void ProjectList::slotAddTitleClip()
347 if (!m_commandStack) kDebug()<<"!!!!!!!!!!!!!!!! NO CMD STK";
348 //QDialog *dia = new QDialog;
350 TitleWidget *dia_ui = new TitleWidget();
351 //dia_ui->setupUi(dia);
352 //dia_ui->clip_name->setText(i18n("Title Clip"));
353 //dia_ui->clip_duration->setText(KdenliveSettings::color_duration());
354 if (dia_ui->exec() == QDialog::Accepted)
356 //QString color = dia_ui->clip_color->color().name();
357 //color = color.replace(0, 1, "0x") + "ff";
358 //m_doc->slotAddColorClipFile(dia_ui->clip_name->text(), color, dia_ui->clip_duration->text(), QString::null);
363 void ProjectList::setDocument(KdenliveDoc *doc)
366 m_timecode = doc->timecode();
367 m_commandStack = doc->commandStack();
369 QDomNodeList prods = doc->producersList();
370 int ct = prods.count();
371 kDebug()<<"//////////// SETTING DOC, FOUND CLIPS: "<<prods.count();
373 for (int i = 0; i < ct ; i++)
375 QDomElement e = prods.item(i).toElement();
376 kDebug()<<"// IMPORT: "<<i<<", :"<<e.attribute("id", "non")<<", NAME: "<<e.attribute("name", "non");
377 if (!e.isNull()) addProducer(e);
379 QTreeWidgetItem *first = listView->topLevelItem(0);
380 if (first) listView->setCurrentItem(first);
381 m_toolbar->setEnabled(true);
384 QDomElement ProjectList::producersList()
387 QDomElement prods = doc.createElement("producerlist");
388 doc.appendChild(prods);
389 kDebug()<<"//////////// PRO LIST BUILD PRDSLIST ";
390 QTreeWidgetItemIterator it(listView);
392 if (!((ProjectItem *)(*it))->isGroup())
393 prods.appendChild(doc.importNode(((ProjectItem *)(*it))->toXml(), true));
400 void ProjectList::slotReplyGetFileProperties(int clipId, const QMap < QString, QString > &properties, const QMap < QString, QString > &metadata)
402 ProjectItem *item = getItemById(clipId);
404 item->setProperties(properties, metadata);
405 emit receivedClipDuration(clipId, item->clipMaxDuration());
411 void ProjectList::slotReplyGetImage(int clipId, int pos, const QPixmap &pix, int w, int h)
413 ProjectItem *item = getItemById(clipId);
414 if (item) item->setIcon(0, pix);
417 ProjectItem *ProjectList::getItemById(int id)
419 QTreeWidgetItemIterator it(listView);
421 if (((ProjectItem *)(*it))->clipId() == id)
425 return ((ProjectItem *)(*it));
429 void ProjectList::addProducer(QDomElement producer, int parentId)
431 if (!m_commandStack) kDebug()<<"!!!!!!!!!!!!!!!! NO CMD STK";
432 CLIPTYPE type = (CLIPTYPE) producer.attribute("type").toInt();
435 QDomElement prods = doc.createElement("list");
436 doc.appendChild(prods);
437 prods.appendChild(doc.importNode(producer, true));*/
440 //kDebug()<<"////// ADDING PRODUCER:\n "<<doc.toString()<<"\n+++++++++++++++++";
441 int id = producer.attribute("id").toInt();
442 QString groupName = producer.attribute("group");
443 if (id >= m_clipIdCounter) m_clipIdCounter = id + 1;
444 else if (id == 0) id = m_clipIdCounter++;
446 if (parentId != -1) {
447 // item is a westley playlist, adjust subproducers ids
448 id = (parentId + 1) * 10000 + id;
450 if (type == AUDIO || type == VIDEO || type == AV || type == IMAGE || type == PLAYLIST)
452 KUrl resource = KUrl(producer.attribute("resource"));
453 if (!resource.isEmpty()) {
454 QStringList itemEntry;
455 itemEntry.append(QString::null);
456 itemEntry.append(resource.fileName());
457 addClip(itemEntry, producer, id, resource, groupName, parentId);
460 else if (type == COLOR) {
461 QString colour = producer.attribute("colour");
463 colour = colour.replace(0, 2, "#");
464 pix.fill(QColor(colour.left(7)));
465 QStringList itemEntry;
466 itemEntry.append(QString::null);
467 itemEntry.append(producer.attribute("name", i18n("Color clip")));
468 addClip(itemEntry, producer, id, KUrl(), groupName, parentId);
473 #include "projectlist.moc"