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