]> git.sesse.net Git - kdenlive/blobdiff - src/clipitem.cpp
Fix dropping new effect in effect stack
[kdenlive] / src / clipitem.cpp
index cac5eda2b4690f0c6f683d3588161c1a903ece7a..1d0e4c7b58a694f25f2ee6ea4ead9a2e6620306a 100644 (file)
@@ -63,6 +63,7 @@ ClipItem::ClipItem(DocClipBase *clip, ItemInfo info, double fps, double speed, i
         m_limitedKeyFrames(false)
 {
     setZValue(2);
+    m_effectList = EffectsList(true);
     FRAME_SIZE = frame_width;
     setRect(0, 0, (info.endPos - info.startPos).frames(fps) - 0.02, (double) itemHeight());
     setPos(info.startPos.frames(fps), (double)(info.track * KdenliveSettings::trackheight()) + 1 + itemOffset());
@@ -124,6 +125,8 @@ ClipItem::ClipItem(DocClipBase *clip, ItemInfo info, double fps, double speed, i
 ClipItem::~ClipItem()
 {
     blockSignals(true);
+    m_endThumbTimer.stop();
+    m_startThumbTimer.stop();
     if (scene()) scene()->removeItem(this);
     if (m_clipType == VIDEO || m_clipType == AV || m_clipType == SLIDESHOW || m_clipType == PLAYLIST) {
         //disconnect(m_clip->thumbProducer(), SIGNAL(thumbReady(int, QImage)), this, SLOT(slotThumbReady(int, QImage)));
@@ -157,9 +160,10 @@ void ClipItem::setEffectList(const EffectsList effectList)
     m_effectNames = m_effectList.effectNames().join(" / ");
     if (!m_effectList.isEmpty()) {
         for (int i = 0; i < m_effectList.count(); i++) {
-            QString effectId = m_effectList.item(i).attribute("id");
+           QDomElement effect = m_effectList.itemFromIndex(i + 1);
+            QString effectId = effect.attribute("id");
             // check if it is a fade effect
-            QDomNodeList params = m_effectList.item(i).elementsByTagName("parameter");
+            QDomNodeList params = effect.elementsByTagName("parameter");
             int fade = 0;
             for (int j = 0; j < params.count(); j++) {
                 QDomElement e = params.item(j).toElement();
@@ -573,7 +577,14 @@ void ClipItem::slotFetchThumbs()
         frames.append((int)(m_speedIndependantInfo.cropStart + m_speedIndependantInfo.cropDuration).frames(m_fps) - 1);
     }
 
-    m_clip->slotExtractImage(frames);
+    if (!frames.isEmpty()) m_clip->slotExtractImage(frames);
+}
+
+void ClipItem::stopThumbs()
+{
+    // Clip is about to be deleted, make sure we don't request thumbnails
+    disconnect(&m_startThumbTimer, SIGNAL(timeout()), this, SLOT(slotGetStartThumb()));
+    disconnect(&m_endThumbTimer, SIGNAL(timeout()), this, SLOT(slotGetEndThumb()));
 }
 
 void ClipItem::slotGetStartThumb()
@@ -1352,25 +1363,41 @@ QStringList ClipItem::effectNames()
 
 QDomElement ClipItem::effectAt(int ix) const
 {
-    if (ix > m_effectList.count() - 1 || ix < 0 || m_effectList.at(ix).isNull()) return QDomElement();
-    return m_effectList.at(ix).cloneNode().toElement();
+    if (ix > m_effectList.count() || ix <= 0) return QDomElement();
+    return m_effectList.itemFromIndex(ix).cloneNode().toElement();
 }
 
 QDomElement ClipItem::getEffectAt(int ix) const
 {
-    if (ix > m_effectList.count() - 1 || ix < 0 || m_effectList.at(ix).isNull()) return QDomElement();
-    return m_effectList.at(ix);
+    if (ix > m_effectList.count() || ix < 0) return QDomElement();
+    return m_effectList.itemFromIndex(ix);
+}
+
+bool ClipItem::updateEffect(QDomElement effect)
+{
+    //kDebug() << "CHange EFFECT AT: " << ix << ", CURR: " << m_effectList.at(ix).attribute("tag") << ", NEW: " << effect.attribute("tag");
+    m_effectList.updateEffect(effect);
+    m_effectNames = m_effectList.effectNames().join(" / ");
+    QString id = effect.attribute("id");
+    if (id == "fadein" || id == "fadeout" || id == "fade_from_black" || id == "fade_to_black")
+        update();
+    else {
+        QRectF r = boundingRect();
+        r.setHeight(20);
+        update(r);
+    }
+    return true;
 }
 
-void ClipItem::setEffectAt(int ix, QDomElement effect)
+bool ClipItem::moveEffect(QDomElement effect, int ix)
 {
-    if (ix < 0 || ix > (m_effectList.count() - 1) || effect.isNull()) {
+    if (ix <= 0 || ix > (m_effectList.count()) || effect.isNull()) {
         kDebug() << "Invalid effect index: " << ix;
-        return;
+        return false;
     }
-    //kDebug() << "CHange EFFECT AT: " << ix << ", CURR: " << m_effectList.at(ix).attribute("tag") << ", NEW: " << effect.attribute("tag");
-    effect.setAttribute("kdenlive_ix", ix + 1);
-    m_effectList.replace(ix, effect);
+    m_effectList.removeAt(effect.attribute("kdenlive_ix").toInt());
+    effect.setAttribute("kdenlive_ix", ix);
+    m_effectList.insert(effect);
     m_effectNames = m_effectList.effectNames().join(" / ");
     QString id = effect.attribute("id");
     if (id == "fadein" || id == "fadeout" || id == "fade_from_black" || id == "fade_to_black")
@@ -1380,6 +1407,7 @@ void ClipItem::setEffectAt(int ix, QDomElement effect)
         r.setHeight(20);
         update(r);
     }
+    return true;
 }
 
 EffectsParameterList ClipItem::addEffect(const QDomElement effect, bool /*animate*/)
@@ -1392,11 +1420,7 @@ EffectsParameterList ClipItem::addEffect(const QDomElement effect, bool /*animat
     } else ix = effect.attribute("kdenlive_ix").toInt();
     if (!m_effectList.isEmpty() && ix <= m_effectList.count()) {
         needRepaint = true;
-        m_effectList.insert(ix - 1, effect);
-        for (int i = ix; i < m_effectList.count(); i++) {
-            int index = m_effectList.item(i).attribute("kdenlive_ix").toInt();
-            if (index >= ix) m_effectList.item(i).setAttribute("kdenlive_ix", index + 1);
-        }
+        m_effectList.insert(effect);
     } else m_effectList.append(effect);
     EffectsParameterList parameters;
     parameters.addParam("tag", effect.attribute("tag"));
@@ -1527,34 +1551,27 @@ EffectsParameterList ClipItem::addEffect(const QDomElement effect, bool /*animat
 void ClipItem::deleteEffect(QString index)
 {
     bool needRepaint = false;
-    QString ix;
-
-    for (int i = 0; i < m_effectList.count(); ++i) {
-        ix = m_effectList.at(i).attribute("kdenlive_ix");
-        if (ix == index) {
-            QString effectId = m_effectList.at(i).attribute("id");
-            if ((effectId == "fadein" && hasEffect(QString(), "fade_from_black") == -1) ||
-                    (effectId == "fade_from_black" && hasEffect(QString(), "fadein") == -1)) {
-                m_startFade = 0;
-                needRepaint = true;
-            } else if ((effectId == "fadeout" && hasEffect(QString(), "fade_to_black") == -1) ||
-                       (effectId == "fade_to_black" && hasEffect(QString(), "fadeout") == -1)) {
-                m_endFade = 0;
-                needRepaint = true;
-            } else if (EffectsList::hasKeyFrames(m_effectList.at(i))) needRepaint = true;
-            m_effectList.removeAt(i);
-            i--;
-        } else if (ix.toInt() > index.toInt()) {
-            m_effectList.item(i).setAttribute("kdenlive_ix", ix.toInt() - 1);
-        }
-    }
+    int ix = index.toInt();
+
+    QDomElement effect = m_effectList.itemFromIndex(ix);
+    QString effectId = effect.attribute("id");
+    if ((effectId == "fadein" && hasEffect(QString(), "fade_from_black") == -1) ||
+       (effectId == "fade_from_black" && hasEffect(QString(), "fadein") == -1)) {
+        m_startFade = 0;
+        needRepaint = true;
+    } else if ((effectId == "fadeout" && hasEffect(QString(), "fade_to_black") == -1) ||
+       (effectId == "fade_to_black" && hasEffect(QString(), "fadeout") == -1)) {
+        m_endFade = 0;
+        needRepaint = true;
+    } else if (EffectsList::hasKeyFrames(effect)) needRepaint = true;
+    m_effectList.removeAt(ix);
     m_effectNames = m_effectList.effectNames().join(" / ");
 
-    if (m_effectList.isEmpty() || m_selectedEffect + 1 == index.toInt()) {
+    if (m_effectList.isEmpty() || m_selectedEffect + 1 == ix) {
         // Current effect was removed
-        if (index.toInt() > m_effectList.count() - 1) {
+        if (ix > m_effectList.count() - 1) {
             setSelectedEffect(m_effectList.count() - 1);
-        } else setSelectedEffect(index.toInt());
+        } else setSelectedEffect(ix);
     }
     if (needRepaint) update(boundingRect());
     else {
@@ -1612,22 +1629,26 @@ const ItemInfo ClipItem::speedIndependantInfo() const
 //virtual
 void ClipItem::dropEvent(QGraphicsSceneDragDropEvent * event)
 {
-    const QString effects = QString::fromUtf8(event->mimeData()->data("kdenlive/effectslist"));
-    QDomDocument doc;
-    doc.setContent(effects, true);
-    const QDomElement e = doc.documentElement();
-    if (scene() && !scene()->views().isEmpty()) {
-        event->accept();
+    if (event->proposedAction() == Qt::CopyAction && scene() && !scene()->views().isEmpty()) {
+       const QString effects = QString::fromUtf8(event->mimeData()->data("kdenlive/effectslist"));
+       event->acceptProposedAction();
+       QDomDocument doc;
+       doc.setContent(effects, true);
+       QDomElement e = doc.documentElement();
+       e.setAttribute("kdenlive_ix", 0);
         CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
         if (view) view->slotAddEffect(e, m_info.startPos, track());
     }
+    else return;
 }
 
 //virtual
 void ClipItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
 {
     if (isItemLocked()) event->setAccepted(false);
-    else event->setAccepted(event->mimeData()->hasFormat("kdenlive/effectslist"));
+    else if (event->mimeData()->hasFormat("kdenlive/effectslist")) {
+       event->acceptProposedAction();
+    } else event->setAccepted(false);
 }
 
 void ClipItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)