]> git.sesse.net Git - kdenlive/blob - src/customtrackview.cpp
* Guides improvements and fixes
[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) {
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 (offset != 0) offset = m_tracksHeight * offset;
193                 m_dragItem->moveTo((int)(snappedPos / m_scale), m_scale, offset, moveTrack);
194             } else if (m_operationMode == RESIZESTART) {
195                 double snappedPos = getSnapPointForPos(mapToScene(event->pos()).x());
196                 m_dragItem->resizeStart((int)(snappedPos / m_scale), m_scale);
197             } else if (m_operationMode == RESIZEEND) {
198                 double snappedPos = getSnapPointForPos(mapToScene(event->pos()).x());
199                 m_dragItem->resizeEnd((int)(snappedPos / m_scale), m_scale);
200             } else if (m_operationMode == FADEIN) {
201                 int pos = (int)(mapToScene(event->pos()).x() / m_scale);
202                 ((ClipItem*) m_dragItem)->setFadeIn((int)(pos - m_dragItem->startPos().frames(m_document->fps())), m_scale);
203             } else if (m_operationMode == FADEOUT) {
204                 int pos = (int)(mapToScene(event->pos()).x() / m_scale);
205                 ((ClipItem*) m_dragItem)->setFadeOut((int)(m_dragItem->endPos().frames(m_document->fps()) - pos), m_scale);
206             } else if (m_operationMode == KEYFRAME) {
207                 GenTime keyFramePos = GenTime((int)(mapToScene(event->pos()).x() / m_scale), m_document->fps()) - m_dragItem->startPos() + m_dragItem->cropStart();
208                 double pos = mapToScene(event->pos()).toPoint().y();
209                 QRectF br = m_dragItem->rect();
210                 double maxh = 100.0 / br.height();
211                 pos = (br.bottom() - pos) * maxh;
212                 m_dragItem->updateKeyFramePos(keyFramePos, pos);
213             }
214
215             if (m_animation) delete m_animation;
216             m_animation = NULL;
217             if (m_visualTip) delete m_visualTip;
218             m_visualTip = NULL;
219             QGraphicsView::mouseMoveEvent(event);
220             return;
221         } else if (m_operationMode == MOVEGUIDE) {
222             if (m_animation) delete m_animation;
223             m_animation = NULL;
224             if (m_visualTip) delete m_visualTip;
225             m_visualTip = NULL;
226             QGraphicsView::mouseMoveEvent(event);
227             return;
228         }
229     }
230
231     if (m_tool == RAZORTOOL) {
232         setCursor(m_razorCursor);
233         //QGraphicsView::mouseMoveEvent(event);
234         //return;
235     }
236
237     QList<QGraphicsItem *> itemList = items(event->pos());
238     QGraphicsRectItem *item = NULL;
239     OPERATIONTYPE opMode = NONE;
240
241     if (itemList.count() == 1 && itemList.at(0)->type() == GUIDEITEM) {
242         opMode = MOVEGUIDE;
243     } else for (int i = 0; i < itemList.count(); i++) {
244             if (itemList.at(i)->type() == AVWIDGET || itemList.at(i)->type() == TRANSITIONWIDGET) {
245                 item = (QGraphicsRectItem*) itemList.at(i);
246                 break;
247             }
248         }
249
250     if (item && event->buttons() == Qt::NoButton) {
251         AbstractClipItem *clip = (AbstractClipItem*) item;
252         if (m_tool == RAZORTOOL) {
253             // razor tool over a clip, display current frame in monitor
254             if (item->type() == AVWIDGET) {
255                 emit showClipFrame(((ClipItem *) item)->baseClip(), mapToScene(event->pos()).x() / m_scale - (clip->startPos() - clip->cropStart()).frames(m_document->fps()));
256             }
257             QGraphicsView::mouseMoveEvent(event);
258             return;
259         }
260         opMode = clip->operationMode(mapToScene(event->pos()), m_scale);
261         double size = 8;
262         if (opMode == m_moveOpMode) {
263             QGraphicsView::mouseMoveEvent(event);
264             return;
265         } else {
266             if (m_visualTip) {
267                 if (m_animation) delete m_animation;
268                 m_animation = NULL;
269                 m_animationTimer->stop();
270                 delete m_visualTip;
271                 m_visualTip = NULL;
272             }
273         }
274         m_moveOpMode = opMode;
275         if (opMode == MOVE) {
276             setCursor(Qt::OpenHandCursor);
277         } else if (opMode == RESIZESTART) {
278             setCursor(KCursor("left_side", Qt::SizeHorCursor));
279             if (m_visualTip == NULL) {
280                 QPolygon polygon;
281                 polygon << QPoint((int)clip->rect().x(), (int)(clip->rect().y() + clip->rect().height() / 2 - size * 2));
282                 polygon << QPoint((int)(clip->rect().x() + size * 2), (int)(clip->rect().y() + clip->rect().height() / 2));
283                 polygon << QPoint((int)clip->rect().x(), (int)(clip->rect().y() + clip->rect().height() / 2 + size * 2));
284                 polygon << QPoint((int)clip->rect().x(), (int)(clip->rect().y() + clip->rect().height() / 2 - size * 2));
285
286                 m_visualTip = new QGraphicsPolygonItem(polygon);
287                 ((QGraphicsPolygonItem*) m_visualTip)->setBrush(m_tipColor);
288                 ((QGraphicsPolygonItem*) m_visualTip)->setPen(m_tipPen);
289                 m_visualTip->setZValue(100);
290                 m_animation = new QGraphicsItemAnimation;
291                 m_animation->setItem(m_visualTip);
292                 m_animation->setTimeLine(m_animationTimer);
293                 m_visualTip->setPos(0, 0);
294                 double scale = 2.0;
295                 m_animation->setScaleAt(.5, scale, 1);
296                 m_animation->setPosAt(.5, QPointF(clip->rect().x() - clip->rect().x() * scale, 0));
297                 scale = 1.0;
298                 m_animation->setScaleAt(1, scale, 1);
299                 m_animation->setPosAt(1, QPointF(clip->rect().x() - clip->rect().x() * scale, 0));
300                 scene()->addItem(m_visualTip);
301                 m_animationTimer->start();
302             }
303         } else if (opMode == RESIZEEND) {
304             setCursor(KCursor("right_side", Qt::SizeHorCursor));
305             if (m_visualTip == NULL) {
306                 QPolygon polygon;
307                 polygon << QPoint((int)(clip->rect().x() + clip->rect().width()), (int)(clip->rect().y() + clip->rect().height() / 2 - size * 2));
308                 polygon << QPoint((int)(clip->rect().x() + clip->rect().width() - size * 2), (int)(clip->rect().y() + clip->rect().height() / 2));
309                 polygon << QPoint((int)(clip->rect().x() + clip->rect().width()), (int)(clip->rect().y() + clip->rect().height() / 2 + size * 2));
310                 polygon << QPoint((int)(clip->rect().x() + clip->rect().width()), (int)(clip->rect().y() + clip->rect().height() / 2 - size * 2));
311
312                 m_visualTip = new QGraphicsPolygonItem(polygon);
313                 ((QGraphicsPolygonItem*) m_visualTip)->setBrush(m_tipColor);
314                 ((QGraphicsPolygonItem*) m_visualTip)->setPen(m_tipPen);
315
316                 m_visualTip->setZValue(100);
317                 m_animation = new QGraphicsItemAnimation;
318                 m_animation->setItem(m_visualTip);
319                 m_animation->setTimeLine(m_animationTimer);
320                 m_visualTip->setPos(0, 0);
321                 double scale = 2.0;
322                 m_animation->setScaleAt(.5, scale, 1);
323                 m_animation->setPosAt(.5, QPointF(clip->rect().x() - clip->rect().x() * scale - clip->rect().width(), 0));
324                 scale = 1.0;
325                 m_animation->setScaleAt(1, scale, 1);
326                 m_animation->setPosAt(1, QPointF(clip->rect().x() - clip->rect().x() * scale, 0));
327                 scene()->addItem(m_visualTip);
328                 m_animationTimer->start();
329             }
330         } else if (opMode == FADEIN) {
331             if (m_visualTip == NULL) {
332                 ClipItem *item = (ClipItem *) clip;
333                 m_visualTip = new QGraphicsEllipseItem(item->rect().x() + item->fadeIn() * m_scale - size, item->rect().y() - 8, size * 2, 16);
334                 ((QGraphicsEllipseItem*) m_visualTip)->setBrush(m_tipColor);
335                 ((QGraphicsEllipseItem*) m_visualTip)->setPen(m_tipPen);
336                 m_visualTip->setZValue(100);
337                 m_animation = new QGraphicsItemAnimation;
338                 m_animation->setItem(m_visualTip);
339                 m_animation->setTimeLine(m_animationTimer);
340                 m_visualTip->setPos(0, 0);
341                 double scale = 2.0;
342                 m_animation->setScaleAt(.5, scale, scale);
343                 m_animation->setPosAt(.5, QPointF(item->rect().x() - item->rect().x() * scale -  item->fadeIn() * m_scale, item->rect().y() - item->rect().y() * scale));
344                 scale = 1.0;
345                 m_animation->setScaleAt(1, scale, scale);
346                 m_animation->setPosAt(1, QPointF(item->rect().x() - item->rect().x() * scale, item->rect().y() - item->rect().y() * scale));
347                 scene()->addItem(m_visualTip);
348                 m_animationTimer->start();
349             }
350             setCursor(Qt::PointingHandCursor);
351         } else if (opMode == FADEOUT) {
352             if (m_visualTip == NULL) {
353                 ClipItem *item = (ClipItem *) clip;
354                 m_visualTip = new QGraphicsEllipseItem(item->rect().x() + item->rect().width() - item->fadeOut() * m_scale - size, item->rect().y() - 8, size*2, 16);
355                 ((QGraphicsEllipseItem*) m_visualTip)->setBrush(m_tipColor);
356                 ((QGraphicsEllipseItem*) m_visualTip)->setPen(m_tipPen);
357                 m_visualTip->setZValue(100);
358                 m_animation = new QGraphicsItemAnimation;
359                 m_animation->setItem(m_visualTip);
360                 m_animation->setTimeLine(m_animationTimer);
361                 m_visualTip->setPos(0, 0);
362                 double scale = 2.0;
363                 m_animation->setScaleAt(.5, scale, scale);
364                 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));
365                 scale = 1.0;
366                 m_animation->setScaleAt(1, scale, scale);
367                 m_animation->setPosAt(1, QPointF(item->rect().x() - item->rect().x() * scale, item->rect().y() - item->rect().y() * scale));
368                 scene()->addItem(m_visualTip);
369                 m_animationTimer->start();
370             }
371             setCursor(Qt::PointingHandCursor);
372         } else if (opMode == TRANSITIONSTART) {
373             if (m_visualTip == NULL) {
374                 m_visualTip = new QGraphicsEllipseItem(-5, -5 , 10, 10);
375                 ((QGraphicsEllipseItem*) m_visualTip)->setBrush(m_tipColor);
376                 ((QGraphicsEllipseItem*) m_visualTip)->setPen(m_tipPen);
377                 m_visualTip->setZValue(100);
378                 m_animation = new QGraphicsItemAnimation;
379                 m_animation->setItem(m_visualTip);
380                 m_animation->setTimeLine(m_animationTimer);
381                 m_visualTip->setPos(clip->rect().x() + 10, clip->rect().y() + clip->rect().height() / 2 + 12);
382                 double scale = 2.0;
383                 m_animation->setScaleAt(.5, scale, scale);
384                 scale = 1.0;
385                 m_animation->setScaleAt(1, scale, scale);
386                 scene()->addItem(m_visualTip);
387                 m_animationTimer->start();
388             }
389             setCursor(Qt::PointingHandCursor);
390         } else if (opMode == TRANSITIONEND) {
391             if (m_visualTip == NULL) {
392                 m_visualTip = new QGraphicsEllipseItem(-5, -5 , 10, 10);
393                 ((QGraphicsEllipseItem*) m_visualTip)->setBrush(m_tipColor);
394                 ((QGraphicsEllipseItem*) m_visualTip)->setPen(m_tipPen);
395                 m_visualTip->setZValue(100);
396                 m_animation = new QGraphicsItemAnimation;
397                 m_animation->setItem(m_visualTip);
398                 m_animation->setTimeLine(m_animationTimer);
399                 m_visualTip->setPos(clip->rect().x() + clip->rect().width() - 10 , clip->rect().y() + clip->rect().height() / 2 + 12);
400                 double scale = 2.0;
401                 m_animation->setScaleAt(.5, scale, scale);
402                 scale = 1.0;
403                 m_animation->setScaleAt(1, scale, scale);
404                 scene()->addItem(m_visualTip);
405                 m_animationTimer->start();
406             }
407             setCursor(Qt::PointingHandCursor);
408         } else if (opMode == KEYFRAME) {
409             setCursor(Qt::PointingHandCursor);
410         }
411     } // no clip under mouse
412     else if (m_tool == RAZORTOOL) {
413         QGraphicsView::mouseMoveEvent(event);
414         return;
415     } else if (opMode == MOVEGUIDE) {
416         m_moveOpMode = opMode;
417         setCursor(Qt::SplitHCursor);
418     } else {
419         m_moveOpMode = NONE;
420         if (event->buttons() != Qt::NoButton && event->modifiers() == Qt::NoModifier) {
421             setCursorPos((int)(mapToScene(event->pos().x(), 0).x() / m_scale));
422         }
423         if (m_visualTip) {
424             if (m_animation) delete m_animation;
425             m_animationTimer->stop();
426             m_animation = NULL;
427             delete m_visualTip;
428             m_visualTip = NULL;
429
430         }
431         setCursor(Qt::ArrowCursor);
432     }
433     QGraphicsView::mouseMoveEvent(event);
434 }
435
436 // virtual
437 void CustomTrackView::mousePressEvent(QMouseEvent * event) {
438     activateMonitor();
439     m_clickEvent = event->pos();
440     if (event->button() == Qt::MidButton) {
441         m_document->renderer()->switchPlay();
442         return;
443     }
444     if (event->modifiers() == Qt::ControlModifier) {
445         setDragMode(QGraphicsView::ScrollHandDrag);
446         QGraphicsView::mousePressEvent(event);
447         return;
448     } else if (event->modifiers() == Qt::ShiftModifier) {
449         setDragMode(QGraphicsView::RubberBandDrag);
450         QGraphicsView::mousePressEvent(event);
451         return;
452     } else {
453         bool collision = false;
454         QList<QGraphicsItem *> collisionList = items(event->pos());
455         if (collisionList.count() == 1 && collisionList.at(0)->type() == GUIDEITEM) {
456             // a guide item was pressed
457             collisionList.at(0)->setFlag(QGraphicsItem::ItemIsMovable, true);
458             m_dragItem = NULL;
459             m_dragGuide = (Guide *) collisionList.at(0);
460             collision = true;
461             m_operationMode = MOVEGUIDE;
462             // deselect all clips so that only the guide will move
463             QList<QGraphicsItem *> clips = scene()->selectedItems();
464             for (int i = 0; i < clips.count(); ++i)
465                 clips.at(i)->setSelected(false);
466             updateSnapPoints(NULL);
467             QGraphicsView::mousePressEvent(event);
468         } else for (int i = 0; i < collisionList.count(); ++i) {
469                 QGraphicsItem *item = collisionList.at(i);
470                 if (item->type() == AVWIDGET || item->type() == TRANSITIONWIDGET) {
471                     if (m_tool == RAZORTOOL) {
472                         if (item->type() == TRANSITIONWIDGET) {
473                             emit displayMessage(i18n("Cannot cut a transition"), ErrorMessage);
474                             return;
475                         }
476                         AbstractClipItem *clip = (AbstractClipItem *) item;
477                         ItemInfo info;
478                         info.startPos = clip->startPos();
479                         info.endPos = clip->endPos();
480                         info.track = clip->track();
481                         RazorClipCommand* command = new RazorClipCommand(this, info, GenTime((int)(mapToScene(event->pos()).x() / m_scale), m_document->fps()), true);
482                         m_commandStack->push(command);
483                         m_document->setModified(true);
484                         return;
485                     }
486                     // select item
487                     if (!item->isSelected()) {
488                         QList<QGraphicsItem *> itemList = items();
489                         for (int i = 0; i < itemList.count(); i++) {
490                             itemList.at(i)->setSelected(false);
491                             itemList.at(i)->update();
492                         }
493                         item->setSelected(true);
494                         item->update();
495                     }
496
497                     m_dragItem = (AbstractClipItem *) item;
498
499                     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()));
500                     m_dragItemInfo.startPos = m_dragItem->startPos();
501                     m_dragItemInfo.endPos = m_dragItem->endPos();
502                     m_dragItemInfo.track = m_dragItem->track();
503
504                     m_operationMode = m_dragItem->operationMode(item->mapFromScene(mapToScene(event->pos())), m_scale);
505                     if (m_operationMode == KEYFRAME) {
506                         m_dragItem->updateSelectedKeyFrame();
507                         return;
508                     } else if (m_operationMode == MOVE) setCursor(Qt::ClosedHandCursor);
509                     else if (m_operationMode == TRANSITIONSTART) {
510                         ItemInfo info;
511                         info.startPos = m_dragItem->startPos();
512                         info.track = m_dragItem->track();
513                         int transitiontrack = getPreviousVideoTrack(info.track);
514                         ClipItem *transitionClip = NULL;
515                         if (transitiontrack != 0) transitionClip = getClipItemAt((int) info.startPos.frames(m_document->fps()), m_tracksList.count() - transitiontrack);
516                         if (transitionClip && transitionClip->endPos() < m_dragItem->endPos()) {
517                             info.endPos = transitionClip->endPos();
518                         } else info.endPos = info.startPos + GenTime(2.5);
519
520                         slotAddTransition((ClipItem *) m_dragItem, info, transitiontrack);
521                     } else if (m_operationMode == TRANSITIONEND) {
522                         ItemInfo info;
523                         info.endPos = m_dragItem->endPos();
524                         info.track = m_dragItem->track();
525                         int transitiontrack = getPreviousVideoTrack(info.track);
526                         ClipItem *transitionClip = NULL;
527                         if (transitiontrack != 0) transitionClip = getClipItemAt((int) info.endPos.frames(m_document->fps()), m_tracksList.count() - transitiontrack);
528                         if (transitionClip && transitionClip->startPos() > m_dragItem->startPos()) {
529                             info.startPos = transitionClip->startPos();
530                         } else info.startPos = info.endPos - GenTime(2.5);
531                         slotAddTransition((ClipItem *) m_dragItem, info, transitiontrack);
532                     }
533                     updateSnapPoints(m_dragItem);
534                     collision = true;
535                     break;
536                 }
537             }
538         if (!collision) {
539             kDebug() << "//////// NO ITEM FOUND ON CLICK";
540             m_dragItem = NULL;
541             setCursor(Qt::ArrowCursor);
542             QList<QGraphicsItem *> itemList = items();
543             for (int i = 0; i < itemList.count(); i++)
544                 itemList.at(i)->setSelected(false);
545             //emit clipItemSelected(NULL);
546             if (event->button() == Qt::RightButton) {
547                 displayContextMenu(event->globalPos());
548             } else setCursorPos((int)(mapToScene(event->x(), 0).x() / m_scale));
549         } else if (event->button() == Qt::RightButton) {
550             m_operationMode = NONE;
551             displayContextMenu(event->globalPos(), m_dragItem);
552             m_dragItem = NULL;
553         }
554         if (m_dragItem && m_dragItem->type() == AVWIDGET) emit clipItemSelected((ClipItem*) m_dragItem);
555         else emit clipItemSelected(NULL);
556     }
557     //kDebug()<<pos;
558     //QGraphicsView::mousePressEvent(event);
559 }
560
561 void CustomTrackView::mouseDoubleClickEvent(QMouseEvent *event) {
562     if (m_dragItem && m_dragItem->hasKeyFrames()) {
563         if (m_moveOpMode == KEYFRAME) {
564             // user double clicked on a keyframe, open edit dialog
565             QDialog d(parentWidget());
566             Ui::KeyFrameDialog_UI view;
567             view.setupUi(&d);
568             view.kfr_position->setText(m_document->timecode().getTimecode(GenTime(m_dragItem->selectedKeyFramePos(), m_document->fps()) - m_dragItem->cropStart(), m_document->fps()));
569             view.kfr_value->setValue(m_dragItem->selectedKeyFrameValue());
570             view.kfr_value->setFocus();
571             if (d.exec() == QDialog::Accepted) {
572                 int pos = m_document->timecode().getFrameCount(view.kfr_position->text(), m_document->fps());
573                 m_dragItem->updateKeyFramePos(GenTime(pos, m_document->fps()) + m_dragItem->cropStart(), (double) view.kfr_value->value() * m_dragItem->keyFrameFactor());
574                 ClipItem *item = (ClipItem *)m_dragItem;
575                 QString previous = item->keyframes(item->selectedEffectIndex());
576                 item->updateKeyframeEffect();
577                 QString next = item->keyframes(item->selectedEffectIndex());
578                 EditKeyFrameCommand *command = new EditKeyFrameCommand(this, item->track(), item->startPos(), item->selectedEffectIndex(), previous, next, false);
579                 m_commandStack->push(command);
580                 updateEffect(m_tracksList.count() - item->track(), item->startPos(), item->selectedEffect(), item->selectedEffectIndex());
581             }
582
583         } else  {
584             // add keyframe
585             GenTime keyFramePos = GenTime((int)(mapToScene(event->pos()).x() / m_scale), m_document->fps()) - m_dragItem->startPos() + m_dragItem->cropStart();
586             m_dragItem->addKeyFrame(keyFramePos, mapToScene(event->pos()).toPoint().y());
587             ClipItem * item = (ClipItem *) m_dragItem;
588             QString previous = item->keyframes(item->selectedEffectIndex());
589             item->updateKeyframeEffect();
590             QString next = item->keyframes(item->selectedEffectIndex());
591             EditKeyFrameCommand *command = new EditKeyFrameCommand(this, m_dragItem->track(), m_dragItem->startPos(), item->selectedEffectIndex(), previous, next, false);
592             m_commandStack->push(command);
593             updateEffect(m_tracksList.count() - item->track(), item->startPos(), item->selectedEffect(), item->selectedEffectIndex());
594         }
595     } else if (m_dragItem) {
596         ClipDurationDialog d(m_dragItem, m_document->timecode(), this);
597         if (d.exec() == QDialog::Accepted) {
598             if (d.startPos() != m_dragItem->startPos()) {
599                 if (m_dragItem->type() == AVWIDGET) {
600                     ItemInfo startInfo;
601                     startInfo.startPos = m_dragItem->startPos();
602                     startInfo.endPos = m_dragItem->endPos();
603                     startInfo.track = m_dragItem->track();
604                     ItemInfo endInfo;
605                     endInfo.startPos = d.startPos();
606                     endInfo.endPos = m_dragItem->endPos() + (endInfo.startPos - startInfo.startPos);
607                     endInfo.track = m_dragItem->track();
608                     MoveClipCommand *command = new MoveClipCommand(this, startInfo, endInfo, true);
609                     m_commandStack->push(command);
610                 } else {
611                     //TODO: move transition
612                 }
613             }
614             if (d.duration() != m_dragItem->duration()) {
615                 if (m_dragItem->type() == AVWIDGET) {
616                     ItemInfo startInfo;
617                     startInfo.startPos = m_dragItem->startPos();
618                     startInfo.endPos = m_dragItem->endPos();
619                     startInfo.track = m_dragItem->track();
620                     ItemInfo endInfo;
621                     endInfo.startPos = startInfo.startPos;
622                     endInfo.endPos = endInfo.startPos + d.duration();
623                     endInfo.track = m_dragItem->track();
624                     ResizeClipCommand *command = new ResizeClipCommand(this, startInfo, endInfo, true);
625                     m_commandStack->push(command);
626                 } else {
627                     //TODO: resize transition
628                 }
629             }
630         }
631     } else {
632         QList<QGraphicsItem *> collisionList = items(event->pos());
633         if (collisionList.count() == 1 && collisionList.at(0)->type() == GUIDEITEM) {
634             Guide *editGuide = (Guide *) collisionList.at(0);
635             if (editGuide) slotEditGuide(editGuide->info());
636         }
637     }
638 }
639
640
641 void CustomTrackView::editKeyFrame(const GenTime pos, const int track, const int index, const QString keyframes) {
642     ClipItem *clip = getClipItemAt((int)pos.frames(m_document->fps()), track);
643     if (clip) {
644         clip->setKeyframes(index, keyframes);
645         updateEffect(m_tracksList.count() - clip->track(), clip->startPos(), clip->effectAt(index), index);
646     } else emit displayMessage(i18n("Cannot find clip with keyframe"), ErrorMessage);
647 }
648
649
650 void CustomTrackView::displayContextMenu(QPoint pos, AbstractClipItem *clip) {
651     if (clip == NULL) m_timelineContextMenu->popup(pos);
652     else if (clip->type() == AVWIDGET) m_timelineContextClipMenu->popup(pos);
653     else if (clip->type() == TRANSITIONWIDGET) m_timelineContextTransitionMenu->popup(pos);
654 }
655
656 void CustomTrackView::activateMonitor() {
657     emit activateDocumentMonitor();
658 }
659
660 void CustomTrackView::dragEnterEvent(QDragEnterEvent * event) {
661     if (event->mimeData()->hasFormat("kdenlive/producerslist")) {
662         kDebug() << "///////////////  DRAG ENTERED, TEXT: " << event->mimeData()->data("kdenlive/producerslist");
663         QStringList ids = QString(event->mimeData()->data("kdenlive/producerslist")).split(";");
664         //TODO: drop of several clips
665         for (int i = 0; i < ids.size(); ++i) {
666         }
667         DocClipBase *clip = m_document->getBaseClip(ids.at(0).toInt());
668         if (clip == NULL) kDebug() << " WARNING))))))))) CLIP NOT FOUND : " << ids.at(0).toInt();
669         addItem(clip, event->pos());
670         event->acceptProposedAction();
671     } else QGraphicsView::dragEnterEvent(event);
672 }
673
674 void CustomTrackView::slotRefreshEffects(ClipItem *clip) {
675     int track = m_tracksList.count() - clip->track();
676     GenTime pos = clip->startPos();
677     if (!m_document->renderer()->mltRemoveEffect(track, pos, "-1", false)) {
678         emit displayMessage(i18n("Problem deleting effect"), ErrorMessage);
679         return;
680     }
681     bool success = true;
682     for (int i = 0; i < clip->effectsCount(); i++) {
683         if (!m_document->renderer()->mltAddEffect(track, pos, clip->getEffectArgs(clip->effectAt(i)), false)) success = false;
684     }
685     if (!success) emit displayMessage(i18n("Problem adding effect to clip"), ErrorMessage);
686     m_document->renderer()->doRefresh();
687 }
688
689 void CustomTrackView::addEffect(int track, GenTime pos, QDomElement effect) {
690     ClipItem *clip = getClipItemAt((int)pos.frames(m_document->fps()) + 1, m_tracksList.count() - track);
691     if (clip) {
692         QMap <QString, QString> effectParams = clip->addEffect(effect);
693         if (!m_document->renderer()->mltAddEffect(track, pos, effectParams))
694             emit displayMessage(i18n("Problem adding effect to clip"), ErrorMessage);
695         emit clipItemSelected(clip);
696     } else emit displayMessage(i18n("Cannot find clip to add effect"), ErrorMessage);
697 }
698
699 void CustomTrackView::deleteEffect(int track, GenTime pos, QDomElement effect) {
700     QString index = effect.attribute("kdenlive_ix");
701     if (!m_document->renderer()->mltRemoveEffect(track, pos, index)) {
702         emit displayMessage(i18n("Problem deleting effect"), ErrorMessage);
703         return;
704     }
705     ClipItem *clip = getClipItemAt((int)pos.frames(m_document->fps()) + 1, m_tracksList.count() - track);
706     if (clip) {
707         clip->deleteEffect(index);
708         emit clipItemSelected(clip);
709     }
710 }
711
712 void CustomTrackView::slotAddEffect(QDomElement effect, GenTime pos, int track) {
713     QList<QGraphicsItem *> itemList;
714     if (track == -1) itemList = scene()->selectedItems();
715     if (itemList.isEmpty()) {
716         ClipItem *clip = getClipItemAt((int)pos.frames(m_document->fps()) + 1, track);
717         if (clip) itemList.append(clip);
718         else emit displayMessage(i18n("Select a clip if you want to apply an effect"), ErrorMessage);
719     }
720     kDebug() << "// REQUESTING EFFECT ON CLIP: " << pos.frames(25) << ", TRK: " << track << "SELECTED ITEMS: " << itemList.count();
721     for (int i = 0; i < itemList.count(); i++) {
722         if (itemList.at(i)->type() == AVWIDGET) {
723             ClipItem *item = (ClipItem *)itemList.at(i);
724             item->initEffect(effect);
725             AddEffectCommand *command = new AddEffectCommand(this, m_tracksList.count() - item->track(), item->startPos(), effect, true);
726             m_commandStack->push(command);
727         }
728     }
729     m_document->setModified(true);
730 }
731
732 void CustomTrackView::slotDeleteEffect(ClipItem *clip, QDomElement effect) {
733     AddEffectCommand *command = new AddEffectCommand(this, m_tracksList.count() - clip->track(), clip->startPos(), effect, false);
734     m_commandStack->push(command);
735     m_document->setModified(true);
736 }
737
738 void CustomTrackView::updateEffect(int track, GenTime pos, QDomElement effect, int ix) {
739     ClipItem *clip = getClipItemAt((int)pos.frames(m_document->fps()) + 1, m_tracksList.count() - track);
740     if (clip) {
741         QMap <QString, QString> effectParams = clip->getEffectArgs(effect);
742         if (effectParams.value("disabled") == "1") {
743             if (m_document->renderer()->mltRemoveEffect(track, pos, effectParams.value("kdenlive_ix"))) {
744                 kDebug() << "//////  DISABLING EFFECT: " << index << ", CURRENTLA: " << clip->selectedEffectIndex();
745             } else emit displayMessage(i18n("Problem deleting effect"), ErrorMessage);
746         } else if (!m_document->renderer()->mltEditEffect(m_tracksList.count() - clip->track(), clip->startPos(), effectParams))
747             emit displayMessage(i18n("Problem editing effect"), ErrorMessage);
748         if (ix == clip->selectedEffectIndex()) {
749             clip->setSelectedEffect(clip->selectedEffectIndex());
750         }
751     }
752     m_document->setModified(true);
753 }
754
755 void CustomTrackView::moveEffect(int track, GenTime pos, int oldPos, int newPos) {
756     ClipItem *clip = getClipItemAt((int)pos.frames(m_document->fps()) + 1, m_tracksList.count() - track);
757     if (clip) {
758         m_document->renderer()->mltMoveEffect(track, pos, oldPos, newPos);
759     }
760     m_document->setModified(true);
761 }
762
763 void CustomTrackView::slotChangeEffectState(ClipItem *clip, int effectPos, bool disable) {
764     QDomElement effect = clip->effectAt(effectPos);
765     QDomElement oldEffect = effect.cloneNode().toElement();
766     effect.setAttribute("disabled", disable);
767     EditEffectCommand *command = new EditEffectCommand(this, m_tracksList.count() - clip->track(), clip->startPos(), oldEffect, effect, effectPos, true);
768     m_commandStack->push(command);
769     m_document->setModified(true);
770 }
771
772 void CustomTrackView::slotChangeEffectPosition(ClipItem *clip, int currentPos, int newPos) {
773     MoveEffectCommand *command = new MoveEffectCommand(this, m_tracksList.count() - clip->track(), clip->startPos(), currentPos, newPos, true);
774     m_commandStack->push(command);
775     m_document->setModified(true);
776 }
777
778 void CustomTrackView::slotUpdateClipEffect(ClipItem *clip, QDomElement oldeffect, QDomElement effect, int ix) {
779     EditEffectCommand *command = new EditEffectCommand(this, m_tracksList.count() - clip->track(), clip->startPos(), oldeffect, effect, ix, true);
780     m_commandStack->push(command);
781 }
782
783 void CustomTrackView::cutClip(ItemInfo info, GenTime cutTime, bool cut) {
784     if (cut) {
785         // cut clip
786         ClipItem *item = getClipItemAt((int) info.startPos.frames(m_document->fps()), info.track);
787         if (!item) {
788             emit displayMessage(i18n("Cannot find clip to cut"), ErrorMessage);
789             return;
790         }
791         int cutPos = (int) cutTime.frames(m_document->fps());
792         ItemInfo newPos;
793         newPos.startPos = cutTime;
794         newPos.endPos = info.endPos;
795         newPos.track = info.track;
796         item->resizeEnd(cutPos, m_scale);
797         ClipItem *dup = new ClipItem(item->baseClip(), newPos, item->cropStart() + (cutTime - info.startPos), m_scale, m_document->fps());
798         scene()->addItem(dup);
799         m_document->renderer()->mltCutClip(m_tracksList.count() - info.track, cutTime);
800         item->baseClip()->addReference();
801         m_document->updateClip(item->baseClip()->getId());
802     } else {
803         // uncut clip
804         ClipItem *item = getClipItemAt((int) info.startPos.frames(m_document->fps()), info.track);
805         ClipItem *dup = getClipItemAt((int) cutTime.frames(m_document->fps()), info.track);
806         if (!item || !dup) {
807             emit displayMessage(i18n("Cannot find clip to uncut"), ErrorMessage);
808             return;
809         }
810         delete dup;
811         item->baseClip()->removeReference();
812         m_document->updateClip(item->baseClip()->getId());
813         item->resizeEnd((int) info.endPos.frames(m_document->fps()), m_scale);
814         m_document->renderer()->mltRemoveClip(m_tracksList.count() - info.track, cutTime);
815         m_document->renderer()->mltResizeClipEnd(m_tracksList.count() - info.track, info.startPos, item->cropStart(), item->cropStart() + info.endPos - info.startPos);
816     }
817 }
818
819
820 void CustomTrackView::slotAddTransitionToSelectedClips(QDomElement transition) {
821     QList<QGraphicsItem *> itemList = scene()->selectedItems();
822     for (int i = 0; i < itemList.count(); i++) {
823         if (itemList.at(i)->type() == AVWIDGET) {
824             ClipItem *item = (ClipItem *) itemList.at(i);
825             ItemInfo info;
826             info.startPos = item->startPos();
827             info.endPos = info.startPos + GenTime(2.5);
828             info.track = item->track();
829             int transitiontrack = getPreviousVideoTrack(info.track);
830             slotAddTransition(item, info, transitiontrack, transition);
831         }
832     }
833 }
834
835 void CustomTrackView::slotAddTransition(ClipItem* clip, ItemInfo transitionInfo, int endTrack, QDomElement transition) {
836     AddTransitionCommand* command = new AddTransitionCommand(this, transitionInfo, endTrack, transition, false, true);
837     m_commandStack->push(command);
838     m_document->setModified(true);
839 }
840
841 void CustomTrackView::addTransition(ItemInfo transitionInfo, int endTrack, QDomElement params) {
842     Transition *tr = new Transition(transitionInfo, endTrack, m_scale, m_document->fps(), params);
843     scene()->addItem(tr);
844
845     //kDebug() << "---- ADDING transition " << e.attribute("tag") << ", on tracks " << m_tracksList.count() - e.attribute("transition_track").toInt() << " / " << getPreviousVideoTrack(e.attribute("transition_track").toInt());
846     m_document->renderer()->mltAddTransition(tr->transitionTag(), endTrack, m_tracksList.count() - transitionInfo.track, transitionInfo.startPos, transitionInfo.endPos, tr->toXML());
847     m_document->setModified(true);
848 }
849
850 void CustomTrackView::deleteTransition(ItemInfo transitionInfo, int endTrack, QDomElement params) {
851     Transition *item = getTransitionItemAt((int)transitionInfo.startPos.frames(m_document->fps()) + 1, transitionInfo.track);
852     m_document->renderer()->mltDeleteTransition(item->transitionTag(), endTrack, m_tracksList.count() - transitionInfo.track, transitionInfo.startPos, transitionInfo.endPos, item->toXML());
853     delete item;
854     emit transitionItemSelected(NULL);
855     m_document->setModified(true);
856 }
857
858 void CustomTrackView::slotTransitionUpdated(Transition *tr, QDomElement old) {
859     EditTransitionCommand *command = new EditTransitionCommand(this, tr->track(), tr->startPos(), old, tr->toXML() , true);
860     m_commandStack->push(command);
861     m_document->setModified(true);
862 }
863
864 void CustomTrackView::updateTransition(int track, GenTime pos, QDomElement oldTransition, QDomElement transition) {
865     Transition *item = getTransitionItemAt((int)pos.frames(m_document->fps()) + 1, track);
866     if (!item) {
867         kWarning() << "Unable to find transition at pos :" << pos.frames(m_document->fps()) << ", ON track: " << track;
868         return;
869     }
870     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);
871     m_document->setModified(true);
872 }
873
874 void CustomTrackView::addItem(DocClipBase *clip, QPoint pos) {
875     ItemInfo info;
876     info.startPos = GenTime((int)(mapToScene(pos).x() / m_scale), m_document->fps());
877     info.endPos = info.startPos + clip->duration();
878     info.track = (int)(pos.y() / m_tracksHeight);
879     //kDebug()<<"------------  ADDING CLIP ITEM----: "<<info.startPos.frames(25)<<", "<<info.endPos.frames(25)<<", "<<info.track;
880     m_dropItem = new ClipItem(clip, info, GenTime(), m_scale, m_document->fps());
881     scene()->addItem(m_dropItem);
882 }
883
884
885 void CustomTrackView::dragMoveEvent(QDragMoveEvent * event) {
886     event->setDropAction(Qt::IgnoreAction);
887     if (m_dropItem) {
888         int track = (int)(mapToScene(event->pos()).y() / m_tracksHeight);  //) * (m_scale * 50) + m_scale;
889         m_dropItem->moveTo((int)(mapToScene(event->pos()).x() / m_scale), m_scale, (int)((track - m_dropItem->track()) * m_tracksHeight), track);
890         event->setDropAction(Qt::MoveAction);
891         if (event->mimeData()->hasFormat("kdenlive/producerslist")) {
892             event->acceptProposedAction();
893         }
894     } else {
895         QGraphicsView::dragMoveEvent(event);
896     }
897 }
898
899 void CustomTrackView::dragLeaveEvent(QDragLeaveEvent * event) {
900     if (m_dropItem) {
901         delete m_dropItem;
902         m_dropItem = NULL;
903     } else QGraphicsView::dragLeaveEvent(event);
904 }
905
906 void CustomTrackView::dropEvent(QDropEvent * event) {
907     if (m_dropItem) {
908         ItemInfo info;
909         info.startPos = m_dropItem->startPos();
910         info.endPos = m_dropItem->endPos();
911         info.track = m_dropItem->track();
912         AddTimelineClipCommand *command = new AddTimelineClipCommand(this, m_dropItem->xml(), m_dropItem->clipProducer(), info, false, false);
913         m_commandStack->push(command);
914         m_dropItem->baseClip()->addReference();
915         m_document->updateClip(m_dropItem->baseClip()->getId());
916         // kDebug()<<"IIIIIIIIIIIIIIIIIIIIIIII TRAX CNT: "<<m_tracksList.count()<<", DROP: "<<m_dropItem->track();
917         m_document->renderer()->mltInsertClip(m_tracksList.count() - m_dropItem->track(), m_dropItem->startPos(), m_dropItem->xml());
918         m_document->setModified(true);
919     } else QGraphicsView::dropEvent(event);
920     m_dropItem = NULL;
921 }
922
923
924 QStringList CustomTrackView::mimeTypes() const {
925     QStringList qstrList;
926     // list of accepted mime types for drop
927     qstrList.append("text/plain");
928     qstrList.append("kdenlive/producerslist");
929     return qstrList;
930 }
931
932 Qt::DropActions CustomTrackView::supportedDropActions() const {
933     // returns what actions are supported when dropping
934     return Qt::MoveAction;
935 }
936
937 void CustomTrackView::setDuration(int duration) {
938     kDebug() << "/////////////  PRO DUR: " << duration << ", SCALE. " << (m_projectDuration + 500) * m_scale << ", height: " << 50 * m_tracksList.count();
939     m_projectDuration = duration;
940     setSceneRect(0, 0, (m_projectDuration + 100) * m_scale, sceneRect().height());
941 }
942
943 int CustomTrackView::duration() const {
944     return m_projectDuration;
945 }
946
947 void CustomTrackView::addTrack(TrackInfo type) {
948     m_tracksList << type;
949     m_cursorLine->setLine(m_cursorLine->line().x1(), 0, m_cursorLine->line().x1(), m_tracksHeight * m_tracksList.count());
950     setSceneRect(0, 0, sceneRect().width(), m_tracksHeight * m_tracksList.count());
951     verticalScrollBar()->setMaximum(m_tracksHeight * m_tracksList.count());
952     //setFixedHeight(50 * m_tracksCount);
953 }
954
955 void CustomTrackView::removeTrack() {
956     // TODO: implement track deletion
957     //m_tracksCount--;
958     m_cursorLine->setLine(m_cursorLine->line().x1(), 0, m_cursorLine->line().x1(), m_tracksHeight * m_tracksList.count());
959 }
960
961
962 void CustomTrackView::slotSwitchTrackAudio(int ix) {
963     int tracknumber = m_tracksList.count() - ix;
964     kDebug() << "/////  MUTING TRK: " << ix << "; PL NUM: " << tracknumber;
965     m_tracksList[tracknumber - 1].isMute = !m_tracksList.at(tracknumber - 1).isMute;
966     m_document->renderer()->mltChangeTrackState(tracknumber, m_tracksList.at(tracknumber - 1).isMute, m_tracksList.at(tracknumber - 1).isBlind);
967 }
968
969 void CustomTrackView::slotSwitchTrackVideo(int ix) {
970     int tracknumber = m_tracksList.count() - ix;
971     m_tracksList[tracknumber - 1].isBlind = !m_tracksList.at(tracknumber - 1).isBlind;
972     m_document->renderer()->mltChangeTrackState(tracknumber, m_tracksList.at(tracknumber - 1).isMute, m_tracksList.at(tracknumber - 1).isBlind);
973 }
974
975 void CustomTrackView::deleteClip(int clipId) {
976     QList<QGraphicsItem *> itemList = items();
977     for (int i = 0; i < itemList.count(); i++) {
978         if (itemList.at(i)->type() == AVWIDGET) {
979             ClipItem *item = (ClipItem *)itemList.at(i);
980             if (item->clipProducer() == clipId) {
981                 ItemInfo info;
982                 info.startPos = item->startPos();
983                 info.endPos = item->endPos();
984                 info.track = item->track();
985                 AddTimelineClipCommand *command = new AddTimelineClipCommand(this, item->xml(), item->clipProducer(), info, true, true);
986                 m_commandStack->push(command);
987                 //delete item;
988             }
989         }
990     }
991 }
992
993 void CustomTrackView::setCursorPos(int pos, bool seek) {
994     emit cursorMoved((int)(m_cursorPos * m_scale), (int)(pos * m_scale));
995     m_cursorPos = pos;
996     m_cursorLine->setPos(pos * m_scale, 0);
997     if (seek) m_document->renderer()->seek(GenTime(pos, m_document->fps()));
998     else if (m_autoScroll && m_scale < 50) checkScrolling();
999 }
1000
1001 void CustomTrackView::updateCursorPos() {
1002     m_cursorLine->setPos(m_cursorPos * m_scale, 0);
1003 }
1004
1005 int CustomTrackView::cursorPos() {
1006     return (int)(m_cursorPos * m_scale);
1007 }
1008
1009 void CustomTrackView::moveCursorPos(int delta) {
1010     if (m_cursorPos + delta < 0) delta = 0 - m_cursorPos;
1011     emit cursorMoved((int)(m_cursorPos * m_scale), (int)((m_cursorPos + delta) * m_scale));
1012     m_cursorPos += delta;
1013     m_cursorLine->setPos(m_cursorPos * m_scale, 0);
1014     m_document->renderer()->seek(GenTime(m_cursorPos, m_document->fps()));
1015     //if (m_autoScroll && m_scale < 50) checkScrolling();
1016 }
1017
1018 void CustomTrackView::checkScrolling() {
1019     int vert = verticalScrollBar()->value();
1020     int hor = cursorPos();
1021     ensureVisible(hor, vert + 10, 2, 2, 50, 0);
1022     //centerOn(QPointF(cursorPos(), m_tracksHeight));
1023     /*QRect rectInView = viewport()->rect();
1024     int delta = rectInView.width() / 3;
1025     int max = rectInView.right() + horizontalScrollBar()->value() - delta;
1026     //kDebug() << "CURSOR POS: "<<m_cursorPos<< "Scale: "<<m_scale;
1027     if (m_cursorPos * m_scale >= max) horizontalScrollBar()->setValue((int)(horizontalScrollBar()->value() + 1 + m_scale));*/
1028 }
1029
1030 void CustomTrackView::mouseReleaseEvent(QMouseEvent * event) {
1031     if (event->button() == Qt::MidButton) {
1032         return;
1033     }
1034     QGraphicsView::mouseReleaseEvent(event);
1035     setDragMode(QGraphicsView::NoDrag);
1036     if (m_operationMode == MOVEGUIDE) {
1037         setCursor(Qt::ArrowCursor);
1038         m_operationMode = NONE;
1039         m_dragGuide->setFlag(QGraphicsItem::ItemIsMovable, false);
1040         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);
1041         m_commandStack->push(command);
1042         m_dragGuide->updateGuide(GenTime(m_dragGuide->pos().x() / m_scale, m_document->fps()));
1043         m_dragGuide = NULL;
1044         m_dragItem = NULL;
1045         return;
1046     }
1047     if (m_dragItem == NULL) {
1048         emit transitionItemSelected(NULL);
1049         return;
1050     }
1051     ItemInfo info;
1052     info.startPos = m_dragItem->startPos();
1053     info.endPos = m_dragItem->endPos();
1054     info.track = m_dragItem->track();
1055
1056     if (m_operationMode == MOVE) {
1057         setCursor(Qt::OpenHandCursor);
1058         // move clip
1059         if (m_dragItem->type() == AVWIDGET && (m_dragItemInfo.startPos != info.startPos || m_dragItemInfo.track != info.track)) {
1060             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())));
1061             if (success) {
1062                 MoveClipCommand *command = new MoveClipCommand(this, m_dragItemInfo, info, false);
1063                 m_commandStack->push(command);
1064             } else {
1065                 // undo last move and emit error message
1066                 MoveClipCommand *command = new MoveClipCommand(this, info, m_dragItemInfo, true);
1067                 m_commandStack->push(command);
1068                 emit displayMessage(i18n("Cannot move clip to position %1seconds", QString::number(m_dragItemInfo.startPos.seconds(), 'g', 2)), ErrorMessage);
1069             }
1070         }
1071         if (m_dragItem->type() == TRANSITIONWIDGET && (m_dragItemInfo.startPos != info.startPos || m_dragItemInfo.track != info.track)) {
1072             MoveTransitionCommand *command = new MoveTransitionCommand(this, m_dragItemInfo, info, false);
1073             m_commandStack->push(command);
1074             Transition *transition = (Transition *) m_dragItem;
1075             transition->updateTransitionEndTrack(getPreviousVideoTrack(m_dragItem->track()));
1076             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);
1077         }
1078
1079     } else if (m_operationMode == RESIZESTART && m_dragItem->startPos() != m_dragItemInfo.startPos) {
1080         // resize start
1081         if (m_dragItem->type() == AVWIDGET) {
1082             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());
1083             updateClipFade((ClipItem *) m_dragItem);
1084             ResizeClipCommand *command = new ResizeClipCommand(this, m_dragItemInfo, info, false);
1085             m_commandStack->push(command);
1086         } else if (m_dragItem->type() == TRANSITIONWIDGET) {
1087             MoveTransitionCommand *command = new MoveTransitionCommand(this, m_dragItemInfo, info, false);
1088             m_commandStack->push(command);
1089             Transition *transition = (Transition *) m_dragItem;
1090             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);
1091         }
1092
1093         //m_document->renderer()->doRefresh();
1094     } else if (m_operationMode == RESIZEEND && m_dragItem->endPos() != m_dragItemInfo.endPos) {
1095         // resize end
1096         if (m_dragItem->type() == AVWIDGET) {
1097             ResizeClipCommand *command = new ResizeClipCommand(this, m_dragItemInfo, info, false);
1098             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());
1099             m_commandStack->push(command);
1100         } else if (m_dragItem->type() == TRANSITIONWIDGET) {
1101             MoveTransitionCommand *command = new MoveTransitionCommand(this, m_dragItemInfo, info, false);
1102             m_commandStack->push(command);
1103             Transition *transition = (Transition *) m_dragItem;
1104             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);
1105         }
1106         //m_document->renderer()->doRefresh();
1107     } else if (m_operationMode == FADEIN) {
1108         // resize fade in effect
1109         ClipItem * item = (ClipItem *) m_dragItem;
1110         QStringList clipeffects = item->effectNames();
1111         if (clipeffects.contains(i18n("Fade in"))) {
1112             int ix = clipeffects.indexOf(i18n("Fade in"));
1113             QDomElement oldeffect = item->effectAt(ix);
1114             int start = item->cropStart().frames(m_document->fps());
1115             int end = item->fadeIn();
1116             if (end == 0) {
1117                 slotDeleteEffect(item, oldeffect);
1118             } else {
1119                 end += start;
1120                 QDomElement effect = MainWindow::audioEffects.getEffectByName("Fade in");
1121                 EffectsList::setParameter(effect, "in", QString::number(start));
1122                 EffectsList::setParameter(effect, "out", QString::number(end));
1123                 slotUpdateClipEffect(item, oldeffect, effect, ix);
1124             }
1125         } else if (item->fadeIn() != 0) {
1126             QDomElement effect = MainWindow::audioEffects.getEffectByName("Fade in");
1127             int start = item->cropStart().frames(m_document->fps());
1128             int end = item->fadeIn() + start;
1129             EffectsList::setParameter(effect, "in", QString::number(start));
1130             EffectsList::setParameter(effect, "out", QString::number(end));
1131             slotAddEffect(effect, m_dragItem->startPos(), m_dragItem->track());
1132         }
1133     } else if (m_operationMode == FADEOUT) {
1134         // resize fade in effect
1135         ClipItem * item = (ClipItem *) m_dragItem;
1136         QStringList clipeffects = item->effectNames();
1137         if (clipeffects.contains(i18n("Fade out"))) {
1138             int ix = clipeffects.indexOf(i18n("Fade out"));
1139             QDomElement oldeffect = item->effectAt(ix);
1140             int end = (item->duration() + item->cropStart()).frames(m_document->fps());
1141             int start = item->fadeOut();
1142             if (start == 0) {
1143                 slotDeleteEffect(item, oldeffect);
1144             } else {
1145                 start = end - start;
1146                 QDomElement effect = MainWindow::audioEffects.getEffectByName("Fade out");
1147                 EffectsList::setParameter(effect, "in", QString::number(start));
1148                 EffectsList::setParameter(effect, "out", QString::number(end));
1149                 slotUpdateClipEffect(item, oldeffect, effect, ix);
1150             }
1151         } else if (item->fadeOut() != 0) {
1152             QDomElement effect = MainWindow::audioEffects.getEffectByName("Fade out");
1153             int end = (item->duration() + item->cropStart()).frames(m_document->fps());
1154             int start = end - item->fadeOut();
1155             EffectsList::setParameter(effect, "in", QString::number(start));
1156             EffectsList::setParameter(effect, "out", QString::number(end));
1157             slotAddEffect(effect, m_dragItem->startPos(), m_dragItem->track());
1158         }
1159     } else if (m_operationMode == KEYFRAME) {
1160         // update the MLT effect
1161         ClipItem * item = (ClipItem *) m_dragItem;
1162         QString previous = item->keyframes(item->selectedEffectIndex());
1163         item->updateKeyframeEffect();
1164         QString next = item->keyframes(item->selectedEffectIndex());
1165         EditKeyFrameCommand *command = new EditKeyFrameCommand(this, item->track(), item->startPos(), item->selectedEffectIndex(), previous, next, false);
1166         m_commandStack->push(command);
1167         updateEffect(m_tracksList.count() - item->track(), item->startPos(), item->selectedEffect(), item->selectedEffectIndex());
1168     }
1169
1170     emit transitionItemSelected((m_dragItem && m_dragItem->type() == TRANSITIONWIDGET) ? (Transition*) m_dragItem : NULL);
1171     m_document->setModified(true);
1172     m_operationMode = NONE;
1173 }
1174
1175 void CustomTrackView::deleteClip(ItemInfo info) {
1176     ClipItem *item = getClipItemAt((int) info.startPos.frames(m_document->fps()), info.track);
1177     if (!item) {
1178         kDebug() << "----------------  ERROR, CANNOT find clip to move at...";// << rect.x();
1179         return;
1180     }
1181     if (item->isSelected()) emit clipItemSelected(NULL);
1182     item->baseClip()->removeReference();
1183     m_document->updateClip(item->baseClip()->getId());
1184     delete item;
1185     m_document->renderer()->mltRemoveClip(m_tracksList.count() - info.track, info.startPos);
1186     m_document->renderer()->doRefresh();
1187 }
1188
1189 void CustomTrackView::deleteSelectedClips() {
1190     QList<QGraphicsItem *> itemList = scene()->selectedItems();
1191     for (int i = 0; i < itemList.count(); i++) {
1192         if (itemList.at(i)->type() == AVWIDGET) {
1193             ClipItem *item = (ClipItem *) itemList.at(i);
1194             ItemInfo info;
1195             info.startPos = item->startPos();
1196             info.endPos = item->endPos();
1197             info.track = item->track();
1198             AddTimelineClipCommand *command = new AddTimelineClipCommand(this, item->xml(), item->clipProducer(), info, true, true);
1199             m_commandStack->push(command);
1200         } else if (itemList.at(i)->type() == TRANSITIONWIDGET) {
1201             Transition *item = (Transition *) itemList.at(i);
1202             ItemInfo info;
1203             info.startPos = item->startPos();
1204             info.endPos = item->endPos();
1205             info.track = item->track();
1206             AddTransitionCommand *command = new AddTransitionCommand(this, info, item->transitionEndTrack(), QDomElement(), true, true);
1207             m_commandStack->push(command);
1208         }
1209     }
1210 }
1211
1212 void CustomTrackView::cutSelectedClips() {
1213     QList<QGraphicsItem *> itemList = scene()->selectedItems();
1214     GenTime currentPos = GenTime(m_cursorPos, m_document->fps());
1215     for (int i = 0; i < itemList.count(); i++) {
1216         if (itemList.at(i)->type() == AVWIDGET) {
1217             ClipItem *item = (ClipItem *) itemList.at(i);
1218             ItemInfo info;
1219             info.startPos = item->startPos();
1220             info.endPos = item->endPos();
1221             if (currentPos > info.startPos && currentPos <  info.endPos) {
1222                 info.track = item->track();
1223                 RazorClipCommand *command = new RazorClipCommand(this, info, currentPos, true);
1224                 m_commandStack->push(command);
1225             }
1226         }
1227     }
1228 }
1229
1230 void CustomTrackView::addClip(QDomElement xml, int clipId, ItemInfo info) {
1231     DocClipBase *baseclip = m_document->clipManager()->getClipById(clipId);
1232     int crop = xml.attribute("in").toInt();
1233     ClipItem *item = new ClipItem(baseclip, info, GenTime(crop, m_document->fps()), m_scale, m_document->fps());
1234     scene()->addItem(item);
1235     baseclip->addReference();
1236     m_document->updateClip(baseclip->getId());
1237     m_document->renderer()->mltInsertClip(m_tracksList.count() - info.track, info.startPos, xml);
1238     m_document->renderer()->doRefresh();
1239 }
1240
1241 void CustomTrackView::slotUpdateClip(int clipId) {
1242     QList<QGraphicsItem *> list = scene()->items();
1243     ClipItem *clip = NULL;
1244     for (int i = 0; i < list.size(); ++i) {
1245         if (list.at(i)->type() == AVWIDGET) {
1246             clip = static_cast <ClipItem *>(list.at(i));
1247             if (clip->clipProducer() == clipId) {
1248                 clip->refreshClip();
1249                 m_document->renderer()->mltUpdateClip(m_tracksList.count() - clip->track(), clip->startPos(), clip->xml());
1250             }
1251         }
1252     }
1253 }
1254
1255 ClipItem *CustomTrackView::getClipItemAt(int pos, int track) {
1256     QList<QGraphicsItem *> list = scene()->items(QPointF(pos * m_scale, track * m_tracksHeight + m_tracksHeight / 2));
1257     ClipItem *clip = NULL;
1258     for (int i = 0; i < list.size(); ++i) {
1259         if (list.at(i)->type() == AVWIDGET) {
1260             clip = static_cast <ClipItem *>(list.at(i));
1261             break;
1262         }
1263     }
1264     return clip;
1265 }
1266
1267 ClipItem *CustomTrackView::getClipItemAt(GenTime pos, int track) {
1268     int framepos = (int)(pos.frames(m_document->fps()) * m_scale);
1269     return getClipItemAt(framepos, track);
1270 }
1271
1272 Transition *CustomTrackView::getTransitionItemAt(int pos, int track) {
1273     QList<QGraphicsItem *> list = scene()->items(QPointF(pos * m_scale, (track + 1) * m_tracksHeight));
1274     Transition *clip = NULL;
1275     for (int i = 0; i < list.size(); ++i) {
1276         if (list.at(i)->type() == TRANSITIONWIDGET) {
1277             clip = static_cast <Transition *>(list.at(i));
1278             break;
1279         }
1280     }
1281     return clip;
1282 }
1283
1284 Transition *CustomTrackView::getTransitionItemAt(GenTime pos, int track) {
1285     int framepos = (int)(pos.frames(m_document->fps()) * m_scale);
1286     return getTransitionItemAt(framepos, track);
1287 }
1288
1289 void CustomTrackView::moveClip(const ItemInfo start, const ItemInfo end) {
1290     ClipItem *item = getClipItemAt((int) start.startPos.frames(m_document->fps()) + 1, start.track);
1291     if (!item) {
1292         emit displayMessage(i18n("Cannot move clip at time: %1s on track %2", QString::number(start.startPos.seconds(), 'g', 2), start.track), ErrorMessage);
1293         kDebug() << "----------------  ERROR, CANNOT find clip to move at.. ";// << startPos.x() * m_scale * FRAME_SIZE + 1 << ", " << startPos.y() * m_tracksHeight + m_tracksHeight / 2;
1294         return;
1295     }
1296     //kDebug() << "----------------  Move CLIP FROM: " << startPos.x() << ", END:" << endPos.x() << ",TRACKS: " << startPos.y() << " TO " << endPos.y();
1297
1298     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()));
1299     if (success) {
1300         item->moveTo((int) end.startPos.frames(m_document->fps()), m_scale, (int)((end.track - start.track) * m_tracksHeight), end.track);
1301     } else {
1302         // undo last move and emit error message
1303         emit displayMessage(i18n("Cannot move clip to position %1seconds", QString::number(end.startPos.seconds(), 'g', 2)), ErrorMessage);
1304     }
1305 }
1306
1307 void CustomTrackView::moveTransition(const ItemInfo start, const ItemInfo end) {
1308     Transition *item = getTransitionItemAt((int)start.startPos.frames(m_document->fps()) + 1, start.track);
1309     if (!item) {
1310         emit displayMessage(i18n("Cannot move transition at time: %1s on track %2", start.startPos.seconds(), start.track), ErrorMessage);
1311         kDebug() << "----------------  ERROR, CANNOT find transition to move... ";// << startPos.x() * m_scale * FRAME_SIZE + 1 << ", " << startPos.y() * m_tracksHeight + m_tracksHeight / 2;
1312         return;
1313     }
1314     //kDebug() << "----------------  Move TRANSITION FROM: " << startPos.x() << ", END:" << endPos.x() << ",TRACKS: " << oldtrack << " TO " << newtrack;
1315
1316     //kDebug()<<"///  RESIZE TRANS START: ("<< startPos.x()<<"x"<< startPos.y()<<") / ("<<endPos.x()<<"x"<< endPos.y()<<")";
1317     if (end.endPos - end.startPos == start.endPos - start.startPos) {
1318         // Transition was moved
1319         item->moveTo((int) end.startPos.frames(m_document->fps()), m_scale, (end.track - start.track) * m_tracksHeight, end.track);
1320     } else if (end.endPos == start.endPos) {
1321         // Transition start resize
1322         item->resizeStart((int) end.startPos.frames(m_document->fps()), m_scale);
1323     } else {
1324         // Transition end resize;
1325         item->resizeEnd((int) end.endPos.frames(m_document->fps()), m_scale);
1326     }
1327     //item->moveTransition(GenTime((int) (endPos.x() - startPos.x()), m_document->fps()));
1328     item->updateTransitionEndTrack(getPreviousVideoTrack(end.track));
1329     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);
1330 }
1331
1332 void CustomTrackView::resizeClip(const ItemInfo start, const ItemInfo end) {
1333     int offset = 0;
1334     bool resizeClipStart = true;
1335     if (start.startPos == end.startPos) resizeClipStart = false;
1336     /*if (resizeClipStart) offset = 1;
1337     else offset = -1;*/
1338     ClipItem *item = getClipItemAt((int)(start.startPos.frames(m_document->fps()) + offset), start.track);
1339     if (!item) {
1340         emit displayMessage(i18n("Cannot move clip at time: %1s on track %2", start.startPos.seconds(), start.track), ErrorMessage);
1341         kDebug() << "----------------  ERROR, CANNOT find clip to resize at... "; // << startPos;
1342         return;
1343     }
1344     if (resizeClipStart) {
1345         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);
1346         item->resizeStart((int) end.startPos.frames(m_document->fps()), m_scale);
1347         updateClipFade(item);
1348     } else {
1349         m_document->renderer()->mltResizeClipEnd(m_tracksList.count() - item->track(), item->startPos(), item->cropStart(), item->cropStart() + end.endPos - item->startPos());
1350         item->resizeEnd((int) end.endPos.frames(m_document->fps()), m_scale);
1351         updateClipFade(item, true);
1352     }
1353     m_document->renderer()->doRefresh();
1354 }
1355
1356 void CustomTrackView::updateClipFade(ClipItem * item, bool updateFadeOut) {
1357     if (!updateFadeOut) {
1358         int end = item->fadeIn();
1359         if (end != 0) {
1360             // there is a fade in effect
1361             QStringList clipeffects = item->effectNames();
1362             QDomElement oldeffect = item->effectAt(clipeffects.indexOf("Fade in"));
1363             int start = item->cropStart().frames(m_document->fps());
1364             end += start;
1365             EffectsList::setParameter(oldeffect, "in", QString::number(start));
1366             EffectsList::setParameter(oldeffect, "out", QString::number(end));
1367             QMap <QString, QString> effectParams = item->getEffectArgs(oldeffect);
1368             if (!m_document->renderer()->mltEditEffect(m_tracksList.count() - item->track(), item->startPos(), effectParams))
1369                 emit displayMessage(i18n("Problem editing effect"), ErrorMessage);
1370         }
1371     } else {
1372         int start = item->fadeOut();
1373         if (start != 0) {
1374             // there is a fade in effect
1375             QStringList clipeffects = item->effectNames();
1376             QDomElement oldeffect = item->effectAt(clipeffects.indexOf("Fade out"));
1377             int end = (item->duration() - item->cropStart()).frames(m_document->fps());
1378             start = end - start;
1379             EffectsList::setParameter(oldeffect, "in", QString::number(start));
1380             EffectsList::setParameter(oldeffect, "out", QString::number(end));
1381             QMap <QString, QString> effectParams = item->getEffectArgs(oldeffect);
1382             if (m_document->renderer()->mltEditEffect(m_tracksList.count() - item->track(), item->startPos(), effectParams))
1383                 emit displayMessage(i18n("Problem editing effect"), ErrorMessage);
1384         }
1385     }
1386 }
1387
1388 double CustomTrackView::getSnapPointForPos(double pos) {
1389     for (int i = 0; i < m_snapPoints.size(); ++i) {
1390         if (abs((int)(pos - m_snapPoints.at(i).frames(m_document->fps()) * m_scale)) < 10) {
1391             //kDebug()<<" FOUND SNAP POINT AT: "<<m_snapPoints.at(i)<<", current pos: "<<pos / m_scale;
1392             return m_snapPoints.at(i).frames(m_document->fps()) * m_scale + 0.5;
1393         }
1394         if (m_snapPoints.at(i).frames(m_document->fps() * m_scale) > pos) break;
1395     }
1396     return pos;
1397 }
1398
1399 void CustomTrackView::updateSnapPoints(AbstractClipItem *selected) {
1400     m_snapPoints.clear();
1401     if (!KdenliveSettings::snaptopoints()) return;
1402     GenTime offset;
1403     if (selected) offset = selected->duration();
1404     QList<QGraphicsItem *> itemList = items();
1405     for (int i = 0; i < itemList.count(); i++) {
1406         if (itemList.at(i)->type() == AVWIDGET && itemList.at(i) != selected) {
1407             ClipItem *item = static_cast <ClipItem *>(itemList.at(i));
1408             GenTime start = item->startPos();
1409             GenTime end = item->endPos();
1410             m_snapPoints.append(start);
1411             m_snapPoints.append(end);
1412             QList < GenTime > markers = item->snapMarkers();
1413             for (int i = 0; i < markers.size(); ++i) {
1414                 GenTime t = markers.at(i);
1415                 m_snapPoints.append(t);
1416                 if (t > offset) m_snapPoints.append(t - offset);
1417             }
1418             if (offset != GenTime()) {
1419                 if (start > offset) m_snapPoints.append(start - offset);
1420                 if (end > offset) m_snapPoints.append(end - offset);
1421             }
1422         } else if (itemList.at(i)->type() == TRANSITIONWIDGET) {
1423             Transition *transition = static_cast <Transition*>(itemList.at(i));
1424             GenTime start = transition->startPos();
1425             GenTime end = transition->endPos();
1426             m_snapPoints.append(start);
1427             m_snapPoints.append(end);
1428             if (offset != GenTime()) {
1429                 if (start > offset) m_snapPoints.append(start - offset);
1430                 if (end > offset) m_snapPoints.append(end - offset);
1431             }
1432         }
1433     }
1434
1435     // add cursor position
1436     GenTime pos = GenTime(m_cursorPos, m_document->fps());
1437     m_snapPoints.append(pos);
1438     if (offset != GenTime()) m_snapPoints.append(pos - offset);
1439
1440     // add guides
1441     for (int i = 0; i < m_guides.count(); i++) {
1442         m_snapPoints.append(m_guides.at(i)->position());
1443         if (offset != GenTime()) m_snapPoints.append(m_guides.at(i)->position() - offset);
1444     }
1445
1446     qSort(m_snapPoints);
1447     //for (int i = 0; i < m_snapPoints.size(); ++i)
1448     //    kDebug() << "SNAP POINT: " << m_snapPoints.at(i).frames(25);
1449 }
1450
1451 void CustomTrackView::slotSeekToPreviousSnap() {
1452     updateSnapPoints(NULL);
1453     GenTime pos = GenTime(m_cursorPos, m_document->fps());
1454     GenTime res = GenTime();
1455     for (int i = 0; i < m_snapPoints.size(); ++i) {
1456         if (m_snapPoints.at(i) >= pos) {
1457             if (i == 0) i = 1;
1458             res = m_snapPoints.at(i - 1);
1459             break;
1460         }
1461     }
1462     setCursorPos((int) res.frames(m_document->fps()));
1463 }
1464
1465 void CustomTrackView::slotSeekToNextSnap() {
1466     updateSnapPoints(NULL);
1467     GenTime pos = GenTime(m_cursorPos, m_document->fps());
1468     GenTime res = GenTime(m_projectDuration, m_document->fps());
1469     for (int i = 0; i < m_snapPoints.size(); ++i) {
1470         if (m_snapPoints.at(i) > pos) {
1471             res = m_snapPoints.at(i);
1472             break;
1473         }
1474     }
1475     setCursorPos((int) res.frames(m_document->fps()));
1476 }
1477
1478 void CustomTrackView::slotAddClipMarker() {
1479     QList<QGraphicsItem *> itemList = scene()->selectedItems();
1480     if (itemList.count() != 1) {
1481         emit displayMessage(i18n("Cannot add marker if more than one clip is selected"), ErrorMessage);
1482         kDebug() << "// CANNOT ADD MARKER IF MORE TAN ONE CLIP IS SELECTED....";
1483         return;
1484     }
1485     AbstractClipItem *item = (AbstractClipItem *)itemList.at(0);
1486     if (item->type() != AVWIDGET) return;
1487     GenTime pos = GenTime(m_cursorPos, m_document->fps());
1488     if (item->startPos() > pos || item->endPos() < pos) return;
1489     ClipItem *clip = (ClipItem *) item;
1490     int id = clip->baseClip()->getId();
1491     GenTime position = pos - item->startPos() + item->cropStart();
1492     CommentedTime marker(position, i18n("Marker"));
1493     MarkerDialog d(clip->baseClip(), marker, m_document->timecode(), this);
1494     if (d.exec() == QDialog::Accepted) {
1495         slotAddClipMarker(id, d.newMarker().time(), d.newMarker().comment());
1496     }
1497 }
1498
1499 void CustomTrackView::slotAddClipMarker(int id, GenTime t, QString c) {
1500     QString oldcomment = m_document->clipManager()->getClipById(id)->markerComment(t);
1501     AddMarkerCommand *command = new AddMarkerCommand(this, oldcomment, c, id, t, true);
1502     m_commandStack->push(command);
1503 }
1504
1505 void CustomTrackView::slotDeleteClipMarker() {
1506     QList<QGraphicsItem *> itemList = scene()->selectedItems();
1507     if (itemList.count() != 1) {
1508         emit displayMessage(i18n("Cannot delete marker if more than one clip is selected"), ErrorMessage);
1509         kDebug() << "// CANNOT DELETE MARKER IF MORE TAN ONE CLIP IS SELECTED....";
1510         return;
1511     }
1512     AbstractClipItem *item = (AbstractClipItem *)itemList.at(0);
1513     if (item->type() != AVWIDGET) {
1514         emit displayMessage(i18n("No clip at cursor time"), ErrorMessage);
1515         return;
1516     }
1517     GenTime pos = GenTime(m_cursorPos, m_document->fps());
1518     if (item->startPos() > pos || item->endPos() < pos) {
1519         emit displayMessage(i18n("No selected clip at cursor time"), ErrorMessage);
1520         return;
1521     }
1522     ClipItem *clip = (ClipItem *) item;
1523     int id = clip->baseClip()->getId();
1524     GenTime position = pos - item->startPos() + item->cropStart();
1525     QString comment = clip->baseClip()->markerComment(position);
1526     if (comment.isEmpty()) {
1527         emit displayMessage(i18n("No marker found at cursor time"), ErrorMessage);
1528         return;
1529     }
1530     AddMarkerCommand *command = new AddMarkerCommand(this, comment, QString(), id, position, true);
1531     m_commandStack->push(command);
1532 }
1533
1534 void CustomTrackView::slotEditClipMarker() {
1535     QList<QGraphicsItem *> itemList = scene()->selectedItems();
1536     if (itemList.count() != 1) {
1537         emit displayMessage(i18n("Cannot edit marker if more than one clip is selected"), ErrorMessage);
1538         kDebug() << "// CANNOT DELETE MARKER IF MORE TAN ONE CLIP IS SELECTED....";
1539         return;
1540     }
1541     AbstractClipItem *item = (AbstractClipItem *)itemList.at(0);
1542     if (item->type() != AVWIDGET) {
1543         emit displayMessage(i18n("No clip at cursor time"), ErrorMessage);
1544         return;
1545     }
1546     GenTime pos = GenTime(m_cursorPos, m_document->fps());
1547     if (item->startPos() > pos || item->endPos() < pos) {
1548         emit displayMessage(i18n("No selected clip at cursor time"), ErrorMessage);
1549         return;
1550     }
1551     ClipItem *clip = (ClipItem *) item;
1552     int id = clip->baseClip()->getId();
1553     GenTime position = pos - item->startPos() + item->cropStart();
1554     QString oldcomment = clip->baseClip()->markerComment(position);
1555     if (oldcomment.isEmpty()) {
1556         emit displayMessage(i18n("No marker found at cursor time"), ErrorMessage);
1557         return;
1558     }
1559
1560     CommentedTime marker(position, oldcomment);
1561     MarkerDialog d(clip->baseClip(), marker, m_document->timecode(), this);
1562     if (d.exec() == QDialog::Accepted) {
1563         if (d.newMarker().time() == position) {
1564             // marker position was not changed, only text
1565             AddMarkerCommand *command = new AddMarkerCommand(this, oldcomment, d.newMarker().comment(), id, position, true);
1566             m_commandStack->push(command);
1567         } else {
1568             // marker text and position were changed, remove previous marker and add new one
1569             AddMarkerCommand *command1 = new AddMarkerCommand(this, oldcomment, QString(), id, position, true);
1570             AddMarkerCommand *command2 = new AddMarkerCommand(this, QString(), d.newMarker().comment(), id, d.newMarker().time(), true);
1571             m_commandStack->push(command1);
1572             m_commandStack->push(command2);
1573         }
1574     }
1575 }
1576
1577 void CustomTrackView::addMarker(const int id, const GenTime &pos, const QString comment) {
1578     DocClipBase *base = m_document->clipManager()->getClipById(id);
1579     if (!comment.isEmpty()) base->addSnapMarker(pos, comment);
1580     else base->deleteSnapMarker(pos);
1581     m_document->setModified(true);
1582     viewport()->update();
1583 }
1584
1585
1586
1587 void CustomTrackView::editGuide(const GenTime oldPos, const GenTime pos, const QString &comment) {
1588     if (oldPos > GenTime() && pos > GenTime()) {
1589         // move guide
1590         for (int i = 0; i < m_guides.count(); i++) {
1591             if (m_guides.at(i)->position() == oldPos) {
1592                 Guide *item = m_guides.at(i);
1593                 item->updateGuide(pos, comment);
1594                 break;
1595             }
1596         }
1597     } else if (pos > GenTime()) addGuide(pos, comment);
1598     else {
1599         // remove guide
1600         bool found = false;
1601         for (int i = 0; i < m_guides.count(); i++) {
1602             if (m_guides.at(i)->position() == oldPos) {
1603                 Guide *item = m_guides.takeAt(i);
1604                 delete item;
1605                 found = true;
1606                 break;
1607             }
1608         }
1609         if (!found) emit displayMessage(i18n("No guide at cursor time"), ErrorMessage);
1610     }
1611 }
1612
1613 bool CustomTrackView::addGuide(const GenTime pos, const QString &comment) {
1614     for (int i = 0; i < m_guides.count(); i++) {
1615         if (m_guides.at(i)->position() == pos) {
1616             emit displayMessage(i18n("A guide already exists at that position"), ErrorMessage);
1617             return false;
1618         }
1619     }
1620     Guide *g = new Guide(this, pos, comment, m_scale, m_document->fps(), m_tracksHeight * m_tracksList.count());
1621     scene()->addItem(g);
1622     m_guides.append(g);
1623     return true;
1624 }
1625
1626 void CustomTrackView::slotAddGuide() {
1627     CommentedTime marker(GenTime(m_cursorPos, m_document->fps()), i18n("Guide"));
1628     MarkerDialog d(NULL, marker, m_document->timecode(), this);
1629     if (d.exec() != QDialog::Accepted) return;
1630     if (addGuide(d.newMarker().time(), d.newMarker().comment())) {
1631         EditGuideCommand *command = new EditGuideCommand(this, GenTime(), QString(), d.newMarker().time(), d.newMarker().comment(), false);
1632         m_commandStack->push(command);
1633     }
1634 }
1635
1636 void CustomTrackView::slotEditGuide() {
1637     GenTime pos = GenTime(m_cursorPos, m_document->fps());
1638     bool found = false;
1639     for (int i = 0; i < m_guides.count(); i++) {
1640         if (m_guides.at(i)->position() == pos) {
1641             slotEditGuide(m_guides.at(i)->info());
1642             found = true;
1643             break;
1644         }
1645     }
1646     if (!found) emit displayMessage(i18n("No guide at cursor time"), ErrorMessage);
1647 }
1648
1649 void CustomTrackView::slotEditGuide(CommentedTime guide) {
1650     MarkerDialog d(NULL, guide, m_document->timecode(), this);
1651     if (d.exec() == QDialog::Accepted) {
1652         EditGuideCommand *command = new EditGuideCommand(this, guide.time(), guide.comment(), d.newMarker().time(), d.newMarker().comment(), true);
1653         m_commandStack->push(command);
1654     }
1655 }
1656
1657
1658 void CustomTrackView::slotDeleteGuide() {
1659     GenTime pos = GenTime(m_cursorPos, m_document->fps());
1660     bool found = false;
1661     for (int i = 0; i < m_guides.count(); i++) {
1662         if (m_guides.at(i)->position() == pos) {
1663             EditGuideCommand *command = new EditGuideCommand(this, m_guides.at(i)->position(), m_guides.at(i)->label(), GenTime(), QString(), true);
1664             m_commandStack->push(command);
1665             found = true;
1666             break;
1667         }
1668     }
1669     if (!found) emit displayMessage(i18n("No guide at cursor time"), ErrorMessage);
1670 }
1671
1672 void CustomTrackView::setTool(PROJECTTOOL tool) {
1673     m_tool = tool;
1674 }
1675
1676 void CustomTrackView::setScale(double scaleFactor) {
1677     //scale(scaleFactor, scaleFactor);
1678     m_animationTimer->stop();
1679     if (m_visualTip) {
1680         delete m_visualTip;
1681         m_visualTip = NULL;
1682     }
1683     if (m_animation) {
1684         delete m_animation;
1685         m_animation = NULL;
1686     }
1687     double pos = cursorPos() / m_scale;
1688     m_scale = scaleFactor;
1689     int vert = verticalScrollBar()->value();
1690     kDebug() << " HHHHHHHH  SCALING: " << m_scale;
1691     QList<QGraphicsItem *> itemList = items();
1692     for (int i = 0; i < itemList.count(); i++) {
1693         if (itemList.at(i)->type() == AVWIDGET || itemList.at(i)->type() == TRANSITIONWIDGET) {
1694             AbstractClipItem *clip = (AbstractClipItem *)itemList.at(i);
1695             clip->setRect(clip->startPos().frames(m_document->fps()) * m_scale, clip->rect().y(), clip->duration().frames(m_document->fps()) * m_scale, clip->rect().height());
1696         }
1697     }
1698
1699     for (int i = 0; i < m_guides.count(); i++) {
1700         m_guides.at(i)->updatePosition(m_scale);
1701     }
1702
1703     setSceneRect(0, 0, (m_projectDuration + 100) * m_scale, sceneRect().height());
1704     updateCursorPos();
1705     centerOn(QPointF(cursorPos(), m_tracksHeight));
1706     verticalScrollBar()->setValue(vert);
1707 }
1708
1709 void CustomTrackView::slotRefreshGuides() {
1710     if (KdenliveSettings::showmarkers()) {
1711         kDebug() << "// refresh GUIDES";
1712         for (int i = 0; i < m_guides.count(); i++) {
1713             m_guides.at(i)->update();
1714         }
1715     }
1716 }
1717
1718 void CustomTrackView::drawBackground(QPainter * painter, const QRectF & rect) {
1719     QRect rectInView = viewport()->rect();
1720     rectInView.moveTo(horizontalScrollBar()->value(), verticalScrollBar()->value());
1721
1722     QColor base = palette().button().color();
1723     painter->setClipRect(rect);
1724     painter->drawLine(rectInView.left(), 0, rectInView.right(), 0);
1725     uint max = m_tracksList.count();
1726     for (uint i = 0; i < max;i++) {
1727         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)));
1728         painter->drawLine(rectInView.left(), m_tracksHeight * (i + 1), rectInView.right(), m_tracksHeight * (i + 1));
1729         //painter->drawText(QRectF(10, 50 * i, 100, 50 * i + 49), Qt::AlignLeft, i18n(" Track ") + QString::number(i + 1));
1730     }
1731     int lowerLimit = m_tracksHeight * m_tracksList.count() + 1;
1732     if (height() > lowerLimit)
1733         painter->fillRect(QRectF(rectInView.left(), lowerLimit, rectInView.width(), height() - lowerLimit), QBrush(base));
1734 }
1735
1736 QDomElement CustomTrackView::xmlInfo() {
1737     QDomDocument doc;
1738     QDomElement e;
1739     QDomElement guides = doc.createElement("guides");
1740     for (int i = 0; i < m_guides.count(); i++) {
1741         e = doc.createElement("guide");
1742         e.setAttribute("time", m_guides.at(i)->position().ms() / 1000);
1743         e.setAttribute("comment", m_guides.at(i)->label());
1744         guides.appendChild(e);
1745     }
1746     return guides;
1747 }
1748
1749 bool CustomTrackView::findString(const QString &text) {
1750     QString marker;
1751     for (int i = 0; i < m_searchPoints.size(); ++i) {
1752         marker = m_searchPoints.at(i).comment();
1753         if (marker.contains(text, Qt::CaseInsensitive)) {
1754             setCursorPos(m_searchPoints.at(i).time().frames(m_document->fps()), true);
1755             int vert = verticalScrollBar()->value();
1756             int hor = cursorPos();
1757             ensureVisible(hor, vert + 10, 2, 2, 50, 0);
1758             m_findIndex = i;
1759             return true;
1760         }
1761     }
1762     return false;
1763 }
1764
1765 bool CustomTrackView::findNextString(const QString &text) {
1766     QString marker;
1767     for (int i = m_findIndex + 1; i < m_searchPoints.size(); ++i) {
1768         marker = m_searchPoints.at(i).comment();
1769         if (marker.contains(text, Qt::CaseInsensitive)) {
1770             setCursorPos(m_searchPoints.at(i).time().frames(m_document->fps()), true);
1771             int vert = verticalScrollBar()->value();
1772             int hor = cursorPos();
1773             ensureVisible(hor, vert + 10, 2, 2, 50, 0);
1774             m_findIndex = i;
1775             return true;
1776         }
1777     }
1778     m_findIndex = -1;
1779     return false;
1780 }
1781
1782 void CustomTrackView::initSearchStrings() {
1783     m_searchPoints.clear();
1784     QList<QGraphicsItem *> itemList = items();
1785     for (int i = 0; i < itemList.count(); i++) {
1786         // parse all clip names
1787         if (itemList.at(i)->type() == AVWIDGET) {
1788             ClipItem *item = static_cast <ClipItem *>(itemList.at(i));
1789             GenTime start = item->startPos();
1790             CommentedTime t(start, item->clipName());
1791             m_searchPoints.append(t);
1792             // add all clip markers
1793             QList < CommentedTime > markers = item->commentedSnapMarkers();
1794             m_searchPoints += markers;
1795         }
1796     }
1797
1798     // add guides
1799     for (int i = 0; i < m_guides.count(); i++) {
1800         m_searchPoints.append(m_guides.at(i)->info());
1801     }
1802
1803     qSort(m_searchPoints);
1804 }
1805
1806 void CustomTrackView::clearSearchStrings() {
1807     m_searchPoints.clear();
1808     m_findIndex = 0;
1809 }
1810
1811 /*
1812 void CustomTrackView::drawForeground ( QPainter * painter, const QRectF & rect )
1813 {
1814   //kDebug()<<"/////  DRAWING FB: "<<rect.x()<<", width: "<<rect.width();
1815   painter->fillRect(rect, QColor(50, rand() % 250,50,100));
1816   painter->drawLine(m_cursorPos, rect.y(), m_cursorPos, rect.y() + rect.height());
1817 }
1818 */
1819 #include "customtrackview.moc"