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