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