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