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