]> git.sesse.net Git - kdenlive/blob - src/customtrackview.cpp
are the tracknumbers in reverse order ?
[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
26 #include <KDebug>
27 #include <KLocale>
28 #include <KUrl>
29 #include <KCursor>
30
31 #include "customtrackview.h"
32 #include "clipitem.h"
33 #include "definitions.h"
34 #include "moveclipcommand.h"
35 #include "resizeclipcommand.h"
36 #include "addtimelineclipcommand.h"
37 #include "addeffectcommand.h"
38 #include "editeffectcommand.h"
39 #include "addtransitioncommand.h"
40 #include "kdenlivesettings.h"
41 #include "transition.h"
42
43 //TODO:
44 // disable animation if user asked it in KDE's global settings
45 // http://lists.kde.org/?l=kde-commits&m=120398724717624&w=2
46 // needs something like below (taken from dolphin)
47 // #include <kglobalsettings.h>
48 // const bool animate = KGlobalSettings::graphicEffectsLevel() & KGlobalSettings::SimpleAnimationEffects;
49 // const int duration = animate ? 1500 : 1;
50
51 CustomTrackView::CustomTrackView(KdenliveDoc *doc, QGraphicsScene * projectscene, QWidget *parent)
52         : QGraphicsView(projectscene, parent), m_tracksCount(0), m_cursorPos(0), m_dropItem(NULL), m_cursorLine(NULL), m_operationMode(NONE), m_startPos(QPointF()), 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()) {
53     if (doc) m_commandStack = doc->commandStack();
54     else m_commandStack == NULL;
55     setMouseTracking(true);
56     setAcceptDrops(true);
57     m_animationTimer = new QTimeLine(800);
58     m_animationTimer->setFrameRange(0, 5);
59     m_animationTimer->setUpdateInterval(100);
60     m_animationTimer->setLoopCount(0);
61     m_tipColor = QColor(0, 192, 0, 200);
62     QColor border = QColor(255, 255, 255, 100);
63     m_tipPen.setColor(border);
64     m_tipPen.setWidth(3);
65     setContentsMargins(0, 0, 0, 0);
66     if (projectscene) {
67         m_cursorLine = projectscene->addLine(0, 0, 0, 50);
68         m_cursorLine->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIgnoresTransformations);
69         m_cursorLine->setZValue(1000);
70     }
71 }
72
73 void CustomTrackView::setContextMenu(QMenu *timeline, QMenu *clip, QMenu *transition) {
74     m_timelineContextMenu = timeline;
75     m_timelineContextClipMenu = clip;
76     m_timelineContextTransitionMenu = transition;
77 }
78
79 void CustomTrackView::checkAutoScroll() {
80     m_autoScroll = KdenliveSettings::autoscroll();
81 }
82
83 // virtual
84 void CustomTrackView::resizeEvent(QResizeEvent * event) {
85     QGraphicsView::resizeEvent(event);
86 }
87
88 // virtual
89 void CustomTrackView::wheelEvent(QWheelEvent * e) {
90     if (e->modifiers() == Qt::ControlModifier) {
91         if (e->delta() > 0) emit zoomIn();
92         else emit zoomOut();
93     } else {
94         if (e->delta() > 0) horizontalScrollBar()->setValue(horizontalScrollBar()->value() + horizontalScrollBar()->singleStep());
95         else  horizontalScrollBar()->setValue(horizontalScrollBar()->value() - horizontalScrollBar()->singleStep());
96     }
97 }
98
99
100 // virtual
101 void CustomTrackView::mouseMoveEvent(QMouseEvent * event) {
102     int pos = event->x();
103     emit mousePosition(mapToScene(event->pos()).x() / m_scale);
104     /*if (event->modifiers() == Qt::ControlModifier)
105       setDragMode(QGraphicsView::ScrollHandDrag);
106     else if (event->modifiers() == Qt::ShiftModifier)
107       setDragMode(QGraphicsView::RubberBandDrag);
108     else*/
109     {
110
111         if (m_dragItem) { //event->button() == Qt::LeftButton) {
112             // a button was pressed, delete visual tips
113             if (m_operationMode == MOVE) {
114                 double snappedPos = getSnapPointForPos(mapToScene(event->pos()).x() - m_clickPoint.x());
115                 //kDebug() << "///////  MOVE CLIP, EVENT Y: "<<m_clickPoint.y();//<<event->scenePos().y()<<", SCENE HEIGHT: "<<scene()->sceneRect().height();
116                 int moveTrack = (int)  mapToScene(event->pos() + QPoint(0, (m_dragItem->type() == TRANSITIONWIDGET ? 50 - m_clickPoint.y() : 0))).y() / 50;
117                 int currentTrack = m_dragItem->track();
118
119                 if (moveTrack > m_tracksCount - 1) moveTrack = m_tracksCount - 1;
120                 else if (moveTrack < 0) moveTrack = 0;
121
122                 int offset = moveTrack - currentTrack;
123                 if (offset != 0) offset = 50 * offset;
124                 m_dragItem->moveTo(snappedPos / m_scale, m_scale, offset, moveTrack);
125             } else if (m_operationMode == RESIZESTART) {
126                 double snappedPos = getSnapPointForPos(mapToScene(event->pos()).x());
127                 m_dragItem->resizeStart(snappedPos / m_scale, m_scale);
128             } else if (m_operationMode == RESIZEEND) {
129                 double snappedPos = getSnapPointForPos(mapToScene(event->pos()).x());
130                 m_dragItem->resizeEnd(snappedPos / m_scale, m_scale);
131             } else if (m_operationMode == FADEIN) {
132                 int pos = mapToScene(event->pos()).x() / m_scale;
133                 m_dragItem->setFadeIn(pos - m_dragItem->startPos().frames(m_document->fps()), m_scale);
134             } else if (m_operationMode == FADEOUT) {
135                 int pos = mapToScene(event->pos()).x() / m_scale;
136                 m_dragItem->setFadeOut(m_dragItem->endPos().frames(m_document->fps()) - pos, m_scale);
137             }
138
139             if (m_animation) delete m_animation;
140             m_animation = NULL;
141             if (m_visualTip) delete m_visualTip;
142             m_visualTip = NULL;
143             QGraphicsView::mouseMoveEvent(event);
144             return;
145         }
146
147         QList<QGraphicsItem *> itemList = items(event->pos());
148         QGraphicsRectItem *item = NULL;
149         for (int i = 0; i < itemList.count(); i++) {
150             if (itemList.at(i)->type() == AVWIDGET || itemList.at(i)->type() == TRANSITIONWIDGET) {
151                 item = (QGraphicsRectItem*) itemList.at(i);
152                 break;
153             }
154         }
155         if (item && event->buttons() == Qt::NoButton) {
156             AbstractClipItem *clip = (AbstractClipItem*) item;
157             OPERATIONTYPE opMode = opMode = clip->operationMode(mapToScene(event->pos()), m_scale);
158             double size = 8;
159
160             if (opMode == m_moveOpMode) {
161                 QGraphicsView::mouseMoveEvent(event);
162                 return;
163             } else {
164                 if (m_visualTip) {
165                     if (m_animation) delete m_animation;
166                     m_animation = NULL;
167                     m_animationTimer->stop();
168                     delete m_visualTip;
169                     m_visualTip = NULL;
170                 }
171             }
172             m_moveOpMode = opMode;
173             if (opMode == MOVE) {
174                 setCursor(Qt::OpenHandCursor);
175             } else if (opMode == RESIZESTART) {
176                 setCursor(KCursor("left_side", Qt::SizeHorCursor));
177                 kDebug() << "********  RESIZE CLIP START; WIDTH: " << size;
178                 if (m_visualTip == NULL) {
179                     QPolygon polygon;
180                     polygon << QPoint(clip->rect().x(), clip->rect().y() + clip->rect().height() / 2 - size * 2);
181                     polygon << QPoint(clip->rect().x() + size * 2, clip->rect().y() + clip->rect().height() / 2);
182                     polygon << QPoint(clip->rect().x(), clip->rect().y() + clip->rect().height() / 2 + size * 2);
183                     polygon << QPoint(clip->rect().x(), clip->rect().y() + clip->rect().height() / 2 - size * 2);
184
185                     m_visualTip = new QGraphicsPolygonItem(polygon);
186                     ((QGraphicsPolygonItem*) m_visualTip)->setBrush(m_tipColor);
187                     ((QGraphicsPolygonItem*) m_visualTip)->setPen(m_tipPen);
188                     m_visualTip->setZValue(100);
189                     m_animation = new QGraphicsItemAnimation;
190                     m_animation->setItem(m_visualTip);
191                     m_animation->setTimeLine(m_animationTimer);
192                     m_visualTip->setPos(0, 0);
193                     double scale = 2.0;
194                     m_animation->setScaleAt(.5, scale, 1);
195                     m_animation->setPosAt(.5, QPointF(clip->rect().x() - clip->rect().x() * scale, 0));
196                     scale = 1.0;
197                     m_animation->setScaleAt(1, scale, 1);
198                     m_animation->setPosAt(1, QPointF(clip->rect().x() - clip->rect().x() * scale, 0));
199                     scene()->addItem(m_visualTip);
200                     m_animationTimer->start();
201                 }
202             } else if (opMode == RESIZEEND) {
203                 setCursor(KCursor("right_side", Qt::SizeHorCursor));
204                 if (m_visualTip == NULL) {
205                     QPolygon polygon;
206                     polygon << QPoint(clip->rect().x() + clip->rect().width(), clip->rect().y() + clip->rect().height() / 2 - size * 2);
207                     polygon << QPoint(clip->rect().x() + clip->rect().width() - size * 2, clip->rect().y() + clip->rect().height() / 2);
208                     polygon << QPoint(clip->rect().x() + clip->rect().width(), clip->rect().y() + clip->rect().height() / 2 + size * 2);
209                     polygon << QPoint(clip->rect().x() + clip->rect().width(), clip->rect().y() + clip->rect().height() / 2 - size * 2);
210
211                     m_visualTip = new QGraphicsPolygonItem(polygon);
212                     ((QGraphicsPolygonItem*) m_visualTip)->setBrush(m_tipColor);
213                     ((QGraphicsPolygonItem*) m_visualTip)->setPen(m_tipPen);
214
215                     m_visualTip->setZValue(100);
216                     m_animation = new QGraphicsItemAnimation;
217                     m_animation->setItem(m_visualTip);
218                     m_animation->setTimeLine(m_animationTimer);
219                     m_visualTip->setPos(0, 0);
220                     double scale = 2.0;
221                     m_animation->setScaleAt(.5, scale, 1);
222                     m_animation->setPosAt(.5, QPointF(clip->rect().x() - clip->rect().x() * scale - clip->rect().width(), 0));
223                     scale = 1.0;
224                     m_animation->setScaleAt(1, scale, 1);
225                     m_animation->setPosAt(1, QPointF(clip->rect().x() - clip->rect().x() * scale, 0));
226                     scene()->addItem(m_visualTip);
227                     m_animationTimer->start();
228                 }
229             } else if (opMode == FADEIN) {
230                 if (m_visualTip == NULL) {
231                     m_visualTip = new QGraphicsEllipseItem(clip->rect().x() + clip->fadeIn() * m_scale - size, clip->rect().y() - 8, size * 2, 16);
232                     ((QGraphicsEllipseItem*) m_visualTip)->setBrush(m_tipColor);
233                     ((QGraphicsEllipseItem*) m_visualTip)->setPen(m_tipPen);
234                     m_visualTip->setZValue(100);
235                     m_animation = new QGraphicsItemAnimation;
236                     m_animation->setItem(m_visualTip);
237                     m_animation->setTimeLine(m_animationTimer);
238                     m_visualTip->setPos(0, 0);
239                     double scale = 2.0;
240                     m_animation->setScaleAt(.5, scale, scale);
241                     m_animation->setPosAt(.5, QPointF(clip->rect().x() - clip->rect().x() * scale -  clip->fadeIn() * m_scale, clip->rect().y() - clip->rect().y() * scale));
242                     scale = 1.0;
243                     m_animation->setScaleAt(1, scale, scale);
244                     m_animation->setPosAt(1, QPointF(clip->rect().x() - clip->rect().x() * scale, clip->rect().y() - clip->rect().y() * scale));
245                     scene()->addItem(m_visualTip);
246                     m_animationTimer->start();
247                 }
248                 setCursor(Qt::PointingHandCursor);
249             } else if (opMode == FADEOUT) {
250                 if (m_visualTip == NULL) {
251                     m_visualTip = new QGraphicsEllipseItem(clip->rect().x() + clip->rect().width() - clip->fadeOut() * m_scale - size, clip->rect().y() - 8, size*2, 16);
252                     ((QGraphicsEllipseItem*) m_visualTip)->setBrush(m_tipColor);
253                     ((QGraphicsEllipseItem*) m_visualTip)->setPen(m_tipPen);
254                     m_visualTip->setZValue(100);
255                     m_animation = new QGraphicsItemAnimation;
256                     m_animation->setItem(m_visualTip);
257                     m_animation->setTimeLine(m_animationTimer);
258                     m_visualTip->setPos(0, 0);
259                     double scale = 2.0;
260                     m_animation->setScaleAt(.5, scale, scale);
261                     m_animation->setPosAt(.5, QPointF(clip->rect().x() - clip->rect().x() * scale - clip->rect().width() + clip->fadeOut() * m_scale, clip->rect().y() - clip->rect().y() * scale));
262                     scale = 1.0;
263                     m_animation->setScaleAt(1, scale, scale);
264                     m_animation->setPosAt(1, QPointF(clip->rect().x() - clip->rect().x() * scale, clip->rect().y() - clip->rect().y() * scale));
265                     scene()->addItem(m_visualTip);
266                     m_animationTimer->start();
267                 }
268                 setCursor(Qt::PointingHandCursor);
269             } else if (opMode == TRANSITIONSTART) {
270                 if (m_visualTip == NULL) {
271                     m_visualTip = new QGraphicsEllipseItem(-5, -5 , 10, 10);
272                     ((QGraphicsEllipseItem*) m_visualTip)->setBrush(m_tipColor);
273                     ((QGraphicsEllipseItem*) m_visualTip)->setPen(m_tipPen);
274                     m_visualTip->setZValue(100);
275                     m_animation = new QGraphicsItemAnimation;
276                     m_animation->setItem(m_visualTip);
277                     m_animation->setTimeLine(m_animationTimer);
278                     m_visualTip->setPos(clip->rect().x() + 15, clip->rect().y() + clip->rect().height() / 2);
279                     double scale = 2.0;
280                     m_animation->setScaleAt(.5, scale, scale);
281                     scale = 1.0;
282                     m_animation->setScaleAt(1, scale, scale);
283                     scene()->addItem(m_visualTip);
284                     m_animationTimer->start();
285                 }
286                 setCursor(Qt::PointingHandCursor);
287             } else if (opMode == TRANSITIONEND) {
288                 if (m_visualTip == NULL) {
289                     m_visualTip = new QGraphicsEllipseItem(-5, -5 , 10, 10);
290                     ((QGraphicsEllipseItem*) m_visualTip)->setBrush(m_tipColor);
291                     ((QGraphicsEllipseItem*) m_visualTip)->setPen(m_tipPen);
292                     m_visualTip->setZValue(100);
293                     m_animation = new QGraphicsItemAnimation;
294                     m_animation->setItem(m_visualTip);
295                     m_animation->setTimeLine(m_animationTimer);
296                     m_visualTip->setPos(clip->rect().x() + clip->rect().width() - 15 , clip->rect().y() + clip->rect().height() / 2);
297                     double scale = 2.0;
298                     m_animation->setScaleAt(.5, scale, scale);
299                     scale = 1.0;
300                     m_animation->setScaleAt(1, scale, scale);
301                     scene()->addItem(m_visualTip);
302                     m_animationTimer->start();
303                 }
304                 setCursor(Qt::PointingHandCursor);
305             }
306         } else {
307             m_moveOpMode = NONE;
308             if (event->buttons() != Qt::NoButton && event->modifiers() == Qt::NoModifier) {
309                 setCursorPos((int) mapToScene(event->pos().x(), 0).x() / m_scale);
310             }
311             if (m_visualTip) {
312                 if (m_animation) delete m_animation;
313                 m_animationTimer->stop();
314                 m_animation = NULL;
315                 delete m_visualTip;
316                 m_visualTip = NULL;
317
318             }
319             setCursor(Qt::ArrowCursor);
320         }
321     }
322     QGraphicsView::mouseMoveEvent(event);
323 }
324
325 // virtual
326 void CustomTrackView::mousePressEvent(QMouseEvent * event) {
327     activateMonitor();
328     int pos = event->x();
329     if (event->modifiers() == Qt::ControlModifier) {
330         setDragMode(QGraphicsView::ScrollHandDrag);
331         QGraphicsView::mousePressEvent(event);
332         return;
333     } else if (event->modifiers() == Qt::ShiftModifier) {
334         setDragMode(QGraphicsView::RubberBandDrag);
335         QGraphicsView::mousePressEvent(event);
336         return;
337     } else {
338         bool collision = false;
339         QList<QGraphicsItem *> collisionList = items(event->pos());
340         AbstractClipItem *clipItem = NULL, *transitionItem = NULL;
341         for (int i = 0; i < collisionList.size(); ++i) {
342             QGraphicsItem *item = collisionList.at(i);
343             if (item->type() == AVWIDGET || item->type() == TRANSITIONWIDGET) {
344                 // select item
345                 if (!item->isSelected()) {
346                     QList<QGraphicsItem *> itemList = items();
347                     for (int i = 0; i < itemList.count(); i++)
348                         itemList.at(i)->setSelected(false);
349                     item->setSelected(true);
350                     update();
351                 }
352
353                 m_dragItem = (AbstractClipItem *) item;
354                 if (item->type() == AVWIDGET) {
355                     clipItem = m_dragItem;
356                 } else if (item->type() == TRANSITIONWIDGET) {
357                     transitionItem = m_dragItem;
358                 }
359                 m_clickPoint = QPoint(mapToScene(event->pos()).x() - m_dragItem->startPos().frames(m_document->fps()) * m_scale, event->pos().y() - m_dragItem->rect().top());
360                 m_operationMode = m_dragItem->operationMode(item->mapFromScene(mapToScene(event->pos())), m_scale);
361                 if (m_operationMode == MOVE) setCursor(Qt::ClosedHandCursor);
362                 if (m_operationMode == MOVE || m_operationMode == RESIZESTART)
363                     m_startPos = QPointF(m_dragItem->startPos().frames(m_document->fps()), m_dragItem->track());
364                 else if (m_operationMode == RESIZEEND)
365                     m_startPos = QPointF(m_dragItem->endPos().frames(m_document->fps()), m_dragItem->track());
366                 else if (m_operationMode == TRANSITIONSTART) {
367                     Transition *tr = new Transition(
368                         QRect(m_dragItem->startPos().frames(m_document->fps()) *m_scale , m_dragItem->rect().y() + m_dragItem->rect().height() / 2,
369                               GenTime(2.5).frames(m_document->fps()) *m_scale ,  m_dragItem->rect().height()
370                              ),
371                         (ClipItem*)m_dragItem, LUMA_TRANSITION, m_dragItem->startPos(), m_dragItem->startPos() + GenTime(2.5), m_document->fps());
372                     tr->setTrack(m_dragItem->track());
373                     scene()->addItem(tr);
374                     //m_dragItem->addTransition(tra);
375                 }
376                 updateSnapPoints(m_dragItem);
377                 kDebug() << "//////// ITEM CLICKED: " << m_startPos;
378                 collision = true;
379                 break;
380             }
381         }
382         emit clipItemSelected((ClipItem*) clipItem);
383         emit transitionItemSelected((Transition*) transitionItem);
384         if (!collision) {
385             kDebug() << "//////// NO ITEM FOUND ON CLICK";
386             m_dragItem = NULL;
387             setCursor(Qt::ArrowCursor);
388             QList<QGraphicsItem *> itemList = items();
389             for (int i = 0; i < itemList.count(); i++)
390                 itemList.at(i)->setSelected(false);
391             //emit clipItemSelected(NULL);
392             if (event->button() == Qt::RightButton) {
393                 displayContextMenu(event->globalPos());
394             } else setCursorPos((int) mapToScene(event->x(), 0).x() / m_scale);
395         } else if (event->button() == Qt::RightButton) {
396             m_operationMode = NONE;
397             displayContextMenu(event->globalPos(), (ClipItem *) m_dragItem);
398             m_dragItem = NULL;
399         }
400     }
401     //kDebug()<<pos;
402     //QGraphicsView::mousePressEvent(event);
403 }
404
405 void CustomTrackView::displayContextMenu(QPoint pos, ClipItem *clip) {
406     m_timelineContextClipMenu->popup(pos);
407 }
408
409 void CustomTrackView::activateMonitor() {
410     emit activateDocumentMonitor();
411 }
412
413 void CustomTrackView::dragEnterEvent(QDragEnterEvent * event) {
414     if (event->mimeData()->hasFormat("kdenlive/producerslist")) {
415         kDebug() << "///////////////  DRAG ENTERED, TEXT: " << event->mimeData()->data("kdenlive/producerslist");
416         QStringList ids = QString(event->mimeData()->data("kdenlive/producerslist")).split(";");
417         //TODO: drop of several clips
418         for (int i = 0; i < ids.size(); ++i) {
419         }
420         DocClipBase *clip = m_document->getBaseClip(ids.at(0).toInt());
421         if (clip == NULL) kDebug() << " WARNING))))))))) CLIP NOT FOUND : " << ids.at(0).toInt();
422         addItem(clip, event->pos());
423         event->acceptProposedAction();
424     } else QGraphicsView::dragEnterEvent(event);
425 }
426
427 void CustomTrackView::slotRefreshEffects(ClipItem *clip) {
428     int track = m_tracksCount - clip->track();
429     GenTime pos = clip->startPos();
430     m_document->renderer()->mltRemoveEffect(track, pos, "-1", false);
431     for (int i = 0; i < clip->effectsCount(); i++) {
432         m_document->renderer()->mltAddEffect(track, pos, clip->getEffectArgs(clip->effectAt(i)), false);
433     }
434     m_document->renderer()->doRefresh();
435 }
436
437 void CustomTrackView::addEffect(int track, GenTime pos, QDomElement effect) {
438     ClipItem *clip = getClipItemAt(pos.frames(m_document->fps()) + 1, m_tracksCount - track);
439     if (clip) {
440         QMap <QString, QString> effectParams = clip->addEffect(effect);
441         m_document->renderer()->mltAddEffect(track, pos, effectParams);
442         emit clipItemSelected(clip);
443     }
444 }
445
446 void CustomTrackView::deleteEffect(int track, GenTime pos, QDomElement effect) {
447     QString index = effect.attribute("kdenlive_ix");
448     m_document->renderer()->mltRemoveEffect(track, pos, index);
449     ClipItem *clip = getClipItemAt(pos.frames(m_document->fps()) + 1, m_tracksCount - track);
450     if (clip) {
451         clip->deleteEffect(index);
452         emit clipItemSelected(clip);
453     }
454 }
455
456 void CustomTrackView::slotAddEffect(QDomElement effect, GenTime pos, int track) {
457     QList<QGraphicsItem *> itemList;
458     if (track == -1)
459         itemList = items();
460     else {
461         ClipItem *clip = getClipItemAt(pos.frames(m_document->fps()) + 1, track);
462         if (clip) itemList.append(clip);
463         else kDebug() << "------   wrning, clip eff not found";
464     }
465     kDebug() << "// REQUESTING EFFECT ON CLIP: " << pos.frames(25) << ", TRK: " << track;
466     for (int i = 0; i < itemList.count(); i++) {
467         if (itemList.at(i)->type() == AVWIDGET && (itemList.at(i)->isSelected() || track != -1)) {
468             ClipItem *item = (ClipItem *)itemList.at(i);
469             // the kdenlive_ix int is used to identify an effect in mlt's playlist, should
470             // not be changed
471             if (effect.attribute("kdenlive_ix").toInt() == 0)
472                 effect.setAttribute("kdenlive_ix", QString::number(item->effectsCounter()));
473             AddEffectCommand *command = new AddEffectCommand(this, m_tracksCount - item->track(), item->startPos(), effect, true);
474             m_commandStack->push(command);
475         }
476     }
477     m_document->setModified(true);
478 }
479
480 void CustomTrackView::slotDeleteEffect(ClipItem *clip, QDomElement effect) {
481     AddEffectCommand *command = new AddEffectCommand(this, m_tracksCount - clip->track(), clip->startPos(), effect, false);
482     m_commandStack->push(command);
483     m_document->setModified(true);
484 }
485
486 void CustomTrackView::updateEffect(int track, GenTime pos, QDomElement effect) {
487     ClipItem *clip = getClipItemAt(pos.frames(m_document->fps()) + 1, m_tracksCount - track);
488     if (clip) {
489         QMap <QString, QString> effectParams = clip->getEffectArgs(effect);
490         if (effectParams["disabled"] == "1") {
491             QString index = effectParams["kdenlive_ix"];
492             m_document->renderer()->mltRemoveEffect(track, pos, index);
493         } else m_document->renderer()->mltEditEffect(m_tracksCount - clip->track(), clip->startPos(), effectParams);
494     }
495     m_document->setModified(true);
496 }
497
498 void CustomTrackView::slotChangeEffectState(ClipItem *clip, QDomElement effect, bool disable) {
499     QDomElement oldEffect = effect.cloneNode().toElement();
500     effect.setAttribute("disabled", disable);
501     EditEffectCommand *command = new EditEffectCommand(this, m_tracksCount - clip->track(), clip->startPos(), oldEffect, effect, true);
502     m_commandStack->push(command);
503     m_document->setModified(true);
504 }
505
506 void CustomTrackView::slotUpdateClipEffect(ClipItem *clip, QDomElement oldeffect, QDomElement effect) {
507     EditEffectCommand *command = new EditEffectCommand(this, m_tracksCount - clip->track(), clip->startPos(), oldeffect, effect, true);
508     m_commandStack->push(command);
509 }
510
511 void CustomTrackView::slotAddTransition(ClipItem* clip , QDomElement transition, GenTime startTime , int startTrack) {
512     AddTransitionCommand* command = new AddTransitionCommand(this, startTrack, transition, startTime, true);
513     m_commandStack->push(command);
514 }
515
516 void CustomTrackView::addTransition(int startTrack, GenTime startPos , QDomElement) {
517     QMap < QString, QString> map;
518     /*map["start"] = "0.0";
519     map["end"] = "1.0";*/
520     m_document->renderer()->mltAddTransition("luma", startTrack+4, startTrack + 5 , startPos, startPos + GenTime(2.5), map);
521     m_document->setModified(true);
522 }
523
524 void CustomTrackView::deleteTransition(int, GenTime, QDomElement) {
525
526 }
527
528 void CustomTrackView::addItem(DocClipBase *clip, QPoint pos) {
529     int in = 0;
530     GenTime out = clip->duration();
531     //kdDebug()<<"- - - -CREATING CLIP, duration = "<<out<<", URL: "<<clip->fileURL();
532     int trackTop = ((int) mapToScene(pos).y() / 50) * 50 + 1;
533     m_dropItem = new ClipItem(clip, ((int) mapToScene(pos).y() / 50), GenTime(), QRectF(mapToScene(pos).x() * m_scale, trackTop, out.frames(m_document->fps()) * m_scale, 49), out, m_document->fps());
534     scene()->addItem(m_dropItem);
535 }
536
537
538 void CustomTrackView::dragMoveEvent(QDragMoveEvent * event) {
539     event->setDropAction(Qt::IgnoreAction);
540     //kDebug()<<"+++++++++++++   DRAG MOVE, : "<<mapToScene(event->pos()).x()<<", SCAL: "<<m_scale;
541     if (m_dropItem) {
542         int track = (int) mapToScene(event->pos()).y() / 50; //) * (m_scale * 50) + m_scale;
543         m_dropItem->moveTo(mapToScene(event->pos()).x() / m_scale, m_scale, (track - m_dropItem->track()) * 50, track);
544         event->setDropAction(Qt::MoveAction);
545         if (event->mimeData()->hasFormat("kdenlive/producerslist")) {
546             event->acceptProposedAction();
547         }
548     } else {
549         QGraphicsView::dragMoveEvent(event);
550     }
551 }
552
553 void CustomTrackView::dragLeaveEvent(QDragLeaveEvent * event) {
554     if (m_dropItem) {
555         delete m_dropItem;
556         m_dropItem = NULL;
557     } else QGraphicsView::dragLeaveEvent(event);
558 }
559
560 void CustomTrackView::dropEvent(QDropEvent * event) {
561     if (m_dropItem) {
562         AddTimelineClipCommand *command = new AddTimelineClipCommand(this, m_dropItem->xml(), m_dropItem->clipProducer(), m_dropItem->track(), m_dropItem->startPos(), m_dropItem->rect(), m_dropItem->duration(), false, false);
563         m_commandStack->push(command);
564         m_dropItem->baseClip()->addReference();
565         m_document->updateClip(m_dropItem->baseClip()->getId());
566         // kDebug()<<"IIIIIIIIIIIIIIIIIIIIIIII TRAX CNT: "<<m_tracksCount<<", DROP: "<<m_dropItem->track();
567         m_document->renderer()->mltInsertClip(m_tracksCount - m_dropItem->track(), m_dropItem->startPos(), m_dropItem->xml());
568         m_document->setModified(true);
569     } else QGraphicsView::dropEvent(event);
570     m_dropItem = NULL;
571 }
572
573
574 QStringList CustomTrackView::mimeTypes() const {
575     QStringList qstrList;
576     // list of accepted mime types for drop
577     qstrList.append("text/plain");
578     qstrList.append("kdenlive/producerslist");
579     return qstrList;
580 }
581
582 Qt::DropActions CustomTrackView::supportedDropActions() const {
583     // returns what actions are supported when dropping
584     return Qt::MoveAction;
585 }
586
587 void CustomTrackView::setDuration(int duration) {
588     kDebug() << "/////////////  PRO DUR: " << duration << ", height: " << 50 * m_tracksCount;
589     m_projectDuration = duration;
590     scene()->setSceneRect(0, 0, (m_projectDuration + 500) * m_scale, scene()->sceneRect().height()); //50 * m_tracksCount);
591 }
592
593 int CustomTrackView::duration() const {
594     return m_projectDuration;
595 }
596
597 void CustomTrackView::addTrack() {
598     m_tracksCount++;
599     m_cursorLine->setLine(m_cursorLine->line().x1(), 0, m_cursorLine->line().x1(), 50 * m_tracksCount);
600     //setSceneRect(0, 0, sceneRect().width(), 50 * m_tracksCount);
601     //verticalScrollBar()->setMaximum(50 * m_tracksCount);
602     //setFixedHeight(50 * m_tracksCount);
603 }
604
605 void CustomTrackView::removeTrack() {
606     m_tracksCount--;
607     m_cursorLine->setLine(m_cursorLine->line().x1(), 0, m_cursorLine->line().x1(), 50 * m_tracksCount);
608 }
609
610 void CustomTrackView::deleteClip(int clipId) {
611     QList<QGraphicsItem *> itemList = items();
612     for (int i = 0; i < itemList.count(); i++) {
613         if (itemList.at(i)->type() == AVWIDGET) {
614             ClipItem *item = (ClipItem *)itemList.at(i);
615             if (item->clipProducer() == clipId) {
616                 AddTimelineClipCommand *command = new AddTimelineClipCommand(this, item->xml(), item->clipProducer(), item->track(), item->startPos(), item->rect(), item->duration(), true, true);
617                 m_commandStack->push(command);
618                 //delete item;
619             }
620         }
621     }
622 }
623
624 void CustomTrackView::setCursorPos(int pos, bool seek) {
625     emit cursorMoved(m_cursorPos * m_scale, pos * m_scale);
626     m_cursorPos = pos;
627     m_cursorLine->setPos(pos * m_scale, 0);
628     if (seek) m_document->renderer()->seek(GenTime(pos, m_document->fps()));
629     else if (m_autoScroll && m_scale < 50) checkScrolling();
630 }
631
632 void CustomTrackView::updateCursorPos() {
633     m_cursorLine->setPos(m_cursorPos * m_scale, 0);
634 }
635
636 int CustomTrackView::cursorPos() {
637     return m_cursorPos * m_scale;
638 }
639
640 void CustomTrackView::checkScrolling() {
641     QRect rectInView = viewport()->rect();
642     int delta = rectInView.width() / 3;
643     int max = rectInView.right() + horizontalScrollBar()->value() - delta;
644     //kDebug() << "CURSOR POS: "<<m_cursorPos<< "Scale: "<<m_scale;
645     if (m_cursorPos * m_scale >= max) horizontalScrollBar()->setValue(horizontalScrollBar()->value() + 1 + m_scale);
646 }
647
648 void CustomTrackView::mouseReleaseEvent(QMouseEvent * event) {
649     QGraphicsView::mouseReleaseEvent(event);
650     setDragMode(QGraphicsView::NoDrag);
651     if (m_dragItem == NULL) return;
652     if (m_operationMode == MOVE) setCursor(Qt::OpenHandCursor);
653     if (m_operationMode == MOVE && m_startPos.x() != m_dragItem->startPos().frames(m_document->fps())) {
654         // move clip
655         MoveClipCommand *command = new MoveClipCommand(this, m_startPos, QPointF(m_dragItem->startPos().frames(m_document->fps()), m_dragItem->track()), false);
656         m_commandStack->push(command);
657         if (m_dragItem->type() == AVWIDGET) m_document->renderer()->mltMoveClip(m_tracksCount - m_startPos.y(), m_tracksCount - m_dragItem->track(), m_startPos.x(), m_dragItem->startPos().frames(m_document->fps()));
658     } else if (m_operationMode == RESIZESTART) {
659         // resize start
660         ResizeClipCommand *command = new ResizeClipCommand(this, m_startPos, QPointF(m_dragItem->startPos().frames(m_document->fps()), m_dragItem->track()), true, false);
661
662         if (m_dragItem->type() == AVWIDGET) m_document->renderer()->mltResizeClipStart(m_tracksCount - m_dragItem->track(), m_dragItem->endPos(), m_dragItem->startPos(), GenTime(m_startPos.x(), m_document->fps()), m_dragItem->cropStart(), m_dragItem->cropStart() + m_dragItem->endPos() - m_dragItem->startPos());
663         m_commandStack->push(command);
664         m_document->renderer()->doRefresh();
665     } else if (m_operationMode == RESIZEEND) {
666         // resize end
667         ResizeClipCommand *command = new ResizeClipCommand(this, m_startPos, QPointF(m_dragItem->endPos().frames(m_document->fps()), m_dragItem->track()), false, false);
668
669         if (m_dragItem->type() == AVWIDGET) m_document->renderer()->mltResizeClipEnd(m_tracksCount - m_dragItem->track(), m_dragItem->startPos(), m_dragItem->cropStart(), m_dragItem->cropStart() + m_dragItem->endPos() - m_dragItem->startPos());
670         m_commandStack->push(command);
671         m_document->renderer()->doRefresh();
672     }
673     m_document->setModified(true);
674     m_operationMode = NONE;
675     m_dragItem = NULL;
676 }
677
678 void CustomTrackView::deleteClip(int track, GenTime startpos, const QRectF &rect) {
679     ClipItem *item = getClipItemAt(startpos, track);
680     if (!item) {
681         kDebug() << "----------------  ERROR, CANNOT find clip to move at: " << rect.x();
682         return;
683     }
684     item->baseClip()->removeReference();
685     m_document->updateClip(item->baseClip()->getId());
686     delete item;
687     m_document->renderer()->mltRemoveClip(m_tracksCount - track, startpos);
688     m_document->renderer()->doRefresh();
689 }
690
691 void CustomTrackView::deleteSelectedClips() {
692     QList<QGraphicsItem *> itemList = items();
693     for (int i = 0; i < itemList.count(); i++) {
694         if (itemList.at(i)->type() == AVWIDGET && itemList.at(i)->isSelected()) {
695             ClipItem *item = (ClipItem *) itemList.at(i);
696             AddTimelineClipCommand *command = new AddTimelineClipCommand(this, item->xml(), item->clipProducer(), item->track(), item->startPos(), item->rect(), item->duration(), true, true);
697             m_commandStack->push(command);
698         }
699     }
700 }
701
702 void CustomTrackView::addClip(QDomElement xml, int clipId, int track, GenTime startpos, const QRectF &rect, GenTime duration) {
703     QRect r(startpos.frames(m_document->fps()) * m_scale, 50 * track, duration.frames(m_document->fps()) * m_scale, 49);
704     DocClipBase *baseclip = m_document->clipManager()->getClipById(clipId);
705     ClipItem *item = new ClipItem(baseclip, track, startpos, r, duration, m_document->fps());
706     scene()->addItem(item);
707     baseclip->addReference();
708     m_document->updateClip(baseclip->getId());
709     m_document->renderer()->mltInsertClip(m_tracksCount - track, startpos, xml);
710     m_document->renderer()->doRefresh();
711 }
712
713 ClipItem *CustomTrackView::getClipItemAt(int pos, int track) {
714     return (ClipItem *) scene()->itemAt(pos * m_scale, track * 50 + 25);
715 }
716
717 ClipItem *CustomTrackView::getClipItemAt(GenTime pos, int track) {
718     return (ClipItem *) scene()->itemAt(pos.frames(m_document->fps()) * m_scale, track * 50 + 25);
719 }
720
721 void CustomTrackView::moveClip(const QPointF &startPos, const QPointF &endPos) {
722     ClipItem *item = getClipItemAt(startPos.x() + 1, startPos.y());
723     if (!item) {
724         kDebug() << "----------------  ERROR, CANNOT find clip to move at: " << startPos.x() * m_scale * FRAME_SIZE + 1 << ", " << startPos.y() * 50 + 25;
725         return;
726     }
727     kDebug() << "----------------  Move CLIP FROM: " << startPos.x() << ", END:" << endPos.x();
728     item->moveTo(endPos.x(), m_scale, (endPos.y() - startPos.y()) * 50, endPos.y());
729     m_document->renderer()->mltMoveClip(m_tracksCount - startPos.y(), m_tracksCount - endPos.y(), startPos.x(), endPos.x());
730 }
731
732 void CustomTrackView::resizeClip(const QPointF &startPos, const QPointF &endPos, bool resizeClipStart) {
733     int offset;
734     if (resizeClipStart) offset = 1;
735     else offset = -1;
736     ClipItem *item = getClipItemAt(startPos.x() + offset, startPos.y());
737     if (!item) {
738         kDebug() << "----------------  ERROR, CANNOT find clip to resize at: " << startPos;
739         return;
740     }
741     qreal diff = endPos.x() - startPos.x();
742     if (resizeClipStart) {
743         m_document->renderer()->mltResizeClipStart(m_tracksCount - item->track(), item->endPos(), GenTime(endPos.x(), m_document->fps()), item->startPos(), item->cropStart() + GenTime(diff, m_document->fps()), item->cropStart() + GenTime(diff, m_document->fps()) + item->endPos() - GenTime(endPos.x(), m_document->fps()));
744         item->resizeStart(endPos.x(), m_scale);
745     } else {
746         m_document->renderer()->mltResizeClipEnd(m_tracksCount - item->track(), item->startPos(), item->cropStart(), item->cropStart() + GenTime(endPos.x(), m_document->fps()) - item->startPos());
747         item->resizeEnd(endPos.x(), m_scale);
748     }
749     m_document->renderer()->doRefresh();
750 }
751
752 double CustomTrackView::getSnapPointForPos(double pos) {
753     for (int i = 0; i < m_snapPoints.size(); ++i) {
754         if (abs(pos - m_snapPoints.at(i).frames(m_document->fps()) * m_scale) < 10) {
755             //kDebug()<<" FOUND SNAP POINT AT: "<<m_snapPoints.at(i)<<", current pos: "<<pos / m_scale;
756             return m_snapPoints.at(i).frames(m_document->fps()) * m_scale + 0.5;
757         }
758         if (m_snapPoints.at(i).frames(m_document->fps() * m_scale) > pos) break;
759     }
760     return pos;
761 }
762
763 void CustomTrackView::updateSnapPoints(AbstractClipItem *selected) {
764     m_snapPoints.clear();
765     GenTime offset;
766     if (selected) offset = selected->duration();
767     QList<QGraphicsItem *> itemList = items();
768     for (int i = 0; i < itemList.count(); i++) {
769         if (itemList.at(i)->type() == AVWIDGET && itemList.at(i) != selected) {
770             ClipItem *item = (ClipItem *)itemList.at(i);
771             GenTime start = item->startPos();
772             GenTime end = item->endPos();
773             m_snapPoints.append(start);
774             m_snapPoints.append(end);
775             if (offset != GenTime()) {
776                 if (start > offset) m_snapPoints.append(start - offset);
777                 if (end > offset) m_snapPoints.append(end - offset);
778             }
779         }
780     }
781     qSort(m_snapPoints);
782     //for (int i = 0; i < m_snapPoints.size(); ++i)
783     //    kDebug() << "SNAP POINT: " << m_snapPoints.at(i).frames(25);
784 }
785
786
787 void CustomTrackView::setScale(double scaleFactor) {
788     //scale(scaleFactor, scaleFactor);
789     double pos = cursorPos() / m_scale;
790     m_scale = scaleFactor;
791     kDebug() << " HHHHHHHH  SCALING: " << m_scale;
792     QList<QGraphicsItem *> itemList = items();
793
794     for (int i = 0; i < itemList.count(); i++) {
795         if (itemList.at(i)->type() == AVWIDGET || itemList.at(i)->type() == TRANSITIONWIDGET) {
796             AbstractClipItem *clip = (AbstractClipItem *)itemList.at(i);
797             clip->setRect(clip->startPos().frames(m_document->fps()) * m_scale, clip->rect().y(), clip->duration().frames(m_document->fps()) * m_scale, clip->rect().height());
798         }
799     }
800     updateCursorPos();
801     centerOn(QPointF(cursorPos(), 50));
802     scene()->setSceneRect(0, 0, (m_projectDuration + 500) * m_scale, scene()->sceneRect().height());
803 }
804
805 void CustomTrackView::drawBackground(QPainter * painter, const QRectF & rect) {
806     QRect rectInView = viewport()->rect();
807     rectInView.moveTo(horizontalScrollBar()->value(), verticalScrollBar()->value());
808
809     QColor base = palette().button().color();
810     painter->setClipRect(rect);
811     painter->drawLine(rectInView.left(), 0, rectInView.right(), 0);
812     for (uint i = 0; i < m_tracksCount;i++) {
813         painter->drawLine(rectInView.left(), 50 * (i + 1), rectInView.right(), 50 * (i + 1));
814         //painter->drawText(QRectF(10, 50 * i, 100, 50 * i + 49), Qt::AlignLeft, i18n(" Track ") + QString::number(i + 1));
815     }
816     int lowerLimit = 50 * m_tracksCount + 1;
817     if (height() > lowerLimit)
818         painter->fillRect(QRectF(rectInView.left(), lowerLimit, rectInView.width(), height() - lowerLimit), QBrush(base));
819 }
820 /*
821 void CustomTrackView::drawForeground ( QPainter * painter, const QRectF & rect )
822 {
823   //kDebug()<<"/////  DRAWING FB: "<<rect.x()<<", width: "<<rect.width();
824   painter->fillRect(rect, QColor(50, rand() % 250,50,100));
825   painter->drawLine(m_cursorPos, rect.y(), m_cursorPos, rect.y() + rect.height());
826 }
827 */
828 #include "customtrackview.moc"