]> git.sesse.net Git - kdenlive/blobdiff - src/customtrackview.cpp
clips now respect maximum length
[kdenlive] / src / customtrackview.cpp
index 376be5c7eda17bec7f101bc8cc6902da036f89f6..93aed657b763566a2a4b4c77155d1943977f67f2 100644 (file)
 #include <QDomDocument>
 
 #include <KDebug>
+#include <KLocale>
 #include <KUrl>
 
 #include "customtrackview.h"
 #include "clipitem.h"
 #include "definitions.h"
+#include "moveclipcommand.h"
+#include "resizeclipcommand.h"
+#include "addtimelineclipcommand.h"
 
-CustomTrackView::CustomTrackView(QGraphicsScene * scene, QWidget *parent)
-    : QGraphicsView(scene, parent), m_tracksCount(0), m_cursorPos(0), m_dropItem(NULL)
+CustomTrackView::CustomTrackView(KUndoStack *commandStack, QGraphicsScene * scene, QWidget *parent)
+    : QGraphicsView(scene, parent), m_commandStack(commandStack), m_tracksCount(0), m_cursorPos(0), m_dropItem(NULL), m_cursorLine(NULL), m_operationMode(0), m_startPos(QPointF()), m_dragItem(NULL)
 {
   setMouseTracking(true);
   setAcceptDrops(true);
 }
 
+void CustomTrackView::initView()
+{
+  m_cursorLine = scene()->addLine(0, 0, 0, height());
+}
+
+// virtual
+void CustomTrackView::resizeEvent ( QResizeEvent * event )
+{
+  if (m_cursorLine) m_cursorLine->setLine(m_cursorLine->line().x1(), 0, m_cursorLine->line().x1(), height());
+}
+
 // virtual 
 void CustomTrackView::mouseMoveEvent ( QMouseEvent * event )
 {
@@ -45,10 +60,16 @@ void CustomTrackView::mouseMoveEvent ( QMouseEvent * event )
   else if (event->modifiers() == Qt::ShiftModifier) 
     setDragMode(QGraphicsView::RubberBandDrag);
   else {
-    QGraphicsItem * item = itemAt(event->pos());
+    QList<QGraphicsItem *> itemList = items ( event->pos());
+    int i = 0;
+    QGraphicsItem *item = NULL;
+    for (int i = 0; i < itemList.count(); i++) {
+      if (itemList.at(i)->type() == 70000) {
+       item = itemList.at(i);
+       break;
+      }
+    }
     if (item) {
-      while (item->parentItem()) 
-       item = item->parentItem();
       int cursorPos = event->x();
       QRectF itemRect = item->sceneBoundingRect();
       int itemStart = mapFromScene(itemRect.x(), 0).x();
@@ -75,7 +96,14 @@ void CustomTrackView::mousePressEvent ( QMouseEvent * event )
     setDragMode(QGraphicsView::RubberBandDrag);
   else {
     QGraphicsItem * item = itemAt(event->pos());
-    if (item) {
+    if (item && item->type() != 70000) item = item->parentItem();
+    if (item && item->type() == 70000) {
+      m_dragItem = (ClipItem *) item;
+      m_operationMode = m_dragItem->operationMode(item->mapFromScene(mapToScene(event->pos())));
+      if (m_operationMode != 2) m_startPos = QPointF(m_dragItem->rect().x(), m_dragItem->rect().y());
+      else 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();
 
@@ -89,6 +117,8 @@ void CustomTrackView::mousePressEvent ( QMouseEvent * event )
          ((ClipItem *) item )->setResizeMode(2);
     */}
     else {
+      kDebug()<<"//////// NO ITEM FOUND ON CLICK";
+      m_dragItem = NULL;
       setCursor(Qt::ArrowCursor);
       emit cursorMoved((int) mapToScene(event->x(), 0).x());
     }
@@ -119,7 +149,7 @@ void CustomTrackView::addItem(QString producer, QPoint pos)
   int trackTop = ((int) mapToScene(pos).y()/50) * 50 + 1;
   QString clipName = elem.attribute("name");
   if (clipName.isEmpty()) clipName = KUrl(elem.attribute("resource")).fileName();
-  m_dropItem = new ClipItem(elem.attribute("type").toInt(), clipName, elem.attribute("id").toInt(), QRectF(mapToScene(pos).x(), trackTop, out, 49));
+  m_dropItem = new ClipItem(elem.attribute("type").toInt(), clipName, elem.attribute("id").toInt(), out, QRectF(mapToScene(pos).x(), trackTop, out, 49));
   scene()->addItem(m_dropItem);
 }
 
@@ -139,11 +169,17 @@ void CustomTrackView::dragMoveEvent(QDragMoveEvent * event) {
 }
 
 void CustomTrackView::dragLeaveEvent ( QDragLeaveEvent * event ) {
-  if (m_dropItem) delete m_dropItem;
-  m_dropItem = NULL;
+  if (m_dropItem) {
+    delete m_dropItem;
+    m_dropItem = NULL;
+  }
 }
 
 void CustomTrackView::dropEvent ( QDropEvent * event ) {
+  if (m_dropItem) {
+    AddTimelineClipCommand *command = new AddTimelineClipCommand(this, m_dropItem->clipType(), m_dropItem->clipName(), m_dropItem->clipProducer(), m_dropItem->maxDuration(), m_dropItem->rect(), false);
+    m_commandStack->push(command);
+  }
   m_dropItem = NULL;
 }
 
@@ -175,6 +211,7 @@ void CustomTrackView::removeTrack ()
 void CustomTrackView::setCursorPos(int pos)
 {
   m_cursorPos = pos;
+  m_cursorLine->setPos(pos, 0);
 }
 
 int CustomTrackView::cursorPos()
@@ -186,22 +223,100 @@ void CustomTrackView::mouseReleaseEvent ( QMouseEvent * event )
 {
   QGraphicsView::mouseReleaseEvent(event);
   setDragMode(QGraphicsView::NoDrag);
+  if (m_dragItem == NULL) return;
+  //kDebug()<<"/// MOVING CLIP: "<<m_startPos<<", END: "<<QPoint(m_dragItem->rect().x(),m_dragItem->rect().y());
+  if (m_operationMode == 0) {
+    // move clip
+    MoveClipCommand *command = new MoveClipCommand(this, m_startPos, QPointF(m_dragItem->rect().x(), m_dragItem->rect().y()), false);
+    m_commandStack->push(command);
+  }
+  else if (m_operationMode == 1) {
+    // resize start
+    ResizeClipCommand *command = new ResizeClipCommand(this, m_startPos, QPointF(m_dragItem->rect().x(), m_dragItem->rect().y()), true, false);
+    m_commandStack->push(command);
+  }
+  else if (m_operationMode == 2) {
+    // resize end
+    ResizeClipCommand *command = new ResizeClipCommand(this, m_startPos, QPointF(m_dragItem->rect().x() + m_dragItem->rect().width(), m_dragItem->rect().y()), false, false);
+    m_commandStack->push(command);
+  }
 }
 
+void CustomTrackView::deleteClip ( const QRectF &rect )
+{
+  ClipItem *item = (ClipItem *) scene()->itemAt(rect.x() + 1, rect.y() + 1);
+  if (!item) {
+    kDebug()<<"----------------  ERROR, CANNOT find clip to move at: "<<rect.x();
+    return;
+  }
+  delete item;
+}
+
+void CustomTrackView::addClip ( int clipType, QString clipName, int clipProducer, int maxDuration, const QRectF &rect )
+{
+  ClipItem *item = new ClipItem(clipType, clipName, clipProducer, maxDuration, rect);
+  scene()->addItem(item);
+}
+
+void CustomTrackView::moveClip ( const QPointF &startPos, const QPointF &endPos )
+{
+  ClipItem *item = (ClipItem *) scene()->itemAt(startPos.x() + 1, startPos.y() + 1);
+  if (!item) {
+    kDebug()<<"----------------  ERROR, CANNOT find clip to move at: "<<startPos;
+    return;
+  }
+  item->setRect(QRectF(endPos.x(), endPos.y(), item->rect().width(), item->rect().height()));
+  QList <QGraphicsItem *> childrenList = item->children();
+  for (int i = 0; i < childrenList.size(); ++i) {
+    childrenList.at(i)->moveBy(endPos.x() - startPos.x() , endPos.y() - startPos.y());
+  }
+}
+
+void CustomTrackView::resizeClip ( const QPointF &startPos, const QPointF &endPos, bool resizeClipStart )
+{
+  int offset;
+  if (resizeClipStart) offset = 1;
+  else offset = -1;
+  ClipItem *item = (ClipItem *) scene()->itemAt(startPos.x() + offset, startPos.y() + 1);
+  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->children();
+    for (int i = 0; i < childrenList.size(); ++i) {
+      childrenList.at(i)->moveBy(diff / 2 , endPos.y() - startPos.y());
+    }
+  }
+  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->children();
+    for (int i = 0; i < childrenList.size(); ++i) {
+      childrenList.at(i)->moveBy(-diff/2, endPos.y() - startPos.y());
+    }
+  }
+}
+
+
 void CustomTrackView::drawBackground ( QPainter * painter, const QRectF & rect )  
 {
   //kDebug()<<"/////  DRAWING BG: "<<rect.x()<<", width: "<<rect.width();
-  painter->drawRect(rect);
+  painter->drawLine(rect.x(), 0, rect.x() + rect.width(), 0);
     for (uint i = 0; i < m_tracksCount;i++)
     {
-      painter->drawLine(rect.x(), 50 * i, rect.x() + rect.width(), 50 * i);
+      painter->drawLine(rect.x(), 50 * (i+1), rect.x() + rect.width(), 50 * (i+1));
+      painter->drawText(QRectF(10, 50 * i, 100, 50 * i + 49), Qt::AlignLeft, i18n(" Track ") + QString::number(i));
     }
 }
-
+/*
 void CustomTrackView::drawForeground ( QPainter * painter, const QRectF & rect )  
 {
   //kDebug()<<"/////  DRAWING FB: "<<rect.x()<<", width: "<<rect.width();
+  painter->fillRect(rect, QColor(50, rand() % 250,50,100));
   painter->drawLine(m_cursorPos, rect.y(), m_cursorPos, rect.y() + rect.height());
 }
-
+*/
 #include "customtrackview.moc"