]> git.sesse.net Git - kdenlive/blob - src/customtrackview.cpp
dd569e52c12f74ecad0b7bba3723d2bc1abba734
[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() - QPoint(0, (m_dragItem->type() == 70001 ? 25 : 0))).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(
351                         QRect(m_dragItem->startPos().frames(m_document->fps()) *m_scale , m_dragItem->rect().height() / 2,
352                               GenTime(2.5).frames(m_document->fps()) *m_scale ,  m_dragItem->rect().height()
353                              ),
354                         (ClipItem*)m_dragItem, LUMA_TRANSITION, m_dragItem->startPos(), m_dragItem->startPos() + GenTime(2.5), m_document->fps());
355                     tr->setTrack(m_dragItem->track());
356                     scene()->addItem(tr);
357                     //m_dragItem->addTransition(tra);
358                 }
359
360                 kDebug() << "//////// ITEM CLICKED: " << m_startPos;
361                 collision = true;
362                 break;
363             }
364         }
365         if (!collision) {
366             kDebug() << "//////// NO ITEM FOUND ON CLICK";
367             m_dragItem = NULL;
368             setCursor(Qt::ArrowCursor);
369             QList<QGraphicsItem *> itemList = items();
370             for (int i = 0; i < itemList.count(); i++)
371                 itemList.at(i)->setSelected(false);
372             emit clipItemSelected(NULL);
373             setCursorPos((int) mapToScene(event->x(), 0).x() / m_scale);
374         }
375     }
376     updateSnapPoints(m_dragItem);
377     //kDebug()<<pos;
378     //QGraphicsView::mousePressEvent(event);
379 }
380
381 void CustomTrackView::dragEnterEvent(QDragEnterEvent * event) {
382     if (event->mimeData()->hasFormat("kdenlive/producerslist")) {
383         kDebug() << "///////////////  DRAG ENTERED, TEXT: " << event->mimeData()->data("kdenlive/producerslist");
384         QStringList ids = QString(event->mimeData()->data("kdenlive/producerslist")).split(";");
385         //TODO: drop of several clips
386         for (int i = 0; i < ids.size(); ++i) {
387         }
388         DocClipBase *clip = m_document->getBaseClip(ids.at(0).toInt());
389         if (clip == NULL) kDebug() << " WARNING))))))))) CLIP NOT FOUND : " << ids.at(0).toInt();
390         addItem(clip, event->pos());
391         event->acceptProposedAction();
392     } else QGraphicsView::dragEnterEvent(event);
393 }
394
395 void CustomTrackView::slotRefreshEffects(ClipItem *clip) {
396     int track = m_tracksCount - clip->track();
397     GenTime pos = clip->startPos();
398     m_document->renderer()->mltRemoveEffect(track, pos, "-1", false);
399     for (int i = 0; i < clip->effectsCount(); i++) {
400         m_document->renderer()->mltAddEffect(track, pos, clip->getEffectArgs(clip->effectAt(i)), false);
401     }
402     m_document->renderer()->doRefresh();
403 }
404
405 void CustomTrackView::addEffect(int track, GenTime pos, QDomElement effect) {
406     ClipItem *clip = getClipItemAt(pos.frames(m_document->fps()) + 1, m_tracksCount - track);
407     if (clip) {
408         QMap <QString, QString> effectParams = clip->addEffect(effect);
409         m_document->renderer()->mltAddEffect(track, pos, effectParams);
410         emit clipItemSelected(clip);
411     }
412 }
413
414 void CustomTrackView::deleteEffect(int track, GenTime pos, QDomElement effect) {
415     QString index = effect.attribute("kdenlive_ix");
416     m_document->renderer()->mltRemoveEffect(track, pos, index);
417     ClipItem *clip = getClipItemAt(pos.frames(m_document->fps()) + 1, m_tracksCount - track);
418     if (clip) {
419         clip->deleteEffect(index);
420         emit clipItemSelected(clip);
421     }
422 }
423
424 void CustomTrackView::slotAddEffect(QDomElement effect, GenTime pos, int track) {
425     QList<QGraphicsItem *> itemList;
426     if (track == -1)
427         itemList = items();
428     else {
429         ClipItem *clip = getClipItemAt(pos.frames(m_document->fps()) + 1, track);
430         if (clip) itemList.append(clip);
431         else kDebug() << "------   wrning, clip eff not found";
432     }
433     kDebug() << "// REQUESTING EFFECT ON CLIP: " << pos.frames(25) << ", TRK: " << track;
434     for (int i = 0; i < itemList.count(); i++) {
435         if (itemList.at(i)->type() == 70000 && (itemList.at(i)->isSelected() || track != -1)) {
436             ClipItem *item = (ClipItem *)itemList.at(i);
437             // the kdenlive_ix int is used to identify an effect in mlt's playlist, should
438             // not be changed
439             if (effect.attribute("kdenlive_ix").toInt() == 0)
440                 effect.setAttribute("kdenlive_ix", QString::number(item->effectsCounter()));
441             AddEffectCommand *command = new AddEffectCommand(this, m_tracksCount - item->track(), item->startPos(), effect, true);
442             m_commandStack->push(command);
443         }
444     }
445 }
446
447 void CustomTrackView::slotDeleteEffect(ClipItem *clip, QDomElement effect) {
448     AddEffectCommand *command = new AddEffectCommand(this, m_tracksCount - clip->track(), clip->startPos(), effect, false);
449     m_commandStack->push(command);
450 }
451
452 void CustomTrackView::updateEffect(int track, GenTime pos, QDomElement effect) {
453     ClipItem *clip = getClipItemAt(pos.frames(m_document->fps()) + 1, m_tracksCount - track);
454     if (clip) {
455         QMap <QString, QString> effectParams = clip->getEffectArgs(effect);
456         if (effectParams["disabled"] == "1") {
457             QString index = effectParams["kdenlive_ix"];
458             m_document->renderer()->mltRemoveEffect(track, pos, index);
459         } else m_document->renderer()->mltEditEffect(m_tracksCount - clip->track(), clip->startPos(), effectParams);
460     }
461 }
462
463 void CustomTrackView::slotChangeEffectState(ClipItem *clip, QDomElement effect, bool disable) {
464     QDomElement oldEffect = effect.cloneNode().toElement();
465     effect.setAttribute("disabled", disable);
466     EditEffectCommand *command = new EditEffectCommand(this, m_tracksCount - clip->track(), clip->startPos(), oldEffect, effect, true);
467     m_commandStack->push(command);
468 }
469
470 void CustomTrackView::slotUpdateClipEffect(ClipItem *clip, QDomElement oldeffect, QDomElement effect) {
471     EditEffectCommand *command = new EditEffectCommand(this, m_tracksCount - clip->track(), clip->startPos(), oldeffect, effect, true);
472     m_commandStack->push(command);
473 }
474
475
476 void CustomTrackView::addItem(DocClipBase *clip, QPoint pos) {
477     int in = 0;
478     GenTime out = clip->duration();
479     //kdDebug()<<"- - - -CREATING CLIP, duration = "<<out<<", URL: "<<clip->fileURL();
480     int trackTop = ((int) mapToScene(pos).y() / 50) * 50 + 1;
481     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());
482     scene()->addItem(m_dropItem);
483 }
484
485
486 void CustomTrackView::dragMoveEvent(QDragMoveEvent * event) {
487     event->setDropAction(Qt::IgnoreAction);
488     //kDebug()<<"+++++++++++++   DRAG MOVE, : "<<mapToScene(event->pos()).x()<<", SCAL: "<<m_scale;
489     if (m_dropItem) {
490         int track = (int) mapToScene(event->pos()).y() / 50; //) * (m_scale * 50) + m_scale;
491         m_dropItem->moveTo(mapToScene(event->pos()).x() / m_scale, m_scale, (track - m_dropItem->track()) * 50, track);
492         event->setDropAction(Qt::MoveAction);
493         if (event->mimeData()->hasFormat("kdenlive/producerslist")) {
494             event->acceptProposedAction();
495         }
496     } else {
497         QGraphicsView::dragMoveEvent(event);
498     }
499 }
500
501 void CustomTrackView::dragLeaveEvent(QDragLeaveEvent * event) {
502     if (m_dropItem) {
503         delete m_dropItem;
504         m_dropItem = NULL;
505     } else QGraphicsView::dragLeaveEvent(event);
506 }
507
508 void CustomTrackView::dropEvent(QDropEvent * event) {
509     if (m_dropItem) {
510         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);
511         m_commandStack->push(command);
512         m_dropItem->baseClip()->addReference();
513         m_document->updateClip(m_dropItem->baseClip()->getId());
514         // kDebug()<<"IIIIIIIIIIIIIIIIIIIIIIII TRAX CNT: "<<m_tracksCount<<", DROP: "<<m_dropItem->track();
515         m_document->renderer()->mltInsertClip(m_tracksCount - m_dropItem->track(), m_dropItem->startPos(), m_dropItem->xml());
516     } else QGraphicsView::dropEvent(event);
517     m_dropItem = NULL;
518 }
519
520
521 QStringList CustomTrackView::mimeTypes() const {
522     QStringList qstrList;
523     // list of accepted mime types for drop
524     qstrList.append("text/plain");
525     qstrList.append("kdenlive/producerslist");
526     return qstrList;
527 }
528
529 Qt::DropActions CustomTrackView::supportedDropActions() const {
530     // returns what actions are supported when dropping
531     return Qt::MoveAction;
532 }
533
534 void CustomTrackView::setDuration(int duration) {
535     kDebug() << "/////////////  PRO DUR: " << duration << ", height: " << 50 * m_tracksCount;
536     m_projectDuration = duration;
537     scene()->setSceneRect(0, 0, (m_projectDuration + 500) * m_scale, scene()->sceneRect().height()); //50 * m_tracksCount);
538 }
539
540
541 void CustomTrackView::addTrack() {
542     m_tracksCount++;
543     m_cursorLine->setLine(m_cursorLine->line().x1(), 0, m_cursorLine->line().x1(), 50 * m_tracksCount);
544     //setSceneRect(0, 0, sceneRect().width(), 50 * m_tracksCount);
545     //verticalScrollBar()->setMaximum(50 * m_tracksCount);
546     //setFixedHeight(50 * m_tracksCount);
547 }
548
549 void CustomTrackView::removeTrack() {
550     m_tracksCount--;
551     m_cursorLine->setLine(m_cursorLine->line().x1(), 0, m_cursorLine->line().x1(), 50 * m_tracksCount);
552 }
553
554 void CustomTrackView::deleteClip(int clipId) {
555     QList<QGraphicsItem *> itemList = items();
556     for (int i = 0; i < itemList.count(); i++) {
557         if (itemList.at(i)->type() == 70000) {
558             ClipItem *item = (ClipItem *)itemList.at(i);
559             if (item->clipProducer() == clipId) {
560                 AddTimelineClipCommand *command = new AddTimelineClipCommand(this, item->xml(), item->clipProducer(), item->track(), item->startPos(), item->rect(), item->duration(), true, true);
561                 m_commandStack->push(command);
562                 //delete item;
563             }
564         }
565     }
566 }
567
568 void CustomTrackView::setCursorPos(int pos, bool seek) {
569     emit cursorMoved(m_cursorPos * m_scale, pos * m_scale);
570     m_cursorPos = pos;
571     m_cursorLine->setPos(pos * m_scale, 0);
572     if (seek) m_document->renderer()->seek(GenTime(pos, m_document->fps()));
573     else if (m_autoScroll && m_scale < 50) checkScrolling();
574 }
575
576 void CustomTrackView::updateCursorPos() {
577     m_cursorLine->setPos(m_cursorPos * m_scale, 0);
578 }
579
580 int CustomTrackView::cursorPos() {
581     return m_cursorPos * m_scale;
582 }
583
584 void CustomTrackView::checkScrolling() {
585     QRect rectInView = viewport()->rect();
586     int delta = rectInView.width() / 3;
587     int max = rectInView.right() + horizontalScrollBar()->value() - delta;
588     //kDebug() << "CURSOR POS: "<<m_cursorPos<< "Scale: "<<m_scale;
589     if (m_cursorPos * m_scale >= max) horizontalScrollBar()->setValue(horizontalScrollBar()->value() + 1 + m_scale);
590 }
591
592 void CustomTrackView::mouseReleaseEvent(QMouseEvent * event) {
593     QGraphicsView::mouseReleaseEvent(event);
594     setDragMode(QGraphicsView::NoDrag);
595     if (m_dragItem == NULL) return;
596     if (m_operationMode == MOVE) setCursor(Qt::OpenHandCursor);
597     if (m_operationMode == MOVE && m_startPos.x() != m_dragItem->startPos().frames(m_document->fps())) {
598         // move clip
599         MoveClipCommand *command = new MoveClipCommand(this, m_startPos, QPointF(m_dragItem->startPos().frames(m_document->fps()), m_dragItem->track()), false);
600         m_commandStack->push(command);
601         m_document->renderer()->mltMoveClip(m_tracksCount - m_startPos.y(), m_tracksCount - m_dragItem->track(), m_startPos.x(), m_dragItem->startPos().frames(m_document->fps()));
602     } else if (m_operationMode == RESIZESTART) {
603         // resize start
604         ResizeClipCommand *command = new ResizeClipCommand(this, m_startPos, QPointF(m_dragItem->startPos().frames(m_document->fps()), m_dragItem->track()), true, false);
605
606         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());
607         m_commandStack->push(command);
608         m_document->renderer()->doRefresh();
609     } else if (m_operationMode == RESIZEEND) {
610         // resize end
611         ResizeClipCommand *command = new ResizeClipCommand(this, m_startPos, QPointF(m_dragItem->endPos().frames(m_document->fps()), m_dragItem->track()), false, false);
612
613         m_document->renderer()->mltResizeClipEnd(m_tracksCount - m_dragItem->track(), m_dragItem->startPos(), m_dragItem->cropStart(), m_dragItem->cropStart() + m_dragItem->endPos() - m_dragItem->startPos());
614         m_commandStack->push(command);
615         m_document->renderer()->doRefresh();
616     }
617     m_operationMode = NONE;
618     m_dragItem = NULL;
619 }
620
621 void CustomTrackView::deleteClip(int track, GenTime startpos, const QRectF &rect) {
622     ClipItem *item = getClipItemAt(startpos, track);
623     if (!item) {
624         kDebug() << "----------------  ERROR, CANNOT find clip to move at: " << rect.x();
625         return;
626     }
627     item->baseClip()->removeReference();
628     m_document->updateClip(item->baseClip()->getId());
629     delete item;
630     m_document->renderer()->mltRemoveClip(m_tracksCount - track, startpos);
631     m_document->renderer()->doRefresh();
632 }
633
634 void CustomTrackView::addClip(QDomElement xml, int clipId, int track, GenTime startpos, const QRectF &rect, GenTime duration) {
635     QRect r(startpos.frames(m_document->fps()) * m_scale, 50 * track, duration.frames(m_document->fps()) * m_scale, 49);
636     DocClipBase *baseclip = m_document->clipManager()->getClipById(clipId);
637     ClipItem *item = new ClipItem(baseclip, track, startpos, r, duration, m_document->fps());
638     scene()->addItem(item);
639     baseclip->addReference();
640     m_document->updateClip(baseclip->getId());
641     m_document->renderer()->mltInsertClip(m_tracksCount - track, startpos, xml);
642     m_document->renderer()->doRefresh();
643 }
644
645 ClipItem *CustomTrackView::getClipItemAt(int pos, int track) {
646     return (ClipItem *) scene()->itemAt(pos * m_scale, track * 50 + 25);
647 }
648
649 ClipItem *CustomTrackView::getClipItemAt(GenTime pos, int track) {
650     return (ClipItem *) scene()->itemAt(pos.frames(m_document->fps()) * m_scale, track * 50 + 25);
651 }
652
653 void CustomTrackView::moveClip(const QPointF &startPos, const QPointF &endPos) {
654     ClipItem *item = getClipItemAt(startPos.x() + 1, startPos.y());
655     if (!item) {
656         kDebug() << "----------------  ERROR, CANNOT find clip to move at: " << startPos.x() * m_scale * FRAME_SIZE + 1 << ", " << startPos.y() * 50 + 25;
657         return;
658     }
659     kDebug() << "----------------  Move CLIP FROM: " << startPos.x() << ", END:" << endPos.x();
660     item->moveTo(endPos.x(), m_scale, (endPos.y() - startPos.y()) * 50, endPos.y());
661     m_document->renderer()->mltMoveClip(m_tracksCount - startPos.y(), m_tracksCount - endPos.y(), startPos.x(), endPos.x());
662 }
663
664 void CustomTrackView::resizeClip(const QPointF &startPos, const QPointF &endPos, bool resizeClipStart) {
665     int offset;
666     if (resizeClipStart) offset = 1;
667     else offset = -1;
668     ClipItem *item = getClipItemAt(startPos.x() + offset, startPos.y());
669     if (!item) {
670         kDebug() << "----------------  ERROR, CANNOT find clip to resize at: " << startPos;
671         return;
672     }
673     qreal diff = endPos.x() - startPos.x();
674     if (resizeClipStart) {
675         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()));
676         item->resizeStart(endPos.x(), m_scale);
677     } else {
678         m_document->renderer()->mltResizeClipEnd(m_tracksCount - item->track(), item->startPos(), item->cropStart(), item->cropStart() + GenTime(endPos.x(), m_document->fps()) - item->startPos());
679         item->resizeEnd(endPos.x(), m_scale);
680     }
681     m_document->renderer()->doRefresh();
682 }
683
684 double CustomTrackView::getSnapPointForPos(double pos) {
685     for (int i = 0; i < m_snapPoints.size(); ++i) {
686         if (abs(pos - m_snapPoints.at(i).frames(m_document->fps()) * m_scale) < 10) {
687             //kDebug()<<" FOUND SNAP POINT AT: "<<m_snapPoints.at(i)<<", current pos: "<<pos / m_scale;
688             return m_snapPoints.at(i).frames(m_document->fps()) * m_scale + 0.5;
689         }
690         if (m_snapPoints.at(i).frames(m_document->fps() * m_scale) > pos) break;
691     }
692     return pos;
693 }
694
695 void CustomTrackView::updateSnapPoints(AbstractClipItem *selected) {
696     m_snapPoints.clear();
697     GenTime offset;
698     if (selected) offset = selected->duration();
699     QList<QGraphicsItem *> itemList = items();
700     for (int i = 0; i < itemList.count(); i++) {
701         if (itemList.at(i)->type() == 70000 && itemList.at(i) != selected) {
702             ClipItem *item = (ClipItem *)itemList.at(i);
703             GenTime start = item->startPos();
704             GenTime end = item->endPos();
705             m_snapPoints.append(start);
706             m_snapPoints.append(end);
707             if (offset != GenTime()) {
708                 if (start > offset) m_snapPoints.append(start - offset);
709                 if (end > offset) m_snapPoints.append(end - offset);
710             }
711         }
712     }
713     qSort(m_snapPoints);
714     //for (int i = 0; i < m_snapPoints.size(); ++i)
715     //    kDebug() << "SNAP POINT: " << m_snapPoints.at(i).frames(25);
716 }
717
718
719 void CustomTrackView::setScale(double scaleFactor) {
720     //scale(scaleFactor, scaleFactor);
721     double pos = cursorPos() / m_scale;
722     m_scale = scaleFactor;
723     kDebug() << " HHHHHHHH  SCALING: " << m_scale;
724     QList<QGraphicsItem *> itemList = items();
725
726     for (int i = 0; i < itemList.count(); i++) {
727         if (itemList.at(i)->type() == 70000 || itemList.at(i)->type() == 70001) {
728             AbstractClipItem *clip = (AbstractClipItem *)itemList.at(i);
729             clip->setRect(clip->startPos().frames(m_document->fps()) * m_scale, clip->rect().y(), clip->duration().frames(m_document->fps()) * m_scale, clip->rect().height());
730         }
731     }
732     updateCursorPos();
733     centerOn(QPointF(cursorPos(), 50));
734     scene()->setSceneRect(0, 0, (m_projectDuration + 500) * m_scale, scene()->sceneRect().height());
735 }
736
737 void CustomTrackView::drawBackground(QPainter * painter, const QRectF & rect) {
738     QRect rectInView = viewport()->rect();
739     rectInView.moveTo(horizontalScrollBar()->value(), verticalScrollBar()->value());
740
741     QColor base = palette().button().color();
742     painter->setClipRect(rect);
743     painter->drawLine(rectInView.left(), 0, rectInView.right(), 0);
744     for (uint i = 0; i < m_tracksCount;i++) {
745         painter->drawLine(rectInView.left(), 50 * (i + 1), rectInView.right(), 50 * (i + 1));
746         painter->drawText(QRectF(10, 50 * i, 100, 50 * i + 49), Qt::AlignLeft, i18n(" Track ") + QString::number(i + 1));
747     }
748     int lowerLimit = 50 * m_tracksCount + 1;
749     if (height() > lowerLimit)
750         painter->fillRect(QRectF(rectInView.left(), lowerLimit, rectInView.width(), height() - lowerLimit), QBrush(base));
751 }
752 /*
753 void CustomTrackView::drawForeground ( QPainter * painter, const QRectF & rect )
754 {
755   //kDebug()<<"/////  DRAWING FB: "<<rect.x()<<", width: "<<rect.width();
756   painter->fillRect(rect, QColor(50, rand() % 250,50,100));
757   painter->drawLine(m_cursorPos, rect.y(), m_cursorPos, rect.y() + rect.height());
758 }
759 */
760 #include "customtrackview.moc"