]> git.sesse.net Git - kdenlive/commitdiff
Fix drag & drop of effects:
authorJean-Baptiste Mardelle <jb@kdenlive.org>
Thu, 12 Mar 2009 21:08:08 +0000 (21:08 +0000)
committerJean-Baptiste Mardelle <jb@kdenlive.org>
Thu, 12 Mar 2009 21:08:08 +0000 (21:08 +0000)
http://www.kdenlive.org:80/mantis/view.php?id=715

svn path=/trunk/kdenlive/; revision=3142

src/abstractgroupitem.cpp
src/abstractgroupitem.h
src/clipitem.cpp
src/customtrackview.cpp
src/customtrackview.h

index 136745daeea515cda61586bd34719d2a3d7bf0c3..897ed0c0e023e21f8fc9faa26baa600a04d790d9 100644 (file)
 #include "abstractclipitem.h"
 #include "kdenlivesettings.h"
 #include "customtrackscene.h"
+#include "customtrackview.h"
 
 #include <KDebug>
 
 #include <QPainter>
 #include <QStyleOptionGraphicsItem>
+#include <QDomDocument>
+#include <QMimeData>
 
 
 AbstractGroupItem::AbstractGroupItem(double fps): QGraphicsItemGroup(), m_fps(fps) {
     setZValue(2);
     setFlags(QGraphicsItem::ItemClipsToShape | QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
+    setAcceptDrops(true);
 }
 
 int AbstractGroupItem::type() const {
@@ -175,3 +179,21 @@ QVariant AbstractGroupItem::itemChange(GraphicsItemChange change, const QVariant
     return QGraphicsItemGroup::itemChange(change, value);
 }
 
+//virtual
+void AbstractGroupItem::dropEvent(QGraphicsSceneDragDropEvent * event) {
+    QString effects = QString(event->mimeData()->data("kdenlive/effectslist"));
+    QDomDocument doc;
+    doc.setContent(effects, true);
+    QDomElement e = doc.documentElement();
+    CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
+    if (view) view->slotAddGroupEffect(e, this);
+}
+
+//virtual
+void AbstractGroupItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event) {
+    event->setAccepted(event->mimeData()->hasFormat("kdenlive/effectslist"));
+}
+
+void AbstractGroupItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event) {
+    Q_UNUSED(event);
+}
index 315563c5e11ddd9222290a8e5cd38aa7a8e20491..0513ec503e3068d0e6864098df42d54502b215b5 100644 (file)
 #ifndef ABSTRACTGROUPITEM
 #define ABSTRACTGROUPITEM
 
-#include <QGraphicsItemGroup>
 #include "definitions.h"
 #include "gentime.h"
 
+#include <QGraphicsItemGroup>
+#include <QGraphicsSceneMouseEvent>
+
 class CustomTrackScene;
 
 class AbstractGroupItem : public QObject , public QGraphicsItemGroup {
@@ -39,6 +41,9 @@ public:
 protected:
     virtual QVariant itemChange(GraphicsItemChange change, const QVariant &value);
     virtual void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *);
+    virtual void dragEnterEvent(QGraphicsSceneDragDropEvent *event);
+    virtual void dragLeaveEvent(QGraphicsSceneDragDropEvent *event);
+    virtual void dropEvent(QGraphicsSceneDragDropEvent *event);
 
 private:
     QPainterPath groupShape(QPointF);
index 529a23aff783c81bba92b65c5c63b25d3f66e205..af18b89125b6854dd330d52c1d7a84021a3cc619 100644 (file)
@@ -1379,6 +1379,7 @@ void ClipItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event) {
 void ClipItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event) {
     Q_UNUSED(event);
 }
+
 void ClipItem::addTransition(Transition* t) {
     m_transitionsList.append(t);
     //CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
index 3397058b3b9bbcbbcfb92473d964adea2e1c7e94..720b88410ef63e95866daa62f5f7562481759d09 100644 (file)
@@ -79,7 +79,7 @@
 // const int duration = animate ? 1500 : 1;
 
 CustomTrackView::CustomTrackView(KdenliveDoc *doc, CustomTrackScene* projectscene, QWidget *parent)
-        : QGraphicsView(projectscene, parent), m_scene(projectscene), m_cursorPos(0), m_cursorLine(NULL), m_operationMode(NONE), m_dragItem(NULL), m_visualTip(NULL), m_moveOpMode(NONE), m_animation(NULL), m_projectDuration(0), m_clickPoint(QPoint()), m_document(doc), m_autoScroll(KdenliveSettings::autoscroll()), m_tracksHeight(KdenliveSettings::trackheight()), m_tool(SELECTTOOL), m_dragGuide(NULL), m_findIndex(0), m_menuPosition(QPoint()), m_blockRefresh(false), m_selectionGroup(NULL), m_selectedTrack(0), m_copiedItems(QList<AbstractClipItem *> ()), m_scrollOffset(0), m_changeSpeedAction(NULL), m_pasteEffectsAction(NULL) {
+        : QGraphicsView(projectscene, parent), m_scene(projectscene), m_cursorPos(0), m_cursorLine(NULL), m_operationMode(NONE), m_dragItem(NULL), m_visualTip(NULL), m_moveOpMode(NONE), m_animation(NULL), m_projectDuration(0), m_clickPoint(QPoint()), m_document(doc), m_autoScroll(KdenliveSettings::autoscroll()), m_tracksHeight(KdenliveSettings::trackheight()), m_tool(SELECTTOOL), m_dragGuide(NULL), m_findIndex(0), m_menuPosition(QPoint()), m_blockRefresh(false), m_selectionGroup(NULL), m_selectedTrack(0), m_copiedItems(QList<AbstractClipItem *> ()), m_scrollOffset(0), m_changeSpeedAction(NULL), m_pasteEffectsAction(NULL), m_clipDrag(false) {
     if (doc) m_commandStack = doc->commandStack();
     else m_commandStack = NULL;
     setMouseTracking(true);
@@ -955,6 +955,7 @@ void CustomTrackView::activateMonitor() {
 
 void CustomTrackView::dragEnterEvent(QDragEnterEvent * event) {
     if (event->mimeData()->hasFormat("kdenlive/clip")) {
+        m_clipDrag = true;
         resetSelectionGroup();
         QStringList list = QString(event->mimeData()->data("kdenlive/clip")).split(';');
         m_selectionGroup = new AbstractGroupItem(m_document->fps());
@@ -977,6 +978,7 @@ void CustomTrackView::dragEnterEvent(QDragEnterEvent * event) {
         scene()->addItem(m_selectionGroup);
         event->acceptProposedAction();
     } else if (event->mimeData()->hasFormat("kdenlive/producerslist")) {
+        m_clipDrag = true;
         QStringList ids = QString(event->mimeData()->data("kdenlive/producerslist")).split(';');
         m_scene->clearSelection();
         resetSelectionGroup(false);
@@ -1003,7 +1005,11 @@ void CustomTrackView::dragEnterEvent(QDragEnterEvent * event) {
         updateSnapPoints(NULL, offsetList);
         scene()->addItem(m_selectionGroup);
         event->acceptProposedAction();
-    } else QGraphicsView::dragEnterEvent(event);
+    } else {
+        // the drag is not a clip (may be effect, ...)
+        m_clipDrag = false;
+        QGraphicsView::dragEnterEvent(event);
+    }
 }
 
 
@@ -1075,8 +1081,50 @@ void CustomTrackView::deleteEffect(int track, GenTime pos, QDomElement effect) {
     }
 }
 
+void CustomTrackView::slotAddGroupEffect(QDomElement effect, AbstractGroupItem *group) {
+    QList<QGraphicsItem *> itemList = group->childItems();
+    QUndoCommand *effectCommand = new QUndoCommand();
+    QString effectName;
+    QDomNode namenode = effect.elementsByTagName("name").item(0);
+    if (!namenode.isNull()) effectName = i18n(namenode.toElement().text().toUtf8().data());
+    else effectName = i18n("effect");
+    effectCommand->setText(i18n("Add %1", effectName));
+    int count = 0;
+    for (int i = 0; i < itemList.count(); i++) {
+        if (itemList.at(i)->type() == AVWIDGET) {
+            ClipItem *item = (ClipItem *)itemList.at(i);
+            if (item->hasEffect(effect.attribute("tag"), effect.attribute("id")) != -1 && effect.attribute("unique", "0") != "0") {
+                emit displayMessage(i18n("Effect already present in clip"), ErrorMessage);
+                continue;
+            }
+            if (item->isItemLocked()) {
+                continue;
+            }
+            item->initEffect(effect);
+            if (effect.attribute("tag") == "ladspa") {
+                QString ladpsaFile = m_document->getLadspaFile();
+                initEffects::ladspaEffectFile(ladpsaFile, effect.attribute("ladspaid").toInt(), getLadspaParams(effect));
+                effect.setAttribute("src", ladpsaFile);
+            }
+            new AddEffectCommand(this, m_document->tracksCount() - item->track(), item->startPos(), effect, true, effectCommand);
+            count++;
+        }
+    }
+    if (count > 0) {
+        m_commandStack->push(effectCommand);
+        m_document->setModified(true);
+    } else delete effectCommand;
+}
+
 void CustomTrackView::slotAddEffect(QDomElement effect, GenTime pos, int track) {
     QList<QGraphicsItem *> itemList;
+    QUndoCommand *effectCommand = new QUndoCommand();
+    QString effectName;
+    QDomNode namenode = effect.elementsByTagName("name").item(0);
+    if (!namenode.isNull()) effectName = i18n(namenode.toElement().text().toUtf8().data());
+    else effectName = i18n("effect");
+    effectCommand->setText(i18n("Add %1", effectName));
+    int count = 0;
     if (track == -1) itemList = scene()->selectedItems();
     if (itemList.isEmpty()) {
         ClipItem *clip = getClipItemAt((int)pos.frames(m_document->fps()) + 1, track);
@@ -1091,17 +1139,23 @@ void CustomTrackView::slotAddEffect(QDomElement effect, GenTime pos, int track)
                 emit displayMessage(i18n("Effect already present in clip"), ErrorMessage);
                 continue;
             }
+            if (item->isItemLocked()) {
+                continue;
+            }
             item->initEffect(effect);
             if (effect.attribute("tag") == "ladspa") {
                 QString ladpsaFile = m_document->getLadspaFile();
                 initEffects::ladspaEffectFile(ladpsaFile, effect.attribute("ladspaid").toInt(), getLadspaParams(effect));
                 effect.setAttribute("src", ladpsaFile);
             }
-            AddEffectCommand *command = new AddEffectCommand(this, m_document->tracksCount() - item->track(), item->startPos(), effect, true);
-            m_commandStack->push(command);
+            new AddEffectCommand(this, m_document->tracksCount() - item->track(), item->startPos(), effect, true, effectCommand);
+            count++;
         }
     }
-    m_document->setModified(true);
+    if (count > 0) {
+        m_commandStack->push(effectCommand);
+        m_document->setModified(true);
+    } else delete effectCommand;
 }
 
 void CustomTrackView::slotDeleteEffect(ClipItem *clip, QDomElement effect) {
@@ -1407,19 +1461,17 @@ void CustomTrackView::updateTransition(int track, GenTime pos, QDomElement oldTr
 void CustomTrackView::dragMoveEvent(QDragMoveEvent * event) {
     event->setDropAction(Qt::IgnoreAction);
     const QPointF pos = mapToScene(event->pos());
-    if (m_selectionGroup) {
+    if (m_selectionGroup && m_clipDrag) {
         m_selectionGroup->setPos(pos.x(), pos.y());
         event->setDropAction(Qt::MoveAction);
-        if (event->mimeData()->hasFormat("kdenlive/producerslist") || event->mimeData()->hasFormat("kdenlive/clip")) {
-            event->acceptProposedAction();
-        }
+        event->acceptProposedAction();
     } else {
         QGraphicsView::dragMoveEvent(event);
     }
 }
 
 void CustomTrackView::dragLeaveEvent(QDragLeaveEvent * event) {
-    if (m_selectionGroup) {
+    if (m_selectionGroup && m_clipDrag) {
         QList<QGraphicsItem *> items = m_selectionGroup->childItems();
         qDeleteAll(items);
         scene()->destroyItemGroup(m_selectionGroup);
@@ -1428,7 +1480,7 @@ void CustomTrackView::dragLeaveEvent(QDragLeaveEvent * event) {
 }
 
 void CustomTrackView::dropEvent(QDropEvent * event) {
-    if (m_selectionGroup) {
+    if (m_selectionGroup && m_clipDrag) {
         QList<QGraphicsItem *> items = m_selectionGroup->childItems();
         resetSelectionGroup();
         m_scene->clearSelection();
@@ -1818,8 +1870,7 @@ void CustomTrackView::deleteClip(const QString &clipId) {
             }
         }
     }
-    if (count > 0) m_commandStack->push(deleteCommand);
-    else delete deleteCommand;
+
 }
 
 void CustomTrackView::setCursorPos(int pos, bool seek) {
index f708867169fb1409dff5c522ddc269eeb410d9b4..9d6ef6cd95f63ce1cba321c07d1dc7ffeb859102 100644 (file)
@@ -66,6 +66,7 @@ public:
     void setScale(double scaleFactor);
     void deleteClip(const QString &clipId);
     void slotAddEffect(QDomElement effect, GenTime pos, int track);
+    void slotAddGroupEffect(QDomElement effect, AbstractGroupItem *group);
     void addEffect(int track, GenTime pos, QDomElement effect);
     void deleteEffect(int track, GenTime pos, QDomElement effect);
     void updateEffect(int track, GenTime pos, QDomElement effect, int ix, bool triggeredByUser = true);
@@ -201,6 +202,7 @@ private:
     QAction *m_pasteEffectsAction;
     QTimer m_scrollTimer;
     int m_scrollOffset;
+    bool m_clipDrag;
 
     QList <CommentedTime> m_searchStrings;
     int m_findIndex;