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);
87 m_startThumbTimer.setSingleShot(true);
88 connect(&m_startThumbTimer, SIGNAL(timeout()), this, SLOT(slotGetStartThumb()));
89 m_endThumbTimer.setSingleShot(true);
90 connect(&m_endThumbTimer, SIGNAL(timeout()), this, SLOT(slotGetEndThumb()));
92 connect(this, SIGNAL(getThumb(int, int)), m_clip->thumbProducer(), SLOT(extractImage(int, int)));
94 connect(m_clip->thumbProducer(), SIGNAL(thumbReady(int, QImage)), this, SLOT(slotThumbReady(int, QImage)));
95 connect(m_clip, SIGNAL(gotAudioData()), this, SLOT(slotGotAudioData()));
96 if (generateThumbs) QTimer::singleShot(200, this, SLOT(slotFetchThumbs()));
98 } else if (m_clipType == COLOR) {
99 QString colour = m_clip->getProperty("colour");
100 colour = colour.replace(0, 2, "#");
101 m_baseColor = QColor(colour.left(7));
102 } else if (m_clipType == IMAGE || m_clipType == TEXT) {
103 m_baseColor = QColor(141, 166, 215);
104 if (m_clipType == TEXT) {
105 connect(this, SIGNAL(getThumb(int, int)), m_clip->thumbProducer(), SLOT(extractImage(int, int)));
106 connect(m_clip->thumbProducer(), SIGNAL(thumbReady(int, QImage)), this, SLOT(slotThumbReady(int, QImage)));
108 //m_startPix = KThumb::getImage(KUrl(clip->getProperty("resource")), (int)(KdenliveSettings::trackheight() * KdenliveSettings::project_display_ratio()), KdenliveSettings::trackheight());
109 } else if (m_clipType == AUDIO) {
110 m_baseColor = QColor(141, 215, 166);
111 connect(m_clip, SIGNAL(gotAudioData()), this, SLOT(slotGotAudioData()));
116 ClipItem::~ClipItem()
119 if (scene()) scene()->removeItem(this);
120 if (m_clipType == VIDEO || m_clipType == AV || m_clipType == SLIDESHOW || m_clipType == PLAYLIST) {
121 disconnect(m_clip->thumbProducer(), SIGNAL(thumbReady(int, QImage)), this, SLOT(slotThumbReady(int, QImage)));
122 disconnect(m_clip, SIGNAL(gotAudioData()), this, SLOT(slotGotAudioData()));
127 ClipItem *ClipItem::clone(ItemInfo info) const
129 ClipItem *duplicate = new ClipItem(m_clip, info, m_fps, m_speed, m_strobe);
130 if (m_clipType == IMAGE || m_clipType == TEXT) duplicate->slotSetStartThumb(m_startPix);
131 else if (m_clipType != COLOR) {
132 if (info.cropStart == m_info.cropStart) duplicate->slotSetStartThumb(m_startPix);
133 if (info.cropStart + (info.endPos - info.startPos) == m_info.cropStart + (m_info.endPos - m_info.startPos)) duplicate->slotSetEndThumb(m_endPix);
135 //kDebug() << "// CLoning clip: " << (info.cropStart + (info.endPos - info.startPos)).frames(m_fps) << ", CURRENT end: " << (cropStart() + duration()).frames(m_fps);
136 duplicate->setEffectList(m_effectList);
137 duplicate->setVideoOnly(m_videoOnly);
138 duplicate->setAudioOnly(m_audioOnly);
139 //duplicate->setSpeed(m_speed);
143 void ClipItem::setEffectList(const EffectsList effectList)
145 m_effectList.clone(effectList);
146 m_effectNames = m_effectList.effectNames().join(" / ");
147 if (!m_effectList.isEmpty()) setSelectedEffect(0);
150 const EffectsList ClipItem::effectList() const
155 int ClipItem::selectedEffectIndex() const
157 return m_selectedEffect;
160 void ClipItem::initEffect(QDomElement effect, int diff)
162 // the kdenlive_ix int is used to identify an effect in mlt's playlist, should
164 if (effect.attribute("kdenlive_ix").toInt() == 0)
165 effect.setAttribute("kdenlive_ix", QString::number(effectsCounter()));
167 if (effect.attribute("id") == "freeze" && diff > 0) {
168 EffectsList::setParameter(effect, "frame", QString::number(diff));
171 // Init parameter value & keyframes if required
172 QDomNodeList params = effect.elementsByTagName("parameter");
173 for (int i = 0; i < params.count(); i++) {
174 QDomElement e = params.item(i).toElement();
175 kDebug() << "// init eff: " << e.attribute("name");
177 // Check if this effect has a variable parameter
178 if (e.attribute("default").startsWith('%')) {
179 double evaluatedValue = ProfilesDialog::getStringEval(projectScene()->profile(), e.attribute("default"));
180 e.setAttribute("default", evaluatedValue);
181 if (e.hasAttribute("value") && e.attribute("value").startsWith('%')) {
182 e.setAttribute("value", evaluatedValue);
186 if (!e.isNull() && (e.attribute("type") == "keyframe" || e.attribute("type") == "simplekeyframe")) {
187 QString def = e.attribute("default");
188 // Effect has a keyframe type parameter, we need to set the values
189 if (e.attribute("keyframes").isEmpty()) {
190 e.setAttribute("keyframes", QString::number(cropStart().frames(m_fps)) + ':' + def + ';' + QString::number((cropStart() + cropDuration()).frames(m_fps) - 1) + ':' + def);
191 //kDebug() << "///// EFFECT KEYFRAMES INITED: " << e.attribute("keyframes");
196 if (effect.attribute("tag") == "volume" || effect.attribute("tag") == "brightness") {
197 if (effect.attribute("id") == "fadeout" || effect.attribute("id") == "fade_to_black") {
198 int end = (cropDuration() + cropStart()).frames(m_fps);
200 if (effect.attribute("id") == "fadeout") {
201 if (m_effectList.hasEffect(QString(), "fade_to_black") == -1) {
202 int effectDuration = EffectsList::parameter(effect, "in").toInt();
203 if (effectDuration > cropDuration().frames(m_fps)) {
204 effectDuration = cropDuration().frames(m_fps) / 2;
206 start -= effectDuration;
208 QDomElement fadeout = m_effectList.getEffectByTag(QString(), "fade_to_black");
209 start -= EffectsList::parameter(fadeout, "out").toInt() - EffectsList::parameter(fadeout, "in").toInt();
211 } else if (effect.attribute("id") == "fade_to_black") {
212 if (m_effectList.hasEffect(QString(), "fadeout") == -1) {
213 int effectDuration = EffectsList::parameter(effect, "in").toInt();
214 if (effectDuration > cropDuration().frames(m_fps)) {
215 effectDuration = cropDuration().frames(m_fps) / 2;
217 start -= effectDuration;
219 QDomElement fadeout = m_effectList.getEffectByTag(QString(), "fadeout");
220 start -= EffectsList::parameter(fadeout, "out").toInt() - EffectsList::parameter(fadeout, "in").toInt();
223 EffectsList::setParameter(effect, "in", QString::number(start));
224 EffectsList::setParameter(effect, "out", QString::number(end));
225 } else if (effect.attribute("id") == "fadein" || effect.attribute("id") == "fade_from_black") {
226 int start = cropStart().frames(m_fps);
228 if (effect.attribute("id") == "fadein") {
229 if (m_effectList.hasEffect(QString(), "fade_from_black") == -1) {
230 int effectDuration = EffectsList::parameter(effect, "out").toInt();
231 if (effectDuration > cropDuration().frames(m_fps)) {
232 effectDuration = cropDuration().frames(m_fps) / 2;
234 end += effectDuration;
236 end += EffectsList::parameter(m_effectList.getEffectByTag(QString(), "fade_from_black"), "out").toInt();
237 } else if (effect.attribute("id") == "fade_from_black") {
238 if (m_effectList.hasEffect(QString(), "fadein") == -1) {
239 int effectDuration = EffectsList::parameter(effect, "out").toInt();
240 if (effectDuration > cropDuration().frames(m_fps)) {
241 effectDuration = cropDuration().frames(m_fps) / 2;
243 end += effectDuration;
245 end += EffectsList::parameter(m_effectList.getEffectByTag(QString(), "fadein"), "out").toInt();
247 EffectsList::setParameter(effect, "in", QString::number(start));
248 EffectsList::setParameter(effect, "out", QString::number(end));
253 bool ClipItem::checkKeyFrames()
255 bool clipEffectsModified = false;
256 for (int ix = 0; ix < m_effectList.count(); ix ++) {
257 QString kfr = keyframes(ix);
258 if (!kfr.isEmpty()) {
259 const QStringList keyframes = kfr.split(';', QString::SkipEmptyParts);
260 QStringList newKeyFrames;
261 bool cutKeyFrame = false;
262 bool modified = false;
264 double lastValue = -1;
265 int start = cropStart().frames(m_fps);
266 int end = (cropStart() + cropDuration()).frames(m_fps);
267 foreach(const QString &str, keyframes) {
268 int pos = str.section(':', 0, 0).toInt();
269 double val = str.section(':', 1, 1).toDouble();
270 if (pos - start < 0) {
271 // a keyframe is defined before the start of the clip
273 } else if (cutKeyFrame) {
274 // create new keyframe at clip start, calculate interpolated value
276 int diff = pos - lastPos;
277 double ratio = (double)(start - lastPos) / diff;
278 double newValue = lastValue + (val - lastValue) * ratio;
279 newKeyFrames.append(QString::number(start) + ':' + QString::number(newValue));
286 // create new keyframe at clip end, calculate interpolated value
287 int diff = pos - lastPos;
289 double ratio = (double)(end - lastPos) / diff;
290 double newValue = lastValue + (val - lastValue) * ratio;
291 newKeyFrames.append(QString::number(end) + ':' + QString::number(newValue));
296 newKeyFrames.append(QString::number(pos) + ':' + QString::number(val));
304 setKeyframes(ix, newKeyFrames.join(";"));
305 clipEffectsModified = true;
309 return clipEffectsModified;
312 void ClipItem::setKeyframes(const int ix, const QString keyframes)
314 QDomElement effect = getEffectAt(ix);
315 if (effect.attribute("disabled") == "1") return;
316 QDomNodeList params = effect.elementsByTagName("parameter");
317 for (int i = 0; i < params.count(); i++) {
318 QDomElement e = params.item(i).toElement();
319 if (!e.isNull() && (e.attribute("type") == "keyframe" || e.attribute("type") == "simplekeyframe")) {
320 e.setAttribute("keyframes", keyframes);
321 if (ix == m_selectedEffect) {
323 double max = e.attribute("max").toDouble();
324 double min = e.attribute("min").toDouble();
325 m_keyframeFactor = 100.0 / (max - min);
326 m_keyframeDefault = e.attribute("default").toDouble();
328 const QStringList keyframes = e.attribute("keyframes").split(';', QString::SkipEmptyParts);
329 foreach(const QString &str, keyframes) {
330 int pos = str.section(':', 0, 0).toInt();
331 double val = str.section(':', 1, 1).toDouble();
332 m_keyframes[pos] = val;
343 void ClipItem::setSelectedEffect(const int ix)
345 m_selectedEffect = ix;
346 QDomElement effect = effectAt(m_selectedEffect);
347 if (effect.isNull() == false) {
348 QDomNodeList params = effect.elementsByTagName("parameter");
349 if (effect.attribute("disabled") != "1")
350 for (int i = 0; i < params.count(); i++) {
351 QDomElement e = params.item(i).toElement();
352 if (!e.isNull() && (e.attribute("type") == "keyframe" || e.attribute("type") == "simplekeyframe")) {
354 double max = e.attribute("max").toDouble();
355 double min = e.attribute("min").toDouble();
356 m_keyframeFactor = 100.0 / (max - min);
357 m_keyframeDefault = e.attribute("default").toDouble();
359 const QStringList keyframes = e.attribute("keyframes").split(';', QString::SkipEmptyParts);
360 foreach(const QString &str, keyframes) {
361 int pos = str.section(':', 0, 0).toInt();
362 double val = str.section(':', 1, 1).toDouble();
363 m_keyframes[pos] = val;
370 if (!m_keyframes.isEmpty()) {
376 QString ClipItem::keyframes(const int index)
379 QDomElement effect = effectAt(index);
380 QDomNodeList params = effect.elementsByTagName("parameter");
382 for (int i = 0; i < params.count(); i++) {
383 QDomElement e = params.item(i).toElement();
384 if (!e.isNull() && (e.attribute("type") == "keyframe" || e.attribute("type") == "simplekeyframe")) {
385 result = e.attribute("keyframes");
392 void ClipItem::updateKeyframeEffect()
394 // regenerate xml parameter from the clip keyframes
395 QDomElement effect = getEffectAt(m_selectedEffect);
396 if (effect.attribute("disabled") == "1") return;
397 QDomNodeList params = effect.elementsByTagName("parameter");
399 for (int i = 0; i < params.count(); i++) {
400 QDomElement e = params.item(i).toElement();
401 if (!e.isNull() && (e.attribute("type") == "keyframe" || e.attribute("type") == "simplekeyframe")) {
403 if (m_keyframes.count() > 1) {
404 QMap<int, int>::const_iterator i = m_keyframes.constBegin();
405 while (i != m_keyframes.constEnd()) {
406 keyframes.append(QString::number(i.key()) + ':' + QString::number(i.value()) + ';');
410 // Effect has a keyframe type parameter, we need to set the values
411 //kDebug() << "::::::::::::::: SETTING EFFECT KEYFRAMES: " << keyframes;
412 e.setAttribute("keyframes", keyframes);
418 QDomElement ClipItem::selectedEffect()
420 if (m_selectedEffect == -1 || m_effectList.isEmpty()) return QDomElement();
421 return effectAt(m_selectedEffect);
424 void ClipItem::resetThumbs(bool clearExistingThumbs)
426 if (clearExistingThumbs) {
427 m_startPix = QPixmap();
428 m_endPix = QPixmap();
429 m_audioThumbCachePic.clear();
435 void ClipItem::refreshClip(bool checkDuration)
437 if (checkDuration && (m_maxDuration != m_clip->maxDuration())) {
438 m_maxDuration = m_clip->maxDuration();
439 if (m_clipType != IMAGE && m_clipType != TEXT && m_clipType != COLOR) {
440 if (m_maxDuration != GenTime() && m_info.cropStart + m_info.cropDuration > m_maxDuration) {
441 // Clip duration changed, make sure to stay in correct range
442 if (m_info.cropStart > m_maxDuration) {
443 m_info.cropStart = GenTime();
444 m_info.cropDuration = qMin(m_info.cropDuration, m_maxDuration);
445 updateRectGeometry();
447 m_info.cropDuration = m_maxDuration;
448 updateRectGeometry();
453 if (m_clipType == COLOR) {
454 QString colour = m_clip->getProperty("colour");
455 colour = colour.replace(0, 2, "#");
456 m_baseColor = QColor(colour.left(7));
457 } else resetThumbs(checkDuration);
460 void ClipItem::slotFetchThumbs()
462 if (scene() == NULL || m_clipType == AUDIO || m_clipType == COLOR) return;
463 if (m_clipType == IMAGE) {
464 if (m_startPix.isNull()) {
465 m_startPix = KThumb::getImage(KUrl(m_clip->getProperty("resource")), (int)(KdenliveSettings::trackheight() * KdenliveSettings::project_display_ratio()), KdenliveSettings::trackheight());
471 if (m_clipType == TEXT) {
472 if (m_startPix.isNull()) slotGetStartThumb();
476 if (m_endPix.isNull() && m_startPix.isNull()) {
477 m_startThumbRequested = true;
478 m_endThumbRequested = true;
479 emit getThumb((int)m_speedIndependantInfo.cropStart.frames(m_fps), (int)(m_speedIndependantInfo.cropStart + m_speedIndependantInfo.cropDuration).frames(m_fps) - 1);
481 if (m_endPix.isNull()) {
484 if (m_startPix.isNull()) {
490 void ClipItem::slotGetStartThumb()
492 m_startThumbRequested = true;
493 emit getThumb((int)m_speedIndependantInfo.cropStart.frames(m_fps), -1);
496 void ClipItem::slotGetEndThumb()
498 m_endThumbRequested = true;
499 emit getThumb(-1, (int)(m_speedIndependantInfo.cropStart + m_speedIndependantInfo.cropDuration).frames(m_fps) - 1);
503 void ClipItem::slotSetStartThumb(QImage img)
505 if (!img.isNull() && img.format() == QImage::Format_ARGB32) {
506 QPixmap pix = QPixmap::fromImage(img);
508 QRectF r = sceneBoundingRect();
509 r.setRight(pix.width() + 2);
514 void ClipItem::slotSetEndThumb(QImage img)
516 if (!img.isNull() && img.format() == QImage::Format_ARGB32) {
517 QPixmap pix = QPixmap::fromImage(img);
519 QRectF r = sceneBoundingRect();
520 r.setLeft(r.right() - pix.width() - 2);
525 void ClipItem::slotThumbReady(int frame, QImage img)
527 if (scene() == NULL) return;
528 QRectF r = boundingRect();
529 QPixmap pix = QPixmap::fromImage(img);
530 double width = pix.width() / projectScene()->scale().x();
531 if (m_startThumbRequested && frame == m_speedIndependantInfo.cropStart.frames(m_fps)) {
533 m_startThumbRequested = false;
534 update(r.left(), r.top(), width, pix.height());
535 if (m_clipType == IMAGE || m_clipType == TEXT) {
536 update(r.right() - width, r.top(), width, pix.height());
538 } else if (m_endThumbRequested && frame == (m_speedIndependantInfo.cropStart + m_speedIndependantInfo.cropDuration).frames(m_fps) - 1) {
540 m_endThumbRequested = false;
541 update(r.right() - width, r.top(), width, pix.height());
545 void ClipItem::slotSetStartThumb(const QPixmap pix)
550 void ClipItem::slotSetEndThumb(const QPixmap pix)
555 QPixmap ClipItem::startThumb() const
560 QPixmap ClipItem::endThumb() const
565 void ClipItem::slotGotAudioData()
567 m_audioThumbReady = true;
568 if (m_clipType == AV && !isAudioOnly()) {
569 QRectF r = boundingRect();
570 r.setTop(r.top() + r.height() / 2 - 1);
575 int ClipItem::type() const
580 DocClipBase *ClipItem::baseClip() const
585 QDomElement ClipItem::xml() const
587 QDomElement xml = m_clip->toXML();
588 if (m_speed != 1.0) xml.setAttribute("speed", m_speed);
589 if (m_strobe > 1) xml.setAttribute("strobe", m_strobe);
590 if (m_audioOnly) xml.setAttribute("audio_only", 1);
591 else if (m_videoOnly) xml.setAttribute("video_only", 1);
595 int ClipItem::clipType() const
600 QString ClipItem::clipName() const
605 void ClipItem::setClipName(const QString &name)
610 const QString ClipItem::clipProducer() const
615 void ClipItem::flashClip()
617 if (m_timeLine == 0) {
618 m_timeLine = new QTimeLine(750, this);
619 m_timeLine->setUpdateInterval(80);
620 m_timeLine->setCurveShape(QTimeLine::EaseInOutCurve);
621 m_timeLine->setFrameRange(0, 100);
622 connect(m_timeLine, SIGNAL(valueChanged(qreal)), this, SLOT(animate(qreal)));
624 //m_timeLine->start();
627 void ClipItem::animate(qreal /*value*/)
629 QRectF r = boundingRect();
635 void ClipItem::paint(QPainter *painter,
636 const QStyleOptionGraphicsItem *option,
640 if (parentItem()) paintColor = QColor(255, 248, 149);
641 else paintColor = m_baseColor;
642 if (isSelected() || (parentItem() && parentItem()->isSelected())) paintColor = paintColor.darker();
644 painter->setMatrixEnabled(false);
645 const QRectF mapped = painter->matrix().mapRect(rect()).adjusted(0.5, 0, 0.5, 0);
646 const QRectF exposed = option->exposedRect;
647 painter->setClipRect(mapped);
648 painter->fillRect(mapped, paintColor);
651 if (KdenliveSettings::videothumbnails() && !isAudioOnly()) {
652 QPen pen = painter->pen();
653 pen.setColor(QColor(255, 255, 255, 150));
654 const QRectF source(0.0, 0.0, (double) m_startPix.width(), (double) m_startPix.height());
655 painter->setPen(pen);
656 if ((m_clipType == IMAGE || m_clipType == TEXT) && !m_startPix.isNull()) {
657 const QPointF top = mapped.topRight() - QPointF(m_startPix.width() - 1, 0);
658 painter->drawPixmap(top, m_startPix);
659 QLineF l2(top.x(), mapped.top(), top.x(), mapped.bottom());
660 painter->drawLine(l2);
661 } else if (!m_endPix.isNull()) {
662 const QPointF top = mapped.topRight() - QPointF(m_endPix.width() - 1, 0);
663 painter->drawPixmap(top, m_endPix);
664 QLineF l2(top.x(), mapped.top(), top.x(), mapped.bottom());
665 painter->drawLine(l2);
667 if (!m_startPix.isNull()) {
668 painter->drawPixmap(mapped.topLeft(), m_startPix);
669 QLineF l2(mapped.left() + m_startPix.width(), mapped.top(), mapped.left() + m_startPix.width(), mapped.bottom());
670 painter->drawLine(l2);
672 painter->setPen(Qt::black);
675 // draw audio thumbnails
676 if (KdenliveSettings::audiothumbnails() && m_speed == 1.0 && !isVideoOnly() && ((m_clipType == AV && (exposed.bottom() > (rect().height() / 2) || isAudioOnly())) || m_clipType == AUDIO) && m_audioThumbReady) {
678 double startpixel = exposed.left();
681 double endpixel = exposed.right();
684 //kDebug()<<"/// REPAINTING AUDIO THMBS ZONE: "<<startpixel<<"x"<<endpixel;
686 /*QPainterPath path = m_clipType == AV ? roundRectPathLower : resultClipPath;*/
688 if (m_clipType == AV && !isAudioOnly()) {
690 mappedRect.setTop(mappedRect.bottom() - mapped.height() / 2);
691 } else mappedRect = mapped;
693 double scale = painter->matrix().m11();
694 int channels = baseClip()->getProperty("channels").toInt();
695 if (scale != m_framePixelWidth)
696 m_audioThumbCachePic.clear();
697 double cropLeft = m_info.cropStart.frames(m_fps);
698 const int clipStart = mappedRect.x();
699 const int mappedStartPixel = painter->matrix().map(QPointF(startpixel + cropLeft, 0)).x() - clipStart;
700 const int mappedEndPixel = painter->matrix().map(QPointF(endpixel + cropLeft, 0)).x() - clipStart;
701 cropLeft = cropLeft * scale;
704 emit prepareAudioThumb(scale, mappedStartPixel, mappedEndPixel, channels);
707 for (int startCache = mappedStartPixel - (mappedStartPixel) % 100; startCache < mappedEndPixel; startCache += 100) {
708 if (m_audioThumbCachePic.contains(startCache) && !m_audioThumbCachePic[startCache].isNull())
709 painter->drawPixmap(clipStart + startCache - cropLeft, mappedRect.y(), m_audioThumbCachePic[startCache]);
713 // Draw effects names
714 if (!m_effectNames.isEmpty() && mapped.width() > 40) {
715 QRectF txtBounding = painter->boundingRect(mapped, Qt::AlignLeft | Qt::AlignTop, m_effectNames);
717 if (m_timeLine && m_timeLine->state() == QTimeLine::Running) {
718 qreal value = m_timeLine->currentValue();
719 txtBounding.setWidth(txtBounding.width() * value);
720 bgColor.setRgb(50 + 200 *(1.0 - value), 50, 50, 100 + 50 * value);
721 } else bgColor.setRgb(50, 50, 90, 180);
723 QPainterPath rounded;
724 rounded.moveTo(txtBounding.bottomRight());
725 rounded.arcTo(txtBounding.right() - txtBounding.height() - 2, txtBounding.top() - txtBounding.height(), txtBounding.height() * 2, txtBounding.height() * 2, 270, 90);
726 rounded.lineTo(txtBounding.topLeft());
727 rounded.lineTo(txtBounding.bottomLeft());
728 painter->fillPath(rounded, bgColor);
729 painter->setPen(Qt::lightGray);
730 painter->drawText(txtBounding.adjusted(1, 0, 1, 0), Qt::AlignCenter, m_effectNames);
734 QColor frameColor(paintColor.darker());
735 if (isSelected() || (parentItem() && parentItem()->isSelected())) {
736 frameColor = QColor(Qt::red);
738 frameColor.setAlpha(160);
740 const QRectF txtBounding2 = painter->boundingRect(mapped, Qt::AlignHCenter | Qt::AlignVCenter, ' ' + m_clipName + ' ');
741 //painter->fillRect(txtBounding2, frameColor);
742 painter->setBrush(frameColor);
743 painter->setPen(Qt::NoPen);
744 painter->drawRoundedRect(txtBounding2, 3, 3);
745 painter->setBrush(QBrush(Qt::NoBrush));
747 //painter->setPen(QColor(0, 0, 0, 180));
748 //painter->drawText(txtBounding, Qt::AlignCenter, m_clipName);
750 painter->drawPixmap(txtBounding2.topLeft() - QPointF(17, -1), m_videoPix);
751 } else if (m_audioOnly) {
752 painter->drawPixmap(txtBounding2.topLeft() - QPointF(17, -1), m_audioPix);
754 painter->setPen(Qt::white);
755 painter->drawText(txtBounding2, Qt::AlignCenter, m_clipName);
759 QList < CommentedTime > markers = baseClip()->commentedSnapMarkers();
760 QList < CommentedTime >::Iterator it = markers.begin();
763 QBrush markerBrush(QColor(120, 120, 0, 140));
764 QPen pen = painter->pen();
765 pen.setColor(QColor(255, 255, 255, 200));
766 pen.setStyle(Qt::DotLine);
768 for (; it != markers.end(); ++it) {
769 pos = GenTime((int)((*it).time().frames(m_fps) / m_speed + 0.5), m_fps) - cropStart();
770 if (pos > GenTime()) {
771 if (pos > cropDuration()) break;
772 QLineF l(rect().x() + pos.frames(m_fps), rect().y(), rect().x() + pos.frames(m_fps), rect().bottom());
773 QLineF l2 = painter->matrix().map(l);
774 //framepos = scale * pos.frames(m_fps);
775 //QLineF l(framepos, 5, framepos, itemHeight - 5);
776 painter->setPen(pen);
777 painter->drawLine(l2);
778 if (KdenliveSettings::showmarkers()) {
779 framepos = rect().x() + pos.frames(m_fps);
780 const QRectF r1(framepos + 0.04, 10, rect().width() - framepos - 2, rect().height() - 10);
781 const QRectF r2 = painter->matrix().mapRect(r1);
782 const QRectF txtBounding3 = painter->boundingRect(r2, Qt::AlignLeft | Qt::AlignTop, ' ' + (*it).comment() + ' ');
783 painter->setBrush(markerBrush);
784 painter->setPen(Qt::NoPen);
785 painter->drawRoundedRect(txtBounding3, 3, 3);
786 painter->setBrush(QBrush(Qt::NoBrush));
787 painter->setPen(Qt::white);
788 painter->drawText(txtBounding3, Qt::AlignCenter, (*it).comment());
790 //painter->fillRect(QRect(br.x() + framepos, br.y(), 10, br.height()), QBrush(QColor(0, 0, 0, 150)));
794 // draw start / end fades
797 fades = QBrush(QColor(200, 50, 50, 150));
798 } else fades = QBrush(QColor(200, 200, 200, 200));
800 if (m_startFade != 0) {
801 QPainterPath fadeInPath;
802 fadeInPath.moveTo(0, 0);
803 fadeInPath.lineTo(0, rect().height());
804 fadeInPath.lineTo(m_startFade, 0);
805 fadeInPath.closeSubpath();
806 QPainterPath f1 = painter->matrix().map(fadeInPath);
807 painter->fillPath(f1/*.intersected(resultClipPath)*/, fades);
808 /*if (isSelected()) {
809 QLineF l(m_startFade * scale, 0, 0, itemHeight);
810 painter->drawLine(l);
813 if (m_endFade != 0) {
814 QPainterPath fadeOutPath;
815 fadeOutPath.moveTo(rect().width(), 0);
816 fadeOutPath.lineTo(rect().width(), rect().height());
817 fadeOutPath.lineTo(rect().width() - m_endFade, 0);
818 fadeOutPath.closeSubpath();
819 QPainterPath f1 = painter->matrix().map(fadeOutPath);
820 painter->fillPath(f1/*.intersected(resultClipPath)*/, fades);
821 /*if (isSelected()) {
822 QLineF l(itemWidth - m_endFade * scale, 0, itemWidth, itemHeight);
823 painter->drawLine(l);
828 painter->setPen(QPen(Qt::lightGray));
829 // draw effect or transition keyframes
830 if (mapped.width() > 20) drawKeyFrames(painter, exposed);
832 //painter->setMatrixEnabled(true);
835 // expand clip rect to allow correct painting of clip border
836 QPen pen1(frameColor);
837 painter->setPen(pen1);
838 painter->setClipping(false);
839 painter->drawRect(painter->matrix().mapRect(rect()));
843 OPERATIONTYPE ClipItem::operationMode(QPointF pos)
845 if (isItemLocked()) return NONE;
846 const double scale = projectScene()->scale().x();
847 double maximumOffset = 6 / scale;
848 if (isSelected() || (parentItem() && parentItem()->isSelected())) {
849 m_editedKeyframe = mouseOverKeyFrames(pos, maximumOffset);
850 if (m_editedKeyframe != -1) return KEYFRAME;
852 QRectF rect = sceneBoundingRect();
853 int addtransitionOffset = 10;
854 // Don't allow add transition if track height is very small
855 if (rect.height() < 30) addtransitionOffset = 0;
857 if (qAbs((int)(pos.x() - (rect.x() + m_startFade))) < maximumOffset && qAbs((int)(pos.y() - rect.y())) < 6) {
858 if (m_startFade == 0) setToolTip(i18n("Add audio fade"));
859 else setToolTip(i18n("Audio fade duration: %1s", GenTime(m_startFade, m_fps).seconds()));
861 } else if (pos.x() - rect.x() < maximumOffset && (rect.bottom() - pos.y() > addtransitionOffset)) {
862 setToolTip(i18n("Crop from start: %1s", cropStart().seconds()));
864 } else if (qAbs((int)(pos.x() - (rect.x() + rect.width() - m_endFade))) < maximumOffset && qAbs((int)(pos.y() - rect.y())) < 6) {
865 if (m_endFade == 0) setToolTip(i18n("Add audio fade"));
866 else setToolTip(i18n("Audio fade duration: %1s", GenTime(m_endFade, m_fps).seconds()));
868 } else if ((rect.right() - pos.x() < maximumOffset) && (rect.bottom() - pos.y() > addtransitionOffset)) {
869 setToolTip(i18n("Clip duration: %1s", cropDuration().seconds()));
871 } else if ((pos.x() - rect.x() < 16 / scale) && (rect.bottom() - pos.y() <= addtransitionOffset)) {
872 setToolTip(i18n("Add transition"));
873 return TRANSITIONSTART;
874 } else if ((rect.right() - pos.x() < 16 / scale) && (rect.bottom() - pos.y() <= addtransitionOffset)) {
875 setToolTip(i18n("Add transition"));
876 return TRANSITIONEND;
878 setToolTip(QString());
882 QList <GenTime> ClipItem::snapMarkers() const
884 QList < GenTime > snaps;
885 QList < GenTime > markers = baseClip()->snapMarkers();
888 for (int i = 0; i < markers.size(); i++) {
890 pos = GenTime((int)(markers.at(i).frames(m_fps) / m_speed + 0.5), m_fps) - cropStart();
891 if (pos > GenTime()) {
892 if (pos > cropDuration()) break;
893 else snaps.append(pos + startPos());
899 QList <CommentedTime> ClipItem::commentedSnapMarkers() const
901 QList < CommentedTime > snaps;
902 QList < CommentedTime > markers = baseClip()->commentedSnapMarkers();
905 for (int i = 0; i < markers.size(); i++) {
906 pos = GenTime((int)(markers.at(i).time().frames(m_fps) / m_speed + 0.5), m_fps) - cropStart();
907 if (pos > GenTime()) {
908 if (pos > cropDuration()) break;
909 else snaps.append(CommentedTime(pos + startPos(), markers.at(i).comment()));
915 void ClipItem::slotPrepareAudioThumb(double pixelForOneFrame, int startpixel, int endpixel, int channels)
917 QRectF re = sceneBoundingRect();
918 if (m_clipType == AV && !isAudioOnly()) re.setTop(re.y() + re.height() / 2);
920 //kDebug() << "// PREP AUDIO THMB FRMO : scale:" << pixelForOneFrame<< ", from: " << startpixel << ", to: " << endpixel;
921 //if ( (!audioThumbWasDrawn || framePixelWidth!=pixelForOneFrame ) && !baseClip()->audioFrameChache.isEmpty()){
923 for (int startCache = startpixel - startpixel % 100; startCache < endpixel; startCache += 100) {
924 //kDebug() << "creating " << startCache;
925 //if (framePixelWidth!=pixelForOneFrame ||
926 if (m_framePixelWidth == pixelForOneFrame && m_audioThumbCachePic.contains(startCache))
928 if (m_audioThumbCachePic[startCache].isNull() || m_framePixelWidth != pixelForOneFrame) {
929 m_audioThumbCachePic[startCache] = QPixmap(100, (int)(re.height()));
930 m_audioThumbCachePic[startCache].fill(QColor(180, 180, 200, 140));
932 bool fullAreaDraw = pixelForOneFrame < 10;
933 QMap<int, QPainterPath > positiveChannelPaths;
934 QMap<int, QPainterPath > negativeChannelPaths;
935 QPainter pixpainter(&m_audioThumbCachePic[startCache]);
937 audiopen.setWidth(0);
938 pixpainter.setPen(audiopen);
939 //pixpainter.setRenderHint(QPainter::Antialiasing,true);
940 //pixpainter.drawLine(0,0,100,re.height());
941 // Bail out, if caller provided invalid data
943 kWarning() << "Unable to draw image with " << channels << "number of channels";
947 int channelHeight = m_audioThumbCachePic[startCache].height() / channels;
949 for (int i = 0; i < channels; i++) {
951 positiveChannelPaths[i].moveTo(0, channelHeight*i + channelHeight / 2);
952 negativeChannelPaths[i].moveTo(0, channelHeight*i + channelHeight / 2);
955 for (int samples = 0; samples <= 100; samples++) {
956 double frame = (double)(samples + startCache - 0) / pixelForOneFrame;
957 int sample = (int)((frame - (int)(frame)) * 20); // AUDIO_FRAME_SIZE
958 if (frame < 0 || sample < 0 || sample > 19)
960 QMap<int, QByteArray> frame_channel_data = baseClip()->m_audioFrameCache[(int)frame];
962 for (int channel = 0; channel < channels && frame_channel_data[channel].size() > 0; channel++) {
964 int y = channelHeight * channel + channelHeight / 2;
965 int delta = (int)(frame_channel_data[channel][sample] - 127 / 2) * channelHeight / 64;
967 positiveChannelPaths[channel].lineTo(samples, 0.1 + y + qAbs(delta));
968 negativeChannelPaths[channel].lineTo(samples, 0.1 + y - qAbs(delta));
970 positiveChannelPaths[channel].lineTo(samples, 0.1 + y + delta);
971 negativeChannelPaths[channel].lineTo(samples, 0.1 + y - delta);
974 for (int channel = 0; channel < channels ; channel++)
975 if (fullAreaDraw && samples == 100) {
976 positiveChannelPaths[channel].lineTo(samples, channelHeight*channel + channelHeight / 2);
977 negativeChannelPaths[channel].lineTo(samples, channelHeight*channel + channelHeight / 2);
978 positiveChannelPaths[channel].lineTo(0, channelHeight*channel + channelHeight / 2);
979 negativeChannelPaths[channel].lineTo(0, channelHeight*channel + channelHeight / 2);
983 pixpainter.setPen(QPen(QColor(0, 0, 0)));
984 pixpainter.setBrush(QBrush(QColor(60, 60, 60)));
986 for (int i = 0; i < channels; i++) {
988 //pixpainter.fillPath(positiveChannelPaths[i].united(negativeChannelPaths[i]),QBrush(Qt::SolidPattern));//or singleif looks better
989 pixpainter.drawPath(positiveChannelPaths[i].united(negativeChannelPaths[i]));//or singleif looks better
991 pixpainter.drawPath(positiveChannelPaths[i]);
994 //audioThumbWasDrawn=true;
995 m_framePixelWidth = pixelForOneFrame;
1000 int ClipItem::fadeIn() const
1005 int ClipItem::fadeOut() const
1011 void ClipItem::setFadeIn(int pos)
1013 if (pos == m_startFade) return;
1014 int oldIn = m_startFade;
1015 if (pos < 0) pos = 0;
1016 if (pos > cropDuration().frames(m_fps)) pos = (int)(cropDuration().frames(m_fps));
1018 QRectF rect = boundingRect();
1019 update(rect.x(), rect.y(), qMax(oldIn, pos), rect.height());
1022 void ClipItem::setFadeOut(int pos)
1024 if (pos == m_endFade) return;
1025 int oldOut = m_endFade;
1026 if (pos < 0) pos = 0;
1027 if (pos > cropDuration().frames(m_fps)) pos = (int)(cropDuration().frames(m_fps));
1029 QRectF rect = boundingRect();
1030 update(rect.x() + rect.width() - qMax(oldOut, pos), rect.y(), qMax(oldOut, pos), rect.height());
1036 void ClipItem::hoverEnterEvent(QGraphicsSceneHoverEvent *e)
1038 //if (e->pos().x() < 20) m_hover = true;
1040 if (isItemLocked()) return;
1042 QRectF r = boundingRect();
1043 double width = 35 / projectScene()->scale().x();
1044 double height = r.height() / 2;
1045 //WARNING: seems like it generates a full repaint of the clip, maybe not so good...
1046 update(r.x(), r.y() + height, width, height);
1047 update(r.right() - width, r.y() + height, width, height);
1051 void ClipItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *)
1053 if (isItemLocked()) return;
1055 QRectF r = boundingRect();
1056 double width = 35 / projectScene()->scale().x();
1057 double height = r.height() / 2;
1058 //WARNING: seems like it generates a full repaint of the clip, maybe not so good...
1059 update(r.x(), r.y() + height, width, height);
1060 update(r.right() - width, r.y() + height, width, height);
1064 void ClipItem::resizeStart(int posx)
1066 const int min = (startPos() - cropStart()).frames(m_fps);
1067 if (posx < min) posx = min;
1068 if (posx == startPos().frames(m_fps)) return;
1069 const int previous = cropStart().frames(m_fps);
1070 AbstractClipItem::resizeStart(posx);
1072 // set speed independant info
1073 m_speedIndependantInfo = m_info;
1074 m_speedIndependantInfo.cropStart = GenTime((int)(m_info.cropStart.frames(m_fps) * m_speed), m_fps);
1075 m_speedIndependantInfo.cropDuration = GenTime((int)(m_info.cropDuration.frames(m_fps) * m_speed), m_fps);
1077 if ((int) cropStart().frames(m_fps) != previous) {
1078 if (m_hasThumbs && KdenliveSettings::videothumbnails()) {
1079 m_startThumbTimer.start(150);
1084 void ClipItem::resizeEnd(int posx)
1086 const int max = (startPos() - cropStart() + maxDuration()).frames(m_fps);
1087 if (posx > max && maxDuration() != GenTime()) posx = max;
1088 if (posx == endPos().frames(m_fps)) return;
1089 //kDebug() << "// NEW POS: " << posx << ", OLD END: " << endPos().frames(m_fps);
1090 const int previous = cropDuration().frames(m_fps);
1091 AbstractClipItem::resizeEnd(posx);
1093 // set speed independant info
1094 m_speedIndependantInfo = m_info;
1095 m_speedIndependantInfo.cropStart = GenTime((int)(m_info.cropStart.frames(m_fps) * m_speed), m_fps);
1096 m_speedIndependantInfo.cropDuration = GenTime((int)(m_info.cropDuration.frames(m_fps) * m_speed), m_fps);
1098 if ((int) cropDuration().frames(m_fps) != previous) {
1099 if (m_hasThumbs && KdenliveSettings::videothumbnails()) {
1100 m_endThumbTimer.start(150);
1106 bool ClipItem::checkEffectsKeyframesPos(const int previous, const int current, bool fromStart)
1108 bool modified = false;
1109 for (int i = 0; i < m_effectList.count(); i++) {
1110 QDomElement effect = m_effectList.at(i);
1111 QDomNodeList params = effect.elementsByTagName("parameter");
1112 for (int j = 0; j < params.count(); j++) {
1113 QDomElement e = params.item(i).toElement();
1114 if (!e.isNull() && (e.attribute("type") == "keyframe" || e.attribute("type") == "simplekeyframe")) {
1115 // parse keyframes and adjust values
1116 const QStringList keyframes = e.attribute("keyframes").split(';', QString::SkipEmptyParts);
1117 QMap <int, double> kfr;
1120 foreach(const QString &str, keyframes) {
1121 pos = str.section(':', 0, 0).toInt();
1122 val = str.section(':', 1, 1).toDouble();
1123 if (pos == previous) {
1127 if ((fromStart && pos >= current) || (!fromStart && pos <= current)) {
1135 QMap<int, double>::const_iterator k = kfr.constBegin();
1136 while (k != kfr.constEnd()) {
1137 newkfr.append(QString::number(k.key()) + ':' + QString::number(k.value()) + ';');
1140 e.setAttribute("keyframes", newkfr);
1146 if (modified && m_selectedEffect >= 0) setSelectedEffect(m_selectedEffect);
1151 QVariant ClipItem::itemChange(GraphicsItemChange change, const QVariant &value)
1153 if (change == ItemPositionChange && scene()) {
1154 // calculate new position.
1155 //if (parentItem()) return pos();
1156 QPointF newPos = value.toPointF();
1157 //kDebug() << "/// MOVING CLIP ITEM.------------\n++++++++++";
1158 int xpos = projectScene()->getSnapPointForPos((int) newPos.x(), KdenliveSettings::snaptopoints());
1159 xpos = qMax(xpos, 0);
1161 int newTrack = newPos.y() / KdenliveSettings::trackheight();
1162 newTrack = qMin(newTrack, projectScene()->tracksCount() - 1);
1163 newTrack = qMax(newTrack, 0);
1164 newPos.setY((int)(newTrack * KdenliveSettings::trackheight() + 1));
1165 // Only one clip is moving
1166 QRectF sceneShape = rect();
1167 sceneShape.translate(newPos);
1168 QList<QGraphicsItem*> items;
1169 if (projectScene()->editMode() == NORMALEDIT)
1170 items = scene()->items(sceneShape, Qt::IntersectsItemShape);
1171 items.removeAll(this);
1172 bool forwardMove = newPos.x() > pos().x();
1174 if (!items.isEmpty()) {
1175 for (int i = 0; i < items.count(); i++) {
1176 if (!items.at(i)->isEnabled()) continue;
1177 if (items.at(i)->type() == type()) {
1179 QPointF otherPos = items.at(i)->pos();
1180 if ((int) otherPos.y() != (int) pos().y()) {
1184 offset = qMax(offset, (int)(newPos.x() - (static_cast < AbstractClipItem* >(items.at(i))->startPos() - cropDuration()).frames(m_fps)));
1186 offset = qMax(offset, (int)((static_cast < AbstractClipItem* >(items.at(i))->endPos().frames(m_fps)) - newPos.x()));
1191 sceneShape.translate(QPointF(-offset, 0));
1192 newPos.setX(newPos.x() - offset);
1194 sceneShape.translate(QPointF(offset, 0));
1195 newPos.setX(newPos.x() + offset);
1197 QList<QGraphicsItem*> subitems = scene()->items(sceneShape, Qt::IntersectsItemShape);
1198 subitems.removeAll(this);
1199 for (int j = 0; j < subitems.count(); j++) {
1200 if (!subitems.at(j)->isEnabled()) continue;
1201 if (subitems.at(j)->type() == type()) {
1202 // move was not successful, revert to previous pos
1203 m_info.startPos = GenTime((int) pos().x(), m_fps);
1209 m_info.track = newTrack;
1210 m_info.startPos = GenTime((int) newPos.x(), m_fps);
1216 m_info.track = newTrack;
1217 m_info.startPos = GenTime((int) newPos.x(), m_fps);
1218 //kDebug()<<"// ITEM NEW POS: "<<newPos.x()<<", mapped: "<<mapToScene(newPos.x(), 0).x();
1221 return QGraphicsItem::itemChange(change, value);
1225 /*void ClipItem::mouseMoveEvent(QGraphicsSceneMouseEvent * event) {
1228 int ClipItem::effectsCounter()
1230 return effectsCount() + 1;
1233 int ClipItem::effectsCount()
1235 return m_effectList.count();
1238 int ClipItem::hasEffect(const QString &tag, const QString &id) const
1240 return m_effectList.hasEffect(tag, id);
1243 QStringList ClipItem::effectNames()
1245 return m_effectList.effectNames();
1248 QDomElement ClipItem::effectAt(int ix) const
1250 if (ix > m_effectList.count() - 1 || ix < 0 || m_effectList.at(ix).isNull()) return QDomElement();
1251 return m_effectList.at(ix).cloneNode().toElement();
1254 QDomElement ClipItem::getEffectAt(int ix) const
1256 if (ix > m_effectList.count() - 1 || ix < 0 || m_effectList.at(ix).isNull()) return QDomElement();
1257 return m_effectList.at(ix);
1260 void ClipItem::setEffectAt(int ix, QDomElement effect)
1262 if (ix < 0 || ix > (m_effectList.count() - 1) || effect.isNull()) {
1263 kDebug() << "Invalid effect index: " << ix;
1266 //kDebug() << "CHange EFFECT AT: " << ix << ", CURR: " << m_effectList.at(ix).attribute("tag") << ", NEW: " << effect.attribute("tag");
1267 effect.setAttribute("kdenlive_ix", ix + 1);
1268 m_effectList.replace(ix, effect);
1269 m_effectNames = m_effectList.effectNames().join(" / ");
1270 QString id = effect.attribute("id");
1271 if (id == "fadein" || id == "fadeout" || id == "fade_from_black" || id == "fade_to_black")
1274 QRectF r = boundingRect();
1280 EffectsParameterList ClipItem::addEffect(const QDomElement effect, bool /*animate*/)
1282 bool needRepaint = false;
1284 if (!effect.hasAttribute("kdenlive_ix")) {
1285 ix = effectsCounter();
1286 } else ix = effect.attribute("kdenlive_ix").toInt();
1287 if (!m_effectList.isEmpty() && ix <= m_effectList.count()) {
1289 m_effectList.insert(ix - 1, effect);
1290 for (int i = ix; i < m_effectList.count(); i++) {
1291 int index = m_effectList.item(i).attribute("kdenlive_ix").toInt();
1292 if (index >= ix) m_effectList.item(i).setAttribute("kdenlive_ix", index + 1);
1294 } else m_effectList.append(effect);
1295 EffectsParameterList parameters;
1296 parameters.addParam("tag", effect.attribute("tag"));
1297 parameters.addParam("kdenlive_ix", effect.attribute("kdenlive_ix"));
1298 if (effect.hasAttribute("src")) parameters.addParam("src", effect.attribute("src"));
1299 if (effect.hasAttribute("disabled")) parameters.addParam("disabled", effect.attribute("disabled"));
1302 QString effectId = effect.attribute("id");
1303 if (effectId.isEmpty()) effectId = effect.attribute("tag");
1304 parameters.addParam("id", effectId);
1306 QDomNodeList params = effect.elementsByTagName("parameter");
1308 for (int i = 0; i < params.count(); i++) {
1309 QDomElement e = params.item(i).toElement();
1311 if (e.attribute("type") == "simplekeyframe") {
1312 QStringList values = e.attribute("keyframes").split(";", QString::SkipEmptyParts);
1313 double factor = e.attribute("factor", "1").toDouble();
1315 for (int j = 0; j < values.count(); j++) {
1316 QString pos = values.at(j).section(":", 0, 0);
1317 double val = values.at(j).section(":", 1, 1).toDouble() / factor;
1318 values[j] = pos + "=" + QString::number(val);
1321 parameters.addParam(e.attribute("name"), values.join(";"));
1322 /*parameters.addParam("max", e.attribute("max"));
1323 parameters.addParam("min", e.attribute("min"));
1324 parameters.addParam("factor", );*/
1326 else if (e.attribute("type") == "keyframe") {
1327 parameters.addParam("keyframes", e.attribute("keyframes"));
1328 parameters.addParam("max", e.attribute("max"));
1329 parameters.addParam("min", e.attribute("min"));
1330 parameters.addParam("factor", e.attribute("factor", "1"));
1331 parameters.addParam("starttag", e.attribute("starttag", "start"));
1332 parameters.addParam("endtag", e.attribute("endtag", "end"));
1335 if (e.attribute("factor", "1") == "1") {
1336 parameters.addParam(e.attribute("name"), e.attribute("value"));
1338 // check if it is a fade effect
1339 if (effectId == "fadein") {
1341 if (m_effectList.hasEffect(QString(), "fade_from_black") == -1) {
1342 if (e.attribute("name") == "out") fade += e.attribute("value").toInt();
1343 else if (e.attribute("name") == "in") fade -= e.attribute("value").toInt();
1345 QDomElement fadein = m_effectList.getEffectByTag(QString(), "fade_from_black");
1346 if (fadein.attribute("name") == "out") fade += fadein.attribute("value").toInt();
1347 else if (fadein.attribute("name") == "in") fade -= fadein.attribute("value").toInt();
1349 } else if (effectId == "fade_from_black") {
1351 if (m_effectList.hasEffect(QString(), "fadein") == -1) {
1352 if (e.attribute("name") == "out") fade += e.attribute("value").toInt();
1353 else if (e.attribute("name") == "in") fade -= e.attribute("value").toInt();
1355 QDomElement fadein = m_effectList.getEffectByTag(QString(), "fadein");
1356 if (fadein.attribute("name") == "out") fade += fadein.attribute("value").toInt();
1357 else if (fadein.attribute("name") == "in") fade -= fadein.attribute("value").toInt();
1359 } else if (effectId == "fadeout") {
1361 if (m_effectList.hasEffect(QString(), "fade_to_black") == -1) {
1362 if (e.attribute("name") == "out") fade -= e.attribute("value").toInt();
1363 else if (e.attribute("name") == "in") fade += e.attribute("value").toInt();
1365 QDomElement fadeout = m_effectList.getEffectByTag(QString(), "fade_to_black");
1366 if (fadeout.attribute("name") == "out") fade -= fadeout.attribute("value").toInt();
1367 else if (fadeout.attribute("name") == "in") fade += fadeout.attribute("value").toInt();
1369 } else if (effectId == "fade_to_black") {
1371 if (m_effectList.hasEffect(QString(), "fadeout") == -1) {
1372 if (e.attribute("name") == "out") fade -= e.attribute("value").toInt();
1373 else if (e.attribute("name") == "in") fade += e.attribute("value").toInt();
1375 QDomElement fadeout = m_effectList.getEffectByTag(QString(), "fadeout");
1376 if (fadeout.attribute("name") == "out") fade -= fadeout.attribute("value").toInt();
1377 else if (fadeout.attribute("name") == "in") fade += fadeout.attribute("value").toInt();
1382 if (e.attribute("factor").startsWith('%')) {
1383 fact = ProfilesDialog::getStringEval(projectScene()->profile(), e.attribute("factor"));
1384 } else fact = e.attribute("factor", "1").toDouble();
1385 parameters.addParam(e.attribute("name"), QString::number(e.attribute("value").toDouble() / fact));
1389 m_effectNames = m_effectList.effectNames().join(" / ");
1390 if (fade > 0) m_startFade = fade;
1391 else if (fade < 0) m_endFade = -fade;
1393 if (m_selectedEffect == -1) {
1394 setSelectedEffect(0);
1395 } else if (m_selectedEffect == ix - 1) setSelectedEffect(m_selectedEffect);
1396 if (needRepaint) update(boundingRect());
1400 else { /*if (!needRepaint) */
1401 QRectF r = boundingRect();
1408 EffectsParameterList ClipItem::getEffectArgs(const QDomElement effect)
1410 EffectsParameterList parameters;
1411 parameters.addParam("tag", effect.attribute("tag"));
1412 parameters.addParam("kdenlive_ix", effect.attribute("kdenlive_ix"));
1413 parameters.addParam("id", effect.attribute("id"));
1414 if (effect.hasAttribute("src")) parameters.addParam("src", effect.attribute("src"));
1415 if (effect.hasAttribute("disabled")) parameters.addParam("disabled", effect.attribute("disabled"));
1417 QDomNodeList params = effect.elementsByTagName("parameter");
1418 for (int i = 0; i < params.count(); i++) {
1419 QDomElement e = params.item(i).toElement();
1420 //kDebug() << "/ / / /SENDING EFFECT PARAM: " << e.attribute("type") << ", NAME_ " << e.attribute("tag");
1421 if (e.attribute("type") == "simplekeyframe") {
1422 kDebug() << "/ / / /SENDING KEYFR EFFECT TYPE";
1423 QStringList values = e.attribute("keyframes").split(";", QString::SkipEmptyParts);
1424 double factor = e.attribute("factor", "1").toDouble();
1426 for (int j = 0; j < values.count(); j++) {
1427 QString pos = values.at(j).section(":", 0, 0);
1428 double val = values.at(j).section(":", 1, 1).toDouble() / factor;
1429 values[j] = pos + "=" + QString::number(val);
1432 parameters.addParam(e.attribute("name"), values.join(";"));
1433 /*parameters.addParam(e.attribute("name"), e.attribute("keyframes").replace(":", "="));
1434 parameters.addParam("max", e.attribute("max"));
1435 parameters.addParam("min", e.attribute("min"));
1436 parameters.addParam("factor", e.attribute("factor", "1"));*/
1438 else if (e.attribute("type") == "keyframe") {
1439 kDebug() << "/ / / /SENDING KEYFR EFFECT TYPE";
1440 parameters.addParam("keyframes", e.attribute("keyframes"));
1441 parameters.addParam("max", e.attribute("max"));
1442 parameters.addParam("min", e.attribute("min"));
1443 parameters.addParam("factor", e.attribute("factor", "1"));
1444 parameters.addParam("starttag", e.attribute("starttag", "start"));
1445 parameters.addParam("endtag", e.attribute("endtag", "end"));
1446 } else if (e.attribute("namedesc").contains(';')) {
1447 QString format = e.attribute("format");
1448 QStringList separators = format.split("%d", QString::SkipEmptyParts);
1449 QStringList values = e.attribute("value").split(QRegExp("[,:;x]"));
1451 QTextStream txtNeu(&neu);
1452 if (values.size() > 0)
1453 txtNeu << (int)values[0].toDouble();
1454 for (int i = 0; i < separators.size() && i + 1 < values.size(); i++) {
1455 txtNeu << separators[i];
1456 txtNeu << (int)(values[i+1].toDouble());
1458 parameters.addParam("start", neu);
1460 if (e.attribute("factor", "1") != "1") {
1462 if (e.attribute("factor").startsWith('%')) {
1463 fact = ProfilesDialog::getStringEval(projectScene()->profile(), e.attribute("factor"));
1464 } else fact = e.attribute("factor", "1").toDouble();
1465 parameters.addParam(e.attribute("name"), QString::number(e.attribute("value").toDouble() / fact));
1467 parameters.addParam(e.attribute("name"), e.attribute("value"));
1474 void ClipItem::deleteEffect(QString index)
1476 bool needRepaint = false;
1479 for (int i = 0; i < m_effectList.count(); ++i) {
1480 ix = m_effectList.at(i).attribute("kdenlive_ix");
1482 QString effectId = m_effectList.at(i).attribute("id");
1483 if ((effectId == "fadein" && hasEffect(QString(), "fade_from_black") == -1) ||
1484 (effectId == "fade_from_black" && hasEffect(QString(), "fadein") == -1)) {
1487 } else if ((effectId == "fadeout" && hasEffect(QString(), "fade_to_black") == -1) ||
1488 (effectId == "fade_to_black" && hasEffect(QString(), "fadeout") == -1)) {
1491 } else if (EffectsList::hasKeyFrames(m_effectList.at(i))) needRepaint = true;
1492 m_effectList.removeAt(i);
1494 } else if (ix.toInt() > index.toInt()) {
1495 m_effectList.item(i).setAttribute("kdenlive_ix", ix.toInt() - 1);
1498 m_effectNames = m_effectList.effectNames().join(" / ");
1500 if (m_effectList.isEmpty() || m_selectedEffect + 1 == index.toInt()) {
1501 // Current effect was removed
1502 if (index.toInt() > m_effectList.count() - 1) {
1503 setSelectedEffect(m_effectList.count() - 1);
1504 } else setSelectedEffect(index.toInt());
1506 if (needRepaint) update(boundingRect());
1508 QRectF r = boundingRect();
1512 //if (!m_effectList.isEmpty()) flashClip();
1515 double ClipItem::speed() const
1520 int ClipItem::strobe() const
1525 void ClipItem::setSpeed(const double speed, const int strobe)
1529 if (m_speed == 1.0) m_clipName = baseClip()->name();
1530 else m_clipName = baseClip()->name() + " - " + QString::number(speed * 100, 'f', 0) + '%';
1531 m_info.cropStart = GenTime((int)(m_speedIndependantInfo.cropStart.frames(m_fps) / m_speed + 0.5), m_fps);
1532 m_info.cropDuration = GenTime((int)(m_speedIndependantInfo.cropDuration.frames(m_fps) / m_speed + 0.5), m_fps);
1536 GenTime ClipItem::maxDuration() const
1538 return GenTime((int)(m_maxDuration.frames(m_fps) / m_speed + 0.5), m_fps);
1541 GenTime ClipItem::speedIndependantCropStart() const
1543 return m_speedIndependantInfo.cropStart;
1546 GenTime ClipItem::speedIndependantCropDuration() const
1548 return m_speedIndependantInfo.cropDuration;
1552 const ItemInfo ClipItem::speedIndependantInfo() const
1554 return m_speedIndependantInfo;
1558 void ClipItem::dropEvent(QGraphicsSceneDragDropEvent * event)
1560 const QString effects = QString(event->mimeData()->data("kdenlive/effectslist"));
1562 doc.setContent(effects, true);
1563 const QDomElement e = doc.documentElement();
1564 if (scene() && !scene()->views().isEmpty()) {
1566 CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
1567 if (view) view->slotAddEffect(e, m_info.startPos, track());
1572 void ClipItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
1574 if (isItemLocked()) event->setAccepted(false);
1575 else event->setAccepted(event->mimeData()->hasFormat("kdenlive/effectslist"));
1578 void ClipItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
1583 void ClipItem::addTransition(Transition* t)
1585 m_transitionsList.append(t);
1586 //CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
1588 QDomElement e = doc.documentElement();
1589 //if (view) view->slotAddTransition(this, t->toXML() , t->startPos(), track());
1592 void ClipItem::setVideoOnly(bool force)
1594 m_videoOnly = force;
1597 void ClipItem::setAudioOnly(bool force)
1599 m_audioOnly = force;
1600 if (m_audioOnly) m_baseColor = QColor(141, 215, 166);
1602 if (m_clipType == COLOR) {
1603 QString colour = m_clip->getProperty("colour");
1604 colour = colour.replace(0, 2, "#");
1605 m_baseColor = QColor(colour.left(7));
1606 } else if (m_clipType == AUDIO) m_baseColor = QColor(141, 215, 166);
1607 else m_baseColor = QColor(141, 166, 215);
1609 m_audioThumbCachePic.clear();
1612 bool ClipItem::isAudioOnly() const
1617 bool ClipItem::isVideoOnly() const
1623 #include "clipitem.moc"