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"
49 ProjectList::ProjectList(QWidget *parent)
50 : 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 QAction *addTitleClip = addMenu->addAction(KIcon("document-new"), i18n("Add Title Clip"));
75 connect(addTitleClip, SIGNAL(triggered()), this, SLOT(slotAddTitleClip()));
77 m_deleteAction = m_toolbar->addAction(KIcon("edit-delete"), i18n("Delete Clip"));
78 connect(m_deleteAction, SIGNAL(triggered()), this, SLOT(slotRemoveClip()));
80 m_editAction = m_toolbar->addAction(KIcon("document-properties"), i18n("Edit Clip"));
81 connect(m_editAction, SIGNAL(triggered()), this, SLOT(slotEditClip()));
83 QAction *addFolderButton = addMenu->addAction(KIcon("folder-new"), i18n("Create Folder"));
84 connect(addFolderButton, SIGNAL(triggered()), this, SLOT(slotAddFolder()));
86 addButton->setDefaultAction(addClipButton);
88 layout->addWidget(m_toolbar);
89 layout->addWidget(listView);
91 //m_toolbar->setEnabled(false);
93 searchView->setTreeWidget(listView);
94 listView->setColumnCount(3);
96 headers << i18n("Thumbnail") << i18n("Filename") << i18n("Description");
97 listView->setHeaderLabels(headers);
98 listView->sortByColumn(1, Qt::AscendingOrder);
100 m_menu = new QMenu();
101 m_menu->addAction(addClipButton);
102 m_menu->addAction(addColorClip);
103 m_menu->addAction(addTitleClip);
104 m_menu->addAction(m_editAction);
105 m_menu->addAction(m_deleteAction);
106 m_menu->addAction(addFolderButton);
107 m_menu->insertSeparator(m_deleteAction);
109 connect(listView, SIGNAL(itemSelectionChanged()), this, SLOT(slotClipSelected()));
110 connect(listView, SIGNAL(requestMenu(const QPoint &, QTreeWidgetItem *)), this, SLOT(slotContextMenu(const QPoint &, QTreeWidgetItem *)));
111 connect(listView, SIGNAL(addClip()), this, SLOT(slotAddClip()));
112 connect(listView, SIGNAL(addClip(QUrl, const QString &)), this, SLOT(slotAddClip(QUrl, const QString &)));
113 connect(listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotUpdateItemDescription(QTreeWidgetItem *, int)));
115 m_listViewDelegate = new ItemDelegate(listView);
116 listView->setItemDelegate(m_listViewDelegate);
117 listView->setIconSize(QSize(60, 40));
118 listView->setSortingEnabled(true);
121 ProjectList::~ProjectList() {
126 void ProjectList::setRenderer(Render *projectRender) {
127 m_render = projectRender;
130 void ProjectList::slotClipSelected() {
131 ProjectItem *item = (ProjectItem*) listView->currentItem();
132 if (item && !item->isGroup()) emit clipSelected(item->toXml());
135 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() {
148 kDebug() << "//////////////////////////////////////// DBL CLK";
152 void ProjectList::slotEditClip(QTreeWidgetItem *item, int column) {
153 kDebug() << "//////////////////////////////////////// DBL CLK";
156 void ProjectList::slotContextMenu(const QPoint &pos, QTreeWidgetItem *item) {
159 QFrame *w = new QFrame;
160 w->setFrameShape(QFrame::StyledPanel);
162 w->setAutoFillBackground(true);
163 QHBoxLayout *layout = new QHBoxLayout;
164 layout->addWidget(new QLabel(i18n("Color:")));
165 layout->addWidget(new KColorButton());
166 layout->addWidget(new QLabel(i18n("Duration:")));
167 layout->addWidget(new KRestrictedLine());
168 w->setLayout(layout);
169 m_listViewDelegate->extendItem(w, listView->currentIndex());
172 m_editAction->setEnabled(enable);
173 m_deleteAction->setEnabled(enable);
178 void ProjectList::slotRemoveClip() {
180 if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
181 if (!listView->currentItem()) return;
182 ProjectItem *item = ((ProjectItem *)listView->currentItem());
183 if (item->numReferences() > 0) {
184 if (KMessageBox::questionYesNo(this, i18n("Delete clip <b>%1</b> ?<br>This will also remove its %2 clips in timeline").arg(item->names().at(1)).arg(item->numReferences()), i18n("Delete Clip")) != KMessageBox::Yes) return;
186 m_doc->deleteProjectClip(item->clipId());
189 void ProjectList::selectItemById(const int clipId) {
190 ProjectItem *item = getItemById(clipId);
191 if (item) listView->setCurrentItem(item);
194 void ProjectList::addClip(const QStringList &name, const QDomElement &elem, const int clipId, const KUrl &url, const QString &group, int parentId) {
195 kDebug() << "///////// ADDING VCLIP=: " << name;
197 ProjectItem *groupItem = NULL;
199 if (group.isEmpty()) groupName = elem.attribute("group", QString::null);
200 else groupName = group;
201 if (elem.isNull() && url.isEmpty()) {
203 groupName = name.at(1);
204 QList<QTreeWidgetItem *> groupList = listView->findItems(groupName, Qt::MatchExactly, 1);
205 if (groupList.isEmpty()) {
206 (void) new ProjectItem(listView, name, clipId);
211 if (parentId != -1) {
212 groupItem = getItemById(parentId);
213 } else if (!groupName.isEmpty()) {
214 // Clip is in a group
215 QList<QTreeWidgetItem *> groupList = listView->findItems(groupName, Qt::MatchExactly, 1);
217 if (groupList.isEmpty()) {
218 QStringList itemName;
219 itemName << QString::null << groupName;
220 kDebug() << "------- CREATING NEW GRP: " << itemName;
221 groupItem = new ProjectItem(listView, itemName, m_clipIdCounter++);
222 } else groupItem = (ProjectItem *) groupList.first();
224 if (groupItem) item = new ProjectItem(groupItem, name, elem, clipId);
225 else item = new ProjectItem(listView, name, elem, clipId);
226 if (!url.isEmpty()) {
227 // if file has Nepomuk comment, use it
228 Nepomuk::Resource f(url.path());
229 QString annotation = f.description();
230 if (!annotation.isEmpty()) item->setText(2, annotation);
231 QString resource = url.path();
232 if (resource.endsWith("westley") || resource.endsWith("kdenlive")) {
235 if (KIO::NetAccess::download(url, tmpfile, 0)) {
237 if (file.open(QIODevice::ReadOnly)) {
238 doc.setContent(&file, false);
241 KIO::NetAccess::removeTempFile(tmpfile);
243 QDomNodeList subProds = doc.elementsByTagName("producer");
244 int ct = subProds.count();
245 for (int i = 0; i < ct ; i++) {
246 QDomElement e = subProds.item(i).toElement();
248 addProducer(e, clipId);
258 QDomElement element = doc.createElement("producer");
259 element.setAttribute("resource", url.path());
260 emit getFileProperties(element, clipId);
261 } else emit getFileProperties(elem, clipId);
262 selectItemById(clipId);
265 void ProjectList::slotDeleteClip(int clipId) {
266 ProjectItem *item = getItemById(clipId);
267 if (item) delete item;
270 void ProjectList::slotAddFolder() {
272 QString folderName = KInputDialog::getText(i18n("New Folder"), i18n("Enter new folder name: "));
273 if (folderName.isEmpty()) return;
274 QStringList itemEntry;
275 itemEntry.append(QString::null);
276 itemEntry.append(folderName);
277 AddClipCommand *command = new AddClipCommand(this, itemEntry, QDomElement(), m_clipIdCounter++, KUrl(), folderName, true);
278 m_commandStack->push(command);*/
281 void ProjectList::slotAddClip(DocClipBase *clip) {
282 ProjectItem *item = new ProjectItem(listView, clip);
283 listView->setCurrentItem(item);
284 emit getFileProperties(clip->toXML(), clip->getId());
287 void ProjectList::slotUpdateClip(int id) {
288 ProjectItem *item = getItemById(id);
289 item->setData(1, UsageRole, QString::number(item->numReferences()));
292 void ProjectList::slotAddClip(QUrl givenUrl, const QString &group) {
293 if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
295 if (givenUrl.isEmpty())
296 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");
297 else list.append(givenUrl);
298 if (list.isEmpty()) return;
299 KUrl::List::Iterator it;
301 for (it = list.begin(); it != list.end(); it++) {
302 m_doc->slotAddClipFile(*it, group);
306 void ProjectList::slotAddColorClip() {
307 if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
308 QDialog *dia = new QDialog;
309 Ui::ColorClip_UI *dia_ui = new Ui::ColorClip_UI();
310 dia_ui->setupUi(dia);
311 dia_ui->clip_name->setText(i18n("Color Clip"));
312 dia_ui->clip_duration->setText(KdenliveSettings::color_duration());
313 if (dia->exec() == QDialog::Accepted) {
314 QString color = dia_ui->clip_color->color().name();
315 color = color.replace(0, 1, "0x") + "ff";
316 m_doc->slotAddColorClipFile(dia_ui->clip_name->text(), color, dia_ui->clip_duration->text(), QString::null);
322 void ProjectList::slotAddTitleClip() {
324 if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
325 //QDialog *dia = new QDialog;
327 TitleWidget *dia_ui = new TitleWidget();
328 //dia_ui->setupUi(dia);
329 //dia_ui->clip_name->setText(i18n("Title Clip"));
330 //dia_ui->clip_duration->setText(KdenliveSettings::color_duration());
331 if (dia_ui->exec() == QDialog::Accepted) {
332 //QString color = dia_ui->clip_color->color().name();
333 //color = color.replace(0, 1, "0x") + "ff";
334 //m_doc->slotAddColorClipFile(dia_ui->clip_name->text(), color, dia_ui->clip_duration->text(), QString::null);
339 void ProjectList::setDocument(KdenliveDoc *doc) {
341 m_timecode = doc->timecode();
342 m_commandStack = doc->commandStack();
344 QDomNodeList prods = doc->producersList();
345 int ct = prods.count();
346 kDebug() << "//////////// SETTING DOC, FOUND CLIPS: " << prods.count();
348 for (int i = 0; i < ct ; i++) {
349 QDomElement e = prods.item(i).toElement();
350 kDebug() << "// IMPORT: " << i << ", :" << e.attribute("id", "non") << ", NAME: " << e.attribute("name", "non");
351 if (!e.isNull()) addProducer(e);
353 QTreeWidgetItem *first = listView->topLevelItem(0);
354 if (first) listView->setCurrentItem(first);
355 m_toolbar->setEnabled(true);
358 QDomElement ProjectList::producersList() {
360 QDomElement prods = doc.createElement("producerlist");
361 doc.appendChild(prods);
362 kDebug() << "//////////// PRO LIST BUILD PRDSLIST ";
363 QTreeWidgetItemIterator it(listView);
365 if (!((ProjectItem *)(*it))->isGroup())
366 prods.appendChild(doc.importNode(((ProjectItem *)(*it))->toXml(), true));
373 void ProjectList::slotReplyGetFileProperties(int clipId, const QMap < QString, QString > &properties, const QMap < QString, QString > &metadata) {
374 ProjectItem *item = getItemById(clipId);
376 item->setProperties(properties, metadata);
377 emit receivedClipDuration(clipId, item->clipMaxDuration());
383 void ProjectList::slotReplyGetImage(int clipId, int pos, const QPixmap &pix, int w, int h) {
384 ProjectItem *item = getItemById(clipId);
385 if (item) item->setIcon(0, pix);
388 ProjectItem *ProjectList::getItemById(int id) {
389 QTreeWidgetItemIterator it(listView);
391 if (((ProjectItem *)(*it))->clipId() == id)
395 return ((ProjectItem *)(*it));
399 void ProjectList::addProducer(QDomElement producer, int parentId) {
400 if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!! NO CMD STK";
401 CLIPTYPE type = (CLIPTYPE) producer.attribute("type").toInt();
404 QDomElement prods = doc.createElement("list");
405 doc.appendChild(prods);
406 prods.appendChild(doc.importNode(producer, true));*/
409 //kDebug()<<"////// ADDING PRODUCER:\n "<<doc.toString()<<"\n+++++++++++++++++";
410 int id = producer.attribute("id").toInt();
411 QString groupName = producer.attribute("group");
412 if (id >= m_clipIdCounter) m_clipIdCounter = id + 1;
413 else if (id == 0) id = m_clipIdCounter++;
415 if (parentId != -1) {
416 // item is a westley playlist, adjust subproducers ids
417 id = (parentId + 1) * 10000 + id;
419 if (type == AUDIO || type == VIDEO || type == AV || type == IMAGE || type == PLAYLIST) {
420 KUrl resource = KUrl(producer.attribute("resource"));
421 if (!resource.isEmpty()) {
422 QStringList itemEntry;
423 itemEntry.append(QString::null);
424 itemEntry.append(resource.fileName());
425 addClip(itemEntry, producer, id, resource, groupName, parentId);
427 } else if (type == COLOR) {
428 QString colour = producer.attribute("colour");
430 colour = colour.replace(0, 2, "#");
431 pix.fill(QColor(colour.left(7)));
432 QStringList itemEntry;
433 itemEntry.append(QString::null);
434 itemEntry.append(producer.attribute("name", i18n("Color clip")));
435 addClip(itemEntry, producer, id, KUrl(), groupName, parentId);
440 #include "projectlist.moc"