X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=src%2Fcustomtrackview.cpp;h=25228bc7257164d3e62ebcfe4752929000f9ce41;hb=0cbb07741ba0231769020d4370ee56dbd9ae3519;hp=1217d1ab5e32d7ecc3eb4477f8f2324cc69a7221;hpb=19d0cd870d3a0026e30bbe024ea860794b9a38c0;p=kdenlive diff --git a/src/customtrackview.cpp b/src/customtrackview.cpp index 1217d1ab..25228bc7 100644 --- a/src/customtrackview.cpp +++ b/src/customtrackview.cpp @@ -22,10 +22,13 @@ #include #include #include +#include +#include #include #include #include +#include #include #include "customtrackview.h" @@ -33,18 +36,31 @@ #include "clipitem.h" #include "definitions.h" #include "moveclipcommand.h" +#include "movetransitioncommand.h" #include "resizeclipcommand.h" +#include "editguidecommand.h" #include "addtimelineclipcommand.h" #include "addeffectcommand.h" #include "editeffectcommand.h" +#include "moveeffectcommand.h" #include "addtransitioncommand.h" #include "edittransitioncommand.h" +#include "editkeyframecommand.h" +#include "changespeedcommand.h" +#include "addmarkercommand.h" +#include "razorclipcommand.h" #include "kdenlivesettings.h" #include "transition.h" #include "clipitem.h" #include "customtrackview.h" #include "clipmanager.h" #include "renderer.h" +#include "markerdialog.h" +#include "mainwindow.h" +#include "ui_keyframedialog_ui.h" +#include "clipdurationdialog.h" + + //TODO: // disable animation if user asked it in KDE's global settings // http://lists.kde.org/?l=kde-commits&m=120398724717624&w=2 @@ -54,7 +70,7 @@ // const int duration = animate ? 1500 : 1; CustomTrackView::CustomTrackView(KdenliveDoc *doc, QGraphicsScene * projectscene, QWidget *parent) - : QGraphicsView(projectscene, parent), 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(QPoint()), m_document(doc), m_autoScroll(KdenliveSettings::autoscroll()), m_tracksHeight(KdenliveSettings::trackheight()) { + : QGraphicsView(projectscene, parent), m_cursorPos(0), m_dropItem(NULL), m_cursorLine(NULL), m_operationMode(NONE), m_dragItem(NULL), m_visualTip(NULL), m_moveOpMode(NONE), m_animation(NULL), m_projectDuration(0), m_scale(1.0), m_clickPoint(QPoint()), m_document(doc), m_autoScroll(KdenliveSettings::autoscroll()), m_tracksHeight(KdenliveSettings::trackheight()), m_tool(SELECTTOOL), m_dragGuide(NULL), m_findIndex(0), m_menuPosition(QPoint()), m_blockRefresh(false) { if (doc) m_commandStack = doc->commandStack(); else m_commandStack == NULL; setMouseTracking(true); @@ -70,11 +86,21 @@ CustomTrackView::CustomTrackView(KdenliveDoc *doc, QGraphicsScene * projectscene setContentsMargins(0, 0, 0, 0); if (projectscene) { m_cursorLine = projectscene->addLine(0, 0, 0, m_tracksHeight); - m_cursorLine->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIgnoresTransformations); + m_cursorLine->setFlags(QGraphicsItem::ItemIsMovable); m_cursorLine->setZValue(1000); } + + KIcon razorIcon("edit-cut"); + m_razorCursor = QCursor(razorIcon.pixmap(22, 22)); + verticalScrollBar()->setTracking(true); + connect(verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(slotRefreshGuides())); } +CustomTrackView::~CustomTrackView() { + qDeleteAll(m_guides); +} + + void CustomTrackView::setContextMenu(QMenu *timeline, QMenu *clip, QMenu *transition) { m_timelineContextMenu = timeline; m_timelineContextClipMenu = clip; @@ -99,14 +125,23 @@ void CustomTrackView::checkTrackHeight() { for (int i = 0; i < itemList.count(); i++) { if (itemList.at(i)->type() == AVWIDGET) { item = (ClipItem*) itemList.at(i); - item->setRect(item->rect().x(), item->track() * m_tracksHeight, item->rect().width(), m_tracksHeight - 1); + item->setRect(0, 0, item->rect().width(), m_tracksHeight - 1); + item->setPos((qreal) item->startPos().frames(m_document->fps()) * m_scale, (qreal) item->track() * m_tracksHeight); item->resetThumbs(); } else if (itemList.at(i)->type() == TRANSITIONWIDGET) { transitionitem = (Transition*) itemList.at(i); - transitionitem->setRect(transitionitem->rect().x(), transitionitem->track() * m_tracksHeight + m_tracksHeight / 2, transitionitem->rect().width(), m_tracksHeight - 1); + transitionitem->setRect(0, 0, transitionitem->rect().width(), m_tracksHeight / 3 * 2 - 1); + transitionitem->setPos((qreal) transitionitem->startPos().frames(m_document->fps()) * m_scale, (qreal) transitionitem->track() * m_tracksHeight + m_tracksHeight / 3 * 2); } } m_cursorLine->setLine(m_cursorLine->line().x1(), 0, m_cursorLine->line().x1(), m_tracksHeight * m_tracksList.count()); + + for (int i = 0; i < m_guides.count(); i++) { + QLineF l = m_guides.at(i)->line(); + l.setP2(QPointF(l.x2(), m_tracksHeight * m_tracksList.count())); + m_guides.at(i)->setLine(l); + } + setSceneRect(0, 0, sceneRect().width(), m_tracksHeight * m_tracksList.count()); verticalScrollBar()->setMaximum(m_tracksHeight * m_tracksList.count()); update(); @@ -130,7 +165,6 @@ void CustomTrackView::wheelEvent(QWheelEvent * e) { int CustomTrackView::getPreviousVideoTrack(int track) { track = m_tracksList.count() - track - 1; - int videoTracksCount = 0; track --; for (int i = track; i > -1; i--) { if (m_tracksList.at(i).type == VIDEOTRACK) return i + 1; @@ -139,44 +173,64 @@ int CustomTrackView::getPreviousVideoTrack(int track) { } // virtual + void CustomTrackView::mouseMoveEvent(QMouseEvent * event) { int pos = event->x(); emit mousePosition((int)(mapToScene(event->pos()).x() / m_scale)); - /*if (event->modifiers() == Qt::ControlModifier) - setDragMode(QGraphicsView::ScrollHandDrag); - else if (event->modifiers() == Qt::ShiftModifier) - setDragMode(QGraphicsView::RubberBandDrag); - else*/ - { - - if (m_dragItem) { //event->button() == Qt::LeftButton) { + if (event->buttons() & Qt::MidButton) return; + if (event->buttons() != Qt::NoButton) { + if (m_dragItem && m_tool == SELECTTOOL) { //event->button() == Qt::LeftButton) { // a button was pressed, delete visual tips - if (m_operationMode == MOVE) { + if (m_operationMode == MOVE && (event->pos() - m_clickEvent).manhattanLength() >= QApplication::startDragDistance()) { double snappedPos = getSnapPointForPos(mapToScene(event->pos()).x() - m_clickPoint.x()); //kDebug() << "/////// MOVE CLIP, EVENT Y: "<scenePos().y()<<", SCENE HEIGHT: "<sceneRect().height(); - int moveTrack = (int) mapToScene(event->pos() + QPoint(0, (m_dragItem->type() == TRANSITIONWIDGET ? m_tracksHeight - m_clickPoint.y() : 0))).y() / m_tracksHeight; + int moveTrack = (int) mapToScene(event->pos() - QPoint(0, (m_dragItem->type() == TRANSITIONWIDGET ? m_dragItem->boundingRect().height() / 2 : 0))).y() / m_tracksHeight; int currentTrack = m_dragItem->track(); - if (moveTrack > m_tracksList.count() - 1) moveTrack = m_tracksList.count() - 1; + if (moveTrack > 1000) moveTrack = 0; + else if (moveTrack > m_tracksList.count() - 1) moveTrack = m_tracksList.count() - 1; else if (moveTrack < 0) moveTrack = 0; int offset = moveTrack - currentTrack; - if (offset != 0) offset = m_tracksHeight * offset; - m_dragItem->moveTo((int)(snappedPos / m_scale), m_scale, offset, moveTrack); + if (m_selectedClipList.count() == 1) m_dragItem->moveTo((int)(snappedPos / m_scale), m_scale, offset * m_tracksHeight, moveTrack); + else { + int moveOffset = (int)(snappedPos / m_scale) - m_dragItem->startPos().frames(m_document->fps()); + if (canBeMoved(m_selectedClipList, GenTime(moveOffset, m_document->fps()), offset)) { + for (int i = 0; i < m_selectedClipList.count(); i++) { + AbstractClipItem *item = m_selectedClipList.at(i); + item->moveTo(item->startPos().frames(m_document->fps()) + moveOffset, m_scale, offset * m_tracksHeight, item->track() + offset, false); + } + } + } + } else if (m_operationMode == RESIZESTART) { double snappedPos = getSnapPointForPos(mapToScene(event->pos()).x()); - m_dragItem->resizeStart(snappedPos / m_scale, m_scale); + m_dragItem->resizeStart((int)(snappedPos / m_scale), m_scale); } else if (m_operationMode == RESIZEEND) { double snappedPos = getSnapPointForPos(mapToScene(event->pos()).x()); - m_dragItem->resizeEnd(snappedPos / m_scale, m_scale); + m_dragItem->resizeEnd((int)(snappedPos / m_scale), m_scale); } else if (m_operationMode == FADEIN) { - int pos = mapToScene(event->pos()).x() / m_scale; - m_dragItem->setFadeIn(pos - m_dragItem->startPos().frames(m_document->fps()), m_scale); + int pos = (int)(mapToScene(event->pos()).x() / m_scale); + ((ClipItem*) m_dragItem)->setFadeIn((int)(pos - m_dragItem->startPos().frames(m_document->fps())), m_scale); } else if (m_operationMode == FADEOUT) { - int pos = mapToScene(event->pos()).x() / m_scale; - m_dragItem->setFadeOut(m_dragItem->endPos().frames(m_document->fps()) - pos, m_scale); + int pos = (int)(mapToScene(event->pos()).x() / m_scale); + ((ClipItem*) m_dragItem)->setFadeOut((int)(m_dragItem->endPos().frames(m_document->fps()) - pos), m_scale); + } else if (m_operationMode == KEYFRAME) { + GenTime keyFramePos = GenTime((int)(mapToScene(event->pos()).x() / m_scale), m_document->fps()) - m_dragItem->startPos() + m_dragItem->cropStart(); + double pos = mapToScene(event->pos()).toPoint().y(); + QRectF br = m_dragItem->sceneBoundingRect(); + double maxh = 100.0 / br.height(); + pos = (br.bottom() - pos) * maxh; + m_dragItem->updateKeyFramePos(keyFramePos, pos); } + if (m_animation) delete m_animation; + m_animation = NULL; + if (m_visualTip) delete m_visualTip; + m_visualTip = NULL; + QGraphicsView::mouseMoveEvent(event); + return; + } else if (m_operationMode == MOVEGUIDE) { if (m_animation) delete m_animation; m_animation = NULL; if (m_visualTip) delete m_visualTip; @@ -184,242 +238,335 @@ void CustomTrackView::mouseMoveEvent(QMouseEvent * event) { QGraphicsView::mouseMoveEvent(event); return; } + } + + if (m_tool == RAZORTOOL) { + setCursor(m_razorCursor); + //QGraphicsView::mouseMoveEvent(event); + //return; + } - QList itemList = items(event->pos()); - QGraphicsRectItem *item = NULL; - for (int i = 0; i < itemList.count(); i++) { + QList itemList = items(event->pos()); + QGraphicsRectItem *item = NULL; + OPERATIONTYPE opMode = NONE; + + if (itemList.count() == 1 && itemList.at(0)->type() == GUIDEITEM) { + opMode = MOVEGUIDE; + } else for (int i = 0; i < itemList.count(); i++) { if (itemList.at(i)->type() == AVWIDGET || itemList.at(i)->type() == TRANSITIONWIDGET) { item = (QGraphicsRectItem*) itemList.at(i); break; } } - if (item && event->buttons() == Qt::NoButton) { - AbstractClipItem *clip = (AbstractClipItem*) item; - OPERATIONTYPE opMode = opMode = clip->operationMode(mapToScene(event->pos()), m_scale); - double size = 8; - if (opMode == m_moveOpMode) { - QGraphicsView::mouseMoveEvent(event); - return; - } else { - if (m_visualTip) { - if (m_animation) delete m_animation; - m_animation = NULL; - m_animationTimer->stop(); - delete m_visualTip; - m_visualTip = NULL; - } - } - m_moveOpMode = opMode; - if (opMode == MOVE) { - setCursor(Qt::OpenHandCursor); - } else if (opMode == RESIZESTART) { - setCursor(KCursor("left_side", Qt::SizeHorCursor)); - kDebug() << "******** RESIZE CLIP START; WIDTH: " << size; - if (m_visualTip == NULL) { - QPolygon polygon; - polygon << QPoint(clip->rect().x(), clip->rect().y() + clip->rect().height() / 2 - size * 2); - polygon << QPoint(clip->rect().x() + size * 2, clip->rect().y() + clip->rect().height() / 2); - polygon << QPoint(clip->rect().x(), clip->rect().y() + clip->rect().height() / 2 + size * 2); - polygon << QPoint(clip->rect().x(), clip->rect().y() + clip->rect().height() / 2 - size * 2); - - m_visualTip = new QGraphicsPolygonItem(polygon); - ((QGraphicsPolygonItem*) m_visualTip)->setBrush(m_tipColor); - ((QGraphicsPolygonItem*) m_visualTip)->setPen(m_tipPen); - m_visualTip->setZValue(100); - m_animation = new QGraphicsItemAnimation; - m_animation->setItem(m_visualTip); - m_animation->setTimeLine(m_animationTimer); - m_visualTip->setPos(0, 0); - double scale = 2.0; - m_animation->setScaleAt(.5, scale, 1); - m_animation->setPosAt(.5, QPointF(clip->rect().x() - clip->rect().x() * scale, 0)); - scale = 1.0; - m_animation->setScaleAt(1, scale, 1); - m_animation->setPosAt(1, QPointF(clip->rect().x() - clip->rect().x() * scale, 0)); - scene()->addItem(m_visualTip); - m_animationTimer->start(); - } - } else if (opMode == RESIZEEND) { - setCursor(KCursor("right_side", Qt::SizeHorCursor)); - if (m_visualTip == NULL) { - QPolygon polygon; - polygon << QPoint(clip->rect().x() + clip->rect().width(), clip->rect().y() + clip->rect().height() / 2 - size * 2); - polygon << QPoint(clip->rect().x() + clip->rect().width() - size * 2, clip->rect().y() + clip->rect().height() / 2); - polygon << QPoint(clip->rect().x() + clip->rect().width(), clip->rect().y() + clip->rect().height() / 2 + size * 2); - polygon << QPoint(clip->rect().x() + clip->rect().width(), clip->rect().y() + clip->rect().height() / 2 - size * 2); - - m_visualTip = new QGraphicsPolygonItem(polygon); - ((QGraphicsPolygonItem*) m_visualTip)->setBrush(m_tipColor); - ((QGraphicsPolygonItem*) m_visualTip)->setPen(m_tipPen); - - m_visualTip->setZValue(100); - m_animation = new QGraphicsItemAnimation; - m_animation->setItem(m_visualTip); - m_animation->setTimeLine(m_animationTimer); - m_visualTip->setPos(0, 0); - double scale = 2.0; - m_animation->setScaleAt(.5, scale, 1); - m_animation->setPosAt(.5, QPointF(clip->rect().x() - clip->rect().x() * scale - clip->rect().width(), 0)); - scale = 1.0; - m_animation->setScaleAt(1, scale, 1); - m_animation->setPosAt(1, QPointF(clip->rect().x() - clip->rect().x() * scale, 0)); - scene()->addItem(m_visualTip); - m_animationTimer->start(); - } - } else if (opMode == FADEIN) { - if (m_visualTip == NULL) { - 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(m_tipPen); - m_visualTip->setZValue(100); - m_animation = new QGraphicsItemAnimation; - m_animation->setItem(m_visualTip); - m_animation->setTimeLine(m_animationTimer); - 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->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)); - scene()->addItem(m_visualTip); - m_animationTimer->start(); - } - setCursor(Qt::PointingHandCursor); - } else if (opMode == FADEOUT) { - if (m_visualTip == NULL) { - 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(m_tipPen); - m_visualTip->setZValue(100); - m_animation = new QGraphicsItemAnimation; - m_animation->setItem(m_visualTip); - m_animation->setTimeLine(m_animationTimer); - 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->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)); - scene()->addItem(m_visualTip); - m_animationTimer->start(); - } - setCursor(Qt::PointingHandCursor); - } else if (opMode == TRANSITIONSTART) { - if (m_visualTip == NULL) { - m_visualTip = new QGraphicsEllipseItem(-5, -5 , 10, 10); - ((QGraphicsEllipseItem*) m_visualTip)->setBrush(m_tipColor); - ((QGraphicsEllipseItem*) m_visualTip)->setPen(m_tipPen); - m_visualTip->setZValue(100); - m_animation = new QGraphicsItemAnimation; - m_animation->setItem(m_visualTip); - m_animation->setTimeLine(m_animationTimer); - m_visualTip->setPos(clip->rect().x() + 15, clip->rect().y() + clip->rect().height() / 2); - double scale = 2.0; - m_animation->setScaleAt(.5, scale, scale); - scale = 1.0; - m_animation->setScaleAt(1, scale, scale); - scene()->addItem(m_visualTip); - m_animationTimer->start(); - } - setCursor(Qt::PointingHandCursor); - } else if (opMode == TRANSITIONEND) { - if (m_visualTip == NULL) { - m_visualTip = new QGraphicsEllipseItem(-5, -5 , 10, 10); - ((QGraphicsEllipseItem*) m_visualTip)->setBrush(m_tipColor); - ((QGraphicsEllipseItem*) m_visualTip)->setPen(m_tipPen); - m_visualTip->setZValue(100); - m_animation = new QGraphicsItemAnimation; - m_animation->setItem(m_visualTip); - m_animation->setTimeLine(m_animationTimer); - m_visualTip->setPos(clip->rect().x() + clip->rect().width() - 15 , clip->rect().y() + clip->rect().height() / 2); - double scale = 2.0; - m_animation->setScaleAt(.5, scale, scale); - scale = 1.0; - m_animation->setScaleAt(1, scale, scale); - scene()->addItem(m_visualTip); - m_animationTimer->start(); - } - setCursor(Qt::PointingHandCursor); + if (item && event->buttons() == Qt::NoButton) { + AbstractClipItem *clip = static_cast (item); + if (m_tool == RAZORTOOL) { + // razor tool over a clip, display current frame in monitor + if (!m_blockRefresh && item->type() == AVWIDGET) { + //TODO: solve crash when showing frame when moving razor over clip + //emit showClipFrame(((ClipItem *) item)->baseClip(), mapToScene(event->pos()).x() / m_scale - (clip->startPos() - clip->cropStart()).frames(m_document->fps())); } + event->accept(); + return; + } + opMode = clip->operationMode(mapToScene(event->pos()), m_scale); + double size = 8; + if (opMode == m_moveOpMode) { + QGraphicsView::mouseMoveEvent(event); + return; } else { - m_moveOpMode = NONE; - if (event->buttons() != Qt::NoButton && event->modifiers() == Qt::NoModifier) { - setCursorPos((int) mapToScene(event->pos().x(), 0).x() / m_scale); - } if (m_visualTip) { if (m_animation) delete m_animation; - m_animationTimer->stop(); m_animation = NULL; + m_animationTimer->stop(); delete m_visualTip; m_visualTip = NULL; - } - setCursor(Qt::ArrowCursor); } + m_moveOpMode = opMode; + if (opMode == MOVE) { + setCursor(Qt::OpenHandCursor); + } else if (opMode == RESIZESTART) { + setCursor(KCursor("left_side", Qt::SizeHorCursor)); + if (m_visualTip == NULL) { + QRectF rect = clip->sceneBoundingRect(); + QPolygon polygon; + polygon << QPoint((int)rect.x(), (int)(rect.y() + rect.height() / 2 - size * 2)); + polygon << QPoint((int)(rect.x() + size * 2), (int)(rect.y() + rect.height() / 2)); + polygon << QPoint((int)rect.x(), (int)(rect.y() + rect.height() / 2 + size * 2)); + polygon << QPoint((int)rect.x(), (int)(rect.y() + rect.height() / 2 - size * 2)); + + m_visualTip = new QGraphicsPolygonItem(polygon); + ((QGraphicsPolygonItem*) m_visualTip)->setBrush(m_tipColor); + ((QGraphicsPolygonItem*) m_visualTip)->setPen(m_tipPen); + m_visualTip->setZValue(100); + m_animation = new QGraphicsItemAnimation; + m_animation->setItem(m_visualTip); + m_animation->setTimeLine(m_animationTimer); + m_visualTip->setPos(0, 0); + double scale = 2.0; + m_animation->setScaleAt(.5, scale, 1); + m_animation->setPosAt(.5, QPointF(rect.x() - rect.x() * scale, 0)); + scale = 1.0; + m_animation->setScaleAt(1, scale, 1); + m_animation->setPosAt(1, QPointF(rect.x() - rect.x() * scale, 0)); + scene()->addItem(m_visualTip); + m_animationTimer->start(); + } + } else if (opMode == RESIZEEND) { + setCursor(KCursor("right_side", Qt::SizeHorCursor)); + if (m_visualTip == NULL) { + QRectF rect = clip->sceneBoundingRect(); + QPolygon polygon; + polygon << QPoint((int)(rect.x() + rect.width()), (int)(rect.y() + rect.height() / 2 - size * 2)); + polygon << QPoint((int)(rect.x() + rect.width() - size * 2), (int)(rect.y() + rect.height() / 2)); + polygon << QPoint((int)(rect.x() + rect.width()), (int)(rect.y() + rect.height() / 2 + size * 2)); + polygon << QPoint((int)(rect.x() + rect.width()), (int)(rect.y() + rect.height() / 2 - size * 2)); + + m_visualTip = new QGraphicsPolygonItem(polygon); + ((QGraphicsPolygonItem*) m_visualTip)->setBrush(m_tipColor); + ((QGraphicsPolygonItem*) m_visualTip)->setPen(m_tipPen); + + m_visualTip->setZValue(100); + m_animation = new QGraphicsItemAnimation; + m_animation->setItem(m_visualTip); + m_animation->setTimeLine(m_animationTimer); + m_visualTip->setPos(0, 0); + double scale = 2.0; + m_animation->setScaleAt(.5, scale, 1); + m_animation->setPosAt(.5, QPointF(rect.x() - rect.x() * scale - rect.width(), 0)); + scale = 1.0; + m_animation->setScaleAt(1, scale, 1); + m_animation->setPosAt(1, QPointF(rect.x() - rect.x() * scale, 0)); + scene()->addItem(m_visualTip); + m_animationTimer->start(); + } + } else if (opMode == FADEIN) { + if (m_visualTip == NULL) { + ClipItem *item = (ClipItem *) clip; + m_visualTip = new QGraphicsEllipseItem(item->pos().x() + item->fadeIn() * m_scale - size, item->pos().y() - 8, size * 2, 16); + ((QGraphicsEllipseItem*) m_visualTip)->setBrush(m_tipColor); + ((QGraphicsEllipseItem*) m_visualTip)->setPen(m_tipPen); + m_visualTip->setZValue(100); + m_animation = new QGraphicsItemAnimation; + m_animation->setItem(m_visualTip); + m_animation->setTimeLine(m_animationTimer); + m_visualTip->setPos(0, 0); + double scale = 2.0; + m_animation->setScaleAt(.5, scale, scale); + m_animation->setPosAt(.5, QPointF(item->pos().x() - item->pos().x() * scale - item->fadeIn() * m_scale, item->pos().y() - item->pos().y() * scale)); + scale = 1.0; + m_animation->setScaleAt(1, scale, scale); + m_animation->setPosAt(1, QPointF(item->pos().x() - item->pos().x() * scale, item->pos().y() - item->pos().y() * scale)); + scene()->addItem(m_visualTip); + m_animationTimer->start(); + } + setCursor(Qt::PointingHandCursor); + } else if (opMode == FADEOUT) { + if (m_visualTip == NULL) { + ClipItem *item = (ClipItem *) clip; + m_visualTip = new QGraphicsEllipseItem(item->pos().x() + item->rect().width() - item->fadeOut() * m_scale - size, item->pos().y() - 8, size*2, 16); + ((QGraphicsEllipseItem*) m_visualTip)->setBrush(m_tipColor); + ((QGraphicsEllipseItem*) m_visualTip)->setPen(m_tipPen); + m_visualTip->setZValue(100); + m_animation = new QGraphicsItemAnimation; + m_animation->setItem(m_visualTip); + m_animation->setTimeLine(m_animationTimer); + m_visualTip->setPos(0, 0); + double scale = 2.0; + m_animation->setScaleAt(.5, scale, scale); + m_animation->setPosAt(.5, QPointF(item->pos().x() - item->pos().x() * scale - item->rect().width() + item->fadeOut() * m_scale, item->pos().y() - item->pos().y() * scale)); + scale = 1.0; + m_animation->setScaleAt(1, scale, scale); + m_animation->setPosAt(1, QPointF(item->pos().x() - item->pos().x() * scale, item->pos().y() - item->pos().y() * scale)); + scene()->addItem(m_visualTip); + m_animationTimer->start(); + } + setCursor(Qt::PointingHandCursor); + } else if (opMode == TRANSITIONSTART) { + if (m_visualTip == NULL) { + QRectF rect = clip->sceneBoundingRect(); + m_visualTip = new QGraphicsEllipseItem(-5, -5 , 10, 10); + ((QGraphicsEllipseItem*) m_visualTip)->setBrush(m_tipColor); + ((QGraphicsEllipseItem*) m_visualTip)->setPen(m_tipPen); + m_visualTip->setZValue(100); + m_animation = new QGraphicsItemAnimation; + m_animation->setItem(m_visualTip); + m_animation->setTimeLine(m_animationTimer); + m_visualTip->setPos(rect.x() + 10, rect.y() + rect.height() / 2 + 12); + double scale = 2.0; + m_animation->setScaleAt(.5, scale, scale); + scale = 1.0; + m_animation->setScaleAt(1, scale, scale); + scene()->addItem(m_visualTip); + m_animationTimer->start(); + } + setCursor(Qt::PointingHandCursor); + } else if (opMode == TRANSITIONEND) { + if (m_visualTip == NULL) { + QRectF rect = clip->sceneBoundingRect(); + m_visualTip = new QGraphicsEllipseItem(-5, -5 , 10, 10); + ((QGraphicsEllipseItem*) m_visualTip)->setBrush(m_tipColor); + ((QGraphicsEllipseItem*) m_visualTip)->setPen(m_tipPen); + m_visualTip->setZValue(100); + m_animation = new QGraphicsItemAnimation; + m_animation->setItem(m_visualTip); + m_animation->setTimeLine(m_animationTimer); + m_visualTip->setPos(rect.x() + rect.width() - 10 , rect.y() + rect.height() / 2 + 12); + double scale = 2.0; + m_animation->setScaleAt(.5, scale, scale); + scale = 1.0; + m_animation->setScaleAt(1, scale, scale); + scene()->addItem(m_visualTip); + m_animationTimer->start(); + } + setCursor(Qt::PointingHandCursor); + } else if (opMode == KEYFRAME) { + setCursor(Qt::PointingHandCursor); + } + } // no clip under mouse + else if (m_tool == RAZORTOOL) { + event->accept(); + return; + } else if (opMode == MOVEGUIDE) { + m_moveOpMode = opMode; + setCursor(Qt::SplitHCursor); + } else { + m_moveOpMode = NONE; + if (event->buttons() != Qt::NoButton && event->modifiers() == Qt::NoModifier) { + setCursorPos((int)(mapToScene(event->pos().x(), 0).x() / m_scale)); + } + if (m_visualTip) { + if (m_animation) delete m_animation; + m_animationTimer->stop(); + m_animation = NULL; + delete m_visualTip; + m_visualTip = NULL; + + } + setCursor(Qt::ArrowCursor); } QGraphicsView::mouseMoveEvent(event); } // virtual void CustomTrackView::mousePressEvent(QMouseEvent * event) { - activateMonitor(); - int pos = event->x(); - if (event->modifiers() == Qt::ControlModifier) { + m_menuPosition = QPoint(); + m_blockRefresh = true; + if (m_tool != RAZORTOOL) activateMonitor(); + m_clickEvent = event->pos(); + QList collisionList = items(event->pos()); + if (event->button() == Qt::MidButton) { + m_document->renderer()->switchPlay(); + m_blockRefresh = false; + return; + } + if (event->modifiers() == Qt::ControlModifier && collisionList.count() == 0) { setDragMode(QGraphicsView::ScrollHandDrag); QGraphicsView::mousePressEvent(event); + m_blockRefresh = false; return; - } else if (event->modifiers() == Qt::ShiftModifier) { + } else if (event->modifiers() == Qt::ShiftModifier && collisionList.count() == 0) { setDragMode(QGraphicsView::RubberBandDrag); QGraphicsView::mousePressEvent(event); + m_blockRefresh = false; return; } else { bool collision = false; - QList collisionList = items(event->pos()); - AbstractClipItem *clipItem = NULL, *transitionItem = NULL; - for (int i = 0; i < collisionList.size(); ++i) { - QGraphicsItem *item = collisionList.at(i); - if (item->type() == AVWIDGET || item->type() == TRANSITIONWIDGET) { - // select item - if (!item->isSelected()) { - - item->setSelected(true); - update(); - } - - m_dragItem = (AbstractClipItem *) item; - if (item->type() == AVWIDGET) { - clipItem = m_dragItem; - } else if (item->type() == TRANSITIONWIDGET) { - transitionItem = m_dragItem; - } - m_clickPoint = QPoint(mapToScene(event->pos()).x() - m_dragItem->startPos().frames(m_document->fps()) * m_scale, event->pos().y() - m_dragItem->rect().top()); - m_operationMode = m_dragItem->operationMode(item->mapFromScene(mapToScene(event->pos())), m_scale); - if (m_operationMode == MOVE) setCursor(Qt::ClosedHandCursor); - if (m_operationMode == MOVE || m_operationMode == RESIZESTART) - m_startPos = QPointF(m_dragItem->startPos().frames(m_document->fps()), m_dragItem->track()); - else if (m_operationMode == RESIZEEND) - m_startPos = QPointF(m_dragItem->endPos().frames(m_document->fps()), m_dragItem->track()); - else if (m_operationMode == TRANSITIONSTART) { - Transition *tr = new Transition( - QRect(m_dragItem->startPos().frames(m_document->fps()) *m_scale , m_dragItem->rect().y() + m_dragItem->rect().height() / 2, - GenTime(2.5).frames(m_document->fps()) *m_scale , m_dragItem->rect().height() - ), - (ClipItem*)m_dragItem, "luma" , m_dragItem->startPos(), m_dragItem->startPos() + GenTime(2.5), m_document->fps()); - tr->setTrack(m_dragItem->track()); - scene()->addItem(tr); - //m_dragItem->addTransition(tra); + if (collisionList.count() == 1 && collisionList.at(0)->type() == GUIDEITEM) { + // a guide item was pressed + collisionList.at(0)->setFlag(QGraphicsItem::ItemIsMovable, true); + m_dragItem = NULL; + m_dragGuide = (Guide *) collisionList.at(0); + collision = true; + m_operationMode = MOVEGUIDE; + // deselect all clips so that only the guide will move + QList clips = scene()->selectedItems(); + for (int i = 0; i < clips.count(); ++i) + clips.at(i)->setSelected(false); + updateSnapPoints(NULL); + QGraphicsView::mousePressEvent(event); + } else for (int i = 0; i < collisionList.count(); ++i) { + QGraphicsItem *item = collisionList.at(i); + if (item->type() == AVWIDGET || item->type() == TRANSITIONWIDGET) { + if (m_tool == RAZORTOOL) { + m_dragItem = NULL; + if (item->type() == TRANSITIONWIDGET) { + emit displayMessage(i18n("Cannot cut a transition"), ErrorMessage); + event->accept(); + return; + } + AbstractClipItem *clip = static_cast (item); + RazorClipCommand* command = new RazorClipCommand(this, clip->info(), GenTime((int)(mapToScene(event->pos()).x() / m_scale), m_document->fps()), true); + m_commandStack->push(command); + m_document->setModified(true); + event->accept(); //QGraphicsView::mousePressEvent(event); + return; + } + // select item + if (!item->isSelected()) { + + if (event->modifiers() != Qt::ControlModifier) { + QList itemList = items(); + for (int i = 0; i < itemList.count(); i++) { + itemList.at(i)->setSelected(false); + itemList.at(i)->update(); + } + } + item->setSelected(true); + item->update(); + } + + m_dragItem = (AbstractClipItem *) item; + + m_clickPoint = QPoint((int)(mapToScene(event->pos()).x() - m_dragItem->startPos().frames(m_document->fps()) * m_scale), (int)(event->pos().y() - m_dragItem->pos().y())); + m_dragItemInfo.startPos = m_dragItem->startPos(); + m_dragItemInfo.endPos = m_dragItem->endPos(); + m_dragItemInfo.track = m_dragItem->track(); + + m_selectedClipList.clear(); + QList selected = scene()->selectedItems(); + for (int i = 0; i < selected.count(); i++) { + if (selected.at(i)->type() == AVWIDGET || selected.at(i)->type() == TRANSITIONWIDGET) + m_selectedClipList.append(static_cast (selected.at(i))); + } + + m_operationMode = m_dragItem->operationMode(mapToScene(event->pos()), m_scale); + if (m_operationMode == KEYFRAME) { + m_dragItem->updateSelectedKeyFrame(); + m_blockRefresh = false; + return; + } else if (m_operationMode == MOVE) setCursor(Qt::ClosedHandCursor); + else if (m_operationMode == TRANSITIONSTART) { + ItemInfo info; + info.startPos = m_dragItem->startPos(); + info.track = m_dragItem->track(); + int transitiontrack = getPreviousVideoTrack(info.track); + ClipItem *transitionClip = NULL; + if (transitiontrack != 0) transitionClip = getClipItemAt((int) info.startPos.frames(m_document->fps()), m_tracksList.count() - transitiontrack); + if (transitionClip && transitionClip->endPos() < m_dragItem->endPos()) { + info.endPos = transitionClip->endPos(); + } else info.endPos = info.startPos + GenTime(2.5); + + slotAddTransition((ClipItem *) m_dragItem, info, transitiontrack); + } else if (m_operationMode == TRANSITIONEND) { + ItemInfo info; + info.endPos = m_dragItem->endPos(); + info.track = m_dragItem->track(); + int transitiontrack = getPreviousVideoTrack(info.track); + ClipItem *transitionClip = NULL; + if (transitiontrack != 0) transitionClip = getClipItemAt((int) info.endPos.frames(m_document->fps()), m_tracksList.count() - transitiontrack); + if (transitionClip && transitionClip->startPos() > m_dragItem->startPos()) { + info.startPos = transitionClip->startPos(); + } else info.startPos = info.endPos - GenTime(2.5); + slotAddTransition((ClipItem *) m_dragItem, info, transitiontrack); + } + updateSnapPoints(m_dragItem); + collision = true; + break; } - updateSnapPoints(m_dragItem); - kDebug() << "//////// ITEM CLICKED: " << m_startPos; - collision = true; - break; } - } - emit clipItemSelected((ClipItem*) clipItem); - emit transitionItemSelected((Transition*) transitionItem); if (!collision) { kDebug() << "//////// NO ITEM FOUND ON CLICK"; m_dragItem = NULL; @@ -430,19 +577,114 @@ void CustomTrackView::mousePressEvent(QMouseEvent * event) { //emit clipItemSelected(NULL); if (event->button() == Qt::RightButton) { displayContextMenu(event->globalPos()); - } else setCursorPos((int) mapToScene(event->x(), 0).x() / m_scale); + m_menuPosition = event->pos(); + } else setCursorPos((int)(mapToScene(event->x(), 0).x() / m_scale)); } else if (event->button() == Qt::RightButton) { m_operationMode = NONE; - displayContextMenu(event->globalPos(), (ClipItem *) m_dragItem); + displayContextMenu(event->globalPos(), m_dragItem); m_dragItem = NULL; } + if (m_dragItem && m_dragItem->type() == AVWIDGET) emit clipItemSelected((ClipItem*) m_dragItem); + else emit clipItemSelected(NULL); } + m_blockRefresh = false; //kDebug()<popup(pos); +void CustomTrackView::mouseDoubleClickEvent(QMouseEvent *event) { + if (m_dragItem && m_dragItem->hasKeyFrames()) { + if (m_moveOpMode == KEYFRAME) { + // user double clicked on a keyframe, open edit dialog + QDialog d(parentWidget()); + Ui::KeyFrameDialog_UI view; + view.setupUi(&d); + view.kfr_position->setText(m_document->timecode().getTimecode(GenTime(m_dragItem->selectedKeyFramePos(), m_document->fps()) - m_dragItem->cropStart(), m_document->fps())); + view.kfr_value->setValue(m_dragItem->selectedKeyFrameValue()); + view.kfr_value->setFocus(); + if (d.exec() == QDialog::Accepted) { + int pos = m_document->timecode().getFrameCount(view.kfr_position->text(), m_document->fps()); + m_dragItem->updateKeyFramePos(GenTime(pos, m_document->fps()) + m_dragItem->cropStart(), (double) view.kfr_value->value() * m_dragItem->keyFrameFactor()); + ClipItem *item = (ClipItem *)m_dragItem; + QString previous = item->keyframes(item->selectedEffectIndex()); + item->updateKeyframeEffect(); + QString next = item->keyframes(item->selectedEffectIndex()); + EditKeyFrameCommand *command = new EditKeyFrameCommand(this, item->track(), item->startPos(), item->selectedEffectIndex(), previous, next, false); + m_commandStack->push(command); + updateEffect(m_tracksList.count() - item->track(), item->startPos(), item->selectedEffect(), item->selectedEffectIndex()); + } + + } else { + // add keyframe + GenTime keyFramePos = GenTime((int)(mapToScene(event->pos()).x() / m_scale), m_document->fps()) - m_dragItem->startPos() + m_dragItem->cropStart(); + m_dragItem->addKeyFrame(keyFramePos, mapToScene(event->pos()).toPoint().y()); + ClipItem * item = (ClipItem *) m_dragItem; + QString previous = item->keyframes(item->selectedEffectIndex()); + item->updateKeyframeEffect(); + QString next = item->keyframes(item->selectedEffectIndex()); + EditKeyFrameCommand *command = new EditKeyFrameCommand(this, m_dragItem->track(), m_dragItem->startPos(), item->selectedEffectIndex(), previous, next, false); + m_commandStack->push(command); + updateEffect(m_tracksList.count() - item->track(), item->startPos(), item->selectedEffect(), item->selectedEffectIndex()); + } + } else if (m_dragItem) { + ClipDurationDialog d(m_dragItem, m_document->timecode(), this); + if (d.exec() == QDialog::Accepted) { + if (d.startPos() != m_dragItem->startPos()) { + if (m_dragItem->type() == AVWIDGET) { + ItemInfo startInfo; + startInfo.startPos = m_dragItem->startPos(); + startInfo.endPos = m_dragItem->endPos(); + startInfo.track = m_dragItem->track(); + ItemInfo endInfo; + endInfo.startPos = d.startPos(); + endInfo.endPos = m_dragItem->endPos() + (endInfo.startPos - startInfo.startPos); + endInfo.track = m_dragItem->track(); + MoveClipCommand *command = new MoveClipCommand(this, startInfo, endInfo, true); + m_commandStack->push(command); + } else { + //TODO: move transition + } + } + if (d.duration() != m_dragItem->duration()) { + if (m_dragItem->type() == AVWIDGET) { + ItemInfo startInfo; + startInfo.startPos = m_dragItem->startPos(); + startInfo.endPos = m_dragItem->endPos(); + startInfo.track = m_dragItem->track(); + ItemInfo endInfo; + endInfo.startPos = startInfo.startPos; + endInfo.endPos = endInfo.startPos + d.duration(); + endInfo.track = m_dragItem->track(); + ResizeClipCommand *command = new ResizeClipCommand(this, startInfo, endInfo, true); + m_commandStack->push(command); + } else { + //TODO: resize transition + } + } + } + } else { + QList collisionList = items(event->pos()); + if (collisionList.count() == 1 && collisionList.at(0)->type() == GUIDEITEM) { + Guide *editGuide = (Guide *) collisionList.at(0); + if (editGuide) slotEditGuide(editGuide->info()); + } + } +} + + +void CustomTrackView::editKeyFrame(const GenTime pos, const int track, const int index, const QString keyframes) { + ClipItem *clip = getClipItemAt((int)pos.frames(m_document->fps()), track); + if (clip) { + clip->setKeyframes(index, keyframes); + updateEffect(m_tracksList.count() - clip->track(), clip->startPos(), clip->effectAt(index), index); + } else emit displayMessage(i18n("Cannot find clip with keyframe"), ErrorMessage); +} + + +void CustomTrackView::displayContextMenu(QPoint pos, AbstractClipItem *clip) { + if (clip == NULL) m_timelineContextMenu->popup(pos); + else if (clip->type() == AVWIDGET) m_timelineContextClipMenu->popup(pos); + else if (clip->type() == TRANSITIONWIDGET) m_timelineContextTransitionMenu->popup(pos); } void CustomTrackView::activateMonitor() { @@ -466,26 +708,35 @@ void CustomTrackView::dragEnterEvent(QDragEnterEvent * event) { void CustomTrackView::slotRefreshEffects(ClipItem *clip) { int track = m_tracksList.count() - clip->track(); GenTime pos = clip->startPos(); - m_document->renderer()->mltRemoveEffect(track, pos, "-1", false); + if (!m_document->renderer()->mltRemoveEffect(track, pos, "-1", false)) { + emit displayMessage(i18n("Problem deleting effect"), ErrorMessage); + return; + } + bool success = true; for (int i = 0; i < clip->effectsCount(); i++) { - m_document->renderer()->mltAddEffect(track, pos, clip->getEffectArgs(clip->effectAt(i)), false); + if (!m_document->renderer()->mltAddEffect(track, pos, clip->getEffectArgs(clip->effectAt(i)), false)) success = false; } + if (!success) emit displayMessage(i18n("Problem adding effect to clip"), ErrorMessage); m_document->renderer()->doRefresh(); } void CustomTrackView::addEffect(int track, GenTime pos, QDomElement effect) { - ClipItem *clip = getClipItemAt(pos.frames(m_document->fps()) + 1, m_tracksList.count() - track); + ClipItem *clip = getClipItemAt((int)pos.frames(m_document->fps()) + 1, m_tracksList.count() - track); if (clip) { QMap effectParams = clip->addEffect(effect); - m_document->renderer()->mltAddEffect(track, pos, effectParams); + if (!m_document->renderer()->mltAddEffect(track, pos, effectParams)) + emit displayMessage(i18n("Problem adding effect to clip"), ErrorMessage); emit clipItemSelected(clip); - } + } else emit displayMessage(i18n("Cannot find clip to add effect"), ErrorMessage); } void CustomTrackView::deleteEffect(int track, GenTime pos, QDomElement effect) { QString index = effect.attribute("kdenlive_ix"); - m_document->renderer()->mltRemoveEffect(track, pos, index); - ClipItem *clip = getClipItemAt(pos.frames(m_document->fps()) + 1, m_tracksList.count() - track); + if (effect.attribute("disabled") != "1" && !m_document->renderer()->mltRemoveEffect(track, pos, index)) { + emit displayMessage(i18n("Problem deleting effect"), ErrorMessage); + return; + } + ClipItem *clip = getClipItemAt((int)pos.frames(m_document->fps()) + 1, m_tracksList.count() - track); if (clip) { clip->deleteEffect(index); emit clipItemSelected(clip); @@ -494,21 +745,17 @@ void CustomTrackView::deleteEffect(int track, GenTime pos, QDomElement effect) { void CustomTrackView::slotAddEffect(QDomElement effect, GenTime pos, int track) { QList itemList; - if (track == -1) - itemList = items(); - else { - ClipItem *clip = getClipItemAt(pos.frames(m_document->fps()) + 1, track); + if (track == -1) itemList = scene()->selectedItems(); + if (itemList.isEmpty()) { + ClipItem *clip = getClipItemAt((int)pos.frames(m_document->fps()) + 1, track); if (clip) itemList.append(clip); - else kDebug() << "------ wrning, clip eff not found"; + else emit displayMessage(i18n("Select a clip if you want to apply an effect"), ErrorMessage); } - kDebug() << "// REQUESTING EFFECT ON CLIP: " << pos.frames(25) << ", TRK: " << track; + kDebug() << "// REQUESTING EFFECT ON CLIP: " << pos.frames(25) << ", TRK: " << track << "SELECTED ITEMS: " << itemList.count(); for (int i = 0; i < itemList.count(); i++) { - if (itemList.at(i)->type() == AVWIDGET && (itemList.at(i)->isSelected() || track != -1)) { + if (itemList.at(i)->type() == AVWIDGET) { ClipItem *item = (ClipItem *)itemList.at(i); - // the kdenlive_ix int is used to identify an effect in mlt's playlist, should - // not be changed - if (effect.attribute("kdenlive_ix").toInt() == 0) - effect.setAttribute("kdenlive_ix", QString::number(item->effectsCounter())); + item->initEffect(effect); AddEffectCommand *command = new AddEffectCommand(this, m_tracksList.count() - item->track(), item->startPos(), effect, true); m_commandStack->push(command); } @@ -522,111 +769,189 @@ void CustomTrackView::slotDeleteEffect(ClipItem *clip, QDomElement effect) { m_document->setModified(true); } -void CustomTrackView::updateEffect(int track, GenTime pos, QDomElement effect) { - ClipItem *clip = getClipItemAt(pos.frames(m_document->fps()) + 1, m_tracksList.count() - track); +void CustomTrackView::updateEffect(int track, GenTime pos, QDomElement effect, int ix) { + ClipItem *clip = getClipItemAt((int)pos.frames(m_document->fps()) + 1, m_tracksList.count() - track); if (clip) { QMap effectParams = clip->getEffectArgs(effect); - if (effectParams["disabled"] == "1") { - QString index = effectParams["kdenlive_ix"]; - m_document->renderer()->mltRemoveEffect(track, pos, index); - } else m_document->renderer()->mltEditEffect(m_tracksList.count() - clip->track(), clip->startPos(), effectParams); + if (effectParams.value("disabled") == "1") { + if (m_document->renderer()->mltRemoveEffect(track, pos, effectParams.value("kdenlive_ix"))) { + kDebug() << "////// DISABLING EFFECT: " << index << ", CURRENTLA: " << clip->selectedEffectIndex(); + } else emit displayMessage(i18n("Problem deleting effect"), ErrorMessage); + } else if (!m_document->renderer()->mltEditEffect(m_tracksList.count() - clip->track(), clip->startPos(), effectParams)) + emit displayMessage(i18n("Problem editing effect"), ErrorMessage); + if (ix == clip->selectedEffectIndex()) { + clip->setSelectedEffect(clip->selectedEffectIndex()); + } } m_document->setModified(true); } -void CustomTrackView::slotChangeEffectState(ClipItem *clip, QDomElement effect, bool disable) { +void CustomTrackView::moveEffect(int track, GenTime pos, int oldPos, int newPos) { + ClipItem *clip = getClipItemAt((int)pos.frames(m_document->fps()) + 1, m_tracksList.count() - track); + if (clip) { + m_document->renderer()->mltMoveEffect(track, pos, oldPos, newPos); + } + m_document->setModified(true); +} + +void CustomTrackView::slotChangeEffectState(ClipItem *clip, int effectPos, bool disable) { + QDomElement effect = clip->effectAt(effectPos); QDomElement oldEffect = effect.cloneNode().toElement(); effect.setAttribute("disabled", disable); - EditEffectCommand *command = new EditEffectCommand(this, m_tracksList.count() - clip->track(), clip->startPos(), oldEffect, effect, true); + EditEffectCommand *command = new EditEffectCommand(this, m_tracksList.count() - clip->track(), clip->startPos(), oldEffect, effect, effectPos, true); m_commandStack->push(command); m_document->setModified(true); } -void CustomTrackView::slotUpdateClipEffect(ClipItem *clip, QDomElement oldeffect, QDomElement effect) { - EditEffectCommand *command = new EditEffectCommand(this, m_tracksList.count() - clip->track(), clip->startPos(), oldeffect, effect, true); +void CustomTrackView::slotChangeEffectPosition(ClipItem *clip, int currentPos, int newPos) { + MoveEffectCommand *command = new MoveEffectCommand(this, m_tracksList.count() - clip->track(), clip->startPos(), currentPos, newPos, true); m_commandStack->push(command); + m_document->setModified(true); } -void CustomTrackView::slotAddTransition(ClipItem* clip , QDomElement transition, GenTime startTime , int startTrack) { - AddTransitionCommand* command = new AddTransitionCommand(this, startTrack, transition, startTime, true); +void CustomTrackView::slotUpdateClipEffect(ClipItem *clip, QDomElement oldeffect, QDomElement effect, int ix) { + EditEffectCommand *command = new EditEffectCommand(this, m_tracksList.count() - clip->track(), clip->startPos(), oldeffect, effect, ix, true); m_commandStack->push(command); - m_document->setModified(true); } -void CustomTrackView::addTransition(int startTrack, GenTime startPos , QDomElement e) { - QMap < QString, QString> map; +void CustomTrackView::cutClip(ItemInfo info, GenTime cutTime, bool cut) { + if (cut) { + // cut clip + ClipItem *item = getClipItemAt((int) info.startPos.frames(m_document->fps()) + 1, info.track); + if (!item || cutTime >= item->endPos() || cutTime <= item->startPos()) { + emit displayMessage(i18n("Cannot find clip to cut"), ErrorMessage); + kDebug() << "///////// ERROR CUTTING CLIP : (" << item->startPos().frames(25) << "-" << item->endPos().frames(25) << "), INFO: (" << info.startPos.frames(25) << "-" << info.endPos.frames(25) << ")" << ", CUT: " << cutTime.frames(25); + m_blockRefresh = false; + return; + } + kDebug() << "///////// CUTTING CLIP : (" << item->startPos().frames(25) << "-" << item->endPos().frames(25) << "), INFO: (" << info.startPos.frames(25) << "-" << info.endPos.frames(25) << ")" << ", CUT: " << cutTime.frames(25); + + m_document->renderer()->mltCutClip(m_tracksList.count() - info.track, cutTime); + int cutPos = (int) cutTime.frames(m_document->fps()); + ItemInfo newPos; + newPos.startPos = cutTime; + newPos.endPos = info.endPos; + newPos.cropStart = item->cropStart() + (cutTime - info.startPos); + newPos.track = info.track; + ClipItem *dup = item->clone(m_scale, newPos); + kDebug() << "// REsizing item to: " << cutPos; + item->resizeEnd(cutPos, m_scale); + scene()->addItem(dup); + item->baseClip()->addReference(); + m_document->updateClip(item->baseClip()->getId()); + kDebug() << "///////// CUTTING CLIP RESULT: (" << item->startPos().frames(25) << "-" << item->endPos().frames(25) << "), DUP: (" << dup->startPos().frames(25) << "-" << dup->endPos().frames(25) << ")" << ", CUT: " << cutTime.frames(25); + kDebug() << "// CUTTING CLIP dONE"; + } else { + // uncut clip + + ClipItem *item = getClipItemAt((int) info.startPos.frames(m_document->fps()), info.track); + ClipItem *dup = getClipItemAt((int) cutTime.frames(m_document->fps()) + 1, info.track); + if (!item || !dup || item == dup) { + emit displayMessage(i18n("Cannot find clip to uncut"), ErrorMessage); + m_blockRefresh = false; + return; + } + + kDebug() << "// UNCUTTING CLIPS: ITEM 1 (" << item->startPos().frames(25) << "x" << item->endPos().frames(25) << ")"; + kDebug() << "// UNCUTTING CLIPS: ITEM 2 (" << dup->startPos().frames(25) << "x" << dup->endPos().frames(25) << ")"; + kDebug() << "// UNCUTTING CLIPS, INFO (" << info.startPos.frames(25) << "x" << info.endPos.frames(25) << ") , CUT: " << cutTime.frames(25);; + //deleteClip(dup->info()); + + + if (dup->isSelected()) emit clipItemSelected(NULL); + dup->baseClip()->removeReference(); + m_document->updateClip(dup->baseClip()->getId()); + scene()->removeItem(dup); + delete dup; + m_document->renderer()->mltRemoveClip(m_tracksList.count() - info.track, cutTime); + + ItemInfo clipinfo = item->info(); + clipinfo.track = m_tracksList.count() - clipinfo.track; + bool success = m_document->renderer()->mltResizeClipEnd(clipinfo, info.endPos - info.startPos); + if (success) { + item->resizeEnd((int) info.endPos.frames(m_document->fps()), m_scale); + } else + emit displayMessage(i18n("Error when resizing clip"), ErrorMessage); - QDomNamedNodeMap attribs = e.attributes(); - for (int i = 0;i < attribs.count();i++) { - if (attribs.item(i).nodeName() != "type" && - attribs.item(i).nodeName() != "start" && - attribs.item(i).nodeName() != "end" - ) - map[attribs.item(i).nodeName()] = attribs.item(i).nodeValue(); } - //map["resource"] = "%luma12.pgm"; - kDebug() << "---- ADDING transition " << e.attribute("type") << ", on tracks " << m_tracksList.count() - e.attribute("transition_track").toInt() << " / " << getPreviousVideoTrack(e.attribute("transition_track").toInt()); - m_document->renderer()->mltAddTransition(e.attribute("type"), getPreviousVideoTrack(e.attribute("transition_track").toInt()), m_tracksList.count() - e.attribute("transition_track").toInt() , - GenTime(e.attribute("start").toInt(), m_document->renderer()->fps()), - GenTime(e.attribute("end").toInt(), m_document->renderer()->fps()), - map); + QTimer::singleShot(3000, this, SLOT(slotEnableRefresh())); +} + +void CustomTrackView::slotEnableRefresh() { + m_blockRefresh = false; +} +void CustomTrackView::slotAddTransitionToSelectedClips(QDomElement transition) { + QList itemList = scene()->selectedItems(); + for (int i = 0; i < itemList.count(); i++) { + if (itemList.at(i)->type() == AVWIDGET) { + ClipItem *item = (ClipItem *) itemList.at(i); + ItemInfo info; + info.startPos = item->startPos(); + info.endPos = info.startPos + GenTime(2.5); + info.track = item->track(); + int transitiontrack = getPreviousVideoTrack(info.track); + slotAddTransition(item, info, transitiontrack, transition); + } + } +} + +void CustomTrackView::slotAddTransition(ClipItem* clip, ItemInfo transitionInfo, int endTrack, QDomElement transition) { + AddTransitionCommand* command = new AddTransitionCommand(this, transitionInfo, endTrack, transition, false, true); + m_commandStack->push(command); m_document->setModified(true); } -void CustomTrackView::deleteTransition(int, GenTime, QDomElement e) { - QMap < QString, QString> map; - QDomNamedNodeMap attribs = e.attributes(); - m_document->renderer()->mltDeleteTransition(e.attribute("type"), getPreviousVideoTrack(e.attribute("transition_track").toInt()), m_tracksList.count() - e.attribute("transition_track").toInt() , - GenTime(e.attribute("start").toInt(), m_document->renderer()->fps()), - GenTime(e.attribute("end").toInt(), m_document->renderer()->fps()), - map); +void CustomTrackView::addTransition(ItemInfo transitionInfo, int endTrack, QDomElement params) { + Transition *tr = new Transition(transitionInfo, endTrack, m_scale, m_document->fps(), params); + scene()->addItem(tr); + + //kDebug() << "---- ADDING transition " << e.attribute("tag") << ", on tracks " << m_tracksList.count() - e.attribute("transition_track").toInt() << " / " << getPreviousVideoTrack(e.attribute("transition_track").toInt()); + m_document->renderer()->mltAddTransition(tr->transitionTag(), endTrack, m_tracksList.count() - transitionInfo.track, transitionInfo.startPos, transitionInfo.endPos, tr->toXML()); + m_document->setModified(true); +} + +void CustomTrackView::deleteTransition(ItemInfo transitionInfo, int endTrack, QDomElement params) { + Transition *item = getTransitionItemAt((int)transitionInfo.startPos.frames(m_document->fps()) + 1, transitionInfo.track); + m_document->renderer()->mltDeleteTransition(item->transitionTag(), endTrack, m_tracksList.count() - transitionInfo.track, transitionInfo.startPos, transitionInfo.endPos, item->toXML()); + delete item; + emit transitionItemSelected(NULL); m_document->setModified(true); } -void CustomTrackView::slotTransitionUpdated(QDomElement old, QDomElement newEffect) { - EditTransitionCommand *command = new EditTransitionCommand(this, newEffect.attribute("a_track").toInt(), GenTime(newEffect.attribute("start").toInt(), m_document->renderer()->fps()) , old, newEffect , true); +void CustomTrackView::slotTransitionUpdated(Transition *tr, QDomElement old) { + EditTransitionCommand *command = new EditTransitionCommand(this, tr->track(), tr->startPos(), old, tr->toXML() , true); m_commandStack->push(command); m_document->setModified(true); } void CustomTrackView::updateTransition(int track, GenTime pos, QDomElement oldTransition, QDomElement transition) { - QString s; - QTextStream tx(&s); - transition.save(tx,2); - kDebug() << "in" << s; - QMap < QString, QString> map; - QDomNamedNodeMap attribs = transition.attributes(); - for (int i = 0;i < attribs.count();i++) { - if (attribs.item(i).nodeName() != "type" && - attribs.item(i).nodeName() != "start" && - attribs.item(i).nodeName() != "end" - ) - map[attribs.item(i).nodeName()] = attribs.item(i).nodeValue(); - } - m_document->renderer()->mltUpdateTransition(oldTransition.attribute("type"), transition.attribute("type"), m_tracksList.count() - 1 - transition.attribute("transition_track").toInt(), m_tracksList.count() - transition.attribute("transition_track").toInt() , - GenTime(transition.attribute("start").toInt(), m_document->renderer()->fps()), - GenTime(transition.attribute("end").toInt(), m_document->renderer()->fps()), - map); + Transition *item = getTransitionItemAt((int)pos.frames(m_document->fps()) + 1, track); + if (!item) { + kWarning() << "Unable to find transition at pos :" << pos.frames(m_document->fps()) << ", ON track: " << track; + return; + } + m_document->renderer()->mltUpdateTransition(oldTransition.attribute("tag"), transition.attribute("tag"), transition.attribute("transition_btrack").toInt(), m_tracksList.count() - transition.attribute("transition_atrack").toInt(), item->startPos(), item->endPos(), transition); + item->setTransitionParameters(transition); m_document->setModified(true); } void CustomTrackView::addItem(DocClipBase *clip, QPoint pos) { - int in = 0; - GenTime out = clip->duration(); - //kdDebug()<<"- - - -CREATING CLIP, duration = "<fileURL(); - int trackTop = ((int) mapToScene(pos).y() / m_tracksHeight) * m_tracksHeight + 1; - m_dropItem = new ClipItem(clip, ((int) mapToScene(pos).y() / m_tracksHeight), GenTime(), QRectF(mapToScene(pos).x() * m_scale, trackTop, out.frames(m_document->fps()) * m_scale, m_tracksHeight - 1), out, m_document->fps()); + ItemInfo info; + info.startPos = GenTime((int)(mapToScene(pos).x() / m_scale), m_document->fps()); + info.endPos = info.startPos + clip->duration(); + info.track = (int)(pos.y() / m_tracksHeight); + kDebug() << "------------ ADDING CLIP ITEM----: " << info.startPos.frames(25) << ", " << info.endPos.frames(25) << ", " << info.track; + m_dropItem = new ClipItem(clip, info, m_scale, m_document->fps()); scene()->addItem(m_dropItem); } void CustomTrackView::dragMoveEvent(QDragMoveEvent * event) { event->setDropAction(Qt::IgnoreAction); - //kDebug()<<"+++++++++++++ DRAG MOVE, : "<pos()).x()<<", SCAL: "<pos()).y() / m_tracksHeight; //) * (m_scale * 50) + m_scale; - m_dropItem->moveTo(mapToScene(event->pos()).x() / m_scale, m_scale, (track - m_dropItem->track()) * m_tracksHeight, track); + int track = (int)(mapToScene(event->pos()).y() / m_tracksHeight); //) * (m_scale * 50) + m_scale; + m_dropItem->moveTo((int)(mapToScene(event->pos()).x() / m_scale), m_scale, (int)((track - m_dropItem->track()) * m_tracksHeight), track); event->setDropAction(Qt::MoveAction); if (event->mimeData()->hasFormat("kdenlive/producerslist")) { event->acceptProposedAction(); @@ -645,12 +970,17 @@ void CustomTrackView::dragLeaveEvent(QDragLeaveEvent * event) { void CustomTrackView::dropEvent(QDropEvent * event) { if (m_dropItem) { - AddTimelineClipCommand *command = new AddTimelineClipCommand(this, m_dropItem->xml(), m_dropItem->clipProducer(), m_dropItem->track(), m_dropItem->startPos(), m_dropItem->rect(), m_dropItem->duration(), false, false); + AddTimelineClipCommand *command = new AddTimelineClipCommand(this, m_dropItem->xml(), m_dropItem->clipProducer(), m_dropItem->info(), m_dropItem->effectList(), false, false); m_commandStack->push(command); m_dropItem->baseClip()->addReference(); m_document->updateClip(m_dropItem->baseClip()->getId()); - // kDebug()<<"IIIIIIIIIIIIIIIIIIIIIIII TRAX CNT: "<track(); - m_document->renderer()->mltInsertClip(m_tracksList.count() - m_dropItem->track(), m_dropItem->startPos(), m_dropItem->xml()); + ItemInfo info; + info = m_dropItem->info(); + info.track = m_tracksList.count() - m_dropItem->track(); + //kDebug()<<"IIIIIIIIIIIIIIIIIIIIIIII TRAX CNT: "<track(); + m_document->renderer()->mltInsertClip(info, m_dropItem->xml(), m_dropItem->baseClip()->producer()); + //if (m_dropItem->baseClip()->isTransparent()) m_document->renderer()->mltAddClipTransparency(info, getPreviousVideoTrack(m_dropItem->track()), m_dropItem->baseClip()->getId()); + m_dropItem = NULL; m_document->setModified(true); } else QGraphicsView::dropEvent(event); m_dropItem = NULL; @@ -671,10 +1001,9 @@ Qt::DropActions CustomTrackView::supportedDropActions() const { } void CustomTrackView::setDuration(int duration) { - kDebug() << "///////////// PRO DUR: " << duration << ", SCALE. " << m_scale << ", height: " << 50 * m_tracksList.count(); + kDebug() << "///////////// PRO DUR: " << duration << ", SCALE. " << (m_projectDuration + 500) * m_scale << ", height: " << 50 * m_tracksList.count(); m_projectDuration = duration; - scene()->setSceneRect(0, 0, (m_projectDuration + 500) * m_scale, scene()->sceneRect().height()); //50 * m_tracksCount); - horizontalScrollBar()->setMaximum((m_projectDuration + 500) * m_scale); + setSceneRect(0, 0, (m_projectDuration + 100) * m_scale, sceneRect().height()); } int CustomTrackView::duration() const { @@ -715,7 +1044,7 @@ void CustomTrackView::deleteClip(int clipId) { if (itemList.at(i)->type() == AVWIDGET) { ClipItem *item = (ClipItem *)itemList.at(i); if (item->clipProducer() == clipId) { - AddTimelineClipCommand *command = new AddTimelineClipCommand(this, item->xml(), item->clipProducer(), item->track(), item->startPos(), item->rect(), item->duration(), true, true); + AddTimelineClipCommand *command = new AddTimelineClipCommand(this, item->xml(), item->clipProducer(), item->info(), item->effectList(), true, true); m_commandStack->push(command); //delete item; } @@ -724,7 +1053,7 @@ void CustomTrackView::deleteClip(int clipId) { } void CustomTrackView::setCursorPos(int pos, bool seek) { - emit cursorMoved(m_cursorPos * m_scale, pos * m_scale); + emit cursorMoved((int)(m_cursorPos * m_scale), (int)(pos * m_scale)); m_cursorPos = pos; m_cursorLine->setPos(pos * m_scale, 0); if (seek) m_document->renderer()->seek(GenTime(pos, m_document->fps())); @@ -736,124 +1065,499 @@ void CustomTrackView::updateCursorPos() { } int CustomTrackView::cursorPos() { - return m_cursorPos * m_scale; + return (int)(m_cursorPos * m_scale); +} + +void CustomTrackView::moveCursorPos(int delta) { + if (m_cursorPos + delta < 0) delta = 0 - m_cursorPos; + emit cursorMoved((int)(m_cursorPos * m_scale), (int)((m_cursorPos + delta) * m_scale)); + m_cursorPos += delta; + m_cursorLine->setPos(m_cursorPos * m_scale, 0); + m_document->renderer()->seek(GenTime(m_cursorPos, m_document->fps())); + //if (m_autoScroll && m_scale < 50) checkScrolling(); } void CustomTrackView::checkScrolling() { - QRect rectInView = viewport()->rect(); + int vert = verticalScrollBar()->value(); + int hor = cursorPos(); + ensureVisible(hor, vert + 10, 2, 2, 50, 0); + //centerOn(QPointF(cursorPos(), m_tracksHeight)); + /*QRect rectInView = viewport()->rect(); int delta = rectInView.width() / 3; int max = rectInView.right() + horizontalScrollBar()->value() - delta; //kDebug() << "CURSOR POS: "<= max) horizontalScrollBar()->setValue(horizontalScrollBar()->value() + 1 + m_scale); + if (m_cursorPos * m_scale >= max) horizontalScrollBar()->setValue((int)(horizontalScrollBar()->value() + 1 + m_scale));*/ } void CustomTrackView::mouseReleaseEvent(QMouseEvent * event) { QGraphicsView::mouseReleaseEvent(event); + if (event->button() == Qt::MidButton) { + return; + } setDragMode(QGraphicsView::NoDrag); - if (m_dragItem == NULL) return; - if (m_operationMode == MOVE) setCursor(Qt::OpenHandCursor); - if (m_operationMode == MOVE && m_startPos.x() != m_dragItem->startPos().frames(m_document->fps())) { - // move clip - MoveClipCommand *command = new MoveClipCommand(this, m_startPos, QPointF(m_dragItem->startPos().frames(m_document->fps()), m_dragItem->track()), false); + if (m_operationMode == MOVEGUIDE) { + 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_scale, m_document->fps()), m_dragGuide->label(), false); m_commandStack->push(command); - if (m_dragItem->type() == AVWIDGET) m_document->renderer()->mltMoveClip(m_tracksList.count() - m_startPos.y(), m_tracksList.count() - m_dragItem->track(), m_startPos.x(), m_dragItem->startPos().frames(m_document->fps())); - } else if (m_operationMode == RESIZESTART) { + m_dragGuide->updateGuide(GenTime(m_dragGuide->pos().x() / m_scale, m_document->fps())); + m_dragGuide = NULL; + m_dragItem = NULL; + return; + } + if (m_dragItem == NULL) { + emit transitionItemSelected(NULL); + return; + } + ItemInfo info = m_dragItem->info(); + + if (m_operationMode == MOVE) { + setCursor(Qt::OpenHandCursor); + + if (m_selectedClipList.count() == 1) { + // we are moving one clip, easy + if (m_dragItem->type() == AVWIDGET && (m_dragItemInfo.startPos != info.startPos || m_dragItemInfo.track != info.track)) { + bool success = m_document->renderer()->mltMoveClip((int)(m_tracksList.count() - m_dragItemInfo.track), (int)(m_tracksList.count() - m_dragItem->track()), (int) m_dragItemInfo.startPos.frames(m_document->fps()), (int)(m_dragItem->startPos().frames(m_document->fps()))); + if (success) { + MoveClipCommand *command = new MoveClipCommand(this, m_dragItemInfo, info, false); + m_commandStack->push(command); + } else { + // undo last move and emit error message + MoveClipCommand *command = new MoveClipCommand(this, info, m_dragItemInfo, true); + m_commandStack->push(command); + emit displayMessage(i18n("Cannot move clip to position %1seconds", QString::number(m_dragItemInfo.startPos.seconds(), 'g', 2)), ErrorMessage); + } + } + if (m_dragItem->type() == TRANSITIONWIDGET && (m_dragItemInfo.startPos != info.startPos || m_dragItemInfo.track != info.track)) { + MoveTransitionCommand *command = new MoveTransitionCommand(this, m_dragItemInfo, info, false); + m_commandStack->push(command); + Transition *transition = (Transition *) m_dragItem; + transition->updateTransitionEndTrack(getPreviousVideoTrack(m_dragItem->track())); + m_document->renderer()->mltMoveTransition(transition->transitionTag(), (int)(m_tracksList.count() - m_dragItemInfo.track), (int)(m_tracksList.count() - m_dragItem->track()), transition->transitionEndTrack(), m_dragItemInfo.startPos, m_dragItemInfo.endPos, info.startPos, info.endPos); + } + } else { + // Moving several clips. We need to delete them and readd them to new position, + // or they might overlap each other during the move + + GenTime timeOffset = info.startPos - m_dragItemInfo.startPos; + int trackOffset = info.track - m_dragItemInfo.track; + if (timeOffset != GenTime() || trackOffset != 0) { + QUndoCommand *moveClips = new QUndoCommand(); + moveClips->setText("Move clips"); + // remove items in MLT playlist + for (int i = 0; i < m_selectedClipList.count(); i++) { + AbstractClipItem *item = m_selectedClipList.at(i); + ItemInfo info = item->info(); + info.startPos = info.startPos - timeOffset; + info.endPos = info.endPos - timeOffset; + info.track = info.track - trackOffset; + if (item->type() == AVWIDGET) { + ClipItem *clip = static_cast (item); + new AddTimelineClipCommand(this, clip->xml(), clip->clipProducer(), info, clip->effectList(), false, true, moveClips); + m_document->renderer()->mltRemoveClip(m_tracksList.count() - info.track, info.startPos); + } else { + Transition *tr = static_cast (item); + new AddTransitionCommand(this, info, tr->transitionEndTrack(), tr->toXML(), false, true, moveClips); + m_document->renderer()->mltDeleteTransition(tr->transitionTag(), tr->transitionEndTrack(), m_tracksList.count() - info.track, info.startPos, info.endPos, tr->toXML()); + } + } + + for (int i = 0; i < m_selectedClipList.count(); i++) { + // re-add items in correct place + AbstractClipItem *item = m_selectedClipList.at(i); + if (item->type() == AVWIDGET) { + ClipItem *clip = static_cast (item); + new AddTimelineClipCommand(this, clip->xml(), clip->clipProducer(), item->info(), clip->effectList(), false, false, moveClips); + ItemInfo info = item->info(); + info.track = m_tracksList.count() - item->track(); + m_document->renderer()->mltInsertClip(info, clip->xml(), clip->baseClip()->producer()); + } else { + Transition *tr = static_cast (item); + ItemInfo transitionInfo = tr->info(); + new AddTransitionCommand(this, info, tr->transitionEndTrack(), tr->toXML(), false, false, moveClips); + m_document->renderer()->mltAddTransition(tr->transitionTag(), tr->transitionEndTrack() + trackOffset, m_tracksList.count() - transitionInfo.track, transitionInfo.startPos, transitionInfo.endPos, tr->toXML()); + } + } + m_commandStack->push(moveClips); + } + } + + } else if (m_operationMode == RESIZESTART && m_dragItem->startPos() != m_dragItemInfo.startPos) { // resize start - ResizeClipCommand *command = new ResizeClipCommand(this, m_startPos, QPointF(m_dragItem->startPos().frames(m_document->fps()), m_dragItem->track()), true, false); + if (m_dragItem->type() == AVWIDGET) { + ItemInfo resizeinfo = m_dragItemInfo; + resizeinfo.track = m_tracksList.count() - resizeinfo.track; + bool success = m_document->renderer()->mltResizeClipStart(resizeinfo, m_dragItem->startPos() - m_dragItemInfo.startPos); + if (success) { + updateClipFade((ClipItem *) m_dragItem); + ResizeClipCommand *command = new ResizeClipCommand(this, m_dragItemInfo, info, false); + m_commandStack->push(command); + } else { + m_dragItem->resizeStart((int) m_dragItemInfo.startPos.frames(m_document->fps()), m_scale); + emit displayMessage(i18n("Error when resizing clip"), ErrorMessage); + } + } else if (m_dragItem->type() == TRANSITIONWIDGET) { + MoveTransitionCommand *command = new MoveTransitionCommand(this, m_dragItemInfo, info, false); + m_commandStack->push(command); + Transition *transition = (Transition *) m_dragItem; + m_document->renderer()->mltMoveTransition(transition->transitionTag(), (int)(m_tracksList.count() - m_dragItemInfo.track), (int)(m_tracksList.count() - m_dragItemInfo.track), 0, m_dragItemInfo.startPos, m_dragItemInfo.endPos, info.startPos, info.endPos); + } - if (m_dragItem->type() == AVWIDGET) m_document->renderer()->mltResizeClipStart(m_tracksList.count() - m_dragItem->track(), m_dragItem->endPos(), m_dragItem->startPos(), GenTime(m_startPos.x(), m_document->fps()), m_dragItem->cropStart(), m_dragItem->cropStart() + m_dragItem->endPos() - m_dragItem->startPos()); - m_commandStack->push(command); - m_document->renderer()->doRefresh(); - } else if (m_operationMode == RESIZEEND) { + //m_document->renderer()->doRefresh(); + } else if (m_operationMode == RESIZEEND && m_dragItem->endPos() != m_dragItemInfo.endPos) { // resize end - ResizeClipCommand *command = new ResizeClipCommand(this, m_startPos, QPointF(m_dragItem->endPos().frames(m_document->fps()), m_dragItem->track()), false, false); - - if (m_dragItem->type() == AVWIDGET) m_document->renderer()->mltResizeClipEnd(m_tracksList.count() - m_dragItem->track(), m_dragItem->startPos(), m_dragItem->cropStart(), m_dragItem->cropStart() + m_dragItem->endPos() - m_dragItem->startPos()); + if (m_dragItem->type() == AVWIDGET) { + ItemInfo resizeinfo = info; + resizeinfo.track = m_tracksList.count() - resizeinfo.track; + bool success = m_document->renderer()->mltResizeClipEnd(resizeinfo, resizeinfo.endPos - resizeinfo.startPos); + if (success) { + ResizeClipCommand *command = new ResizeClipCommand(this, m_dragItemInfo, info, false); + m_commandStack->push(command); + } else { + m_dragItem->resizeEnd((int) m_dragItemInfo.endPos.frames(m_document->fps()), m_scale); + emit displayMessage(i18n("Error when resizing clip"), ErrorMessage); + } + } else if (m_dragItem->type() == TRANSITIONWIDGET) { + MoveTransitionCommand *command = new MoveTransitionCommand(this, m_dragItemInfo, info, false); + m_commandStack->push(command); + Transition *transition = (Transition *) m_dragItem; +// m_document->renderer()->mltMoveTransition(transition->transitionTag(), (int)(m_tracksList.count() - m_dragItemInfo.track), (int)(m_tracksList.count() - m_dragItemInfo.track), 0, m_dragItemInfo.startPos, m_dragItemInfo.endPos, info.startPos, info.endPos); + } + //m_document->renderer()->doRefresh(); + } else if (m_operationMode == FADEIN) { + // resize fade in effect + ClipItem * item = (ClipItem *) m_dragItem; + QStringList clipeffects = item->effectNames(); + if (clipeffects.contains(i18n("Fade in"))) { + int ix = clipeffects.indexOf(i18n("Fade in")); + QDomElement oldeffect = item->effectAt(ix); + int start = item->cropStart().frames(m_document->fps()); + int end = item->fadeIn(); + if (end == 0) { + slotDeleteEffect(item, oldeffect); + } else { + end += start; + QDomElement effect = MainWindow::audioEffects.getEffectByName("Fade in"); + EffectsList::setParameter(effect, "in", QString::number(start)); + EffectsList::setParameter(effect, "out", QString::number(end)); + slotUpdateClipEffect(item, oldeffect, effect, ix); + } + } else if (item->fadeIn() != 0) { + QDomElement effect = MainWindow::audioEffects.getEffectByName("Fade in"); + int start = item->cropStart().frames(m_document->fps()); + int end = item->fadeIn() + start; + EffectsList::setParameter(effect, "in", QString::number(start)); + EffectsList::setParameter(effect, "out", QString::number(end)); + slotAddEffect(effect, m_dragItem->startPos(), m_dragItem->track()); + } + } else if (m_operationMode == FADEOUT) { + // resize fade in effect + ClipItem * item = (ClipItem *) m_dragItem; + QStringList clipeffects = item->effectNames(); + if (clipeffects.contains(i18n("Fade out"))) { + int ix = clipeffects.indexOf(i18n("Fade out")); + QDomElement oldeffect = item->effectAt(ix); + int end = (item->duration() + item->cropStart()).frames(m_document->fps()); + int start = item->fadeOut(); + if (start == 0) { + slotDeleteEffect(item, oldeffect); + } else { + start = end - start; + QDomElement effect = MainWindow::audioEffects.getEffectByName("Fade out"); + EffectsList::setParameter(effect, "in", QString::number(start)); + EffectsList::setParameter(effect, "out", QString::number(end)); + slotUpdateClipEffect(item, oldeffect, effect, ix); + } + } else if (item->fadeOut() != 0) { + QDomElement effect = MainWindow::audioEffects.getEffectByName("Fade out"); + int end = (item->duration() + item->cropStart()).frames(m_document->fps()); + int start = end - item->fadeOut(); + EffectsList::setParameter(effect, "in", QString::number(start)); + EffectsList::setParameter(effect, "out", QString::number(end)); + slotAddEffect(effect, m_dragItem->startPos(), m_dragItem->track()); + } + } else if (m_operationMode == KEYFRAME) { + // update the MLT effect + ClipItem * item = (ClipItem *) m_dragItem; + QString previous = item->keyframes(item->selectedEffectIndex()); + item->updateKeyframeEffect(); + QString next = item->keyframes(item->selectedEffectIndex()); + EditKeyFrameCommand *command = new EditKeyFrameCommand(this, item->track(), item->startPos(), item->selectedEffectIndex(), previous, next, false); m_commandStack->push(command); - m_document->renderer()->doRefresh(); + updateEffect(m_tracksList.count() - item->track(), item->startPos(), item->selectedEffect(), item->selectedEffectIndex()); } + + emit transitionItemSelected((m_dragItem && m_dragItem->type() == TRANSITIONWIDGET) ? (Transition*) m_dragItem : NULL); m_document->setModified(true); m_operationMode = NONE; - m_dragItem = NULL; } -void CustomTrackView::deleteClip(int track, GenTime startpos, const QRectF &rect) { - ClipItem *item = getClipItemAt(startpos, track); +void CustomTrackView::deleteClip(ItemInfo info) { + ClipItem *item = getClipItemAt((int) info.startPos.frames(m_document->fps()) + 1, info.track); if (!item) { - kDebug() << "----------------  ERROR, CANNOT find clip to move at: " << rect.x(); + kDebug() << "----------------  ERROR, CANNOT find clip to delete at...";// << rect.x(); return; } + if (item->isSelected()) emit clipItemSelected(NULL); item->baseClip()->removeReference(); m_document->updateClip(item->baseClip()->getId()); + scene()->removeItem(item); delete item; - m_document->renderer()->mltRemoveClip(m_tracksList.count() - track, startpos); + m_document->renderer()->mltRemoveClip(m_tracksList.count() - info.track, info.startPos); m_document->renderer()->doRefresh(); } void CustomTrackView::deleteSelectedClips() { - QList itemList = items(); + QList itemList = scene()->selectedItems(); + if (itemList.count() == 0) { + emit displayMessage(i18n("Select clip to delete"), ErrorMessage); + return; + } + QUndoCommand *deleteSelected = new QUndoCommand(); + deleteSelected->setText("Delete selected items"); for (int i = 0; i < itemList.count(); i++) { - if (itemList.at(i)->type() == AVWIDGET && itemList.at(i)->isSelected()) { - ClipItem *item = (ClipItem *) itemList.at(i); - AddTimelineClipCommand *command = new AddTimelineClipCommand(this, item->xml(), item->clipProducer(), item->track(), item->startPos(), item->rect(), item->duration(), true, true); - m_commandStack->push(command); + if (itemList.at(i)->type() == AVWIDGET) { + ClipItem *item = static_cast (itemList.at(i)); + new AddTimelineClipCommand(this, item->xml(), item->clipProducer(), item->info(), item->effectList(), true, true, deleteSelected); + } else if (itemList.at(i)->type() == TRANSITIONWIDGET) { + Transition *item = static_cast (itemList.at(i)); + ItemInfo info; + info.startPos = item->startPos(); + info.endPos = item->endPos(); + info.track = item->track(); + new AddTransitionCommand(this, info, item->transitionEndTrack(), item->toXML(), true, true, deleteSelected); + } + } + m_commandStack->push(deleteSelected); +} + +void CustomTrackView::changeClipSpeed() { + QList itemList = scene()->selectedItems(); + if (itemList.count() == 0) { + emit displayMessage(i18n("Select clip to change speed"), ErrorMessage); + return; + } + QUndoCommand *changeSelected = new QUndoCommand(); + changeSelected->setText("Edit clip speed"); + for (int i = 0; i < itemList.count(); i++) { + if (itemList.at(i)->type() == AVWIDGET) { + ClipItem *item = static_cast (itemList.at(i)); + ItemInfo info = item->info(); + int percent = QInputDialog::getInteger(this, i18n("Edit Clip Speed"), i18n("New speed (percents)"), 100, 1, 300); + double speed = (double) percent / 100.0; + if (item->speed() != speed) + new ChangeSpeedCommand(this, info, item->speed(), speed, item->clipProducer(), true, changeSelected); + } + } + m_commandStack->push(changeSelected); +} + +void CustomTrackView::doChangeClipSpeed(ItemInfo info, double speed, int id) { + DocClipBase *baseclip = m_document->clipManager()->getClipById(id); + ClipItem *item = getClipItemAt((int) info.startPos.frames(m_document->fps()) + 1, info.track); + info.track = m_tracksList.count() - item->track(); + m_document->renderer()->mltChangeClipSpeed(info, speed, baseclip->producer()); + item->setSpeed(speed); + GenTime maxDuration = item->maxDuration(); + if (maxDuration < item->duration()) { + info = item->info(); + ItemInfo endInfo = info; + endInfo.endPos = info.startPos + maxDuration; + ResizeClipCommand *command = new ResizeClipCommand(this, info, endInfo, true); + m_commandStack->push(command); + } +} + +void CustomTrackView::cutSelectedClips() { + QList itemList = scene()->selectedItems(); + GenTime currentPos = GenTime(m_cursorPos, m_document->fps()); + for (int i = 0; i < itemList.count(); i++) { + if (itemList.at(i)->type() == AVWIDGET) { + ClipItem *item = static_cast (itemList.at(i)); + if (currentPos > item->startPos() && currentPos < item->endPos()) { + RazorClipCommand *command = new RazorClipCommand(this, item->info(), currentPos, true); + m_commandStack->push(command); + } } } } -void CustomTrackView::addClip(QDomElement xml, int clipId, int track, GenTime startpos, const QRectF &rect, GenTime duration) { - QRect r(startpos.frames(m_document->fps()) * m_scale, m_tracksHeight * track, duration.frames(m_document->fps()) * m_scale, m_tracksHeight - 1); +void CustomTrackView::addClip(QDomElement xml, int clipId, ItemInfo info, EffectsList effects) { DocClipBase *baseclip = m_document->clipManager()->getClipById(clipId); - ClipItem *item = new ClipItem(baseclip, track, startpos, r, duration, m_document->fps()); + ClipItem *item = new ClipItem(baseclip, info, m_scale, m_document->fps()); + item->setEffectList(effects); scene()->addItem(item); baseclip->addReference(); m_document->updateClip(baseclip->getId()); - m_document->renderer()->mltInsertClip(m_tracksList.count() - track, startpos, xml); + info.track = m_tracksList.count() - info.track; + m_document->renderer()->mltInsertClip(info, xml, baseclip->producer()); + for (int i = 0; i < item->effectsCount(); i++) { + m_document->renderer()->mltAddEffect(info.track, info.startPos, item->getEffectArgs(item->effectAt(i)), false); + } m_document->renderer()->doRefresh(); } +void CustomTrackView::slotUpdateClip(int clipId) { + QList list = scene()->items(); + ClipItem *clip = NULL; + for (int i = 0; i < list.size(); ++i) { + if (list.at(i)->type() == AVWIDGET) { + clip = static_cast (list.at(i)); + if (clip->clipProducer() == clipId) { + clip->refreshClip(); + ItemInfo info = clip->info(); + info.track = m_tracksList.count() - clip->track(); + m_document->renderer()->mltUpdateClip(info, clip->xml(), clip->baseClip()->producer()); + } + } + } +} + ClipItem *CustomTrackView::getClipItemAt(int pos, int track) { - return (ClipItem *) scene()->itemAt(pos * m_scale, track * m_tracksHeight + m_tracksHeight / 2); + QList list = scene()->items(QPointF(pos * m_scale, track * m_tracksHeight + m_tracksHeight / 2)); + ClipItem *clip = NULL; + for (int i = 0; i < list.size(); ++i) { + if (list.at(i)->type() == AVWIDGET) { + clip = static_cast (list.at(i)); + break; + } + } + return clip; } ClipItem *CustomTrackView::getClipItemAt(GenTime pos, int track) { - return (ClipItem *) scene()->itemAt(pos.frames(m_document->fps()) * m_scale, track * m_tracksHeight + m_tracksHeight / 2); + int framepos = (int)(pos.frames(m_document->fps()) * m_scale); + return getClipItemAt(framepos, track); +} + +Transition *CustomTrackView::getTransitionItemAt(int pos, int track) { + QList list = scene()->items(QPointF(pos * m_scale, (track + 1) * m_tracksHeight)); + Transition *clip = NULL; + for (int i = 0; i < list.size(); ++i) { + if (list.at(i)->type() == TRANSITIONWIDGET) { + clip = static_cast (list.at(i)); + break; + } + } + return clip; +} + +Transition *CustomTrackView::getTransitionItemAt(GenTime pos, int track) { + int framepos = (int)(pos.frames(m_document->fps()) * m_scale); + return getTransitionItemAt(framepos, track); +} + +void CustomTrackView::moveClip(const ItemInfo start, const ItemInfo end) { + ClipItem *item = getClipItemAt((int) start.startPos.frames(m_document->fps()) + 1, start.track); + if (!item) { + emit displayMessage(i18n("Cannot move clip at time: %1s on track %2", QString::number(start.startPos.seconds(), 'g', 2), start.track), ErrorMessage); + kDebug() << "----------------  ERROR, CANNOT find clip to move at.. ";// << startPos.x() * m_scale * FRAME_SIZE + 1 << ", " << startPos.y() * m_tracksHeight + m_tracksHeight / 2; + return; + } + //kDebug() << "----------------  Move CLIP FROM: " << startPos.x() << ", END:" << endPos.x() << ",TRACKS: " << startPos.y() << " TO " << endPos.y(); + + bool success = m_document->renderer()->mltMoveClip((int)(m_tracksList.count() - start.track), (int)(m_tracksList.count() - end.track), (int) start.startPos.frames(m_document->fps()), (int)end.startPos.frames(m_document->fps())); + if (success) { + item->moveTo((int) end.startPos.frames(m_document->fps()), m_scale, (int)((end.track - start.track) * m_tracksHeight), end.track); + } else { + // undo last move and emit error message + emit displayMessage(i18n("Cannot move clip to position %1seconds", QString::number(end.startPos.seconds(), 'g', 2)), ErrorMessage); + } } -void CustomTrackView::moveClip(const QPointF &startPos, const QPointF &endPos) { - ClipItem *item = getClipItemAt(startPos.x() + 1, startPos.y()); +void CustomTrackView::moveTransition(const ItemInfo start, const ItemInfo end) { + Transition *item = getTransitionItemAt((int)start.startPos.frames(m_document->fps()) + 1, start.track); if (!item) { - kDebug() << "----------------  ERROR, CANNOT find clip to move at: " << startPos.x() * m_scale * FRAME_SIZE + 1 << ", " << startPos.y() * m_tracksHeight + m_tracksHeight / 2; + emit displayMessage(i18n("Cannot move transition at time: %1s on track %2", start.startPos.seconds(), start.track), ErrorMessage); + kDebug() << "----------------  ERROR, CANNOT find transition to move... ";// << startPos.x() * m_scale * FRAME_SIZE + 1 << ", " << startPos.y() * m_tracksHeight + m_tracksHeight / 2; return; } - kDebug() << "----------------  Move CLIP FROM: " << startPos.x() << ", END:" << endPos.x(); - item->moveTo(endPos.x(), m_scale, (endPos.y() - startPos.y()) * m_tracksHeight, endPos.y()); - m_document->renderer()->mltMoveClip(m_tracksList.count() - startPos.y(), m_tracksList.count() - endPos.y(), startPos.x(), endPos.x()); + //kDebug() << "----------------  Move TRANSITION FROM: " << startPos.x() << ", END:" << endPos.x() << ",TRACKS: " << oldtrack << " TO " << newtrack; + + //kDebug()<<"/// RESIZE TRANS START: ("<< startPos.x()<<"x"<< startPos.y()<<") / ("<moveTo((int) end.startPos.frames(m_document->fps()), m_scale, (end.track - start.track) * m_tracksHeight, end.track); + } else if (end.endPos == start.endPos) { + // Transition start resize + item->resizeStart((int) end.startPos.frames(m_document->fps()), m_scale); + } else { + // Transition end resize; + item->resizeEnd((int) end.endPos.frames(m_document->fps()), m_scale); + } + //item->moveTransition(GenTime((int) (endPos.x() - startPos.x()), m_document->fps())); + item->updateTransitionEndTrack(getPreviousVideoTrack(end.track)); + m_document->renderer()->mltMoveTransition(item->transitionTag(), m_tracksList.count() - start.track, m_tracksList.count() - end.track, item->transitionEndTrack(), start.startPos, start.endPos, end.startPos, end.endPos); } -void CustomTrackView::resizeClip(const QPointF &startPos, const QPointF &endPos, bool resizeClipStart) { - int offset; - if (resizeClipStart) offset = 1; - else offset = -1; - ClipItem *item = getClipItemAt(startPos.x() + offset, startPos.y()); +void CustomTrackView::resizeClip(const ItemInfo start, const ItemInfo end) { + int offset = 0; + bool resizeClipStart = true; + if (start.startPos == end.startPos) resizeClipStart = false; + /*if (resizeClipStart) offset = 1; + else offset = -1;*/ + ClipItem *item = getClipItemAt((int)(start.startPos.frames(m_document->fps()) + offset), start.track); if (!item) { - kDebug() << "----------------  ERROR, CANNOT find clip to resize at: " << startPos; + emit displayMessage(i18n("Cannot move clip at time: %1s on track %2", start.startPos.seconds(), start.track), ErrorMessage); + kDebug() << "----------------  ERROR, CANNOT find clip to resize at... "; // << startPos; return; } - qreal diff = endPos.x() - startPos.x(); if (resizeClipStart) { - m_document->renderer()->mltResizeClipStart(m_tracksList.count() - item->track(), item->endPos(), GenTime(endPos.x(), m_document->fps()), item->startPos(), item->cropStart() + GenTime(diff, m_document->fps()), item->cropStart() + GenTime(diff, m_document->fps()) + item->endPos() - GenTime(endPos.x(), m_document->fps())); - item->resizeStart(endPos.x(), m_scale); + ItemInfo clipinfo = item->info(); + clipinfo.track = m_tracksList.count() - clipinfo.track; + bool success = m_document->renderer()->mltResizeClipStart(clipinfo, item->startPos() - end.startPos); + if (success) { + item->resizeStart((int) end.startPos.frames(m_document->fps()), m_scale); + updateClipFade(item); + } else emit displayMessage(i18n("Error when resizing clip"), ErrorMessage); } else { - m_document->renderer()->mltResizeClipEnd(m_tracksList.count() - item->track(), item->startPos(), item->cropStart(), item->cropStart() + GenTime(endPos.x(), m_document->fps()) - item->startPos()); - item->resizeEnd(endPos.x(), m_scale); + ItemInfo clipinfo = item->info(); + clipinfo.track = m_tracksList.count() - clipinfo.track; + bool success = m_document->renderer()->mltResizeClipEnd(clipinfo, end.endPos - clipinfo.startPos); + if (success) { + item->resizeEnd((int) end.endPos.frames(m_document->fps()), m_scale); + updateClipFade(item, true); + } else emit displayMessage(i18n("Error when resizing clip"), ErrorMessage); } m_document->renderer()->doRefresh(); } +void CustomTrackView::updateClipFade(ClipItem * item, bool updateFadeOut) { + if (!updateFadeOut) { + int end = item->fadeIn(); + if (end != 0) { + // there is a fade in effect + QStringList clipeffects = item->effectNames(); + QDomElement oldeffect = item->effectAt(clipeffects.indexOf("Fade in")); + int start = item->cropStart().frames(m_document->fps()); + end += start; + EffectsList::setParameter(oldeffect, "in", QString::number(start)); + EffectsList::setParameter(oldeffect, "out", QString::number(end)); + QMap effectParams = item->getEffectArgs(oldeffect); + if (!m_document->renderer()->mltEditEffect(m_tracksList.count() - item->track(), item->startPos(), effectParams)) + emit displayMessage(i18n("Problem editing effect"), ErrorMessage); + } + } else { + int start = item->fadeOut(); + if (start != 0) { + // there is a fade in effect + QStringList clipeffects = item->effectNames(); + QDomElement oldeffect = item->effectAt(clipeffects.indexOf("Fade out")); + int end = (item->duration() - item->cropStart()).frames(m_document->fps()); + start = end - start; + EffectsList::setParameter(oldeffect, "in", QString::number(start)); + EffectsList::setParameter(oldeffect, "out", QString::number(end)); + QMap effectParams = item->getEffectArgs(oldeffect); + if (m_document->renderer()->mltEditEffect(m_tracksList.count() - item->track(), item->startPos(), effectParams)) + emit displayMessage(i18n("Problem editing effect"), ErrorMessage); + } + } +} + double CustomTrackView::getSnapPointForPos(double pos) { for (int i = 0; i < m_snapPoints.size(); ++i) { - if (abs(pos - m_snapPoints.at(i).frames(m_document->fps()) * m_scale) < 10) { + if (abs((int)(pos - m_snapPoints.at(i).frames(m_document->fps()) * m_scale)) < 10) { //kDebug()<<" FOUND SNAP POINT AT: "<fps()) * m_scale + 0.5; } @@ -864,44 +1568,389 @@ double CustomTrackView::getSnapPointForPos(double pos) { void CustomTrackView::updateSnapPoints(AbstractClipItem *selected) { m_snapPoints.clear(); + if (!KdenliveSettings::snaptopoints()) return; GenTime offset; if (selected) offset = selected->duration(); QList itemList = items(); for (int i = 0; i < itemList.count(); i++) { if (itemList.at(i)->type() == AVWIDGET && itemList.at(i) != selected) { - ClipItem *item = (ClipItem *)itemList.at(i); + ClipItem *item = static_cast (itemList.at(i)); GenTime start = item->startPos(); GenTime end = item->endPos(); m_snapPoints.append(start); m_snapPoints.append(end); + QList < GenTime > markers = item->snapMarkers(); + for (int i = 0; i < markers.size(); ++i) { + GenTime t = markers.at(i); + m_snapPoints.append(t); + if (t > offset) m_snapPoints.append(t - offset); + } + if (offset != GenTime()) { + if (start > offset) m_snapPoints.append(start - offset); + if (end > offset) m_snapPoints.append(end - offset); + } + } else if (itemList.at(i)->type() == TRANSITIONWIDGET) { + Transition *transition = static_cast (itemList.at(i)); + GenTime start = transition->startPos(); + GenTime end = transition->endPos(); + m_snapPoints.append(start); + m_snapPoints.append(end); if (offset != GenTime()) { if (start > offset) m_snapPoints.append(start - offset); if (end > offset) m_snapPoints.append(end - offset); } } } + + // add cursor position + GenTime pos = GenTime(m_cursorPos, m_document->fps()); + m_snapPoints.append(pos); + if (offset != GenTime()) m_snapPoints.append(pos - offset); + + // add guides + for (int i = 0; i < m_guides.count(); i++) { + m_snapPoints.append(m_guides.at(i)->position()); + if (offset != GenTime()) m_snapPoints.append(m_guides.at(i)->position() - offset); + } + qSort(m_snapPoints); //for (int i = 0; i < m_snapPoints.size(); ++i) // kDebug() << "SNAP POINT: " << m_snapPoints.at(i).frames(25); } +void CustomTrackView::slotSeekToPreviousSnap() { + updateSnapPoints(NULL); + GenTime pos = GenTime(m_cursorPos, m_document->fps()); + GenTime res = GenTime(); + for (int i = 0; i < m_snapPoints.size(); ++i) { + if (m_snapPoints.at(i) >= pos) { + if (i == 0) i = 1; + res = m_snapPoints.at(i - 1); + break; + } + } + setCursorPos((int) res.frames(m_document->fps())); + checkScrolling(); +} + +void CustomTrackView::slotSeekToNextSnap() { + updateSnapPoints(NULL); + GenTime pos = GenTime(m_cursorPos, m_document->fps()); + GenTime res = GenTime(m_projectDuration, m_document->fps()); + for (int i = 0; i < m_snapPoints.size(); ++i) { + if (m_snapPoints.at(i) > pos) { + res = m_snapPoints.at(i); + break; + } + } + setCursorPos((int) res.frames(m_document->fps())); + checkScrolling(); +} + +void CustomTrackView::clipStart() { + QList itemList = scene()->selectedItems(); + for (int i = 0; i < itemList.count(); i++) { + if (itemList.at(i)->type() == AVWIDGET) { + ClipItem *item = (ClipItem *) itemList.at(i); + setCursorPos((int) item->startPos().frames(m_document->fps())); + checkScrolling(); + break; + } + } +} + +void CustomTrackView::clipEnd() { + QList itemList = scene()->selectedItems(); + for (int i = 0; i < itemList.count(); i++) { + if (itemList.at(i)->type() == AVWIDGET) { + ClipItem *item = (ClipItem *) itemList.at(i); + setCursorPos((int) item->endPos().frames(m_document->fps())); + checkScrolling(); + break; + } + } +} + +void CustomTrackView::slotAddClipMarker() { + QList itemList = scene()->selectedItems(); + if (itemList.count() != 1) { + emit displayMessage(i18n("Cannot add marker if more than one clip is selected"), ErrorMessage); + kDebug() << "// CANNOT ADD MARKER IF MORE TAN ONE CLIP IS SELECTED...."; + return; + } + AbstractClipItem *item = (AbstractClipItem *)itemList.at(0); + if (item->type() != AVWIDGET) return; + GenTime pos = GenTime(m_cursorPos, m_document->fps()); + if (item->startPos() > pos || item->endPos() < pos) return; + ClipItem *clip = (ClipItem *) item; + int id = clip->baseClip()->getId(); + GenTime position = pos - item->startPos() + item->cropStart(); + CommentedTime marker(position, i18n("Marker")); + MarkerDialog d(clip->baseClip(), marker, m_document->timecode(), this); + if (d.exec() == QDialog::Accepted) { + slotAddClipMarker(id, d.newMarker().time(), d.newMarker().comment()); + } +} + +void CustomTrackView::slotAddClipMarker(int id, GenTime t, QString c) { + QString oldcomment = m_document->clipManager()->getClipById(id)->markerComment(t); + AddMarkerCommand *command = new AddMarkerCommand(this, oldcomment, c, id, t, true); + m_commandStack->push(command); +} + +void CustomTrackView::slotDeleteClipMarker() { + QList itemList = scene()->selectedItems(); + if (itemList.count() != 1) { + emit displayMessage(i18n("Cannot delete marker if more than one clip is selected"), ErrorMessage); + kDebug() << "// CANNOT DELETE MARKER IF MORE TAN ONE CLIP IS SELECTED...."; + return; + } + AbstractClipItem *item = (AbstractClipItem *)itemList.at(0); + if (item->type() != AVWIDGET) { + emit displayMessage(i18n("No clip selected"), ErrorMessage); + return; + } + GenTime pos = GenTime(m_cursorPos, m_document->fps()); + if (item->startPos() > pos || item->endPos() < pos) { + emit displayMessage(i18n("No selected clip at cursor time"), ErrorMessage); + return; + } + ClipItem *clip = (ClipItem *) item; + int id = clip->baseClip()->getId(); + GenTime position = pos - item->startPos() + item->cropStart(); + QString comment = clip->baseClip()->markerComment(position); + if (comment.isEmpty()) { + emit displayMessage(i18n("No marker found at cursor time"), ErrorMessage); + return; + } + AddMarkerCommand *command = new AddMarkerCommand(this, comment, QString(), id, position, true); + m_commandStack->push(command); +} + +void CustomTrackView::slotDeleteAllClipMarkers() { + QList itemList = scene()->selectedItems(); + if (itemList.count() != 1) { + emit displayMessage(i18n("Cannot delete marker if more than one clip is selected"), ErrorMessage); + kDebug() << "// CANNOT DELETE MARKER IF MORE TAN ONE CLIP IS SELECTED...."; + return; + } + AbstractClipItem *item = (AbstractClipItem *)itemList.at(0); + if (item->type() != AVWIDGET) { + emit displayMessage(i18n("No clip selected"), ErrorMessage); + return; + } + + ClipItem *clip = static_cast (item); + QList markers = clip->baseClip()->commentedSnapMarkers(); + + if (markers.isEmpty()) { + emit displayMessage(i18n("Clip has no markers"), ErrorMessage); + return; + } + int id = clip->baseClip()->getId(); + QUndoCommand *deleteMarkers = new QUndoCommand(); + deleteMarkers->setText("Delete clip markers"); + + for (int i = 0; i < markers.size(); i++) { + new AddMarkerCommand(this, markers.at(i).comment(), QString(), id, markers.at(i).time(), true, deleteMarkers); + } + m_commandStack->push(deleteMarkers); +} + +void CustomTrackView::slotEditClipMarker() { + QList itemList = scene()->selectedItems(); + if (itemList.count() != 1) { + emit displayMessage(i18n("Cannot edit marker if more than one clip is selected"), ErrorMessage); + kDebug() << "// CANNOT DELETE MARKER IF MORE TAN ONE CLIP IS SELECTED...."; + return; + } + AbstractClipItem *item = (AbstractClipItem *)itemList.at(0); + if (item->type() != AVWIDGET) { + emit displayMessage(i18n("No clip at cursor time"), ErrorMessage); + return; + } + GenTime pos = GenTime(m_cursorPos, m_document->fps()); + if (item->startPos() > pos || item->endPos() < pos) { + emit displayMessage(i18n("No selected clip at cursor time"), ErrorMessage); + return; + } + ClipItem *clip = (ClipItem *) item; + int id = clip->baseClip()->getId(); + GenTime position = pos - item->startPos() + item->cropStart(); + QString oldcomment = clip->baseClip()->markerComment(position); + if (oldcomment.isEmpty()) { + emit displayMessage(i18n("No marker found at cursor time"), ErrorMessage); + return; + } + + CommentedTime marker(position, oldcomment); + MarkerDialog d(clip->baseClip(), marker, m_document->timecode(), this); + if (d.exec() == QDialog::Accepted) { + if (d.newMarker().time() == position) { + // marker position was not changed, only text + AddMarkerCommand *command = new AddMarkerCommand(this, oldcomment, d.newMarker().comment(), id, position, true); + m_commandStack->push(command); + } else { + // marker text and position were changed, remove previous marker and add new one + AddMarkerCommand *command1 = new AddMarkerCommand(this, oldcomment, QString(), id, position, true); + AddMarkerCommand *command2 = new AddMarkerCommand(this, QString(), d.newMarker().comment(), id, d.newMarker().time(), true); + m_commandStack->push(command1); + m_commandStack->push(command2); + } + } +} + +void CustomTrackView::addMarker(const int id, const GenTime &pos, const QString comment) { + DocClipBase *base = m_document->clipManager()->getClipById(id); + if (!comment.isEmpty()) base->addSnapMarker(pos, comment); + else base->deleteSnapMarker(pos); + m_document->setModified(true); + viewport()->update(); +} + + + +void CustomTrackView::editGuide(const GenTime oldPos, const GenTime pos, const QString &comment) { + if (oldPos > GenTime() && pos > GenTime()) { + // move guide + for (int i = 0; i < m_guides.count(); i++) { + if (m_guides.at(i)->position() == oldPos) { + Guide *item = m_guides.at(i); + item->updateGuide(pos, comment); + break; + } + } + } else if (pos > GenTime()) addGuide(pos, comment); + else { + // remove guide + bool found = false; + for (int i = 0; i < m_guides.count(); i++) { + if (m_guides.at(i)->position() == oldPos) { + Guide *item = m_guides.takeAt(i); + delete item; + found = true; + break; + } + } + if (!found) emit displayMessage(i18n("No guide at cursor time"), ErrorMessage); + } + m_document->syncGuides(m_guides); +} + +bool CustomTrackView::addGuide(const GenTime pos, const QString &comment) { + for (int i = 0; i < m_guides.count(); i++) { + if (m_guides.at(i)->position() == pos) { + emit displayMessage(i18n("A guide already exists at that position"), ErrorMessage); + return false; + } + } + Guide *g = new Guide(this, pos, comment, m_scale, m_document->fps(), m_tracksHeight * m_tracksList.count()); + scene()->addItem(g); + m_guides.append(g); + m_document->syncGuides(m_guides); + return true; +} + +void CustomTrackView::slotAddGuide() { + CommentedTime marker(GenTime(m_cursorPos, m_document->fps()), i18n("Guide")); + MarkerDialog d(NULL, marker, m_document->timecode(), this); + if (d.exec() != QDialog::Accepted) return; + if (addGuide(d.newMarker().time(), d.newMarker().comment())) { + EditGuideCommand *command = new EditGuideCommand(this, GenTime(), QString(), d.newMarker().time(), d.newMarker().comment(), false); + m_commandStack->push(command); + } +} + +void CustomTrackView::slotEditGuide() { + GenTime pos = GenTime(m_cursorPos, m_document->fps()); + bool found = false; + for (int i = 0; i < m_guides.count(); i++) { + if (m_guides.at(i)->position() == pos) { + slotEditGuide(m_guides.at(i)->info()); + found = true; + break; + } + } + if (!found) emit displayMessage(i18n("No guide at cursor time"), ErrorMessage); +} + +void CustomTrackView::slotEditGuide(CommentedTime guide) { + MarkerDialog d(NULL, guide, m_document->timecode(), 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() { + GenTime pos = GenTime(m_cursorPos, m_document->fps()); + bool found = false; + for (int i = 0; i < m_guides.count(); i++) { + if (m_guides.at(i)->position() == pos) { + EditGuideCommand *command = new EditGuideCommand(this, m_guides.at(i)->position(), m_guides.at(i)->label(), GenTime(), QString(), true); + m_commandStack->push(command); + found = true; + break; + } + } + if (!found) emit displayMessage(i18n("No guide at cursor time"), ErrorMessage); +} + +void CustomTrackView::slotDeleteAllGuides() { + QUndoCommand *deleteAll = new QUndoCommand(); + deleteAll->setText("Delete all guides"); + for (int i = 0; i < m_guides.count(); i++) { + EditGuideCommand *command = new EditGuideCommand(this, m_guides.at(i)->position(), m_guides.at(i)->label(), GenTime(), QString(), true, deleteAll); + } + m_commandStack->push(deleteAll); +} + +void CustomTrackView::setTool(PROJECTTOOL tool) { + m_tool = tool; +} void CustomTrackView::setScale(double scaleFactor) { //scale(scaleFactor, scaleFactor); + m_animationTimer->stop(); + if (m_visualTip) { + delete m_visualTip; + m_visualTip = NULL; + } + if (m_animation) { + delete m_animation; + m_animation = NULL; + } double pos = cursorPos() / m_scale; m_scale = scaleFactor; + int vert = verticalScrollBar()->value(); kDebug() << " HHHHHHHH SCALING: " << m_scale; QList itemList = items(); - for (int i = 0; i < itemList.count(); i++) { if (itemList.at(i)->type() == AVWIDGET || itemList.at(i)->type() == TRANSITIONWIDGET) { AbstractClipItem *clip = (AbstractClipItem *)itemList.at(i); - clip->setRect(clip->startPos().frames(m_document->fps()) * m_scale, clip->rect().y(), clip->duration().frames(m_document->fps()) * m_scale, clip->rect().height()); + clip->setRect(0, 0, (qreal) clip->duration().frames(m_document->fps()) * m_scale - .5, clip->rect().height()); + clip->setPos((qreal) clip->startPos().frames(m_document->fps()) * m_scale, clip->pos().y()); } } + + for (int i = 0; i < m_guides.count(); i++) { + m_guides.at(i)->updatePosition(m_scale); + } + + setSceneRect(0, 0, (m_projectDuration + 100) * m_scale, sceneRect().height()); updateCursorPos(); centerOn(QPointF(cursorPos(), m_tracksHeight)); - scene()->setSceneRect(0, 0, (m_projectDuration + 500) * m_scale, scene()->sceneRect().height()); + verticalScrollBar()->setValue(vert); +} + +void CustomTrackView::slotRefreshGuides() { + if (KdenliveSettings::showmarkers()) { + kDebug() << "// refresh GUIDES"; + for (int i = 0; i < m_guides.count(); i++) { + m_guides.at(i)->update(); + } + } } void CustomTrackView::drawBackground(QPainter * painter, const QRectF & rect) { @@ -921,6 +1970,207 @@ void CustomTrackView::drawBackground(QPainter * painter, const QRectF & rect) { if (height() > lowerLimit) painter->fillRect(QRectF(rectInView.left(), lowerLimit, rectInView.width(), height() - lowerLimit), QBrush(base)); } + +bool CustomTrackView::findString(const QString &text) { + QString marker; + for (int i = 0; i < m_searchPoints.size(); ++i) { + marker = m_searchPoints.at(i).comment(); + if (marker.contains(text, Qt::CaseInsensitive)) { + setCursorPos(m_searchPoints.at(i).time().frames(m_document->fps()), true); + int vert = verticalScrollBar()->value(); + int hor = cursorPos(); + ensureVisible(hor, vert + 10, 2, 2, 50, 0); + m_findIndex = i; + return true; + } + } + return false; +} + +bool CustomTrackView::findNextString(const QString &text) { + QString marker; + for (int i = m_findIndex + 1; i < m_searchPoints.size(); ++i) { + marker = m_searchPoints.at(i).comment(); + if (marker.contains(text, Qt::CaseInsensitive)) { + setCursorPos(m_searchPoints.at(i).time().frames(m_document->fps()), true); + int vert = verticalScrollBar()->value(); + int hor = cursorPos(); + ensureVisible(hor, vert + 10, 2, 2, 50, 0); + m_findIndex = i; + return true; + } + } + m_findIndex = -1; + return false; +} + +void CustomTrackView::initSearchStrings() { + m_searchPoints.clear(); + QList itemList = items(); + for (int i = 0; i < itemList.count(); i++) { + // parse all clip names + if (itemList.at(i)->type() == AVWIDGET) { + ClipItem *item = static_cast (itemList.at(i)); + GenTime start = item->startPos(); + CommentedTime t(start, item->clipName()); + m_searchPoints.append(t); + // add all clip markers + QList < CommentedTime > markers = item->commentedSnapMarkers(); + m_searchPoints += markers; + } + } + + // add guides + for (int i = 0; i < m_guides.count(); i++) { + m_searchPoints.append(m_guides.at(i)->info()); + } + + qSort(m_searchPoints); +} + +void CustomTrackView::clearSearchStrings() { + m_searchPoints.clear(); + m_findIndex = 0; +} + +void CustomTrackView::copyClip() { + while (m_copiedItems.count() > 0) { + delete m_copiedItems.takeFirst(); + } + QList itemList = scene()->selectedItems(); + if (itemList.count() == 0) { + emit displayMessage(i18n("Select a clip before copying"), ErrorMessage); + return; + } + for (int i = 0; i < itemList.count(); i++) { + if (itemList.at(i)->type() == AVWIDGET) { + ClipItem *dup = static_cast (itemList.at(i)); + m_copiedItems.append(dup->clone(m_scale, dup->info())); + } else if (itemList.at(i)->type() == TRANSITIONWIDGET) { + Transition *dup = static_cast (itemList.at(i)); + m_copiedItems.append(dup->clone(m_scale)); + } + } +} + +bool CustomTrackView::canBePastedTo(ItemInfo info, int type) const { + QRectF rect((double) info.startPos.frames(m_document->fps()) * m_scale, (double)(info.track * m_tracksHeight + 1), (double)(info.endPos - info.startPos).frames(m_document->fps()) * m_scale, (double)(m_tracksHeight - 1)); + QList collisions = scene()->items(rect, Qt::IntersectsItemBoundingRect); + for (int i = 0; i < collisions.count(); i++) { + if (collisions.at(i)->type() == type) return false; + } + return true; +} + +bool CustomTrackView::canBePasted(QList items, GenTime offset, int trackOffset) const { + for (int i = 0; i < items.count(); i++) { + ItemInfo info = items.at(i)->info(); + info.startPos += offset; + info.endPos += offset; + info.track += trackOffset; + if (!canBePastedTo(info, items.at(i)->type())) return false; + } + return true; +} + +bool CustomTrackView::canBeMoved(QList items, GenTime offset, int trackOffset) const { + QPainterPath movePath; + movePath.moveTo(0, 0); + + for (int i = 0; i < items.count(); i++) { + ItemInfo info = items.at(i)->info(); + info.startPos = info.startPos + offset; + info.endPos = info.endPos + offset; + info.track = info.track + trackOffset; + if (info.startPos < GenTime()) { + // No clip should go below 0 + return false; + } + QRectF rect((double) info.startPos.frames(m_document->fps()) * m_scale, (double)(info.track * m_tracksHeight + 1), (double)(info.endPos - info.startPos).frames(m_document->fps()) * m_scale, (double)(m_tracksHeight - 1)); + movePath.addRect(rect); + } + QList collisions = scene()->items(movePath, Qt::IntersectsItemBoundingRect); + for (int i = 0; i < collisions.count(); i++) { + if ((collisions.at(i)->type() == AVWIDGET || collisions.at(i)->type() == TRANSITIONWIDGET) && !items.contains(static_cast (collisions.at(i)))) { + kDebug() << " //////////// CLIP COLLISION, MOVE NOT ALLOWED"; + return false; + } + } + return true; +} + +void CustomTrackView::pasteClip() { + if (m_copiedItems.count() == 0) { + emit displayMessage(i18n("No clip copied"), ErrorMessage); + return; + } + QPoint position; + if (m_menuPosition.isNull()) position = mapFromGlobal(QCursor::pos()); + else position = m_menuPosition; + GenTime pos = GenTime((int)(mapToScene(position).x() / m_scale), m_document->fps()); + int track = (int)(position.y() / m_tracksHeight); + ItemInfo first = m_copiedItems.at(0)->info(); + + GenTime offset = pos - first.startPos; + int trackOffset = track - first.track; + + if (!canBePasted(m_copiedItems, offset, trackOffset)) { + emit displayMessage(i18n("Cannot paste selected clips"), ErrorMessage); + return; + } + QUndoCommand *pasteClips = new QUndoCommand(); + pasteClips->setText("Paste clips"); + + for (int i = 0; i < m_copiedItems.count(); i++) { + // parse all clip names + if (m_copiedItems.at(i) && m_copiedItems.at(i)->type() == AVWIDGET) { + ClipItem *clip = static_cast (m_copiedItems.at(i)); + ItemInfo info; + info.startPos = clip->startPos() + offset; + info.endPos = clip->endPos() + offset; + info.cropStart = clip->cropStart(); + info.track = clip->track() + trackOffset; + if (canBePastedTo(info, AVWIDGET)) { + new AddTimelineClipCommand(this, clip->xml(), clip->clipProducer(), info, clip->effectList(), true, false, pasteClips); + } else emit displayMessage(i18n("Cannot paste clip to selected place"), ErrorMessage); + } else if (m_copiedItems.at(i) && m_copiedItems.at(i)->type() == TRANSITIONWIDGET) { + Transition *tr = static_cast (m_copiedItems.at(i)); + ItemInfo info; + info.startPos = tr->startPos() + offset; + info.endPos = tr->endPos() + offset; + info.track = tr->track() + trackOffset; + if (canBePastedTo(info, TRANSITIONWIDGET)) { + new AddTransitionCommand(this, info, tr->transitionEndTrack() + trackOffset, tr->toXML(), false, true, pasteClips); + } else emit displayMessage(i18n("Cannot paste transition to selected place"), ErrorMessage); + } + } + m_commandStack->push(pasteClips); +} + +void CustomTrackView::pasteClipEffects() { + if (m_copiedItems.count() != 1 || m_copiedItems.at(0)->type() != AVWIDGET) { + emit displayMessage(i18n("You must copy exactly one clip before pasting effects"), ErrorMessage); + return; + } + ClipItem *clip = static_cast < ClipItem *>(m_copiedItems.at(0)); + EffectsList effects = clip->effectList(); + + QUndoCommand *paste = new QUndoCommand(); + paste->setText("Paste effects"); + + QList clips = scene()->selectedItems(); + for (int i = 0; i < clips.count(); ++i) { + if (clips.at(i)->type() == AVWIDGET) { + ClipItem *item = static_cast < ClipItem *>(clips.at(i)); + for (int i = 0; i < clip->effectsCount(); i++) { + new AddEffectCommand(this, m_tracksList.count() - item->track(), item->startPos(), clip->effectAt(i), true, paste); + } + } + } + m_commandStack->push(paste); +} + + /* void CustomTrackView::drawForeground ( QPainter * painter, const QRectF & rect ) {