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