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"
48 ProjectList::ProjectList(QWidget *parent)
49 : QWidget(parent), m_render(NULL), m_fps(-1), m_commandStack(NULL)
52 QWidget *vbox = new QWidget;
53 listView = new ProjectListView(this);;
54 QVBoxLayout *layout = new QVBoxLayout;
58 searchView = new KTreeWidgetSearchLine (this);
59 m_toolbar = new QToolBar("projectToolBar", this);
60 m_toolbar->addWidget (searchView);
62 QToolButton *addButton = new QToolButton( m_toolbar );
63 QMenu *addMenu = new QMenu(this);
64 addButton->setMenu( addMenu );
65 addButton->setPopupMode(QToolButton::MenuButtonPopup);
66 m_toolbar->addWidget (addButton);
68 QAction *addClipButton = addMenu->addAction (KIcon("document-new"), i18n("Add Clip"));
69 connect(addClipButton, SIGNAL(triggered()), this, SLOT(slotAddClip()));
71 QAction *addColorClip = addMenu->addAction (KIcon("document-new"), i18n("Add Color Clip"));
72 connect(addColorClip, SIGNAL(triggered()), this, SLOT(slotAddColorClip()));
74 m_deleteAction = m_toolbar->addAction (KIcon("edit-delete"), i18n("Delete Clip"));
75 connect(m_deleteAction, SIGNAL(triggered()), this, SLOT(slotRemoveClip()));
77 m_editAction = m_toolbar->addAction (KIcon("document-properties"), i18n("Edit Clip"));
78 connect(m_editAction, SIGNAL(triggered()), this, SLOT(slotEditClip()));
80 QAction *addFolderButton = addMenu->addAction (KIcon("folder-new"), i18n("Create Folder"));
81 connect(addFolderButton, SIGNAL(triggered()), this, SLOT(slotAddFolder()));
83 addButton->setDefaultAction( addClipButton );
85 layout->addWidget( m_toolbar );
86 layout->addWidget( listView );
88 //m_toolbar->setEnabled(false);
90 searchView->setTreeWidget(listView);
91 listView->setColumnCount(3);
93 headers<<i18n("Thumbnail")<<i18n("Filename")<<i18n("Description");
94 listView->setHeaderLabels(headers);
95 listView->sortByColumn(1, Qt::AscendingOrder);
98 m_menu->addAction(addClipButton);
99 m_menu->addAction(addColorClip);
100 m_menu->addAction(m_editAction);
101 m_menu->addAction(m_deleteAction);
102 m_menu->addAction(addFolderButton);
103 m_menu->insertSeparator(m_deleteAction);
105 connect(listView, SIGNAL(itemSelectionChanged()), this, SLOT(slotClipSelected()));
106 connect(listView, SIGNAL(requestMenu ( const QPoint &, QTreeWidgetItem * )), this, SLOT(slotContextMenu(const QPoint &, QTreeWidgetItem *)));
107 connect(listView, SIGNAL(addClip ()), this, SLOT(slotAddClip()));
108 connect(listView, SIGNAL(addClip (QUrl, const QString &)), this, SLOT(slotAddClip(QUrl, const QString &)));
109 connect(listView, SIGNAL (itemChanged ( QTreeWidgetItem *, int )), this, SLOT(slotUpdateItemDescription(QTreeWidgetItem *, int )));
111 m_listViewDelegate = new ItemDelegate(listView);
112 listView->setItemDelegate(m_listViewDelegate);
113 listView->setIconSize(QSize(60, 40));
114 listView->setSortingEnabled (true);
117 ProjectList::~ProjectList()
123 void ProjectList::setRenderer(Render *projectRender)
125 m_render = projectRender;
128 void ProjectList::slotClipSelected()
130 ProjectItem *item = (ProjectItem*) listView->currentItem();
131 if (item && !item->isGroup()) emit clipSelected(item->toXml());
134 void ProjectList::slotUpdateItemDescription( QTreeWidgetItem *item, int column)
136 if (column != 2) return;
137 ProjectItem *clip = (ProjectItem*) item;
138 CLIPTYPE type = clip->clipType();
139 if (type == AUDIO || type == VIDEO || type == AV || type == IMAGE || type == PLAYLIST) {
140 // Use Nepomuk system to store clip description
141 Nepomuk::Resource f( clip->clipUrl().path() );
142 f.setDescription(item->text(2));
143 kDebug()<<"NEPOMUK, SETTING CLIP: "<<clip->clipUrl().path()<<", TO TEXT: "<<item->text(2);
147 void ProjectList::slotEditClip()
149 kDebug()<<"//////////////////////////////////////// DBL CLK";
153 void ProjectList::slotEditClip(QTreeWidgetItem *item, int column)
155 kDebug()<<"//////////////////////////////////////// DBL CLK";
158 void ProjectList::slotContextMenu( const QPoint &pos, QTreeWidgetItem *item )
162 QFrame *w = new QFrame;
163 w->setFrameShape(QFrame::StyledPanel);
165 w->setAutoFillBackground(true);
166 QHBoxLayout *layout = new QHBoxLayout;
167 layout->addWidget( new QLabel(i18n("Color:")));
168 layout->addWidget( new KColorButton());
169 layout->addWidget( new QLabel(i18n("Duration:")));
170 layout->addWidget( new KRestrictedLine());
171 w->setLayout( layout );
172 m_listViewDelegate->extendItem(w, listView->currentIndex());
175 m_editAction->setEnabled(enable);
176 m_deleteAction->setEnabled(enable);
181 void ProjectList::slotRemoveClip()
184 if (!m_commandStack) kDebug()<<"!!!!!!!!!!!!!!!! NO CMD STK";
185 if (!listView->currentItem()) return;
186 ProjectItem *item = ((ProjectItem *)listView->currentItem());
187 if (!item) kDebug()<<"/////////////// ERROR NOT FOUND";
188 if (KMessageBox::questionYesNo(this, i18n("Delete clip <b>%1</b> ?").arg(item->names().at(1)), i18n("Delete Clip")) != KMessageBox::Yes) return;
189 AddClipCommand *command = new AddClipCommand(this, item->names(), item->toXml(), item->clipId(), item->clipUrl(), item->groupName(), false);
190 m_commandStack->push(command);*/
193 void ProjectList::selectItemById(const int clipId)
195 ProjectItem *item = getItemById(clipId);
196 if (item) listView->setCurrentItem(item);
199 void ProjectList::addClip(const QStringList &name, const QDomElement &elem, const int clipId, const KUrl &url, const QString &group, int parentId)
201 kDebug()<<"///////// ADDING VCLIP=: "<<name;
203 ProjectItem *groupItem = NULL;
205 if (group.isEmpty()) groupName = elem.attribute("group", QString::null);
206 else groupName = group;
207 if (elem.isNull() && url.isEmpty()) {
209 groupName = name.at(1);
210 QList<QTreeWidgetItem *> groupList = listView->findItems(groupName, Qt::MatchExactly, 1);
211 if (groupList.isEmpty()) {
212 (void) new ProjectItem(listView, name, clipId);
217 if (parentId != -1) {
218 groupItem = getItemById(parentId);
220 else if (!groupName.isEmpty()) {
221 // Clip is in a group
222 QList<QTreeWidgetItem *> groupList = listView->findItems(groupName, Qt::MatchExactly, 1);
224 if (groupList.isEmpty()) {
225 QStringList itemName;
226 itemName<<QString::null<<groupName;
227 kDebug()<<"------- CREATING NEW GRP: "<<itemName;
228 groupItem = new ProjectItem(listView, itemName, m_clipIdCounter++);
230 else groupItem = (ProjectItem *) groupList.first();
232 if (groupItem) item = new ProjectItem(groupItem, name, elem, clipId);
233 else item = new ProjectItem(listView, name, elem, clipId);
234 if (!url.isEmpty()) {
235 item->setData(1, FullPathRole, url.path());
236 // if file has Nepomuk comment, use it
237 Nepomuk::Resource f( url.path() );
238 QString annotation = f.description();
239 if (!annotation.isEmpty()) item->setText(2, annotation);
240 QString resource = url.path();
241 if (resource.endsWith("westley") || resource.endsWith("kdenlive")) {
244 if (KIO::NetAccess::download(url, tmpfile, 0)) {
246 if (file.open(QIODevice::ReadOnly)) {
247 doc.setContent(&file, false);
250 KIO::NetAccess::removeTempFile(tmpfile);
252 QDomNodeList subProds = doc.elementsByTagName("producer");
253 int ct = subProds.count();
254 for (int i = 0; i < ct ; i++)
256 QDomElement e = subProds.item(i).toElement();
258 addProducer(e, clipId);
266 if (elem.isNull() ) {
268 QDomElement element = doc.createElement("producer");
269 element.setAttribute("resource", url.path());
270 emit getFileProperties(element, clipId);
272 else emit getFileProperties(elem, clipId);
273 selectItemById(clipId);
276 void ProjectList::deleteClip(const int clipId)
278 ProjectItem *item = getItemById(clipId);
279 if (item) delete item;
282 void ProjectList::slotAddFolder()
285 QString folderName = KInputDialog::getText(i18n("New Folder"), i18n("Enter new folder name: "));
286 if (folderName.isEmpty()) return;
287 QStringList itemEntry;
288 itemEntry.append(QString::null);
289 itemEntry.append(folderName);
290 AddClipCommand *command = new AddClipCommand(this, itemEntry, QDomElement(), m_clipIdCounter++, KUrl(), folderName, true);
291 m_commandStack->push(command);*/
294 void ProjectList::slotAddClip(DocClipBase *clip)
296 ProjectItem *item = new ProjectItem(listView, clip);
297 listView->setCurrentItem(item);
299 if (clip->clipType() != COLOR) {
300 Nepomuk::Resource f( clip->fileURL().path() );
301 QString annotation = f.description();
302 if (!annotation.isEmpty()) item->setText(2, annotation);
304 emit getFileProperties(clip->toXML(), clip->getId());
307 void ProjectList::slotAddClip(QUrl givenUrl, const QString &group)
309 if (!m_commandStack) kDebug()<<"!!!!!!!!!!!!!!!! NO CMD STK";
311 if (givenUrl.isEmpty())
312 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");
313 else list.append(givenUrl);
314 if (list.isEmpty()) return;
315 KUrl::List::Iterator it;
317 for (it = list.begin(); it != list.end(); it++) {
318 m_doc->slotAddClipFile(*it, group);
322 void ProjectList::slotAddColorClip()
324 if (!m_commandStack) kDebug()<<"!!!!!!!!!!!!!!!! NO CMD STK";
325 QDialog *dia = new QDialog;
326 Ui::ColorClip_UI *dia_ui = new Ui::ColorClip_UI();
327 dia_ui->setupUi(dia);
328 dia_ui->clip_name->setText(i18n("Color Clip"));
329 dia_ui->clip_duration->setText(KdenliveSettings::color_duration());
330 if (dia->exec() == QDialog::Accepted)
332 QString color = dia_ui->clip_color->color().name();
333 color = color.replace(0, 1, "0x") + "ff";
334 m_doc->slotAddColorClipFile(dia_ui->clip_name->text(), color, dia_ui->clip_duration->text(), QString::null);
340 void ProjectList::setDocument(KdenliveDoc *doc)
343 m_timecode = doc->timecode();
344 m_commandStack = doc->commandStack();
346 QDomNodeList prods = doc->producersList();
347 int ct = prods.count();
348 kDebug()<<"//////////// SETTING DOC, FOUND CLIPS: "<<prods.count();
350 for (int i = 0; i < ct ; i++)
352 QDomElement e = prods.item(i).toElement();
353 kDebug()<<"// IMPORT: "<<i<<", :"<<e.attribute("id", "non")<<", NAME: "<<e.attribute("name", "non");
354 if (!e.isNull()) addProducer(e);
356 QTreeWidgetItem *first = listView->topLevelItem(0);
357 if (first) listView->setCurrentItem(first);
358 m_toolbar->setEnabled(true);
361 QDomElement ProjectList::producersList()
364 QDomElement prods = doc.createElement("producerlist");
365 doc.appendChild(prods);
366 kDebug()<<"//////////// PRO LIST BUILD PRDSLIST ";
367 QTreeWidgetItemIterator it(listView);
369 if (!((ProjectItem *)(*it))->isGroup())
370 prods.appendChild(doc.importNode(((ProjectItem *)(*it))->toXml(), true));
377 void ProjectList::slotReplyGetFileProperties(int clipId, const QMap < QString, QString > &properties, const QMap < QString, QString > &metadata)
379 ProjectItem *item = getItemById(clipId);
381 item->setProperties(properties, metadata);
382 emit receivedClipDuration(clipId, item->clipMaxDuration());
388 void ProjectList::slotReplyGetImage(int clipId, int pos, const QPixmap &pix, int w, int h)
390 ProjectItem *item = getItemById(clipId);
391 if (item) item->setIcon(0, pix);
394 ProjectItem *ProjectList::getItemById(int id)
396 QTreeWidgetItemIterator it(listView);
398 if (((ProjectItem *)(*it))->clipId() == id)
402 return ((ProjectItem *)(*it));
406 void ProjectList::addProducer(QDomElement producer, int parentId)
408 if (!m_commandStack) kDebug()<<"!!!!!!!!!!!!!!!! NO CMD STK";
409 CLIPTYPE type = (CLIPTYPE) producer.attribute("type").toInt();
412 QDomElement prods = doc.createElement("list");
413 doc.appendChild(prods);
414 prods.appendChild(doc.importNode(producer, true));*/
417 //kDebug()<<"////// ADDING PRODUCER:\n "<<doc.toString()<<"\n+++++++++++++++++";
418 int id = producer.attribute("id").toInt();
419 QString groupName = producer.attribute("group");
420 if (id >= m_clipIdCounter) m_clipIdCounter = id + 1;
421 else if (id == 0) id = m_clipIdCounter++;
423 if (parentId != -1) {
424 // item is a westley playlist, adjust subproducers ids
425 id = (parentId + 1) * 10000 + id;
427 if (type == AUDIO || type == VIDEO || type == AV || type == IMAGE || type == PLAYLIST)
429 KUrl resource = KUrl(producer.attribute("resource"));
430 if (!resource.isEmpty()) {
431 QStringList itemEntry;
432 itemEntry.append(QString::null);
433 itemEntry.append(resource.fileName());
434 addClip(itemEntry, producer, id, resource, groupName, parentId);
435 /*AddClipCommand *command = new AddClipCommand(this, itemEntry, producer, id, resource, true);
436 m_commandStack->push(command);*/
439 /*ProjectItem *item = new ProjectItem(listView, itemEntry, producer);
440 item->setData(1, FullPathRole, resource.path());
441 item->setData(1, ClipTypeRole, (int) type);*/
445 else if (type == COLOR) {
446 QString colour = producer.attribute("colour");
448 colour = colour.replace(0, 2, "#");
449 pix.fill(QColor(colour.left(7)));
450 QStringList itemEntry;
451 itemEntry.append(QString::null);
452 itemEntry.append(producer.attribute("name", i18n("Color clip")));
453 addClip(itemEntry, producer, id, KUrl(), groupName, parentId);
454 /*AddClipCommand *command = new AddClipCommand(this, itemEntry, producer, id, KUrl(), true);
455 m_commandStack->push(command);*/
456 //ProjectItem *item = new ProjectItem(listView, itemEntry, producer);
457 /*item->setIcon(0, QIcon(pix));
458 item->setData(1, ClipTypeRole, (int) type);*/
463 #include "projectlist.moc"