]> git.sesse.net Git - kdenlive/blob - src/customtrackview.cpp
Nicer timeline visual tips, fix video thumbnails being set on wrong clip
[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       effect.setAttribute("kdenlive_ix", QString::number(item->effectsCounter()));
413       AddEffectCommand *command = new AddEffectCommand(this, m_tracksCount - item->track(),GenTime(item->startPos(), m_document->fps()), effect, true);
414       m_commandStack->push(command);    
415     }
416   }
417 }
418
419 void CustomTrackView::slotDeleteEffect(ClipItem *clip, QDomElement effect)
420 {
421   AddEffectCommand *command = new AddEffectCommand(this, m_tracksCount - clip->track(), GenTime(clip->startPos(), m_document->fps()), effect, false);
422   m_commandStack->push(command);
423 }
424
425 void CustomTrackView::updateEffect(int track, GenTime pos, QDomElement effect)
426 {
427   ClipItem *clip = getClipItemAt(pos.frames(m_document->fps()) + 1, m_tracksCount - track);
428   if (clip){
429     QMap <QString, QString> effectParams = clip->getEffectArgs(effect);
430     if (effectParams["disabled"] == "1") {
431       QString index = effectParams["kdenlive_ix"];
432       m_document->renderer()->mltRemoveEffect(track, pos, index);  
433     }
434     else m_document->renderer()->mltEditEffect(m_tracksCount - clip->track(), GenTime(clip->startPos(), m_document->fps()), effectParams);
435   }
436 }
437
438 void CustomTrackView::slotChangeEffectState(ClipItem *clip, QDomElement effect, bool disable)
439 {
440   QDomElement oldEffect = effect.cloneNode().toElement();
441   effect.setAttribute("disabled", disable);
442   EditEffectCommand *command = new EditEffectCommand(this, m_tracksCount - clip->track(), GenTime(clip->startPos(), m_document->fps()), oldEffect, effect, true);
443   m_commandStack->push(command);
444 }
445
446 void CustomTrackView::slotUpdateClipEffect(ClipItem *clip, QDomElement oldeffect, QDomElement effect)
447 {
448   EditEffectCommand *command = new EditEffectCommand(this, m_tracksCount - clip->track(), GenTime(clip->startPos(), m_document->fps()), oldeffect, effect, true);
449   m_commandStack->push(command);
450 }
451
452
453 void CustomTrackView::addItem(DocClipBase *clip, QPoint pos)
454 {
455   int in =0;
456   int out = clip->duration().frames(m_document->fps());
457   //kdDebug()<<"- - - -CREATING CLIP, duration = "<<out<<", URL: "<<clip->fileURL();
458   int trackTop = ((int) mapToScene(pos).y()/50) * 50 + 1;
459   m_dropItem = new ClipItem(clip, ((int) mapToScene(pos).y()/50), in, QRectF(mapToScene(pos).x() * m_scale, trackTop, out * m_scale, 49), out);
460   scene()->addItem(m_dropItem);
461 }
462
463
464 void CustomTrackView::dragMoveEvent(QDragMoveEvent * event) {
465   event->setDropAction(Qt::IgnoreAction);
466   //kDebug()<<"+++++++++++++   DRAG MOVE, : "<<mapToScene(event->pos()).x()<<", SCAL: "<<m_scale;
467   if (m_dropItem) {
468     int track = (int) mapToScene(event->pos()).y()/50; //) * (m_scale * 50) + m_scale;
469     m_dropItem->moveTo(mapToScene(event->pos()).x() / m_scale, m_scale, (track - m_dropItem->track()) * 50, track);
470     event->setDropAction(Qt::MoveAction);
471     if (event->mimeData()->hasFormat("kdenlive/producerslist")) {
472       event->acceptProposedAction();
473     }
474   }
475   else {
476     QGraphicsView::dragMoveEvent(event);
477   }
478 }
479
480 void CustomTrackView::dragLeaveEvent ( QDragLeaveEvent * event ) {
481   if (m_dropItem) {
482     delete m_dropItem;
483     m_dropItem = NULL;
484   }
485   else QGraphicsView::dragLeaveEvent(event);
486 }
487
488 void CustomTrackView::dropEvent ( QDropEvent * event ) {
489   if (m_dropItem) {
490     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);
491     m_commandStack->push(command);
492     m_dropItem->baseClip()->addReference();
493     m_document->updateClip(m_dropItem->baseClip()->getId());
494     // kDebug()<<"IIIIIIIIIIIIIIIIIIIIIIII TRAX CNT: "<<m_tracksCount<<", DROP: "<<m_dropItem->track();
495     m_document->renderer()->mltInsertClip(m_tracksCount - m_dropItem->track(), GenTime(m_dropItem->startPos(), m_document->fps()), m_dropItem->xml());
496   }
497   else QGraphicsView::dropEvent(event);  
498   m_dropItem = NULL;
499 }
500
501
502 QStringList CustomTrackView::mimeTypes () const
503 {
504     QStringList qstrList;
505     // list of accepted mime types for drop
506     qstrList.append("text/plain");
507     qstrList.append("kdenlive/producerslist");
508     return qstrList;
509 }
510
511 Qt::DropActions CustomTrackView::supportedDropActions () const
512 {
513     // returns what actions are supported when dropping
514     return Qt::MoveAction;
515 }
516
517 void CustomTrackView::setDuration(int duration)
518 {
519   kDebug()<<"/////////////  PRO DUR: "<<duration<<", height: "<<50 * m_tracksCount;
520   m_projectDuration = duration;
521   scene()->setSceneRect(0, 0, (m_projectDuration + 500) * m_scale, scene()->sceneRect().height()); //50 * m_tracksCount);
522 }
523
524
525 void CustomTrackView::addTrack ()
526 {
527   m_tracksCount++;
528   m_cursorLine->setLine(m_cursorLine->line().x1(), 0, m_cursorLine->line().x1(), 50 * m_tracksCount);
529   //setSceneRect(0, 0, sceneRect().width(), 50 * m_tracksCount);
530   //verticalScrollBar()->setMaximum(50 * m_tracksCount); 
531   //setFixedHeight(50 * m_tracksCount);
532 }
533
534 void CustomTrackView::removeTrack ()
535 {
536   m_tracksCount--;
537   m_cursorLine->setLine(m_cursorLine->line().x1(), 0, m_cursorLine->line().x1(), 50 * m_tracksCount);
538 }
539
540 void CustomTrackView::deleteClip(int clipId)
541 {
542   QList<QGraphicsItem *> itemList = items();
543   for (int i = 0; i < itemList.count(); i++) {
544     if (itemList.at(i)->type() == 70000) {
545       ClipItem *item = (ClipItem *)itemList.at(i);
546       if (item->clipProducer() == clipId) {
547         AddTimelineClipCommand *command = new AddTimelineClipCommand(this, item->xml(), item->clipProducer(), item->track(), item->startPos(), item->rect(), item->duration(), true, true);
548         m_commandStack->push(command);
549         //delete item;
550       }
551     }
552   }
553 }
554
555 void CustomTrackView::setCursorPos(int pos, bool seek)
556 {
557   m_cursorPos = pos;
558   m_cursorLine->setPos(pos, 0);
559   int frame =  pos / m_scale;
560   if (seek) m_document->renderer()->seek(GenTime(frame, m_document->fps()));
561 }
562
563 int CustomTrackView::cursorPos()
564 {
565   return m_cursorPos;
566 }
567
568 void CustomTrackView::mouseReleaseEvent ( QMouseEvent * event )
569 {
570   QGraphicsView::mouseReleaseEvent(event);
571   setDragMode(QGraphicsView::NoDrag);
572   if (m_dragItem == NULL) return;
573   //kDebug()<<"/// MOVING CLIP: "<<m_startPos<<", END: "<<QPoint(m_dragItem->rect().x(),m_dragItem->rect().y());
574   if (m_operationMode == MOVE && m_startPos.x() != m_dragItem->startPos()) {
575     // move clip
576     MoveClipCommand *command = new MoveClipCommand(this, m_startPos, QPointF(m_dragItem->startPos(), m_dragItem->track()), false);
577     m_commandStack->push(command);
578     m_document->renderer()->mltMoveClip(m_tracksCount - m_startPos.y(), m_tracksCount - m_dragItem->track(), m_startPos.x(), m_dragItem->startPos());
579   }
580   else if (m_operationMode == RESIZESTART) {
581     // resize start
582     ResizeClipCommand *command = new ResizeClipCommand(this, m_startPos, QPointF(m_dragItem->startPos(), m_dragItem->track()), true, false);
583
584     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()));
585     m_commandStack->push(command);
586     m_document->renderer()->doRefresh();
587   }
588   else if (m_operationMode == RESIZEEND) {
589     // resize end
590     ResizeClipCommand *command = new ResizeClipCommand(this, m_startPos, QPointF(m_dragItem->endPos(), m_dragItem->track()), false, false);
591
592     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()));
593     m_commandStack->push(command);
594     m_document->renderer()->doRefresh();
595   }
596   m_operationMode = NONE;
597   m_dragItem = NULL; 
598 }
599
600 void CustomTrackView::deleteClip (int track, int startpos, const QRectF &rect )
601 {
602   ClipItem *item = getClipItemAt(startpos, track);
603   if (!item) {
604     kDebug()<<"----------------  ERROR, CANNOT find clip to move at: "<<rect.x();
605     return;
606   }
607   item->baseClip()->removeReference();
608   m_document->updateClip(item->baseClip()->getId());
609   delete item;
610   m_document->renderer()->mltRemoveClip(m_tracksCount - track, GenTime(startpos, m_document->fps()));
611   m_document->renderer()->doRefresh();
612 }
613
614 void CustomTrackView::addClip ( QDomElement xml, int clipId, int track, int startpos, const QRectF &rect, int duration )
615 {
616   QRect r(startpos * m_scale, 50 * track, duration * m_scale, 49); 
617   DocClipBase *baseclip = m_document->clipManager()->getClipById(clipId);
618   ClipItem *item = new ClipItem(baseclip, track, startpos, r, duration);
619   scene()->addItem(item);
620   baseclip->addReference();
621   m_document->updateClip(baseclip->getId());
622   m_document->renderer()->mltInsertClip(m_tracksCount - track, GenTime(startpos, m_document->fps()), xml);
623   m_document->renderer()->doRefresh();
624 }
625
626 ClipItem *CustomTrackView::getClipItemAt(int pos, int track)
627 {
628   return (ClipItem *) scene()->itemAt(pos * m_scale, track * 50 + 25);
629 }
630
631 void CustomTrackView::moveClip ( const QPointF &startPos, const QPointF &endPos )
632 {
633   ClipItem *item = getClipItemAt(startPos.x() + 1, startPos.y());
634   if (!item) {
635     kDebug()<<"----------------  ERROR, CANNOT find clip to move at: "<<startPos.x() * m_scale * FRAME_SIZE + 1<<", "<<startPos.y() * 50 + 25;
636     return;
637   }
638   kDebug()<<"----------------  Move CLIP FROM: "<<startPos.x()<<", END:"<<endPos.x();
639   item->moveTo(endPos.x(), m_scale, (endPos.y() - startPos.y()) * 50, endPos.y());
640   m_document->renderer()->mltMoveClip(m_tracksCount - startPos.y(), m_tracksCount - endPos.y(), startPos.x(), endPos.x());
641 }
642
643 void CustomTrackView::resizeClip ( const QPointF &startPos, const QPointF &endPos, bool resizeClipStart )
644 {
645   int offset;
646   if (resizeClipStart) offset = 1;
647   else offset = -1;
648   ClipItem *item = getClipItemAt(startPos.x() + offset, startPos.y());
649   if (!item) {
650     kDebug()<<"----------------  ERROR, CANNOT find clip to resize at: "<<startPos;
651     return;
652   }
653   qreal diff = endPos.x() - startPos.x();
654   if (resizeClipStart) {
655     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()));
656     item->resizeStart(endPos.x(), m_scale);
657   }
658   else {
659     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()));
660     item->resizeEnd(endPos.x(), m_scale);
661   }
662   m_document->renderer()->doRefresh();
663 }
664
665 double CustomTrackView::getSnapPointForPos(double pos)
666 {
667   for (int i = 0; i < m_snapPoints.size(); ++i) {
668     //kDebug()<<"SNAP POINT: "<<m_snapPoints.at(i);
669     if (abs(pos - m_snapPoints.at(i) * m_scale) < 6 * m_scale) {
670       //kDebug()<<" FOUND SNAP POINT AT: "<<m_snapPoints.at(i)<<", current pos: "<<pos / m_scale;
671       return m_snapPoints.at(i) * m_scale + 0.5;
672     }
673     if (m_snapPoints.at(i) > pos) break;
674   }
675   return pos;
676 }
677
678 void CustomTrackView::updateSnapPoints(ClipItem *selected)
679 {
680   m_snapPoints.clear();
681   int offset = 0;
682   if (selected) offset = selected->duration();
683   QList<QGraphicsItem *> itemList = items();
684   for (int i = 0; i < itemList.count(); i++) {
685     if (itemList.at(i)->type() == 70000 && itemList.at(i) != selected) {
686       ClipItem *item = (ClipItem *)itemList.at(i);
687       int start = item->startPos();
688       int fadein = item->fadeIn() + start;
689       int end = item->endPos();
690       int fadeout = end - item->fadeOut();
691       m_snapPoints.append(start);
692       if (fadein != start) m_snapPoints.append(fadein);
693       m_snapPoints.append(end);
694       if (fadeout != end) m_snapPoints.append(fadeout);
695       if (offset != 0) {
696         m_snapPoints.append(start - offset);
697         if (fadein != start) m_snapPoints.append(fadein - offset);
698         m_snapPoints.append(end - offset);
699         if (fadeout != end) m_snapPoints.append(fadeout - offset);
700       }
701     }
702   }
703   kDebug()<<" GOT SNAPPOINTS TOTAL: "<<m_snapPoints.count();
704   qSort(m_snapPoints);
705   for (int i = 0; i < m_snapPoints.size(); ++i)
706     kDebug()<<"SNAP POINT: "<<m_snapPoints.at(i);
707 }
708
709
710 void CustomTrackView::setScale(double scaleFactor)
711 {
712   //scale(scaleFactor, scaleFactor);
713   m_scale = scaleFactor;
714   kDebug()<<" HHHHHHHH  SCALING: "<<m_scale;
715   QList<QGraphicsItem *> itemList = items();
716   scene()->setSceneRect(0, 0, (m_projectDuration + 500) * m_scale, scene()->sceneRect().height()); //50 *
717
718   for (int i = 0; i < itemList.count(); i++) {
719       if (itemList.at(i)->type() == 70000) {
720         ClipItem *clip = (ClipItem *)itemList.at(i);
721         clip->setRect(clip->startPos() * m_scale, clip->rect().y(), clip->duration() * m_scale, clip->rect().height());
722       }
723       /*else if (itemList.at(i)->type() == 70001) {
724         LabelItem *label = (LabelItem *)itemList.at(i);
725         QGraphicsItem *parent = label->parentItem();
726         QRectF r = label->boundingRect();
727         QRectF p = parent->boundingRect();
728         label->setPos(p.x() + p.width() / 2 - r.width() / 2, p.y() + p.height() / 2 - r.height() / 2);
729         //label->setRect(clip->startPos() * m_scale, clip->rect().y(), clip->duration() * m_scale, clip->rect().height());
730       }*/
731     }
732 }
733
734 void CustomTrackView::drawBackground ( QPainter * painter, const QRectF & rect )  
735 {
736   QRect rectInView;//this is the rect that is visible by the user
737   if (scene()->views().size()>0){ 
738     rectInView=scene()->views()[0]->viewport()->rect();
739     rectInView.moveTo(scene()->views()[0]->horizontalScrollBar()->value(),scene()->views()[0]->verticalScrollBar()->value());
740   }
741   if (rectInView.isNull()) return;
742
743   QColor base = palette().button().color();
744   painter->setClipRect(rect);
745   painter->drawLine(rectInView.left(), 0, rectInView.right(), 0);
746   for (uint i = 0; i < m_tracksCount;i++)
747   {
748     painter->drawLine(rectInView.left(), 50 * (i+1), rectInView.right(), 50 * (i+1));
749     painter->drawText(QRectF(10, 50 * i, 100, 50 * i + 49), Qt::AlignLeft, i18n(" Track ") + QString::number(i + 1));
750   }
751   int lowerLimit = 50 * m_tracksCount + 1;
752   if (height() > lowerLimit)
753   painter->fillRect(QRectF(rectInView.left(), lowerLimit, rectInView.width(), height() - lowerLimit), QBrush(base));
754 }
755 /*
756 void CustomTrackView::drawForeground ( QPainter * painter, const QRectF & rect )  
757 {
758   //kDebug()<<"/////  DRAWING FB: "<<rect.x()<<", width: "<<rect.width();
759   painter->fillRect(rect, QColor(50, rand() % 250,50,100));
760   painter->drawLine(m_cursorPos, rect.y(), m_cursorPos, rect.y() + rect.height());
761 }
762 */
763 #include "customtrackview.moc"