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