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"
42 #include "definitions.h"
46 const int NameRole = Qt::UserRole;
47 const int DurationRole = NameRole + 1;
48 const int FullPathRole = NameRole + 2;
49 const int ClipTypeRole = NameRole + 3;
51 class ItemDelegate: public QItemDelegate
54 ItemDelegate(QObject* parent = 0): QItemDelegate(parent)
58 void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
60 if (index.column() == 1)
62 const bool hover = option.state & (QStyle::State_Selected|QStyle::State_MouseOver|QStyle::State_HasFocus);
63 QRect r1 = option.rect;
66 painter->setPen(option.palette.color(QPalette::HighlightedText));
67 QColor backgroundColor = option.palette.color(QPalette::Highlight);
68 painter->setBrush(QBrush(backgroundColor));
69 painter->fillRect(r1, QBrush(backgroundColor));
71 QFont font = painter->font();
72 font.setPointSize(font.pointSize() - 1 );
74 painter->setFont(font);
75 int mid = (int) ((r1.height() / 2 ));
76 r1.setBottom(r1.y() + mid);
77 QRect r2 = option.rect;
78 r2.setTop(r2.y() + mid);
79 painter->drawText(r1, Qt::AlignLeft | Qt::AlignBottom , index.data().toString());
80 //painter->setPen(Qt::green);
82 painter->setFont(font);
83 painter->drawText(r2, Qt::AlignLeft | Qt::AlignVCenter , index.data(DurationRole).toString());
88 QItemDelegate::paint(painter, option, index);
94 ProjectList::ProjectList(QWidget *parent)
95 : QWidget(parent), m_render(NULL), m_fps(-1), m_commandStack(NULL)
98 QWidget *vbox = new QWidget;
99 listView = new ProjectListView(this);;
100 QVBoxLayout *layout = new QVBoxLayout;
104 searchView = new KTreeWidgetSearchLine (this);
105 m_toolbar = new QToolBar("projectToolBar", this);
106 m_toolbar->addWidget (searchView);
108 QToolButton *addButton = new QToolButton( m_toolbar );
109 QMenu *addMenu = new QMenu(this);
110 addButton->setMenu( addMenu );
111 addButton->setPopupMode(QToolButton::MenuButtonPopup);
112 m_toolbar->addWidget (addButton);
114 QAction *addClipButton = addMenu->addAction (KIcon("document-new"), i18n("Add Clip"));
115 connect(addClipButton, SIGNAL(triggered()), this, SLOT(slotAddClip()));
117 QAction *addColorClip = addMenu->addAction (KIcon("document-new"), i18n("Add Color Clip"));
118 connect(addColorClip, SIGNAL(triggered()), this, SLOT(slotAddColorClip()));
120 m_deleteAction = m_toolbar->addAction (KIcon("edit-delete"), i18n("Delete Clip"));
121 connect(m_deleteAction, SIGNAL(triggered()), this, SLOT(slotRemoveClip()));
123 m_editAction = m_toolbar->addAction (KIcon("document-properties"), i18n("Edit Clip"));
124 connect(m_editAction, SIGNAL(triggered()), this, SLOT(slotEditClip()));
126 QAction *addFolderButton = addMenu->addAction (KIcon("folder-new"), i18n("Create Folder"));
127 connect(addFolderButton, SIGNAL(triggered()), this, SLOT(slotAddFolder()));
129 addButton->setDefaultAction( addClipButton );
131 layout->addWidget( m_toolbar );
132 layout->addWidget( listView );
134 m_toolbar->setEnabled(false);
136 searchView->setTreeWidget(listView);
137 listView->setColumnCount(3);
139 headers<<i18n("Thumbnail")<<i18n("Filename")<<i18n("Description");
140 listView->setHeaderLabels(headers);
141 listView->sortByColumn(1, Qt::AscendingOrder);
143 m_menu = new QMenu();
144 m_menu->addAction(addClipButton);
145 m_menu->addAction(addColorClip);
146 m_menu->addAction(m_editAction);
147 m_menu->addAction(m_deleteAction);
148 m_menu->addAction(addFolderButton);
149 m_menu->insertSeparator(m_deleteAction);
151 connect(listView, SIGNAL(itemSelectionChanged()), this, SLOT(slotClipSelected()));
152 connect(listView, SIGNAL(requestMenu ( const QPoint &, QTreeWidgetItem * )), this, SLOT(slotContextMenu(const QPoint &, QTreeWidgetItem *)));
153 connect(listView, SIGNAL(addClip ()), this, SLOT(slotAddClip()));
154 connect(listView, SIGNAL(addClip (QUrl, const QString &)), this, SLOT(slotAddClip(QUrl, const QString &)));
157 listView->setItemDelegate(new ItemDelegate(listView));
158 listView->setIconSize(QSize(60, 40));
159 listView->setSortingEnabled (true);
163 ProjectList::~ProjectList()
169 void ProjectList::setRenderer(Render *projectRender)
171 m_render = projectRender;
174 void ProjectList::slotClipSelected()
176 ProjectItem *item = (ProjectItem*) listView->currentItem();
177 if (item && !item->isGroup()) emit clipSelected(item->toXml());
180 void ProjectList::slotEditClip()
182 kDebug()<<"//////////////////////////////////////// DBL CLK";
186 void ProjectList::slotEditClip(QTreeWidgetItem *item, int column)
188 kDebug()<<"//////////////////////////////////////// DBL CLK";
191 void ProjectList::slotContextMenu( const QPoint &pos, QTreeWidgetItem *item )
197 m_editAction->setEnabled(enable);
198 m_deleteAction->setEnabled(enable);
203 void ProjectList::slotRemoveClip()
205 if (!m_commandStack) kDebug()<<"!!!!!!!!!!!!!!!! NO CMD STK";
206 if (!listView->currentItem()) return;
207 ProjectItem *item = ((ProjectItem *)listView->currentItem());
208 if (!item) kDebug()<<"/////////////// ERROR NOT FOUND";
209 if (KMessageBox::questionYesNo(this, i18n("Delete clip <b>%1</b> ?").arg(item->names().at(1)), i18n("Delete Clip")) != KMessageBox::Yes) return;
210 AddClipCommand *command = new AddClipCommand(this, item->names(), item->toXml(), item->clipId(), item->clipUrl(), item->groupName(), false);
211 m_commandStack->push(command);
214 void ProjectList::selectItemById(const int clipId)
216 ProjectItem *item = getItemById(clipId);
217 if (item) listView->setCurrentItem(item);
220 void ProjectList::addClip(const QStringList &name, const QDomElement &elem, const int clipId, const KUrl &url, const QString &group, int parentId)
222 kDebug()<<"///////// ADDING VCLIP=: "<<name;
224 ProjectItem *groupItem = NULL;
226 if (group.isEmpty()) groupName = elem.attribute("group", QString::null);
227 else groupName = group;
228 if (elem.isNull() && url.isEmpty()) {
230 groupName = name.at(1);
231 QList<QTreeWidgetItem *> groupList = listView->findItems(groupName, Qt::MatchExactly, 1);
232 if (groupList.isEmpty()) {
233 (void) new ProjectItem(listView, name, clipId);
238 if (parentId != -1) {
239 groupItem = getItemById(parentId);
241 else if (!groupName.isEmpty()) {
242 // Clip is in a group
243 QList<QTreeWidgetItem *> groupList = listView->findItems(groupName, Qt::MatchExactly, 1);
245 if (groupList.isEmpty()) {
246 QStringList itemName;
247 itemName<<QString::null<<groupName;
248 kDebug()<<"------- CREATING NEW GRP: "<<itemName;
249 groupItem = new ProjectItem(listView, itemName, m_clipIdCounter++);
251 else groupItem = (ProjectItem *) groupList.first();
253 if (groupItem) item = new ProjectItem(groupItem, name, elem, clipId);
254 else item = new ProjectItem(listView, name, elem, clipId);
255 if (!url.isEmpty()) {
256 item->setData(1, FullPathRole, url.path());
257 /* Nepomuk::File f( url.path() );
258 QString annotation = f.getAnnotation();
259 if (!annotation.isEmpty()) item->setText(2, annotation);*/
260 QString resource = url.path();
261 if (resource.endsWith("westley") || resource.endsWith("kdenlive")) {
264 if (KIO::NetAccess::download(url, tmpfile, 0)) {
266 if (file.open(QIODevice::ReadOnly)) {
267 doc.setContent(&file, false);
270 KIO::NetAccess::removeTempFile(tmpfile);
272 QDomNodeList subProds = doc.elementsByTagName("producer");
273 int ct = subProds.count();
274 for (int i = 0; i < ct ; i++)
276 QDomElement e = subProds.item(i).toElement();
278 addProducer(e, clipId);
286 if (elem.isNull() ) {
288 QDomElement element = doc.createElement("producer");
289 element.setAttribute("resource", url.path());
290 emit getFileProperties(element, clipId);
292 else emit getFileProperties(elem, clipId);
293 selectItemById(clipId);
296 void ProjectList::deleteClip(const int clipId)
298 ProjectItem *item = getItemById(clipId);
299 if (item) delete item;
302 void ProjectList::slotAddFolder()
304 QString folderName = KInputDialog::getText(i18n("New Folder"), i18n("Enter new folder name: "));
305 if (folderName.isEmpty()) return;
306 QStringList itemEntry;
307 itemEntry.append(QString::null);
308 itemEntry.append(folderName);
309 AddClipCommand *command = new AddClipCommand(this, itemEntry, QDomElement(), m_clipIdCounter++, KUrl(), folderName, true);
310 m_commandStack->push(command);
313 void ProjectList::slotAddClip(QUrl givenUrl, const QString &group)
315 if (!m_commandStack) kDebug()<<"!!!!!!!!!!!!!!!! NO CMD STK";
317 if (givenUrl.isEmpty())
318 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");
319 else list.append(givenUrl);
320 if (list.isEmpty()) return;
321 KUrl::List::Iterator it;
323 // ProjectItem *item;
325 for (it = list.begin(); it != list.end(); it++) {
326 QStringList itemEntry;
328 QDomElement prod = doc.createElement("producer");
329 prod.setAttribute("resource", (*it).path());
330 itemEntry.append(QString::null);
331 itemEntry.append((*it).fileName());
332 KMimeType::Ptr type = KMimeType::findByUrl(*it);
333 if (type->name().startsWith("image/")) {
334 prod.setAttribute("type", (int) IMAGE);
335 prod.setAttribute("in", "0");
336 prod.setAttribute("out", m_timecode.getFrameCount(KdenliveSettings::image_duration(), m_fps));
338 AddClipCommand *command = new AddClipCommand(this, itemEntry, prod, m_clipIdCounter++, *it, group, true);
339 m_commandStack->push(command);
340 //item = new ProjectItem(listView, itemEntry, QDomElement());
341 //item->setData(1, FullPathRole, (*it).path());
343 //listView->setCurrentItem(item);
346 void ProjectList::slotAddColorClip()
348 if (!m_commandStack) kDebug()<<"!!!!!!!!!!!!!!!! NO CMD STK";
349 QDialog *dia = new QDialog;
350 Ui::ColorClip_UI *dia_ui = new Ui::ColorClip_UI();
351 dia_ui->setupUi(dia);
352 dia_ui->clip_name->setText(i18n("Color Clip"));
353 dia_ui->clip_duration->setText(KdenliveSettings::color_duration());
354 if (dia->exec() == QDialog::Accepted)
357 QDomElement element = doc.createElement("producer");
358 element.setAttribute("mlt_service", "colour");
359 QString color = dia_ui->clip_color->color().name();
360 color = color.replace(0, 1, "0x") + "ff";
361 element.setAttribute("colour", color);
362 element.setAttribute("type", (int) COLOR);
363 element.setAttribute("in", "0");
364 element.setAttribute("out", m_timecode.getFrameCount(dia_ui->clip_duration->text(), m_fps));
365 element.setAttribute("name", dia_ui->clip_name->text());
366 QStringList itemEntry;
367 itemEntry.append(QString::null);
368 itemEntry.append(dia_ui->clip_name->text());
369 AddClipCommand *command = new AddClipCommand(this, itemEntry, element, m_clipIdCounter++, KUrl(), QString::null, true);
370 m_commandStack->push(command);
371 // ProjectItem *item = new ProjectItem(listView, itemEntry, element, m_clipIdCounter++);
372 /*QPixmap pix(60, 40);
373 pix.fill(dia_ui->clip_color->color());
374 item->setIcon(0, QIcon(pix));*/
375 //listView->setCurrentItem(item);
382 void ProjectList::setDocument(KdenliveDoc *doc)
385 m_timecode = doc->timecode();
386 m_commandStack = doc->commandStack();
387 QDomNodeList prods = doc->producersList();
388 int ct = prods.count();
389 kDebug()<<"//////////// SETTING DOC, FOUND CLIPS: "<<prods.count();
391 for (int i = 0; i < ct ; i++)
393 QDomElement e = prods.item(i).toElement();
394 kDebug()<<"// IMPORT: "<<i<<", :"<<e.attribute("id", "non")<<", NAME: "<<e.attribute("name", "non");
395 if (!e.isNull()) addProducer(e);
397 QTreeWidgetItem *first = listView->topLevelItem(0);
398 if (first) listView->setCurrentItem(first);
399 m_toolbar->setEnabled(true);
402 QDomElement ProjectList::producersList()
405 QDomElement prods = doc.createElement("producerlist");
406 doc.appendChild(prods);
407 kDebug()<<"//////////// PRO LIST BUILD PRDSLIST ";
408 QTreeWidgetItemIterator it(listView);
410 if (!((ProjectItem *)(*it))->isGroup())
411 prods.appendChild(doc.importNode(((ProjectItem *)(*it))->toXml(), true));
418 void ProjectList::slotReplyGetFileProperties(int clipId, const QMap < QString, QString > &properties, const QMap < QString, QString > &metadata)
420 ProjectItem *item = getItemById(clipId);
422 item->setProperties(properties, metadata);
423 emit receivedClipDuration(clipId, item->clipMaxDuration());
429 void ProjectList::slotReplyGetImage(int clipId, int pos, const QPixmap &pix, int w, int h)
431 ProjectItem *item = getItemById(clipId);
432 if (item) item->setIcon(0, pix);
435 ProjectItem *ProjectList::getItemById(int id)
437 QTreeWidgetItemIterator it(listView);
439 if (((ProjectItem *)(*it))->clipId() == id)
443 return ((ProjectItem *)(*it));
447 void ProjectList::addProducer(QDomElement producer, int parentId)
449 if (!m_commandStack) kDebug()<<"!!!!!!!!!!!!!!!! NO CMD STK";
450 CLIPTYPE type = (CLIPTYPE) producer.attribute("type").toInt();
453 QDomElement prods = doc.createElement("list");
454 doc.appendChild(prods);
455 prods.appendChild(doc.importNode(producer, true));*/
458 //kDebug()<<"////// ADDING PRODUCER:\n "<<doc.toString()<<"\n+++++++++++++++++";
459 int id = producer.attribute("id").toInt();
460 QString groupName = producer.attribute("group");
461 if (id >= m_clipIdCounter) m_clipIdCounter = id + 1;
462 else if (id == 0) id = m_clipIdCounter++;
464 if (parentId != -1) {
465 // item is a westley playlist, adjust subproducers ids
466 id = (parentId + 1) * 10000 + id;
468 if (type == AUDIO || type == VIDEO || type == AV || type == IMAGE || type == PLAYLIST)
470 KUrl resource = KUrl(producer.attribute("resource"));
471 if (!resource.isEmpty()) {
472 QStringList itemEntry;
473 itemEntry.append(QString::null);
474 itemEntry.append(resource.fileName());
475 addClip(itemEntry, producer, id, resource, groupName, parentId);
476 /*AddClipCommand *command = new AddClipCommand(this, itemEntry, producer, id, resource, true);
477 m_commandStack->push(command);*/
480 /*ProjectItem *item = new ProjectItem(listView, itemEntry, producer);
481 item->setData(1, FullPathRole, resource.path());
482 item->setData(1, ClipTypeRole, (int) type);*/
486 else if (type == COLOR) {
487 QString colour = producer.attribute("colour");
489 colour = colour.replace(0, 2, "#");
490 pix.fill(QColor(colour.left(7)));
491 QStringList itemEntry;
492 itemEntry.append(QString::null);
493 itemEntry.append(producer.attribute("name", i18n("Color clip")));
494 addClip(itemEntry, producer, id, KUrl(), groupName, parentId);
495 /*AddClipCommand *command = new AddClipCommand(this, itemEntry, producer, id, KUrl(), true);
496 m_commandStack->push(command);*/
497 //ProjectItem *item = new ProjectItem(listView, itemEntry, producer);
498 /*item->setIcon(0, QIcon(pix));
499 item->setData(1, ClipTypeRole, (int) type);*/
504 #include "projectlist.moc"