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(2), m_startFade(0), m_endFade(0), m_hover(false), m_selectedEffect(-1), m_speed(1.0), framePixelWidth(0) {
44 setRect(0, 0, (qreal)(info.endPos - info.startPos).frames(fps) * scale - .5, (qreal)(KdenliveSettings::trackheight() - 1));
45 setPos((qreal) info.startPos.frames(fps) * scale, (qreal)(info.track * KdenliveSettings::trackheight()) + 1);
46 kDebug() << "// ADDing CLIP TRK HGTH: " << KdenliveSettings::trackheight();
48 m_clipName = clip->name();
49 m_producer = clip->getId();
50 m_clipType = clip->clipType();
51 m_cropStart = info.cropStart;
52 m_maxDuration = clip->maxDuration();
54 audioThumbReady = clip->audioThumbCreated();
57 m_cropStart = xml.attribute("in", 0).toInt();
58 m_maxDuration = xml.attribute("duration", 0).toInt();
59 if (m_maxDuration == 0) m_maxDuration = xml.attribute("out", 0).toInt() - m_cropStart;
61 if (duration != -1) m_cropDuration = duration;
62 else m_cropDuration = m_maxDuration;*/
65 setFlags(QGraphicsItem::ItemClipsToShape | QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
66 setAcceptsHoverEvents(true);
67 connect(this , SIGNAL(prepareAudioThumb(double, QPainterPath, int, int, int)) , this, SLOT(slotPrepareAudioThumb(double, QPainterPath, int, int, int)));
69 setBrush(QColor(141, 166, 215));
70 if (m_clipType == VIDEO || m_clipType == AV || m_clipType == SLIDESHOW) {
72 connect(this, SIGNAL(getThumb(int, int)), clip->thumbProducer(), SLOT(extractImage(int, int)));
73 connect(clip->thumbProducer(), SIGNAL(thumbReady(int, QPixmap)), this, SLOT(slotThumbReady(int, QPixmap)));
74 connect(clip, SIGNAL(gotAudioData()), this, SLOT(slotGotAudioData()));
75 QTimer::singleShot(200, this, SLOT(slotFetchThumbs()));
77 startThumbTimer = new QTimer(this);
78 startThumbTimer->setSingleShot(true);
79 connect(startThumbTimer, SIGNAL(timeout()), this, SLOT(slotGetStartThumb()));
80 endThumbTimer = new QTimer(this);
81 endThumbTimer->setSingleShot(true);
82 connect(endThumbTimer, SIGNAL(timeout()), this, SLOT(slotGetEndThumb()));
83 } else if (m_clipType == COLOR) {
84 QString colour = clip->getProperty("colour");
85 colour = colour.replace(0, 2, "#");
86 setBrush(QColor(colour.left(7)));
87 } else if (m_clipType == IMAGE || m_clipType == TEXT) {
88 m_startPix = KThumb::getImage(KUrl(clip->getProperty("resource")), (int)(50 * KdenliveSettings::project_display_ratio()), 50);
89 m_endPix = m_startPix;
90 } else if (m_clipType == AUDIO) {
91 connect(clip, SIGNAL(gotAudioData()), this, SLOT(slotGotAudioData()));
96 ClipItem::~ClipItem() {
97 if (startThumbTimer) delete startThumbTimer;
98 if (endThumbTimer) delete endThumbTimer;
99 if (m_timeLine) m_timeLine;
102 ClipItem *ClipItem::clone(double scale, ItemInfo info) const {
103 ClipItem *duplicate = new ClipItem(m_clip, info, scale, m_fps);
104 if (info.cropStart == cropStart()) duplicate->slotThumbReady(info.cropStart.frames(m_fps), m_startPix);
105 if (info.cropStart + (info.endPos - info.startPos) == m_cropStart + m_cropDuration) duplicate->slotThumbReady((m_cropStart + m_cropDuration).frames(m_fps) - 1, m_endPix);
106 kDebug() << "// CLoning clip: " << (info.cropStart + (info.endPos - info.startPos)).frames(m_fps) << ", CURRENT end: " << (cropStart() + duration()).frames(m_fps);
107 duplicate->setEffectList(m_effectList);
108 duplicate->setSpeed(m_speed);
112 void ClipItem::setEffectList(const EffectsList effectList) {
113 m_effectList = effectList;
114 m_effectNames = m_effectList.effectNames().join(" / ");
117 const EffectsList ClipItem::effectList() {
121 int ClipItem::selectedEffectIndex() const {
122 return m_selectedEffect;
125 void ClipItem::initEffect(QDomElement effect) {
126 // the kdenlive_ix int is used to identify an effect in mlt's playlist, should
128 if (effect.attribute("kdenlive_ix").toInt() == 0)
129 effect.setAttribute("kdenlive_ix", QString::number(effectsCounter()));
130 // init keyframes if required
131 QDomNodeList params = effect.elementsByTagName("parameter");
132 for (int i = 0; i < params.count(); i++) {
133 QDomElement e = params.item(i).toElement();
134 if (!e.isNull() && e.attribute("type") == "keyframe") {
135 QString def = e.attribute("default");
136 // Effect has a keyframe type parameter, we need to set the values
137 if (e.attribute("keyframes").isEmpty()) {
138 e.setAttribute("keyframes", QString::number(m_cropStart.frames(m_fps)) + ":" + def + ";" + QString::number((m_cropStart + m_cropDuration).frames(m_fps)) + ":" + def);
139 //kDebug() << "///// EFFECT KEYFRAMES INITED: " << e.attribute("keyframes");
146 void ClipItem::setKeyframes(const int ix, const QString keyframes) {
147 QDomElement effect = effectAt(ix);
148 if (effect.attribute("disabled") == "1") return;
149 QDomNodeList params = effect.elementsByTagName("parameter");
150 for (int i = 0; i < params.count(); i++) {
151 QDomElement e = params.item(i).toElement();
152 if (!e.isNull() && e.attribute("type") == "keyframe") {
153 e.setAttribute("keyframes", keyframes);
154 if (ix == m_selectedEffect) {
156 double max = e.attribute("max").toDouble();
157 double min = e.attribute("min").toDouble();
158 m_keyframeFactor = 100.0 / (max - min);
159 m_keyframeDefault = e.attribute("default").toDouble();
161 const QStringList keyframes = e.attribute("keyframes").split(";", QString::SkipEmptyParts);
162 foreach(const QString str, keyframes) {
163 int pos = str.section(":", 0, 0).toInt();
164 double val = str.section(":", 1, 1).toDouble();
165 m_keyframes[pos] = val;
176 void ClipItem::setSelectedEffect(const int ix) {
177 m_selectedEffect = ix;
178 QDomElement effect = effectAt(m_selectedEffect);
179 QDomNodeList params = effect.elementsByTagName("parameter");
180 if (effect.attribute("disabled") != "1")
181 for (int i = 0; i < params.count(); i++) {
182 QDomElement e = params.item(i).toElement();
183 if (!e.isNull() && e.attribute("type") == "keyframe") {
185 double max = e.attribute("max").toDouble();
186 double min = e.attribute("min").toDouble();
187 m_keyframeFactor = 100.0 / (max - min);
188 m_keyframeDefault = e.attribute("default").toDouble();
190 const QStringList keyframes = e.attribute("keyframes").split(";", QString::SkipEmptyParts);
191 foreach(const QString str, keyframes) {
192 int pos = str.section(":", 0, 0).toInt();
193 double val = str.section(":", 1, 1).toDouble();
194 m_keyframes[pos] = val;
200 if (!m_keyframes.isEmpty()) {
206 QString ClipItem::keyframes(const int index) {
208 QDomElement effect = effectAt(index);
209 QDomNodeList params = effect.elementsByTagName("parameter");
211 for (int i = 0; i < params.count(); i++) {
212 QDomElement e = params.item(i).toElement();
213 if (!e.isNull() && e.attribute("type") == "keyframe") {
214 result = e.attribute("keyframes");
221 void ClipItem::updateKeyframeEffect() {
222 // regenerate xml parameter from the clip keyframes
223 QDomElement effect = effectAt(m_selectedEffect);
224 if (effect.attribute("disabled") == "1") return;
225 QDomNodeList params = effect.elementsByTagName("parameter");
227 for (int i = 0; i < params.count(); i++) {
228 QDomElement e = params.item(i).toElement();
229 if (!e.isNull() && e.attribute("type") == "keyframe") {
231 if (m_keyframes.count() > 1) {
232 QMap<int, double>::const_iterator i = m_keyframes.constBegin();
235 while (i != m_keyframes.constEnd()) {
236 keyframes.append(QString::number(i.key()) + ":" + QString::number(i.value()) + ";");
240 // Effect has a keyframe type parameter, we need to set the values
241 //kDebug() << "::::::::::::::: SETTING EFFECT KEYFRAMES: " << keyframes;
242 e.setAttribute("keyframes", keyframes);
248 QDomElement ClipItem::selectedEffect() {
249 if (m_selectedEffect == -1 || m_effectList.isEmpty()) return QDomElement();
250 return effectAt(m_selectedEffect);
253 void ClipItem::resetThumbs() {
254 m_startPix = QPixmap();
255 m_endPix = QPixmap();
256 m_thumbsRequested = 2;
258 audioThumbCachePic.clear();
262 void ClipItem::refreshClip() {
263 m_maxDuration = m_clip->maxDuration();
264 if (m_clipType == VIDEO || m_clipType == AV || m_clipType == SLIDESHOW) slotFetchThumbs();
265 else if (m_clipType == COLOR) {
266 QString colour = m_clip->getProperty("colour");
267 colour = colour.replace(0, 2, "#");
268 setBrush(QColor(colour.left(7)));
269 } else if (m_clipType == IMAGE || m_clipType == TEXT) {
270 m_startPix = KThumb::getImage(KUrl(m_clip->getProperty("resource")), (int)(50 * KdenliveSettings::project_display_ratio()), 50);
271 m_endPix = m_startPix;
275 void ClipItem::slotFetchThumbs() {
276 if (m_endPix.isNull() && m_startPix.isNull()) {
277 emit getThumb((int)m_cropStart.frames(m_fps), (int)(m_cropStart + m_cropDuration).frames(m_fps) - 1);
279 if (m_endPix.isNull()) slotGetEndThumb();
280 if (m_startPix.isNull()) slotGetStartThumb();
284 void ClipItem::slotGetStartThumb() {
285 emit getThumb((int)m_cropStart.frames(m_fps), -1);
288 void ClipItem::slotGetEndThumb() {
289 emit getThumb(-1, (int)(m_cropStart + m_cropDuration).frames(m_fps) - 1);
292 void ClipItem::slotThumbReady(int frame, QPixmap pix) {
293 if (frame == m_cropStart.frames(m_fps)) {
295 QRectF r = sceneBoundingRect();
296 r.setRight(pix.width() + 2);
299 } else if (frame == (m_cropStart + m_cropDuration).frames(m_fps) - 1) {
301 QRectF r = sceneBoundingRect();
302 r.setLeft(r.right() - pix.width() - 2);
306 if (m_thumbsRequested == 0) {
307 // Ok, we have out start and end thumbnails...
308 disconnect(m_clip->thumbProducer(), SIGNAL(thumbReady(int, QPixmap)), this, SLOT(slotThumbReady(int, QPixmap)));
312 void ClipItem::slotGotAudioData() {
313 audioThumbReady = true;
314 if (m_clipType == AV) {
315 QRectF r = boundingRect();
316 r.setTop(r.top() + r.height() / 2 - 1);
321 int ClipItem::type() const {
325 DocClipBase *ClipItem::baseClip() const {
329 QDomElement ClipItem::xml() const {
330 return m_clip->toXML();
333 int ClipItem::clipType() const {
337 QString ClipItem::clipName() const {
341 int ClipItem::clipProducer() const {
345 void ClipItem::flashClip() {
346 if (m_timeLine == 0) {
347 m_timeLine = new QTimeLine(750, this);
348 m_timeLine->setCurveShape(QTimeLine::EaseInOutCurve);
349 connect(m_timeLine, SIGNAL(valueChanged(qreal)), this, SLOT(animate(qreal)));
354 void ClipItem::animate(qreal value) {
355 QRectF r = boundingRect();
361 void ClipItem::paint(QPainter *painter,
362 const QStyleOptionGraphicsItem *option,
364 painter->setOpacity(m_opacity);
365 QBrush paintColor = brush();
366 if (isSelected()) paintColor = QBrush(QColor(79, 93, 121));
368 const double itemWidth = br.width();
369 const double itemHeight = br.height();
370 kDebug() << "/// ITEM RECT: " << br << ", EPXOSED: " << option->exposedRect;
371 double scale = itemWidth / (double) m_cropDuration.frames(m_fps);
373 // kDebug()<<"/// EXPOSED RECT: "<<option->exposedRect.x()<<" X "<<option->exposedRect.right();
375 double startpixel = option->exposedRect.x(); // - pos().x();
379 double endpixel = option->exposedRect.right();
383 //painter->setRenderHints(QPainter::Antialiasing);
385 QPainterPath roundRectPathUpper = upperRectPart(br), roundRectPathLower = lowerRectPart(br);
386 painter->setClipRect(option->exposedRect);
388 // build path around clip
389 QPainterPath resultClipPath = roundRectPathUpper.united(roundRectPathLower);
390 painter->fillPath(resultClipPath, paintColor);
392 painter->setClipPath(resultClipPath, Qt::IntersectClip);
395 if (KdenliveSettings::videothumbnails()) {
396 if (m_clipType == IMAGE && !m_startPix.isNull()) {
397 painter->drawPixmap(QPointF(itemWidth - m_startPix.width(), 0), m_startPix);
398 QLine l(itemWidth - m_startPix.width(), 0, itemWidth - m_startPix.width(), itemHeight);
399 painter->drawLine(l);
401 else if (!m_endPix.isNull()) {
402 painter->drawPixmap(QPointF(itemWidth - m_endPix.width(), 0), m_endPix);
403 QLine l(itemWidth - m_endPix.width(), 0, itemWidth - m_endPix.width(), itemHeight);
404 painter->drawLine(l);
406 if (!m_startPix.isNull()) {
407 painter->drawPixmap(QPointF(0, 0), m_startPix);
408 QLine l2(m_startPix.width(), 0, 0 + m_startPix.width(), itemHeight);
409 painter->drawLine(l2);
413 // draw audio thumbnails
414 if (KdenliveSettings::audiothumbnails() && ((m_clipType == AV && option->exposedRect.bottom() > (itemHeight / 2)) || m_clipType == AUDIO) && audioThumbReady) {
416 QPainterPath path = m_clipType == AV ? roundRectPathLower : resultClipPath;
417 if (m_clipType == AV) painter->fillPath(path, QBrush(QColor(200, 200, 200, 140)));
419 int channels = baseClip()->getProperty("channels").toInt();
420 if (scale != framePixelWidth)
421 audioThumbCachePic.clear();
422 double cropLeft = m_cropStart.frames(m_fps) * scale;
423 emit prepareAudioThumb(scale, path, startpixel + cropLeft, endpixel + cropLeft, channels);//200 more for less missing parts before repaint after scrolling
424 int newstart = startpixel + cropLeft;
425 for (int startCache = newstart - (newstart) % 100; startCache < endpixel + cropLeft; startCache += 100) {
426 if (audioThumbCachePic.contains(startCache) && !audioThumbCachePic[startCache].isNull())
427 painter->drawPixmap((int)(startCache - cropLeft), (int)(path.boundingRect().y()), audioThumbCachePic[startCache]);
432 QList < CommentedTime > markers = baseClip()->commentedSnapMarkers();
433 QList < CommentedTime >::Iterator it = markers.begin();
436 const int markerwidth = 4;
438 markerBrush = QBrush(QColor(120, 120, 0, 100));
439 QPen pen = painter->pen();
440 pen.setColor(QColor(255, 255, 255, 200));
441 pen.setStyle(Qt::DotLine);
442 painter->setPen(pen);
443 for (; it != markers.end(); ++it) {
444 pos = (*it).time() - cropStart();
445 if (pos > GenTime()) {
446 if (pos > duration()) break;
447 framepos = scale * pos.frames(m_fps);
448 QLineF l(framepos, 5, framepos, itemHeight - 5);
449 painter->drawLine(l);
450 if (KdenliveSettings::showmarkers()) {
451 const QRectF txtBounding = painter->boundingRect(framepos + 1, 10, itemWidth - framepos - 2, itemHeight - 10, Qt::AlignLeft | Qt::AlignTop, " " + (*it).comment() + " ");
453 path.addRoundedRect(txtBounding, 3, 3);
454 painter->fillPath(path, markerBrush);
455 painter->drawText(txtBounding, Qt::AlignCenter, (*it).comment());
457 //painter->fillRect(QRect(br.x() + framepos, br.y(), 10, br.height()), QBrush(QColor(0, 0, 0, 150)));
460 pen.setColor(Qt::black);
461 pen.setStyle(Qt::SolidLine);
462 painter->setPen(pen);
464 // draw start / end fades
467 fades = QBrush(QColor(200, 50, 50, 150));
468 } else fades = QBrush(QColor(200, 200, 200, 200));
470 if (m_startFade != 0) {
471 QPainterPath fadeInPath;
472 fadeInPath.moveTo(0, 0);
473 fadeInPath.lineTo(0, itemHeight);
474 fadeInPath.lineTo(m_startFade * scale, itemHeight);
475 fadeInPath.closeSubpath();
476 painter->fillPath(fadeInPath/*.intersected(resultClipPath)*/, fades);
478 QLineF l(m_startFade * scale, 0, 0, itemHeight);
479 painter->drawLine(l);
482 if (m_endFade != 0) {
483 QPainterPath fadeOutPath;
484 fadeOutPath.moveTo(itemWidth, 0);
485 fadeOutPath.lineTo(itemWidth, itemHeight);
486 fadeOutPath.lineTo(itemWidth - m_endFade * scale, 0);
487 fadeOutPath.closeSubpath();
488 painter->fillPath(fadeOutPath/*.intersected(resultClipPath)*/, fades);
490 QLineF l(itemWidth - m_endFade * scale, 0, itemWidth, itemHeight);
491 painter->drawLine(l);
495 // Draw effects names
496 if (!m_effectNames.isEmpty() && itemWidth > 30) {
497 QRectF txtBounding = painter->boundingRect(br, Qt::AlignLeft | Qt::AlignTop, m_effectNames);
498 txtBounding.setRight(txtBounding.right() + 15);
499 painter->setPen(Qt::white);
500 QBrush markerBrush(Qt::SolidPattern);
501 if (m_timeLine && m_timeLine->state() == QTimeLine::Running) {
502 qreal value = m_timeLine->currentValue();
503 txtBounding.setWidth(txtBounding.width() * value);
504 markerBrush.setColor(QColor(50 + 200 * (1.0 - value), 50, 50, 100 + 50 * value));
505 } else markerBrush.setColor(QColor(50, 50, 50, 150));
507 path.addRoundedRect(txtBounding, 4, 4);
508 painter->fillPath(path/*.intersected(resultClipPath)*/, markerBrush);
509 painter->drawText(txtBounding, Qt::AlignCenter, m_effectNames);
510 painter->setPen(Qt::black);
514 QRectF txtBounding = painter->boundingRect(br, Qt::AlignHCenter | Qt::AlignTop, " " + m_clipName + " ");
515 //painter->fillRect(txtBounding, QBrush(QColor(255, 255, 255, 150)));
516 painter->setPen(QColor(0, 0, 0, 180));
517 painter->drawText(txtBounding, Qt::AlignCenter, m_clipName);
518 txtBounding.translate(QPointF(1, 1));
519 painter->setPen(QColor(255, 255, 255, 255));
520 painter->drawText(txtBounding, Qt::AlignCenter, m_clipName);
521 // draw frame around clip
523 pen.setColor(Qt::red);
526 pen.setColor(Qt::black);
531 // draw effect or transition keyframes
532 if (itemWidth > 20) drawKeyFrames(painter, option->exposedRect);
535 painter->setClipRect(option->exposedRect);
536 painter->setPen(pen);
537 //painter->setClipRect(option->exposedRect);
538 painter->drawPath(resultClipPath);
540 if (m_hover && itemWidth > 30) {
541 painter->setBrush(QColor(180, 180, 50, 180)); //gradient);
543 // draw transitions handles
544 QPainterPath transitionHandle;
545 const int handle_size = 4;
546 transitionHandle.moveTo(0, 0);
547 transitionHandle.lineTo(handle_size, handle_size);
548 transitionHandle.lineTo(handle_size * 2, 0);
549 transitionHandle.lineTo(handle_size * 3, handle_size);
550 transitionHandle.lineTo(handle_size * 2, handle_size * 2);
551 transitionHandle.lineTo(handle_size * 3, handle_size * 3);
552 transitionHandle.lineTo(0, handle_size * 3);
553 transitionHandle.closeSubpath();
554 int pointy = (int)(itemHeight / 2);
556 int pointx2 = (int)(itemWidth - (10 + handle_size * 3));
558 painter->setPen(QPen(Qt::black));
559 painter->setBrush(QBrush(QColor(50, 50, 0)));
561 /*QRadialGradient gradient(pointx1 + 5, pointy + 5 , 5, 2, 2);
562 gradient.setColorAt(0.2, Qt::white);
563 gradient.setColorAt(0.8, Qt::yellow);
564 gradient.setColorAt(1, Qt::black);*/
567 painter->translate(pointx1, pointy);
568 painter->drawPath(transitionHandle); //Ellipse(0, 0 , 10, 10);
569 painter->translate(-pointx1, -pointy);
571 /* QRadialGradient gradient1(pointx2 + 5, pointy + 5 , 5, 2, 2);
572 gradient1.setColorAt(0.2, Qt::white);
573 gradient1.setColorAt(0.8, Qt::yellow);
574 gradient1.setColorAt(1, Qt::black);
575 painter->setBrush(gradient1);*/
576 painter->translate(pointx2, pointy);
579 //painter->setMatrix(m);
580 painter->drawPath(transitionHandle); // Ellipse(0, 0, 10, 10);
581 //painter->setMatrix(m);
582 painter->translate(- pointx2, -pointy);
587 OPERATIONTYPE ClipItem::operationMode(QPointF pos, double scale) {
589 m_editedKeyframe = mouseOverKeyFrames(pos);
590 if (m_editedKeyframe != -1) return KEYFRAME;
592 QRectF rect = sceneBoundingRect();
593 if (qAbs((int)(pos.x() - (rect.x() + scale * m_startFade))) < 6 && qAbs((int)(pos.y() - rect.y())) < 6) {
594 if (m_startFade == 0) setToolTip(i18n("Add audio fade"));
595 else setToolTip(i18n("Audio fade duration: %1s", GenTime(m_startFade, m_fps).seconds()));
597 } else if (qAbs((int)(pos.x() - rect.x())) < 6) {
598 setToolTip(i18n("Crop from start: %1s", cropStart().seconds()));
600 } else if (qAbs((int)(pos.x() - (rect.x() + rect.width() - scale * m_endFade))) < 6 && qAbs((int)(pos.y() - rect.y())) < 6) {
601 if (m_endFade == 0) setToolTip(i18n("Add audio fade"));
602 else setToolTip(i18n("Audio fade duration: %1s", GenTime(m_endFade, m_fps).seconds()));
604 } else if (qAbs((int)(pos.x() - (rect.x() + rect.width()))) < 6) {
605 setToolTip(i18n("Clip duration: %1s", duration().seconds()));
607 } else if (qAbs((int)(pos.x() - (rect.x() + 16))) < 10 && qAbs((int)(pos.y() - (rect.y() + rect.height() / 2 + 5))) < 8) {
608 setToolTip(i18n("Add transition"));
609 return TRANSITIONSTART;
610 } else if (qAbs((int)(pos.x() - (rect.x() + rect.width() - 21))) < 10 && qAbs((int)(pos.y() - (rect.y() + rect.height() / 2 + 5))) < 8) {
611 setToolTip(i18n("Add transition"));
612 return TRANSITIONEND;
614 setToolTip(QString());
618 QList <GenTime> ClipItem::snapMarkers() const {
619 QList < GenTime > snaps;
620 QList < GenTime > markers = baseClip()->snapMarkers();
624 for (int i = 0; i < markers.size(); i++) {
625 pos = markers.at(i) - cropStart();
626 if (pos > GenTime()) {
627 if (pos > duration()) break;
628 else snaps.append(pos + startPos());
634 QList <CommentedTime> ClipItem::commentedSnapMarkers() const {
635 QList < CommentedTime > snaps;
636 QList < CommentedTime > markers = baseClip()->commentedSnapMarkers();
640 for (int i = 0; i < markers.size(); i++) {
641 pos = markers.at(i).time() - cropStart();
642 if (pos > GenTime()) {
643 if (pos > duration()) break;
644 else snaps.append(CommentedTime(pos + startPos(), markers.at(i).comment()));
650 void ClipItem::slotPrepareAudioThumb(double pixelForOneFrame, QPainterPath path, int startpixel, int endpixel, int channels) {
652 QRectF re = path.boundingRect();
653 kDebug() << "// PREP AUDIO THMB FRMO : " << startpixel << ", to: " << endpixel;
654 //if ( (!audioThumbWasDrawn || framePixelWidth!=pixelForOneFrame ) && !baseClip()->audioFrameChache.isEmpty()){
656 for (int startCache = startpixel - startpixel % 100;startCache + 100 < endpixel + 100;startCache += 100) {
657 //kDebug() << "creating " << startCache;
658 //if (framePixelWidth!=pixelForOneFrame ||
659 if (framePixelWidth == pixelForOneFrame && audioThumbCachePic.contains(startCache))
661 if (audioThumbCachePic[startCache].isNull() || framePixelWidth != pixelForOneFrame) {
662 audioThumbCachePic[startCache] = QPixmap(100, (int)(re.height()));
663 audioThumbCachePic[startCache].fill(QColor(200, 200, 200, 0));
665 bool fullAreaDraw = pixelForOneFrame < 10;
666 QMap<int, QPainterPath > positiveChannelPaths;
667 QMap<int, QPainterPath > negativeChannelPaths;
668 QPainter pixpainter(&audioThumbCachePic[startCache]);
670 audiopen.setWidth(0);
671 pixpainter.setPen(audiopen);
672 //pixpainter.setRenderHint(QPainter::Antialiasing,true);
673 //pixpainter.drawLine(0,0,100,re.height());
674 int channelHeight = audioThumbCachePic[startCache].height() / channels;
676 for (int i = 0;i < channels;i++) {
678 positiveChannelPaths[i].moveTo(0, channelHeight*i + channelHeight / 2);
679 negativeChannelPaths[i].moveTo(0, channelHeight*i + channelHeight / 2);
682 for (int samples = 0;samples <= 100;samples++) {
683 double frame = (double)(samples + startCache - 0) / pixelForOneFrame;
684 int sample = (int)((frame - (int)(frame)) * 20); // AUDIO_FRAME_SIZE
685 if (frame < 0 || sample < 0 || sample > 19)
687 QMap<int, QByteArray> frame_channel_data = baseClip()->audioFrameChache[(int)frame];
689 for (int channel = 0;channel < channels && frame_channel_data[channel].size() > 0;channel++) {
691 int y = channelHeight * channel + channelHeight / 2;
692 int delta = (int)(frame_channel_data[channel][sample] - 127 / 2) * channelHeight / 64;
694 positiveChannelPaths[channel].lineTo(samples, 0.1 + y + qAbs(delta));
695 negativeChannelPaths[channel].lineTo(samples, 0.1 + y - qAbs(delta));
697 positiveChannelPaths[channel].lineTo(samples, 0.1 + y + delta);
698 negativeChannelPaths[channel].lineTo(samples, 0.1 + y - delta);
701 for (int channel = 0;channel < channels ;channel++)
702 if (fullAreaDraw && samples == 100) {
703 positiveChannelPaths[channel].lineTo(samples, channelHeight*channel + channelHeight / 2);
704 negativeChannelPaths[channel].lineTo(samples, channelHeight*channel + channelHeight / 2);
705 positiveChannelPaths[channel].lineTo(0, channelHeight*channel + channelHeight / 2);
706 negativeChannelPaths[channel].lineTo(0, channelHeight*channel + channelHeight / 2);
710 if (m_clipType != AV) pixpainter.setBrush(QBrush(QColor(200, 200, 100)));
712 pixpainter.setPen(QPen(QColor(0, 0, 0)));
713 pixpainter.setBrush(QBrush(QColor(60, 60, 60)));
715 for (int i = 0;i < channels;i++) {
717 //pixpainter.fillPath(positiveChannelPaths[i].united(negativeChannelPaths[i]),QBrush(Qt::SolidPattern));//or singleif looks better
718 pixpainter.drawPath(positiveChannelPaths[i].united(negativeChannelPaths[i]));//or singleif looks better
720 pixpainter.drawPath(positiveChannelPaths[i]);
723 //audioThumbWasDrawn=true;
724 framePixelWidth = pixelForOneFrame;
729 uint ClipItem::fadeIn() const {
733 uint ClipItem::fadeOut() const {
738 void ClipItem::setFadeIn(int pos, double scale) {
739 int oldIn = m_startFade;
740 if (pos < 0) pos = 0;
741 if (pos > m_cropDuration.frames(m_fps)) pos = (int)(m_cropDuration.frames(m_fps) / 2);
743 QRectF rect = boundingRect();
744 update(rect.x(), rect.y(), qMax(oldIn, pos) * scale, rect.height());
747 void ClipItem::setFadeOut(int pos, double scale) {
748 int oldOut = m_endFade;
749 if (pos < 0) pos = 0;
750 if (pos > m_cropDuration.frames(m_fps)) pos = (int)(m_cropDuration.frames(m_fps) / 2);
752 QRectF rect = boundingRect();
753 update(rect.x() + rect.width() - qMax(oldOut, pos) * scale, rect.y(), pos * scale, rect.height());
758 void ClipItem::mousePressEvent(QGraphicsSceneMouseEvent * event) {
759 /*m_resizeMode = operationMode(event->pos());
760 if (m_resizeMode == MOVE) {
761 m_maxTrack = scene()->sceneRect().height();
762 m_grabPoint = (int) (event->pos().x() - rect().x());
764 QGraphicsRectItem::mousePressEvent(event);
768 void ClipItem::mouseReleaseEvent(QGraphicsSceneMouseEvent * event) {
770 QGraphicsRectItem::mouseReleaseEvent(event);
774 void ClipItem::hoverEnterEvent(QGraphicsSceneHoverEvent *) {
776 QRectF r = boundingRect();
777 qreal width = qMin(25.0, r.width());
778 update(r.x(), r.y(), width, r.height());
779 update(r.right() - width, r.y(), width, r.height());
783 void ClipItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *) {
785 QRectF r = boundingRect();
786 qreal width = qMin(25.0, r.width());
787 update(r.x(), r.y(), width, r.height());
788 update(r.right() - width, r.y(), width, r.height());
791 void ClipItem::resizeStart(int posx, double scale) {
792 const int min = (startPos() - cropStart()).frames(m_fps);
793 if (posx < min) posx = min;
794 if (posx == startPos().frames(m_fps)) return;
795 const int previous = cropStart().frames(m_fps);
796 AbstractClipItem::resizeStart(posx, scale);
797 checkEffectsKeyframesPos(previous, cropStart().frames(m_fps), true);
798 if (m_hasThumbs && KdenliveSettings::videothumbnails()) {
800 connect(m_clip->thumbProducer(), SIGNAL(thumbReady(int, QPixmap)), this, SLOT(slotThumbReady(int, QPixmap)));
801 startThumbTimer->start(100);
805 void ClipItem::resizeEnd(int posx, double scale) {
806 const int max = (startPos() - cropStart() + maxDuration()).frames(m_fps) + 1;
807 if (posx > max) posx = max;
808 if (posx == endPos().frames(m_fps)) return;
809 const int previous = (cropStart() + duration()).frames(m_fps);
810 AbstractClipItem::resizeEnd(posx, scale);
811 checkEffectsKeyframesPos(previous, (cropStart() + duration()).frames(m_fps), false);
812 if (m_hasThumbs && KdenliveSettings::videothumbnails()) {
814 connect(m_clip->thumbProducer(), SIGNAL(thumbReady(int, QPixmap)), this, SLOT(slotThumbReady(int, QPixmap)));
815 endThumbTimer->start(100);
820 void ClipItem::checkEffectsKeyframesPos(const int previous, const int current, bool fromStart) {
821 for (int i = 0; i < m_effectList.size(); i++) {
822 QDomElement effect = m_effectList.at(i);
823 QDomNodeList params = effect.elementsByTagName("parameter");
824 for (int j = 0; j < params.count(); j++) {
825 QDomElement e = params.item(i).toElement();
826 if (e.attribute("type") == "keyframe") {
827 // parse keyframes and adjust values
828 const QStringList keyframes = e.attribute("keyframes").split(";", QString::SkipEmptyParts);
829 QMap <int, double> kfr;
830 foreach(const QString str, keyframes) {
831 int pos = str.section(":", 0, 0).toInt();
832 double val = str.section(":", 1, 1).toDouble();
833 if (pos == previous) kfr[current] = val;
835 if (fromStart && pos >= current) kfr[pos] = val;
836 else if (!fromStart && pos <= current) kfr[pos] = val;
840 QMap<int, double>::const_iterator k = kfr.constBegin();
841 while (k != kfr.constEnd()) {
842 newkfr.append(QString::number(k.key()) + ":" + QString::number(k.value()) + ";");
845 e.setAttribute("keyframes", newkfr);
850 if (m_selectedEffect >= 0) setSelectedEffect(m_selectedEffect);
855 /*void ClipItem::mouseMoveEvent(QGraphicsSceneMouseEvent * event) {
858 int ClipItem::effectsCounter() {
859 return m_effectsCounter++;
862 int ClipItem::effectsCount() {
863 return m_effectList.size();
866 QStringList ClipItem::effectNames() {
867 return m_effectList.effectNames();
870 QDomElement ClipItem::effectAt(int ix) {
871 if (ix > m_effectList.count() - 1 || ix < 0) return QDomElement();
872 return m_effectList.at(ix);
875 void ClipItem::setEffectAt(int ix, QDomElement effect) {
876 kDebug() << "CHange EFFECT AT: " << ix << ", CURR: " << m_effectList.at(ix).attribute("tag") << ", NEW: " << effect.attribute("tag");
877 m_effectList.insert(ix, effect);
878 m_effectList.removeAt(ix + 1);
879 m_effectNames = m_effectList.effectNames().join(" / ");
880 if (effect.attribute("id") == "fadein" || effect.attribute("id") == "fadeout") update(boundingRect());
882 QRectF r = boundingRect();
888 QMap <QString, QString> ClipItem::addEffect(QDomElement effect, bool animate) {
889 QMap <QString, QString> effectParams;
890 bool needRepaint = false;
892 doc.appendChild(doc.importNode(effect, true));
893 kDebug() << "/////// CLIP ADD EFFECT: "<< doc.toString();*/
894 m_effectList.append(effect);
895 effectParams["tag"] = effect.attribute("tag");
896 QString effectId = effect.attribute("id");
897 if (effectId.isEmpty()) effectId = effect.attribute("tag");
898 effectParams["id"] = effectId;
899 effectParams["kdenlive_ix"] = effect.attribute("kdenlive_ix");
900 QString state = effect.attribute("disabled");
901 if (!state.isEmpty()) effectParams["disabled"] = state;
902 QDomNodeList params = effect.elementsByTagName("parameter");
904 for (int i = 0; i < params.count(); i++) {
905 QDomElement e = params.item(i).toElement();
907 if (e.attribute("type") == "keyframe") {
908 effectParams["keyframes"] = e.attribute("keyframes");
909 effectParams["min"] = e.attribute("min");
910 effectParams["max"] = e.attribute("max");
911 effectParams["factor"] = e.attribute("factor", "1");
912 effectParams["starttag"] = e.attribute("starttag", "start");
913 effectParams["endtag"] = e.attribute("endtag", "end");
916 double f = e.attribute("factor", "1").toDouble();
919 effectParams[e.attribute("name")] = e.attribute("value");
920 // check if it is a fade effect
921 if (effectId == "fadein") {
923 if (e.attribute("name") == "out") fade += e.attribute("value").toInt();
924 else if (e.attribute("name") == "in") fade -= e.attribute("value").toInt();
925 } else if (effectId == "fadeout") {
927 if (e.attribute("name") == "out") fade -= e.attribute("value").toInt();
928 else if (e.attribute("name") == "in") fade += e.attribute("value").toInt();
931 effectParams[e.attribute("name")] = QString::number(effectParams[e.attribute("name")].toDouble() / f);
935 m_effectNames = m_effectList.effectNames().join(" / ");
936 if (fade > 0) m_startFade = fade;
937 else if (fade < 0) m_endFade = -fade;
938 if (needRepaint) update(boundingRect());
941 } else if (!needRepaint) {
942 QRectF r = boundingRect();
946 if (m_selectedEffect == -1) {
947 m_selectedEffect = 0;
948 setSelectedEffect(m_selectedEffect);
953 QMap <QString, QString> ClipItem::getEffectArgs(QDomElement effect) {
954 QMap <QString, QString> effectParams;
955 effectParams["tag"] = effect.attribute("tag");
956 effectParams["kdenlive_ix"] = effect.attribute("kdenlive_ix");
957 effectParams["id"] = effect.attribute("id");
958 QString state = effect.attribute("disabled");
959 if (!state.isEmpty()) effectParams["disabled"] = state;
960 QDomNodeList params = effect.elementsByTagName("parameter");
961 for (int i = 0; i < params.count(); i++) {
962 QDomElement e = params.item(i).toElement();
963 kDebug() << "/ / / /SENDING EFFECT PARAM: " << e.attribute("type") << ", NAME_ " << e.attribute("tag");
964 if (e.attribute("type") == "keyframe") {
965 kDebug() << "/ / / /SENDING KEYFR EFFECT TYPE";
966 effectParams["keyframes"] = e.attribute("keyframes");
967 effectParams["max"] = e.attribute("max");
968 effectParams["min"] = e.attribute("min");
969 effectParams["factor"] = e.attribute("factor", "1");
970 effectParams["starttag"] = e.attribute("starttag", "start");
971 effectParams["endtag"] = e.attribute("endtag", "end");
972 } else if (e.attribute("namedesc").contains(";")) {
973 QString format = e.attribute("format");
974 QStringList separators = format.split("%d", QString::SkipEmptyParts);
975 QStringList values = e.attribute("value").split(QRegExp("[,:;x]"));
977 QTextStream txtNeu(&neu);
978 if (values.size() > 0)
979 txtNeu << (int)values[0].toDouble();
980 for (int i = 0;i < separators.size() && i + 1 < values.size();i++) {
981 txtNeu << separators[i];
982 txtNeu << (int)(values[i+1].toDouble());
984 effectParams["start"] = neu;
986 if (e.attribute("factor", "1") != "1")
987 effectParams[e.attribute("name")] = QString::number(e.attribute("value").toDouble() / e.attribute("factor").toDouble());
988 else effectParams[e.attribute("name")] = e.attribute("value");
994 void ClipItem::deleteEffect(QString index) {
995 bool needRepaint = false;
996 for (int i = 0; i < m_effectList.size(); ++i) {
997 if (m_effectList.at(i).attribute("kdenlive_ix") == index) {
998 if (m_effectList.at(i).attribute("id") == "fadein") {
1001 } else if (m_effectList.at(i).attribute("id") == "fadeout") {
1005 m_effectList.removeAt(i);
1009 m_effectNames = m_effectList.effectNames().join(" / ");
1010 if (needRepaint) update(boundingRect());
1014 double ClipItem::speed() const {
1018 void ClipItem::setSpeed(const double speed) {
1020 if (m_speed == 1.0) m_clipName = baseClip()->name();
1021 else m_clipName = baseClip()->name() + " - " + QString::number(speed * 100, 'f', 0) + "%";
1025 GenTime ClipItem::maxDuration() const {
1026 return m_maxDuration / m_speed;
1030 void ClipItem::dropEvent(QGraphicsSceneDragDropEvent * event) {
1031 QString effects = QString(event->mimeData()->data("kdenlive/effectslist"));
1033 doc.setContent(effects, true);
1034 QDomElement e = doc.documentElement();
1035 CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
1036 if (view) view->slotAddEffect(e, m_startPos, track());
1040 void ClipItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event) {
1041 event->setAccepted(event->mimeData()->hasFormat("kdenlive/effectslist"));
1044 void ClipItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event) {
1047 void ClipItem::addTransition(Transition* t) {
1048 m_transitionsList.append(t);
1049 CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
1051 QDomElement e = doc.documentElement();
1052 //if (view) view->slotAddTransition(this, t->toXML() , t->startPos(), track());
1056 void CustomTrackView::mousePressEvent ( QMouseEvent * event )
1058 int pos = event->x();
1059 if (event->modifiers() == Qt::ControlModifier)
1060 setDragMode(QGraphicsView::ScrollHandDrag);
1061 else if (event->modifiers() == Qt::ShiftModifier)
1062 setDragMode(QGraphicsView::RubberBandDrag);
1064 QGraphicsItem * item = itemAt(event->pos());
1067 else emit cursorMoved((int) mapToScene(event->x(), 0).x());
1070 QGraphicsView::mousePressEvent(event);
1073 void CustomTrackView::mouseReleaseEvent ( QMouseEvent * event )
1075 QGraphicsView::mouseReleaseEvent(event);
1076 setDragMode(QGraphicsView::NoDrag);
1080 #include "clipitem.moc"