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