]> git.sesse.net Git - kdenlive/blobdiff - src/customtrackview.cpp
reindent + nice (working) progress bar for document loading
[kdenlive] / src / customtrackview.cpp
index 50d6dbacb1ce2c7f20c0f04c45084683d428efb0..c65d3b91001bf2c4738c96c7d5757bb4ed8e6e2c 100644 (file)
@@ -324,6 +324,7 @@ void CustomTrackView::mouseMoveEvent(QMouseEvent * event)
 {
     int pos = event->x();
     int mappedXPos = (int)(mapToScene(event->pos()).x() + 0.5);
+    double snappedPos = getSnapPointForPos(mappedXPos);
     emit mousePosition(mappedXPos);
 
     if (event->buttons() & Qt::MidButton) return;
@@ -349,11 +350,9 @@ void CustomTrackView::mouseMoveEvent(QMouseEvent * event)
                 } else if (m_scrollTimer.isActive()) m_scrollTimer.stop();
 
             } else if (m_operationMode == RESIZESTART && move) {
-                double snappedPos = getSnapPointForPos(mappedXPos);
                 m_document->renderer()->pause();
                 m_dragItem->resizeStart((int)(snappedPos));
             } else if (m_operationMode == RESIZEEND && move) {
-                double snappedPos = getSnapPointForPos(mappedXPos);
                 m_document->renderer()->pause();
                 m_dragItem->resizeEnd((int)(snappedPos));
             } else if (m_operationMode == FADEIN && move) {
@@ -387,7 +386,40 @@ void CustomTrackView::mouseMoveEvent(QMouseEvent * event)
         } else if (m_operationMode == SPACER && move && m_selectionGroup) {
             // spacer tool
             int mappedClick = (int)(mapToScene(m_clickEvent).x() + 0.5);
-            m_selectionGroup->translate(mappedXPos - m_selectionGroup->sceneBoundingRect().left(), 0);
+            if (snappedPos < 0) snappedPos = 0;
+            // Make sure there is no collision
+            QList<QGraphicsItem *> children = m_selectionGroup->childItems();
+            QPainterPath shape = m_selectionGroup->clipGroupShape(QPointF(snappedPos - m_selectionGroup->sceneBoundingRect().left(), 0));
+            QList<QGraphicsItem*> collidingItems = scene()->items(shape, Qt::IntersectsItemShape);
+            collidingItems.removeAll(m_selectionGroup);
+            for (int i = 0; i < children.count(); i++) {
+                collidingItems.removeAll(children.at(i));
+            }
+            bool collision = false;
+            for (int i = 0; i < collidingItems.count(); i++) {
+                if (collidingItems.at(i)->type() == AVWIDGET) {
+                    collision = true;
+                    break;
+                }
+            }
+            if (!collision) {
+                // Check transitions
+                shape = m_selectionGroup->transitionGroupShape(QPointF(snappedPos - m_selectionGroup->sceneBoundingRect().left(), 0));
+                collidingItems = scene()->items(shape, Qt::IntersectsItemShape);
+                collidingItems.removeAll(m_selectionGroup);
+                for (int i = 0; i < children.count(); i++) {
+                    collidingItems.removeAll(children.at(i));
+                }
+                for (int i = 0; i < collidingItems.count(); i++) {
+                    if (collidingItems.at(i)->type() == TRANSITIONWIDGET) {
+                        collision = true;
+                        break;
+                    }
+                }
+            }
+
+            if (!collision)
+                m_selectionGroup->translate(snappedPos - m_selectionGroup->sceneBoundingRect().left(), 0);
             //m_selectionGroup->setPos(mappedXPos + (((int) m_selectionGroup->boundingRect().topLeft().x() + 0.5) - mappedClick) , m_selectionGroup->pos().y());
         }
     }
@@ -758,17 +790,14 @@ void CustomTrackView::mousePressEvent(QMouseEvent * event)
             if (event->modifiers() == Qt::ControlModifier) {
                 // Ctrl + click, select all items on track after click position
                 int track = (int)(mapToScene(m_clickEvent).y() / m_tracksHeight);
-                selection = items(m_clickEvent.x(), track * m_tracksHeight + m_tracksHeight / 2, mapFromScene(sceneRect().width(), 0).x() - m_clickEvent.x(), m_tracksHeight / 2 - 2);
-                int maxHeight = m_tracksHeight * 1.5;
-                for (int i = 0; i < selection.count(); i++) {
-                    // Check that we don't try to move a group with clips on other tracks
-                    if (selection.at(i)->type() == GROUPWIDGET && (selection.at(i)->boundingRect().height() >= maxHeight)) {
-                        emit displayMessage(i18n("Cannot use spacer in a track with a group"), ErrorMessage);
-                        return;
-                    } else if (selection.at(i)->parentItem() && (selection.at(i)->parentItem()->boundingRect().height() >= maxHeight)) {
-                        emit displayMessage(i18n("Cannot use spacer in a track with a group"), ErrorMessage);
-                        return;
-                    }
+                QRectF rect(mapToScene(m_clickEvent).x(), track * m_tracksHeight + m_tracksHeight / 2, sceneRect().width() - mapToScene(m_clickEvent).x(), m_tracksHeight / 2 - 2);
+
+                bool isOk;
+                selection = checkForGroups(rect, isOk);
+                if (!isOk) {
+                    // groups found on track, do not allow the move
+                    emit displayMessage(i18n("Cannot use spacer in a track with a group"), ErrorMessage);
+                    return;
                 }
 
                 kDebug() << "SPACER TOOL + CTRL, SELECTING ALL CLIPS ON TRACK " << track << " WITH SELECTION RECT " << m_clickEvent.x() << "/" <<  track * m_tracksHeight + 1 << "; " << mapFromScene(sceneRect().width(), 0).x() - m_clickEvent.x() << "/" << m_tracksHeight - 2;
@@ -1025,6 +1054,8 @@ void CustomTrackView::groupSelectedItems(bool force, bool createNewGroup)
     for (int i = 0; i < selection.count(); i++) {
         if (selection.at(i)->parentItem() == 0 && (selection.at(i)->type() == AVWIDGET || selection.at(i)->type() == TRANSITIONWIDGET || selection.at(i)->type() == GROUPWIDGET)) {
             rectUnion = rectUnion.united(selection.at(i)->sceneBoundingRect());
+        } else if (selection.at(i)->parentItem()) {
+            rectUnion = rectUnion.united(selection.at(i)->parentItem()->sceneBoundingRect());
         }
     }
 
@@ -1380,12 +1411,17 @@ void CustomTrackView::addEffect(int track, GenTime pos, QDomElement effect)
             int strobe = EffectsList::parameter(effect, "strobe").toInt();
             if (strobe == 0) strobe = 1;
             doChangeClipSpeed(info, speed, 1.0, strobe, clip->baseClip()->getId());
-            clip->addEffect(effect);
+            EffectsParameterList params = clip->addEffect(effect);
+            m_document->renderer()->mltAddEffect(track, pos, params);
             if (clip->isSelected()) emit clipItemSelected(clip);
             return;
         }
-
         EffectsParameterList params = clip->addEffect(effect);
+        if (effect.attribute("disabled") == "1") {
+            // Effect is disabled, don't add it to MLT playlist
+            if (clip->isSelected()) emit clipItemSelected(clip);
+            return;
+        }
         if (!m_document->renderer()->mltAddEffect(track, pos, params))
             emit displayMessage(i18n("Problem adding effect to clip"), ErrorMessage);
         if (clip->isSelected()) emit clipItemSelected(clip);
@@ -1403,6 +1439,7 @@ void CustomTrackView::deleteEffect(int track, GenTime pos, QDomElement effect)
             doChangeClipSpeed(info, 1.0, clip->speed(), 1, clip->baseClip()->getId());
             clip->deleteEffect(index);
             emit clipItemSelected(clip);
+            m_document->renderer()->mltRemoveEffect(track, pos, index, true);
             return;
         }
     }
@@ -1533,13 +1570,17 @@ void CustomTrackView::updateEffect(int track, GenTime pos, QDomElement insertedE
         // Special case: speed effect
         if (effect.attribute("id") == "speed") {
             ItemInfo info = clip->info();
-            double speed = EffectsList::parameter(effect, "speed").toDouble() / 100.0;
-            int strobe = EffectsList::parameter(effect, "strobe").toInt();
-            if (strobe == 0) strobe = 1;
-            doChangeClipSpeed(info, speed, clip->speed(), strobe, clip->baseClip()->getId());
+            if (effect.attribute("disabled") == "1") doChangeClipSpeed(info, 1.0, clip->speed(), 1, clip->baseClip()->getId());
+            else {
+                double speed = EffectsList::parameter(effect, "speed").toDouble() / 100.0;
+                int strobe = EffectsList::parameter(effect, "strobe").toInt();
+                if (strobe == 0) strobe = 1;
+                doChangeClipSpeed(info, speed, clip->speed(), strobe, clip->baseClip()->getId());
+            }
             clip->setEffectAt(ix, effect);
             if (ix == clip->selectedEffectIndex()) {
                 clip->setSelectedEffect(ix);
+                if (!triggeredByUser) emit clipItemSelected(clip, ix);
             }
             return;
         }
@@ -1585,11 +1626,16 @@ void CustomTrackView::moveEffect(int track, GenTime pos, int oldPos, int newPos)
 {
     ClipItem *clip = getClipItemAt((int)pos.frames(m_document->fps()), m_document->tracksCount() - track);
     if (clip && !clip->effectAt(newPos - 1).isNull() && !clip->effectAt(oldPos - 1).isNull()) {
-        QDomElement act = clip->effectAt(newPos - 1).cloneNode().toElement();
-        QDomElement before = clip->effectAt(oldPos - 1).cloneNode().toElement();
+        QDomElement act = clip->effectAt(newPos - 1);
+        QDomElement before = clip->effectAt(oldPos - 1);
         clip->setEffectAt(oldPos - 1, act);
         clip->setEffectAt(newPos - 1, before);
-        m_document->renderer()->mltMoveEffect(track, pos, oldPos, newPos);
+        // special case: speed effect, which is a pseudo-effect, not appearing in MLT's effects
+        if (act.attribute("id") == "speed") {
+            m_document->renderer()->mltUpdateEffectPosition(track, pos, oldPos, newPos);
+        } else if (before.attribute("id") == "speed") {
+            m_document->renderer()->mltUpdateEffectPosition(track, pos, newPos, oldPos);
+        } else m_document->renderer()->mltMoveEffect(track, pos, oldPos, newPos);
         emit clipItemSelected(clip, newPos - 1);
         setDocumentModified();
     } else emit displayMessage(i18n("Cannot move effect"), ErrorMessage);
@@ -1597,22 +1643,9 @@ void CustomTrackView::moveEffect(int track, GenTime pos, int oldPos, int newPos)
 
 void CustomTrackView::slotChangeEffectState(ClipItem *clip, int effectPos, bool disable)
 {
-    QDomElement effect = clip->effectAt(effectPos).cloneNode().toElement();
+    QDomElement effect = clip->effectAt(effectPos);
     QDomElement oldEffect = effect.cloneNode().toElement();
-    if (effect.attribute("id") == "speed") {
-        if (clip) {
-            ItemInfo info = clip->info();
-            effect.setAttribute("disabled", (int) disable);
-            if (disable) doChangeClipSpeed(info, 1.0, clip->speed(), 1, clip->baseClip()->getId());
-            else {
-                double speed = EffectsList::parameter(effect, "speed").toDouble() / 100.0;
-                int strobe = EffectsList::parameter(effect, "strobe").toInt();
-                if (strobe == 0) strobe = 1;
-                doChangeClipSpeed(info, speed, 1.0, strobe, clip->baseClip()->getId());
-            }
-            return;
-        }
-    }
+
     effect.setAttribute("disabled", (int) disable);
     EditEffectCommand *command = new EditEffectCommand(this, m_document->tracksCount() - clip->track(), clip->startPos(), oldEffect, effect, effectPos, true);
     m_commandStack->push(command);
@@ -2194,6 +2227,24 @@ void CustomTrackView::slotSwitchTrackVideo(int ix)
     setDocumentModified();
 }
 
+QList<QGraphicsItem *> CustomTrackView::checkForGroups(const QRectF &rect, bool &ok)
+{
+    // Check there is no group going over several tracks there, or that would result in timeline corruption
+    QList<QGraphicsItem *> selection = scene()->items(rect);
+    int maxHeight = m_tracksHeight * 1.5;
+    for (int i = 0; i < selection.count(); i++) {
+        // Check that we don't try to move a group with clips on other tracks
+        if (selection.at(i)->type() == GROUPWIDGET && (selection.at(i)->boundingRect().height() >= maxHeight)) {
+            ok = false;
+            break;
+        } else if (selection.at(i)->parentItem() && (selection.at(i)->parentItem()->boundingRect().height() >= maxHeight)) {
+            ok = false;
+            break;
+        }
+    }
+    return selection;
+}
+
 void CustomTrackView::slotRemoveSpace()
 {
     GenTime pos;
@@ -2207,6 +2258,7 @@ void CustomTrackView::slotRemoveSpace()
         pos = GenTime((int)(mapToScene(m_menuPosition).x()), m_document->fps());
         track = (int)(mapToScene(m_menuPosition).y() / m_tracksHeight);
     }
+
     ClipItem *item = getClipItemAt(pos, track);
     if (item) {
         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);
@@ -2219,8 +2271,16 @@ void CustomTrackView::slotRemoveSpace()
         return;
     }
 
-    QRectF r(pos.frames(m_document->fps()), track * m_tracksHeight + m_tracksHeight / 2, sceneRect().width() - pos.frames(m_document->fps()), m_tracksHeight / 2 - 1);
-    QList<QGraphicsItem *> items = m_scene->items(r);
+    // Make sure there is no group in the way
+    QRectF rect(pos.frames(m_document->fps()), track * m_tracksHeight + m_tracksHeight / 2, sceneRect().width() - pos.frames(m_document->fps()), m_tracksHeight / 2 - 2);
+
+    bool isOk;
+    QList<QGraphicsItem *> items = checkForGroups(rect, isOk);
+    if (!isOk) {
+        // groups found on track, do not allow the move
+        emit displayMessage(i18n("Cannot remove space in a track with a group"), ErrorMessage);
+        return;
+    }
 
     QList<ItemInfo> clipsToMove;
     QList<ItemInfo> transitionsToMove;
@@ -2255,19 +2315,20 @@ void CustomTrackView::slotInsertSpace()
     if (d.exec() != QDialog::Accepted) return;
     GenTime spaceDuration = d.selectedDuration();
     track = d.selectedTrack();
+
     ClipItem *item = getClipItemAt(pos, track);
     if (item) pos = item->startPos();
 
-    int minh = 0;
-    int maxh = sceneRect().height();
-    if (track != -1) {
-        minh = track * m_tracksHeight + m_tracksHeight / 2;
-        maxh = m_tracksHeight / 2 - 1;
+    // Make sure there is no group in the way
+    QRectF rect(pos.frames(m_document->fps()), track * m_tracksHeight + m_tracksHeight / 2, sceneRect().width() - pos.frames(m_document->fps()), m_tracksHeight / 2 - 2);
+    bool isOk;
+    QList<QGraphicsItem *> items = checkForGroups(rect, isOk);
+    if (!isOk) {
+        // groups found on track, do not allow the move
+        emit displayMessage(i18n("Cannot insert space in a track with a group"), ErrorMessage);
+        return;
     }
 
-    QRectF r(pos.frames(m_document->fps()), minh, sceneRect().width() - pos.frames(m_document->fps()), maxh);
-    QList<QGraphicsItem *> items = m_scene->items(r);
-
     QList<ItemInfo> clipsToMove;
     QList<ItemInfo> transitionsToMove;
 
@@ -2532,7 +2593,6 @@ void CustomTrackView::mouseReleaseEvent(QMouseEvent * event)
                     int tracknumber = m_document->tracksCount() - item->track() - 1;
                     bool isLocked = m_document->trackInfoAt(tracknumber).isLocked;
                     if (isLocked) item->setItemLocked(true);
-
                     QUndoCommand *moveCommand = new QUndoCommand();
                     moveCommand->setText(i18n("Move clip"));
                     new MoveClipCommand(this, m_dragItemInfo, info, false, moveCommand);
@@ -2591,6 +2651,8 @@ void CustomTrackView::mouseReleaseEvent(QMouseEvent * event)
                             ItemInfo trInfo = tr->info();
                             ItemInfo newTrInfo = trInfo;
                             newTrInfo.endPos = m_dragItem->endPos();
+                            kDebug() << "CLIP ENDS AT: " << newTrInfo.endPos.frames(25);
+                            kDebug() << "CLIP STARTS AT: " << newTrInfo.startPos.frames(25);
                             ClipItem * upperClip = getClipItemAt(m_dragItemInfo.startPos, m_dragItemInfo.track - 1);
                             if (!upperClip || !upperClip->baseClip()->isTransparent()) {
                                 if (!getClipItemAtStart(trInfo.startPos, tr->track())) {
@@ -2905,7 +2967,7 @@ void CustomTrackView::mouseReleaseEvent(QMouseEvent * event)
         ClipItem * item = static_cast <ClipItem *>(m_dragItem);
         int ix = item->hasEffect("volume", "fadein");
         if (ix != -1) {
-            QDomElement oldeffect = item->effectAt(ix).cloneNode().toElement();
+            QDomElement oldeffect = item->effectAt(ix);
             int start = item->cropStart().frames(m_document->fps());
             int end = item->fadeIn();
             if (end == 0) {
@@ -2925,7 +2987,7 @@ void CustomTrackView::mouseReleaseEvent(QMouseEvent * event)
         }
         ix = item->hasEffect("volume", "fade_from_black");
         if (ix != -1) {
-            QDomElement oldeffect = item->effectAt(ix).cloneNode().toElement();
+            QDomElement oldeffect = item->effectAt(ix);
             int start = item->cropStart().frames(m_document->fps());
             int end = item->fadeIn();
             if (end == 0) {
@@ -2944,7 +3006,7 @@ void CustomTrackView::mouseReleaseEvent(QMouseEvent * event)
         ClipItem * item = static_cast <ClipItem *>(m_dragItem);
         int ix = item->hasEffect("volume", "fadeout");
         if (ix != -1) {
-            QDomElement oldeffect = item->effectAt(ix).cloneNode().toElement();
+            QDomElement oldeffect = item->effectAt(ix);
             int end = (item->cropDuration() + item->cropStart()).frames(m_document->fps());
             int start = item->fadeOut();
             if (start == 0) {
@@ -2966,7 +3028,7 @@ void CustomTrackView::mouseReleaseEvent(QMouseEvent * event)
         }
         ix = item->hasEffect("brightness", "fade_to_black");
         if (ix != -1) {
-            QDomElement oldeffect = item->effectAt(ix).cloneNode().toElement();
+            QDomElement oldeffect = item->effectAt(ix);
             int end = (item->cropDuration() + item->cropStart()).frames(m_document->fps());
             int start = item->fadeOut();
             if (start == 0) {
@@ -3149,6 +3211,7 @@ void CustomTrackView::doChangeClipSpeed(ItemInfo info, const double speed, const
         item->updateRectGeometry();
         if (item->cropDuration().frames(m_document->fps()) > endPos)
             item->AbstractClipItem::resizeEnd(info.startPos.frames(m_document->fps()) + endPos, speed);
+        updateClipFade(item);
         setDocumentModified();
     } else emit displayMessage(i18n("Invalid clip"), ErrorMessage);
 }
@@ -3576,6 +3639,7 @@ void CustomTrackView::moveTransition(const ItemInfo start, const ItemInfo end, b
         item->resizeStart((int) end.startPos.frames(m_document->fps()));
     } else if (end.startPos == start.startPos) {
         // Transition end resize;
+        kDebug() << "// resize END: " << end.endPos.frames(m_document->fps());
         item->resizeEnd((int) end.endPos.frames(m_document->fps()));
     } else {
         // Move & resize
@@ -3880,6 +3944,7 @@ void CustomTrackView::addMarker(const QString &id, const GenTime &pos, const QSt
     DocClipBase *base = m_document->clipManager()->getClipById(id);
     if (!comment.isEmpty()) base->addSnapMarker(pos, comment);
     else base->deleteSnapMarker(pos);
+    emit updateClipMarkers(base);
     setDocumentModified();
     viewport()->update();
 }
@@ -5046,5 +5111,3 @@ void CustomTrackView::updateProjectFps()
     }
     viewport()->update();
 }
-
-#include "customtrackview.moc"
\ No newline at end of file