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"
37 CustomTrackView::CustomTrackView(KUndoStack *commandStack, QGraphicsScene * projectscene, QWidget *parent)
38 : QGraphicsView(projectscene, parent), m_commandStack(commandStack), 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)
40 setMouseTracking(true);
42 m_animationTimer = new QTimeLine(800);
43 m_animationTimer->setFrameRange(0, 5);
44 m_animationTimer->setUpdateInterval(100);
45 m_animationTimer->setLoopCount(0);
46 m_tipColor = QColor(230, 50, 0, 150);
47 setContentsMargins(0, 0, 0, 0);
49 m_cursorLine = projectscene->addLine(0, 0, 0, 50);
50 m_cursorLine->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIgnoresTransformations);
51 m_cursorLine->setZValue(1000);
55 void CustomTrackView::initView()
61 void CustomTrackView::resizeEvent ( QResizeEvent * event )
63 QGraphicsView::resizeEvent(event);
67 void CustomTrackView::wheelEvent ( QWheelEvent * e )
69 if (e->modifiers() == Qt::ControlModifier) {
70 if (e->delta() > 0) emit zoomIn();
74 if (e->delta() > 0) horizontalScrollBar()->setValue (horizontalScrollBar()->value() + horizontalScrollBar()->singleStep ());
75 else horizontalScrollBar()->setValue (horizontalScrollBar()->value() - horizontalScrollBar()->singleStep ());
81 void CustomTrackView::mouseMoveEvent ( QMouseEvent * event )
84 emit mousePosition(mapToScene(event->pos()).x() / m_scale);
85 /*if (event->modifiers() == Qt::ControlModifier)
86 setDragMode(QGraphicsView::ScrollHandDrag);
87 else if (event->modifiers() == Qt::ShiftModifier)
88 setDragMode(QGraphicsView::RubberBandDrag);
91 if (m_dragItem) { //event->button() == Qt::LeftButton) {
92 // a button was pressed, delete visual tips
93 if (m_operationMode == MOVE) {
94 double snappedPos = getSnapPointForPos(mapToScene(event->pos()).x() - m_clickPoint);
95 double moveX = snappedPos; //mapToScene(event->pos()).x();
96 //kDebug()<<"/////// MOVE CLIP, EVENT Y: "<<event->scenePos().y()<<", SCENE HEIGHT: "<<scene()->sceneRect().height();
97 int moveTrack = (int) mapToScene(event->pos()).y() / 50;
98 int currentTrack = m_dragItem->track();
100 if (moveTrack > m_tracksCount - 1) moveTrack = m_tracksCount - 1;
101 else if (moveTrack < 0) moveTrack = 0;
103 int offset = moveTrack - currentTrack;
104 if (offset != 0) offset = 50 * offset;
105 m_dragItem->moveTo(moveX / m_scale, m_scale, offset, moveTrack);
107 else if (m_operationMode == RESIZESTART) {
108 int pos = mapToScene(event->pos()).x();
109 m_dragItem->resizeStart(pos / m_scale, m_scale);
111 else if (m_operationMode == RESIZEEND) {
112 int pos = mapToScene(event->pos()).x();
113 m_dragItem->resizeEnd(pos / m_scale, m_scale);
115 else if (m_operationMode == FADEIN) {
116 int pos = mapToScene(event->pos()).x() / m_scale;
117 m_dragItem->setFadeIn(pos - m_dragItem->startPos(), m_scale);
119 else if (m_operationMode == FADEOUT) {
120 int pos = mapToScene(event->pos()).x() / m_scale;
121 m_dragItem->setFadeOut(m_dragItem->endPos() - pos, m_scale);
124 if (m_animation) delete m_animation;
126 if (m_visualTip) delete m_visualTip;
128 QGraphicsView::mouseMoveEvent(event);
132 QList<QGraphicsItem *> itemList = items( event->pos());
134 QGraphicsItem *item = NULL;
135 for (int i = 0; i < itemList.count(); i++) {
136 if (itemList.at(i)->type() == 70000) {
137 item = itemList.at(i);
142 ClipItem *clip = (ClipItem*) item;
143 double size = mapToScene(QPoint(8, 0)).x();
144 OPERATIONTYPE opMode = clip->operationMode(mapToScene(event->pos()), m_scale);
145 if (opMode == m_moveOpMode) {
146 QGraphicsView::mouseMoveEvent(event);
151 if (m_animation) delete m_animation;
157 m_moveOpMode = opMode;
158 if (opMode == MOVE) {
159 setCursor(Qt::OpenHandCursor);
161 else if (opMode == RESIZESTART) {
162 kDebug()<<"******** RESIZE CLIP START; WIDTH: "<<size;
163 if (m_visualTip == NULL) {
164 m_visualTip = new QGraphicsRectItem(clip->rect().x(), clip->rect().y(), size, clip->rect().height());
165 ((QGraphicsRectItem*) m_visualTip)->setBrush(m_tipColor);
166 ((QGraphicsRectItem*) m_visualTip)->setPen(QPen(Qt::transparent));
167 m_visualTip->setZValue (100);
168 m_animation = new QGraphicsItemAnimation;
169 m_animation->setItem(m_visualTip);
170 m_animation->setTimeLine(m_animationTimer);
171 m_visualTip->setPos(0, 0);
173 m_animation->setScaleAt(.5, scale, 1);
174 m_animation->setPosAt(.5, QPointF(clip->rect().x() - clip->rect().x() * scale, 0));
176 m_animation->setScaleAt(1, scale, 1);
177 m_animation->setPosAt(1, QPointF(clip->rect().x() - clip->rect().x() * scale, 0));
178 scene()->addItem(m_visualTip);
179 m_animationTimer->start();
181 setCursor(Qt::SizeHorCursor);
183 else if (opMode == RESIZEEND) {
184 if (m_visualTip == NULL) {
185 m_visualTip = new QGraphicsRectItem(clip->rect().x() + clip->rect().width() - size, clip->rect().y(), size, clip->rect().height());
186 ((QGraphicsRectItem*) m_visualTip)->setBrush(m_tipColor);
187 ((QGraphicsRectItem*) m_visualTip)->setPen(QPen(Qt::transparent));
188 m_visualTip->setZValue (100);
189 m_animation = new QGraphicsItemAnimation;
190 m_animation->setItem(m_visualTip);
191 m_animation->setTimeLine(m_animationTimer);
192 m_visualTip->setPos(0, 0);
194 m_animation->setScaleAt(.5, scale, 1);
195 m_animation->setPosAt(.5, QPointF(clip->rect().x() - clip->rect().x() * scale - clip->rect().width(), 0));
197 m_animation->setScaleAt(1, scale, 1);
198 m_animation->setPosAt(1, QPointF(clip->rect().x() - clip->rect().x() * scale, 0));
199 scene()->addItem(m_visualTip);
200 m_animationTimer->start();
202 setCursor(Qt::SizeHorCursor);
204 else if (opMode == FADEIN) {
205 if (m_visualTip == NULL) {
206 m_visualTip = new QGraphicsEllipseItem(clip->rect().x() + clip->fadeIn() * m_scale - size, clip->rect().y() - 8, size * 2, 16);
207 ((QGraphicsEllipseItem*) m_visualTip)->setBrush(m_tipColor);
208 ((QGraphicsEllipseItem*) m_visualTip)->setPen(QPen(Qt::transparent));
209 m_visualTip->setZValue (100);
210 m_animation = new QGraphicsItemAnimation;
211 m_animation->setItem(m_visualTip);
212 m_animation->setTimeLine(m_animationTimer);
213 m_visualTip->setPos(0, 0);
215 m_animation->setScaleAt(.5, scale, scale);
216 m_animation->setPosAt(.5, QPointF(clip->rect().x() - clip->rect().x() * scale - clip->fadeIn() * m_scale, clip->rect().y() - clip->rect().y() * scale));
218 m_animation->setScaleAt(1, scale, scale);
219 m_animation->setPosAt(1, QPointF(clip->rect().x() - clip->rect().x() * scale, clip->rect().y() - clip->rect().y() * scale));
220 scene()->addItem(m_visualTip);
221 m_animationTimer->start();
223 setCursor(Qt::PointingHandCursor);
225 else if (opMode == FADEOUT) {
226 if (m_visualTip == NULL) {
227 m_visualTip = new QGraphicsEllipseItem(clip->rect().x() + clip->rect().width() - clip->fadeOut() * m_scale - size, clip->rect().y() - 8, size*2, 16);
228 ((QGraphicsEllipseItem*) m_visualTip)->setBrush(m_tipColor);
229 ((QGraphicsEllipseItem*) m_visualTip)->setPen(QPen(Qt::transparent));
230 m_visualTip->setZValue (100);
231 m_animation = new QGraphicsItemAnimation;
232 m_animation->setItem(m_visualTip);
233 m_animation->setTimeLine(m_animationTimer);
234 m_visualTip->setPos(0, 0);
236 m_animation->setScaleAt(.5, scale, scale);
237 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));
239 m_animation->setScaleAt(1, scale, scale);
240 m_animation->setPosAt(1, QPointF(clip->rect().x() - clip->rect().x() * scale, clip->rect().y() - clip->rect().y() * scale));
241 scene()->addItem(m_visualTip);
242 m_animationTimer->start();
244 setCursor(Qt::PointingHandCursor);
250 if (m_animation) delete m_animation;
255 setCursor(Qt::ArrowCursor);
258 QGraphicsView::mouseMoveEvent(event);
262 void CustomTrackView::mousePressEvent ( QMouseEvent * event )
264 kDebug()<<"-- TIMELINE MSE PRESSED";
265 int pos = event->x();
266 if (event->modifiers() == Qt::ControlModifier)
267 setDragMode(QGraphicsView::ScrollHandDrag);
268 else if (event->modifiers() == Qt::ShiftModifier)
269 setDragMode(QGraphicsView::RubberBandDrag);
271 bool collision = false;
272 QList<QGraphicsItem *> collisionList = items(event->pos());
273 for (int i = 0; i < collisionList.size(); ++i) {
274 QGraphicsItem *item = collisionList.at(i);
275 if (item->type() == 70000) {
276 m_dragItem = (ClipItem *) item;
277 m_clickPoint = mapToScene(event->pos()).x() - m_dragItem->startPos() * m_scale;
278 m_operationMode = m_dragItem->operationMode(item->mapFromScene(mapToScene(event->pos())), m_scale);
279 if (m_operationMode == MOVE || m_operationMode == RESIZESTART) m_startPos = QPointF(m_dragItem->startPos(), m_dragItem->track());
280 else if (m_operationMode == RESIZEEND) m_startPos = QPointF(m_dragItem->endPos(), m_dragItem->track());
281 kDebug()<<"//////// ITEM CLICKED: "<<m_startPos;
287 kDebug()<<"//////// NO ITEM FOUND ON CLICK";
289 setCursor(Qt::ArrowCursor);
290 setCursorPos((int) mapToScene(event->x(), 0).x());
291 emit cursorMoved(cursorPos());
294 updateSnapPoints(m_dragItem);
296 QGraphicsView::mousePressEvent(event);
299 void CustomTrackView::dragEnterEvent ( QDragEnterEvent * event )
301 if (event->mimeData()->hasText()) {
302 QString clip = event->mimeData()->text();
303 addItem(clip, event->pos());
304 event->acceptProposedAction();
309 void CustomTrackView::addItem(QString producer, QPoint pos)
312 doc.setContent(producer);
313 QDomElement elem = doc.documentElement();
314 int in = elem.attribute("in", 0).toInt();
315 int out = elem.attribute("out", 0).toInt() - in;
316 if (out == 0) out = elem.attribute("duration", 0).toInt();
317 kDebug()<<"ADDING CLIP: "<<producer<<", OUT: "<<out<<", POS: "<<mapToScene(pos);
318 int trackTop = ((int) mapToScene(pos).y()/50) * 50 + 1;
319 m_dropItem = new ClipItem(elem, ((int) mapToScene(pos).y()/50), in, QRectF(mapToScene(pos).x() * m_scale, trackTop, out * m_scale, 49), out);
320 scene()->addItem(m_dropItem);
324 void CustomTrackView::dragMoveEvent(QDragMoveEvent * event) {
325 event->setDropAction(Qt::IgnoreAction);
327 int track = (int) mapToScene(event->pos()).y()/50; //) * (m_scale * 50) + m_scale;
328 kDebug()<<"+++++++++++++ DRAG MOVE, : "<<mapToScene(event->pos()).x()<<", SCAL: "<<m_scale;
329 m_dropItem->moveTo(mapToScene(event->pos()).x() / m_scale, m_scale, (track - m_dropItem->track()) * 50, track);
332 event->setDropAction(Qt::MoveAction);
333 if (event->mimeData()->hasText()) {
334 event->acceptProposedAction();
339 void CustomTrackView::dragLeaveEvent ( QDragLeaveEvent * event ) {
346 void CustomTrackView::dropEvent ( QDropEvent * event ) {
348 AddTimelineClipCommand *command = new AddTimelineClipCommand(this, m_dropItem->xml(), m_dropItem->track(), m_dropItem->startPos(), m_dropItem->rect(), m_dropItem->duration(), false);
349 m_commandStack->push(command);
355 QStringList CustomTrackView::mimeTypes () const
357 QStringList qstrList;
358 // list of accepted mime types for drop
359 qstrList.append("text/plain");
363 Qt::DropActions CustomTrackView::supportedDropActions () const
365 // returns what actions are supported when dropping
366 return Qt::MoveAction;
369 void CustomTrackView::setDuration(int duration)
371 kDebug()<<"///////////// PRO DUR: "<<duration<<", height: "<<50 * m_tracksCount;
372 m_projectDuration = duration;
373 scene()->setSceneRect(0, 0, m_projectDuration + 500, scene()->sceneRect().height()); //50 * m_tracksCount);
377 void CustomTrackView::addTrack ()
380 m_cursorLine->setLine(m_cursorLine->line().x1(), 0, m_cursorLine->line().x1(), 50 * m_tracksCount);
381 //setSceneRect(0, 0, sceneRect().width(), 50 * m_tracksCount);
382 //verticalScrollBar()->setMaximum(50 * m_tracksCount);
383 //setFixedHeight(50 * m_tracksCount);
386 void CustomTrackView::removeTrack ()
389 m_cursorLine->setLine(m_cursorLine->line().x1(), 0, m_cursorLine->line().x1(), 50 * m_tracksCount);
392 void CustomTrackView::setCursorPos(int pos)
395 m_cursorLine->setPos(pos, 0);
398 int CustomTrackView::cursorPos()
403 void CustomTrackView::mouseReleaseEvent ( QMouseEvent * event )
405 QGraphicsView::mouseReleaseEvent(event);
406 setDragMode(QGraphicsView::NoDrag);
407 if (m_dragItem == NULL) return;
408 //kDebug()<<"/// MOVING CLIP: "<<m_startPos<<", END: "<<QPoint(m_dragItem->rect().x(),m_dragItem->rect().y());
409 if (m_operationMode == MOVE) {
411 MoveClipCommand *command = new MoveClipCommand(this, m_startPos, QPointF(m_dragItem->startPos(), m_dragItem->track()), false);
412 m_commandStack->push(command);
414 else if (m_operationMode == RESIZESTART) {
416 ResizeClipCommand *command = new ResizeClipCommand(this, m_startPos, QPointF(m_dragItem->startPos(), m_dragItem->track()), true, false);
417 m_commandStack->push(command);
419 else if (m_operationMode == RESIZEEND) {
421 ResizeClipCommand *command = new ResizeClipCommand(this, m_startPos, QPointF(m_dragItem->endPos(), m_dragItem->track()), false, false);
422 m_commandStack->push(command);
424 m_operationMode = NONE;
428 void CustomTrackView::deleteClip ( const QRectF &rect )
430 ClipItem *item = (ClipItem *) scene()->itemAt(rect.x() + 1, rect.y() + 1);
432 kDebug()<<"---------------- ERROR, CANNOT find clip to move at: "<<rect.x();
438 void CustomTrackView::addClip ( QDomElement xml, int track, int startpos, const QRectF &rect, int duration )
440 QRect r(startpos * m_scale, 50 * track, duration * m_scale, 49);
441 ClipItem *item = new ClipItem(xml, track, startpos, r, duration);
442 scene()->addItem(item);
445 void CustomTrackView::moveClip ( const QPointF &startPos, const QPointF &endPos )
447 ClipItem *item = (ClipItem *) scene()->itemAt((startPos.x() + 1) * m_scale, startPos.y() * 50 + 25);
449 kDebug()<<"---------------- ERROR, CANNOT find clip to move at: "<<startPos.x() * m_scale * FRAME_SIZE + 1<<", "<<startPos.y() * 50 + 25;
452 kDebug()<<"---------------- Move CLIP FROM: "<<startPos.x()<<", END:"<<endPos.x();
453 item->moveTo(endPos.x(), m_scale, (endPos.y() - startPos.y()) * 50, endPos.y());
456 void CustomTrackView::resizeClip ( const QPointF &startPos, const QPointF &endPos, bool resizeClipStart )
459 if (resizeClipStart) offset = 1;
461 ClipItem *item = (ClipItem *) scene()->itemAt((startPos.x() + offset) * m_scale, startPos.y() * 50 + 25);
463 kDebug()<<"---------------- ERROR, CANNOT find clip to resize at: "<<startPos;
466 qreal diff = endPos.x() - startPos.x();
467 if (resizeClipStart) {
468 item->resizeStart(endPos.x(), m_scale);
471 item->resizeEnd(endPos.x(), m_scale);
475 double CustomTrackView::getSnapPointForPos(double pos)
477 for (int i = 0; i < m_snapPoints.size(); ++i) {
478 //kDebug()<<"SNAP POINT: "<<m_snapPoints.at(i);
479 if (abs(pos - m_snapPoints.at(i) * m_scale) < 6 * m_scale) {
480 //kDebug()<<" FOUND SNAP POINT AT: "<<m_snapPoints.at(i)<<", current pos: "<<pos / m_scale;
481 return m_snapPoints.at(i) * m_scale + 0.5;
483 if (m_snapPoints.at(i) > pos) break;
488 void CustomTrackView::updateSnapPoints(ClipItem *selected)
490 m_snapPoints.clear();
492 if (selected) offset = selected->duration();
493 QList<QGraphicsItem *> itemList = items();
494 for (int i = 0; i < itemList.count(); i++) {
495 if (itemList.at(i)->type() == 70000 && itemList.at(i) != selected) {
496 ClipItem *item = (ClipItem *)itemList.at(i);
497 int start = item->startPos();
498 int fadein = item->fadeIn() + start;
499 int end = item->endPos();
500 int fadeout = end - item->fadeOut();
501 m_snapPoints.append(start);
502 if (fadein != start) m_snapPoints.append(fadein);
503 m_snapPoints.append(end);
504 if (fadeout != end) m_snapPoints.append(fadeout);
506 m_snapPoints.append(start - offset);
507 if (fadein != start) m_snapPoints.append(fadein - offset);
508 m_snapPoints.append(end - offset);
509 if (fadeout != end) m_snapPoints.append(fadeout - offset);
513 kDebug()<<" GOT SNAPPOINTS TOTAL: "<<m_snapPoints.count();
515 for (int i = 0; i < m_snapPoints.size(); ++i)
516 kDebug()<<"SNAP POINT: "<<m_snapPoints.at(i);
520 void CustomTrackView::setScale(double scaleFactor)
522 //scale(scaleFactor, scaleFactor);
523 m_scale = scaleFactor;
524 kDebug()<<" HHHHHHHH SCALING: "<<m_scale;
525 QList<QGraphicsItem *> itemList = items();
527 for (int i = 0; i < itemList.count(); i++) {
528 if (itemList.at(i)->type() == 70000) {
529 ClipItem *clip = (ClipItem *)itemList.at(i);
530 clip->setRect(clip->startPos() * m_scale, clip->rect().y(), clip->duration() * m_scale, clip->rect().height());
532 /*else if (itemList.at(i)->type() == 70001) {
533 LabelItem *label = (LabelItem *)itemList.at(i);
534 QGraphicsItem *parent = label->parentItem();
535 QRectF r = label->boundingRect();
536 QRectF p = parent->boundingRect();
537 label->setPos(p.x() + p.width() / 2 - r.width() / 2, p.y() + p.height() / 2 - r.height() / 2);
538 //label->setRect(clip->startPos() * m_scale, clip->rect().y(), clip->duration() * m_scale, clip->rect().height());
543 void CustomTrackView::drawBackground ( QPainter * painter, const QRectF & rect )
545 QColor base = palette().button().color();
546 painter->setPen(base);
547 painter->setClipRect(rect);
548 painter->drawLine(0, 0, rect.width(), 0);
549 for (uint i = 0; i < m_tracksCount;i++)
551 painter->drawLine(0, 50 * (i+1), width(), 50 * (i+1));
552 //painter->drawText(QRectF(10, 50 * i, 100, 50 * i + 49), Qt::AlignLeft, i18n(" Track ") + QString::number(i));
554 int lowerLimit = 50 * m_tracksCount;
555 if (height() > lowerLimit)
556 painter->fillRect(QRectF(0, lowerLimit, rect.width(), height() - lowerLimit), QBrush(base));
559 void CustomTrackView::drawForeground ( QPainter * painter, const QRectF & rect )
561 //kDebug()<<"///// DRAWING FB: "<<rect.x()<<", width: "<<rect.width();
562 painter->fillRect(rect, QColor(50, rand() % 250,50,100));
563 painter->drawLine(m_cursorPos, rect.y(), m_cursorPos, rect.y() + rect.height());
566 #include "customtrackview.moc"