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