]> git.sesse.net Git - kdenlive/blob - src/projectlist.cpp
Reindent all source files
[kdenlive] / src / projectlist.cpp
1 /***************************************************************************
2  *   Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
3  *                                                                         *
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.                                   *
8  *                                                                         *
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.                          *
13  *                                                                         *
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  ***************************************************************************/
19
20
21 #include <QMouseEvent>
22 #include <QStylePainter>
23 #include <QPixmap>
24 #include <QIcon>
25 #include <QDialog>
26
27 #include <KDebug>
28 #include <KAction>
29 #include <KLocale>
30 #include <KFileDialog>
31 #include <KInputDialog>
32 #include <kio/netaccess.h>
33 #include <KMessageBox>
34
35 #include <nepomuk/global.h>
36 #include <nepomuk/resource.h>
37 #include <nepomuk/tag.h>
38
39 #include "projectlist.h"
40 #include "projectitem.h"
41 #include "kdenlivesettings.h"
42 #include "ui_colorclip_ui.h"
43
44 #include "definitions.h"
45 #include "titlewidget.h"
46
47 #include <QtGui>
48
49 ProjectList::ProjectList(QWidget *parent)
50         : QWidget(parent), m_render(NULL), m_fps(-1), m_commandStack(NULL) {
51
52     QWidget *vbox = new QWidget;
53     listView = new ProjectListView(this);;
54     QVBoxLayout *layout = new QVBoxLayout;
55     m_clipIdCounter = 0;
56
57     // setup toolbar
58     searchView = new KTreeWidgetSearchLine(this);
59     m_toolbar = new QToolBar("projectToolBar", this);
60     m_toolbar->addWidget(searchView);
61
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);
67
68     QAction *addClipButton = addMenu->addAction(KIcon("document-new"), i18n("Add Clip"));
69     connect(addClipButton, SIGNAL(triggered()), this, SLOT(slotAddClip()));
70
71     QAction *addColorClip = addMenu->addAction(KIcon("document-new"), i18n("Add Color Clip"));
72     connect(addColorClip, SIGNAL(triggered()), this, SLOT(slotAddColorClip()));
73
74     QAction *addTitleClip = addMenu->addAction(KIcon("document-new"), i18n("Add Title Clip"));
75     connect(addTitleClip, SIGNAL(triggered()), this, SLOT(slotAddTitleClip()));
76
77     m_deleteAction = m_toolbar->addAction(KIcon("edit-delete"), i18n("Delete Clip"));
78     connect(m_deleteAction, SIGNAL(triggered()), this, SLOT(slotRemoveClip()));
79
80     m_editAction = m_toolbar->addAction(KIcon("document-properties"), i18n("Edit Clip"));
81     connect(m_editAction, SIGNAL(triggered()), this, SLOT(slotEditClip()));
82
83     QAction *addFolderButton = addMenu->addAction(KIcon("folder-new"), i18n("Create Folder"));
84     connect(addFolderButton, SIGNAL(triggered()), this, SLOT(slotAddFolder()));
85
86     addButton->setDefaultAction(addClipButton);
87
88     layout->addWidget(m_toolbar);
89     layout->addWidget(listView);
90     setLayout(layout);
91     //m_toolbar->setEnabled(false);
92
93     searchView->setTreeWidget(listView);
94     listView->setColumnCount(3);
95     QStringList headers;
96     headers << i18n("Thumbnail") << i18n("Filename") << i18n("Description");
97     listView->setHeaderLabels(headers);
98     listView->sortByColumn(1, Qt::AscendingOrder);
99
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);
108
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)));
114
115     m_listViewDelegate = new ItemDelegate(listView);
116     listView->setItemDelegate(m_listViewDelegate);
117     listView->setIconSize(QSize(60, 40));
118     listView->setSortingEnabled(true);
119 }
120
121 ProjectList::~ProjectList() {
122     delete m_menu;
123     delete m_toolbar;
124 }
125
126 void ProjectList::setRenderer(Render *projectRender) {
127     m_render = projectRender;
128 }
129
130 void ProjectList::slotClipSelected() {
131     ProjectItem *item = (ProjectItem*) listView->currentItem();
132     if (item && !item->isGroup()) emit clipSelected(item->toXml());
133 }
134
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);
144     }
145 }
146
147 void ProjectList::slotEditClip() {
148     kDebug() << "////////////////////////////////////////   DBL CLK";
149 }
150
151
152 void ProjectList::slotEditClip(QTreeWidgetItem *item, int column) {
153     kDebug() << "////////////////////////////////////////   DBL CLK";
154 }
155
156 void ProjectList::slotContextMenu(const QPoint &pos, QTreeWidgetItem *item) {
157     bool enable = false;
158     if (item) {
159         QFrame *w = new QFrame;
160         w->setFrameShape(QFrame::StyledPanel);
161         w->setLineWidth(2);
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());
170         enable = true;
171     }
172     m_editAction->setEnabled(enable);
173     m_deleteAction->setEnabled(enable);
174
175     m_menu->popup(pos);
176 }
177
178 void ProjectList::slotRemoveClip() {
179
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;
185     }
186     m_doc->deleteProjectClip(item->clipId());
187 }
188
189 void ProjectList::selectItemById(const int clipId) {
190     ProjectItem *item = getItemById(clipId);
191     if (item) listView->setCurrentItem(item);
192 }
193
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;
196     ProjectItem *item;
197     ProjectItem *groupItem = NULL;
198     QString groupName;
199     if (group.isEmpty()) groupName = elem.attribute("group", QString::null);
200     else groupName = group;
201     if (elem.isNull() && url.isEmpty()) {
202         // this is a folder
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);
207         }
208         return;
209     }
210
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);
216
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();
223     }
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")) {
233             QString tmpfile;
234             QDomDocument doc;
235             if (KIO::NetAccess::download(url, tmpfile, 0)) {
236                 QFile file(tmpfile);
237                 if (file.open(QIODevice::ReadOnly)) {
238                     doc.setContent(&file, false);
239                     file.close();
240                 }
241                 KIO::NetAccess::removeTempFile(tmpfile);
242
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();
247                     if (!e.isNull()) {
248                         addProducer(e, clipId);
249                     }
250                 }
251             }
252         }
253
254     }
255
256     if (elem.isNull()) {
257         QDomDocument doc;
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);
263 }
264
265 void ProjectList::slotDeleteClip(int clipId) {
266     ProjectItem *item = getItemById(clipId);
267     if (item) delete item;
268 }
269
270 void ProjectList::slotAddFolder() {
271     /*
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);*/
279 }
280
281 void ProjectList::slotAddClip(DocClipBase *clip) {
282     ProjectItem *item = new ProjectItem(listView, clip);
283     listView->setCurrentItem(item);
284     emit getFileProperties(clip->toXML(), clip->getId());
285 }
286
287 void ProjectList::slotUpdateClip(int id) {
288     ProjectItem *item = getItemById(id);
289     item->setData(1, UsageRole, QString::number(item->numReferences()));
290 }
291
292 void ProjectList::slotAddClip(QUrl givenUrl, const QString &group) {
293     if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!!  NO CMD STK";
294     KUrl::List list;
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;
300
301     for (it = list.begin(); it != list.end(); it++) {
302         m_doc->slotAddClipFile(*it, group);
303     }
304 }
305
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);
317     }
318     delete dia_ui;
319     delete dia;
320 }
321
322 void ProjectList::slotAddTitleClip() {
323
324     if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!!  NO CMD STK";
325     //QDialog *dia = new QDialog;
326
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);
335     }
336     delete dia_ui;
337     //delete dia;
338 }
339 void ProjectList::setDocument(KdenliveDoc *doc) {
340     m_fps = doc->fps();
341     m_timecode = doc->timecode();
342     m_commandStack = doc->commandStack();
343     m_doc = doc;
344     QDomNodeList prods = doc->producersList();
345     int ct = prods.count();
346     kDebug() << "////////////  SETTING DOC, FOUND CLIPS: " << prods.count();
347     listView->clear();
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);
352     }
353     QTreeWidgetItem *first = listView->topLevelItem(0);
354     if (first) listView->setCurrentItem(first);
355     m_toolbar->setEnabled(true);
356 }
357
358 QDomElement ProjectList::producersList() {
359     QDomDocument doc;
360     QDomElement prods = doc.createElement("producerlist");
361     doc.appendChild(prods);
362     kDebug() << "////////////  PRO LIST BUILD PRDSLIST ";
363     QTreeWidgetItemIterator it(listView);
364     while (*it) {
365         if (!((ProjectItem *)(*it))->isGroup())
366             prods.appendChild(doc.importNode(((ProjectItem *)(*it))->toXml(), true));
367         ++it;
368     }
369     return prods;
370 }
371
372
373 void ProjectList::slotReplyGetFileProperties(int clipId, const QMap < QString, QString > &properties, const QMap < QString, QString > &metadata) {
374     ProjectItem *item = getItemById(clipId);
375     if (item) {
376         item->setProperties(properties, metadata);
377         emit receivedClipDuration(clipId, item->clipMaxDuration());
378     }
379 }
380
381
382
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);
386 }
387
388 ProjectItem *ProjectList::getItemById(int id) {
389     QTreeWidgetItemIterator it(listView);
390     while (*it) {
391         if (((ProjectItem *)(*it))->clipId() == id)
392             break;
393         ++it;
394     }
395     return ((ProjectItem *)(*it));
396 }
397
398
399 void ProjectList::addProducer(QDomElement producer, int parentId) {
400     if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!!  NO CMD STK";
401     CLIPTYPE type = (CLIPTYPE) producer.attribute("type").toInt();
402
403     /*QDomDocument doc;
404     QDomElement prods = doc.createElement("list");
405     doc.appendChild(prods);
406     prods.appendChild(doc.importNode(producer, true));*/
407
408
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++;
414
415     if (parentId != -1) {
416         // item is a westley playlist, adjust subproducers ids
417         id = (parentId + 1) * 10000 + id;
418     }
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);
426         }
427     } else if (type == COLOR) {
428         QString colour = producer.attribute("colour");
429         QPixmap pix(60, 40);
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);
436     }
437
438 }
439
440 #include "projectlist.moc"