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 <nepomuk/resourcemanager.h>
33 #include <kio/netaccess.h>
34 #include <KMessageBox>
36 #include "projectlist.h"
37 #include "projectitem.h"
38 #include "kdenlivesettings.h"
39 #include "ui_colorclip_ui.h"
41 #include "addclipcommand.h"
45 const int NameRole = Qt::UserRole;
46 const int DurationRole = NameRole + 1;
47 const int FullPathRole = NameRole + 2;
48 const int ClipTypeRole = NameRole + 3;
50 class ItemDelegate: public QItemDelegate
53 ItemDelegate(QObject* parent = 0): QItemDelegate(parent)
57 void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
59 if (index.column() == 1)
61 const bool hover = option.state & (QStyle::State_Selected|QStyle::State_MouseOver|QStyle::State_HasFocus);
62 QRect r1 = option.rect;
65 painter->setPen(option.palette.color(QPalette::HighlightedText));
66 QColor backgroundColor = option.palette.color(QPalette::Highlight);
67 painter->setBrush(QBrush(backgroundColor));
68 painter->fillRect(r1, QBrush(backgroundColor));
70 QFont font = painter->font();
71 font.setPointSize(font.pointSize() - 1 );
73 painter->setFont(font);
74 int mid = (int) ((r1.height() / 2 ));
75 r1.setBottom(r1.y() + mid);
76 QRect r2 = option.rect;
77 r2.setTop(r2.y() + mid);
78 painter->drawText(r1, Qt::AlignLeft | Qt::AlignBottom , index.data().toString());
79 //painter->setPen(Qt::green);
81 painter->setFont(font);
82 painter->drawText(r2, Qt::AlignLeft | Qt::AlignVCenter , index.data(DurationRole).toString());
87 QItemDelegate::paint(painter, option, index);
93 ProjectList::ProjectList(QWidget *parent)
94 : QWidget(parent), m_render(NULL), m_fps(-1), m_commandStack(NULL)
97 QWidget *vbox = new QWidget;
98 listView = new ProjectListView(this);;
99 QVBoxLayout *layout = new QVBoxLayout;
103 searchView = new KTreeWidgetSearchLine (this);
104 m_toolbar = new QToolBar("projectToolBar", this);
105 m_toolbar->addWidget (searchView);
107 QToolButton *addButton = new QToolButton( m_toolbar );
108 QMenu *addMenu = new QMenu(this);
109 addButton->setMenu( addMenu );
110 addButton->setPopupMode(QToolButton::MenuButtonPopup);
111 m_toolbar->addWidget (addButton);
113 QAction *addClipButton = addMenu->addAction (KIcon("document-new"), i18n("Add Clip"));
114 connect(addClipButton, SIGNAL(triggered()), this, SLOT(slotAddClip()));
116 QAction *addColorClip = addMenu->addAction (KIcon("document-new"), i18n("Add Color Clip"));
117 connect(addColorClip, SIGNAL(triggered()), this, SLOT(slotAddColorClip()));
119 m_deleteAction = m_toolbar->addAction (KIcon("edit-delete"), i18n("Delete Clip"));
120 connect(m_deleteAction, SIGNAL(triggered()), this, SLOT(slotRemoveClip()));
122 m_editAction = m_toolbar->addAction (KIcon("document-properties"), i18n("Edit Clip"));
123 connect(m_editAction, SIGNAL(triggered()), this, SLOT(slotEditClip()));
125 QAction *addFolderButton = addMenu->addAction (KIcon("folder-new"), i18n("Create Folder"));
126 connect(addFolderButton, SIGNAL(triggered()), this, SLOT(slotAddFolder()));
128 addButton->setDefaultAction( addClipButton );
130 layout->addWidget( m_toolbar );
131 layout->addWidget( listView );
133 m_toolbar->setEnabled(false);
135 searchView->setTreeWidget(listView);
136 listView->setColumnCount(3);
138 headers<<i18n("Thumbnail")<<i18n("Filename")<<i18n("Description");
139 listView->setHeaderLabels(headers);
140 listView->sortByColumn(1, Qt::AscendingOrder);
142 m_menu = new QMenu();
143 m_menu->addAction(addClipButton);
144 m_menu->addAction(addColorClip);
145 m_menu->addAction(m_editAction);
146 m_menu->addAction(m_deleteAction);
147 m_menu->addAction(addFolderButton);
148 m_menu->insertSeparator(m_deleteAction);
150 connect(listView, SIGNAL(itemSelectionChanged()), this, SLOT(slotClipSelected()));
151 connect(listView, SIGNAL(requestMenu ( const QPoint &, QTreeWidgetItem * )), this, SLOT(slotContextMenu(const QPoint &, QTreeWidgetItem *)));
152 connect(listView, SIGNAL(addClip ()), this, SLOT(slotAddClip()));
153 connect(listView, SIGNAL(addClip (QUrl, const QString &)), this, SLOT(slotAddClip(QUrl, const QString &)));
156 listView->setItemDelegate(new ItemDelegate(listView));
157 listView->setIconSize(QSize(60, 40));
158 listView->setSortingEnabled (true);
162 ProjectList::~ProjectList()
168 void ProjectList::setRenderer(Render *projectRender)
170 m_render = projectRender;
173 void ProjectList::slotClipSelected()
175 ProjectItem *item = (ProjectItem*) listView->currentItem();
176 if (item && !item->isGroup()) emit clipSelected(item->toXml());
179 void ProjectList::slotEditClip()
181 kDebug()<<"//////////////////////////////////////// DBL CLK";
185 void ProjectList::slotEditClip(QTreeWidgetItem *item, int column)
187 kDebug()<<"//////////////////////////////////////// DBL CLK";
190 void ProjectList::slotContextMenu( const QPoint &pos, QTreeWidgetItem *item )
196 m_editAction->setEnabled(enable);
197 m_deleteAction->setEnabled(enable);
202 void ProjectList::slotRemoveClip()
204 if (!m_commandStack) kDebug()<<"!!!!!!!!!!!!!!!! NO CMD STK";
205 if (!listView->currentItem()) return;
206 ProjectItem *item = ((ProjectItem *)listView->currentItem());
207 if (!item) kDebug()<<"/////////////// ERROR NOT FOUND";
208 if (KMessageBox::questionYesNo(this, i18n("Delete clip <b>%1</b> ?").arg(item->names().at(1)), i18n("Delete Clip")) != KMessageBox::Yes) return;
209 AddClipCommand *command = new AddClipCommand(this, item->names(), item->toXml(), item->clipId(), item->clipUrl(), item->groupName(), false);
210 m_commandStack->push(command);
213 void ProjectList::selectItemById(const int clipId)
215 ProjectItem *item = getItemById(clipId);
216 if (item) listView->setCurrentItem(item);
219 void ProjectList::addClip(const QStringList &name, const QDomElement &elem, const int clipId, const KUrl &url, const QString &group, int parentId)
221 kDebug()<<"///////// ADDING VCLIP=: "<<name;
223 ProjectItem *groupItem = NULL;
225 if (group.isEmpty()) groupName = elem.attribute("group", QString::null);
226 else groupName = group;
227 if (elem.isNull() && url.isEmpty()) {
229 groupName = name.at(1);
230 QList<QTreeWidgetItem *> groupList = listView->findItems(groupName, Qt::MatchExactly, 1);
231 if (groupList.isEmpty()) {
232 (void) new ProjectItem(listView, name, clipId);
237 if (parentId != -1) {
238 groupItem = getItemById(parentId);
240 else if (!groupName.isEmpty()) {
241 // Clip is in a group
242 QList<QTreeWidgetItem *> groupList = listView->findItems(groupName, Qt::MatchExactly, 1);
244 if (groupList.isEmpty()) {
245 QStringList itemName;
246 itemName<<QString::null<<groupName;
247 kDebug()<<"------- CREATING NEW GRP: "<<itemName;
248 groupItem = new ProjectItem(listView, itemName, m_clipIdCounter++);
250 else groupItem = (ProjectItem *) groupList.first();
252 if (groupItem) item = new ProjectItem(groupItem, name, elem, clipId);
253 else item = new ProjectItem(listView, name, elem, clipId);
254 if (!url.isEmpty()) {
255 item->setData(1, FullPathRole, url.path());
256 /* Nepomuk::File f( url.path() );
257 QString annotation = f.getAnnotation();
258 if (!annotation.isEmpty()) item->setText(2, annotation);*/
259 QString resource = url.path();
260 if (resource.endsWith("westley") || resource.endsWith("kdenlive")) {
263 if (KIO::NetAccess::download(url, tmpfile, 0)) {
265 if (file.open(QIODevice::ReadOnly)) {
266 doc.setContent(&file, false);
269 KIO::NetAccess::removeTempFile(tmpfile);
271 QDomNodeList subProds = doc.elementsByTagName("producer");
272 int ct = subProds.count();
273 for (int i = 0; i < ct ; i++)
275 QDomElement e = subProds.item(i).toElement();
277 addProducer(e, clipId);
285 if (elem.isNull() ) {
287 QDomElement element = doc.createElement("producer");
288 element.setAttribute("resource", url.path());
289 emit getFileProperties(element, clipId);
291 else emit getFileProperties(elem, clipId);
292 selectItemById(clipId);
295 void ProjectList::deleteClip(const int clipId)
297 ProjectItem *item = getItemById(clipId);
298 if (item) delete item;
301 void ProjectList::slotAddFolder()
303 QString folderName = KInputDialog::getText(i18n("New Folder"), i18n("Enter new folder name: "));
304 if (folderName.isEmpty()) return;
305 QStringList itemEntry;
306 itemEntry.append(QString::null);
307 itemEntry.append(folderName);
308 AddClipCommand *command = new AddClipCommand(this, itemEntry, QDomElement(), m_clipIdCounter++, KUrl(), folderName, true);
309 m_commandStack->push(command);
312 void ProjectList::slotAddClip(QUrl givenUrl, const QString &group)
314 if (!m_commandStack) kDebug()<<"!!!!!!!!!!!!!!!! NO CMD STK";
316 if (givenUrl.isEmpty())
317 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");
318 else list.append(givenUrl);
319 if (list.isEmpty()) return;
320 KUrl::List::Iterator it;
322 // ProjectItem *item;
324 for (it = list.begin(); it != list.end(); it++) {
325 QStringList itemEntry;
326 itemEntry.append(QString::null);
327 itemEntry.append((*it).fileName());
328 AddClipCommand *command = new AddClipCommand(this, itemEntry, QDomElement(), m_clipIdCounter++, *it, group, true);
329 m_commandStack->push(command);
330 //item = new ProjectItem(listView, itemEntry, QDomElement());
331 //item->setData(1, FullPathRole, (*it).path());
333 //listView->setCurrentItem(item);
336 void ProjectList::slotAddColorClip()
338 if (!m_commandStack) kDebug()<<"!!!!!!!!!!!!!!!! NO CMD STK";
339 QDialog *dia = new QDialog;
340 Ui::ColorClip_UI *dia_ui = new Ui::ColorClip_UI();
341 dia_ui->setupUi(dia);
342 dia_ui->clip_name->setText(i18n("Color Clip"));
343 dia_ui->clip_duration->setText(KdenliveSettings::color_duration());
344 if (dia->exec() == QDialog::Accepted)
347 QDomElement element = doc.createElement("producer");
348 element.setAttribute("mlt_service", "colour");
349 QString color = dia_ui->clip_color->color().name();
350 color = color.replace(0, 1, "0x") + "ff";
351 element.setAttribute("colour", color);
352 element.setAttribute("type", (int) DocClipBase::COLOR);
353 element.setAttribute("in", "0");
354 element.setAttribute("out", m_timecode.getFrameCount(dia_ui->clip_duration->text(), m_fps));
355 element.setAttribute("name", dia_ui->clip_name->text());
356 QStringList itemEntry;
357 itemEntry.append(QString::null);
358 itemEntry.append(dia_ui->clip_name->text());
359 AddClipCommand *command = new AddClipCommand(this, itemEntry, element, m_clipIdCounter++, KUrl(), QString::null, true);
360 m_commandStack->push(command);
361 // ProjectItem *item = new ProjectItem(listView, itemEntry, element, m_clipIdCounter++);
362 /*QPixmap pix(60, 40);
363 pix.fill(dia_ui->clip_color->color());
364 item->setIcon(0, QIcon(pix));*/
365 //listView->setCurrentItem(item);
372 void ProjectList::setDocument(KdenliveDoc *doc)
375 m_timecode = doc->timecode();
376 m_commandStack = doc->commandStack();
377 QDomNodeList prods = doc->producersList();
378 int ct = prods.count();
379 kDebug()<<"//////////// SETTING DOC, FOUND CLIPS: "<<prods.count();
381 for (int i = 0; i < ct ; i++)
383 QDomElement e = prods.item(i).toElement();
384 kDebug()<<"// IMPORT: "<<i<<", :"<<e.attribute("id", "non")<<", NAME: "<<e.attribute("name", "non");
385 if (!e.isNull()) addProducer(e);
387 QTreeWidgetItem *first = listView->topLevelItem(0);
388 if (first) listView->setCurrentItem(first);
389 m_toolbar->setEnabled(true);
392 QDomElement ProjectList::producersList()
395 QDomElement prods = doc.createElement("producerlist");
396 doc.appendChild(prods);
397 kDebug()<<"//////////// PRO LIST BUILD PRDSLIST ";
398 QTreeWidgetItemIterator it(listView);
400 if (!((ProjectItem *)(*it))->isGroup())
401 prods.appendChild(doc.importNode(((ProjectItem *)(*it))->toXml(), true));
408 void ProjectList::slotReplyGetFileProperties(int clipId, const QMap < QString, QString > &properties, const QMap < QString, QString > &metadata)
410 ProjectItem *item = getItemById(clipId);
412 item->setProperties(properties, metadata);
413 emit receivedClipDuration(clipId, item->clipMaxDuration());
419 void ProjectList::slotReplyGetImage(int clipId, int pos, const QPixmap &pix, int w, int h)
421 ProjectItem *item = getItemById(clipId);
422 if (item) item->setIcon(0, pix);
425 ProjectItem *ProjectList::getItemById(int id)
427 QTreeWidgetItemIterator it(listView);
429 if (((ProjectItem *)(*it))->clipId() == id)
433 return ((ProjectItem *)(*it));
437 void ProjectList::addProducer(QDomElement producer, int parentId)
439 if (!m_commandStack) kDebug()<<"!!!!!!!!!!!!!!!! NO CMD STK";
440 DocClipBase::CLIPTYPE type = (DocClipBase::CLIPTYPE) producer.attribute("type").toInt();
443 QDomElement prods = doc.createElement("list");
444 doc.appendChild(prods);
445 prods.appendChild(doc.importNode(producer, true));*/
448 //kDebug()<<"////// ADDING PRODUCER:\n "<<doc.toString()<<"\n+++++++++++++++++";
449 int id = producer.attribute("id").toInt();
450 QString groupName = producer.attribute("group");
451 if (id >= m_clipIdCounter) m_clipIdCounter = id + 1;
452 else if (id == 0) id = m_clipIdCounter++;
454 if (parentId != -1) {
455 // item is a westley playlist, adjust subproducers ids
456 id = (parentId + 1) * 10000 + id;
458 if (type == DocClipBase::AUDIO || type == DocClipBase::VIDEO || type == DocClipBase::AV || type == DocClipBase::IMAGE || type == DocClipBase::PLAYLIST)
460 KUrl resource = KUrl(producer.attribute("resource"));
461 if (!resource.isEmpty()) {
462 QStringList itemEntry;
463 itemEntry.append(QString::null);
464 itemEntry.append(resource.fileName());
465 addClip(itemEntry, producer, id, resource, groupName, parentId);
466 /*AddClipCommand *command = new AddClipCommand(this, itemEntry, producer, id, resource, true);
467 m_commandStack->push(command);*/
470 /*ProjectItem *item = new ProjectItem(listView, itemEntry, producer);
471 item->setData(1, FullPathRole, resource.path());
472 item->setData(1, ClipTypeRole, (int) type);*/
476 else if (type == DocClipBase::COLOR) {
477 QString colour = producer.attribute("colour");
479 colour = colour.replace(0, 2, "#");
480 pix.fill(QColor(colour.left(7)));
481 QStringList itemEntry;
482 itemEntry.append(QString::null);
483 itemEntry.append(producer.attribute("name", i18n("Color clip")));
484 addClip(itemEntry, producer, id, KUrl(), groupName, parentId);
485 /*AddClipCommand *command = new AddClipCommand(this, itemEntry, producer, id, KUrl(), true);
486 m_commandStack->push(command);*/
487 //ProjectItem *item = new ProjectItem(listView, itemEntry, producer);
488 /*item->setIcon(0, QIcon(pix));
489 item->setData(1, ClipTypeRole, (int) type);*/
494 #include "projectlist.moc"