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 ***************************************************************************/
24 #include <QStyleOptionGraphicsItem>
25 #include <QGraphicsScene>
26 #include <QGraphicsView>
29 #include <QApplication>
34 #include "customtrackview.h"
36 #include "docclipbase.h"
37 #include "transition.h"
39 #include "kdenlivesettings.h"
42 ClipItem::ClipItem(DocClipBase *clip, ItemInfo info, double scale, double fps)
43 : AbstractClipItem(info, QRectF(), fps), m_clip(clip), m_resizeMode(NONE), m_grabPoint(0), m_maxTrack(0), m_hasThumbs(false), startThumbTimer(NULL), endThumbTimer(NULL), m_effectsCounter(1), audioThumbWasDrawn(false), m_opacity(1.0), m_timeLine(0), m_thumbsRequested(0), m_startFade(0), m_endFade(0), m_hover(false), m_selectedEffect(-1), m_speed(1.0) {
44 QRectF rect((double) info.startPos.frames(fps) * scale, (double)(info.track * KdenliveSettings::trackheight() + 1), (double)(info.endPos - info.startPos).frames(fps) * scale, (double)(KdenliveSettings::trackheight() - 1));
47 m_clipName = clip->name();
48 m_producer = clip->getId();
49 m_clipType = clip->clipType();
50 m_cropStart = info.cropStart;
51 m_maxDuration = clip->maxDuration();
53 audioThumbReady = clip->audioThumbCreated();
56 m_cropStart = xml.attribute("in", 0).toInt();
57 m_maxDuration = xml.attribute("duration", 0).toInt();
58 if (m_maxDuration == 0) m_maxDuration = xml.attribute("out", 0).toInt() - m_cropStart;
60 if (duration != -1) m_cropDuration = duration;
61 else m_cropDuration = m_maxDuration;*/
64 setFlags(QGraphicsItem::ItemClipsToShape | QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
65 setAcceptsHoverEvents(true);
66 connect(this , SIGNAL(prepareAudioThumb(double, QPainterPath, int, int, int)) , this, SLOT(slotPrepareAudioThumb(double, QPainterPath, int, int, int)));
68 setBrush(QColor(141, 166, 215));
69 if (m_clipType == VIDEO || m_clipType == AV || m_clipType == SLIDESHOW) {
71 connect(this, SIGNAL(getThumb(int, int)), clip->thumbProducer(), SLOT(extractImage(int, int)));
72 connect(clip->thumbProducer(), SIGNAL(thumbReady(int, QPixmap)), this, SLOT(slotThumbReady(int, QPixmap)));
73 connect(clip, SIGNAL(gotAudioData()), this, SLOT(slotGotAudioData()));
74 QTimer::singleShot(300, this, SLOT(slotFetchThumbs()));
76 startThumbTimer = new QTimer(this);
77 startThumbTimer->setSingleShot(true);
78 connect(startThumbTimer, SIGNAL(timeout()), this, SLOT(slotGetStartThumb()));
79 endThumbTimer = new QTimer(this);
80 endThumbTimer->setSingleShot(true);
81 connect(endThumbTimer, SIGNAL(timeout()), this, SLOT(slotGetEndThumb()));
82 } else if (m_clipType == COLOR) {
83 QString colour = clip->getProperty("colour");
84 colour = colour.replace(0, 2, "#");
85 setBrush(QColor(colour.left(7)));
86 } else if (m_clipType == IMAGE || m_clipType == TEXT) {
87 m_startPix = KThumb::getImage(KUrl(clip->getProperty("resource")), (int)(50 * KdenliveSettings::project_display_ratio()), 50);
88 m_endPix = m_startPix;
89 } else if (m_clipType == AUDIO) {
90 connect(clip, SIGNAL(gotAudioData()), this, SLOT(slotGotAudioData()));
95 ClipItem::~ClipItem() {
96 if (startThumbTimer) delete startThumbTimer;
97 if (endThumbTimer) delete endThumbTimer;
100 ClipItem *ClipItem::clone(double scale) const {
101 ClipItem *duplicate = new ClipItem(m_clip, info(), scale, m_fps);
102 duplicate->setEffectList(m_effectList);
106 void ClipItem::setEffectList(const EffectsList effectList) {
107 m_effectList = effectList;
108 m_effectNames = m_effectList.effectNames().join(" / ");
111 const EffectsList ClipItem::effectList() {
115 int ClipItem::selectedEffectIndex() const {
116 return m_selectedEffect;
119 void ClipItem::initEffect(QDomElement effect) {
120 // the kdenlive_ix int is used to identify an effect in mlt's playlist, should
122 if (effect.attribute("kdenlive_ix").toInt() == 0)
123 effect.setAttribute("kdenlive_ix", QString::number(effectsCounter()));
124 // init keyframes if required
125 QDomNodeList params = effect.elementsByTagName("parameter");
126 for (int i = 0; i < params.count(); i++) {
127 QDomElement e = params.item(i).toElement();
128 if (!e.isNull() && e.attribute("type") == "keyframe") {
129 QString def = e.attribute("default");
130 // Effect has a keyframe type parameter, we need to set the values
131 if (e.attribute("keyframes").isEmpty()) {
132 e.setAttribute("keyframes", QString::number(m_cropStart.frames(m_fps)) + ":" + def + ";" + QString::number((m_cropStart + m_cropDuration).frames(m_fps)) + ":" + def);
133 //kDebug() << "///// EFFECT KEYFRAMES INITED: " << e.attribute("keyframes");
140 void ClipItem::setKeyframes(const int ix, const QString keyframes) {
141 QDomElement effect = effectAt(ix);
142 if (effect.attribute("disabled") == "1") return;
143 QDomNodeList params = effect.elementsByTagName("parameter");
144 for (int i = 0; i < params.count(); i++) {
145 QDomElement e = params.item(i).toElement();
146 if (!e.isNull() && e.attribute("type") == "keyframe") {
147 e.setAttribute("keyframes", keyframes);
148 if (ix == m_selectedEffect) {
150 double max = e.attribute("max").toDouble();
151 double min = e.attribute("min").toDouble();
152 m_keyframeFactor = 100.0 / (max - min);
153 m_keyframeDefault = e.attribute("default").toDouble();
155 const QStringList keyframes = e.attribute("keyframes").split(";", QString::SkipEmptyParts);
156 foreach(const QString str, keyframes) {
157 int pos = str.section(":", 0, 0).toInt();
158 double val = str.section(":", 1, 1).toDouble();
159 m_keyframes[pos] = val;
170 void ClipItem::setSelectedEffect(const int ix) {
171 m_selectedEffect = ix;
172 QDomElement effect = effectAt(m_selectedEffect);
173 QDomNodeList params = effect.elementsByTagName("parameter");
174 if (effect.attribute("disabled") != "1")
175 for (int i = 0; i < params.count(); i++) {
176 QDomElement e = params.item(i).toElement();
177 if (!e.isNull() && e.attribute("type") == "keyframe") {
179 double max = e.attribute("max").toDouble();
180 double min = e.attribute("min").toDouble();
181 m_keyframeFactor = 100.0 / (max - min);
182 m_keyframeDefault = e.attribute("default").toDouble();
184 const QStringList keyframes = e.attribute("keyframes").split(";", QString::SkipEmptyParts);
185 foreach(const QString str, keyframes) {
186 int pos = str.section(":", 0, 0).toInt();
187 double val = str.section(":", 1, 1).toDouble();
188 m_keyframes[pos] = val;
194 if (!m_keyframes.isEmpty()) {
200 QString ClipItem::keyframes(const int index) {
202 QDomElement effect = effectAt(index);
203 QDomNodeList params = effect.elementsByTagName("parameter");
205 for (int i = 0; i < params.count(); i++) {
206 QDomElement e = params.item(i).toElement();
207 if (!e.isNull() && e.attribute("type") == "keyframe") {
208 result = e.attribute("keyframes");
215 void ClipItem::updateKeyframeEffect() {
216 // regenerate xml parameter from the clip keyframes
217 QDomElement effect = effectAt(m_selectedEffect);
218 if (effect.attribute("disabled") == "1") return;
219 QDomNodeList params = effect.elementsByTagName("parameter");
221 for (int i = 0; i < params.count(); i++) {
222 QDomElement e = params.item(i).toElement();
223 if (!e.isNull() && e.attribute("type") == "keyframe") {
225 if (m_keyframes.count() > 1) {
226 QMap<int, double>::const_iterator i = m_keyframes.constBegin();
229 while (i != m_keyframes.constEnd()) {
230 keyframes.append(QString::number(i.key()) + ":" + QString::number(i.value()) + ";");
234 // Effect has a keyframe type parameter, we need to set the values
235 //kDebug() << "::::::::::::::: SETTING EFFECT KEYFRAMES: " << keyframes;
236 e.setAttribute("keyframes", keyframes);
242 QDomElement ClipItem::selectedEffect() {
243 if (m_selectedEffect == -1 || m_effectList.isEmpty()) return QDomElement();
244 return effectAt(m_selectedEffect);
247 void ClipItem::resetThumbs() {
249 audioThumbCachePic.clear();
253 void ClipItem::refreshClip() {
254 m_maxDuration = m_clip->maxDuration();
255 if (m_clipType == VIDEO || m_clipType == AV || m_clipType == SLIDESHOW) slotFetchThumbs();
256 else if (m_clipType == COLOR) {
257 QString colour = m_clip->getProperty("colour");
258 colour = colour.replace(0, 2, "#");
259 setBrush(QColor(colour.left(7)));
260 } else if (m_clipType == IMAGE || m_clipType == TEXT) {
261 m_startPix = KThumb::getImage(KUrl(m_clip->getProperty("resource")), (int)(50 * KdenliveSettings::project_display_ratio()), 50);
262 m_endPix = m_startPix;
266 void ClipItem::slotFetchThumbs() {
267 m_thumbsRequested += 2;
268 emit getThumb((int)m_cropStart.frames(m_fps), (int)(m_cropStart + m_cropDuration).frames(m_fps));
271 void ClipItem::slotGetStartThumb() {
273 emit getThumb((int)m_cropStart.frames(m_fps), -1);
276 void ClipItem::slotGetEndThumb() {
278 emit getThumb(-1, (int)(m_cropStart + m_cropDuration).frames(m_fps));
281 void ClipItem::slotThumbReady(int frame, QPixmap pix) {
282 if (m_thumbsRequested == 0) return;
283 if (frame == m_cropStart.frames(m_fps)) {
285 QRectF r = boundingRect();
286 r.setRight(pix.width() + 2);
290 QRectF r = boundingRect();
291 r.setLeft(r.right() - pix.width() - 2);
297 void ClipItem::slotGotAudioData() {
298 audioThumbReady = true;
299 if (m_clipType == AV) {
300 QRectF r = boundingRect();
301 r.setTop(r.top() + r.height() / 2 - 1);
306 int ClipItem::type() const {
310 DocClipBase *ClipItem::baseClip() const {
314 QDomElement ClipItem::xml() const {
315 return m_clip->toXML();
318 int ClipItem::clipType() const {
322 QString ClipItem::clipName() const {
326 int ClipItem::clipProducer() const {
330 void ClipItem::flashClip() {
331 if (m_timeLine == 0) {
332 m_timeLine = new QTimeLine(750, this);
333 m_timeLine->setCurveShape(QTimeLine::EaseInOutCurve);
334 connect(m_timeLine, SIGNAL(valueChanged(qreal)), this, SLOT(animate(qreal)));
339 void ClipItem::animate(qreal value) {
340 QRectF r = boundingRect();
346 void ClipItem::paint(QPainter *painter,
347 const QStyleOptionGraphicsItem *option,
349 painter->setOpacity(m_opacity);
350 QBrush paintColor = brush();
351 if (isSelected()) paintColor = QBrush(QColor(79, 93, 121));
353 double scale = br.width() / m_cropDuration.frames(m_fps);
355 // kDebug()<<"/// EXPOSED RECT: "<<option->exposedRect.x()<<" X "<<option->exposedRect.right();
357 int startpixel = (int)option->exposedRect.x() - rect().x();
361 int endpixel = (int)option->exposedRect.right();
365 //painter->setRenderHints(QPainter::Antialiasing);
367 QPainterPath roundRectPathUpper = upperRectPart(br), roundRectPathLower = lowerRectPart(br);
368 painter->setClipRect(option->exposedRect);
370 // build path around clip
371 QPainterPath resultClipPath = roundRectPathUpper.united(roundRectPathLower);
372 painter->fillPath(resultClipPath, paintColor);
374 painter->setClipPath(resultClipPath, Qt::IntersectClip);
376 if (!m_startPix.isNull() && KdenliveSettings::videothumbnails()) {
377 if (m_clipType == IMAGE) {
378 painter->drawPixmap(QPointF(br.right() - m_startPix.width(), br.y()), m_startPix);
379 QLine l(br.right() - m_startPix.width(), br.y(), br.right() - m_startPix.width(), br.y() + br.height());
380 painter->drawLine(l);
382 painter->drawPixmap(QPointF(br.right() - m_endPix.width(), br.y()), m_endPix);
383 QLine l(br.right() - m_endPix.width(), br.y(), br.right() - m_endPix.width(), br.y() + br.height());
384 painter->drawLine(l);
387 painter->drawPixmap(QPointF(br.x(), br.y()), m_startPix);
388 QLine l2(br.x() + m_startPix.width(), br.y(), br.x() + m_startPix.width(), br.y() + br.height());
389 painter->drawLine(l2);
392 // draw audio thumbnails
393 if (KdenliveSettings::audiothumbnails() && ((m_clipType == AV && option->exposedRect.bottom() > (br.y() + br.height() / 2)) || m_clipType == AUDIO) && audioThumbReady) {
395 QPainterPath path = m_clipType == AV ? roundRectPathLower : resultClipPath;
396 if (m_clipType == AV) painter->fillPath(path, QBrush(QColor(200, 200, 200, 140)));
398 int channels = baseClip()->getProperty("channels").toInt();
399 if (scale != framePixelWidth)
400 audioThumbCachePic.clear();
401 emit prepareAudioThumb(scale, path, startpixel, endpixel + 200, channels);//200 more for less missing parts before repaint after scrolling
402 int cropLeft = (int)((m_cropStart).frames(m_fps) * scale);
403 for (int startCache = startpixel - startpixel % 100; startCache < endpixel + 300;startCache += 100) {
404 if (audioThumbCachePic.contains(startCache) && !audioThumbCachePic[startCache].isNull())
405 painter->drawPixmap((int)(roundRectPathUpper.united(roundRectPathLower).boundingRect().x() + startCache - cropLeft), (int)(path.boundingRect().y()), audioThumbCachePic[startCache]);
410 QList < CommentedTime > markers = baseClip()->commentedSnapMarkers();
411 QList < CommentedTime >::Iterator it = markers.begin();
414 const int markerwidth = 4;
416 markerBrush = QBrush(QColor(120, 120, 0, 100));
417 QPen pen = painter->pen();
418 pen.setColor(QColor(255, 255, 255, 200));
419 pen.setStyle(Qt::DotLine);
420 painter->setPen(pen);
421 for (; it != markers.end(); ++it) {
422 pos = (*it).time() - cropStart();
423 if (pos > GenTime()) {
424 if (pos > duration()) break;
425 framepos = scale * pos.frames(m_fps);
426 QLineF l(br.x() + framepos, br.y() + 5, br.x() + framepos, br.y() + br.height() - 5);
427 painter->drawLine(l);
428 if (KdenliveSettings::showmarkers()) {
429 const QRectF txtBounding = painter->boundingRect(br.x() + framepos + 1, br.y() + 10, br.width() - framepos - 2, br.height() - 10, Qt::AlignLeft | Qt::AlignTop, " " + (*it).comment() + " ");
431 path.addRoundedRect(txtBounding, 3, 3);
432 painter->fillPath(path, markerBrush);
433 painter->drawText(txtBounding, Qt::AlignCenter, (*it).comment());
435 //painter->fillRect(QRect(br.x() + framepos, br.y(), 10, br.height()), QBrush(QColor(0, 0, 0, 150)));
438 pen.setColor(Qt::black);
439 pen.setStyle(Qt::SolidLine);
440 painter->setPen(pen);
442 // draw start / end fades
445 fades = QBrush(QColor(200, 50, 50, 150));
446 } else fades = QBrush(QColor(200, 200, 200, 200));
448 if (m_startFade != 0) {
449 QPainterPath fadeInPath;
450 fadeInPath.moveTo(br.x() , br.y());
451 fadeInPath.lineTo(br.x() , br.bottom());
452 fadeInPath.lineTo(br.x() + m_startFade * scale, br.y());
453 fadeInPath.closeSubpath();
454 painter->fillPath(fadeInPath/*.intersected(resultClipPath)*/, fades);
456 QLineF l(br.x() + m_startFade * scale, br.y(), br.x(), br.bottom());
457 painter->drawLine(l);
460 if (m_endFade != 0) {
461 QPainterPath fadeOutPath;
462 fadeOutPath.moveTo(br.right(), br.y());
463 fadeOutPath.lineTo(br.right(), br.bottom());
464 fadeOutPath.lineTo(br.right() - m_endFade * scale, br.y());
465 fadeOutPath.closeSubpath();
466 painter->fillPath(fadeOutPath/*.intersected(resultClipPath)*/, fades);
468 QLineF l(br.right() - m_endFade * scale, br.y(), br.x() + br.width(), br.bottom());
469 painter->drawLine(l);
473 // Draw effects names
474 if (!m_effectNames.isEmpty() && br.width() > 30) {
475 QRectF txtBounding = painter->boundingRect(br, Qt::AlignLeft | Qt::AlignTop, m_effectNames);
476 txtBounding.setRight(txtBounding.right() + 15);
477 painter->setPen(Qt::white);
478 QBrush markerBrush(Qt::SolidPattern);
479 if (m_timeLine && m_timeLine->state() == QTimeLine::Running) {
480 qreal value = m_timeLine->currentValue();
481 txtBounding.setWidth(txtBounding.width() * value);
482 markerBrush.setColor(QColor(50 + 200 * (1.0 - value), 50, 50, 100 + 50 * value));
483 } else markerBrush.setColor(QColor(50, 50, 50, 150));
485 path.addRoundedRect(txtBounding, 4, 4);
486 painter->fillPath(path/*.intersected(resultClipPath)*/, markerBrush);
487 painter->drawText(txtBounding, Qt::AlignCenter, m_effectNames);
488 painter->setPen(Qt::black);
492 QRectF txtBounding = painter->boundingRect(br, Qt::AlignHCenter | Qt::AlignTop, " " + m_clipName + " ");
493 //painter->fillRect(txtBounding, QBrush(QColor(255, 255, 255, 150)));
494 painter->setPen(QColor(0, 0, 0, 180));
495 painter->drawText(txtBounding, Qt::AlignCenter, m_clipName);
496 txtBounding.translate(QPointF(1, 1));
497 painter->setPen(QColor(255, 255, 255, 255));
498 painter->drawText(txtBounding, Qt::AlignCenter, m_clipName);
499 // draw frame around clip
501 pen.setColor(Qt::red);
504 pen.setColor(Qt::black);
509 // draw effect or transition keyframes
510 if (br.width() > 20) drawKeyFrames(painter, option->exposedRect);
513 painter->setClipRect(option->exposedRect);
514 painter->setPen(pen);
515 //painter->setClipRect(option->exposedRect);
516 painter->drawPath(resultClipPath);
518 if (m_hover && br.width() > 30) {
519 painter->setBrush(QColor(180, 180, 50, 180)); //gradient);
521 // draw transitions handles
522 QPainterPath transitionHandle;
523 const int handle_size = 4;
524 transitionHandle.moveTo(0, 0);
525 transitionHandle.lineTo(handle_size, handle_size);
526 transitionHandle.lineTo(handle_size * 2, 0);
527 transitionHandle.lineTo(handle_size * 3, handle_size);
528 transitionHandle.lineTo(handle_size * 2, handle_size * 2);
529 transitionHandle.lineTo(handle_size * 3, handle_size * 3);
530 transitionHandle.lineTo(0, handle_size * 3);
531 transitionHandle.closeSubpath();
532 int pointy = (int)(br.y() + br.height() / 2);
533 int pointx1 = (int)(br.x() + 10);
534 int pointx2 = (int)(br.x() + br.width() - (10 + handle_size * 3));
536 painter->setPen(QPen(Qt::black));
537 painter->setBrush(QBrush(QColor(50, 50, 0)));
539 /*QRadialGradient gradient(pointx1 + 5, pointy + 5 , 5, 2, 2);
540 gradient.setColorAt(0.2, Qt::white);
541 gradient.setColorAt(0.8, Qt::yellow);
542 gradient.setColorAt(1, Qt::black);*/
545 painter->translate(pointx1, pointy);
546 painter->drawPath(transitionHandle); //Ellipse(0, 0 , 10, 10);
547 painter->translate(-pointx1, -pointy);
549 /* QRadialGradient gradient1(pointx2 + 5, pointy + 5 , 5, 2, 2);
550 gradient1.setColorAt(0.2, Qt::white);
551 gradient1.setColorAt(0.8, Qt::yellow);
552 gradient1.setColorAt(1, Qt::black);
553 painter->setBrush(gradient1);*/
554 painter->translate(pointx2, pointy);
557 //painter->setMatrix(m);
558 painter->drawPath(transitionHandle); // Ellipse(0, 0, 10, 10);
559 //painter->setMatrix(m);
560 painter->translate(- pointx2, -pointy);
565 OPERATIONTYPE ClipItem::operationMode(QPointF pos, double scale) {
567 m_editedKeyframe = mouseOverKeyFrames(pos);
568 if (m_editedKeyframe != -1) return KEYFRAME;
570 if (qAbs((int)(pos.x() - (rect().x() + scale * m_startFade))) < 6 && qAbs((int)(pos.y() - rect().y())) < 6) {
571 if (m_startFade == 0) setToolTip(i18n("Add audio fade"));
572 else setToolTip(i18n("Audio fade duration: %1s", GenTime(m_startFade, m_fps).seconds()));
574 } else if (qAbs((int)(pos.x() - rect().x())) < 6) {
575 setToolTip(i18n("Crop from start: %1s", cropStart().seconds()));
577 } else if (qAbs((int)(pos.x() - (rect().x() + rect().width() - scale * m_endFade))) < 6 && qAbs((int)(pos.y() - rect().y())) < 6) {
578 if (m_endFade == 0) setToolTip(i18n("Add audio fade"));
579 else setToolTip(i18n("Audio fade duration: %1s", GenTime(m_endFade, m_fps).seconds()));
581 } else if (qAbs((int)(pos.x() - (rect().x() + rect().width()))) < 6) {
582 setToolTip(i18n("Clip duration: %1s", duration().seconds()));
584 } else if (qAbs((int)(pos.x() - (rect().x() + 16))) < 10 && qAbs((int)(pos.y() - (rect().y() + rect().height() / 2 + 5))) < 8) {
585 setToolTip(i18n("Add transition"));
586 return TRANSITIONSTART;
587 } else if (qAbs((int)(pos.x() - (rect().x() + rect().width() - 21))) < 10 && qAbs((int)(pos.y() - (rect().y() + rect().height() / 2 + 5))) < 8) {
588 setToolTip(i18n("Add transition"));
589 return TRANSITIONEND;
591 setToolTip(QString());
595 QList <GenTime> ClipItem::snapMarkers() const {
596 QList < GenTime > snaps;
597 QList < GenTime > markers = baseClip()->snapMarkers();
601 for (int i = 0; i < markers.size(); i++) {
602 pos = markers.at(i) - cropStart();
603 if (pos > GenTime()) {
604 if (pos > duration()) break;
605 else snaps.append(pos + startPos());
611 QList <CommentedTime> ClipItem::commentedSnapMarkers() const {
612 QList < CommentedTime > snaps;
613 QList < CommentedTime > markers = baseClip()->commentedSnapMarkers();
617 for (int i = 0; i < markers.size(); i++) {
618 pos = markers.at(i).time() - cropStart();
619 if (pos > GenTime()) {
620 if (pos > duration()) break;
621 else snaps.append(CommentedTime(pos + startPos(), markers.at(i).comment()));
627 void ClipItem::slotPrepareAudioThumb(double pixelForOneFrame, QPainterPath path, int startpixel, int endpixel, int channels) {
629 QRectF re = path.boundingRect();
630 //kDebug() << "// PREP AUDIO THMB FRMO : " << startpixel << ", to: " << endpixel;
631 //if ( (!audioThumbWasDrawn || framePixelWidth!=pixelForOneFrame ) && !baseClip()->audioFrameChache.isEmpty()){
633 for (int startCache = startpixel - startpixel % 100;startCache + 100 < endpixel ;startCache += 100) {
634 //kDebug() << "creating " << startCache;
635 //if (framePixelWidth!=pixelForOneFrame ||
636 if (framePixelWidth == pixelForOneFrame && audioThumbCachePic.contains(startCache))
638 if (audioThumbCachePic[startCache].isNull() || framePixelWidth != pixelForOneFrame) {
639 audioThumbCachePic[startCache] = QPixmap(100, (int)(re.height()));
640 audioThumbCachePic[startCache].fill(QColor(200, 200, 200, 0));
642 bool fullAreaDraw = pixelForOneFrame < 10;
643 QMap<int, QPainterPath > positiveChannelPaths;
644 QMap<int, QPainterPath > negativeChannelPaths;
645 QPainter pixpainter(&audioThumbCachePic[startCache]);
647 audiopen.setWidth(0);
648 pixpainter.setPen(audiopen);
649 //pixpainter.setRenderHint(QPainter::Antialiasing,true);
650 //pixpainter.drawLine(0,0,100,re.height());
651 int channelHeight = audioThumbCachePic[startCache].height() / channels;
653 for (int i = 0;i < channels;i++) {
655 positiveChannelPaths[i].moveTo(0, channelHeight*i + channelHeight / 2);
656 negativeChannelPaths[i].moveTo(0, channelHeight*i + channelHeight / 2);
659 for (int samples = 0;samples <= 100;samples++) {
660 double frame = (double)(samples + startCache - 0) / pixelForOneFrame;
661 int sample = (int)((frame - (int)(frame)) * 20); // AUDIO_FRAME_SIZE
662 if (frame < 0 || sample < 0 || sample > 19)
664 QMap<int, QByteArray> frame_channel_data = baseClip()->audioFrameChache[(int)frame];
666 for (int channel = 0;channel < channels && frame_channel_data[channel].size() > 0;channel++) {
668 int y = channelHeight * channel + channelHeight / 2;
669 int delta = (int)(frame_channel_data[channel][sample] - 127 / 2) * channelHeight / 64;
671 positiveChannelPaths[channel].lineTo(samples, 0.1 + y + qAbs(delta));
672 negativeChannelPaths[channel].lineTo(samples, 0.1 + y - qAbs(delta));
674 positiveChannelPaths[channel].lineTo(samples, 0.1 + y + delta);
675 negativeChannelPaths[channel].lineTo(samples, 0.1 + y - delta);
678 for (int channel = 0;channel < channels ;channel++)
679 if (fullAreaDraw && samples == 100) {
680 positiveChannelPaths[channel].lineTo(samples, channelHeight*channel + channelHeight / 2);
681 negativeChannelPaths[channel].lineTo(samples, channelHeight*channel + channelHeight / 2);
682 positiveChannelPaths[channel].lineTo(0, channelHeight*channel + channelHeight / 2);
683 negativeChannelPaths[channel].lineTo(0, channelHeight*channel + channelHeight / 2);
687 if (m_clipType != AV) pixpainter.setBrush(QBrush(QColor(200, 200, 100)));
689 pixpainter.setPen(QPen(QColor(0, 0, 0)));
690 pixpainter.setBrush(QBrush(QColor(60, 60, 60)));
692 for (int i = 0;i < channels;i++) {
694 //pixpainter.fillPath(positiveChannelPaths[i].united(negativeChannelPaths[i]),QBrush(Qt::SolidPattern));//or singleif looks better
695 pixpainter.drawPath(positiveChannelPaths[i].united(negativeChannelPaths[i]));//or singleif looks better
697 pixpainter.drawPath(positiveChannelPaths[i]);
700 //audioThumbWasDrawn=true;
701 framePixelWidth = pixelForOneFrame;
706 uint ClipItem::fadeIn() const {
710 uint ClipItem::fadeOut() const {
715 void ClipItem::setFadeIn(int pos, double scale) {
716 int oldIn = m_startFade;
717 if (pos < 0) pos = 0;
718 if (pos > m_cropDuration.frames(m_fps)) pos = (int)(m_cropDuration.frames(m_fps) / 2);
720 update(rect().x(), rect().y(), qMax(oldIn, pos) * scale, rect().height());
723 void ClipItem::setFadeOut(int pos, double scale) {
724 int oldOut = m_endFade;
725 if (pos < 0) pos = 0;
726 if (pos > m_cropDuration.frames(m_fps)) pos = (int)(m_cropDuration.frames(m_fps) / 2);
728 update(rect().x() + rect().width() - qMax(oldOut, pos) * scale, rect().y(), pos * scale, rect().height());
733 void ClipItem::mousePressEvent(QGraphicsSceneMouseEvent * event) {
734 /*m_resizeMode = operationMode(event->pos());
735 if (m_resizeMode == MOVE) {
736 m_maxTrack = scene()->sceneRect().height();
737 m_grabPoint = (int) (event->pos().x() - rect().x());
739 QGraphicsRectItem::mousePressEvent(event);
743 void ClipItem::mouseReleaseEvent(QGraphicsSceneMouseEvent * event) {
745 QGraphicsRectItem::mouseReleaseEvent(event);
749 void ClipItem::hoverEnterEvent(QGraphicsSceneHoverEvent *) {
751 QRectF r = boundingRect();
752 qreal width = qMin(25.0, r.width());
753 update(r.x(), r.y(), width, r.height());
754 update(r.right() - width, r.y(), width, r.height());
758 void ClipItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *) {
760 QRectF r = boundingRect();
761 qreal width = qMin(25.0, r.width());
762 update(r.x(), r.y(), width, r.height());
763 update(r.right() - width, r.y(), width, r.height());
766 void ClipItem::resizeStart(int posx, double scale) {
767 const int previous = cropStart().frames(m_fps);
768 AbstractClipItem::resizeStart(posx, scale);
769 checkEffectsKeyframesPos(previous, cropStart().frames(m_fps), true);
770 if (m_hasThumbs && KdenliveSettings::videothumbnails()) startThumbTimer->start(100);
773 void ClipItem::resizeEnd(int posx, double scale) {
774 const int previous = (cropStart() + duration()).frames(m_fps);
775 AbstractClipItem::resizeEnd(posx, scale);
776 checkEffectsKeyframesPos(previous, (cropStart() + duration()).frames(m_fps), false);
777 if (m_hasThumbs && KdenliveSettings::videothumbnails()) endThumbTimer->start(100);
781 void ClipItem::checkEffectsKeyframesPos(const int previous, const int current, bool fromStart) {
782 for (int i = 0; i < m_effectList.size(); i++) {
783 QDomElement effect = m_effectList.at(i);
784 QDomNodeList params = effect.elementsByTagName("parameter");
785 for (int j = 0; j < params.count(); j++) {
786 QDomElement e = params.item(i).toElement();
787 if (e.attribute("type") == "keyframe") {
788 // parse keyframes and adjust values
789 const QStringList keyframes = e.attribute("keyframes").split(";", QString::SkipEmptyParts);
790 QMap <int, double> kfr;
791 foreach(const QString str, keyframes) {
792 int pos = str.section(":", 0, 0).toInt();
793 double val = str.section(":", 1, 1).toDouble();
794 if (pos == previous) kfr[current] = val;
796 if (fromStart && pos >= current) kfr[pos] = val;
797 else if (!fromStart && pos <= current) kfr[pos] = val;
801 QMap<int, double>::const_iterator k = kfr.constBegin();
802 while (k != kfr.constEnd()) {
803 newkfr.append(QString::number(k.key()) + ":" + QString::number(k.value()) + ";");
806 e.setAttribute("keyframes", newkfr);
811 if (m_selectedEffect >= 0) setSelectedEffect(m_selectedEffect);
816 /*void ClipItem::mouseMoveEvent(QGraphicsSceneMouseEvent * event) {
819 int ClipItem::effectsCounter() {
820 return m_effectsCounter++;
823 int ClipItem::effectsCount() {
824 return m_effectList.size();
827 QStringList ClipItem::effectNames() {
828 return m_effectList.effectNames();
831 QDomElement ClipItem::effectAt(int ix) {
832 if (ix > m_effectList.count() - 1 || ix < 0) return QDomElement();
833 return m_effectList.at(ix);
836 void ClipItem::setEffectAt(int ix, QDomElement effect) {
837 kDebug() << "CHange EFFECT AT: " << ix << ", CURR: " << m_effectList.at(ix).attribute("tag") << ", NEW: " << effect.attribute("tag");
838 m_effectList.insert(ix, effect);
839 m_effectList.removeAt(ix + 1);
840 m_effectNames = m_effectList.effectNames().join(" / ");
841 if (effect.attribute("id") == "fadein" || effect.attribute("id") == "fadeout") update(boundingRect());
843 QRectF r = boundingRect();
849 QMap <QString, QString> ClipItem::addEffect(QDomElement effect, bool animate) {
850 QMap <QString, QString> effectParams;
851 bool needRepaint = false;
853 doc.appendChild(doc.importNode(effect, true));
854 kDebug() << "/////// CLIP ADD EFFECT: "<< doc.toString();*/
855 m_effectList.append(effect);
856 effectParams["tag"] = effect.attribute("tag");
857 QString effectId = effect.attribute("id");
858 if (effectId.isEmpty()) effectId = effect.attribute("tag");
859 effectParams["id"] = effectId;
860 effectParams["kdenlive_ix"] = effect.attribute("kdenlive_ix");
861 QString state = effect.attribute("disabled");
862 if (!state.isEmpty()) effectParams["disabled"] = state;
863 QDomNodeList params = effect.elementsByTagName("parameter");
865 for (int i = 0; i < params.count(); i++) {
866 QDomElement e = params.item(i).toElement();
868 if (e.attribute("type") == "keyframe") {
869 effectParams["keyframes"] = e.attribute("keyframes");
870 effectParams["min"] = e.attribute("min");
871 effectParams["max"] = e.attribute("max");
872 effectParams["factor"] = e.attribute("factor", "1");
873 effectParams["starttag"] = e.attribute("starttag", "start");
874 effectParams["endtag"] = e.attribute("endtag", "end");
877 double f = e.attribute("factor", "1").toDouble();
880 effectParams[e.attribute("name")] = e.attribute("value");
881 // check if it is a fade effect
882 if (effectId == "fadein") {
884 if (e.attribute("name") == "out") fade += e.attribute("value").toInt();
885 else if (e.attribute("name") == "in") fade -= e.attribute("value").toInt();
886 } else if (effectId == "fadeout") {
888 if (e.attribute("name") == "out") fade -= e.attribute("value").toInt();
889 else if (e.attribute("name") == "in") fade += e.attribute("value").toInt();
892 effectParams[e.attribute("name")] = QString::number(effectParams[e.attribute("name")].toDouble() / f);
896 m_effectNames = m_effectList.effectNames().join(" / ");
897 if (fade > 0) m_startFade = fade;
898 else if (fade < 0) m_endFade = -fade;
899 if (needRepaint) update(boundingRect());
902 } else if (!needRepaint) {
903 QRectF r = boundingRect();
907 if (m_selectedEffect == -1) {
908 m_selectedEffect = 0;
909 setSelectedEffect(m_selectedEffect);
914 QMap <QString, QString> ClipItem::getEffectArgs(QDomElement effect) {
915 QMap <QString, QString> effectParams;
916 effectParams["tag"] = effect.attribute("tag");
917 effectParams["kdenlive_ix"] = effect.attribute("kdenlive_ix");
918 effectParams["id"] = effect.attribute("id");
919 QString state = effect.attribute("disabled");
920 if (!state.isEmpty()) effectParams["disabled"] = state;
921 QDomNodeList params = effect.elementsByTagName("parameter");
922 for (int i = 0; i < params.count(); i++) {
923 QDomElement e = params.item(i).toElement();
924 kDebug() << "/ / / /SENDING EFFECT PARAM: " << e.attribute("type") << ", NAME_ " << e.attribute("tag");
925 if (e.attribute("type") == "keyframe") {
926 kDebug() << "/ / / /SENDING KEYFR EFFECT TYPE";
927 effectParams["keyframes"] = e.attribute("keyframes");
928 effectParams["max"] = e.attribute("max");
929 effectParams["min"] = e.attribute("min");
930 effectParams["factor"] = e.attribute("factor", "1");
931 effectParams["starttag"] = e.attribute("starttag", "start");
932 effectParams["endtag"] = e.attribute("endtag", "end");
933 } else if (e.attribute("namedesc").contains(";")) {
934 QString format = e.attribute("format");
935 QStringList separators = format.split("%d", QString::SkipEmptyParts);
936 QStringList values = e.attribute("value").split(QRegExp("[,:;x]"));
938 QTextStream txtNeu(&neu);
939 if (values.size() > 0)
940 txtNeu << (int)values[0].toDouble();
941 for (int i = 0;i < separators.size() && i + 1 < values.size();i++) {
942 txtNeu << separators[i];
943 txtNeu << (int)(values[i+1].toDouble());
945 effectParams["start"] = neu;
947 if (e.attribute("factor", "1") != "1")
948 effectParams[e.attribute("name")] = QString::number(e.attribute("value").toDouble() / e.attribute("factor").toDouble());
949 else effectParams[e.attribute("name")] = e.attribute("value");
955 void ClipItem::deleteEffect(QString index) {
956 bool needRepaint = false;
957 for (int i = 0; i < m_effectList.size(); ++i) {
958 if (m_effectList.at(i).attribute("kdenlive_ix") == index) {
959 if (m_effectList.at(i).attribute("id") == "fadein") {
962 } else if (m_effectList.at(i).attribute("id") == "fadeout") {
966 m_effectList.removeAt(i);
970 m_effectNames = m_effectList.effectNames().join(" / ");
971 if (needRepaint) update(boundingRect());
975 double ClipItem::speed() const {
979 void ClipItem::setSpeed(const double speed) {
981 if (m_speed == 1.0) m_clipName = baseClip()->name();
982 else m_clipName = baseClip()->name() + " - " + QString::number(speed * 100, 'f', 0) + "%";
987 void ClipItem::dropEvent(QGraphicsSceneDragDropEvent * event) {
988 QString effects = QString(event->mimeData()->data("kdenlive/effectslist"));
990 doc.setContent(effects, true);
991 QDomElement e = doc.documentElement();
992 CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
993 if (view) view->slotAddEffect(e, m_startPos, track());
997 void ClipItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event) {
998 event->setAccepted(event->mimeData()->hasFormat("kdenlive/effectslist"));
1001 void ClipItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event) {
1004 void ClipItem::addTransition(Transition* t) {
1005 m_transitionsList.append(t);
1006 CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
1008 QDomElement e = doc.documentElement();
1009 //if (view) view->slotAddTransition(this, t->toXML() , t->startPos(), track());
1013 void CustomTrackView::mousePressEvent ( QMouseEvent * event )
1015 int pos = event->x();
1016 if (event->modifiers() == Qt::ControlModifier)
1017 setDragMode(QGraphicsView::ScrollHandDrag);
1018 else if (event->modifiers() == Qt::ShiftModifier)
1019 setDragMode(QGraphicsView::RubberBandDrag);
1021 QGraphicsItem * item = itemAt(event->pos());
1024 else emit cursorMoved((int) mapToScene(event->x(), 0).x());
1027 QGraphicsView::mousePressEvent(event);
1030 void CustomTrackView::mouseReleaseEvent ( QMouseEvent * event )
1032 QGraphicsView::mouseReleaseEvent(event);
1033 setDragMode(QGraphicsView::NoDrag);
1037 #include "clipitem.moc"