]> git.sesse.net Git - kdenlive/blobdiff - src/customtrackview.cpp
Improve guide actions from timeline ruler context menu:
[kdenlive] / src / customtrackview.cpp
index c74204daabbfb2cb824379bd8a76c3f71b096fcf..d8a432acbecbc586c0c5252fc7d73f80d9866d20 100644 (file)
@@ -63,6 +63,7 @@
 #include <KUrl>
 #include <KIcon>
 #include <KCursor>
+#include <KColorScheme>
 
 #include <QMouseEvent>
 #include <QStylePainter>
@@ -171,6 +172,15 @@ void CustomTrackView::setContextMenu(QMenu *timeline, QMenu *clip, QMenu *transi
             break;
         }
     }
+
+    m_timelineContextMenu->addSeparator();
+    m_deleteGuide = new KAction(KIcon("edit-delete"), i18n("Delete Guide"), this);
+    connect(m_deleteGuide, SIGNAL(triggered()), this, SLOT(slotDeleteTimeLineGuide()));
+    m_timelineContextMenu->addAction(m_deleteGuide);
+
+    m_editGuide = new KAction(KIcon("document-properties"), i18n("Edit Guide"), this);
+    connect(m_editGuide, SIGNAL(triggered()), this, SLOT(slotEditTimeLineGuide()));
+    m_timelineContextMenu->addAction(m_editGuide);
 }
 
 void CustomTrackView::checkAutoScroll()
@@ -262,15 +272,13 @@ void CustomTrackView::slotCheckPositionScrolling()
 {
     // If mouse is at a border of the view, scroll
     if (m_moveOpMode != SEEK) return;
-    int pos = cursorPos();
-    if (mapFromScene(pos, 0).x() < 7) {
+    if (mapFromScene(m_cursorPos, 0).x() < 3) {
         horizontalScrollBar()->setValue(horizontalScrollBar()->value() - 2);
-        setCursorPos(mapToScene(QPoint()).x() - 1);
         QTimer::singleShot(200, this, SLOT(slotCheckPositionScrolling()));
-
-    } else if (viewport()->width() - 5 < mapFromScene(pos + 1, 0).x()) {
+        setCursorPos(mapToScene(QPoint(-2, 0)).x());
+    } else if (viewport()->width() - 3 < mapFromScene(m_cursorPos + 1, 0).x()) {
         horizontalScrollBar()->setValue(horizontalScrollBar()->value() + 2);
-        setCursorPos(mapToScene(viewport()->width(), 0).x() + 1);
+        setCursorPos(mapToScene(QPoint(viewport()->width(), 0)).x() + 1);
         QTimer::singleShot(200, this, SLOT(slotCheckPositionScrolling()));
     }
 }
@@ -540,11 +548,6 @@ void CustomTrackView::mouseMoveEvent(QMouseEvent * event)
         m_moveOpMode = opMode;
         setCursor(Qt::SplitHCursor);
     } else {
-        if (event->buttons() != Qt::NoButton && event->modifiers() == Qt::NoModifier) {
-            m_moveOpMode = SEEK;
-            setCursorPos(mappedXPos);
-            slotCheckPositionScrolling();
-        } else m_moveOpMode = NONE;
         if (m_visualTip) {
             delete m_animation;
             m_animationTimer->stop();
@@ -554,6 +557,13 @@ void CustomTrackView::mouseMoveEvent(QMouseEvent * event)
 
         }
         setCursor(Qt::ArrowCursor);
+        if (event->buttons() != Qt::NoButton && event->modifiers() == Qt::NoModifier) {
+            QGraphicsView::mouseMoveEvent(event);
+            m_moveOpMode = SEEK;
+            setCursorPos(mappedXPos);
+            slotCheckPositionScrolling();
+            return;
+        } else m_moveOpMode = NONE;
     }
     QGraphicsView::mouseMoveEvent(event);
 }
@@ -582,7 +592,7 @@ void CustomTrackView::mousePressEvent(QMouseEvent * event)
     }
 
     // check item under mouse
-    QList<QGraphicsItem *> collisionList = items(event->pos());
+    QList<QGraphicsItem *> collisionList = items(m_clickEvent);
 
     if (event->modifiers() == Qt::ControlModifier && m_tool != SPACERTOOL && collisionList.count() == 0) {
         setDragMode(QGraphicsView::ScrollHandDrag);
@@ -604,7 +614,7 @@ void CustomTrackView::mousePressEvent(QMouseEvent * event)
         return;
     }
 
-    if (collisionList.count() == 1 && collisionList.at(0)->type() == GUIDEITEM) {
+    if (event->button() == Qt::LeftButton && collisionList.count() == 1 && collisionList.at(0)->type() == GUIDEITEM) {
         // a guide item was pressed
         collisionList.at(0)->setFlag(QGraphicsItem::ItemIsMovable, true);
         m_dragItem = NULL;
@@ -644,6 +654,7 @@ void CustomTrackView::mousePressEvent(QMouseEvent * event)
 
     // context menu requested
     if (event->button() == Qt::RightButton) {
+        m_dragGuide = NULL;
         if (m_dragItem) {
             if (dragGroup) dragGroup->setSelected(true);
             else if (!m_dragItem->isSelected()) {
@@ -652,6 +663,15 @@ void CustomTrackView::mousePressEvent(QMouseEvent * event)
                 m_dragItem->setSelected(true);
             }
         }
+
+        // check if there is a guide close to mouse click
+        QList<QGraphicsItem *> guidesCollisionList = items(event->pos().x() - 5, event->pos().y(), 10, 1);
+        for (int i = 0; i < guidesCollisionList.count(); i++) {
+            if (guidesCollisionList.at(0)->type() != GUIDEITEM)
+                guidesCollisionList.removeAt(0);
+        }
+        if (!guidesCollisionList.isEmpty())
+            m_dragGuide = static_cast <Guide *>(guidesCollisionList.at(0));
         m_operationMode = NONE;
         displayContextMenu(event->globalPos(), m_dragItem, dragGroup);
         m_menuPosition = m_clickEvent;
@@ -1045,6 +1065,8 @@ void CustomTrackView::editKeyFrame(const GenTime pos, const int track, const int
 
 void CustomTrackView::displayContextMenu(QPoint pos, AbstractClipItem *clip, AbstractGroupItem *group)
 {
+    m_deleteGuide->setEnabled(m_dragGuide != NULL);
+    m_editGuide->setEnabled(m_dragGuide != NULL);
     if (clip == NULL) m_timelineContextMenu->popup(pos);
     else if (group != NULL) {
         m_changeSpeedAction->setEnabled(false);
@@ -1325,7 +1347,6 @@ void CustomTrackView::updateEffect(int track, GenTime pos, QDomElement effect, i
                 int pos = effectParams.paramValue("out").toInt() - effectParams.paramValue("in").toInt();
                 clip->setFadeOut(pos);
             }
-
         }
     }
     m_document->setModified(true);
@@ -1395,6 +1416,17 @@ void CustomTrackView::cutClip(ItemInfo info, GenTime cutTime, bool cut)
         else newPos.cropStart = item->info().cropStart + (cutTime - info.startPos) * speed;
         newPos.track = info.track;
         ClipItem *dup = item->clone(newPos);
+        // remove unwanted effects (fade in) from 2nd part of cutted clip
+        int ix = dup->hasEffect(QString(), "fadein");
+        if (ix != -1) {
+            QDomElement oldeffect = item->effectAt(ix);
+            dup->deleteEffect(oldeffect.attribute("kdenlive_ix"));
+        }
+        ix = dup->hasEffect(QString(), "fade_from_black");
+        if (ix != -1) {
+            QDomElement oldeffect = item->effectAt(ix);
+            dup->deleteEffect(oldeffect.attribute("kdenlive_ix"));
+        }
         item->resizeEnd(cutPos, false);
         scene()->addItem(dup);
         if (item->checkKeyFrames()) slotRefreshEffects(item);
@@ -1603,6 +1635,7 @@ void CustomTrackView::dragMoveEvent(QDragMoveEvent * event)
     const QPointF pos = mapToScene(event->pos());
     if (m_selectionGroup && m_clipDrag) {
         m_selectionGroup->setPos(pos.x(), pos.y());
+        emit mousePosition((int)(m_selectionGroup->scenePos().x() + 0.5));
         event->setDropAction(Qt::MoveAction);
         event->acceptProposedAction();
     } else {
@@ -1702,8 +1735,10 @@ int CustomTrackView::duration() const
 
 void CustomTrackView::addTrack(TrackInfo type, int ix)
 {
-    if (ix == -1) m_document->insertTrack(ix, type);
-    else {
+    if (ix == -1 || ix == m_document->tracksCount()) {
+        m_document->insertTrack(ix, type);
+        m_document->renderer()->mltInsertTrack(1, type.type == VIDEOTRACK);
+    } else {
         m_document->insertTrack(m_document->tracksCount() - ix, type);
         // insert track in MLT playlist
         m_document->renderer()->mltInsertTrack(m_document->tracksCount() - ix, type.type == VIDEOTRACK);
@@ -1843,10 +1878,10 @@ void CustomTrackView::slotSwitchTrackAudio(int ix)
 {
     /*for (int i = 0; i < m_document->tracksCount(); i++)
         kDebug() << "TRK " << i << " STATE: " << m_document->trackInfoAt(i).isMute << m_document->trackInfoAt(i).isBlind;*/
-    int tracknumber = m_document->tracksCount() - ix;
-    m_document->switchTrackAudio(tracknumber - 1, !m_document->trackInfoAt(tracknumber - 1).isMute);
-    kDebug() << "NEXT TRK STATE: " << m_document->trackInfoAt(tracknumber - 1).isMute << m_document->trackInfoAt(tracknumber - 1).isBlind;
-    m_document->renderer()->mltChangeTrackState(tracknumber, m_document->trackInfoAt(tracknumber - 1).isMute, m_document->trackInfoAt(tracknumber - 1).isBlind);
+    int tracknumber = m_document->tracksCount() - ix - 1;
+    m_document->switchTrackAudio(tracknumber, !m_document->trackInfoAt(tracknumber).isMute);
+    kDebug() << "NEXT TRK STATE: " << m_document->trackInfoAt(tracknumber).isMute << m_document->trackInfoAt(tracknumber).isBlind;
+    m_document->renderer()->mltChangeTrackState(tracknumber + 1, m_document->trackInfoAt(tracknumber).isMute, m_document->trackInfoAt(tracknumber).isBlind);
     m_document->setModified(true);
 }
 
@@ -1863,7 +1898,7 @@ void CustomTrackView::lockTrack(int ix, bool lock)
     int tracknumber = m_document->tracksCount() - ix - 1;
     m_document->switchTrackLock(tracknumber, lock);
     emit doTrackLock(ix, lock);
-    QList<QGraphicsItem *> selection = items(0, ix * m_tracksHeight + m_tracksHeight / 2, mapFromScene(sceneRect().width(), 0).x(), m_tracksHeight / 2 - 2);
+    QList<QGraphicsItem *> selection = m_scene->items(0, ix * m_tracksHeight + m_tracksHeight / 2, sceneRect().width(), m_tracksHeight / 2 - 2);
 
     for (int i = 0; i < selection.count(); i++) {
         if (selection.at(i)->type() != AVWIDGET && selection.at(i)->type() != TRANSITIONWIDGET) continue;
@@ -2066,11 +2101,12 @@ void CustomTrackView::deleteClip(const QString &clipId)
 
 void CustomTrackView::setCursorPos(int pos, bool seek)
 {
+    if (pos == m_cursorPos) return;
     emit cursorMoved((int)(m_cursorPos), (int)(pos));
     m_cursorPos = pos;
-    m_cursorLine->setPos(pos, 0);
-    if (seek) m_document->renderer()->seek(GenTime(pos, m_document->fps()));
+    if (seek) m_document->renderer()->seek(GenTime(m_cursorPos, m_document->fps()));
     else if (m_autoScroll) checkScrolling();
+    m_cursorLine->setPos(m_cursorPos, 0);
 }
 
 void CustomTrackView::updateCursorPos()
@@ -2102,9 +2138,7 @@ void CustomTrackView::initCursorPos(int pos)
 
 void CustomTrackView::checkScrolling()
 {
-    int vert = verticalScrollBar()->value();
-    int hor = cursorPos();
-    ensureVisible(hor, vert + 10, 2, 2, 50, 0);
+    ensureVisible(m_cursorPos, verticalScrollBar()->value() + 10, 2, 2, 50, 0);
 }
 
 void CustomTrackView::mouseReleaseEvent(QMouseEvent * event)
@@ -2120,9 +2154,12 @@ void CustomTrackView::mouseReleaseEvent(QMouseEvent * event)
         setCursor(Qt::ArrowCursor);
         m_operationMode = NONE;
         m_dragGuide->setFlag(QGraphicsItem::ItemIsMovable, false);
-        EditGuideCommand *command = new EditGuideCommand(this, m_dragGuide->position(), m_dragGuide->label(), GenTime(m_dragGuide->pos().x(), m_document->fps()), m_dragGuide->label(), false);
-        m_commandStack->push(command);
-        m_dragGuide->updateGuide(GenTime(m_dragGuide->pos().x(), m_document->fps()));
+        GenTime newPos = GenTime(m_dragGuide->pos().x(), m_document->fps());
+        if (newPos != m_dragGuide->position()) {
+            EditGuideCommand *command = new EditGuideCommand(this, m_dragGuide->position(), m_dragGuide->label(), newPos, m_dragGuide->label(), false);
+            m_commandStack->push(command);
+            m_dragGuide->updateGuide(GenTime(m_dragGuide->pos().x(), m_document->fps()));
+        }
         m_dragGuide = NULL;
         m_dragItem = NULL;
         return;
@@ -3047,15 +3084,10 @@ void CustomTrackView::moveClip(const ItemInfo start, const ItemInfo end)
 void CustomTrackView::moveGroup(QList <ItemInfo> startClip, QList <ItemInfo> startTransition, const GenTime offset, const int trackOffset, bool reverseMove)
 {
     // Group Items
-    kDebug() << "//GRP MOVE, REVERS:" << reverseMove;
-    kDebug() << "// GROUP MOV; OFFSET: " << offset.frames(25) << ", TK OFF: " << trackOffset;
+    /*kDebug() << "//GRP MOVE, REVERS:" << reverseMove;
+    kDebug() << "// GROUP MOV; OFFSET: " << offset.frames(25) << ", TK OFF: " << trackOffset;*/
     resetSelectionGroup();
     m_scene->clearSelection();
-    QList<QGraphicsItem *> itemList = items();
-    for (int i = 0; i < itemList.count(); i++) {
-        if (itemList.at(i)->type() == AVWIDGET)
-            kDebug() << "ITEM " << i << ": " << static_cast <AbstractClipItem *>(itemList.at(i))->startPos().frames(25) << ",REEL: " << itemList.at(i)->scenePos();
-    }
 
     for (int i = 0; i < startClip.count(); i++) {
         if (reverseMove) {
@@ -3138,13 +3170,6 @@ void CustomTrackView::moveGroup(QList <ItemInfo> startClip, QList <ItemInfo> sta
         }
         KdenliveSettings::setSnaptopoints(snap);
     } else kDebug() << "///////// WARNING; NO GROUP TO MOVE";
-
-    kDebug() << "///////// DONE+++++++++++++";
-    itemList = items();
-    for (int i = 0; i < itemList.count(); i++) {
-        if (itemList.at(i)->type() == AVWIDGET)
-            kDebug() << "ITEM " << i << ": " << static_cast <AbstractClipItem *>(itemList.at(i))->startPos().frames(25) << ",REEL: " << itemList.at(i)->scenePos();
-    }
 }
 
 void CustomTrackView::moveTransition(const ItemInfo start, const ItemInfo end)
@@ -3469,6 +3494,16 @@ bool sortGuidesList(const Guide *g1 , const Guide *g2)
     return (*g1).position() < (*g2).position();
 }
 
+int CustomTrackView::hasGuide(int pos, int offset)
+{
+    for (int i = 0; i < m_guides.count(); i++) {
+        int guidePos = m_guides.at(i)->position().frames(m_document->fps());
+        if (qAbs(guidePos - pos) < offset) return guidePos;
+        else if (guidePos > pos) return -1;
+    }
+    return -1;
+}
+
 void CustomTrackView::editGuide(const GenTime oldPos, const GenTime pos, const QString &comment)
 {
     if (oldPos > GenTime() && pos > GenTime()) {
@@ -3524,9 +3559,11 @@ void CustomTrackView::slotAddGuide()
     }
 }
 
-void CustomTrackView::slotEditGuide()
+void CustomTrackView::slotEditGuide(int guidePos)
 {
-    GenTime pos = GenTime(m_cursorPos, m_document->fps());
+    GenTime pos;
+    if (guidePos == -1) pos = GenTime(m_cursorPos, m_document->fps());
+    else pos = GenTime(guidePos, m_document->fps());
     bool found = false;
     for (int i = 0; i < m_guides.count(); i++) {
         if (m_guides.at(i)->position() == pos) {
@@ -3548,9 +3585,22 @@ void CustomTrackView::slotEditGuide(CommentedTime guide)
 }
 
 
-void CustomTrackView::slotDeleteGuide()
+void CustomTrackView::slotEditTimeLineGuide()
 {
-    GenTime pos = GenTime(m_cursorPos, m_document->fps());
+    if (m_dragGuide == NULL) return;
+    CommentedTime guide = m_dragGuide->info();
+    MarkerDialog d(NULL, guide, m_document->timecode(), i18n("Edit Guide"), this);
+    if (d.exec() == QDialog::Accepted) {
+        EditGuideCommand *command = new EditGuideCommand(this, guide.time(), guide.comment(), d.newMarker().time(), d.newMarker().comment(), true);
+        m_commandStack->push(command);
+    }
+}
+
+void CustomTrackView::slotDeleteGuide(int guidePos)
+{
+    GenTime pos;
+    if (guidePos == -1) pos = GenTime(m_cursorPos, m_document->fps());
+    else pos = GenTime(guidePos, m_document->fps());
     bool found = false;
     for (int i = 0; i < m_guides.count(); i++) {
         if (m_guides.at(i)->position() == pos) {
@@ -3563,6 +3613,15 @@ void CustomTrackView::slotDeleteGuide()
     if (!found) emit displayMessage(i18n("No guide at cursor time"), ErrorMessage);
 }
 
+
+void CustomTrackView::slotDeleteTimeLineGuide()
+{
+    if (m_dragGuide == NULL) return;
+    EditGuideCommand *command = new EditGuideCommand(this, m_dragGuide->position(), m_dragGuide->label(), GenTime(), QString(), true);
+    m_commandStack->push(command);
+}
+
+
 void CustomTrackView::slotDeleteAllGuides()
 {
     QUndoCommand *deleteAll = new QUndoCommand();
@@ -3614,15 +3673,18 @@ void CustomTrackView::slotRefreshGuides()
 
 void CustomTrackView::drawBackground(QPainter * painter, const QRectF & rect)
 {
-    QColor base = palette().button().color();
     QRectF r = rect;
     r.setWidth(r.width() + 1);
     painter->setClipRect(r);
     painter->drawLine(r.left(), 0, r.right(), 0);
     uint max = m_document->tracksCount();
+    KColorScheme scheme(palette().currentColorGroup(), KColorScheme::Window);
+    QColor lockedColor = scheme.background(KColorScheme::NegativeBackground).color();
+    QColor audioColor = palette().alternateBase().color();
+    QColor base = scheme.background(KColorScheme::NormalBackground).color();
     for (uint i = 0; i < max;i++) {
-        if (m_document->trackInfoAt(max - i - 1).isLocked == true) painter->fillRect(r.left(), m_tracksHeight * i + 1, r.right() - r.left() + 1, m_tracksHeight - 1, QBrush(QColor(250, 250, 100)));
-        else if (m_document->trackInfoAt(max - i - 1).type == AUDIOTRACK) painter->fillRect(r.left(), m_tracksHeight * i + 1, r.right() - r.left() + 1, m_tracksHeight - 1, QBrush(QColor(240, 240, 255)));
+        if (m_document->trackInfoAt(max - i - 1).isLocked == true) painter->fillRect(r.left(), m_tracksHeight * i + 1, r.right() - r.left() + 1, m_tracksHeight - 1, QBrush(lockedColor));
+        else if (m_document->trackInfoAt(max - i - 1).type == AUDIOTRACK) painter->fillRect(r.left(), m_tracksHeight * i + 1, r.right() - r.left() + 1, m_tracksHeight - 1, QBrush(audioColor));
         painter->drawLine(r.left(), m_tracksHeight * (i + 1), r.right(), m_tracksHeight * (i + 1));
     }
     int lowerLimit = m_tracksHeight * m_document->tracksCount() + 1;