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