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