]> git.sesse.net Git - kdenlive/blob - src/customtrackview.cpp
Small fix to transition change
[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     item->setTransitionParameters(transition);
872     m_document->setModified(true);
873 }
874
875 void CustomTrackView::addItem(DocClipBase *clip, QPoint pos) {
876     ItemInfo info;
877     info.startPos = GenTime((int)(mapToScene(pos).x() / m_scale), m_document->fps());
878     info.endPos = info.startPos + clip->duration();
879     info.track = (int)(pos.y() / m_tracksHeight);
880     //kDebug()<<"------------  ADDING CLIP ITEM----: "<<info.startPos.frames(25)<<", "<<info.endPos.frames(25)<<", "<<info.track;
881     m_dropItem = new ClipItem(clip, info, GenTime(), m_scale, m_document->fps());
882     scene()->addItem(m_dropItem);
883 }
884
885
886 void CustomTrackView::dragMoveEvent(QDragMoveEvent * event) {
887     event->setDropAction(Qt::IgnoreAction);
888     if (m_dropItem) {
889         int track = (int)(mapToScene(event->pos()).y() / m_tracksHeight);  //) * (m_scale * 50) + m_scale;
890         m_dropItem->moveTo((int)(mapToScene(event->pos()).x() / m_scale), m_scale, (int)((track - m_dropItem->track()) * m_tracksHeight), track);
891         event->setDropAction(Qt::MoveAction);
892         if (event->mimeData()->hasFormat("kdenlive/producerslist")) {
893             event->acceptProposedAction();
894         }
895     } else {
896         QGraphicsView::dragMoveEvent(event);
897     }
898 }
899
900 void CustomTrackView::dragLeaveEvent(QDragLeaveEvent * event) {
901     if (m_dropItem) {
902         delete m_dropItem;
903         m_dropItem = NULL;
904     } else QGraphicsView::dragLeaveEvent(event);
905 }
906
907 void CustomTrackView::dropEvent(QDropEvent * event) {
908     if (m_dropItem) {
909         ItemInfo info;
910         info.startPos = m_dropItem->startPos();
911         info.endPos = m_dropItem->endPos();
912         info.track = m_dropItem->track();
913         AddTimelineClipCommand *command = new AddTimelineClipCommand(this, m_dropItem->xml(), m_dropItem->clipProducer(), info, false, false);
914         m_commandStack->push(command);
915         m_dropItem->baseClip()->addReference();
916         m_document->updateClip(m_dropItem->baseClip()->getId());
917         // kDebug()<<"IIIIIIIIIIIIIIIIIIIIIIII TRAX CNT: "<<m_tracksList.count()<<", DROP: "<<m_dropItem->track();
918         m_document->renderer()->mltInsertClip(m_tracksList.count() - m_dropItem->track(), m_dropItem->startPos(), m_dropItem->xml());
919         m_document->setModified(true);
920     } else QGraphicsView::dropEvent(event);
921     m_dropItem = NULL;
922 }
923
924
925 QStringList CustomTrackView::mimeTypes() const {
926     QStringList qstrList;
927     // list of accepted mime types for drop
928     qstrList.append("text/plain");
929     qstrList.append("kdenlive/producerslist");
930     return qstrList;
931 }
932
933 Qt::DropActions CustomTrackView::supportedDropActions() const {
934     // returns what actions are supported when dropping
935     return Qt::MoveAction;
936 }
937
938 void CustomTrackView::setDuration(int duration) {
939     kDebug() << "/////////////  PRO DUR: " << duration << ", SCALE. " << (m_projectDuration + 500) * m_scale << ", height: " << 50 * m_tracksList.count();
940     m_projectDuration = duration;
941     setSceneRect(0, 0, (m_projectDuration + 100) * m_scale, sceneRect().height());
942 }
943
944 int CustomTrackView::duration() const {
945     return m_projectDuration;
946 }
947
948 void CustomTrackView::addTrack(TrackInfo type) {
949     m_tracksList << type;
950     m_cursorLine->setLine(m_cursorLine->line().x1(), 0, m_cursorLine->line().x1(), m_tracksHeight * m_tracksList.count());
951     setSceneRect(0, 0, sceneRect().width(), m_tracksHeight * m_tracksList.count());
952     verticalScrollBar()->setMaximum(m_tracksHeight * m_tracksList.count());
953     //setFixedHeight(50 * m_tracksCount);
954 }
955
956 void CustomTrackView::removeTrack() {
957     // TODO: implement track deletion
958     //m_tracksCount--;
959     m_cursorLine->setLine(m_cursorLine->line().x1(), 0, m_cursorLine->line().x1(), m_tracksHeight * m_tracksList.count());
960 }
961
962
963 void CustomTrackView::slotSwitchTrackAudio(int ix) {
964     int tracknumber = m_tracksList.count() - ix;
965     kDebug() << "/////  MUTING TRK: " << ix << "; PL NUM: " << tracknumber;
966     m_tracksList[tracknumber - 1].isMute = !m_tracksList.at(tracknumber - 1).isMute;
967     m_document->renderer()->mltChangeTrackState(tracknumber, m_tracksList.at(tracknumber - 1).isMute, m_tracksList.at(tracknumber - 1).isBlind);
968 }
969
970 void CustomTrackView::slotSwitchTrackVideo(int ix) {
971     int tracknumber = m_tracksList.count() - ix;
972     m_tracksList[tracknumber - 1].isBlind = !m_tracksList.at(tracknumber - 1).isBlind;
973     m_document->renderer()->mltChangeTrackState(tracknumber, m_tracksList.at(tracknumber - 1).isMute, m_tracksList.at(tracknumber - 1).isBlind);
974 }
975
976 void CustomTrackView::deleteClip(int clipId) {
977     QList<QGraphicsItem *> itemList = items();
978     for (int i = 0; i < itemList.count(); i++) {
979         if (itemList.at(i)->type() == AVWIDGET) {
980             ClipItem *item = (ClipItem *)itemList.at(i);
981             if (item->clipProducer() == clipId) {
982                 ItemInfo info;
983                 info.startPos = item->startPos();
984                 info.endPos = item->endPos();
985                 info.track = item->track();
986                 AddTimelineClipCommand *command = new AddTimelineClipCommand(this, item->xml(), item->clipProducer(), info, true, true);
987                 m_commandStack->push(command);
988                 //delete item;
989             }
990         }
991     }
992 }
993
994 void CustomTrackView::setCursorPos(int pos, bool seek) {
995     emit cursorMoved((int)(m_cursorPos * m_scale), (int)(pos * m_scale));
996     m_cursorPos = pos;
997     m_cursorLine->setPos(pos * m_scale, 0);
998     if (seek) m_document->renderer()->seek(GenTime(pos, m_document->fps()));
999     else if (m_autoScroll && m_scale < 50) checkScrolling();
1000 }
1001
1002 void CustomTrackView::updateCursorPos() {
1003     m_cursorLine->setPos(m_cursorPos * m_scale, 0);
1004 }
1005
1006 int CustomTrackView::cursorPos() {
1007     return (int)(m_cursorPos * m_scale);
1008 }
1009
1010 void CustomTrackView::moveCursorPos(int delta) {
1011     if (m_cursorPos + delta < 0) delta = 0 - m_cursorPos;
1012     emit cursorMoved((int)(m_cursorPos * m_scale), (int)((m_cursorPos + delta) * m_scale));
1013     m_cursorPos += delta;
1014     m_cursorLine->setPos(m_cursorPos * m_scale, 0);
1015     m_document->renderer()->seek(GenTime(m_cursorPos, m_document->fps()));
1016     //if (m_autoScroll && m_scale < 50) checkScrolling();
1017 }
1018
1019 void CustomTrackView::checkScrolling() {
1020     int vert = verticalScrollBar()->value();
1021     int hor = cursorPos();
1022     ensureVisible(hor, vert + 10, 2, 2, 50, 0);
1023     //centerOn(QPointF(cursorPos(), m_tracksHeight));
1024     /*QRect rectInView = viewport()->rect();
1025     int delta = rectInView.width() / 3;
1026     int max = rectInView.right() + horizontalScrollBar()->value() - delta;
1027     //kDebug() << "CURSOR POS: "<<m_cursorPos<< "Scale: "<<m_scale;
1028     if (m_cursorPos * m_scale >= max) horizontalScrollBar()->setValue((int)(horizontalScrollBar()->value() + 1 + m_scale));*/
1029 }
1030
1031 void CustomTrackView::mouseReleaseEvent(QMouseEvent * event) {
1032     if (event->button() == Qt::MidButton) {
1033         return;
1034     }
1035     QGraphicsView::mouseReleaseEvent(event);
1036     setDragMode(QGraphicsView::NoDrag);
1037     if (m_operationMode == MOVEGUIDE) {
1038         setCursor(Qt::ArrowCursor);
1039         m_operationMode = NONE;
1040         m_dragGuide->setFlag(QGraphicsItem::ItemIsMovable, false);
1041         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);
1042         m_commandStack->push(command);
1043         m_dragGuide->updateGuide(GenTime(m_dragGuide->pos().x() / m_scale, m_document->fps()));
1044         m_dragGuide = NULL;
1045         m_dragItem = NULL;
1046         return;
1047     }
1048     if (m_dragItem == NULL) {
1049         emit transitionItemSelected(NULL);
1050         return;
1051     }
1052     ItemInfo info;
1053     info.startPos = m_dragItem->startPos();
1054     info.endPos = m_dragItem->endPos();
1055     info.track = m_dragItem->track();
1056
1057     if (m_operationMode == MOVE) {
1058         setCursor(Qt::OpenHandCursor);
1059         // move clip
1060         if (m_dragItem->type() == AVWIDGET && (m_dragItemInfo.startPos != info.startPos || m_dragItemInfo.track != info.track)) {
1061             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())));
1062             if (success) {
1063                 MoveClipCommand *command = new MoveClipCommand(this, m_dragItemInfo, info, false);
1064                 m_commandStack->push(command);
1065             } else {
1066                 // undo last move and emit error message
1067                 MoveClipCommand *command = new MoveClipCommand(this, info, m_dragItemInfo, true);
1068                 m_commandStack->push(command);
1069                 emit displayMessage(i18n("Cannot move clip to position %1seconds", QString::number(m_dragItemInfo.startPos.seconds(), 'g', 2)), ErrorMessage);
1070             }
1071         }
1072         if (m_dragItem->type() == TRANSITIONWIDGET && (m_dragItemInfo.startPos != info.startPos || m_dragItemInfo.track != info.track)) {
1073             MoveTransitionCommand *command = new MoveTransitionCommand(this, m_dragItemInfo, info, false);
1074             m_commandStack->push(command);
1075             Transition *transition = (Transition *) m_dragItem;
1076             transition->updateTransitionEndTrack(getPreviousVideoTrack(m_dragItem->track()));
1077             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);
1078         }
1079
1080     } else if (m_operationMode == RESIZESTART && m_dragItem->startPos() != m_dragItemInfo.startPos) {
1081         // resize start
1082         if (m_dragItem->type() == AVWIDGET) {
1083             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());
1084             updateClipFade((ClipItem *) m_dragItem);
1085             ResizeClipCommand *command = new ResizeClipCommand(this, m_dragItemInfo, info, false);
1086             m_commandStack->push(command);
1087         } else if (m_dragItem->type() == TRANSITIONWIDGET) {
1088             MoveTransitionCommand *command = new MoveTransitionCommand(this, m_dragItemInfo, info, false);
1089             m_commandStack->push(command);
1090             Transition *transition = (Transition *) m_dragItem;
1091             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);
1092         }
1093
1094         //m_document->renderer()->doRefresh();
1095     } else if (m_operationMode == RESIZEEND && m_dragItem->endPos() != m_dragItemInfo.endPos) {
1096         // resize end
1097         if (m_dragItem->type() == AVWIDGET) {
1098             ResizeClipCommand *command = new ResizeClipCommand(this, m_dragItemInfo, info, false);
1099             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());
1100             m_commandStack->push(command);
1101         } else if (m_dragItem->type() == TRANSITIONWIDGET) {
1102             MoveTransitionCommand *command = new MoveTransitionCommand(this, m_dragItemInfo, info, false);
1103             m_commandStack->push(command);
1104             Transition *transition = (Transition *) m_dragItem;
1105             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);
1106         }
1107         //m_document->renderer()->doRefresh();
1108     } else if (m_operationMode == FADEIN) {
1109         // resize fade in effect
1110         ClipItem * item = (ClipItem *) m_dragItem;
1111         QStringList clipeffects = item->effectNames();
1112         if (clipeffects.contains(i18n("Fade in"))) {
1113             int ix = clipeffects.indexOf(i18n("Fade in"));
1114             QDomElement oldeffect = item->effectAt(ix);
1115             int start = item->cropStart().frames(m_document->fps());
1116             int end = item->fadeIn();
1117             if (end == 0) {
1118                 slotDeleteEffect(item, oldeffect);
1119             } else {
1120                 end += start;
1121                 QDomElement effect = MainWindow::audioEffects.getEffectByName("Fade in");
1122                 EffectsList::setParameter(effect, "in", QString::number(start));
1123                 EffectsList::setParameter(effect, "out", QString::number(end));
1124                 slotUpdateClipEffect(item, oldeffect, effect, ix);
1125             }
1126         } else if (item->fadeIn() != 0) {
1127             QDomElement effect = MainWindow::audioEffects.getEffectByName("Fade in");
1128             int start = item->cropStart().frames(m_document->fps());
1129             int end = item->fadeIn() + start;
1130             EffectsList::setParameter(effect, "in", QString::number(start));
1131             EffectsList::setParameter(effect, "out", QString::number(end));
1132             slotAddEffect(effect, m_dragItem->startPos(), m_dragItem->track());
1133         }
1134     } else if (m_operationMode == FADEOUT) {
1135         // resize fade in effect
1136         ClipItem * item = (ClipItem *) m_dragItem;
1137         QStringList clipeffects = item->effectNames();
1138         if (clipeffects.contains(i18n("Fade out"))) {
1139             int ix = clipeffects.indexOf(i18n("Fade out"));
1140             QDomElement oldeffect = item->effectAt(ix);
1141             int end = (item->duration() + item->cropStart()).frames(m_document->fps());
1142             int start = item->fadeOut();
1143             if (start == 0) {
1144                 slotDeleteEffect(item, oldeffect);
1145             } else {
1146                 start = end - start;
1147                 QDomElement effect = MainWindow::audioEffects.getEffectByName("Fade out");
1148                 EffectsList::setParameter(effect, "in", QString::number(start));
1149                 EffectsList::setParameter(effect, "out", QString::number(end));
1150                 slotUpdateClipEffect(item, oldeffect, effect, ix);
1151             }
1152         } else if (item->fadeOut() != 0) {
1153             QDomElement effect = MainWindow::audioEffects.getEffectByName("Fade out");
1154             int end = (item->duration() + item->cropStart()).frames(m_document->fps());
1155             int start = end - item->fadeOut();
1156             EffectsList::setParameter(effect, "in", QString::number(start));
1157             EffectsList::setParameter(effect, "out", QString::number(end));
1158             slotAddEffect(effect, m_dragItem->startPos(), m_dragItem->track());
1159         }
1160     } else if (m_operationMode == KEYFRAME) {
1161         // update the MLT effect
1162         ClipItem * item = (ClipItem *) m_dragItem;
1163         QString previous = item->keyframes(item->selectedEffectIndex());
1164         item->updateKeyframeEffect();
1165         QString next = item->keyframes(item->selectedEffectIndex());
1166         EditKeyFrameCommand *command = new EditKeyFrameCommand(this, item->track(), item->startPos(), item->selectedEffectIndex(), previous, next, false);
1167         m_commandStack->push(command);
1168         updateEffect(m_tracksList.count() - item->track(), item->startPos(), item->selectedEffect(), item->selectedEffectIndex());
1169     }
1170
1171     emit transitionItemSelected((m_dragItem && m_dragItem->type() == TRANSITIONWIDGET) ? (Transition*) m_dragItem : NULL);
1172     m_document->setModified(true);
1173     m_operationMode = NONE;
1174 }
1175
1176 void CustomTrackView::deleteClip(ItemInfo info) {
1177     ClipItem *item = getClipItemAt((int) info.startPos.frames(m_document->fps()), info.track);
1178     if (!item) {
1179         kDebug() << "----------------  ERROR, CANNOT find clip to move at...";// << rect.x();
1180         return;
1181     }
1182     if (item->isSelected()) emit clipItemSelected(NULL);
1183     item->baseClip()->removeReference();
1184     m_document->updateClip(item->baseClip()->getId());
1185     delete item;
1186     m_document->renderer()->mltRemoveClip(m_tracksList.count() - info.track, info.startPos);
1187     m_document->renderer()->doRefresh();
1188 }
1189
1190 void CustomTrackView::deleteSelectedClips() {
1191     QList<QGraphicsItem *> itemList = scene()->selectedItems();
1192     for (int i = 0; i < itemList.count(); i++) {
1193         if (itemList.at(i)->type() == AVWIDGET) {
1194             ClipItem *item = (ClipItem *) itemList.at(i);
1195             ItemInfo info;
1196             info.startPos = item->startPos();
1197             info.endPos = item->endPos();
1198             info.track = item->track();
1199             AddTimelineClipCommand *command = new AddTimelineClipCommand(this, item->xml(), item->clipProducer(), info, true, true);
1200             m_commandStack->push(command);
1201         } else if (itemList.at(i)->type() == TRANSITIONWIDGET) {
1202             Transition *item = (Transition *) itemList.at(i);
1203             ItemInfo info;
1204             info.startPos = item->startPos();
1205             info.endPos = item->endPos();
1206             info.track = item->track();
1207             AddTransitionCommand *command = new AddTransitionCommand(this, info, item->transitionEndTrack(), QDomElement(), true, true);
1208             m_commandStack->push(command);
1209         }
1210     }
1211 }
1212
1213 void CustomTrackView::cutSelectedClips() {
1214     QList<QGraphicsItem *> itemList = scene()->selectedItems();
1215     GenTime currentPos = GenTime(m_cursorPos, m_document->fps());
1216     for (int i = 0; i < itemList.count(); i++) {
1217         if (itemList.at(i)->type() == AVWIDGET) {
1218             ClipItem *item = (ClipItem *) itemList.at(i);
1219             ItemInfo info;
1220             info.startPos = item->startPos();
1221             info.endPos = item->endPos();
1222             if (currentPos > info.startPos && currentPos <  info.endPos) {
1223                 info.track = item->track();
1224                 RazorClipCommand *command = new RazorClipCommand(this, info, currentPos, true);
1225                 m_commandStack->push(command);
1226             }
1227         }
1228     }
1229 }
1230
1231 void CustomTrackView::addClip(QDomElement xml, int clipId, ItemInfo info) {
1232     DocClipBase *baseclip = m_document->clipManager()->getClipById(clipId);
1233     int crop = xml.attribute("in").toInt();
1234     ClipItem *item = new ClipItem(baseclip, info, GenTime(crop, m_document->fps()), m_scale, m_document->fps());
1235     scene()->addItem(item);
1236     baseclip->addReference();
1237     m_document->updateClip(baseclip->getId());
1238     m_document->renderer()->mltInsertClip(m_tracksList.count() - info.track, info.startPos, xml);
1239     m_document->renderer()->doRefresh();
1240 }
1241
1242 void CustomTrackView::slotUpdateClip(int clipId) {
1243     QList<QGraphicsItem *> list = scene()->items();
1244     ClipItem *clip = NULL;
1245     for (int i = 0; i < list.size(); ++i) {
1246         if (list.at(i)->type() == AVWIDGET) {
1247             clip = static_cast <ClipItem *>(list.at(i));
1248             if (clip->clipProducer() == clipId) {
1249                 clip->refreshClip();
1250                 m_document->renderer()->mltUpdateClip(m_tracksList.count() - clip->track(), clip->startPos(), clip->xml());
1251             }
1252         }
1253     }
1254 }
1255
1256 ClipItem *CustomTrackView::getClipItemAt(int pos, int track) {
1257     QList<QGraphicsItem *> list = scene()->items(QPointF(pos * m_scale, track * m_tracksHeight + m_tracksHeight / 2));
1258     ClipItem *clip = NULL;
1259     for (int i = 0; i < list.size(); ++i) {
1260         if (list.at(i)->type() == AVWIDGET) {
1261             clip = static_cast <ClipItem *>(list.at(i));
1262             break;
1263         }
1264     }
1265     return clip;
1266 }
1267
1268 ClipItem *CustomTrackView::getClipItemAt(GenTime pos, int track) {
1269     int framepos = (int)(pos.frames(m_document->fps()) * m_scale);
1270     return getClipItemAt(framepos, track);
1271 }
1272
1273 Transition *CustomTrackView::getTransitionItemAt(int pos, int track) {
1274     QList<QGraphicsItem *> list = scene()->items(QPointF(pos * m_scale, (track + 1) * m_tracksHeight));
1275     Transition *clip = NULL;
1276     for (int i = 0; i < list.size(); ++i) {
1277         if (list.at(i)->type() == TRANSITIONWIDGET) {
1278             clip = static_cast <Transition *>(list.at(i));
1279             break;
1280         }
1281     }
1282     return clip;
1283 }
1284
1285 Transition *CustomTrackView::getTransitionItemAt(GenTime pos, int track) {
1286     int framepos = (int)(pos.frames(m_document->fps()) * m_scale);
1287     return getTransitionItemAt(framepos, track);
1288 }
1289
1290 void CustomTrackView::moveClip(const ItemInfo start, const ItemInfo end) {
1291     ClipItem *item = getClipItemAt((int) start.startPos.frames(m_document->fps()) + 1, start.track);
1292     if (!item) {
1293         emit displayMessage(i18n("Cannot move clip at time: %1s on track %2", QString::number(start.startPos.seconds(), 'g', 2), start.track), ErrorMessage);
1294         kDebug() << "----------------  ERROR, CANNOT find clip to move at.. ";// << startPos.x() * m_scale * FRAME_SIZE + 1 << ", " << startPos.y() * m_tracksHeight + m_tracksHeight / 2;
1295         return;
1296     }
1297     //kDebug() << "----------------  Move CLIP FROM: " << startPos.x() << ", END:" << endPos.x() << ",TRACKS: " << startPos.y() << " TO " << endPos.y();
1298
1299     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()));
1300     if (success) {
1301         item->moveTo((int) end.startPos.frames(m_document->fps()), m_scale, (int)((end.track - start.track) * m_tracksHeight), end.track);
1302     } else {
1303         // undo last move and emit error message
1304         emit displayMessage(i18n("Cannot move clip to position %1seconds", QString::number(end.startPos.seconds(), 'g', 2)), ErrorMessage);
1305     }
1306 }
1307
1308 void CustomTrackView::moveTransition(const ItemInfo start, const ItemInfo end) {
1309     Transition *item = getTransitionItemAt((int)start.startPos.frames(m_document->fps()) + 1, start.track);
1310     if (!item) {
1311         emit displayMessage(i18n("Cannot move transition at time: %1s on track %2", start.startPos.seconds(), start.track), ErrorMessage);
1312         kDebug() << "----------------  ERROR, CANNOT find transition to move... ";// << startPos.x() * m_scale * FRAME_SIZE + 1 << ", " << startPos.y() * m_tracksHeight + m_tracksHeight / 2;
1313         return;
1314     }
1315     //kDebug() << "----------------  Move TRANSITION FROM: " << startPos.x() << ", END:" << endPos.x() << ",TRACKS: " << oldtrack << " TO " << newtrack;
1316
1317     //kDebug()<<"///  RESIZE TRANS START: ("<< startPos.x()<<"x"<< startPos.y()<<") / ("<<endPos.x()<<"x"<< endPos.y()<<")";
1318     if (end.endPos - end.startPos == start.endPos - start.startPos) {
1319         // Transition was moved
1320         item->moveTo((int) end.startPos.frames(m_document->fps()), m_scale, (end.track - start.track) * m_tracksHeight, end.track);
1321     } else if (end.endPos == start.endPos) {
1322         // Transition start resize
1323         item->resizeStart((int) end.startPos.frames(m_document->fps()), m_scale);
1324     } else {
1325         // Transition end resize;
1326         item->resizeEnd((int) end.endPos.frames(m_document->fps()), m_scale);
1327     }
1328     //item->moveTransition(GenTime((int) (endPos.x() - startPos.x()), m_document->fps()));
1329     item->updateTransitionEndTrack(getPreviousVideoTrack(end.track));
1330     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);
1331 }
1332
1333 void CustomTrackView::resizeClip(const ItemInfo start, const ItemInfo end) {
1334     int offset = 0;
1335     bool resizeClipStart = true;
1336     if (start.startPos == end.startPos) resizeClipStart = false;
1337     /*if (resizeClipStart) offset = 1;
1338     else offset = -1;*/
1339     ClipItem *item = getClipItemAt((int)(start.startPos.frames(m_document->fps()) + offset), start.track);
1340     if (!item) {
1341         emit displayMessage(i18n("Cannot move clip at time: %1s on track %2", start.startPos.seconds(), start.track), ErrorMessage);
1342         kDebug() << "----------------  ERROR, CANNOT find clip to resize at... "; // << startPos;
1343         return;
1344     }
1345     if (resizeClipStart) {
1346         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);
1347         item->resizeStart((int) end.startPos.frames(m_document->fps()), m_scale);
1348         updateClipFade(item);
1349     } else {
1350         m_document->renderer()->mltResizeClipEnd(m_tracksList.count() - item->track(), item->startPos(), item->cropStart(), item->cropStart() + end.endPos - item->startPos());
1351         item->resizeEnd((int) end.endPos.frames(m_document->fps()), m_scale);
1352         updateClipFade(item, true);
1353     }
1354     m_document->renderer()->doRefresh();
1355 }
1356
1357 void CustomTrackView::updateClipFade(ClipItem * item, bool updateFadeOut) {
1358     if (!updateFadeOut) {
1359         int end = item->fadeIn();
1360         if (end != 0) {
1361             // there is a fade in effect
1362             QStringList clipeffects = item->effectNames();
1363             QDomElement oldeffect = item->effectAt(clipeffects.indexOf("Fade in"));
1364             int start = item->cropStart().frames(m_document->fps());
1365             end += start;
1366             EffectsList::setParameter(oldeffect, "in", QString::number(start));
1367             EffectsList::setParameter(oldeffect, "out", QString::number(end));
1368             QMap <QString, QString> effectParams = item->getEffectArgs(oldeffect);
1369             if (!m_document->renderer()->mltEditEffect(m_tracksList.count() - item->track(), item->startPos(), effectParams))
1370                 emit displayMessage(i18n("Problem editing effect"), ErrorMessage);
1371         }
1372     } else {
1373         int start = item->fadeOut();
1374         if (start != 0) {
1375             // there is a fade in effect
1376             QStringList clipeffects = item->effectNames();
1377             QDomElement oldeffect = item->effectAt(clipeffects.indexOf("Fade out"));
1378             int end = (item->duration() - item->cropStart()).frames(m_document->fps());
1379             start = end - start;
1380             EffectsList::setParameter(oldeffect, "in", QString::number(start));
1381             EffectsList::setParameter(oldeffect, "out", QString::number(end));
1382             QMap <QString, QString> effectParams = item->getEffectArgs(oldeffect);
1383             if (m_document->renderer()->mltEditEffect(m_tracksList.count() - item->track(), item->startPos(), effectParams))
1384                 emit displayMessage(i18n("Problem editing effect"), ErrorMessage);
1385         }
1386     }
1387 }
1388
1389 double CustomTrackView::getSnapPointForPos(double pos) {
1390     for (int i = 0; i < m_snapPoints.size(); ++i) {
1391         if (abs((int)(pos - m_snapPoints.at(i).frames(m_document->fps()) * m_scale)) < 10) {
1392             //kDebug()<<" FOUND SNAP POINT AT: "<<m_snapPoints.at(i)<<", current pos: "<<pos / m_scale;
1393             return m_snapPoints.at(i).frames(m_document->fps()) * m_scale + 0.5;
1394         }
1395         if (m_snapPoints.at(i).frames(m_document->fps() * m_scale) > pos) break;
1396     }
1397     return pos;
1398 }
1399
1400 void CustomTrackView::updateSnapPoints(AbstractClipItem *selected) {
1401     m_snapPoints.clear();
1402     if (!KdenliveSettings::snaptopoints()) return;
1403     GenTime offset;
1404     if (selected) offset = selected->duration();
1405     QList<QGraphicsItem *> itemList = items();
1406     for (int i = 0; i < itemList.count(); i++) {
1407         if (itemList.at(i)->type() == AVWIDGET && itemList.at(i) != selected) {
1408             ClipItem *item = static_cast <ClipItem *>(itemList.at(i));
1409             GenTime start = item->startPos();
1410             GenTime end = item->endPos();
1411             m_snapPoints.append(start);
1412             m_snapPoints.append(end);
1413             QList < GenTime > markers = item->snapMarkers();
1414             for (int i = 0; i < markers.size(); ++i) {
1415                 GenTime t = markers.at(i);
1416                 m_snapPoints.append(t);
1417                 if (t > offset) m_snapPoints.append(t - offset);
1418             }
1419             if (offset != GenTime()) {
1420                 if (start > offset) m_snapPoints.append(start - offset);
1421                 if (end > offset) m_snapPoints.append(end - offset);
1422             }
1423         } else if (itemList.at(i)->type() == TRANSITIONWIDGET) {
1424             Transition *transition = static_cast <Transition*>(itemList.at(i));
1425             GenTime start = transition->startPos();
1426             GenTime end = transition->endPos();
1427             m_snapPoints.append(start);
1428             m_snapPoints.append(end);
1429             if (offset != GenTime()) {
1430                 if (start > offset) m_snapPoints.append(start - offset);
1431                 if (end > offset) m_snapPoints.append(end - offset);
1432             }
1433         }
1434     }
1435
1436     // add cursor position
1437     GenTime pos = GenTime(m_cursorPos, m_document->fps());
1438     m_snapPoints.append(pos);
1439     if (offset != GenTime()) m_snapPoints.append(pos - offset);
1440
1441     // add guides
1442     for (int i = 0; i < m_guides.count(); i++) {
1443         m_snapPoints.append(m_guides.at(i)->position());
1444         if (offset != GenTime()) m_snapPoints.append(m_guides.at(i)->position() - offset);
1445     }
1446
1447     qSort(m_snapPoints);
1448     //for (int i = 0; i < m_snapPoints.size(); ++i)
1449     //    kDebug() << "SNAP POINT: " << m_snapPoints.at(i).frames(25);
1450 }
1451
1452 void CustomTrackView::slotSeekToPreviousSnap() {
1453     updateSnapPoints(NULL);
1454     GenTime pos = GenTime(m_cursorPos, m_document->fps());
1455     GenTime res = GenTime();
1456     for (int i = 0; i < m_snapPoints.size(); ++i) {
1457         if (m_snapPoints.at(i) >= pos) {
1458             if (i == 0) i = 1;
1459             res = m_snapPoints.at(i - 1);
1460             break;
1461         }
1462     }
1463     setCursorPos((int) res.frames(m_document->fps()));
1464     checkScrolling();
1465 }
1466
1467 void CustomTrackView::slotSeekToNextSnap() {
1468     updateSnapPoints(NULL);
1469     GenTime pos = GenTime(m_cursorPos, m_document->fps());
1470     GenTime res = GenTime(m_projectDuration, m_document->fps());
1471     for (int i = 0; i < m_snapPoints.size(); ++i) {
1472         if (m_snapPoints.at(i) > pos) {
1473             res = m_snapPoints.at(i);
1474             break;
1475         }
1476     }
1477     setCursorPos((int) res.frames(m_document->fps()));
1478     checkScrolling();
1479 }
1480
1481 void CustomTrackView::clipStart() {
1482     QList<QGraphicsItem *> itemList = scene()->selectedItems();
1483     for (int i = 0; i < itemList.count(); i++) {
1484         if (itemList.at(i)->type() == AVWIDGET) {
1485             ClipItem *item = (ClipItem *) itemList.at(i);
1486             setCursorPos((int) item->startPos().frames(m_document->fps()));
1487             checkScrolling();
1488             break;
1489         }
1490     }
1491 }
1492
1493 void CustomTrackView::clipEnd() {
1494     QList<QGraphicsItem *> itemList = scene()->selectedItems();
1495     for (int i = 0; i < itemList.count(); i++) {
1496         if (itemList.at(i)->type() == AVWIDGET) {
1497             ClipItem *item = (ClipItem *) itemList.at(i);
1498             setCursorPos((int) item->endPos().frames(m_document->fps()));
1499             checkScrolling();
1500             break;
1501         }
1502     }
1503 }
1504
1505 void CustomTrackView::slotAddClipMarker() {
1506     QList<QGraphicsItem *> itemList = scene()->selectedItems();
1507     if (itemList.count() != 1) {
1508         emit displayMessage(i18n("Cannot add marker if more than one clip is selected"), ErrorMessage);
1509         kDebug() << "// CANNOT ADD MARKER IF MORE TAN ONE CLIP IS SELECTED....";
1510         return;
1511     }
1512     AbstractClipItem *item = (AbstractClipItem *)itemList.at(0);
1513     if (item->type() != AVWIDGET) return;
1514     GenTime pos = GenTime(m_cursorPos, m_document->fps());
1515     if (item->startPos() > pos || item->endPos() < pos) return;
1516     ClipItem *clip = (ClipItem *) item;
1517     int id = clip->baseClip()->getId();
1518     GenTime position = pos - item->startPos() + item->cropStart();
1519     CommentedTime marker(position, i18n("Marker"));
1520     MarkerDialog d(clip->baseClip(), marker, m_document->timecode(), this);
1521     if (d.exec() == QDialog::Accepted) {
1522         slotAddClipMarker(id, d.newMarker().time(), d.newMarker().comment());
1523     }
1524 }
1525
1526 void CustomTrackView::slotAddClipMarker(int id, GenTime t, QString c) {
1527     QString oldcomment = m_document->clipManager()->getClipById(id)->markerComment(t);
1528     AddMarkerCommand *command = new AddMarkerCommand(this, oldcomment, c, id, t, true);
1529     m_commandStack->push(command);
1530 }
1531
1532 void CustomTrackView::slotDeleteClipMarker() {
1533     QList<QGraphicsItem *> itemList = scene()->selectedItems();
1534     if (itemList.count() != 1) {
1535         emit displayMessage(i18n("Cannot delete marker if more than one clip is selected"), ErrorMessage);
1536         kDebug() << "// CANNOT DELETE MARKER IF MORE TAN ONE CLIP IS SELECTED....";
1537         return;
1538     }
1539     AbstractClipItem *item = (AbstractClipItem *)itemList.at(0);
1540     if (item->type() != AVWIDGET) {
1541         emit displayMessage(i18n("No clip at cursor time"), ErrorMessage);
1542         return;
1543     }
1544     GenTime pos = GenTime(m_cursorPos, m_document->fps());
1545     if (item->startPos() > pos || item->endPos() < pos) {
1546         emit displayMessage(i18n("No selected clip at cursor time"), ErrorMessage);
1547         return;
1548     }
1549     ClipItem *clip = (ClipItem *) item;
1550     int id = clip->baseClip()->getId();
1551     GenTime position = pos - item->startPos() + item->cropStart();
1552     QString comment = clip->baseClip()->markerComment(position);
1553     if (comment.isEmpty()) {
1554         emit displayMessage(i18n("No marker found at cursor time"), ErrorMessage);
1555         return;
1556     }
1557     AddMarkerCommand *command = new AddMarkerCommand(this, comment, QString(), id, position, true);
1558     m_commandStack->push(command);
1559 }
1560
1561 void CustomTrackView::slotEditClipMarker() {
1562     QList<QGraphicsItem *> itemList = scene()->selectedItems();
1563     if (itemList.count() != 1) {
1564         emit displayMessage(i18n("Cannot edit marker if more than one clip is selected"), ErrorMessage);
1565         kDebug() << "// CANNOT DELETE MARKER IF MORE TAN ONE CLIP IS SELECTED....";
1566         return;
1567     }
1568     AbstractClipItem *item = (AbstractClipItem *)itemList.at(0);
1569     if (item->type() != AVWIDGET) {
1570         emit displayMessage(i18n("No clip at cursor time"), ErrorMessage);
1571         return;
1572     }
1573     GenTime pos = GenTime(m_cursorPos, m_document->fps());
1574     if (item->startPos() > pos || item->endPos() < pos) {
1575         emit displayMessage(i18n("No selected clip at cursor time"), ErrorMessage);
1576         return;
1577     }
1578     ClipItem *clip = (ClipItem *) item;
1579     int id = clip->baseClip()->getId();
1580     GenTime position = pos - item->startPos() + item->cropStart();
1581     QString oldcomment = clip->baseClip()->markerComment(position);
1582     if (oldcomment.isEmpty()) {
1583         emit displayMessage(i18n("No marker found at cursor time"), ErrorMessage);
1584         return;
1585     }
1586
1587     CommentedTime marker(position, oldcomment);
1588     MarkerDialog d(clip->baseClip(), marker, m_document->timecode(), this);
1589     if (d.exec() == QDialog::Accepted) {
1590         if (d.newMarker().time() == position) {
1591             // marker position was not changed, only text
1592             AddMarkerCommand *command = new AddMarkerCommand(this, oldcomment, d.newMarker().comment(), id, position, true);
1593             m_commandStack->push(command);
1594         } else {
1595             // marker text and position were changed, remove previous marker and add new one
1596             AddMarkerCommand *command1 = new AddMarkerCommand(this, oldcomment, QString(), id, position, true);
1597             AddMarkerCommand *command2 = new AddMarkerCommand(this, QString(), d.newMarker().comment(), id, d.newMarker().time(), true);
1598             m_commandStack->push(command1);
1599             m_commandStack->push(command2);
1600         }
1601     }
1602 }
1603
1604 void CustomTrackView::addMarker(const int id, const GenTime &pos, const QString comment) {
1605     DocClipBase *base = m_document->clipManager()->getClipById(id);
1606     if (!comment.isEmpty()) base->addSnapMarker(pos, comment);
1607     else base->deleteSnapMarker(pos);
1608     m_document->setModified(true);
1609     viewport()->update();
1610 }
1611
1612
1613
1614 void CustomTrackView::editGuide(const GenTime oldPos, const GenTime pos, const QString &comment) {
1615     if (oldPos > GenTime() && pos > GenTime()) {
1616         // move guide
1617         for (int i = 0; i < m_guides.count(); i++) {
1618             if (m_guides.at(i)->position() == oldPos) {
1619                 Guide *item = m_guides.at(i);
1620                 item->updateGuide(pos, comment);
1621                 break;
1622             }
1623         }
1624     } else if (pos > GenTime()) addGuide(pos, comment);
1625     else {
1626         // remove guide
1627         bool found = false;
1628         for (int i = 0; i < m_guides.count(); i++) {
1629             if (m_guides.at(i)->position() == oldPos) {
1630                 Guide *item = m_guides.takeAt(i);
1631                 delete item;
1632                 found = true;
1633                 break;
1634             }
1635         }
1636         if (!found) emit displayMessage(i18n("No guide at cursor time"), ErrorMessage);
1637     }
1638 }
1639
1640 bool CustomTrackView::addGuide(const GenTime pos, const QString &comment) {
1641     for (int i = 0; i < m_guides.count(); i++) {
1642         if (m_guides.at(i)->position() == pos) {
1643             emit displayMessage(i18n("A guide already exists at that position"), ErrorMessage);
1644             return false;
1645         }
1646     }
1647     Guide *g = new Guide(this, pos, comment, m_scale, m_document->fps(), m_tracksHeight * m_tracksList.count());
1648     scene()->addItem(g);
1649     m_guides.append(g);
1650     return true;
1651 }
1652
1653 void CustomTrackView::slotAddGuide() {
1654     CommentedTime marker(GenTime(m_cursorPos, m_document->fps()), i18n("Guide"));
1655     MarkerDialog d(NULL, marker, m_document->timecode(), this);
1656     if (d.exec() != QDialog::Accepted) return;
1657     if (addGuide(d.newMarker().time(), d.newMarker().comment())) {
1658         EditGuideCommand *command = new EditGuideCommand(this, GenTime(), QString(), d.newMarker().time(), d.newMarker().comment(), false);
1659         m_commandStack->push(command);
1660     }
1661 }
1662
1663 void CustomTrackView::slotEditGuide() {
1664     GenTime pos = GenTime(m_cursorPos, m_document->fps());
1665     bool found = false;
1666     for (int i = 0; i < m_guides.count(); i++) {
1667         if (m_guides.at(i)->position() == pos) {
1668             slotEditGuide(m_guides.at(i)->info());
1669             found = true;
1670             break;
1671         }
1672     }
1673     if (!found) emit displayMessage(i18n("No guide at cursor time"), ErrorMessage);
1674 }
1675
1676 void CustomTrackView::slotEditGuide(CommentedTime guide) {
1677     MarkerDialog d(NULL, guide, m_document->timecode(), this);
1678     if (d.exec() == QDialog::Accepted) {
1679         EditGuideCommand *command = new EditGuideCommand(this, guide.time(), guide.comment(), d.newMarker().time(), d.newMarker().comment(), true);
1680         m_commandStack->push(command);
1681     }
1682 }
1683
1684
1685 void CustomTrackView::slotDeleteGuide() {
1686     GenTime pos = GenTime(m_cursorPos, m_document->fps());
1687     bool found = false;
1688     for (int i = 0; i < m_guides.count(); i++) {
1689         if (m_guides.at(i)->position() == pos) {
1690             EditGuideCommand *command = new EditGuideCommand(this, m_guides.at(i)->position(), m_guides.at(i)->label(), GenTime(), QString(), true);
1691             m_commandStack->push(command);
1692             found = true;
1693             break;
1694         }
1695     }
1696     if (!found) emit displayMessage(i18n("No guide at cursor time"), ErrorMessage);
1697 }
1698
1699 void CustomTrackView::setTool(PROJECTTOOL tool) {
1700     m_tool = tool;
1701 }
1702
1703 void CustomTrackView::setScale(double scaleFactor) {
1704     //scale(scaleFactor, scaleFactor);
1705     m_animationTimer->stop();
1706     if (m_visualTip) {
1707         delete m_visualTip;
1708         m_visualTip = NULL;
1709     }
1710     if (m_animation) {
1711         delete m_animation;
1712         m_animation = NULL;
1713     }
1714     double pos = cursorPos() / m_scale;
1715     m_scale = scaleFactor;
1716     int vert = verticalScrollBar()->value();
1717     kDebug() << " HHHHHHHH  SCALING: " << m_scale;
1718     QList<QGraphicsItem *> itemList = items();
1719     for (int i = 0; i < itemList.count(); i++) {
1720         if (itemList.at(i)->type() == AVWIDGET || itemList.at(i)->type() == TRANSITIONWIDGET) {
1721             AbstractClipItem *clip = (AbstractClipItem *)itemList.at(i);
1722             clip->setRect(clip->startPos().frames(m_document->fps()) * m_scale, clip->rect().y(), clip->duration().frames(m_document->fps()) * m_scale, clip->rect().height());
1723         }
1724     }
1725
1726     for (int i = 0; i < m_guides.count(); i++) {
1727         m_guides.at(i)->updatePosition(m_scale);
1728     }
1729
1730     setSceneRect(0, 0, (m_projectDuration + 100) * m_scale, sceneRect().height());
1731     updateCursorPos();
1732     centerOn(QPointF(cursorPos(), m_tracksHeight));
1733     verticalScrollBar()->setValue(vert);
1734 }
1735
1736 void CustomTrackView::slotRefreshGuides() {
1737     if (KdenliveSettings::showmarkers()) {
1738         kDebug() << "// refresh GUIDES";
1739         for (int i = 0; i < m_guides.count(); i++) {
1740             m_guides.at(i)->update();
1741         }
1742     }
1743 }
1744
1745 void CustomTrackView::drawBackground(QPainter * painter, const QRectF & rect) {
1746     QRect rectInView = viewport()->rect();
1747     rectInView.moveTo(horizontalScrollBar()->value(), verticalScrollBar()->value());
1748
1749     QColor base = palette().button().color();
1750     painter->setClipRect(rect);
1751     painter->drawLine(rectInView.left(), 0, rectInView.right(), 0);
1752     uint max = m_tracksList.count();
1753     for (uint i = 0; i < max;i++) {
1754         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)));
1755         painter->drawLine(rectInView.left(), m_tracksHeight * (i + 1), rectInView.right(), m_tracksHeight * (i + 1));
1756         //painter->drawText(QRectF(10, 50 * i, 100, 50 * i + 49), Qt::AlignLeft, i18n(" Track ") + QString::number(i + 1));
1757     }
1758     int lowerLimit = m_tracksHeight * m_tracksList.count() + 1;
1759     if (height() > lowerLimit)
1760         painter->fillRect(QRectF(rectInView.left(), lowerLimit, rectInView.width(), height() - lowerLimit), QBrush(base));
1761 }
1762
1763 QDomElement CustomTrackView::xmlInfo() {
1764     QDomDocument doc;
1765     QDomElement e;
1766     QDomElement guides = doc.createElement("guides");
1767     for (int i = 0; i < m_guides.count(); i++) {
1768         e = doc.createElement("guide");
1769         e.setAttribute("time", m_guides.at(i)->position().ms() / 1000);
1770         e.setAttribute("comment", m_guides.at(i)->label());
1771         guides.appendChild(e);
1772     }
1773     return guides;
1774 }
1775
1776 bool CustomTrackView::findString(const QString &text) {
1777     QString marker;
1778     for (int i = 0; i < m_searchPoints.size(); ++i) {
1779         marker = m_searchPoints.at(i).comment();
1780         if (marker.contains(text, Qt::CaseInsensitive)) {
1781             setCursorPos(m_searchPoints.at(i).time().frames(m_document->fps()), true);
1782             int vert = verticalScrollBar()->value();
1783             int hor = cursorPos();
1784             ensureVisible(hor, vert + 10, 2, 2, 50, 0);
1785             m_findIndex = i;
1786             return true;
1787         }
1788     }
1789     return false;
1790 }
1791
1792 bool CustomTrackView::findNextString(const QString &text) {
1793     QString marker;
1794     for (int i = m_findIndex + 1; i < m_searchPoints.size(); ++i) {
1795         marker = m_searchPoints.at(i).comment();
1796         if (marker.contains(text, Qt::CaseInsensitive)) {
1797             setCursorPos(m_searchPoints.at(i).time().frames(m_document->fps()), true);
1798             int vert = verticalScrollBar()->value();
1799             int hor = cursorPos();
1800             ensureVisible(hor, vert + 10, 2, 2, 50, 0);
1801             m_findIndex = i;
1802             return true;
1803         }
1804     }
1805     m_findIndex = -1;
1806     return false;
1807 }
1808
1809 void CustomTrackView::initSearchStrings() {
1810     m_searchPoints.clear();
1811     QList<QGraphicsItem *> itemList = items();
1812     for (int i = 0; i < itemList.count(); i++) {
1813         // parse all clip names
1814         if (itemList.at(i)->type() == AVWIDGET) {
1815             ClipItem *item = static_cast <ClipItem *>(itemList.at(i));
1816             GenTime start = item->startPos();
1817             CommentedTime t(start, item->clipName());
1818             m_searchPoints.append(t);
1819             // add all clip markers
1820             QList < CommentedTime > markers = item->commentedSnapMarkers();
1821             m_searchPoints += markers;
1822         }
1823     }
1824
1825     // add guides
1826     for (int i = 0; i < m_guides.count(); i++) {
1827         m_searchPoints.append(m_guides.at(i)->info());
1828     }
1829
1830     qSort(m_searchPoints);
1831 }
1832
1833 void CustomTrackView::clearSearchStrings() {
1834     m_searchPoints.clear();
1835     m_findIndex = 0;
1836 }
1837
1838 /*
1839 void CustomTrackView::drawForeground ( QPainter * painter, const QRectF & rect )
1840 {
1841   //kDebug()<<"/////  DRAWING FB: "<<rect.x()<<", width: "<<rect.width();
1842   painter->fillRect(rect, QColor(50, rand() % 250,50,100));
1843   painter->drawLine(m_cursorPos, rect.y(), m_cursorPos, rect.y() + rect.height());
1844 }
1845 */
1846 #include "customtrackview.moc"