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