1 /***************************************************************************
2 * Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org) *
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. *
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. *
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 ***************************************************************************/
20 #include <QMouseEvent>
21 #include <QStylePainter>
22 #include <QGraphicsItem>
23 #include <QDomDocument>
30 #include "customtrackview.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"
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)
42 if (doc) m_commandStack = doc->commandStack();
43 else m_commandStack == NULL;
44 setMouseTracking(true);
46 m_animationTimer = new QTimeLine(800);
47 m_animationTimer->setFrameRange(0, 5);
48 m_animationTimer->setUpdateInterval(100);
49 m_animationTimer->setLoopCount(0);
50 m_tipColor = QColor(0, 192, 0, 200);
51 QColor border = QColor(255,255,255,100);
52 m_tipPen.setColor(border);
55 setContentsMargins(0, 0, 0, 0);
57 m_cursorLine = projectscene->addLine(0, 0, 0, 50);
58 m_cursorLine->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIgnoresTransformations);
59 m_cursorLine->setZValue(1000);
63 void CustomTrackView::initView()
69 void CustomTrackView::resizeEvent ( QResizeEvent * event )
71 QGraphicsView::resizeEvent(event);
75 void CustomTrackView::wheelEvent ( QWheelEvent * e )
77 if (e->modifiers() == Qt::ControlModifier) {
78 if (e->delta() > 0) emit zoomIn();
82 if (e->delta() > 0) horizontalScrollBar()->setValue (horizontalScrollBar()->value() + horizontalScrollBar()->singleStep ());
83 else horizontalScrollBar()->setValue (horizontalScrollBar()->value() - horizontalScrollBar()->singleStep ());
89 void CustomTrackView::mouseMoveEvent ( QMouseEvent * event )
92 emit mousePosition(mapToScene(event->pos()).x() / m_scale);
93 /*if (event->modifiers() == Qt::ControlModifier)
94 setDragMode(QGraphicsView::ScrollHandDrag);
95 else if (event->modifiers() == Qt::ShiftModifier)
96 setDragMode(QGraphicsView::RubberBandDrag);
99 if (m_dragItem) { //event->button() == Qt::LeftButton) {
100 // a button was pressed, delete visual tips
101 if (m_operationMode == MOVE) {
102 double snappedPos = getSnapPointForPos(mapToScene(event->pos()).x() - m_clickPoint);
103 double moveX = snappedPos; //mapToScene(event->pos()).x();
104 //kDebug()<<"/////// MOVE CLIP, EVENT Y: "<<event->scenePos().y()<<", SCENE HEIGHT: "<<scene()->sceneRect().height();
105 int moveTrack = (int) mapToScene(event->pos()).y() / 50;
106 int currentTrack = m_dragItem->track();
108 if (moveTrack > m_tracksCount - 1) moveTrack = m_tracksCount - 1;
109 else if (moveTrack < 0) moveTrack = 0;
111 int offset = moveTrack - currentTrack;
112 if (offset != 0) offset = 50 * offset;
113 m_dragItem->moveTo(moveX / m_scale, m_scale, offset, moveTrack);
115 else if (m_operationMode == RESIZESTART) {
116 int pos = mapToScene(event->pos()).x();
117 m_dragItem->resizeStart(pos / m_scale, m_scale);
119 else if (m_operationMode == RESIZEEND) {
120 int pos = mapToScene(event->pos()).x();
121 m_dragItem->resizeEnd(pos / m_scale, m_scale);
123 else if (m_operationMode == FADEIN) {
124 int pos = mapToScene(event->pos()).x() / m_scale;
125 m_dragItem->setFadeIn(pos - m_dragItem->startPos(), m_scale);
127 else if (m_operationMode == FADEOUT) {
128 int pos = mapToScene(event->pos()).x() / m_scale;
129 m_dragItem->setFadeOut(m_dragItem->endPos() - pos, m_scale);
132 if (m_animation) delete m_animation;
134 if (m_visualTip) delete m_visualTip;
136 QGraphicsView::mouseMoveEvent(event);
140 QList<QGraphicsItem *> itemList = items( event->pos());
142 QGraphicsItem *item = NULL;
143 for (int i = 0; i < itemList.count(); i++) {
144 if (itemList.at(i)->type() == 70000) {
145 item = itemList.at(i);
149 if (item && event->buttons() == Qt::NoButton) {
150 ClipItem *clip = (ClipItem*) item;
152 OPERATIONTYPE opMode = clip->operationMode(mapToScene(event->pos()), m_scale);
153 if (opMode == m_moveOpMode) {
154 QGraphicsView::mouseMoveEvent(event);
159 if (m_animation) delete m_animation;
161 m_animationTimer->stop();
166 m_moveOpMode = opMode;
167 if (opMode == MOVE) {
168 setCursor(Qt::OpenHandCursor);
170 else if (opMode == RESIZESTART) {
171 kDebug()<<"******** RESIZE CLIP START; WIDTH: "<<size;
172 if (m_visualTip == NULL) {
174 polygon << QPoint(clip->rect().x(), clip->rect().y() + clip->rect().height() / 2 - size * 2);
175 polygon << QPoint(clip->rect().x() + size * 2, clip->rect().y() + clip->rect().height() / 2 );
176 polygon << QPoint(clip->rect().x(), clip->rect().y() + clip->rect().height() / 2 + size * 2);
177 polygon << QPoint(clip->rect().x(), clip->rect().y() + clip->rect().height() / 2 - size * 2);
179 m_visualTip = new QGraphicsPolygonItem(polygon);
180 ((QGraphicsPolygonItem*) m_visualTip)->setBrush(m_tipColor);
181 ((QGraphicsPolygonItem*) m_visualTip)->setPen(m_tipPen);
182 m_visualTip->setZValue (100);
183 m_animation = new QGraphicsItemAnimation;
184 m_animation->setItem(m_visualTip);
185 m_animation->setTimeLine(m_animationTimer);
186 m_visualTip->setPos(0, 0);
188 m_animation->setScaleAt(.5, scale, 1);
189 m_animation->setPosAt(.5, QPointF(clip->rect().x() - clip->rect().x() * scale, 0));
191 m_animation->setScaleAt(1, scale, 1);
192 m_animation->setPosAt(1, QPointF(clip->rect().x() - clip->rect().x() * scale, 0));
193 scene()->addItem(m_visualTip);
194 m_animationTimer->start();
196 setCursor(Qt::SizeHorCursor);
198 else if (opMode == RESIZEEND) {
199 if (m_visualTip == NULL) {
201 polygon << QPoint(clip->rect().x() + clip->rect().width(), clip->rect().y() + clip->rect().height() / 2 - size * 2);
202 polygon << QPoint(clip->rect().x() + clip->rect().width() - size * 2, clip->rect().y() + clip->rect().height() / 2 );
203 polygon << QPoint(clip->rect().x() + clip->rect().width(), clip->rect().y() + clip->rect().height() / 2 + size * 2);
204 polygon << QPoint(clip->rect().x() + clip->rect().width(), clip->rect().y() + clip->rect().height() / 2 - size * 2);
206 m_visualTip = new QGraphicsPolygonItem(polygon);
207 ((QGraphicsPolygonItem*) m_visualTip)->setBrush(m_tipColor);
208 ((QGraphicsPolygonItem*) m_visualTip)->setPen(m_tipPen);
210 m_visualTip->setZValue (100);
211 m_animation = new QGraphicsItemAnimation;
212 m_animation->setItem(m_visualTip);
213 m_animation->setTimeLine(m_animationTimer);
214 m_visualTip->setPos(0, 0);
216 m_animation->setScaleAt(.5, scale, 1);
217 m_animation->setPosAt(.5, QPointF(clip->rect().x() - clip->rect().x() * scale - clip->rect().width(), 0));
219 m_animation->setScaleAt(1, scale, 1);
220 m_animation->setPosAt(1, QPointF(clip->rect().x() - clip->rect().x() * scale, 0));
221 scene()->addItem(m_visualTip);
222 m_animationTimer->start();
224 setCursor(Qt::SizeHorCursor);
226 else if (opMode == FADEIN) {
227 if (m_visualTip == NULL) {
228 m_visualTip = new QGraphicsEllipseItem(clip->rect().x() + clip->fadeIn() * m_scale - size, clip->rect().y() - 8, size * 2, 16);
229 ((QGraphicsEllipseItem*) m_visualTip)->setBrush(m_tipColor);
230 ((QGraphicsEllipseItem*) m_visualTip)->setPen(m_tipPen);
231 m_visualTip->setZValue (100);
232 m_animation = new QGraphicsItemAnimation;
233 m_animation->setItem(m_visualTip);
234 m_animation->setTimeLine(m_animationTimer);
235 m_visualTip->setPos(0, 0);
237 m_animation->setScaleAt(.5, scale, scale);
238 m_animation->setPosAt(.5, QPointF(clip->rect().x() - clip->rect().x() * scale - clip->fadeIn() * m_scale, clip->rect().y() - clip->rect().y() * scale));
240 m_animation->setScaleAt(1, scale, scale);
241 m_animation->setPosAt(1, QPointF(clip->rect().x() - clip->rect().x() * scale, clip->rect().y() - clip->rect().y() * scale));
242 scene()->addItem(m_visualTip);
243 m_animationTimer->start();
245 setCursor(Qt::PointingHandCursor);
247 else if (opMode == FADEOUT) {
248 if (m_visualTip == NULL) {
249 m_visualTip = new QGraphicsEllipseItem(clip->rect().x() + clip->rect().width() - clip->fadeOut() * m_scale - size, clip->rect().y() - 8, size*2, 16);
250 ((QGraphicsEllipseItem*) m_visualTip)->setBrush(m_tipColor);
251 ((QGraphicsEllipseItem*) m_visualTip)->setPen(m_tipPen);
252 m_visualTip->setZValue (100);
253 m_animation = new QGraphicsItemAnimation;
254 m_animation->setItem(m_visualTip);
255 m_animation->setTimeLine(m_animationTimer);
256 m_visualTip->setPos(0, 0);
258 m_animation->setScaleAt(.5, scale, scale);
259 m_animation->setPosAt(.5, QPointF(clip->rect().x() - clip->rect().x() * scale - clip->rect().width() + clip->fadeOut() * m_scale, clip->rect().y() - clip->rect().y() * scale));
261 m_animation->setScaleAt(1, scale, scale);
262 m_animation->setPosAt(1, QPointF(clip->rect().x() - clip->rect().x() * scale, clip->rect().y() - clip->rect().y() * scale));
263 scene()->addItem(m_visualTip);
264 m_animationTimer->start();
266 setCursor(Qt::PointingHandCursor);
271 if (event->buttons() != Qt::NoButton)
273 setCursorPos((int) mapToScene(event->pos().x(), 0).x());
274 emit cursorMoved(cursorPos());
277 if (m_animation) delete m_animation;
278 m_animationTimer->stop();
284 setCursor(Qt::ArrowCursor);
287 QGraphicsView::mouseMoveEvent(event);
291 void CustomTrackView::mousePressEvent ( QMouseEvent * event )
293 int pos = event->x();
294 if (event->modifiers() == Qt::ControlModifier) {
295 setDragMode(QGraphicsView::ScrollHandDrag);
296 QGraphicsView::mousePressEvent(event);
299 else if (event->modifiers() == Qt::ShiftModifier) {
300 setDragMode(QGraphicsView::RubberBandDrag);
301 QGraphicsView::mousePressEvent(event);
305 bool collision = false;
306 QList<QGraphicsItem *> collisionList = items(event->pos());
307 for (int i = 0; i < collisionList.size(); ++i) {
308 QGraphicsItem *item = collisionList.at(i);
309 if (item->type() == 70000) {
311 if (!item->isSelected()) {
312 QList<QGraphicsItem *> itemList = items();
313 for (int i = 0; i < itemList.count(); i++)
314 itemList.at(i)->setSelected(false);
315 item->setSelected(true);
318 m_dragItem = (ClipItem *) item;
319 emit clipItemSelected(m_dragItem);
320 m_clickPoint = mapToScene(event->pos()).x() - m_dragItem->startPos() * m_scale;
321 m_operationMode = m_dragItem->operationMode(item->mapFromScene(mapToScene(event->pos())), m_scale);
322 if (m_operationMode == MOVE || m_operationMode == RESIZESTART)
323 m_startPos = QPointF(m_dragItem->startPos(), m_dragItem->track());
324 else if (m_operationMode == RESIZEEND)
325 m_startPos = QPointF(m_dragItem->endPos(), m_dragItem->track());
326 kDebug()<<"//////// ITEM CLICKED: "<<m_startPos;
332 kDebug()<<"//////// NO ITEM FOUND ON CLICK";
334 setCursor(Qt::ArrowCursor);
335 QList<QGraphicsItem *> itemList = items();
336 for (int i = 0; i < itemList.count(); i++)
337 itemList.at(i)->setSelected(false);
338 emit clipItemSelected(NULL);
339 setCursorPos((int) mapToScene(event->x(), 0).x());
340 emit cursorMoved(cursorPos());
343 updateSnapPoints(m_dragItem);
345 //QGraphicsView::mousePressEvent(event);
348 void CustomTrackView::dragEnterEvent( QDragEnterEvent * event )
350 if (event->mimeData()->hasFormat("kdenlive/producerslist")) {
351 kDebug()<<"/////////////// DRAG ENTERED, TEXT: "<<event->mimeData()->data("kdenlive/producerslist");
352 QStringList ids = QString(event->mimeData()->data("kdenlive/producerslist")).split(";");
353 //TODO: drop of several clips
354 for (int i = 0; i < ids.size(); ++i) {
356 DocClipBase *clip = m_document->getBaseClip(ids.at(0).toInt());
357 if (clip == NULL) kDebug()<<" WARNING))))))))) CLIP NOT FOUND : "<<ids.at(0).toInt();
358 addItem(clip, event->pos());
359 event->acceptProposedAction();
361 else QGraphicsView::dragEnterEvent(event);
364 void CustomTrackView::slotRefreshEffects(ClipItem *clip)
366 int track = m_tracksCount - clip->track();
367 GenTime pos = GenTime(clip->startPos(), m_document->fps());
368 m_document->renderer()->mltRemoveEffect(track, pos, "-1", false);
369 for (int i = 0; i < clip->effectsCount(); i++) {
370 m_document->renderer()->mltAddEffect(track, pos, clip->getEffectArgs(clip->effectAt(i)), false);
372 m_document->renderer()->doRefresh();
375 void CustomTrackView::addEffect(int track, GenTime pos, QDomElement effect)
377 ClipItem *clip = getClipItemAt(pos.frames(m_document->fps()) + 1, m_tracksCount - track);
379 QMap <QString, QString> effectParams = clip->addEffect(effect);
380 m_document->renderer()->mltAddEffect(track, pos, effectParams);
381 emit clipItemSelected(clip);
385 void CustomTrackView::deleteEffect(int track, GenTime pos, QDomElement effect)
387 QString index = effect.attribute("kdenlive_ix");
388 m_document->renderer()->mltRemoveEffect(track, pos, index);
389 ClipItem *clip = getClipItemAt(pos.frames(m_document->fps()) + 1, m_tracksCount - track);
391 clip->deleteEffect(index);
392 emit clipItemSelected(clip);
396 void CustomTrackView::slotAddEffect(QDomElement effect, GenTime pos, int track)
398 QList<QGraphicsItem *> itemList;
402 ClipItem *clip = getClipItemAt(pos.frames(m_document->fps()) + 1, track);
403 if (clip) itemList.append(clip);
404 else kDebug()<<"------ wrning, clip eff not found";
406 kDebug()<<"// REQUESTING EFFECT ON CLIP: "<<pos.frames(25)<<", TRK: "<<track;
407 for (int i = 0; i < itemList.count(); i++) {
408 if (itemList.at(i)->type() == 70000 && (itemList.at(i)->isSelected() || track != -1)) {
409 ClipItem *item = (ClipItem *)itemList.at(i);
410 // the kdenlive_ix int is used to identify an effect in mlt's playlist, should
412 if (effect.attribute("kdenlive_ix").toInt() == 0)
413 effect.setAttribute("kdenlive_ix", QString::number(item->effectsCounter()));
414 AddEffectCommand *command = new AddEffectCommand(this, m_tracksCount - item->track(),GenTime(item->startPos(), m_document->fps()), effect, true);
415 m_commandStack->push(command);
420 void CustomTrackView::slotDeleteEffect(ClipItem *clip, QDomElement effect)
422 AddEffectCommand *command = new AddEffectCommand(this, m_tracksCount - clip->track(), GenTime(clip->startPos(), m_document->fps()), effect, false);
423 m_commandStack->push(command);
426 void CustomTrackView::updateEffect(int track, GenTime pos, QDomElement effect)
428 ClipItem *clip = getClipItemAt(pos.frames(m_document->fps()) + 1, m_tracksCount - track);
430 QMap <QString, QString> effectParams = clip->getEffectArgs(effect);
431 if (effectParams["disabled"] == "1") {
432 QString index = effectParams["kdenlive_ix"];
433 m_document->renderer()->mltRemoveEffect(track, pos, index);
435 else m_document->renderer()->mltEditEffect(m_tracksCount - clip->track(), GenTime(clip->startPos(), m_document->fps()), effectParams);
439 void CustomTrackView::slotChangeEffectState(ClipItem *clip, QDomElement effect, bool disable)
441 QDomElement oldEffect = effect.cloneNode().toElement();
442 effect.setAttribute("disabled", disable);
443 EditEffectCommand *command = new EditEffectCommand(this, m_tracksCount - clip->track(), GenTime(clip->startPos(), m_document->fps()), oldEffect, effect, true);
444 m_commandStack->push(command);
447 void CustomTrackView::slotUpdateClipEffect(ClipItem *clip, QDomElement oldeffect, QDomElement effect)
449 EditEffectCommand *command = new EditEffectCommand(this, m_tracksCount - clip->track(), GenTime(clip->startPos(), m_document->fps()), oldeffect, effect, true);
450 m_commandStack->push(command);
454 void CustomTrackView::addItem(DocClipBase *clip, QPoint pos)
457 int out = clip->duration().frames(m_document->fps());
458 //kdDebug()<<"- - - -CREATING CLIP, duration = "<<out<<", URL: "<<clip->fileURL();
459 int trackTop = ((int) mapToScene(pos).y()/50) * 50 + 1;
460 m_dropItem = new ClipItem(clip, ((int) mapToScene(pos).y()/50), in, QRectF(mapToScene(pos).x() * m_scale, trackTop, out * m_scale, 49), out);
461 scene()->addItem(m_dropItem);
465 void CustomTrackView::dragMoveEvent(QDragMoveEvent * event) {
466 event->setDropAction(Qt::IgnoreAction);
467 //kDebug()<<"+++++++++++++ DRAG MOVE, : "<<mapToScene(event->pos()).x()<<", SCAL: "<<m_scale;
469 int track = (int) mapToScene(event->pos()).y()/50; //) * (m_scale * 50) + m_scale;
470 m_dropItem->moveTo(mapToScene(event->pos()).x() / m_scale, m_scale, (track - m_dropItem->track()) * 50, track);
471 event->setDropAction(Qt::MoveAction);
472 if (event->mimeData()->hasFormat("kdenlive/producerslist")) {
473 event->acceptProposedAction();
477 QGraphicsView::dragMoveEvent(event);
481 void CustomTrackView::dragLeaveEvent ( QDragLeaveEvent * event ) {
486 else QGraphicsView::dragLeaveEvent(event);
489 void CustomTrackView::dropEvent ( QDropEvent * event ) {
491 AddTimelineClipCommand *command = new AddTimelineClipCommand(this, m_dropItem->xml(), m_dropItem->clipProducer(), m_dropItem->track(), m_dropItem->startPos(), m_dropItem->rect(), m_dropItem->duration(), false, false);
492 m_commandStack->push(command);
493 m_dropItem->baseClip()->addReference();
494 m_document->updateClip(m_dropItem->baseClip()->getId());
495 // kDebug()<<"IIIIIIIIIIIIIIIIIIIIIIII TRAX CNT: "<<m_tracksCount<<", DROP: "<<m_dropItem->track();
496 m_document->renderer()->mltInsertClip(m_tracksCount - m_dropItem->track(), GenTime(m_dropItem->startPos(), m_document->fps()), m_dropItem->xml());
498 else QGraphicsView::dropEvent(event);
503 QStringList CustomTrackView::mimeTypes () const
505 QStringList qstrList;
506 // list of accepted mime types for drop
507 qstrList.append("text/plain");
508 qstrList.append("kdenlive/producerslist");
512 Qt::DropActions CustomTrackView::supportedDropActions () const
514 // returns what actions are supported when dropping
515 return Qt::MoveAction;
518 void CustomTrackView::setDuration(int duration)
520 kDebug()<<"///////////// PRO DUR: "<<duration<<", height: "<<50 * m_tracksCount;
521 m_projectDuration = duration;
522 scene()->setSceneRect(0, 0, (m_projectDuration + 500) * m_scale, scene()->sceneRect().height()); //50 * m_tracksCount);
526 void CustomTrackView::addTrack ()
529 m_cursorLine->setLine(m_cursorLine->line().x1(), 0, m_cursorLine->line().x1(), 50 * m_tracksCount);
530 //setSceneRect(0, 0, sceneRect().width(), 50 * m_tracksCount);
531 //verticalScrollBar()->setMaximum(50 * m_tracksCount);
532 //setFixedHeight(50 * m_tracksCount);
535 void CustomTrackView::removeTrack ()
538 m_cursorLine->setLine(m_cursorLine->line().x1(), 0, m_cursorLine->line().x1(), 50 * m_tracksCount);
541 void CustomTrackView::deleteClip(int clipId)
543 QList<QGraphicsItem *> itemList = items();
544 for (int i = 0; i < itemList.count(); i++) {
545 if (itemList.at(i)->type() == 70000) {
546 ClipItem *item = (ClipItem *)itemList.at(i);
547 if (item->clipProducer() == clipId) {
548 AddTimelineClipCommand *command = new AddTimelineClipCommand(this, item->xml(), item->clipProducer(), item->track(), item->startPos(), item->rect(), item->duration(), true, true);
549 m_commandStack->push(command);
556 void CustomTrackView::setCursorPos(int pos, bool seek)
559 m_cursorLine->setPos(pos, 0);
560 int frame = pos / m_scale;
561 if (seek) m_document->renderer()->seek(GenTime(frame, m_document->fps()));
564 int CustomTrackView::cursorPos()
569 void CustomTrackView::mouseReleaseEvent ( QMouseEvent * event )
571 QGraphicsView::mouseReleaseEvent(event);
572 setDragMode(QGraphicsView::NoDrag);
573 if (m_dragItem == NULL) return;
574 //kDebug()<<"/// MOVING CLIP: "<<m_startPos<<", END: "<<QPoint(m_dragItem->rect().x(),m_dragItem->rect().y());
575 if (m_operationMode == MOVE && m_startPos.x() != m_dragItem->startPos()) {
577 MoveClipCommand *command = new MoveClipCommand(this, m_startPos, QPointF(m_dragItem->startPos(), m_dragItem->track()), false);
578 m_commandStack->push(command);
579 m_document->renderer()->mltMoveClip(m_tracksCount - m_startPos.y(), m_tracksCount - m_dragItem->track(), m_startPos.x(), m_dragItem->startPos());
581 else if (m_operationMode == RESIZESTART) {
583 ResizeClipCommand *command = new ResizeClipCommand(this, m_startPos, QPointF(m_dragItem->startPos(), m_dragItem->track()), true, false);
585 m_document->renderer()->mltResizeClipStart(m_tracksCount - m_dragItem->track(), GenTime(m_dragItem->endPos(), m_document->fps()), GenTime(m_dragItem->startPos(), m_document->fps()), GenTime(m_startPos.x(), m_document->fps()), GenTime(m_dragItem->cropStart(), m_document->fps()), GenTime(m_dragItem->cropStart(), m_document->fps()) + GenTime(m_dragItem->endPos(), m_document->fps()) - GenTime(m_dragItem->startPos(), m_document->fps()));
586 m_commandStack->push(command);
587 m_document->renderer()->doRefresh();
589 else if (m_operationMode == RESIZEEND) {
591 ResizeClipCommand *command = new ResizeClipCommand(this, m_startPos, QPointF(m_dragItem->endPos(), m_dragItem->track()), false, false);
593 m_document->renderer()->mltResizeClipEnd(m_tracksCount - m_dragItem->track(), GenTime(m_dragItem->startPos(), m_document->fps()), GenTime(m_dragItem->cropStart(), m_document->fps()), GenTime(m_dragItem->cropStart(), m_document->fps()) + GenTime(m_dragItem->endPos(), m_document->fps()) - GenTime(m_dragItem->startPos(), m_document->fps()));
594 m_commandStack->push(command);
595 m_document->renderer()->doRefresh();
597 m_operationMode = NONE;
601 void CustomTrackView::deleteClip (int track, int startpos, const QRectF &rect )
603 ClipItem *item = getClipItemAt(startpos, track);
605 kDebug()<<"---------------- ERROR, CANNOT find clip to move at: "<<rect.x();
608 item->baseClip()->removeReference();
609 m_document->updateClip(item->baseClip()->getId());
611 m_document->renderer()->mltRemoveClip(m_tracksCount - track, GenTime(startpos, m_document->fps()));
612 m_document->renderer()->doRefresh();
615 void CustomTrackView::addClip ( QDomElement xml, int clipId, int track, int startpos, const QRectF &rect, int duration )
617 QRect r(startpos * m_scale, 50 * track, duration * m_scale, 49);
618 DocClipBase *baseclip = m_document->clipManager()->getClipById(clipId);
619 ClipItem *item = new ClipItem(baseclip, track, startpos, r, duration);
620 scene()->addItem(item);
621 baseclip->addReference();
622 m_document->updateClip(baseclip->getId());
623 m_document->renderer()->mltInsertClip(m_tracksCount - track, GenTime(startpos, m_document->fps()), xml);
624 m_document->renderer()->doRefresh();
627 ClipItem *CustomTrackView::getClipItemAt(int pos, int track)
629 return (ClipItem *) scene()->itemAt(pos * m_scale, track * 50 + 25);
632 void CustomTrackView::moveClip ( const QPointF &startPos, const QPointF &endPos )
634 ClipItem *item = getClipItemAt(startPos.x() + 1, startPos.y());
636 kDebug()<<"---------------- ERROR, CANNOT find clip to move at: "<<startPos.x() * m_scale * FRAME_SIZE + 1<<", "<<startPos.y() * 50 + 25;
639 kDebug()<<"---------------- Move CLIP FROM: "<<startPos.x()<<", END:"<<endPos.x();
640 item->moveTo(endPos.x(), m_scale, (endPos.y() - startPos.y()) * 50, endPos.y());
641 m_document->renderer()->mltMoveClip(m_tracksCount - startPos.y(), m_tracksCount - endPos.y(), startPos.x(), endPos.x());
644 void CustomTrackView::resizeClip ( const QPointF &startPos, const QPointF &endPos, bool resizeClipStart )
647 if (resizeClipStart) offset = 1;
649 ClipItem *item = getClipItemAt(startPos.x() + offset, startPos.y());
651 kDebug()<<"---------------- ERROR, CANNOT find clip to resize at: "<<startPos;
654 qreal diff = endPos.x() - startPos.x();
655 if (resizeClipStart) {
656 m_document->renderer()->mltResizeClipStart(m_tracksCount - item->track(), GenTime(item->endPos(), m_document->fps()), GenTime(endPos.x(), m_document->fps()), GenTime(item->startPos(), m_document->fps()), GenTime(item->cropStart() + diff, m_document->fps()), GenTime(item->cropStart() + diff, m_document->fps()) + GenTime(item->endPos(), m_document->fps()) - GenTime(endPos.x(), m_document->fps()));
657 item->resizeStart(endPos.x(), m_scale);
660 m_document->renderer()->mltResizeClipEnd(m_tracksCount - item->track(), GenTime(item->startPos(), m_document->fps()), GenTime(item->cropStart(), m_document->fps()), GenTime(item->cropStart(), m_document->fps()) + GenTime(endPos.x(), m_document->fps()) - GenTime(item->startPos(), m_document->fps()));
661 item->resizeEnd(endPos.x(), m_scale);
663 m_document->renderer()->doRefresh();
666 double CustomTrackView::getSnapPointForPos(double pos)
668 for (int i = 0; i < m_snapPoints.size(); ++i) {
669 //kDebug()<<"SNAP POINT: "<<m_snapPoints.at(i);
670 if (abs(pos - m_snapPoints.at(i) * m_scale) < 6 * m_scale) {
671 //kDebug()<<" FOUND SNAP POINT AT: "<<m_snapPoints.at(i)<<", current pos: "<<pos / m_scale;
672 return m_snapPoints.at(i) * m_scale + 0.5;
674 if (m_snapPoints.at(i) > pos) break;
679 void CustomTrackView::updateSnapPoints(ClipItem *selected)
681 m_snapPoints.clear();
683 if (selected) offset = selected->duration();
684 QList<QGraphicsItem *> itemList = items();
685 for (int i = 0; i < itemList.count(); i++) {
686 if (itemList.at(i)->type() == 70000 && itemList.at(i) != selected) {
687 ClipItem *item = (ClipItem *)itemList.at(i);
688 int start = item->startPos();
689 int fadein = item->fadeIn() + start;
690 int end = item->endPos();
691 int fadeout = end - item->fadeOut();
692 m_snapPoints.append(start);
693 if (fadein != start) m_snapPoints.append(fadein);
694 m_snapPoints.append(end);
695 if (fadeout != end) m_snapPoints.append(fadeout);
697 m_snapPoints.append(start - offset);
698 if (fadein != start) m_snapPoints.append(fadein - offset);
699 m_snapPoints.append(end - offset);
700 if (fadeout != end) m_snapPoints.append(fadeout - offset);
704 kDebug()<<" GOT SNAPPOINTS TOTAL: "<<m_snapPoints.count();
706 for (int i = 0; i < m_snapPoints.size(); ++i)
707 kDebug()<<"SNAP POINT: "<<m_snapPoints.at(i);
711 void CustomTrackView::setScale(double scaleFactor)
713 //scale(scaleFactor, scaleFactor);
714 m_scale = scaleFactor;
715 kDebug()<<" HHHHHHHH SCALING: "<<m_scale;
716 QList<QGraphicsItem *> itemList = items();
717 scene()->setSceneRect(0, 0, (m_projectDuration + 500) * m_scale, scene()->sceneRect().height()); //50 *
719 for (int i = 0; i < itemList.count(); i++) {
720 if (itemList.at(i)->type() == 70000) {
721 ClipItem *clip = (ClipItem *)itemList.at(i);
722 clip->setRect(clip->startPos() * m_scale, clip->rect().y(), clip->duration() * m_scale, clip->rect().height());
724 /*else if (itemList.at(i)->type() == 70001) {
725 LabelItem *label = (LabelItem *)itemList.at(i);
726 QGraphicsItem *parent = label->parentItem();
727 QRectF r = label->boundingRect();
728 QRectF p = parent->boundingRect();
729 label->setPos(p.x() + p.width() / 2 - r.width() / 2, p.y() + p.height() / 2 - r.height() / 2);
730 //label->setRect(clip->startPos() * m_scale, clip->rect().y(), clip->duration() * m_scale, clip->rect().height());
735 void CustomTrackView::drawBackground ( QPainter * painter, const QRectF & rect )
737 QRect rectInView;//this is the rect that is visible by the user
738 if (scene()->views().size()>0){
739 rectInView=scene()->views()[0]->viewport()->rect();
740 rectInView.moveTo(scene()->views()[0]->horizontalScrollBar()->value(),scene()->views()[0]->verticalScrollBar()->value());
742 if (rectInView.isNull()) return;
744 QColor base = palette().button().color();
745 painter->setClipRect(rect);
746 painter->drawLine(rectInView.left(), 0, rectInView.right(), 0);
747 for (uint i = 0; i < m_tracksCount;i++)
749 painter->drawLine(rectInView.left(), 50 * (i+1), rectInView.right(), 50 * (i+1));
750 painter->drawText(QRectF(10, 50 * i, 100, 50 * i + 49), Qt::AlignLeft, i18n(" Track ") + QString::number(i + 1));
752 int lowerLimit = 50 * m_tracksCount + 1;
753 if (height() > lowerLimit)
754 painter->fillRect(QRectF(rectInView.left(), lowerLimit, rectInView.width(), height() - lowerLimit), QBrush(base));
757 void CustomTrackView::drawForeground ( QPainter * painter, const QRectF & rect )
759 //kDebug()<<"///// DRAWING FB: "<<rect.x()<<", width: "<<rect.width();
760 painter->fillRect(rect, QColor(50, rand() % 250,50,100));
761 painter->drawLine(m_cursorPos, rect.y(), m_cursorPos, rect.y() + rect.height());
764 #include "customtrackview.moc"