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>
35 #include "projectlist.h"
36 #include "projectitem.h"
37 #include "kdenlivesettings.h"
38 #include "ui_colorclip_ui.h"
40 #include "addclipcommand.h"
44 const int NameRole = Qt::UserRole;
45 const int DurationRole = NameRole + 1;
46 const int FullPathRole = NameRole + 2;
47 const int ClipTypeRole = NameRole + 3;
49 class ItemDelegate: public QItemDelegate
52 ItemDelegate(QObject* parent = 0): QItemDelegate(parent)
56 void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
58 if (index.column() == 1)
60 const bool hover = option.state & (QStyle::State_Selected|QStyle::State_MouseOver|QStyle::State_HasFocus);
61 QRect r1 = option.rect;
64 painter->setPen(option.palette.color(QPalette::HighlightedText));
65 QColor backgroundColor = option.palette.color(QPalette::Highlight);
66 painter->setBrush(QBrush(backgroundColor));
67 painter->fillRect(r1, QBrush(backgroundColor));
69 QFont font = painter->font();
70 font.setPointSize(font.pointSize() - 1 );
72 painter->setFont(font);
73 int mid = (int) ((r1.height() / 2 ));
74 r1.setBottom(r1.y() + mid);
75 QRect r2 = option.rect;
76 r2.setTop(r2.y() + mid);
77 painter->drawText(r1, Qt::AlignLeft | Qt::AlignBottom , index.data().toString());
78 //painter->setPen(Qt::green);
80 painter->setFont(font);
81 painter->drawText(r2, Qt::AlignLeft | Qt::AlignVCenter , index.data(DurationRole).toString());
86 QItemDelegate::paint(painter, option, index);
92 ProjectList::ProjectList(QWidget *parent)
93 : QWidget(parent), m_render(NULL), m_fps(-1), m_commandStack(NULL)
96 QWidget *vbox = new QWidget;
97 listView = new ProjectListView(this);;
98 QVBoxLayout *layout = new QVBoxLayout;
102 searchView = new KTreeWidgetSearchLine (this);
103 m_toolbar = new QToolBar("projectToolBar", this);
104 m_toolbar->addWidget (searchView);
106 QToolButton *addButton = new QToolButton( m_toolbar );
107 QMenu *addMenu = new QMenu(this);
108 addButton->setMenu( addMenu );
109 addButton->setPopupMode(QToolButton::MenuButtonPopup);
110 m_toolbar->addWidget (addButton);
112 QAction *addClipButton = addMenu->addAction (KIcon("document-new"), i18n("Add Clip"));
113 connect(addClipButton, SIGNAL(triggered()), this, SLOT(slotAddClip()));
115 QAction *addColorClip = addMenu->addAction (KIcon("document-new"), i18n("Add Color Clip"));
116 connect(addColorClip, SIGNAL(triggered()), this, SLOT(slotAddColorClip()));
118 m_deleteAction = m_toolbar->addAction (KIcon("edit-delete"), i18n("Delete Clip"));
119 connect(m_deleteAction, SIGNAL(triggered()), this, SLOT(slotRemoveClip()));
121 m_editAction = m_toolbar->addAction (KIcon("document-properties"), i18n("Edit Clip"));
122 connect(m_editAction, SIGNAL(triggered()), this, SLOT(slotEditClip()));
124 QAction *addFolderButton = addMenu->addAction (KIcon("folder-new"), i18n("Create Folder"));
125 connect(addFolderButton, SIGNAL(triggered()), this, SLOT(slotAddFolder()));
127 addButton->setDefaultAction( addClipButton );
129 layout->addWidget( m_toolbar );
130 layout->addWidget( listView );
132 m_toolbar->setEnabled(false);
134 searchView->setTreeWidget(listView);
135 listView->setColumnCount(3);
137 headers<<i18n("Thumbnail")<<i18n("Filename")<<i18n("Description");
138 listView->setHeaderLabels(headers);
139 listView->sortByColumn(1, Qt::AscendingOrder);
141 m_menu = new QMenu();
142 m_menu->addAction(addClipButton);
143 m_menu->addAction(addColorClip);
144 m_menu->addAction(m_editAction);
145 m_menu->addAction(m_deleteAction);
146 m_menu->addAction(addFolderButton);
147 m_menu->insertSeparator(m_deleteAction);
149 connect(listView, SIGNAL(itemSelectionChanged()), this, SLOT(slotClipSelected()));
150 connect(listView, SIGNAL(requestMenu ( const QPoint &, QTreeWidgetItem * )), this, SLOT(slotContextMenu(const QPoint &, QTreeWidgetItem *)));
151 connect(listView, SIGNAL(addClip ()), this, SLOT(slotAddClip()));
152 connect(listView, SIGNAL(addClip (QUrl, const QString &)), this, SLOT(slotAddClip(QUrl, const QString &)));
155 listView->setItemDelegate(new ItemDelegate(listView));
156 listView->setIconSize(QSize(60, 40));
157 listView->setSortingEnabled (true);
161 ProjectList::~ProjectList()
167 void ProjectList::setRenderer(Render *projectRender)
169 m_render = projectRender;
172 void ProjectList::slotClipSelected()
174 ProjectItem *item = (ProjectItem*) listView->currentItem();
175 if (item && !item->isGroup()) emit clipSelected(item->toXml());
178 void ProjectList::slotEditClip()
180 kDebug()<<"//////////////////////////////////////// DBL CLK";
184 void ProjectList::slotEditClip(QTreeWidgetItem *item, int column)
186 kDebug()<<"//////////////////////////////////////// DBL CLK";
189 void ProjectList::slotContextMenu( const QPoint &pos, QTreeWidgetItem *item )
195 m_editAction->setEnabled(enable);
196 m_deleteAction->setEnabled(enable);
201 void ProjectList::slotRemoveClip()
203 if (!m_commandStack) kDebug()<<"!!!!!!!!!!!!!!!! NO CMD STK";
204 if (!listView->currentItem()) return;
205 ProjectItem *item = ((ProjectItem *)listView->currentItem());
206 AddClipCommand *command = new AddClipCommand(this, item->names(), item->toXml(), item->clipId(), KUrl(item->data(1, FullPathRole).toString()), item->groupName(), false);
207 m_commandStack->push(command);
211 void ProjectList::selectItemById(const int clipId)
213 ProjectItem *item = getItemById(clipId);
214 if (item) listView->setCurrentItem(item);
217 void ProjectList::addClip(const QStringList &name, const QDomElement &elem, const int clipId, const KUrl &url, const QString &group, int parentId)
219 kDebug()<<"///////// ADDING VCLIP=: "<<name;
221 ProjectItem *groupItem = NULL;
223 if (group.isEmpty()) groupName = elem.attribute("group", QString::null);
224 else groupName = group;
225 if (elem.isNull() && url.isEmpty()) {
227 groupName = name.at(1);
228 QList<QTreeWidgetItem *> groupList = listView->findItems(groupName, Qt::MatchExactly, 1);
229 if (groupList.isEmpty()) {
230 (void) new ProjectItem(listView, name, clipId);
235 if (parentId != -1) {
236 groupItem = getItemById(parentId);
238 else if (!groupName.isEmpty()) {
239 // Clip is in a group
240 QList<QTreeWidgetItem *> groupList = listView->findItems(groupName, Qt::MatchExactly, 1);
242 if (groupList.isEmpty()) {
243 QStringList itemName;
244 itemName<<QString::null<<groupName;
245 kDebug()<<"------- CREATING NEW GRP: "<<itemName;
246 groupItem = new ProjectItem(listView, itemName, m_clipIdCounter++);
248 else groupItem = (ProjectItem *) groupList.first();
250 if (groupItem) item = new ProjectItem(groupItem, name, elem, clipId);
251 else item = new ProjectItem(listView, name, elem, clipId);
252 if (!url.isEmpty()) {
253 item->setData(1, FullPathRole, url.path());
254 /* Nepomuk::File f( url.path() );
255 QString annotation = f.getAnnotation();
256 if (!annotation.isEmpty()) item->setText(2, annotation);*/
257 QString resource = url.path();
258 if (resource.endsWith("westley") || resource.endsWith("kdenlive")) {
261 if (KIO::NetAccess::download(url, tmpfile, 0)) {
263 if (file.open(QIODevice::ReadOnly)) {
264 doc.setContent(&file, false);
267 KIO::NetAccess::removeTempFile(tmpfile);
269 QDomNodeList subProds = doc.elementsByTagName("producer");
270 int ct = subProds.count();
271 for (int i = 0; i < ct ; i++)
273 QDomElement e = subProds.item(i).toElement();
275 addProducer(e, clipId);
283 if (elem.isNull() ) {
285 QDomElement element = doc.createElement("producer");
286 element.setAttribute("resource", url.path());
287 emit getFileProperties(element, clipId);
289 else emit getFileProperties(elem, clipId);
290 selectItemById(clipId);
293 void ProjectList::deleteClip(const int clipId)
295 ProjectItem *item = getItemById(clipId);
296 if (item) delete item;
299 void ProjectList::slotAddFolder()
301 QString folderName = KInputDialog::getText(i18n("New Folder"), i18n("Enter new folder name: "));
302 if (folderName.isEmpty()) return;
303 QStringList itemEntry;
304 itemEntry.append(QString::null);
305 itemEntry.append(folderName);
306 AddClipCommand *command = new AddClipCommand(this, itemEntry, QDomElement(), m_clipIdCounter++, KUrl(), folderName, true);
307 m_commandStack->push(command);
310 void ProjectList::slotAddClip(QUrl givenUrl, const QString &group)
312 if (!m_commandStack) kDebug()<<"!!!!!!!!!!!!!!!! NO CMD STK";
314 if (givenUrl.isEmpty())
315 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");
316 else list.append(givenUrl);
317 if (list.isEmpty()) return;
318 KUrl::List::Iterator it;
320 // ProjectItem *item;
322 for (it = list.begin(); it != list.end(); it++) {
323 QStringList itemEntry;
324 itemEntry.append(QString::null);
325 itemEntry.append((*it).fileName());
326 AddClipCommand *command = new AddClipCommand(this, itemEntry, QDomElement(), m_clipIdCounter++, *it, group, true);
327 m_commandStack->push(command);
328 //item = new ProjectItem(listView, itemEntry, QDomElement());
329 //item->setData(1, FullPathRole, (*it).path());
331 //listView->setCurrentItem(item);
334 void ProjectList::slotAddColorClip()
336 if (!m_commandStack) kDebug()<<"!!!!!!!!!!!!!!!! NO CMD STK";
337 QDialog *dia = new QDialog;
338 Ui::ColorClip_UI *dia_ui = new Ui::ColorClip_UI();
339 dia_ui->setupUi(dia);
340 dia_ui->clip_name->setText(i18n("Color Clip"));
341 dia_ui->clip_duration->setText(KdenliveSettings::color_duration());
342 if (dia->exec() == QDialog::Accepted)
345 QDomElement element = doc.createElement("producer");
346 element.setAttribute("mlt_service", "colour");
347 QString color = dia_ui->clip_color->color().name();
348 color = color.replace(0, 1, "0x") + "ff";
349 element.setAttribute("colour", color);
350 element.setAttribute("type", (int) DocClipBase::COLOR);
351 element.setAttribute("in", "0");
352 element.setAttribute("out", m_timecode.getFrameCount(dia_ui->clip_duration->text(), m_fps));
353 element.setAttribute("name", dia_ui->clip_name->text());
354 QStringList itemEntry;
355 itemEntry.append(QString::null);
356 itemEntry.append(dia_ui->clip_name->text());
357 AddClipCommand *command = new AddClipCommand(this, itemEntry, element, m_clipIdCounter++, KUrl(), QString::null, true);
358 m_commandStack->push(command);
359 // ProjectItem *item = new ProjectItem(listView, itemEntry, element, m_clipIdCounter++);
360 /*QPixmap pix(60, 40);
361 pix.fill(dia_ui->clip_color->color());
362 item->setIcon(0, QIcon(pix));*/
363 //listView->setCurrentItem(item);
370 void ProjectList::setDocument(KdenliveDoc *doc)
373 m_timecode = doc->timecode();
374 m_commandStack = doc->commandStack();
375 QDomNodeList prods = doc->producersList();
376 int ct = prods.count();
377 kDebug()<<"//////////// SETTING DOC, FOUND CLIPS: "<<prods.count();
379 for (int i = 0; i < ct ; i++)
381 QDomElement e = prods.item(i).toElement();
382 kDebug()<<"// IMPORT: "<<i<<", :"<<e.attribute("id", "non")<<", NAME: "<<e.attribute("name", "non");
383 if (!e.isNull()) addProducer(e);
385 QTreeWidgetItem *first = listView->topLevelItem(0);
386 if (first) listView->setCurrentItem(first);
387 m_toolbar->setEnabled(true);
390 QDomElement ProjectList::producersList()
393 QDomElement prods = doc.createElement("producerlist");
394 doc.appendChild(prods);
395 kDebug()<<"//////////// PRO LIST BUILD PRDSLIST ";
396 QTreeWidgetItemIterator it(listView);
398 if (!((ProjectItem *)(*it))->isGroup())
399 prods.appendChild(doc.importNode(((ProjectItem *)(*it))->toXml(), true));
406 void ProjectList::slotReplyGetFileProperties(int clipId, const QMap < QString, QString > &properties, const QMap < QString, QString > &metadata)
408 ProjectItem *item = getItemById(clipId);
409 if (item) item->setProperties(properties, metadata);
414 void ProjectList::slotReplyGetImage(int clipId, int pos, const QPixmap &pix, int w, int h)
416 ProjectItem *item = getItemById(clipId);
417 if (item) item->setIcon(0, pix);
420 ProjectItem *ProjectList::getItemById(int id)
422 QTreeWidgetItemIterator it(listView);
424 if (((ProjectItem *)(*it))->clipId() == id)
428 return ((ProjectItem *)(*it));
432 void ProjectList::addProducer(QDomElement producer, int parentId)
434 if (!m_commandStack) kDebug()<<"!!!!!!!!!!!!!!!! NO CMD STK";
435 DocClipBase::CLIPTYPE type = (DocClipBase::CLIPTYPE) producer.attribute("type").toInt();
438 QDomElement prods = doc.createElement("list");
439 doc.appendChild(prods);
440 prods.appendChild(doc.importNode(producer, true));*/
443 //kDebug()<<"////// ADDING PRODUCER:\n "<<doc.toString()<<"\n+++++++++++++++++";
444 int id = producer.attribute("id").toInt();
445 QString groupName = producer.attribute("group");
446 if (id >= m_clipIdCounter) m_clipIdCounter = id + 1;
447 else if (id == 0) id = m_clipIdCounter++;
449 if (type == DocClipBase::AUDIO || type == DocClipBase::VIDEO || type == DocClipBase::AV || type == DocClipBase::IMAGE || type == DocClipBase::PLAYLIST)
451 KUrl resource = KUrl(producer.attribute("resource"));
452 if (!resource.isEmpty()) {
453 QStringList itemEntry;
454 itemEntry.append(QString::null);
455 itemEntry.append(resource.fileName());
456 addClip(itemEntry, producer, id, resource, groupName, parentId);
457 /*AddClipCommand *command = new AddClipCommand(this, itemEntry, producer, id, resource, true);
458 m_commandStack->push(command);*/
461 /*ProjectItem *item = new ProjectItem(listView, itemEntry, producer);
462 item->setData(1, FullPathRole, resource.path());
463 item->setData(1, ClipTypeRole, (int) type);*/
467 else if (type == DocClipBase::COLOR) {
468 QString colour = producer.attribute("colour");
470 colour = colour.replace(0, 2, "#");
471 pix.fill(QColor(colour.left(7)));
472 QStringList itemEntry;
473 itemEntry.append(QString::null);
474 itemEntry.append(producer.attribute("name", i18n("Color clip")));
475 addClip(itemEntry, producer, id, KUrl(), groupName, parentId);
476 /*AddClipCommand *command = new AddClipCommand(this, itemEntry, producer, id, KUrl(), true);
477 m_commandStack->push(command);*/
478 //ProjectItem *item = new ProjectItem(listView, itemEntry, producer);
479 /*item->setIcon(0, QIcon(pix));
480 item->setData(1, ClipTypeRole, (int) type);*/
485 #include "projectlist.moc"