]> git.sesse.net Git - kdenlive/blobdiff - src/customtrackview.cpp
insert & move timeline clips
[kdenlive] / src / customtrackview.cpp
index b386c73a78a25a469407db5781a6f2b97d94fbd9..08f940cebef05292fa59fac443407455cbdebf2f 100644 (file)
 #include "resizeclipcommand.h"
 #include "addtimelineclipcommand.h"
 
-CustomTrackView::CustomTrackView(KUndoStack *commandStack, QGraphicsScene * projectscene, QWidget *parent)
-    : QGraphicsView(projectscene, parent), m_commandStack(commandStack), m_tracksCount(0), m_cursorPos(0), m_dropItem(NULL), m_cursorLine(NULL), m_operationMode(NONE), m_startPos(QPointF()), m_dragItem(NULL), m_visualTip(NULL), m_moveOpMode(NONE), m_animation(NULL), m_projectDuration(0), m_scale(1.0), m_clickPoint(0)
+CustomTrackView::CustomTrackView(KdenliveDoc *doc, QGraphicsScene * projectscene, QWidget *parent)
+    : QGraphicsView(projectscene, parent), m_tracksCount(0), m_cursorPos(0), m_dropItem(NULL), m_cursorLine(NULL), m_operationMode(NONE), m_startPos(QPointF()), m_dragItem(NULL), m_visualTip(NULL), m_moveOpMode(NONE), m_animation(NULL), m_projectDuration(0), m_scale(1.0), m_clickPoint(0), m_document(doc)
 {
+  if (doc) m_commandStack = doc->commandStack();
+  else m_commandStack == NULL;
   setMouseTracking(true);
   setAcceptDrops(true);
   m_animationTimer = new QTimeLine(800);
@@ -81,6 +83,7 @@ void CustomTrackView::wheelEvent ( QWheelEvent * e )
 void CustomTrackView::mouseMoveEvent ( QMouseEvent * event )
 {
   int pos = event->x();
+  emit mousePosition(mapToScene(event->pos()).x() / m_scale);
   /*if (event->modifiers() == Qt::ControlModifier)
     setDragMode(QGraphicsView::ScrollHandDrag);
   else if (event->modifiers() == Qt::ShiftModifier) 
@@ -89,20 +92,36 @@ void CustomTrackView::mouseMoveEvent ( QMouseEvent * event )
 
       if (m_dragItem) { //event->button() == Qt::LeftButton) {
        // a button was pressed, delete visual tips
-
-if (m_operationMode == MOVE) {
-      int moveX = mapToScene(event->pos()).x();
-      //kDebug()<<"///////  MOVE CLIP, EVENT Y: "<<event->scenePos().y()<<", SCENE HEIGHT: "<<scene()->sceneRect().height();
-      int moveTrack = (int)  mapToScene(event->pos()).y() / 50;
-      int currentTrack = m_dragItem->track();
-
-      if (moveTrack > m_tracksCount - 1) moveTrack = m_tracksCount - 1;
-      else if (moveTrack < 0) moveTrack = 0;
-
-      int offset = moveTrack - currentTrack;
-      if (offset != 0) offset = 50 * offset;
-      m_dragItem->moveTo(moveX - m_clickPoint, m_scale, offset, moveTrack);
-  }
+       if (m_operationMode == MOVE) {
+         double snappedPos = getSnapPointForPos(mapToScene(event->pos()).x() - m_clickPoint);
+         double moveX = snappedPos; //mapToScene(event->pos()).x();
+         //kDebug()<<"///////  MOVE CLIP, EVENT Y: "<<event->scenePos().y()<<", SCENE HEIGHT: "<<scene()->sceneRect().height();
+         int moveTrack = (int)  mapToScene(event->pos()).y() / 50;
+         int currentTrack = m_dragItem->track();
+
+         if (moveTrack > m_tracksCount - 1) moveTrack = m_tracksCount - 1;
+         else if (moveTrack < 0) moveTrack = 0;
+
+         int offset = moveTrack - currentTrack;
+         if (offset != 0) offset = 50 * offset;
+         m_dragItem->moveTo(moveX / m_scale, m_scale, offset, moveTrack);
+       }
+       else if (m_operationMode == RESIZESTART) {
+         int pos = mapToScene(event->pos()).x();
+         m_dragItem->resizeStart(pos / m_scale, m_scale);
+       }
+       else if (m_operationMode == RESIZEEND) {
+         int pos = mapToScene(event->pos()).x();
+         m_dragItem->resizeEnd(pos / m_scale, m_scale);
+       }
+       else if (m_operationMode == FADEIN) {
+         int pos = mapToScene(event->pos()).x() / m_scale;
+         m_dragItem->setFadeIn(pos - m_dragItem->startPos(), m_scale);
+       }
+       else if (m_operationMode == FADEOUT) {
+         int pos = mapToScene(event->pos()).x() / m_scale;
+         m_dragItem->setFadeOut(m_dragItem->endPos() - pos, m_scale);
+       }
 
        if (m_animation) delete m_animation;
        m_animation = NULL;
@@ -124,7 +143,7 @@ if (m_operationMode == MOVE) {
     if (item) {
       ClipItem *clip = (ClipItem*) item;
       double size = mapToScene(QPoint(8, 0)).x();
-      OPERATIONTYPE opMode = clip->operationMode(mapToScene(event->pos()));
+      OPERATIONTYPE opMode = clip->operationMode(mapToScene(event->pos()), m_scale);
       if (opMode == m_moveOpMode) {
        QGraphicsView::mouseMoveEvent(event);
        return;
@@ -186,7 +205,7 @@ if (m_operationMode == MOVE) {
       }
       else if (opMode == FADEIN) {
        if (m_visualTip == NULL) {
-         m_visualTip = new QGraphicsEllipseItem(clip->rect().x() - size, clip->rect().y() - 8, size * 2, 16);
+         m_visualTip = new QGraphicsEllipseItem(clip->rect().x() + clip->fadeIn() * m_scale - size, clip->rect().y() - 8, size * 2, 16);
          ((QGraphicsEllipseItem*) m_visualTip)->setBrush(m_tipColor);
          ((QGraphicsEllipseItem*) m_visualTip)->setPen(QPen(Qt::transparent));
          m_visualTip->setZValue (100);
@@ -196,7 +215,7 @@ if (m_operationMode == MOVE) {
          m_visualTip->setPos(0, 0);
          double scale = 2.0;
          m_animation->setScaleAt(.5, scale, scale);
-         m_animation->setPosAt(.5, QPointF(clip->rect().x() - clip->rect().x() * scale, clip->rect().y() - clip->rect().y() * scale));
+         m_animation->setPosAt(.5, QPointF(clip->rect().x() - clip->rect().x() * scale -  clip->fadeIn() * m_scale, clip->rect().y() - clip->rect().y() * scale));
          scale = 1.0;
          m_animation->setScaleAt(1, scale, scale);
          m_animation->setPosAt(1, QPointF(clip->rect().x() - clip->rect().x() * scale, clip->rect().y() - clip->rect().y() * scale));
@@ -207,7 +226,7 @@ if (m_operationMode == MOVE) {
       }
       else if (opMode == FADEOUT) {
        if (m_visualTip == NULL) {
-         m_visualTip = new QGraphicsEllipseItem(clip->rect().x() + clip->rect().width() - size, clip->rect().y() - 8, size*2, 16);
+         m_visualTip = new QGraphicsEllipseItem(clip->rect().x() + clip->rect().width() - clip->fadeOut() * m_scale - size, clip->rect().y() - 8, size*2, 16);
          ((QGraphicsEllipseItem*) m_visualTip)->setBrush(m_tipColor);
          ((QGraphicsEllipseItem*) m_visualTip)->setPen(QPen(Qt::transparent));
          m_visualTip->setZValue (100);
@@ -217,7 +236,7 @@ if (m_operationMode == MOVE) {
          m_visualTip->setPos(0, 0);
          double scale = 2.0;
          m_animation->setScaleAt(.5, scale, scale);      
-         m_animation->setPosAt(.5, QPointF(clip->rect().x() - clip->rect().x() * scale - clip->rect().width(), clip->rect().y() - clip->rect().y() * scale));
+         m_animation->setPosAt(.5, QPointF(clip->rect().x() - clip->rect().x() * scale - clip->rect().width() + clip->fadeOut() * m_scale, clip->rect().y() - clip->rect().y() * scale));
          scale = 1.0;
          m_animation->setScaleAt(1, scale, scale);
          m_animation->setPosAt(1, QPointF(clip->rect().x() - clip->rect().x() * scale, clip->rect().y() - clip->rect().y() * scale));
@@ -244,35 +263,31 @@ if (m_operationMode == MOVE) {
 // virtual 
 void CustomTrackView::mousePressEvent ( QMouseEvent * event )
 {
+  kDebug()<<"-- TIMELINE MSE PRESSED";
   int pos = event->x();
   if (event->modifiers() == Qt::ControlModifier) 
     setDragMode(QGraphicsView::ScrollHandDrag);
   else if (event->modifiers() == Qt::ShiftModifier) 
     setDragMode(QGraphicsView::RubberBandDrag);
   else {
-    QGraphicsItem * item = itemAt(event->pos());
-    if (item && item->type() != 70000) item = item->parentItem();
-    if (item && item->type() == 70000) {
-      m_dragItem = (ClipItem *) item;
-      m_clickPoint = mapToScene(event->pos()).x() - m_dragItem->startPos() * m_scale;
-      m_operationMode = m_dragItem->operationMode(item->mapFromScene(mapToScene(event->pos())));
-      if (m_operationMode == MOVE || m_operationMode == RESIZESTART) m_startPos = QPointF(m_dragItem->startPos(), m_dragItem->track());
-      else if (m_operationMode == RESIZEEND) m_startPos = QPointF(m_dragItem->startPos() + m_dragItem->duration(), m_dragItem->track());
-
-     kDebug()<<"//////// ITEM CLICKED: "<<m_startPos;
-      /*while (item->parentItem()) 
-       item = item->parentItem();
-
-       int cursorPos = event->x();
-       QRectF itemRect = item->sceneBoundingRect();
-       int itemStart = mapFromScene(itemRect.x(), 0).x();
-       int itemEnd = mapFromScene(itemRect.x() + itemRect.width(), 0).x();
-       if (abs(itemStart - cursorPos) < 6)
-         ((ClipItem *) item )->setResizeMode(1);
-       else if (abs(itemEnd - cursorPos) < 6)
-         ((ClipItem *) item )->setResizeMode(2);
-    */}
-    else {
+    bool collision = false;
+    QList<QGraphicsItem *> collisionList = items(event->pos());
+    for (int i = 0; i < collisionList.size(); ++i) {
+      QGraphicsItem *item = collisionList.at(i);
+      if (item->type() == 70000) {
+       m_dragItem = (ClipItem *) item;
+       m_clickPoint = mapToScene(event->pos()).x() - m_dragItem->startPos() * m_scale;
+       m_operationMode = m_dragItem->operationMode(item->mapFromScene(mapToScene(event->pos())), m_scale);
+       if (m_operationMode == MOVE || m_operationMode == RESIZESTART) 
+         m_startPos = QPointF(m_dragItem->startPos(), m_dragItem->track());
+       else if (m_operationMode == RESIZEEND) 
+         m_startPos = QPointF(m_dragItem->endPos(), m_dragItem->track());
+       kDebug()<<"//////// ITEM CLICKED: "<<m_startPos;
+       collision = true;
+       break;
+      }
+    }
+    if (!collision) {
       kDebug()<<"//////// NO ITEM FOUND ON CLICK";
       m_dragItem = NULL;
       setCursor(Qt::ArrowCursor);
@@ -280,6 +295,7 @@ void CustomTrackView::mousePressEvent ( QMouseEvent * event )
       emit cursorMoved(cursorPos());
     }
   }
+  updateSnapPoints(m_dragItem);
   //kDebug()<<pos;
   QGraphicsView::mousePressEvent(event);
 }
@@ -300,11 +316,11 @@ void CustomTrackView::addItem(QString producer, QPoint pos)
   doc.setContent(producer);
   QDomElement elem = doc.documentElement();
   int in = elem.attribute("in", 0).toInt();
-  int out = elem.attribute("duration", 0).toInt();
-  if (out == 0) out = elem.attribute("out", 0).toInt() - in;
+  int out = elem.attribute("out", 0).toInt() - in;
+  if (out == 0) out = elem.attribute("duration", 0).toInt();
   kDebug()<<"ADDING CLIP: "<<producer<<", OUT: "<<out<<", POS: "<<mapToScene(pos);
   int trackTop = ((int) mapToScene(pos).y()/50) * 50 + 1;
-  m_dropItem = new ClipItem(elem, ((int) mapToScene(pos).y()/50), in, QRectF(mapToScene(pos).x() * m_scale, trackTop, out * m_scale, 49));
+  m_dropItem = new ClipItem(elem, ((int) mapToScene(pos).y()/50), in, QRectF(mapToScene(pos).x() * m_scale, trackTop, out * m_scale, 49), out);
   scene()->addItem(m_dropItem);
 }
 
@@ -314,7 +330,7 @@ void CustomTrackView::dragMoveEvent(QDragMoveEvent * event) {
   if (m_dropItem) {
     int track = (int) mapToScene(event->pos()).y()/50; //) * (m_scale * 50) + m_scale;
      kDebug()<<"+++++++++++++   DRAG MOVE, : "<<mapToScene(event->pos()).x()<<", SCAL: "<<m_scale;
-    m_dropItem->moveTo(mapToScene(event->pos()).x(), m_scale, (track - m_dropItem->track()) * 50, track);
+    m_dropItem->moveTo(mapToScene(event->pos()).x() / m_scale, m_scale, (track - m_dropItem->track()) * 50, track);
   }
        //if (item) {
   event->setDropAction(Qt::MoveAction);
@@ -335,6 +351,8 @@ void CustomTrackView::dropEvent ( QDropEvent * event ) {
   if (m_dropItem) {
     AddTimelineClipCommand *command = new AddTimelineClipCommand(this, m_dropItem->xml(), m_dropItem->track(), m_dropItem->startPos(), m_dropItem->rect(), m_dropItem->duration(), false);
     m_commandStack->push(command);
+    kDebug()<<"IIIIIIIIIIIIIIIIIIIIIIII TRAX CNT: "<<m_tracksCount<<", DROP: "<<m_dropItem->track();
+    m_document->renderer()->mltInsertClip(m_tracksCount - m_dropItem->track(), GenTime(m_dropItem->startPos(), 25), m_dropItem->xml());
   }
   m_dropItem = NULL;
 }
@@ -377,10 +395,12 @@ void CustomTrackView::removeTrack ()
   m_cursorLine->setLine(m_cursorLine->line().x1(), 0, m_cursorLine->line().x1(), 50 * m_tracksCount);
 }
 
-void CustomTrackView::setCursorPos(int pos)
+void CustomTrackView::setCursorPos(int pos, bool seek)
 {
   m_cursorPos = pos;
   m_cursorLine->setPos(pos, 0);
+  int frame = mapToScene(QPoint(pos, 0)).x() / m_scale;
+  if (seek) m_document->renderer()->seek(GenTime(frame, 25));
 }
 
 int CustomTrackView::cursorPos()
@@ -398,21 +418,23 @@ void CustomTrackView::mouseReleaseEvent ( QMouseEvent * event )
     // move clip
     MoveClipCommand *command = new MoveClipCommand(this, m_startPos, QPointF(m_dragItem->startPos(), m_dragItem->track()), false);
     m_commandStack->push(command);
+    m_document->renderer()->mltMoveClip(m_tracksCount - m_startPos.y(), m_tracksCount - m_dragItem->track(), m_startPos.x(), m_dragItem->startPos());
   }
   else if (m_operationMode == RESIZESTART) {
     // resize start
-    ResizeClipCommand *command = new ResizeClipCommand(this, m_startPos, QPointF(m_dragItem->rect().x(), m_dragItem->rect().y()), true, false);
+    ResizeClipCommand *command = new ResizeClipCommand(this, m_startPos, QPointF(m_dragItem->startPos(), m_dragItem->track()), true, false);
     m_commandStack->push(command);
   }
   else if (m_operationMode == RESIZEEND) {
     // resize end
-    ResizeClipCommand *command = new ResizeClipCommand(this, m_startPos, QPointF(m_dragItem->rect().x() + m_dragItem->rect().width(), m_dragItem->rect().y()), false, false);
+    ResizeClipCommand *command = new ResizeClipCommand(this, m_startPos, QPointF(m_dragItem->endPos(), m_dragItem->track()), false, false);
     m_commandStack->push(command);
   }
+  m_operationMode = NONE;
   m_dragItem = NULL; 
 }
 
-void CustomTrackView::deleteClip ( const QRectF &rect )
+void CustomTrackView::deleteClip (int track, int startpos, const QRectF &rect )
 {
   ClipItem *item = (ClipItem *) scene()->itemAt(rect.x() + 1, rect.y() + 1);
   if (!item) {
@@ -420,6 +442,7 @@ void CustomTrackView::deleteClip ( const QRectF &rect )
     return;
   }
   delete item;
+  m_document->renderer()->mltRemoveClip(m_tracksCount - track, GenTime(startpos, 25));
 }
 
 void CustomTrackView::addClip ( QDomElement xml, int track, int startpos, const QRectF &rect, int duration )
@@ -427,6 +450,7 @@ void CustomTrackView::addClip ( QDomElement xml, int track, int startpos, const
   QRect r(startpos * m_scale, 50 * track, duration * m_scale, 49); 
   ClipItem *item = new ClipItem(xml, track, startpos, r, duration);
   scene()->addItem(item);
+  m_document->renderer()->mltInsertClip(m_tracksCount - track, GenTime(startpos, 25), xml);
 }
 
 void CustomTrackView::moveClip ( const QPointF &startPos, const QPointF &endPos )
@@ -437,12 +461,8 @@ void CustomTrackView::moveClip ( const QPointF &startPos, const QPointF &endPos
     return;
   }
   kDebug()<<"----------------  Move CLIP FROM: "<<startPos.x()<<", END:"<<endPos.x();
-  //item->setRect(QRectF(endPos.x() * m_scale, endPos.y() * 50, item->rect().width(), item->rect().height()));
-  item->moveTo(endPos.x() * m_scale, m_scale, (endPos.y() - startPos.y()) * 50, endPos.y());
-  /*QList <QGraphicsItem *> childrenList = item->children();
-  for (int i = 0; i < childrenList.size(); ++i) {
-    childrenList.at(i)->moveBy((endPos.x() - startPos.x()) * m_scale , (endPos.y() - startPos.y()) * 50);
-  }*/
+  item->moveTo(endPos.x(), m_scale, (endPos.y() - startPos.y()) * 50, endPos.y());
+  m_document->renderer()->mltMoveClip(m_tracksCount - startPos.y(), m_tracksCount - endPos.y(), startPos.x(), endPos.x());
 }
 
 void CustomTrackView::resizeClip ( const QPointF &startPos, const QPointF &endPos, bool resizeClipStart )
@@ -450,27 +470,62 @@ void CustomTrackView::resizeClip ( const QPointF &startPos, const QPointF &endPo
   int offset;
   if (resizeClipStart) offset = 1;
   else offset = -1;
-  ClipItem *item = (ClipItem *) scene()->itemAt(startPos.x() + offset, startPos.y() + 1);
+  ClipItem *item = (ClipItem *) scene()->itemAt((startPos.x() + offset) * m_scale, startPos.y() * 50 + 25);
   if (!item) {
     kDebug()<<"----------------  ERROR, CANNOT find clip to resize at: "<<startPos;
     return;
   }
   qreal diff = endPos.x() - startPos.x();
   if (resizeClipStart) {
-    item->setRect(QRectF(endPos.x(), endPos.y(), item->rect().width() - diff, item->rect().height()));
-    QList <QGraphicsItem *> childrenList = item->QGraphicsItem::children();
-    for (int i = 0; i < childrenList.size(); ++i) {
-      childrenList.at(i)->moveBy(diff / 2 , endPos.y() - startPos.y());
-    }
+    item->resizeStart(endPos.x(), m_scale);
   }
   else {
-    //kdDebug()<<"///////  RESIZE CLIP END: "<<item->rect().x()<<", "<<item->rect().width()<<", "<<startPos<<", "<<endPos;
-    item->setRect(QRectF(item->rect().x(), item->rect().y(), endPos.x() - item->rect().x(), item->rect().height()));
-    QList <QGraphicsItem *> childrenList = item->QGraphicsItem::children();
-    for (int i = 0; i < childrenList.size(); ++i) {
-      childrenList.at(i)->moveBy(-diff/2, endPos.y() - startPos.y());
+    item->resizeEnd(endPos.x(), m_scale);
+  }
+}
+
+double CustomTrackView::getSnapPointForPos(double pos)
+{
+  for (int i = 0; i < m_snapPoints.size(); ++i) {
+    //kDebug()<<"SNAP POINT: "<<m_snapPoints.at(i);
+    if (abs(pos - m_snapPoints.at(i) * m_scale) < 6 * m_scale) {
+      //kDebug()<<" FOUND SNAP POINT AT: "<<m_snapPoints.at(i)<<", current pos: "<<pos / m_scale;
+      return m_snapPoints.at(i) * m_scale + 0.5;
     }
+    if (m_snapPoints.at(i) > pos) break;
   }
+  return pos;
+}
+
+void CustomTrackView::updateSnapPoints(ClipItem *selected)
+{
+  m_snapPoints.clear();
+  int offset = 0;
+  if (selected) offset = selected->duration();
+  QList<QGraphicsItem *> itemList = items();
+  for (int i = 0; i < itemList.count(); i++) {
+    if (itemList.at(i)->type() == 70000 && itemList.at(i) != selected) {
+      ClipItem *item = (ClipItem *)itemList.at(i);
+      int start = item->startPos();
+      int fadein = item->fadeIn() + start;
+      int end = item->endPos();
+      int fadeout = end - item->fadeOut();
+      m_snapPoints.append(start);
+      if (fadein != start) m_snapPoints.append(fadein);
+      m_snapPoints.append(end);
+      if (fadeout != end) m_snapPoints.append(fadeout);
+      if (offset != 0) {
+       m_snapPoints.append(start - offset);
+       if (fadein != start) m_snapPoints.append(fadein - offset);
+       m_snapPoints.append(end - offset);
+       if (fadeout != end) m_snapPoints.append(fadeout - offset);
+      }
+    }
+  }
+  kDebug()<<" GOT SNAPPOINTS TOTAL: "<<m_snapPoints.count();
+  qSort(m_snapPoints);
+  for (int i = 0; i < m_snapPoints.size(); ++i)
+    kDebug()<<"SNAP POINT: "<<m_snapPoints.at(i);
 }
 
 
@@ -486,27 +541,27 @@ void CustomTrackView::setScale(double scaleFactor)
        ClipItem *clip = (ClipItem *)itemList.at(i);
        clip->setRect(clip->startPos() * m_scale, clip->rect().y(), clip->duration() * m_scale, clip->rect().height());
       }
-      else if (itemList.at(i)->type() == 70001) {
+      /*else if (itemList.at(i)->type() == 70001) {
        LabelItem *label = (LabelItem *)itemList.at(i);
        QGraphicsItem *parent = label->parentItem();
        QRectF r = label->boundingRect();
        QRectF p = parent->boundingRect();
        label->setPos(p.x() + p.width() / 2 - r.width() / 2, p.y() + p.height() / 2 - r.height() / 2);
        //label->setRect(clip->startPos() * m_scale, clip->rect().y(), clip->duration() * m_scale, clip->rect().height());
-      }
+      }*/
     }
 }
 
 void CustomTrackView::drawBackground ( QPainter * painter, const QRectF & rect )  
 {
   QColor base = palette().button().color();
-  painter->setPen(base);
+  //painter->setPen(base);
   painter->setClipRect(rect);
   painter->drawLine(0, 0, rect.width(), 0);
     for (uint i = 0; i < m_tracksCount;i++)
     {
-    painter->drawLine(0, 50 * (i+1), width(), 50 * (i+1));
-      //painter->drawText(QRectF(10, 50 * i, 100, 50 * i + 49), Qt::AlignLeft, i18n(" Track ") + QString::number(i));
+      painter->drawLine(0, 50 * (i+1), width(), 50 * (i+1));
+      painter->drawText(QRectF(10, 50 * i, 100, 50 * i + 49), Qt::AlignLeft, i18n(" Track ") + QString::number(i + 1));
     }
   int lowerLimit = 50 * m_tracksCount;
   if (height() > lowerLimit)