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