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 int snappedPos = getSnapPointForPos(mapToScene(event->pos()).x() - m_clickPoint);
95 int 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 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());
295 QGraphicsView::mousePressEvent(event);
298 void CustomTrackView::dragEnterEvent ( QDragEnterEvent * event )
300 if (event->mimeData()->hasText()) {
301 QString clip = event->mimeData()->text();
302 addItem(clip, event->pos());
303 event->acceptProposedAction();
308 void CustomTrackView::addItem(QString producer, QPoint pos)
311 doc.setContent(producer);
312 QDomElement elem = doc.documentElement();
313 int in = elem.attribute("in", 0).toInt();
314 int out = elem.attribute("out", 0).toInt() - in;
315 if (out == 0) out = elem.attribute("duration", 0).toInt();
316 kDebug()<<"ADDING CLIP: "<<producer<<", OUT: "<<out<<", POS: "<<mapToScene(pos);
317 int trackTop = ((int) mapToScene(pos).y()/50) * 50 + 1;
318 m_dropItem = new ClipItem(elem, ((int) mapToScene(pos).y()/50), in, QRectF(mapToScene(pos).x() * m_scale, trackTop, out * m_scale, 49), out);
319 scene()->addItem(m_dropItem);
323 void CustomTrackView::dragMoveEvent(QDragMoveEvent * event) {
324 event->setDropAction(Qt::IgnoreAction);
326 int track = (int) mapToScene(event->pos()).y()/50; //) * (m_scale * 50) + m_scale;
327 kDebug()<<"+++++++++++++ DRAG MOVE, : "<<mapToScene(event->pos()).x()<<", SCAL: "<<m_scale;
328 m_dropItem->moveTo(mapToScene(event->pos()).x() / m_scale, m_scale, (track - m_dropItem->track()) * 50, track);
331 event->setDropAction(Qt::MoveAction);
332 if (event->mimeData()->hasText()) {
333 event->acceptProposedAction();
338 void CustomTrackView::dragLeaveEvent ( QDragLeaveEvent * event ) {
345 void CustomTrackView::dropEvent ( QDropEvent * event ) {
347 AddTimelineClipCommand *command = new AddTimelineClipCommand(this, m_dropItem->xml(), m_dropItem->track(), m_dropItem->startPos(), m_dropItem->rect(), m_dropItem->duration(), false);
348 m_commandStack->push(command);
354 QStringList CustomTrackView::mimeTypes () const
356 QStringList qstrList;
357 // list of accepted mime types for drop
358 qstrList.append("text/plain");
362 Qt::DropActions CustomTrackView::supportedDropActions () const
364 // returns what actions are supported when dropping
365 return Qt::MoveAction;
368 void CustomTrackView::setDuration(int duration)
370 kDebug()<<"///////////// PRO DUR: "<<duration<<", height: "<<50 * m_tracksCount;
371 m_projectDuration = duration;
372 scene()->setSceneRect(0, 0, m_projectDuration + 500, scene()->sceneRect().height()); //50 * m_tracksCount);
376 void CustomTrackView::addTrack ()
379 m_cursorLine->setLine(m_cursorLine->line().x1(), 0, m_cursorLine->line().x1(), 50 * m_tracksCount);
380 //setSceneRect(0, 0, sceneRect().width(), 50 * m_tracksCount);
381 //verticalScrollBar()->setMaximum(50 * m_tracksCount);
382 //setFixedHeight(50 * m_tracksCount);
385 void CustomTrackView::removeTrack ()
388 m_cursorLine->setLine(m_cursorLine->line().x1(), 0, m_cursorLine->line().x1(), 50 * m_tracksCount);
391 void CustomTrackView::setCursorPos(int pos)
394 m_cursorLine->setPos(pos, 0);
397 int CustomTrackView::cursorPos()
402 void CustomTrackView::mouseReleaseEvent ( QMouseEvent * event )
404 QGraphicsView::mouseReleaseEvent(event);
405 setDragMode(QGraphicsView::NoDrag);
406 if (m_dragItem == NULL) return;
407 //kDebug()<<"/// MOVING CLIP: "<<m_startPos<<", END: "<<QPoint(m_dragItem->rect().x(),m_dragItem->rect().y());
408 if (m_operationMode == MOVE) {
410 MoveClipCommand *command = new MoveClipCommand(this, m_startPos, QPointF(m_dragItem->startPos(), m_dragItem->track()), false);
411 m_commandStack->push(command);
413 else if (m_operationMode == RESIZESTART) {
415 ResizeClipCommand *command = new ResizeClipCommand(this, m_startPos, QPointF(m_dragItem->startPos(), m_dragItem->track()), true, false);
416 m_commandStack->push(command);
418 else if (m_operationMode == RESIZEEND) {
420 ResizeClipCommand *command = new ResizeClipCommand(this, m_startPos, QPointF(m_dragItem->endPos(), m_dragItem->track()), false, false);
421 m_commandStack->push(command);
423 m_operationMode = NONE;
427 void CustomTrackView::deleteClip ( const QRectF &rect )
429 ClipItem *item = (ClipItem *) scene()->itemAt(rect.x() + 1, rect.y() + 1);
431 kDebug()<<"---------------- ERROR, CANNOT find clip to move at: "<<rect.x();
437 void CustomTrackView::addClip ( QDomElement xml, int track, int startpos, const QRectF &rect, int duration )
439 QRect r(startpos * m_scale, 50 * track, duration * m_scale, 49);
440 ClipItem *item = new ClipItem(xml, track, startpos, r, duration);
441 scene()->addItem(item);
444 void CustomTrackView::moveClip ( const QPointF &startPos, const QPointF &endPos )
446 ClipItem *item = (ClipItem *) scene()->itemAt((startPos.x() + 1) * m_scale, startPos.y() * 50 + 25);
448 kDebug()<<"---------------- ERROR, CANNOT find clip to move at: "<<startPos.x() * m_scale * FRAME_SIZE + 1<<", "<<startPos.y() * 50 + 25;
451 kDebug()<<"---------------- Move CLIP FROM: "<<startPos.x()<<", END:"<<endPos.x();
452 item->moveTo(endPos.x(), m_scale, (endPos.y() - startPos.y()) * 50, endPos.y());
455 void CustomTrackView::resizeClip ( const QPointF &startPos, const QPointF &endPos, bool resizeClipStart )
458 if (resizeClipStart) offset = 1;
460 ClipItem *item = (ClipItem *) scene()->itemAt((startPos.x() + offset) * m_scale, startPos.y() * 50 + 25);
462 kDebug()<<"---------------- ERROR, CANNOT find clip to resize at: "<<startPos;
465 qreal diff = endPos.x() - startPos.x();
466 if (resizeClipStart) {
467 item->resizeStart(endPos.x(), m_scale);
470 item->resizeEnd(endPos.x(), m_scale);
474 double CustomTrackView::getSnapPointForPos(double pos)
476 for (int i = 0; i < m_snapPoints.size(); ++i) {
477 if (abs(pos - m_snapPoints.at(i) * m_scale) < 6) return m_snapPoints.at(i) * m_scale;
478 if (m_snapPoints.at(i) > pos) break;
483 void CustomTrackView::updateSnapPoints()
485 m_snapPoints.clear();
486 QList<QGraphicsItem *> itemList = items();
487 for (int i = 0; i < itemList.count(); i++) {
488 if (itemList.at(i)->type() == 70000) {
489 ClipItem *item = (ClipItem *)itemList.at(i);
490 m_snapPoints.append(item->startPos());
491 if (item->fadeIn() != 0) m_snapPoints.append(item->startPos() + item->fadeIn());
492 m_snapPoints.append(item->endPos());
493 if (item->fadeOut() != 0) m_snapPoints.append(item->endPos() - item->fadeOut());
500 void CustomTrackView::setScale(double scaleFactor)
502 //scale(scaleFactor, scaleFactor);
503 m_scale = scaleFactor;
504 kDebug()<<" HHHHHHHH SCALING: "<<m_scale;
505 QList<QGraphicsItem *> itemList = items();
507 for (int i = 0; i < itemList.count(); i++) {
508 if (itemList.at(i)->type() == 70000) {
509 ClipItem *clip = (ClipItem *)itemList.at(i);
510 clip->setRect(clip->startPos() * m_scale, clip->rect().y(), clip->duration() * m_scale, clip->rect().height());
512 /*else if (itemList.at(i)->type() == 70001) {
513 LabelItem *label = (LabelItem *)itemList.at(i);
514 QGraphicsItem *parent = label->parentItem();
515 QRectF r = label->boundingRect();
516 QRectF p = parent->boundingRect();
517 label->setPos(p.x() + p.width() / 2 - r.width() / 2, p.y() + p.height() / 2 - r.height() / 2);
518 //label->setRect(clip->startPos() * m_scale, clip->rect().y(), clip->duration() * m_scale, clip->rect().height());
523 void CustomTrackView::drawBackground ( QPainter * painter, const QRectF & rect )
525 QColor base = palette().button().color();
526 painter->setPen(base);
527 painter->setClipRect(rect);
528 painter->drawLine(0, 0, rect.width(), 0);
529 for (uint i = 0; i < m_tracksCount;i++)
531 painter->drawLine(0, 50 * (i+1), width(), 50 * (i+1));
532 //painter->drawText(QRectF(10, 50 * i, 100, 50 * i + 49), Qt::AlignLeft, i18n(" Track ") + QString::number(i));
534 int lowerLimit = 50 * m_tracksCount;
535 if (height() > lowerLimit)
536 painter->fillRect(QRectF(0, lowerLimit, rect.width(), height() - lowerLimit), QBrush(base));
539 void CustomTrackView::drawForeground ( QPainter * painter, const QRectF & rect )
541 //kDebug()<<"///// DRAWING FB: "<<rect.x()<<", width: "<<rect.width();
542 painter->fillRect(rect, QColor(50, rand() % 250,50,100));
543 painter->drawLine(m_cursorPos, rect.y(), m_cursorPos, rect.y() + rect.height());
546 #include "customtrackview.moc"