]> git.sesse.net Git - kdenlive/blobdiff - src/projectlist.cpp
don't forget to cleanup clipmanager
[kdenlive] / src / projectlist.cpp
index b2ee5011eb5d18e6de3bd3ccb14b5e6dea03fe55..7580fbf1ceb6955943eae18275d9fb2b8c51bf3d 100644 (file)
@@ -23,6 +23,7 @@
 #include <QPixmap>
 #include <QIcon>
 #include <QDialog>
+#include <QtGui>
 
 #include <KDebug>
 #include <KAction>
 #include "projectlist.h"
 #include "projectitem.h"
 #include "kdenlivesettings.h"
+#include "slideshowclip.h"
 #include "ui_colorclip_ui.h"
 
-#include "definitions.h"
-#include "titlewidget.h"
 
-#include <QtGui>
+#include "definitions.h"
+#include "clipmanager.h"
+#include "docclipbase.h"
+#include "kdenlivedoc.h"
+#include "renderer.h"
+#include "kthumb.h"
+#include "projectlistview.h"
 
 ProjectList::ProjectList(QWidget *parent)
         : QWidget(parent), m_render(NULL), m_fps(-1), m_commandStack(NULL) {
@@ -71,6 +77,9 @@ ProjectList::ProjectList(QWidget *parent)
     QAction *addColorClip = addMenu->addAction(KIcon("document-new"), i18n("Add Color Clip"));
     connect(addColorClip, SIGNAL(triggered()), this, SLOT(slotAddColorClip()));
 
+    QAction *addSlideClip = addMenu->addAction(KIcon("document-new"), i18n("Add Slideshow Clip"));
+    connect(addSlideClip, SIGNAL(triggered()), this, SLOT(slotAddSlideshowClip()));
+
     QAction *addTitleClip = addMenu->addAction(KIcon("document-new"), i18n("Add Title Clip"));
     connect(addTitleClip, SIGNAL(triggered()), this, SLOT(slotAddTitleClip()));
 
@@ -91,11 +100,6 @@ ProjectList::ProjectList(QWidget *parent)
     //m_toolbar->setEnabled(false);
 
     searchView->setTreeWidget(listView);
-    listView->setColumnCount(3);
-    QStringList headers;
-    headers << i18n("Thumbnail") << i18n("Filename") << i18n("Description");
-    listView->setHeaderLabels(headers);
-    listView->sortByColumn(1, Qt::AscendingOrder);
 
     m_menu = new QMenu();
     m_menu->addAction(addClipButton);
@@ -110,12 +114,11 @@ ProjectList::ProjectList(QWidget *parent)
     connect(listView, SIGNAL(requestMenu(const QPoint &, QTreeWidgetItem *)), this, SLOT(slotContextMenu(const QPoint &, QTreeWidgetItem *)));
     connect(listView, SIGNAL(addClip()), this, SLOT(slotAddClip()));
     connect(listView, SIGNAL(addClip(QUrl, const QString &)), this, SLOT(slotAddClip(QUrl, const QString &)));
-    connect(listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotUpdateItemDescription(QTreeWidgetItem *, int)));
+    connect(listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotItemEdited(QTreeWidgetItem *, int)));
+    connect(listView, SIGNAL(showProperties(DocClipBase *)), this, SIGNAL(showClipProperties(DocClipBase *)));
 
     m_listViewDelegate = new ItemDelegate(listView);
     listView->setItemDelegate(m_listViewDelegate);
-    listView->setIconSize(QSize(60, 40));
-    listView->setSortingEnabled(true);
 }
 
 ProjectList::~ProjectList() {
@@ -123,50 +126,60 @@ ProjectList::~ProjectList() {
     delete m_toolbar;
 }
 
+void ProjectList::slotEditClip() {
+    ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
+    if (item && !item->isGroup()) emit clipSelected(item->toXml());
+    emit showClipProperties(item->referencedClip());
+}
+
+
+
 void ProjectList::setRenderer(Render *projectRender) {
     m_render = projectRender;
 }
 
 void ProjectList::slotClipSelected() {
-    ProjectItem *item = (ProjectItem*) listView->currentItem();
+    ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
     if (item && !item->isGroup()) emit clipSelected(item->toXml());
 }
 
-void ProjectList::slotUpdateItemDescription(QTreeWidgetItem *item, int column) {
-    if (column != 2) return;
-    ProjectItem *clip = (ProjectItem*) item;
-    CLIPTYPE type = clip->clipType();
-    if (type == AUDIO || type == VIDEO || type == AV || type == IMAGE || type == PLAYLIST) {
-        // Use Nepomuk system to store clip description
-        Nepomuk::Resource f(clip->clipUrl().path());
-        f.setDescription(item->text(2));
-        kDebug() << "NEPOMUK, SETTING CLIP: " << clip->clipUrl().path() << ", TO TEXT: " << item->text(2);
+void ProjectList::slotUpdateClipProperties(int id, QMap <QString, QString> properties) {
+    ProjectItem *item = getItemById(id);
+    if (item) {
+        slotUpdateClipProperties(item, properties);
+        if (properties.contains("colour") || properties.contains("resource")) slotRefreshClipThumbnail(item);
+        if (properties.contains("out")) item->changeDuration(properties.value("out").toInt());
     }
 }
 
-void ProjectList::slotEditClip() {
-    kDebug() << "////////////////////////////////////////   DBL CLK";
+void ProjectList::slotUpdateClipProperties(ProjectItem *clip, QMap <QString, QString> properties) {
+    if (!clip) return;
+    clip->setProperties(properties);
+    if (properties.contains("description")) {
+        CLIPTYPE type = clip->clipType();
+        clip->setText(2, properties.value("description"));
+        if (type == AUDIO || type == VIDEO || type == AV || type == IMAGE || type == PLAYLIST) {
+            // Use Nepomuk system to store clip description
+            Nepomuk::Resource f(clip->clipUrl().path());
+            if (f.isValid()) f.setDescription(properties.value("description"));
+        }
+    }
 }
 
-
-void ProjectList::slotEditClip(QTreeWidgetItem *item, int column) {
-    kDebug() << "////////////////////////////////////////   DBL CLK";
+void ProjectList::slotItemEdited(QTreeWidgetItem *item, int column) {
+    ProjectItem *clip = static_cast <ProjectItem*>(item);
+    if (column == 2) {
+        QMap <QString, QString> props;
+        props["description"] = item->text(2);
+        slotUpdateClipProperties(clip, props);
+    } else if (column == 1 && clip->clipType() == FOLDER) {
+        m_doc->slotEditFolder(item->text(1), clip->groupName(), clip->clipId());
+    }
 }
 
 void ProjectList::slotContextMenu(const QPoint &pos, QTreeWidgetItem *item) {
     bool enable = false;
     if (item) {
-        QFrame *w = new QFrame;
-        w->setFrameShape(QFrame::StyledPanel);
-        w->setLineWidth(2);
-        w->setAutoFillBackground(true);
-        QHBoxLayout *layout = new QHBoxLayout;
-        layout->addWidget(new QLabel(i18n("Color:")));
-        layout->addWidget(new KColorButton());
-        layout->addWidget(new QLabel(i18n("Duration:")));
-        layout->addWidget(new KRestrictedLine());
-        w->setLayout(layout);
-        m_listViewDelegate->extendItem(w, listView->currentIndex());
         enable = true;
     }
     m_editAction->setEnabled(enable);
@@ -176,14 +189,24 @@ void ProjectList::slotContextMenu(const QPoint &pos, QTreeWidgetItem *item) {
 }
 
 void ProjectList::slotRemoveClip() {
-
-    if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!!  NO CMD STK";
     if (!listView->currentItem()) return;
-    ProjectItem *item = ((ProjectItem *)listView->currentItem());
+    ProjectItem *item = static_cast <ProjectItem *>(listView->currentItem());
+    QList <int> ids;
+    QMap <QString, int> folderids;
+    if (item->clipType() == FOLDER) folderids[item->groupName()] = item->clipId();
+    else ids << item->clipId();
     if (item->numReferences() > 0) {
-        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;
+        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;
+    } else if (item->clipType() == FOLDER && item->childCount() > 0) {
+        int children = item->childCount();
+        if (KMessageBox::questionYesNo(this, i18n("Delete folder <b>%2</b> ?<br>This will also remove the %1 clips in that folder", children, item->names().at(1)), i18n("Delete Folder")) != KMessageBox::Yes) return;
+        for (int i = 0; i < children; ++i) {
+            ProjectItem *child = static_cast <ProjectItem *>(item->child(i));
+            ids << child->clipId();
+        }
     }
-    m_doc->deleteProjectClip(item->clipId());
+    if (!ids.isEmpty()) m_doc->deleteProjectClip(ids);
+    if (!folderids.isEmpty()) m_doc->deleteProjectFolder(folderids);
 }
 
 void ProjectList::selectItemById(const int clipId) {
@@ -196,14 +219,14 @@ void ProjectList::addClip(const QStringList &name, const QDomElement &elem, cons
     ProjectItem *item;
     ProjectItem *groupItem = NULL;
     QString groupName;
-    if (group.isEmpty()) groupName = elem.attribute("group", QString::null);
+    if (group.isEmpty()) groupName = elem.attribute("groupname", QString::null);
     else groupName = group;
     if (elem.isNull() && url.isEmpty()) {
         // this is a folder
         groupName = name.at(1);
         QList<QTreeWidgetItem *> groupList = listView->findItems(groupName, Qt::MatchExactly, 1);
         if (groupList.isEmpty())  {
-            (void) new ProjectItem(listView, name, clipId);
+            (void) new ProjectItem(listView, name, m_doc->getFreeClipId());
         }
         return;
     }
@@ -218,7 +241,7 @@ void ProjectList::addClip(const QStringList &name, const QDomElement &elem, cons
             QStringList itemName;
             itemName << QString::null << groupName;
             kDebug() << "-------  CREATING NEW GRP: " << itemName;
-            groupItem = new ProjectItem(listView, itemName, m_clipIdCounter++);
+            groupItem = new ProjectItem(listView, itemName, m_doc->getFreeClipId());
         } else groupItem = (ProjectItem *) groupList.first();
     }
     if (groupItem) item = new ProjectItem(groupItem, name, elem, clipId);
@@ -226,7 +249,9 @@ void ProjectList::addClip(const QStringList &name, const QDomElement &elem, cons
     if (!url.isEmpty()) {
         // if file has Nepomuk comment, use it
         Nepomuk::Resource f(url.path());
-        QString annotation = f.description();
+        QString annotation;
+        if (f.isValid()) annotation = f.description();
+
         if (!annotation.isEmpty()) item->setText(2, annotation);
         QString resource = url.path();
         if (resource.endsWith("westley") || resource.endsWith("kdenlive")) {
@@ -264,23 +289,76 @@ void ProjectList::addClip(const QStringList &name, const QDomElement &elem, cons
 
 void ProjectList::slotDeleteClip(int clipId) {
     ProjectItem *item = getItemById(clipId);
-    if (item) delete item;
+    QTreeWidgetItem *p = item->parent();
+    if (p) {
+        kDebug() << "///////  DELETEED CLIP HAS A PARENT... " << p->indexOfChild(item);
+        QTreeWidgetItem *clone = p->takeChild(p->indexOfChild(item));
+    } else if (item) delete item;
 }
 
 void ProjectList::slotAddFolder() {
-    /*
-      QString folderName = KInputDialog::getText(i18n("New Folder"), i18n("Enter new folder name: "));
-      if (folderName.isEmpty()) return;
-      QStringList itemEntry;
-      itemEntry.append(QString::null);
-      itemEntry.append(folderName);
-      AddClipCommand *command = new AddClipCommand(this, itemEntry, QDomElement(), m_clipIdCounter++, KUrl(), folderName, true);
-      m_commandStack->push(command);*/
+
+    // QString folderName = KInputDialog::getText(i18n("New Folder"), i18n("Enter new folder name: "));
+    // if (folderName.isEmpty()) return;
+    m_doc->slotAddFolder(i18n("Folder")); //folderName);
+}
+
+void ProjectList::slotAddFolder(const QString foldername, int clipId, bool remove, bool edit) {
+    if (remove) {
+        ProjectItem *item;
+        QTreeWidgetItemIterator it(listView);
+        while (*it) {
+            item = static_cast <ProjectItem *>(*it);
+            if (item->clipType() == FOLDER && item->clipId() == clipId) {
+                delete item;
+                break;
+            }
+            ++it;
+        }
+    } else {
+        if (edit) {
+            disconnect(listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotUpdateItemDescription(QTreeWidgetItem *, int)));
+            ProjectItem *item;
+            QTreeWidgetItemIterator it(listView);
+            while (*it) {
+                item = static_cast <ProjectItem *>(*it);
+                if (item->clipType() == FOLDER && item->clipId() == clipId) {
+                    item->setText(1, foldername);
+                    break;
+                }
+                ++it;
+            }
+            connect(listView, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotUpdateItemDescription(QTreeWidgetItem *, int)));
+        } else {
+            QStringList text;
+            text << QString() << foldername;
+            (void) new ProjectItem(listView, text, clipId);
+        }
+    }
 }
 
 void ProjectList::slotAddClip(DocClipBase *clip) {
-    ProjectItem *item = new ProjectItem(listView, clip);
-    listView->setCurrentItem(item);
+    const int parent = clip->toXML().attribute("groupid").toInt();
+    ProjectItem *item = NULL;
+    if (parent != 0) {
+        ProjectItem *parentitem = getItemById(parent);
+        if (parentitem) item = new ProjectItem(parentitem, clip);
+    }
+    if (item == NULL) item = new ProjectItem(listView, clip);
+
+    KUrl url = clip->fileURL();
+    if (!url.isEmpty()) {
+        // if file has Nepomuk comment, use it
+        Nepomuk::Resource f(url.path());
+        QString annotation;
+        if (f.isValid()) {
+            annotation = f.description();
+            /*
+            Nepomuk::Tag tag("test");
+            f.addTag(tag);*/
+        } else kDebug() << "---  CANNOT CONTACT NEPOMUK";
+        if (!annotation.isEmpty()) item->setText(2, annotation);
+    }
     emit getFileProperties(clip->toXML(), clip->getId());
 }
 
@@ -289,7 +367,7 @@ void ProjectList::slotUpdateClip(int id) {
     item->setData(1, UsageRole, QString::number(item->numReferences()));
 }
 
-void ProjectList::slotAddClip(QUrl givenUrl, const QString &group) {
+void ProjectList::slotAddClip(QUrl givenUrl, QString group) {
     if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!!  NO CMD STK";
     KUrl::List list;
     if (givenUrl.isEmpty())
@@ -297,15 +375,28 @@ void ProjectList::slotAddClip(QUrl givenUrl, const QString &group) {
     else list.append(givenUrl);
     if (list.isEmpty()) return;
     KUrl::List::Iterator it;
-
+    int groupId = -1;
+    if (group.isEmpty()) {
+        ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
+        if (item && item->clipType() != FOLDER) {
+            while (item->parent()) {
+                item = static_cast <ProjectItem*>(item->parent());
+                if (item->clipType() == FOLDER) break;
+            }
+        }
+        if (item && item->clipType() == FOLDER) {
+            group = item->groupName();
+            groupId = item->clipId();
+        }
+    }
     for (it = list.begin(); it != list.end(); it++) {
-        m_doc->slotAddClipFile(*it, group);
+        m_doc->slotAddClipFile(*it, group, groupId);
     }
 }
 
 void ProjectList::slotAddColorClip() {
     if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!!  NO CMD STK";
-    QDialog *dia = new QDialog;
+    QDialog *dia = new QDialog(this);
     Ui::ColorClip_UI *dia_ui = new Ui::ColorClip_UI();
     dia_ui->setupUi(dia);
     dia_ui->clip_name->setText(i18n("Color Clip"));
@@ -313,43 +404,90 @@ void ProjectList::slotAddColorClip() {
     if (dia->exec() == QDialog::Accepted) {
         QString color = dia_ui->clip_color->color().name();
         color = color.replace(0, 1, "0x") + "ff";
-        m_doc->slotAddColorClipFile(dia_ui->clip_name->text(), color, dia_ui->clip_duration->text(), QString::null);
+
+        QString group = QString();
+        int groupId = -1;
+        ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
+        if (item && item->clipType() != FOLDER) {
+            while (item->parent()) {
+                item = static_cast <ProjectItem*>(item->parent());
+                if (item->clipType() == FOLDER) break;
+            }
+        }
+        if (item && item->clipType() == FOLDER) {
+            group = item->groupName();
+            groupId = item->clipId();
+        }
+
+        m_doc->slotAddColorClipFile(dia_ui->clip_name->text(), color, dia_ui->clip_duration->text(), group, groupId);
     }
     delete dia_ui;
     delete dia;
 }
 
-void ProjectList::slotAddTitleClip() {
 
+void ProjectList::slotAddSlideshowClip() {
     if (!m_commandStack) kDebug() << "!!!!!!!!!!!!!!!!  NO CMD STK";
-    //QDialog *dia = new QDialog;
-
-    TitleWidget *dia_ui = new TitleWidget();
-    //dia_ui->setupUi(dia);
-    //dia_ui->clip_name->setText(i18n("Title Clip"));
-    //dia_ui->clip_duration->setText(KdenliveSettings::color_duration());
-    if (dia_ui->exec() == QDialog::Accepted) {
-        //QString color = dia_ui->clip_color->color().name();
-        //color = color.replace(0, 1, "0x") + "ff";
-        //m_doc->slotAddColorClipFile(dia_ui->clip_name->text(), color, dia_ui->clip_duration->text(), QString::null);
+    SlideshowClip *dia = new SlideshowClip(this);
+
+    if (dia->exec() == QDialog::Accepted) {
+
+        QString group = QString();
+        int groupId = -1;
+        ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
+        if (item && item->clipType() != FOLDER) {
+            while (item->parent()) {
+                item = static_cast <ProjectItem*>(item->parent());
+                if (item->clipType() == FOLDER) break;
+            }
+        }
+        if (item && item->clipType() == FOLDER) {
+            group = item->groupName();
+            groupId = item->clipId();
+        }
+
+        m_doc->slotAddSlideshowClipFile(dia->clipName(), dia->selectedPath(), dia->imageCount(), dia->clipDuration(), dia->loop(), group, groupId);
     }
-    delete dia_ui;
-    //delete dia;
+    delete dia;
 }
+void ProjectList::slotAddTitleClip() {
+    QString group = QString();
+    int groupId = -1;
+    ProjectItem *item = static_cast <ProjectItem*>(listView->currentItem());
+    if (item && item->clipType() != FOLDER) {
+        while (item->parent()) {
+            item = static_cast <ProjectItem*>(item->parent());
+            if (item->clipType() == FOLDER) break;
+        }
+    }
+    if (item && item->clipType() == FOLDER) {
+        group = item->groupName();
+        groupId = item->clipId();
+    }
+
+    m_doc->slotCreateTextClip(group, groupId);
+}
+
 void ProjectList::setDocument(KdenliveDoc *doc) {
+    listView->clear();
+    QList <DocClipBase*> list = doc->clipManager()->documentClipList();
+    for (int i = 0; i < list.count(); i++) {
+        slotAddClip(list.at(i));
+    }
+
     m_fps = doc->fps();
     m_timecode = doc->timecode();
     m_commandStack = doc->commandStack();
     m_doc = doc;
-    QDomNodeList prods = doc->producersList();
-    int ct = prods.count();
-    kDebug() << "////////////  SETTING DOC, FOUND CLIPS: " << prods.count();
-    listView->clear();
-    for (int i = 0; i <  ct ; i++) {
-        QDomElement e = prods.item(i).toElement();
-        kDebug() << "// IMPORT: " << i << ", :" << e.attribute("id", "non") << ", NAME: " << e.attribute("name", "non");
-        if (!e.isNull()) addProducer(e);
-    }
+    /*    QDomNodeList prods = doc->producersList();
+        int ct = prods.count();
+        kDebug() << "////////////  SETTING DOC, FOUND CLIPS: " << prods.count();
+        listView->clear();
+        for (int i = 0; i <  ct ; i++) {
+            QDomElement e = prods.item(i).toElement();
+            kDebug() << "// IMPORT: " << i << ", :" << e.attribute("id", "non") << ", NAME: " << e.attribute("name", "non");
+            if (!e.isNull()) addProducer(e);
+        }*/
     QTreeWidgetItem *first = listView->topLevelItem(0);
     if (first) listView->setCurrentItem(first);
     m_toolbar->setEnabled(true);
@@ -369,17 +507,30 @@ QDomElement ProjectList::producersList() {
     return prods;
 }
 
+void ProjectList::slotRefreshClipThumbnail(int clipId) {
+    ProjectItem *item = getItemById(clipId);
+    if (item) slotRefreshClipThumbnail(item);
+}
+
+void ProjectList::slotRefreshClipThumbnail(ProjectItem *item) {
+    if (item) {
+        int height = 40;
+        int width = (int)(height  * (double) m_render->renderWidth() / m_render->renderHeight());
+        QPixmap pix = KThumb::getImage(item->toXml(), item->referencedClip()->getProjectThumbFrame(), width, height);
+        //QPixmap pix = KThumb::getFrame(item->toXml()), 0, width, height);
+        item->setIcon(0, pix);
+    }
+}
 
 void ProjectList::slotReplyGetFileProperties(int clipId, const QMap < QString, QString > &properties, const QMap < QString, QString > &metadata) {
     ProjectItem *item = getItemById(clipId);
     if (item) {
         item->setProperties(properties, metadata);
+        listView->setCurrentItem(item);
         emit receivedClipDuration(clipId, item->clipMaxDuration());
     }
 }
 
-
-
 void ProjectList::slotReplyGetImage(int clipId, int pos, const QPixmap &pix, int w, int h) {
     ProjectItem *item = getItemById(clipId);
     if (item) item->setIcon(0, pix);
@@ -392,7 +543,8 @@ ProjectItem *ProjectList::getItemById(int id) {
             break;
         ++it;
     }
-    return ((ProjectItem *)(*it));
+    if (*it) return ((ProjectItem *)(*it));
+    return NULL;
 }
 
 
@@ -408,7 +560,7 @@ void ProjectList::addProducer(QDomElement producer, int parentId) {
 
     //kDebug()<<"//////  ADDING PRODUCER:\n "<<doc.toString()<<"\n+++++++++++++++++";
     int id = producer.attribute("id").toInt();
-    QString groupName = producer.attribute("group");
+    QString groupName = producer.attribute("groupname");
     if (id >= m_clipIdCounter) m_clipIdCounter = id + 1;
     else if (id == 0) id = m_clipIdCounter++;