]> git.sesse.net Git - kdenlive/blob - src/customtrackview.cpp
1fe7b94429f786f45de84bf0da5924083d03222b
[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 "changespeedcommand.h"
50 #include "addmarkercommand.h"
51 #include "razorclipcommand.h"
52 #include "kdenlivesettings.h"
53 #include "transition.h"
54 #include "clipitem.h"
55 #include "customtrackview.h"
56 #include "clipmanager.h"
57 #include "renderer.h"
58 #include "markerdialog.h"
59 #include "mainwindow.h"
60 #include "ui_keyframedialog_ui.h"
61 #include "clipdurationdialog.h"
62
63
64 //TODO:
65 // disable animation if user asked it in KDE's global settings
66 // http://lists.kde.org/?l=kde-commits&m=120398724717624&w=2
67 // needs something like below (taken from dolphin)
68 // #include <kglobalsettings.h>
69 // const bool animate = KGlobalSettings::graphicEffectsLevel() & KGlobalSettings::SimpleAnimationEffects;
70 // const int duration = animate ? 1500 : 1;
71
72 CustomTrackView::CustomTrackView(KdenliveDoc *doc, QGraphicsScene * projectscene, QWidget *parent)
73         : QGraphicsView(projectscene, parent), m_cursorPos(0), m_dropItem(NULL), m_cursorLine(NULL), m_operationMode(NONE), m_dragItem(NULL), m_visualTip(NULL), m_moveOpMode(NONE), m_animation(NULL), m_projectDuration(0), m_scale(1.0), m_clickPoint(QPoint()), m_document(doc), m_autoScroll(KdenliveSettings::autoscroll()), m_tracksHeight(KdenliveSettings::trackheight()), m_tool(SELECTTOOL), m_dragGuide(NULL), m_findIndex(0), m_menuPosition(QPoint()) {
74     if (doc) m_commandStack = doc->commandStack();
75     else m_commandStack == NULL;
76     setMouseTracking(true);
77     setAcceptDrops(true);
78     m_animationTimer = new QTimeLine(800);
79     m_animationTimer->setFrameRange(0, 5);
80     m_animationTimer->setUpdateInterval(100);
81     m_animationTimer->setLoopCount(0);
82     m_tipColor = QColor(0, 192, 0, 200);
83     QColor border = QColor(255, 255, 255, 100);
84     m_tipPen.setColor(border);
85     m_tipPen.setWidth(3);
86     setContentsMargins(0, 0, 0, 0);
87     if (projectscene) {
88         m_cursorLine = projectscene->addLine(0, 0, 0, m_tracksHeight);
89         m_cursorLine->setFlags(QGraphicsItem::ItemIsMovable);
90         m_cursorLine->setZValue(1000);
91     }
92
93     KIcon razorIcon("edit-cut");
94     m_razorCursor = QCursor(razorIcon.pixmap(22, 22));
95     verticalScrollBar()->setTracking(true);
96     connect(verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(slotRefreshGuides()));
97 }
98
99 CustomTrackView::~CustomTrackView() {
100     qDeleteAll(m_guides);
101 }
102
103
104 void CustomTrackView::setContextMenu(QMenu *timeline, QMenu *clip, QMenu *transition) {
105     m_timelineContextMenu = timeline;
106     m_timelineContextClipMenu = clip;
107     m_timelineContextTransitionMenu = transition;
108 }
109
110 void CustomTrackView::checkAutoScroll() {
111     m_autoScroll = KdenliveSettings::autoscroll();
112 }
113
114 QList <TrackInfo> CustomTrackView::tracksList() const {
115     return m_tracksList;
116 }
117
118 void CustomTrackView::checkTrackHeight() {
119     if (m_tracksHeight == KdenliveSettings::trackheight()) return;
120     m_tracksHeight = KdenliveSettings::trackheight();
121     emit trackHeightChanged();
122     QList<QGraphicsItem *> itemList = items();
123     ClipItem *item;
124     Transition *transitionitem;
125     for (int i = 0; i < itemList.count(); i++) {
126         if (itemList.at(i)->type() == AVWIDGET) {
127             item = (ClipItem*) itemList.at(i);
128             item->setRect(item->rect().x(), item->track() * m_tracksHeight, item->rect().width(), m_tracksHeight - 1);
129             item->resetThumbs();
130         } else if (itemList.at(i)->type() == TRANSITIONWIDGET) {
131             transitionitem = (Transition*) itemList.at(i);
132             transitionitem->setRect(transitionitem->rect().x(), transitionitem->track() * m_tracksHeight + m_tracksHeight / 2, transitionitem->rect().width(), m_tracksHeight - 1);
133         }
134     }
135     m_cursorLine->setLine(m_cursorLine->line().x1(), 0, m_cursorLine->line().x1(), m_tracksHeight * m_tracksList.count());
136
137     for (int i = 0; i < m_guides.count(); i++) {
138         QLineF l = m_guides.at(i)->line();
139         l.setP2(QPointF(l.x2(), m_tracksHeight * m_tracksList.count()));
140         m_guides.at(i)->setLine(l);
141     }
142
143     setSceneRect(0, 0, sceneRect().width(), m_tracksHeight * m_tracksList.count());
144     verticalScrollBar()->setMaximum(m_tracksHeight * m_tracksList.count());
145     update();
146 }
147
148 // virtual
149 void CustomTrackView::resizeEvent(QResizeEvent * event) {
150     QGraphicsView::resizeEvent(event);
151 }
152
153 // virtual
154 void CustomTrackView::wheelEvent(QWheelEvent * e) {
155     if (e->modifiers() == Qt::ControlModifier) {
156         if (e->delta() > 0) emit zoomIn();
157         else emit zoomOut();
158     } else {
159         if (e->delta() > 0) horizontalScrollBar()->setValue(horizontalScrollBar()->value() + horizontalScrollBar()->singleStep());
160         else  horizontalScrollBar()->setValue(horizontalScrollBar()->value() - horizontalScrollBar()->singleStep());
161     }
162 }
163
164 int CustomTrackView::getPreviousVideoTrack(int track) {
165     track = m_tracksList.count() - track - 1;
166     track --;
167     for (int i = track; i > -1; i--) {
168         if (m_tracksList.at(i).type == VIDEOTRACK) return i + 1;
169     }
170     return 0;
171 }
172
173 // virtual
174
175 void CustomTrackView::mouseMoveEvent(QMouseEvent * event) {
176     int pos = event->x();
177     emit mousePosition((int)(mapToScene(event->pos()).x() / m_scale));
178     if (event->buttons() & Qt::MidButton) return;
179     if (event->buttons() != Qt::NoButton) {
180         if (m_dragItem && m_tool == SELECTTOOL) { //event->button() == Qt::LeftButton) {
181             // a button was pressed, delete visual tips
182             if (m_operationMode == MOVE && (event->pos() - m_clickEvent).manhattanLength() >= QApplication::startDragDistance()) {
183                 double snappedPos = getSnapPointForPos(mapToScene(event->pos()).x() - m_clickPoint.x());
184                 //kDebug() << "///////  MOVE CLIP, EVENT Y: "<<m_clickPoint.y();//<<event->scenePos().y()<<", SCENE HEIGHT: "<<scene()->sceneRect().height();
185                 int moveTrack = (int)  mapToScene(event->pos() - QPoint(0, (m_dragItem->type() == TRANSITIONWIDGET ? m_dragItem->boundingRect().height() / 2 : 0))).y() / m_tracksHeight;
186                 int currentTrack = m_dragItem->track();
187
188                 if (moveTrack > 1000) moveTrack = 0;
189                 else if (moveTrack > m_tracksList.count() - 1) moveTrack = m_tracksList.count() - 1;
190                 else if (moveTrack < 0) moveTrack = 0;
191
192                 int offset = moveTrack - currentTrack;
193                 if (m_selectedClipList.count() == 1) m_dragItem->moveTo((int)(snappedPos / m_scale), m_scale, offset * m_tracksHeight, moveTrack);
194                 else {
195                     int moveOffset = (int)(snappedPos / m_scale) - m_dragItem->startPos().frames(m_document->fps());
196                     if (canBeMoved(m_selectedClipList, GenTime(moveOffset, m_document->fps()), offset)) {
197                         for (int i = 0; i < m_selectedClipList.count(); i++) {
198                             AbstractClipItem *item = m_selectedClipList.at(i);
199                             item->moveTo(item->startPos().frames(m_document->fps()) + moveOffset, m_scale, offset * m_tracksHeight, item->track() + offset, false);
200                         }
201                     }
202                 }
203
204             } else if (m_operationMode == RESIZESTART) {
205                 double snappedPos = getSnapPointForPos(mapToScene(event->pos()).x());
206                 m_dragItem->resizeStart((int)(snappedPos / m_scale), m_scale);
207             } else if (m_operationMode == RESIZEEND) {
208                 double snappedPos = getSnapPointForPos(mapToScene(event->pos()).x());
209                 m_dragItem->resizeEnd((int)(snappedPos / m_scale), m_scale);
210             } else if (m_operationMode == FADEIN) {
211                 int pos = (int)(mapToScene(event->pos()).x() / m_scale);
212                 ((ClipItem*) m_dragItem)->setFadeIn((int)(pos - m_dragItem->startPos().frames(m_document->fps())), m_scale);
213             } else if (m_operationMode == FADEOUT) {
214                 int pos = (int)(mapToScene(event->pos()).x() / m_scale);
215                 ((ClipItem*) m_dragItem)->setFadeOut((int)(m_dragItem->endPos().frames(m_document->fps()) - pos), m_scale);
216             } else if (m_operationMode == KEYFRAME) {
217                 GenTime keyFramePos = GenTime((int)(mapToScene(event->pos()).x() / m_scale), m_document->fps()) - m_dragItem->startPos() + m_dragItem->cropStart();
218                 double pos = mapToScene(event->pos()).toPoint().y();
219                 QRectF br = m_dragItem->rect();
220                 double maxh = 100.0 / br.height();
221                 pos = (br.bottom() - pos) * maxh;
222                 m_dragItem->updateKeyFramePos(keyFramePos, pos);
223             }
224
225             if (m_animation) delete m_animation;
226             m_animation = NULL;
227             if (m_visualTip) delete m_visualTip;
228             m_visualTip = NULL;
229             QGraphicsView::mouseMoveEvent(event);
230             return;
231         } else if (m_operationMode == MOVEGUIDE) {
232             if (m_animation) delete m_animation;
233             m_animation = NULL;
234             if (m_visualTip) delete m_visualTip;
235             m_visualTip = NULL;
236             QGraphicsView::mouseMoveEvent(event);
237             return;
238         }
239     }
240
241     if (m_tool == RAZORTOOL) {
242         setCursor(m_razorCursor);
243         //QGraphicsView::mouseMoveEvent(event);
244         //return;
245     }
246
247     QList<QGraphicsItem *> itemList = items(event->pos());
248     QGraphicsRectItem *item = NULL;
249     OPERATIONTYPE opMode = NONE;
250
251     if (itemList.count() == 1 && itemList.at(0)->type() == GUIDEITEM) {
252         opMode = MOVEGUIDE;
253     } else for (int i = 0; i < itemList.count(); i++) {
254             if (itemList.at(i)->type() == AVWIDGET || itemList.at(i)->type() == TRANSITIONWIDGET) {
255                 item = (QGraphicsRectItem*) itemList.at(i);
256                 break;
257             }
258         }
259
260     if (item && event->buttons() == Qt::NoButton) {
261         AbstractClipItem *clip = (AbstractClipItem*) item;
262         if (m_tool == RAZORTOOL) {
263             // razor tool over a clip, display current frame in monitor
264             if (item->type() == AVWIDGET) {
265                 emit showClipFrame(((ClipItem *) item)->baseClip(), mapToScene(event->pos()).x() / m_scale - (clip->startPos() - clip->cropStart()).frames(m_document->fps()));
266             }
267             QGraphicsView::mouseMoveEvent(event);
268             return;
269         }
270         opMode = clip->operationMode(mapToScene(event->pos()), m_scale);
271         double size = 8;
272         if (opMode == m_moveOpMode) {
273             QGraphicsView::mouseMoveEvent(event);
274             return;
275         } else {
276             if (m_visualTip) {
277                 if (m_animation) delete m_animation;
278                 m_animation = NULL;
279                 m_animationTimer->stop();
280                 delete m_visualTip;
281                 m_visualTip = NULL;
282             }
283         }
284         m_moveOpMode = opMode;
285         if (opMode == MOVE) {
286             setCursor(Qt::OpenHandCursor);
287         } else if (opMode == RESIZESTART) {
288             setCursor(KCursor("left_side", Qt::SizeHorCursor));
289             if (m_visualTip == NULL) {
290                 QPolygon polygon;
291                 polygon << QPoint((int)clip->rect().x(), (int)(clip->rect().y() + clip->rect().height() / 2 - size * 2));
292                 polygon << QPoint((int)(clip->rect().x() + size * 2), (int)(clip->rect().y() + clip->rect().height() / 2));
293                 polygon << QPoint((int)clip->rect().x(), (int)(clip->rect().y() + clip->rect().height() / 2 + size * 2));
294                 polygon << QPoint((int)clip->rect().x(), (int)(clip->rect().y() + clip->rect().height() / 2 - size * 2));
295
296                 m_visualTip = new QGraphicsPolygonItem(polygon);
297                 ((QGraphicsPolygonItem*) m_visualTip)->setBrush(m_tipColor);
298                 ((QGraphicsPolygonItem*) m_visualTip)->setPen(m_tipPen);
299                 m_visualTip->setZValue(100);
300                 m_animation = new QGraphicsItemAnimation;
301                 m_animation->setItem(m_visualTip);
302                 m_animation->setTimeLine(m_animationTimer);
303                 m_visualTip->setPos(0, 0);
304                 double scale = 2.0;
305                 m_animation->setScaleAt(.5, scale, 1);
306                 m_animation->setPosAt(.5, QPointF(clip->rect().x() - clip->rect().x() * scale, 0));
307                 scale = 1.0;
308                 m_animation->setScaleAt(1, scale, 1);
309                 m_animation->setPosAt(1, QPointF(clip->rect().x() - clip->rect().x() * scale, 0));
310                 scene()->addItem(m_visualTip);
311                 m_animationTimer->start();
312             }
313         } else if (opMode == RESIZEEND) {
314             setCursor(KCursor("right_side", Qt::SizeHorCursor));
315             if (m_visualTip == NULL) {
316                 QPolygon polygon;
317                 polygon << QPoint((int)(clip->rect().x() + clip->rect().width()), (int)(clip->rect().y() + clip->rect().height() / 2 - size * 2));
318                 polygon << QPoint((int)(clip->rect().x() + clip->rect().width() - size * 2), (int)(clip->rect().y() + clip->rect().height() / 2));
319                 polygon << QPoint((int)(clip->rect().x() + clip->rect().width()), (int)(clip->rect().y() + clip->rect().height() / 2 + size * 2));
320                 polygon << QPoint((int)(clip->rect().x() + clip->rect().width()), (int)(clip->rect().y() + clip->rect().height() / 2 - size * 2));
321
322                 m_visualTip = new QGraphicsPolygonItem(polygon);
323                 ((QGraphicsPolygonItem*) m_visualTip)->setBrush(m_tipColor);
324                 ((QGraphicsPolygonItem*) m_visualTip)->setPen(m_tipPen);
325
326                 m_visualTip->setZValue(100);
327                 m_animation = new QGraphicsItemAnimation;
328                 m_animation->setItem(m_visualTip);
329                 m_animation->setTimeLine(m_animationTimer);
330                 m_visualTip->setPos(0, 0);
331                 double scale = 2.0;
332                 m_animation->setScaleAt(.5, scale, 1);
333                 m_animation->setPosAt(.5, QPointF(clip->rect().x() - clip->rect().x() * scale - clip->rect().width(), 0));
334                 scale = 1.0;
335                 m_animation->setScaleAt(1, scale, 1);
336                 m_animation->setPosAt(1, QPointF(clip->rect().x() - clip->rect().x() * scale, 0));
337                 scene()->addItem(m_visualTip);
338                 m_animationTimer->start();
339             }
340         } else if (opMode == FADEIN) {
341             if (m_visualTip == NULL) {
342                 ClipItem *item = (ClipItem *) clip;
343                 m_visualTip = new QGraphicsEllipseItem(item->rect().x() + item->fadeIn() * m_scale - size, item->rect().y() - 8, size * 2, 16);
344                 ((QGraphicsEllipseItem*) m_visualTip)->setBrush(m_tipColor);
345                 ((QGraphicsEllipseItem*) m_visualTip)->setPen(m_tipPen);
346                 m_visualTip->setZValue(100);
347                 m_animation = new QGraphicsItemAnimation;
348                 m_animation->setItem(m_visualTip);
349                 m_animation->setTimeLine(m_animationTimer);
350                 m_visualTip->setPos(0, 0);
351                 double scale = 2.0;
352                 m_animation->setScaleAt(.5, scale, scale);
353                 m_animation->setPosAt(.5, QPointF(item->rect().x() - item->rect().x() * scale -  item->fadeIn() * m_scale, item->rect().y() - item->rect().y() * scale));
354                 scale = 1.0;
355                 m_animation->setScaleAt(1, scale, scale);
356                 m_animation->setPosAt(1, QPointF(item->rect().x() - item->rect().x() * scale, item->rect().y() - item->rect().y() * scale));
357                 scene()->addItem(m_visualTip);
358                 m_animationTimer->start();
359             }
360             setCursor(Qt::PointingHandCursor);
361         } else if (opMode == FADEOUT) {
362             if (m_visualTip == NULL) {
363                 ClipItem *item = (ClipItem *) clip;
364                 m_visualTip = new QGraphicsEllipseItem(item->rect().x() + item->rect().width() - item->fadeOut() * m_scale - size, item->rect().y() - 8, size*2, 16);
365                 ((QGraphicsEllipseItem*) m_visualTip)->setBrush(m_tipColor);
366                 ((QGraphicsEllipseItem*) m_visualTip)->setPen(m_tipPen);
367                 m_visualTip->setZValue(100);
368                 m_animation = new QGraphicsItemAnimation;
369                 m_animation->setItem(m_visualTip);
370                 m_animation->setTimeLine(m_animationTimer);
371                 m_visualTip->setPos(0, 0);
372                 double scale = 2.0;
373                 m_animation->setScaleAt(.5, scale, scale);
374                 m_animation->setPosAt(.5, QPointF(item->rect().x() - item->rect().x() * scale - item->rect().width() + item->fadeOut() * m_scale, item->rect().y() - item->rect().y() * scale));
375                 scale = 1.0;
376                 m_animation->setScaleAt(1, scale, scale);
377                 m_animation->setPosAt(1, QPointF(item->rect().x() - item->rect().x() * scale, item->rect().y() - item->rect().y() * scale));
378                 scene()->addItem(m_visualTip);
379                 m_animationTimer->start();
380             }
381             setCursor(Qt::PointingHandCursor);
382         } else if (opMode == TRANSITIONSTART) {
383             if (m_visualTip == NULL) {
384                 m_visualTip = new QGraphicsEllipseItem(-5, -5 , 10, 10);
385                 ((QGraphicsEllipseItem*) m_visualTip)->setBrush(m_tipColor);
386                 ((QGraphicsEllipseItem*) m_visualTip)->setPen(m_tipPen);
387                 m_visualTip->setZValue(100);
388                 m_animation = new QGraphicsItemAnimation;
389                 m_animation->setItem(m_visualTip);
390                 m_animation->setTimeLine(m_animationTimer);
391                 m_visualTip->setPos(clip->rect().x() + 10, clip->rect().y() + clip->rect().height() / 2 + 12);
392                 double scale = 2.0;
393                 m_animation->setScaleAt(.5, scale, scale);
394                 scale = 1.0;
395                 m_animation->setScaleAt(1, scale, scale);
396                 scene()->addItem(m_visualTip);
397                 m_animationTimer->start();
398             }
399             setCursor(Qt::PointingHandCursor);
400         } else if (opMode == TRANSITIONEND) {
401             if (m_visualTip == NULL) {
402                 m_visualTip = new QGraphicsEllipseItem(-5, -5 , 10, 10);
403                 ((QGraphicsEllipseItem*) m_visualTip)->setBrush(m_tipColor);
404                 ((QGraphicsEllipseItem*) m_visualTip)->setPen(m_tipPen);
405                 m_visualTip->setZValue(100);
406                 m_animation = new QGraphicsItemAnimation;
407                 m_animation->setItem(m_visualTip);
408                 m_animation->setTimeLine(m_animationTimer);
409                 m_visualTip->setPos(clip->rect().x() + clip->rect().width() - 10 , clip->rect().y() + clip->rect().height() / 2 + 12);
410                 double scale = 2.0;
411                 m_animation->setScaleAt(.5, scale, scale);
412                 scale = 1.0;
413                 m_animation->setScaleAt(1, scale, scale);
414                 scene()->addItem(m_visualTip);
415                 m_animationTimer->start();
416             }
417             setCursor(Qt::PointingHandCursor);
418         } else if (opMode == KEYFRAME) {
419             setCursor(Qt::PointingHandCursor);
420         }
421     } // no clip under mouse
422     else if (m_tool == RAZORTOOL) {
423         QGraphicsView::mouseMoveEvent(event);
424         return;
425     } else if (opMode == MOVEGUIDE) {
426         m_moveOpMode = opMode;
427         setCursor(Qt::SplitHCursor);
428     } else {
429         m_moveOpMode = NONE;
430         if (event->buttons() != Qt::NoButton && event->modifiers() == Qt::NoModifier) {
431             setCursorPos((int)(mapToScene(event->pos().x(), 0).x() / m_scale));
432         }
433         if (m_visualTip) {
434             if (m_animation) delete m_animation;
435             m_animationTimer->stop();
436             m_animation = NULL;
437             delete m_visualTip;
438             m_visualTip = NULL;
439
440         }
441         setCursor(Qt::ArrowCursor);
442     }
443     QGraphicsView::mouseMoveEvent(event);
444 }
445
446 // virtual
447 void CustomTrackView::mousePressEvent(QMouseEvent * event) {
448     m_menuPosition = QPoint();
449     activateMonitor();
450     m_clickEvent = event->pos();
451     QList<QGraphicsItem *> collisionList = items(event->pos());
452     if (event->button() == Qt::MidButton) {
453         m_document->renderer()->switchPlay();
454         return;
455     }
456     if (event->modifiers() == Qt::ControlModifier && collisionList.count() == 0) {
457         setDragMode(QGraphicsView::ScrollHandDrag);
458         QGraphicsView::mousePressEvent(event);
459         return;
460     } else if (event->modifiers() == Qt::ShiftModifier && collisionList.count() == 0) {
461         setDragMode(QGraphicsView::RubberBandDrag);
462         QGraphicsView::mousePressEvent(event);
463         return;
464     } else {
465         bool collision = false;
466         if (collisionList.count() == 1 && collisionList.at(0)->type() == GUIDEITEM) {
467             // a guide item was pressed
468             collisionList.at(0)->setFlag(QGraphicsItem::ItemIsMovable, true);
469             m_dragItem = NULL;
470             m_dragGuide = (Guide *) collisionList.at(0);
471             collision = true;
472             m_operationMode = MOVEGUIDE;
473             // deselect all clips so that only the guide will move
474             QList<QGraphicsItem *> clips = scene()->selectedItems();
475             for (int i = 0; i < clips.count(); ++i)
476                 clips.at(i)->setSelected(false);
477             updateSnapPoints(NULL);
478             QGraphicsView::mousePressEvent(event);
479         } else for (int i = 0; i < collisionList.count(); ++i) {
480                 QGraphicsItem *item = collisionList.at(i);
481                 if (item->type() == AVWIDGET || item->type() == TRANSITIONWIDGET) {
482                     if (m_tool == RAZORTOOL) {
483                         if (item->type() == TRANSITIONWIDGET) {
484                             emit displayMessage(i18n("Cannot cut a transition"), ErrorMessage);
485                             return;
486                         }
487                         AbstractClipItem *clip = (AbstractClipItem *) item;
488                         ItemInfo info;
489                         info.startPos = clip->startPos();
490                         info.endPos = clip->endPos();
491                         info.track = clip->track();
492                         RazorClipCommand* command = new RazorClipCommand(this, info, GenTime((int)(mapToScene(event->pos()).x() / m_scale), m_document->fps()), true);
493                         m_commandStack->push(command);
494                         m_document->setModified(true);
495                         return;
496                     }
497                     // select item
498                     if (!item->isSelected()) {
499
500                         if (event->modifiers() != Qt::ControlModifier) {
501                             QList<QGraphicsItem *> itemList = items();
502                             for (int i = 0; i < itemList.count(); i++) {
503                                 itemList.at(i)->setSelected(false);
504                                 itemList.at(i)->update();
505                             }
506                         }
507                         item->setSelected(true);
508                         item->update();
509                     }
510
511                     m_dragItem = (AbstractClipItem *) item;
512
513                     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()));
514                     m_dragItemInfo.startPos = m_dragItem->startPos();
515                     m_dragItemInfo.endPos = m_dragItem->endPos();
516                     m_dragItemInfo.track = m_dragItem->track();
517
518                     m_selectedClipList.clear();
519                     QList<QGraphicsItem *> selected = scene()->selectedItems();
520                     for (int i = 0; i < selected.count(); i++) {
521                         if (selected.at(i)->type() == AVWIDGET || selected.at(i)->type() == TRANSITIONWIDGET)
522                             m_selectedClipList.append(static_cast <AbstractClipItem *>(selected.at(i)));
523                     }
524
525                     m_operationMode = m_dragItem->operationMode(item->mapFromScene(mapToScene(event->pos())), m_scale);
526                     if (m_operationMode == KEYFRAME) {
527                         m_dragItem->updateSelectedKeyFrame();
528                         return;
529                     } else if (m_operationMode == MOVE) setCursor(Qt::ClosedHandCursor);
530                     else if (m_operationMode == TRANSITIONSTART) {
531                         ItemInfo info;
532                         info.startPos = m_dragItem->startPos();
533                         info.track = m_dragItem->track();
534                         int transitiontrack = getPreviousVideoTrack(info.track);
535                         ClipItem *transitionClip = NULL;
536                         if (transitiontrack != 0) transitionClip = getClipItemAt((int) info.startPos.frames(m_document->fps()), m_tracksList.count() - transitiontrack);
537                         if (transitionClip && transitionClip->endPos() < m_dragItem->endPos()) {
538                             info.endPos = transitionClip->endPos();
539                         } else info.endPos = info.startPos + GenTime(2.5);
540
541                         slotAddTransition((ClipItem *) m_dragItem, info, transitiontrack);
542                     } else if (m_operationMode == TRANSITIONEND) {
543                         ItemInfo info;
544                         info.endPos = m_dragItem->endPos();
545                         info.track = m_dragItem->track();
546                         int transitiontrack = getPreviousVideoTrack(info.track);
547                         ClipItem *transitionClip = NULL;
548                         if (transitiontrack != 0) transitionClip = getClipItemAt((int) info.endPos.frames(m_document->fps()), m_tracksList.count() - transitiontrack);
549                         if (transitionClip && transitionClip->startPos() > m_dragItem->startPos()) {
550                             info.startPos = transitionClip->startPos();
551                         } else info.startPos = info.endPos - GenTime(2.5);
552                         slotAddTransition((ClipItem *) m_dragItem, info, transitiontrack);
553                     }
554                     updateSnapPoints(m_dragItem);
555                     collision = true;
556                     break;
557                 }
558             }
559         if (!collision) {
560             kDebug() << "//////// NO ITEM FOUND ON CLICK";
561             m_dragItem = NULL;
562             setCursor(Qt::ArrowCursor);
563             QList<QGraphicsItem *> itemList = items();
564             for (int i = 0; i < itemList.count(); i++)
565                 itemList.at(i)->setSelected(false);
566             //emit clipItemSelected(NULL);
567             if (event->button() == Qt::RightButton) {
568                 displayContextMenu(event->globalPos());
569                 m_menuPosition = event->pos();
570             } else setCursorPos((int)(mapToScene(event->x(), 0).x() / m_scale));
571         } else if (event->button() == Qt::RightButton) {
572             m_operationMode = NONE;
573             displayContextMenu(event->globalPos(), m_dragItem);
574             m_dragItem = NULL;
575         }
576         if (m_dragItem && m_dragItem->type() == AVWIDGET) emit clipItemSelected((ClipItem*) m_dragItem);
577         else emit clipItemSelected(NULL);
578     }
579     //kDebug()<<pos;
580     //QGraphicsView::mousePressEvent(event);
581 }
582
583 void CustomTrackView::mouseDoubleClickEvent(QMouseEvent *event) {
584     if (m_dragItem && m_dragItem->hasKeyFrames()) {
585         if (m_moveOpMode == KEYFRAME) {
586             // user double clicked on a keyframe, open edit dialog
587             QDialog d(parentWidget());
588             Ui::KeyFrameDialog_UI view;
589             view.setupUi(&d);
590             view.kfr_position->setText(m_document->timecode().getTimecode(GenTime(m_dragItem->selectedKeyFramePos(), m_document->fps()) - m_dragItem->cropStart(), m_document->fps()));
591             view.kfr_value->setValue(m_dragItem->selectedKeyFrameValue());
592             view.kfr_value->setFocus();
593             if (d.exec() == QDialog::Accepted) {
594                 int pos = m_document->timecode().getFrameCount(view.kfr_position->text(), m_document->fps());
595                 m_dragItem->updateKeyFramePos(GenTime(pos, m_document->fps()) + m_dragItem->cropStart(), (double) view.kfr_value->value() * m_dragItem->keyFrameFactor());
596                 ClipItem *item = (ClipItem *)m_dragItem;
597                 QString previous = item->keyframes(item->selectedEffectIndex());
598                 item->updateKeyframeEffect();
599                 QString next = item->keyframes(item->selectedEffectIndex());
600                 EditKeyFrameCommand *command = new EditKeyFrameCommand(this, item->track(), item->startPos(), item->selectedEffectIndex(), previous, next, false);
601                 m_commandStack->push(command);
602                 updateEffect(m_tracksList.count() - item->track(), item->startPos(), item->selectedEffect(), item->selectedEffectIndex());
603             }
604
605         } else  {
606             // add keyframe
607             GenTime keyFramePos = GenTime((int)(mapToScene(event->pos()).x() / m_scale), m_document->fps()) - m_dragItem->startPos() + m_dragItem->cropStart();
608             m_dragItem->addKeyFrame(keyFramePos, mapToScene(event->pos()).toPoint().y());
609             ClipItem * item = (ClipItem *) m_dragItem;
610             QString previous = item->keyframes(item->selectedEffectIndex());
611             item->updateKeyframeEffect();
612             QString next = item->keyframes(item->selectedEffectIndex());
613             EditKeyFrameCommand *command = new EditKeyFrameCommand(this, m_dragItem->track(), m_dragItem->startPos(), item->selectedEffectIndex(), previous, next, false);
614             m_commandStack->push(command);
615             updateEffect(m_tracksList.count() - item->track(), item->startPos(), item->selectedEffect(), item->selectedEffectIndex());
616         }
617     } else if (m_dragItem) {
618         ClipDurationDialog d(m_dragItem, m_document->timecode(), this);
619         if (d.exec() == QDialog::Accepted) {
620             if (d.startPos() != m_dragItem->startPos()) {
621                 if (m_dragItem->type() == AVWIDGET) {
622                     ItemInfo startInfo;
623                     startInfo.startPos = m_dragItem->startPos();
624                     startInfo.endPos = m_dragItem->endPos();
625                     startInfo.track = m_dragItem->track();
626                     ItemInfo endInfo;
627                     endInfo.startPos = d.startPos();
628                     endInfo.endPos = m_dragItem->endPos() + (endInfo.startPos - startInfo.startPos);
629                     endInfo.track = m_dragItem->track();
630                     MoveClipCommand *command = new MoveClipCommand(this, startInfo, endInfo, true);
631                     m_commandStack->push(command);
632                 } else {
633                     //TODO: move transition
634                 }
635             }
636             if (d.duration() != m_dragItem->duration()) {
637                 if (m_dragItem->type() == AVWIDGET) {
638                     ItemInfo startInfo;
639                     startInfo.startPos = m_dragItem->startPos();
640                     startInfo.endPos = m_dragItem->endPos();
641                     startInfo.track = m_dragItem->track();
642                     ItemInfo endInfo;
643                     endInfo.startPos = startInfo.startPos;
644                     endInfo.endPos = endInfo.startPos + d.duration();
645                     endInfo.track = m_dragItem->track();
646                     ResizeClipCommand *command = new ResizeClipCommand(this, startInfo, endInfo, true);
647                     m_commandStack->push(command);
648                 } else {
649                     //TODO: resize transition
650                 }
651             }
652         }
653     } else {
654         QList<QGraphicsItem *> collisionList = items(event->pos());
655         if (collisionList.count() == 1 && collisionList.at(0)->type() == GUIDEITEM) {
656             Guide *editGuide = (Guide *) collisionList.at(0);
657             if (editGuide) slotEditGuide(editGuide->info());
658         }
659     }
660 }
661
662
663 void CustomTrackView::editKeyFrame(const GenTime pos, const int track, const int index, const QString keyframes) {
664     ClipItem *clip = getClipItemAt((int)pos.frames(m_document->fps()), track);
665     if (clip) {
666         clip->setKeyframes(index, keyframes);
667         updateEffect(m_tracksList.count() - clip->track(), clip->startPos(), clip->effectAt(index), index);
668     } else emit displayMessage(i18n("Cannot find clip with keyframe"), ErrorMessage);
669 }
670
671
672 void CustomTrackView::displayContextMenu(QPoint pos, AbstractClipItem *clip) {
673     if (clip == NULL) m_timelineContextMenu->popup(pos);
674     else if (clip->type() == AVWIDGET) m_timelineContextClipMenu->popup(pos);
675     else if (clip->type() == TRANSITIONWIDGET) m_timelineContextTransitionMenu->popup(pos);
676 }
677
678 void CustomTrackView::activateMonitor() {
679     emit activateDocumentMonitor();
680 }
681
682 void CustomTrackView::dragEnterEvent(QDragEnterEvent * event) {
683     if (event->mimeData()->hasFormat("kdenlive/producerslist")) {
684         kDebug() << "///////////////  DRAG ENTERED, TEXT: " << event->mimeData()->data("kdenlive/producerslist");
685         QStringList ids = QString(event->mimeData()->data("kdenlive/producerslist")).split(";");
686         //TODO: drop of several clips
687         for (int i = 0; i < ids.size(); ++i) {
688         }
689         DocClipBase *clip = m_document->getBaseClip(ids.at(0).toInt());
690         if (clip == NULL) kDebug() << " WARNING))))))))) CLIP NOT FOUND : " << ids.at(0).toInt();
691         addItem(clip, event->pos());
692         event->acceptProposedAction();
693     } else QGraphicsView::dragEnterEvent(event);
694 }
695
696 void CustomTrackView::slotRefreshEffects(ClipItem *clip) {
697     int track = m_tracksList.count() - clip->track();
698     GenTime pos = clip->startPos();
699     if (!m_document->renderer()->mltRemoveEffect(track, pos, "-1", false)) {
700         emit displayMessage(i18n("Problem deleting effect"), ErrorMessage);
701         return;
702     }
703     bool success = true;
704     for (int i = 0; i < clip->effectsCount(); i++) {
705         if (!m_document->renderer()->mltAddEffect(track, pos, clip->getEffectArgs(clip->effectAt(i)), false)) success = false;
706     }
707     if (!success) emit displayMessage(i18n("Problem adding effect to clip"), ErrorMessage);
708     m_document->renderer()->doRefresh();
709 }
710
711 void CustomTrackView::addEffect(int track, GenTime pos, QDomElement effect) {
712     ClipItem *clip = getClipItemAt((int)pos.frames(m_document->fps()) + 1, m_tracksList.count() - track);
713     if (clip) {
714         QMap <QString, QString> effectParams = clip->addEffect(effect);
715         if (!m_document->renderer()->mltAddEffect(track, pos, effectParams))
716             emit displayMessage(i18n("Problem adding effect to clip"), ErrorMessage);
717         emit clipItemSelected(clip);
718     } else emit displayMessage(i18n("Cannot find clip to add effect"), ErrorMessage);
719 }
720
721 void CustomTrackView::deleteEffect(int track, GenTime pos, QDomElement effect) {
722     QString index = effect.attribute("kdenlive_ix");
723     if (effect.attribute("disabled") != "1" && !m_document->renderer()->mltRemoveEffect(track, pos, index)) {
724         emit displayMessage(i18n("Problem deleting effect"), ErrorMessage);
725         return;
726     }
727     ClipItem *clip = getClipItemAt((int)pos.frames(m_document->fps()) + 1, m_tracksList.count() - track);
728     if (clip) {
729         clip->deleteEffect(index);
730         emit clipItemSelected(clip);
731     }
732 }
733
734 void CustomTrackView::slotAddEffect(QDomElement effect, GenTime pos, int track) {
735     QList<QGraphicsItem *> itemList;
736     if (track == -1) itemList = scene()->selectedItems();
737     if (itemList.isEmpty()) {
738         ClipItem *clip = getClipItemAt((int)pos.frames(m_document->fps()) + 1, track);
739         if (clip) itemList.append(clip);
740         else emit displayMessage(i18n("Select a clip if you want to apply an effect"), ErrorMessage);
741     }
742     kDebug() << "// REQUESTING EFFECT ON CLIP: " << pos.frames(25) << ", TRK: " << track << "SELECTED ITEMS: " << itemList.count();
743     for (int i = 0; i < itemList.count(); i++) {
744         if (itemList.at(i)->type() == AVWIDGET) {
745             ClipItem *item = (ClipItem *)itemList.at(i);
746             item->initEffect(effect);
747             AddEffectCommand *command = new AddEffectCommand(this, m_tracksList.count() - item->track(), item->startPos(), effect, true);
748             m_commandStack->push(command);
749         }
750     }
751     m_document->setModified(true);
752 }
753
754 void CustomTrackView::slotDeleteEffect(ClipItem *clip, QDomElement effect) {
755     AddEffectCommand *command = new AddEffectCommand(this, m_tracksList.count() - clip->track(), clip->startPos(), effect, false);
756     m_commandStack->push(command);
757     m_document->setModified(true);
758 }
759
760 void CustomTrackView::updateEffect(int track, GenTime pos, QDomElement effect, int ix) {
761     ClipItem *clip = getClipItemAt((int)pos.frames(m_document->fps()) + 1, m_tracksList.count() - track);
762     if (clip) {
763         QMap <QString, QString> effectParams = clip->getEffectArgs(effect);
764         if (effectParams.value("disabled") == "1") {
765             if (m_document->renderer()->mltRemoveEffect(track, pos, effectParams.value("kdenlive_ix"))) {
766                 kDebug() << "//////  DISABLING EFFECT: " << index << ", CURRENTLA: " << clip->selectedEffectIndex();
767             } else emit displayMessage(i18n("Problem deleting effect"), ErrorMessage);
768         } else if (!m_document->renderer()->mltEditEffect(m_tracksList.count() - clip->track(), clip->startPos(), effectParams))
769             emit displayMessage(i18n("Problem editing effect"), ErrorMessage);
770         if (ix == clip->selectedEffectIndex()) {
771             clip->setSelectedEffect(clip->selectedEffectIndex());
772         }
773     }
774     m_document->setModified(true);
775 }
776
777 void CustomTrackView::moveEffect(int track, GenTime pos, int oldPos, int newPos) {
778     ClipItem *clip = getClipItemAt((int)pos.frames(m_document->fps()) + 1, m_tracksList.count() - track);
779     if (clip) {
780         m_document->renderer()->mltMoveEffect(track, pos, oldPos, newPos);
781     }
782     m_document->setModified(true);
783 }
784
785 void CustomTrackView::slotChangeEffectState(ClipItem *clip, int effectPos, bool disable) {
786     QDomElement effect = clip->effectAt(effectPos);
787     QDomElement oldEffect = effect.cloneNode().toElement();
788     effect.setAttribute("disabled", disable);
789     EditEffectCommand *command = new EditEffectCommand(this, m_tracksList.count() - clip->track(), clip->startPos(), oldEffect, effect, effectPos, true);
790     m_commandStack->push(command);
791     m_document->setModified(true);
792 }
793
794 void CustomTrackView::slotChangeEffectPosition(ClipItem *clip, int currentPos, int newPos) {
795     MoveEffectCommand *command = new MoveEffectCommand(this, m_tracksList.count() - clip->track(), clip->startPos(), currentPos, newPos, true);
796     m_commandStack->push(command);
797     m_document->setModified(true);
798 }
799
800 void CustomTrackView::slotUpdateClipEffect(ClipItem *clip, QDomElement oldeffect, QDomElement effect, int ix) {
801     EditEffectCommand *command = new EditEffectCommand(this, m_tracksList.count() - clip->track(), clip->startPos(), oldeffect, effect, ix, true);
802     m_commandStack->push(command);
803 }
804
805 void CustomTrackView::cutClip(ItemInfo info, GenTime cutTime, bool cut) {
806     if (cut) {
807         // cut clip
808         ClipItem *item = getClipItemAt((int) info.startPos.frames(m_document->fps()), info.track);
809         if (!item) {
810             emit displayMessage(i18n("Cannot find clip to cut"), ErrorMessage);
811             return;
812         }
813         int cutPos = (int) cutTime.frames(m_document->fps());
814         ItemInfo newPos;
815         newPos.startPos = cutTime;
816         newPos.endPos = info.endPos;
817         newPos.cropStart = item->cropStart() + (cutTime - info.startPos);
818         newPos.track = info.track;
819         item->resizeEnd(cutPos, m_scale);
820         ClipItem *dup = new ClipItem(item->baseClip(), newPos, m_scale, m_document->fps());
821         dup->setEffectList(item->effectList());
822         scene()->addItem(dup);
823         m_document->renderer()->mltCutClip(m_tracksList.count() - info.track, cutTime);
824         item->baseClip()->addReference();
825         m_document->updateClip(item->baseClip()->getId());
826     } else {
827         // uncut clip
828         ClipItem *item = getClipItemAt((int) info.startPos.frames(m_document->fps()), info.track);
829         ClipItem *dup = getClipItemAt((int) cutTime.frames(m_document->fps()), info.track);
830         if (!item || !dup) {
831             emit displayMessage(i18n("Cannot find clip to uncut"), ErrorMessage);
832             return;
833         }
834         delete dup;
835         item->baseClip()->removeReference();
836         m_document->updateClip(item->baseClip()->getId());
837         item->resizeEnd((int) info.endPos.frames(m_document->fps()), m_scale);
838         m_document->renderer()->mltRemoveClip(m_tracksList.count() - info.track, cutTime);
839         m_document->renderer()->mltResizeClipEnd(m_tracksList.count() - info.track, info.startPos, item->cropStart(), item->cropStart() + info.endPos - info.startPos);
840     }
841 }
842
843
844 void CustomTrackView::slotAddTransitionToSelectedClips(QDomElement transition) {
845     QList<QGraphicsItem *> itemList = scene()->selectedItems();
846     for (int i = 0; i < itemList.count(); i++) {
847         if (itemList.at(i)->type() == AVWIDGET) {
848             ClipItem *item = (ClipItem *) itemList.at(i);
849             ItemInfo info;
850             info.startPos = item->startPos();
851             info.endPos = info.startPos + GenTime(2.5);
852             info.track = item->track();
853             int transitiontrack = getPreviousVideoTrack(info.track);
854             slotAddTransition(item, info, transitiontrack, transition);
855         }
856     }
857 }
858
859 void CustomTrackView::slotAddTransition(ClipItem* clip, ItemInfo transitionInfo, int endTrack, QDomElement transition) {
860     AddTransitionCommand* command = new AddTransitionCommand(this, transitionInfo, endTrack, transition, false, true);
861     m_commandStack->push(command);
862     m_document->setModified(true);
863 }
864
865 void CustomTrackView::addTransition(ItemInfo transitionInfo, int endTrack, QDomElement params) {
866     Transition *tr = new Transition(transitionInfo, endTrack, m_scale, m_document->fps(), params);
867     scene()->addItem(tr);
868
869     //kDebug() << "---- ADDING transition " << e.attribute("tag") << ", on tracks " << m_tracksList.count() - e.attribute("transition_track").toInt() << " / " << getPreviousVideoTrack(e.attribute("transition_track").toInt());
870     m_document->renderer()->mltAddTransition(tr->transitionTag(), endTrack, m_tracksList.count() - transitionInfo.track, transitionInfo.startPos, transitionInfo.endPos, tr->toXML());
871     m_document->setModified(true);
872 }
873
874 void CustomTrackView::deleteTransition(ItemInfo transitionInfo, int endTrack, QDomElement params) {
875     Transition *item = getTransitionItemAt((int)transitionInfo.startPos.frames(m_document->fps()) + 1, transitionInfo.track);
876     m_document->renderer()->mltDeleteTransition(item->transitionTag(), endTrack, m_tracksList.count() - transitionInfo.track, transitionInfo.startPos, transitionInfo.endPos, item->toXML());
877     delete item;
878     emit transitionItemSelected(NULL);
879     m_document->setModified(true);
880 }
881
882 void CustomTrackView::slotTransitionUpdated(Transition *tr, QDomElement old) {
883     EditTransitionCommand *command = new EditTransitionCommand(this, tr->track(), tr->startPos(), old, tr->toXML() , true);
884     m_commandStack->push(command);
885     m_document->setModified(true);
886 }
887
888 void CustomTrackView::updateTransition(int track, GenTime pos, QDomElement oldTransition, QDomElement transition) {
889     Transition *item = getTransitionItemAt((int)pos.frames(m_document->fps()) + 1, track);
890     if (!item) {
891         kWarning() << "Unable to find transition at pos :" << pos.frames(m_document->fps()) << ", ON track: " << track;
892         return;
893     }
894     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);
895     item->setTransitionParameters(transition);
896     m_document->setModified(true);
897 }
898
899 void CustomTrackView::addItem(DocClipBase *clip, QPoint pos) {
900     ItemInfo info;
901     info.startPos = GenTime((int)(mapToScene(pos).x() / m_scale), m_document->fps());
902     info.endPos = info.startPos + clip->duration();
903     info.track = (int)(pos.y() / m_tracksHeight);
904     kDebug() << "------------  ADDING CLIP ITEM----: " << info.startPos.frames(25) << ", " << info.endPos.frames(25) << ", " << info.track;
905     m_dropItem = new ClipItem(clip, info, m_scale, m_document->fps());
906     scene()->addItem(m_dropItem);
907 }
908
909
910 void CustomTrackView::dragMoveEvent(QDragMoveEvent * event) {
911     event->setDropAction(Qt::IgnoreAction);
912     if (m_dropItem) {
913         int track = (int)(mapToScene(event->pos()).y() / m_tracksHeight);  //) * (m_scale * 50) + m_scale;
914         m_dropItem->moveTo((int)(mapToScene(event->pos()).x() / m_scale), m_scale, (int)((track - m_dropItem->track()) * m_tracksHeight), track);
915         event->setDropAction(Qt::MoveAction);
916         if (event->mimeData()->hasFormat("kdenlive/producerslist")) {
917             event->acceptProposedAction();
918         }
919     } else {
920         QGraphicsView::dragMoveEvent(event);
921     }
922 }
923
924 void CustomTrackView::dragLeaveEvent(QDragLeaveEvent * event) {
925     if (m_dropItem) {
926         delete m_dropItem;
927         m_dropItem = NULL;
928     } else QGraphicsView::dragLeaveEvent(event);
929 }
930
931 void CustomTrackView::dropEvent(QDropEvent * event) {
932     if (m_dropItem) {
933         AddTimelineClipCommand *command = new AddTimelineClipCommand(this, m_dropItem->xml(), m_dropItem->clipProducer(), m_dropItem->info(), m_dropItem->effectList(), false, false);
934         m_commandStack->push(command);
935         m_dropItem->baseClip()->addReference();
936         m_document->updateClip(m_dropItem->baseClip()->getId());
937         ItemInfo info;
938         info = m_dropItem->info();
939         info.track = m_tracksList.count() - m_dropItem->track();
940         //kDebug()<<"IIIIIIIIIIIIIIIIIIIIIIII TRAX CNT: "<<m_tracksList.count()<<", DROP: "<<m_dropItem->track();
941         m_document->renderer()->mltInsertClip(info, m_dropItem->xml(), m_dropItem->baseClip()->producer());
942         //if (m_dropItem->baseClip()->isTransparent()) m_document->renderer()->mltAddClipTransparency(info, getPreviousVideoTrack(m_dropItem->track()), m_dropItem->baseClip()->getId());
943         m_dropItem = NULL;
944         m_document->setModified(true);
945     } else QGraphicsView::dropEvent(event);
946     m_dropItem = NULL;
947 }
948
949
950 QStringList CustomTrackView::mimeTypes() const {
951     QStringList qstrList;
952     // list of accepted mime types for drop
953     qstrList.append("text/plain");
954     qstrList.append("kdenlive/producerslist");
955     return qstrList;
956 }
957
958 Qt::DropActions CustomTrackView::supportedDropActions() const {
959     // returns what actions are supported when dropping
960     return Qt::MoveAction;
961 }
962
963 void CustomTrackView::setDuration(int duration) {
964     kDebug() << "/////////////  PRO DUR: " << duration << ", SCALE. " << (m_projectDuration + 500) * m_scale << ", height: " << 50 * m_tracksList.count();
965     m_projectDuration = duration;
966     setSceneRect(0, 0, (m_projectDuration + 100) * m_scale, sceneRect().height());
967 }
968
969 int CustomTrackView::duration() const {
970     return m_projectDuration;
971 }
972
973 void CustomTrackView::addTrack(TrackInfo type) {
974     m_tracksList << type;
975     m_cursorLine->setLine(m_cursorLine->line().x1(), 0, m_cursorLine->line().x1(), m_tracksHeight * m_tracksList.count());
976     setSceneRect(0, 0, sceneRect().width(), m_tracksHeight * m_tracksList.count());
977     verticalScrollBar()->setMaximum(m_tracksHeight * m_tracksList.count());
978     //setFixedHeight(50 * m_tracksCount);
979 }
980
981 void CustomTrackView::removeTrack() {
982     // TODO: implement track deletion
983     //m_tracksCount--;
984     m_cursorLine->setLine(m_cursorLine->line().x1(), 0, m_cursorLine->line().x1(), m_tracksHeight * m_tracksList.count());
985 }
986
987
988 void CustomTrackView::slotSwitchTrackAudio(int ix) {
989     int tracknumber = m_tracksList.count() - ix;
990     kDebug() << "/////  MUTING TRK: " << ix << "; PL NUM: " << tracknumber;
991     m_tracksList[tracknumber - 1].isMute = !m_tracksList.at(tracknumber - 1).isMute;
992     m_document->renderer()->mltChangeTrackState(tracknumber, m_tracksList.at(tracknumber - 1).isMute, m_tracksList.at(tracknumber - 1).isBlind);
993 }
994
995 void CustomTrackView::slotSwitchTrackVideo(int ix) {
996     int tracknumber = m_tracksList.count() - ix;
997     m_tracksList[tracknumber - 1].isBlind = !m_tracksList.at(tracknumber - 1).isBlind;
998     m_document->renderer()->mltChangeTrackState(tracknumber, m_tracksList.at(tracknumber - 1).isMute, m_tracksList.at(tracknumber - 1).isBlind);
999 }
1000
1001 void CustomTrackView::deleteClip(int clipId) {
1002     QList<QGraphicsItem *> itemList = items();
1003     for (int i = 0; i < itemList.count(); i++) {
1004         if (itemList.at(i)->type() == AVWIDGET) {
1005             ClipItem *item = (ClipItem *)itemList.at(i);
1006             if (item->clipProducer() == clipId) {
1007                 AddTimelineClipCommand *command = new AddTimelineClipCommand(this, item->xml(), item->clipProducer(), item->info(), item->effectList(), true, true);
1008                 m_commandStack->push(command);
1009                 //delete item;
1010             }
1011         }
1012     }
1013 }
1014
1015 void CustomTrackView::setCursorPos(int pos, bool seek) {
1016     emit cursorMoved((int)(m_cursorPos * m_scale), (int)(pos * m_scale));
1017     m_cursorPos = pos;
1018     m_cursorLine->setPos(pos * m_scale, 0);
1019     if (seek) m_document->renderer()->seek(GenTime(pos, m_document->fps()));
1020     else if (m_autoScroll && m_scale < 50) checkScrolling();
1021 }
1022
1023 void CustomTrackView::updateCursorPos() {
1024     m_cursorLine->setPos(m_cursorPos * m_scale, 0);
1025 }
1026
1027 int CustomTrackView::cursorPos() {
1028     return (int)(m_cursorPos * m_scale);
1029 }
1030
1031 void CustomTrackView::moveCursorPos(int delta) {
1032     if (m_cursorPos + delta < 0) delta = 0 - m_cursorPos;
1033     emit cursorMoved((int)(m_cursorPos * m_scale), (int)((m_cursorPos + delta) * m_scale));
1034     m_cursorPos += delta;
1035     m_cursorLine->setPos(m_cursorPos * m_scale, 0);
1036     m_document->renderer()->seek(GenTime(m_cursorPos, m_document->fps()));
1037     //if (m_autoScroll && m_scale < 50) checkScrolling();
1038 }
1039
1040 void CustomTrackView::checkScrolling() {
1041     int vert = verticalScrollBar()->value();
1042     int hor = cursorPos();
1043     ensureVisible(hor, vert + 10, 2, 2, 50, 0);
1044     //centerOn(QPointF(cursorPos(), m_tracksHeight));
1045     /*QRect rectInView = viewport()->rect();
1046     int delta = rectInView.width() / 3;
1047     int max = rectInView.right() + horizontalScrollBar()->value() - delta;
1048     //kDebug() << "CURSOR POS: "<<m_cursorPos<< "Scale: "<<m_scale;
1049     if (m_cursorPos * m_scale >= max) horizontalScrollBar()->setValue((int)(horizontalScrollBar()->value() + 1 + m_scale));*/
1050 }
1051
1052 void CustomTrackView::mouseReleaseEvent(QMouseEvent * event) {
1053     if (event->button() == Qt::MidButton) {
1054         return;
1055     }
1056     QGraphicsView::mouseReleaseEvent(event);
1057     setDragMode(QGraphicsView::NoDrag);
1058     if (m_operationMode == MOVEGUIDE) {
1059         setCursor(Qt::ArrowCursor);
1060         m_operationMode = NONE;
1061         m_dragGuide->setFlag(QGraphicsItem::ItemIsMovable, false);
1062         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);
1063         m_commandStack->push(command);
1064         m_dragGuide->updateGuide(GenTime(m_dragGuide->pos().x() / m_scale, m_document->fps()));
1065         m_dragGuide = NULL;
1066         m_dragItem = NULL;
1067         return;
1068     }
1069     if (m_dragItem == NULL) {
1070         emit transitionItemSelected(NULL);
1071         return;
1072     }
1073     ItemInfo info;
1074     info.startPos = m_dragItem->startPos();
1075     info.endPos = m_dragItem->endPos();
1076     info.track = m_dragItem->track();
1077
1078     if (m_operationMode == MOVE) {
1079         setCursor(Qt::OpenHandCursor);
1080
1081         if (m_selectedClipList.count() == 1) {
1082             // we are moving one clip, easy
1083             if (m_dragItem->type() == AVWIDGET && (m_dragItemInfo.startPos != info.startPos || m_dragItemInfo.track != info.track)) {
1084                 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())));
1085                 if (success) {
1086                     MoveClipCommand *command = new MoveClipCommand(this, m_dragItemInfo, info, false);
1087                     m_commandStack->push(command);
1088                 } else {
1089                     // undo last move and emit error message
1090                     MoveClipCommand *command = new MoveClipCommand(this, info, m_dragItemInfo, true);
1091                     m_commandStack->push(command);
1092                     emit displayMessage(i18n("Cannot move clip to position %1seconds", QString::number(m_dragItemInfo.startPos.seconds(), 'g', 2)), ErrorMessage);
1093                 }
1094             }
1095             if (m_dragItem->type() == TRANSITIONWIDGET && (m_dragItemInfo.startPos != info.startPos || m_dragItemInfo.track != info.track)) {
1096                 MoveTransitionCommand *command = new MoveTransitionCommand(this, m_dragItemInfo, info, false);
1097                 m_commandStack->push(command);
1098                 Transition *transition = (Transition *) m_dragItem;
1099                 transition->updateTransitionEndTrack(getPreviousVideoTrack(m_dragItem->track()));
1100                 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);
1101             }
1102         } else {
1103             // Moving several clips. We need to delete them and readd them to new position,
1104             // or they might overlap each other during the move
1105
1106             GenTime timeOffset = info.startPos - m_dragItemInfo.startPos;
1107             int trackOffset = info.track - m_dragItemInfo.track;
1108             if (timeOffset != GenTime() || trackOffset != 0) {
1109                 QUndoCommand *moveClips = new QUndoCommand();
1110                 moveClips->setText("Move clips");
1111                 // remove items in MLT playlist
1112                 for (int i = 0; i < m_selectedClipList.count(); i++) {
1113                     AbstractClipItem *item = m_selectedClipList.at(i);
1114                     ItemInfo info = item->info();
1115                     info.startPos = info.startPos - timeOffset;
1116                     info.endPos = info.endPos - timeOffset;
1117                     info.track = info.track - trackOffset;
1118                     if (item->type() == AVWIDGET) {
1119                         ClipItem *clip = static_cast <ClipItem*>(item);
1120                         new AddTimelineClipCommand(this, clip->xml(), clip->clipProducer(), info, clip->effectList(), false, true, moveClips);
1121                         m_document->renderer()->mltRemoveClip(m_tracksList.count() - info.track, info.startPos);
1122                     } else {
1123                         Transition *tr = static_cast <Transition*>(item);
1124                         new AddTransitionCommand(this, info, tr->transitionEndTrack(), tr->toXML(), false, true, moveClips);
1125                         m_document->renderer()->mltDeleteTransition(tr->transitionTag(), tr->transitionEndTrack(), m_tracksList.count() - info.track, info.startPos, info.endPos, tr->toXML());
1126                     }
1127                 }
1128
1129                 for (int i = 0; i < m_selectedClipList.count(); i++) {
1130                     // re-add items in correct place
1131                     AbstractClipItem *item = m_selectedClipList.at(i);
1132                     if (item->type() == AVWIDGET) {
1133                         ClipItem *clip = static_cast <ClipItem*>(item);
1134                         new AddTimelineClipCommand(this, clip->xml(), clip->clipProducer(), item->info(), clip->effectList(), false, false, moveClips);
1135                         ItemInfo info = item->info();
1136                         info.track = m_tracksList.count() - item->track();
1137                         m_document->renderer()->mltInsertClip(info, clip->xml(), clip->baseClip()->producer());
1138                     } else {
1139                         Transition *tr = static_cast <Transition*>(item);
1140                         ItemInfo transitionInfo = tr->info();
1141                         new AddTransitionCommand(this, info, tr->transitionEndTrack(), tr->toXML(), false, false, moveClips);
1142                         m_document->renderer()->mltAddTransition(tr->transitionTag(), tr->transitionEndTrack() + trackOffset, m_tracksList.count() - transitionInfo.track, transitionInfo.startPos, transitionInfo.endPos, tr->toXML());
1143                     }
1144                 }
1145                 m_commandStack->push(moveClips);
1146             }
1147         }
1148
1149     } else if (m_operationMode == RESIZESTART && m_dragItem->startPos() != m_dragItemInfo.startPos) {
1150         // resize start
1151         if (m_dragItem->type() == AVWIDGET) {
1152             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());
1153             updateClipFade((ClipItem *) m_dragItem);
1154             ResizeClipCommand *command = new ResizeClipCommand(this, m_dragItemInfo, info, false);
1155             m_commandStack->push(command);
1156         } else if (m_dragItem->type() == TRANSITIONWIDGET) {
1157             MoveTransitionCommand *command = new MoveTransitionCommand(this, m_dragItemInfo, info, false);
1158             m_commandStack->push(command);
1159             Transition *transition = (Transition *) m_dragItem;
1160             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);
1161         }
1162
1163         //m_document->renderer()->doRefresh();
1164     } else if (m_operationMode == RESIZEEND && m_dragItem->endPos() != m_dragItemInfo.endPos) {
1165         // resize end
1166         if (m_dragItem->type() == AVWIDGET) {
1167             ResizeClipCommand *command = new ResizeClipCommand(this, m_dragItemInfo, info, false);
1168             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());
1169             m_commandStack->push(command);
1170         } else if (m_dragItem->type() == TRANSITIONWIDGET) {
1171             MoveTransitionCommand *command = new MoveTransitionCommand(this, m_dragItemInfo, info, false);
1172             m_commandStack->push(command);
1173             Transition *transition = (Transition *) m_dragItem;
1174             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);
1175         }
1176         //m_document->renderer()->doRefresh();
1177     } else if (m_operationMode == FADEIN) {
1178         // resize fade in effect
1179         ClipItem * item = (ClipItem *) m_dragItem;
1180         QStringList clipeffects = item->effectNames();
1181         if (clipeffects.contains(i18n("Fade in"))) {
1182             int ix = clipeffects.indexOf(i18n("Fade in"));
1183             QDomElement oldeffect = item->effectAt(ix);
1184             int start = item->cropStart().frames(m_document->fps());
1185             int end = item->fadeIn();
1186             if (end == 0) {
1187                 slotDeleteEffect(item, oldeffect);
1188             } else {
1189                 end += start;
1190                 QDomElement effect = MainWindow::audioEffects.getEffectByName("Fade in");
1191                 EffectsList::setParameter(effect, "in", QString::number(start));
1192                 EffectsList::setParameter(effect, "out", QString::number(end));
1193                 slotUpdateClipEffect(item, oldeffect, effect, ix);
1194             }
1195         } else if (item->fadeIn() != 0) {
1196             QDomElement effect = MainWindow::audioEffects.getEffectByName("Fade in");
1197             int start = item->cropStart().frames(m_document->fps());
1198             int end = item->fadeIn() + start;
1199             EffectsList::setParameter(effect, "in", QString::number(start));
1200             EffectsList::setParameter(effect, "out", QString::number(end));
1201             slotAddEffect(effect, m_dragItem->startPos(), m_dragItem->track());
1202         }
1203     } else if (m_operationMode == FADEOUT) {
1204         // resize fade in effect
1205         ClipItem * item = (ClipItem *) m_dragItem;
1206         QStringList clipeffects = item->effectNames();
1207         if (clipeffects.contains(i18n("Fade out"))) {
1208             int ix = clipeffects.indexOf(i18n("Fade out"));
1209             QDomElement oldeffect = item->effectAt(ix);
1210             int end = (item->duration() + item->cropStart()).frames(m_document->fps());
1211             int start = item->fadeOut();
1212             if (start == 0) {
1213                 slotDeleteEffect(item, oldeffect);
1214             } else {
1215                 start = end - start;
1216                 QDomElement effect = MainWindow::audioEffects.getEffectByName("Fade out");
1217                 EffectsList::setParameter(effect, "in", QString::number(start));
1218                 EffectsList::setParameter(effect, "out", QString::number(end));
1219                 slotUpdateClipEffect(item, oldeffect, effect, ix);
1220             }
1221         } else if (item->fadeOut() != 0) {
1222             QDomElement effect = MainWindow::audioEffects.getEffectByName("Fade out");
1223             int end = (item->duration() + item->cropStart()).frames(m_document->fps());
1224             int start = end - item->fadeOut();
1225             EffectsList::setParameter(effect, "in", QString::number(start));
1226             EffectsList::setParameter(effect, "out", QString::number(end));
1227             slotAddEffect(effect, m_dragItem->startPos(), m_dragItem->track());
1228         }
1229     } else if (m_operationMode == KEYFRAME) {
1230         // update the MLT effect
1231         ClipItem * item = (ClipItem *) m_dragItem;
1232         QString previous = item->keyframes(item->selectedEffectIndex());
1233         item->updateKeyframeEffect();
1234         QString next = item->keyframes(item->selectedEffectIndex());
1235         EditKeyFrameCommand *command = new EditKeyFrameCommand(this, item->track(), item->startPos(), item->selectedEffectIndex(), previous, next, false);
1236         m_commandStack->push(command);
1237         updateEffect(m_tracksList.count() - item->track(), item->startPos(), item->selectedEffect(), item->selectedEffectIndex());
1238     }
1239
1240     emit transitionItemSelected((m_dragItem && m_dragItem->type() == TRANSITIONWIDGET) ? (Transition*) m_dragItem : NULL);
1241     m_document->setModified(true);
1242     m_operationMode = NONE;
1243 }
1244
1245 void CustomTrackView::deleteClip(ItemInfo info) {
1246     ClipItem *item = getClipItemAt((int) info.startPos.frames(m_document->fps()) + 1, info.track);
1247     if (!item) {
1248         kDebug() << "----------------  ERROR, CANNOT find clip to delete at...";// << rect.x();
1249         return;
1250     }
1251     if (item->isSelected()) emit clipItemSelected(NULL);
1252     item->baseClip()->removeReference();
1253     m_document->updateClip(item->baseClip()->getId());
1254     delete item;
1255     m_document->renderer()->mltRemoveClip(m_tracksList.count() - info.track, info.startPos);
1256     m_document->renderer()->doRefresh();
1257 }
1258
1259 void CustomTrackView::deleteSelectedClips() {
1260     QList<QGraphicsItem *> itemList = scene()->selectedItems();
1261     if (itemList.count() == 0) {
1262         emit displayMessage(i18n("Select clip to delete"), ErrorMessage);
1263         return;
1264     }
1265     QUndoCommand *deleteSelected = new QUndoCommand();
1266     deleteSelected->setText("Delete selected items");
1267     for (int i = 0; i < itemList.count(); i++) {
1268         if (itemList.at(i)->type() == AVWIDGET) {
1269             ClipItem *item = static_cast <ClipItem *>(itemList.at(i));
1270             new AddTimelineClipCommand(this, item->xml(), item->clipProducer(), item->info(), item->effectList(), true, true, deleteSelected);
1271         } else if (itemList.at(i)->type() == TRANSITIONWIDGET) {
1272             Transition *item = static_cast <Transition *>(itemList.at(i));
1273             ItemInfo info;
1274             info.startPos = item->startPos();
1275             info.endPos = item->endPos();
1276             info.track = item->track();
1277             new AddTransitionCommand(this, info, item->transitionEndTrack(), item->toXML(), true, true, deleteSelected);
1278         }
1279     }
1280     m_commandStack->push(deleteSelected);
1281 }
1282
1283 void CustomTrackView::changeClipSpeed() {
1284     QList<QGraphicsItem *> itemList = scene()->selectedItems();
1285     if (itemList.count() == 0) {
1286         emit displayMessage(i18n("Select clip to change speed"), ErrorMessage);
1287         return;
1288     }
1289     QUndoCommand *changeSelected = new QUndoCommand();
1290     changeSelected->setText("Edit clip speed");
1291     for (int i = 0; i < itemList.count(); i++) {
1292         if (itemList.at(i)->type() == AVWIDGET) {
1293             ClipItem *item = static_cast <ClipItem *>(itemList.at(i));
1294             ItemInfo info = item->info();
1295             int percent = QInputDialog::getInteger(this, i18n("Edit Clip Speed"), i18n("New speed (percents)"), 100, 1, 300);
1296             double speed = (double) percent / 100.0;
1297             new ChangeSpeedCommand(this, info, item->speed(), speed, item->clipProducer(), true, changeSelected);
1298         }
1299     }
1300     m_commandStack->push(changeSelected);
1301 }
1302
1303 void CustomTrackView::doChangeClipSpeed(ItemInfo info, double speed, int id) {
1304     DocClipBase *baseclip = m_document->clipManager()->getClipById(id);
1305     ClipItem *item = getClipItemAt((int) info.startPos.frames(m_document->fps()) + 1, info.track);
1306     info.track = m_tracksList.count() - item->track();
1307     int newLength = m_document->renderer()->mltChangeClipSpeed(info, speed, baseclip->producer());
1308     GenTime maxDuration(newLength, m_document->fps());
1309     item->setMaxDuration(maxDuration);
1310     item->setSpeed(speed);
1311     if (maxDuration < item->duration()) {
1312         info = item->info();
1313         ItemInfo endInfo = info;
1314         endInfo.endPos = info.startPos + maxDuration;
1315         ResizeClipCommand *command = new ResizeClipCommand(this, info, endInfo, true);
1316         m_commandStack->push(command);
1317     }
1318 }
1319
1320 void CustomTrackView::cutSelectedClips() {
1321     QList<QGraphicsItem *> itemList = scene()->selectedItems();
1322     GenTime currentPos = GenTime(m_cursorPos, m_document->fps());
1323     for (int i = 0; i < itemList.count(); i++) {
1324         if (itemList.at(i)->type() == AVWIDGET) {
1325             ClipItem *item = (ClipItem *) itemList.at(i);
1326             ItemInfo info;
1327             info.startPos = item->startPos();
1328             info.endPos = item->endPos();
1329             if (currentPos > info.startPos && currentPos <  info.endPos) {
1330                 info.track = item->track();
1331                 RazorClipCommand *command = new RazorClipCommand(this, info, currentPos, true);
1332                 m_commandStack->push(command);
1333             }
1334         }
1335     }
1336 }
1337
1338 void CustomTrackView::addClip(QDomElement xml, int clipId, ItemInfo info, EffectsList effects) {
1339     DocClipBase *baseclip = m_document->clipManager()->getClipById(clipId);
1340     ClipItem *item = new ClipItem(baseclip, info, m_scale, m_document->fps());
1341     item->setEffectList(effects);
1342     scene()->addItem(item);
1343     baseclip->addReference();
1344     m_document->updateClip(baseclip->getId());
1345     info.track = m_tracksList.count() - info.track;
1346     m_document->renderer()->mltInsertClip(info, xml, baseclip->producer());
1347     for (int i = 0; i < item->effectsCount(); i++) {
1348         m_document->renderer()->mltAddEffect(info.track, info.startPos, item->getEffectArgs(item->effectAt(i)), false);
1349     }
1350     m_document->renderer()->doRefresh();
1351 }
1352
1353 void CustomTrackView::slotUpdateClip(int clipId) {
1354     QList<QGraphicsItem *> list = scene()->items();
1355     ClipItem *clip = NULL;
1356     for (int i = 0; i < list.size(); ++i) {
1357         if (list.at(i)->type() == AVWIDGET) {
1358             clip = static_cast <ClipItem *>(list.at(i));
1359             if (clip->clipProducer() == clipId) {
1360                 clip->refreshClip();
1361                 ItemInfo info = clip->info();
1362                 info.track = m_tracksList.count() - clip->track();
1363                 m_document->renderer()->mltUpdateClip(info, clip->xml(), clip->baseClip()->producer());
1364             }
1365         }
1366     }
1367 }
1368
1369 ClipItem *CustomTrackView::getClipItemAt(int pos, int track) {
1370     QList<QGraphicsItem *> list = scene()->items(QPointF(pos * m_scale, track * m_tracksHeight + m_tracksHeight / 2));
1371     ClipItem *clip = NULL;
1372     for (int i = 0; i < list.size(); ++i) {
1373         if (list.at(i)->type() == AVWIDGET) {
1374             clip = static_cast <ClipItem *>(list.at(i));
1375             break;
1376         }
1377     }
1378     return clip;
1379 }
1380
1381 ClipItem *CustomTrackView::getClipItemAt(GenTime pos, int track) {
1382     int framepos = (int)(pos.frames(m_document->fps()) * m_scale);
1383     return getClipItemAt(framepos, track);
1384 }
1385
1386 Transition *CustomTrackView::getTransitionItemAt(int pos, int track) {
1387     QList<QGraphicsItem *> list = scene()->items(QPointF(pos * m_scale, (track + 1) * m_tracksHeight));
1388     Transition *clip = NULL;
1389     for (int i = 0; i < list.size(); ++i) {
1390         if (list.at(i)->type() == TRANSITIONWIDGET) {
1391             clip = static_cast <Transition *>(list.at(i));
1392             break;
1393         }
1394     }
1395     return clip;
1396 }
1397
1398 Transition *CustomTrackView::getTransitionItemAt(GenTime pos, int track) {
1399     int framepos = (int)(pos.frames(m_document->fps()) * m_scale);
1400     return getTransitionItemAt(framepos, track);
1401 }
1402
1403 void CustomTrackView::moveClip(const ItemInfo start, const ItemInfo end) {
1404     ClipItem *item = getClipItemAt((int) start.startPos.frames(m_document->fps()) + 1, start.track);
1405     if (!item) {
1406         emit displayMessage(i18n("Cannot move clip at time: %1s on track %2", QString::number(start.startPos.seconds(), 'g', 2), start.track), ErrorMessage);
1407         kDebug() << "----------------  ERROR, CANNOT find clip to move at.. ";// << startPos.x() * m_scale * FRAME_SIZE + 1 << ", " << startPos.y() * m_tracksHeight + m_tracksHeight / 2;
1408         return;
1409     }
1410     //kDebug() << "----------------  Move CLIP FROM: " << startPos.x() << ", END:" << endPos.x() << ",TRACKS: " << startPos.y() << " TO " << endPos.y();
1411
1412     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()));
1413     if (success) {
1414         item->moveTo((int) end.startPos.frames(m_document->fps()), m_scale, (int)((end.track - start.track) * m_tracksHeight), end.track);
1415     } else {
1416         // undo last move and emit error message
1417         emit displayMessage(i18n("Cannot move clip to position %1seconds", QString::number(end.startPos.seconds(), 'g', 2)), ErrorMessage);
1418     }
1419 }
1420
1421 void CustomTrackView::moveTransition(const ItemInfo start, const ItemInfo end) {
1422     Transition *item = getTransitionItemAt((int)start.startPos.frames(m_document->fps()) + 1, start.track);
1423     if (!item) {
1424         emit displayMessage(i18n("Cannot move transition at time: %1s on track %2", start.startPos.seconds(), start.track), ErrorMessage);
1425         kDebug() << "----------------  ERROR, CANNOT find transition to move... ";// << startPos.x() * m_scale * FRAME_SIZE + 1 << ", " << startPos.y() * m_tracksHeight + m_tracksHeight / 2;
1426         return;
1427     }
1428     //kDebug() << "----------------  Move TRANSITION FROM: " << startPos.x() << ", END:" << endPos.x() << ",TRACKS: " << oldtrack << " TO " << newtrack;
1429
1430     //kDebug()<<"///  RESIZE TRANS START: ("<< startPos.x()<<"x"<< startPos.y()<<") / ("<<endPos.x()<<"x"<< endPos.y()<<")";
1431     if (end.endPos - end.startPos == start.endPos - start.startPos) {
1432         // Transition was moved
1433         item->moveTo((int) end.startPos.frames(m_document->fps()), m_scale, (end.track - start.track) * m_tracksHeight, end.track);
1434     } else if (end.endPos == start.endPos) {
1435         // Transition start resize
1436         item->resizeStart((int) end.startPos.frames(m_document->fps()), m_scale);
1437     } else {
1438         // Transition end resize;
1439         item->resizeEnd((int) end.endPos.frames(m_document->fps()), m_scale);
1440     }
1441     //item->moveTransition(GenTime((int) (endPos.x() - startPos.x()), m_document->fps()));
1442     item->updateTransitionEndTrack(getPreviousVideoTrack(end.track));
1443     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);
1444 }
1445
1446 void CustomTrackView::resizeClip(const ItemInfo start, const ItemInfo end) {
1447     int offset = 0;
1448     bool resizeClipStart = true;
1449     if (start.startPos == end.startPos) resizeClipStart = false;
1450     /*if (resizeClipStart) offset = 1;
1451     else offset = -1;*/
1452     ClipItem *item = getClipItemAt((int)(start.startPos.frames(m_document->fps()) + offset), start.track);
1453     if (!item) {
1454         emit displayMessage(i18n("Cannot move clip at time: %1s on track %2", start.startPos.seconds(), start.track), ErrorMessage);
1455         kDebug() << "----------------  ERROR, CANNOT find clip to resize at... "; // << startPos;
1456         return;
1457     }
1458     if (resizeClipStart) {
1459         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);
1460         item->resizeStart((int) end.startPos.frames(m_document->fps()), m_scale);
1461         updateClipFade(item);
1462     } else {
1463         m_document->renderer()->mltResizeClipEnd(m_tracksList.count() - item->track(), item->startPos(), item->cropStart(), item->cropStart() + end.endPos - item->startPos());
1464         item->resizeEnd((int) end.endPos.frames(m_document->fps()), m_scale);
1465         updateClipFade(item, true);
1466     }
1467     m_document->renderer()->doRefresh();
1468 }
1469
1470 void CustomTrackView::updateClipFade(ClipItem * item, bool updateFadeOut) {
1471     if (!updateFadeOut) {
1472         int end = item->fadeIn();
1473         if (end != 0) {
1474             // there is a fade in effect
1475             QStringList clipeffects = item->effectNames();
1476             QDomElement oldeffect = item->effectAt(clipeffects.indexOf("Fade in"));
1477             int start = item->cropStart().frames(m_document->fps());
1478             end += start;
1479             EffectsList::setParameter(oldeffect, "in", QString::number(start));
1480             EffectsList::setParameter(oldeffect, "out", QString::number(end));
1481             QMap <QString, QString> effectParams = item->getEffectArgs(oldeffect);
1482             if (!m_document->renderer()->mltEditEffect(m_tracksList.count() - item->track(), item->startPos(), effectParams))
1483                 emit displayMessage(i18n("Problem editing effect"), ErrorMessage);
1484         }
1485     } else {
1486         int start = item->fadeOut();
1487         if (start != 0) {
1488             // there is a fade in effect
1489             QStringList clipeffects = item->effectNames();
1490             QDomElement oldeffect = item->effectAt(clipeffects.indexOf("Fade out"));
1491             int end = (item->duration() - item->cropStart()).frames(m_document->fps());
1492             start = end - start;
1493             EffectsList::setParameter(oldeffect, "in", QString::number(start));
1494             EffectsList::setParameter(oldeffect, "out", QString::number(end));
1495             QMap <QString, QString> effectParams = item->getEffectArgs(oldeffect);
1496             if (m_document->renderer()->mltEditEffect(m_tracksList.count() - item->track(), item->startPos(), effectParams))
1497                 emit displayMessage(i18n("Problem editing effect"), ErrorMessage);
1498         }
1499     }
1500 }
1501
1502 double CustomTrackView::getSnapPointForPos(double pos) {
1503     for (int i = 0; i < m_snapPoints.size(); ++i) {
1504         if (abs((int)(pos - m_snapPoints.at(i).frames(m_document->fps()) * m_scale)) < 10) {
1505             //kDebug()<<" FOUND SNAP POINT AT: "<<m_snapPoints.at(i)<<", current pos: "<<pos / m_scale;
1506             return m_snapPoints.at(i).frames(m_document->fps()) * m_scale + 0.5;
1507         }
1508         if (m_snapPoints.at(i).frames(m_document->fps() * m_scale) > pos) break;
1509     }
1510     return pos;
1511 }
1512
1513 void CustomTrackView::updateSnapPoints(AbstractClipItem *selected) {
1514     m_snapPoints.clear();
1515     if (!KdenliveSettings::snaptopoints()) return;
1516     GenTime offset;
1517     if (selected) offset = selected->duration();
1518     QList<QGraphicsItem *> itemList = items();
1519     for (int i = 0; i < itemList.count(); i++) {
1520         if (itemList.at(i)->type() == AVWIDGET && itemList.at(i) != selected) {
1521             ClipItem *item = static_cast <ClipItem *>(itemList.at(i));
1522             GenTime start = item->startPos();
1523             GenTime end = item->endPos();
1524             m_snapPoints.append(start);
1525             m_snapPoints.append(end);
1526             QList < GenTime > markers = item->snapMarkers();
1527             for (int i = 0; i < markers.size(); ++i) {
1528                 GenTime t = markers.at(i);
1529                 m_snapPoints.append(t);
1530                 if (t > offset) m_snapPoints.append(t - offset);
1531             }
1532             if (offset != GenTime()) {
1533                 if (start > offset) m_snapPoints.append(start - offset);
1534                 if (end > offset) m_snapPoints.append(end - offset);
1535             }
1536         } else if (itemList.at(i)->type() == TRANSITIONWIDGET) {
1537             Transition *transition = static_cast <Transition*>(itemList.at(i));
1538             GenTime start = transition->startPos();
1539             GenTime end = transition->endPos();
1540             m_snapPoints.append(start);
1541             m_snapPoints.append(end);
1542             if (offset != GenTime()) {
1543                 if (start > offset) m_snapPoints.append(start - offset);
1544                 if (end > offset) m_snapPoints.append(end - offset);
1545             }
1546         }
1547     }
1548
1549     // add cursor position
1550     GenTime pos = GenTime(m_cursorPos, m_document->fps());
1551     m_snapPoints.append(pos);
1552     if (offset != GenTime()) m_snapPoints.append(pos - offset);
1553
1554     // add guides
1555     for (int i = 0; i < m_guides.count(); i++) {
1556         m_snapPoints.append(m_guides.at(i)->position());
1557         if (offset != GenTime()) m_snapPoints.append(m_guides.at(i)->position() - offset);
1558     }
1559
1560     qSort(m_snapPoints);
1561     //for (int i = 0; i < m_snapPoints.size(); ++i)
1562     //    kDebug() << "SNAP POINT: " << m_snapPoints.at(i).frames(25);
1563 }
1564
1565 void CustomTrackView::slotSeekToPreviousSnap() {
1566     updateSnapPoints(NULL);
1567     GenTime pos = GenTime(m_cursorPos, m_document->fps());
1568     GenTime res = GenTime();
1569     for (int i = 0; i < m_snapPoints.size(); ++i) {
1570         if (m_snapPoints.at(i) >= pos) {
1571             if (i == 0) i = 1;
1572             res = m_snapPoints.at(i - 1);
1573             break;
1574         }
1575     }
1576     setCursorPos((int) res.frames(m_document->fps()));
1577     checkScrolling();
1578 }
1579
1580 void CustomTrackView::slotSeekToNextSnap() {
1581     updateSnapPoints(NULL);
1582     GenTime pos = GenTime(m_cursorPos, m_document->fps());
1583     GenTime res = GenTime(m_projectDuration, m_document->fps());
1584     for (int i = 0; i < m_snapPoints.size(); ++i) {
1585         if (m_snapPoints.at(i) > pos) {
1586             res = m_snapPoints.at(i);
1587             break;
1588         }
1589     }
1590     setCursorPos((int) res.frames(m_document->fps()));
1591     checkScrolling();
1592 }
1593
1594 void CustomTrackView::clipStart() {
1595     QList<QGraphicsItem *> itemList = scene()->selectedItems();
1596     for (int i = 0; i < itemList.count(); i++) {
1597         if (itemList.at(i)->type() == AVWIDGET) {
1598             ClipItem *item = (ClipItem *) itemList.at(i);
1599             setCursorPos((int) item->startPos().frames(m_document->fps()));
1600             checkScrolling();
1601             break;
1602         }
1603     }
1604 }
1605
1606 void CustomTrackView::clipEnd() {
1607     QList<QGraphicsItem *> itemList = scene()->selectedItems();
1608     for (int i = 0; i < itemList.count(); i++) {
1609         if (itemList.at(i)->type() == AVWIDGET) {
1610             ClipItem *item = (ClipItem *) itemList.at(i);
1611             setCursorPos((int) item->endPos().frames(m_document->fps()));
1612             checkScrolling();
1613             break;
1614         }
1615     }
1616 }
1617
1618 void CustomTrackView::slotAddClipMarker() {
1619     QList<QGraphicsItem *> itemList = scene()->selectedItems();
1620     if (itemList.count() != 1) {
1621         emit displayMessage(i18n("Cannot add marker if more than one clip is selected"), ErrorMessage);
1622         kDebug() << "// CANNOT ADD MARKER IF MORE TAN ONE CLIP IS SELECTED....";
1623         return;
1624     }
1625     AbstractClipItem *item = (AbstractClipItem *)itemList.at(0);
1626     if (item->type() != AVWIDGET) return;
1627     GenTime pos = GenTime(m_cursorPos, m_document->fps());
1628     if (item->startPos() > pos || item->endPos() < pos) return;
1629     ClipItem *clip = (ClipItem *) item;
1630     int id = clip->baseClip()->getId();
1631     GenTime position = pos - item->startPos() + item->cropStart();
1632     CommentedTime marker(position, i18n("Marker"));
1633     MarkerDialog d(clip->baseClip(), marker, m_document->timecode(), this);
1634     if (d.exec() == QDialog::Accepted) {
1635         slotAddClipMarker(id, d.newMarker().time(), d.newMarker().comment());
1636     }
1637 }
1638
1639 void CustomTrackView::slotAddClipMarker(int id, GenTime t, QString c) {
1640     QString oldcomment = m_document->clipManager()->getClipById(id)->markerComment(t);
1641     AddMarkerCommand *command = new AddMarkerCommand(this, oldcomment, c, id, t, true);
1642     m_commandStack->push(command);
1643 }
1644
1645 void CustomTrackView::slotDeleteClipMarker() {
1646     QList<QGraphicsItem *> itemList = scene()->selectedItems();
1647     if (itemList.count() != 1) {
1648         emit displayMessage(i18n("Cannot delete marker if more than one clip is selected"), ErrorMessage);
1649         kDebug() << "// CANNOT DELETE MARKER IF MORE TAN ONE CLIP IS SELECTED....";
1650         return;
1651     }
1652     AbstractClipItem *item = (AbstractClipItem *)itemList.at(0);
1653     if (item->type() != AVWIDGET) {
1654         emit displayMessage(i18n("No clip selected"), ErrorMessage);
1655         return;
1656     }
1657     GenTime pos = GenTime(m_cursorPos, m_document->fps());
1658     if (item->startPos() > pos || item->endPos() < pos) {
1659         emit displayMessage(i18n("No selected clip at cursor time"), ErrorMessage);
1660         return;
1661     }
1662     ClipItem *clip = (ClipItem *) item;
1663     int id = clip->baseClip()->getId();
1664     GenTime position = pos - item->startPos() + item->cropStart();
1665     QString comment = clip->baseClip()->markerComment(position);
1666     if (comment.isEmpty()) {
1667         emit displayMessage(i18n("No marker found at cursor time"), ErrorMessage);
1668         return;
1669     }
1670     AddMarkerCommand *command = new AddMarkerCommand(this, comment, QString(), id, position, true);
1671     m_commandStack->push(command);
1672 }
1673
1674 void CustomTrackView::slotDeleteAllClipMarkers() {
1675     QList<QGraphicsItem *> itemList = scene()->selectedItems();
1676     if (itemList.count() != 1) {
1677         emit displayMessage(i18n("Cannot delete marker if more than one clip is selected"), ErrorMessage);
1678         kDebug() << "// CANNOT DELETE MARKER IF MORE TAN ONE CLIP IS SELECTED....";
1679         return;
1680     }
1681     AbstractClipItem *item = (AbstractClipItem *)itemList.at(0);
1682     if (item->type() != AVWIDGET) {
1683         emit displayMessage(i18n("No clip selected"), ErrorMessage);
1684         return;
1685     }
1686
1687     ClipItem *clip = static_cast <ClipItem *>(item);
1688     QList <CommentedTime> markers = clip->baseClip()->commentedSnapMarkers();
1689
1690     if (markers.isEmpty()) {
1691         emit displayMessage(i18n("Clip has no markers"), ErrorMessage);
1692         return;
1693     }
1694     int id = clip->baseClip()->getId();
1695     QUndoCommand *deleteMarkers = new QUndoCommand();
1696     deleteMarkers->setText("Delete clip markers");
1697
1698     for (int i = 0; i < markers.size(); i++) {
1699         new AddMarkerCommand(this, markers.at(i).comment(), QString(), id, markers.at(i).time(), true, deleteMarkers);
1700     }
1701     m_commandStack->push(deleteMarkers);
1702 }
1703
1704 void CustomTrackView::slotEditClipMarker() {
1705     QList<QGraphicsItem *> itemList = scene()->selectedItems();
1706     if (itemList.count() != 1) {
1707         emit displayMessage(i18n("Cannot edit marker if more than one clip is selected"), ErrorMessage);
1708         kDebug() << "// CANNOT DELETE MARKER IF MORE TAN ONE CLIP IS SELECTED....";
1709         return;
1710     }
1711     AbstractClipItem *item = (AbstractClipItem *)itemList.at(0);
1712     if (item->type() != AVWIDGET) {
1713         emit displayMessage(i18n("No clip at cursor time"), ErrorMessage);
1714         return;
1715     }
1716     GenTime pos = GenTime(m_cursorPos, m_document->fps());
1717     if (item->startPos() > pos || item->endPos() < pos) {
1718         emit displayMessage(i18n("No selected clip at cursor time"), ErrorMessage);
1719         return;
1720     }
1721     ClipItem *clip = (ClipItem *) item;
1722     int id = clip->baseClip()->getId();
1723     GenTime position = pos - item->startPos() + item->cropStart();
1724     QString oldcomment = clip->baseClip()->markerComment(position);
1725     if (oldcomment.isEmpty()) {
1726         emit displayMessage(i18n("No marker found at cursor time"), ErrorMessage);
1727         return;
1728     }
1729
1730     CommentedTime marker(position, oldcomment);
1731     MarkerDialog d(clip->baseClip(), marker, m_document->timecode(), this);
1732     if (d.exec() == QDialog::Accepted) {
1733         if (d.newMarker().time() == position) {
1734             // marker position was not changed, only text
1735             AddMarkerCommand *command = new AddMarkerCommand(this, oldcomment, d.newMarker().comment(), id, position, true);
1736             m_commandStack->push(command);
1737         } else {
1738             // marker text and position were changed, remove previous marker and add new one
1739             AddMarkerCommand *command1 = new AddMarkerCommand(this, oldcomment, QString(), id, position, true);
1740             AddMarkerCommand *command2 = new AddMarkerCommand(this, QString(), d.newMarker().comment(), id, d.newMarker().time(), true);
1741             m_commandStack->push(command1);
1742             m_commandStack->push(command2);
1743         }
1744     }
1745 }
1746
1747 void CustomTrackView::addMarker(const int id, const GenTime &pos, const QString comment) {
1748     DocClipBase *base = m_document->clipManager()->getClipById(id);
1749     if (!comment.isEmpty()) base->addSnapMarker(pos, comment);
1750     else base->deleteSnapMarker(pos);
1751     m_document->setModified(true);
1752     viewport()->update();
1753 }
1754
1755
1756
1757 void CustomTrackView::editGuide(const GenTime oldPos, const GenTime pos, const QString &comment) {
1758     if (oldPos > GenTime() && pos > GenTime()) {
1759         // move guide
1760         for (int i = 0; i < m_guides.count(); i++) {
1761             if (m_guides.at(i)->position() == oldPos) {
1762                 Guide *item = m_guides.at(i);
1763                 item->updateGuide(pos, comment);
1764                 break;
1765             }
1766         }
1767     } else if (pos > GenTime()) addGuide(pos, comment);
1768     else {
1769         // remove guide
1770         bool found = false;
1771         for (int i = 0; i < m_guides.count(); i++) {
1772             if (m_guides.at(i)->position() == oldPos) {
1773                 Guide *item = m_guides.takeAt(i);
1774                 delete item;
1775                 found = true;
1776                 break;
1777             }
1778         }
1779         if (!found) emit displayMessage(i18n("No guide at cursor time"), ErrorMessage);
1780     }
1781     m_document->syncGuides(m_guides);
1782 }
1783
1784 bool CustomTrackView::addGuide(const GenTime pos, const QString &comment) {
1785     for (int i = 0; i < m_guides.count(); i++) {
1786         if (m_guides.at(i)->position() == pos) {
1787             emit displayMessage(i18n("A guide already exists at that position"), ErrorMessage);
1788             return false;
1789         }
1790     }
1791     Guide *g = new Guide(this, pos, comment, m_scale, m_document->fps(), m_tracksHeight * m_tracksList.count());
1792     scene()->addItem(g);
1793     m_guides.append(g);
1794     m_document->syncGuides(m_guides);
1795     return true;
1796 }
1797
1798 void CustomTrackView::slotAddGuide() {
1799     CommentedTime marker(GenTime(m_cursorPos, m_document->fps()), i18n("Guide"));
1800     MarkerDialog d(NULL, marker, m_document->timecode(), this);
1801     if (d.exec() != QDialog::Accepted) return;
1802     if (addGuide(d.newMarker().time(), d.newMarker().comment())) {
1803         EditGuideCommand *command = new EditGuideCommand(this, GenTime(), QString(), d.newMarker().time(), d.newMarker().comment(), false);
1804         m_commandStack->push(command);
1805     }
1806 }
1807
1808 void CustomTrackView::slotEditGuide() {
1809     GenTime pos = GenTime(m_cursorPos, m_document->fps());
1810     bool found = false;
1811     for (int i = 0; i < m_guides.count(); i++) {
1812         if (m_guides.at(i)->position() == pos) {
1813             slotEditGuide(m_guides.at(i)->info());
1814             found = true;
1815             break;
1816         }
1817     }
1818     if (!found) emit displayMessage(i18n("No guide at cursor time"), ErrorMessage);
1819 }
1820
1821 void CustomTrackView::slotEditGuide(CommentedTime guide) {
1822     MarkerDialog d(NULL, guide, m_document->timecode(), this);
1823     if (d.exec() == QDialog::Accepted) {
1824         EditGuideCommand *command = new EditGuideCommand(this, guide.time(), guide.comment(), d.newMarker().time(), d.newMarker().comment(), true);
1825         m_commandStack->push(command);
1826     }
1827 }
1828
1829
1830 void CustomTrackView::slotDeleteGuide() {
1831     GenTime pos = GenTime(m_cursorPos, m_document->fps());
1832     bool found = false;
1833     for (int i = 0; i < m_guides.count(); i++) {
1834         if (m_guides.at(i)->position() == pos) {
1835             EditGuideCommand *command = new EditGuideCommand(this, m_guides.at(i)->position(), m_guides.at(i)->label(), GenTime(), QString(), true);
1836             m_commandStack->push(command);
1837             found = true;
1838             break;
1839         }
1840     }
1841     if (!found) emit displayMessage(i18n("No guide at cursor time"), ErrorMessage);
1842 }
1843
1844 void CustomTrackView::slotDeleteAllGuides() {
1845     QUndoCommand *deleteAll = new QUndoCommand();
1846     deleteAll->setText("Delete all guides");
1847     for (int i = 0; i < m_guides.count(); i++) {
1848         EditGuideCommand *command = new EditGuideCommand(this, m_guides.at(i)->position(), m_guides.at(i)->label(), GenTime(), QString(), true, deleteAll);
1849     }
1850     m_commandStack->push(deleteAll);
1851 }
1852
1853 void CustomTrackView::setTool(PROJECTTOOL tool) {
1854     m_tool = tool;
1855 }
1856
1857 void CustomTrackView::setScale(double scaleFactor) {
1858     //scale(scaleFactor, scaleFactor);
1859     m_animationTimer->stop();
1860     if (m_visualTip) {
1861         delete m_visualTip;
1862         m_visualTip = NULL;
1863     }
1864     if (m_animation) {
1865         delete m_animation;
1866         m_animation = NULL;
1867     }
1868     double pos = cursorPos() / m_scale;
1869     m_scale = scaleFactor;
1870     int vert = verticalScrollBar()->value();
1871     kDebug() << " HHHHHHHH  SCALING: " << m_scale;
1872     QList<QGraphicsItem *> itemList = items();
1873     for (int i = 0; i < itemList.count(); i++) {
1874         if (itemList.at(i)->type() == AVWIDGET || itemList.at(i)->type() == TRANSITIONWIDGET) {
1875             AbstractClipItem *clip = (AbstractClipItem *)itemList.at(i);
1876             clip->setRect(clip->startPos().frames(m_document->fps()) * m_scale, clip->rect().y(), clip->duration().frames(m_document->fps()) * m_scale, clip->rect().height());
1877         }
1878     }
1879
1880     for (int i = 0; i < m_guides.count(); i++) {
1881         m_guides.at(i)->updatePosition(m_scale);
1882     }
1883
1884     setSceneRect(0, 0, (m_projectDuration + 100) * m_scale, sceneRect().height());
1885     updateCursorPos();
1886     centerOn(QPointF(cursorPos(), m_tracksHeight));
1887     verticalScrollBar()->setValue(vert);
1888 }
1889
1890 void CustomTrackView::slotRefreshGuides() {
1891     if (KdenliveSettings::showmarkers()) {
1892         kDebug() << "// refresh GUIDES";
1893         for (int i = 0; i < m_guides.count(); i++) {
1894             m_guides.at(i)->update();
1895         }
1896     }
1897 }
1898
1899 void CustomTrackView::drawBackground(QPainter * painter, const QRectF & rect) {
1900     QRect rectInView = viewport()->rect();
1901     rectInView.moveTo(horizontalScrollBar()->value(), verticalScrollBar()->value());
1902
1903     QColor base = palette().button().color();
1904     painter->setClipRect(rect);
1905     painter->drawLine(rectInView.left(), 0, rectInView.right(), 0);
1906     uint max = m_tracksList.count();
1907     for (uint i = 0; i < max;i++) {
1908         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)));
1909         painter->drawLine(rectInView.left(), m_tracksHeight * (i + 1), rectInView.right(), m_tracksHeight * (i + 1));
1910         //painter->drawText(QRectF(10, 50 * i, 100, 50 * i + 49), Qt::AlignLeft, i18n(" Track ") + QString::number(i + 1));
1911     }
1912     int lowerLimit = m_tracksHeight * m_tracksList.count() + 1;
1913     if (height() > lowerLimit)
1914         painter->fillRect(QRectF(rectInView.left(), lowerLimit, rectInView.width(), height() - lowerLimit), QBrush(base));
1915 }
1916
1917 bool CustomTrackView::findString(const QString &text) {
1918     QString marker;
1919     for (int i = 0; i < m_searchPoints.size(); ++i) {
1920         marker = m_searchPoints.at(i).comment();
1921         if (marker.contains(text, Qt::CaseInsensitive)) {
1922             setCursorPos(m_searchPoints.at(i).time().frames(m_document->fps()), true);
1923             int vert = verticalScrollBar()->value();
1924             int hor = cursorPos();
1925             ensureVisible(hor, vert + 10, 2, 2, 50, 0);
1926             m_findIndex = i;
1927             return true;
1928         }
1929     }
1930     return false;
1931 }
1932
1933 bool CustomTrackView::findNextString(const QString &text) {
1934     QString marker;
1935     for (int i = m_findIndex + 1; i < m_searchPoints.size(); ++i) {
1936         marker = m_searchPoints.at(i).comment();
1937         if (marker.contains(text, Qt::CaseInsensitive)) {
1938             setCursorPos(m_searchPoints.at(i).time().frames(m_document->fps()), true);
1939             int vert = verticalScrollBar()->value();
1940             int hor = cursorPos();
1941             ensureVisible(hor, vert + 10, 2, 2, 50, 0);
1942             m_findIndex = i;
1943             return true;
1944         }
1945     }
1946     m_findIndex = -1;
1947     return false;
1948 }
1949
1950 void CustomTrackView::initSearchStrings() {
1951     m_searchPoints.clear();
1952     QList<QGraphicsItem *> itemList = items();
1953     for (int i = 0; i < itemList.count(); i++) {
1954         // parse all clip names
1955         if (itemList.at(i)->type() == AVWIDGET) {
1956             ClipItem *item = static_cast <ClipItem *>(itemList.at(i));
1957             GenTime start = item->startPos();
1958             CommentedTime t(start, item->clipName());
1959             m_searchPoints.append(t);
1960             // add all clip markers
1961             QList < CommentedTime > markers = item->commentedSnapMarkers();
1962             m_searchPoints += markers;
1963         }
1964     }
1965
1966     // add guides
1967     for (int i = 0; i < m_guides.count(); i++) {
1968         m_searchPoints.append(m_guides.at(i)->info());
1969     }
1970
1971     qSort(m_searchPoints);
1972 }
1973
1974 void CustomTrackView::clearSearchStrings() {
1975     m_searchPoints.clear();
1976     m_findIndex = 0;
1977 }
1978
1979 void CustomTrackView::copyClip() {
1980     while (m_copiedItems.count() > 0) {
1981         delete m_copiedItems.takeFirst();
1982     }
1983     QList<QGraphicsItem *> itemList = scene()->selectedItems();
1984     if (itemList.count() == 0) {
1985         emit displayMessage(i18n("Select a clip before copying"), ErrorMessage);
1986         return;
1987     }
1988     for (int i = 0; i < itemList.count(); i++) {
1989         if (itemList.at(i)->type() == AVWIDGET) {
1990             ClipItem *dup = static_cast <ClipItem *>(itemList.at(i));
1991             m_copiedItems.append(dup->clone(m_scale));
1992         } else if (itemList.at(i)->type() == TRANSITIONWIDGET) {
1993             Transition *dup = static_cast <Transition *>(itemList.at(i));
1994             m_copiedItems.append(dup->clone(m_scale));
1995         }
1996     }
1997 }
1998
1999 bool CustomTrackView::canBePastedTo(ItemInfo info, int type) const {
2000     QRectF rect((double) info.startPos.frames(m_document->fps()) * m_scale, (double)(info.track * m_tracksHeight + 1), (double)(info.endPos - info.startPos).frames(m_document->fps()) * m_scale, (double)(m_tracksHeight - 1));
2001     QList<QGraphicsItem *> collisions = scene()->items(rect, Qt::IntersectsItemBoundingRect);
2002     for (int i = 0; i < collisions.count(); i++) {
2003         if (collisions.at(i)->type() == type) return false;
2004     }
2005     return true;
2006 }
2007
2008 bool CustomTrackView::canBePasted(QList<AbstractClipItem *> items, GenTime offset, int trackOffset) const {
2009     for (int i = 0; i < items.count(); i++) {
2010         ItemInfo info = items.at(i)->info();
2011         info.startPos += offset;
2012         info.endPos += offset;
2013         info.track += trackOffset;
2014         if (!canBePastedTo(info, items.at(i)->type())) return false;
2015     }
2016     return true;
2017 }
2018
2019 bool CustomTrackView::canBeMoved(QList<AbstractClipItem *> items, GenTime offset, int trackOffset) const {
2020     QPainterPath movePath;
2021     movePath.moveTo(0, 0);
2022
2023     for (int i = 0; i < items.count(); i++) {
2024         ItemInfo info = items.at(i)->info();
2025         info.startPos = info.startPos + offset;
2026         info.endPos = info.endPos + offset;
2027         info.track = info.track + trackOffset;
2028         if (info.startPos < GenTime()) {
2029             // No clip should go below 0
2030             return false;
2031         }
2032         QRectF rect((double) info.startPos.frames(m_document->fps()) * m_scale, (double)(info.track * m_tracksHeight + 1), (double)(info.endPos - info.startPos).frames(m_document->fps()) * m_scale, (double)(m_tracksHeight - 1));
2033         movePath.addRect(rect);
2034     }
2035     QList<QGraphicsItem *> collisions = scene()->items(movePath, Qt::IntersectsItemBoundingRect);
2036     for (int i = 0; i < collisions.count(); i++) {
2037         if ((collisions.at(i)->type() == AVWIDGET || collisions.at(i)->type() == TRANSITIONWIDGET) && !items.contains(static_cast <AbstractClipItem *>(collisions.at(i)))) {
2038             kDebug() << "  ////////////   CLIP COLLISION, MOVE NOT ALLOWED";
2039             return false;
2040         }
2041     }
2042     return true;
2043 }
2044
2045 void CustomTrackView::pasteClip() {
2046     if (m_copiedItems.count() == 0) {
2047         emit displayMessage(i18n("No clip copied"), ErrorMessage);
2048         return;
2049     }
2050     QPoint position;
2051     if (m_menuPosition.isNull()) position = mapFromGlobal(QCursor::pos());
2052     else position = m_menuPosition;
2053     GenTime pos = GenTime((int)(mapToScene(position).x() / m_scale), m_document->fps());
2054     int track = (int)(position.y() / m_tracksHeight);
2055     ItemInfo first = m_copiedItems.at(0)->info();
2056
2057     GenTime offset = pos - first.startPos;
2058     int trackOffset = track - first.track;
2059
2060     if (!canBePasted(m_copiedItems, offset, trackOffset)) {
2061         emit displayMessage(i18n("Cannot paste selected clips"), ErrorMessage);
2062         return;
2063     }
2064     QUndoCommand *pasteClips = new QUndoCommand();
2065     pasteClips->setText("Paste clips");
2066
2067     for (int i = 0; i < m_copiedItems.count(); i++) {
2068         // parse all clip names
2069         if (m_copiedItems.at(i) && m_copiedItems.at(i)->type() == AVWIDGET) {
2070             ClipItem *clip = static_cast <ClipItem *>(m_copiedItems.at(i));
2071             ItemInfo info;
2072             info.startPos = clip->startPos() + offset;
2073             info.endPos = clip->endPos() + offset;
2074             info.cropStart = clip->cropStart();
2075             info.track = clip->track() + trackOffset;
2076             if (canBePastedTo(info, AVWIDGET)) {
2077                 new AddTimelineClipCommand(this, clip->xml(), clip->clipProducer(), info, clip->effectList(), true, false, pasteClips);
2078             } else emit displayMessage(i18n("Cannot paste clip to selected place"), ErrorMessage);
2079         } else if (m_copiedItems.at(i) && m_copiedItems.at(i)->type() == TRANSITIONWIDGET) {
2080             Transition *tr = static_cast <Transition *>(m_copiedItems.at(i));
2081             ItemInfo info;
2082             info.startPos = tr->startPos() + offset;
2083             info.endPos = tr->endPos() + offset;
2084             info.track = tr->track() + trackOffset;
2085             if (canBePastedTo(info, TRANSITIONWIDGET)) {
2086                 new AddTransitionCommand(this, info, tr->transitionEndTrack() + trackOffset, tr->toXML(), false, true, pasteClips);
2087             } else emit displayMessage(i18n("Cannot paste transition to selected place"), ErrorMessage);
2088         }
2089     }
2090     m_commandStack->push(pasteClips);
2091 }
2092
2093 void CustomTrackView::pasteClipEffects() {
2094     if (m_copiedItems.count() != 1 || m_copiedItems.at(0)->type() != AVWIDGET) {
2095         emit displayMessage(i18n("You must copy exactly one clip before pasting effects"), ErrorMessage);
2096         return;
2097     }
2098     ClipItem *clip = static_cast < ClipItem *>(m_copiedItems.at(0));
2099     EffectsList effects = clip->effectList();
2100
2101     QUndoCommand *paste = new QUndoCommand();
2102     paste->setText("Paste effects");
2103
2104     QList<QGraphicsItem *> clips = scene()->selectedItems();
2105     for (int i = 0; i < clips.count(); ++i) {
2106         if (clips.at(i)->type() == AVWIDGET) {
2107             ClipItem *item = static_cast < ClipItem *>(clips.at(i));
2108             for (int i = 0; i < clip->effectsCount(); i++) {
2109                 new AddEffectCommand(this, m_tracksList.count() - item->track(), item->startPos(), clip->effectAt(i), true, paste);
2110             }
2111         }
2112     }
2113     m_commandStack->push(paste);
2114 }
2115
2116
2117 /*
2118 void CustomTrackView::drawForeground ( QPainter * painter, const QRectF & rect )
2119 {
2120   //kDebug()<<"/////  DRAWING FB: "<<rect.x()<<", width: "<<rect.width();
2121   painter->fillRect(rect, QColor(50, rand() % 250,50,100));
2122   painter->drawLine(m_cursorPos, rect.y(), m_cursorPos, rect.y() + rect.height());
2123 }
2124 */
2125 #include "customtrackview.moc"