]> git.sesse.net Git - kdenlive/blobdiff - src/clipitem.cpp
When cutting a clip with keyframe effect, ensure that both parts of the clip get...
[kdenlive] / src / clipitem.cpp
index 107e08bd9f4a14cfe64a3331641c37bb6615755d..217467411ca2313502be670e70bd279f2b85b667 100644 (file)
@@ -41,7 +41,7 @@
 
 
 ClipItem::ClipItem(DocClipBase *clip, ItemInfo info, double fps, bool generateThumbs)
-        : AbstractClipItem(info, QRectF(), fps), m_clip(clip), m_resizeMode(NONE), m_grabPoint(0), m_maxTrack(0), m_hasThumbs(false), startThumbTimer(NULL), endThumbTimer(NULL), m_effectsCounter(1), audioThumbWasDrawn(false), m_opacity(1.0), m_timeLine(0), m_startThumbRequested(false), m_endThumbRequested(false), m_startFade(0), m_endFade(0), m_hover(false), m_selectedEffect(-1), m_speed(1.0), framePixelWidth(0), m_startPix(QPixmap()), m_endPix(QPixmap()) {
+        : AbstractClipItem(info, QRectF(), fps), m_clip(clip), m_resizeMode(NONE), m_grabPoint(0), m_maxTrack(0), m_hasThumbs(false), startThumbTimer(NULL), endThumbTimer(NULL), audioThumbWasDrawn(false), m_opacity(1.0), m_timeLine(0), m_startThumbRequested(false), m_endThumbRequested(false), m_startFade(0), m_endFade(0), m_hover(false), m_selectedEffect(-1), m_speed(1.0), framePixelWidth(0), m_startPix(QPixmap()), m_endPix(QPixmap()) {
     setRect(0, 0, (info.endPos - info.startPos).frames(fps) - 0.02, (qreal)(KdenliveSettings::trackheight() - 2));
     setPos((qreal) info.startPos.frames(fps), (qreal)(info.track * KdenliveSettings::trackheight()) + 1);
 
@@ -111,7 +111,7 @@ ClipItem *ClipItem::clone(ItemInfo info) const {
     if (info.cropStart == cropStart()) duplicate->slotSetStartThumb(m_startPix);
     if (info.cropStart + (info.endPos - info.startPos) == m_cropStart + m_cropDuration) duplicate->slotSetEndThumb(m_endPix);
     kDebug() << "// CLoning clip: " << (info.cropStart + (info.endPos - info.startPos)).frames(m_fps) << ", CURRENT end: " << (cropStart() + duration()).frames(m_fps);
-    duplicate->setEffectList(m_effectList);
+    duplicate->setEffectList(m_effectList.clone());
     duplicate->setSpeed(m_speed);
     return duplicate;
 }
@@ -150,6 +150,64 @@ void ClipItem::initEffect(QDomElement effect) {
     }
 }
 
+bool ClipItem::checkKeyFrames() {
+    bool clipEffectsModified = false;
+    for (int ix = 0; ix < m_effectList.count(); ix ++) {
+        QString kfr = keyframes(ix);
+        if (!kfr.isEmpty()) {
+            const QStringList keyframes = kfr.split(";", QString::SkipEmptyParts);
+            QStringList newKeyFrames;
+            bool cutKeyFrame = false;
+            bool modified = false;
+            int lastPos = -1;
+            double lastValue = -1;
+            int start = m_cropStart.frames(m_fps);
+            int end = (m_cropStart + m_cropDuration).frames(m_fps);
+            foreach(const QString str, keyframes) {
+                int pos = str.section(":", 0, 0).toInt();
+                double val = str.section(":", 1, 1).toDouble();
+                if (pos - start < 0) {
+                    // a keyframe is defined before the start of the clip
+                    cutKeyFrame = true;
+                } else if (cutKeyFrame) {
+                    // create new keyframe at clip start, calculate interpolated value
+                    if (pos > start) {
+                        int diff = pos - lastPos;
+                        double ratio = (double)(start - lastPos) / diff;
+                        double newValue = lastValue + (val - lastValue) * ratio;
+                        newKeyFrames.append(QString::number(start) + ":" + QString::number(newValue));
+                        modified = true;
+                    }
+                    cutKeyFrame = false;
+                }
+                if (!cutKeyFrame) {
+                    if (pos > end) {
+                        // create new keyframe at clip end, calculate interpolated value
+                        int diff = pos - lastPos;
+                        if (diff != 0) {
+                            double ratio = (double)(end - lastPos) / diff;
+                            double newValue = lastValue + (val - lastValue) * ratio;
+                            newKeyFrames.append(QString::number(end) + ":" + QString::number(newValue));
+                            modified = true;
+                        }
+                        break;
+                    } else {
+                        newKeyFrames.append(QString::number(pos) + ":" + QString::number(val));
+                    }
+                }
+                lastPos = pos;
+                lastValue = val;
+            }
+            if (modified) {
+                // update KeyFrames
+                setKeyframes(ix, newKeyFrames.join(";"));
+                clipEffectsModified = true;
+            }
+        }
+    }
+    return clipEffectsModified;
+}
+
 void ClipItem::setKeyframes(const int ix, const QString keyframes) {
     QDomElement effect = effectAt(ix);
     if (effect.attribute("disabled") == "1") return;
@@ -654,8 +712,7 @@ OPERATIONTYPE ClipItem::operationMode(QPointF pos) {
     double maximumOffset;
     if (scale > 3) maximumOffset = 25 / scale;
     else maximumOffset = 6 / scale;
-    QMatrix matrix;
-    matrix.scale(scale, 0);
+
     //kDebug()<<"// Item rect: "<<rect.x()<<". pos. "<<pos.x()<<", scale: "<<scale<<", ratio: "<<qAbs((int)(pos.x() - rect.x())) / scale;
 
     if (qAbs((int)(pos.x() - (rect.x() + m_startFade))) < maximumOffset  && qAbs((int)(pos.y() - rect.y())) < 6) {
@@ -875,13 +932,13 @@ void ClipItem::resizeStart(int posx) {
     }
 }
 
-void ClipItem::resizeEnd(int posx) {
+void ClipItem::resizeEnd(int posx, bool updateKeyFrames) {
     const int max = (startPos() - cropStart() + maxDuration()).frames(m_fps) + 1;
     if (posx > max) posx = max;
     if (posx == endPos().frames(m_fps)) return;
     const int previous = (cropStart() + duration()).frames(m_fps);
     AbstractClipItem::resizeEnd(posx);
-    checkEffectsKeyframesPos(previous, (cropStart() + duration()).frames(m_fps), false);
+    if (updateKeyFrames) checkEffectsKeyframesPos(previous, (cropStart() + duration()).frames(m_fps), false);
     if (m_hasThumbs && KdenliveSettings::videothumbnails()) {
         /*connect(m_clip->thumbProducer(), SIGNAL(thumbReady(int, QPixmap)), this, SLOT(slotThumbReady(int, QPixmap)));*/
         endThumbTimer->start(100);
@@ -899,9 +956,11 @@ void ClipItem::checkEffectsKeyframesPos(const int previous, const int current, b
                 // parse keyframes and adjust values
                 const QStringList keyframes = e.attribute("keyframes").split(";", QString::SkipEmptyParts);
                 QMap <int, double> kfr;
+                int pos;
+                double val;
                 foreach(const QString str, keyframes) {
-                    int pos = str.section(":", 0, 0).toInt();
-                    double val = str.section(":", 1, 1).toDouble();
+                    pos = str.section(":", 0, 0).toInt();
+                    val = str.section(":", 1, 1).toDouble();
                     if (pos == previous) kfr[current] = val;
                     else {
                         if (fromStart && pos >= current) kfr[pos] = val;
@@ -993,7 +1052,7 @@ QVariant ClipItem::itemChange(GraphicsItemChange change, const QVariant &value)
 }*/
 
 int ClipItem::effectsCounter() {
-    return m_effectsCounter++;
+    return effectsCount() + 1;
 }
 
 int ClipItem::effectsCount() {
@@ -1015,6 +1074,7 @@ QDomElement ClipItem::effectAt(int ix) {
 
 void ClipItem::setEffectAt(int ix, QDomElement effect) {
     kDebug() << "CHange EFFECT AT: " << ix << ", CURR: " << m_effectList.at(ix).attribute("tag") << ", NEW: " << effect.attribute("tag");
+    effect.setAttribute("kdenlive_ix", ix + 1);
     m_effectList.insert(ix, effect);
     m_effectList.removeAt(ix + 1);
     m_effectNames = m_effectList.effectNames().join(" / ");
@@ -1031,7 +1091,7 @@ QHash <QString, QString> ClipItem::addEffect(QDomElement effect, bool animate) {
     bool needRepaint = false;
     /*QDomDocument doc;
     doc.appendChild(doc.importNode(effect, true));
-    kDebug() << "///////  CLIP ADD EFFECT: "<< doc.toString();*/
+    kDebug() << "///////  CLIP ADD EFFECT: " << doc.toString();*/
     m_effectList.append(effect);
     effectParams["tag"] = effect.attribute("tag");
     QString effectId = effect.attribute("id");
@@ -1069,7 +1129,7 @@ QHash <QString, QString> ClipItem::addEffect(QDomElement effect, bool animate) {
                     else if (e.attribute("name") == "in") fade += e.attribute("value").toInt();
                 }
             } else {
-                effectParams[e.attribute("name")] =  QString::number(effectParams[e.attribute("name")].toDouble() / f);
+                effectParams[e.attribute("name")] =  QString::number(e.attribute("value").toDouble() / f);
             }
         }
     }
@@ -1134,8 +1194,10 @@ QHash <QString, QString> ClipItem::getEffectArgs(QDomElement effect) {
 
 void ClipItem::deleteEffect(QString index) {
     bool needRepaint = false;
+    QString ix;
     for (int i = 0; i < m_effectList.size(); ++i) {
-        if (m_effectList.at(i).attribute("kdenlive_ix") == index) {
+        ix = m_effectList.at(i).attribute("kdenlive_ix");
+        if (ix == index) {
             if (m_effectList.at(i).attribute("id") == "fadein") {
                 m_startFade = 0;
                 needRepaint = true;
@@ -1144,8 +1206,7 @@ void ClipItem::deleteEffect(QString index) {
                 needRepaint = true;
             }
             m_effectList.removeAt(i);
-            break;
-        }
+        } else if (ix.toInt() > index.toInt()) m_effectList[i].setAttribute("kdenlive_ix", ix.toInt() - 1);
     }
     m_effectNames = m_effectList.effectNames().join(" / ");
     if (needRepaint) update(boundingRect());