]> git.sesse.net Git - kdenlive/blob - src/customtrackview.cpp
74488776aa2f2ccbf0851a5c6ef7f055f24865e7
[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             }
255         } else {
256             m_moveOpMode = NONE;
257             if (event->buttons() != Qt::NoButton) {
258                 setCursorPos((int) mapToScene(event->pos().x(), 0).x());
259             }
260             if (m_visualTip) {
261                 if (m_animation) delete m_animation;
262                 m_animationTimer->stop();
263                 m_animation = NULL;
264                 delete m_visualTip;
265                 m_visualTip = NULL;
266
267             }
268             setCursor(Qt::ArrowCursor);
269         }
270     }
271     QGraphicsView::mouseMoveEvent(event);
272 }
273
274 // virtual
275 void CustomTrackView::mousePressEvent(QMouseEvent * event) {
276     int pos = event->x();
277     if (event->modifiers() == Qt::ControlModifier) {
278         setDragMode(QGraphicsView::ScrollHandDrag);
279         QGraphicsView::mousePressEvent(event);
280         return;
281     } else if (event->modifiers() == Qt::ShiftModifier) {
282         setDragMode(QGraphicsView::RubberBandDrag);
283         QGraphicsView::mousePressEvent(event);
284         return;
285     } else {
286         bool collision = false;
287         QList<QGraphicsItem *> collisionList = items(event->pos());
288         for (int i = 0; i < collisionList.size(); ++i) {
289             QGraphicsItem *item = collisionList.at(i);
290             if (item->type() == 70000) {
291                 // select item
292                 if (!item->isSelected()) {
293                     QList<QGraphicsItem *> itemList = items();
294                     for (int i = 0; i < itemList.count(); i++)
295                         itemList.at(i)->setSelected(false);
296                     item->setSelected(true);
297                     update();
298                 }
299                 m_dragItem = (ClipItem *) item;
300                 emit clipItemSelected(m_dragItem);
301                 m_clickPoint = mapToScene(event->pos()).x() - m_dragItem->startPos() * m_scale;
302                 m_operationMode = m_dragItem->operationMode(item->mapFromScene(mapToScene(event->pos())), m_scale);
303                 if (m_operationMode == MOVE || m_operationMode == RESIZESTART)
304                     m_startPos = QPointF(m_dragItem->startPos(), m_dragItem->track());
305                 else if (m_operationMode == RESIZEEND)
306                     m_startPos = QPointF(m_dragItem->endPos(), m_dragItem->track());
307                 kDebug() << "//////// ITEM CLICKED: " << m_startPos;
308                 collision = true;
309                 break;
310             }
311         }
312         if (!collision) {
313             kDebug() << "//////// NO ITEM FOUND ON CLICK";
314             m_dragItem = NULL;
315             setCursor(Qt::ArrowCursor);
316             QList<QGraphicsItem *> itemList = items();
317             for (int i = 0; i < itemList.count(); i++)
318                 itemList.at(i)->setSelected(false);
319             emit clipItemSelected(NULL);
320             setCursorPos((int) mapToScene(event->x(), 0).x());
321         }
322     }
323     updateSnapPoints(m_dragItem);
324     //kDebug()<<pos;
325     //QGraphicsView::mousePressEvent(event);
326 }
327
328 void CustomTrackView::dragEnterEvent(QDragEnterEvent * event) {
329     if (event->mimeData()->hasFormat("kdenlive/producerslist")) {
330         kDebug() << "///////////////  DRAG ENTERED, TEXT: " << event->mimeData()->data("kdenlive/producerslist");
331         QStringList ids = QString(event->mimeData()->data("kdenlive/producerslist")).split(";");
332         //TODO: drop of several clips
333         for (int i = 0; i < ids.size(); ++i) {
334         }
335         DocClipBase *clip = m_document->getBaseClip(ids.at(0).toInt());
336         if (clip == NULL) kDebug() << " WARNING))))))))) CLIP NOT FOUND : " << ids.at(0).toInt();
337         addItem(clip, event->pos());
338         event->acceptProposedAction();
339     } else QGraphicsView::dragEnterEvent(event);
340 }
341
342 void CustomTrackView::slotRefreshEffects(ClipItem *clip) {
343     int track = m_tracksCount - clip->track();
344     GenTime pos = GenTime(clip->startPos(), m_document->fps());
345     m_document->renderer()->mltRemoveEffect(track, pos, "-1", false);
346     for (int i = 0; i < clip->effectsCount(); i++) {
347         m_document->renderer()->mltAddEffect(track, pos, clip->getEffectArgs(clip->effectAt(i)), false);
348     }
349     m_document->renderer()->doRefresh();
350 }
351
352 void CustomTrackView::addEffect(int track, GenTime pos, QDomElement effect) {
353     ClipItem *clip = getClipItemAt(pos.frames(m_document->fps()) + 1, m_tracksCount - track);
354     if (clip) {
355         QMap <QString, QString> effectParams = clip->addEffect(effect);
356         m_document->renderer()->mltAddEffect(track, pos, effectParams);
357         emit clipItemSelected(clip);
358     }
359 }
360
361 void CustomTrackView::deleteEffect(int track, GenTime pos, QDomElement effect) {
362     QString index = effect.attribute("kdenlive_ix");
363     m_document->renderer()->mltRemoveEffect(track, pos, index);
364     ClipItem *clip = getClipItemAt(pos.frames(m_document->fps()) + 1, m_tracksCount - track);
365     if (clip) {
366         clip->deleteEffect(index);
367         emit clipItemSelected(clip);
368     }
369 }
370
371 void CustomTrackView::slotAddEffect(QDomElement effect, GenTime pos, int track) {
372     QList<QGraphicsItem *> itemList;
373     if (track == -1)
374         itemList = items();
375     else {
376         ClipItem *clip = getClipItemAt(pos.frames(m_document->fps()) + 1, track);
377         if (clip) itemList.append(clip);
378         else kDebug() << "------   wrning, clip eff not found";
379     }
380     kDebug() << "// REQUESTING EFFECT ON CLIP: " << pos.frames(25) << ", TRK: " << track;
381     for (int i = 0; i < itemList.count(); i++) {
382         if (itemList.at(i)->type() == 70000 && (itemList.at(i)->isSelected() || track != -1)) {
383             ClipItem *item = (ClipItem *)itemList.at(i);
384             // the kdenlive_ix int is used to identify an effect in mlt's playlist, should
385             // not be changed
386             if (effect.attribute("kdenlive_ix").toInt() == 0)
387                 effect.setAttribute("kdenlive_ix", QString::number(item->effectsCounter()));
388             AddEffectCommand *command = new AddEffectCommand(this, m_tracksCount - item->track(), GenTime(item->startPos(), m_document->fps()), effect, true);
389             m_commandStack->push(command);
390         }
391     }
392 }
393
394 void CustomTrackView::slotDeleteEffect(ClipItem *clip, QDomElement effect) {
395     AddEffectCommand *command = new AddEffectCommand(this, m_tracksCount - clip->track(), GenTime(clip->startPos(), m_document->fps()), effect, false);
396     m_commandStack->push(command);
397 }
398
399 void CustomTrackView::updateEffect(int track, GenTime pos, QDomElement effect) {
400     ClipItem *clip = getClipItemAt(pos.frames(m_document->fps()) + 1, m_tracksCount - track);
401     if (clip) {
402         QMap <QString, QString> effectParams = clip->getEffectArgs(effect);
403         if (effectParams["disabled"] == "1") {
404             QString index = effectParams["kdenlive_ix"];
405             m_document->renderer()->mltRemoveEffect(track, pos, index);
406         } else m_document->renderer()->mltEditEffect(m_tracksCount - clip->track(), GenTime(clip->startPos(), m_document->fps()), effectParams);
407     }
408 }
409
410 void CustomTrackView::slotChangeEffectState(ClipItem *clip, QDomElement effect, bool disable) {
411     QDomElement oldEffect = effect.cloneNode().toElement();
412     effect.setAttribute("disabled", disable);
413     EditEffectCommand *command = new EditEffectCommand(this, m_tracksCount - clip->track(), GenTime(clip->startPos(), m_document->fps()), oldEffect, effect, true);
414     m_commandStack->push(command);
415 }
416
417 void CustomTrackView::slotUpdateClipEffect(ClipItem *clip, QDomElement oldeffect, QDomElement effect) {
418     EditEffectCommand *command = new EditEffectCommand(this, m_tracksCount - clip->track(), GenTime(clip->startPos(), m_document->fps()), oldeffect, effect, true);
419     m_commandStack->push(command);
420 }
421
422
423 void CustomTrackView::addItem(DocClipBase *clip, QPoint pos) {
424     int in = 0;
425     int out = clip->duration().frames(m_document->fps());
426     //kdDebug()<<"- - - -CREATING CLIP, duration = "<<out<<", URL: "<<clip->fileURL();
427     int trackTop = ((int) mapToScene(pos).y() / 50) * 50 + 1;
428     m_dropItem = new ClipItem(clip, ((int) mapToScene(pos).y() / 50), in, QRectF(mapToScene(pos).x() * m_scale, trackTop, out * m_scale, 49), out);
429     scene()->addItem(m_dropItem);
430 }
431
432
433 void CustomTrackView::dragMoveEvent(QDragMoveEvent * event) {
434     event->setDropAction(Qt::IgnoreAction);
435     //kDebug()<<"+++++++++++++   DRAG MOVE, : "<<mapToScene(event->pos()).x()<<", SCAL: "<<m_scale;
436     if (m_dropItem) {
437         int track = (int) mapToScene(event->pos()).y() / 50; //) * (m_scale * 50) + m_scale;
438         m_dropItem->moveTo(mapToScene(event->pos()).x() / m_scale, m_scale, (track - m_dropItem->track()) * 50, track);
439         event->setDropAction(Qt::MoveAction);
440         if (event->mimeData()->hasFormat("kdenlive/producerslist")) {
441             event->acceptProposedAction();
442         }
443     } else {
444         QGraphicsView::dragMoveEvent(event);
445     }
446 }
447
448 void CustomTrackView::dragLeaveEvent(QDragLeaveEvent * event) {
449     if (m_dropItem) {
450         delete m_dropItem;
451         m_dropItem = NULL;
452     } else QGraphicsView::dragLeaveEvent(event);
453 }
454
455 void CustomTrackView::dropEvent(QDropEvent * event) {
456     if (m_dropItem) {
457         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);
458         m_commandStack->push(command);
459         m_dropItem->baseClip()->addReference();
460         m_document->updateClip(m_dropItem->baseClip()->getId());
461         // kDebug()<<"IIIIIIIIIIIIIIIIIIIIIIII TRAX CNT: "<<m_tracksCount<<", DROP: "<<m_dropItem->track();
462         m_document->renderer()->mltInsertClip(m_tracksCount - m_dropItem->track(), GenTime(m_dropItem->startPos(), m_document->fps()), m_dropItem->xml());
463     } else QGraphicsView::dropEvent(event);
464     m_dropItem = NULL;
465 }
466
467
468 QStringList CustomTrackView::mimeTypes() const {
469     QStringList qstrList;
470     // list of accepted mime types for drop
471     qstrList.append("text/plain");
472     qstrList.append("kdenlive/producerslist");
473     return qstrList;
474 }
475
476 Qt::DropActions CustomTrackView::supportedDropActions() const {
477     // returns what actions are supported when dropping
478     return Qt::MoveAction;
479 }
480
481 void CustomTrackView::setDuration(int duration) {
482     kDebug() << "/////////////  PRO DUR: " << duration << ", height: " << 50 * m_tracksCount;
483     m_projectDuration = duration;
484     scene()->setSceneRect(0, 0, (m_projectDuration + 500) * m_scale, scene()->sceneRect().height()); //50 * m_tracksCount);
485 }
486
487
488 void CustomTrackView::addTrack() {
489     m_tracksCount++;
490     m_cursorLine->setLine(m_cursorLine->line().x1(), 0, m_cursorLine->line().x1(), 50 * m_tracksCount);
491     //setSceneRect(0, 0, sceneRect().width(), 50 * m_tracksCount);
492     //verticalScrollBar()->setMaximum(50 * m_tracksCount);
493     //setFixedHeight(50 * m_tracksCount);
494 }
495
496 void CustomTrackView::removeTrack() {
497     m_tracksCount--;
498     m_cursorLine->setLine(m_cursorLine->line().x1(), 0, m_cursorLine->line().x1(), 50 * m_tracksCount);
499 }
500
501 void CustomTrackView::deleteClip(int clipId) {
502     QList<QGraphicsItem *> itemList = items();
503     for (int i = 0; i < itemList.count(); i++) {
504         if (itemList.at(i)->type() == 70000) {
505             ClipItem *item = (ClipItem *)itemList.at(i);
506             if (item->clipProducer() == clipId) {
507                 AddTimelineClipCommand *command = new AddTimelineClipCommand(this, item->xml(), item->clipProducer(), item->track(), item->startPos(), item->rect(), item->duration(), true, true);
508                 m_commandStack->push(command);
509                 //delete item;
510             }
511         }
512     }
513 }
514
515 void CustomTrackView::setCursorPos(int pos, bool seek) {
516     emit cursorMoved(m_cursorPos, pos);
517     m_cursorPos = pos;
518     m_cursorLine->setPos(pos, 0);
519     int frame =  pos / m_scale;
520     if (seek) m_document->renderer()->seek(GenTime(frame, m_document->fps()));
521     else if (m_autoScroll && m_scale < 50) checkScrolling();
522 }
523
524 int CustomTrackView::cursorPos() {
525     return m_cursorPos;
526 }
527
528 void CustomTrackView::checkScrolling() {
529     QRect rectInView = viewport()->rect();
530     int delta = rectInView.width() / 3;
531     int max = rectInView.right() + horizontalScrollBar()->value() - delta;
532     //kDebug() << "CURSOR POS: "<<m_cursorPos<< "Scale: "<<m_scale;
533     if (m_cursorPos >= max) horizontalScrollBar()->setValue(horizontalScrollBar()->value() + 1 + m_scale);
534 }
535
536 void CustomTrackView::mouseReleaseEvent(QMouseEvent * event) {
537     QGraphicsView::mouseReleaseEvent(event);
538     setDragMode(QGraphicsView::NoDrag);
539     if (m_dragItem == NULL) return;
540     //kDebug()<<"/// MOVING CLIP: "<<m_startPos<<", END: "<<QPoint(m_dragItem->rect().x(),m_dragItem->rect().y());
541     if (m_operationMode == MOVE && m_startPos.x() != m_dragItem->startPos()) {
542         // move clip
543         MoveClipCommand *command = new MoveClipCommand(this, m_startPos, QPointF(m_dragItem->startPos(), m_dragItem->track()), false);
544         m_commandStack->push(command);
545         m_document->renderer()->mltMoveClip(m_tracksCount - m_startPos.y(), m_tracksCount - m_dragItem->track(), m_startPos.x(), m_dragItem->startPos());
546     } else if (m_operationMode == RESIZESTART) {
547         // resize start
548         ResizeClipCommand *command = new ResizeClipCommand(this, m_startPos, QPointF(m_dragItem->startPos(), m_dragItem->track()), true, false);
549
550         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()));
551         m_commandStack->push(command);
552         m_document->renderer()->doRefresh();
553     } else if (m_operationMode == RESIZEEND) {
554         // resize end
555         ResizeClipCommand *command = new ResizeClipCommand(this, m_startPos, QPointF(m_dragItem->endPos(), m_dragItem->track()), false, false);
556
557         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()));
558         m_commandStack->push(command);
559         m_document->renderer()->doRefresh();
560     }
561     m_operationMode = NONE;
562     m_dragItem = NULL;
563 }
564
565 void CustomTrackView::deleteClip(int track, int startpos, const QRectF &rect) {
566     ClipItem *item = getClipItemAt(startpos, track);
567     if (!item) {
568         kDebug() << "----------------  ERROR, CANNOT find clip to move at: " << rect.x();
569         return;
570     }
571     item->baseClip()->removeReference();
572     m_document->updateClip(item->baseClip()->getId());
573     delete item;
574     m_document->renderer()->mltRemoveClip(m_tracksCount - track, GenTime(startpos, m_document->fps()));
575     m_document->renderer()->doRefresh();
576 }
577
578 void CustomTrackView::addClip(QDomElement xml, int clipId, int track, int startpos, const QRectF &rect, int duration) {
579     QRect r(startpos * m_scale, 50 * track, duration * m_scale, 49);
580     DocClipBase *baseclip = m_document->clipManager()->getClipById(clipId);
581     ClipItem *item = new ClipItem(baseclip, track, startpos, r, duration);
582     scene()->addItem(item);
583     baseclip->addReference();
584     m_document->updateClip(baseclip->getId());
585     m_document->renderer()->mltInsertClip(m_tracksCount - track, GenTime(startpos, m_document->fps()), xml);
586     m_document->renderer()->doRefresh();
587 }
588
589 ClipItem *CustomTrackView::getClipItemAt(int pos, int track) {
590     return (ClipItem *) scene()->itemAt(pos * m_scale, track * 50 + 25);
591 }
592
593 void CustomTrackView::moveClip(const QPointF &startPos, const QPointF &endPos) {
594     ClipItem *item = getClipItemAt(startPos.x() + 1, startPos.y());
595     if (!item) {
596         kDebug() << "----------------  ERROR, CANNOT find clip to move at: " << startPos.x() * m_scale * FRAME_SIZE + 1 << ", " << startPos.y() * 50 + 25;
597         return;
598     }
599     kDebug() << "----------------  Move CLIP FROM: " << startPos.x() << ", END:" << endPos.x();
600     item->moveTo(endPos.x(), m_scale, (endPos.y() - startPos.y()) * 50, endPos.y());
601     m_document->renderer()->mltMoveClip(m_tracksCount - startPos.y(), m_tracksCount - endPos.y(), startPos.x(), endPos.x());
602 }
603
604 void CustomTrackView::resizeClip(const QPointF &startPos, const QPointF &endPos, bool resizeClipStart) {
605     int offset;
606     if (resizeClipStart) offset = 1;
607     else offset = -1;
608     ClipItem *item = getClipItemAt(startPos.x() + offset, startPos.y());
609     if (!item) {
610         kDebug() << "----------------  ERROR, CANNOT find clip to resize at: " << startPos;
611         return;
612     }
613     qreal diff = endPos.x() - startPos.x();
614     if (resizeClipStart) {
615         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()));
616         item->resizeStart(endPos.x(), m_scale);
617     } else {
618         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()));
619         item->resizeEnd(endPos.x(), m_scale);
620     }
621     m_document->renderer()->doRefresh();
622 }
623
624 double CustomTrackView::getSnapPointForPos(double pos) {
625     for (int i = 0; i < m_snapPoints.size(); ++i) {
626         //kDebug()<<"SNAP POINT: "<<m_snapPoints.at(i);
627         if (abs(pos - m_snapPoints.at(i) * m_scale) < 6 * m_scale) {
628             //kDebug()<<" FOUND SNAP POINT AT: "<<m_snapPoints.at(i)<<", current pos: "<<pos / m_scale;
629             return m_snapPoints.at(i) * m_scale + 0.5;
630         }
631         if (m_snapPoints.at(i) > pos) break;
632     }
633     return pos;
634 }
635
636 void CustomTrackView::updateSnapPoints(ClipItem *selected) {
637     m_snapPoints.clear();
638     int offset = 0;
639     if (selected) offset = selected->duration();
640     QList<QGraphicsItem *> itemList = items();
641     for (int i = 0; i < itemList.count(); i++) {
642         if (itemList.at(i)->type() == 70000 && itemList.at(i) != selected) {
643             ClipItem *item = (ClipItem *)itemList.at(i);
644             int start = item->startPos();
645             int fadein = item->fadeIn() + start;
646             int end = item->endPos();
647             int fadeout = end - item->fadeOut();
648             m_snapPoints.append(start);
649             if (fadein != start) m_snapPoints.append(fadein);
650             m_snapPoints.append(end);
651             if (fadeout != end) m_snapPoints.append(fadeout);
652             if (offset != 0) {
653                 m_snapPoints.append(start - offset);
654                 if (fadein != start) m_snapPoints.append(fadein - offset);
655                 m_snapPoints.append(end - offset);
656                 if (fadeout != end) m_snapPoints.append(fadeout - offset);
657             }
658         }
659     }
660     kDebug() << " GOT SNAPPOINTS TOTAL: " << m_snapPoints.count();
661     qSort(m_snapPoints);
662     for (int i = 0; i < m_snapPoints.size(); ++i)
663         kDebug() << "SNAP POINT: " << m_snapPoints.at(i);
664 }
665
666
667 void CustomTrackView::setScale(double scaleFactor) {
668     //scale(scaleFactor, scaleFactor);
669     m_scale = scaleFactor;
670     kDebug() << " HHHHHHHH  SCALING: " << m_scale;
671     QList<QGraphicsItem *> itemList = items();
672     scene()->setSceneRect(0, 0, (m_projectDuration + 500) * m_scale, scene()->sceneRect().height()); //50 *
673
674     for (int i = 0; i < itemList.count(); i++) {
675         if (itemList.at(i)->type() == 70000) {
676             ClipItem *clip = (ClipItem *)itemList.at(i);
677             clip->setRect(clip->startPos() * m_scale, clip->rect().y(), clip->duration() * m_scale, clip->rect().height());
678         }
679         /*else if (itemList.at(i)->type() == 70001) {
680         LabelItem *label = (LabelItem *)itemList.at(i);
681         QGraphicsItem *parent = label->parentItem();
682         QRectF r = label->boundingRect();
683         QRectF p = parent->boundingRect();
684         label->setPos(p.x() + p.width() / 2 - r.width() / 2, p.y() + p.height() / 2 - r.height() / 2);
685         //label->setRect(clip->startPos() * m_scale, clip->rect().y(), clip->duration() * m_scale, clip->rect().height());
686         }*/
687     }
688 }
689
690 void CustomTrackView::drawBackground(QPainter * painter, const QRectF & rect) {
691     QRect rectInView = viewport()->rect();
692     rectInView.moveTo(horizontalScrollBar()->value(), verticalScrollBar()->value());
693
694     QColor base = palette().button().color();
695     painter->setClipRect(rect);
696     painter->drawLine(rectInView.left(), 0, rectInView.right(), 0);
697     for (uint i = 0; i < m_tracksCount;i++) {
698         painter->drawLine(rectInView.left(), 50 * (i + 1), rectInView.right(), 50 * (i + 1));
699         painter->drawText(QRectF(10, 50 * i, 100, 50 * i + 49), Qt::AlignLeft, i18n(" Track ") + QString::number(i + 1));
700     }
701     int lowerLimit = 50 * m_tracksCount + 1;
702     if (height() > lowerLimit)
703         painter->fillRect(QRectF(rectInView.left(), lowerLimit, rectInView.width(), height() - lowerLimit), QBrush(base));
704 }
705 /*
706 void CustomTrackView::drawForeground ( QPainter * painter, const QRectF & rect )
707 {
708   //kDebug()<<"/////  DRAWING FB: "<<rect.x()<<", width: "<<rect.width();
709   painter->fillRect(rect, QColor(50, rand() % 250,50,100));
710   painter->drawLine(m_cursorPos, rect.y(), m_cursorPos, rect.y() + rect.height());
711 }
712 */
713 #include "customtrackview.moc"