]> git.sesse.net Git - kdenlive/blobdiff - src/projectlist.cpp
add context menu
[kdenlive] / src / projectlist.cpp
index baeafea651e3364819168bdfca1b7a45473c2983..9bec68fbdb39bbb28558d567e11671bf88edb6f7 100644 (file)
@@ -1,3 +1,22 @@
+/***************************************************************************
+ *   Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ *   This program is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+ *   GNU General Public License for more details.                          *
+ *                                                                         *
+ *   You should have received a copy of the GNU General Public License     *
+ *   along with this program; if not, write to the                         *
+ *   Free Software Foundation, Inc.,                                       *
+ *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
+ ***************************************************************************/
+
 
 #include <QMouseEvent>
 #include <QStylePainter>
@@ -9,6 +28,8 @@
 #include <KAction>
 #include <KLocale>
 #include <KFileDialog>
+#include <nepomuk/resourcemanager.h>
+#include <kio/netaccess.h>
 
 #include "projectlist.h"
 #include "projectitem.h"
@@ -67,12 +88,12 @@ void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIn
 };
 
 
-ProjectList::ProjectList(KUndoStack *commandStack, QWidget *parent)
-    : QWidget(parent), m_render(NULL), m_fps(-1), m_commandStack(commandStack)
+ProjectList::ProjectList(QWidget *parent)
+    : QWidget(parent), m_render(NULL), m_fps(-1), m_commandStack(NULL)
 {
 
   QWidget *vbox = new QWidget;
-  listView = new QTreeWidget(this);;
+  listView = new ProjectListView(this);;
   QVBoxLayout *layout = new QVBoxLayout;
   m_clipIdCounter = 0;
 
@@ -87,19 +108,19 @@ ProjectList::ProjectList(KUndoStack *commandStack, QWidget *parent)
   addButton->setPopupMode(QToolButton::MenuButtonPopup);
   m_toolbar->addWidget (addButton);
   
-  QAction *addClip = addMenu->addAction (KIcon("document-new"), i18n("Add Clip"));
-  connect(addClip, SIGNAL(triggered()), this, SLOT(slotAddClip()));
+  QAction *addClipButton = addMenu->addAction (KIcon("document-new"), i18n("Add Clip"));
+  connect(addClipButton, SIGNAL(triggered()), this, SLOT(slotAddClip()));
 
   QAction *addColorClip = addMenu->addAction (KIcon("document-new"), i18n("Add Color Clip"));
   connect(addColorClip, SIGNAL(triggered()), this, SLOT(slotAddColorClip()));
 
-  QAction *deleteClip = m_toolbar->addAction (KIcon("edit-delete"), i18n("Delete Clip"));
-  connect(deleteClip, SIGNAL(triggered()), this, SLOT(slotRemoveClip()));
+  m_deleteAction = m_toolbar->addAction (KIcon("edit-delete"), i18n("Delete Clip"));
+  connect(m_deleteAction, SIGNAL(triggered()), this, SLOT(slotRemoveClip()));
 
-  QAction *editClip = m_toolbar->addAction (KIcon("document-properties"), i18n("Edit Clip"));
-  connect(editClip, SIGNAL(triggered()), this, SLOT(slotEditClip()));
+  m_editAction = m_toolbar->addAction (KIcon("document-properties"), i18n("Edit Clip"));
+  connect(m_editAction, SIGNAL(triggered()), this, SLOT(slotEditClip()));
 
-  addButton->setDefaultAction( addClip );
+  addButton->setDefaultAction( addClipButton );
 
   layout->addWidget( m_toolbar );
   layout->addWidget( listView );
@@ -109,14 +130,21 @@ ProjectList::ProjectList(KUndoStack *commandStack, QWidget *parent)
   searchView->setTreeWidget(listView);
   listView->setColumnCount(3);
   listView->setDragEnabled(true);
-  listView->setDragDropMode(QAbstractItemView::DragOnly);
+  listView->setDragDropMode(QAbstractItemView::DragDrop);
   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);
+  m_menu->addAction(addColorClip);
+  m_menu->addAction(m_editAction);
+  m_menu->addAction(m_deleteAction);
+  m_menu->insertSeparator(m_deleteAction);
+
   connect(listView, SIGNAL(itemSelectionChanged()), this, SLOT(slotClipSelected()));
-  //connect(listView, SIGNAL(itemDoubleClicked ( QTreeWidgetItem *, int )), this, SLOT(slotEditClip(QTreeWidgetItem *, int)));
+  connect(listView, SIGNAL(requestMenu ( const QPoint &, QTreeWidgetItem * )), this, SLOT(slotContextMenu(const QPoint &, QTreeWidgetItem *)));
 
 
   listView->setItemDelegate(new ItemDelegate(listView));
@@ -125,6 +153,12 @@ ProjectList::ProjectList(KUndoStack *commandStack, QWidget *parent)
 
 }
 
+ProjectList::~ProjectList()
+{
+  delete m_menu;
+  delete m_toolbar;
+}
+
 void ProjectList::setRenderer(Render *projectRender)
 {
   m_render = projectRender;
@@ -133,16 +167,35 @@ void ProjectList::setRenderer(Render *projectRender)
 void ProjectList::slotClipSelected()
 {
   ProjectItem *item = (ProjectItem*) listView->currentItem();
-  if (item) emit clipSelected(item->toXml());
+  if (item && !item->isGroup()) emit clipSelected(item->toXml());
 }
 
 void ProjectList::slotEditClip()
 {
+  kDebug()<<"////////////////////////////////////////   DBL CLK";
+}
 
+
+void ProjectList::slotEditClip(QTreeWidgetItem *item, int column)
+{
+  kDebug()<<"////////////////////////////////////////   DBL CLK";
+}
+
+void ProjectList::slotContextMenu( const QPoint &pos, QTreeWidgetItem *item )
+{
+  bool enable = false;
+  if (item) {
+    enable = true;
+  }
+  m_editAction->setEnabled(enable);
+  m_deleteAction->setEnabled(enable);
+
+  m_menu->popup(pos);
 }
 
 void ProjectList::slotRemoveClip()
 {
+  if (!m_commandStack) kDebug()<<"!!!!!!!!!!!!!!!!  NO CMD STK";
   if (!listView->currentItem()) return;
   ProjectItem *item = ((ProjectItem *)listView->currentItem());
   AddClipCommand *command = new AddClipCommand(this, item->names(), item->toXml(), item->clipId(), KUrl(item->data(1, FullPathRole).toString()), false);
@@ -150,35 +203,85 @@ void ProjectList::slotRemoveClip()
 
 }
 
-void ProjectList::addClip(const QStringList &name, const QDomElement &elem, const int clipId, const KUrl &url)
+void ProjectList::selectItemById(const int clipId)
 {
-  ProjectItem *item = new ProjectItem(listView, name, elem, clipId);
+  ProjectItem *item = getItemById(clipId);
+  if (item) listView->setCurrentItem(item);
+}
+
+void ProjectList::addClip(const QStringList &name, const QDomElement &elem, const int clipId, const KUrl &url, int parentId)
+{
+  kDebug()<<"/////////  ADDING VCLIP=: "<<name;
+  ProjectItem *item;
+  ProjectItem *groupItem = NULL;
+  QString group = elem.attribute("group", QString::null);
+  if (parentId != -1) {
+    groupItem = getItemById(parentId);
+  }
+  else if (!group.isEmpty()) {
+    // Clip is in a group
+    QList<QTreeWidgetItem *> groupList = listView->findItems(group, Qt::MatchExactly, 1);
+
+    if (groupList.isEmpty())  {
+       QStringList groupName;
+       groupName<<QString::null<<group;
+       kDebug()<<"-------  CREATING NEW GRP: "<<groupName;
+       groupItem = new ProjectItem(listView, groupName);
+    }
+    else groupItem = (ProjectItem *) groupList.first();
+  }
+  if (groupItem) item = new ProjectItem(groupItem, name, elem, clipId);
+  else item = new ProjectItem(listView, name, elem, clipId);
   if (!url.isEmpty()) {
     item->setData(1, FullPathRole, url.path());
-    emit getFileProperties(url, elem.attribute("frame_thumbnail", 0).toInt());
+/*    Nepomuk::File f( url.path() );
+    QString annotation = f.getAnnotation();
+    if (!annotation.isEmpty()) item->setText(2, annotation);*/
+    QString resource = url.path();
+    if (resource.endsWith("westley") || resource.endsWith("kdenlive")) {
+       QString tmpfile;
+       QDomDocument doc;
+       if (KIO::NetAccess::download(url, tmpfile, 0)) {
+           QFile file(tmpfile);
+           if (file.open(QIODevice::ReadOnly)) {
+             doc.setContent(&file, false);
+             file.close();
+           }
+           KIO::NetAccess::removeTempFile(tmpfile);
+
+           QDomNodeList subProds = doc.elementsByTagName("producer");
+           int ct = subProds.count();
+           for (int i = 0; i <  ct ; i++)
+           {
+             QDomElement e = subProds.item(i).toElement();
+             if (!e.isNull()) {
+               addProducer(e, clipId);
+             }
+           }  
+         }
+    }
+
+  }
+
+  if (elem.isNull() ) {
+    QDomDocument doc;
+    QDomElement element = doc.createElement("producer");
+    element.setAttribute("resource", url.path());
+    emit getFileProperties(element, clipId);
   }
+  else emit getFileProperties(elem, clipId);
+  selectItemById(clipId);
 }
 
 void ProjectList::deleteClip(const int clipId)
 {
-  QTreeWidgetItem *parent = 0;
-  int count =
-    parent ? parent->childCount() : listView->topLevelItemCount();
-
-  for (int i = 0; i < count; i++)
-  {
-    QTreeWidgetItem *item =
-      parent ? parent->child(i) : listView->topLevelItem(i);
-    if (((ProjectItem *)item)->clipId() == clipId) {
-      delete item;
-      break;
-    }
-  }
+  ProjectItem *item = getItemById(clipId);
+  if (item) delete item;
 }
 
 void ProjectList::slotAddClip()
 {
-   
+  if (!m_commandStack) kDebug()<<"!!!!!!!!!!!!!!!!  NO CMD STK";
   KUrl::List 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");
   if (list.isEmpty()) return;
   KUrl::List::Iterator it;
@@ -199,6 +302,7 @@ void ProjectList::slotAddClip()
 
 void ProjectList::slotAddColorClip()
 {
+  if (!m_commandStack) kDebug()<<"!!!!!!!!!!!!!!!!  NO CMD STK";
   QDialog *dia = new QDialog;
   Ui::ColorClip_UI *dia_ui = new Ui::ColorClip_UI();
   dia_ui->setupUi(dia);
@@ -221,7 +325,7 @@ void ProjectList::slotAddColorClip()
     itemEntry.append(dia_ui->clip_name->text());
     AddClipCommand *command = new AddClipCommand(this, itemEntry, element, m_clipIdCounter++, KUrl(), true);
     m_commandStack->push(command);
-    //ProjectItem *item = new ProjectItem(listView, itemEntry, element);
+    // ProjectItem *item = new ProjectItem(listView, itemEntry, element, m_clipIdCounter++);
     /*QPixmap pix(60, 40);
     pix.fill(dia_ui->clip_color->color());
     item->setIcon(0, QIcon(pix));*/
@@ -230,26 +334,22 @@ void ProjectList::slotAddColorClip()
   }
   delete dia_ui;
   delete dia;
-  /*for (it = list.begin(); it != list.end(); it++) {
-      QStringList itemEntry;
-      itemEntry.append(QString::null);
-      itemEntry.append((*it).fileName());
-      ProjectItem *item = new ProjectItem(listView, itemEntry, QDomElement());
-      item->setData(1, FullPathRole, (*it).path());
-      emit getFileProperties((*it), 0);
-  }*/
 }
 
 void ProjectList::setDocument(KdenliveDoc *doc)
 {
   m_fps = doc->fps();
   m_timecode = doc->timecode();
-
+  m_commandStack = doc->commandStack();
   QDomNodeList prods = doc->producersList();
+  int ct = prods.count();
+  kDebug()<<"////////////  SETTING DOC, FOUND CLIPS: "<<prods.count();
   listView->clear();
-  for (int i = 0; i <  prods.count () ; i++)
+  for (int i = 0; i <  ct ; i++)
   {
-    addProducer(prods.item(i).toElement());
+    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);
@@ -261,73 +361,70 @@ QDomElement ProjectList::producersList()
   QDomDocument doc;
   QDomElement prods = doc.createElement("producerlist");
   doc.appendChild(prods);
-  QTreeWidgetItem *parent = 0;
-  int count =
-    parent ? parent->childCount() : listView->topLevelItemCount();
-
-  for (int i = 0; i < count; i++)
-  {
-    QTreeWidgetItem *item =
-      parent ? parent->child(i) : listView->topLevelItem(i);
-    prods.appendChild(doc.importNode(((ProjectItem *)item)->toXml(), true));
-  }
-  //kDebug()<<"PRODUCERS: \n"<<doc.toString();
+  kDebug()<<"////////////  PRO LIST BUILD PRDSLIST ";
+    QTreeWidgetItemIterator it(listView);
+     while (*it) {
+         if (!((ProjectItem *)(*it))->isGroup())
+         prods.appendChild(doc.importNode(((ProjectItem *)(*it))->toXml(), true));
+         ++it;
+     }
   return prods;
 }
 
-void ProjectList::slotReplyGetFileProperties(const QMap < QString, QString > &properties, const QMap < QString, QString > &metadata)
-{
-  QTreeWidgetItem *parent = 0;
-  int count =
-    parent ? parent->childCount() : listView->topLevelItemCount();
 
-  for (int i = 0; i < count; i++)
-  {
-    QTreeWidgetItem *item =
-      parent ? parent->child(i) : listView->topLevelItem(i);
-
-    if (item->data(1, FullPathRole).toString() == properties["filename"]) {
-      ((ProjectItem *) item)->setProperties(properties, metadata);
-      break;
-    }
-  }
+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);
 }
 
 
-void ProjectList::slotReplyGetImage(const KUrl &url, int pos, const QPixmap &pix, int w, int h)
-{
-  QTreeWidgetItem *parent = 0;
-  int count =
-    parent ? parent->childCount() : listView->topLevelItemCount();
 
-  for (int i = 0; i < count; i++)
-  {
-    QTreeWidgetItem *item =
-      parent ? parent->child(i) : listView->topLevelItem(i);
-
-    if (item->data(1, FullPathRole).toString() == url.path()) {
-      item->setIcon(0, pix);
-      break;
-    }
-  }
+void ProjectList::slotReplyGetImage(int clipId, int pos, const QPixmap &pix, int w, int h)
+{
+  ProjectItem *item = getItemById(clipId);
+  if (item) item->setIcon(0, pix);
+}
 
+ProjectItem *ProjectList::getItemById(int id)
+{
+    QTreeWidgetItemIterator it(listView);
+     while (*it) {
+         if (((ProjectItem *)(*it))->clipId() == id)
+         break;
+         ++it;
+     }
+  return ((ProjectItem *)(*it));
 }
 
 
-void ProjectList::addProducer(QDomElement producer)
+void ProjectList::addProducer(QDomElement producer, int parentId)
 {
+  if (!m_commandStack) kDebug()<<"!!!!!!!!!!!!!!!!  NO CMD STK";
   DocClipBase::CLIPTYPE type = (DocClipBase::CLIPTYPE) producer.attribute("type").toInt();
-  
-  if (type == DocClipBase::AUDIO || type == DocClipBase::VIDEO || type == DocClipBase::AV)
+
+    /*QDomDocument doc;
+    QDomElement prods = doc.createElement("list");
+    doc.appendChild(prods);
+    prods.appendChild(doc.importNode(producer, true));*/
+    
+
+  //kDebug()<<"//////  ADDING PRODUCER:\n "<<doc.toString()<<"\n+++++++++++++++++";
+  int id = producer.attribute("id").toInt();
+  if (id >= m_clipIdCounter) m_clipIdCounter = id + 1;
+  else if (id == 0) id = m_clipIdCounter++;
+
+  if (type == DocClipBase::AUDIO || type == DocClipBase::VIDEO || type == DocClipBase::AV || type == DocClipBase::IMAGE  || type == DocClipBase::PLAYLIST)
   {
     KUrl resource = KUrl(producer.attribute("resource"));
     if (!resource.isEmpty()) {
       QStringList itemEntry;
       itemEntry.append(QString::null);
       itemEntry.append(resource.fileName());
+      addClip(itemEntry, producer, id, resource, parentId);
+      /*AddClipCommand *command = new AddClipCommand(this, itemEntry, producer, id, resource, true);
+      m_commandStack->push(command);*/
 
-      AddClipCommand *command = new AddClipCommand(this, itemEntry, producer, producer.attribute("id").toInt(), resource, true);
-      m_commandStack->push(command);
 
       /*ProjectItem *item = new ProjectItem(listView, itemEntry, producer);
       item->setData(1, FullPathRole, resource.path());
@@ -342,10 +439,10 @@ void ProjectList::addProducer(QDomElement producer)
     pix.fill(QColor(colour.left(7)));
     QStringList itemEntry;
     itemEntry.append(QString::null);
-    itemEntry.append(producer.attribute("name"));
-
-    AddClipCommand *command = new AddClipCommand(this, itemEntry, producer, producer.attribute("id").toInt(), KUrl(), true);
-    m_commandStack->push(command);
+    itemEntry.append(producer.attribute("name", i18n("Color clip")));
+    addClip(itemEntry, producer, id, KUrl(), parentId);
+    /*AddClipCommand *command = new AddClipCommand(this, itemEntry, producer, id, KUrl(), true);
+    m_commandStack->push(command);*/
     //ProjectItem *item = new ProjectItem(listView, itemEntry, producer);
     /*item->setIcon(0, QIcon(pix));
     item->setData(1, ClipTypeRole, (int) type);*/