1 /***************************************************************************
2 * Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org) *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
20 #include <QMouseEvent>
21 #include <QStylePainter>
22 #include <QGraphicsItem>
23 #include <QDomDocument>
25 #include <QApplication>
26 #include <QInputDialog>
34 #include "customtrackview.h"
35 #include "docclipbase.h"
37 #include "definitions.h"
38 #include "moveclipcommand.h"
39 #include "movetransitioncommand.h"
40 #include "resizeclipcommand.h"
41 #include "editguidecommand.h"
42 #include "addtimelineclipcommand.h"
43 #include "addeffectcommand.h"
44 #include "editeffectcommand.h"
45 #include "moveeffectcommand.h"
46 #include "addtransitioncommand.h"
47 #include "edittransitioncommand.h"
48 #include "editkeyframecommand.h"
49 #include "addmarkercommand.h"
50 #include "razorclipcommand.h"
51 #include "kdenlivesettings.h"
52 #include "transition.h"
54 #include "customtrackview.h"
55 #include "clipmanager.h"
57 #include "markerdialog.h"
58 #include "mainwindow.h"
59 #include "ui_keyframedialog_ui.h"
60 #include "clipdurationdialog.h"
64 // disable animation if user asked it in KDE's global settings
65 // http://lists.kde.org/?l=kde-commits&m=120398724717624&w=2
66 // needs something like below (taken from dolphin)
67 // #include <kglobalsettings.h>
68 // const bool animate = KGlobalSettings::graphicEffectsLevel() & KGlobalSettings::SimpleAnimationEffects;
69 // const int duration = animate ? 1500 : 1;
71 CustomTrackView::CustomTrackView(KdenliveDoc *doc, QGraphicsScene * projectscene, QWidget *parent)
72 : 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()) {
73 if (doc) m_commandStack = doc->commandStack();
74 else m_commandStack == NULL;
75 setMouseTracking(true);
77 m_animationTimer = new QTimeLine(800);
78 m_animationTimer->setFrameRange(0, 5);
79 m_animationTimer->setUpdateInterval(100);
80 m_animationTimer->setLoopCount(0);
81 m_tipColor = QColor(0, 192, 0, 200);
82 QColor border = QColor(255, 255, 255, 100);
83 m_tipPen.setColor(border);
85 setContentsMargins(0, 0, 0, 0);
87 m_cursorLine = projectscene->addLine(0, 0, 0, m_tracksHeight);
88 m_cursorLine->setFlags(QGraphicsItem::ItemIsMovable);
89 m_cursorLine->setZValue(1000);
92 KIcon razorIcon("edit-cut");
93 m_razorCursor = QCursor(razorIcon.pixmap(22, 22));
94 verticalScrollBar()->setTracking(true);
95 connect(verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(slotRefreshGuides()));
98 CustomTrackView::~CustomTrackView() {
103 void CustomTrackView::setContextMenu(QMenu *timeline, QMenu *clip, QMenu *transition) {
104 m_timelineContextMenu = timeline;
105 m_timelineContextClipMenu = clip;
106 m_timelineContextTransitionMenu = transition;
109 void CustomTrackView::checkAutoScroll() {
110 m_autoScroll = KdenliveSettings::autoscroll();
113 QList <TrackInfo> CustomTrackView::tracksList() const {
117 void CustomTrackView::checkTrackHeight() {
118 if (m_tracksHeight == KdenliveSettings::trackheight()) return;
119 m_tracksHeight = KdenliveSettings::trackheight();
120 emit trackHeightChanged();
121 QList<QGraphicsItem *> itemList = items();
123 Transition *transitionitem;
124 for (int i = 0; i < itemList.count(); i++) {
125 if (itemList.at(i)->type() == AVWIDGET) {
126 item = (ClipItem*) itemList.at(i);
127 item->setRect(item->rect().x(), item->track() * m_tracksHeight, item->rect().width(), m_tracksHeight - 1);
129 } else if (itemList.at(i)->type() == TRANSITIONWIDGET) {
130 transitionitem = (Transition*) itemList.at(i);
131 transitionitem->setRect(transitionitem->rect().x(), transitionitem->track() * m_tracksHeight + m_tracksHeight / 2, transitionitem->rect().width(), m_tracksHeight - 1);
134 m_cursorLine->setLine(m_cursorLine->line().x1(), 0, m_cursorLine->line().x1(), m_tracksHeight * m_tracksList.count());
136 for (int i = 0; i < m_guides.count(); i++) {
137 QLineF l = m_guides.at(i)->line();
138 l.setP2(QPointF(l.x2(), m_tracksHeight * m_tracksList.count()));
139 m_guides.at(i)->setLine(l);
142 setSceneRect(0, 0, sceneRect().width(), m_tracksHeight * m_tracksList.count());
143 verticalScrollBar()->setMaximum(m_tracksHeight * m_tracksList.count());
148 void CustomTrackView::resizeEvent(QResizeEvent * event) {
149 QGraphicsView::resizeEvent(event);
153 void CustomTrackView::wheelEvent(QWheelEvent * e) {
154 if (e->modifiers() == Qt::ControlModifier) {
155 if (e->delta() > 0) emit zoomIn();
158 if (e->delta() > 0) horizontalScrollBar()->setValue(horizontalScrollBar()->value() + horizontalScrollBar()->singleStep());
159 else horizontalScrollBar()->setValue(horizontalScrollBar()->value() - horizontalScrollBar()->singleStep());
163 int CustomTrackView::getPreviousVideoTrack(int track) {
164 track = m_tracksList.count() - track - 1;
166 for (int i = track; i > -1; i--) {
167 if (m_tracksList.at(i).type == VIDEOTRACK) return i + 1;
174 void CustomTrackView::mouseMoveEvent(QMouseEvent * event) {
175 int pos = event->x();
176 emit mousePosition((int)(mapToScene(event->pos()).x() / m_scale));
177 if (event->buttons() & Qt::MidButton) return;
178 if (event->buttons() != Qt::NoButton) {
179 if (m_dragItem && m_tool == SELECTTOOL) { //event->button() == Qt::LeftButton) {
180 // a button was pressed, delete visual tips
181 if (m_operationMode == MOVE && (event->pos() - m_clickEvent).manhattanLength() >= QApplication::startDragDistance()) {
182 double snappedPos = getSnapPointForPos(mapToScene(event->pos()).x() - m_clickPoint.x());
183 //kDebug() << "/////// MOVE CLIP, EVENT Y: "<<m_clickPoint.y();//<<event->scenePos().y()<<", SCENE HEIGHT: "<<scene()->sceneRect().height();
184 int moveTrack = (int) mapToScene(event->pos() - QPoint(0, (m_dragItem->type() == TRANSITIONWIDGET ? m_dragItem->boundingRect().height() / 2 : 0))).y() / m_tracksHeight;
185 int currentTrack = m_dragItem->track();
187 if (moveTrack > 1000) moveTrack = 0;
188 else if (moveTrack > m_tracksList.count() - 1) moveTrack = m_tracksList.count() - 1;
189 else if (moveTrack < 0) moveTrack = 0;
191 int offset = moveTrack - currentTrack;
192 if (m_selectedClipList.count() == 1) m_dragItem->moveTo((int)(snappedPos / m_scale), m_scale, offset * m_tracksHeight, moveTrack);
194 int moveOffset = (int)(snappedPos / m_scale) - m_dragItem->startPos().frames(m_document->fps());
195 if (canBeMoved(m_selectedClipList, GenTime(moveOffset, m_document->fps()), offset)) {
196 for (int i = 0; i < m_selectedClipList.count(); i++) {
197 AbstractClipItem *item = m_selectedClipList.at(i);
198 item->moveTo(item->startPos().frames(m_document->fps()) + moveOffset, m_scale, offset * m_tracksHeight, item->track() + offset, false);
203 } else if (m_operationMode == RESIZESTART) {
204 double snappedPos = getSnapPointForPos(mapToScene(event->pos()).x());
205 m_dragItem->resizeStart((int)(snappedPos / m_scale), m_scale);
206 } else if (m_operationMode == RESIZEEND) {
207 double snappedPos = getSnapPointForPos(mapToScene(event->pos()).x());
208 m_dragItem->resizeEnd((int)(snappedPos / m_scale), m_scale);
209 } else if (m_operationMode == FADEIN) {
210 int pos = (int)(mapToScene(event->pos()).x() / m_scale);
211 ((ClipItem*) m_dragItem)->setFadeIn((int)(pos - m_dragItem->startPos().frames(m_document->fps())), m_scale);
212 } else if (m_operationMode == FADEOUT) {
213 int pos = (int)(mapToScene(event->pos()).x() / m_scale);
214 ((ClipItem*) m_dragItem)->setFadeOut((int)(m_dragItem->endPos().frames(m_document->fps()) - pos), m_scale);
215 } else if (m_operationMode == KEYFRAME) {
216 GenTime keyFramePos = GenTime((int)(mapToScene(event->pos()).x() / m_scale), m_document->fps()) - m_dragItem->startPos() + m_dragItem->cropStart();
217 double pos = mapToScene(event->pos()).toPoint().y();
218 QRectF br = m_dragItem->rect();
219 double maxh = 100.0 / br.height();
220 pos = (br.bottom() - pos) * maxh;
221 m_dragItem->updateKeyFramePos(keyFramePos, pos);
224 if (m_animation) delete m_animation;
226 if (m_visualTip) delete m_visualTip;
228 QGraphicsView::mouseMoveEvent(event);
230 } else if (m_operationMode == MOVEGUIDE) {
231 if (m_animation) delete m_animation;
233 if (m_visualTip) delete m_visualTip;
235 QGraphicsView::mouseMoveEvent(event);
240 if (m_tool == RAZORTOOL) {
241 setCursor(m_razorCursor);
242 //QGraphicsView::mouseMoveEvent(event);
246 QList<QGraphicsItem *> itemList = items(event->pos());
247 QGraphicsRectItem *item = NULL;
248 OPERATIONTYPE opMode = NONE;
250 if (itemList.count() == 1 && itemList.at(0)->type() == GUIDEITEM) {
252 } else for (int i = 0; i < itemList.count(); i++) {
253 if (itemList.at(i)->type() == AVWIDGET || itemList.at(i)->type() == TRANSITIONWIDGET) {
254 item = (QGraphicsRectItem*) itemList.at(i);
259 if (item && event->buttons() == Qt::NoButton) {
260 AbstractClipItem *clip = (AbstractClipItem*) item;
261 if (m_tool == RAZORTOOL) {
262 // razor tool over a clip, display current frame in monitor
263 if (item->type() == AVWIDGET) {
264 emit showClipFrame(((ClipItem *) item)->baseClip(), mapToScene(event->pos()).x() / m_scale - (clip->startPos() - clip->cropStart()).frames(m_document->fps()));
266 QGraphicsView::mouseMoveEvent(event);
269 opMode = clip->operationMode(mapToScene(event->pos()), m_scale);
271 if (opMode == m_moveOpMode) {
272 QGraphicsView::mouseMoveEvent(event);
276 if (m_animation) delete m_animation;
278 m_animationTimer->stop();
283 m_moveOpMode = opMode;
284 if (opMode == MOVE) {
285 setCursor(Qt::OpenHandCursor);
286 } else if (opMode == RESIZESTART) {
287 setCursor(KCursor("left_side", Qt::SizeHorCursor));
288 if (m_visualTip == NULL) {
290 polygon << QPoint((int)clip->rect().x(), (int)(clip->rect().y() + clip->rect().height() / 2 - size * 2));
291 polygon << QPoint((int)(clip->rect().x() + size * 2), (int)(clip->rect().y() + clip->rect().height() / 2));
292 polygon << QPoint((int)clip->rect().x(), (int)(clip->rect().y() + clip->rect().height() / 2 + size * 2));
293 polygon << QPoint((int)clip->rect().x(), (int)(clip->rect().y() + clip->rect().height() / 2 - size * 2));
295 m_visualTip = new QGraphicsPolygonItem(polygon);
296 ((QGraphicsPolygonItem*) m_visualTip)->setBrush(m_tipColor);
297 ((QGraphicsPolygonItem*) m_visualTip)->setPen(m_tipPen);
298 m_visualTip->setZValue(100);
299 m_animation = new QGraphicsItemAnimation;
300 m_animation->setItem(m_visualTip);
301 m_animation->setTimeLine(m_animationTimer);
302 m_visualTip->setPos(0, 0);
304 m_animation->setScaleAt(.5, scale, 1);
305 m_animation->setPosAt(.5, QPointF(clip->rect().x() - clip->rect().x() * scale, 0));
307 m_animation->setScaleAt(1, scale, 1);
308 m_animation->setPosAt(1, QPointF(clip->rect().x() - clip->rect().x() * scale, 0));
309 scene()->addItem(m_visualTip);
310 m_animationTimer->start();
312 } else if (opMode == RESIZEEND) {
313 setCursor(KCursor("right_side", Qt::SizeHorCursor));
314 if (m_visualTip == NULL) {
316 polygon << QPoint((int)(clip->rect().x() + clip->rect().width()), (int)(clip->rect().y() + clip->rect().height() / 2 - size * 2));
317 polygon << QPoint((int)(clip->rect().x() + clip->rect().width() - size * 2), (int)(clip->rect().y() + clip->rect().height() / 2));
318 polygon << QPoint((int)(clip->rect().x() + clip->rect().width()), (int)(clip->rect().y() + clip->rect().height() / 2 + size * 2));
319 polygon << QPoint((int)(clip->rect().x() + clip->rect().width()), (int)(clip->rect().y() + clip->rect().height() / 2 - size * 2));
321 m_visualTip = new QGraphicsPolygonItem(polygon);
322 ((QGraphicsPolygonItem*) m_visualTip)->setBrush(m_tipColor);
323 ((QGraphicsPolygonItem*) m_visualTip)->setPen(m_tipPen);
325 m_visualTip->setZValue(100);
326 m_animation = new QGraphicsItemAnimation;
327 m_animation->setItem(m_visualTip);
328 m_animation->setTimeLine(m_animationTimer);
329 m_visualTip->setPos(0, 0);
331 m_animation->setScaleAt(.5, scale, 1);
332 m_animation->setPosAt(.5, QPointF(clip->rect().x() - clip->rect().x() * scale - clip->rect().width(), 0));
334 m_animation->setScaleAt(1, scale, 1);
335 m_animation->setPosAt(1, QPointF(clip->rect().x() - clip->rect().x() * scale, 0));
336 scene()->addItem(m_visualTip);
337 m_animationTimer->start();
339 } else if (opMode == FADEIN) {
340 if (m_visualTip == NULL) {
341 ClipItem *item = (ClipItem *) clip;
342 m_visualTip = new QGraphicsEllipseItem(item->rect().x() + item->fadeIn() * m_scale - size, item->rect().y() - 8, size * 2, 16);
343 ((QGraphicsEllipseItem*) m_visualTip)->setBrush(m_tipColor);
344 ((QGraphicsEllipseItem*) m_visualTip)->setPen(m_tipPen);
345 m_visualTip->setZValue(100);
346 m_animation = new QGraphicsItemAnimation;
347 m_animation->setItem(m_visualTip);
348 m_animation->setTimeLine(m_animationTimer);
349 m_visualTip->setPos(0, 0);
351 m_animation->setScaleAt(.5, scale, scale);
352 m_animation->setPosAt(.5, QPointF(item->rect().x() - item->rect().x() * scale - item->fadeIn() * m_scale, item->rect().y() - item->rect().y() * scale));
354 m_animation->setScaleAt(1, scale, scale);
355 m_animation->setPosAt(1, QPointF(item->rect().x() - item->rect().x() * scale, item->rect().y() - item->rect().y() * scale));
356 scene()->addItem(m_visualTip);
357 m_animationTimer->start();
359 setCursor(Qt::PointingHandCursor);
360 } else if (opMode == FADEOUT) {
361 if (m_visualTip == NULL) {
362 ClipItem *item = (ClipItem *) clip;
363 m_visualTip = new QGraphicsEllipseItem(item->rect().x() + item->rect().width() - item->fadeOut() * m_scale - size, item->rect().y() - 8, size*2, 16);
364 ((QGraphicsEllipseItem*) m_visualTip)->setBrush(m_tipColor);
365 ((QGraphicsEllipseItem*) m_visualTip)->setPen(m_tipPen);
366 m_visualTip->setZValue(100);
367 m_animation = new QGraphicsItemAnimation;
368 m_animation->setItem(m_visualTip);
369 m_animation->setTimeLine(m_animationTimer);
370 m_visualTip->setPos(0, 0);
372 m_animation->setScaleAt(.5, scale, scale);
373 m_animation->setPosAt(.5, QPointF(item->rect().x() - item->rect().x() * scale - item->rect().width() + item->fadeOut() * m_scale, item->rect().y() - item->rect().y() * scale));
375 m_animation->setScaleAt(1, scale, scale);
376 m_animation->setPosAt(1, QPointF(item->rect().x() - item->rect().x() * scale, item->rect().y() - item->rect().y() * scale));
377 scene()->addItem(m_visualTip);
378 m_animationTimer->start();
380 setCursor(Qt::PointingHandCursor);
381 } else if (opMode == TRANSITIONSTART) {
382 if (m_visualTip == NULL) {
383 m_visualTip = new QGraphicsEllipseItem(-5, -5 , 10, 10);
384 ((QGraphicsEllipseItem*) m_visualTip)->setBrush(m_tipColor);
385 ((QGraphicsEllipseItem*) m_visualTip)->setPen(m_tipPen);
386 m_visualTip->setZValue(100);
387 m_animation = new QGraphicsItemAnimation;
388 m_animation->setItem(m_visualTip);
389 m_animation->setTimeLine(m_animationTimer);
390 m_visualTip->setPos(clip->rect().x() + 10, clip->rect().y() + clip->rect().height() / 2 + 12);
392 m_animation->setScaleAt(.5, scale, scale);
394 m_animation->setScaleAt(1, scale, scale);
395 scene()->addItem(m_visualTip);
396 m_animationTimer->start();
398 setCursor(Qt::PointingHandCursor);
399 } else if (opMode == TRANSITIONEND) {
400 if (m_visualTip == NULL) {
401 m_visualTip = new QGraphicsEllipseItem(-5, -5 , 10, 10);
402 ((QGraphicsEllipseItem*) m_visualTip)->setBrush(m_tipColor);
403 ((QGraphicsEllipseItem*) m_visualTip)->setPen(m_tipPen);
404 m_visualTip->setZValue(100);
405 m_animation = new QGraphicsItemAnimation;
406 m_animation->setItem(m_visualTip);
407 m_animation->setTimeLine(m_animationTimer);
408 m_visualTip->setPos(clip->rect().x() + clip->rect().width() - 10 , clip->rect().y() + clip->rect().height() / 2 + 12);
410 m_animation->setScaleAt(.5, scale, scale);
412 m_animation->setScaleAt(1, scale, scale);
413 scene()->addItem(m_visualTip);
414 m_animationTimer->start();
416 setCursor(Qt::PointingHandCursor);
417 } else if (opMode == KEYFRAME) {
418 setCursor(Qt::PointingHandCursor);
420 } // no clip under mouse
421 else if (m_tool == RAZORTOOL) {
422 QGraphicsView::mouseMoveEvent(event);
424 } else if (opMode == MOVEGUIDE) {
425 m_moveOpMode = opMode;
426 setCursor(Qt::SplitHCursor);
429 if (event->buttons() != Qt::NoButton && event->modifiers() == Qt::NoModifier) {
430 setCursorPos((int)(mapToScene(event->pos().x(), 0).x() / m_scale));
433 if (m_animation) delete m_animation;
434 m_animationTimer->stop();
440 setCursor(Qt::ArrowCursor);
442 QGraphicsView::mouseMoveEvent(event);
446 void CustomTrackView::mousePressEvent(QMouseEvent * event) {
447 m_menuPosition = QPoint();
449 m_clickEvent = event->pos();
450 QList<QGraphicsItem *> collisionList = items(event->pos());
451 if (event->button() == Qt::MidButton) {
452 m_document->renderer()->switchPlay();
455 if (event->modifiers() == Qt::ControlModifier && collisionList.count() == 0) {
456 setDragMode(QGraphicsView::ScrollHandDrag);
457 QGraphicsView::mousePressEvent(event);
459 } else if (event->modifiers() == Qt::ShiftModifier && collisionList.count() == 0) {
460 setDragMode(QGraphicsView::RubberBandDrag);
461 QGraphicsView::mousePressEvent(event);
464 bool collision = false;
465 if (collisionList.count() == 1 && collisionList.at(0)->type() == GUIDEITEM) {
466 // a guide item was pressed
467 collisionList.at(0)->setFlag(QGraphicsItem::ItemIsMovable, true);
469 m_dragGuide = (Guide *) collisionList.at(0);
471 m_operationMode = MOVEGUIDE;
472 // deselect all clips so that only the guide will move
473 QList<QGraphicsItem *> clips = scene()->selectedItems();
474 for (int i = 0; i < clips.count(); ++i)
475 clips.at(i)->setSelected(false);
476 updateSnapPoints(NULL);
477 QGraphicsView::mousePressEvent(event);
478 } else for (int i = 0; i < collisionList.count(); ++i) {
479 QGraphicsItem *item = collisionList.at(i);
480 if (item->type() == AVWIDGET || item->type() == TRANSITIONWIDGET) {
481 if (m_tool == RAZORTOOL) {
482 if (item->type() == TRANSITIONWIDGET) {
483 emit displayMessage(i18n("Cannot cut a transition"), ErrorMessage);
486 AbstractClipItem *clip = (AbstractClipItem *) item;
488 info.startPos = clip->startPos();
489 info.endPos = clip->endPos();
490 info.track = clip->track();
491 RazorClipCommand* command = new RazorClipCommand(this, info, GenTime((int)(mapToScene(event->pos()).x() / m_scale), m_document->fps()), true);
492 m_commandStack->push(command);
493 m_document->setModified(true);
497 if (!item->isSelected()) {
499 if (event->modifiers() != Qt::ControlModifier) {
500 QList<QGraphicsItem *> itemList = items();
501 for (int i = 0; i < itemList.count(); i++) {
502 itemList.at(i)->setSelected(false);
503 itemList.at(i)->update();
506 item->setSelected(true);
510 m_dragItem = (AbstractClipItem *) item;
512 m_clickPoint = QPoint((int)(mapToScene(event->pos()).x() - m_dragItem->startPos().frames(m_document->fps()) * m_scale), (int)(event->pos().y() - m_dragItem->rect().top()));
513 m_dragItemInfo.startPos = m_dragItem->startPos();
514 m_dragItemInfo.endPos = m_dragItem->endPos();
515 m_dragItemInfo.track = m_dragItem->track();
517 m_selectedClipList.clear();
518 QList<QGraphicsItem *> selected = scene()->selectedItems();
519 for (int i = 0; i < selected.count(); i++) {
520 if (selected.at(i)->type() == AVWIDGET || selected.at(i)->type() == TRANSITIONWIDGET)
521 m_selectedClipList.append(static_cast <AbstractClipItem *>(selected.at(i)));
524 m_operationMode = m_dragItem->operationMode(item->mapFromScene(mapToScene(event->pos())), m_scale);
525 if (m_operationMode == KEYFRAME) {
526 m_dragItem->updateSelectedKeyFrame();
528 } else if (m_operationMode == MOVE) setCursor(Qt::ClosedHandCursor);
529 else if (m_operationMode == TRANSITIONSTART) {
531 info.startPos = m_dragItem->startPos();
532 info.track = m_dragItem->track();
533 int transitiontrack = getPreviousVideoTrack(info.track);
534 ClipItem *transitionClip = NULL;
535 if (transitiontrack != 0) transitionClip = getClipItemAt((int) info.startPos.frames(m_document->fps()), m_tracksList.count() - transitiontrack);
536 if (transitionClip && transitionClip->endPos() < m_dragItem->endPos()) {
537 info.endPos = transitionClip->endPos();
538 } else info.endPos = info.startPos + GenTime(2.5);
540 slotAddTransition((ClipItem *) m_dragItem, info, transitiontrack);
541 } else if (m_operationMode == TRANSITIONEND) {
543 info.endPos = m_dragItem->endPos();
544 info.track = m_dragItem->track();
545 int transitiontrack = getPreviousVideoTrack(info.track);
546 ClipItem *transitionClip = NULL;
547 if (transitiontrack != 0) transitionClip = getClipItemAt((int) info.endPos.frames(m_document->fps()), m_tracksList.count() - transitiontrack);
548 if (transitionClip && transitionClip->startPos() > m_dragItem->startPos()) {
549 info.startPos = transitionClip->startPos();
550 } else info.startPos = info.endPos - GenTime(2.5);
551 slotAddTransition((ClipItem *) m_dragItem, info, transitiontrack);
553 updateSnapPoints(m_dragItem);
559 kDebug() << "//////// NO ITEM FOUND ON CLICK";
561 setCursor(Qt::ArrowCursor);
562 QList<QGraphicsItem *> itemList = items();
563 for (int i = 0; i < itemList.count(); i++)
564 itemList.at(i)->setSelected(false);
565 //emit clipItemSelected(NULL);
566 if (event->button() == Qt::RightButton) {
567 displayContextMenu(event->globalPos());
568 m_menuPosition = event->pos();
569 } else setCursorPos((int)(mapToScene(event->x(), 0).x() / m_scale));
570 } else if (event->button() == Qt::RightButton) {
571 m_operationMode = NONE;
572 displayContextMenu(event->globalPos(), m_dragItem);
575 if (m_dragItem && m_dragItem->type() == AVWIDGET) emit clipItemSelected((ClipItem*) m_dragItem);
576 else emit clipItemSelected(NULL);
579 //QGraphicsView::mousePressEvent(event);
582 void CustomTrackView::mouseDoubleClickEvent(QMouseEvent *event) {
583 if (m_dragItem && m_dragItem->hasKeyFrames()) {
584 if (m_moveOpMode == KEYFRAME) {
585 // user double clicked on a keyframe, open edit dialog
586 QDialog d(parentWidget());
587 Ui::KeyFrameDialog_UI view;
589 view.kfr_position->setText(m_document->timecode().getTimecode(GenTime(m_dragItem->selectedKeyFramePos(), m_document->fps()) - m_dragItem->cropStart(), m_document->fps()));
590 view.kfr_value->setValue(m_dragItem->selectedKeyFrameValue());
591 view.kfr_value->setFocus();
592 if (d.exec() == QDialog::Accepted) {
593 int pos = m_document->timecode().getFrameCount(view.kfr_position->text(), m_document->fps());
594 m_dragItem->updateKeyFramePos(GenTime(pos, m_document->fps()) + m_dragItem->cropStart(), (double) view.kfr_value->value() * m_dragItem->keyFrameFactor());
595 ClipItem *item = (ClipItem *)m_dragItem;
596 QString previous = item->keyframes(item->selectedEffectIndex());
597 item->updateKeyframeEffect();
598 QString next = item->keyframes(item->selectedEffectIndex());
599 EditKeyFrameCommand *command = new EditKeyFrameCommand(this, item->track(), item->startPos(), item->selectedEffectIndex(), previous, next, false);
600 m_commandStack->push(command);
601 updateEffect(m_tracksList.count() - item->track(), item->startPos(), item->selectedEffect(), item->selectedEffectIndex());
606 GenTime keyFramePos = GenTime((int)(mapToScene(event->pos()).x() / m_scale), m_document->fps()) - m_dragItem->startPos() + m_dragItem->cropStart();
607 m_dragItem->addKeyFrame(keyFramePos, mapToScene(event->pos()).toPoint().y());
608 ClipItem * item = (ClipItem *) m_dragItem;
609 QString previous = item->keyframes(item->selectedEffectIndex());
610 item->updateKeyframeEffect();
611 QString next = item->keyframes(item->selectedEffectIndex());
612 EditKeyFrameCommand *command = new EditKeyFrameCommand(this, m_dragItem->track(), m_dragItem->startPos(), item->selectedEffectIndex(), previous, next, false);
613 m_commandStack->push(command);
614 updateEffect(m_tracksList.count() - item->track(), item->startPos(), item->selectedEffect(), item->selectedEffectIndex());
616 } else if (m_dragItem) {
617 ClipDurationDialog d(m_dragItem, m_document->timecode(), this);
618 if (d.exec() == QDialog::Accepted) {
619 if (d.startPos() != m_dragItem->startPos()) {
620 if (m_dragItem->type() == AVWIDGET) {
622 startInfo.startPos = m_dragItem->startPos();
623 startInfo.endPos = m_dragItem->endPos();
624 startInfo.track = m_dragItem->track();
626 endInfo.startPos = d.startPos();
627 endInfo.endPos = m_dragItem->endPos() + (endInfo.startPos - startInfo.startPos);
628 endInfo.track = m_dragItem->track();
629 MoveClipCommand *command = new MoveClipCommand(this, startInfo, endInfo, true);
630 m_commandStack->push(command);
632 //TODO: move transition
635 if (d.duration() != m_dragItem->duration()) {
636 if (m_dragItem->type() == AVWIDGET) {
638 startInfo.startPos = m_dragItem->startPos();
639 startInfo.endPos = m_dragItem->endPos();
640 startInfo.track = m_dragItem->track();
642 endInfo.startPos = startInfo.startPos;
643 endInfo.endPos = endInfo.startPos + d.duration();
644 endInfo.track = m_dragItem->track();
645 ResizeClipCommand *command = new ResizeClipCommand(this, startInfo, endInfo, true);
646 m_commandStack->push(command);
648 //TODO: resize transition
653 QList<QGraphicsItem *> collisionList = items(event->pos());
654 if (collisionList.count() == 1 && collisionList.at(0)->type() == GUIDEITEM) {
655 Guide *editGuide = (Guide *) collisionList.at(0);
656 if (editGuide) slotEditGuide(editGuide->info());
662 void CustomTrackView::editKeyFrame(const GenTime pos, const int track, const int index, const QString keyframes) {
663 ClipItem *clip = getClipItemAt((int)pos.frames(m_document->fps()), track);
665 clip->setKeyframes(index, keyframes);
666 updateEffect(m_tracksList.count() - clip->track(), clip->startPos(), clip->effectAt(index), index);
667 } else emit displayMessage(i18n("Cannot find clip with keyframe"), ErrorMessage);
671 void CustomTrackView::displayContextMenu(QPoint pos, AbstractClipItem *clip) {
672 if (clip == NULL) m_timelineContextMenu->popup(pos);
673 else if (clip->type() == AVWIDGET) m_timelineContextClipMenu->popup(pos);
674 else if (clip->type() == TRANSITIONWIDGET) m_timelineContextTransitionMenu->popup(pos);
677 void CustomTrackView::activateMonitor() {
678 emit activateDocumentMonitor();
681 void CustomTrackView::dragEnterEvent(QDragEnterEvent * event) {
682 if (event->mimeData()->hasFormat("kdenlive/producerslist")) {
683 kDebug() << "/////////////// DRAG ENTERED, TEXT: " << event->mimeData()->data("kdenlive/producerslist");
684 QStringList ids = QString(event->mimeData()->data("kdenlive/producerslist")).split(";");
685 //TODO: drop of several clips
686 for (int i = 0; i < ids.size(); ++i) {
688 DocClipBase *clip = m_document->getBaseClip(ids.at(0).toInt());
689 if (clip == NULL) kDebug() << " WARNING))))))))) CLIP NOT FOUND : " << ids.at(0).toInt();
690 addItem(clip, event->pos());
691 event->acceptProposedAction();
692 } else QGraphicsView::dragEnterEvent(event);
695 void CustomTrackView::slotRefreshEffects(ClipItem *clip) {
696 int track = m_tracksList.count() - clip->track();
697 GenTime pos = clip->startPos();
698 if (!m_document->renderer()->mltRemoveEffect(track, pos, "-1", false)) {
699 emit displayMessage(i18n("Problem deleting effect"), ErrorMessage);
703 for (int i = 0; i < clip->effectsCount(); i++) {
704 if (!m_document->renderer()->mltAddEffect(track, pos, clip->getEffectArgs(clip->effectAt(i)), false)) success = false;
706 if (!success) emit displayMessage(i18n("Problem adding effect to clip"), ErrorMessage);
707 m_document->renderer()->doRefresh();
710 void CustomTrackView::addEffect(int track, GenTime pos, QDomElement effect) {
711 ClipItem *clip = getClipItemAt((int)pos.frames(m_document->fps()) + 1, m_tracksList.count() - track);
713 QMap <QString, QString> effectParams = clip->addEffect(effect);
714 if (!m_document->renderer()->mltAddEffect(track, pos, effectParams))
715 emit displayMessage(i18n("Problem adding effect to clip"), ErrorMessage);
716 emit clipItemSelected(clip);
717 } else emit displayMessage(i18n("Cannot find clip to add effect"), ErrorMessage);
720 void CustomTrackView::deleteEffect(int track, GenTime pos, QDomElement effect) {
721 QString index = effect.attribute("kdenlive_ix");
722 if (!m_document->renderer()->mltRemoveEffect(track, pos, index)) {
723 emit displayMessage(i18n("Problem deleting effect"), ErrorMessage);
726 ClipItem *clip = getClipItemAt((int)pos.frames(m_document->fps()) + 1, m_tracksList.count() - track);
728 clip->deleteEffect(index);
729 emit clipItemSelected(clip);
733 void CustomTrackView::slotAddEffect(QDomElement effect, GenTime pos, int track) {
734 QList<QGraphicsItem *> itemList;
735 if (track == -1) itemList = scene()->selectedItems();
736 if (itemList.isEmpty()) {
737 ClipItem *clip = getClipItemAt((int)pos.frames(m_document->fps()) + 1, track);
738 if (clip) itemList.append(clip);
739 else emit displayMessage(i18n("Select a clip if you want to apply an effect"), ErrorMessage);
741 kDebug() << "// REQUESTING EFFECT ON CLIP: " << pos.frames(25) << ", TRK: " << track << "SELECTED ITEMS: " << itemList.count();
742 for (int i = 0; i < itemList.count(); i++) {
743 if (itemList.at(i)->type() == AVWIDGET) {
744 ClipItem *item = (ClipItem *)itemList.at(i);
745 item->initEffect(effect);
746 AddEffectCommand *command = new AddEffectCommand(this, m_tracksList.count() - item->track(), item->startPos(), effect, true);
747 m_commandStack->push(command);
750 m_document->setModified(true);
753 void CustomTrackView::slotDeleteEffect(ClipItem *clip, QDomElement effect) {
754 AddEffectCommand *command = new AddEffectCommand(this, m_tracksList.count() - clip->track(), clip->startPos(), effect, false);
755 m_commandStack->push(command);
756 m_document->setModified(true);
759 void CustomTrackView::updateEffect(int track, GenTime pos, QDomElement effect, int ix) {
760 ClipItem *clip = getClipItemAt((int)pos.frames(m_document->fps()) + 1, m_tracksList.count() - track);
762 QMap <QString, QString> effectParams = clip->getEffectArgs(effect);
763 if (effectParams.value("disabled") == "1") {
764 if (m_document->renderer()->mltRemoveEffect(track, pos, effectParams.value("kdenlive_ix"))) {
765 kDebug() << "////// DISABLING EFFECT: " << index << ", CURRENTLA: " << clip->selectedEffectIndex();
766 } else emit displayMessage(i18n("Problem deleting effect"), ErrorMessage);
767 } else if (!m_document->renderer()->mltEditEffect(m_tracksList.count() - clip->track(), clip->startPos(), effectParams))
768 emit displayMessage(i18n("Problem editing effect"), ErrorMessage);
769 if (ix == clip->selectedEffectIndex()) {
770 clip->setSelectedEffect(clip->selectedEffectIndex());
773 m_document->setModified(true);
776 void CustomTrackView::moveEffect(int track, GenTime pos, int oldPos, int newPos) {
777 ClipItem *clip = getClipItemAt((int)pos.frames(m_document->fps()) + 1, m_tracksList.count() - track);
779 m_document->renderer()->mltMoveEffect(track, pos, oldPos, newPos);
781 m_document->setModified(true);
784 void CustomTrackView::slotChangeEffectState(ClipItem *clip, int effectPos, bool disable) {
785 QDomElement effect = clip->effectAt(effectPos);
786 QDomElement oldEffect = effect.cloneNode().toElement();
787 effect.setAttribute("disabled", disable);
788 EditEffectCommand *command = new EditEffectCommand(this, m_tracksList.count() - clip->track(), clip->startPos(), oldEffect, effect, effectPos, true);
789 m_commandStack->push(command);
790 m_document->setModified(true);
793 void CustomTrackView::slotChangeEffectPosition(ClipItem *clip, int currentPos, int newPos) {
794 MoveEffectCommand *command = new MoveEffectCommand(this, m_tracksList.count() - clip->track(), clip->startPos(), currentPos, newPos, true);
795 m_commandStack->push(command);
796 m_document->setModified(true);
799 void CustomTrackView::slotUpdateClipEffect(ClipItem *clip, QDomElement oldeffect, QDomElement effect, int ix) {
800 EditEffectCommand *command = new EditEffectCommand(this, m_tracksList.count() - clip->track(), clip->startPos(), oldeffect, effect, ix, true);
801 m_commandStack->push(command);
804 void CustomTrackView::cutClip(ItemInfo info, GenTime cutTime, bool cut) {
807 ClipItem *item = getClipItemAt((int) info.startPos.frames(m_document->fps()), info.track);
809 emit displayMessage(i18n("Cannot find clip to cut"), ErrorMessage);
812 int cutPos = (int) cutTime.frames(m_document->fps());
814 newPos.startPos = cutTime;
815 newPos.endPos = info.endPos;
816 newPos.track = info.track;
817 item->resizeEnd(cutPos, m_scale);
818 ClipItem *dup = new ClipItem(item->baseClip(), newPos, item->cropStart() + (cutTime - info.startPos), m_scale, m_document->fps());
819 scene()->addItem(dup);
820 m_document->renderer()->mltCutClip(m_tracksList.count() - info.track, cutTime);
821 item->baseClip()->addReference();
822 m_document->updateClip(item->baseClip()->getId());
825 ClipItem *item = getClipItemAt((int) info.startPos.frames(m_document->fps()), info.track);
826 ClipItem *dup = getClipItemAt((int) cutTime.frames(m_document->fps()), info.track);
828 emit displayMessage(i18n("Cannot find clip to uncut"), ErrorMessage);
832 item->baseClip()->removeReference();
833 m_document->updateClip(item->baseClip()->getId());
834 item->resizeEnd((int) info.endPos.frames(m_document->fps()), m_scale);
835 m_document->renderer()->mltRemoveClip(m_tracksList.count() - info.track, cutTime);
836 m_document->renderer()->mltResizeClipEnd(m_tracksList.count() - info.track, info.startPos, item->cropStart(), item->cropStart() + info.endPos - info.startPos);
841 void CustomTrackView::slotAddTransitionToSelectedClips(QDomElement transition) {
842 QList<QGraphicsItem *> itemList = scene()->selectedItems();
843 for (int i = 0; i < itemList.count(); i++) {
844 if (itemList.at(i)->type() == AVWIDGET) {
845 ClipItem *item = (ClipItem *) itemList.at(i);
847 info.startPos = item->startPos();
848 info.endPos = info.startPos + GenTime(2.5);
849 info.track = item->track();
850 int transitiontrack = getPreviousVideoTrack(info.track);
851 slotAddTransition(item, info, transitiontrack, transition);
856 void CustomTrackView::slotAddTransition(ClipItem* clip, ItemInfo transitionInfo, int endTrack, QDomElement transition) {
857 AddTransitionCommand* command = new AddTransitionCommand(this, transitionInfo, endTrack, transition, false, true);
858 m_commandStack->push(command);
859 m_document->setModified(true);
862 void CustomTrackView::addTransition(ItemInfo transitionInfo, int endTrack, QDomElement params) {
863 Transition *tr = new Transition(transitionInfo, endTrack, m_scale, m_document->fps(), params);
864 scene()->addItem(tr);
866 //kDebug() << "---- ADDING transition " << e.attribute("tag") << ", on tracks " << m_tracksList.count() - e.attribute("transition_track").toInt() << " / " << getPreviousVideoTrack(e.attribute("transition_track").toInt());
867 m_document->renderer()->mltAddTransition(tr->transitionTag(), endTrack, m_tracksList.count() - transitionInfo.track, transitionInfo.startPos, transitionInfo.endPos, tr->toXML());
868 m_document->setModified(true);
871 void CustomTrackView::deleteTransition(ItemInfo transitionInfo, int endTrack, QDomElement params) {
872 Transition *item = getTransitionItemAt((int)transitionInfo.startPos.frames(m_document->fps()) + 1, transitionInfo.track);
873 m_document->renderer()->mltDeleteTransition(item->transitionTag(), endTrack, m_tracksList.count() - transitionInfo.track, transitionInfo.startPos, transitionInfo.endPos, item->toXML());
875 emit transitionItemSelected(NULL);
876 m_document->setModified(true);
879 void CustomTrackView::slotTransitionUpdated(Transition *tr, QDomElement old) {
880 EditTransitionCommand *command = new EditTransitionCommand(this, tr->track(), tr->startPos(), old, tr->toXML() , true);
881 m_commandStack->push(command);
882 m_document->setModified(true);
885 void CustomTrackView::updateTransition(int track, GenTime pos, QDomElement oldTransition, QDomElement transition) {
886 Transition *item = getTransitionItemAt((int)pos.frames(m_document->fps()) + 1, track);
888 kWarning() << "Unable to find transition at pos :" << pos.frames(m_document->fps()) << ", ON track: " << track;
891 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);
892 item->setTransitionParameters(transition);
893 m_document->setModified(true);
896 void CustomTrackView::addItem(DocClipBase *clip, QPoint pos) {
898 info.startPos = GenTime((int)(mapToScene(pos).x() / m_scale), m_document->fps());
899 info.endPos = info.startPos + clip->duration();
900 info.track = (int)(pos.y() / m_tracksHeight);
901 //kDebug()<<"------------ ADDING CLIP ITEM----: "<<info.startPos.frames(25)<<", "<<info.endPos.frames(25)<<", "<<info.track;
902 m_dropItem = new ClipItem(clip, info, GenTime(), m_scale, m_document->fps());
903 scene()->addItem(m_dropItem);
907 void CustomTrackView::dragMoveEvent(QDragMoveEvent * event) {
908 event->setDropAction(Qt::IgnoreAction);
910 int track = (int)(mapToScene(event->pos()).y() / m_tracksHeight); //) * (m_scale * 50) + m_scale;
911 m_dropItem->moveTo((int)(mapToScene(event->pos()).x() / m_scale), m_scale, (int)((track - m_dropItem->track()) * m_tracksHeight), track);
912 event->setDropAction(Qt::MoveAction);
913 if (event->mimeData()->hasFormat("kdenlive/producerslist")) {
914 event->acceptProposedAction();
917 QGraphicsView::dragMoveEvent(event);
921 void CustomTrackView::dragLeaveEvent(QDragLeaveEvent * event) {
925 } else QGraphicsView::dragLeaveEvent(event);
928 void CustomTrackView::dropEvent(QDropEvent * event) {
930 AddTimelineClipCommand *command = new AddTimelineClipCommand(this, m_dropItem->xml(), m_dropItem->clipProducer(), m_dropItem->info(), false, false);
931 m_commandStack->push(command);
932 m_dropItem->baseClip()->addReference();
933 m_document->updateClip(m_dropItem->baseClip()->getId());
934 // kDebug()<<"IIIIIIIIIIIIIIIIIIIIIIII TRAX CNT: "<<m_tracksList.count()<<", DROP: "<<m_dropItem->track();
935 m_document->renderer()->mltInsertClip(m_tracksList.count() - m_dropItem->track(), m_dropItem->startPos(), m_dropItem->xml());
936 m_document->setModified(true);
937 } else QGraphicsView::dropEvent(event);
942 QStringList CustomTrackView::mimeTypes() const {
943 QStringList qstrList;
944 // list of accepted mime types for drop
945 qstrList.append("text/plain");
946 qstrList.append("kdenlive/producerslist");
950 Qt::DropActions CustomTrackView::supportedDropActions() const {
951 // returns what actions are supported when dropping
952 return Qt::MoveAction;
955 void CustomTrackView::setDuration(int duration) {
956 kDebug() << "///////////// PRO DUR: " << duration << ", SCALE. " << (m_projectDuration + 500) * m_scale << ", height: " << 50 * m_tracksList.count();
957 m_projectDuration = duration;
958 setSceneRect(0, 0, (m_projectDuration + 100) * m_scale, sceneRect().height());
961 int CustomTrackView::duration() const {
962 return m_projectDuration;
965 void CustomTrackView::addTrack(TrackInfo type) {
966 m_tracksList << type;
967 m_cursorLine->setLine(m_cursorLine->line().x1(), 0, m_cursorLine->line().x1(), m_tracksHeight * m_tracksList.count());
968 setSceneRect(0, 0, sceneRect().width(), m_tracksHeight * m_tracksList.count());
969 verticalScrollBar()->setMaximum(m_tracksHeight * m_tracksList.count());
970 //setFixedHeight(50 * m_tracksCount);
973 void CustomTrackView::removeTrack() {
974 // TODO: implement track deletion
976 m_cursorLine->setLine(m_cursorLine->line().x1(), 0, m_cursorLine->line().x1(), m_tracksHeight * m_tracksList.count());
980 void CustomTrackView::slotSwitchTrackAudio(int ix) {
981 int tracknumber = m_tracksList.count() - ix;
982 kDebug() << "///// MUTING TRK: " << ix << "; PL NUM: " << tracknumber;
983 m_tracksList[tracknumber - 1].isMute = !m_tracksList.at(tracknumber - 1).isMute;
984 m_document->renderer()->mltChangeTrackState(tracknumber, m_tracksList.at(tracknumber - 1).isMute, m_tracksList.at(tracknumber - 1).isBlind);
987 void CustomTrackView::slotSwitchTrackVideo(int ix) {
988 int tracknumber = m_tracksList.count() - ix;
989 m_tracksList[tracknumber - 1].isBlind = !m_tracksList.at(tracknumber - 1).isBlind;
990 m_document->renderer()->mltChangeTrackState(tracknumber, m_tracksList.at(tracknumber - 1).isMute, m_tracksList.at(tracknumber - 1).isBlind);
993 void CustomTrackView::deleteClip(int clipId) {
994 QList<QGraphicsItem *> itemList = items();
995 for (int i = 0; i < itemList.count(); i++) {
996 if (itemList.at(i)->type() == AVWIDGET) {
997 ClipItem *item = (ClipItem *)itemList.at(i);
998 if (item->clipProducer() == clipId) {
999 AddTimelineClipCommand *command = new AddTimelineClipCommand(this, item->xml(), item->clipProducer(), item->info(), true, true);
1000 m_commandStack->push(command);
1007 void CustomTrackView::setCursorPos(int pos, bool seek) {
1008 emit cursorMoved((int)(m_cursorPos * m_scale), (int)(pos * m_scale));
1010 m_cursorLine->setPos(pos * m_scale, 0);
1011 if (seek) m_document->renderer()->seek(GenTime(pos, m_document->fps()));
1012 else if (m_autoScroll && m_scale < 50) checkScrolling();
1015 void CustomTrackView::updateCursorPos() {
1016 m_cursorLine->setPos(m_cursorPos * m_scale, 0);
1019 int CustomTrackView::cursorPos() {
1020 return (int)(m_cursorPos * m_scale);
1023 void CustomTrackView::moveCursorPos(int delta) {
1024 if (m_cursorPos + delta < 0) delta = 0 - m_cursorPos;
1025 emit cursorMoved((int)(m_cursorPos * m_scale), (int)((m_cursorPos + delta) * m_scale));
1026 m_cursorPos += delta;
1027 m_cursorLine->setPos(m_cursorPos * m_scale, 0);
1028 m_document->renderer()->seek(GenTime(m_cursorPos, m_document->fps()));
1029 //if (m_autoScroll && m_scale < 50) checkScrolling();
1032 void CustomTrackView::checkScrolling() {
1033 int vert = verticalScrollBar()->value();
1034 int hor = cursorPos();
1035 ensureVisible(hor, vert + 10, 2, 2, 50, 0);
1036 //centerOn(QPointF(cursorPos(), m_tracksHeight));
1037 /*QRect rectInView = viewport()->rect();
1038 int delta = rectInView.width() / 3;
1039 int max = rectInView.right() + horizontalScrollBar()->value() - delta;
1040 //kDebug() << "CURSOR POS: "<<m_cursorPos<< "Scale: "<<m_scale;
1041 if (m_cursorPos * m_scale >= max) horizontalScrollBar()->setValue((int)(horizontalScrollBar()->value() + 1 + m_scale));*/
1044 void CustomTrackView::mouseReleaseEvent(QMouseEvent * event) {
1045 if (event->button() == Qt::MidButton) {
1048 QGraphicsView::mouseReleaseEvent(event);
1049 setDragMode(QGraphicsView::NoDrag);
1050 if (m_operationMode == MOVEGUIDE) {
1051 setCursor(Qt::ArrowCursor);
1052 m_operationMode = NONE;
1053 m_dragGuide->setFlag(QGraphicsItem::ItemIsMovable, false);
1054 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);
1055 m_commandStack->push(command);
1056 m_dragGuide->updateGuide(GenTime(m_dragGuide->pos().x() / m_scale, m_document->fps()));
1061 if (m_dragItem == NULL) {
1062 emit transitionItemSelected(NULL);
1066 info.startPos = m_dragItem->startPos();
1067 info.endPos = m_dragItem->endPos();
1068 info.track = m_dragItem->track();
1070 if (m_operationMode == MOVE) {
1071 setCursor(Qt::OpenHandCursor);
1073 if (m_selectedClipList.count() == 1) {
1074 // we are moving one clip, easy
1075 if (m_dragItem->type() == AVWIDGET && (m_dragItemInfo.startPos != info.startPos || m_dragItemInfo.track != info.track)) {
1076 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())));
1078 MoveClipCommand *command = new MoveClipCommand(this, m_dragItemInfo, info, false);
1079 m_commandStack->push(command);
1081 // undo last move and emit error message
1082 MoveClipCommand *command = new MoveClipCommand(this, info, m_dragItemInfo, true);
1083 m_commandStack->push(command);
1084 emit displayMessage(i18n("Cannot move clip to position %1seconds", QString::number(m_dragItemInfo.startPos.seconds(), 'g', 2)), ErrorMessage);
1087 if (m_dragItem->type() == TRANSITIONWIDGET && (m_dragItemInfo.startPos != info.startPos || m_dragItemInfo.track != info.track)) {
1088 MoveTransitionCommand *command = new MoveTransitionCommand(this, m_dragItemInfo, info, false);
1089 m_commandStack->push(command);
1090 Transition *transition = (Transition *) m_dragItem;
1091 transition->updateTransitionEndTrack(getPreviousVideoTrack(m_dragItem->track()));
1092 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);
1095 // Moving several clips. We need to delete them and readd them to new position,
1096 // or they might overlap each other during the move
1098 GenTime timeOffset = info.startPos - m_dragItemInfo.startPos;
1099 int trackOffset = info.track - m_dragItemInfo.track;
1100 if (timeOffset != GenTime() || trackOffset != 0) {
1101 QUndoCommand *moveClips = new QUndoCommand();
1102 moveClips->setText("Move clips");
1103 // remove items in MLT playlist
1104 for (int i = 0; i < m_selectedClipList.count(); i++) {
1105 AbstractClipItem *item = m_selectedClipList.at(i);
1106 ItemInfo info = item->info();
1107 info.startPos = info.startPos - timeOffset;
1108 info.endPos = info.endPos - timeOffset;
1109 info.track = info.track - trackOffset;
1110 if (item->type() == AVWIDGET) {
1111 ClipItem *clip = static_cast <ClipItem*>(item);
1112 new AddTimelineClipCommand(this, clip->xml(), clip->clipProducer(), info, false, true, moveClips);
1113 m_document->renderer()->mltRemoveClip(m_tracksList.count() - info.track, info.startPos);
1115 Transition *tr = static_cast <Transition*>(item);
1116 new AddTransitionCommand(this, info, tr->transitionEndTrack(), tr->toXML(), false, true, moveClips);
1117 m_document->renderer()->mltDeleteTransition(tr->transitionTag(), tr->transitionEndTrack(), m_tracksList.count() - info.track, info.startPos, info.endPos, tr->toXML());
1121 for (int i = 0; i < m_selectedClipList.count(); i++) {
1122 // re-add items in correct place
1123 AbstractClipItem *item = m_selectedClipList.at(i);
1124 if (item->type() == AVWIDGET) {
1125 ClipItem *clip = static_cast <ClipItem*>(item);
1126 new AddTimelineClipCommand(this, clip->xml(), clip->clipProducer(), item->info(), false, false, moveClips);
1127 m_document->renderer()->mltInsertClip(m_tracksList.count() - item->track(), item->startPos(), clip->xml());
1129 Transition *tr = static_cast <Transition*>(item);
1130 ItemInfo transitionInfo = tr->info();
1131 new AddTransitionCommand(this, info, tr->transitionEndTrack(), tr->toXML(), false, false, moveClips);
1132 m_document->renderer()->mltAddTransition(tr->transitionTag(), tr->transitionEndTrack() + trackOffset, m_tracksList.count() - transitionInfo.track, transitionInfo.startPos, transitionInfo.endPos, tr->toXML());
1135 m_commandStack->push(moveClips);
1139 } else if (m_operationMode == RESIZESTART && m_dragItem->startPos() != m_dragItemInfo.startPos) {
1141 if (m_dragItem->type() == AVWIDGET) {
1142 m_document->renderer()->mltResizeClipStart(m_tracksList.count() - m_dragItem->track(), m_dragItem->endPos(), m_dragItem->startPos(), m_dragItemInfo.startPos, m_dragItem->cropStart(), m_dragItem->cropStart() + m_dragItem->endPos() - m_dragItem->startPos());
1143 updateClipFade((ClipItem *) m_dragItem);
1144 ResizeClipCommand *command = new ResizeClipCommand(this, m_dragItemInfo, info, false);
1145 m_commandStack->push(command);
1146 } else if (m_dragItem->type() == TRANSITIONWIDGET) {
1147 MoveTransitionCommand *command = new MoveTransitionCommand(this, m_dragItemInfo, info, false);
1148 m_commandStack->push(command);
1149 Transition *transition = (Transition *) m_dragItem;
1150 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);
1153 //m_document->renderer()->doRefresh();
1154 } else if (m_operationMode == RESIZEEND && m_dragItem->endPos() != m_dragItemInfo.endPos) {
1156 if (m_dragItem->type() == AVWIDGET) {
1157 ResizeClipCommand *command = new ResizeClipCommand(this, m_dragItemInfo, info, false);
1158 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());
1159 m_commandStack->push(command);
1160 } else if (m_dragItem->type() == TRANSITIONWIDGET) {
1161 MoveTransitionCommand *command = new MoveTransitionCommand(this, m_dragItemInfo, info, false);
1162 m_commandStack->push(command);
1163 Transition *transition = (Transition *) m_dragItem;
1164 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);
1166 //m_document->renderer()->doRefresh();
1167 } else if (m_operationMode == FADEIN) {
1168 // resize fade in effect
1169 ClipItem * item = (ClipItem *) m_dragItem;
1170 QStringList clipeffects = item->effectNames();
1171 if (clipeffects.contains(i18n("Fade in"))) {
1172 int ix = clipeffects.indexOf(i18n("Fade in"));
1173 QDomElement oldeffect = item->effectAt(ix);
1174 int start = item->cropStart().frames(m_document->fps());
1175 int end = item->fadeIn();
1177 slotDeleteEffect(item, oldeffect);
1180 QDomElement effect = MainWindow::audioEffects.getEffectByName("Fade in");
1181 EffectsList::setParameter(effect, "in", QString::number(start));
1182 EffectsList::setParameter(effect, "out", QString::number(end));
1183 slotUpdateClipEffect(item, oldeffect, effect, ix);
1185 } else if (item->fadeIn() != 0) {
1186 QDomElement effect = MainWindow::audioEffects.getEffectByName("Fade in");
1187 int start = item->cropStart().frames(m_document->fps());
1188 int end = item->fadeIn() + start;
1189 EffectsList::setParameter(effect, "in", QString::number(start));
1190 EffectsList::setParameter(effect, "out", QString::number(end));
1191 slotAddEffect(effect, m_dragItem->startPos(), m_dragItem->track());
1193 } else if (m_operationMode == FADEOUT) {
1194 // resize fade in effect
1195 ClipItem * item = (ClipItem *) m_dragItem;
1196 QStringList clipeffects = item->effectNames();
1197 if (clipeffects.contains(i18n("Fade out"))) {
1198 int ix = clipeffects.indexOf(i18n("Fade out"));
1199 QDomElement oldeffect = item->effectAt(ix);
1200 int end = (item->duration() + item->cropStart()).frames(m_document->fps());
1201 int start = item->fadeOut();
1203 slotDeleteEffect(item, oldeffect);
1205 start = end - start;
1206 QDomElement effect = MainWindow::audioEffects.getEffectByName("Fade out");
1207 EffectsList::setParameter(effect, "in", QString::number(start));
1208 EffectsList::setParameter(effect, "out", QString::number(end));
1209 slotUpdateClipEffect(item, oldeffect, effect, ix);
1211 } else if (item->fadeOut() != 0) {
1212 QDomElement effect = MainWindow::audioEffects.getEffectByName("Fade out");
1213 int end = (item->duration() + item->cropStart()).frames(m_document->fps());
1214 int start = end - item->fadeOut();
1215 EffectsList::setParameter(effect, "in", QString::number(start));
1216 EffectsList::setParameter(effect, "out", QString::number(end));
1217 slotAddEffect(effect, m_dragItem->startPos(), m_dragItem->track());
1219 } else if (m_operationMode == KEYFRAME) {
1220 // update the MLT effect
1221 ClipItem * item = (ClipItem *) m_dragItem;
1222 QString previous = item->keyframes(item->selectedEffectIndex());
1223 item->updateKeyframeEffect();
1224 QString next = item->keyframes(item->selectedEffectIndex());
1225 EditKeyFrameCommand *command = new EditKeyFrameCommand(this, item->track(), item->startPos(), item->selectedEffectIndex(), previous, next, false);
1226 m_commandStack->push(command);
1227 updateEffect(m_tracksList.count() - item->track(), item->startPos(), item->selectedEffect(), item->selectedEffectIndex());
1230 emit transitionItemSelected((m_dragItem && m_dragItem->type() == TRANSITIONWIDGET) ? (Transition*) m_dragItem : NULL);
1231 m_document->setModified(true);
1232 m_operationMode = NONE;
1235 void CustomTrackView::deleteClip(ItemInfo info) {
1236 ClipItem *item = getClipItemAt((int) info.startPos.frames(m_document->fps()) + 1, info.track);
1238 kDebug() << "---------------- ERROR, CANNOT find clip to delete at...";// << rect.x();
1241 if (item->isSelected()) emit clipItemSelected(NULL);
1242 item->baseClip()->removeReference();
1243 m_document->updateClip(item->baseClip()->getId());
1245 m_document->renderer()->mltRemoveClip(m_tracksList.count() - info.track, info.startPos);
1246 m_document->renderer()->doRefresh();
1249 void CustomTrackView::deleteSelectedClips() {
1250 QList<QGraphicsItem *> itemList = scene()->selectedItems();
1251 if (itemList.count() == 0) {
1252 emit displayMessage(i18n("Select clip to delete"), ErrorMessage);
1255 QUndoCommand *deleteSelected = new QUndoCommand();
1256 deleteSelected->setText("Delete selected items");
1257 for (int i = 0; i < itemList.count(); i++) {
1258 if (itemList.at(i)->type() == AVWIDGET) {
1259 ClipItem *item = (ClipItem *) itemList.at(i);
1260 new AddTimelineClipCommand(this, item->xml(), item->clipProducer(), item->info(), true, true, deleteSelected);
1261 } else if (itemList.at(i)->type() == TRANSITIONWIDGET) {
1262 Transition *item = (Transition *) itemList.at(i);
1264 info.startPos = item->startPos();
1265 info.endPos = item->endPos();
1266 info.track = item->track();
1267 new AddTransitionCommand(this, info, item->transitionEndTrack(), QDomElement(), true, true, deleteSelected);
1270 m_commandStack->push(deleteSelected);
1273 void CustomTrackView::cutSelectedClips() {
1274 QList<QGraphicsItem *> itemList = scene()->selectedItems();
1275 GenTime currentPos = GenTime(m_cursorPos, m_document->fps());
1276 for (int i = 0; i < itemList.count(); i++) {
1277 if (itemList.at(i)->type() == AVWIDGET) {
1278 ClipItem *item = (ClipItem *) itemList.at(i);
1280 info.startPos = item->startPos();
1281 info.endPos = item->endPos();
1282 if (currentPos > info.startPos && currentPos < info.endPos) {
1283 info.track = item->track();
1284 RazorClipCommand *command = new RazorClipCommand(this, info, currentPos, true);
1285 m_commandStack->push(command);
1291 void CustomTrackView::addClip(QDomElement xml, int clipId, ItemInfo info) {
1292 DocClipBase *baseclip = m_document->clipManager()->getClipById(clipId);
1293 int crop = xml.attribute("in").toInt();
1294 ClipItem *item = new ClipItem(baseclip, info, GenTime(crop, m_document->fps()), m_scale, m_document->fps());
1295 scene()->addItem(item);
1296 baseclip->addReference();
1297 m_document->updateClip(baseclip->getId());
1298 m_document->renderer()->mltInsertClip(m_tracksList.count() - info.track, info.startPos, xml);
1299 m_document->renderer()->doRefresh();
1302 void CustomTrackView::slotUpdateClip(int clipId) {
1303 QList<QGraphicsItem *> list = scene()->items();
1304 ClipItem *clip = NULL;
1305 for (int i = 0; i < list.size(); ++i) {
1306 if (list.at(i)->type() == AVWIDGET) {
1307 clip = static_cast <ClipItem *>(list.at(i));
1308 if (clip->clipProducer() == clipId) {
1309 clip->refreshClip();
1310 m_document->renderer()->mltUpdateClip(m_tracksList.count() - clip->track(), clip->startPos(), clip->xml());
1316 ClipItem *CustomTrackView::getClipItemAt(int pos, int track) {
1317 QList<QGraphicsItem *> list = scene()->items(QPointF(pos * m_scale, track * m_tracksHeight + m_tracksHeight / 2));
1318 ClipItem *clip = NULL;
1319 for (int i = 0; i < list.size(); ++i) {
1320 if (list.at(i)->type() == AVWIDGET) {
1321 clip = static_cast <ClipItem *>(list.at(i));
1328 ClipItem *CustomTrackView::getClipItemAt(GenTime pos, int track) {
1329 int framepos = (int)(pos.frames(m_document->fps()) * m_scale);
1330 return getClipItemAt(framepos, track);
1333 Transition *CustomTrackView::getTransitionItemAt(int pos, int track) {
1334 QList<QGraphicsItem *> list = scene()->items(QPointF(pos * m_scale, (track + 1) * m_tracksHeight));
1335 Transition *clip = NULL;
1336 for (int i = 0; i < list.size(); ++i) {
1337 if (list.at(i)->type() == TRANSITIONWIDGET) {
1338 clip = static_cast <Transition *>(list.at(i));
1345 Transition *CustomTrackView::getTransitionItemAt(GenTime pos, int track) {
1346 int framepos = (int)(pos.frames(m_document->fps()) * m_scale);
1347 return getTransitionItemAt(framepos, track);
1350 void CustomTrackView::moveClip(const ItemInfo start, const ItemInfo end) {
1351 ClipItem *item = getClipItemAt((int) start.startPos.frames(m_document->fps()) + 1, start.track);
1353 emit displayMessage(i18n("Cannot move clip at time: %1s on track %2", QString::number(start.startPos.seconds(), 'g', 2), start.track), ErrorMessage);
1354 kDebug() << "---------------- ERROR, CANNOT find clip to move at.. ";// << startPos.x() * m_scale * FRAME_SIZE + 1 << ", " << startPos.y() * m_tracksHeight + m_tracksHeight / 2;
1357 //kDebug() << "---------------- Move CLIP FROM: " << startPos.x() << ", END:" << endPos.x() << ",TRACKS: " << startPos.y() << " TO " << endPos.y();
1359 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()));
1361 item->moveTo((int) end.startPos.frames(m_document->fps()), m_scale, (int)((end.track - start.track) * m_tracksHeight), end.track);
1363 // undo last move and emit error message
1364 emit displayMessage(i18n("Cannot move clip to position %1seconds", QString::number(end.startPos.seconds(), 'g', 2)), ErrorMessage);
1368 void CustomTrackView::moveTransition(const ItemInfo start, const ItemInfo end) {
1369 Transition *item = getTransitionItemAt((int)start.startPos.frames(m_document->fps()) + 1, start.track);
1371 emit displayMessage(i18n("Cannot move transition at time: %1s on track %2", start.startPos.seconds(), start.track), ErrorMessage);
1372 kDebug() << "---------------- ERROR, CANNOT find transition to move... ";// << startPos.x() * m_scale * FRAME_SIZE + 1 << ", " << startPos.y() * m_tracksHeight + m_tracksHeight / 2;
1375 //kDebug() << "---------------- Move TRANSITION FROM: " << startPos.x() << ", END:" << endPos.x() << ",TRACKS: " << oldtrack << " TO " << newtrack;
1377 //kDebug()<<"/// RESIZE TRANS START: ("<< startPos.x()<<"x"<< startPos.y()<<") / ("<<endPos.x()<<"x"<< endPos.y()<<")";
1378 if (end.endPos - end.startPos == start.endPos - start.startPos) {
1379 // Transition was moved
1380 item->moveTo((int) end.startPos.frames(m_document->fps()), m_scale, (end.track - start.track) * m_tracksHeight, end.track);
1381 } else if (end.endPos == start.endPos) {
1382 // Transition start resize
1383 item->resizeStart((int) end.startPos.frames(m_document->fps()), m_scale);
1385 // Transition end resize;
1386 item->resizeEnd((int) end.endPos.frames(m_document->fps()), m_scale);
1388 //item->moveTransition(GenTime((int) (endPos.x() - startPos.x()), m_document->fps()));
1389 item->updateTransitionEndTrack(getPreviousVideoTrack(end.track));
1390 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);
1393 void CustomTrackView::resizeClip(const ItemInfo start, const ItemInfo end) {
1395 bool resizeClipStart = true;
1396 if (start.startPos == end.startPos) resizeClipStart = false;
1397 /*if (resizeClipStart) offset = 1;
1399 ClipItem *item = getClipItemAt((int)(start.startPos.frames(m_document->fps()) + offset), start.track);
1401 emit displayMessage(i18n("Cannot move clip at time: %1s on track %2", start.startPos.seconds(), start.track), ErrorMessage);
1402 kDebug() << "---------------- ERROR, CANNOT find clip to resize at... "; // << startPos;
1405 if (resizeClipStart) {
1406 m_document->renderer()->mltResizeClipStart(m_tracksList.count() - item->track(), item->endPos(), end.startPos, item->startPos(), item->cropStart() + end.startPos - start.startPos, item->cropStart() + end.startPos - start.startPos + item->endPos() - end.startPos);
1407 item->resizeStart((int) end.startPos.frames(m_document->fps()), m_scale);
1408 updateClipFade(item);
1410 m_document->renderer()->mltResizeClipEnd(m_tracksList.count() - item->track(), item->startPos(), item->cropStart(), item->cropStart() + end.endPos - item->startPos());
1411 item->resizeEnd((int) end.endPos.frames(m_document->fps()), m_scale);
1412 updateClipFade(item, true);
1414 m_document->renderer()->doRefresh();
1417 void CustomTrackView::updateClipFade(ClipItem * item, bool updateFadeOut) {
1418 if (!updateFadeOut) {
1419 int end = item->fadeIn();
1421 // there is a fade in effect
1422 QStringList clipeffects = item->effectNames();
1423 QDomElement oldeffect = item->effectAt(clipeffects.indexOf("Fade in"));
1424 int start = item->cropStart().frames(m_document->fps());
1426 EffectsList::setParameter(oldeffect, "in", QString::number(start));
1427 EffectsList::setParameter(oldeffect, "out", QString::number(end));
1428 QMap <QString, QString> effectParams = item->getEffectArgs(oldeffect);
1429 if (!m_document->renderer()->mltEditEffect(m_tracksList.count() - item->track(), item->startPos(), effectParams))
1430 emit displayMessage(i18n("Problem editing effect"), ErrorMessage);
1433 int start = item->fadeOut();
1435 // there is a fade in effect
1436 QStringList clipeffects = item->effectNames();
1437 QDomElement oldeffect = item->effectAt(clipeffects.indexOf("Fade out"));
1438 int end = (item->duration() - item->cropStart()).frames(m_document->fps());
1439 start = end - start;
1440 EffectsList::setParameter(oldeffect, "in", QString::number(start));
1441 EffectsList::setParameter(oldeffect, "out", QString::number(end));
1442 QMap <QString, QString> effectParams = item->getEffectArgs(oldeffect);
1443 if (m_document->renderer()->mltEditEffect(m_tracksList.count() - item->track(), item->startPos(), effectParams))
1444 emit displayMessage(i18n("Problem editing effect"), ErrorMessage);
1449 double CustomTrackView::getSnapPointForPos(double pos) {
1450 for (int i = 0; i < m_snapPoints.size(); ++i) {
1451 if (abs((int)(pos - m_snapPoints.at(i).frames(m_document->fps()) * m_scale)) < 10) {
1452 //kDebug()<<" FOUND SNAP POINT AT: "<<m_snapPoints.at(i)<<", current pos: "<<pos / m_scale;
1453 return m_snapPoints.at(i).frames(m_document->fps()) * m_scale + 0.5;
1455 if (m_snapPoints.at(i).frames(m_document->fps() * m_scale) > pos) break;
1460 void CustomTrackView::updateSnapPoints(AbstractClipItem *selected) {
1461 m_snapPoints.clear();
1462 if (!KdenliveSettings::snaptopoints()) return;
1464 if (selected) offset = selected->duration();
1465 QList<QGraphicsItem *> itemList = items();
1466 for (int i = 0; i < itemList.count(); i++) {
1467 if (itemList.at(i)->type() == AVWIDGET && itemList.at(i) != selected) {
1468 ClipItem *item = static_cast <ClipItem *>(itemList.at(i));
1469 GenTime start = item->startPos();
1470 GenTime end = item->endPos();
1471 m_snapPoints.append(start);
1472 m_snapPoints.append(end);
1473 QList < GenTime > markers = item->snapMarkers();
1474 for (int i = 0; i < markers.size(); ++i) {
1475 GenTime t = markers.at(i);
1476 m_snapPoints.append(t);
1477 if (t > offset) m_snapPoints.append(t - offset);
1479 if (offset != GenTime()) {
1480 if (start > offset) m_snapPoints.append(start - offset);
1481 if (end > offset) m_snapPoints.append(end - offset);
1483 } else if (itemList.at(i)->type() == TRANSITIONWIDGET) {
1484 Transition *transition = static_cast <Transition*>(itemList.at(i));
1485 GenTime start = transition->startPos();
1486 GenTime end = transition->endPos();
1487 m_snapPoints.append(start);
1488 m_snapPoints.append(end);
1489 if (offset != GenTime()) {
1490 if (start > offset) m_snapPoints.append(start - offset);
1491 if (end > offset) m_snapPoints.append(end - offset);
1496 // add cursor position
1497 GenTime pos = GenTime(m_cursorPos, m_document->fps());
1498 m_snapPoints.append(pos);
1499 if (offset != GenTime()) m_snapPoints.append(pos - offset);
1502 for (int i = 0; i < m_guides.count(); i++) {
1503 m_snapPoints.append(m_guides.at(i)->position());
1504 if (offset != GenTime()) m_snapPoints.append(m_guides.at(i)->position() - offset);
1507 qSort(m_snapPoints);
1508 //for (int i = 0; i < m_snapPoints.size(); ++i)
1509 // kDebug() << "SNAP POINT: " << m_snapPoints.at(i).frames(25);
1512 void CustomTrackView::slotSeekToPreviousSnap() {
1513 updateSnapPoints(NULL);
1514 GenTime pos = GenTime(m_cursorPos, m_document->fps());
1515 GenTime res = GenTime();
1516 for (int i = 0; i < m_snapPoints.size(); ++i) {
1517 if (m_snapPoints.at(i) >= pos) {
1519 res = m_snapPoints.at(i - 1);
1523 setCursorPos((int) res.frames(m_document->fps()));
1527 void CustomTrackView::slotSeekToNextSnap() {
1528 updateSnapPoints(NULL);
1529 GenTime pos = GenTime(m_cursorPos, m_document->fps());
1530 GenTime res = GenTime(m_projectDuration, m_document->fps());
1531 for (int i = 0; i < m_snapPoints.size(); ++i) {
1532 if (m_snapPoints.at(i) > pos) {
1533 res = m_snapPoints.at(i);
1537 setCursorPos((int) res.frames(m_document->fps()));
1541 void CustomTrackView::clipStart() {
1542 QList<QGraphicsItem *> itemList = scene()->selectedItems();
1543 for (int i = 0; i < itemList.count(); i++) {
1544 if (itemList.at(i)->type() == AVWIDGET) {
1545 ClipItem *item = (ClipItem *) itemList.at(i);
1546 setCursorPos((int) item->startPos().frames(m_document->fps()));
1553 void CustomTrackView::clipEnd() {
1554 QList<QGraphicsItem *> itemList = scene()->selectedItems();
1555 for (int i = 0; i < itemList.count(); i++) {
1556 if (itemList.at(i)->type() == AVWIDGET) {
1557 ClipItem *item = (ClipItem *) itemList.at(i);
1558 setCursorPos((int) item->endPos().frames(m_document->fps()));
1565 void CustomTrackView::slotAddClipMarker() {
1566 QList<QGraphicsItem *> itemList = scene()->selectedItems();
1567 if (itemList.count() != 1) {
1568 emit displayMessage(i18n("Cannot add marker if more than one clip is selected"), ErrorMessage);
1569 kDebug() << "// CANNOT ADD MARKER IF MORE TAN ONE CLIP IS SELECTED....";
1572 AbstractClipItem *item = (AbstractClipItem *)itemList.at(0);
1573 if (item->type() != AVWIDGET) return;
1574 GenTime pos = GenTime(m_cursorPos, m_document->fps());
1575 if (item->startPos() > pos || item->endPos() < pos) return;
1576 ClipItem *clip = (ClipItem *) item;
1577 int id = clip->baseClip()->getId();
1578 GenTime position = pos - item->startPos() + item->cropStart();
1579 CommentedTime marker(position, i18n("Marker"));
1580 MarkerDialog d(clip->baseClip(), marker, m_document->timecode(), this);
1581 if (d.exec() == QDialog::Accepted) {
1582 slotAddClipMarker(id, d.newMarker().time(), d.newMarker().comment());
1586 void CustomTrackView::slotAddClipMarker(int id, GenTime t, QString c) {
1587 QString oldcomment = m_document->clipManager()->getClipById(id)->markerComment(t);
1588 AddMarkerCommand *command = new AddMarkerCommand(this, oldcomment, c, id, t, true);
1589 m_commandStack->push(command);
1592 void CustomTrackView::slotDeleteClipMarker() {
1593 QList<QGraphicsItem *> itemList = scene()->selectedItems();
1594 if (itemList.count() != 1) {
1595 emit displayMessage(i18n("Cannot delete marker if more than one clip is selected"), ErrorMessage);
1596 kDebug() << "// CANNOT DELETE MARKER IF MORE TAN ONE CLIP IS SELECTED....";
1599 AbstractClipItem *item = (AbstractClipItem *)itemList.at(0);
1600 if (item->type() != AVWIDGET) {
1601 emit displayMessage(i18n("No clip selected"), ErrorMessage);
1604 GenTime pos = GenTime(m_cursorPos, m_document->fps());
1605 if (item->startPos() > pos || item->endPos() < pos) {
1606 emit displayMessage(i18n("No selected clip at cursor time"), ErrorMessage);
1609 ClipItem *clip = (ClipItem *) item;
1610 int id = clip->baseClip()->getId();
1611 GenTime position = pos - item->startPos() + item->cropStart();
1612 QString comment = clip->baseClip()->markerComment(position);
1613 if (comment.isEmpty()) {
1614 emit displayMessage(i18n("No marker found at cursor time"), ErrorMessage);
1617 AddMarkerCommand *command = new AddMarkerCommand(this, comment, QString(), id, position, true);
1618 m_commandStack->push(command);
1621 void CustomTrackView::slotDeleteAllClipMarkers() {
1622 QList<QGraphicsItem *> itemList = scene()->selectedItems();
1623 if (itemList.count() != 1) {
1624 emit displayMessage(i18n("Cannot delete marker if more than one clip is selected"), ErrorMessage);
1625 kDebug() << "// CANNOT DELETE MARKER IF MORE TAN ONE CLIP IS SELECTED....";
1628 AbstractClipItem *item = (AbstractClipItem *)itemList.at(0);
1629 if (item->type() != AVWIDGET) {
1630 emit displayMessage(i18n("No clip selected"), ErrorMessage);
1634 ClipItem *clip = static_cast <ClipItem *>(item);
1635 QList <CommentedTime> markers = clip->baseClip()->commentedSnapMarkers();
1637 if (markers.isEmpty()) {
1638 emit displayMessage(i18n("Clip has no markers"), ErrorMessage);
1641 int id = clip->baseClip()->getId();
1642 QUndoCommand *deleteMarkers = new QUndoCommand();
1643 deleteMarkers->setText("Delete clip markers");
1645 for (int i = 0; i < markers.size(); i++) {
1646 new AddMarkerCommand(this, markers.at(i).comment(), QString(), id, markers.at(i).time(), true, deleteMarkers);
1648 m_commandStack->push(deleteMarkers);
1651 void CustomTrackView::slotEditClipMarker() {
1652 QList<QGraphicsItem *> itemList = scene()->selectedItems();
1653 if (itemList.count() != 1) {
1654 emit displayMessage(i18n("Cannot edit marker if more than one clip is selected"), ErrorMessage);
1655 kDebug() << "// CANNOT DELETE MARKER IF MORE TAN ONE CLIP IS SELECTED....";
1658 AbstractClipItem *item = (AbstractClipItem *)itemList.at(0);
1659 if (item->type() != AVWIDGET) {
1660 emit displayMessage(i18n("No clip at cursor time"), ErrorMessage);
1663 GenTime pos = GenTime(m_cursorPos, m_document->fps());
1664 if (item->startPos() > pos || item->endPos() < pos) {
1665 emit displayMessage(i18n("No selected clip at cursor time"), ErrorMessage);
1668 ClipItem *clip = (ClipItem *) item;
1669 int id = clip->baseClip()->getId();
1670 GenTime position = pos - item->startPos() + item->cropStart();
1671 QString oldcomment = clip->baseClip()->markerComment(position);
1672 if (oldcomment.isEmpty()) {
1673 emit displayMessage(i18n("No marker found at cursor time"), ErrorMessage);
1677 CommentedTime marker(position, oldcomment);
1678 MarkerDialog d(clip->baseClip(), marker, m_document->timecode(), this);
1679 if (d.exec() == QDialog::Accepted) {
1680 if (d.newMarker().time() == position) {
1681 // marker position was not changed, only text
1682 AddMarkerCommand *command = new AddMarkerCommand(this, oldcomment, d.newMarker().comment(), id, position, true);
1683 m_commandStack->push(command);
1685 // marker text and position were changed, remove previous marker and add new one
1686 AddMarkerCommand *command1 = new AddMarkerCommand(this, oldcomment, QString(), id, position, true);
1687 AddMarkerCommand *command2 = new AddMarkerCommand(this, QString(), d.newMarker().comment(), id, d.newMarker().time(), true);
1688 m_commandStack->push(command1);
1689 m_commandStack->push(command2);
1694 void CustomTrackView::addMarker(const int id, const GenTime &pos, const QString comment) {
1695 DocClipBase *base = m_document->clipManager()->getClipById(id);
1696 if (!comment.isEmpty()) base->addSnapMarker(pos, comment);
1697 else base->deleteSnapMarker(pos);
1698 m_document->setModified(true);
1699 viewport()->update();
1704 void CustomTrackView::editGuide(const GenTime oldPos, const GenTime pos, const QString &comment) {
1705 if (oldPos > GenTime() && pos > GenTime()) {
1707 for (int i = 0; i < m_guides.count(); i++) {
1708 if (m_guides.at(i)->position() == oldPos) {
1709 Guide *item = m_guides.at(i);
1710 item->updateGuide(pos, comment);
1714 } else if (pos > GenTime()) addGuide(pos, comment);
1718 for (int i = 0; i < m_guides.count(); i++) {
1719 if (m_guides.at(i)->position() == oldPos) {
1720 Guide *item = m_guides.takeAt(i);
1726 if (!found) emit displayMessage(i18n("No guide at cursor time"), ErrorMessage);
1730 bool CustomTrackView::addGuide(const GenTime pos, const QString &comment) {
1731 for (int i = 0; i < m_guides.count(); i++) {
1732 if (m_guides.at(i)->position() == pos) {
1733 emit displayMessage(i18n("A guide already exists at that position"), ErrorMessage);
1737 Guide *g = new Guide(this, pos, comment, m_scale, m_document->fps(), m_tracksHeight * m_tracksList.count());
1738 scene()->addItem(g);
1743 void CustomTrackView::slotAddGuide() {
1744 CommentedTime marker(GenTime(m_cursorPos, m_document->fps()), i18n("Guide"));
1745 MarkerDialog d(NULL, marker, m_document->timecode(), this);
1746 if (d.exec() != QDialog::Accepted) return;
1747 if (addGuide(d.newMarker().time(), d.newMarker().comment())) {
1748 EditGuideCommand *command = new EditGuideCommand(this, GenTime(), QString(), d.newMarker().time(), d.newMarker().comment(), false);
1749 m_commandStack->push(command);
1753 void CustomTrackView::slotEditGuide() {
1754 GenTime pos = GenTime(m_cursorPos, m_document->fps());
1756 for (int i = 0; i < m_guides.count(); i++) {
1757 if (m_guides.at(i)->position() == pos) {
1758 slotEditGuide(m_guides.at(i)->info());
1763 if (!found) emit displayMessage(i18n("No guide at cursor time"), ErrorMessage);
1766 void CustomTrackView::slotEditGuide(CommentedTime guide) {
1767 MarkerDialog d(NULL, guide, m_document->timecode(), this);
1768 if (d.exec() == QDialog::Accepted) {
1769 EditGuideCommand *command = new EditGuideCommand(this, guide.time(), guide.comment(), d.newMarker().time(), d.newMarker().comment(), true);
1770 m_commandStack->push(command);
1775 void CustomTrackView::slotDeleteGuide() {
1776 GenTime pos = GenTime(m_cursorPos, m_document->fps());
1778 for (int i = 0; i < m_guides.count(); i++) {
1779 if (m_guides.at(i)->position() == pos) {
1780 EditGuideCommand *command = new EditGuideCommand(this, m_guides.at(i)->position(), m_guides.at(i)->label(), GenTime(), QString(), true);
1781 m_commandStack->push(command);
1786 if (!found) emit displayMessage(i18n("No guide at cursor time"), ErrorMessage);
1789 void CustomTrackView::slotDeleteAllGuides() {
1790 QUndoCommand *deleteAll = new QUndoCommand();
1791 deleteAll->setText("Delete all guides");
1792 for (int i = 0; i < m_guides.count(); i++) {
1793 EditGuideCommand *command = new EditGuideCommand(this, m_guides.at(i)->position(), m_guides.at(i)->label(), GenTime(), QString(), true, deleteAll);
1795 m_commandStack->push(deleteAll);
1798 void CustomTrackView::setTool(PROJECTTOOL tool) {
1802 void CustomTrackView::setScale(double scaleFactor) {
1803 //scale(scaleFactor, scaleFactor);
1804 m_animationTimer->stop();
1813 double pos = cursorPos() / m_scale;
1814 m_scale = scaleFactor;
1815 int vert = verticalScrollBar()->value();
1816 kDebug() << " HHHHHHHH SCALING: " << m_scale;
1817 QList<QGraphicsItem *> itemList = items();
1818 for (int i = 0; i < itemList.count(); i++) {
1819 if (itemList.at(i)->type() == AVWIDGET || itemList.at(i)->type() == TRANSITIONWIDGET) {
1820 AbstractClipItem *clip = (AbstractClipItem *)itemList.at(i);
1821 clip->setRect(clip->startPos().frames(m_document->fps()) * m_scale, clip->rect().y(), clip->duration().frames(m_document->fps()) * m_scale, clip->rect().height());
1825 for (int i = 0; i < m_guides.count(); i++) {
1826 m_guides.at(i)->updatePosition(m_scale);
1829 setSceneRect(0, 0, (m_projectDuration + 100) * m_scale, sceneRect().height());
1831 centerOn(QPointF(cursorPos(), m_tracksHeight));
1832 verticalScrollBar()->setValue(vert);
1835 void CustomTrackView::slotRefreshGuides() {
1836 if (KdenliveSettings::showmarkers()) {
1837 kDebug() << "// refresh GUIDES";
1838 for (int i = 0; i < m_guides.count(); i++) {
1839 m_guides.at(i)->update();
1844 void CustomTrackView::drawBackground(QPainter * painter, const QRectF & rect) {
1845 QRect rectInView = viewport()->rect();
1846 rectInView.moveTo(horizontalScrollBar()->value(), verticalScrollBar()->value());
1848 QColor base = palette().button().color();
1849 painter->setClipRect(rect);
1850 painter->drawLine(rectInView.left(), 0, rectInView.right(), 0);
1851 uint max = m_tracksList.count();
1852 for (uint i = 0; i < max;i++) {
1853 if (m_tracksList.at(max - i - 1).type == AUDIOTRACK) painter->fillRect(rectInView.left(), m_tracksHeight * i + 1, rectInView.right() - rectInView.left() + 1, m_tracksHeight - 1, QBrush(QColor(240, 240, 255)));
1854 painter->drawLine(rectInView.left(), m_tracksHeight * (i + 1), rectInView.right(), m_tracksHeight * (i + 1));
1855 //painter->drawText(QRectF(10, 50 * i, 100, 50 * i + 49), Qt::AlignLeft, i18n(" Track ") + QString::number(i + 1));
1857 int lowerLimit = m_tracksHeight * m_tracksList.count() + 1;
1858 if (height() > lowerLimit)
1859 painter->fillRect(QRectF(rectInView.left(), lowerLimit, rectInView.width(), height() - lowerLimit), QBrush(base));
1862 QDomElement CustomTrackView::xmlInfo() {
1865 QDomElement guides = doc.createElement("guides");
1866 for (int i = 0; i < m_guides.count(); i++) {
1867 e = doc.createElement("guide");
1868 e.setAttribute("time", m_guides.at(i)->position().ms() / 1000);
1869 e.setAttribute("comment", m_guides.at(i)->label());
1870 guides.appendChild(e);
1875 bool CustomTrackView::findString(const QString &text) {
1877 for (int i = 0; i < m_searchPoints.size(); ++i) {
1878 marker = m_searchPoints.at(i).comment();
1879 if (marker.contains(text, Qt::CaseInsensitive)) {
1880 setCursorPos(m_searchPoints.at(i).time().frames(m_document->fps()), true);
1881 int vert = verticalScrollBar()->value();
1882 int hor = cursorPos();
1883 ensureVisible(hor, vert + 10, 2, 2, 50, 0);
1891 bool CustomTrackView::findNextString(const QString &text) {
1893 for (int i = m_findIndex + 1; i < m_searchPoints.size(); ++i) {
1894 marker = m_searchPoints.at(i).comment();
1895 if (marker.contains(text, Qt::CaseInsensitive)) {
1896 setCursorPos(m_searchPoints.at(i).time().frames(m_document->fps()), true);
1897 int vert = verticalScrollBar()->value();
1898 int hor = cursorPos();
1899 ensureVisible(hor, vert + 10, 2, 2, 50, 0);
1908 void CustomTrackView::initSearchStrings() {
1909 m_searchPoints.clear();
1910 QList<QGraphicsItem *> itemList = items();
1911 for (int i = 0; i < itemList.count(); i++) {
1912 // parse all clip names
1913 if (itemList.at(i)->type() == AVWIDGET) {
1914 ClipItem *item = static_cast <ClipItem *>(itemList.at(i));
1915 GenTime start = item->startPos();
1916 CommentedTime t(start, item->clipName());
1917 m_searchPoints.append(t);
1918 // add all clip markers
1919 QList < CommentedTime > markers = item->commentedSnapMarkers();
1920 m_searchPoints += markers;
1925 for (int i = 0; i < m_guides.count(); i++) {
1926 m_searchPoints.append(m_guides.at(i)->info());
1929 qSort(m_searchPoints);
1932 void CustomTrackView::clearSearchStrings() {
1933 m_searchPoints.clear();
1937 void CustomTrackView::copyClip() {
1938 while (m_copiedItems.count() > 0) {
1939 delete m_copiedItems.takeFirst();
1941 QList<QGraphicsItem *> itemList = scene()->selectedItems();
1942 if (itemList.count() == 0) {
1943 emit displayMessage(i18n("Select a clip before copying"), ErrorMessage);
1946 for (int i = 0; i < itemList.count(); i++) {
1947 if (itemList.at(i)->type() == AVWIDGET) {
1948 ClipItem *dup = static_cast <ClipItem *>(itemList.at(i));
1949 m_copiedItems.append(dup->clone(m_scale));
1950 } else if (itemList.at(i)->type() == TRANSITIONWIDGET) {
1951 Transition *dup = static_cast <Transition *>(itemList.at(i));
1952 m_copiedItems.append(dup->clone(m_scale));
1957 bool CustomTrackView::canBePastedTo(ItemInfo info, int type) const {
1958 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));
1959 QList<QGraphicsItem *> collisions = scene()->items(rect, Qt::IntersectsItemBoundingRect);
1960 for (int i = 0; i < collisions.count(); i++) {
1961 if (collisions.at(i)->type() == type) return false;
1966 bool CustomTrackView::canBePasted(QList<AbstractClipItem *> items, GenTime offset, int trackOffset) const {
1967 for (int i = 0; i < items.count(); i++) {
1968 ItemInfo info = items.at(i)->info();
1969 info.startPos += offset;
1970 info.endPos += offset;
1971 info.track += trackOffset;
1972 if (!canBePastedTo(info, items.at(i)->type())) return false;
1977 bool CustomTrackView::canBeMoved(QList<AbstractClipItem *> items, GenTime offset, int trackOffset) const {
1978 QPainterPath movePath;
1979 movePath.moveTo(0, 0);
1981 for (int i = 0; i < items.count(); i++) {
1982 ItemInfo info = items.at(i)->info();
1983 info.startPos = info.startPos + offset;
1984 info.endPos = info.endPos + offset;
1985 info.track = info.track + trackOffset;
1986 if (info.startPos < GenTime()) {
1987 // No clip should go below 0
1990 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));
1991 movePath.addRect(rect);
1993 QList<QGraphicsItem *> collisions = scene()->items(movePath, Qt::IntersectsItemBoundingRect);
1994 for (int i = 0; i < collisions.count(); i++) {
1995 if ((collisions.at(i)->type() == AVWIDGET || collisions.at(i)->type() == TRANSITIONWIDGET) && !items.contains(static_cast <AbstractClipItem *>(collisions.at(i)))) {
1996 kDebug() << " //////////// CLIP COLLISION, MOVE NOT ALLOWED";
2003 void CustomTrackView::pasteClip() {
2004 if (m_copiedItems.count() == 0) {
2005 emit displayMessage(i18n("No clip copied"), ErrorMessage);
2009 if (m_menuPosition.isNull()) position = mapFromGlobal(QCursor::pos());
2010 else position = m_menuPosition;
2011 GenTime pos = GenTime((int)(mapToScene(position).x() / m_scale), m_document->fps());
2012 int track = (int)(position.y() / m_tracksHeight);
2013 ItemInfo first = m_copiedItems.at(0)->info();
2015 GenTime offset = pos - first.startPos;
2016 int trackOffset = track - first.track;
2018 if (!canBePasted(m_copiedItems, offset, trackOffset)) {
2019 emit displayMessage(i18n("Cannot paste selected clips"), ErrorMessage);
2022 QUndoCommand *pasteClips = new QUndoCommand();
2023 pasteClips->setText("Paste clips");
2025 for (int i = 0; i < m_copiedItems.count(); i++) {
2026 // parse all clip names
2027 if (m_copiedItems.at(i) && m_copiedItems.at(i)->type() == AVWIDGET) {
2028 ClipItem *clip = static_cast <ClipItem *>(m_copiedItems.at(i));
2030 info.startPos = clip->startPos() + offset;
2031 info.endPos = clip->endPos() + offset;
2032 info.track = clip->track() + trackOffset;
2033 if (canBePastedTo(info, AVWIDGET)) {
2034 new AddTimelineClipCommand(this, clip->xml(), clip->clipProducer(), info, true, false, pasteClips);
2035 } else emit displayMessage(i18n("Cannot paste clip to selected place"), ErrorMessage);
2036 } else if (m_copiedItems.at(i) && m_copiedItems.at(i)->type() == TRANSITIONWIDGET) {
2037 Transition *tr = static_cast <Transition *>(m_copiedItems.at(i));
2039 info.startPos = tr->startPos() + offset;
2040 info.endPos = tr->endPos() + offset;
2041 info.track = tr->track() + trackOffset;
2042 if (canBePastedTo(info, TRANSITIONWIDGET)) {
2043 new AddTransitionCommand(this, info, tr->transitionEndTrack() + trackOffset, tr->toXML(), false, true, pasteClips);
2044 } else emit displayMessage(i18n("Cannot paste transition to selected place"), ErrorMessage);
2047 m_commandStack->push(pasteClips);
2051 void CustomTrackView::drawForeground ( QPainter * painter, const QRectF & rect )
2053 //kDebug()<<"///// DRAWING FB: "<<rect.x()<<", width: "<<rect.width();
2054 painter->fillRect(rect, QColor(50, rand() % 250,50,100));
2055 painter->drawLine(m_cursorPos, rect.y(), m_cursorPos, rect.y() + rect.height());
2058 #include "customtrackview.moc"