]> git.sesse.net Git - kdenlive/blobdiff - src/customtrackview.cpp
Fix crash on clip deletion, fix issues with placeholder clips
[kdenlive] / src / customtrackview.cpp
index 80e043617143a0b6e7486dc06d06d756a1e50b27..680c7fc9cc32978c0c757461bfd64cb96f48edbc 100644 (file)
@@ -171,6 +171,7 @@ CustomTrackView::CustomTrackView(KdenliveDoc *doc, CustomTrackScene* projectscen
 CustomTrackView::~CustomTrackView()
 {
     qDeleteAll(m_guides);
+    m_guides.clear();
     m_waitingThumbs.clear();
 }
 
@@ -252,11 +253,12 @@ void CustomTrackView::checkTrackHeight()
             transitionitem->setPos((qreal) transitionitem->startPos().frames(m_document->fps()), (qreal) transitionitem->track() * m_tracksHeight + m_tracksHeight / 3 * 2);
         }
     }
-    m_cursorLine->setLine(m_cursorLine->line().x1(), 0, m_cursorLine->line().x1(), m_tracksHeight * m_document->tracksCount());
+    double newHeight = m_tracksHeight * m_document->tracksCount() * matrix().m22();
+    m_cursorLine->setLine(m_cursorLine->line().x1(), 0, m_cursorLine->line().x1(), newHeight);
 
     for (int i = 0; i < m_guides.count(); i++) {
         QLineF l = m_guides.at(i)->line();
-        l.setP2(QPointF(l.x2(), m_tracksHeight * m_document->tracksCount()));
+        l.setP2(QPointF(l.x2(), newHeight));
         m_guides.at(i)->setLine(l);
     }
 
@@ -404,6 +406,7 @@ void CustomTrackView::mouseMoveEvent(QMouseEvent * event)
             return;
         } else if (m_operationMode == SPACER && move && m_selectionGroup) {
             // spacer tool
+            snappedPos = getSnapPointForPos(mappedXPos + m_spacerOffset);
             if (snappedPos < 0) snappedPos = 0;
             // Make sure there is no collision
             QList<QGraphicsItem *> children = m_selectionGroup->childItems();
@@ -471,7 +474,7 @@ void CustomTrackView::mouseMoveEvent(QMouseEvent * event)
             // razor tool over a clip, display current frame in monitor
             if (false && !m_blockRefresh && item->type() == AVWIDGET) {
                 //TODO: solve crash when showing frame when moving razor over clip
-                emit showClipFrame(((ClipItem *) item)->baseClip(), mappedXPos - (clip->startPos() - clip->cropStart()).frames(m_document->fps()));
+                emit showClipFrame(((ClipItem *) item)->baseClip(), QPoint(), mappedXPos - (clip->startPos() - clip->cropStart()).frames(m_document->fps()));
             }
             event->accept();
             return;
@@ -668,7 +671,7 @@ void CustomTrackView::mousePressEvent(QMouseEvent * event)
 
     // special cases (middle click button or ctrl / shift click
     if (event->button() == Qt::MidButton) {
-        m_document->renderer()->switchPlay();
+        emit playMonitor();
         m_blockRefresh = false;
         m_operationMode = NONE;
         return;
@@ -867,7 +870,7 @@ void CustomTrackView::mousePressEvent(QMouseEvent * event)
                     selection.at(i)->parentItem()->setFlag(QGraphicsItem::ItemIsMovable, false);
                 }
             }
-
+            m_spacerOffset = m_selectionGroup->sceneBoundingRect().left() - (int)(mapToScene(m_clickEvent).x());
             if (!offsetList.isEmpty()) {
                 qSort(offsetList);
                 QList <GenTime> cleandOffsetList;
@@ -985,8 +988,6 @@ void CustomTrackView::mousePressEvent(QMouseEvent * event)
         return;
     } else if (m_operationMode == MOVE) {
         setCursor(Qt::ClosedHandCursor);
-        if (m_dragItem) m_dragItem->setZValue(10);
-        if (m_selectionGroup) m_selectionGroup->setZValue(10);
     } else if (m_operationMode == TRANSITIONSTART && event->modifiers() != Qt::ControlModifier) {
         ItemInfo info;
         info.startPos = m_dragItem->startPos();
@@ -1298,6 +1299,30 @@ void CustomTrackView::activateMonitor()
     emit activateDocumentMonitor();
 }
 
+void CustomTrackView::insertClipCut(DocClipBase *clip, int in, int out)
+{
+    resetSelectionGroup();
+    ItemInfo info;
+    info.startPos = GenTime();
+    info.cropStart = GenTime(in, m_document->fps());
+    info.endPos = GenTime(out - in, m_document->fps());
+    info.cropDuration = info.endPos - info.startPos;
+    info.track = 0;
+
+    // Check if clip can be inserted at that position
+    ItemInfo pasteInfo = info;
+    pasteInfo.startPos = GenTime(m_cursorPos, m_document->fps());
+    pasteInfo.endPos = pasteInfo.startPos + info.endPos;
+    pasteInfo.track = selectedTrack();
+    if (!canBePastedTo(pasteInfo, AVWIDGET)) {
+        emit displayMessage(i18n("Cannot insert clip in timeline"), ErrorMessage);
+        return;
+    }
+
+    AddTimelineClipCommand *command = new AddTimelineClipCommand(this, clip->toXML(), clip->getId(), pasteInfo, EffectsList(), m_scene->editMode() == OVERWRITEEDIT, m_scene->editMode() == INSERTEDIT, true, false);
+    m_commandStack->push(command);
+}
+
 bool CustomTrackView::insertDropClips(const QMimeData *data, const QPoint pos)
 {
     if (data->hasFormat("kdenlive/clip")) {
@@ -1337,7 +1362,7 @@ bool CustomTrackView::insertDropClips(const QMimeData *data, const QPoint pos)
         updateSnapPoints(NULL, offsetList);
         m_selectionGroup->setPos(framePos);
         scene()->addItem(m_selectionGroup);
-        m_selectionGroup->setZValue(10);
+        //m_selectionGroup->setZValue(10);
         return true;
     } else if (data->hasFormat("kdenlive/producerslist")) {
         m_clipDrag = true;
@@ -1383,16 +1408,16 @@ bool CustomTrackView::insertDropClips(const QMimeData *data, const QPoint pos)
             start += info.cropDuration;
             offsetList.append(start);
             ClipItem *item = new ClipItem(clip, info, m_document->fps(), 1.0, 1, false);
-            item->setZValue(10);
+            //item->setZValue(10);
             item->setFlag(QGraphicsItem::ItemIsMovable, false);
             m_selectionGroup->addToGroup(item);
-            m_waitingThumbs.append(item);
+            if (!clip->isPlaceHolder()) m_waitingThumbs.append(item);
         }
 
         updateSnapPoints(NULL, offsetList);
         m_selectionGroup->setPos(framePos);
         scene()->addItem(m_selectionGroup);
-        m_selectionGroup->setZValue(10);
+        //m_selectionGroup->setZValue(10);
         m_thumbsTimer.start();
         return true;
 
@@ -1726,7 +1751,8 @@ ClipItem *CustomTrackView::cutClip(ItemInfo info, GenTime cutTime, bool cut, boo
         ClipItem *item = getClipItemAt((int) info.startPos.frames(m_document->fps()), info.track);
         if (!item || cutTime >= item->endPos() || cutTime <= item->startPos()) {
             emit displayMessage(i18n("Cannot find clip to cut"), ErrorMessage);
-            kDebug() << "/////////  ERROR CUTTING CLIP : (" << item->startPos().frames(25) << "-" << item->endPos().frames(25) << "), INFO: (" << info.startPos.frames(25) << "-" << info.endPos.frames(25) << ")" << ", CUT: " << cutTime.frames(25);
+            if (item) kDebug() << "/////////  ERROR CUTTING CLIP : (" << item->startPos().frames(25) << "-" << item->endPos().frames(25) << "), INFO: (" << info.startPos.frames(25) << "-" << info.endPos.frames(25) << ")" << ", CUT: " << cutTime.frames(25);
+            else kDebug() << "/// ERROR NO CLIP at: " << info.startPos.frames(m_document->fps()) << ", track: " << info.track;
             m_blockRefresh = false;
             return NULL;
         }
@@ -2024,6 +2050,7 @@ void CustomTrackView::dropEvent(QDropEvent * event)
         bool hasVideoClip = false;
         QUndoCommand *addCommand = new QUndoCommand();
         addCommand->setText(i18n("Add timeline clip"));
+        QList <ClipItem *> brokenClips;
 
         for (int i = 0; i < items.count(); i++) {
             ClipItem *item = static_cast <ClipItem *>(items.at(i));
@@ -2036,7 +2063,7 @@ void CustomTrackView::dropEvent(QDropEvent * event)
 
             //TODO: take care of edit mode for undo
             item->baseClip()->addReference();
-            item->setZValue(item->defaultZValue());
+            //item->setZValue(item->defaultZValue());
             m_document->updateClip(item->baseClip()->getId());
             ItemInfo info = item->info();
 
@@ -2047,6 +2074,8 @@ void CustomTrackView::dropEvent(QDropEvent * event)
             clipInfo.track = m_document->tracksCount() - item->track();
             if (m_document->renderer()->mltInsertClip(clipInfo, item->xml(), item->baseClip()->producer(item->track()), m_scene->editMode() == OVERWRITEEDIT, m_scene->editMode() == INSERTEDIT) == -1) {
                 emit displayMessage(i18n("Cannot insert clip in timeline"), ErrorMessage);
+                brokenClips.append(item);
+                continue;
             }
             adjustTimelineClips(m_scene->editMode(), item, ItemInfo(), addCommand);
 
@@ -2059,7 +2088,10 @@ void CustomTrackView::dropEvent(QDropEvent * event)
             }
             item->setSelected(true);
         }
-        m_commandStack->push(addCommand);
+        qDeleteAll(brokenClips);
+        brokenClips.clear();
+        if (addCommand->childCount() > 0) m_commandStack->push(addCommand);
+        else delete addCommand;
         setDocumentModified();
 
         /*
@@ -2127,7 +2159,7 @@ void CustomTrackView::adjustTimelineClips(EDITMODE mode, ClipItem *item, ItemInf
                         new ResizeClipCommand(this, clip->info(), newclipInfo, false, false, command);
                         clip->resizeEnd(info.startPos.frames(m_document->fps()));
                     }
-                } else if (clip->endPos() < info.endPos) {
+                } else if (clip->endPos() <= info.endPos) {
                     new AddTimelineClipCommand(this, clip->xml(), clip->clipProducer(), clip->info(), clip->effectList(), false, false, false, true, command);
                     scene()->removeItem(clip);
                     delete clip;
@@ -2245,7 +2277,7 @@ int CustomTrackView::duration() const
 void CustomTrackView::addTrack(TrackInfo type, int ix)
 {
     if (ix == -1 || ix == m_document->tracksCount()) {
-        m_document->insertTrack(ix, type);
+        m_document->insertTrack(0, type);
         m_document->renderer()->mltInsertTrack(1, type.type == VIDEOTRACK);
     } else {
         m_document->insertTrack(m_document->tracksCount() - ix, type);
@@ -2281,6 +2313,8 @@ void CustomTrackView::addTrack(TrackInfo type, int ix)
             ItemInfo clipinfo = item->info();
             if (item->type() == AVWIDGET) {
                 ClipItem *clip = static_cast <ClipItem *>(item);
+                // slowmotion clips are not track dependant, so no need to update them
+                if (clip->speed() != 1.0) continue;
                 // We add a move clip command so that we get the correct producer for new track number
                 if (clip->clipType() == AV || clip->clipType() == AUDIO) {
                     Mlt::Producer *prod;
@@ -2303,14 +2337,14 @@ void CustomTrackView::addTrack(TrackInfo type, int ix)
         resetSelectionGroup(false);
     }
 
-    int maxHeight = m_tracksHeight * m_document->tracksCount();
+    int maxHeight = m_tracksHeight * m_document->tracksCount() * matrix().m22();
     for (int i = 0; i < m_guides.count(); i++) {
         QLineF l = m_guides.at(i)->line();
         l.setP2(QPointF(l.x2(), maxHeight));
         m_guides.at(i)->setLine(l);
     }
     m_cursorLine->setLine(m_cursorLine->line().x1(), 0, m_cursorLine->line().x1(), maxHeight);
-    setSceneRect(0, 0, sceneRect().width(), maxHeight);
+    setSceneRect(0, 0, sceneRect().width(), m_tracksHeight * m_document->tracksCount());
     viewport()->update();
     emit tracksChanged();
     //QTimer::singleShot(500, this, SIGNAL(trackHeightChanged()));
@@ -2376,14 +2410,14 @@ void CustomTrackView::removeTrack(int ix)
     }
     resetSelectionGroup(false);
 
-    int maxHeight = m_tracksHeight * m_document->tracksCount();
+    int maxHeight = m_tracksHeight * m_document->tracksCount() * matrix().m22();
     for (int i = 0; i < m_guides.count(); i++) {
         QLineF l = m_guides.at(i)->line();
         l.setP2(QPointF(l.x2(), maxHeight));
         m_guides.at(i)->setLine(l);
     }
     m_cursorLine->setLine(m_cursorLine->line().x1(), 0, m_cursorLine->line().x1(), maxHeight);
-    setSceneRect(0, 0, sceneRect().width(), maxHeight);
+    setSceneRect(0, 0, sceneRect().width(), m_tracksHeight * m_document->tracksCount());
 
     m_selectedTrack = qMin(m_selectedTrack, m_document->tracksCount() - 1);
     viewport()->update();
@@ -2489,7 +2523,7 @@ void CustomTrackView::slotRemoveSpace()
     int length = m_document->renderer()->mltGetSpaceLength(pos, m_document->tracksCount() - track, true);
     //kDebug() << "// GOT LENGT; " << length;
     if (length <= 0) {
-        emit displayMessage(i18n("You must be in an empty space to remove space (time: %1, track:%2)", m_document->timecode().getTimecodeFromFrames(mapToScene(m_menuPosition).x()), track), ErrorMessage);
+        emit displayMessage(i18n("You must be in an empty space to remove space (time: %1, track: %2)", m_document->timecode().getTimecodeFromFrames(mapToScene(m_menuPosition).x()), track), ErrorMessage);
         return;
     }
 
@@ -2803,8 +2837,6 @@ void CustomTrackView::mouseReleaseEvent(QMouseEvent * event)
 
     if (m_operationMode == MOVE) {
         setCursor(Qt::OpenHandCursor);
-        if (m_dragItem) m_dragItem->setZValue(m_dragItem->defaultZValue());
-        if (m_selectionGroup) m_selectionGroup->setZValue(1);
         if (m_dragItem->parentItem() == 0) {
             // we are moving one clip, easy
             if (m_dragItem->type() == AVWIDGET && (m_dragItemInfo.startPos != info.startPos || m_dragItemInfo.track != info.track)) {
@@ -3499,7 +3531,10 @@ void CustomTrackView::doChangeClipSpeed(ItemInfo info, ItemInfo speedIndependant
         return;
     }
     info.track = m_document->tracksCount() - item->track();
-    int endPos = m_document->renderer()->mltChangeClipSpeed(info, speedIndependantInfo, speed, oldspeed, strobe, baseclip->producer());
+    int endPos;
+    if (item->isVideoOnly()) endPos = m_document->renderer()->mltChangeClipSpeed(info, speedIndependantInfo, speed, oldspeed, strobe, baseclip->videoProducer());
+    else if (item->isAudioOnly()) endPos = m_document->renderer()->mltChangeClipSpeed(info, speedIndependantInfo, speed, oldspeed, strobe, baseclip->audioProducer(item->track()));
+    else endPos = m_document->renderer()->mltChangeClipSpeed(info, speedIndependantInfo, speed, oldspeed, strobe, baseclip->producer());
     if (endPos >= 0) {
         item->setSpeed(speed, strobe);
         item->updateRectGeometry();
@@ -3635,7 +3670,7 @@ void CustomTrackView::addClip(QDomElement xml, const QString &clipId, ItemInfo i
     }
     setDocumentModified();
     if (refresh) m_document->renderer()->doRefresh();
-    m_waitingThumbs.append(item);
+    if (!baseclip->isPlaceHolder()) m_waitingThumbs.append(item);
     m_thumbsTimer.start();
 }
 
@@ -4312,7 +4347,7 @@ bool CustomTrackView::addGuide(const GenTime pos, const QString &comment)
             return false;
         }
     }
-    Guide *g = new Guide(this, pos, comment, m_tracksHeight * m_document->tracksCount());
+    Guide *g = new Guide(this, pos, comment, m_tracksHeight * m_document->tracksCount() * matrix().m22());
     scene()->addItem(g);
     m_guides.append(g);
     qSort(m_guides.begin(), m_guides.end(), sortGuidesList);
@@ -4411,8 +4446,8 @@ void CustomTrackView::setTool(PROJECTTOOL tool)
 
 void CustomTrackView::setScale(double scaleFactor, double verticalScale)
 {
-    QMatrix matrix;
-    matrix = matrix.scale(scaleFactor, verticalScale);
+    QMatrix newmatrix;
+    newmatrix = newmatrix.scale(scaleFactor, verticalScale);
     m_scene->setScale(scaleFactor, verticalScale);
     if (m_visualTip) {
         scene()->removeItem(m_visualTip);
@@ -4423,11 +4458,23 @@ void CustomTrackView::setScale(double scaleFactor, double verticalScale)
         m_visualTip = NULL;
     }
     double verticalPos = mapToScene(QPoint(0, viewport()->height() / 2)).y();
-    setMatrix(matrix);
-    m_cursorLine->setLine(m_cursorLine->line().x1(), 0, m_cursorLine->line().x1(), m_tracksHeight * m_document->tracksCount() * verticalScale);
+    bool adjust = false;
+    if (verticalScale != matrix().m22()) adjust = true;
+    setMatrix(newmatrix);
+    if (adjust) {
+        double newHeight = m_tracksHeight * m_document->tracksCount() * matrix().m22();
+        m_cursorLine->setLine(m_cursorLine->line().x1(), 0, m_cursorLine->line().x1(), newHeight);
+        for (int i = 0; i < m_guides.count(); i++) {
+            QLineF l = m_guides.at(i)->line();
+            l.setP2(QPointF(l.x2(), newHeight));
+            m_guides.at(i)->setLine(l);
+        }
+        setSceneRect(0, 0, sceneRect().width(), m_tracksHeight * m_document->tracksCount());
+    }
+
     int diff = sceneRect().width() - m_projectDuration;
-    if (diff * matrix.m11() < 50) {
-        if (matrix.m11() < 0.4) setSceneRect(0, 0, (m_projectDuration + 100 / matrix.m11()), sceneRect().height());
+    if (diff * newmatrix.m11() < 50) {
+        if (newmatrix.m11() < 0.4) setSceneRect(0, 0, (m_projectDuration + 100 / newmatrix.m11()), sceneRect().height());
         else setSceneRect(0, 0, (m_projectDuration + 300), sceneRect().height());
     }
     centerOn(QPointF(cursorPos(), verticalPos));
@@ -4703,6 +4750,7 @@ void CustomTrackView::pasteClipEffects()
             for (int j = 0; j < clip->effectsCount(); j++) {
                 QDomElement eff = clip->effectAt(j);
                 if (eff.attribute("unique", "0") == "0" || item->hasEffect(eff.attribute("tag"), eff.attribute("id")) == -1) {
+                    adjustKeyfames(clip->cropStart(), item->cropStart(), item->cropDuration(), eff);
                     new AddEffectCommand(this, m_document->tracksCount() - item->track(), item->startPos(), eff, true, paste);
                 }
             }
@@ -4718,6 +4766,34 @@ void CustomTrackView::pasteClipEffects()
 }
 
 
+void CustomTrackView::adjustKeyfames(GenTime oldstart, GenTime newstart, GenTime duration, QDomElement xml)
+{
+    // parse parameters to check if we need to adjust to the new crop start
+    int diff = (newstart - oldstart).frames(m_document->fps());
+    int max = (newstart + duration).frames(m_document->fps());
+    QDomNodeList params = xml.elementsByTagName("parameter");
+    for (int i = 0; i < params.count(); i++) {
+        QDomElement e = params.item(i).toElement();
+        if (!e.isNull() && (e.attribute("type") == "keyframe" || e.attribute("type") == "simplekeyframe")) {
+            QString def = e.attribute("default");
+            // Effect has a keyframe type parameter, we need to adjust the values
+            QStringList keys = e.attribute("keyframes").split(";", QString::SkipEmptyParts);
+            QStringList newKeyFrames;
+            foreach(const QString &str, keys) {
+                int pos = str.section(':', 0, 0).toInt();
+                double val = str.section(':', 1, 1).toDouble();
+                pos += diff;
+                if (pos > max) {
+                    newKeyFrames.append(QString::number(max) + ':' + QString::number(val));
+                    break;
+                } else newKeyFrames.append(QString::number(pos) + ':' + QString::number(val));
+            }
+            //kDebug()<<"ORIGIN: "<<keys<<", FIXED: "<<newKeyFrames;
+            e.setAttribute("keyframes", newKeyFrames.join(";"));
+        }
+    }
+}
+
 ClipItem *CustomTrackView::getClipUnderCursor() const
 {
     QRectF rect((double) m_cursorPos, 0.0, 1.0, (double)(m_tracksHeight * m_document->tracksCount()));
@@ -4839,6 +4915,7 @@ void CustomTrackView::setOutPoint()
 
 void CustomTrackView::slotUpdateAllThumbs()
 {
+    if (!isEnabled()) return;
     QList<QGraphicsItem *> itemList = items();
     //if (itemList.isEmpty()) return;
     ClipItem *item;