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"
45 #include "titlewidget.h"
46 #include "clipmanager.h"
47 #include "docclipbase.h"
48 #include "kdenlivedoc.h"
50 #include "projectlistview.h"
53 ProjectList::ProjectList(QWidget *parent)
54 : QWidget(parent), m_render(NULL), m_fps(-1), m_commandStack(NULL) {
56 QWidget *vbox = new QWidget;
57 listView = new ProjectListView(this);;
58 QVBoxLayout *layout = new QVBoxLayout;
62 searchView = new KTreeWidgetSearchLine(this);
63 m_toolbar = new QToolBar("projectToolBar", this);
64 m_toolbar->addWidget(searchView);
66 QToolButton *addButton = new QToolButton(m_toolbar);
67 QMenu *addMenu = new QMenu(this);
68 addButton->setMenu(addMenu);
69 addButton->setPopupMode(QToolButton::MenuButtonPopup);
70 m_toolbar->addWidget(addButton);
72 QAction *addClipButton = addMenu->addAction(KIcon("document-new"), i18n("Add Clip"));
73 connect(addClipButton, SIGNAL(triggered()), this, SLOT(slotAddClip()));
75 QAction *addColorClip = addMenu->addAction(KIcon("document-new"), i18n("Add Color Clip"));
76 connect(addColorClip, SIGNAL(triggered()), this, SLOT(slotAddColorClip()));
78 QAction *addTitleClip = addMenu->addAction(KIcon("document-new"), i18n("Add Title Clip"));
79 connect(addTitleClip, SIGNAL(triggered()), this, SLOT(slotAddTitleClip()));
81 m_deleteAction = m_toolbar->addAction(KIcon("edit-delete"), i18n("Delete Clip"));
82 connect(m_deleteAction, SIGNAL(triggered()), this, SLOT(slotRemoveClip()));
84 m_editAction = m_toolbar->addAction(KIcon("document-properties"), i18n("Edit Clip"));
85 connect(m_editAction, SIGNAL(triggered()), this, SLOT(slotEditClip()));
87 QAction *addFolderButton = addMenu->addAction(KIcon("folder-new"), i18n("Create Folder"));
88 connect(addFolderButton, SIGNAL(triggered()), this, SLOT(slotAddFolder()));
90 addButton->setDefaultAction(addClipButton);
92 layout->addWidget(m_toolbar);
93 layout->addWidget(listView);
95 //m_toolbar->setEnabled(false);
97 searchView->setTreeWidget(listView);
98 listView->setColumnCount(3);
100 headers << i18n("Thumbnail") << i18n("Filename") << i18n("Description");
101 listView->setHeaderLabels(headers);
102 listView->sortByColumn(1, Qt::AscendingOrder);
104 m_menu = new QMenu();
105 m_menu->addAction(addClipButton);
106 m_menu->addAction(addColorClip);
107 m_menu->addAction(addTitleClip);
108 m_menu->addAction(m_editAction);
109 m_menu->addAction(m_deleteAction);
110 m_menu->addAction(addFolderButton);
111 m_menu->insertSeparator(m_deleteAction);
113 connect(listView, SIGNAL(itemSelectionChanged()), this, SLOT(slotClipSelected()));
114 connect(listView, SIGNAL(requestMenu(const QPoint &, QTreeWidgetItem *)), this, SLOT(slotContextMenu(const QPoint &, QTreeWidgetItem *)));
115 connect(listView, SIGNAL(addClip()), this, SLOT(slotAddClip()));
116 connect(listView, SIGNAL(addClip(QUrl, const QString &)), this, SLOT(slotAddClip(QUrl, const QString &)));
117 connect(listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotUpdateItemDescription(QTreeWidgetItem *, int)));
119 m_listViewDelegate = new ItemDelegate(listView);
120 listView->setItemDelegate(m_listViewDelegate);
121 listView->setIconSize(QSize(60, 40));
122 listView->setSortingEnabled(true);
125 ProjectList::~ProjectList() {
130 void ProjectList::setRenderer(Render *projectRender) {
131 m_render = projectRender;
134 void ProjectList::slotClipSelected() {
135 ProjectItem *item = (ProjectItem*) listView->currentItem();
136 if (item && !item->isGroup()) emit clipSelected(item->toXml());
139 void ProjectList::slotUpdateItemDescription(QTreeWidgetItem *item, int column) {
140 if (column != 2) return;
141 ProjectItem *clip = (ProjectItem*) item;
142 CLIPTYPE type = clip->clipType();
143 if (type == AUDIO || type == VIDEO || type == AV || type == IMAGE || type == PLAYLIST) {
144 // Use Nepomuk system to store clip description
145 Nepomuk::Resource f(clip->clipUrl().path());
146 f.setDescription(item->text(2));
147 kDebug() << "NEPOMUK, SETTING CLIP: " << clip->clipUrl().path() << ", TO TEXT: " << item->text(2);
151 void ProjectList::slotEditClip() {
152 kDebug() << "//////////////////////////////////////// DBL CLK";
156 void ProjectList::slotEditClip(QTreeWidgetItem *item, int column) {
157 kDebug() << "//////////////////////////////////////// DBL CLK";
160 void ProjectList::slotContextMenu(const QPoint &pos, QTreeWidgetItem *item) {
163 QFrame *w = new QFrame;
164 w->setFrameShape(QFrame::StyledPanel);
166 w->setAutoFillBackground(true);
167 QHBoxLayout *layout = new QHBoxLayout;
168 layout->addWidget(new QLabel(i18n("Color:")));
169 layout->addWidget(new KColorButton());
170 layout->addWidget(new QLabel(i18n("Duration:")));
171 layout->addWidget(new KRestrictedLine());
172 w->setLayout(layout);
173 m_listViewDelegate->extendItem(w, listView->currentIndex());
176 m_editAction->setEnabled(enable);
177 m_deleteAction->setEnabled(enable);
182 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->numReferences() > 0) {
188 if (KMessageBox::questionYesNo(this, i18np("Delete clip <b>%2</b> ?<br>This will also remove the clip in timeline", "Delete clip <b>%2</b> ?<br>This will also remove its %1 clips in timeline", item->numReferences(), item->names().at(1)), i18n("Delete Clip")) != KMessageBox::Yes) return;
190 m_doc->deleteProjectClip(item->clipId());
193 void ProjectList::selectItemById(const int clipId) {
194 ProjectItem *item = getItemById(clipId);
195 if (item) listView->setCurrentItem(item);
198 void ProjectList::addClip(const QStringList &name, const QDomElement &elem, const int clipId, const KUrl &url, const QString &group, int parentId) {
199 kDebug() << "///////// ADDING VCLIP=: " << name;
201 ProjectItem *groupItem = NULL;
203 if (group.isEmpty()) groupName = elem.attribute("group", QString::null);
204 else groupName = group;
205 if (elem.isNull() && url.isEmpty()) {
207 groupName = name.at(1);
208 QList<QTreeWidgetItem *> groupList = listView->findItems(groupName, Qt::MatchExactly, 1);
209 if (groupList.isEmpty()) {
210 (void) new ProjectItem(listView, name, clipId);
215 if (parentId != -1) {
216 groupItem = getItemById(parentId);
217 } else if (!groupName.isEmpty()) {
218 // Clip is in a group
219 QList<QTreeWidgetItem *> groupList = listView->findItems(groupName, Qt::MatchExactly, 1);
221 if (groupList.isEmpty()) {
222 QStringList itemName;
223 itemName << QString::null << groupName;
224 kDebug() << "------- CREATING NEW GRP: " << itemName;
225 groupItem = new ProjectItem(listView, itemName, m_clipIdCounter++);
226 } else groupItem = (ProjectItem *) groupList.first();
228 if (groupItem) item = new ProjectItem(groupItem, name, elem, clipId);
229 else item = new ProjectItem(listView, name, elem, clipId);
230 if (!url.isEmpty()) {
231 // if file has Nepomuk comment, use it
232 Nepomuk::Resource f(url.path());
233 QString annotation = f.description();
234 if (!annotation.isEmpty()) item->setText(2, annotation);
235 QString resource = url.path();
236 if (resource.endsWith("westley") || resource.endsWith("kdenlive")) {
239 if (KIO::NetAccess::download(url, tmpfile, 0)) {
241 if (file.open(QIODevice::ReadOnly)) {
242 doc.setContent(&file, false);
245 KIO::NetAccess::removeTempFile(tmpfile);
247 QDomNodeList subProds = doc.elementsByTagName("producer");
248 int ct = subProds.count();
249 for (int i = 0; i < ct ; i++) {
250 QDomElement e = subProds.item(i).toElement();
252 addProducer(e, clipId);
262 QDomElement element = doc.createElement("producer");
263 element.setAttribute("resource", url.path());
264 emit getFileProperties(element, clipId);
265 } else emit getFileProperties(elem, clipId);
266 selectItemById(clipId);
269 void ProjectList::slotDeleteClip(int clipId) {
270 ProjectItem *item = getItemById(clipId);
271 if (item) delete item;
274 void ProjectList::slotAddFolder() {
276 QString folderName = KInputDialog::getText(i18n("New Folder"), i18n("Enter new folder name: "));
277 if (folderName.isEmpty()) return;
278 QStringList itemEntry;
279 itemEntry.append(QString::null);
280 itemEntry.append(folderName);
281 AddClipCommand *command = new AddClipCommand(this, itemEntry, QDomElement(), m_clipIdCounter++, KUrl(), folderName, true);
282 m_commandStack->push(command);*/
285 void ProjectList::slotAddClip(DocClipBase *clip) {
286 ProjectItem *item = new ProjectItem(listView, clip);
287 listView->setCurrentItem(item);
288 emit getFileProperties(clip->toXML(), clip->getId());
291 void ProjectList::slotUpdateClip(int id) {
292 ProjectItem *item = getItemById(id);
293 item->setData(1, UsageRole, QString::number(item->numReferences()));
296 void ProjectList::slotAddClip(QUrl givenUrl, const QString &group) {
297 if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
299 if (givenUrl.isEmpty())
300 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/mpeg 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");
301 else list.append(givenUrl);
302 if (list.isEmpty()) return;
303 KUrl::List::Iterator it;
305 for (it = list.begin(); it != list.end(); it++) {
306 m_doc->slotAddClipFile(*it, group);
310 void ProjectList::slotAddColorClip() {
311 if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
312 QDialog *dia = new QDialog;
313 Ui::ColorClip_UI *dia_ui = new Ui::ColorClip_UI();
314 dia_ui->setupUi(dia);
315 dia_ui->clip_name->setText(i18n("Color Clip"));
316 dia_ui->clip_duration->setText(KdenliveSettings::color_duration());
317 if (dia->exec() == QDialog::Accepted) {
318 QString color = dia_ui->clip_color->color().name();
319 color = color.replace(0, 1, "0x") + "ff";
320 m_doc->slotAddColorClipFile(dia_ui->clip_name->text(), color, dia_ui->clip_duration->text(), QString::null);
326 void ProjectList::slotAddTitleClip() {
328 if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
329 //QDialog *dia = new QDialog;
331 TitleWidget *dia_ui = new TitleWidget();
332 //dia_ui->setupUi(dia);
333 //dia_ui->clip_name->setText(i18n("Title Clip"));
334 //dia_ui->clip_duration->setText(KdenliveSettings::color_duration());
335 if (dia_ui->exec() == QDialog::Accepted) {
336 //QString color = dia_ui->clip_color->color().name();
337 //color = color.replace(0, 1, "0x") + "ff";
338 //m_doc->slotAddColorClipFile(dia_ui->clip_name->text(), color, dia_ui->clip_duration->text(), QString::null);
343 void ProjectList::setDocument(KdenliveDoc *doc) {
345 QList <DocClipBase*> list = doc->clipManager()->documentClipList();
346 for (int i = 0; i < list.count(); i++) {
347 slotAddClip(list.at(i));
351 m_timecode = doc->timecode();
352 m_commandStack = doc->commandStack();
354 /* QDomNodeList prods = doc->producersList();
355 int ct = prods.count();
356 kDebug() << "//////////// SETTING DOC, FOUND CLIPS: " << prods.count();
358 for (int i = 0; i < ct ; i++) {
359 QDomElement e = prods.item(i).toElement();
360 kDebug() << "// IMPORT: " << i << ", :" << e.attribute("id", "non") << ", NAME: " << e.attribute("name", "non");
361 if (!e.isNull()) addProducer(e);
363 QTreeWidgetItem *first = listView->topLevelItem(0);
364 if (first) listView->setCurrentItem(first);
365 m_toolbar->setEnabled(true);
368 QDomElement ProjectList::producersList() {
370 QDomElement prods = doc.createElement("producerlist");
371 doc.appendChild(prods);
372 kDebug() << "//////////// PRO LIST BUILD PRDSLIST ";
373 QTreeWidgetItemIterator it(listView);
375 if (!((ProjectItem *)(*it))->isGroup())
376 prods.appendChild(doc.importNode(((ProjectItem *)(*it))->toXml(), true));
383 void ProjectList::slotReplyGetFileProperties(int clipId, const QMap < QString, QString > &properties, const QMap < QString, QString > &metadata) {
384 ProjectItem *item = getItemById(clipId);
386 item->setProperties(properties, metadata);
387 emit receivedClipDuration(clipId, item->clipMaxDuration());
393 void ProjectList::slotReplyGetImage(int clipId, int pos, const QPixmap &pix, int w, int h) {
394 ProjectItem *item = getItemById(clipId);
395 if (item) item->setIcon(0, pix);
398 ProjectItem *ProjectList::getItemById(int id) {
399 QTreeWidgetItemIterator it(listView);
401 if (((ProjectItem *)(*it))->clipId() == id)
405 return ((ProjectItem *)(*it));
409 void ProjectList::addProducer(QDomElement producer, int parentId) {
410 if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
411 CLIPTYPE type = (CLIPTYPE) producer.attribute("type").toInt();
414 QDomElement prods = doc.createElement("list");
415 doc.appendChild(prods);
416 prods.appendChild(doc.importNode(producer, true));*/
419 //kDebug()<<"////// ADDING PRODUCER:\n "<<doc.toString()<<"\n+++++++++++++++++";
420 int id = producer.attribute("id").toInt();
421 QString groupName = producer.attribute("group");
422 if (id >= m_clipIdCounter) m_clipIdCounter = id + 1;
423 else if (id == 0) id = m_clipIdCounter++;
425 if (parentId != -1) {
426 // item is a westley playlist, adjust subproducers ids
427 id = (parentId + 1) * 10000 + id;
429 if (type == AUDIO || type == VIDEO || type == AV || type == IMAGE || type == PLAYLIST) {
430 KUrl resource = KUrl(producer.attribute("resource"));
431 if (!resource.isEmpty()) {
432 QStringList itemEntry;
433 itemEntry.append(QString::null);
434 itemEntry.append(resource.fileName());
435 addClip(itemEntry, producer, id, resource, groupName, parentId);
437 } else if (type == COLOR) {
438 QString colour = producer.attribute("colour");
440 colour = colour.replace(0, 2, "#");
441 pix.fill(QColor(colour.left(7)));
442 QStringList itemEntry;
443 itemEntry.append(QString::null);
444 itemEntry.append(producer.attribute("name", i18n("Color clip")));
445 addClip(itemEntry, producer, id, KUrl(), groupName, parentId);
450 #include "projectlist.moc"