]> git.sesse.net Git - kdenlive/blob - src/customtrackview.cpp
More efficient usage of MLT producers
[kdenlive] / src / customtrackview.cpp
1 /***************************************************************************
2  *   Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
3  *                                                                         *
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.                                   *
8  *                                                                         *
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.                          *
13  *                                                                         *
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  ***************************************************************************/
19
20 #include <QMouseEvent>
21 #include <QStylePainter>
22 #include <QGraphicsItem>
23 #include <QDomDocument>
24 #include <QScrollBar>
25 #include <QApplication>
26 #include <QInputDialog>
27
28 #include <KDebug>
29 #include <KLocale>
30 #include <KUrl>
31 #include <KIcon>
32 #include <KCursor>
33
34 #include "customtrackview.h"
35 #include "docclipbase.h"
36 #include "clipitem.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"
53 #include "clipitem.h"
54 #include "customtrackview.h"
55 #include "clipmanager.h"
56 #include "renderer.h"
57 #include "markerdialog.h"
58 #include "mainwindow.h"
59 #include "ui_keyframedialog_ui.h"
60 #include "clipdurationdialog.h"
61
62
63 //TODO:
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;
70
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);
76     setAcceptDrops(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);
84     m_tipPen.setWidth(3);
85     setContentsMargins(0, 0, 0, 0);
86     if (projectscene) {
87         m_cursorLine = projectscene->addLine(0, 0, 0, m_tracksHeight);
88         m_cursorLine->setFlags(QGraphicsItem::ItemIsMovable);
89         m_cursorLine->setZValue(1000);
90     }
91
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()));
96 }
97
98 CustomTrackView::~CustomTrackView() {
99     qDeleteAll(m_guides);
100 }
101
102
103 void CustomTrackView::setContextMenu(QMenu *timeline, QMenu *clip, QMenu *transition) {
104     m_timelineContextMenu = timeline;
105     m_timelineContextClipMenu = clip;
106     m_timelineContextTransitionMenu = transition;
107 }
108
109 void CustomTrackView::checkAutoScroll() {
110     m_autoScroll = KdenliveSettings::autoscroll();
111 }
112
113 QList <TrackInfo> CustomTrackView::tracksList() const {
114     return m_tracksList;
115 }
116
117 void CustomTrackView::checkTrackHeight() {
118     if (m_tracksHeight == KdenliveSettings::trackheight()) return;
119     m_tracksHeight = KdenliveSettings::trackheight();
120     emit trackHeightChanged();
121     QList<QGraphicsItem *> itemList = items();
122     ClipItem *item;
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);
128             item->resetThumbs();
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);
132         }
133     }
134     m_cursorLine->setLine(m_cursorLine->line().x1(), 0, m_cursorLine->line().x1(), m_tracksHeight * m_tracksList.count());
135
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);
140     }
141
142     setSceneRect(0, 0, sceneRect().width(), m_tracksHeight * m_tracksList.count());
143     verticalScrollBar()->setMaximum(m_tracksHeight * m_tracksList.count());
144     update();
145 }
146
147 // virtual
148 void CustomTrackView::resizeEvent(QResizeEvent * event) {
149     QGraphicsView::resizeEvent(event);
150 }
151
152 // virtual
153 void CustomTrackView::wheelEvent(QWheelEvent * e) {
154     if (e->modifiers() == Qt::ControlModifier) {
155         if (e->delta() > 0) emit zoomIn();
156         else emit zoomOut();
157     } else {
158         if (e->delta() > 0) horizontalScrollBar()->setValue(horizontalScrollBar()->value() + horizontalScrollBar()->singleStep());
159         else  horizontalScrollBar()->setValue(horizontalScrollBar()->value() - horizontalScrollBar()->singleStep());
160     }
161 }
162
163 int CustomTrackView::getPreviousVideoTrack(int track) {
164     track = m_tracksList.count() - track - 1;
165     track --;
166     for (int i = track; i > -1; i--) {
167         if (m_tracksList.at(i).type == VIDEOTRACK) return i + 1;
168     }
169     return 0;
170 }
171
172 // virtual
173
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();
186
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;
190
191                 int offset = moveTrack - currentTrack;
192                 if (m_selectedClipList.count() == 1) m_dragItem->moveTo((int)(snappedPos / m_scale), m_scale, offset * m_tracksHeight, moveTrack);
193                 else {
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);
199                         }
200                     }
201                 }
202
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);
222             }
223
224             if (m_animation) delete m_animation;
225             m_animation = NULL;
226             if (m_visualTip) delete m_visualTip;
227             m_visualTip = NULL;
228             QGraphicsView::mouseMoveEvent(event);
229             return;
230         } else if (m_operationMode == MOVEGUIDE) {
231             if (m_animation) delete m_animation;
232             m_animation = NULL;
233             if (m_visualTip) delete m_visualTip;
234             m_visualTip = NULL;
235             QGraphicsView::mouseMoveEvent(event);
236             return;
237         }
238     }
239
240     if (m_tool == RAZORTOOL) {
241         setCursor(m_razorCursor);
242         //QGraphicsView::mouseMoveEvent(event);
243         //return;
244     }
245
246     QList<QGraphicsItem *> itemList = items(event->pos());
247     QGraphicsRectItem *item = NULL;
248     OPERATIONTYPE opMode = NONE;
249
250     if (itemList.count() == 1 && itemList.at(0)->type() == GUIDEITEM) {
251         opMode = MOVEGUIDE;
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);
255                 break;
256             }
257         }
258
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()));
265             }
266             QGraphicsView::mouseMoveEvent(event);
267             return;
268         }
269         opMode = clip->operationMode(mapToScene(event->pos()), m_scale);
270         double size = 8;
271         if (opMode == m_moveOpMode) {
272             QGraphicsView::mouseMoveEvent(event);
273             return;
274         } else {
275             if (m_visualTip) {
276                 if (m_animation) delete m_animation;
277                 m_animation = NULL;
278                 m_animationTimer->stop();
279                 delete m_visualTip;
280                 m_visualTip = NULL;
281             }
282         }
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) {
289                 QPolygon polygon;
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));
294
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);
303                 double scale = 2.0;
304                 m_animation->setScaleAt(.5, scale, 1);
305                 m_animation->setPosAt(.5, QPointF(clip->rect().x() - clip->rect().x() * scale, 0));
306                 scale = 1.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();
311             }
312         } else if (opMode == RESIZEEND) {
313             setCursor(KCursor("right_side", Qt::SizeHorCursor));
314             if (m_visualTip == NULL) {
315                 QPolygon polygon;
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));
320
321                 m_visualTip = new QGraphicsPolygonItem(polygon);
322                 ((QGraphicsPolygonItem*) m_visualTip)->setBrush(m_tipColor);
323                 ((QGraphicsPolygonItem*) m_visualTip)->setPen(m_tipPen);
324
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);
330                 double scale = 2.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));
333                 scale = 1.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();
338             }
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);
350                 double scale = 2.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));
353                 scale = 1.0;
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();
358             }
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);
371                 double scale = 2.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));
374                 scale = 1.0;
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();
379             }
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);
391                 double scale = 2.0;
392                 m_animation->setScaleAt(.5, scale, scale);
393                 scale = 1.0;
394                 m_animation->setScaleAt(1, scale, scale);
395                 scene()->addItem(m_visualTip);
396                 m_animationTimer->start();
397             }
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);
409                 double scale = 2.0;
410                 m_animation->setScaleAt(.5, scale, scale);
411                 scale = 1.0;
412                 m_animation->setScaleAt(1, scale, scale);
413                 scene()->addItem(m_visualTip);
414                 m_animationTimer->start();
415             }
416             setCursor(Qt::PointingHandCursor);
417         } else if (opMode == KEYFRAME) {
418             setCursor(Qt::PointingHandCursor);
419         }
420     } // no clip under mouse
421     else if (m_tool == RAZORTOOL) {
422         QGraphicsView::mouseMoveEvent(event);
423         return;
424     } else if (opMode == MOVEGUIDE) {
425         m_moveOpMode = opMode;
426         setCursor(Qt::SplitHCursor);
427     } else {
428         m_moveOpMode = NONE;
429         if (event->buttons() != Qt::NoButton && event->modifiers() == Qt::NoModifier) {
430             setCursorPos((int)(mapToScene(event->pos().x(), 0).x() / m_scale));
431         }
432         if (m_visualTip) {
433             if (m_animation) delete m_animation;
434             m_animationTimer->stop();
435             m_animation = NULL;
436             delete m_visualTip;
437             m_visualTip = NULL;
438
439         }
440         setCursor(Qt::ArrowCursor);
441     }
442     QGraphicsView::mouseMoveEvent(event);
443 }
444
445 // virtual
446 void CustomTrackView::mousePressEvent(QMouseEvent * event) {
447     m_menuPosition = QPoint();
448     activateMonitor();
449     m_clickEvent = event->pos();
450     QList<QGraphicsItem *> collisionList = items(event->pos());
451     if (event->button() == Qt::MidButton) {
452         m_document->renderer()->switchPlay();
453         return;
454     }
455     if (event->modifiers() == Qt::ControlModifier && collisionList.count() == 0) {
456         setDragMode(QGraphicsView::ScrollHandDrag);
457         QGraphicsView::mousePressEvent(event);
458         return;
459     } else if (event->modifiers() == Qt::ShiftModifier && collisionList.count() == 0) {
460         setDragMode(QGraphicsView::RubberBandDrag);
461         QGraphicsView::mousePressEvent(event);
462         return;
463     } else {
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);
468             m_dragItem = NULL;
469             m_dragGuide = (Guide *) collisionList.at(0);
470             collision = true;
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);
484                             return;
485                         }
486                         AbstractClipItem *clip = (AbstractClipItem *) item;
487                         ItemInfo info;
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);
494                         return;
495                     }
496                     // select item
497                     if (!item->isSelected()) {
498
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();
504                             }
505                         }
506                         item->setSelected(true);
507                         item->update();
508                     }
509
510                     m_dragItem = (AbstractClipItem *) item;
511
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();
516
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)));
522                     }
523
524                     m_operationMode = m_dragItem->operationMode(item->mapFromScene(mapToScene(event->pos())), m_scale);
525                     if (m_operationMode == KEYFRAME) {
526                         m_dragItem->updateSelectedKeyFrame();
527                         return;
528                     } else if (m_operationMode == MOVE) setCursor(Qt::ClosedHandCursor);
529                     else if (m_operationMode == TRANSITIONSTART) {
530                         ItemInfo info;
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);
539
540                         slotAddTransition((ClipItem *) m_dragItem, info, transitiontrack);
541                     } else if (m_operationMode == TRANSITIONEND) {
542                         ItemInfo info;
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);
552                     }
553                     updateSnapPoints(m_dragItem);
554                     collision = true;
555                     break;
556                 }
557             }
558         if (!collision) {
559             kDebug() << "//////// NO ITEM FOUND ON CLICK";
560             m_dragItem = NULL;
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);
573             m_dragItem = NULL;
574         }
575         if (m_dragItem && m_dragItem->type() == AVWIDGET) emit clipItemSelected((ClipItem*) m_dragItem);
576         else emit clipItemSelected(NULL);
577     }
578     //kDebug()<<pos;
579     //QGraphicsView::mousePressEvent(event);
580 }
581
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;
588             view.setupUi(&d);
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());
602             }
603
604         } else  {
605             // add keyframe
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());
615         }
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) {
621                     ItemInfo startInfo;
622                     startInfo.startPos = m_dragItem->startPos();
623                     startInfo.endPos = m_dragItem->endPos();
624                     startInfo.track = m_dragItem->track();
625                     ItemInfo endInfo;
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);
631                 } else {
632                     //TODO: move transition
633                 }
634             }
635             if (d.duration() != m_dragItem->duration()) {
636                 if (m_dragItem->type() == AVWIDGET) {
637                     ItemInfo startInfo;
638                     startInfo.startPos = m_dragItem->startPos();
639                     startInfo.endPos = m_dragItem->endPos();
640                     startInfo.track = m_dragItem->track();
641                     ItemInfo endInfo;
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);
647                 } else {
648                     //TODO: resize transition
649                 }
650             }
651         }
652     } else {
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());
657         }
658     }
659 }
660
661
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);
664     if (clip) {
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);
668 }
669
670
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);
675 }
676
677 void CustomTrackView::activateMonitor() {
678     emit activateDocumentMonitor();
679 }
680
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) {
687         }
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);
693 }
694
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);
700         return;
701     }
702     bool success = true;
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;
705     }
706     if (!success) emit displayMessage(i18n("Problem adding effect to clip"), ErrorMessage);
707     m_document->renderer()->doRefresh();
708 }
709
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);
712     if (clip) {
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);
718 }
719
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);
724         return;
725     }
726     ClipItem *clip = getClipItemAt((int)pos.frames(m_document->fps()) + 1, m_tracksList.count() - track);
727     if (clip) {
728         clip->deleteEffect(index);
729         emit clipItemSelected(clip);
730     }
731 }
732
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);
740     }
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);
748         }
749     }
750     m_document->setModified(true);
751 }
752
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);
757 }
758
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);
761     if (clip) {
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());
771         }
772     }
773     m_document->setModified(true);
774 }
775
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);
778     if (clip) {
779         m_document->renderer()->mltMoveEffect(track, pos, oldPos, newPos);
780     }
781     m_document->setModified(true);
782 }
783
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);
791 }
792
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);
797 }
798
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);
802 }
803
804 void CustomTrackView::cutClip(ItemInfo info, GenTime cutTime, bool cut) {
805     if (cut) {
806         // cut clip
807         ClipItem *item = getClipItemAt((int) info.startPos.frames(m_document->fps()), info.track);
808         if (!item) {
809             emit displayMessage(i18n("Cannot find clip to cut"), ErrorMessage);
810             return;
811         }
812         int cutPos = (int) cutTime.frames(m_document->fps());
813         ItemInfo newPos;
814         newPos.startPos = cutTime;
815         newPos.endPos = info.endPos;
816         newPos.cropStart = item->cropStart() + (cutTime - info.startPos);
817         newPos.track = info.track;
818         item->resizeEnd(cutPos, m_scale);
819         ClipItem *dup = new ClipItem(item->baseClip(), newPos, m_scale, m_document->fps());
820         scene()->addItem(dup);
821         m_document->renderer()->mltCutClip(m_tracksList.count() - info.track, cutTime);
822         item->baseClip()->addReference();
823         m_document->updateClip(item->baseClip()->getId());
824     } else {
825         // uncut clip
826         ClipItem *item = getClipItemAt((int) info.startPos.frames(m_document->fps()), info.track);
827         ClipItem *dup = getClipItemAt((int) cutTime.frames(m_document->fps()), info.track);
828         if (!item || !dup) {
829             emit displayMessage(i18n("Cannot find clip to uncut"), ErrorMessage);
830             return;
831         }
832         delete dup;
833         item->baseClip()->removeReference();
834         m_document->updateClip(item->baseClip()->getId());
835         item->resizeEnd((int) info.endPos.frames(m_document->fps()), m_scale);
836         m_document->renderer()->mltRemoveClip(m_tracksList.count() - info.track, cutTime);
837         m_document->renderer()->mltResizeClipEnd(m_tracksList.count() - info.track, info.startPos, item->cropStart(), item->cropStart() + info.endPos - info.startPos);
838     }
839 }
840
841
842 void CustomTrackView::slotAddTransitionToSelectedClips(QDomElement transition) {
843     QList<QGraphicsItem *> itemList = scene()->selectedItems();
844     for (int i = 0; i < itemList.count(); i++) {
845         if (itemList.at(i)->type() == AVWIDGET) {
846             ClipItem *item = (ClipItem *) itemList.at(i);
847             ItemInfo info;
848             info.startPos = item->startPos();
849             info.endPos = info.startPos + GenTime(2.5);
850             info.track = item->track();
851             int transitiontrack = getPreviousVideoTrack(info.track);
852             slotAddTransition(item, info, transitiontrack, transition);
853         }
854     }
855 }
856
857 void CustomTrackView::slotAddTransition(ClipItem* clip, ItemInfo transitionInfo, int endTrack, QDomElement transition) {
858     AddTransitionCommand* command = new AddTransitionCommand(this, transitionInfo, endTrack, transition, false, true);
859     m_commandStack->push(command);
860     m_document->setModified(true);
861 }
862
863 void CustomTrackView::addTransition(ItemInfo transitionInfo, int endTrack, QDomElement params) {
864     Transition *tr = new Transition(transitionInfo, endTrack, m_scale, m_document->fps(), params);
865     scene()->addItem(tr);
866
867     //kDebug() << "---- ADDING transition " << e.attribute("tag") << ", on tracks " << m_tracksList.count() - e.attribute("transition_track").toInt() << " / " << getPreviousVideoTrack(e.attribute("transition_track").toInt());
868     m_document->renderer()->mltAddTransition(tr->transitionTag(), endTrack, m_tracksList.count() - transitionInfo.track, transitionInfo.startPos, transitionInfo.endPos, tr->toXML());
869     m_document->setModified(true);
870 }
871
872 void CustomTrackView::deleteTransition(ItemInfo transitionInfo, int endTrack, QDomElement params) {
873     Transition *item = getTransitionItemAt((int)transitionInfo.startPos.frames(m_document->fps()) + 1, transitionInfo.track);
874     m_document->renderer()->mltDeleteTransition(item->transitionTag(), endTrack, m_tracksList.count() - transitionInfo.track, transitionInfo.startPos, transitionInfo.endPos, item->toXML());
875     delete item;
876     emit transitionItemSelected(NULL);
877     m_document->setModified(true);
878 }
879
880 void CustomTrackView::slotTransitionUpdated(Transition *tr, QDomElement old) {
881     EditTransitionCommand *command = new EditTransitionCommand(this, tr->track(), tr->startPos(), old, tr->toXML() , true);
882     m_commandStack->push(command);
883     m_document->setModified(true);
884 }
885
886 void CustomTrackView::updateTransition(int track, GenTime pos, QDomElement oldTransition, QDomElement transition) {
887     Transition *item = getTransitionItemAt((int)pos.frames(m_document->fps()) + 1, track);
888     if (!item) {
889         kWarning() << "Unable to find transition at pos :" << pos.frames(m_document->fps()) << ", ON track: " << track;
890         return;
891     }
892     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);
893     item->setTransitionParameters(transition);
894     m_document->setModified(true);
895 }
896
897 void CustomTrackView::addItem(DocClipBase *clip, QPoint pos) {
898     ItemInfo info;
899     info.startPos = GenTime((int)(mapToScene(pos).x() / m_scale), m_document->fps());
900     info.endPos = info.startPos + clip->duration();
901     info.track = (int)(pos.y() / m_tracksHeight);
902     //kDebug()<<"------------  ADDING CLIP ITEM----: "<<info.startPos.frames(25)<<", "<<info.endPos.frames(25)<<", "<<info.track;
903     m_dropItem = new ClipItem(clip, info, m_scale, m_document->fps());
904     scene()->addItem(m_dropItem);
905 }
906
907
908 void CustomTrackView::dragMoveEvent(QDragMoveEvent * event) {
909     event->setDropAction(Qt::IgnoreAction);
910     if (m_dropItem) {
911         int track = (int)(mapToScene(event->pos()).y() / m_tracksHeight);  //) * (m_scale * 50) + m_scale;
912         m_dropItem->moveTo((int)(mapToScene(event->pos()).x() / m_scale), m_scale, (int)((track - m_dropItem->track()) * m_tracksHeight), track);
913         event->setDropAction(Qt::MoveAction);
914         if (event->mimeData()->hasFormat("kdenlive/producerslist")) {
915             event->acceptProposedAction();
916         }
917     } else {
918         QGraphicsView::dragMoveEvent(event);
919     }
920 }
921
922 void CustomTrackView::dragLeaveEvent(QDragLeaveEvent * event) {
923     if (m_dropItem) {
924         delete m_dropItem;
925         m_dropItem = NULL;
926     } else QGraphicsView::dragLeaveEvent(event);
927 }
928
929 void CustomTrackView::dropEvent(QDropEvent * event) {
930     if (m_dropItem) {
931         AddTimelineClipCommand *command = new AddTimelineClipCommand(this, m_dropItem->xml(), m_dropItem->clipProducer(), m_dropItem->info(), false, false);
932         m_commandStack->push(command);
933         m_dropItem->baseClip()->addReference();
934         m_document->updateClip(m_dropItem->baseClip()->getId());
935         ItemInfo info;
936         info = m_dropItem->info();
937         info.track = m_tracksList.count() - m_dropItem->track();
938         // kDebug()<<"IIIIIIIIIIIIIIIIIIIIIIII TRAX CNT: "<<m_tracksList.count()<<", DROP: "<<m_dropItem->track();
939         m_document->renderer()->mltInsertClip(info, m_dropItem->xml(), m_dropItem->baseClip()->producer());
940         m_document->setModified(true);
941     } else QGraphicsView::dropEvent(event);
942     m_dropItem = NULL;
943 }
944
945
946 QStringList CustomTrackView::mimeTypes() const {
947     QStringList qstrList;
948     // list of accepted mime types for drop
949     qstrList.append("text/plain");
950     qstrList.append("kdenlive/producerslist");
951     return qstrList;
952 }
953
954 Qt::DropActions CustomTrackView::supportedDropActions() const {
955     // returns what actions are supported when dropping
956     return Qt::MoveAction;
957 }
958
959 void CustomTrackView::setDuration(int duration) {
960     kDebug() << "/////////////  PRO DUR: " << duration << ", SCALE. " << (m_projectDuration + 500) * m_scale << ", height: " << 50 * m_tracksList.count();
961     m_projectDuration = duration;
962     setSceneRect(0, 0, (m_projectDuration + 100) * m_scale, sceneRect().height());
963 }
964
965 int CustomTrackView::duration() const {
966     return m_projectDuration;
967 }
968
969 void CustomTrackView::addTrack(TrackInfo type) {
970     m_tracksList << type;
971     m_cursorLine->setLine(m_cursorLine->line().x1(), 0, m_cursorLine->line().x1(), m_tracksHeight * m_tracksList.count());
972     setSceneRect(0, 0, sceneRect().width(), m_tracksHeight * m_tracksList.count());
973     verticalScrollBar()->setMaximum(m_tracksHeight * m_tracksList.count());
974     //setFixedHeight(50 * m_tracksCount);
975 }
976
977 void CustomTrackView::removeTrack() {
978     // TODO: implement track deletion
979     //m_tracksCount--;
980     m_cursorLine->setLine(m_cursorLine->line().x1(), 0, m_cursorLine->line().x1(), m_tracksHeight * m_tracksList.count());
981 }
982
983
984 void CustomTrackView::slotSwitchTrackAudio(int ix) {
985     int tracknumber = m_tracksList.count() - ix;
986     kDebug() << "/////  MUTING TRK: " << ix << "; PL NUM: " << tracknumber;
987     m_tracksList[tracknumber - 1].isMute = !m_tracksList.at(tracknumber - 1).isMute;
988     m_document->renderer()->mltChangeTrackState(tracknumber, m_tracksList.at(tracknumber - 1).isMute, m_tracksList.at(tracknumber - 1).isBlind);
989 }
990
991 void CustomTrackView::slotSwitchTrackVideo(int ix) {
992     int tracknumber = m_tracksList.count() - ix;
993     m_tracksList[tracknumber - 1].isBlind = !m_tracksList.at(tracknumber - 1).isBlind;
994     m_document->renderer()->mltChangeTrackState(tracknumber, m_tracksList.at(tracknumber - 1).isMute, m_tracksList.at(tracknumber - 1).isBlind);
995 }
996
997 void CustomTrackView::deleteClip(int clipId) {
998     QList<QGraphicsItem *> itemList = items();
999     for (int i = 0; i < itemList.count(); i++) {
1000         if (itemList.at(i)->type() == AVWIDGET) {
1001             ClipItem *item = (ClipItem *)itemList.at(i);
1002             if (item->clipProducer() == clipId) {
1003                 AddTimelineClipCommand *command = new AddTimelineClipCommand(this, item->xml(), item->clipProducer(), item->info(), true, true);
1004                 m_commandStack->push(command);
1005                 //delete item;
1006             }
1007         }
1008     }
1009 }
1010
1011 void CustomTrackView::setCursorPos(int pos, bool seek) {
1012     emit cursorMoved((int)(m_cursorPos * m_scale), (int)(pos * m_scale));
1013     m_cursorPos = pos;
1014     m_cursorLine->setPos(pos * m_scale, 0);
1015     if (seek) m_document->renderer()->seek(GenTime(pos, m_document->fps()));
1016     else if (m_autoScroll && m_scale < 50) checkScrolling();
1017 }
1018
1019 void CustomTrackView::updateCursorPos() {
1020     m_cursorLine->setPos(m_cursorPos * m_scale, 0);
1021 }
1022
1023 int CustomTrackView::cursorPos() {
1024     return (int)(m_cursorPos * m_scale);
1025 }
1026
1027 void CustomTrackView::moveCursorPos(int delta) {
1028     if (m_cursorPos + delta < 0) delta = 0 - m_cursorPos;
1029     emit cursorMoved((int)(m_cursorPos * m_scale), (int)((m_cursorPos + delta) * m_scale));
1030     m_cursorPos += delta;
1031     m_cursorLine->setPos(m_cursorPos * m_scale, 0);
1032     m_document->renderer()->seek(GenTime(m_cursorPos, m_document->fps()));
1033     //if (m_autoScroll && m_scale < 50) checkScrolling();
1034 }
1035
1036 void CustomTrackView::checkScrolling() {
1037     int vert = verticalScrollBar()->value();
1038     int hor = cursorPos();
1039     ensureVisible(hor, vert + 10, 2, 2, 50, 0);
1040     //centerOn(QPointF(cursorPos(), m_tracksHeight));
1041     /*QRect rectInView = viewport()->rect();
1042     int delta = rectInView.width() / 3;
1043     int max = rectInView.right() + horizontalScrollBar()->value() - delta;
1044     //kDebug() << "CURSOR POS: "<<m_cursorPos<< "Scale: "<<m_scale;
1045     if (m_cursorPos * m_scale >= max) horizontalScrollBar()->setValue((int)(horizontalScrollBar()->value() + 1 + m_scale));*/
1046 }
1047
1048 void CustomTrackView::mouseReleaseEvent(QMouseEvent * event) {
1049     if (event->button() == Qt::MidButton) {
1050         return;
1051     }
1052     QGraphicsView::mouseReleaseEvent(event);
1053     setDragMode(QGraphicsView::NoDrag);
1054     if (m_operationMode == MOVEGUIDE) {
1055         setCursor(Qt::ArrowCursor);
1056         m_operationMode = NONE;
1057         m_dragGuide->setFlag(QGraphicsItem::ItemIsMovable, false);
1058         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);
1059         m_commandStack->push(command);
1060         m_dragGuide->updateGuide(GenTime(m_dragGuide->pos().x() / m_scale, m_document->fps()));
1061         m_dragGuide = NULL;
1062         m_dragItem = NULL;
1063         return;
1064     }
1065     if (m_dragItem == NULL) {
1066         emit transitionItemSelected(NULL);
1067         return;
1068     }
1069     ItemInfo info;
1070     info.startPos = m_dragItem->startPos();
1071     info.endPos = m_dragItem->endPos();
1072     info.track = m_dragItem->track();
1073
1074     if (m_operationMode == MOVE) {
1075         setCursor(Qt::OpenHandCursor);
1076
1077         if (m_selectedClipList.count() == 1) {
1078             // we are moving one clip, easy
1079             if (m_dragItem->type() == AVWIDGET && (m_dragItemInfo.startPos != info.startPos || m_dragItemInfo.track != info.track)) {
1080                 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())));
1081                 if (success) {
1082                     MoveClipCommand *command = new MoveClipCommand(this, m_dragItemInfo, info, false);
1083                     m_commandStack->push(command);
1084                 } else {
1085                     // undo last move and emit error message
1086                     MoveClipCommand *command = new MoveClipCommand(this, info, m_dragItemInfo, true);
1087                     m_commandStack->push(command);
1088                     emit displayMessage(i18n("Cannot move clip to position %1seconds", QString::number(m_dragItemInfo.startPos.seconds(), 'g', 2)), ErrorMessage);
1089                 }
1090             }
1091             if (m_dragItem->type() == TRANSITIONWIDGET && (m_dragItemInfo.startPos != info.startPos || m_dragItemInfo.track != info.track)) {
1092                 MoveTransitionCommand *command = new MoveTransitionCommand(this, m_dragItemInfo, info, false);
1093                 m_commandStack->push(command);
1094                 Transition *transition = (Transition *) m_dragItem;
1095                 transition->updateTransitionEndTrack(getPreviousVideoTrack(m_dragItem->track()));
1096                 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);
1097             }
1098         } else {
1099             // Moving several clips. We need to delete them and readd them to new position,
1100             // or they might overlap each other during the move
1101
1102             GenTime timeOffset = info.startPos - m_dragItemInfo.startPos;
1103             int trackOffset = info.track - m_dragItemInfo.track;
1104             if (timeOffset != GenTime() || trackOffset != 0) {
1105                 QUndoCommand *moveClips = new QUndoCommand();
1106                 moveClips->setText("Move clips");
1107                 // remove items in MLT playlist
1108                 for (int i = 0; i < m_selectedClipList.count(); i++) {
1109                     AbstractClipItem *item = m_selectedClipList.at(i);
1110                     ItemInfo info = item->info();
1111                     info.startPos = info.startPos - timeOffset;
1112                     info.endPos = info.endPos - timeOffset;
1113                     info.track = info.track - trackOffset;
1114                     if (item->type() == AVWIDGET) {
1115                         ClipItem *clip = static_cast <ClipItem*>(item);
1116                         new AddTimelineClipCommand(this, clip->xml(), clip->clipProducer(), info, false, true, moveClips);
1117                         m_document->renderer()->mltRemoveClip(m_tracksList.count() - info.track, info.startPos);
1118                     } else {
1119                         Transition *tr = static_cast <Transition*>(item);
1120                         new AddTransitionCommand(this, info, tr->transitionEndTrack(), tr->toXML(), false, true, moveClips);
1121                         m_document->renderer()->mltDeleteTransition(tr->transitionTag(), tr->transitionEndTrack(), m_tracksList.count() - info.track, info.startPos, info.endPos, tr->toXML());
1122                     }
1123                 }
1124
1125                 for (int i = 0; i < m_selectedClipList.count(); i++) {
1126                     // re-add items in correct place
1127                     AbstractClipItem *item = m_selectedClipList.at(i);
1128                     if (item->type() == AVWIDGET) {
1129                         ClipItem *clip = static_cast <ClipItem*>(item);
1130                         new AddTimelineClipCommand(this, clip->xml(), clip->clipProducer(), item->info(), false, false, moveClips);
1131                         ItemInfo info = item->info();
1132                         info.track = m_tracksList.count() - item->track();
1133                         m_document->renderer()->mltInsertClip(info, clip->xml(), clip->baseClip()->producer());
1134                     } else {
1135                         Transition *tr = static_cast <Transition*>(item);
1136                         ItemInfo transitionInfo = tr->info();
1137                         new AddTransitionCommand(this, info, tr->transitionEndTrack(), tr->toXML(), false, false, moveClips);
1138                         m_document->renderer()->mltAddTransition(tr->transitionTag(), tr->transitionEndTrack() + trackOffset, m_tracksList.count() - transitionInfo.track, transitionInfo.startPos, transitionInfo.endPos, tr->toXML());
1139                     }
1140                 }
1141                 m_commandStack->push(moveClips);
1142             }
1143         }
1144
1145     } else if (m_operationMode == RESIZESTART && m_dragItem->startPos() != m_dragItemInfo.startPos) {
1146         // resize start
1147         if (m_dragItem->type() == AVWIDGET) {
1148             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());
1149             updateClipFade((ClipItem *) m_dragItem);
1150             ResizeClipCommand *command = new ResizeClipCommand(this, m_dragItemInfo, info, false);
1151             m_commandStack->push(command);
1152         } else if (m_dragItem->type() == TRANSITIONWIDGET) {
1153             MoveTransitionCommand *command = new MoveTransitionCommand(this, m_dragItemInfo, info, false);
1154             m_commandStack->push(command);
1155             Transition *transition = (Transition *) m_dragItem;
1156             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);
1157         }
1158
1159         //m_document->renderer()->doRefresh();
1160     } else if (m_operationMode == RESIZEEND && m_dragItem->endPos() != m_dragItemInfo.endPos) {
1161         // resize end
1162         if (m_dragItem->type() == AVWIDGET) {
1163             ResizeClipCommand *command = new ResizeClipCommand(this, m_dragItemInfo, info, false);
1164             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());
1165             m_commandStack->push(command);
1166         } else if (m_dragItem->type() == TRANSITIONWIDGET) {
1167             MoveTransitionCommand *command = new MoveTransitionCommand(this, m_dragItemInfo, info, false);
1168             m_commandStack->push(command);
1169             Transition *transition = (Transition *) m_dragItem;
1170             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);
1171         }
1172         //m_document->renderer()->doRefresh();
1173     } else if (m_operationMode == FADEIN) {
1174         // resize fade in effect
1175         ClipItem * item = (ClipItem *) m_dragItem;
1176         QStringList clipeffects = item->effectNames();
1177         if (clipeffects.contains(i18n("Fade in"))) {
1178             int ix = clipeffects.indexOf(i18n("Fade in"));
1179             QDomElement oldeffect = item->effectAt(ix);
1180             int start = item->cropStart().frames(m_document->fps());
1181             int end = item->fadeIn();
1182             if (end == 0) {
1183                 slotDeleteEffect(item, oldeffect);
1184             } else {
1185                 end += start;
1186                 QDomElement effect = MainWindow::audioEffects.getEffectByName("Fade in");
1187                 EffectsList::setParameter(effect, "in", QString::number(start));
1188                 EffectsList::setParameter(effect, "out", QString::number(end));
1189                 slotUpdateClipEffect(item, oldeffect, effect, ix);
1190             }
1191         } else if (item->fadeIn() != 0) {
1192             QDomElement effect = MainWindow::audioEffects.getEffectByName("Fade in");
1193             int start = item->cropStart().frames(m_document->fps());
1194             int end = item->fadeIn() + start;
1195             EffectsList::setParameter(effect, "in", QString::number(start));
1196             EffectsList::setParameter(effect, "out", QString::number(end));
1197             slotAddEffect(effect, m_dragItem->startPos(), m_dragItem->track());
1198         }
1199     } else if (m_operationMode == FADEOUT) {
1200         // resize fade in effect
1201         ClipItem * item = (ClipItem *) m_dragItem;
1202         QStringList clipeffects = item->effectNames();
1203         if (clipeffects.contains(i18n("Fade out"))) {
1204             int ix = clipeffects.indexOf(i18n("Fade out"));
1205             QDomElement oldeffect = item->effectAt(ix);
1206             int end = (item->duration() + item->cropStart()).frames(m_document->fps());
1207             int start = item->fadeOut();
1208             if (start == 0) {
1209                 slotDeleteEffect(item, oldeffect);
1210             } else {
1211                 start = end - start;
1212                 QDomElement effect = MainWindow::audioEffects.getEffectByName("Fade out");
1213                 EffectsList::setParameter(effect, "in", QString::number(start));
1214                 EffectsList::setParameter(effect, "out", QString::number(end));
1215                 slotUpdateClipEffect(item, oldeffect, effect, ix);
1216             }
1217         } else if (item->fadeOut() != 0) {
1218             QDomElement effect = MainWindow::audioEffects.getEffectByName("Fade out");
1219             int end = (item->duration() + item->cropStart()).frames(m_document->fps());
1220             int start = end - item->fadeOut();
1221             EffectsList::setParameter(effect, "in", QString::number(start));
1222             EffectsList::setParameter(effect, "out", QString::number(end));
1223             slotAddEffect(effect, m_dragItem->startPos(), m_dragItem->track());
1224         }
1225     } else if (m_operationMode == KEYFRAME) {
1226         // update the MLT effect
1227         ClipItem * item = (ClipItem *) m_dragItem;
1228         QString previous = item->keyframes(item->selectedEffectIndex());
1229         item->updateKeyframeEffect();
1230         QString next = item->keyframes(item->selectedEffectIndex());
1231         EditKeyFrameCommand *command = new EditKeyFrameCommand(this, item->track(), item->startPos(), item->selectedEffectIndex(), previous, next, false);
1232         m_commandStack->push(command);
1233         updateEffect(m_tracksList.count() - item->track(), item->startPos(), item->selectedEffect(), item->selectedEffectIndex());
1234     }
1235
1236     emit transitionItemSelected((m_dragItem && m_dragItem->type() == TRANSITIONWIDGET) ? (Transition*) m_dragItem : NULL);
1237     m_document->setModified(true);
1238     m_operationMode = NONE;
1239 }
1240
1241 void CustomTrackView::deleteClip(ItemInfo info) {
1242     ClipItem *item = getClipItemAt((int) info.startPos.frames(m_document->fps()) + 1, info.track);
1243     if (!item) {
1244         kDebug() << "----------------  ERROR, CANNOT find clip to delete at...";// << rect.x();
1245         return;
1246     }
1247     if (item->isSelected()) emit clipItemSelected(NULL);
1248     item->baseClip()->removeReference();
1249     m_document->updateClip(item->baseClip()->getId());
1250     delete item;
1251     m_document->renderer()->mltRemoveClip(m_tracksList.count() - info.track, info.startPos);
1252     m_document->renderer()->doRefresh();
1253 }
1254
1255 void CustomTrackView::deleteSelectedClips() {
1256     QList<QGraphicsItem *> itemList = scene()->selectedItems();
1257     if (itemList.count() == 0) {
1258         emit displayMessage(i18n("Select clip to delete"), ErrorMessage);
1259         return;
1260     }
1261     QUndoCommand *deleteSelected = new QUndoCommand();
1262     deleteSelected->setText("Delete selected items");
1263     for (int i = 0; i < itemList.count(); i++) {
1264         if (itemList.at(i)->type() == AVWIDGET) {
1265             ClipItem *item = static_cast <ClipItem *>(itemList.at(i));
1266             new AddTimelineClipCommand(this, item->xml(), item->clipProducer(), item->info(), true, true, deleteSelected);
1267         } else if (itemList.at(i)->type() == TRANSITIONWIDGET) {
1268             Transition *item = static_cast <Transition *>(itemList.at(i));
1269             ItemInfo info;
1270             info.startPos = item->startPos();
1271             info.endPos = item->endPos();
1272             info.track = item->track();
1273             new AddTransitionCommand(this, info, item->transitionEndTrack(), item->toXML(), true, true, deleteSelected);
1274         }
1275     }
1276     m_commandStack->push(deleteSelected);
1277 }
1278
1279 void CustomTrackView::cutSelectedClips() {
1280     QList<QGraphicsItem *> itemList = scene()->selectedItems();
1281     GenTime currentPos = GenTime(m_cursorPos, m_document->fps());
1282     for (int i = 0; i < itemList.count(); i++) {
1283         if (itemList.at(i)->type() == AVWIDGET) {
1284             ClipItem *item = (ClipItem *) itemList.at(i);
1285             ItemInfo info;
1286             info.startPos = item->startPos();
1287             info.endPos = item->endPos();
1288             if (currentPos > info.startPos && currentPos <  info.endPos) {
1289                 info.track = item->track();
1290                 RazorClipCommand *command = new RazorClipCommand(this, info, currentPos, true);
1291                 m_commandStack->push(command);
1292             }
1293         }
1294     }
1295 }
1296
1297 void CustomTrackView::addClip(QDomElement xml, int clipId, ItemInfo info) {
1298     DocClipBase *baseclip = m_document->clipManager()->getClipById(clipId);
1299     ClipItem *item = new ClipItem(baseclip, info, m_scale, m_document->fps());
1300     scene()->addItem(item);
1301     baseclip->addReference();
1302     m_document->updateClip(baseclip->getId());
1303     info.track = m_tracksList.count() - info.track;
1304     m_document->renderer()->mltInsertClip(info, xml, baseclip->producer());
1305     m_document->renderer()->doRefresh();
1306 }
1307
1308 void CustomTrackView::slotUpdateClip(int clipId) {
1309     QList<QGraphicsItem *> list = scene()->items();
1310     ClipItem *clip = NULL;
1311     for (int i = 0; i < list.size(); ++i) {
1312         if (list.at(i)->type() == AVWIDGET) {
1313             clip = static_cast <ClipItem *>(list.at(i));
1314             if (clip->clipProducer() == clipId) {
1315                 clip->refreshClip();
1316                 ItemInfo info = clip->info();
1317                 info.track = m_tracksList.count() - clip->track();
1318                 m_document->renderer()->mltUpdateClip(info, clip->xml(), clip->baseClip()->producer());
1319             }
1320         }
1321     }
1322 }
1323
1324 ClipItem *CustomTrackView::getClipItemAt(int pos, int track) {
1325     QList<QGraphicsItem *> list = scene()->items(QPointF(pos * m_scale, track * m_tracksHeight + m_tracksHeight / 2));
1326     ClipItem *clip = NULL;
1327     for (int i = 0; i < list.size(); ++i) {
1328         if (list.at(i)->type() == AVWIDGET) {
1329             clip = static_cast <ClipItem *>(list.at(i));
1330             break;
1331         }
1332     }
1333     return clip;
1334 }
1335
1336 ClipItem *CustomTrackView::getClipItemAt(GenTime pos, int track) {
1337     int framepos = (int)(pos.frames(m_document->fps()) * m_scale);
1338     return getClipItemAt(framepos, track);
1339 }
1340
1341 Transition *CustomTrackView::getTransitionItemAt(int pos, int track) {
1342     QList<QGraphicsItem *> list = scene()->items(QPointF(pos * m_scale, (track + 1) * m_tracksHeight));
1343     Transition *clip = NULL;
1344     for (int i = 0; i < list.size(); ++i) {
1345         if (list.at(i)->type() == TRANSITIONWIDGET) {
1346             clip = static_cast <Transition *>(list.at(i));
1347             break;
1348         }
1349     }
1350     return clip;
1351 }
1352
1353 Transition *CustomTrackView::getTransitionItemAt(GenTime pos, int track) {
1354     int framepos = (int)(pos.frames(m_document->fps()) * m_scale);
1355     return getTransitionItemAt(framepos, track);
1356 }
1357
1358 void CustomTrackView::moveClip(const ItemInfo start, const ItemInfo end) {
1359     ClipItem *item = getClipItemAt((int) start.startPos.frames(m_document->fps()) + 1, start.track);
1360     if (!item) {
1361         emit displayMessage(i18n("Cannot move clip at time: %1s on track %2", QString::number(start.startPos.seconds(), 'g', 2), start.track), ErrorMessage);
1362         kDebug() << "----------------  ERROR, CANNOT find clip to move at.. ";// << startPos.x() * m_scale * FRAME_SIZE + 1 << ", " << startPos.y() * m_tracksHeight + m_tracksHeight / 2;
1363         return;
1364     }
1365     //kDebug() << "----------------  Move CLIP FROM: " << startPos.x() << ", END:" << endPos.x() << ",TRACKS: " << startPos.y() << " TO " << endPos.y();
1366
1367     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()));
1368     if (success) {
1369         item->moveTo((int) end.startPos.frames(m_document->fps()), m_scale, (int)((end.track - start.track) * m_tracksHeight), end.track);
1370     } else {
1371         // undo last move and emit error message
1372         emit displayMessage(i18n("Cannot move clip to position %1seconds", QString::number(end.startPos.seconds(), 'g', 2)), ErrorMessage);
1373     }
1374 }
1375
1376 void CustomTrackView::moveTransition(const ItemInfo start, const ItemInfo end) {
1377     Transition *item = getTransitionItemAt((int)start.startPos.frames(m_document->fps()) + 1, start.track);
1378     if (!item) {
1379         emit displayMessage(i18n("Cannot move transition at time: %1s on track %2", start.startPos.seconds(), start.track), ErrorMessage);
1380         kDebug() << "----------------  ERROR, CANNOT find transition to move... ";// << startPos.x() * m_scale * FRAME_SIZE + 1 << ", " << startPos.y() * m_tracksHeight + m_tracksHeight / 2;
1381         return;
1382     }
1383     //kDebug() << "----------------  Move TRANSITION FROM: " << startPos.x() << ", END:" << endPos.x() << ",TRACKS: " << oldtrack << " TO " << newtrack;
1384
1385     //kDebug()<<"///  RESIZE TRANS START: ("<< startPos.x()<<"x"<< startPos.y()<<") / ("<<endPos.x()<<"x"<< endPos.y()<<")";
1386     if (end.endPos - end.startPos == start.endPos - start.startPos) {
1387         // Transition was moved
1388         item->moveTo((int) end.startPos.frames(m_document->fps()), m_scale, (end.track - start.track) * m_tracksHeight, end.track);
1389     } else if (end.endPos == start.endPos) {
1390         // Transition start resize
1391         item->resizeStart((int) end.startPos.frames(m_document->fps()), m_scale);
1392     } else {
1393         // Transition end resize;
1394         item->resizeEnd((int) end.endPos.frames(m_document->fps()), m_scale);
1395     }
1396     //item->moveTransition(GenTime((int) (endPos.x() - startPos.x()), m_document->fps()));
1397     item->updateTransitionEndTrack(getPreviousVideoTrack(end.track));
1398     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);
1399 }
1400
1401 void CustomTrackView::resizeClip(const ItemInfo start, const ItemInfo end) {
1402     int offset = 0;
1403     bool resizeClipStart = true;
1404     if (start.startPos == end.startPos) resizeClipStart = false;
1405     /*if (resizeClipStart) offset = 1;
1406     else offset = -1;*/
1407     ClipItem *item = getClipItemAt((int)(start.startPos.frames(m_document->fps()) + offset), start.track);
1408     if (!item) {
1409         emit displayMessage(i18n("Cannot move clip at time: %1s on track %2", start.startPos.seconds(), start.track), ErrorMessage);
1410         kDebug() << "----------------  ERROR, CANNOT find clip to resize at... "; // << startPos;
1411         return;
1412     }
1413     if (resizeClipStart) {
1414         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);
1415         item->resizeStart((int) end.startPos.frames(m_document->fps()), m_scale);
1416         updateClipFade(item);
1417     } else {
1418         m_document->renderer()->mltResizeClipEnd(m_tracksList.count() - item->track(), item->startPos(), item->cropStart(), item->cropStart() + end.endPos - item->startPos());
1419         item->resizeEnd((int) end.endPos.frames(m_document->fps()), m_scale);
1420         updateClipFade(item, true);
1421     }
1422     m_document->renderer()->doRefresh();
1423 }
1424
1425 void CustomTrackView::updateClipFade(ClipItem * item, bool updateFadeOut) {
1426     if (!updateFadeOut) {
1427         int end = item->fadeIn();
1428         if (end != 0) {
1429             // there is a fade in effect
1430             QStringList clipeffects = item->effectNames();
1431             QDomElement oldeffect = item->effectAt(clipeffects.indexOf("Fade in"));
1432             int start = item->cropStart().frames(m_document->fps());
1433             end += start;
1434             EffectsList::setParameter(oldeffect, "in", QString::number(start));
1435             EffectsList::setParameter(oldeffect, "out", QString::number(end));
1436             QMap <QString, QString> effectParams = item->getEffectArgs(oldeffect);
1437             if (!m_document->renderer()->mltEditEffect(m_tracksList.count() - item->track(), item->startPos(), effectParams))
1438                 emit displayMessage(i18n("Problem editing effect"), ErrorMessage);
1439         }
1440     } else {
1441         int start = item->fadeOut();
1442         if (start != 0) {
1443             // there is a fade in effect
1444             QStringList clipeffects = item->effectNames();
1445             QDomElement oldeffect = item->effectAt(clipeffects.indexOf("Fade out"));
1446             int end = (item->duration() - item->cropStart()).frames(m_document->fps());
1447             start = end - start;
1448             EffectsList::setParameter(oldeffect, "in", QString::number(start));
1449             EffectsList::setParameter(oldeffect, "out", QString::number(end));
1450             QMap <QString, QString> effectParams = item->getEffectArgs(oldeffect);
1451             if (m_document->renderer()->mltEditEffect(m_tracksList.count() - item->track(), item->startPos(), effectParams))
1452                 emit displayMessage(i18n("Problem editing effect"), ErrorMessage);
1453         }
1454     }
1455 }
1456
1457 double CustomTrackView::getSnapPointForPos(double pos) {
1458     for (int i = 0; i < m_snapPoints.size(); ++i) {
1459         if (abs((int)(pos - m_snapPoints.at(i).frames(m_document->fps()) * m_scale)) < 10) {
1460             //kDebug()<<" FOUND SNAP POINT AT: "<<m_snapPoints.at(i)<<", current pos: "<<pos / m_scale;
1461             return m_snapPoints.at(i).frames(m_document->fps()) * m_scale + 0.5;
1462         }
1463         if (m_snapPoints.at(i).frames(m_document->fps() * m_scale) > pos) break;
1464     }
1465     return pos;
1466 }
1467
1468 void CustomTrackView::updateSnapPoints(AbstractClipItem *selected) {
1469     m_snapPoints.clear();
1470     if (!KdenliveSettings::snaptopoints()) return;
1471     GenTime offset;
1472     if (selected) offset = selected->duration();
1473     QList<QGraphicsItem *> itemList = items();
1474     for (int i = 0; i < itemList.count(); i++) {
1475         if (itemList.at(i)->type() == AVWIDGET && itemList.at(i) != selected) {
1476             ClipItem *item = static_cast <ClipItem *>(itemList.at(i));
1477             GenTime start = item->startPos();
1478             GenTime end = item->endPos();
1479             m_snapPoints.append(start);
1480             m_snapPoints.append(end);
1481             QList < GenTime > markers = item->snapMarkers();
1482             for (int i = 0; i < markers.size(); ++i) {
1483                 GenTime t = markers.at(i);
1484                 m_snapPoints.append(t);
1485                 if (t > offset) m_snapPoints.append(t - offset);
1486             }
1487             if (offset != GenTime()) {
1488                 if (start > offset) m_snapPoints.append(start - offset);
1489                 if (end > offset) m_snapPoints.append(end - offset);
1490             }
1491         } else if (itemList.at(i)->type() == TRANSITIONWIDGET) {
1492             Transition *transition = static_cast <Transition*>(itemList.at(i));
1493             GenTime start = transition->startPos();
1494             GenTime end = transition->endPos();
1495             m_snapPoints.append(start);
1496             m_snapPoints.append(end);
1497             if (offset != GenTime()) {
1498                 if (start > offset) m_snapPoints.append(start - offset);
1499                 if (end > offset) m_snapPoints.append(end - offset);
1500             }
1501         }
1502     }
1503
1504     // add cursor position
1505     GenTime pos = GenTime(m_cursorPos, m_document->fps());
1506     m_snapPoints.append(pos);
1507     if (offset != GenTime()) m_snapPoints.append(pos - offset);
1508
1509     // add guides
1510     for (int i = 0; i < m_guides.count(); i++) {
1511         m_snapPoints.append(m_guides.at(i)->position());
1512         if (offset != GenTime()) m_snapPoints.append(m_guides.at(i)->position() - offset);
1513     }
1514
1515     qSort(m_snapPoints);
1516     //for (int i = 0; i < m_snapPoints.size(); ++i)
1517     //    kDebug() << "SNAP POINT: " << m_snapPoints.at(i).frames(25);
1518 }
1519
1520 void CustomTrackView::slotSeekToPreviousSnap() {
1521     updateSnapPoints(NULL);
1522     GenTime pos = GenTime(m_cursorPos, m_document->fps());
1523     GenTime res = GenTime();
1524     for (int i = 0; i < m_snapPoints.size(); ++i) {
1525         if (m_snapPoints.at(i) >= pos) {
1526             if (i == 0) i = 1;
1527             res = m_snapPoints.at(i - 1);
1528             break;
1529         }
1530     }
1531     setCursorPos((int) res.frames(m_document->fps()));
1532     checkScrolling();
1533 }
1534
1535 void CustomTrackView::slotSeekToNextSnap() {
1536     updateSnapPoints(NULL);
1537     GenTime pos = GenTime(m_cursorPos, m_document->fps());
1538     GenTime res = GenTime(m_projectDuration, m_document->fps());
1539     for (int i = 0; i < m_snapPoints.size(); ++i) {
1540         if (m_snapPoints.at(i) > pos) {
1541             res = m_snapPoints.at(i);
1542             break;
1543         }
1544     }
1545     setCursorPos((int) res.frames(m_document->fps()));
1546     checkScrolling();
1547 }
1548
1549 void CustomTrackView::clipStart() {
1550     QList<QGraphicsItem *> itemList = scene()->selectedItems();
1551     for (int i = 0; i < itemList.count(); i++) {
1552         if (itemList.at(i)->type() == AVWIDGET) {
1553             ClipItem *item = (ClipItem *) itemList.at(i);
1554             setCursorPos((int) item->startPos().frames(m_document->fps()));
1555             checkScrolling();
1556             break;
1557         }
1558     }
1559 }
1560
1561 void CustomTrackView::clipEnd() {
1562     QList<QGraphicsItem *> itemList = scene()->selectedItems();
1563     for (int i = 0; i < itemList.count(); i++) {
1564         if (itemList.at(i)->type() == AVWIDGET) {
1565             ClipItem *item = (ClipItem *) itemList.at(i);
1566             setCursorPos((int) item->endPos().frames(m_document->fps()));
1567             checkScrolling();
1568             break;
1569         }
1570     }
1571 }
1572
1573 void CustomTrackView::slotAddClipMarker() {
1574     QList<QGraphicsItem *> itemList = scene()->selectedItems();
1575     if (itemList.count() != 1) {
1576         emit displayMessage(i18n("Cannot add marker if more than one clip is selected"), ErrorMessage);
1577         kDebug() << "// CANNOT ADD MARKER IF MORE TAN ONE CLIP IS SELECTED....";
1578         return;
1579     }
1580     AbstractClipItem *item = (AbstractClipItem *)itemList.at(0);
1581     if (item->type() != AVWIDGET) return;
1582     GenTime pos = GenTime(m_cursorPos, m_document->fps());
1583     if (item->startPos() > pos || item->endPos() < pos) return;
1584     ClipItem *clip = (ClipItem *) item;
1585     int id = clip->baseClip()->getId();
1586     GenTime position = pos - item->startPos() + item->cropStart();
1587     CommentedTime marker(position, i18n("Marker"));
1588     MarkerDialog d(clip->baseClip(), marker, m_document->timecode(), this);
1589     if (d.exec() == QDialog::Accepted) {
1590         slotAddClipMarker(id, d.newMarker().time(), d.newMarker().comment());
1591     }
1592 }
1593
1594 void CustomTrackView::slotAddClipMarker(int id, GenTime t, QString c) {
1595     QString oldcomment = m_document->clipManager()->getClipById(id)->markerComment(t);
1596     AddMarkerCommand *command = new AddMarkerCommand(this, oldcomment, c, id, t, true);
1597     m_commandStack->push(command);
1598 }
1599
1600 void CustomTrackView::slotDeleteClipMarker() {
1601     QList<QGraphicsItem *> itemList = scene()->selectedItems();
1602     if (itemList.count() != 1) {
1603         emit displayMessage(i18n("Cannot delete marker if more than one clip is selected"), ErrorMessage);
1604         kDebug() << "// CANNOT DELETE MARKER IF MORE TAN ONE CLIP IS SELECTED....";
1605         return;
1606     }
1607     AbstractClipItem *item = (AbstractClipItem *)itemList.at(0);
1608     if (item->type() != AVWIDGET) {
1609         emit displayMessage(i18n("No clip selected"), ErrorMessage);
1610         return;
1611     }
1612     GenTime pos = GenTime(m_cursorPos, m_document->fps());
1613     if (item->startPos() > pos || item->endPos() < pos) {
1614         emit displayMessage(i18n("No selected clip at cursor time"), ErrorMessage);
1615         return;
1616     }
1617     ClipItem *clip = (ClipItem *) item;
1618     int id = clip->baseClip()->getId();
1619     GenTime position = pos - item->startPos() + item->cropStart();
1620     QString comment = clip->baseClip()->markerComment(position);
1621     if (comment.isEmpty()) {
1622         emit displayMessage(i18n("No marker found at cursor time"), ErrorMessage);
1623         return;
1624     }
1625     AddMarkerCommand *command = new AddMarkerCommand(this, comment, QString(), id, position, true);
1626     m_commandStack->push(command);
1627 }
1628
1629 void CustomTrackView::slotDeleteAllClipMarkers() {
1630     QList<QGraphicsItem *> itemList = scene()->selectedItems();
1631     if (itemList.count() != 1) {
1632         emit displayMessage(i18n("Cannot delete marker if more than one clip is selected"), ErrorMessage);
1633         kDebug() << "// CANNOT DELETE MARKER IF MORE TAN ONE CLIP IS SELECTED....";
1634         return;
1635     }
1636     AbstractClipItem *item = (AbstractClipItem *)itemList.at(0);
1637     if (item->type() != AVWIDGET) {
1638         emit displayMessage(i18n("No clip selected"), ErrorMessage);
1639         return;
1640     }
1641
1642     ClipItem *clip = static_cast <ClipItem *>(item);
1643     QList <CommentedTime> markers = clip->baseClip()->commentedSnapMarkers();
1644
1645     if (markers.isEmpty()) {
1646         emit displayMessage(i18n("Clip has no markers"), ErrorMessage);
1647         return;
1648     }
1649     int id = clip->baseClip()->getId();
1650     QUndoCommand *deleteMarkers = new QUndoCommand();
1651     deleteMarkers->setText("Delete clip markers");
1652
1653     for (int i = 0; i < markers.size(); i++) {
1654         new AddMarkerCommand(this, markers.at(i).comment(), QString(), id, markers.at(i).time(), true, deleteMarkers);
1655     }
1656     m_commandStack->push(deleteMarkers);
1657 }
1658
1659 void CustomTrackView::slotEditClipMarker() {
1660     QList<QGraphicsItem *> itemList = scene()->selectedItems();
1661     if (itemList.count() != 1) {
1662         emit displayMessage(i18n("Cannot edit marker if more than one clip is selected"), ErrorMessage);
1663         kDebug() << "// CANNOT DELETE MARKER IF MORE TAN ONE CLIP IS SELECTED....";
1664         return;
1665     }
1666     AbstractClipItem *item = (AbstractClipItem *)itemList.at(0);
1667     if (item->type() != AVWIDGET) {
1668         emit displayMessage(i18n("No clip at cursor time"), ErrorMessage);
1669         return;
1670     }
1671     GenTime pos = GenTime(m_cursorPos, m_document->fps());
1672     if (item->startPos() > pos || item->endPos() < pos) {
1673         emit displayMessage(i18n("No selected clip at cursor time"), ErrorMessage);
1674         return;
1675     }
1676     ClipItem *clip = (ClipItem *) item;
1677     int id = clip->baseClip()->getId();
1678     GenTime position = pos - item->startPos() + item->cropStart();
1679     QString oldcomment = clip->baseClip()->markerComment(position);
1680     if (oldcomment.isEmpty()) {
1681         emit displayMessage(i18n("No marker found at cursor time"), ErrorMessage);
1682         return;
1683     }
1684
1685     CommentedTime marker(position, oldcomment);
1686     MarkerDialog d(clip->baseClip(), marker, m_document->timecode(), this);
1687     if (d.exec() == QDialog::Accepted) {
1688         if (d.newMarker().time() == position) {
1689             // marker position was not changed, only text
1690             AddMarkerCommand *command = new AddMarkerCommand(this, oldcomment, d.newMarker().comment(), id, position, true);
1691             m_commandStack->push(command);
1692         } else {
1693             // marker text and position were changed, remove previous marker and add new one
1694             AddMarkerCommand *command1 = new AddMarkerCommand(this, oldcomment, QString(), id, position, true);
1695             AddMarkerCommand *command2 = new AddMarkerCommand(this, QString(), d.newMarker().comment(), id, d.newMarker().time(), true);
1696             m_commandStack->push(command1);
1697             m_commandStack->push(command2);
1698         }
1699     }
1700 }
1701
1702 void CustomTrackView::addMarker(const int id, const GenTime &pos, const QString comment) {
1703     DocClipBase *base = m_document->clipManager()->getClipById(id);
1704     if (!comment.isEmpty()) base->addSnapMarker(pos, comment);
1705     else base->deleteSnapMarker(pos);
1706     m_document->setModified(true);
1707     viewport()->update();
1708 }
1709
1710
1711
1712 void CustomTrackView::editGuide(const GenTime oldPos, const GenTime pos, const QString &comment) {
1713     if (oldPos > GenTime() && pos > GenTime()) {
1714         // move guide
1715         for (int i = 0; i < m_guides.count(); i++) {
1716             if (m_guides.at(i)->position() == oldPos) {
1717                 Guide *item = m_guides.at(i);
1718                 item->updateGuide(pos, comment);
1719                 break;
1720             }
1721         }
1722     } else if (pos > GenTime()) addGuide(pos, comment);
1723     else {
1724         // remove guide
1725         bool found = false;
1726         for (int i = 0; i < m_guides.count(); i++) {
1727             if (m_guides.at(i)->position() == oldPos) {
1728                 Guide *item = m_guides.takeAt(i);
1729                 delete item;
1730                 found = true;
1731                 break;
1732             }
1733         }
1734         if (!found) emit displayMessage(i18n("No guide at cursor time"), ErrorMessage);
1735     }
1736     m_document->syncGuides(m_guides);
1737 }
1738
1739 bool CustomTrackView::addGuide(const GenTime pos, const QString &comment) {
1740     for (int i = 0; i < m_guides.count(); i++) {
1741         if (m_guides.at(i)->position() == pos) {
1742             emit displayMessage(i18n("A guide already exists at that position"), ErrorMessage);
1743             return false;
1744         }
1745     }
1746     Guide *g = new Guide(this, pos, comment, m_scale, m_document->fps(), m_tracksHeight * m_tracksList.count());
1747     scene()->addItem(g);
1748     m_guides.append(g);
1749     m_document->syncGuides(m_guides);
1750     return true;
1751 }
1752
1753 void CustomTrackView::slotAddGuide() {
1754     CommentedTime marker(GenTime(m_cursorPos, m_document->fps()), i18n("Guide"));
1755     MarkerDialog d(NULL, marker, m_document->timecode(), this);
1756     if (d.exec() != QDialog::Accepted) return;
1757     if (addGuide(d.newMarker().time(), d.newMarker().comment())) {
1758         EditGuideCommand *command = new EditGuideCommand(this, GenTime(), QString(), d.newMarker().time(), d.newMarker().comment(), false);
1759         m_commandStack->push(command);
1760     }
1761 }
1762
1763 void CustomTrackView::slotEditGuide() {
1764     GenTime pos = GenTime(m_cursorPos, m_document->fps());
1765     bool found = false;
1766     for (int i = 0; i < m_guides.count(); i++) {
1767         if (m_guides.at(i)->position() == pos) {
1768             slotEditGuide(m_guides.at(i)->info());
1769             found = true;
1770             break;
1771         }
1772     }
1773     if (!found) emit displayMessage(i18n("No guide at cursor time"), ErrorMessage);
1774 }
1775
1776 void CustomTrackView::slotEditGuide(CommentedTime guide) {
1777     MarkerDialog d(NULL, guide, m_document->timecode(), this);
1778     if (d.exec() == QDialog::Accepted) {
1779         EditGuideCommand *command = new EditGuideCommand(this, guide.time(), guide.comment(), d.newMarker().time(), d.newMarker().comment(), true);
1780         m_commandStack->push(command);
1781     }
1782 }
1783
1784
1785 void CustomTrackView::slotDeleteGuide() {
1786     GenTime pos = GenTime(m_cursorPos, m_document->fps());
1787     bool found = false;
1788     for (int i = 0; i < m_guides.count(); i++) {
1789         if (m_guides.at(i)->position() == pos) {
1790             EditGuideCommand *command = new EditGuideCommand(this, m_guides.at(i)->position(), m_guides.at(i)->label(), GenTime(), QString(), true);
1791             m_commandStack->push(command);
1792             found = true;
1793             break;
1794         }
1795     }
1796     if (!found) emit displayMessage(i18n("No guide at cursor time"), ErrorMessage);
1797 }
1798
1799 void CustomTrackView::slotDeleteAllGuides() {
1800     QUndoCommand *deleteAll = new QUndoCommand();
1801     deleteAll->setText("Delete all guides");
1802     for (int i = 0; i < m_guides.count(); i++) {
1803         EditGuideCommand *command = new EditGuideCommand(this, m_guides.at(i)->position(), m_guides.at(i)->label(), GenTime(), QString(), true, deleteAll);
1804     }
1805     m_commandStack->push(deleteAll);
1806 }
1807
1808 void CustomTrackView::setTool(PROJECTTOOL tool) {
1809     m_tool = tool;
1810 }
1811
1812 void CustomTrackView::setScale(double scaleFactor) {
1813     //scale(scaleFactor, scaleFactor);
1814     m_animationTimer->stop();
1815     if (m_visualTip) {
1816         delete m_visualTip;
1817         m_visualTip = NULL;
1818     }
1819     if (m_animation) {
1820         delete m_animation;
1821         m_animation = NULL;
1822     }
1823     double pos = cursorPos() / m_scale;
1824     m_scale = scaleFactor;
1825     int vert = verticalScrollBar()->value();
1826     kDebug() << " HHHHHHHH  SCALING: " << m_scale;
1827     QList<QGraphicsItem *> itemList = items();
1828     for (int i = 0; i < itemList.count(); i++) {
1829         if (itemList.at(i)->type() == AVWIDGET || itemList.at(i)->type() == TRANSITIONWIDGET) {
1830             AbstractClipItem *clip = (AbstractClipItem *)itemList.at(i);
1831             clip->setRect(clip->startPos().frames(m_document->fps()) * m_scale, clip->rect().y(), clip->duration().frames(m_document->fps()) * m_scale, clip->rect().height());
1832         }
1833     }
1834
1835     for (int i = 0; i < m_guides.count(); i++) {
1836         m_guides.at(i)->updatePosition(m_scale);
1837     }
1838
1839     setSceneRect(0, 0, (m_projectDuration + 100) * m_scale, sceneRect().height());
1840     updateCursorPos();
1841     centerOn(QPointF(cursorPos(), m_tracksHeight));
1842     verticalScrollBar()->setValue(vert);
1843 }
1844
1845 void CustomTrackView::slotRefreshGuides() {
1846     if (KdenliveSettings::showmarkers()) {
1847         kDebug() << "// refresh GUIDES";
1848         for (int i = 0; i < m_guides.count(); i++) {
1849             m_guides.at(i)->update();
1850         }
1851     }
1852 }
1853
1854 void CustomTrackView::drawBackground(QPainter * painter, const QRectF & rect) {
1855     QRect rectInView = viewport()->rect();
1856     rectInView.moveTo(horizontalScrollBar()->value(), verticalScrollBar()->value());
1857
1858     QColor base = palette().button().color();
1859     painter->setClipRect(rect);
1860     painter->drawLine(rectInView.left(), 0, rectInView.right(), 0);
1861     uint max = m_tracksList.count();
1862     for (uint i = 0; i < max;i++) {
1863         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)));
1864         painter->drawLine(rectInView.left(), m_tracksHeight * (i + 1), rectInView.right(), m_tracksHeight * (i + 1));
1865         //painter->drawText(QRectF(10, 50 * i, 100, 50 * i + 49), Qt::AlignLeft, i18n(" Track ") + QString::number(i + 1));
1866     }
1867     int lowerLimit = m_tracksHeight * m_tracksList.count() + 1;
1868     if (height() > lowerLimit)
1869         painter->fillRect(QRectF(rectInView.left(), lowerLimit, rectInView.width(), height() - lowerLimit), QBrush(base));
1870 }
1871
1872 bool CustomTrackView::findString(const QString &text) {
1873     QString marker;
1874     for (int i = 0; i < m_searchPoints.size(); ++i) {
1875         marker = m_searchPoints.at(i).comment();
1876         if (marker.contains(text, Qt::CaseInsensitive)) {
1877             setCursorPos(m_searchPoints.at(i).time().frames(m_document->fps()), true);
1878             int vert = verticalScrollBar()->value();
1879             int hor = cursorPos();
1880             ensureVisible(hor, vert + 10, 2, 2, 50, 0);
1881             m_findIndex = i;
1882             return true;
1883         }
1884     }
1885     return false;
1886 }
1887
1888 bool CustomTrackView::findNextString(const QString &text) {
1889     QString marker;
1890     for (int i = m_findIndex + 1; i < m_searchPoints.size(); ++i) {
1891         marker = m_searchPoints.at(i).comment();
1892         if (marker.contains(text, Qt::CaseInsensitive)) {
1893             setCursorPos(m_searchPoints.at(i).time().frames(m_document->fps()), true);
1894             int vert = verticalScrollBar()->value();
1895             int hor = cursorPos();
1896             ensureVisible(hor, vert + 10, 2, 2, 50, 0);
1897             m_findIndex = i;
1898             return true;
1899         }
1900     }
1901     m_findIndex = -1;
1902     return false;
1903 }
1904
1905 void CustomTrackView::initSearchStrings() {
1906     m_searchPoints.clear();
1907     QList<QGraphicsItem *> itemList = items();
1908     for (int i = 0; i < itemList.count(); i++) {
1909         // parse all clip names
1910         if (itemList.at(i)->type() == AVWIDGET) {
1911             ClipItem *item = static_cast <ClipItem *>(itemList.at(i));
1912             GenTime start = item->startPos();
1913             CommentedTime t(start, item->clipName());
1914             m_searchPoints.append(t);
1915             // add all clip markers
1916             QList < CommentedTime > markers = item->commentedSnapMarkers();
1917             m_searchPoints += markers;
1918         }
1919     }
1920
1921     // add guides
1922     for (int i = 0; i < m_guides.count(); i++) {
1923         m_searchPoints.append(m_guides.at(i)->info());
1924     }
1925
1926     qSort(m_searchPoints);
1927 }
1928
1929 void CustomTrackView::clearSearchStrings() {
1930     m_searchPoints.clear();
1931     m_findIndex = 0;
1932 }
1933
1934 void CustomTrackView::copyClip() {
1935     while (m_copiedItems.count() > 0) {
1936         delete m_copiedItems.takeFirst();
1937     }
1938     QList<QGraphicsItem *> itemList = scene()->selectedItems();
1939     if (itemList.count() == 0) {
1940         emit displayMessage(i18n("Select a clip before copying"), ErrorMessage);
1941         return;
1942     }
1943     for (int i = 0; i < itemList.count(); i++) {
1944         if (itemList.at(i)->type() == AVWIDGET) {
1945             ClipItem *dup = static_cast <ClipItem *>(itemList.at(i));
1946             m_copiedItems.append(dup->clone(m_scale));
1947         } else if (itemList.at(i)->type() == TRANSITIONWIDGET) {
1948             Transition *dup = static_cast <Transition *>(itemList.at(i));
1949             m_copiedItems.append(dup->clone(m_scale));
1950         }
1951     }
1952 }
1953
1954 bool CustomTrackView::canBePastedTo(ItemInfo info, int type) const {
1955     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));
1956     QList<QGraphicsItem *> collisions = scene()->items(rect, Qt::IntersectsItemBoundingRect);
1957     for (int i = 0; i < collisions.count(); i++) {
1958         if (collisions.at(i)->type() == type) return false;
1959     }
1960     return true;
1961 }
1962
1963 bool CustomTrackView::canBePasted(QList<AbstractClipItem *> items, GenTime offset, int trackOffset) const {
1964     for (int i = 0; i < items.count(); i++) {
1965         ItemInfo info = items.at(i)->info();
1966         info.startPos += offset;
1967         info.endPos += offset;
1968         info.track += trackOffset;
1969         if (!canBePastedTo(info, items.at(i)->type())) return false;
1970     }
1971     return true;
1972 }
1973
1974 bool CustomTrackView::canBeMoved(QList<AbstractClipItem *> items, GenTime offset, int trackOffset) const {
1975     QPainterPath movePath;
1976     movePath.moveTo(0, 0);
1977
1978     for (int i = 0; i < items.count(); i++) {
1979         ItemInfo info = items.at(i)->info();
1980         info.startPos = info.startPos + offset;
1981         info.endPos = info.endPos + offset;
1982         info.track = info.track + trackOffset;
1983         if (info.startPos < GenTime()) {
1984             // No clip should go below 0
1985             return false;
1986         }
1987         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));
1988         movePath.addRect(rect);
1989     }
1990     QList<QGraphicsItem *> collisions = scene()->items(movePath, Qt::IntersectsItemBoundingRect);
1991     for (int i = 0; i < collisions.count(); i++) {
1992         if ((collisions.at(i)->type() == AVWIDGET || collisions.at(i)->type() == TRANSITIONWIDGET) && !items.contains(static_cast <AbstractClipItem *>(collisions.at(i)))) {
1993             kDebug() << "  ////////////   CLIP COLLISION, MOVE NOT ALLOWED";
1994             return false;
1995         }
1996     }
1997     return true;
1998 }
1999
2000 void CustomTrackView::pasteClip() {
2001     if (m_copiedItems.count() == 0) {
2002         emit displayMessage(i18n("No clip copied"), ErrorMessage);
2003         return;
2004     }
2005     QPoint position;
2006     if (m_menuPosition.isNull()) position = mapFromGlobal(QCursor::pos());
2007     else position = m_menuPosition;
2008     GenTime pos = GenTime((int)(mapToScene(position).x() / m_scale), m_document->fps());
2009     int track = (int)(position.y() / m_tracksHeight);
2010     ItemInfo first = m_copiedItems.at(0)->info();
2011
2012     GenTime offset = pos - first.startPos;
2013     int trackOffset = track - first.track;
2014
2015     if (!canBePasted(m_copiedItems, offset, trackOffset)) {
2016         emit displayMessage(i18n("Cannot paste selected clips"), ErrorMessage);
2017         return;
2018     }
2019     QUndoCommand *pasteClips = new QUndoCommand();
2020     pasteClips->setText("Paste clips");
2021
2022     for (int i = 0; i < m_copiedItems.count(); i++) {
2023         // parse all clip names
2024         if (m_copiedItems.at(i) && m_copiedItems.at(i)->type() == AVWIDGET) {
2025             ClipItem *clip = static_cast <ClipItem *>(m_copiedItems.at(i));
2026             ItemInfo info;
2027             info.startPos = clip->startPos() + offset;
2028             info.endPos = clip->endPos() + offset;
2029             info.cropStart = clip->cropStart();
2030             info.track = clip->track() + trackOffset;
2031             if (canBePastedTo(info, AVWIDGET)) {
2032                 new AddTimelineClipCommand(this, clip->xml(), clip->clipProducer(), info, true, false, pasteClips);
2033             } else emit displayMessage(i18n("Cannot paste clip to selected place"), ErrorMessage);
2034         } else if (m_copiedItems.at(i) && m_copiedItems.at(i)->type() == TRANSITIONWIDGET) {
2035             Transition *tr = static_cast <Transition *>(m_copiedItems.at(i));
2036             ItemInfo info;
2037             info.startPos = tr->startPos() + offset;
2038             info.endPos = tr->endPos() + offset;
2039             info.track = tr->track() + trackOffset;
2040             if (canBePastedTo(info, TRANSITIONWIDGET)) {
2041                 new AddTransitionCommand(this, info, tr->transitionEndTrack() + trackOffset, tr->toXML(), false, true, pasteClips);
2042             } else emit displayMessage(i18n("Cannot paste transition to selected place"), ErrorMessage);
2043         }
2044     }
2045     m_commandStack->push(pasteClips);
2046 }
2047
2048 /*
2049 void CustomTrackView::drawForeground ( QPainter * painter, const QRectF & rect )
2050 {
2051   //kDebug()<<"/////  DRAWING FB: "<<rect.x()<<", width: "<<rect.width();
2052   painter->fillRect(rect, QColor(50, rand() % 250,50,100));
2053   painter->drawLine(m_cursorPos, rect.y(), m_cursorPos, rect.y() + rect.height());
2054 }
2055 */
2056 #include "customtrackview.moc"