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