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