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 = clip->name();
73 m_clipName = clip->name() + " - " + QString::number(m_speed * 100, 'f', 0) + '%';
75 m_producer = clip->getId();
76 m_clipType = clip->clipType();
77 //m_cropStart = info.cropStart;
78 m_maxDuration = clip->maxDuration();
80 m_audioThumbReady = 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)), clip->thumbProducer(), SLOT(extractImage(int, int)));
93 //connect(this, SIGNAL(getThumb(int, int)), clip->thumbProducer(), SLOT(getVideoThumbs(int, int)));
95 connect(clip->thumbProducer(), SIGNAL(thumbReady(int, QPixmap)), this, SLOT(slotThumbReady(int, QPixmap)));
96 connect(clip, SIGNAL(gotAudioData()), this, SLOT(slotGotAudioData()));
97 if (generateThumbs) QTimer::singleShot(200, this, SLOT(slotFetchThumbs()));
99 /*if (m_clip->producer()) {
100 videoThumbProducer.init(this, m_clip->producer(), KdenliveSettings::trackheight() * KdenliveSettings::project_display_ratio(), KdenliveSettings::trackheight());
103 } else if (m_clipType == COLOR) {
104 QString colour = clip->getProperty("colour");
105 colour = colour.replace(0, 2, "#");
106 m_baseColor = QColor(colour.left(7));
107 } else if (m_clipType == IMAGE || m_clipType == TEXT) {
108 m_baseColor = QColor(141, 166, 215);
109 if (m_clipType == TEXT) {
110 connect(this, SIGNAL(getThumb(int, int)), clip->thumbProducer(), SLOT(extractImage(int, int)));
111 connect(clip->thumbProducer(), SIGNAL(thumbReady(int, QPixmap)), this, SLOT(slotThumbReady(int, QPixmap)));
113 //m_startPix = KThumb::getImage(KUrl(clip->getProperty("resource")), (int)(KdenliveSettings::trackheight() * KdenliveSettings::project_display_ratio()), KdenliveSettings::trackheight());
114 } else if (m_clipType == AUDIO) {
115 m_baseColor = QColor(141, 215, 166);
116 connect(clip, SIGNAL(gotAudioData()), this, SLOT(slotGotAudioData()));
121 ClipItem::~ClipItem()
124 if (m_clipType == VIDEO || m_clipType == AV || m_clipType == SLIDESHOW || m_clipType == PLAYLIST) {
125 disconnect(m_clip->thumbProducer(), SIGNAL(thumbReady(int, QPixmap)), this, SLOT(slotThumbReady(int, QPixmap)));
126 disconnect(m_clip, SIGNAL(gotAudioData()), this, SLOT(slotGotAudioData()));
131 ClipItem *ClipItem::clone(ItemInfo info) const
133 ClipItem *duplicate = new ClipItem(m_clip, info, m_fps, m_speed, m_strobe);
134 if (m_clipType == IMAGE || m_clipType == TEXT) duplicate->slotSetStartThumb(m_startPix);
136 if (info.cropStart == m_info.cropStart) duplicate->slotSetStartThumb(m_startPix);
137 if (info.cropStart + (info.endPos - info.startPos) == m_info.cropStart + (m_info.endPos - m_info.startPos)) duplicate->slotSetEndThumb(m_endPix);
139 //kDebug() << "// CLoning clip: " << (info.cropStart + (info.endPos - info.startPos)).frames(m_fps) << ", CURRENT end: " << (cropStart() + duration()).frames(m_fps);
140 duplicate->setEffectList(m_effectList);
141 duplicate->setVideoOnly(m_videoOnly);
142 duplicate->setAudioOnly(m_audioOnly);
143 //duplicate->setSpeed(m_speed);
147 void ClipItem::setEffectList(const EffectsList effectList)
149 m_effectList.clone(effectList);
150 m_effectNames = m_effectList.effectNames().join(" / ");
151 if (!m_effectList.isEmpty()) setSelectedEffect(0);
154 const EffectsList ClipItem::effectList() const
159 int ClipItem::selectedEffectIndex() const
161 return m_selectedEffect;
164 void ClipItem::initEffect(QDomElement effect, int diff)
166 // the kdenlive_ix int is used to identify an effect in mlt's playlist, should
168 if (effect.attribute("kdenlive_ix").toInt() == 0)
169 effect.setAttribute("kdenlive_ix", QString::number(effectsCounter()));
171 if (effect.attribute("id") == "freeze" && diff > 0) {
172 EffectsList::setParameter(effect, "frame", QString::number(diff));
175 // Init parameter value & keyframes if required
176 QDomNodeList params = effect.elementsByTagName("parameter");
177 for (int i = 0; i < params.count(); i++) {
178 QDomElement e = params.item(i).toElement();
179 kDebug() << "// init eff: " << e.attribute("name");
181 // Check if this effect has a variable parameter
182 if (e.attribute("default").startsWith('%')) {
183 double evaluatedValue = ProfilesDialog::getStringEval(projectScene()->profile(), e.attribute("default"));
184 e.setAttribute("default", evaluatedValue);
185 if (e.hasAttribute("value") && e.attribute("value").startsWith('%')) {
186 e.setAttribute("value", evaluatedValue);
190 if (!e.isNull() && e.attribute("type") == "keyframe") {
191 QString def = e.attribute("default");
192 // Effect has a keyframe type parameter, we need to set the values
193 if (e.attribute("keyframes").isEmpty()) {
194 e.setAttribute("keyframes", QString::number(cropStart().frames(m_fps)) + ':' + def + ';' + QString::number((cropStart() + cropDuration()).frames(m_fps) - 1) + ':' + def);
195 //kDebug() << "///// EFFECT KEYFRAMES INITED: " << e.attribute("keyframes");
200 if (effect.attribute("tag") == "volume" || effect.attribute("tag") == "brightness") {
201 if (effect.attribute("id") == "fadeout" || effect.attribute("id") == "fade_to_black") {
202 int end = (cropDuration() + cropStart()).frames(m_fps);
204 if (effect.attribute("id") == "fadeout") {
205 if (m_effectList.hasEffect(QString(), "fade_to_black") == -1) {
206 int effectDuration = EffectsList::parameter(effect, "in").toInt();
207 if (effectDuration > cropDuration().frames(m_fps)) {
208 effectDuration = cropDuration().frames(m_fps) / 2;
210 start -= effectDuration;
212 QDomElement fadeout = m_effectList.getEffectByTag(QString(), "fade_to_black");
213 start -= EffectsList::parameter(fadeout, "out").toInt() - EffectsList::parameter(fadeout, "in").toInt();
215 } else if (effect.attribute("id") == "fade_to_black") {
216 if (m_effectList.hasEffect(QString(), "fadeout") == -1) {
217 int effectDuration = EffectsList::parameter(effect, "in").toInt();
218 if (effectDuration > cropDuration().frames(m_fps)) {
219 effectDuration = cropDuration().frames(m_fps) / 2;
221 start -= effectDuration;
223 QDomElement fadeout = m_effectList.getEffectByTag(QString(), "fadeout");
224 start -= EffectsList::parameter(fadeout, "out").toInt() - EffectsList::parameter(fadeout, "in").toInt();
227 EffectsList::setParameter(effect, "in", QString::number(start));
228 EffectsList::setParameter(effect, "out", QString::number(end));
229 } else if (effect.attribute("id") == "fadein" || effect.attribute("id") == "fade_from_black") {
230 int start = cropStart().frames(m_fps);
232 if (effect.attribute("id") == "fadein") {
233 if (m_effectList.hasEffect(QString(), "fade_from_black") == -1) {
234 int effectDuration = EffectsList::parameter(effect, "out").toInt();
235 if (effectDuration > cropDuration().frames(m_fps)) {
236 effectDuration = cropDuration().frames(m_fps) / 2;
238 end += effectDuration;
240 end += EffectsList::parameter(m_effectList.getEffectByTag(QString(), "fade_from_black"), "out").toInt();
241 } else if (effect.attribute("id") == "fade_from_black") {
242 if (m_effectList.hasEffect(QString(), "fadein") == -1) {
243 int effectDuration = EffectsList::parameter(effect, "out").toInt();
244 if (effectDuration > cropDuration().frames(m_fps)) {
245 effectDuration = cropDuration().frames(m_fps) / 2;
247 end += effectDuration;
249 end += EffectsList::parameter(m_effectList.getEffectByTag(QString(), "fadein"), "out").toInt();
251 EffectsList::setParameter(effect, "in", QString::number(start));
252 EffectsList::setParameter(effect, "out", QString::number(end));
257 bool ClipItem::checkKeyFrames()
259 bool clipEffectsModified = false;
260 for (int ix = 0; ix < m_effectList.count(); ix ++) {
261 QString kfr = keyframes(ix);
262 if (!kfr.isEmpty()) {
263 const QStringList keyframes = kfr.split(';', QString::SkipEmptyParts);
264 QStringList newKeyFrames;
265 bool cutKeyFrame = false;
266 bool modified = false;
268 double lastValue = -1;
269 int start = cropStart().frames(m_fps);
270 int end = (cropStart() + cropDuration()).frames(m_fps);
271 foreach(const QString &str, keyframes) {
272 int pos = str.section(':', 0, 0).toInt();
273 double val = str.section(':', 1, 1).toDouble();
274 if (pos - start < 0) {
275 // a keyframe is defined before the start of the clip
277 } else if (cutKeyFrame) {
278 // create new keyframe at clip start, calculate interpolated value
280 int diff = pos - lastPos;
281 double ratio = (double)(start - lastPos) / diff;
282 double newValue = lastValue + (val - lastValue) * ratio;
283 newKeyFrames.append(QString::number(start) + ':' + QString::number(newValue));
290 // create new keyframe at clip end, calculate interpolated value
291 int diff = pos - lastPos;
293 double ratio = (double)(end - lastPos) / diff;
294 double newValue = lastValue + (val - lastValue) * ratio;
295 newKeyFrames.append(QString::number(end) + ':' + QString::number(newValue));
300 newKeyFrames.append(QString::number(pos) + ':' + QString::number(val));
308 setKeyframes(ix, newKeyFrames.join(";"));
309 clipEffectsModified = true;
313 return clipEffectsModified;
316 void ClipItem::setKeyframes(const int ix, const QString keyframes)
318 QDomElement effect = getEffectAt(ix);
319 if (effect.attribute("disabled") == "1") return;
320 QDomNodeList params = effect.elementsByTagName("parameter");
321 for (int i = 0; i < params.count(); i++) {
322 QDomElement e = params.item(i).toElement();
323 if (!e.isNull() && e.attribute("type") == "keyframe") {
324 e.setAttribute("keyframes", keyframes);
325 if (ix == m_selectedEffect) {
327 double max = e.attribute("max").toDouble();
328 double min = e.attribute("min").toDouble();
329 m_keyframeFactor = 100.0 / (max - min);
330 m_keyframeDefault = e.attribute("default").toDouble();
332 const QStringList keyframes = e.attribute("keyframes").split(';', QString::SkipEmptyParts);
333 foreach(const QString &str, keyframes) {
334 int pos = str.section(':', 0, 0).toInt();
335 double val = str.section(':', 1, 1).toDouble();
336 m_keyframes[pos] = val;
347 void ClipItem::setSelectedEffect(const int ix)
349 m_selectedEffect = ix;
350 QDomElement effect = effectAt(m_selectedEffect);
351 if (effect.isNull() == false) {
352 QDomNodeList params = effect.elementsByTagName("parameter");
353 if (effect.attribute("disabled") != "1")
354 for (int i = 0; i < params.count(); i++) {
355 QDomElement e = params.item(i).toElement();
356 if (!e.isNull() && e.attribute("type") == "keyframe") {
358 double max = e.attribute("max").toDouble();
359 double min = e.attribute("min").toDouble();
360 m_keyframeFactor = 100.0 / (max - min);
361 m_keyframeDefault = e.attribute("default").toDouble();
363 const QStringList keyframes = e.attribute("keyframes").split(';', QString::SkipEmptyParts);
364 foreach(const QString &str, keyframes) {
365 int pos = str.section(':', 0, 0).toInt();
366 double val = str.section(':', 1, 1).toDouble();
367 m_keyframes[pos] = val;
374 if (!m_keyframes.isEmpty()) {
380 QString ClipItem::keyframes(const int index)
383 QDomElement effect = effectAt(index);
384 QDomNodeList params = effect.elementsByTagName("parameter");
386 for (int i = 0; i < params.count(); i++) {
387 QDomElement e = params.item(i).toElement();
388 if (!e.isNull() && e.attribute("type") == "keyframe") {
389 result = e.attribute("keyframes");
396 void ClipItem::updateKeyframeEffect()
398 // regenerate xml parameter from the clip keyframes
399 QDomElement effect = getEffectAt(m_selectedEffect);
400 if (effect.attribute("disabled") == "1") return;
401 QDomNodeList params = effect.elementsByTagName("parameter");
403 for (int i = 0; i < params.count(); i++) {
404 QDomElement e = params.item(i).toElement();
405 if (!e.isNull() && e.attribute("type") == "keyframe") {
407 if (m_keyframes.count() > 1) {
408 QMap<int, int>::const_iterator i = m_keyframes.constBegin();
409 while (i != m_keyframes.constEnd()) {
410 keyframes.append(QString::number(i.key()) + ':' + QString::number(i.value()) + ';');
414 // Effect has a keyframe type parameter, we need to set the values
415 //kDebug() << "::::::::::::::: SETTING EFFECT KEYFRAMES: " << keyframes;
416 e.setAttribute("keyframes", keyframes);
422 QDomElement ClipItem::selectedEffect()
424 if (m_selectedEffect == -1 || m_effectList.isEmpty()) return QDomElement();
425 return effectAt(m_selectedEffect);
428 void ClipItem::resetThumbs(bool clearExistingThumbs)
430 if (clearExistingThumbs) {
431 m_startPix = QPixmap();
432 m_endPix = QPixmap();
433 m_audioThumbCachePic.clear();
439 void ClipItem::refreshClip(bool checkDuration)
441 if (checkDuration && (m_maxDuration != m_clip->maxDuration())) {
442 m_maxDuration = m_clip->maxDuration();
443 if (m_clipType != IMAGE && m_clipType != TEXT && m_clipType != COLOR) {
444 if (m_maxDuration != GenTime() && m_info.cropStart + m_info.cropDuration > m_maxDuration) {
445 // Clip duration changed, make sure to stay in correct range
446 if (m_info.cropStart > m_maxDuration) {
447 m_info.cropStart = GenTime();
448 m_info.cropDuration = qMin(m_info.cropDuration, m_maxDuration);
449 updateRectGeometry();
451 m_info.cropDuration = m_maxDuration;
452 updateRectGeometry();
457 if (m_clipType == COLOR) {
458 QString colour = m_clip->getProperty("colour");
459 colour = colour.replace(0, 2, "#");
460 m_baseColor = QColor(colour.left(7));
461 } else resetThumbs(checkDuration);
464 void ClipItem::slotFetchThumbs()
466 if (scene() == NULL) return;
467 if (m_clipType == IMAGE) {
468 if (m_startPix.isNull()) {
469 m_startPix = KThumb::getImage(KUrl(m_clip->getProperty("resource")), (int)(KdenliveSettings::trackheight() * KdenliveSettings::project_display_ratio()), KdenliveSettings::trackheight());
475 if (m_clipType == TEXT) {
476 if (m_startPix.isNull()) slotGetStartThumb();
480 if (m_endPix.isNull() && m_startPix.isNull()) {
481 m_startThumbRequested = true;
482 m_endThumbRequested = true;
483 emit getThumb((int)m_speedIndependantInfo.cropStart.frames(m_fps), (int)(m_speedIndependantInfo.cropStart + m_speedIndependantInfo.cropDuration).frames(m_fps) - 1);
485 if (m_endPix.isNull()) {
488 if (m_startPix.isNull()) {
494 if (m_endPix.isNull() && m_startPix.isNull()) {
495 int frame1 = (int)m_cropStart.frames(m_fps);
496 int frame2 = (int)(m_cropStart + m_cropDuration).frames(m_fps) - 1;
497 //videoThumbProducer.setThumbFrames(m_clip->producer(), frame1, frame2);
498 //videoThumbProducer.start(QThread::LowestPriority);
500 if (m_endPix.isNull()) slotGetEndThumb();
501 else slotGetStartThumb();
504 } else if (m_startPix.isNull()) slotGetStartThumb();*/
507 void ClipItem::slotGetStartThumb()
509 m_startThumbRequested = true;
510 emit getThumb((int)m_speedIndependantInfo.cropStart.frames(m_fps), -1);
511 //videoThumbProducer.setThumbFrames(m_clip->producer(), (int)m_cropStart.frames(m_fps), - 1);
512 //videoThumbProducer.start(QThread::LowestPriority);
515 void ClipItem::slotGetEndThumb()
517 m_endThumbRequested = true;
518 emit getThumb(-1, (int)(m_speedIndependantInfo.cropStart + m_speedIndependantInfo.cropDuration).frames(m_fps) - 1);
519 //videoThumbProducer.setThumbFrames(m_clip->producer(), -1, (int)(m_cropStart + m_cropDuration).frames(m_fps) - 1);
520 //videoThumbProducer.start(QThread::LowestPriority);
524 void ClipItem::slotSetStartThumb(QImage img)
526 if (!img.isNull() && img.format() == QImage::Format_ARGB32) {
527 QPixmap pix = QPixmap::fromImage(img);
529 QRectF r = sceneBoundingRect();
530 r.setRight(pix.width() + 2);
535 void ClipItem::slotSetEndThumb(QImage img)
537 if (!img.isNull() && img.format() == QImage::Format_ARGB32) {
538 QPixmap pix = QPixmap::fromImage(img);
540 QRectF r = sceneBoundingRect();
541 r.setLeft(r.right() - pix.width() - 2);
546 void ClipItem::slotThumbReady(int frame, QPixmap pix)
548 if (scene() == NULL) return;
549 QRectF r = boundingRect();
550 double width = pix.width() / projectScene()->scale().x();
551 if (m_startThumbRequested && frame == m_speedIndependantInfo.cropStart.frames(m_fps)) {
553 m_startThumbRequested = false;
554 update(r.left(), r.top(), width, pix.height());
555 if (m_clipType == IMAGE || m_clipType == TEXT) {
556 update(r.right() - width, r.top(), width, pix.height());
558 } else if (m_endThumbRequested && frame == (m_speedIndependantInfo.cropStart + m_speedIndependantInfo.cropDuration).frames(m_fps) - 1) {
560 m_endThumbRequested = false;
561 update(r.right() - width, r.top(), width, pix.height());
565 void ClipItem::slotSetStartThumb(const QPixmap pix)
570 void ClipItem::slotSetEndThumb(const QPixmap pix)
575 QPixmap ClipItem::startThumb() const
580 QPixmap ClipItem::endThumb() const
585 void ClipItem::slotGotAudioData()
587 m_audioThumbReady = true;
588 if (m_clipType == AV && !isAudioOnly()) {
589 QRectF r = boundingRect();
590 r.setTop(r.top() + r.height() / 2 - 1);
595 int ClipItem::type() const
600 DocClipBase *ClipItem::baseClip() const
605 QDomElement ClipItem::xml() const
607 QDomElement xml = m_clip->toXML();
608 if (m_speed != 1.0) xml.setAttribute("speed", m_speed);
609 if (m_strobe > 1) xml.setAttribute("strobe", m_strobe);
610 if (m_audioOnly) xml.setAttribute("audio_only", 1);
611 else if (m_videoOnly) xml.setAttribute("video_only", 1);
615 int ClipItem::clipType() const
620 QString ClipItem::clipName() const
625 void ClipItem::setClipName(const QString &name)
630 const QString ClipItem::clipProducer() const
635 void ClipItem::flashClip()
637 if (m_timeLine == 0) {
638 m_timeLine = new QTimeLine(750, this);
639 m_timeLine->setUpdateInterval(80);
640 m_timeLine->setCurveShape(QTimeLine::EaseInOutCurve);
641 m_timeLine->setFrameRange(0, 100);
642 connect(m_timeLine, SIGNAL(valueChanged(qreal)), this, SLOT(animate(qreal)));
644 //m_timeLine->start();
647 void ClipItem::animate(qreal /*value*/)
649 QRectF r = boundingRect();
655 void ClipItem::paint(QPainter *painter,
656 const QStyleOptionGraphicsItem *option,
660 if (parentItem()) paintColor = QColor(255, 248, 149);
661 else paintColor = m_baseColor;
662 if (isSelected() || (parentItem() && parentItem()->isSelected())) paintColor = paintColor.darker();
664 painter->setMatrixEnabled(false);
665 const QRectF mapped = painter->matrix().mapRect(rect()).adjusted(0.5, 0, 0.5, 0);
666 const QRectF exposed = option->exposedRect;
667 painter->setClipRect(mapped);
668 painter->fillRect(mapped, paintColor);
671 if (KdenliveSettings::videothumbnails() && !isAudioOnly()) {
672 QPen pen = painter->pen();
673 pen.setColor(QColor(255, 255, 255, 150));
674 const QRectF source(0.0, 0.0, (double) m_startPix.width(), (double) m_startPix.height());
675 painter->setPen(pen);
676 if ((m_clipType == IMAGE || m_clipType == TEXT) && !m_startPix.isNull()) {
677 const QPointF top = mapped.topRight() - QPointF(m_startPix.width() - 1, 0);
678 painter->drawPixmap(top, m_startPix);
679 QLineF l2(top.x(), mapped.top(), top.x(), mapped.bottom());
680 painter->drawLine(l2);
681 } else if (!m_endPix.isNull()) {
682 const QPointF top = mapped.topRight() - QPointF(m_endPix.width() - 1, 0);
683 painter->drawPixmap(top, m_endPix);
684 QLineF l2(top.x(), mapped.top(), top.x(), mapped.bottom());
685 painter->drawLine(l2);
687 if (!m_startPix.isNull()) {
688 painter->drawPixmap(mapped.topLeft(), m_startPix);
689 QLineF l2(mapped.left() + m_startPix.width(), mapped.top(), mapped.left() + m_startPix.width(), mapped.bottom());
690 painter->drawLine(l2);
692 painter->setPen(Qt::black);
695 // draw audio thumbnails
696 if (KdenliveSettings::audiothumbnails() && m_speed == 1.0 && !isVideoOnly() && ((m_clipType == AV && (exposed.bottom() > (rect().height() / 2) || isAudioOnly())) || m_clipType == AUDIO) && m_audioThumbReady) {
698 double startpixel = exposed.left();
701 double endpixel = exposed.right();
704 //kDebug()<<"/// REPAINTING AUDIO THMBS ZONE: "<<startpixel<<"x"<<endpixel;
706 /*QPainterPath path = m_clipType == AV ? roundRectPathLower : resultClipPath;*/
708 if (m_clipType == AV && !isAudioOnly()) {
710 mappedRect.setTop(mappedRect.bottom() - mapped.height() / 2);
711 } else mappedRect = mapped;
713 double scale = painter->matrix().m11();
714 int channels = baseClip()->getProperty("channels").toInt();
715 if (scale != m_framePixelWidth)
716 m_audioThumbCachePic.clear();
717 double cropLeft = m_info.cropStart.frames(m_fps);
718 const int clipStart = mappedRect.x();
719 const int mappedStartPixel = painter->matrix().map(QPointF(startpixel + cropLeft, 0)).x() - clipStart;
720 const int mappedEndPixel = painter->matrix().map(QPointF(endpixel + cropLeft, 0)).x() - clipStart;
721 cropLeft = cropLeft * scale;
724 emit prepareAudioThumb(scale, mappedStartPixel, mappedEndPixel, channels);
727 for (int startCache = mappedStartPixel - (mappedStartPixel) % 100; startCache < mappedEndPixel; startCache += 100) {
728 if (m_audioThumbCachePic.contains(startCache) && !m_audioThumbCachePic[startCache].isNull())
729 painter->drawPixmap(clipStart + startCache - cropLeft, mappedRect.y(), m_audioThumbCachePic[startCache]);
733 // Draw effects names
734 if (!m_effectNames.isEmpty() && mapped.width() > 40) {
735 QRectF txtBounding = painter->boundingRect(mapped, Qt::AlignLeft | Qt::AlignTop, m_effectNames);
737 if (m_timeLine && m_timeLine->state() == QTimeLine::Running) {
738 qreal value = m_timeLine->currentValue();
739 txtBounding.setWidth(txtBounding.width() * value);
740 bgColor.setRgb(50 + 200 *(1.0 - value), 50, 50, 100 + 50 * value);
741 } else bgColor.setRgb(50, 50, 90, 180);
743 QPainterPath rounded;
744 rounded.moveTo(txtBounding.bottomRight());
745 rounded.arcTo(txtBounding.right() - txtBounding.height() - 2, txtBounding.top() - txtBounding.height(), txtBounding.height() * 2, txtBounding.height() * 2, 270, 90);
746 rounded.lineTo(txtBounding.topLeft());
747 rounded.lineTo(txtBounding.bottomLeft());
748 painter->fillPath(rounded, bgColor);
749 painter->setPen(Qt::lightGray);
750 painter->drawText(txtBounding.adjusted(1, 0, 1, 0), Qt::AlignCenter, m_effectNames);
754 QColor frameColor(paintColor.darker());
755 if (isSelected() || (parentItem() && parentItem()->isSelected())) {
756 frameColor = QColor(Qt::red);
758 frameColor.setAlpha(160);
760 const QRectF txtBounding2 = painter->boundingRect(mapped, Qt::AlignHCenter | Qt::AlignVCenter, ' ' + m_clipName + ' ');
761 //painter->fillRect(txtBounding2, frameColor);
762 painter->setBrush(frameColor);
763 painter->setPen(Qt::NoPen);
764 painter->drawRoundedRect(txtBounding2, 3, 3);
765 painter->setBrush(QBrush(Qt::NoBrush));
767 //painter->setPen(QColor(0, 0, 0, 180));
768 //painter->drawText(txtBounding, Qt::AlignCenter, m_clipName);
770 painter->drawPixmap(txtBounding2.topLeft() - QPointF(17, -1), m_videoPix);
771 } else if (m_audioOnly) {
772 painter->drawPixmap(txtBounding2.topLeft() - QPointF(17, -1), m_audioPix);
774 painter->setPen(Qt::white);
775 painter->drawText(txtBounding2, Qt::AlignCenter, m_clipName);
779 QList < CommentedTime > markers = baseClip()->commentedSnapMarkers();
780 QList < CommentedTime >::Iterator it = markers.begin();
783 QBrush markerBrush(QColor(120, 120, 0, 140));
784 QPen pen = painter->pen();
785 pen.setColor(QColor(255, 255, 255, 200));
786 pen.setStyle(Qt::DotLine);
788 for (; it != markers.end(); ++it) {
789 pos = GenTime((int)((*it).time().frames(m_fps) / m_speed + 0.5), m_fps) - cropStart();
790 if (pos > GenTime()) {
791 if (pos > cropDuration()) break;
792 QLineF l(rect().x() + pos.frames(m_fps), rect().y(), rect().x() + pos.frames(m_fps), rect().bottom());
793 QLineF l2 = painter->matrix().map(l);
794 //framepos = scale * pos.frames(m_fps);
795 //QLineF l(framepos, 5, framepos, itemHeight - 5);
796 painter->setPen(pen);
797 painter->drawLine(l2);
798 if (KdenliveSettings::showmarkers()) {
799 framepos = rect().x() + pos.frames(m_fps);
800 const QRectF r1(framepos + 0.04, 10, rect().width() - framepos - 2, rect().height() - 10);
801 const QRectF r2 = painter->matrix().mapRect(r1);
802 const QRectF txtBounding3 = painter->boundingRect(r2, Qt::AlignLeft | Qt::AlignTop, ' ' + (*it).comment() + ' ');
803 painter->setBrush(markerBrush);
804 painter->setPen(Qt::NoPen);
805 painter->drawRoundedRect(txtBounding3, 3, 3);
806 painter->setBrush(QBrush(Qt::NoBrush));
807 painter->setPen(Qt::white);
808 painter->drawText(txtBounding3, Qt::AlignCenter, (*it).comment());
810 //painter->fillRect(QRect(br.x() + framepos, br.y(), 10, br.height()), QBrush(QColor(0, 0, 0, 150)));
814 // draw start / end fades
817 fades = QBrush(QColor(200, 50, 50, 150));
818 } else fades = QBrush(QColor(200, 200, 200, 200));
820 if (m_startFade != 0) {
821 QPainterPath fadeInPath;
822 fadeInPath.moveTo(0, 0);
823 fadeInPath.lineTo(0, rect().height());
824 fadeInPath.lineTo(m_startFade, 0);
825 fadeInPath.closeSubpath();
826 QPainterPath f1 = painter->matrix().map(fadeInPath);
827 painter->fillPath(f1/*.intersected(resultClipPath)*/, fades);
828 /*if (isSelected()) {
829 QLineF l(m_startFade * scale, 0, 0, itemHeight);
830 painter->drawLine(l);
833 if (m_endFade != 0) {
834 QPainterPath fadeOutPath;
835 fadeOutPath.moveTo(rect().width(), 0);
836 fadeOutPath.lineTo(rect().width(), rect().height());
837 fadeOutPath.lineTo(rect().width() - m_endFade, 0);
838 fadeOutPath.closeSubpath();
839 QPainterPath f1 = painter->matrix().map(fadeOutPath);
840 painter->fillPath(f1/*.intersected(resultClipPath)*/, fades);
841 /*if (isSelected()) {
842 QLineF l(itemWidth - m_endFade * scale, 0, itemWidth, itemHeight);
843 painter->drawLine(l);
848 painter->setPen(QPen(Qt::lightGray));
849 // draw effect or transition keyframes
850 if (mapped.width() > 20) drawKeyFrames(painter, exposed);
852 //painter->setMatrixEnabled(true);
855 // expand clip rect to allow correct painting of clip border
856 QPen pen1(frameColor);
857 painter->setPen(pen1);
858 painter->setClipping(false);
859 painter->drawRect(painter->matrix().mapRect(rect()));
863 OPERATIONTYPE ClipItem::operationMode(QPointF pos)
865 if (isItemLocked()) return NONE;
866 const double scale = projectScene()->scale().x();
867 double maximumOffset = 6 / scale;
868 if (isSelected() || (parentItem() && parentItem()->isSelected())) {
869 m_editedKeyframe = mouseOverKeyFrames(pos, maximumOffset);
870 if (m_editedKeyframe != -1) return KEYFRAME;
872 QRectF rect = sceneBoundingRect();
873 int addtransitionOffset = 10;
874 // Don't allow add transition if track height is very small
875 if (rect.height() < 30) addtransitionOffset = 0;
877 if (qAbs((int)(pos.x() - (rect.x() + m_startFade))) < maximumOffset && qAbs((int)(pos.y() - rect.y())) < 6) {
878 if (m_startFade == 0) setToolTip(i18n("Add audio fade"));
879 else setToolTip(i18n("Audio fade duration: %1s", GenTime(m_startFade, m_fps).seconds()));
881 } else if (pos.x() - rect.x() < maximumOffset && (rect.bottom() - pos.y() > addtransitionOffset)) {
882 setToolTip(i18n("Crop from start: %1s", cropStart().seconds()));
884 } else if (qAbs((int)(pos.x() - (rect.x() + rect.width() - m_endFade))) < maximumOffset && qAbs((int)(pos.y() - rect.y())) < 6) {
885 if (m_endFade == 0) setToolTip(i18n("Add audio fade"));
886 else setToolTip(i18n("Audio fade duration: %1s", GenTime(m_endFade, m_fps).seconds()));
888 } else if ((rect.right() - pos.x() < maximumOffset) && (rect.bottom() - pos.y() > addtransitionOffset)) {
889 setToolTip(i18n("Clip duration: %1s", cropDuration().seconds()));
891 } else if ((pos.x() - rect.x() < 16 / scale) && (rect.bottom() - pos.y() <= addtransitionOffset)) {
892 setToolTip(i18n("Add transition"));
893 return TRANSITIONSTART;
894 } else if ((rect.right() - pos.x() < 16 / scale) && (rect.bottom() - pos.y() <= addtransitionOffset)) {
895 setToolTip(i18n("Add transition"));
896 return TRANSITIONEND;
898 setToolTip(QString());
902 QList <GenTime> ClipItem::snapMarkers() const
904 QList < GenTime > snaps;
905 QList < GenTime > markers = baseClip()->snapMarkers();
908 for (int i = 0; i < markers.size(); i++) {
910 pos = GenTime((int)(markers.at(i).frames(m_fps) / m_speed + 0.5), m_fps) - cropStart();
911 if (pos > GenTime()) {
912 if (pos > cropDuration()) break;
913 else snaps.append(pos + startPos());
919 QList <CommentedTime> ClipItem::commentedSnapMarkers() const
921 QList < CommentedTime > snaps;
922 QList < CommentedTime > markers = baseClip()->commentedSnapMarkers();
925 for (int i = 0; i < markers.size(); i++) {
926 pos = GenTime((int)(markers.at(i).time().frames(m_fps) / m_speed + 0.5), m_fps) - cropStart();
927 if (pos > GenTime()) {
928 if (pos > cropDuration()) break;
929 else snaps.append(CommentedTime(pos + startPos(), markers.at(i).comment()));
935 void ClipItem::slotPrepareAudioThumb(double pixelForOneFrame, int startpixel, int endpixel, int channels)
937 QRectF re = sceneBoundingRect();
938 if (m_clipType == AV && !isAudioOnly()) re.setTop(re.y() + re.height() / 2);
940 //kDebug() << "// PREP AUDIO THMB FRMO : scale:" << pixelForOneFrame<< ", from: " << startpixel << ", to: " << endpixel;
941 //if ( (!audioThumbWasDrawn || framePixelWidth!=pixelForOneFrame ) && !baseClip()->audioFrameChache.isEmpty()){
943 for (int startCache = startpixel - startpixel % 100; startCache < endpixel; startCache += 100) {
944 //kDebug() << "creating " << startCache;
945 //if (framePixelWidth!=pixelForOneFrame ||
946 if (m_framePixelWidth == pixelForOneFrame && m_audioThumbCachePic.contains(startCache))
948 if (m_audioThumbCachePic[startCache].isNull() || m_framePixelWidth != pixelForOneFrame) {
949 m_audioThumbCachePic[startCache] = QPixmap(100, (int)(re.height()));
950 m_audioThumbCachePic[startCache].fill(QColor(180, 180, 200, 140));
952 bool fullAreaDraw = pixelForOneFrame < 10;
953 QMap<int, QPainterPath > positiveChannelPaths;
954 QMap<int, QPainterPath > negativeChannelPaths;
955 QPainter pixpainter(&m_audioThumbCachePic[startCache]);
957 audiopen.setWidth(0);
958 pixpainter.setPen(audiopen);
959 //pixpainter.setRenderHint(QPainter::Antialiasing,true);
960 //pixpainter.drawLine(0,0,100,re.height());
961 // Bail out, if caller provided invalid data
963 kWarning() << "Unable to draw image with " << channels << "number of channels";
967 int channelHeight = m_audioThumbCachePic[startCache].height() / channels;
969 for (int i = 0; i < channels; i++) {
971 positiveChannelPaths[i].moveTo(0, channelHeight*i + channelHeight / 2);
972 negativeChannelPaths[i].moveTo(0, channelHeight*i + channelHeight / 2);
975 for (int samples = 0; samples <= 100; samples++) {
976 double frame = (double)(samples + startCache - 0) / pixelForOneFrame;
977 int sample = (int)((frame - (int)(frame)) * 20); // AUDIO_FRAME_SIZE
978 if (frame < 0 || sample < 0 || sample > 19)
980 QMap<int, QByteArray> frame_channel_data = baseClip()->m_audioFrameCache[(int)frame];
982 for (int channel = 0; channel < channels && frame_channel_data[channel].size() > 0; channel++) {
984 int y = channelHeight * channel + channelHeight / 2;
985 int delta = (int)(frame_channel_data[channel][sample] - 127 / 2) * channelHeight / 64;
987 positiveChannelPaths[channel].lineTo(samples, 0.1 + y + qAbs(delta));
988 negativeChannelPaths[channel].lineTo(samples, 0.1 + y - qAbs(delta));
990 positiveChannelPaths[channel].lineTo(samples, 0.1 + y + delta);
991 negativeChannelPaths[channel].lineTo(samples, 0.1 + y - delta);
994 for (int channel = 0; channel < channels ; channel++)
995 if (fullAreaDraw && samples == 100) {
996 positiveChannelPaths[channel].lineTo(samples, channelHeight*channel + channelHeight / 2);
997 negativeChannelPaths[channel].lineTo(samples, channelHeight*channel + channelHeight / 2);
998 positiveChannelPaths[channel].lineTo(0, channelHeight*channel + channelHeight / 2);
999 negativeChannelPaths[channel].lineTo(0, channelHeight*channel + channelHeight / 2);
1003 pixpainter.setPen(QPen(QColor(0, 0, 0)));
1004 pixpainter.setBrush(QBrush(QColor(60, 60, 60)));
1006 for (int i = 0; i < channels; i++) {
1008 //pixpainter.fillPath(positiveChannelPaths[i].united(negativeChannelPaths[i]),QBrush(Qt::SolidPattern));//or singleif looks better
1009 pixpainter.drawPath(positiveChannelPaths[i].united(negativeChannelPaths[i]));//or singleif looks better
1011 pixpainter.drawPath(positiveChannelPaths[i]);
1014 //audioThumbWasDrawn=true;
1015 m_framePixelWidth = pixelForOneFrame;
1020 int ClipItem::fadeIn() const
1025 int ClipItem::fadeOut() const
1031 void ClipItem::setFadeIn(int pos)
1033 if (pos == m_startFade) return;
1034 int oldIn = m_startFade;
1035 if (pos < 0) pos = 0;
1036 if (pos > cropDuration().frames(m_fps)) pos = (int)(cropDuration().frames(m_fps));
1038 QRectF rect = boundingRect();
1039 update(rect.x(), rect.y(), qMax(oldIn, pos), rect.height());
1042 void ClipItem::setFadeOut(int pos)
1044 if (pos == m_endFade) return;
1045 int oldOut = m_endFade;
1046 if (pos < 0) pos = 0;
1047 if (pos > cropDuration().frames(m_fps)) pos = (int)(cropDuration().frames(m_fps));
1049 QRectF rect = boundingRect();
1050 update(rect.x() + rect.width() - qMax(oldOut, pos), rect.y(), qMax(oldOut, pos), rect.height());
1056 void ClipItem::hoverEnterEvent(QGraphicsSceneHoverEvent *e)
1058 //if (e->pos().x() < 20) m_hover = true;
1060 if (isItemLocked()) return;
1062 QRectF r = boundingRect();
1063 double width = 35 / projectScene()->scale().x();
1064 double height = r.height() / 2;
1065 //WARNING: seems like it generates a full repaint of the clip, maybe not so good...
1066 update(r.x(), r.y() + height, width, height);
1067 update(r.right() - width, r.y() + height, width, height);
1071 void ClipItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *)
1073 if (isItemLocked()) return;
1075 QRectF r = boundingRect();
1076 double width = 35 / projectScene()->scale().x();
1077 double height = r.height() / 2;
1078 //WARNING: seems like it generates a full repaint of the clip, maybe not so good...
1079 update(r.x(), r.y() + height, width, height);
1080 update(r.right() - width, r.y() + height, width, height);
1084 void ClipItem::resizeStart(int posx)
1086 const int min = (startPos() - cropStart()).frames(m_fps);
1087 if (posx < min) posx = min;
1088 if (posx == startPos().frames(m_fps)) return;
1089 const int previous = cropStart().frames(m_fps);
1090 AbstractClipItem::resizeStart(posx);
1092 // set speed independant info
1093 m_speedIndependantInfo = m_info;
1094 m_speedIndependantInfo.cropStart = GenTime((int)(m_info.cropStart.frames(m_fps) * m_speed), m_fps);
1095 m_speedIndependantInfo.cropDuration = GenTime((int)(m_info.cropDuration.frames(m_fps) * m_speed), m_fps);
1097 if ((int) cropStart().frames(m_fps) != previous) {
1098 if (m_hasThumbs && KdenliveSettings::videothumbnails()) {
1099 /*connect(m_clip->thumbProducer(), SIGNAL(thumbReady(int, QPixmap)), this, SLOT(slotThumbReady(int, QPixmap)));*/
1100 m_startThumbTimer.start(150);
1105 void ClipItem::resizeEnd(int posx)
1107 const int max = (startPos() - cropStart() + maxDuration()).frames(m_fps);
1108 if (posx > max && maxDuration() != GenTime()) posx = max;
1109 if (posx == endPos().frames(m_fps)) return;
1110 //kDebug() << "// NEW POS: " << posx << ", OLD END: " << endPos().frames(m_fps);
1111 const int previous = cropDuration().frames(m_fps);
1112 AbstractClipItem::resizeEnd(posx);
1114 // set speed independant info
1115 m_speedIndependantInfo = m_info;
1116 m_speedIndependantInfo.cropStart = GenTime((int)(m_info.cropStart.frames(m_fps) * m_speed), m_fps);
1117 m_speedIndependantInfo.cropDuration = GenTime((int)(m_info.cropDuration.frames(m_fps) * m_speed), m_fps);
1119 if ((int) cropDuration().frames(m_fps) != previous) {
1120 if (m_hasThumbs && KdenliveSettings::videothumbnails()) {
1121 /*connect(m_clip->thumbProducer(), SIGNAL(thumbReady(int, QPixmap)), this, SLOT(slotThumbReady(int, QPixmap)));*/
1122 m_endThumbTimer.start(150);
1128 bool ClipItem::checkEffectsKeyframesPos(const int previous, const int current, bool fromStart)
1130 bool modified = false;
1131 for (int i = 0; i < m_effectList.count(); i++) {
1132 QDomElement effect = m_effectList.at(i);
1133 QDomNodeList params = effect.elementsByTagName("parameter");
1134 for (int j = 0; j < params.count(); j++) {
1135 QDomElement e = params.item(i).toElement();
1136 if (e.attribute("type") == "keyframe") {
1137 // parse keyframes and adjust values
1138 const QStringList keyframes = e.attribute("keyframes").split(';', QString::SkipEmptyParts);
1139 QMap <int, double> kfr;
1142 foreach(const QString &str, keyframes) {
1143 pos = str.section(':', 0, 0).toInt();
1144 val = str.section(':', 1, 1).toDouble();
1145 if (pos == previous) {
1149 if ((fromStart && pos >= current) || (!fromStart && pos <= current)) {
1157 QMap<int, double>::const_iterator k = kfr.constBegin();
1158 while (k != kfr.constEnd()) {
1159 newkfr.append(QString::number(k.key()) + ':' + QString::number(k.value()) + ';');
1162 e.setAttribute("keyframes", newkfr);
1168 if (modified && m_selectedEffect >= 0) setSelectedEffect(m_selectedEffect);
1173 QVariant ClipItem::itemChange(GraphicsItemChange change, const QVariant &value)
1175 if (change == ItemPositionChange && scene()) {
1176 // calculate new position.
1177 //if (parentItem()) return pos();
1178 QPointF newPos = value.toPointF();
1179 //kDebug() << "/// MOVING CLIP ITEM.------------\n++++++++++";
1180 int xpos = projectScene()->getSnapPointForPos((int) newPos.x(), KdenliveSettings::snaptopoints());
1181 xpos = qMax(xpos, 0);
1183 int newTrack = newPos.y() / KdenliveSettings::trackheight();
1184 newTrack = qMin(newTrack, projectScene()->tracksCount() - 1);
1185 newTrack = qMax(newTrack, 0);
1186 newPos.setY((int)(newTrack * KdenliveSettings::trackheight() + 1));
1187 // Only one clip is moving
1188 QRectF sceneShape = rect();
1189 sceneShape.translate(newPos);
1190 QList<QGraphicsItem*> items;
1191 if (projectScene()->editMode() == NORMALEDIT)
1192 items = scene()->items(sceneShape, Qt::IntersectsItemShape);
1193 items.removeAll(this);
1194 bool forwardMove = newPos.x() > pos().x();
1196 if (!items.isEmpty()) {
1197 for (int i = 0; i < items.count(); i++) {
1198 if (items.at(i)->type() == type()) {
1200 QPointF otherPos = items.at(i)->pos();
1201 if ((int) otherPos.y() != (int) pos().y()) {
1205 offset = qMax(offset, (int)(newPos.x() - (static_cast < AbstractClipItem* >(items.at(i))->startPos() - cropDuration()).frames(m_fps)));
1207 offset = qMax(offset, (int)((static_cast < AbstractClipItem* >(items.at(i))->endPos().frames(m_fps)) - newPos.x()));
1212 sceneShape.translate(QPointF(-offset, 0));
1213 newPos.setX(newPos.x() - offset);
1215 sceneShape.translate(QPointF(offset, 0));
1216 newPos.setX(newPos.x() + offset);
1218 QList<QGraphicsItem*> subitems = scene()->items(sceneShape, Qt::IntersectsItemShape);
1219 subitems.removeAll(this);
1220 for (int j = 0; j < subitems.count(); j++) {
1221 if (subitems.at(j)->type() == type()) {
1222 m_info.startPos = GenTime((int) pos().x(), m_fps);
1228 m_info.track = newTrack;
1229 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("disabled")) parameters.addParam("disabled", effect.attribute("disabled"));
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") == "keyframe") {
1330 parameters.addParam("keyframes", e.attribute("keyframes"));
1331 parameters.addParam("max", e.attribute("max"));
1332 parameters.addParam("min", e.attribute("min"));
1333 parameters.addParam("factor", e.attribute("factor", "1"));
1334 parameters.addParam("starttag", e.attribute("starttag", "start"));
1335 parameters.addParam("endtag", e.attribute("endtag", "end"));
1338 if (e.attribute("factor", "1") == "1") {
1339 parameters.addParam(e.attribute("name"), e.attribute("value"));
1341 // check if it is a fade effect
1342 if (effectId == "fadein") {
1344 if (m_effectList.hasEffect(QString(), "fade_from_black") == -1) {
1345 if (e.attribute("name") == "out") fade += e.attribute("value").toInt();
1346 else if (e.attribute("name") == "in") fade -= e.attribute("value").toInt();
1348 QDomElement fadein = m_effectList.getEffectByTag(QString(), "fade_from_black");
1349 if (fadein.attribute("name") == "out") fade += fadein.attribute("value").toInt();
1350 else if (fadein.attribute("name") == "in") fade -= fadein.attribute("value").toInt();
1352 } else if (effectId == "fade_from_black") {
1354 if (m_effectList.hasEffect(QString(), "fadein") == -1) {
1355 if (e.attribute("name") == "out") fade += e.attribute("value").toInt();
1356 else if (e.attribute("name") == "in") fade -= e.attribute("value").toInt();
1358 QDomElement fadein = m_effectList.getEffectByTag(QString(), "fadein");
1359 if (fadein.attribute("name") == "out") fade += fadein.attribute("value").toInt();
1360 else if (fadein.attribute("name") == "in") fade -= fadein.attribute("value").toInt();
1362 } else if (effectId == "fadeout") {
1364 if (m_effectList.hasEffect(QString(), "fade_to_black") == -1) {
1365 if (e.attribute("name") == "out") fade -= e.attribute("value").toInt();
1366 else if (e.attribute("name") == "in") fade += e.attribute("value").toInt();
1368 QDomElement fadeout = m_effectList.getEffectByTag(QString(), "fade_to_black");
1369 if (fadeout.attribute("name") == "out") fade -= fadeout.attribute("value").toInt();
1370 else if (fadeout.attribute("name") == "in") fade += fadeout.attribute("value").toInt();
1372 } else if (effectId == "fade_to_black") {
1374 if (m_effectList.hasEffect(QString(), "fadeout") == -1) {
1375 if (e.attribute("name") == "out") fade -= e.attribute("value").toInt();
1376 else if (e.attribute("name") == "in") fade += e.attribute("value").toInt();
1378 QDomElement fadeout = m_effectList.getEffectByTag(QString(), "fadeout");
1379 if (fadeout.attribute("name") == "out") fade -= fadeout.attribute("value").toInt();
1380 else if (fadeout.attribute("name") == "in") fade += fadeout.attribute("value").toInt();
1385 if (e.attribute("factor").startsWith('%')) {
1386 fact = ProfilesDialog::getStringEval(projectScene()->profile(), e.attribute("factor"));
1387 } else fact = e.attribute("factor", "1").toDouble();
1388 parameters.addParam(e.attribute("name"), QString::number(e.attribute("value").toDouble() / fact));
1392 m_effectNames = m_effectList.effectNames().join(" / ");
1393 if (fade > 0) m_startFade = fade;
1394 else if (fade < 0) m_endFade = -fade;
1396 if (m_selectedEffect == -1) {
1397 setSelectedEffect(0);
1398 } else if (m_selectedEffect == ix - 1) setSelectedEffect(m_selectedEffect);
1399 if (needRepaint) update(boundingRect());
1403 else { /*if (!needRepaint) */
1404 QRectF r = boundingRect();
1411 EffectsParameterList ClipItem::getEffectArgs(const QDomElement effect)
1413 EffectsParameterList parameters;
1414 parameters.addParam("tag", effect.attribute("tag"));
1415 parameters.addParam("kdenlive_ix", effect.attribute("kdenlive_ix"));
1416 parameters.addParam("id", effect.attribute("id"));
1417 if (effect.hasAttribute("src")) parameters.addParam("src", effect.attribute("src"));
1418 if (effect.hasAttribute("disabled")) parameters.addParam("disabled", effect.attribute("disabled"));
1420 QDomNodeList params = effect.elementsByTagName("parameter");
1421 for (int i = 0; i < params.count(); i++) {
1422 QDomElement e = params.item(i).toElement();
1423 //kDebug() << "/ / / /SENDING EFFECT PARAM: " << e.attribute("type") << ", NAME_ " << e.attribute("tag");
1424 if (e.attribute("type") == "keyframe") {
1425 kDebug() << "/ / / /SENDING KEYFR EFFECT TYPE";
1426 parameters.addParam("keyframes", e.attribute("keyframes"));
1427 parameters.addParam("max", e.attribute("max"));
1428 parameters.addParam("min", e.attribute("min"));
1429 parameters.addParam("factor", e.attribute("factor", "1"));
1430 parameters.addParam("starttag", e.attribute("starttag", "start"));
1431 parameters.addParam("endtag", e.attribute("endtag", "end"));
1432 } else if (e.attribute("namedesc").contains(';')) {
1433 QString format = e.attribute("format");
1434 QStringList separators = format.split("%d", QString::SkipEmptyParts);
1435 QStringList values = e.attribute("value").split(QRegExp("[,:;x]"));
1437 QTextStream txtNeu(&neu);
1438 if (values.size() > 0)
1439 txtNeu << (int)values[0].toDouble();
1440 for (int i = 0; i < separators.size() && i + 1 < values.size(); i++) {
1441 txtNeu << separators[i];
1442 txtNeu << (int)(values[i+1].toDouble());
1444 parameters.addParam("start", neu);
1446 if (e.attribute("factor", "1") != "1") {
1448 if (e.attribute("factor").startsWith('%')) {
1449 fact = ProfilesDialog::getStringEval(projectScene()->profile(), e.attribute("factor"));
1450 } else fact = e.attribute("factor", "1").toDouble();
1451 parameters.addParam(e.attribute("name"), QString::number(e.attribute("value").toDouble() / fact));
1453 parameters.addParam(e.attribute("name"), e.attribute("value"));
1460 void ClipItem::deleteEffect(QString index)
1462 bool needRepaint = false;
1465 for (int i = 0; i < m_effectList.count(); ++i) {
1466 ix = m_effectList.at(i).attribute("kdenlive_ix");
1468 QString effectId = m_effectList.at(i).attribute("id");
1469 if ((effectId == "fadein" && hasEffect(QString(), "fade_from_black") == -1) ||
1470 (effectId == "fade_from_black" && hasEffect(QString(), "fadein") == -1)) {
1473 } else if ((effectId == "fadeout" && hasEffect(QString(), "fade_to_black") == -1) ||
1474 (effectId == "fade_to_black" && hasEffect(QString(), "fadeout") == -1)) {
1477 } else if (EffectsList::hasKeyFrames(m_effectList.at(i))) needRepaint = true;
1478 m_effectList.removeAt(i);
1480 } else if (ix.toInt() > index.toInt()) {
1481 m_effectList.item(i).setAttribute("kdenlive_ix", ix.toInt() - 1);
1484 m_effectNames = m_effectList.effectNames().join(" / ");
1486 if (m_effectList.isEmpty() || m_selectedEffect + 1 == index.toInt()) {
1487 // Current effect was removed
1488 if (index.toInt() > m_effectList.count() - 1) {
1489 setSelectedEffect(m_effectList.count() - 1);
1490 } else setSelectedEffect(index.toInt());
1492 if (needRepaint) update(boundingRect());
1494 QRectF r = boundingRect();
1498 //if (!m_effectList.isEmpty()) flashClip();
1501 double ClipItem::speed() const
1506 int ClipItem::strobe() const
1511 void ClipItem::setSpeed(const double speed, const int strobe)
1515 if (m_speed == 1.0) m_clipName = baseClip()->name();
1516 else m_clipName = baseClip()->name() + " - " + QString::number(speed * 100, 'f', 0) + '%';
1517 m_info.cropStart = GenTime((int)(m_speedIndependantInfo.cropStart.frames(m_fps) / m_speed + 0.5), m_fps);
1518 m_info.cropDuration = GenTime((int)(m_speedIndependantInfo.cropDuration.frames(m_fps) / m_speed + 0.5), m_fps);
1522 GenTime ClipItem::maxDuration() const
1524 return GenTime((int)(m_maxDuration.frames(m_fps) / m_speed + 0.5), m_fps);
1527 GenTime ClipItem::speedIndependantCropStart() const
1529 return m_speedIndependantInfo.cropStart;
1532 GenTime ClipItem::speedIndependantCropDuration() const
1534 return m_speedIndependantInfo.cropDuration;
1538 const ItemInfo ClipItem::speedIndependantInfo() const
1540 return m_speedIndependantInfo;
1544 void ClipItem::dropEvent(QGraphicsSceneDragDropEvent * event)
1546 const QString effects = QString(event->mimeData()->data("kdenlive/effectslist"));
1548 doc.setContent(effects, true);
1549 const QDomElement e = doc.documentElement();
1550 if (scene() && !scene()->views().isEmpty()) {
1552 CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
1553 if (view) view->slotAddEffect(e, m_info.startPos, track());
1558 void ClipItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
1560 if (isItemLocked()) event->setAccepted(false);
1561 else event->setAccepted(event->mimeData()->hasFormat("kdenlive/effectslist"));
1564 void ClipItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
1569 void ClipItem::addTransition(Transition* t)
1571 m_transitionsList.append(t);
1572 //CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
1574 QDomElement e = doc.documentElement();
1575 //if (view) view->slotAddTransition(this, t->toXML() , t->startPos(), track());
1578 void ClipItem::setVideoOnly(bool force)
1580 m_videoOnly = force;
1583 void ClipItem::setAudioOnly(bool force)
1585 m_audioOnly = force;
1586 if (m_audioOnly) m_baseColor = QColor(141, 215, 166);
1587 else m_baseColor = QColor(141, 166, 215);
1588 m_audioThumbCachePic.clear();
1591 bool ClipItem::isAudioOnly() const
1596 bool ClipItem::isVideoOnly() const
1602 #include "clipitem.moc"