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 // if file has Nepomuk comment, use it
236 Nepomuk::Resource f( url.path() );
237 QString annotation = f.description();
238 if (!annotation.isEmpty()) item->setText(2, annotation);
239 QString resource = url.path();
240 if (resource.endsWith("westley") || resource.endsWith("kdenlive")) {
243 if (KIO::NetAccess::download(url, tmpfile, 0)) {
245 if (file.open(QIODevice::ReadOnly)) {
246 doc.setContent(&file, false);
249 KIO::NetAccess::removeTempFile(tmpfile);
251 QDomNodeList subProds = doc.elementsByTagName("producer");
252 int ct = subProds.count();
253 for (int i = 0; i < ct ; i++)
255 QDomElement e = subProds.item(i).toElement();
257 addProducer(e, clipId);
265 if (elem.isNull() ) {
267 QDomElement element = doc.createElement("producer");
268 element.setAttribute("resource", url.path());
269 emit getFileProperties(element, clipId);
271 else emit getFileProperties(elem, clipId);
272 selectItemById(clipId);
275 void ProjectList::slotDeleteClip( int clipId)
277 ProjectItem *item = getItemById(clipId);
278 if (item) delete item;
281 void ProjectList::slotAddFolder()
284 QString folderName = KInputDialog::getText(i18n("New Folder"), i18n("Enter new folder name: "));
285 if (folderName.isEmpty()) return;
286 QStringList itemEntry;
287 itemEntry.append(QString::null);
288 itemEntry.append(folderName);
289 AddClipCommand *command = new AddClipCommand(this, itemEntry, QDomElement(), m_clipIdCounter++, KUrl(), folderName, true);
290 m_commandStack->push(command);*/
293 void ProjectList::slotAddClip(DocClipBase *clip)
295 ProjectItem *item = new ProjectItem(listView, clip);
296 listView->setCurrentItem(item);
297 emit getFileProperties(clip->toXML(), clip->getId());
300 void ProjectList::slotUpdateClip(int id)
302 ProjectItem *item = getItemById(id);
303 item->setData(1, UsageRole, QString::number(item->numReferences()));
306 void ProjectList::slotAddClip(QUrl givenUrl, const QString &group)
308 if (!m_commandStack) kDebug()<<"!!!!!!!!!!!!!!!! NO CMD STK";
310 if (givenUrl.isEmpty())
311 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");
312 else list.append(givenUrl);
313 if (list.isEmpty()) return;
314 KUrl::List::Iterator it;
316 for (it = list.begin(); it != list.end(); it++) {
317 m_doc->slotAddClipFile(*it, group);
321 void ProjectList::slotAddColorClip()
323 if (!m_commandStack) kDebug()<<"!!!!!!!!!!!!!!!! NO CMD STK";
324 QDialog *dia = new QDialog;
325 Ui::ColorClip_UI *dia_ui = new Ui::ColorClip_UI();
326 dia_ui->setupUi(dia);
327 dia_ui->clip_name->setText(i18n("Color Clip"));
328 dia_ui->clip_duration->setText(KdenliveSettings::color_duration());
329 if (dia->exec() == QDialog::Accepted)
331 QString color = dia_ui->clip_color->color().name();
332 color = color.replace(0, 1, "0x") + "ff";
333 m_doc->slotAddColorClipFile(dia_ui->clip_name->text(), color, dia_ui->clip_duration->text(), QString::null);
339 void ProjectList::setDocument(KdenliveDoc *doc)
342 m_timecode = doc->timecode();
343 m_commandStack = doc->commandStack();
345 QDomNodeList prods = doc->producersList();
346 int ct = prods.count();
347 kDebug()<<"//////////// SETTING DOC, FOUND CLIPS: "<<prods.count();
349 for (int i = 0; i < ct ; i++)
351 QDomElement e = prods.item(i).toElement();
352 kDebug()<<"// IMPORT: "<<i<<", :"<<e.attribute("id", "non")<<", NAME: "<<e.attribute("name", "non");
353 if (!e.isNull()) addProducer(e);
355 QTreeWidgetItem *first = listView->topLevelItem(0);
356 if (first) listView->setCurrentItem(first);
357 m_toolbar->setEnabled(true);
360 QDomElement ProjectList::producersList()
363 QDomElement prods = doc.createElement("producerlist");
364 doc.appendChild(prods);
365 kDebug()<<"//////////// PRO LIST BUILD PRDSLIST ";
366 QTreeWidgetItemIterator it(listView);
368 if (!((ProjectItem *)(*it))->isGroup())
369 prods.appendChild(doc.importNode(((ProjectItem *)(*it))->toXml(), true));
376 void ProjectList::slotReplyGetFileProperties(int clipId, const QMap < QString, QString > &properties, const QMap < QString, QString > &metadata)
378 ProjectItem *item = getItemById(clipId);
380 item->setProperties(properties, metadata);
381 emit receivedClipDuration(clipId, item->clipMaxDuration());
387 void ProjectList::slotReplyGetImage(int clipId, int pos, const QPixmap &pix, int w, int h)
389 ProjectItem *item = getItemById(clipId);
390 if (item) item->setIcon(0, pix);
393 ProjectItem *ProjectList::getItemById(int id)
395 QTreeWidgetItemIterator it(listView);
397 if (((ProjectItem *)(*it))->clipId() == id)
401 return ((ProjectItem *)(*it));
405 void ProjectList::addProducer(QDomElement producer, int parentId)
407 if (!m_commandStack) kDebug()<<"!!!!!!!!!!!!!!!! NO CMD STK";
408 CLIPTYPE type = (CLIPTYPE) producer.attribute("type").toInt();
411 QDomElement prods = doc.createElement("list");
412 doc.appendChild(prods);
413 prods.appendChild(doc.importNode(producer, true));*/
416 //kDebug()<<"////// ADDING PRODUCER:\n "<<doc.toString()<<"\n+++++++++++++++++";
417 int id = producer.attribute("id").toInt();
418 QString groupName = producer.attribute("group");
419 if (id >= m_clipIdCounter) m_clipIdCounter = id + 1;
420 else if (id == 0) id = m_clipIdCounter++;
422 if (parentId != -1) {
423 // item is a westley playlist, adjust subproducers ids
424 id = (parentId + 1) * 10000 + id;
426 if (type == AUDIO || type == VIDEO || type == AV || type == IMAGE || type == PLAYLIST)
428 KUrl resource = KUrl(producer.attribute("resource"));
429 if (!resource.isEmpty()) {
430 QStringList itemEntry;
431 itemEntry.append(QString::null);
432 itemEntry.append(resource.fileName());
433 addClip(itemEntry, producer, id, resource, groupName, parentId);
436 else if (type == COLOR) {
437 QString colour = producer.attribute("colour");
439 colour = colour.replace(0, 2, "#");
440 pix.fill(QColor(colour.left(7)));
441 QStringList itemEntry;
442 itemEntry.append(QString::null);
443 itemEntry.append(producer.attribute("name", i18n("Color clip")));
444 addClip(itemEntry, producer, id, KUrl(), groupName, parentId);
449 #include "projectlist.moc"