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 if (m_speed <= 0 && m_speed > -1) m_speed = 1.0;
65 m_speedIndependantInfo = m_info;
66 m_speedIndependantInfo.cropStart = GenTime((int)(m_info.cropStart.frames(m_fps) * qAbs(m_speed)), m_fps);
67 m_speedIndependantInfo.cropDuration = GenTime((int)(m_info.cropDuration.frames(m_fps) * qAbs(m_speed)), m_fps);
69 m_videoPix = KIcon("kdenlive-show-video").pixmap(QSize(16, 16));
70 m_audioPix = KIcon("kdenlive-show-audio").pixmap(QSize(16, 16));
72 if (m_speed == 1.0) m_clipName = m_clip->name();
74 m_clipName = m_clip->name() + " - " + QString::number(m_speed * 100, 'f', 0) + '%';
76 m_producer = m_clip->getId();
77 m_clipType = m_clip->clipType();
78 //m_cropStart = info.cropStart;
79 m_maxDuration = m_clip->maxDuration();
81 m_audioThumbReady = m_clip->audioThumbCreated();
82 //setAcceptsHoverEvents(true);
83 connect(this , SIGNAL(prepareAudioThumb(double, int, int, int)) , this, SLOT(slotPrepareAudioThumb(double, int, int, int)));
85 if (m_clipType == VIDEO || m_clipType == AV || m_clipType == SLIDESHOW || m_clipType == PLAYLIST) {
86 m_baseColor = QColor(141, 166, 215);
87 if (!m_clip->isPlaceHolder()) {
89 m_startThumbTimer.setSingleShot(true);
90 connect(&m_startThumbTimer, SIGNAL(timeout()), this, SLOT(slotGetStartThumb()));
91 m_endThumbTimer.setSingleShot(true);
92 connect(&m_endThumbTimer, SIGNAL(timeout()), this, SLOT(slotGetEndThumb()));
94 connect(this, SIGNAL(getThumb(int, int)), m_clip->thumbProducer(), SLOT(extractImage(int, int)));
96 connect(m_clip->thumbProducer(), SIGNAL(thumbReady(int, QImage)), this, SLOT(slotThumbReady(int, QImage)));
97 connect(m_clip, SIGNAL(gotAudioData()), this, SLOT(slotGotAudioData()));
98 if (generateThumbs) QTimer::singleShot(200, this, SLOT(slotFetchThumbs()));
101 } else if (m_clipType == COLOR) {
102 QString colour = m_clip->getProperty("colour");
103 colour = colour.replace(0, 2, "#");
104 m_baseColor = QColor(colour.left(7));
105 } else if (m_clipType == IMAGE || m_clipType == TEXT) {
106 m_baseColor = QColor(141, 166, 215);
107 if (m_clipType == TEXT) {
108 connect(this, SIGNAL(getThumb(int, int)), m_clip->thumbProducer(), SLOT(extractImage(int, int)));
109 connect(m_clip->thumbProducer(), SIGNAL(thumbReady(int, QImage)), this, SLOT(slotThumbReady(int, QImage)));
111 //m_startPix = KThumb::getImage(KUrl(clip->getProperty("resource")), (int)(KdenliveSettings::trackheight() * KdenliveSettings::project_display_ratio()), KdenliveSettings::trackheight());
112 } else if (m_clipType == AUDIO) {
113 m_baseColor = QColor(141, 215, 166);
114 connect(m_clip, SIGNAL(gotAudioData()), this, SLOT(slotGotAudioData()));
119 ClipItem::~ClipItem()
122 if (scene()) scene()->removeItem(this);
123 if (m_clipType == VIDEO || m_clipType == AV || m_clipType == SLIDESHOW || m_clipType == PLAYLIST) {
124 //disconnect(m_clip->thumbProducer(), SIGNAL(thumbReady(int, QImage)), this, SLOT(slotThumbReady(int, QImage)));
125 //disconnect(m_clip, SIGNAL(gotAudioData()), this, SLOT(slotGotAudioData()));
130 ClipItem *ClipItem::clone(ItemInfo info) const
132 ClipItem *duplicate = new ClipItem(m_clip, info, m_fps, m_speed, m_strobe);
133 if (m_clipType == IMAGE || m_clipType == TEXT) duplicate->slotSetStartThumb(m_startPix);
134 else if (m_clipType != COLOR) {
135 if (info.cropStart == m_info.cropStart) duplicate->slotSetStartThumb(m_startPix);
136 if (info.cropStart + (info.endPos - info.startPos) == m_info.cropStart + (m_info.endPos - m_info.startPos)) duplicate->slotSetEndThumb(m_endPix);
138 //kDebug() << "// CLoning clip: " << (info.cropStart + (info.endPos - info.startPos)).frames(m_fps) << ", CURRENT end: " << (cropStart() + duration()).frames(m_fps);
139 duplicate->setEffectList(m_effectList);
140 duplicate->setVideoOnly(m_videoOnly);
141 duplicate->setAudioOnly(m_audioOnly);
142 duplicate->setFades(fadeIn(), fadeOut());
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()) {
152 for (int i = 0; i < m_effectList.count(); i++) {
153 QString effectId = m_effectList.item(i).attribute("id");
154 // check if it is a fade effect
155 QDomNodeList params = m_effectList.item(i).elementsByTagName("parameter");
157 for (int j = 0; j < params.count(); j++) {
158 QDomElement e = params.item(j).toElement();
160 if (effectId == "fadein") {
161 if (m_effectList.hasEffect(QString(), "fade_from_black") == -1) {
162 if (e.attribute("name") == "out") fade += e.attribute("value").toInt();
163 else if (e.attribute("name") == "in") fade -= e.attribute("value").toInt();
165 QDomElement fadein = m_effectList.getEffectByTag(QString(), "fade_from_black");
166 if (fadein.attribute("name") == "out") fade += fadein.attribute("value").toInt();
167 else if (fadein.attribute("name") == "in") fade -= fadein.attribute("value").toInt();
169 } else if (effectId == "fade_from_black") {
170 if (m_effectList.hasEffect(QString(), "fadein") == -1) {
171 if (e.attribute("name") == "out") fade += e.attribute("value").toInt();
172 else if (e.attribute("name") == "in") fade -= e.attribute("value").toInt();
174 QDomElement fadein = m_effectList.getEffectByTag(QString(), "fadein");
175 if (fadein.attribute("name") == "out") fade += fadein.attribute("value").toInt();
176 else if (fadein.attribute("name") == "in") fade -= fadein.attribute("value").toInt();
178 } else if (effectId == "fadeout") {
179 if (m_effectList.hasEffect(QString(), "fade_to_black") == -1) {
180 if (e.attribute("name") == "out") fade -= e.attribute("value").toInt();
181 else if (e.attribute("name") == "in") fade += e.attribute("value").toInt();
183 QDomElement fadeout = m_effectList.getEffectByTag(QString(), "fade_to_black");
184 if (fadeout.attribute("name") == "out") fade -= fadeout.attribute("value").toInt();
185 else if (fadeout.attribute("name") == "in") fade += fadeout.attribute("value").toInt();
187 } else if (effectId == "fade_to_black") {
188 if (m_effectList.hasEffect(QString(), "fadeout") == -1) {
189 if (e.attribute("name") == "out") fade -= e.attribute("value").toInt();
190 else if (e.attribute("name") == "in") fade += e.attribute("value").toInt();
192 QDomElement fadeout = m_effectList.getEffectByTag(QString(), "fadeout");
193 if (fadeout.attribute("name") == "out") fade -= fadeout.attribute("value").toInt();
194 else if (fadeout.attribute("name") == "in") fade += fadeout.attribute("value").toInt();
199 if (fade > 0) m_startFade = fade;
200 else if (fade < 0) m_endFade = -fade;
202 setSelectedEffect(0);
206 const EffectsList ClipItem::effectList() const
211 int ClipItem::selectedEffectIndex() const
213 return m_selectedEffect;
216 void ClipItem::initEffect(QDomElement effect, int diff)
218 // the kdenlive_ix int is used to identify an effect in mlt's playlist, should
220 if (effect.attribute("kdenlive_ix").toInt() == 0)
221 effect.setAttribute("kdenlive_ix", QString::number(effectsCounter()));
223 if (effect.attribute("id") == "freeze" && diff > 0) {
224 EffectsList::setParameter(effect, "frame", QString::number(diff));
227 // Init parameter value & keyframes if required
228 QDomNodeList params = effect.elementsByTagName("parameter");
229 for (int i = 0; i < params.count(); i++) {
230 QDomElement e = params.item(i).toElement();
231 kDebug() << "// init eff: " << e.attribute("name");
233 // Check if this effect has a variable parameter
234 if (e.attribute("default").startsWith('%')) {
235 double evaluatedValue = ProfilesDialog::getStringEval(projectScene()->profile(), e.attribute("default"));
236 e.setAttribute("default", evaluatedValue);
237 if (e.hasAttribute("value") && e.attribute("value").startsWith('%')) {
238 e.setAttribute("value", evaluatedValue);
242 if (!e.isNull() && (e.attribute("type") == "keyframe" || e.attribute("type") == "simplekeyframe")) {
243 QString def = e.attribute("default");
244 // Effect has a keyframe type parameter, we need to set the values
245 if (e.attribute("keyframes").isEmpty()) {
246 e.setAttribute("keyframes", QString::number(cropStart().frames(m_fps)) + ':' + def + ';' + QString::number((cropStart() + cropDuration()).frames(m_fps) - 1) + ':' + def);
247 kDebug() << "///// EFFECT KEYFRAMES INITED: " << e.attribute("keyframes");
252 if (effect.attribute("tag") == "volume" || effect.attribute("tag") == "brightness") {
253 if (effect.attribute("id") == "fadeout" || effect.attribute("id") == "fade_to_black") {
254 int end = (cropDuration() + cropStart()).frames(m_fps);
256 if (effect.attribute("id") == "fadeout") {
257 if (m_effectList.hasEffect(QString(), "fade_to_black") == -1) {
258 int effectDuration = EffectsList::parameter(effect, "in").toInt();
259 if (effectDuration > cropDuration().frames(m_fps)) {
260 effectDuration = cropDuration().frames(m_fps) / 2;
262 start -= effectDuration;
264 QDomElement fadeout = m_effectList.getEffectByTag(QString(), "fade_to_black");
265 start -= EffectsList::parameter(fadeout, "out").toInt() - EffectsList::parameter(fadeout, "in").toInt();
267 } else if (effect.attribute("id") == "fade_to_black") {
268 if (m_effectList.hasEffect(QString(), "fadeout") == -1) {
269 int effectDuration = EffectsList::parameter(effect, "in").toInt();
270 if (effectDuration > cropDuration().frames(m_fps)) {
271 effectDuration = cropDuration().frames(m_fps) / 2;
273 start -= effectDuration;
275 QDomElement fadeout = m_effectList.getEffectByTag(QString(), "fadeout");
276 start -= EffectsList::parameter(fadeout, "out").toInt() - EffectsList::parameter(fadeout, "in").toInt();
279 EffectsList::setParameter(effect, "in", QString::number(start));
280 EffectsList::setParameter(effect, "out", QString::number(end));
281 } else if (effect.attribute("id") == "fadein" || effect.attribute("id") == "fade_from_black") {
282 int start = cropStart().frames(m_fps);
284 if (effect.attribute("id") == "fadein") {
285 if (m_effectList.hasEffect(QString(), "fade_from_black") == -1) {
286 int effectDuration = EffectsList::parameter(effect, "out").toInt();
287 if (effectDuration > cropDuration().frames(m_fps)) {
288 effectDuration = cropDuration().frames(m_fps) / 2;
290 end += effectDuration;
292 end += EffectsList::parameter(m_effectList.getEffectByTag(QString(), "fade_from_black"), "out").toInt();
293 } else if (effect.attribute("id") == "fade_from_black") {
294 if (m_effectList.hasEffect(QString(), "fadein") == -1) {
295 int effectDuration = EffectsList::parameter(effect, "out").toInt();
296 if (effectDuration > cropDuration().frames(m_fps)) {
297 effectDuration = cropDuration().frames(m_fps) / 2;
299 end += effectDuration;
301 end += EffectsList::parameter(m_effectList.getEffectByTag(QString(), "fadein"), "out").toInt();
303 EffectsList::setParameter(effect, "in", QString::number(start));
304 EffectsList::setParameter(effect, "out", QString::number(end));
309 bool ClipItem::checkKeyFrames()
311 bool clipEffectsModified = false;
312 // go through all effects this clip has
313 for (int ix = 0; ix < m_effectList.count(); ++ix) {
314 QStringList keyframeParams = keyframes(ix);
315 QStringList newKeyFrameParams;
316 bool effModified = false;
318 // go through all params which have keyframes
319 foreach(const QString &kfr, keyframeParams) {
320 const QStringList keyframes = kfr.split(';', QString::SkipEmptyParts);
321 QStringList newKeyFrames;
322 bool cutKeyFrame = false;
323 bool modified = false;
325 double lastValue = -1;
326 int start = cropStart().frames(m_fps);
327 int end = (cropStart() + cropDuration()).frames(m_fps);
329 // go through all keyframes for one param
330 foreach(const QString &str, keyframes) {
331 int pos = str.section(':', 0, 0).toInt();
332 double val = str.section(':', 1, 1).toDouble();
333 if (pos - start < 0) {
334 // a keyframe is defined before the start of the clip
336 } else if (cutKeyFrame) {
337 // create new keyframe at clip start, calculate interpolated value
339 int diff = pos - lastPos;
340 double ratio = (double)(start - lastPos) / diff;
341 double newValue = lastValue + (val - lastValue) * ratio;
342 newKeyFrames.append(QString::number(start) + ':' + QString::number(newValue));
349 // create new keyframe at clip end, calculate interpolated value
350 int diff = pos - lastPos;
352 double ratio = (double)(end - lastPos) / diff;
353 double newValue = lastValue + (val - lastValue) * ratio;
354 newKeyFrames.append(QString::number(end) + ':' + QString::number(newValue));
359 newKeyFrames.append(QString::number(pos) + ':' + QString::number(val));
366 newKeyFrameParams.append(newKeyFrames.join(";"));
373 setKeyframes(ix, newKeyFrameParams);
374 clipEffectsModified = true;
377 return clipEffectsModified;
380 void ClipItem::setKeyframes(const int ix, const QStringList keyframes)
382 QDomElement effect = getEffectAt(ix);
383 if (effect.attribute("disable") == "1") return;
384 QDomNodeList params = effect.elementsByTagName("parameter");
385 int keyframeParams = 0;
386 for (int i = 0; i < params.count(); i++) {
387 QDomElement e = params.item(i).toElement();
388 if (!e.isNull() && (e.attribute("type") == "keyframe" || e.attribute("type") == "simplekeyframe")) {
389 e.setAttribute("keyframes", keyframes.at(keyframeParams));
390 if (ix == m_selectedEffect && keyframeParams == 0) {
392 double max = e.attribute("max").toDouble();
393 double min = e.attribute("min").toDouble();
394 m_keyframeFactor = 100.0 / (max - min);
395 m_keyframeDefault = e.attribute("default").toDouble();
396 m_selectedKeyframe = 0;
398 const QStringList keyframes = e.attribute("keyframes").split(';', QString::SkipEmptyParts);
399 foreach(const QString &str, keyframes) {
400 int pos = str.section(':', 0, 0).toInt();
401 double val = str.section(':', 1, 1).toDouble();
402 m_keyframes[pos] = val;
404 if (m_keyframes.find(m_editedKeyframe) == m_keyframes.end()) m_editedKeyframe = -1;
405 if (m_keyframes.find(m_editedKeyframe) == m_keyframes.end()) m_editedKeyframe = -1;
414 void ClipItem::setSelectedEffect(const int ix)
416 m_selectedEffect = ix;
417 QDomElement effect = effectAt(m_selectedEffect);
418 if (effect.isNull() == false) {
419 QDomNodeList params = effect.elementsByTagName("parameter");
420 if (effect.attribute("disable") != "1")
421 for (int i = 0; i < params.count(); i++) {
422 QDomElement e = params.item(i).toElement();
423 if (!e.isNull() && (e.attribute("type") == "keyframe" || e.attribute("type") == "simplekeyframe")) {
425 double max = e.attribute("max").toDouble();
426 double min = e.attribute("min").toDouble();
427 m_keyframeFactor = 100.0 / (max - min);
428 m_keyframeDefault = e.attribute("default").toDouble();
429 m_selectedKeyframe = 0;
432 const QStringList keyframes = e.attribute("keyframes").split(';', QString::SkipEmptyParts);
433 foreach(const QString &str, keyframes) {
434 int pos = str.section(':', 0, 0).toInt();
435 double val = str.section(':', 1, 1).toDouble();
436 m_keyframes[pos] = val;
438 if (m_keyframes.find(m_editedKeyframe) == m_keyframes.end()) m_editedKeyframe = -1;
444 if (!m_keyframes.isEmpty()) {
450 QStringList ClipItem::keyframes(const int index)
453 QDomElement effect = effectAt(index);
454 QDomNodeList params = effect.elementsByTagName("parameter");
456 for (int i = 0; i < params.count(); i++) {
457 QDomElement e = params.item(i).toElement();
458 if (!e.isNull() && (e.attribute("type") == "keyframe" || e.attribute("type") == "simplekeyframe"))
459 result.append(e.attribute("keyframes"));
464 void ClipItem::updateKeyframeEffect()
466 // regenerate xml parameter from the clip keyframes
467 QDomElement effect = getEffectAt(m_selectedEffect);
468 if (effect.attribute("disable") == "1") return;
469 QDomNodeList params = effect.elementsByTagName("parameter");
471 for (int i = 0; i < params.count(); i++) {
472 QDomElement e = params.item(i).toElement();
473 if (!e.isNull() && (e.attribute("type") == "keyframe" || e.attribute("type") == "simplekeyframe")) {
475 if (m_keyframes.count() > 0) {
476 QMap<int, int>::const_iterator i = m_keyframes.constBegin();
477 while (i != m_keyframes.constEnd()) {
478 keyframes.append(QString::number(i.key()) + ':' + QString::number(i.value()) + ';');
482 // Effect has a keyframe type parameter, we need to set the values
483 //kDebug() << "::::::::::::::: SETTING EFFECT KEYFRAMES: " << keyframes;
484 e.setAttribute("keyframes", keyframes);
490 QDomElement ClipItem::selectedEffect()
492 if (m_selectedEffect == -1 || m_effectList.isEmpty()) return QDomElement();
493 return effectAt(m_selectedEffect);
496 void ClipItem::resetThumbs(bool clearExistingThumbs)
498 if (clearExistingThumbs) {
499 m_startPix = QPixmap();
500 m_endPix = QPixmap();
501 m_audioThumbCachePic.clear();
507 void ClipItem::refreshClip(bool checkDuration)
509 if (checkDuration && (m_maxDuration != m_clip->maxDuration())) {
510 m_maxDuration = m_clip->maxDuration();
511 if (m_clipType != IMAGE && m_clipType != TEXT && m_clipType != COLOR) {
512 if (m_maxDuration != GenTime() && m_info.cropStart + m_info.cropDuration > m_maxDuration) {
513 // Clip duration changed, make sure to stay in correct range
514 if (m_info.cropStart > m_maxDuration) {
515 m_info.cropStart = GenTime();
516 m_info.cropDuration = qMin(m_info.cropDuration, m_maxDuration);
518 m_info.cropDuration = m_maxDuration;
520 updateRectGeometry();
524 if (m_clipType == COLOR) {
525 QString colour = m_clip->getProperty("colour");
526 colour = colour.replace(0, 2, "#");
527 m_baseColor = QColor(colour.left(7));
528 } else resetThumbs(checkDuration);
531 void ClipItem::slotFetchThumbs()
533 if (scene() == NULL || m_clipType == AUDIO || m_clipType == COLOR) return;
534 if (m_clipType == IMAGE) {
535 if (m_startPix.isNull()) {
536 m_startPix = KThumb::getImage(KUrl(m_clip->getProperty("resource")), (int)(KdenliveSettings::trackheight() * KdenliveSettings::project_display_ratio()), KdenliveSettings::trackheight());
542 if (m_clipType == TEXT) {
543 if (m_startPix.isNull()) slotGetStartThumb();
547 if (m_endPix.isNull() && m_startPix.isNull()) {
548 m_startThumbRequested = true;
549 m_endThumbRequested = true;
550 emit getThumb((int)m_speedIndependantInfo.cropStart.frames(m_fps), (int)(m_speedIndependantInfo.cropStart + m_speedIndependantInfo.cropDuration).frames(m_fps) - 1);
552 if (m_endPix.isNull()) {
555 if (m_startPix.isNull()) {
561 void ClipItem::slotGetStartThumb()
563 m_startThumbRequested = true;
564 emit getThumb((int)m_speedIndependantInfo.cropStart.frames(m_fps), -1);
567 void ClipItem::slotGetEndThumb()
569 m_endThumbRequested = true;
570 emit getThumb(-1, (int)(m_speedIndependantInfo.cropStart + m_speedIndependantInfo.cropDuration).frames(m_fps) - 1);
574 void ClipItem::slotSetStartThumb(QImage img)
576 if (!img.isNull() && img.format() == QImage::Format_ARGB32) {
577 QPixmap pix = QPixmap::fromImage(img);
579 QRectF r = sceneBoundingRect();
580 r.setRight(pix.width() + 2);
585 void ClipItem::slotSetEndThumb(QImage img)
587 if (!img.isNull() && img.format() == QImage::Format_ARGB32) {
588 QPixmap pix = QPixmap::fromImage(img);
590 QRectF r = sceneBoundingRect();
591 r.setLeft(r.right() - pix.width() - 2);
596 void ClipItem::slotThumbReady(int frame, QImage img)
598 if (scene() == NULL) return;
599 QRectF r = boundingRect();
600 QPixmap pix = QPixmap::fromImage(img);
601 double width = pix.width() / projectScene()->scale().x();
602 if (m_startThumbRequested && frame == m_speedIndependantInfo.cropStart.frames(m_fps)) {
604 m_startThumbRequested = false;
605 update(r.left(), r.top(), width, pix.height());
606 if (m_clipType == IMAGE || m_clipType == TEXT) {
607 update(r.right() - width, r.top(), width, pix.height());
609 } else if (m_endThumbRequested && frame == (m_speedIndependantInfo.cropStart + m_speedIndependantInfo.cropDuration).frames(m_fps) - 1) {
611 m_endThumbRequested = false;
612 update(r.right() - width, r.top(), width, pix.height());
616 void ClipItem::slotSetStartThumb(const QPixmap pix)
621 void ClipItem::slotSetEndThumb(const QPixmap pix)
626 QPixmap ClipItem::startThumb() const
631 QPixmap ClipItem::endThumb() const
636 void ClipItem::slotGotAudioData()
638 m_audioThumbReady = true;
639 if (m_clipType == AV && !isAudioOnly()) {
640 QRectF r = boundingRect();
641 r.setTop(r.top() + r.height() / 2 - 1);
646 int ClipItem::type() const
651 DocClipBase *ClipItem::baseClip() const
656 QDomElement ClipItem::xml() const
661 QDomElement ClipItem::itemXml() const
663 QDomElement xml = m_clip->toXML();
664 if (m_speed != 1.0) xml.setAttribute("speed", m_speed);
665 if (m_strobe > 1) xml.setAttribute("strobe", m_strobe);
666 if (m_audioOnly) xml.setAttribute("audio_only", 1);
667 else if (m_videoOnly) xml.setAttribute("video_only", 1);
671 int ClipItem::clipType() const
676 QString ClipItem::clipName() const
681 void ClipItem::setClipName(const QString &name)
686 const QString ClipItem::clipProducer() const
691 void ClipItem::flashClip()
693 if (m_timeLine == 0) {
694 m_timeLine = new QTimeLine(750, this);
695 m_timeLine->setUpdateInterval(80);
696 m_timeLine->setCurveShape(QTimeLine::EaseInOutCurve);
697 m_timeLine->setFrameRange(0, 100);
698 connect(m_timeLine, SIGNAL(valueChanged(qreal)), this, SLOT(animate(qreal)));
700 //m_timeLine->start();
703 void ClipItem::animate(qreal /*value*/)
705 QRectF r = boundingRect();
711 void ClipItem::paint(QPainter *painter,
712 const QStyleOptionGraphicsItem *option,
716 if (parentItem()) paintColor = QColor(255, 248, 149);
717 else paintColor = m_baseColor;
718 if (isSelected() || (parentItem() && parentItem()->isSelected())) paintColor = paintColor.darker();
720 painter->setMatrixEnabled(false);
721 const QRectF mapped = painter->matrix().mapRect(rect()).adjusted(0.5, 0, 0.5, 0);
722 const QRectF exposed = option->exposedRect;
723 painter->setClipRect(mapped);
724 painter->fillRect(mapped, paintColor);
727 if (KdenliveSettings::videothumbnails() && !isAudioOnly()) {
728 QPen pen = painter->pen();
729 pen.setColor(QColor(255, 255, 255, 150));
730 painter->setPen(pen);
731 if ((m_clipType == IMAGE || m_clipType == TEXT) && !m_startPix.isNull()) {
732 const QPointF top = mapped.topRight() - QPointF(m_startPix.width() - 1, 0);
733 painter->drawPixmap(top, m_startPix);
734 QLineF l2(top.x(), mapped.top(), top.x(), mapped.bottom());
735 painter->drawLine(l2);
736 } else if (!m_endPix.isNull()) {
737 const QPointF top = mapped.topRight() - QPointF(m_endPix.width() - 1, 0);
738 painter->drawPixmap(top, m_endPix);
739 QLineF l2(top.x(), mapped.top(), top.x(), mapped.bottom());
740 painter->drawLine(l2);
742 if (!m_startPix.isNull()) {
743 painter->drawPixmap(mapped.topLeft(), m_startPix);
744 QLineF l2(mapped.left() + m_startPix.width(), mapped.top(), mapped.left() + m_startPix.width(), mapped.bottom());
745 painter->drawLine(l2);
747 if (painter->matrix().m11() == FRAME_SIZE) {
748 int offset = (m_info.startPos - m_info.cropStart).frames(m_fps);
749 int left = qMax((int) m_info.startPos.frames(m_fps) + 1, (int) mapToScene(exposed.left(), 0).x());
750 int right = qMin((int)(m_info.startPos + m_info.cropDuration).frames(m_fps) - 1, (int) mapToScene(exposed.right(), 0).x());
751 doGetIntraThumbs(painter, mapped.topLeft(), m_info.cropStart.frames(m_fps), left - offset, right - offset);
753 painter->setPen(Qt::black);
756 // draw audio thumbnails
757 if (KdenliveSettings::audiothumbnails() && m_speed == 1.0 && !isVideoOnly() && ((m_clipType == AV && (exposed.bottom() > (rect().height() / 2) || isAudioOnly())) || m_clipType == AUDIO) && m_audioThumbReady) {
759 double startpixel = exposed.left();
762 double endpixel = exposed.right();
765 //kDebug()<<"/// REPAINTING AUDIO THMBS ZONE: "<<startpixel<<"x"<<endpixel;
767 /*QPainterPath path = m_clipType == AV ? roundRectPathLower : resultClipPath;*/
769 if (m_clipType == AV && !isAudioOnly()) {
771 mappedRect.setTop(mappedRect.bottom() - mapped.height() / 2);
772 } else mappedRect = mapped;
774 double scale = painter->matrix().m11();
776 if (isEnabled() && m_clip) channels = m_clip->getProperty("channels").toInt();
777 if (scale != m_framePixelWidth)
778 m_audioThumbCachePic.clear();
779 double cropLeft = m_info.cropStart.frames(m_fps);
780 const int clipStart = mappedRect.x();
781 const int mappedStartPixel = painter->matrix().map(QPointF(startpixel + cropLeft, 0)).x() - clipStart;
782 const int mappedEndPixel = painter->matrix().map(QPointF(endpixel + cropLeft, 0)).x() - clipStart;
783 cropLeft = cropLeft * scale;
787 emit prepareAudioThumb(scale, mappedStartPixel, mappedEndPixel, channels);
790 for (int startCache = mappedStartPixel - (mappedStartPixel) % 100; startCache < mappedEndPixel; startCache += 100) {
791 if (m_audioThumbCachePic.contains(startCache) && !m_audioThumbCachePic[startCache].isNull())
792 painter->drawPixmap(clipStart + startCache - cropLeft, mappedRect.y(), m_audioThumbCachePic[startCache]);
796 // Draw effects names
797 if (!m_effectNames.isEmpty() && mapped.width() > 40) {
798 QRectF txtBounding = painter->boundingRect(mapped, Qt::AlignLeft | Qt::AlignTop, m_effectNames);
800 if (m_timeLine && m_timeLine->state() == QTimeLine::Running) {
801 qreal value = m_timeLine->currentValue();
802 txtBounding.setWidth(txtBounding.width() * value);
803 bgColor.setRgb(50 + 200 *(1.0 - value), 50, 50, 100 + 50 * value);
804 } else bgColor.setRgb(50, 50, 90, 180);
806 QPainterPath rounded;
807 rounded.moveTo(txtBounding.bottomRight());
808 rounded.arcTo(txtBounding.right() - txtBounding.height() - 2, txtBounding.top() - txtBounding.height(), txtBounding.height() * 2, txtBounding.height() * 2, 270, 90);
809 rounded.lineTo(txtBounding.topLeft());
810 rounded.lineTo(txtBounding.bottomLeft());
811 painter->fillPath(rounded, bgColor);
812 painter->setPen(Qt::lightGray);
813 painter->drawText(txtBounding.adjusted(1, 0, 1, 0), Qt::AlignCenter, m_effectNames);
817 QColor frameColor(paintColor.darker());
818 if (isSelected() || (parentItem() && parentItem()->isSelected())) {
819 frameColor = QColor(Qt::red);
821 frameColor.setAlpha(160);
823 const QRectF txtBounding2 = painter->boundingRect(mapped, Qt::AlignHCenter | Qt::AlignVCenter, ' ' + m_clipName + ' ');
824 //painter->fillRect(txtBounding2, frameColor);
825 painter->setBrush(frameColor);
826 painter->setPen(Qt::NoPen);
827 painter->drawRoundedRect(txtBounding2, 3, 3);
828 painter->setBrush(QBrush(Qt::NoBrush));
830 //painter->setPen(QColor(0, 0, 0, 180));
831 //painter->drawText(txtBounding, Qt::AlignCenter, m_clipName);
833 painter->drawPixmap(txtBounding2.topLeft() - QPointF(17, -1), m_videoPix);
834 } else if (m_audioOnly) {
835 painter->drawPixmap(txtBounding2.topLeft() - QPointF(17, -1), m_audioPix);
837 painter->setPen(Qt::white);
838 painter->drawText(txtBounding2, Qt::AlignCenter, m_clipName);
842 if (isEnabled() && m_clip) {
843 QList < CommentedTime > markers = m_clip->commentedSnapMarkers();
844 QList < CommentedTime >::Iterator it = markers.begin();
847 QBrush markerBrush(QColor(120, 120, 0, 140));
848 QPen pen = painter->pen();
849 pen.setColor(QColor(255, 255, 255, 200));
850 pen.setStyle(Qt::DotLine);
852 for (; it != markers.end(); ++it) {
853 pos = GenTime((int)((*it).time().frames(m_fps) / qAbs(m_speed) + 0.5), m_fps) - cropStart();
854 if (pos > GenTime()) {
855 if (pos > cropDuration()) break;
856 QLineF l(rect().x() + pos.frames(m_fps), rect().y(), rect().x() + pos.frames(m_fps), rect().bottom());
857 QLineF l2 = painter->matrix().map(l);
858 painter->setPen(pen);
859 painter->drawLine(l2);
860 if (KdenliveSettings::showmarkers()) {
861 framepos = rect().x() + pos.frames(m_fps);
862 const QRectF r1(framepos + 0.04, 10, rect().width() - framepos - 2, rect().height() - 10);
863 const QRectF r2 = painter->matrix().mapRect(r1);
864 const QRectF txtBounding3 = painter->boundingRect(r2, Qt::AlignLeft | Qt::AlignTop, ' ' + (*it).comment() + ' ');
865 painter->setBrush(markerBrush);
866 painter->setPen(Qt::NoPen);
867 painter->drawRoundedRect(txtBounding3, 3, 3);
868 painter->setBrush(QBrush(Qt::NoBrush));
869 painter->setPen(Qt::white);
870 painter->drawText(txtBounding3, Qt::AlignCenter, (*it).comment());
872 //painter->fillRect(QRect(br.x() + framepos, br.y(), 10, br.height()), QBrush(QColor(0, 0, 0, 150)));
877 // draw start / end fades
880 fades = QBrush(QColor(200, 50, 50, 150));
881 } else fades = QBrush(QColor(200, 200, 200, 200));
883 if (m_startFade != 0) {
884 QPainterPath fadeInPath;
885 fadeInPath.moveTo(0, 0);
886 fadeInPath.lineTo(0, rect().height());
887 fadeInPath.lineTo(m_startFade, 0);
888 fadeInPath.closeSubpath();
889 QPainterPath f1 = painter->matrix().map(fadeInPath);
890 painter->fillPath(f1/*.intersected(resultClipPath)*/, fades);
891 /*if (isSelected()) {
892 QLineF l(m_startFade * scale, 0, 0, itemHeight);
893 painter->drawLine(l);
896 if (m_endFade != 0) {
897 QPainterPath fadeOutPath;
898 fadeOutPath.moveTo(rect().width(), 0);
899 fadeOutPath.lineTo(rect().width(), rect().height());
900 fadeOutPath.lineTo(rect().width() - m_endFade, 0);
901 fadeOutPath.closeSubpath();
902 QPainterPath f1 = painter->matrix().map(fadeOutPath);
903 painter->fillPath(f1/*.intersected(resultClipPath)*/, fades);
904 /*if (isSelected()) {
905 QLineF l(itemWidth - m_endFade * scale, 0, itemWidth, itemHeight);
906 painter->drawLine(l);
911 painter->setPen(QPen(Qt::lightGray));
912 // draw effect or transition keyframes
913 if (mapped.width() > 20) drawKeyFrames(painter, exposed);
915 //painter->setMatrixEnabled(true);
918 // expand clip rect to allow correct painting of clip border
919 QPen pen1(frameColor);
920 painter->setPen(pen1);
921 painter->setClipping(false);
922 painter->drawRect(painter->matrix().mapRect(rect()));
926 OPERATIONTYPE ClipItem::operationMode(QPointF pos)
928 if (isItemLocked()) return NONE;
929 const double scale = projectScene()->scale().x();
930 double maximumOffset = 6 / scale;
931 if (isSelected() || (parentItem() && parentItem()->isSelected())) {
932 int kf = mouseOverKeyFrames(pos, maximumOffset);
934 m_editedKeyframe = kf;
938 QRectF rect = sceneBoundingRect();
939 int addtransitionOffset = 10;
940 // Don't allow add transition if track height is very small
941 if (rect.height() < 30) addtransitionOffset = 0;
943 if (qAbs((int)(pos.x() - (rect.x() + m_startFade))) < maximumOffset && qAbs((int)(pos.y() - rect.y())) < 6) {
944 if (m_startFade == 0) setToolTip(i18n("Add audio fade"));
945 // xgettext:no-c-format
946 else setToolTip(i18n("Audio fade duration: %1s", GenTime(m_startFade, m_fps).seconds()));
948 } else if (pos.x() - rect.x() < maximumOffset && (rect.bottom() - pos.y() > addtransitionOffset)) {
949 // xgettext:no-c-format
950 setToolTip(i18n("Crop from start: %1s", cropStart().seconds()));
952 } else if (qAbs((int)(pos.x() - (rect.x() + rect.width() - m_endFade))) < maximumOffset && qAbs((int)(pos.y() - rect.y())) < 6) {
953 if (m_endFade == 0) setToolTip(i18n("Add audio fade"));
954 // xgettext:no-c-format
955 else setToolTip(i18n("Audio fade duration: %1s", GenTime(m_endFade, m_fps).seconds()));
957 } else if ((rect.right() - pos.x() < maximumOffset) && (rect.bottom() - pos.y() > addtransitionOffset)) {
958 // xgettext:no-c-format
959 setToolTip(i18n("Clip duration: %1s", cropDuration().seconds()));
961 } else if ((pos.x() - rect.x() < 16 / scale) && (rect.bottom() - pos.y() <= addtransitionOffset)) {
962 setToolTip(i18n("Add transition"));
963 return TRANSITIONSTART;
964 } else if ((rect.right() - pos.x() < 16 / scale) && (rect.bottom() - pos.y() <= addtransitionOffset)) {
965 setToolTip(i18n("Add transition"));
966 return TRANSITIONEND;
968 QString tooltip = "<b>" + m_clipName + "</b>";
969 if (!baseClip()->fileURL().isEmpty())
970 tooltip.append("<br />" + baseClip()->fileURL().path());
971 if (!baseClip()->description().isEmpty())
972 tooltip.append("<br />" + baseClip()->description());
977 QList <GenTime> ClipItem::snapMarkers() const
979 QList < GenTime > snaps;
980 QList < GenTime > markers = baseClip()->snapMarkers();
983 for (int i = 0; i < markers.size(); i++) {
984 pos = GenTime((int)(markers.at(i).frames(m_fps) / qAbs(m_speed) + 0.5), m_fps) - cropStart();
985 if (pos > GenTime()) {
986 if (pos > cropDuration()) break;
987 else snaps.append(pos + startPos());
993 QList <CommentedTime> ClipItem::commentedSnapMarkers() const
995 QList < CommentedTime > snaps;
996 QList < CommentedTime > markers = baseClip()->commentedSnapMarkers();
999 for (int i = 0; i < markers.size(); i++) {
1000 pos = GenTime((int)(markers.at(i).time().frames(m_fps) / qAbs(m_speed) + 0.5), m_fps) - cropStart();
1001 if (pos > GenTime()) {
1002 if (pos > cropDuration()) break;
1003 else snaps.append(CommentedTime(pos + startPos(), markers.at(i).comment()));
1009 void ClipItem::slotPrepareAudioThumb(double pixelForOneFrame, int startpixel, int endpixel, int channels)
1011 QRectF re = sceneBoundingRect();
1012 if (m_clipType == AV && !isAudioOnly()) re.setTop(re.y() + re.height() / 2);
1014 //kDebug() << "// PREP AUDIO THMB FRMO : scale:" << pixelForOneFrame<< ", from: " << startpixel << ", to: " << endpixel;
1015 //if ( (!audioThumbWasDrawn || framePixelWidth!=pixelForOneFrame ) && !baseClip()->audioFrameChache.isEmpty()){
1017 for (int startCache = startpixel - startpixel % 100; startCache < endpixel; startCache += 100) {
1018 //kDebug() << "creating " << startCache;
1019 //if (framePixelWidth!=pixelForOneFrame ||
1020 if (m_framePixelWidth == pixelForOneFrame && m_audioThumbCachePic.contains(startCache))
1022 if (m_audioThumbCachePic[startCache].isNull() || m_framePixelWidth != pixelForOneFrame) {
1023 m_audioThumbCachePic[startCache] = QPixmap(100, (int)(re.height()));
1024 m_audioThumbCachePic[startCache].fill(QColor(180, 180, 200, 140));
1026 bool fullAreaDraw = pixelForOneFrame < 10;
1027 QMap<int, QPainterPath > positiveChannelPaths;
1028 QMap<int, QPainterPath > negativeChannelPaths;
1029 QPainter pixpainter(&m_audioThumbCachePic[startCache]);
1031 audiopen.setWidth(0);
1032 pixpainter.setPen(audiopen);
1033 //pixpainter.setRenderHint(QPainter::Antialiasing,true);
1034 //pixpainter.drawLine(0,0,100,re.height());
1035 // Bail out, if caller provided invalid data
1036 if (channels <= 0) {
1037 kWarning() << "Unable to draw image with " << channels << "number of channels";
1041 int channelHeight = m_audioThumbCachePic[startCache].height() / channels;
1043 for (int i = 0; i < channels; i++) {
1045 positiveChannelPaths[i].moveTo(0, channelHeight*i + channelHeight / 2);
1046 negativeChannelPaths[i].moveTo(0, channelHeight*i + channelHeight / 2);
1049 for (int samples = 0; samples <= 100; samples++) {
1050 double frame = (double)(samples + startCache - 0) / pixelForOneFrame;
1051 int sample = (int)((frame - (int)(frame)) * 20); // AUDIO_FRAME_SIZE
1052 if (frame < 0 || sample < 0 || sample > 19)
1054 QMap<int, QByteArray> frame_channel_data = baseClip()->m_audioFrameCache[(int)frame];
1056 for (int channel = 0; channel < channels && frame_channel_data[channel].size() > 0; channel++) {
1058 int y = channelHeight * channel + channelHeight / 2;
1059 int delta = (int)(frame_channel_data[channel][sample] - 127 / 2) * channelHeight / 64;
1061 positiveChannelPaths[channel].lineTo(samples, 0.1 + y + qAbs(delta));
1062 negativeChannelPaths[channel].lineTo(samples, 0.1 + y - qAbs(delta));
1064 positiveChannelPaths[channel].lineTo(samples, 0.1 + y + delta);
1065 negativeChannelPaths[channel].lineTo(samples, 0.1 + y - delta);
1068 for (int channel = 0; channel < channels ; channel++)
1069 if (fullAreaDraw && samples == 100) {
1070 positiveChannelPaths[channel].lineTo(samples, channelHeight*channel + channelHeight / 2);
1071 negativeChannelPaths[channel].lineTo(samples, channelHeight*channel + channelHeight / 2);
1072 positiveChannelPaths[channel].lineTo(0, channelHeight*channel + channelHeight / 2);
1073 negativeChannelPaths[channel].lineTo(0, channelHeight*channel + channelHeight / 2);
1077 pixpainter.setPen(QPen(QColor(0, 0, 0)));
1078 pixpainter.setBrush(QBrush(QColor(60, 60, 60)));
1080 for (int i = 0; i < channels; i++) {
1082 //pixpainter.fillPath(positiveChannelPaths[i].united(negativeChannelPaths[i]),QBrush(Qt::SolidPattern));//or singleif looks better
1083 pixpainter.drawPath(positiveChannelPaths[i].united(negativeChannelPaths[i]));//or singleif looks better
1085 pixpainter.drawPath(positiveChannelPaths[i]);
1088 //audioThumbWasDrawn=true;
1089 m_framePixelWidth = pixelForOneFrame;
1094 int ClipItem::fadeIn() const
1099 int ClipItem::fadeOut() const
1105 void ClipItem::setFadeIn(int pos)
1107 if (pos == m_startFade) return;
1108 int oldIn = m_startFade;
1109 if (pos < 0) pos = 0;
1110 if (pos > cropDuration().frames(m_fps)) pos = (int)(cropDuration().frames(m_fps));
1112 QRectF rect = boundingRect();
1113 update(rect.x(), rect.y(), qMax(oldIn, pos), rect.height());
1116 void ClipItem::setFadeOut(int pos)
1118 if (pos == m_endFade) return;
1119 int oldOut = m_endFade;
1120 if (pos < 0) pos = 0;
1121 if (pos > cropDuration().frames(m_fps)) pos = (int)(cropDuration().frames(m_fps));
1123 QRectF rect = boundingRect();
1124 update(rect.x() + rect.width() - qMax(oldOut, pos), rect.y(), qMax(oldOut, pos), rect.height());
1128 void ClipItem::setFades(int in, int out)
1136 void ClipItem::hoverEnterEvent(QGraphicsSceneHoverEvent *e)
1138 //if (e->pos().x() < 20) m_hover = true;
1140 if (isItemLocked()) return;
1142 QRectF r = boundingRect();
1143 double width = 35 / projectScene()->scale().x();
1144 double height = r.height() / 2;
1145 //WARNING: seems like it generates a full repaint of the clip, maybe not so good...
1146 update(r.x(), r.y() + height, width, height);
1147 update(r.right() - width, r.y() + height, width, height);
1151 void ClipItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *)
1153 if (isItemLocked()) return;
1155 QRectF r = boundingRect();
1156 double width = 35 / projectScene()->scale().x();
1157 double height = r.height() / 2;
1158 //WARNING: seems like it generates a full repaint of the clip, maybe not so good...
1159 update(r.x(), r.y() + height, width, height);
1160 update(r.right() - width, r.y() + height, width, height);
1164 void ClipItem::resizeStart(int posx, bool /*size*/)
1166 bool sizeLimit = false;
1167 if (clipType() != IMAGE && clipType() != COLOR && clipType() != TEXT) {
1168 const int min = (startPos() - cropStart()).frames(m_fps);
1169 if (posx < min) posx = min;
1173 if (posx == startPos().frames(m_fps)) return;
1174 const int previous = cropStart().frames(m_fps);
1175 AbstractClipItem::resizeStart(posx, sizeLimit);
1177 // set speed independant info
1178 m_speedIndependantInfo = m_info;
1179 m_speedIndependantInfo.cropStart = GenTime((int)(m_info.cropStart.frames(m_fps) * qAbs(m_speed)), m_fps);
1180 m_speedIndependantInfo.cropDuration = GenTime((int)(m_info.cropDuration.frames(m_fps) * qAbs(m_speed)), m_fps);
1182 if ((int) cropStart().frames(m_fps) != previous) {
1183 if (m_hasThumbs && KdenliveSettings::videothumbnails()) {
1184 m_startThumbTimer.start(150);
1189 void ClipItem::resizeEnd(int posx)
1191 const int max = (startPos() - cropStart() + maxDuration()).frames(m_fps);
1192 if (posx > max && maxDuration() != GenTime()) posx = max;
1193 if (posx == endPos().frames(m_fps)) return;
1194 //kDebug() << "// NEW POS: " << posx << ", OLD END: " << endPos().frames(m_fps);
1195 const int previous = cropDuration().frames(m_fps);
1196 AbstractClipItem::resizeEnd(posx);
1198 // set speed independant info
1199 m_speedIndependantInfo = m_info;
1200 m_speedIndependantInfo.cropStart = GenTime((int)(m_info.cropStart.frames(m_fps) * qAbs(m_speed)), m_fps);
1201 m_speedIndependantInfo.cropDuration = GenTime((int)(m_info.cropDuration.frames(m_fps) * qAbs(m_speed)), m_fps);
1203 if ((int) cropDuration().frames(m_fps) != previous) {
1204 if (m_hasThumbs && KdenliveSettings::videothumbnails()) {
1205 m_endThumbTimer.start(150);
1211 bool ClipItem::checkEffectsKeyframesPos(const int previous, const int current, bool fromStart)
1213 bool effModified = false;
1214 for (int i = 0; i < m_effectList.count(); i++) {
1215 QDomElement effect = m_effectList.at(i);
1216 QDomNodeList params = effect.elementsByTagName("parameter");
1217 for (int j = 0; j < params.count(); j++) {
1218 bool modified = false;
1219 QDomElement e = params.item(j).toElement();
1220 if (!e.isNull() && (e.attribute("type") == "keyframe" || e.attribute("type") == "simplekeyframe")) {
1221 // parse keyframes and adjust values
1222 const QStringList keyframes = e.attribute("keyframes").split(';', QString::SkipEmptyParts);
1223 QMap <int, double> kfr;
1226 foreach(const QString &str, keyframes) {
1227 pos = str.section(':', 0, 0).toInt();
1228 val = str.section(':', 1, 1).toDouble();
1229 if (pos == previous) {
1230 // first or last keyframe
1234 if ((fromStart && pos >= current) || (!fromStart && pos <= current)) {
1235 // only keyframes in range
1244 QMap<int, double>::const_iterator k = kfr.constBegin();
1245 while (k != kfr.constEnd()) {
1246 newkfr.append(QString::number(k.key()) + ':' + QString::number(k.value()) + ';');
1249 e.setAttribute("keyframes", newkfr);
1254 if (effModified && m_selectedEffect >= 0) setSelectedEffect(m_selectedEffect);
1259 QVariant ClipItem::itemChange(GraphicsItemChange change, const QVariant &value)
1261 if (change == QGraphicsItem::ItemSelectedChange) {
1262 if (value.toBool()) setZValue(10);
1265 if (change == ItemPositionChange && scene()) {
1266 // calculate new position.
1267 //if (parentItem()) return pos();
1268 QPointF newPos = value.toPointF();
1269 //kDebug() << "/// MOVING CLIP ITEM.------------\n++++++++++";
1270 int xpos = projectScene()->getSnapPointForPos((int) newPos.x(), KdenliveSettings::snaptopoints());
1271 xpos = qMax(xpos, 0);
1273 int newTrack = newPos.y() / KdenliveSettings::trackheight();
1274 newTrack = qMin(newTrack, projectScene()->tracksCount() - 1);
1275 newTrack = qMax(newTrack, 0);
1276 newPos.setY((int)(newTrack * KdenliveSettings::trackheight() + 1));
1277 // Only one clip is moving
1278 QRectF sceneShape = rect();
1279 sceneShape.translate(newPos);
1280 QList<QGraphicsItem*> items;
1281 if (projectScene()->editMode() == NORMALEDIT)
1282 items = scene()->items(sceneShape, Qt::IntersectsItemShape);
1283 items.removeAll(this);
1284 bool forwardMove = newPos.x() > pos().x();
1286 if (!items.isEmpty()) {
1287 for (int i = 0; i < items.count(); i++) {
1288 if (!items.at(i)->isEnabled()) continue;
1289 if (items.at(i)->type() == type()) {
1291 QPointF otherPos = items.at(i)->pos();
1292 if ((int) otherPos.y() != (int) pos().y()) {
1296 offset = qMax(offset, (int)(newPos.x() - (static_cast < AbstractClipItem* >(items.at(i))->startPos() - cropDuration()).frames(m_fps)));
1298 offset = qMax(offset, (int)((static_cast < AbstractClipItem* >(items.at(i))->endPos().frames(m_fps)) - newPos.x()));
1303 sceneShape.translate(QPointF(-offset, 0));
1304 newPos.setX(newPos.x() - offset);
1306 sceneShape.translate(QPointF(offset, 0));
1307 newPos.setX(newPos.x() + offset);
1309 QList<QGraphicsItem*> subitems = scene()->items(sceneShape, Qt::IntersectsItemShape);
1310 subitems.removeAll(this);
1311 for (int j = 0; j < subitems.count(); j++) {
1312 if (!subitems.at(j)->isEnabled()) continue;
1313 if (subitems.at(j)->type() == type()) {
1314 // move was not successful, revert to previous pos
1315 m_info.startPos = GenTime((int) pos().x(), m_fps);
1321 m_info.track = newTrack;
1322 m_info.startPos = GenTime((int) newPos.x(), m_fps);
1328 m_info.track = newTrack;
1329 m_info.startPos = GenTime((int) newPos.x(), m_fps);
1330 //kDebug()<<"// ITEM NEW POS: "<<newPos.x()<<", mapped: "<<mapToScene(newPos.x(), 0).x();
1333 return QGraphicsItem::itemChange(change, value);
1337 /*void ClipItem::mouseMoveEvent(QGraphicsSceneMouseEvent * event) {
1340 int ClipItem::effectsCounter()
1342 return effectsCount() + 1;
1345 int ClipItem::effectsCount()
1347 return m_effectList.count();
1350 int ClipItem::hasEffect(const QString &tag, const QString &id) const
1352 return m_effectList.hasEffect(tag, id);
1355 QStringList ClipItem::effectNames()
1357 return m_effectList.effectNames();
1360 QDomElement ClipItem::effectAt(int ix) const
1362 if (ix > m_effectList.count() - 1 || ix < 0 || m_effectList.at(ix).isNull()) return QDomElement();
1363 return m_effectList.at(ix).cloneNode().toElement();
1366 QDomElement ClipItem::getEffectAt(int ix) const
1368 if (ix > m_effectList.count() - 1 || ix < 0 || m_effectList.at(ix).isNull()) return QDomElement();
1369 return m_effectList.at(ix);
1372 void ClipItem::setEffectAt(int ix, QDomElement effect)
1374 if (ix < 0 || ix > (m_effectList.count() - 1) || effect.isNull()) {
1375 kDebug() << "Invalid effect index: " << ix;
1378 //kDebug() << "CHange EFFECT AT: " << ix << ", CURR: " << m_effectList.at(ix).attribute("tag") << ", NEW: " << effect.attribute("tag");
1379 effect.setAttribute("kdenlive_ix", ix + 1);
1380 m_effectList.replace(ix, effect);
1381 m_effectNames = m_effectList.effectNames().join(" / ");
1382 QString id = effect.attribute("id");
1383 if (id == "fadein" || id == "fadeout" || id == "fade_from_black" || id == "fade_to_black")
1386 QRectF r = boundingRect();
1392 EffectsParameterList ClipItem::addEffect(const QDomElement effect, bool /*animate*/)
1394 bool needRepaint = false;
1396 if (!effect.hasAttribute("kdenlive_ix")) {
1397 ix = effectsCounter();
1398 } else ix = effect.attribute("kdenlive_ix").toInt();
1399 if (!m_effectList.isEmpty() && ix <= m_effectList.count()) {
1401 m_effectList.insert(ix - 1, effect);
1402 for (int i = ix; i < m_effectList.count(); i++) {
1403 int index = m_effectList.item(i).attribute("kdenlive_ix").toInt();
1404 if (index >= ix) m_effectList.item(i).setAttribute("kdenlive_ix", index + 1);
1406 } else m_effectList.append(effect);
1407 EffectsParameterList parameters;
1408 parameters.addParam("tag", effect.attribute("tag"));
1409 parameters.addParam("kdenlive_ix", effect.attribute("kdenlive_ix"));
1410 if (effect.hasAttribute("src")) parameters.addParam("src", effect.attribute("src"));
1411 if (effect.hasAttribute("disable")) parameters.addParam("disable", effect.attribute("disable"));
1413 QString effectId = effect.attribute("id");
1414 if (effectId.isEmpty()) effectId = effect.attribute("tag");
1415 parameters.addParam("id", effectId);
1417 // special case:Â the affine effect need in / out points
1418 if (effectId == "pan_zoom") {
1419 int start = cropStart().frames(m_fps);
1420 int end = (cropStart() + cropDuration()).frames(m_fps);
1425 parameters.addParam("in", QString::number(start));
1426 parameters.addParam("out", QString::number(end));
1429 QDomNodeList params = effect.elementsByTagName("parameter");
1431 for (int i = 0; i < params.count(); i++) {
1432 QDomElement e = params.item(i).toElement();
1434 if (e.attribute("type") == "simplekeyframe") {
1435 QStringList values = e.attribute("keyframes").split(";", QString::SkipEmptyParts);
1436 double factor = e.attribute("factor", "1").toDouble();
1438 for (int j = 0; j < values.count(); j++) {
1439 QString pos = values.at(j).section(":", 0, 0);
1440 double val = values.at(j).section(":", 1, 1).toDouble() / factor;
1441 values[j] = pos + "=" + QString::number(val);
1444 parameters.addParam(e.attribute("name"), values.join(";"));
1445 /*parameters.addParam("max", e.attribute("max"));
1446 parameters.addParam("min", e.attribute("min"));
1447 parameters.addParam("factor", );*/
1448 } else if (e.attribute("type") == "keyframe") {
1449 parameters.addParam("keyframes", e.attribute("keyframes"));
1450 parameters.addParam("max", e.attribute("max"));
1451 parameters.addParam("min", e.attribute("min"));
1452 parameters.addParam("factor", e.attribute("factor", "1"));
1453 parameters.addParam("starttag", e.attribute("starttag", "start"));
1454 parameters.addParam("endtag", e.attribute("endtag", "end"));
1455 } else if (e.attribute("factor", "1") == "1") {
1456 parameters.addParam(e.attribute("name"), e.attribute("value"));
1458 // check if it is a fade effect
1459 if (effectId == "fadein") {
1461 if (m_effectList.hasEffect(QString(), "fade_from_black") == -1) {
1462 if (e.attribute("name") == "out") fade += e.attribute("value").toInt();
1463 else if (e.attribute("name") == "in") fade -= e.attribute("value").toInt();
1465 QDomElement fadein = m_effectList.getEffectByTag(QString(), "fade_from_black");
1466 if (fadein.attribute("name") == "out") fade += fadein.attribute("value").toInt();
1467 else if (fadein.attribute("name") == "in") fade -= fadein.attribute("value").toInt();
1469 } else if (effectId == "fade_from_black") {
1471 if (m_effectList.hasEffect(QString(), "fadein") == -1) {
1472 if (e.attribute("name") == "out") fade += e.attribute("value").toInt();
1473 else if (e.attribute("name") == "in") fade -= e.attribute("value").toInt();
1475 QDomElement fadein = m_effectList.getEffectByTag(QString(), "fadein");
1476 if (fadein.attribute("name") == "out") fade += fadein.attribute("value").toInt();
1477 else if (fadein.attribute("name") == "in") fade -= fadein.attribute("value").toInt();
1479 } else if (effectId == "fadeout") {
1481 if (m_effectList.hasEffect(QString(), "fade_to_black") == -1) {
1482 if (e.attribute("name") == "out") fade -= e.attribute("value").toInt();
1483 else if (e.attribute("name") == "in") fade += e.attribute("value").toInt();
1485 QDomElement fadeout = m_effectList.getEffectByTag(QString(), "fade_to_black");
1486 if (fadeout.attribute("name") == "out") fade -= fadeout.attribute("value").toInt();
1487 else if (fadeout.attribute("name") == "in") fade += fadeout.attribute("value").toInt();
1489 } else if (effectId == "fade_to_black") {
1491 if (m_effectList.hasEffect(QString(), "fadeout") == -1) {
1492 if (e.attribute("name") == "out") fade -= e.attribute("value").toInt();
1493 else if (e.attribute("name") == "in") fade += e.attribute("value").toInt();
1495 QDomElement fadeout = m_effectList.getEffectByTag(QString(), "fadeout");
1496 if (fadeout.attribute("name") == "out") fade -= fadeout.attribute("value").toInt();
1497 else if (fadeout.attribute("name") == "in") fade += fadeout.attribute("value").toInt();
1502 if (e.attribute("factor").startsWith('%')) {
1503 fact = ProfilesDialog::getStringEval(projectScene()->profile(), e.attribute("factor"));
1504 } else fact = e.attribute("factor", "1").toDouble();
1505 parameters.addParam(e.attribute("name"), QString::number(e.attribute("value").toDouble() / fact));
1509 m_effectNames = m_effectList.effectNames().join(" / ");
1510 if (fade > 0) m_startFade = fade;
1511 else if (fade < 0) m_endFade = -fade;
1513 if (m_selectedEffect == -1) {
1514 setSelectedEffect(0);
1515 } else if (m_selectedEffect == ix - 1) setSelectedEffect(m_selectedEffect);
1516 if (needRepaint) update(boundingRect());
1520 else { /*if (!needRepaint) */
1521 QRectF r = boundingRect();
1528 EffectsParameterList ClipItem::getEffectArgs(const QDomElement effect)
1530 EffectsParameterList parameters;
1531 parameters.addParam("tag", effect.attribute("tag"));
1532 if (effect.hasAttribute("region")) parameters.addParam("region", effect.attribute("region"));
1533 parameters.addParam("kdenlive_ix", effect.attribute("kdenlive_ix"));
1534 parameters.addParam("id", effect.attribute("id"));
1535 if (effect.hasAttribute("src")) parameters.addParam("src", effect.attribute("src"));
1536 if (effect.hasAttribute("disable")) parameters.addParam("disable", effect.attribute("disable"));
1537 if (effect.hasAttribute("in")) parameters.addParam("in", effect.attribute("in"));
1538 if (effect.hasAttribute("out")) parameters.addParam("out", effect.attribute("out"));
1540 QDomNodeList params = effect.elementsByTagName("parameter");
1541 for (int i = 0; i < params.count(); i++) {
1542 QDomElement e = params.item(i).toElement();
1543 //kDebug() << "/ / / /SENDING EFFECT PARAM: " << e.attribute("type") << ", NAME_ " << e.attribute("tag");
1544 if (e.attribute("type") == "simplekeyframe") {
1546 QStringList values = e.attribute("keyframes").split(";", QString::SkipEmptyParts);
1547 double factor = e.attribute("factor", "1").toDouble();
1548 for (int j = 0; j < values.count(); j++) {
1549 QString pos = values.at(j).section(":", 0, 0);
1550 double val = values.at(j).section(":", 1, 1).toDouble() / factor;
1551 values[j] = pos + "=" + QString::number(val);
1553 // kDebug() << "/ / / /SENDING KEYFR:" << values;
1554 parameters.addParam(e.attribute("name"), values.join(";"));
1555 /*parameters.addParam(e.attribute("name"), e.attribute("keyframes").replace(":", "="));
1556 parameters.addParam("max", e.attribute("max"));
1557 parameters.addParam("min", e.attribute("min"));
1558 parameters.addParam("factor", e.attribute("factor", "1"));*/
1559 } else if (e.attribute("type") == "keyframe") {
1560 kDebug() << "/ / / /SENDING KEYFR EFFECT TYPE";
1561 parameters.addParam("keyframes", e.attribute("keyframes"));
1562 parameters.addParam("max", e.attribute("max"));
1563 parameters.addParam("min", e.attribute("min"));
1564 parameters.addParam("factor", e.attribute("factor", "1"));
1565 parameters.addParam("starttag", e.attribute("starttag", "start"));
1566 parameters.addParam("endtag", e.attribute("endtag", "end"));
1567 } else if (e.attribute("namedesc").contains(';')) {
1568 QString format = e.attribute("format");
1569 QStringList separators = format.split("%d", QString::SkipEmptyParts);
1570 QStringList values = e.attribute("value").split(QRegExp("[,:;x]"));
1572 QTextStream txtNeu(&neu);
1573 if (values.size() > 0)
1574 txtNeu << (int)values[0].toDouble();
1575 for (int i = 0; i < separators.size() && i + 1 < values.size(); i++) {
1576 txtNeu << separators[i];
1577 txtNeu << (int)(values[i+1].toDouble());
1579 parameters.addParam("start", neu);
1581 if (e.attribute("factor", "1") != "1") {
1583 if (e.attribute("factor").startsWith('%')) {
1584 fact = ProfilesDialog::getStringEval(projectScene()->profile(), e.attribute("factor"));
1585 } else fact = e.attribute("factor", "1").toDouble();
1586 parameters.addParam(e.attribute("name"), QString::number(e.attribute("value").toDouble() / fact));
1588 parameters.addParam(e.attribute("name"), e.attribute("value"));
1595 void ClipItem::deleteEffect(QString index)
1597 bool needRepaint = false;
1600 for (int i = 0; i < m_effectList.count(); ++i) {
1601 ix = m_effectList.at(i).attribute("kdenlive_ix");
1603 QString effectId = m_effectList.at(i).attribute("id");
1604 if ((effectId == "fadein" && hasEffect(QString(), "fade_from_black") == -1) ||
1605 (effectId == "fade_from_black" && hasEffect(QString(), "fadein") == -1)) {
1608 } else if ((effectId == "fadeout" && hasEffect(QString(), "fade_to_black") == -1) ||
1609 (effectId == "fade_to_black" && hasEffect(QString(), "fadeout") == -1)) {
1612 } else if (EffectsList::hasKeyFrames(m_effectList.at(i))) needRepaint = true;
1613 m_effectList.removeAt(i);
1615 } else if (ix.toInt() > index.toInt()) {
1616 m_effectList.item(i).setAttribute("kdenlive_ix", ix.toInt() - 1);
1619 m_effectNames = m_effectList.effectNames().join(" / ");
1621 if (m_effectList.isEmpty() || m_selectedEffect + 1 == index.toInt()) {
1622 // Current effect was removed
1623 if (index.toInt() > m_effectList.count() - 1) {
1624 setSelectedEffect(m_effectList.count() - 1);
1625 } else setSelectedEffect(index.toInt());
1627 if (needRepaint) update(boundingRect());
1629 QRectF r = boundingRect();
1633 //if (!m_effectList.isEmpty()) flashClip();
1636 double ClipItem::speed() const
1641 int ClipItem::strobe() const
1646 void ClipItem::setSpeed(const double speed, const int strobe)
1649 if (m_speed <= 0 && m_speed > -1)
1652 if (m_speed == 1.0) m_clipName = baseClip()->name();
1653 else m_clipName = baseClip()->name() + " - " + QString::number(speed * 100, 'f', 0) + '%';
1654 m_info.cropStart = GenTime((int)(m_speedIndependantInfo.cropStart.frames(m_fps) / qAbs(m_speed) + 0.5), m_fps);
1655 m_info.cropDuration = GenTime((int)(m_speedIndependantInfo.cropDuration.frames(m_fps) / qAbs(m_speed) + 0.5), m_fps);
1659 GenTime ClipItem::maxDuration() const
1661 return GenTime((int)(m_maxDuration.frames(m_fps) / qAbs(m_speed) + 0.5), m_fps);
1664 GenTime ClipItem::speedIndependantCropStart() const
1666 return m_speedIndependantInfo.cropStart;
1669 GenTime ClipItem::speedIndependantCropDuration() const
1671 return m_speedIndependantInfo.cropDuration;
1675 const ItemInfo ClipItem::speedIndependantInfo() const
1677 return m_speedIndependantInfo;
1681 void ClipItem::dropEvent(QGraphicsSceneDragDropEvent * event)
1683 const QString effects = QString(event->mimeData()->data("kdenlive/effectslist"));
1685 doc.setContent(effects, true);
1686 const QDomElement e = doc.documentElement();
1687 if (scene() && !scene()->views().isEmpty()) {
1689 CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
1690 if (view) view->slotAddEffect(e, m_info.startPos, track());
1695 void ClipItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
1697 if (isItemLocked()) event->setAccepted(false);
1698 else event->setAccepted(event->mimeData()->hasFormat("kdenlive/effectslist"));
1701 void ClipItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
1706 void ClipItem::addTransition(Transition* t)
1708 m_transitionsList.append(t);
1709 //CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
1711 QDomElement e = doc.documentElement();
1712 //if (view) view->slotAddTransition(this, t->toXML() , t->startPos(), track());
1715 void ClipItem::setVideoOnly(bool force)
1717 m_videoOnly = force;
1720 void ClipItem::setAudioOnly(bool force)
1722 m_audioOnly = force;
1723 if (m_audioOnly) m_baseColor = QColor(141, 215, 166);
1725 if (m_clipType == COLOR) {
1726 QString colour = m_clip->getProperty("colour");
1727 colour = colour.replace(0, 2, "#");
1728 m_baseColor = QColor(colour.left(7));
1729 } else if (m_clipType == AUDIO) m_baseColor = QColor(141, 215, 166);
1730 else m_baseColor = QColor(141, 166, 215);
1732 m_audioThumbCachePic.clear();
1735 bool ClipItem::isAudioOnly() const
1740 bool ClipItem::isVideoOnly() const
1745 void ClipItem::insertKeyframe(QDomElement effect, int pos, int val)
1747 if (effect.attribute("disable") == "1") return;
1748 effect.setAttribute("active_keyframe", pos);
1749 m_editedKeyframe = pos;
1750 QDomNodeList params = effect.elementsByTagName("parameter");
1751 for (int i = 0; i < params.count(); i++) {
1752 QDomElement e = params.item(i).toElement();
1753 QString kfr = e.attribute("keyframes");
1754 const QStringList keyframes = kfr.split(';', QString::SkipEmptyParts);
1757 foreach(const QString &str, keyframes) {
1758 int kpos = str.section(':', 0, 0).toInt();
1759 double newval = str.section(':', 1, 1).toDouble();
1762 } else if (!added) {
1763 if (i == 0) newkfr.append(QString::number(pos) + ":" + QString::number(val));
1764 else newkfr.append(QString::number(pos) + ":" + QString::number(newval));
1765 if (kpos > pos) newkfr.append(str);
1767 } else newkfr.append(str);
1769 if (!added) newkfr.append(QString::number(pos) + ":" + QString::number(val));
1770 e.setAttribute("keyframes", newkfr.join(";"));
1774 void ClipItem::movedKeyframe(QDomElement effect, int oldpos, int newpos, double value)
1776 if (effect.attribute("disable") == "1") return;
1777 effect.setAttribute("active_keyframe", newpos);
1778 QDomNodeList params = effect.elementsByTagName("parameter");
1779 int start = cropStart().frames(m_fps);
1780 int end = (cropStart() + cropDuration()).frames(m_fps) - 1;
1781 for (int i = 0; i < params.count(); i++) {
1782 QDomElement e = params.item(i).toElement();
1783 QString kfr = e.attribute("keyframes");
1784 const QStringList keyframes = kfr.split(';', QString::SkipEmptyParts);
1786 foreach(const QString &str, keyframes) {
1787 if (str.section(':', 0, 0).toInt() != oldpos) {
1789 } else if (newpos != -1) {
1790 newpos = qMax(newpos, start);
1791 newpos = qMin(newpos, end);
1792 if (i == 0) newkfr.append(QString::number(newpos) + ":" + QString::number(value));
1793 else newkfr.append(QString::number(newpos) + ":" + str.section(':', 1, 1));
1796 e.setAttribute("keyframes", newkfr.join(";"));
1799 updateKeyframes(effect);
1803 void ClipItem::updateKeyframes(QDomElement effect)
1805 m_keyframes.clear();
1807 QDomNodeList params = effect.elementsByTagName("parameter");
1808 QDomElement e = params.item(0).toElement();
1809 const QStringList keyframes = e.attribute("keyframes").split(';', QString::SkipEmptyParts);
1810 foreach(const QString &str, keyframes) {
1811 int pos = str.section(':', 0, 0).toInt();
1812 double val = str.section(':', 1, 1).toDouble();
1813 m_keyframes[pos] = val;
1815 if (!m_keyframes.contains(m_selectedKeyframe)) m_selectedKeyframe = -1;
1818 void ClipItem::doGetIntraThumbs(QPainter *painter, const QPointF startPos, int offset, int start, int end)
1820 if (!m_clip->thumbProducer() || clipType() == COLOR) return;
1821 if (scene() && scene()->views().isEmpty()) return;
1822 CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
1823 if (view == NULL) return;
1824 const int theight = KdenliveSettings::trackheight();
1825 const int twidth = FRAME_SIZE;
1827 if (clipType() == IMAGE || clipType() == TEXT) {
1828 for (int i = start; i <= end; i++)
1829 painter->drawPixmap(startPos + QPointF(twidth *(i - offset), 0), m_startPix);
1832 for (int i = start; i <= end; i++) {
1833 if (!view->pixmapCache->find(m_clip->fileURL().path() + "%" + QString::number(i), p)) {
1834 p = m_clip->thumbProducer()->extractImage(i, twidth, theight);
1835 view->pixmapCache->insert(m_clip->fileURL().path() + "%" + QString::number(i), p);
1837 painter->drawPixmap(startPos + QPointF(twidth *(i - offset), 0), p);
1842 #include "clipitem.moc"