]> git.sesse.net Git - kdenlive/blob - src/customtrackview.cpp
96f904875f78a501ac1a4361f347adea85db9dfc
[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     m_document->renderer()->mltRemoveEffect(track, pos, "-1", false);
635     for (int i = 0; i < clip->effectsCount(); i++) {
636         m_document->renderer()->mltAddEffect(track, pos, clip->getEffectArgs(clip->effectAt(i)), false);
637     }
638     m_document->renderer()->doRefresh();
639 }
640
641 void CustomTrackView::addEffect(int track, GenTime pos, QDomElement effect) {
642     ClipItem *clip = getClipItemAt((int)pos.frames(m_document->fps()) + 1, m_tracksList.count() - track);
643     if (clip) {
644         QMap <QString, QString> effectParams = clip->addEffect(effect);
645         m_document->renderer()->mltAddEffect(track, pos, effectParams);
646         emit clipItemSelected(clip);
647     }
648 }
649
650 void CustomTrackView::deleteEffect(int track, GenTime pos, QDomElement effect) {
651     QString index = effect.attribute("kdenlive_ix");
652     m_document->renderer()->mltRemoveEffect(track, pos, index);
653     ClipItem *clip = getClipItemAt((int)pos.frames(m_document->fps()) + 1, m_tracksList.count() - track);
654     if (clip) {
655         clip->deleteEffect(index);
656         emit clipItemSelected(clip);
657     }
658 }
659
660 void CustomTrackView::slotAddEffect(QDomElement effect, GenTime pos, int track) {
661     QList<QGraphicsItem *> itemList;
662     if (track == -1) itemList = scene()->selectedItems();
663     if (itemList.isEmpty()) {
664         ClipItem *clip = getClipItemAt((int)pos.frames(m_document->fps()) + 1, track);
665         if (clip) itemList.append(clip);
666         else emit displayMessage(i18n("Select a clip if you want to apply an effect"), ErrorMessage);
667     }
668     kDebug() << "// REQUESTING EFFECT ON CLIP: " << pos.frames(25) << ", TRK: " << track << "SELECTED ITEMS: " << itemList.count();
669     for (int i = 0; i < itemList.count(); i++) {
670         if (itemList.at(i)->type() == AVWIDGET) {
671             ClipItem *item = (ClipItem *)itemList.at(i);
672             item->initEffect(effect);
673             AddEffectCommand *command = new AddEffectCommand(this, m_tracksList.count() - item->track(), item->startPos(), effect, true);
674             m_commandStack->push(command);
675         }
676     }
677     m_document->setModified(true);
678 }
679
680 void CustomTrackView::slotDeleteEffect(ClipItem *clip, QDomElement effect) {
681     AddEffectCommand *command = new AddEffectCommand(this, m_tracksList.count() - clip->track(), clip->startPos(), effect, false);
682     m_commandStack->push(command);
683     m_document->setModified(true);
684 }
685
686 void CustomTrackView::updateEffect(int track, GenTime pos, QDomElement effect) {
687     ClipItem *clip = getClipItemAt((int)pos.frames(m_document->fps()) + 1, m_tracksList.count() - track);
688     if (clip) {
689         QMap <QString, QString> effectParams = clip->getEffectArgs(effect);
690         if (effectParams.value("disabled") == "1") {
691             QString index = effectParams.value("kdenlive_ix");
692             m_document->renderer()->mltRemoveEffect(track, pos, index);
693         } else m_document->renderer()->mltEditEffect(m_tracksList.count() - clip->track(), clip->startPos(), effectParams);
694     }
695     m_document->setModified(true);
696 }
697
698 void CustomTrackView::moveEffect(int track, GenTime pos, int oldPos, int newPos) {
699     ClipItem *clip = getClipItemAt((int)pos.frames(m_document->fps()) + 1, m_tracksList.count() - track);
700     if (clip) {
701         m_document->renderer()->mltMoveEffect(track, pos, oldPos, newPos);
702     }
703     m_document->setModified(true);
704 }
705
706 void CustomTrackView::slotChangeEffectState(ClipItem *clip, QDomElement effect, bool disable) {
707     QDomElement oldEffect = effect.cloneNode().toElement();
708     effect.setAttribute("disabled", disable);
709     EditEffectCommand *command = new EditEffectCommand(this, m_tracksList.count() - clip->track(), clip->startPos(), oldEffect, effect, true);
710     m_commandStack->push(command);
711     m_document->setModified(true);
712 }
713
714 void CustomTrackView::slotChangeEffectPosition(ClipItem *clip, int currentPos, int newPos) {
715     MoveEffectCommand *command = new MoveEffectCommand(this, m_tracksList.count() - clip->track(), clip->startPos(), currentPos, newPos, true);
716     m_commandStack->push(command);
717     m_document->setModified(true);
718 }
719
720 void CustomTrackView::slotUpdateClipEffect(ClipItem *clip, QDomElement oldeffect, QDomElement effect) {
721     EditEffectCommand *command = new EditEffectCommand(this, m_tracksList.count() - clip->track(), clip->startPos(), oldeffect, effect, true);
722     m_commandStack->push(command);
723 }
724
725 void CustomTrackView::cutClip(ItemInfo info, GenTime cutTime, bool cut) {
726     if (cut) {
727         // cut clip
728         ClipItem *item = getClipItemAt((int) info.startPos.frames(m_document->fps()), info.track);
729         int cutPos = (int) cutTime.frames(m_document->fps());
730         ItemInfo newPos;
731         newPos.startPos = cutTime;
732         newPos.endPos = info.endPos;
733         newPos.track = info.track;
734         ClipItem *dup = new ClipItem(item->baseClip(), newPos, item->cropStart(), m_scale, m_document->fps());
735         dup->setCropStart(dup->cropStart() + (cutTime - info.startPos));
736         item->resizeEnd(cutPos, m_scale);
737         scene()->addItem(dup);
738         m_document->renderer()->mltCutClip(m_tracksList.count() - info.track, cutTime);
739     } else {
740         // uncut clip
741         ClipItem *item = getClipItemAt((int) info.startPos.frames(m_document->fps()), info.track);
742         ClipItem *dup = getClipItemAt((int) cutTime.frames(m_document->fps()), info.track);
743         delete dup;
744         item->resizeEnd((int) info.endPos.frames(m_document->fps()), m_scale);
745         m_document->renderer()->mltRemoveClip(m_tracksList.count() - info.track, cutTime);
746         m_document->renderer()->mltResizeClipEnd(m_tracksList.count() - info.track, info.startPos, item->cropStart(), item->cropStart() + info.endPos - info.startPos);
747     }
748 }
749
750
751 void CustomTrackView::slotAddTransitionToSelectedClips(QDomElement transition) {
752     QList<QGraphicsItem *> itemList = scene()->selectedItems();
753     for (int i = 0; i < itemList.count(); i++) {
754         if (itemList.at(i)->type() == AVWIDGET) {
755             ClipItem *item = (ClipItem *) itemList.at(i);
756             ItemInfo info;
757             info.startPos = item->startPos();
758             info.endPos = info.startPos + GenTime(2.5);
759             info.track = item->track();
760             int transitiontrack = getPreviousVideoTrack(info.track);
761             slotAddTransition(item, info, transitiontrack, transition);
762         }
763     }
764 }
765
766 void CustomTrackView::slotAddTransition(ClipItem* clip, ItemInfo transitionInfo, int endTrack, QDomElement transition) {
767     AddTransitionCommand* command = new AddTransitionCommand(this, transitionInfo, endTrack, transition, false, true);
768     m_commandStack->push(command);
769     m_document->setModified(true);
770 }
771
772 void CustomTrackView::addTransition(ItemInfo transitionInfo, int endTrack, QDomElement params) {
773     Transition *tr = new Transition(transitionInfo, endTrack, m_scale, m_document->fps(), params);
774     scene()->addItem(tr);
775
776     //kDebug() << "---- ADDING transition " << e.attribute("tag") << ", on tracks " << m_tracksList.count() - e.attribute("transition_track").toInt() << " / " << getPreviousVideoTrack(e.attribute("transition_track").toInt());
777     m_document->renderer()->mltAddTransition(tr->transitionTag(), endTrack, m_tracksList.count() - transitionInfo.track, transitionInfo.startPos, transitionInfo.endPos, tr->toXML());
778     m_document->setModified(true);
779 }
780
781 void CustomTrackView::deleteTransition(ItemInfo transitionInfo, int endTrack, QDomElement params) {
782     Transition *item = getTransitionItemAt((int)transitionInfo.startPos.frames(m_document->fps()) + 1, transitionInfo.track);
783     m_document->renderer()->mltDeleteTransition(item->transitionTag(), endTrack, m_tracksList.count() - transitionInfo.track, transitionInfo.startPos, transitionInfo.endPos, item->toXML());
784     delete item;
785     emit transitionItemSelected(NULL);
786     m_document->setModified(true);
787 }
788
789 void CustomTrackView::slotTransitionUpdated(Transition *tr, QDomElement old) {
790     EditTransitionCommand *command = new EditTransitionCommand(this, tr->track(), tr->startPos(), old, tr->toXML() , true);
791     m_commandStack->push(command);
792     m_document->setModified(true);
793 }
794
795 void CustomTrackView::updateTransition(int track, GenTime pos, QDomElement oldTransition, QDomElement transition) {
796     Transition *item = getTransitionItemAt((int)pos.frames(m_document->fps()) + 1, track);
797     if (!item) {
798         kWarning() << "Unable to find transition at pos :" << pos.frames(m_document->fps()) << ", ON track: " << track;
799         return;
800     }
801     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);
802     m_document->setModified(true);
803 }
804
805 void CustomTrackView::addItem(DocClipBase *clip, QPoint pos) {
806     ItemInfo info;
807     info.startPos = GenTime((int)(mapToScene(pos).x() / m_scale), m_document->fps());
808     info.endPos = info.startPos + clip->duration();
809     info.track = (int)(pos.y() / m_tracksHeight);
810     //kDebug()<<"------------  ADDING CLIP ITEM----: "<<info.startPos.frames(25)<<", "<<info.endPos.frames(25)<<", "<<info.track;
811     m_dropItem = new ClipItem(clip, info, GenTime(), m_scale, m_document->fps());
812     scene()->addItem(m_dropItem);
813 }
814
815
816 void CustomTrackView::dragMoveEvent(QDragMoveEvent * event) {
817     event->setDropAction(Qt::IgnoreAction);
818     if (m_dropItem) {
819         int track = (int)(mapToScene(event->pos()).y() / m_tracksHeight);  //) * (m_scale * 50) + m_scale;
820         m_dropItem->moveTo((int)(mapToScene(event->pos()).x() / m_scale), m_scale, (int)((track - m_dropItem->track()) * m_tracksHeight), track);
821         event->setDropAction(Qt::MoveAction);
822         if (event->mimeData()->hasFormat("kdenlive/producerslist")) {
823             event->acceptProposedAction();
824         }
825     } else {
826         QGraphicsView::dragMoveEvent(event);
827     }
828 }
829
830 void CustomTrackView::dragLeaveEvent(QDragLeaveEvent * event) {
831     if (m_dropItem) {
832         delete m_dropItem;
833         m_dropItem = NULL;
834     } else QGraphicsView::dragLeaveEvent(event);
835 }
836
837 void CustomTrackView::dropEvent(QDropEvent * event) {
838     if (m_dropItem) {
839         ItemInfo info;
840         info.startPos = m_dropItem->startPos();
841         info.endPos = m_dropItem->endPos();
842         info.track = m_dropItem->track();
843         AddTimelineClipCommand *command = new AddTimelineClipCommand(this, m_dropItem->xml(), m_dropItem->clipProducer(), info, false, false);
844         m_commandStack->push(command);
845         m_dropItem->baseClip()->addReference();
846         m_document->updateClip(m_dropItem->baseClip()->getId());
847         // kDebug()<<"IIIIIIIIIIIIIIIIIIIIIIII TRAX CNT: "<<m_tracksList.count()<<", DROP: "<<m_dropItem->track();
848         m_document->renderer()->mltInsertClip(m_tracksList.count() - m_dropItem->track(), m_dropItem->startPos(), m_dropItem->xml());
849         m_document->setModified(true);
850     } else QGraphicsView::dropEvent(event);
851     m_dropItem = NULL;
852 }
853
854
855 QStringList CustomTrackView::mimeTypes() const {
856     QStringList qstrList;
857     // list of accepted mime types for drop
858     qstrList.append("text/plain");
859     qstrList.append("kdenlive/producerslist");
860     return qstrList;
861 }
862
863 Qt::DropActions CustomTrackView::supportedDropActions() const {
864     // returns what actions are supported when dropping
865     return Qt::MoveAction;
866 }
867
868 void CustomTrackView::setDuration(int duration) {
869     kDebug() << "/////////////  PRO DUR: " << duration << ", SCALE. " << (m_projectDuration + 500) * m_scale << ", height: " << 50 * m_tracksList.count();
870     m_projectDuration = duration;
871     setSceneRect(0, 0, (m_projectDuration + 100) * m_scale, sceneRect().height());
872 }
873
874 int CustomTrackView::duration() const {
875     return m_projectDuration;
876 }
877
878 void CustomTrackView::addTrack(TrackInfo type) {
879     m_tracksList << type;
880     m_cursorLine->setLine(m_cursorLine->line().x1(), 0, m_cursorLine->line().x1(), m_tracksHeight * m_tracksList.count());
881     setSceneRect(0, 0, sceneRect().width(), m_tracksHeight * m_tracksList.count());
882     verticalScrollBar()->setMaximum(m_tracksHeight * m_tracksList.count());
883     //setFixedHeight(50 * m_tracksCount);
884 }
885
886 void CustomTrackView::removeTrack() {
887     // TODO: implement track deletion
888     //m_tracksCount--;
889     m_cursorLine->setLine(m_cursorLine->line().x1(), 0, m_cursorLine->line().x1(), m_tracksHeight * m_tracksList.count());
890 }
891
892
893 void CustomTrackView::slotSwitchTrackAudio(int ix) {
894     int tracknumber = m_tracksList.count() - ix;
895     kDebug() << "/////  MUTING TRK: " << ix << "; PL NUM: " << tracknumber;
896     m_tracksList[tracknumber - 1].isMute = !m_tracksList.at(tracknumber - 1).isMute;
897     m_document->renderer()->mltChangeTrackState(tracknumber, m_tracksList.at(tracknumber - 1).isMute, m_tracksList.at(tracknumber - 1).isBlind);
898 }
899
900 void CustomTrackView::slotSwitchTrackVideo(int ix) {
901     int tracknumber = m_tracksList.count() - ix;
902     m_tracksList[tracknumber - 1].isBlind = !m_tracksList.at(tracknumber - 1).isBlind;
903     m_document->renderer()->mltChangeTrackState(tracknumber, m_tracksList.at(tracknumber - 1).isMute, m_tracksList.at(tracknumber - 1).isBlind);
904 }
905
906 void CustomTrackView::deleteClip(int clipId) {
907     QList<QGraphicsItem *> itemList = items();
908     for (int i = 0; i < itemList.count(); i++) {
909         if (itemList.at(i)->type() == AVWIDGET) {
910             ClipItem *item = (ClipItem *)itemList.at(i);
911             if (item->clipProducer() == clipId) {
912                 ItemInfo info;
913                 info.startPos = item->startPos();
914                 info.endPos = item->endPos();
915                 info.track = item->track();
916                 AddTimelineClipCommand *command = new AddTimelineClipCommand(this, item->xml(), item->clipProducer(), info, true, true);
917                 m_commandStack->push(command);
918                 //delete item;
919             }
920         }
921     }
922 }
923
924 void CustomTrackView::setCursorPos(int pos, bool seek) {
925     emit cursorMoved((int)(m_cursorPos * m_scale), (int)(pos * m_scale));
926     m_cursorPos = pos;
927     m_cursorLine->setPos(pos * m_scale, 0);
928     if (seek) m_document->renderer()->seek(GenTime(pos, m_document->fps()));
929     else if (m_autoScroll && m_scale < 50) checkScrolling();
930 }
931
932 void CustomTrackView::updateCursorPos() {
933     m_cursorLine->setPos(m_cursorPos * m_scale, 0);
934 }
935
936 int CustomTrackView::cursorPos() {
937     return (int)(m_cursorPos * m_scale);
938 }
939
940 void CustomTrackView::moveCursorPos(int delta) {
941     emit cursorMoved((int)(m_cursorPos * m_scale), (int)((m_cursorPos + delta) * m_scale));
942     m_cursorPos += delta;
943     m_cursorLine->setPos(m_cursorPos * m_scale, 0);
944     m_document->renderer()->seek(GenTime(m_cursorPos, m_document->fps()));
945     //if (m_autoScroll && m_scale < 50) checkScrolling();
946 }
947
948 void CustomTrackView::checkScrolling() {
949     QRect rectInView = viewport()->rect();
950     int delta = rectInView.width() / 3;
951     int max = rectInView.right() + horizontalScrollBar()->value() - delta;
952     //kDebug() << "CURSOR POS: "<<m_cursorPos<< "Scale: "<<m_scale;
953     if (m_cursorPos * m_scale >= max) horizontalScrollBar()->setValue((int)(horizontalScrollBar()->value() + 1 + m_scale));
954 }
955
956 void CustomTrackView::mouseReleaseEvent(QMouseEvent * event) {
957     if (event->button() == Qt::MidButton) {
958         return;
959     }
960     QGraphicsView::mouseReleaseEvent(event);
961     setDragMode(QGraphicsView::NoDrag);
962     if (m_operationMode == MOVEGUIDE) {
963         setCursor(Qt::ArrowCursor);
964         m_operationMode = NONE;
965         m_dragGuide->setFlag(QGraphicsItem::ItemIsMovable, false);
966         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);
967         m_commandStack->push(command);
968         m_dragGuide->update(GenTime(m_dragGuide->pos().x() / m_scale, m_document->fps()));
969         m_dragGuide = NULL;
970         m_dragItem = NULL;
971         return;
972     }
973     if (m_dragItem == NULL) {
974         emit transitionItemSelected(NULL);
975         return;
976     }
977     ItemInfo info;
978     info.startPos = m_dragItem->startPos();
979     info.endPos = m_dragItem->endPos();
980     info.track = m_dragItem->track();
981
982     if (m_operationMode == MOVE) {
983         setCursor(Qt::OpenHandCursor);
984         // move clip
985         if (m_dragItem->type() == AVWIDGET && (m_dragItemInfo.startPos != info.startPos || m_dragItemInfo.track != info.track)) {
986             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())));
987                         if (success) {
988                                 MoveClipCommand *command = new MoveClipCommand(this, m_dragItemInfo, info, false);
989                                 m_commandStack->push(command);
990                         }
991                         else {
992                                 // undo last move and emit error message
993                                 MoveClipCommand *command = new MoveClipCommand(this, info, m_dragItemInfo, true);
994                                 m_commandStack->push(command);
995                                 emit displayMessage(i18n("Cannot move clip to requested position"), ErrorMessage);
996                         }
997         }
998         if (m_dragItem->type() == TRANSITIONWIDGET && (m_dragItemInfo.startPos != info.startPos || m_dragItemInfo.track != info.track)) {
999             MoveTransitionCommand *command = new MoveTransitionCommand(this, m_dragItemInfo, info, false);
1000             m_commandStack->push(command);
1001             Transition *transition = (Transition *) m_dragItem;
1002             transition->updateTransitionEndTrack(getPreviousVideoTrack(m_dragItem->track()));
1003             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);
1004         }
1005
1006     } else if (m_operationMode == RESIZESTART && m_dragItem->startPos() != m_dragItemInfo.startPos) {
1007         // resize start
1008         if (m_dragItem->type() == AVWIDGET) {
1009             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());
1010             updateClipFade((ClipItem *) m_dragItem);
1011             ResizeClipCommand *command = new ResizeClipCommand(this, m_dragItemInfo, info, false);
1012             m_commandStack->push(command);
1013         } else if (m_dragItem->type() == TRANSITIONWIDGET) {
1014             MoveTransitionCommand *command = new MoveTransitionCommand(this, m_dragItemInfo, info, false);
1015             m_commandStack->push(command);
1016             Transition *transition = (Transition *) m_dragItem;
1017             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);
1018         }
1019
1020         //m_document->renderer()->doRefresh();
1021     } else if (m_operationMode == RESIZEEND && m_dragItem->endPos() != m_dragItemInfo.endPos) {
1022         // resize end
1023         if (m_dragItem->type() == AVWIDGET) {
1024             ResizeClipCommand *command = new ResizeClipCommand(this, m_dragItemInfo, info, false);
1025             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());
1026             m_commandStack->push(command);
1027         } else if (m_dragItem->type() == TRANSITIONWIDGET) {
1028             MoveTransitionCommand *command = new MoveTransitionCommand(this, m_dragItemInfo, info, false);
1029             m_commandStack->push(command);
1030             Transition *transition = (Transition *) m_dragItem;
1031             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);
1032         }
1033         //m_document->renderer()->doRefresh();
1034     } else if (m_operationMode == FADEIN) {
1035         // resize fade in effect
1036         ClipItem * item = (ClipItem *) m_dragItem;
1037         QStringList clipeffects = item->effectNames();
1038         if (clipeffects.contains(i18n("Fade in"))) {
1039             QDomElement oldeffect = item->effectAt(clipeffects.indexOf("Fade in"));
1040             int start = item->cropStart().frames(m_document->fps());
1041             int end = item->fadeIn();
1042             if (end == 0) {
1043                 slotDeleteEffect(item, oldeffect);
1044             } else {
1045                 end += start;
1046                 QDomElement effect = MainWindow::audioEffects.getEffectByName("Fade in");
1047                 EffectsList::setParameter(effect, "in", QString::number(start));
1048                 EffectsList::setParameter(effect, "out", QString::number(end));
1049                 slotUpdateClipEffect(item, oldeffect, effect);
1050             }
1051         } else if (item->fadeIn() != 0) {
1052             QDomElement effect = MainWindow::audioEffects.getEffectByName("Fade in");
1053             int start = item->cropStart().frames(m_document->fps());
1054             int end = item->fadeIn() + start;
1055             EffectsList::setParameter(effect, "in", QString::number(start));
1056             EffectsList::setParameter(effect, "out", QString::number(end));
1057             slotAddEffect(effect, m_dragItem->startPos(), m_dragItem->track());
1058         }
1059     } else if (m_operationMode == FADEOUT) {
1060         // resize fade in effect
1061         ClipItem * item = (ClipItem *) m_dragItem;
1062         QStringList clipeffects = item->effectNames();
1063         if (clipeffects.contains(i18n("Fade out"))) {
1064             QDomElement oldeffect = item->effectAt(clipeffects.indexOf("Fade out"));
1065             int end = (item->duration() + item->cropStart()).frames(m_document->fps());
1066             int start = item->fadeOut();
1067             if (start == 0) {
1068                 slotDeleteEffect(item, oldeffect);
1069             } else {
1070                 start = end - start;
1071                 QDomElement effect = MainWindow::audioEffects.getEffectByName("Fade out");
1072                 EffectsList::setParameter(effect, "in", QString::number(start));
1073                 EffectsList::setParameter(effect, "out", QString::number(end));
1074                 slotUpdateClipEffect(item, oldeffect, effect);
1075             }
1076         } else if (item->fadeOut() != 0) {
1077             QDomElement effect = MainWindow::audioEffects.getEffectByName("Fade out");
1078             int end = (item->duration() + item->cropStart()).frames(m_document->fps());
1079             int start = end - item->fadeOut();
1080             EffectsList::setParameter(effect, "in", QString::number(start));
1081             EffectsList::setParameter(effect, "out", QString::number(end));
1082             slotAddEffect(effect, m_dragItem->startPos(), m_dragItem->track());
1083         }
1084     } else if (m_operationMode == KEYFRAME) {
1085         // update the MLT effect
1086         ClipItem * item = (ClipItem *) m_dragItem;
1087         QString previous = item->keyframes(item->selectedEffectIndex());
1088         item->updateKeyframeEffect();
1089         QString next = item->keyframes(item->selectedEffectIndex());
1090         EditKeyFrameCommand *command = new EditKeyFrameCommand(this, item->track(), item->startPos(), item->selectedEffectIndex(), previous, next, false);
1091         m_commandStack->push(command);
1092         updateEffect(m_tracksList.count() - item->track(), item->startPos(), item->selectedEffect());
1093     }
1094
1095     emit transitionItemSelected((m_dragItem && m_dragItem->type() == TRANSITIONWIDGET) ? (Transition*) m_dragItem : NULL);
1096     m_document->setModified(true);
1097     m_operationMode = NONE;
1098 }
1099
1100 void CustomTrackView::deleteClip(ItemInfo info) {
1101     ClipItem *item = getClipItemAt((int) info.startPos.frames(m_document->fps()), info.track);
1102     if (!item) {
1103         kDebug() << "----------------  ERROR, CANNOT find clip to move at...";// << rect.x();
1104         return;
1105     }
1106     if (item->isSelected()) emit clipItemSelected(NULL);
1107     item->baseClip()->removeReference();
1108     m_document->updateClip(item->baseClip()->getId());
1109     delete item;
1110     m_document->renderer()->mltRemoveClip(m_tracksList.count() - info.track, info.startPos);
1111     m_document->renderer()->doRefresh();
1112 }
1113
1114 void CustomTrackView::deleteSelectedClips() {
1115     QList<QGraphicsItem *> itemList = scene()->selectedItems();
1116     for (int i = 0; i < itemList.count(); i++) {
1117         if (itemList.at(i)->type() == AVWIDGET) {
1118             ClipItem *item = (ClipItem *) itemList.at(i);
1119             ItemInfo info;
1120             info.startPos = item->startPos();
1121             info.endPos = item->endPos();
1122             info.track = item->track();
1123             AddTimelineClipCommand *command = new AddTimelineClipCommand(this, item->xml(), item->clipProducer(), info, true, true);
1124             m_commandStack->push(command);
1125         } else if (itemList.at(i)->type() == TRANSITIONWIDGET) {
1126             Transition *item = (Transition *) itemList.at(i);
1127             ItemInfo info;
1128             info.startPos = item->startPos();
1129             info.endPos = item->endPos();
1130             info.track = item->track();
1131             AddTransitionCommand *command = new AddTransitionCommand(this, info, item->transitionEndTrack(), QDomElement(), true, true);
1132             m_commandStack->push(command);
1133         }
1134     }
1135 }
1136
1137 void CustomTrackView::cutSelectedClips() {
1138     QList<QGraphicsItem *> itemList = scene()->selectedItems();
1139     GenTime currentPos = GenTime(m_cursorPos, m_document->fps());
1140     for (int i = 0; i < itemList.count(); i++) {
1141         if (itemList.at(i)->type() == AVWIDGET) {
1142             ClipItem *item = (ClipItem *) itemList.at(i);
1143             ItemInfo info;
1144             info.startPos = item->startPos();
1145             info.endPos = item->endPos();
1146             if (currentPos > info.startPos && currentPos <  info.endPos) {
1147                 info.track = item->track();
1148                 RazorClipCommand *command = new RazorClipCommand(this, info, currentPos, true);
1149                 m_commandStack->push(command);
1150             }
1151         }
1152     }
1153 }
1154
1155 void CustomTrackView::addClip(QDomElement xml, int clipId, ItemInfo info) {
1156     DocClipBase *baseclip = m_document->clipManager()->getClipById(clipId);
1157     int crop = xml.attribute("in").toInt();
1158     ClipItem *item = new ClipItem(baseclip, info, GenTime(crop, m_document->fps()), m_scale, m_document->fps());
1159     scene()->addItem(item);
1160     baseclip->addReference();
1161     m_document->updateClip(baseclip->getId());
1162     m_document->renderer()->mltInsertClip(m_tracksList.count() - info.track, info.startPos, xml);
1163     m_document->renderer()->doRefresh();
1164 }
1165
1166 void CustomTrackView::slotUpdateClip(int clipId) {
1167     QList<QGraphicsItem *> list = scene()->items();
1168     ClipItem *clip = NULL;
1169     for (int i = 0; i < list.size(); ++i) {
1170         if (list.at(i)->type() == AVWIDGET) {
1171             clip = static_cast <ClipItem *>(list.at(i));
1172             if (clip->clipProducer() == clipId) {
1173                 clip->refreshClip();
1174                 m_document->renderer()->mltUpdateClip(m_tracksList.count() - clip->track(), clip->startPos(), clip->xml());
1175             }
1176         }
1177     }
1178 }
1179
1180 ClipItem *CustomTrackView::getClipItemAt(int pos, int track) {
1181     QList<QGraphicsItem *> list = scene()->items(QPointF(pos * m_scale, track * m_tracksHeight + m_tracksHeight / 2));
1182     ClipItem *clip = NULL;
1183     for (int i = 0; i < list.size(); ++i) {
1184         if (list.at(i)->type() == AVWIDGET) {
1185             clip = static_cast <ClipItem *>(list.at(i));
1186             break;
1187         }
1188     }
1189     return clip;
1190 }
1191
1192 ClipItem *CustomTrackView::getClipItemAt(GenTime pos, int track) {
1193     int framepos = (int)(pos.frames(m_document->fps()) * m_scale);
1194     return getClipItemAt(framepos, track);
1195 }
1196
1197 Transition *CustomTrackView::getTransitionItemAt(int pos, int track) {
1198     QList<QGraphicsItem *> list = scene()->items(QPointF(pos * m_scale, (track + 1) * m_tracksHeight));
1199     Transition *clip = NULL;
1200     for (int i = 0; i < list.size(); ++i) {
1201         if (list.at(i)->type() == TRANSITIONWIDGET) {
1202             clip = static_cast <Transition *>(list.at(i));
1203             break;
1204         }
1205     }
1206     return clip;
1207 }
1208
1209 Transition *CustomTrackView::getTransitionItemAt(GenTime pos, int track) {
1210     int framepos = (int)(pos.frames(m_document->fps()) * m_scale);
1211     return getTransitionItemAt(framepos, track);
1212 }
1213
1214 void CustomTrackView::moveClip(const ItemInfo start, const ItemInfo end) {
1215     ClipItem *item = getClipItemAt((int) start.startPos.frames(m_document->fps()) + 1, start.track);
1216     if (!item) {
1217         emit displayMessage(i18n("Cannot move clip at time: %1s on track %2", start.startPos.seconds(), start.track), ErrorMessage);
1218         kDebug() << "----------------  ERROR, CANNOT find clip to move at.. ";// << startPos.x() * m_scale * FRAME_SIZE + 1 << ", " << startPos.y() * m_tracksHeight + m_tracksHeight / 2;
1219         return;
1220     }
1221     //kDebug() << "----------------  Move CLIP FROM: " << startPos.x() << ", END:" << endPos.x() << ",TRACKS: " << startPos.y() << " TO " << endPos.y();
1222     
1223     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()));
1224         if (success) {
1225                 item->moveTo((int) end.startPos.frames(m_document->fps()), m_scale, (int)((end.track - start.track) * m_tracksHeight), end.track);
1226         }
1227         else {
1228                 // undo last move and emit error message
1229                 emit displayMessage(i18n("Cannot move clip to requested position"), ErrorMessage);
1230         }
1231 }
1232
1233 void CustomTrackView::moveTransition(const ItemInfo start, const ItemInfo end) {
1234     Transition *item = getTransitionItemAt((int)start.startPos.frames(m_document->fps()) + 1, start.track);
1235     if (!item) {
1236         emit displayMessage(i18n("Cannot move transition at time: %1s on track %2", start.startPos.seconds(), start.track), ErrorMessage);
1237         kDebug() << "----------------  ERROR, CANNOT find transition to move... ";// << startPos.x() * m_scale * FRAME_SIZE + 1 << ", " << startPos.y() * m_tracksHeight + m_tracksHeight / 2;
1238         return;
1239     }
1240     //kDebug() << "----------------  Move TRANSITION FROM: " << startPos.x() << ", END:" << endPos.x() << ",TRACKS: " << oldtrack << " TO " << newtrack;
1241
1242     //kDebug()<<"///  RESIZE TRANS START: ("<< startPos.x()<<"x"<< startPos.y()<<") / ("<<endPos.x()<<"x"<< endPos.y()<<")";
1243     if (end.endPos - end.startPos == start.endPos - start.startPos) {
1244         // Transition was moved
1245         item->moveTo((int) end.startPos.frames(m_document->fps()), m_scale, (end.track - start.track) * m_tracksHeight, end.track);
1246     } else if (end.endPos == start.endPos) {
1247         // Transition start resize
1248         item->resizeStart((int) end.startPos.frames(m_document->fps()), m_scale);
1249     } else {
1250         // Transition end resize;
1251         item->resizeEnd((int) end.endPos.frames(m_document->fps()), m_scale);
1252     }
1253     //item->moveTransition(GenTime((int) (endPos.x() - startPos.x()), m_document->fps()));
1254     item->updateTransitionEndTrack(getPreviousVideoTrack(end.track));
1255     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);
1256 }
1257
1258 void CustomTrackView::resizeClip(const ItemInfo start, const ItemInfo end) {
1259     int offset;
1260     bool resizeClipStart = true;
1261     if (start.startPos == end.startPos) resizeClipStart = false;
1262     if (resizeClipStart) offset = 1;
1263     else offset = -1;
1264     ClipItem *item = getClipItemAt((int)(start.startPos.frames(m_document->fps()) + offset), start.track);
1265     if (!item) {
1266         emit displayMessage(i18n("Cannot move clip at time: %1s on track %2", start.startPos.seconds(), start.track), ErrorMessage);
1267         kDebug() << "----------------  ERROR, CANNOT find clip to resize at... "; // << startPos;
1268         return;
1269     }
1270     if (resizeClipStart) {
1271         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);
1272         item->resizeStart((int) end.startPos.frames(m_document->fps()), m_scale);
1273         updateClipFade(item);
1274     } else {
1275         m_document->renderer()->mltResizeClipEnd(m_tracksList.count() - item->track(), item->startPos(), item->cropStart(), item->cropStart() + end.startPos - item->startPos());
1276         item->resizeEnd((int) end.startPos.frames(m_document->fps()), m_scale);
1277         updateClipFade(item, true);
1278     }
1279     m_document->renderer()->doRefresh();
1280 }
1281
1282 void CustomTrackView::updateClipFade(ClipItem * item, bool updateFadeOut) {
1283     if (!updateFadeOut) {
1284         int end = item->fadeIn();
1285         if (end != 0) {
1286             // there is a fade in effect
1287             QStringList clipeffects = item->effectNames();
1288             QDomElement oldeffect = item->effectAt(clipeffects.indexOf("Fade in"));
1289             int start = item->cropStart().frames(m_document->fps());
1290             end += start;
1291             EffectsList::setParameter(oldeffect, "in", QString::number(start));
1292             EffectsList::setParameter(oldeffect, "out", QString::number(end));
1293             QMap <QString, QString> effectParams = item->getEffectArgs(oldeffect);
1294             m_document->renderer()->mltEditEffect(m_tracksList.count() - item->track(), item->startPos(), effectParams);
1295         }
1296     } else {
1297         int start = item->fadeOut();
1298         if (start != 0) {
1299             // there is a fade in effect
1300             QStringList clipeffects = item->effectNames();
1301             QDomElement oldeffect = item->effectAt(clipeffects.indexOf("Fade out"));
1302             int end = (item->duration() - item->cropStart()).frames(m_document->fps());
1303             start = end - start;
1304             EffectsList::setParameter(oldeffect, "in", QString::number(start));
1305             EffectsList::setParameter(oldeffect, "out", QString::number(end));
1306             QMap <QString, QString> effectParams = item->getEffectArgs(oldeffect);
1307             m_document->renderer()->mltEditEffect(m_tracksList.count() - item->track(), item->startPos(), effectParams);
1308         }
1309     }
1310 }
1311
1312 double CustomTrackView::getSnapPointForPos(double pos) {
1313     for (int i = 0; i < m_snapPoints.size(); ++i) {
1314         if (abs((int)(pos - m_snapPoints.at(i).frames(m_document->fps()) * m_scale)) < 10) {
1315             //kDebug()<<" FOUND SNAP POINT AT: "<<m_snapPoints.at(i)<<", current pos: "<<pos / m_scale;
1316             return m_snapPoints.at(i).frames(m_document->fps()) * m_scale + 0.5;
1317         }
1318         if (m_snapPoints.at(i).frames(m_document->fps() * m_scale) > pos) break;
1319     }
1320     return pos;
1321 }
1322
1323 void CustomTrackView::updateSnapPoints(AbstractClipItem *selected) {
1324     m_snapPoints.clear();
1325         if (!KdenliveSettings::snaptopoints()) return;
1326     GenTime offset;
1327     if (selected) offset = selected->duration();
1328     QList<QGraphicsItem *> itemList = items();
1329     for (int i = 0; i < itemList.count(); i++) {
1330         if (itemList.at(i)->type() == AVWIDGET && itemList.at(i) != selected) {
1331             ClipItem *item = static_cast <ClipItem *>(itemList.at(i));
1332             GenTime start = item->startPos();
1333             GenTime end = item->endPos();
1334             m_snapPoints.append(start);
1335             m_snapPoints.append(end);
1336             QList < GenTime > markers = item->snapMarkers();
1337             for (int i = 0; i < markers.size(); ++i) {
1338                 GenTime t = markers.at(i);
1339                 m_snapPoints.append(t);
1340                 if (t > offset) m_snapPoints.append(t - offset);
1341             }
1342             if (offset != GenTime()) {
1343                 if (start > offset) m_snapPoints.append(start - offset);
1344                 if (end > offset) m_snapPoints.append(end - offset);
1345             }
1346         } else if (itemList.at(i)->type() == TRANSITIONWIDGET) {
1347             Transition *transition = static_cast <Transition*>(itemList.at(i));
1348             GenTime start = transition->startPos();
1349             GenTime end = transition->endPos();
1350             m_snapPoints.append(start);
1351             m_snapPoints.append(end);
1352             if (offset != GenTime()) {
1353                 if (start > offset) m_snapPoints.append(start - offset);
1354                 if (end > offset) m_snapPoints.append(end - offset);
1355             }
1356         }
1357     }
1358
1359     // add cursor position
1360     GenTime pos = GenTime(m_cursorPos, m_document->fps());
1361     m_snapPoints.append(pos);
1362     if (offset != GenTime()) m_snapPoints.append(pos - offset);
1363
1364     // add guides
1365     for (int i = 0; i < m_guides.count(); i++) {
1366         m_snapPoints.append(m_guides.at(i)->position());
1367         if (offset != GenTime()) m_snapPoints.append(m_guides.at(i)->position() - offset);
1368     }
1369
1370     qSort(m_snapPoints);
1371     //for (int i = 0; i < m_snapPoints.size(); ++i)
1372     //    kDebug() << "SNAP POINT: " << m_snapPoints.at(i).frames(25);
1373 }
1374
1375 void CustomTrackView::slotSeekToPreviousSnap() {
1376     updateSnapPoints(NULL);
1377     GenTime pos = GenTime(m_cursorPos, m_document->fps());
1378     GenTime res = GenTime();
1379     for (int i = 0; i < m_snapPoints.size(); ++i) {
1380         if (m_snapPoints.at(i) >= pos) {
1381             if (i == 0) i = 1;
1382             res = m_snapPoints.at(i - 1);
1383             break;
1384         }
1385     }
1386     setCursorPos((int) res.frames(m_document->fps()));
1387 }
1388
1389 void CustomTrackView::slotSeekToNextSnap() {
1390     updateSnapPoints(NULL);
1391     GenTime pos = GenTime(m_cursorPos, m_document->fps());
1392     GenTime res = GenTime(m_projectDuration, m_document->fps());
1393     for (int i = 0; i < m_snapPoints.size(); ++i) {
1394         if (m_snapPoints.at(i) > pos) {
1395             res = m_snapPoints.at(i);
1396             break;
1397         }
1398     }
1399     setCursorPos((int) res.frames(m_document->fps()));
1400 }
1401
1402 void CustomTrackView::slotAddClipMarker() {
1403     QList<QGraphicsItem *> itemList = scene()->selectedItems();
1404     if (itemList.count() != 1) {
1405         emit displayMessage(i18n("Cannot add marker if more than one clip is selected"), ErrorMessage);
1406         kDebug() << "// CANNOT ADD MARKER IF MORE TAN ONE CLIP IS SELECTED....";
1407         return;
1408     }
1409     AbstractClipItem *item = (AbstractClipItem *)itemList.at(0);
1410     if (item->type() != AVWIDGET) return;
1411     GenTime pos = GenTime(m_cursorPos, m_document->fps());
1412     if (item->startPos() > pos || item->endPos() < pos) return;
1413     ClipItem *clip = (ClipItem *) item;
1414     int id = clip->baseClip()->getId();
1415     GenTime position = pos - item->startPos() + item->cropStart();
1416     CommentedTime marker(position, i18n("Marker"));
1417     MarkerDialog d(clip->baseClip(), marker, m_document->timecode(), this);
1418     if (d.exec() == QDialog::Accepted) {
1419         slotAddClipMarker(id, d.newMarker().time(), d.newMarker().comment());
1420     }
1421 }
1422
1423 void CustomTrackView::slotAddClipMarker(int id, GenTime t, QString c) {
1424     QString oldcomment = m_document->clipManager()->getClipById(id)->markerComment(t);
1425     AddMarkerCommand *command = new AddMarkerCommand(this, oldcomment, c, id, t, true);
1426     m_commandStack->push(command);
1427 }
1428
1429 void CustomTrackView::slotDeleteClipMarker() {
1430     QList<QGraphicsItem *> itemList = scene()->selectedItems();
1431     if (itemList.count() != 1) {
1432         emit displayMessage(i18n("Cannot delete marker if more than one clip is selected"), ErrorMessage);
1433         kDebug() << "// CANNOT DELETE MARKER IF MORE TAN ONE CLIP IS SELECTED....";
1434         return;
1435     }
1436     AbstractClipItem *item = (AbstractClipItem *)itemList.at(0);
1437     if (item->type() != AVWIDGET) {
1438                 emit displayMessage(i18n("No clip at cursor time"), ErrorMessage);
1439                 return;
1440         }
1441     GenTime pos = GenTime(m_cursorPos, m_document->fps());
1442     if (item->startPos() > pos || item->endPos() < pos) {
1443                 emit displayMessage(i18n("No selected clip at cursor time"), ErrorMessage);
1444                 return;
1445         }
1446     ClipItem *clip = (ClipItem *) item;
1447     int id = clip->baseClip()->getId();
1448     GenTime position = pos - item->startPos() + item->cropStart();
1449     QString comment = clip->baseClip()->markerComment(position);
1450     if (comment.isEmpty()) {
1451                 emit displayMessage(i18n("No marker found at cursor time"), ErrorMessage);
1452                 return;
1453         }
1454     AddMarkerCommand *command = new AddMarkerCommand(this, comment, QString(), id, position, true);
1455     m_commandStack->push(command);
1456 }
1457
1458 void CustomTrackView::slotEditClipMarker() {
1459     QList<QGraphicsItem *> itemList = scene()->selectedItems();
1460     if (itemList.count() != 1) {
1461         emit displayMessage(i18n("Cannot edit marker if more than one clip is selected"), ErrorMessage);
1462         kDebug() << "// CANNOT DELETE MARKER IF MORE TAN ONE CLIP IS SELECTED....";
1463         return;
1464     }
1465     AbstractClipItem *item = (AbstractClipItem *)itemList.at(0);
1466     if (item->type() != AVWIDGET) {
1467                 emit displayMessage(i18n("No clip at cursor time"), ErrorMessage);
1468                 return;
1469         }
1470     GenTime pos = GenTime(m_cursorPos, m_document->fps());
1471     if (item->startPos() > pos || item->endPos() < pos) {
1472                 emit displayMessage(i18n("No selected clip at cursor time"), ErrorMessage);
1473                 return;
1474         }
1475     ClipItem *clip = (ClipItem *) item;
1476     int id = clip->baseClip()->getId();
1477     GenTime position = pos - item->startPos() + item->cropStart();
1478     QString oldcomment = clip->baseClip()->markerComment(position);
1479     if (oldcomment.isEmpty()) {
1480                 emit displayMessage(i18n("No marker found at cursor time"), ErrorMessage);
1481                 return;
1482         }
1483
1484     CommentedTime marker(position, oldcomment);
1485     MarkerDialog d(clip->baseClip(), marker, m_document->timecode(), this);
1486     if (d.exec() == QDialog::Accepted) {
1487         if (d.newMarker().time() == position) {
1488             // marker position was not changed, only text
1489             AddMarkerCommand *command = new AddMarkerCommand(this, oldcomment, d.newMarker().comment(), id, position, true);
1490             m_commandStack->push(command);
1491         } else {
1492             // marker text and position were changed, remove previous marker and add new one
1493             AddMarkerCommand *command1 = new AddMarkerCommand(this, oldcomment, QString(), id, position, true);
1494             AddMarkerCommand *command2 = new AddMarkerCommand(this, QString(), d.newMarker().comment(), id, d.newMarker().time(), true);
1495             m_commandStack->push(command1);
1496             m_commandStack->push(command2);
1497         }
1498     }
1499 }
1500
1501 void CustomTrackView::addMarker(const int id, const GenTime &pos, const QString comment) {
1502     DocClipBase *base = m_document->clipManager()->getClipById(id);
1503     if (!comment.isEmpty()) base->addSnapMarker(pos, comment);
1504     else base->deleteSnapMarker(pos);
1505     m_document->setModified(true);
1506     viewport()->update();
1507 }
1508
1509
1510
1511 void CustomTrackView::editGuide(const GenTime oldPos, const GenTime pos, const QString &comment) {
1512     if (oldPos > GenTime() && pos > GenTime()) {
1513         // move guide
1514         for (int i = 0; i < m_guides.count(); i++) {
1515             kDebug() << "// LOOKING FOR GUIDE (" << i << "): " << m_guides.at(i)->position().frames(25) << ", LOOK: " << oldPos.frames(25) << "x" << pos.frames(25);
1516             if (m_guides.at(i)->position() == oldPos) {
1517                 Guide *item = m_guides.at(i);
1518                 item->update(pos, comment);
1519                 item->updatePosition(m_scale);
1520                 break;
1521             }
1522         }
1523     } else if (pos > GenTime()) addGuide(pos, comment);
1524     else {
1525         // remove guide
1526                 bool found = false;
1527         for (int i = 0; i < m_guides.count(); i++) {
1528             if (m_guides.at(i)->position() == oldPos) {
1529                 Guide *item = m_guides.takeAt(i);
1530                 delete item;
1531                                 found = true;
1532                 break;
1533             }
1534         }
1535                 if (!found) emit displayMessage(i18n("No guide at cursor time"), ErrorMessage);
1536     }
1537 }
1538
1539 bool CustomTrackView::addGuide(const GenTime pos, const QString &comment) {
1540     for (int i = 0; i < m_guides.count(); i++) {
1541                 if (m_guides.at(i)->position() == pos) {
1542                         emit displayMessage(i18n("A guide already exists at that position"), ErrorMessage);
1543                         return false;
1544                 }
1545     }
1546     Guide *g = new Guide(this, pos, comment, m_scale, m_document->fps(), m_tracksHeight * m_tracksList.count());
1547     scene()->addItem(g);
1548     m_guides.append(g);
1549         return true;
1550 }
1551
1552 void CustomTrackView::slotAddGuide() {
1553     if (addGuide(GenTime(m_cursorPos, m_document->fps()), i18n("guide"))) {
1554                 EditGuideCommand *command = new EditGuideCommand(this, GenTime(), QString(), GenTime(m_cursorPos, m_document->fps()), i18n("guide"), false);
1555                 m_commandStack->push(command);
1556         }
1557 }
1558
1559 void CustomTrackView::slotDeleteGuide() {
1560     GenTime pos = GenTime(m_cursorPos, m_document->fps());
1561         bool found = false;
1562     for (int i = 0; i < m_guides.count(); i++) {
1563         if (m_guides.at(i)->position() == pos) {
1564             EditGuideCommand *command = new EditGuideCommand(this, m_guides.at(i)->position(), m_guides.at(i)->label(), GenTime(), QString(), true);
1565             m_commandStack->push(command);
1566                         found = true;
1567             break;
1568         }
1569     }
1570         if (!found) emit displayMessage(i18n("No guide at cursor time"), ErrorMessage);
1571 }
1572
1573 void CustomTrackView::setTool(PROJECTTOOL tool) {
1574     m_tool = tool;
1575 }
1576
1577 void CustomTrackView::setScale(double scaleFactor) {
1578     //scale(scaleFactor, scaleFactor);
1579     m_animationTimer->stop();
1580     if (m_visualTip) {
1581         delete m_visualTip;
1582         m_visualTip = NULL;
1583     }
1584     if (m_animation) {
1585         delete m_animation;
1586         m_animation = NULL;
1587     }
1588     double pos = cursorPos() / m_scale;
1589     m_scale = scaleFactor;
1590     int vert = verticalScrollBar()->value();
1591     kDebug() << " HHHHHHHH  SCALING: " << m_scale;
1592     QList<QGraphicsItem *> itemList = items();
1593     for (int i = 0; i < itemList.count(); i++) {
1594         if (itemList.at(i)->type() == AVWIDGET || itemList.at(i)->type() == TRANSITIONWIDGET) {
1595             AbstractClipItem *clip = (AbstractClipItem *)itemList.at(i);
1596             clip->setRect(clip->startPos().frames(m_document->fps()) * m_scale, clip->rect().y(), clip->duration().frames(m_document->fps()) * m_scale, clip->rect().height());
1597         }
1598     }
1599
1600     for (int i = 0; i < m_guides.count(); i++) {
1601         m_guides.at(i)->updatePosition(m_scale);
1602     }
1603
1604     setSceneRect(0, 0, (m_projectDuration + 100) * m_scale, sceneRect().height());
1605     updateCursorPos();
1606     centerOn(QPointF(cursorPos(), m_tracksHeight));
1607     verticalScrollBar()->setValue(vert);
1608 }
1609
1610 void CustomTrackView::drawBackground(QPainter * painter, const QRectF & rect) {
1611     QRect rectInView = viewport()->rect();
1612     rectInView.moveTo(horizontalScrollBar()->value(), verticalScrollBar()->value());
1613
1614     QColor base = palette().button().color();
1615     painter->setClipRect(rect);
1616     painter->drawLine(rectInView.left(), 0, rectInView.right(), 0);
1617     uint max = m_tracksList.count();
1618     for (uint i = 0; i < max;i++) {
1619         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)));
1620         painter->drawLine(rectInView.left(), m_tracksHeight * (i + 1), rectInView.right(), m_tracksHeight * (i + 1));
1621         //painter->drawText(QRectF(10, 50 * i, 100, 50 * i + 49), Qt::AlignLeft, i18n(" Track ") + QString::number(i + 1));
1622     }
1623     int lowerLimit = m_tracksHeight * m_tracksList.count() + 1;
1624     if (height() > lowerLimit)
1625         painter->fillRect(QRectF(rectInView.left(), lowerLimit, rectInView.width(), height() - lowerLimit), QBrush(base));
1626 }
1627
1628 QDomElement CustomTrackView::xmlInfo() {
1629     QDomDocument doc;
1630     QDomElement e;
1631     QDomElement guides = doc.createElement("guides");
1632     for (int i = 0; i < m_guides.count(); i++) {
1633         e = doc.createElement("guide");
1634         e.setAttribute("time", m_guides.at(i)->position().ms() / 1000);
1635         e.setAttribute("comment", m_guides.at(i)->label());
1636         guides.appendChild(e);
1637     }
1638     return guides;
1639 }
1640
1641 /*
1642 void CustomTrackView::drawForeground ( QPainter * painter, const QRectF & rect )
1643 {
1644   //kDebug()<<"/////  DRAWING FB: "<<rect.x()<<", width: "<<rect.width();
1645   painter->fillRect(rect, QColor(50, rand() % 250,50,100));
1646   painter->drawLine(m_cursorPos, rect.y(), m_cursorPos, rect.y() + rect.height());
1647 }
1648 */
1649 #include "customtrackview.moc"