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 ***************************************************************************/
22 #include "customtrackview.h"
23 #include "customtrackscene.h"
25 #include "docclipbase.h"
26 #include "transition.h"
27 #include "kdenlivesettings.h"
29 #include "profilesdialog.h"
36 #include <QStyleOptionGraphicsItem>
37 #include <QGraphicsScene>
40 ClipItem::ClipItem(DocClipBase *clip, ItemInfo info, double fps, double speed, int strobe, bool generateThumbs) :
41 AbstractClipItem(info, QRectF(), fps),
47 m_startPix(QPixmap()),
52 m_startThumbRequested(false),
53 m_endThumbRequested(false),
60 setRect(0, 0, (info.endPos - info.startPos).frames(fps) - 0.02, (double)(KdenliveSettings::trackheight() - 2));
61 setPos(info.startPos.frames(fps), (double)(info.track * KdenliveSettings::trackheight()) + 1);
63 // set speed independant info
64 m_speedIndependantInfo = m_info;
65 m_speedIndependantInfo.cropStart = GenTime((int)(m_info.cropStart.frames(m_fps) * m_speed), m_fps);
66 m_speedIndependantInfo.cropDuration = GenTime((int)(m_info.cropDuration.frames(m_fps) * m_speed), m_fps);
68 m_videoPix = KIcon("kdenlive-show-video").pixmap(QSize(16, 16));
69 m_audioPix = KIcon("kdenlive-show-audio").pixmap(QSize(16, 16));
71 if (m_speed == 1.0) m_clipName = m_clip->name();
73 m_clipName = m_clip->name() + " - " + QString::number(m_speed * 100, 'f', 0) + '%';
75 m_producer = m_clip->getId();
76 m_clipType = m_clip->clipType();
77 //m_cropStart = info.cropStart;
78 m_maxDuration = m_clip->maxDuration();
80 m_audioThumbReady = m_clip->audioThumbCreated();
81 //setAcceptsHoverEvents(true);
82 connect(this , SIGNAL(prepareAudioThumb(double, int, int, int)) , this, SLOT(slotPrepareAudioThumb(double, int, int, int)));
84 if (m_clipType == VIDEO || m_clipType == AV || m_clipType == SLIDESHOW || m_clipType == PLAYLIST) {
85 m_baseColor = QColor(141, 166, 215);
86 if (!m_clip->isPlaceHolder()) {
88 m_startThumbTimer.setSingleShot(true);
89 connect(&m_startThumbTimer, SIGNAL(timeout()), this, SLOT(slotGetStartThumb()));
90 m_endThumbTimer.setSingleShot(true);
91 connect(&m_endThumbTimer, SIGNAL(timeout()), this, SLOT(slotGetEndThumb()));
93 connect(this, SIGNAL(getThumb(int, int)), m_clip->thumbProducer(), SLOT(extractImage(int, int)));
95 connect(m_clip->thumbProducer(), SIGNAL(thumbReady(int, QImage)), this, SLOT(slotThumbReady(int, QImage)));
96 connect(m_clip, SIGNAL(gotAudioData()), this, SLOT(slotGotAudioData()));
97 if (generateThumbs) QTimer::singleShot(200, this, SLOT(slotFetchThumbs()));
100 } else if (m_clipType == COLOR) {
101 QString colour = m_clip->getProperty("colour");
102 colour = colour.replace(0, 2, "#");
103 m_baseColor = QColor(colour.left(7));
104 } else if (m_clipType == IMAGE || m_clipType == TEXT) {
105 m_baseColor = QColor(141, 166, 215);
106 if (m_clipType == TEXT) {
107 connect(this, SIGNAL(getThumb(int, int)), m_clip->thumbProducer(), SLOT(extractImage(int, int)));
108 connect(m_clip->thumbProducer(), SIGNAL(thumbReady(int, QImage)), this, SLOT(slotThumbReady(int, QImage)));
110 //m_startPix = KThumb::getImage(KUrl(clip->getProperty("resource")), (int)(KdenliveSettings::trackheight() * KdenliveSettings::project_display_ratio()), KdenliveSettings::trackheight());
111 } else if (m_clipType == AUDIO) {
112 m_baseColor = QColor(141, 215, 166);
113 connect(m_clip, SIGNAL(gotAudioData()), this, SLOT(slotGotAudioData()));
118 ClipItem::~ClipItem()
121 if (scene()) scene()->removeItem(this);
122 if (m_clipType == VIDEO || m_clipType == AV || m_clipType == SLIDESHOW || m_clipType == PLAYLIST) {
123 //disconnect(m_clip->thumbProducer(), SIGNAL(thumbReady(int, QImage)), this, SLOT(slotThumbReady(int, QImage)));
124 //disconnect(m_clip, SIGNAL(gotAudioData()), this, SLOT(slotGotAudioData()));
129 ClipItem *ClipItem::clone(ItemInfo info) const
131 ClipItem *duplicate = new ClipItem(m_clip, info, m_fps, m_speed, m_strobe);
132 if (m_clipType == IMAGE || m_clipType == TEXT) duplicate->slotSetStartThumb(m_startPix);
133 else if (m_clipType != COLOR) {
134 if (info.cropStart == m_info.cropStart) duplicate->slotSetStartThumb(m_startPix);
135 if (info.cropStart + (info.endPos - info.startPos) == m_info.cropStart + (m_info.endPos - m_info.startPos)) duplicate->slotSetEndThumb(m_endPix);
137 //kDebug() << "// CLoning clip: " << (info.cropStart + (info.endPos - info.startPos)).frames(m_fps) << ", CURRENT end: " << (cropStart() + duration()).frames(m_fps);
138 duplicate->setEffectList(m_effectList);
139 duplicate->setVideoOnly(m_videoOnly);
140 duplicate->setAudioOnly(m_audioOnly);
141 //duplicate->setSpeed(m_speed);
145 void ClipItem::setEffectList(const EffectsList effectList)
147 m_effectList.clone(effectList);
148 m_effectNames = m_effectList.effectNames().join(" / ");
149 if (!m_effectList.isEmpty()) setSelectedEffect(0);
152 const EffectsList ClipItem::effectList() const
157 int ClipItem::selectedEffectIndex() const
159 return m_selectedEffect;
162 void ClipItem::initEffect(QDomElement effect, int diff)
164 // the kdenlive_ix int is used to identify an effect in mlt's playlist, should
166 if (effect.attribute("kdenlive_ix").toInt() == 0)
167 effect.setAttribute("kdenlive_ix", QString::number(effectsCounter()));
169 if (effect.attribute("id") == "freeze" && diff > 0) {
170 EffectsList::setParameter(effect, "frame", QString::number(diff));
173 // Init parameter value & keyframes if required
174 QDomNodeList params = effect.elementsByTagName("parameter");
175 for (int i = 0; i < params.count(); i++) {
176 QDomElement e = params.item(i).toElement();
177 kDebug() << "// init eff: " << e.attribute("name");
179 // Check if this effect has a variable parameter
180 if (e.attribute("default").startsWith('%')) {
181 double evaluatedValue = ProfilesDialog::getStringEval(projectScene()->profile(), e.attribute("default"));
182 e.setAttribute("default", evaluatedValue);
183 if (e.hasAttribute("value") && e.attribute("value").startsWith('%')) {
184 e.setAttribute("value", evaluatedValue);
188 if (!e.isNull() && (e.attribute("type") == "keyframe" || e.attribute("type") == "simplekeyframe")) {
189 QString def = e.attribute("default");
190 // Effect has a keyframe type parameter, we need to set the values
191 if (e.attribute("keyframes").isEmpty()) {
192 e.setAttribute("keyframes", QString::number(cropStart().frames(m_fps)) + ':' + def + ';' + QString::number((cropStart() + cropDuration()).frames(m_fps) - 1) + ':' + def);
193 kDebug() << "///// EFFECT KEYFRAMES INITED: " << e.attribute("keyframes");
198 if (effect.attribute("tag") == "volume" || effect.attribute("tag") == "brightness") {
199 if (effect.attribute("id") == "fadeout" || effect.attribute("id") == "fade_to_black") {
200 int end = (cropDuration() + cropStart()).frames(m_fps);
202 if (effect.attribute("id") == "fadeout") {
203 if (m_effectList.hasEffect(QString(), "fade_to_black") == -1) {
204 int effectDuration = EffectsList::parameter(effect, "in").toInt();
205 if (effectDuration > cropDuration().frames(m_fps)) {
206 effectDuration = cropDuration().frames(m_fps) / 2;
208 start -= effectDuration;
210 QDomElement fadeout = m_effectList.getEffectByTag(QString(), "fade_to_black");
211 start -= EffectsList::parameter(fadeout, "out").toInt() - EffectsList::parameter(fadeout, "in").toInt();
213 } else if (effect.attribute("id") == "fade_to_black") {
214 if (m_effectList.hasEffect(QString(), "fadeout") == -1) {
215 int effectDuration = EffectsList::parameter(effect, "in").toInt();
216 if (effectDuration > cropDuration().frames(m_fps)) {
217 effectDuration = cropDuration().frames(m_fps) / 2;
219 start -= effectDuration;
221 QDomElement fadeout = m_effectList.getEffectByTag(QString(), "fadeout");
222 start -= EffectsList::parameter(fadeout, "out").toInt() - EffectsList::parameter(fadeout, "in").toInt();
225 EffectsList::setParameter(effect, "in", QString::number(start));
226 EffectsList::setParameter(effect, "out", QString::number(end));
227 } else if (effect.attribute("id") == "fadein" || effect.attribute("id") == "fade_from_black") {
228 int start = cropStart().frames(m_fps);
230 if (effect.attribute("id") == "fadein") {
231 if (m_effectList.hasEffect(QString(), "fade_from_black") == -1) {
232 int effectDuration = EffectsList::parameter(effect, "out").toInt();
233 if (effectDuration > cropDuration().frames(m_fps)) {
234 effectDuration = cropDuration().frames(m_fps) / 2;
236 end += effectDuration;
238 end += EffectsList::parameter(m_effectList.getEffectByTag(QString(), "fade_from_black"), "out").toInt();
239 } else if (effect.attribute("id") == "fade_from_black") {
240 if (m_effectList.hasEffect(QString(), "fadein") == -1) {
241 int effectDuration = EffectsList::parameter(effect, "out").toInt();
242 if (effectDuration > cropDuration().frames(m_fps)) {
243 effectDuration = cropDuration().frames(m_fps) / 2;
245 end += effectDuration;
247 end += EffectsList::parameter(m_effectList.getEffectByTag(QString(), "fadein"), "out").toInt();
249 EffectsList::setParameter(effect, "in", QString::number(start));
250 EffectsList::setParameter(effect, "out", QString::number(end));
255 bool ClipItem::checkKeyFrames()
257 bool clipEffectsModified = false;
258 for (int ix = 0; ix < m_effectList.count(); ix ++) {
259 QString kfr = keyframes(ix);
260 if (!kfr.isEmpty()) {
261 const QStringList keyframes = kfr.split(';', QString::SkipEmptyParts);
262 QStringList newKeyFrames;
263 bool cutKeyFrame = false;
264 bool modified = false;
266 double lastValue = -1;
267 int start = cropStart().frames(m_fps);
268 int end = (cropStart() + cropDuration()).frames(m_fps);
269 foreach(const QString &str, keyframes) {
270 int pos = str.section(':', 0, 0).toInt();
271 double val = str.section(':', 1, 1).toDouble();
272 if (pos - start < 0) {
273 // a keyframe is defined before the start of the clip
275 } else if (cutKeyFrame) {
276 // create new keyframe at clip start, calculate interpolated value
278 int diff = pos - lastPos;
279 double ratio = (double)(start - lastPos) / diff;
280 double newValue = lastValue + (val - lastValue) * ratio;
281 newKeyFrames.append(QString::number(start) + ':' + QString::number(newValue));
288 // create new keyframe at clip end, calculate interpolated value
289 int diff = pos - lastPos;
291 double ratio = (double)(end - lastPos) / diff;
292 double newValue = lastValue + (val - lastValue) * ratio;
293 newKeyFrames.append(QString::number(end) + ':' + QString::number(newValue));
298 newKeyFrames.append(QString::number(pos) + ':' + QString::number(val));
306 setKeyframes(ix, newKeyFrames.join(";"));
307 clipEffectsModified = true;
311 return clipEffectsModified;
314 void ClipItem::setKeyframes(const int ix, const QString keyframes)
316 QDomElement effect = getEffectAt(ix);
317 if (effect.attribute("disable") == "1") return;
318 QDomNodeList params = effect.elementsByTagName("parameter");
319 for (int i = 0; i < params.count(); i++) {
320 QDomElement e = params.item(i).toElement();
321 if (!e.isNull() && (e.attribute("type") == "keyframe" || e.attribute("type") == "simplekeyframe")) {
322 e.setAttribute("keyframes", keyframes);
323 if (ix == m_selectedEffect) {
325 double max = e.attribute("max").toDouble();
326 double min = e.attribute("min").toDouble();
327 m_keyframeFactor = 100.0 / (max - min);
328 m_keyframeDefault = e.attribute("default").toDouble();
329 m_selectedKeyframe = 0;
331 const QStringList keyframes = e.attribute("keyframes").split(';', QString::SkipEmptyParts);
332 foreach(const QString &str, keyframes) {
333 int pos = str.section(':', 0, 0).toInt();
334 double val = str.section(':', 1, 1).toDouble();
335 m_keyframes[pos] = val;
337 if (m_keyframes.find(m_editedKeyframe) == m_keyframes.end()) m_editedKeyframe = -1;
338 if (m_keyframes.find(m_editedKeyframe) == m_keyframes.end()) m_editedKeyframe = -1;
348 void ClipItem::setSelectedEffect(const int ix)
350 m_selectedEffect = ix;
351 QDomElement effect = effectAt(m_selectedEffect);
352 if (effect.isNull() == false) {
353 QDomNodeList params = effect.elementsByTagName("parameter");
354 if (effect.attribute("disable") != "1")
355 for (int i = 0; i < params.count(); i++) {
356 QDomElement e = params.item(i).toElement();
357 if (!e.isNull() && (e.attribute("type") == "keyframe" || e.attribute("type") == "simplekeyframe")) {
359 double max = e.attribute("max").toDouble();
360 double min = e.attribute("min").toDouble();
361 m_keyframeFactor = 100.0 / (max - min);
362 m_keyframeDefault = e.attribute("default").toDouble();
363 m_selectedKeyframe = 0;
366 const QStringList keyframes = e.attribute("keyframes").split(';', QString::SkipEmptyParts);
367 foreach(const QString &str, keyframes) {
368 int pos = str.section(':', 0, 0).toInt();
369 double val = str.section(':', 1, 1).toDouble();
370 m_keyframes[pos] = val;
372 if (m_keyframes.find(m_editedKeyframe) == m_keyframes.end()) m_editedKeyframe = -1;
378 if (!m_keyframes.isEmpty()) {
384 QString ClipItem::keyframes(const int index)
387 QDomElement effect = effectAt(index);
388 QDomNodeList params = effect.elementsByTagName("parameter");
390 for (int i = 0; i < params.count(); i++) {
391 QDomElement e = params.item(i).toElement();
392 if (!e.isNull() && (e.attribute("type") == "keyframe" || e.attribute("type") == "simplekeyframe")) {
393 result = e.attribute("keyframes");
400 void ClipItem::updateKeyframeEffect()
402 // regenerate xml parameter from the clip keyframes
403 QDomElement effect = getEffectAt(m_selectedEffect);
404 if (effect.attribute("disable") == "1") return;
405 QDomNodeList params = effect.elementsByTagName("parameter");
407 for (int i = 0; i < params.count(); i++) {
408 QDomElement e = params.item(i).toElement();
409 if (!e.isNull() && (e.attribute("type") == "keyframe" || e.attribute("type") == "simplekeyframe")) {
411 if (m_keyframes.count() > 0) {
412 QMap<int, int>::const_iterator i = m_keyframes.constBegin();
413 while (i != m_keyframes.constEnd()) {
414 keyframes.append(QString::number(i.key()) + ':' + QString::number(i.value()) + ';');
418 // Effect has a keyframe type parameter, we need to set the values
419 //kDebug() << "::::::::::::::: SETTING EFFECT KEYFRAMES: " << keyframes;
420 e.setAttribute("keyframes", keyframes);
426 QDomElement ClipItem::selectedEffect()
428 if (m_selectedEffect == -1 || m_effectList.isEmpty()) return QDomElement();
429 return effectAt(m_selectedEffect);
432 void ClipItem::resetThumbs(bool clearExistingThumbs)
434 if (clearExistingThumbs) {
435 m_startPix = QPixmap();
436 m_endPix = QPixmap();
437 m_audioThumbCachePic.clear();
443 void ClipItem::refreshClip(bool checkDuration)
445 if (checkDuration && (m_maxDuration != m_clip->maxDuration())) {
446 m_maxDuration = m_clip->maxDuration();
447 if (m_clipType != IMAGE && m_clipType != TEXT && m_clipType != COLOR) {
448 if (m_maxDuration != GenTime() && m_info.cropStart + m_info.cropDuration > m_maxDuration) {
449 // Clip duration changed, make sure to stay in correct range
450 if (m_info.cropStart > m_maxDuration) {
451 m_info.cropStart = GenTime();
452 m_info.cropDuration = qMin(m_info.cropDuration, m_maxDuration);
453 updateRectGeometry();
455 m_info.cropDuration = m_maxDuration;
456 updateRectGeometry();
461 if (m_clipType == COLOR) {
462 QString colour = m_clip->getProperty("colour");
463 colour = colour.replace(0, 2, "#");
464 m_baseColor = QColor(colour.left(7));
465 } else resetThumbs(checkDuration);
468 void ClipItem::slotFetchThumbs()
470 if (scene() == NULL || m_clipType == AUDIO || m_clipType == COLOR) return;
471 if (m_clipType == IMAGE) {
472 if (m_startPix.isNull()) {
473 m_startPix = KThumb::getImage(KUrl(m_clip->getProperty("resource")), (int)(KdenliveSettings::trackheight() * KdenliveSettings::project_display_ratio()), KdenliveSettings::trackheight());
479 if (m_clipType == TEXT) {
480 if (m_startPix.isNull()) slotGetStartThumb();
484 if (m_endPix.isNull() && m_startPix.isNull()) {
485 m_startThumbRequested = true;
486 m_endThumbRequested = true;
487 emit getThumb((int)m_speedIndependantInfo.cropStart.frames(m_fps), (int)(m_speedIndependantInfo.cropStart + m_speedIndependantInfo.cropDuration).frames(m_fps) - 1);
489 if (m_endPix.isNull()) {
492 if (m_startPix.isNull()) {
498 void ClipItem::slotGetStartThumb()
500 m_startThumbRequested = true;
501 emit getThumb((int)m_speedIndependantInfo.cropStart.frames(m_fps), -1);
504 void ClipItem::slotGetEndThumb()
506 m_endThumbRequested = true;
507 emit getThumb(-1, (int)(m_speedIndependantInfo.cropStart + m_speedIndependantInfo.cropDuration).frames(m_fps) - 1);
511 void ClipItem::slotSetStartThumb(QImage img)
513 if (!img.isNull() && img.format() == QImage::Format_ARGB32) {
514 QPixmap pix = QPixmap::fromImage(img);
516 QRectF r = sceneBoundingRect();
517 r.setRight(pix.width() + 2);
522 void ClipItem::slotSetEndThumb(QImage img)
524 if (!img.isNull() && img.format() == QImage::Format_ARGB32) {
525 QPixmap pix = QPixmap::fromImage(img);
527 QRectF r = sceneBoundingRect();
528 r.setLeft(r.right() - pix.width() - 2);
533 void ClipItem::slotThumbReady(int frame, QImage img)
535 if (scene() == NULL) return;
536 QRectF r = boundingRect();
537 QPixmap pix = QPixmap::fromImage(img);
538 double width = pix.width() / projectScene()->scale().x();
539 if (m_startThumbRequested && frame == m_speedIndependantInfo.cropStart.frames(m_fps)) {
541 m_startThumbRequested = false;
542 update(r.left(), r.top(), width, pix.height());
543 if (m_clipType == IMAGE || m_clipType == TEXT) {
544 update(r.right() - width, r.top(), width, pix.height());
546 } else if (m_endThumbRequested && frame == (m_speedIndependantInfo.cropStart + m_speedIndependantInfo.cropDuration).frames(m_fps) - 1) {
548 m_endThumbRequested = false;
549 update(r.right() - width, r.top(), width, pix.height());
553 void ClipItem::slotSetStartThumb(const QPixmap pix)
558 void ClipItem::slotSetEndThumb(const QPixmap pix)
563 QPixmap ClipItem::startThumb() const
568 QPixmap ClipItem::endThumb() const
573 void ClipItem::slotGotAudioData()
575 m_audioThumbReady = true;
576 if (m_clipType == AV && !isAudioOnly()) {
577 QRectF r = boundingRect();
578 r.setTop(r.top() + r.height() / 2 - 1);
583 int ClipItem::type() const
588 DocClipBase *ClipItem::baseClip() const
593 QDomElement ClipItem::xml() const
595 QDomElement xml = m_clip->toXML();
596 if (m_speed != 1.0) xml.setAttribute("speed", m_speed);
597 if (m_strobe > 1) xml.setAttribute("strobe", m_strobe);
598 if (m_audioOnly) xml.setAttribute("audio_only", 1);
599 else if (m_videoOnly) xml.setAttribute("video_only", 1);
603 int ClipItem::clipType() const
608 QString ClipItem::clipName() const
613 void ClipItem::setClipName(const QString &name)
618 const QString ClipItem::clipProducer() const
623 void ClipItem::flashClip()
625 if (m_timeLine == 0) {
626 m_timeLine = new QTimeLine(750, this);
627 m_timeLine->setUpdateInterval(80);
628 m_timeLine->setCurveShape(QTimeLine::EaseInOutCurve);
629 m_timeLine->setFrameRange(0, 100);
630 connect(m_timeLine, SIGNAL(valueChanged(qreal)), this, SLOT(animate(qreal)));
632 //m_timeLine->start();
635 void ClipItem::animate(qreal /*value*/)
637 QRectF r = boundingRect();
643 void ClipItem::paint(QPainter *painter,
644 const QStyleOptionGraphicsItem *option,
648 if (parentItem()) paintColor = QColor(255, 248, 149);
649 else paintColor = m_baseColor;
650 if (isSelected() || (parentItem() && parentItem()->isSelected())) paintColor = paintColor.darker();
652 painter->setMatrixEnabled(false);
653 const QRectF mapped = painter->matrix().mapRect(rect()).adjusted(0.5, 0, 0.5, 0);
654 const QRectF exposed = option->exposedRect;
655 painter->setClipRect(mapped);
656 painter->fillRect(mapped, paintColor);
659 if (KdenliveSettings::videothumbnails() && !isAudioOnly()) {
660 QPen pen = painter->pen();
661 pen.setColor(QColor(255, 255, 255, 150));
662 painter->setPen(pen);
663 if ((m_clipType == IMAGE || m_clipType == TEXT) && !m_startPix.isNull()) {
664 const QPointF top = mapped.topRight() - QPointF(m_startPix.width() - 1, 0);
665 painter->drawPixmap(top, m_startPix);
666 QLineF l2(top.x(), mapped.top(), top.x(), mapped.bottom());
667 painter->drawLine(l2);
668 } else if (!m_endPix.isNull()) {
669 const QPointF top = mapped.topRight() - QPointF(m_endPix.width() - 1, 0);
670 painter->drawPixmap(top, m_endPix);
671 QLineF l2(top.x(), mapped.top(), top.x(), mapped.bottom());
672 painter->drawLine(l2);
674 if (!m_startPix.isNull()) {
675 painter->drawPixmap(mapped.topLeft(), m_startPix);
676 QLineF l2(mapped.left() + m_startPix.width(), mapped.top(), mapped.left() + m_startPix.width(), mapped.bottom());
677 painter->drawLine(l2);
679 painter->setPen(Qt::black);
682 // draw audio thumbnails
683 if (KdenliveSettings::audiothumbnails() && m_speed == 1.0 && !isVideoOnly() && ((m_clipType == AV && (exposed.bottom() > (rect().height() / 2) || isAudioOnly())) || m_clipType == AUDIO) && m_audioThumbReady) {
685 double startpixel = exposed.left();
688 double endpixel = exposed.right();
691 //kDebug()<<"/// REPAINTING AUDIO THMBS ZONE: "<<startpixel<<"x"<<endpixel;
693 /*QPainterPath path = m_clipType == AV ? roundRectPathLower : resultClipPath;*/
695 if (m_clipType == AV && !isAudioOnly()) {
697 mappedRect.setTop(mappedRect.bottom() - mapped.height() / 2);
698 } else mappedRect = mapped;
700 double scale = painter->matrix().m11();
701 int channels = baseClip()->getProperty("channels").toInt();
702 if (scale != m_framePixelWidth)
703 m_audioThumbCachePic.clear();
704 double cropLeft = m_info.cropStart.frames(m_fps);
705 const int clipStart = mappedRect.x();
706 const int mappedStartPixel = painter->matrix().map(QPointF(startpixel + cropLeft, 0)).x() - clipStart;
707 const int mappedEndPixel = painter->matrix().map(QPointF(endpixel + cropLeft, 0)).x() - clipStart;
708 cropLeft = cropLeft * scale;
711 emit prepareAudioThumb(scale, mappedStartPixel, mappedEndPixel, channels);
714 for (int startCache = mappedStartPixel - (mappedStartPixel) % 100; startCache < mappedEndPixel; startCache += 100) {
715 if (m_audioThumbCachePic.contains(startCache) && !m_audioThumbCachePic[startCache].isNull())
716 painter->drawPixmap(clipStart + startCache - cropLeft, mappedRect.y(), m_audioThumbCachePic[startCache]);
720 // Draw effects names
721 if (!m_effectNames.isEmpty() && mapped.width() > 40) {
722 QRectF txtBounding = painter->boundingRect(mapped, Qt::AlignLeft | Qt::AlignTop, m_effectNames);
724 if (m_timeLine && m_timeLine->state() == QTimeLine::Running) {
725 qreal value = m_timeLine->currentValue();
726 txtBounding.setWidth(txtBounding.width() * value);
727 bgColor.setRgb(50 + 200 *(1.0 - value), 50, 50, 100 + 50 * value);
728 } else bgColor.setRgb(50, 50, 90, 180);
730 QPainterPath rounded;
731 rounded.moveTo(txtBounding.bottomRight());
732 rounded.arcTo(txtBounding.right() - txtBounding.height() - 2, txtBounding.top() - txtBounding.height(), txtBounding.height() * 2, txtBounding.height() * 2, 270, 90);
733 rounded.lineTo(txtBounding.topLeft());
734 rounded.lineTo(txtBounding.bottomLeft());
735 painter->fillPath(rounded, bgColor);
736 painter->setPen(Qt::lightGray);
737 painter->drawText(txtBounding.adjusted(1, 0, 1, 0), Qt::AlignCenter, m_effectNames);
741 QColor frameColor(paintColor.darker());
742 if (isSelected() || (parentItem() && parentItem()->isSelected())) {
743 frameColor = QColor(Qt::red);
745 frameColor.setAlpha(160);
747 const QRectF txtBounding2 = painter->boundingRect(mapped, Qt::AlignHCenter | Qt::AlignVCenter, ' ' + m_clipName + ' ');
748 //painter->fillRect(txtBounding2, frameColor);
749 painter->setBrush(frameColor);
750 painter->setPen(Qt::NoPen);
751 painter->drawRoundedRect(txtBounding2, 3, 3);
752 painter->setBrush(QBrush(Qt::NoBrush));
754 //painter->setPen(QColor(0, 0, 0, 180));
755 //painter->drawText(txtBounding, Qt::AlignCenter, m_clipName);
757 painter->drawPixmap(txtBounding2.topLeft() - QPointF(17, -1), m_videoPix);
758 } else if (m_audioOnly) {
759 painter->drawPixmap(txtBounding2.topLeft() - QPointF(17, -1), m_audioPix);
761 painter->setPen(Qt::white);
762 painter->drawText(txtBounding2, Qt::AlignCenter, m_clipName);
766 if (isEnabled() && m_clip) {
767 QList < CommentedTime > markers = m_clip->commentedSnapMarkers();
768 QList < CommentedTime >::Iterator it = markers.begin();
771 QBrush markerBrush(QColor(120, 120, 0, 140));
772 QPen pen = painter->pen();
773 pen.setColor(QColor(255, 255, 255, 200));
774 pen.setStyle(Qt::DotLine);
776 for (; it != markers.end(); ++it) {
777 pos = GenTime((int)((*it).time().frames(m_fps) / m_speed + 0.5), m_fps) - cropStart();
778 if (pos > GenTime()) {
779 if (pos > cropDuration()) break;
780 QLineF l(rect().x() + pos.frames(m_fps), rect().y(), rect().x() + pos.frames(m_fps), rect().bottom());
781 QLineF l2 = painter->matrix().map(l);
782 painter->setPen(pen);
783 painter->drawLine(l2);
784 if (KdenliveSettings::showmarkers()) {
785 framepos = rect().x() + pos.frames(m_fps);
786 const QRectF r1(framepos + 0.04, 10, rect().width() - framepos - 2, rect().height() - 10);
787 const QRectF r2 = painter->matrix().mapRect(r1);
788 const QRectF txtBounding3 = painter->boundingRect(r2, Qt::AlignLeft | Qt::AlignTop, ' ' + (*it).comment() + ' ');
789 painter->setBrush(markerBrush);
790 painter->setPen(Qt::NoPen);
791 painter->drawRoundedRect(txtBounding3, 3, 3);
792 painter->setBrush(QBrush(Qt::NoBrush));
793 painter->setPen(Qt::white);
794 painter->drawText(txtBounding3, Qt::AlignCenter, (*it).comment());
796 //painter->fillRect(QRect(br.x() + framepos, br.y(), 10, br.height()), QBrush(QColor(0, 0, 0, 150)));
801 // draw start / end fades
804 fades = QBrush(QColor(200, 50, 50, 150));
805 } else fades = QBrush(QColor(200, 200, 200, 200));
807 if (m_startFade != 0) {
808 QPainterPath fadeInPath;
809 fadeInPath.moveTo(0, 0);
810 fadeInPath.lineTo(0, rect().height());
811 fadeInPath.lineTo(m_startFade, 0);
812 fadeInPath.closeSubpath();
813 QPainterPath f1 = painter->matrix().map(fadeInPath);
814 painter->fillPath(f1/*.intersected(resultClipPath)*/, fades);
815 /*if (isSelected()) {
816 QLineF l(m_startFade * scale, 0, 0, itemHeight);
817 painter->drawLine(l);
820 if (m_endFade != 0) {
821 QPainterPath fadeOutPath;
822 fadeOutPath.moveTo(rect().width(), 0);
823 fadeOutPath.lineTo(rect().width(), rect().height());
824 fadeOutPath.lineTo(rect().width() - m_endFade, 0);
825 fadeOutPath.closeSubpath();
826 QPainterPath f1 = painter->matrix().map(fadeOutPath);
827 painter->fillPath(f1/*.intersected(resultClipPath)*/, fades);
828 /*if (isSelected()) {
829 QLineF l(itemWidth - m_endFade * scale, 0, itemWidth, itemHeight);
830 painter->drawLine(l);
835 painter->setPen(QPen(Qt::lightGray));
836 // draw effect or transition keyframes
837 if (mapped.width() > 20) drawKeyFrames(painter, exposed);
839 //painter->setMatrixEnabled(true);
842 // expand clip rect to allow correct painting of clip border
843 QPen pen1(frameColor);
844 painter->setPen(pen1);
845 painter->setClipping(false);
846 painter->drawRect(painter->matrix().mapRect(rect()));
850 OPERATIONTYPE ClipItem::operationMode(QPointF pos)
852 if (isItemLocked()) return NONE;
853 const double scale = projectScene()->scale().x();
854 double maximumOffset = 6 / scale;
855 if (isSelected() || (parentItem() && parentItem()->isSelected())) {
856 int kf = mouseOverKeyFrames(pos, maximumOffset);
858 m_editedKeyframe = kf;
862 QRectF rect = sceneBoundingRect();
863 int addtransitionOffset = 10;
864 // Don't allow add transition if track height is very small
865 if (rect.height() < 30) addtransitionOffset = 0;
867 if (qAbs((int)(pos.x() - (rect.x() + m_startFade))) < maximumOffset && qAbs((int)(pos.y() - rect.y())) < 6) {
868 if (m_startFade == 0) setToolTip(i18n("Add audio fade"));
869 // xgettext:no-c-format
870 else setToolTip(i18n("Audio fade duration: %1s", GenTime(m_startFade, m_fps).seconds()));
872 } else if (pos.x() - rect.x() < maximumOffset && (rect.bottom() - pos.y() > addtransitionOffset)) {
873 // xgettext:no-c-format
874 setToolTip(i18n("Crop from start: %1s", cropStart().seconds()));
876 } else if (qAbs((int)(pos.x() - (rect.x() + rect.width() - m_endFade))) < maximumOffset && qAbs((int)(pos.y() - rect.y())) < 6) {
877 if (m_endFade == 0) setToolTip(i18n("Add audio fade"));
878 // xgettext:no-c-format
879 else setToolTip(i18n("Audio fade duration: %1s", GenTime(m_endFade, m_fps).seconds()));
881 } else if ((rect.right() - pos.x() < maximumOffset) && (rect.bottom() - pos.y() > addtransitionOffset)) {
882 // xgettext:no-c-format
883 setToolTip(i18n("Clip duration: %1s", cropDuration().seconds()));
885 } else if ((pos.x() - rect.x() < 16 / scale) && (rect.bottom() - pos.y() <= addtransitionOffset)) {
886 setToolTip(i18n("Add transition"));
887 return TRANSITIONSTART;
888 } else if ((rect.right() - pos.x() < 16 / scale) && (rect.bottom() - pos.y() <= addtransitionOffset)) {
889 setToolTip(i18n("Add transition"));
890 return TRANSITIONEND;
892 setToolTip(QString());
896 QList <GenTime> ClipItem::snapMarkers() const
898 QList < GenTime > snaps;
899 QList < GenTime > markers = baseClip()->snapMarkers();
902 for (int i = 0; i < markers.size(); i++) {
904 pos = GenTime((int)(markers.at(i).frames(m_fps) / m_speed + 0.5), m_fps) - cropStart();
905 if (pos > GenTime()) {
906 if (pos > cropDuration()) break;
907 else snaps.append(pos + startPos());
913 QList <CommentedTime> ClipItem::commentedSnapMarkers() const
915 QList < CommentedTime > snaps;
916 QList < CommentedTime > markers = baseClip()->commentedSnapMarkers();
919 for (int i = 0; i < markers.size(); i++) {
920 pos = GenTime((int)(markers.at(i).time().frames(m_fps) / m_speed + 0.5), m_fps) - cropStart();
921 if (pos > GenTime()) {
922 if (pos > cropDuration()) break;
923 else snaps.append(CommentedTime(pos + startPos(), markers.at(i).comment()));
929 void ClipItem::slotPrepareAudioThumb(double pixelForOneFrame, int startpixel, int endpixel, int channels)
931 QRectF re = sceneBoundingRect();
932 if (m_clipType == AV && !isAudioOnly()) re.setTop(re.y() + re.height() / 2);
934 //kDebug() << "// PREP AUDIO THMB FRMO : scale:" << pixelForOneFrame<< ", from: " << startpixel << ", to: " << endpixel;
935 //if ( (!audioThumbWasDrawn || framePixelWidth!=pixelForOneFrame ) && !baseClip()->audioFrameChache.isEmpty()){
937 for (int startCache = startpixel - startpixel % 100; startCache < endpixel; startCache += 100) {
938 //kDebug() << "creating " << startCache;
939 //if (framePixelWidth!=pixelForOneFrame ||
940 if (m_framePixelWidth == pixelForOneFrame && m_audioThumbCachePic.contains(startCache))
942 if (m_audioThumbCachePic[startCache].isNull() || m_framePixelWidth != pixelForOneFrame) {
943 m_audioThumbCachePic[startCache] = QPixmap(100, (int)(re.height()));
944 m_audioThumbCachePic[startCache].fill(QColor(180, 180, 200, 140));
946 bool fullAreaDraw = pixelForOneFrame < 10;
947 QMap<int, QPainterPath > positiveChannelPaths;
948 QMap<int, QPainterPath > negativeChannelPaths;
949 QPainter pixpainter(&m_audioThumbCachePic[startCache]);
951 audiopen.setWidth(0);
952 pixpainter.setPen(audiopen);
953 //pixpainter.setRenderHint(QPainter::Antialiasing,true);
954 //pixpainter.drawLine(0,0,100,re.height());
955 // Bail out, if caller provided invalid data
957 kWarning() << "Unable to draw image with " << channels << "number of channels";
961 int channelHeight = m_audioThumbCachePic[startCache].height() / channels;
963 for (int i = 0; i < channels; i++) {
965 positiveChannelPaths[i].moveTo(0, channelHeight*i + channelHeight / 2);
966 negativeChannelPaths[i].moveTo(0, channelHeight*i + channelHeight / 2);
969 for (int samples = 0; samples <= 100; samples++) {
970 double frame = (double)(samples + startCache - 0) / pixelForOneFrame;
971 int sample = (int)((frame - (int)(frame)) * 20); // AUDIO_FRAME_SIZE
972 if (frame < 0 || sample < 0 || sample > 19)
974 QMap<int, QByteArray> frame_channel_data = baseClip()->m_audioFrameCache[(int)frame];
976 for (int channel = 0; channel < channels && frame_channel_data[channel].size() > 0; channel++) {
978 int y = channelHeight * channel + channelHeight / 2;
979 int delta = (int)(frame_channel_data[channel][sample] - 127 / 2) * channelHeight / 64;
981 positiveChannelPaths[channel].lineTo(samples, 0.1 + y + qAbs(delta));
982 negativeChannelPaths[channel].lineTo(samples, 0.1 + y - qAbs(delta));
984 positiveChannelPaths[channel].lineTo(samples, 0.1 + y + delta);
985 negativeChannelPaths[channel].lineTo(samples, 0.1 + y - delta);
988 for (int channel = 0; channel < channels ; channel++)
989 if (fullAreaDraw && samples == 100) {
990 positiveChannelPaths[channel].lineTo(samples, channelHeight*channel + channelHeight / 2);
991 negativeChannelPaths[channel].lineTo(samples, channelHeight*channel + channelHeight / 2);
992 positiveChannelPaths[channel].lineTo(0, channelHeight*channel + channelHeight / 2);
993 negativeChannelPaths[channel].lineTo(0, channelHeight*channel + channelHeight / 2);
997 pixpainter.setPen(QPen(QColor(0, 0, 0)));
998 pixpainter.setBrush(QBrush(QColor(60, 60, 60)));
1000 for (int i = 0; i < channels; i++) {
1002 //pixpainter.fillPath(positiveChannelPaths[i].united(negativeChannelPaths[i]),QBrush(Qt::SolidPattern));//or singleif looks better
1003 pixpainter.drawPath(positiveChannelPaths[i].united(negativeChannelPaths[i]));//or singleif looks better
1005 pixpainter.drawPath(positiveChannelPaths[i]);
1008 //audioThumbWasDrawn=true;
1009 m_framePixelWidth = pixelForOneFrame;
1014 int ClipItem::fadeIn() const
1019 int ClipItem::fadeOut() const
1025 void ClipItem::setFadeIn(int pos)
1027 if (pos == m_startFade) return;
1028 int oldIn = m_startFade;
1029 if (pos < 0) pos = 0;
1030 if (pos > cropDuration().frames(m_fps)) pos = (int)(cropDuration().frames(m_fps));
1032 QRectF rect = boundingRect();
1033 update(rect.x(), rect.y(), qMax(oldIn, pos), rect.height());
1036 void ClipItem::setFadeOut(int pos)
1038 if (pos == m_endFade) return;
1039 int oldOut = m_endFade;
1040 if (pos < 0) pos = 0;
1041 if (pos > cropDuration().frames(m_fps)) pos = (int)(cropDuration().frames(m_fps));
1043 QRectF rect = boundingRect();
1044 update(rect.x() + rect.width() - qMax(oldOut, pos), rect.y(), qMax(oldOut, pos), rect.height());
1050 void ClipItem::hoverEnterEvent(QGraphicsSceneHoverEvent *e)
1052 //if (e->pos().x() < 20) m_hover = true;
1054 if (isItemLocked()) return;
1056 QRectF r = boundingRect();
1057 double width = 35 / projectScene()->scale().x();
1058 double height = r.height() / 2;
1059 //WARNING: seems like it generates a full repaint of the clip, maybe not so good...
1060 update(r.x(), r.y() + height, width, height);
1061 update(r.right() - width, r.y() + height, width, height);
1065 void ClipItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *)
1067 if (isItemLocked()) return;
1069 QRectF r = boundingRect();
1070 double width = 35 / projectScene()->scale().x();
1071 double height = r.height() / 2;
1072 //WARNING: seems like it generates a full repaint of the clip, maybe not so good...
1073 update(r.x(), r.y() + height, width, height);
1074 update(r.right() - width, r.y() + height, width, height);
1078 void ClipItem::resizeStart(int posx)
1080 const int min = (startPos() - cropStart()).frames(m_fps);
1081 if (posx < min) posx = min;
1082 if (posx == startPos().frames(m_fps)) return;
1083 const int previous = cropStart().frames(m_fps);
1084 AbstractClipItem::resizeStart(posx);
1086 // set speed independant info
1087 m_speedIndependantInfo = m_info;
1088 m_speedIndependantInfo.cropStart = GenTime((int)(m_info.cropStart.frames(m_fps) * m_speed), m_fps);
1089 m_speedIndependantInfo.cropDuration = GenTime((int)(m_info.cropDuration.frames(m_fps) * m_speed), m_fps);
1091 if ((int) cropStart().frames(m_fps) != previous) {
1092 if (m_hasThumbs && KdenliveSettings::videothumbnails()) {
1093 m_startThumbTimer.start(150);
1098 void ClipItem::resizeEnd(int posx)
1100 const int max = (startPos() - cropStart() + maxDuration()).frames(m_fps);
1101 if (posx > max && maxDuration() != GenTime()) posx = max;
1102 if (posx == endPos().frames(m_fps)) return;
1103 //kDebug() << "// NEW POS: " << posx << ", OLD END: " << endPos().frames(m_fps);
1104 const int previous = cropDuration().frames(m_fps);
1105 AbstractClipItem::resizeEnd(posx);
1107 // set speed independant info
1108 m_speedIndependantInfo = m_info;
1109 m_speedIndependantInfo.cropStart = GenTime((int)(m_info.cropStart.frames(m_fps) * m_speed), m_fps);
1110 m_speedIndependantInfo.cropDuration = GenTime((int)(m_info.cropDuration.frames(m_fps) * m_speed), m_fps);
1112 if ((int) cropDuration().frames(m_fps) != previous) {
1113 if (m_hasThumbs && KdenliveSettings::videothumbnails()) {
1114 m_endThumbTimer.start(150);
1120 bool ClipItem::checkEffectsKeyframesPos(const int previous, const int current, bool fromStart)
1122 bool modified = false;
1123 for (int i = 0; i < m_effectList.count(); i++) {
1124 QDomElement effect = m_effectList.at(i);
1125 QDomNodeList params = effect.elementsByTagName("parameter");
1126 for (int j = 0; j < params.count(); j++) {
1127 QDomElement e = params.item(i).toElement();
1128 if (!e.isNull() && (e.attribute("type") == "keyframe" || e.attribute("type") == "simplekeyframe")) {
1129 // parse keyframes and adjust values
1130 const QStringList keyframes = e.attribute("keyframes").split(';', QString::SkipEmptyParts);
1131 QMap <int, double> kfr;
1134 foreach(const QString &str, keyframes) {
1135 pos = str.section(':', 0, 0).toInt();
1136 val = str.section(':', 1, 1).toDouble();
1137 if (pos == previous) {
1141 if ((fromStart && pos >= current) || (!fromStart && pos <= current)) {
1149 QMap<int, double>::const_iterator k = kfr.constBegin();
1150 while (k != kfr.constEnd()) {
1151 newkfr.append(QString::number(k.key()) + ':' + QString::number(k.value()) + ';');
1154 e.setAttribute("keyframes", newkfr);
1160 if (modified && m_selectedEffect >= 0) setSelectedEffect(m_selectedEffect);
1165 QVariant ClipItem::itemChange(GraphicsItemChange change, const QVariant &value)
1167 if (change == QGraphicsItem::ItemSelectedChange) {
1168 if (value.toBool()) setZValue(10);
1171 if (change == ItemPositionChange && scene()) {
1172 // calculate new position.
1173 //if (parentItem()) return pos();
1174 QPointF newPos = value.toPointF();
1175 //kDebug() << "/// MOVING CLIP ITEM.------------\n++++++++++";
1176 int xpos = projectScene()->getSnapPointForPos((int) newPos.x(), KdenliveSettings::snaptopoints());
1177 xpos = qMax(xpos, 0);
1179 int newTrack = newPos.y() / KdenliveSettings::trackheight();
1180 newTrack = qMin(newTrack, projectScene()->tracksCount() - 1);
1181 newTrack = qMax(newTrack, 0);
1182 newPos.setY((int)(newTrack * KdenliveSettings::trackheight() + 1));
1183 // Only one clip is moving
1184 QRectF sceneShape = rect();
1185 sceneShape.translate(newPos);
1186 QList<QGraphicsItem*> items;
1187 if (projectScene()->editMode() == NORMALEDIT)
1188 items = scene()->items(sceneShape, Qt::IntersectsItemShape);
1189 items.removeAll(this);
1190 bool forwardMove = newPos.x() > pos().x();
1192 if (!items.isEmpty()) {
1193 for (int i = 0; i < items.count(); i++) {
1194 if (!items.at(i)->isEnabled()) continue;
1195 if (items.at(i)->type() == type()) {
1197 QPointF otherPos = items.at(i)->pos();
1198 if ((int) otherPos.y() != (int) pos().y()) {
1202 offset = qMax(offset, (int)(newPos.x() - (static_cast < AbstractClipItem* >(items.at(i))->startPos() - cropDuration()).frames(m_fps)));
1204 offset = qMax(offset, (int)((static_cast < AbstractClipItem* >(items.at(i))->endPos().frames(m_fps)) - newPos.x()));
1209 sceneShape.translate(QPointF(-offset, 0));
1210 newPos.setX(newPos.x() - offset);
1212 sceneShape.translate(QPointF(offset, 0));
1213 newPos.setX(newPos.x() + offset);
1215 QList<QGraphicsItem*> subitems = scene()->items(sceneShape, Qt::IntersectsItemShape);
1216 subitems.removeAll(this);
1217 for (int j = 0; j < subitems.count(); j++) {
1218 if (!subitems.at(j)->isEnabled()) continue;
1219 if (subitems.at(j)->type() == type()) {
1220 // move was not successful, revert to previous pos
1221 m_info.startPos = GenTime((int) pos().x(), m_fps);
1227 m_info.track = newTrack;
1228 m_info.startPos = GenTime((int) newPos.x(), m_fps);
1234 m_info.track = newTrack;
1235 m_info.startPos = GenTime((int) newPos.x(), m_fps);
1236 //kDebug()<<"// ITEM NEW POS: "<<newPos.x()<<", mapped: "<<mapToScene(newPos.x(), 0).x();
1239 return QGraphicsItem::itemChange(change, value);
1243 /*void ClipItem::mouseMoveEvent(QGraphicsSceneMouseEvent * event) {
1246 int ClipItem::effectsCounter()
1248 return effectsCount() + 1;
1251 int ClipItem::effectsCount()
1253 return m_effectList.count();
1256 int ClipItem::hasEffect(const QString &tag, const QString &id) const
1258 return m_effectList.hasEffect(tag, id);
1261 QStringList ClipItem::effectNames()
1263 return m_effectList.effectNames();
1266 QDomElement ClipItem::effectAt(int ix) const
1268 if (ix > m_effectList.count() - 1 || ix < 0 || m_effectList.at(ix).isNull()) return QDomElement();
1269 return m_effectList.at(ix).cloneNode().toElement();
1272 QDomElement ClipItem::getEffectAt(int ix) const
1274 if (ix > m_effectList.count() - 1 || ix < 0 || m_effectList.at(ix).isNull()) return QDomElement();
1275 return m_effectList.at(ix);
1278 void ClipItem::setEffectAt(int ix, QDomElement effect)
1280 if (ix < 0 || ix > (m_effectList.count() - 1) || effect.isNull()) {
1281 kDebug() << "Invalid effect index: " << ix;
1284 //kDebug() << "CHange EFFECT AT: " << ix << ", CURR: " << m_effectList.at(ix).attribute("tag") << ", NEW: " << effect.attribute("tag");
1285 effect.setAttribute("kdenlive_ix", ix + 1);
1286 m_effectList.replace(ix, effect);
1287 m_effectNames = m_effectList.effectNames().join(" / ");
1288 QString id = effect.attribute("id");
1289 if (id == "fadein" || id == "fadeout" || id == "fade_from_black" || id == "fade_to_black")
1292 QRectF r = boundingRect();
1298 EffectsParameterList ClipItem::addEffect(const QDomElement effect, bool /*animate*/)
1300 bool needRepaint = false;
1302 if (!effect.hasAttribute("kdenlive_ix")) {
1303 ix = effectsCounter();
1304 } else ix = effect.attribute("kdenlive_ix").toInt();
1305 if (!m_effectList.isEmpty() && ix <= m_effectList.count()) {
1307 m_effectList.insert(ix - 1, effect);
1308 for (int i = ix; i < m_effectList.count(); i++) {
1309 int index = m_effectList.item(i).attribute("kdenlive_ix").toInt();
1310 if (index >= ix) m_effectList.item(i).setAttribute("kdenlive_ix", index + 1);
1312 } else m_effectList.append(effect);
1313 EffectsParameterList parameters;
1314 parameters.addParam("tag", effect.attribute("tag"));
1315 parameters.addParam("kdenlive_ix", effect.attribute("kdenlive_ix"));
1316 if (effect.hasAttribute("src")) parameters.addParam("src", effect.attribute("src"));
1317 if (effect.hasAttribute("disable")) parameters.addParam("disable", effect.attribute("disable"));
1320 QString effectId = effect.attribute("id");
1321 if (effectId.isEmpty()) effectId = effect.attribute("tag");
1322 parameters.addParam("id", effectId);
1324 QDomNodeList params = effect.elementsByTagName("parameter");
1326 for (int i = 0; i < params.count(); i++) {
1327 QDomElement e = params.item(i).toElement();
1329 if (e.attribute("type") == "simplekeyframe") {
1330 QStringList values = e.attribute("keyframes").split(";", QString::SkipEmptyParts);
1331 double factor = e.attribute("factor", "1").toDouble();
1333 for (int j = 0; j < values.count(); j++) {
1334 QString pos = values.at(j).section(":", 0, 0);
1335 double val = values.at(j).section(":", 1, 1).toDouble() / factor;
1336 values[j] = pos + "=" + QString::number(val);
1339 parameters.addParam(e.attribute("name"), values.join(";"));
1340 /*parameters.addParam("max", e.attribute("max"));
1341 parameters.addParam("min", e.attribute("min"));
1342 parameters.addParam("factor", );*/
1343 } else if (e.attribute("type") == "keyframe") {
1344 parameters.addParam("keyframes", e.attribute("keyframes"));
1345 parameters.addParam("max", e.attribute("max"));
1346 parameters.addParam("min", e.attribute("min"));
1347 parameters.addParam("factor", e.attribute("factor", "1"));
1348 parameters.addParam("starttag", e.attribute("starttag", "start"));
1349 parameters.addParam("endtag", e.attribute("endtag", "end"));
1350 } else if (e.attribute("factor", "1") == "1") {
1351 parameters.addParam(e.attribute("name"), e.attribute("value"));
1353 // check if it is a fade effect
1354 if (effectId == "fadein") {
1356 if (m_effectList.hasEffect(QString(), "fade_from_black") == -1) {
1357 if (e.attribute("name") == "out") fade += e.attribute("value").toInt();
1358 else if (e.attribute("name") == "in") fade -= e.attribute("value").toInt();
1360 QDomElement fadein = m_effectList.getEffectByTag(QString(), "fade_from_black");
1361 if (fadein.attribute("name") == "out") fade += fadein.attribute("value").toInt();
1362 else if (fadein.attribute("name") == "in") fade -= fadein.attribute("value").toInt();
1364 } else if (effectId == "fade_from_black") {
1366 if (m_effectList.hasEffect(QString(), "fadein") == -1) {
1367 if (e.attribute("name") == "out") fade += e.attribute("value").toInt();
1368 else if (e.attribute("name") == "in") fade -= e.attribute("value").toInt();
1370 QDomElement fadein = m_effectList.getEffectByTag(QString(), "fadein");
1371 if (fadein.attribute("name") == "out") fade += fadein.attribute("value").toInt();
1372 else if (fadein.attribute("name") == "in") fade -= fadein.attribute("value").toInt();
1374 } else if (effectId == "fadeout") {
1376 if (m_effectList.hasEffect(QString(), "fade_to_black") == -1) {
1377 if (e.attribute("name") == "out") fade -= e.attribute("value").toInt();
1378 else if (e.attribute("name") == "in") fade += e.attribute("value").toInt();
1380 QDomElement fadeout = m_effectList.getEffectByTag(QString(), "fade_to_black");
1381 if (fadeout.attribute("name") == "out") fade -= fadeout.attribute("value").toInt();
1382 else if (fadeout.attribute("name") == "in") fade += fadeout.attribute("value").toInt();
1384 } else if (effectId == "fade_to_black") {
1386 if (m_effectList.hasEffect(QString(), "fadeout") == -1) {
1387 if (e.attribute("name") == "out") fade -= e.attribute("value").toInt();
1388 else if (e.attribute("name") == "in") fade += e.attribute("value").toInt();
1390 QDomElement fadeout = m_effectList.getEffectByTag(QString(), "fadeout");
1391 if (fadeout.attribute("name") == "out") fade -= fadeout.attribute("value").toInt();
1392 else if (fadeout.attribute("name") == "in") fade += fadeout.attribute("value").toInt();
1397 if (e.attribute("factor").startsWith('%')) {
1398 fact = ProfilesDialog::getStringEval(projectScene()->profile(), e.attribute("factor"));
1399 } else fact = e.attribute("factor", "1").toDouble();
1400 parameters.addParam(e.attribute("name"), QString::number(e.attribute("value").toDouble() / fact));
1404 m_effectNames = m_effectList.effectNames().join(" / ");
1405 if (fade > 0) m_startFade = fade;
1406 else if (fade < 0) m_endFade = -fade;
1408 if (m_selectedEffect == -1) {
1409 setSelectedEffect(0);
1410 } else if (m_selectedEffect == ix - 1) setSelectedEffect(m_selectedEffect);
1411 if (needRepaint) update(boundingRect());
1415 else { /*if (!needRepaint) */
1416 QRectF r = boundingRect();
1423 EffectsParameterList ClipItem::getEffectArgs(const QDomElement effect)
1425 EffectsParameterList parameters;
1426 parameters.addParam("tag", effect.attribute("tag"));
1427 parameters.addParam("kdenlive_ix", effect.attribute("kdenlive_ix"));
1428 parameters.addParam("id", effect.attribute("id"));
1429 if (effect.hasAttribute("src")) parameters.addParam("src", effect.attribute("src"));
1430 if (effect.hasAttribute("disable")) parameters.addParam("disable", effect.attribute("disable"));
1432 QDomNodeList params = effect.elementsByTagName("parameter");
1433 for (int i = 0; i < params.count(); i++) {
1434 QDomElement e = params.item(i).toElement();
1435 //kDebug() << "/ / / /SENDING EFFECT PARAM: " << e.attribute("type") << ", NAME_ " << e.attribute("tag");
1436 if (e.attribute("type") == "simplekeyframe") {
1438 QStringList values = e.attribute("keyframes").split(";", QString::SkipEmptyParts);
1439 double factor = e.attribute("factor", "1").toDouble();
1440 for (int j = 0; j < values.count(); j++) {
1441 QString pos = values.at(j).section(":", 0, 0);
1442 double val = values.at(j).section(":", 1, 1).toDouble() / factor;
1443 values[j] = pos + "=" + QString::number(val);
1445 // kDebug() << "/ / / /SENDING KEYFR:" << values;
1446 parameters.addParam(e.attribute("name"), values.join(";"));
1447 /*parameters.addParam(e.attribute("name"), e.attribute("keyframes").replace(":", "="));
1448 parameters.addParam("max", e.attribute("max"));
1449 parameters.addParam("min", e.attribute("min"));
1450 parameters.addParam("factor", e.attribute("factor", "1"));*/
1451 } else if (e.attribute("type") == "keyframe") {
1452 kDebug() << "/ / / /SENDING KEYFR EFFECT TYPE";
1453 parameters.addParam("keyframes", e.attribute("keyframes"));
1454 parameters.addParam("max", e.attribute("max"));
1455 parameters.addParam("min", e.attribute("min"));
1456 parameters.addParam("factor", e.attribute("factor", "1"));
1457 parameters.addParam("starttag", e.attribute("starttag", "start"));
1458 parameters.addParam("endtag", e.attribute("endtag", "end"));
1459 } else if (e.attribute("namedesc").contains(';')) {
1460 QString format = e.attribute("format");
1461 QStringList separators = format.split("%d", QString::SkipEmptyParts);
1462 QStringList values = e.attribute("value").split(QRegExp("[,:;x]"));
1464 QTextStream txtNeu(&neu);
1465 if (values.size() > 0)
1466 txtNeu << (int)values[0].toDouble();
1467 for (int i = 0; i < separators.size() && i + 1 < values.size(); i++) {
1468 txtNeu << separators[i];
1469 txtNeu << (int)(values[i+1].toDouble());
1471 parameters.addParam("start", neu);
1473 if (e.attribute("factor", "1") != "1") {
1475 if (e.attribute("factor").startsWith('%')) {
1476 fact = ProfilesDialog::getStringEval(projectScene()->profile(), e.attribute("factor"));
1477 } else fact = e.attribute("factor", "1").toDouble();
1478 parameters.addParam(e.attribute("name"), QString::number(e.attribute("value").toDouble() / fact));
1480 parameters.addParam(e.attribute("name"), e.attribute("value"));
1487 void ClipItem::deleteEffect(QString index)
1489 bool needRepaint = false;
1492 for (int i = 0; i < m_effectList.count(); ++i) {
1493 ix = m_effectList.at(i).attribute("kdenlive_ix");
1495 QString effectId = m_effectList.at(i).attribute("id");
1496 if ((effectId == "fadein" && hasEffect(QString(), "fade_from_black") == -1) ||
1497 (effectId == "fade_from_black" && hasEffect(QString(), "fadein") == -1)) {
1500 } else if ((effectId == "fadeout" && hasEffect(QString(), "fade_to_black") == -1) ||
1501 (effectId == "fade_to_black" && hasEffect(QString(), "fadeout") == -1)) {
1504 } else if (EffectsList::hasKeyFrames(m_effectList.at(i))) needRepaint = true;
1505 m_effectList.removeAt(i);
1507 } else if (ix.toInt() > index.toInt()) {
1508 m_effectList.item(i).setAttribute("kdenlive_ix", ix.toInt() - 1);
1511 m_effectNames = m_effectList.effectNames().join(" / ");
1513 if (m_effectList.isEmpty() || m_selectedEffect + 1 == index.toInt()) {
1514 // Current effect was removed
1515 if (index.toInt() > m_effectList.count() - 1) {
1516 setSelectedEffect(m_effectList.count() - 1);
1517 } else setSelectedEffect(index.toInt());
1519 if (needRepaint) update(boundingRect());
1521 QRectF r = boundingRect();
1525 //if (!m_effectList.isEmpty()) flashClip();
1528 double ClipItem::speed() const
1533 int ClipItem::strobe() const
1538 void ClipItem::setSpeed(const double speed, const int strobe)
1542 if (m_speed == 1.0) m_clipName = baseClip()->name();
1543 else m_clipName = baseClip()->name() + " - " + QString::number(speed * 100, 'f', 0) + '%';
1544 m_info.cropStart = GenTime((int)(m_speedIndependantInfo.cropStart.frames(m_fps) / m_speed + 0.5), m_fps);
1545 m_info.cropDuration = GenTime((int)(m_speedIndependantInfo.cropDuration.frames(m_fps) / m_speed + 0.5), m_fps);
1549 GenTime ClipItem::maxDuration() const
1551 return GenTime((int)(m_maxDuration.frames(m_fps) / m_speed + 0.5), m_fps);
1554 GenTime ClipItem::speedIndependantCropStart() const
1556 return m_speedIndependantInfo.cropStart;
1559 GenTime ClipItem::speedIndependantCropDuration() const
1561 return m_speedIndependantInfo.cropDuration;
1565 const ItemInfo ClipItem::speedIndependantInfo() const
1567 return m_speedIndependantInfo;
1571 void ClipItem::dropEvent(QGraphicsSceneDragDropEvent * event)
1573 const QString effects = QString(event->mimeData()->data("kdenlive/effectslist"));
1575 doc.setContent(effects, true);
1576 const QDomElement e = doc.documentElement();
1577 if (scene() && !scene()->views().isEmpty()) {
1579 CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
1580 if (view) view->slotAddEffect(e, m_info.startPos, track());
1585 void ClipItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
1587 if (isItemLocked()) event->setAccepted(false);
1588 else event->setAccepted(event->mimeData()->hasFormat("kdenlive/effectslist"));
1591 void ClipItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
1596 void ClipItem::addTransition(Transition* t)
1598 m_transitionsList.append(t);
1599 //CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
1601 QDomElement e = doc.documentElement();
1602 //if (view) view->slotAddTransition(this, t->toXML() , t->startPos(), track());
1605 void ClipItem::setVideoOnly(bool force)
1607 m_videoOnly = force;
1610 void ClipItem::setAudioOnly(bool force)
1612 m_audioOnly = force;
1613 if (m_audioOnly) m_baseColor = QColor(141, 215, 166);
1615 if (m_clipType == COLOR) {
1616 QString colour = m_clip->getProperty("colour");
1617 colour = colour.replace(0, 2, "#");
1618 m_baseColor = QColor(colour.left(7));
1619 } else if (m_clipType == AUDIO) m_baseColor = QColor(141, 215, 166);
1620 else m_baseColor = QColor(141, 166, 215);
1622 m_audioThumbCachePic.clear();
1625 bool ClipItem::isAudioOnly() const
1630 bool ClipItem::isVideoOnly() const
1635 void ClipItem::insertKeyframe(QDomElement effect, int pos, int val)
1637 if (effect.attribute("disable") == "1") return;
1638 effect.setAttribute("active_keyframe", pos);
1639 m_editedKeyframe = pos;
1640 QDomNodeList params = effect.elementsByTagName("parameter");
1641 for (int i = 0; i < params.count(); i++) {
1642 QDomElement e = params.item(i).toElement();
1643 QString kfr = e.attribute("keyframes");
1644 const QStringList keyframes = kfr.split(';', QString::SkipEmptyParts);
1647 foreach(const QString &str, keyframes) {
1648 int kpos = str.section(':', 0, 0).toInt();
1649 double newval = str.section(':', 1, 1).toDouble();
1652 } else if (!added) {
1653 if (i == 0) newkfr.append(QString::number(pos) + ":" + QString::number(val));
1654 else newkfr.append(QString::number(pos) + ":" + QString::number(newval));
1655 if (kpos > pos) newkfr.append(str);
1657 } else newkfr.append(str);
1659 if (!added) newkfr.append(QString::number(pos) + ":" + QString::number(val));
1660 e.setAttribute("keyframes", newkfr.join(";"));
1664 void ClipItem::movedKeyframe(QDomElement effect, int oldpos, int newpos, double value)
1666 if (effect.attribute("disable") == "1") return;
1667 effect.setAttribute("active_keyframe", newpos);
1668 QDomNodeList params = effect.elementsByTagName("parameter");
1669 int start = cropStart().frames(m_fps);
1670 int end = (cropStart() + cropDuration()).frames(m_fps) - 1;
1671 for (int i = 0; i < params.count(); i++) {
1672 QDomElement e = params.item(i).toElement();
1673 QString kfr = e.attribute("keyframes");
1674 const QStringList keyframes = kfr.split(';', QString::SkipEmptyParts);
1676 foreach(const QString &str, keyframes) {
1677 if (str.section(':', 0, 0).toInt() != oldpos) {
1679 } else if (newpos != -1) {
1680 newpos = qMax(newpos, start);
1681 newpos = qMin(newpos, end);
1682 if (i == 0) newkfr.append(QString::number(newpos) + ":" + QString::number(value));
1683 else newkfr.append(QString::number(newpos) + ":" + str.section(':', 1, 1));
1686 e.setAttribute("keyframes", newkfr.join(";"));
1689 updateKeyframes(effect);
1693 void ClipItem::updateKeyframes(QDomElement effect)
1695 m_keyframes.clear();
1697 QDomNodeList params = effect.elementsByTagName("parameter");
1698 QDomElement e = params.item(0).toElement();
1699 const QStringList keyframes = e.attribute("keyframes").split(';', QString::SkipEmptyParts);
1700 foreach(const QString &str, keyframes) {
1701 int pos = str.section(':', 0, 0).toInt();
1702 double val = str.section(':', 1, 1).toDouble();
1703 m_keyframes[pos] = val;
1705 if (!m_keyframes.contains(m_selectedKeyframe)) m_selectedKeyframe = -1;
1707 #include "clipitem.moc"