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