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