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>
31 #include "customtrackview.h"
33 #include "definitions.h"
34 #include "moveclipcommand.h"
35 #include "resizeclipcommand.h"
36 #include "addtimelineclipcommand.h"
37 #include "addeffectcommand.h"
38 #include "editeffectcommand.h"
39 #include "kdenlivesettings.h"
40 #include "transition.h"
42 CustomTrackView::CustomTrackView(KdenliveDoc *doc, QGraphicsScene * projectscene, QWidget *parent)
43 : 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), m_autoScroll(KdenliveSettings::autoscroll()) {
44 if (doc) m_commandStack = doc->commandStack();
45 else m_commandStack == NULL;
46 setMouseTracking(true);
48 m_animationTimer = new QTimeLine(800);
49 m_animationTimer->setFrameRange(0, 5);
50 m_animationTimer->setUpdateInterval(100);
51 m_animationTimer->setLoopCount(0);
52 m_tipColor = QColor(0, 192, 0, 200);
53 QColor border = QColor(255, 255, 255, 100);
54 m_tipPen.setColor(border);
57 setContentsMargins(0, 0, 0, 0);
59 m_cursorLine = projectscene->addLine(0, 0, 0, 50);
60 m_cursorLine->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIgnoresTransformations);
61 m_cursorLine->setZValue(1000);
65 void CustomTrackView::checkAutoScroll() {
66 m_autoScroll = KdenliveSettings::autoscroll();
70 void CustomTrackView::resizeEvent(QResizeEvent * event) {
71 QGraphicsView::resizeEvent(event);
75 void CustomTrackView::wheelEvent(QWheelEvent * e) {
76 if (e->modifiers() == Qt::ControlModifier) {
77 if (e->delta() > 0) emit zoomIn();
80 if (e->delta() > 0) horizontalScrollBar()->setValue(horizontalScrollBar()->value() + horizontalScrollBar()->singleStep());
81 else horizontalScrollBar()->setValue(horizontalScrollBar()->value() - horizontalScrollBar()->singleStep());
87 void CustomTrackView::mouseMoveEvent(QMouseEvent * event) {
89 emit mousePosition(mapToScene(event->pos()).x() / m_scale);
90 /*if (event->modifiers() == Qt::ControlModifier)
91 setDragMode(QGraphicsView::ScrollHandDrag);
92 else if (event->modifiers() == Qt::ShiftModifier)
93 setDragMode(QGraphicsView::RubberBandDrag);
97 if (m_dragItem) { //event->button() == Qt::LeftButton) {
98 // a button was pressed, delete visual tips
99 if (m_operationMode == MOVE) {
100 double snappedPos = getSnapPointForPos(mapToScene(event->pos()).x() - m_clickPoint);
101 //kDebug()<<"/////// MOVE CLIP, EVENT Y: "<<event->scenePos().y()<<", SCENE HEIGHT: "<<scene()->sceneRect().height();
102 int moveTrack = (int) mapToScene(event->pos()).y() / 50;
103 int currentTrack = m_dragItem->track();
105 if (moveTrack > m_tracksCount - 1) moveTrack = m_tracksCount - 1;
106 else if (moveTrack < 0) moveTrack = 0;
108 int offset = moveTrack - currentTrack;
109 if (offset != 0) offset = 50 * offset;
110 m_dragItem->moveTo(snappedPos / m_scale, m_scale, offset, moveTrack);
111 } else if (m_operationMode == RESIZESTART) {
112 double snappedPos = getSnapPointForPos(mapToScene(event->pos()).x());
113 m_dragItem->resizeStart(snappedPos / m_scale, m_scale);
114 } else if (m_operationMode == RESIZEEND) {
115 double snappedPos = getSnapPointForPos(mapToScene(event->pos()).x());
116 m_dragItem->resizeEnd(snappedPos / m_scale, m_scale);
117 } else if (m_operationMode == FADEIN) {
118 int pos = mapToScene(event->pos()).x() / m_scale;
119 m_dragItem->setFadeIn(pos - m_dragItem->startPos().frames(m_document->fps()), m_scale);
120 } else if (m_operationMode == FADEOUT) {
121 int pos = mapToScene(event->pos()).x() / m_scale;
122 m_dragItem->setFadeOut(m_dragItem->endPos().frames(m_document->fps()) - pos, m_scale);
125 if (m_animation) delete m_animation;
127 if (m_visualTip) delete m_visualTip;
129 QGraphicsView::mouseMoveEvent(event);
133 QList<QGraphicsItem *> itemList = items(event->pos());
135 QGraphicsItem *item = NULL;
136 for (int i = 0; i < itemList.count(); i++) {
137 if (itemList.at(i)->type() == 70000) {
138 item = itemList.at(i);
142 if (item && event->buttons() == Qt::NoButton) {
143 ClipItem *clip = (ClipItem*) item;
145 OPERATIONTYPE opMode = clip->operationMode(mapToScene(event->pos()), m_scale);
146 if (opMode == m_moveOpMode) {
147 QGraphicsView::mouseMoveEvent(event);
151 if (m_animation) delete m_animation;
153 m_animationTimer->stop();
158 m_moveOpMode = opMode;
159 if (opMode == MOVE) {
160 setCursor(Qt::OpenHandCursor);
161 } else if (opMode == RESIZESTART) {
162 setCursor(KCursor("left_side", Qt::SizeHorCursor));
163 kDebug() << "******** RESIZE CLIP START; WIDTH: " << size;
164 if (m_visualTip == NULL) {
166 polygon << QPoint(clip->rect().x(), clip->rect().y() + clip->rect().height() / 2 - size * 2);
167 polygon << QPoint(clip->rect().x() + size * 2, clip->rect().y() + clip->rect().height() / 2);
168 polygon << QPoint(clip->rect().x(), clip->rect().y() + clip->rect().height() / 2 + size * 2);
169 polygon << QPoint(clip->rect().x(), clip->rect().y() + clip->rect().height() / 2 - size * 2);
171 m_visualTip = new QGraphicsPolygonItem(polygon);
172 ((QGraphicsPolygonItem*) m_visualTip)->setBrush(m_tipColor);
173 ((QGraphicsPolygonItem*) m_visualTip)->setPen(m_tipPen);
174 m_visualTip->setZValue(100);
175 m_animation = new QGraphicsItemAnimation;
176 m_animation->setItem(m_visualTip);
177 m_animation->setTimeLine(m_animationTimer);
178 m_visualTip->setPos(0, 0);
180 m_animation->setScaleAt(.5, scale, 1);
181 m_animation->setPosAt(.5, QPointF(clip->rect().x() - clip->rect().x() * scale, 0));
183 m_animation->setScaleAt(1, scale, 1);
184 m_animation->setPosAt(1, QPointF(clip->rect().x() - clip->rect().x() * scale, 0));
185 scene()->addItem(m_visualTip);
186 m_animationTimer->start();
188 } else if (opMode == RESIZEEND) {
189 setCursor(KCursor("right_side", Qt::SizeHorCursor));
190 if (m_visualTip == NULL) {
192 polygon << QPoint(clip->rect().x() + clip->rect().width(), clip->rect().y() + clip->rect().height() / 2 - size * 2);
193 polygon << QPoint(clip->rect().x() + clip->rect().width() - size * 2, clip->rect().y() + clip->rect().height() / 2);
194 polygon << QPoint(clip->rect().x() + clip->rect().width(), clip->rect().y() + clip->rect().height() / 2 + size * 2);
195 polygon << QPoint(clip->rect().x() + clip->rect().width(), clip->rect().y() + clip->rect().height() / 2 - size * 2);
197 m_visualTip = new QGraphicsPolygonItem(polygon);
198 ((QGraphicsPolygonItem*) m_visualTip)->setBrush(m_tipColor);
199 ((QGraphicsPolygonItem*) m_visualTip)->setPen(m_tipPen);
201 m_visualTip->setZValue(100);
202 m_animation = new QGraphicsItemAnimation;
203 m_animation->setItem(m_visualTip);
204 m_animation->setTimeLine(m_animationTimer);
205 m_visualTip->setPos(0, 0);
207 m_animation->setScaleAt(.5, scale, 1);
208 m_animation->setPosAt(.5, QPointF(clip->rect().x() - clip->rect().x() * scale - clip->rect().width(), 0));
210 m_animation->setScaleAt(1, scale, 1);
211 m_animation->setPosAt(1, QPointF(clip->rect().x() - clip->rect().x() * scale, 0));
212 scene()->addItem(m_visualTip);
213 m_animationTimer->start();
215 } else if (opMode == FADEIN) {
216 if (m_visualTip == NULL) {
217 m_visualTip = new QGraphicsEllipseItem(clip->rect().x() + clip->fadeIn() * m_scale - size, clip->rect().y() - 8, size * 2, 16);
218 ((QGraphicsEllipseItem*) m_visualTip)->setBrush(m_tipColor);
219 ((QGraphicsEllipseItem*) m_visualTip)->setPen(m_tipPen);
220 m_visualTip->setZValue(100);
221 m_animation = new QGraphicsItemAnimation;
222 m_animation->setItem(m_visualTip);
223 m_animation->setTimeLine(m_animationTimer);
224 m_visualTip->setPos(0, 0);
226 m_animation->setScaleAt(.5, scale, scale);
227 m_animation->setPosAt(.5, QPointF(clip->rect().x() - clip->rect().x() * scale - clip->fadeIn() * m_scale, clip->rect().y() - clip->rect().y() * scale));
229 m_animation->setScaleAt(1, scale, scale);
230 m_animation->setPosAt(1, QPointF(clip->rect().x() - clip->rect().x() * scale, clip->rect().y() - clip->rect().y() * scale));
231 scene()->addItem(m_visualTip);
232 m_animationTimer->start();
234 setCursor(Qt::PointingHandCursor);
235 } else if (opMode == FADEOUT) {
236 if (m_visualTip == NULL) {
237 m_visualTip = new QGraphicsEllipseItem(clip->rect().x() + clip->rect().width() - clip->fadeOut() * m_scale - size, clip->rect().y() - 8, size*2, 16);
238 ((QGraphicsEllipseItem*) m_visualTip)->setBrush(m_tipColor);
239 ((QGraphicsEllipseItem*) m_visualTip)->setPen(m_tipPen);
240 m_visualTip->setZValue(100);
241 m_animation = new QGraphicsItemAnimation;
242 m_animation->setItem(m_visualTip);
243 m_animation->setTimeLine(m_animationTimer);
244 m_visualTip->setPos(0, 0);
246 m_animation->setScaleAt(.5, scale, scale);
247 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));
249 m_animation->setScaleAt(1, scale, scale);
250 m_animation->setPosAt(1, QPointF(clip->rect().x() - clip->rect().x() * scale, clip->rect().y() - clip->rect().y() * scale));
251 scene()->addItem(m_visualTip);
252 m_animationTimer->start();
254 setCursor(Qt::PointingHandCursor);
255 } else if (opMode == TRANSITIONSTART) {
256 if (m_visualTip == NULL) {
257 m_visualTip = new QGraphicsEllipseItem(-5, -5 , 10, 10);
258 ((QGraphicsEllipseItem*) m_visualTip)->setBrush(m_tipColor);
259 ((QGraphicsEllipseItem*) m_visualTip)->setPen(m_tipPen);
260 m_visualTip->setZValue(100);
261 m_animation = new QGraphicsItemAnimation;
262 m_animation->setItem(m_visualTip);
263 m_animation->setTimeLine(m_animationTimer);
264 m_visualTip->setPos(clip->rect().x() + 15, clip->rect().y() + clip->rect().height() / 2);
266 m_animation->setScaleAt(.5, scale, scale);
268 m_animation->setScaleAt(1, scale, scale);
269 scene()->addItem(m_visualTip);
270 m_animationTimer->start();
272 setCursor(Qt::PointingHandCursor);
273 } else if (opMode == TRANSITIONEND) {
274 if (m_visualTip == NULL) {
275 m_visualTip = new QGraphicsEllipseItem(-5, -5 , 10, 10);
276 ((QGraphicsEllipseItem*) m_visualTip)->setBrush(m_tipColor);
277 ((QGraphicsEllipseItem*) m_visualTip)->setPen(m_tipPen);
278 m_visualTip->setZValue(100);
279 m_animation = new QGraphicsItemAnimation;
280 m_animation->setItem(m_visualTip);
281 m_animation->setTimeLine(m_animationTimer);
282 m_visualTip->setPos(clip->rect().x() + clip->rect().width() - 15 , clip->rect().y() + clip->rect().height() / 2);
284 m_animation->setScaleAt(.5, scale, scale);
286 m_animation->setScaleAt(1, scale, scale);
287 scene()->addItem(m_visualTip);
288 m_animationTimer->start();
290 setCursor(Qt::PointingHandCursor);
294 if (event->buttons() != Qt::NoButton && event->modifiers() == Qt::NoModifier) {
295 setCursorPos((int) mapToScene(event->pos().x(), 0).x() / m_scale);
298 if (m_animation) delete m_animation;
299 m_animationTimer->stop();
305 setCursor(Qt::ArrowCursor);
308 QGraphicsView::mouseMoveEvent(event);
312 void CustomTrackView::mousePressEvent(QMouseEvent * event) {
313 int pos = event->x();
314 if (event->modifiers() == Qt::ControlModifier) {
315 setDragMode(QGraphicsView::ScrollHandDrag);
316 QGraphicsView::mousePressEvent(event);
318 } else if (event->modifiers() == Qt::ShiftModifier) {
319 setDragMode(QGraphicsView::RubberBandDrag);
320 QGraphicsView::mousePressEvent(event);
323 bool collision = false;
324 QList<QGraphicsItem *> collisionList = items(event->pos());
325 for (int i = 0; i < collisionList.size(); ++i) {
326 QGraphicsItem *item = collisionList.at(i);
327 if (item->type() == 70000) {
329 if (!item->isSelected()) {
330 QList<QGraphicsItem *> itemList = items();
331 for (int i = 0; i < itemList.count(); i++)
332 itemList.at(i)->setSelected(false);
333 item->setSelected(true);
336 m_dragItem = (ClipItem *) item;
337 emit clipItemSelected(m_dragItem);
338 m_clickPoint = mapToScene(event->pos()).x() - m_dragItem->startPos().frames(m_document->fps()) * m_scale;
339 m_operationMode = m_dragItem->operationMode(item->mapFromScene(mapToScene(event->pos())), m_scale);
340 if (m_operationMode == MOVE) setCursor(Qt::ClosedHandCursor);
341 if (m_operationMode == MOVE || m_operationMode == RESIZESTART)
342 m_startPos = QPointF(m_dragItem->startPos().frames(m_document->fps()), m_dragItem->track());
343 else if (m_operationMode == RESIZEEND)
344 m_startPos = QPointF(m_dragItem->endPos().frames(m_document->fps()), m_dragItem->track());
345 else if (m_operationMode == TRANSITIONSTART) {
346 Transition tra(m_dragItem, LUMA_TRANSITION, m_dragItem->startPos(), m_dragItem->startPos() + GenTime(2.5));
347 m_dragItem->addTransition(tra);
349 kDebug() << "//////// ITEM CLICKED: " << m_startPos;
355 kDebug() << "//////// NO ITEM FOUND ON CLICK";
357 setCursor(Qt::ArrowCursor);
358 QList<QGraphicsItem *> itemList = items();
359 for (int i = 0; i < itemList.count(); i++)
360 itemList.at(i)->setSelected(false);
361 emit clipItemSelected(NULL);
362 setCursorPos((int) mapToScene(event->x(), 0).x() / m_scale);
365 updateSnapPoints(m_dragItem);
367 //QGraphicsView::mousePressEvent(event);
370 void CustomTrackView::dragEnterEvent(QDragEnterEvent * event) {
371 if (event->mimeData()->hasFormat("kdenlive/producerslist")) {
372 kDebug() << "/////////////// DRAG ENTERED, TEXT: " << event->mimeData()->data("kdenlive/producerslist");
373 QStringList ids = QString(event->mimeData()->data("kdenlive/producerslist")).split(";");
374 //TODO: drop of several clips
375 for (int i = 0; i < ids.size(); ++i) {
377 DocClipBase *clip = m_document->getBaseClip(ids.at(0).toInt());
378 if (clip == NULL) kDebug() << " WARNING))))))))) CLIP NOT FOUND : " << ids.at(0).toInt();
379 addItem(clip, event->pos());
380 event->acceptProposedAction();
381 } else QGraphicsView::dragEnterEvent(event);
384 void CustomTrackView::slotRefreshEffects(ClipItem *clip) {
385 int track = m_tracksCount - clip->track();
386 GenTime pos = clip->startPos();
387 m_document->renderer()->mltRemoveEffect(track, pos, "-1", false);
388 for (int i = 0; i < clip->effectsCount(); i++) {
389 m_document->renderer()->mltAddEffect(track, pos, clip->getEffectArgs(clip->effectAt(i)), false);
391 m_document->renderer()->doRefresh();
394 void CustomTrackView::addEffect(int track, GenTime pos, QDomElement effect) {
395 ClipItem *clip = getClipItemAt(pos.frames(m_document->fps()) + 1, m_tracksCount - track);
397 QMap <QString, QString> effectParams = clip->addEffect(effect);
398 m_document->renderer()->mltAddEffect(track, pos, effectParams);
399 emit clipItemSelected(clip);
403 void CustomTrackView::deleteEffect(int track, GenTime pos, QDomElement effect) {
404 QString index = effect.attribute("kdenlive_ix");
405 m_document->renderer()->mltRemoveEffect(track, pos, index);
406 ClipItem *clip = getClipItemAt(pos.frames(m_document->fps()) + 1, m_tracksCount - track);
408 clip->deleteEffect(index);
409 emit clipItemSelected(clip);
413 void CustomTrackView::slotAddEffect(QDomElement effect, GenTime pos, int track) {
414 QList<QGraphicsItem *> itemList;
418 ClipItem *clip = getClipItemAt(pos.frames(m_document->fps()) + 1, track);
419 if (clip) itemList.append(clip);
420 else kDebug() << "------ wrning, clip eff not found";
422 kDebug() << "// REQUESTING EFFECT ON CLIP: " << pos.frames(25) << ", TRK: " << track;
423 for (int i = 0; i < itemList.count(); i++) {
424 if (itemList.at(i)->type() == 70000 && (itemList.at(i)->isSelected() || track != -1)) {
425 ClipItem *item = (ClipItem *)itemList.at(i);
426 // the kdenlive_ix int is used to identify an effect in mlt's playlist, should
428 if (effect.attribute("kdenlive_ix").toInt() == 0)
429 effect.setAttribute("kdenlive_ix", QString::number(item->effectsCounter()));
430 AddEffectCommand *command = new AddEffectCommand(this, m_tracksCount - item->track(), item->startPos(), effect, true);
431 m_commandStack->push(command);
436 void CustomTrackView::slotDeleteEffect(ClipItem *clip, QDomElement effect) {
437 AddEffectCommand *command = new AddEffectCommand(this, m_tracksCount - clip->track(), clip->startPos(), effect, false);
438 m_commandStack->push(command);
441 void CustomTrackView::updateEffect(int track, GenTime pos, QDomElement effect) {
442 ClipItem *clip = getClipItemAt(pos.frames(m_document->fps()) + 1, m_tracksCount - track);
444 QMap <QString, QString> effectParams = clip->getEffectArgs(effect);
445 if (effectParams["disabled"] == "1") {
446 QString index = effectParams["kdenlive_ix"];
447 m_document->renderer()->mltRemoveEffect(track, pos, index);
448 } else m_document->renderer()->mltEditEffect(m_tracksCount - clip->track(), clip->startPos(), effectParams);
452 void CustomTrackView::slotChangeEffectState(ClipItem *clip, QDomElement effect, bool disable) {
453 QDomElement oldEffect = effect.cloneNode().toElement();
454 effect.setAttribute("disabled", disable);
455 EditEffectCommand *command = new EditEffectCommand(this, m_tracksCount - clip->track(), clip->startPos(), oldEffect, effect, true);
456 m_commandStack->push(command);
459 void CustomTrackView::slotUpdateClipEffect(ClipItem *clip, QDomElement oldeffect, QDomElement effect) {
460 EditEffectCommand *command = new EditEffectCommand(this, m_tracksCount - clip->track(), clip->startPos(), oldeffect, effect, true);
461 m_commandStack->push(command);
465 void CustomTrackView::addItem(DocClipBase *clip, QPoint pos) {
467 GenTime out = clip->duration();
468 //kdDebug()<<"- - - -CREATING CLIP, duration = "<<out<<", URL: "<<clip->fileURL();
469 int trackTop = ((int) mapToScene(pos).y() / 50) * 50 + 1;
470 m_dropItem = new ClipItem(clip, ((int) mapToScene(pos).y() / 50), GenTime(), QRectF(mapToScene(pos).x() * m_scale, trackTop, out.frames(m_document->fps()) * m_scale, 49), out, m_document->fps());
471 scene()->addItem(m_dropItem);
475 void CustomTrackView::dragMoveEvent(QDragMoveEvent * event) {
476 event->setDropAction(Qt::IgnoreAction);
477 //kDebug()<<"+++++++++++++ DRAG MOVE, : "<<mapToScene(event->pos()).x()<<", SCAL: "<<m_scale;
479 int track = (int) mapToScene(event->pos()).y() / 50; //) * (m_scale * 50) + m_scale;
480 m_dropItem->moveTo(mapToScene(event->pos()).x() / m_scale, m_scale, (track - m_dropItem->track()) * 50, track);
481 event->setDropAction(Qt::MoveAction);
482 if (event->mimeData()->hasFormat("kdenlive/producerslist")) {
483 event->acceptProposedAction();
486 QGraphicsView::dragMoveEvent(event);
490 void CustomTrackView::dragLeaveEvent(QDragLeaveEvent * event) {
494 } else QGraphicsView::dragLeaveEvent(event);
497 void CustomTrackView::dropEvent(QDropEvent * event) {
499 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);
500 m_commandStack->push(command);
501 m_dropItem->baseClip()->addReference();
502 m_document->updateClip(m_dropItem->baseClip()->getId());
503 // kDebug()<<"IIIIIIIIIIIIIIIIIIIIIIII TRAX CNT: "<<m_tracksCount<<", DROP: "<<m_dropItem->track();
504 m_document->renderer()->mltInsertClip(m_tracksCount - m_dropItem->track(), m_dropItem->startPos(), m_dropItem->xml());
505 } else QGraphicsView::dropEvent(event);
510 QStringList CustomTrackView::mimeTypes() const {
511 QStringList qstrList;
512 // list of accepted mime types for drop
513 qstrList.append("text/plain");
514 qstrList.append("kdenlive/producerslist");
518 Qt::DropActions CustomTrackView::supportedDropActions() const {
519 // returns what actions are supported when dropping
520 return Qt::MoveAction;
523 void CustomTrackView::setDuration(int duration) {
524 kDebug() << "///////////// PRO DUR: " << duration << ", height: " << 50 * m_tracksCount;
525 m_projectDuration = duration;
526 scene()->setSceneRect(0, 0, (m_projectDuration + 500) * m_scale, scene()->sceneRect().height()); //50 * m_tracksCount);
530 void CustomTrackView::addTrack() {
532 m_cursorLine->setLine(m_cursorLine->line().x1(), 0, m_cursorLine->line().x1(), 50 * m_tracksCount);
533 //setSceneRect(0, 0, sceneRect().width(), 50 * m_tracksCount);
534 //verticalScrollBar()->setMaximum(50 * m_tracksCount);
535 //setFixedHeight(50 * m_tracksCount);
538 void CustomTrackView::removeTrack() {
540 m_cursorLine->setLine(m_cursorLine->line().x1(), 0, m_cursorLine->line().x1(), 50 * m_tracksCount);
543 void CustomTrackView::deleteClip(int clipId) {
544 QList<QGraphicsItem *> itemList = items();
545 for (int i = 0; i < itemList.count(); i++) {
546 if (itemList.at(i)->type() == 70000) {
547 ClipItem *item = (ClipItem *)itemList.at(i);
548 if (item->clipProducer() == clipId) {
549 AddTimelineClipCommand *command = new AddTimelineClipCommand(this, item->xml(), item->clipProducer(), item->track(), item->startPos(), item->rect(), item->duration(), true, true);
550 m_commandStack->push(command);
557 void CustomTrackView::setCursorPos(int pos, bool seek) {
558 emit cursorMoved(m_cursorPos * m_scale, pos * m_scale);
560 m_cursorLine->setPos(pos * m_scale, 0);
561 if (seek) m_document->renderer()->seek(GenTime(pos, m_document->fps()));
562 else if (m_autoScroll && m_scale < 50) checkScrolling();
565 void CustomTrackView::updateCursorPos() {
566 m_cursorLine->setPos(m_cursorPos * m_scale, 0);
569 int CustomTrackView::cursorPos() {
570 return m_cursorPos * m_scale;
573 void CustomTrackView::checkScrolling() {
574 QRect rectInView = viewport()->rect();
575 int delta = rectInView.width() / 3;
576 int max = rectInView.right() + horizontalScrollBar()->value() - delta;
577 //kDebug() << "CURSOR POS: "<<m_cursorPos<< "Scale: "<<m_scale;
578 if (m_cursorPos * m_scale >= max) horizontalScrollBar()->setValue(horizontalScrollBar()->value() + 1 + m_scale);
581 void CustomTrackView::mouseReleaseEvent(QMouseEvent * event) {
582 QGraphicsView::mouseReleaseEvent(event);
583 setDragMode(QGraphicsView::NoDrag);
584 if (m_dragItem == NULL) return;
585 if (m_operationMode == MOVE) setCursor(Qt::OpenHandCursor);
586 if (m_operationMode == MOVE && m_startPos.x() != m_dragItem->startPos().frames(m_document->fps())) {
588 MoveClipCommand *command = new MoveClipCommand(this, m_startPos, QPointF(m_dragItem->startPos().frames(m_document->fps()), m_dragItem->track()), false);
589 m_commandStack->push(command);
590 m_document->renderer()->mltMoveClip(m_tracksCount - m_startPos.y(), m_tracksCount - m_dragItem->track(), m_startPos.x(), m_dragItem->startPos().frames(m_document->fps()));
591 } else if (m_operationMode == RESIZESTART) {
593 ResizeClipCommand *command = new ResizeClipCommand(this, m_startPos, QPointF(m_dragItem->startPos().frames(m_document->fps()), m_dragItem->track()), true, false);
595 m_document->renderer()->mltResizeClipStart(m_tracksCount - m_dragItem->track(), m_dragItem->endPos(), m_dragItem->startPos(), GenTime(m_startPos.x(), m_document->fps()), m_dragItem->cropStart(), m_dragItem->cropStart() + m_dragItem->endPos() - m_dragItem->startPos());
596 m_commandStack->push(command);
597 m_document->renderer()->doRefresh();
598 } else if (m_operationMode == RESIZEEND) {
600 ResizeClipCommand *command = new ResizeClipCommand(this, m_startPos, QPointF(m_dragItem->endPos().frames(m_document->fps()), m_dragItem->track()), false, false);
602 m_document->renderer()->mltResizeClipEnd(m_tracksCount - m_dragItem->track(), m_dragItem->startPos(), m_dragItem->cropStart(), m_dragItem->cropStart() + m_dragItem->endPos() - m_dragItem->startPos());
603 m_commandStack->push(command);
604 m_document->renderer()->doRefresh();
606 m_operationMode = NONE;
610 void CustomTrackView::deleteClip(int track, GenTime startpos, const QRectF &rect) {
611 ClipItem *item = getClipItemAt(startpos, track);
613 kDebug() << "---------------- ERROR, CANNOT find clip to move at: " << rect.x();
616 item->baseClip()->removeReference();
617 m_document->updateClip(item->baseClip()->getId());
619 m_document->renderer()->mltRemoveClip(m_tracksCount - track, startpos);
620 m_document->renderer()->doRefresh();
623 void CustomTrackView::addClip(QDomElement xml, int clipId, int track, GenTime startpos, const QRectF &rect, GenTime duration) {
624 QRect r(startpos.frames(m_document->fps()) * m_scale, 50 * track, duration.frames(m_document->fps()) * m_scale, 49);
625 DocClipBase *baseclip = m_document->clipManager()->getClipById(clipId);
626 ClipItem *item = new ClipItem(baseclip, track, startpos, r, duration, m_document->fps());
627 scene()->addItem(item);
628 baseclip->addReference();
629 m_document->updateClip(baseclip->getId());
630 m_document->renderer()->mltInsertClip(m_tracksCount - track, startpos, xml);
631 m_document->renderer()->doRefresh();
634 ClipItem *CustomTrackView::getClipItemAt(int pos, int track) {
635 return (ClipItem *) scene()->itemAt(pos * m_scale, track * 50 + 25);
638 ClipItem *CustomTrackView::getClipItemAt(GenTime pos, int track) {
639 return (ClipItem *) scene()->itemAt(pos.frames(m_document->fps()) * m_scale, track * 50 + 25);
642 void CustomTrackView::moveClip(const QPointF &startPos, const QPointF &endPos) {
643 ClipItem *item = getClipItemAt(startPos.x() + 1, startPos.y());
645 kDebug() << "---------------- ERROR, CANNOT find clip to move at: " << startPos.x() * m_scale * FRAME_SIZE + 1 << ", " << startPos.y() * 50 + 25;
648 kDebug() << "---------------- Move CLIP FROM: " << startPos.x() << ", END:" << endPos.x();
649 item->moveTo(endPos.x(), m_scale, (endPos.y() - startPos.y()) * 50, endPos.y());
650 m_document->renderer()->mltMoveClip(m_tracksCount - startPos.y(), m_tracksCount - endPos.y(), startPos.x(), endPos.x());
653 void CustomTrackView::resizeClip(const QPointF &startPos, const QPointF &endPos, bool resizeClipStart) {
655 if (resizeClipStart) offset = 1;
657 ClipItem *item = getClipItemAt(startPos.x() + offset, startPos.y());
659 kDebug() << "---------------- ERROR, CANNOT find clip to resize at: " << startPos;
662 qreal diff = endPos.x() - startPos.x();
663 if (resizeClipStart) {
664 m_document->renderer()->mltResizeClipStart(m_tracksCount - item->track(), item->endPos(), GenTime(endPos.x(), m_document->fps()), item->startPos(), item->cropStart() + GenTime(diff, m_document->fps()), item->cropStart() + GenTime(diff, m_document->fps()) + item->endPos() - GenTime(endPos.x(), m_document->fps()));
665 item->resizeStart(endPos.x(), m_scale);
667 m_document->renderer()->mltResizeClipEnd(m_tracksCount - item->track(), item->startPos(), item->cropStart(), item->cropStart() + GenTime(endPos.x(), m_document->fps()) - item->startPos());
668 item->resizeEnd(endPos.x(), m_scale);
670 m_document->renderer()->doRefresh();
673 double CustomTrackView::getSnapPointForPos(double pos) {
674 for (int i = 0; i < m_snapPoints.size(); ++i) {
675 if (abs(pos - m_snapPoints.at(i).frames(m_document->fps()) * m_scale) < 10) {
676 //kDebug()<<" FOUND SNAP POINT AT: "<<m_snapPoints.at(i)<<", current pos: "<<pos / m_scale;
677 return m_snapPoints.at(i).frames(m_document->fps()) * m_scale + 0.5;
679 if (m_snapPoints.at(i).frames(m_document->fps() * m_scale) > pos) break;
684 void CustomTrackView::updateSnapPoints(ClipItem *selected) {
685 m_snapPoints.clear();
687 if (selected) offset = selected->duration();
688 QList<QGraphicsItem *> itemList = items();
689 for (int i = 0; i < itemList.count(); i++) {
690 if (itemList.at(i)->type() == 70000 && itemList.at(i) != selected) {
691 ClipItem *item = (ClipItem *)itemList.at(i);
692 GenTime start = item->startPos();
693 GenTime end = item->endPos();
694 m_snapPoints.append(start);
695 m_snapPoints.append(end);
696 if (offset != GenTime()) {
697 if (start > offset) m_snapPoints.append(start - offset);
698 if (end > offset) m_snapPoints.append(end - offset);
703 //for (int i = 0; i < m_snapPoints.size(); ++i)
704 // kDebug() << "SNAP POINT: " << m_snapPoints.at(i).frames(25);
708 void CustomTrackView::setScale(double scaleFactor) {
709 //scale(scaleFactor, scaleFactor);
710 double pos = cursorPos() / m_scale;
711 m_scale = scaleFactor;
712 kDebug() << " HHHHHHHH SCALING: " << m_scale;
713 QList<QGraphicsItem *> itemList = items();
715 for (int i = 0; i < itemList.count(); i++) {
716 if (itemList.at(i)->type() == 70000) {
717 ClipItem *clip = (ClipItem *)itemList.at(i);
718 clip->setRect(clip->startPos().frames(m_document->fps()) * m_scale, clip->rect().y(), clip->duration().frames(m_document->fps()) * m_scale, clip->rect().height());
722 centerOn(QPointF(cursorPos(), 50));
723 scene()->setSceneRect(0, 0, (m_projectDuration + 500) * m_scale, scene()->sceneRect().height());
726 void CustomTrackView::drawBackground(QPainter * painter, const QRectF & rect) {
727 QRect rectInView = viewport()->rect();
728 rectInView.moveTo(horizontalScrollBar()->value(), verticalScrollBar()->value());
730 QColor base = palette().button().color();
731 painter->setClipRect(rect);
732 painter->drawLine(rectInView.left(), 0, rectInView.right(), 0);
733 for (uint i = 0; i < m_tracksCount;i++) {
734 painter->drawLine(rectInView.left(), 50 * (i + 1), rectInView.right(), 50 * (i + 1));
735 painter->drawText(QRectF(10, 50 * i, 100, 50 * i + 49), Qt::AlignLeft, i18n(" Track ") + QString::number(i + 1));
737 int lowerLimit = 50 * m_tracksCount + 1;
738 if (height() > lowerLimit)
739 painter->fillRect(QRectF(rectInView.left(), lowerLimit, rectInView.width(), height() - lowerLimit), QBrush(base));
742 void CustomTrackView::drawForeground ( QPainter * painter, const QRectF & rect )
744 //kDebug()<<"///// DRAWING FB: "<<rect.x()<<", width: "<<rect.width();
745 painter->fillRect(rect, QColor(50, rand() % 250,50,100));
746 painter->drawLine(m_cursorPos, rect.y(), m_cursorPos, rect.y() + rect.height());
749 #include "customtrackview.moc"