]> git.sesse.net Git - kdenlive/blobdiff - src/customtrackview.cpp
Display mouse timecode position in statusbar
[kdenlive] / src / customtrackview.cpp
index d92fcd5427d0630659fbff5f2fa490e1f9f97b21..1ca8a722a49f8771d86edc5d94fe33b04011eb8a 100644 (file)
@@ -81,6 +81,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 +90,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) {
+         int snappedPos = getSnapPointForPos(mapToScene(event->pos()).x() - m_clickPoint);
+         int 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 +141,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 +203,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 +213,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 +224,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 +234,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));
@@ -245,34 +262,28 @@ if (m_operationMode == MOVE) {
 void CustomTrackView::mousePressEvent ( QMouseEvent * event )
 {
   int pos = event->x();
+  updateSnapPoints();
   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->rect().x() + m_dragItem->rect().width(), m_dragItem->rect().y());
-
-     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);
@@ -300,11 +311,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 +325,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);
@@ -401,14 +412,15 @@ void CustomTrackView::mouseReleaseEvent ( QMouseEvent * event )
   }
   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; 
 }
 
@@ -437,12 +449,7 @@ 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());
 }
 
 void CustomTrackView::resizeClip ( const QPointF &startPos, const QPointF &endPos, bool resizeClipStart )
@@ -450,27 +457,43 @@ 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) {
+    if (abs(pos - m_snapPoints.at(i) * m_scale) < 6) return m_snapPoints.at(i) * m_scale;
+    if (m_snapPoints.at(i) > pos) break;
+  }
+  return pos;
+}
+
+void CustomTrackView::updateSnapPoints()
+{
+  m_snapPoints.clear();
+  QList<QGraphicsItem *> itemList = items();
+  for (int i = 0; i < itemList.count(); i++) {
+    if (itemList.at(i)->type() == 70000) {
+      ClipItem *item = (ClipItem *)itemList.at(i);
+      m_snapPoints.append(item->startPos());
+      if (item->fadeIn() != 0) m_snapPoints.append(item->startPos() + item->fadeIn());
+      m_snapPoints.append(item->endPos());
+      if (item->fadeOut() != 0) m_snapPoints.append(item->endPos() - item->fadeOut());      
     }
   }
+  qSort(m_snapPoints);
 }
 
 
@@ -486,14 +509,14 @@ 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());
-      }
+      }*/
     }
 }