]> git.sesse.net Git - kdenlive/blob - src/customtrackview.cpp
indent fixes and geometry param will be used in initeffects
[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 = item->clone(m_scale, newPos);
821         scene()->addItem(dup);
822         m_document->renderer()->mltCutClip(m_tracksList.count() - info.track, cutTime);
823         item->baseClip()->addReference();
824         m_document->updateClip(item->baseClip()->getId());
825     } else {
826         // uncut clip
827         ClipItem *item = getClipItemAt((int) info.startPos.frames(m_document->fps()), info.track);
828         ClipItem *dup = getClipItemAt((int) cutTime.frames(m_document->fps()), info.track);
829         if (!item || !dup) {
830             emit displayMessage(i18n("Cannot find clip to uncut"), ErrorMessage);
831             return;
832         }
833         delete dup;
834         item->baseClip()->removeReference();
835         m_document->updateClip(item->baseClip()->getId());
836         item->resizeEnd((int) info.endPos.frames(m_document->fps()), m_scale);
837         m_document->renderer()->mltRemoveClip(m_tracksList.count() - info.track, cutTime);
838         m_document->renderer()->mltResizeClipEnd(m_tracksList.count() - info.track, info.startPos, item->cropStart(), item->cropStart() + info.endPos - info.startPos);
839     }
840 }
841
842
843 void CustomTrackView::slotAddTransitionToSelectedClips(QDomElement transition) {
844     QList<QGraphicsItem *> itemList = scene()->selectedItems();
845     for (int i = 0; i < itemList.count(); i++) {
846         if (itemList.at(i)->type() == AVWIDGET) {
847             ClipItem *item = (ClipItem *) itemList.at(i);
848             ItemInfo info;
849             info.startPos = item->startPos();
850             info.endPos = info.startPos + GenTime(2.5);
851             info.track = item->track();
852             int transitiontrack = getPreviousVideoTrack(info.track);
853             slotAddTransition(item, info, transitiontrack, transition);
854         }
855     }
856 }
857
858 void CustomTrackView::slotAddTransition(ClipItem* clip, ItemInfo transitionInfo, int endTrack, QDomElement transition) {
859     AddTransitionCommand* command = new AddTransitionCommand(this, transitionInfo, endTrack, transition, false, true);
860     m_commandStack->push(command);
861     m_document->setModified(true);
862 }
863
864 void CustomTrackView::addTransition(ItemInfo transitionInfo, int endTrack, QDomElement params) {
865     Transition *tr = new Transition(transitionInfo, endTrack, m_scale, m_document->fps(), params);
866     scene()->addItem(tr);
867
868     //kDebug() << "---- ADDING transition " << e.attribute("tag") << ", on tracks " << m_tracksList.count() - e.attribute("transition_track").toInt() << " / " << getPreviousVideoTrack(e.attribute("transition_track").toInt());
869     m_document->renderer()->mltAddTransition(tr->transitionTag(), endTrack, m_tracksList.count() - transitionInfo.track, transitionInfo.startPos, transitionInfo.endPos, tr->toXML());
870     m_document->setModified(true);
871 }
872
873 void CustomTrackView::deleteTransition(ItemInfo transitionInfo, int endTrack, QDomElement params) {
874     Transition *item = getTransitionItemAt((int)transitionInfo.startPos.frames(m_document->fps()) + 1, transitionInfo.track);
875     m_document->renderer()->mltDeleteTransition(item->transitionTag(), endTrack, m_tracksList.count() - transitionInfo.track, transitionInfo.startPos, transitionInfo.endPos, item->toXML());
876     delete item;
877     emit transitionItemSelected(NULL);
878     m_document->setModified(true);
879 }
880
881 void CustomTrackView::slotTransitionUpdated(Transition *tr, QDomElement old) {
882     EditTransitionCommand *command = new EditTransitionCommand(this, tr->track(), tr->startPos(), old, tr->toXML() , true);
883     m_commandStack->push(command);
884     m_document->setModified(true);
885 }
886
887 void CustomTrackView::updateTransition(int track, GenTime pos, QDomElement oldTransition, QDomElement transition) {
888     Transition *item = getTransitionItemAt((int)pos.frames(m_document->fps()) + 1, track);
889     if (!item) {
890         kWarning() << "Unable to find transition at pos :" << pos.frames(m_document->fps()) << ", ON track: " << track;
891         return;
892     }
893     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);
894     item->setTransitionParameters(transition);
895     m_document->setModified(true);
896 }
897
898 void CustomTrackView::addItem(DocClipBase *clip, QPoint pos) {
899     ItemInfo info;
900     info.startPos = GenTime((int)(mapToScene(pos).x() / m_scale), m_document->fps());
901     info.endPos = info.startPos + clip->duration();
902     info.track = (int)(pos.y() / m_tracksHeight);
903     kDebug() << "------------  ADDING CLIP ITEM----: " << info.startPos.frames(25) << ", " << info.endPos.frames(25) << ", " << info.track;
904     m_dropItem = new ClipItem(clip, info, m_scale, m_document->fps());
905     scene()->addItem(m_dropItem);
906 }
907
908
909 void CustomTrackView::dragMoveEvent(QDragMoveEvent * event) {
910     event->setDropAction(Qt::IgnoreAction);
911     if (m_dropItem) {
912         int track = (int)(mapToScene(event->pos()).y() / m_tracksHeight);  //) * (m_scale * 50) + m_scale;
913         m_dropItem->moveTo((int)(mapToScene(event->pos()).x() / m_scale), m_scale, (int)((track - m_dropItem->track()) * m_tracksHeight), track);
914         event->setDropAction(Qt::MoveAction);
915         if (event->mimeData()->hasFormat("kdenlive/producerslist")) {
916             event->acceptProposedAction();
917         }
918     } else {
919         QGraphicsView::dragMoveEvent(event);
920     }
921 }
922
923 void CustomTrackView::dragLeaveEvent(QDragLeaveEvent * event) {
924     if (m_dropItem) {
925         delete m_dropItem;
926         m_dropItem = NULL;
927     } else QGraphicsView::dragLeaveEvent(event);
928 }
929
930 void CustomTrackView::dropEvent(QDropEvent * event) {
931     if (m_dropItem) {
932         AddTimelineClipCommand *command = new AddTimelineClipCommand(this, m_dropItem->xml(), m_dropItem->clipProducer(), m_dropItem->info(), m_dropItem->effectList(), false, false);
933         m_commandStack->push(command);
934         m_dropItem->baseClip()->addReference();
935         m_document->updateClip(m_dropItem->baseClip()->getId());
936         ItemInfo info;
937         info = m_dropItem->info();
938         info.track = m_tracksList.count() - m_dropItem->track();
939         //kDebug()<<"IIIIIIIIIIIIIIIIIIIIIIII TRAX CNT: "<<m_tracksList.count()<<", DROP: "<<m_dropItem->track();
940         m_document->renderer()->mltInsertClip(info, m_dropItem->xml(), m_dropItem->baseClip()->producer());
941         //if (m_dropItem->baseClip()->isTransparent()) m_document->renderer()->mltAddClipTransparency(info, getPreviousVideoTrack(m_dropItem->track()), m_dropItem->baseClip()->getId());
942         m_dropItem = NULL;
943         m_document->setModified(true);
944     } else QGraphicsView::dropEvent(event);
945     m_dropItem = NULL;
946 }
947
948
949 QStringList CustomTrackView::mimeTypes() const {
950     QStringList qstrList;
951     // list of accepted mime types for drop
952     qstrList.append("text/plain");
953     qstrList.append("kdenlive/producerslist");
954     return qstrList;
955 }
956
957 Qt::DropActions CustomTrackView::supportedDropActions() const {
958     // returns what actions are supported when dropping
959     return Qt::MoveAction;
960 }
961
962 void CustomTrackView::setDuration(int duration) {
963     kDebug() << "/////////////  PRO DUR: " << duration << ", SCALE. " << (m_projectDuration + 500) * m_scale << ", height: " << 50 * m_tracksList.count();
964     m_projectDuration = duration;
965     setSceneRect(0, 0, (m_projectDuration + 100) * m_scale, sceneRect().height());
966 }
967
968 int CustomTrackView::duration() const {
969     return m_projectDuration;
970 }
971
972 void CustomTrackView::addTrack(TrackInfo type) {
973     m_tracksList << type;
974     m_cursorLine->setLine(m_cursorLine->line().x1(), 0, m_cursorLine->line().x1(), m_tracksHeight * m_tracksList.count());
975     setSceneRect(0, 0, sceneRect().width(), m_tracksHeight * m_tracksList.count());
976     verticalScrollBar()->setMaximum(m_tracksHeight * m_tracksList.count());
977     //setFixedHeight(50 * m_tracksCount);
978 }
979
980 void CustomTrackView::removeTrack() {
981     // TODO: implement track deletion
982     //m_tracksCount--;
983     m_cursorLine->setLine(m_cursorLine->line().x1(), 0, m_cursorLine->line().x1(), m_tracksHeight * m_tracksList.count());
984 }
985
986
987 void CustomTrackView::slotSwitchTrackAudio(int ix) {
988     int tracknumber = m_tracksList.count() - ix;
989     kDebug() << "/////  MUTING TRK: " << ix << "; PL NUM: " << tracknumber;
990     m_tracksList[tracknumber - 1].isMute = !m_tracksList.at(tracknumber - 1).isMute;
991     m_document->renderer()->mltChangeTrackState(tracknumber, m_tracksList.at(tracknumber - 1).isMute, m_tracksList.at(tracknumber - 1).isBlind);
992 }
993
994 void CustomTrackView::slotSwitchTrackVideo(int ix) {
995     int tracknumber = m_tracksList.count() - ix;
996     m_tracksList[tracknumber - 1].isBlind = !m_tracksList.at(tracknumber - 1).isBlind;
997     m_document->renderer()->mltChangeTrackState(tracknumber, m_tracksList.at(tracknumber - 1).isMute, m_tracksList.at(tracknumber - 1).isBlind);
998 }
999
1000 void CustomTrackView::deleteClip(int clipId) {
1001     QList<QGraphicsItem *> itemList = items();
1002     for (int i = 0; i < itemList.count(); i++) {
1003         if (itemList.at(i)->type() == AVWIDGET) {
1004             ClipItem *item = (ClipItem *)itemList.at(i);
1005             if (item->clipProducer() == clipId) {
1006                 AddTimelineClipCommand *command = new AddTimelineClipCommand(this, item->xml(), item->clipProducer(), item->info(), item->effectList(), true, true);
1007                 m_commandStack->push(command);
1008                 //delete item;
1009             }
1010         }
1011     }
1012 }
1013
1014 void CustomTrackView::setCursorPos(int pos, bool seek) {
1015     emit cursorMoved((int)(m_cursorPos * m_scale), (int)(pos * m_scale));
1016     m_cursorPos = pos;
1017     m_cursorLine->setPos(pos * m_scale, 0);
1018     if (seek) m_document->renderer()->seek(GenTime(pos, m_document->fps()));
1019     else if (m_autoScroll && m_scale < 50) checkScrolling();
1020 }
1021
1022 void CustomTrackView::updateCursorPos() {
1023     m_cursorLine->setPos(m_cursorPos * m_scale, 0);
1024 }
1025
1026 int CustomTrackView::cursorPos() {
1027     return (int)(m_cursorPos * m_scale);
1028 }
1029
1030 void CustomTrackView::moveCursorPos(int delta) {
1031     if (m_cursorPos + delta < 0) delta = 0 - m_cursorPos;
1032     emit cursorMoved((int)(m_cursorPos * m_scale), (int)((m_cursorPos + delta) * m_scale));
1033     m_cursorPos += delta;
1034     m_cursorLine->setPos(m_cursorPos * m_scale, 0);
1035     m_document->renderer()->seek(GenTime(m_cursorPos, m_document->fps()));
1036     //if (m_autoScroll && m_scale < 50) checkScrolling();
1037 }
1038
1039 void CustomTrackView::checkScrolling() {
1040     int vert = verticalScrollBar()->value();
1041     int hor = cursorPos();
1042     ensureVisible(hor, vert + 10, 2, 2, 50, 0);
1043     //centerOn(QPointF(cursorPos(), m_tracksHeight));
1044     /*QRect rectInView = viewport()->rect();
1045     int delta = rectInView.width() / 3;
1046     int max = rectInView.right() + horizontalScrollBar()->value() - delta;
1047     //kDebug() << "CURSOR POS: "<<m_cursorPos<< "Scale: "<<m_scale;
1048     if (m_cursorPos * m_scale >= max) horizontalScrollBar()->setValue((int)(horizontalScrollBar()->value() + 1 + m_scale));*/
1049 }
1050
1051 void CustomTrackView::mouseReleaseEvent(QMouseEvent * event) {
1052     if (event->button() == Qt::MidButton) {
1053         return;
1054     }
1055     QGraphicsView::mouseReleaseEvent(event);
1056     setDragMode(QGraphicsView::NoDrag);
1057     if (m_operationMode == MOVEGUIDE) {
1058         setCursor(Qt::ArrowCursor);
1059         m_operationMode = NONE;
1060         m_dragGuide->setFlag(QGraphicsItem::ItemIsMovable, false);
1061         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);
1062         m_commandStack->push(command);
1063         m_dragGuide->updateGuide(GenTime(m_dragGuide->pos().x() / m_scale, m_document->fps()));
1064         m_dragGuide = NULL;
1065         m_dragItem = NULL;
1066         return;
1067     }
1068     if (m_dragItem == NULL) {
1069         emit transitionItemSelected(NULL);
1070         return;
1071     }
1072     ItemInfo info;
1073     info.startPos = m_dragItem->startPos();
1074     info.endPos = m_dragItem->endPos();
1075     info.track = m_dragItem->track();
1076
1077     if (m_operationMode == MOVE) {
1078         setCursor(Qt::OpenHandCursor);
1079
1080         if (m_selectedClipList.count() == 1) {
1081             // we are moving one clip, easy
1082             if (m_dragItem->type() == AVWIDGET && (m_dragItemInfo.startPos != info.startPos || m_dragItemInfo.track != info.track)) {
1083                 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())));
1084                 if (success) {
1085                     MoveClipCommand *command = new MoveClipCommand(this, m_dragItemInfo, info, false);
1086                     m_commandStack->push(command);
1087                 } else {
1088                     // undo last move and emit error message
1089                     MoveClipCommand *command = new MoveClipCommand(this, info, m_dragItemInfo, true);
1090                     m_commandStack->push(command);
1091                     emit displayMessage(i18n("Cannot move clip to position %1seconds", QString::number(m_dragItemInfo.startPos.seconds(), 'g', 2)), ErrorMessage);
1092                 }
1093             }
1094             if (m_dragItem->type() == TRANSITIONWIDGET && (m_dragItemInfo.startPos != info.startPos || m_dragItemInfo.track != info.track)) {
1095                 MoveTransitionCommand *command = new MoveTransitionCommand(this, m_dragItemInfo, info, false);
1096                 m_commandStack->push(command);
1097                 Transition *transition = (Transition *) m_dragItem;
1098                 transition->updateTransitionEndTrack(getPreviousVideoTrack(m_dragItem->track()));
1099                 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);
1100             }
1101         } else {
1102             // Moving several clips. We need to delete them and readd them to new position,
1103             // or they might overlap each other during the move
1104
1105             GenTime timeOffset = info.startPos - m_dragItemInfo.startPos;
1106             int trackOffset = info.track - m_dragItemInfo.track;
1107             if (timeOffset != GenTime() || trackOffset != 0) {
1108                 QUndoCommand *moveClips = new QUndoCommand();
1109                 moveClips->setText("Move clips");
1110                 // remove items in MLT playlist
1111                 for (int i = 0; i < m_selectedClipList.count(); i++) {
1112                     AbstractClipItem *item = m_selectedClipList.at(i);
1113                     ItemInfo info = item->info();
1114                     info.startPos = info.startPos - timeOffset;
1115                     info.endPos = info.endPos - timeOffset;
1116                     info.track = info.track - trackOffset;
1117                     if (item->type() == AVWIDGET) {
1118                         ClipItem *clip = static_cast <ClipItem*>(item);
1119                         new AddTimelineClipCommand(this, clip->xml(), clip->clipProducer(), info, clip->effectList(), false, true, moveClips);
1120                         m_document->renderer()->mltRemoveClip(m_tracksList.count() - info.track, info.startPos);
1121                     } else {
1122                         Transition *tr = static_cast <Transition*>(item);
1123                         new AddTransitionCommand(this, info, tr->transitionEndTrack(), tr->toXML(), false, true, moveClips);
1124                         m_document->renderer()->mltDeleteTransition(tr->transitionTag(), tr->transitionEndTrack(), m_tracksList.count() - info.track, info.startPos, info.endPos, tr->toXML());
1125                     }
1126                 }
1127
1128                 for (int i = 0; i < m_selectedClipList.count(); i++) {
1129                     // re-add items in correct place
1130                     AbstractClipItem *item = m_selectedClipList.at(i);
1131                     if (item->type() == AVWIDGET) {
1132                         ClipItem *clip = static_cast <ClipItem*>(item);
1133                         new AddTimelineClipCommand(this, clip->xml(), clip->clipProducer(), item->info(), clip->effectList(), false, false, moveClips);
1134                         ItemInfo info = item->info();
1135                         info.track = m_tracksList.count() - item->track();
1136                         m_document->renderer()->mltInsertClip(info, clip->xml(), clip->baseClip()->producer());
1137                     } else {
1138                         Transition *tr = static_cast <Transition*>(item);
1139                         ItemInfo transitionInfo = tr->info();
1140                         new AddTransitionCommand(this, info, tr->transitionEndTrack(), tr->toXML(), false, false, moveClips);
1141                         m_document->renderer()->mltAddTransition(tr->transitionTag(), tr->transitionEndTrack() + trackOffset, m_tracksList.count() - transitionInfo.track, transitionInfo.startPos, transitionInfo.endPos, tr->toXML());
1142                     }
1143                 }
1144                 m_commandStack->push(moveClips);
1145             }
1146         }
1147
1148     } else if (m_operationMode == RESIZESTART && m_dragItem->startPos() != m_dragItemInfo.startPos) {
1149         // resize start
1150         if (m_dragItem->type() == AVWIDGET) {
1151             bool success = 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());
1152             if (success) {
1153                 updateClipFade((ClipItem *) m_dragItem);
1154                 ResizeClipCommand *command = new ResizeClipCommand(this, m_dragItemInfo, info, false);
1155                 m_commandStack->push(command);
1156             } else {
1157                 m_dragItem->resizeStart((int) m_dragItemInfo.startPos.frames(m_document->fps()), m_scale);
1158                 emit displayMessage(i18n("Error when resizing clip"), ErrorMessage);
1159             }
1160         } else if (m_dragItem->type() == TRANSITIONWIDGET) {
1161             MoveTransitionCommand *command = new MoveTransitionCommand(this, m_dragItemInfo, info, false);
1162             m_commandStack->push(command);
1163             Transition *transition = (Transition *) m_dragItem;
1164             m_document->renderer()->mltMoveTransition(transition->transitionTag(), (int)(m_tracksList.count() - m_dragItemInfo.track), (int)(m_tracksList.count() - m_dragItemInfo.track), 0, m_dragItemInfo.startPos, m_dragItemInfo.endPos, info.startPos, info.endPos);
1165         }
1166
1167         //m_document->renderer()->doRefresh();
1168     } else if (m_operationMode == RESIZEEND && m_dragItem->endPos() != m_dragItemInfo.endPos) {
1169         // resize end
1170         if (m_dragItem->type() == AVWIDGET) {
1171             ResizeClipCommand *command = new ResizeClipCommand(this, m_dragItemInfo, info, false);
1172             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());
1173             m_commandStack->push(command);
1174         } else if (m_dragItem->type() == TRANSITIONWIDGET) {
1175             MoveTransitionCommand *command = new MoveTransitionCommand(this, m_dragItemInfo, info, false);
1176             m_commandStack->push(command);
1177             Transition *transition = (Transition *) m_dragItem;
1178             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);
1179         }
1180         //m_document->renderer()->doRefresh();
1181     } else if (m_operationMode == FADEIN) {
1182         // resize fade in effect
1183         ClipItem * item = (ClipItem *) m_dragItem;
1184         QStringList clipeffects = item->effectNames();
1185         if (clipeffects.contains(i18n("Fade in"))) {
1186             int ix = clipeffects.indexOf(i18n("Fade in"));
1187             QDomElement oldeffect = item->effectAt(ix);
1188             int start = item->cropStart().frames(m_document->fps());
1189             int end = item->fadeIn();
1190             if (end == 0) {
1191                 slotDeleteEffect(item, oldeffect);
1192             } else {
1193                 end += start;
1194                 QDomElement effect = MainWindow::audioEffects.getEffectByName("Fade in");
1195                 EffectsList::setParameter(effect, "in", QString::number(start));
1196                 EffectsList::setParameter(effect, "out", QString::number(end));
1197                 slotUpdateClipEffect(item, oldeffect, effect, ix);
1198             }
1199         } else if (item->fadeIn() != 0) {
1200             QDomElement effect = MainWindow::audioEffects.getEffectByName("Fade in");
1201             int start = item->cropStart().frames(m_document->fps());
1202             int end = item->fadeIn() + start;
1203             EffectsList::setParameter(effect, "in", QString::number(start));
1204             EffectsList::setParameter(effect, "out", QString::number(end));
1205             slotAddEffect(effect, m_dragItem->startPos(), m_dragItem->track());
1206         }
1207     } else if (m_operationMode == FADEOUT) {
1208         // resize fade in effect
1209         ClipItem * item = (ClipItem *) m_dragItem;
1210         QStringList clipeffects = item->effectNames();
1211         if (clipeffects.contains(i18n("Fade out"))) {
1212             int ix = clipeffects.indexOf(i18n("Fade out"));
1213             QDomElement oldeffect = item->effectAt(ix);
1214             int end = (item->duration() + item->cropStart()).frames(m_document->fps());
1215             int start = item->fadeOut();
1216             if (start == 0) {
1217                 slotDeleteEffect(item, oldeffect);
1218             } else {
1219                 start = end - start;
1220                 QDomElement effect = MainWindow::audioEffects.getEffectByName("Fade out");
1221                 EffectsList::setParameter(effect, "in", QString::number(start));
1222                 EffectsList::setParameter(effect, "out", QString::number(end));
1223                 slotUpdateClipEffect(item, oldeffect, effect, ix);
1224             }
1225         } else if (item->fadeOut() != 0) {
1226             QDomElement effect = MainWindow::audioEffects.getEffectByName("Fade out");
1227             int end = (item->duration() + item->cropStart()).frames(m_document->fps());
1228             int start = end - item->fadeOut();
1229             EffectsList::setParameter(effect, "in", QString::number(start));
1230             EffectsList::setParameter(effect, "out", QString::number(end));
1231             slotAddEffect(effect, m_dragItem->startPos(), m_dragItem->track());
1232         }
1233     } else if (m_operationMode == KEYFRAME) {
1234         // update the MLT effect
1235         ClipItem * item = (ClipItem *) m_dragItem;
1236         QString previous = item->keyframes(item->selectedEffectIndex());
1237         item->updateKeyframeEffect();
1238         QString next = item->keyframes(item->selectedEffectIndex());
1239         EditKeyFrameCommand *command = new EditKeyFrameCommand(this, item->track(), item->startPos(), item->selectedEffectIndex(), previous, next, false);
1240         m_commandStack->push(command);
1241         updateEffect(m_tracksList.count() - item->track(), item->startPos(), item->selectedEffect(), item->selectedEffectIndex());
1242     }
1243
1244     emit transitionItemSelected((m_dragItem && m_dragItem->type() == TRANSITIONWIDGET) ? (Transition*) m_dragItem : NULL);
1245     m_document->setModified(true);
1246     m_operationMode = NONE;
1247 }
1248
1249 void CustomTrackView::deleteClip(ItemInfo info) {
1250     ClipItem *item = getClipItemAt((int) info.startPos.frames(m_document->fps()) + 1, info.track);
1251     if (!item) {
1252         kDebug() << "----------------  ERROR, CANNOT find clip to delete at...";// << rect.x();
1253         return;
1254     }
1255     if (item->isSelected()) emit clipItemSelected(NULL);
1256     item->baseClip()->removeReference();
1257     m_document->updateClip(item->baseClip()->getId());
1258     delete item;
1259     m_document->renderer()->mltRemoveClip(m_tracksList.count() - info.track, info.startPos);
1260     m_document->renderer()->doRefresh();
1261 }
1262
1263 void CustomTrackView::deleteSelectedClips() {
1264     QList<QGraphicsItem *> itemList = scene()->selectedItems();
1265     if (itemList.count() == 0) {
1266         emit displayMessage(i18n("Select clip to delete"), ErrorMessage);
1267         return;
1268     }
1269     QUndoCommand *deleteSelected = new QUndoCommand();
1270     deleteSelected->setText("Delete selected items");
1271     for (int i = 0; i < itemList.count(); i++) {
1272         if (itemList.at(i)->type() == AVWIDGET) {
1273             ClipItem *item = static_cast <ClipItem *>(itemList.at(i));
1274             new AddTimelineClipCommand(this, item->xml(), item->clipProducer(), item->info(), item->effectList(), true, true, deleteSelected);
1275         } else if (itemList.at(i)->type() == TRANSITIONWIDGET) {
1276             Transition *item = static_cast <Transition *>(itemList.at(i));
1277             ItemInfo info;
1278             info.startPos = item->startPos();
1279             info.endPos = item->endPos();
1280             info.track = item->track();
1281             new AddTransitionCommand(this, info, item->transitionEndTrack(), item->toXML(), true, true, deleteSelected);
1282         }
1283     }
1284     m_commandStack->push(deleteSelected);
1285 }
1286
1287 void CustomTrackView::changeClipSpeed() {
1288     QList<QGraphicsItem *> itemList = scene()->selectedItems();
1289     if (itemList.count() == 0) {
1290         emit displayMessage(i18n("Select clip to change speed"), ErrorMessage);
1291         return;
1292     }
1293     QUndoCommand *changeSelected = new QUndoCommand();
1294     changeSelected->setText("Edit clip speed");
1295     for (int i = 0; i < itemList.count(); i++) {
1296         if (itemList.at(i)->type() == AVWIDGET) {
1297             ClipItem *item = static_cast <ClipItem *>(itemList.at(i));
1298             ItemInfo info = item->info();
1299             int percent = QInputDialog::getInteger(this, i18n("Edit Clip Speed"), i18n("New speed (percents)"), 100, 1, 300);
1300             double speed = (double) percent / 100.0;
1301             if (item->speed() != speed)
1302                 new ChangeSpeedCommand(this, info, item->speed(), speed, item->clipProducer(), true, changeSelected);
1303         }
1304     }
1305     m_commandStack->push(changeSelected);
1306 }
1307
1308 void CustomTrackView::doChangeClipSpeed(ItemInfo info, double speed, int id) {
1309     DocClipBase *baseclip = m_document->clipManager()->getClipById(id);
1310     ClipItem *item = getClipItemAt((int) info.startPos.frames(m_document->fps()) + 1, info.track);
1311     info.track = m_tracksList.count() - item->track();
1312     m_document->renderer()->mltChangeClipSpeed(info, speed, baseclip->producer());
1313     item->setSpeed(speed);
1314     GenTime maxDuration = item->maxDuration();
1315     if (maxDuration < item->duration()) {
1316         info = item->info();
1317         ItemInfo endInfo = info;
1318         endInfo.endPos = info.startPos + maxDuration;
1319         ResizeClipCommand *command = new ResizeClipCommand(this, info, endInfo, true);
1320         m_commandStack->push(command);
1321     }
1322 }
1323
1324 void CustomTrackView::cutSelectedClips() {
1325     QList<QGraphicsItem *> itemList = scene()->selectedItems();
1326     GenTime currentPos = GenTime(m_cursorPos, m_document->fps());
1327     for (int i = 0; i < itemList.count(); i++) {
1328         if (itemList.at(i)->type() == AVWIDGET) {
1329             ClipItem *item = (ClipItem *) itemList.at(i);
1330             ItemInfo info;
1331             info.startPos = item->startPos();
1332             info.endPos = item->endPos();
1333             if (currentPos > info.startPos && currentPos <  info.endPos) {
1334                 info.track = item->track();
1335                 RazorClipCommand *command = new RazorClipCommand(this, info, currentPos, true);
1336                 m_commandStack->push(command);
1337             }
1338         }
1339     }
1340 }
1341
1342 void CustomTrackView::addClip(QDomElement xml, int clipId, ItemInfo info, EffectsList effects) {
1343     DocClipBase *baseclip = m_document->clipManager()->getClipById(clipId);
1344     ClipItem *item = new ClipItem(baseclip, info, m_scale, m_document->fps());
1345     item->setEffectList(effects);
1346     scene()->addItem(item);
1347     baseclip->addReference();
1348     m_document->updateClip(baseclip->getId());
1349     info.track = m_tracksList.count() - info.track;
1350     m_document->renderer()->mltInsertClip(info, xml, baseclip->producer());
1351     for (int i = 0; i < item->effectsCount(); i++) {
1352         m_document->renderer()->mltAddEffect(info.track, info.startPos, item->getEffectArgs(item->effectAt(i)), false);
1353     }
1354     m_document->renderer()->doRefresh();
1355 }
1356
1357 void CustomTrackView::slotUpdateClip(int clipId) {
1358     QList<QGraphicsItem *> list = scene()->items();
1359     ClipItem *clip = NULL;
1360     for (int i = 0; i < list.size(); ++i) {
1361         if (list.at(i)->type() == AVWIDGET) {
1362             clip = static_cast <ClipItem *>(list.at(i));
1363             if (clip->clipProducer() == clipId) {
1364                 clip->refreshClip();
1365                 ItemInfo info = clip->info();
1366                 info.track = m_tracksList.count() - clip->track();
1367                 m_document->renderer()->mltUpdateClip(info, clip->xml(), clip->baseClip()->producer());
1368             }
1369         }
1370     }
1371 }
1372
1373 ClipItem *CustomTrackView::getClipItemAt(int pos, int track) {
1374     QList<QGraphicsItem *> list = scene()->items(QPointF(pos * m_scale, track * m_tracksHeight + m_tracksHeight / 2));
1375     ClipItem *clip = NULL;
1376     for (int i = 0; i < list.size(); ++i) {
1377         if (list.at(i)->type() == AVWIDGET) {
1378             clip = static_cast <ClipItem *>(list.at(i));
1379             break;
1380         }
1381     }
1382     return clip;
1383 }
1384
1385 ClipItem *CustomTrackView::getClipItemAt(GenTime pos, int track) {
1386     int framepos = (int)(pos.frames(m_document->fps()) * m_scale);
1387     return getClipItemAt(framepos, track);
1388 }
1389
1390 Transition *CustomTrackView::getTransitionItemAt(int pos, int track) {
1391     QList<QGraphicsItem *> list = scene()->items(QPointF(pos * m_scale, (track + 1) * m_tracksHeight));
1392     Transition *clip = NULL;
1393     for (int i = 0; i < list.size(); ++i) {
1394         if (list.at(i)->type() == TRANSITIONWIDGET) {
1395             clip = static_cast <Transition *>(list.at(i));
1396             break;
1397         }
1398     }
1399     return clip;
1400 }
1401
1402 Transition *CustomTrackView::getTransitionItemAt(GenTime pos, int track) {
1403     int framepos = (int)(pos.frames(m_document->fps()) * m_scale);
1404     return getTransitionItemAt(framepos, track);
1405 }
1406
1407 void CustomTrackView::moveClip(const ItemInfo start, const ItemInfo end) {
1408     ClipItem *item = getClipItemAt((int) start.startPos.frames(m_document->fps()) + 1, start.track);
1409     if (!item) {
1410         emit displayMessage(i18n("Cannot move clip at time: %1s on track %2", QString::number(start.startPos.seconds(), 'g', 2), start.track), ErrorMessage);
1411         kDebug() << "----------------  ERROR, CANNOT find clip to move at.. ";// << startPos.x() * m_scale * FRAME_SIZE + 1 << ", " << startPos.y() * m_tracksHeight + m_tracksHeight / 2;
1412         return;
1413     }
1414     //kDebug() << "----------------  Move CLIP FROM: " << startPos.x() << ", END:" << endPos.x() << ",TRACKS: " << startPos.y() << " TO " << endPos.y();
1415
1416     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()));
1417     if (success) {
1418         item->moveTo((int) end.startPos.frames(m_document->fps()), m_scale, (int)((end.track - start.track) * m_tracksHeight), end.track);
1419     } else {
1420         // undo last move and emit error message
1421         emit displayMessage(i18n("Cannot move clip to position %1seconds", QString::number(end.startPos.seconds(), 'g', 2)), ErrorMessage);
1422     }
1423 }
1424
1425 void CustomTrackView::moveTransition(const ItemInfo start, const ItemInfo end) {
1426     Transition *item = getTransitionItemAt((int)start.startPos.frames(m_document->fps()) + 1, start.track);
1427     if (!item) {
1428         emit displayMessage(i18n("Cannot move transition at time: %1s on track %2", start.startPos.seconds(), start.track), ErrorMessage);
1429         kDebug() << "----------------  ERROR, CANNOT find transition to move... ";// << startPos.x() * m_scale * FRAME_SIZE + 1 << ", " << startPos.y() * m_tracksHeight + m_tracksHeight / 2;
1430         return;
1431     }
1432     //kDebug() << "----------------  Move TRANSITION FROM: " << startPos.x() << ", END:" << endPos.x() << ",TRACKS: " << oldtrack << " TO " << newtrack;
1433
1434     //kDebug()<<"///  RESIZE TRANS START: ("<< startPos.x()<<"x"<< startPos.y()<<") / ("<<endPos.x()<<"x"<< endPos.y()<<")";
1435     if (end.endPos - end.startPos == start.endPos - start.startPos) {
1436         // Transition was moved
1437         item->moveTo((int) end.startPos.frames(m_document->fps()), m_scale, (end.track - start.track) * m_tracksHeight, end.track);
1438     } else if (end.endPos == start.endPos) {
1439         // Transition start resize
1440         item->resizeStart((int) end.startPos.frames(m_document->fps()), m_scale);
1441     } else {
1442         // Transition end resize;
1443         item->resizeEnd((int) end.endPos.frames(m_document->fps()), m_scale);
1444     }
1445     //item->moveTransition(GenTime((int) (endPos.x() - startPos.x()), m_document->fps()));
1446     item->updateTransitionEndTrack(getPreviousVideoTrack(end.track));
1447     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);
1448 }
1449
1450 void CustomTrackView::resizeClip(const ItemInfo start, const ItemInfo end) {
1451     int offset = 0;
1452     bool resizeClipStart = true;
1453     if (start.startPos == end.startPos) resizeClipStart = false;
1454     /*if (resizeClipStart) offset = 1;
1455     else offset = -1;*/
1456     ClipItem *item = getClipItemAt((int)(start.startPos.frames(m_document->fps()) + offset), start.track);
1457     if (!item) {
1458         emit displayMessage(i18n("Cannot move clip at time: %1s on track %2", start.startPos.seconds(), start.track), ErrorMessage);
1459         kDebug() << "----------------  ERROR, CANNOT find clip to resize at... "; // << startPos;
1460         return;
1461     }
1462     if (resizeClipStart) {
1463         bool success = 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);
1464         if (success) {
1465             item->resizeStart((int) end.startPos.frames(m_document->fps()), m_scale);
1466             updateClipFade(item);
1467         } else emit displayMessage(i18n("Error when resizing clip"), ErrorMessage);
1468     } else {
1469         m_document->renderer()->mltResizeClipEnd(m_tracksList.count() - item->track(), item->startPos(), item->cropStart(), item->cropStart() + end.endPos - item->startPos());
1470         item->resizeEnd((int) end.endPos.frames(m_document->fps()), m_scale);
1471         updateClipFade(item, true);
1472     }
1473     m_document->renderer()->doRefresh();
1474 }
1475
1476 void CustomTrackView::updateClipFade(ClipItem * item, bool updateFadeOut) {
1477     if (!updateFadeOut) {
1478         int end = item->fadeIn();
1479         if (end != 0) {
1480             // there is a fade in effect
1481             QStringList clipeffects = item->effectNames();
1482             QDomElement oldeffect = item->effectAt(clipeffects.indexOf("Fade in"));
1483             int start = item->cropStart().frames(m_document->fps());
1484             end += start;
1485             EffectsList::setParameter(oldeffect, "in", QString::number(start));
1486             EffectsList::setParameter(oldeffect, "out", QString::number(end));
1487             QMap <QString, QString> effectParams = item->getEffectArgs(oldeffect);
1488             if (!m_document->renderer()->mltEditEffect(m_tracksList.count() - item->track(), item->startPos(), effectParams))
1489                 emit displayMessage(i18n("Problem editing effect"), ErrorMessage);
1490         }
1491     } else {
1492         int start = item->fadeOut();
1493         if (start != 0) {
1494             // there is a fade in effect
1495             QStringList clipeffects = item->effectNames();
1496             QDomElement oldeffect = item->effectAt(clipeffects.indexOf("Fade out"));
1497             int end = (item->duration() - item->cropStart()).frames(m_document->fps());
1498             start = end - start;
1499             EffectsList::setParameter(oldeffect, "in", QString::number(start));
1500             EffectsList::setParameter(oldeffect, "out", QString::number(end));
1501             QMap <QString, QString> effectParams = item->getEffectArgs(oldeffect);
1502             if (m_document->renderer()->mltEditEffect(m_tracksList.count() - item->track(), item->startPos(), effectParams))
1503                 emit displayMessage(i18n("Problem editing effect"), ErrorMessage);
1504         }
1505     }
1506 }
1507
1508 double CustomTrackView::getSnapPointForPos(double pos) {
1509     for (int i = 0; i < m_snapPoints.size(); ++i) {
1510         if (abs((int)(pos - m_snapPoints.at(i).frames(m_document->fps()) * m_scale)) < 10) {
1511             //kDebug()<<" FOUND SNAP POINT AT: "<<m_snapPoints.at(i)<<", current pos: "<<pos / m_scale;
1512             return m_snapPoints.at(i).frames(m_document->fps()) * m_scale + 0.5;
1513         }
1514         if (m_snapPoints.at(i).frames(m_document->fps() * m_scale) > pos) break;
1515     }
1516     return pos;
1517 }
1518
1519 void CustomTrackView::updateSnapPoints(AbstractClipItem *selected) {
1520     m_snapPoints.clear();
1521     if (!KdenliveSettings::snaptopoints()) return;
1522     GenTime offset;
1523     if (selected) offset = selected->duration();
1524     QList<QGraphicsItem *> itemList = items();
1525     for (int i = 0; i < itemList.count(); i++) {
1526         if (itemList.at(i)->type() == AVWIDGET && itemList.at(i) != selected) {
1527             ClipItem *item = static_cast <ClipItem *>(itemList.at(i));
1528             GenTime start = item->startPos();
1529             GenTime end = item->endPos();
1530             m_snapPoints.append(start);
1531             m_snapPoints.append(end);
1532             QList < GenTime > markers = item->snapMarkers();
1533             for (int i = 0; i < markers.size(); ++i) {
1534                 GenTime t = markers.at(i);
1535                 m_snapPoints.append(t);
1536                 if (t > offset) m_snapPoints.append(t - offset);
1537             }
1538             if (offset != GenTime()) {
1539                 if (start > offset) m_snapPoints.append(start - offset);
1540                 if (end > offset) m_snapPoints.append(end - offset);
1541             }
1542         } else if (itemList.at(i)->type() == TRANSITIONWIDGET) {
1543             Transition *transition = static_cast <Transition*>(itemList.at(i));
1544             GenTime start = transition->startPos();
1545             GenTime end = transition->endPos();
1546             m_snapPoints.append(start);
1547             m_snapPoints.append(end);
1548             if (offset != GenTime()) {
1549                 if (start > offset) m_snapPoints.append(start - offset);
1550                 if (end > offset) m_snapPoints.append(end - offset);
1551             }
1552         }
1553     }
1554
1555     // add cursor position
1556     GenTime pos = GenTime(m_cursorPos, m_document->fps());
1557     m_snapPoints.append(pos);
1558     if (offset != GenTime()) m_snapPoints.append(pos - offset);
1559
1560     // add guides
1561     for (int i = 0; i < m_guides.count(); i++) {
1562         m_snapPoints.append(m_guides.at(i)->position());
1563         if (offset != GenTime()) m_snapPoints.append(m_guides.at(i)->position() - offset);
1564     }
1565
1566     qSort(m_snapPoints);
1567     //for (int i = 0; i < m_snapPoints.size(); ++i)
1568     //    kDebug() << "SNAP POINT: " << m_snapPoints.at(i).frames(25);
1569 }
1570
1571 void CustomTrackView::slotSeekToPreviousSnap() {
1572     updateSnapPoints(NULL);
1573     GenTime pos = GenTime(m_cursorPos, m_document->fps());
1574     GenTime res = GenTime();
1575     for (int i = 0; i < m_snapPoints.size(); ++i) {
1576         if (m_snapPoints.at(i) >= pos) {
1577             if (i == 0) i = 1;
1578             res = m_snapPoints.at(i - 1);
1579             break;
1580         }
1581     }
1582     setCursorPos((int) res.frames(m_document->fps()));
1583     checkScrolling();
1584 }
1585
1586 void CustomTrackView::slotSeekToNextSnap() {
1587     updateSnapPoints(NULL);
1588     GenTime pos = GenTime(m_cursorPos, m_document->fps());
1589     GenTime res = GenTime(m_projectDuration, m_document->fps());
1590     for (int i = 0; i < m_snapPoints.size(); ++i) {
1591         if (m_snapPoints.at(i) > pos) {
1592             res = m_snapPoints.at(i);
1593             break;
1594         }
1595     }
1596     setCursorPos((int) res.frames(m_document->fps()));
1597     checkScrolling();
1598 }
1599
1600 void CustomTrackView::clipStart() {
1601     QList<QGraphicsItem *> itemList = scene()->selectedItems();
1602     for (int i = 0; i < itemList.count(); i++) {
1603         if (itemList.at(i)->type() == AVWIDGET) {
1604             ClipItem *item = (ClipItem *) itemList.at(i);
1605             setCursorPos((int) item->startPos().frames(m_document->fps()));
1606             checkScrolling();
1607             break;
1608         }
1609     }
1610 }
1611
1612 void CustomTrackView::clipEnd() {
1613     QList<QGraphicsItem *> itemList = scene()->selectedItems();
1614     for (int i = 0; i < itemList.count(); i++) {
1615         if (itemList.at(i)->type() == AVWIDGET) {
1616             ClipItem *item = (ClipItem *) itemList.at(i);
1617             setCursorPos((int) item->endPos().frames(m_document->fps()));
1618             checkScrolling();
1619             break;
1620         }
1621     }
1622 }
1623
1624 void CustomTrackView::slotAddClipMarker() {
1625     QList<QGraphicsItem *> itemList = scene()->selectedItems();
1626     if (itemList.count() != 1) {
1627         emit displayMessage(i18n("Cannot add marker if more than one clip is selected"), ErrorMessage);
1628         kDebug() << "// CANNOT ADD MARKER IF MORE TAN ONE CLIP IS SELECTED....";
1629         return;
1630     }
1631     AbstractClipItem *item = (AbstractClipItem *)itemList.at(0);
1632     if (item->type() != AVWIDGET) return;
1633     GenTime pos = GenTime(m_cursorPos, m_document->fps());
1634     if (item->startPos() > pos || item->endPos() < pos) return;
1635     ClipItem *clip = (ClipItem *) item;
1636     int id = clip->baseClip()->getId();
1637     GenTime position = pos - item->startPos() + item->cropStart();
1638     CommentedTime marker(position, i18n("Marker"));
1639     MarkerDialog d(clip->baseClip(), marker, m_document->timecode(), this);
1640     if (d.exec() == QDialog::Accepted) {
1641         slotAddClipMarker(id, d.newMarker().time(), d.newMarker().comment());
1642     }
1643 }
1644
1645 void CustomTrackView::slotAddClipMarker(int id, GenTime t, QString c) {
1646     QString oldcomment = m_document->clipManager()->getClipById(id)->markerComment(t);
1647     AddMarkerCommand *command = new AddMarkerCommand(this, oldcomment, c, id, t, true);
1648     m_commandStack->push(command);
1649 }
1650
1651 void CustomTrackView::slotDeleteClipMarker() {
1652     QList<QGraphicsItem *> itemList = scene()->selectedItems();
1653     if (itemList.count() != 1) {
1654         emit displayMessage(i18n("Cannot delete marker if more than one clip is selected"), ErrorMessage);
1655         kDebug() << "// CANNOT DELETE MARKER IF MORE TAN ONE CLIP IS SELECTED....";
1656         return;
1657     }
1658     AbstractClipItem *item = (AbstractClipItem *)itemList.at(0);
1659     if (item->type() != AVWIDGET) {
1660         emit displayMessage(i18n("No clip selected"), ErrorMessage);
1661         return;
1662     }
1663     GenTime pos = GenTime(m_cursorPos, m_document->fps());
1664     if (item->startPos() > pos || item->endPos() < pos) {
1665         emit displayMessage(i18n("No selected clip at cursor time"), ErrorMessage);
1666         return;
1667     }
1668     ClipItem *clip = (ClipItem *) item;
1669     int id = clip->baseClip()->getId();
1670     GenTime position = pos - item->startPos() + item->cropStart();
1671     QString comment = clip->baseClip()->markerComment(position);
1672     if (comment.isEmpty()) {
1673         emit displayMessage(i18n("No marker found at cursor time"), ErrorMessage);
1674         return;
1675     }
1676     AddMarkerCommand *command = new AddMarkerCommand(this, comment, QString(), id, position, true);
1677     m_commandStack->push(command);
1678 }
1679
1680 void CustomTrackView::slotDeleteAllClipMarkers() {
1681     QList<QGraphicsItem *> itemList = scene()->selectedItems();
1682     if (itemList.count() != 1) {
1683         emit displayMessage(i18n("Cannot delete marker if more than one clip is selected"), ErrorMessage);
1684         kDebug() << "// CANNOT DELETE MARKER IF MORE TAN ONE CLIP IS SELECTED....";
1685         return;
1686     }
1687     AbstractClipItem *item = (AbstractClipItem *)itemList.at(0);
1688     if (item->type() != AVWIDGET) {
1689         emit displayMessage(i18n("No clip selected"), ErrorMessage);
1690         return;
1691     }
1692
1693     ClipItem *clip = static_cast <ClipItem *>(item);
1694     QList <CommentedTime> markers = clip->baseClip()->commentedSnapMarkers();
1695
1696     if (markers.isEmpty()) {
1697         emit displayMessage(i18n("Clip has no markers"), ErrorMessage);
1698         return;
1699     }
1700     int id = clip->baseClip()->getId();
1701     QUndoCommand *deleteMarkers = new QUndoCommand();
1702     deleteMarkers->setText("Delete clip markers");
1703
1704     for (int i = 0; i < markers.size(); i++) {
1705         new AddMarkerCommand(this, markers.at(i).comment(), QString(), id, markers.at(i).time(), true, deleteMarkers);
1706     }
1707     m_commandStack->push(deleteMarkers);
1708 }
1709
1710 void CustomTrackView::slotEditClipMarker() {
1711     QList<QGraphicsItem *> itemList = scene()->selectedItems();
1712     if (itemList.count() != 1) {
1713         emit displayMessage(i18n("Cannot edit marker if more than one clip is selected"), ErrorMessage);
1714         kDebug() << "// CANNOT DELETE MARKER IF MORE TAN ONE CLIP IS SELECTED....";
1715         return;
1716     }
1717     AbstractClipItem *item = (AbstractClipItem *)itemList.at(0);
1718     if (item->type() != AVWIDGET) {
1719         emit displayMessage(i18n("No clip at cursor time"), ErrorMessage);
1720         return;
1721     }
1722     GenTime pos = GenTime(m_cursorPos, m_document->fps());
1723     if (item->startPos() > pos || item->endPos() < pos) {
1724         emit displayMessage(i18n("No selected clip at cursor time"), ErrorMessage);
1725         return;
1726     }
1727     ClipItem *clip = (ClipItem *) item;
1728     int id = clip->baseClip()->getId();
1729     GenTime position = pos - item->startPos() + item->cropStart();
1730     QString oldcomment = clip->baseClip()->markerComment(position);
1731     if (oldcomment.isEmpty()) {
1732         emit displayMessage(i18n("No marker found at cursor time"), ErrorMessage);
1733         return;
1734     }
1735
1736     CommentedTime marker(position, oldcomment);
1737     MarkerDialog d(clip->baseClip(), marker, m_document->timecode(), this);
1738     if (d.exec() == QDialog::Accepted) {
1739         if (d.newMarker().time() == position) {
1740             // marker position was not changed, only text
1741             AddMarkerCommand *command = new AddMarkerCommand(this, oldcomment, d.newMarker().comment(), id, position, true);
1742             m_commandStack->push(command);
1743         } else {
1744             // marker text and position were changed, remove previous marker and add new one
1745             AddMarkerCommand *command1 = new AddMarkerCommand(this, oldcomment, QString(), id, position, true);
1746             AddMarkerCommand *command2 = new AddMarkerCommand(this, QString(), d.newMarker().comment(), id, d.newMarker().time(), true);
1747             m_commandStack->push(command1);
1748             m_commandStack->push(command2);
1749         }
1750     }
1751 }
1752
1753 void CustomTrackView::addMarker(const int id, const GenTime &pos, const QString comment) {
1754     DocClipBase *base = m_document->clipManager()->getClipById(id);
1755     if (!comment.isEmpty()) base->addSnapMarker(pos, comment);
1756     else base->deleteSnapMarker(pos);
1757     m_document->setModified(true);
1758     viewport()->update();
1759 }
1760
1761
1762
1763 void CustomTrackView::editGuide(const GenTime oldPos, const GenTime pos, const QString &comment) {
1764     if (oldPos > GenTime() && pos > GenTime()) {
1765         // move guide
1766         for (int i = 0; i < m_guides.count(); i++) {
1767             if (m_guides.at(i)->position() == oldPos) {
1768                 Guide *item = m_guides.at(i);
1769                 item->updateGuide(pos, comment);
1770                 break;
1771             }
1772         }
1773     } else if (pos > GenTime()) addGuide(pos, comment);
1774     else {
1775         // remove guide
1776         bool found = false;
1777         for (int i = 0; i < m_guides.count(); i++) {
1778             if (m_guides.at(i)->position() == oldPos) {
1779                 Guide *item = m_guides.takeAt(i);
1780                 delete item;
1781                 found = true;
1782                 break;
1783             }
1784         }
1785         if (!found) emit displayMessage(i18n("No guide at cursor time"), ErrorMessage);
1786     }
1787     m_document->syncGuides(m_guides);
1788 }
1789
1790 bool CustomTrackView::addGuide(const GenTime pos, const QString &comment) {
1791     for (int i = 0; i < m_guides.count(); i++) {
1792         if (m_guides.at(i)->position() == pos) {
1793             emit displayMessage(i18n("A guide already exists at that position"), ErrorMessage);
1794             return false;
1795         }
1796     }
1797     Guide *g = new Guide(this, pos, comment, m_scale, m_document->fps(), m_tracksHeight * m_tracksList.count());
1798     scene()->addItem(g);
1799     m_guides.append(g);
1800     m_document->syncGuides(m_guides);
1801     return true;
1802 }
1803
1804 void CustomTrackView::slotAddGuide() {
1805     CommentedTime marker(GenTime(m_cursorPos, m_document->fps()), i18n("Guide"));
1806     MarkerDialog d(NULL, marker, m_document->timecode(), this);
1807     if (d.exec() != QDialog::Accepted) return;
1808     if (addGuide(d.newMarker().time(), d.newMarker().comment())) {
1809         EditGuideCommand *command = new EditGuideCommand(this, GenTime(), QString(), d.newMarker().time(), d.newMarker().comment(), false);
1810         m_commandStack->push(command);
1811     }
1812 }
1813
1814 void CustomTrackView::slotEditGuide() {
1815     GenTime pos = GenTime(m_cursorPos, m_document->fps());
1816     bool found = false;
1817     for (int i = 0; i < m_guides.count(); i++) {
1818         if (m_guides.at(i)->position() == pos) {
1819             slotEditGuide(m_guides.at(i)->info());
1820             found = true;
1821             break;
1822         }
1823     }
1824     if (!found) emit displayMessage(i18n("No guide at cursor time"), ErrorMessage);
1825 }
1826
1827 void CustomTrackView::slotEditGuide(CommentedTime guide) {
1828     MarkerDialog d(NULL, guide, m_document->timecode(), this);
1829     if (d.exec() == QDialog::Accepted) {
1830         EditGuideCommand *command = new EditGuideCommand(this, guide.time(), guide.comment(), d.newMarker().time(), d.newMarker().comment(), true);
1831         m_commandStack->push(command);
1832     }
1833 }
1834
1835
1836 void CustomTrackView::slotDeleteGuide() {
1837     GenTime pos = GenTime(m_cursorPos, m_document->fps());
1838     bool found = false;
1839     for (int i = 0; i < m_guides.count(); i++) {
1840         if (m_guides.at(i)->position() == pos) {
1841             EditGuideCommand *command = new EditGuideCommand(this, m_guides.at(i)->position(), m_guides.at(i)->label(), GenTime(), QString(), true);
1842             m_commandStack->push(command);
1843             found = true;
1844             break;
1845         }
1846     }
1847     if (!found) emit displayMessage(i18n("No guide at cursor time"), ErrorMessage);
1848 }
1849
1850 void CustomTrackView::slotDeleteAllGuides() {
1851     QUndoCommand *deleteAll = new QUndoCommand();
1852     deleteAll->setText("Delete all guides");
1853     for (int i = 0; i < m_guides.count(); i++) {
1854         EditGuideCommand *command = new EditGuideCommand(this, m_guides.at(i)->position(), m_guides.at(i)->label(), GenTime(), QString(), true, deleteAll);
1855     }
1856     m_commandStack->push(deleteAll);
1857 }
1858
1859 void CustomTrackView::setTool(PROJECTTOOL tool) {
1860     m_tool = tool;
1861 }
1862
1863 void CustomTrackView::setScale(double scaleFactor) {
1864     //scale(scaleFactor, scaleFactor);
1865     m_animationTimer->stop();
1866     if (m_visualTip) {
1867         delete m_visualTip;
1868         m_visualTip = NULL;
1869     }
1870     if (m_animation) {
1871         delete m_animation;
1872         m_animation = NULL;
1873     }
1874     double pos = cursorPos() / m_scale;
1875     m_scale = scaleFactor;
1876     int vert = verticalScrollBar()->value();
1877     kDebug() << " HHHHHHHH  SCALING: " << m_scale;
1878     QList<QGraphicsItem *> itemList = items();
1879     for (int i = 0; i < itemList.count(); i++) {
1880         if (itemList.at(i)->type() == AVWIDGET || itemList.at(i)->type() == TRANSITIONWIDGET) {
1881             AbstractClipItem *clip = (AbstractClipItem *)itemList.at(i);
1882             clip->setRect(clip->startPos().frames(m_document->fps()) * m_scale, clip->rect().y(), clip->duration().frames(m_document->fps()) * m_scale, clip->rect().height());
1883         }
1884     }
1885
1886     for (int i = 0; i < m_guides.count(); i++) {
1887         m_guides.at(i)->updatePosition(m_scale);
1888     }
1889
1890     setSceneRect(0, 0, (m_projectDuration + 100) * m_scale, sceneRect().height());
1891     updateCursorPos();
1892     centerOn(QPointF(cursorPos(), m_tracksHeight));
1893     verticalScrollBar()->setValue(vert);
1894 }
1895
1896 void CustomTrackView::slotRefreshGuides() {
1897     if (KdenliveSettings::showmarkers()) {
1898         kDebug() << "// refresh GUIDES";
1899         for (int i = 0; i < m_guides.count(); i++) {
1900             m_guides.at(i)->update();
1901         }
1902     }
1903 }
1904
1905 void CustomTrackView::drawBackground(QPainter * painter, const QRectF & rect) {
1906     QRect rectInView = viewport()->rect();
1907     rectInView.moveTo(horizontalScrollBar()->value(), verticalScrollBar()->value());
1908
1909     QColor base = palette().button().color();
1910     painter->setClipRect(rect);
1911     painter->drawLine(rectInView.left(), 0, rectInView.right(), 0);
1912     uint max = m_tracksList.count();
1913     for (uint i = 0; i < max;i++) {
1914         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)));
1915         painter->drawLine(rectInView.left(), m_tracksHeight * (i + 1), rectInView.right(), m_tracksHeight * (i + 1));
1916         //painter->drawText(QRectF(10, 50 * i, 100, 50 * i + 49), Qt::AlignLeft, i18n(" Track ") + QString::number(i + 1));
1917     }
1918     int lowerLimit = m_tracksHeight * m_tracksList.count() + 1;
1919     if (height() > lowerLimit)
1920         painter->fillRect(QRectF(rectInView.left(), lowerLimit, rectInView.width(), height() - lowerLimit), QBrush(base));
1921 }
1922
1923 bool CustomTrackView::findString(const QString &text) {
1924     QString marker;
1925     for (int i = 0; i < m_searchPoints.size(); ++i) {
1926         marker = m_searchPoints.at(i).comment();
1927         if (marker.contains(text, Qt::CaseInsensitive)) {
1928             setCursorPos(m_searchPoints.at(i).time().frames(m_document->fps()), true);
1929             int vert = verticalScrollBar()->value();
1930             int hor = cursorPos();
1931             ensureVisible(hor, vert + 10, 2, 2, 50, 0);
1932             m_findIndex = i;
1933             return true;
1934         }
1935     }
1936     return false;
1937 }
1938
1939 bool CustomTrackView::findNextString(const QString &text) {
1940     QString marker;
1941     for (int i = m_findIndex + 1; i < m_searchPoints.size(); ++i) {
1942         marker = m_searchPoints.at(i).comment();
1943         if (marker.contains(text, Qt::CaseInsensitive)) {
1944             setCursorPos(m_searchPoints.at(i).time().frames(m_document->fps()), true);
1945             int vert = verticalScrollBar()->value();
1946             int hor = cursorPos();
1947             ensureVisible(hor, vert + 10, 2, 2, 50, 0);
1948             m_findIndex = i;
1949             return true;
1950         }
1951     }
1952     m_findIndex = -1;
1953     return false;
1954 }
1955
1956 void CustomTrackView::initSearchStrings() {
1957     m_searchPoints.clear();
1958     QList<QGraphicsItem *> itemList = items();
1959     for (int i = 0; i < itemList.count(); i++) {
1960         // parse all clip names
1961         if (itemList.at(i)->type() == AVWIDGET) {
1962             ClipItem *item = static_cast <ClipItem *>(itemList.at(i));
1963             GenTime start = item->startPos();
1964             CommentedTime t(start, item->clipName());
1965             m_searchPoints.append(t);
1966             // add all clip markers
1967             QList < CommentedTime > markers = item->commentedSnapMarkers();
1968             m_searchPoints += markers;
1969         }
1970     }
1971
1972     // add guides
1973     for (int i = 0; i < m_guides.count(); i++) {
1974         m_searchPoints.append(m_guides.at(i)->info());
1975     }
1976
1977     qSort(m_searchPoints);
1978 }
1979
1980 void CustomTrackView::clearSearchStrings() {
1981     m_searchPoints.clear();
1982     m_findIndex = 0;
1983 }
1984
1985 void CustomTrackView::copyClip() {
1986     while (m_copiedItems.count() > 0) {
1987         delete m_copiedItems.takeFirst();
1988     }
1989     QList<QGraphicsItem *> itemList = scene()->selectedItems();
1990     if (itemList.count() == 0) {
1991         emit displayMessage(i18n("Select a clip before copying"), ErrorMessage);
1992         return;
1993     }
1994     for (int i = 0; i < itemList.count(); i++) {
1995         if (itemList.at(i)->type() == AVWIDGET) {
1996             ClipItem *dup = static_cast <ClipItem *>(itemList.at(i));
1997             m_copiedItems.append(dup->clone(m_scale, dup->info()));
1998         } else if (itemList.at(i)->type() == TRANSITIONWIDGET) {
1999             Transition *dup = static_cast <Transition *>(itemList.at(i));
2000             m_copiedItems.append(dup->clone(m_scale));
2001         }
2002     }
2003 }
2004
2005 bool CustomTrackView::canBePastedTo(ItemInfo info, int type) const {
2006     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));
2007     QList<QGraphicsItem *> collisions = scene()->items(rect, Qt::IntersectsItemBoundingRect);
2008     for (int i = 0; i < collisions.count(); i++) {
2009         if (collisions.at(i)->type() == type) return false;
2010     }
2011     return true;
2012 }
2013
2014 bool CustomTrackView::canBePasted(QList<AbstractClipItem *> items, GenTime offset, int trackOffset) const {
2015     for (int i = 0; i < items.count(); i++) {
2016         ItemInfo info = items.at(i)->info();
2017         info.startPos += offset;
2018         info.endPos += offset;
2019         info.track += trackOffset;
2020         if (!canBePastedTo(info, items.at(i)->type())) return false;
2021     }
2022     return true;
2023 }
2024
2025 bool CustomTrackView::canBeMoved(QList<AbstractClipItem *> items, GenTime offset, int trackOffset) const {
2026     QPainterPath movePath;
2027     movePath.moveTo(0, 0);
2028
2029     for (int i = 0; i < items.count(); i++) {
2030         ItemInfo info = items.at(i)->info();
2031         info.startPos = info.startPos + offset;
2032         info.endPos = info.endPos + offset;
2033         info.track = info.track + trackOffset;
2034         if (info.startPos < GenTime()) {
2035             // No clip should go below 0
2036             return false;
2037         }
2038         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));
2039         movePath.addRect(rect);
2040     }
2041     QList<QGraphicsItem *> collisions = scene()->items(movePath, Qt::IntersectsItemBoundingRect);
2042     for (int i = 0; i < collisions.count(); i++) {
2043         if ((collisions.at(i)->type() == AVWIDGET || collisions.at(i)->type() == TRANSITIONWIDGET) && !items.contains(static_cast <AbstractClipItem *>(collisions.at(i)))) {
2044             kDebug() << "  ////////////   CLIP COLLISION, MOVE NOT ALLOWED";
2045             return false;
2046         }
2047     }
2048     return true;
2049 }
2050
2051 void CustomTrackView::pasteClip() {
2052     if (m_copiedItems.count() == 0) {
2053         emit displayMessage(i18n("No clip copied"), ErrorMessage);
2054         return;
2055     }
2056     QPoint position;
2057     if (m_menuPosition.isNull()) position = mapFromGlobal(QCursor::pos());
2058     else position = m_menuPosition;
2059     GenTime pos = GenTime((int)(mapToScene(position).x() / m_scale), m_document->fps());
2060     int track = (int)(position.y() / m_tracksHeight);
2061     ItemInfo first = m_copiedItems.at(0)->info();
2062
2063     GenTime offset = pos - first.startPos;
2064     int trackOffset = track - first.track;
2065
2066     if (!canBePasted(m_copiedItems, offset, trackOffset)) {
2067         emit displayMessage(i18n("Cannot paste selected clips"), ErrorMessage);
2068         return;
2069     }
2070     QUndoCommand *pasteClips = new QUndoCommand();
2071     pasteClips->setText("Paste clips");
2072
2073     for (int i = 0; i < m_copiedItems.count(); i++) {
2074         // parse all clip names
2075         if (m_copiedItems.at(i) && m_copiedItems.at(i)->type() == AVWIDGET) {
2076             ClipItem *clip = static_cast <ClipItem *>(m_copiedItems.at(i));
2077             ItemInfo info;
2078             info.startPos = clip->startPos() + offset;
2079             info.endPos = clip->endPos() + offset;
2080             info.cropStart = clip->cropStart();
2081             info.track = clip->track() + trackOffset;
2082             if (canBePastedTo(info, AVWIDGET)) {
2083                 new AddTimelineClipCommand(this, clip->xml(), clip->clipProducer(), info, clip->effectList(), true, false, pasteClips);
2084             } else emit displayMessage(i18n("Cannot paste clip to selected place"), ErrorMessage);
2085         } else if (m_copiedItems.at(i) && m_copiedItems.at(i)->type() == TRANSITIONWIDGET) {
2086             Transition *tr = static_cast <Transition *>(m_copiedItems.at(i));
2087             ItemInfo info;
2088             info.startPos = tr->startPos() + offset;
2089             info.endPos = tr->endPos() + offset;
2090             info.track = tr->track() + trackOffset;
2091             if (canBePastedTo(info, TRANSITIONWIDGET)) {
2092                 new AddTransitionCommand(this, info, tr->transitionEndTrack() + trackOffset, tr->toXML(), false, true, pasteClips);
2093             } else emit displayMessage(i18n("Cannot paste transition to selected place"), ErrorMessage);
2094         }
2095     }
2096     m_commandStack->push(pasteClips);
2097 }
2098
2099 void CustomTrackView::pasteClipEffects() {
2100     if (m_copiedItems.count() != 1 || m_copiedItems.at(0)->type() != AVWIDGET) {
2101         emit displayMessage(i18n("You must copy exactly one clip before pasting effects"), ErrorMessage);
2102         return;
2103     }
2104     ClipItem *clip = static_cast < ClipItem *>(m_copiedItems.at(0));
2105     EffectsList effects = clip->effectList();
2106
2107     QUndoCommand *paste = new QUndoCommand();
2108     paste->setText("Paste effects");
2109
2110     QList<QGraphicsItem *> clips = scene()->selectedItems();
2111     for (int i = 0; i < clips.count(); ++i) {
2112         if (clips.at(i)->type() == AVWIDGET) {
2113             ClipItem *item = static_cast < ClipItem *>(clips.at(i));
2114             for (int i = 0; i < clip->effectsCount(); i++) {
2115                 new AddEffectCommand(this, m_tracksList.count() - item->track(), item->startPos(), clip->effectAt(i), true, paste);
2116             }
2117         }
2118     }
2119     m_commandStack->push(paste);
2120 }
2121
2122
2123 /*
2124 void CustomTrackView::drawForeground ( QPainter * painter, const QRectF & rect )
2125 {
2126   //kDebug()<<"/////  DRAWING FB: "<<rect.x()<<", width: "<<rect.width();
2127   painter->fillRect(rect, QColor(50, rand() % 250,50,100));
2128   painter->drawLine(m_cursorPos, rect.y(), m_cursorPos, rect.y() + rect.height());
2129 }
2130 */
2131 #include "customtrackview.moc"