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)
66 m_speedIndependantInfo = m_info;
67 m_speedIndependantInfo.cropStart = GenTime((int)(m_info.cropStart.frames(m_fps) * qAbs(m_speed)), m_fps);
68 m_speedIndependantInfo.cropDuration = GenTime((int)(m_info.cropDuration.frames(m_fps) * qAbs(m_speed)), m_fps);
70 m_videoPix = KIcon("kdenlive-show-video").pixmap(QSize(16, 16));
71 m_audioPix = KIcon("kdenlive-show-audio").pixmap(QSize(16, 16));
74 m_clipName = m_clip->name();
76 m_clipName = m_clip->name() + " - " + QString::number(m_speed * 100, 'f', 0) + '%';
78 m_producer = m_clip->getId();
79 m_clipType = m_clip->clipType();
80 //m_cropStart = info.cropStart;
81 m_maxDuration = m_clip->maxDuration();
83 m_audioThumbReady = m_clip->audioThumbCreated();
84 //setAcceptsHoverEvents(true);
85 connect(this , SIGNAL(prepareAudioThumb(double, int, int, int)) , this, SLOT(slotPrepareAudioThumb(double, int, int, int)));
87 if (m_clipType == VIDEO || m_clipType == AV || m_clipType == SLIDESHOW || m_clipType == PLAYLIST) {
88 m_baseColor = QColor(141, 166, 215);
89 if (!m_clip->isPlaceHolder()) {
91 m_startThumbTimer.setSingleShot(true);
92 connect(&m_startThumbTimer, SIGNAL(timeout()), this, SLOT(slotGetStartThumb()));
93 m_endThumbTimer.setSingleShot(true);
94 connect(&m_endThumbTimer, SIGNAL(timeout()), this, SLOT(slotGetEndThumb()));
96 connect(this, SIGNAL(getThumb(int, int)), m_clip->thumbProducer(), SLOT(extractImage(int, int)));
98 connect(m_clip->thumbProducer(), SIGNAL(thumbReady(int, QImage)), this, SLOT(slotThumbReady(int, QImage)));
99 connect(m_clip, SIGNAL(gotAudioData()), this, SLOT(slotGotAudioData()));
100 if (generateThumbs) QTimer::singleShot(200, this, SLOT(slotFetchThumbs()));
103 } else if (m_clipType == COLOR) {
104 QString colour = m_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)), m_clip->thumbProducer(), SLOT(extractImage(int, int)));
111 connect(m_clip->thumbProducer(), SIGNAL(thumbReady(int, QImage)), this, SLOT(slotThumbReady(int, QImage)));
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(m_clip, SIGNAL(gotAudioData()), this, SLOT(slotGotAudioData()));
121 ClipItem::~ClipItem()
124 if (scene()) scene()->removeItem(this);
125 if (m_clipType == VIDEO || m_clipType == AV || m_clipType == SLIDESHOW || m_clipType == PLAYLIST) {
126 //disconnect(m_clip->thumbProducer(), SIGNAL(thumbReady(int, QImage)), this, SLOT(slotThumbReady(int, QImage)));
127 //disconnect(m_clip, SIGNAL(gotAudioData()), this, SLOT(slotGotAudioData()));
132 ClipItem *ClipItem::clone(ItemInfo info) const
134 ClipItem *duplicate = new ClipItem(m_clip, info, m_fps, m_speed, m_strobe);
135 if (m_clipType == IMAGE || m_clipType == TEXT) duplicate->slotSetStartThumb(m_startPix);
136 else if (m_clipType != COLOR) {
137 if (info.cropStart == m_info.cropStart) duplicate->slotSetStartThumb(m_startPix);
138 if (info.cropStart + (info.endPos - info.startPos) == m_info.cropStart + (m_info.endPos - m_info.startPos)) duplicate->slotSetEndThumb(m_endPix);
140 //kDebug() << "// CLoning clip: " << (info.cropStart + (info.endPos - info.startPos)).frames(m_fps) << ", CURRENT end: " << (cropStart() + duration()).frames(m_fps);
141 duplicate->setEffectList(m_effectList);
142 duplicate->setVideoOnly(m_videoOnly);
143 duplicate->setAudioOnly(m_audioOnly);
144 duplicate->setFades(fadeIn(), fadeOut());
145 //duplicate->setSpeed(m_speed);
149 void ClipItem::setEffectList(const EffectsList effectList)
151 m_effectList.clone(effectList);
152 m_effectNames = m_effectList.effectNames().join(" / ");
153 if (!m_effectList.isEmpty()) {
154 for (int i = 0; i < m_effectList.count(); i++) {
155 QString effectId = m_effectList.item(i).attribute("id");
156 // check if it is a fade effect
157 QDomNodeList params = m_effectList.item(i).elementsByTagName("parameter");
159 for (int j = 0; j < params.count(); j++) {
160 QDomElement e = params.item(j).toElement();
162 if (effectId == "fadein") {
163 if (m_effectList.hasEffect(QString(), "fade_from_black") == -1) {
164 if (e.attribute("name") == "out") fade += e.attribute("value").toInt();
165 else if (e.attribute("name") == "in") fade -= e.attribute("value").toInt();
167 QDomElement fadein = m_effectList.getEffectByTag(QString(), "fade_from_black");
168 if (fadein.attribute("name") == "out") fade += fadein.attribute("value").toInt();
169 else if (fadein.attribute("name") == "in") fade -= fadein.attribute("value").toInt();
171 } else if (effectId == "fade_from_black") {
172 if (m_effectList.hasEffect(QString(), "fadein") == -1) {
173 if (e.attribute("name") == "out") fade += e.attribute("value").toInt();
174 else if (e.attribute("name") == "in") fade -= e.attribute("value").toInt();
176 QDomElement fadein = m_effectList.getEffectByTag(QString(), "fadein");
177 if (fadein.attribute("name") == "out") fade += fadein.attribute("value").toInt();
178 else if (fadein.attribute("name") == "in") fade -= fadein.attribute("value").toInt();
180 } else if (effectId == "fadeout") {
181 if (m_effectList.hasEffect(QString(), "fade_to_black") == -1) {
182 if (e.attribute("name") == "out") fade -= e.attribute("value").toInt();
183 else if (e.attribute("name") == "in") fade += e.attribute("value").toInt();
185 QDomElement fadeout = m_effectList.getEffectByTag(QString(), "fade_to_black");
186 if (fadeout.attribute("name") == "out") fade -= fadeout.attribute("value").toInt();
187 else if (fadeout.attribute("name") == "in") fade += fadeout.attribute("value").toInt();
189 } else if (effectId == "fade_to_black") {
190 if (m_effectList.hasEffect(QString(), "fadeout") == -1) {
191 if (e.attribute("name") == "out") fade -= e.attribute("value").toInt();
192 else if (e.attribute("name") == "in") fade += e.attribute("value").toInt();
194 QDomElement fadeout = m_effectList.getEffectByTag(QString(), "fadeout");
195 if (fadeout.attribute("name") == "out") fade -= fadeout.attribute("value").toInt();
196 else if (fadeout.attribute("name") == "in") fade += fadeout.attribute("value").toInt();
206 setSelectedEffect(0);
210 const EffectsList ClipItem::effectList() const
215 int ClipItem::selectedEffectIndex() const
217 return m_selectedEffect;
220 void ClipItem::initEffect(QDomElement effect, int diff)
222 // the kdenlive_ix int is used to identify an effect in mlt's playlist, should
224 if (effect.attribute("kdenlive_ix").toInt() == 0)
225 effect.setAttribute("kdenlive_ix", QString::number(effectsCounter()));
227 if (effect.attribute("id") == "freeze" && diff > 0) {
228 EffectsList::setParameter(effect, "frame", QString::number(diff));
231 // Init parameter value & keyframes if required
232 QDomNodeList params = effect.elementsByTagName("parameter");
233 for (int i = 0; i < params.count(); i++) {
234 QDomElement e = params.item(i).toElement();
235 kDebug() << "// init eff: " << e.attribute("name");
237 // Check if this effect has a variable parameter
238 if (e.attribute("default").startsWith('%')) {
239 double evaluatedValue = ProfilesDialog::getStringEval(projectScene()->profile(), e.attribute("default"));
240 e.setAttribute("default", evaluatedValue);
241 if (e.hasAttribute("value") && e.attribute("value").startsWith('%')) {
242 e.setAttribute("value", evaluatedValue);
246 if (!e.isNull() && (e.attribute("type") == "keyframe" || e.attribute("type") == "simplekeyframe")) {
247 QString def = e.attribute("default");
248 // Effect has a keyframe type parameter, we need to set the values
249 if (e.attribute("keyframes").isEmpty()) {
250 e.setAttribute("keyframes", QString::number(cropStart().frames(m_fps)) + ':' + def);
251 kDebug() << "///// EFFECT KEYFRAMES INITED: " << e.attribute("keyframes");
256 if (effect.attribute("tag") == "volume" || effect.attribute("tag") == "brightness") {
257 if (effect.attribute("id") == "fadeout" || effect.attribute("id") == "fade_to_black") {
258 int end = (cropDuration() + cropStart()).frames(m_fps) - 1;
260 if (effect.attribute("id") == "fadeout") {
261 if (m_effectList.hasEffect(QString(), "fade_to_black") == -1) {
262 int effectDuration = EffectsList::parameter(effect, "in").toInt();
263 if (effectDuration > cropDuration().frames(m_fps)) {
264 effectDuration = cropDuration().frames(m_fps) / 2;
266 start -= effectDuration;
268 QDomElement fadeout = m_effectList.getEffectByTag(QString(), "fade_to_black");
269 start -= EffectsList::parameter(fadeout, "out").toInt() - EffectsList::parameter(fadeout, "in").toInt();
271 } else if (effect.attribute("id") == "fade_to_black") {
272 if (m_effectList.hasEffect(QString(), "fadeout") == -1) {
273 int effectDuration = EffectsList::parameter(effect, "in").toInt();
274 if (effectDuration > cropDuration().frames(m_fps)) {
275 effectDuration = cropDuration().frames(m_fps) / 2;
277 start -= effectDuration;
279 QDomElement fadeout = m_effectList.getEffectByTag(QString(), "fadeout");
280 start -= EffectsList::parameter(fadeout, "out").toInt() - EffectsList::parameter(fadeout, "in").toInt();
283 EffectsList::setParameter(effect, "in", QString::number(start));
284 EffectsList::setParameter(effect, "out", QString::number(end));
285 } else if (effect.attribute("id") == "fadein" || effect.attribute("id") == "fade_from_black") {
286 int start = cropStart().frames(m_fps);
288 if (effect.attribute("id") == "fadein") {
289 if (m_effectList.hasEffect(QString(), "fade_from_black") == -1) {
290 int effectDuration = EffectsList::parameter(effect, "out").toInt();
291 if (effectDuration > cropDuration().frames(m_fps)) {
292 effectDuration = cropDuration().frames(m_fps) / 2;
294 end += effectDuration;
296 end += EffectsList::parameter(m_effectList.getEffectByTag(QString(), "fade_from_black"), "out").toInt();
297 } else if (effect.attribute("id") == "fade_from_black") {
298 if (m_effectList.hasEffect(QString(), "fadein") == -1) {
299 int effectDuration = EffectsList::parameter(effect, "out").toInt();
300 if (effectDuration > cropDuration().frames(m_fps)) {
301 effectDuration = cropDuration().frames(m_fps) / 2;
303 end += effectDuration;
305 end += EffectsList::parameter(m_effectList.getEffectByTag(QString(), "fadein"), "out").toInt();
307 EffectsList::setParameter(effect, "in", QString::number(start));
308 EffectsList::setParameter(effect, "out", QString::number(end));
313 bool ClipItem::checkKeyFrames()
315 bool clipEffectsModified = false;
316 // go through all effects this clip has
317 for (int ix = 0; ix < m_effectList.count(); ++ix) {
318 QStringList keyframeParams = keyframes(ix);
319 QStringList newKeyFrameParams;
320 bool effModified = false;
322 // go through all params which have keyframes
323 foreach(const QString &kfr, keyframeParams) {
324 const QStringList keyframes = kfr.split(';', QString::SkipEmptyParts);
325 QStringList newKeyFrames;
326 bool cutKeyFrame = false;
327 bool modified = false;
329 double lastValue = -1;
330 int start = cropStart().frames(m_fps);
331 int end = (cropStart() + cropDuration()).frames(m_fps);
333 // go through all keyframes for one param
334 foreach(const QString &str, keyframes) {
335 int pos = str.section(':', 0, 0).toInt();
336 double val = str.section(':', 1, 1).toDouble();
337 if (pos - start < 0) {
338 // a keyframe is defined before the start of the clip
340 } else if (cutKeyFrame) {
341 // create new keyframe at clip start, calculate interpolated value
343 int diff = pos - lastPos;
344 double ratio = (double)(start - lastPos) / diff;
345 double newValue = lastValue + (val - lastValue) * ratio;
346 newKeyFrames.append(QString::number(start) + ':' + QString::number(newValue));
353 // create new keyframe at clip end, calculate interpolated value
354 int diff = pos - lastPos;
356 double ratio = (double)(end - lastPos) / diff;
357 double newValue = lastValue + (val - lastValue) * ratio;
358 newKeyFrames.append(QString::number(end) + ':' + QString::number(newValue));
363 newKeyFrames.append(QString::number(pos) + ':' + QString::number(val));
370 newKeyFrameParams.append(newKeyFrames.join(";"));
377 setKeyframes(ix, newKeyFrameParams);
378 clipEffectsModified = true;
381 return clipEffectsModified;
384 void ClipItem::setKeyframes(const int ix, const QStringList keyframes)
386 QDomElement effect = getEffectAt(ix);
387 if (effect.attribute("disable") == "1") return;
388 QDomNodeList params = effect.elementsByTagName("parameter");
389 int keyframeParams = 0;
390 for (int i = 0; i < params.count(); i++) {
391 QDomElement e = params.item(i).toElement();
392 if (!e.isNull() && (e.attribute("type") == "keyframe" || e.attribute("type") == "simplekeyframe") && e.attribute("intimeline") == "1") {
393 e.setAttribute("keyframes", keyframes.at(keyframeParams));
394 if (ix == m_selectedEffect && keyframeParams == 0) {
397 double max = e.attribute("max").toDouble();
398 double min = e.attribute("min").toDouble();
399 m_keyframeFactor = 100.0 / (max - min);
400 m_keyframeOffset = min;
401 m_keyframeDefault = e.attribute("default").toDouble();
402 m_selectedKeyframe = 0;
404 const QStringList keyframes = e.attribute("keyframes").split(';', QString::SkipEmptyParts);
405 foreach(const QString &str, keyframes) {
406 int pos = str.section(':', 0, 0).toInt();
407 double val = str.section(':', 1, 1).toDouble();
408 m_keyframes[pos] = val;
410 if (m_keyframes.find(m_editedKeyframe) == m_keyframes.end()) m_editedKeyframe = -1;
411 if (m_keyframes.find(m_editedKeyframe) == m_keyframes.end()) m_editedKeyframe = -1;
420 void ClipItem::setSelectedEffect(const int ix)
422 m_selectedEffect = ix;
423 QDomElement effect = effectAt(m_selectedEffect);
424 if (effect.isNull() == false) {
425 QDomNodeList params = effect.elementsByTagName("parameter");
426 if (effect.attribute("disable") != "1")
427 for (int i = 0; i < params.count(); i++) {
428 QDomElement e = params.item(i).toElement();
429 if (!e.isNull() && (e.attribute("type") == "keyframe" || e.attribute("type") == "simplekeyframe") && e.attribute("intimeline") == "1") {
432 double max = e.attribute("max").toDouble();
433 double min = e.attribute("min").toDouble();
434 m_keyframeFactor = 100.0 / (max - min);
435 m_keyframeOffset = min;
436 m_keyframeDefault = e.attribute("default").toDouble();
437 m_selectedKeyframe = 0;
440 const QStringList keyframes = e.attribute("keyframes").split(';', QString::SkipEmptyParts);
441 foreach(const QString &str, keyframes) {
442 int pos = str.section(':', 0, 0).toInt();
443 double val = str.section(':', 1, 1).toDouble();
444 m_keyframes[pos] = val;
446 if (m_keyframes.find(m_editedKeyframe) == m_keyframes.end()) m_editedKeyframe = -1;
452 if (!m_keyframes.isEmpty()) {
458 QStringList ClipItem::keyframes(const int index)
461 QDomElement effect = effectAt(index);
462 QDomNodeList params = effect.elementsByTagName("parameter");
464 for (int i = 0; i < params.count(); i++) {
465 QDomElement e = params.item(i).toElement();
466 if (!e.isNull() && (e.attribute("type") == "keyframe" || e.attribute("type") == "simplekeyframe"))
467 result.append(e.attribute("keyframes"));
472 void ClipItem::updateKeyframeEffect()
474 // regenerate xml parameter from the clip keyframes
475 QDomElement effect = getEffectAt(m_selectedEffect);
476 if (effect.attribute("disable") == "1") return;
477 QDomNodeList params = effect.elementsByTagName("parameter");
478 QDomElement e = params.item(m_visibleParam).toElement();
482 if (m_keyframes.count() > 0) {
483 QMap<int, int>::const_iterator i = m_keyframes.constBegin();
484 while (i != m_keyframes.constEnd()) {
485 keyframes.append(QString::number(i.key()) + ':' + QString::number(i.value()) + ';');
489 // Effect has a keyframe type parameter, we need to set the values
490 e.setAttribute("keyframes", keyframes);
494 QDomElement ClipItem::selectedEffect()
496 if (m_selectedEffect == -1 || m_effectList.isEmpty()) return QDomElement();
497 return effectAt(m_selectedEffect);
500 void ClipItem::resetThumbs(bool clearExistingThumbs)
502 if (clearExistingThumbs) {
503 m_startPix = QPixmap();
504 m_endPix = QPixmap();
505 m_audioThumbCachePic.clear();
511 void ClipItem::refreshClip(bool checkDuration, bool forceResetThumbs)
513 if (checkDuration && (m_maxDuration != m_clip->maxDuration())) {
514 m_maxDuration = m_clip->maxDuration();
515 if (m_clipType != IMAGE && m_clipType != TEXT && m_clipType != COLOR) {
516 if (m_maxDuration != GenTime() && m_info.cropStart + m_info.cropDuration > m_maxDuration) {
517 // Clip duration changed, make sure to stay in correct range
518 if (m_info.cropStart > m_maxDuration) {
519 m_info.cropStart = GenTime();
520 m_info.cropDuration = qMin(m_info.cropDuration, m_maxDuration);
522 m_info.cropDuration = m_maxDuration;
524 updateRectGeometry();
528 if (m_clipType == COLOR) {
529 QString colour = m_clip->getProperty("colour");
530 colour = colour.replace(0, 2, "#");
531 m_baseColor = QColor(colour.left(7));
533 } else resetThumbs(forceResetThumbs);
536 void ClipItem::slotFetchThumbs()
538 if (scene() == NULL || m_clipType == AUDIO || m_clipType == COLOR) return;
539 if (m_clipType == IMAGE) {
540 if (m_startPix.isNull()) {
541 m_startPix = KThumb::getImage(KUrl(m_clip->getProperty("resource")), (int)(KdenliveSettings::trackheight() * KdenliveSettings::project_display_ratio()), KdenliveSettings::trackheight());
547 if (m_clipType == TEXT) {
548 if (m_startPix.isNull()) slotGetStartThumb();
552 if (m_endPix.isNull() && m_startPix.isNull()) {
553 m_startThumbRequested = true;
554 m_endThumbRequested = true;
555 emit getThumb((int)m_speedIndependantInfo.cropStart.frames(m_fps), (int)(m_speedIndependantInfo.cropStart + m_speedIndependantInfo.cropDuration).frames(m_fps) - 1);
557 if (m_endPix.isNull()) {
560 if (m_startPix.isNull()) {
566 void ClipItem::slotGetStartThumb()
568 m_startThumbRequested = true;
569 emit getThumb((int)m_speedIndependantInfo.cropStart.frames(m_fps), -1);
572 void ClipItem::slotGetEndThumb()
574 m_endThumbRequested = true;
575 emit getThumb(-1, (int)(m_speedIndependantInfo.cropStart + m_speedIndependantInfo.cropDuration).frames(m_fps) - 1);
579 void ClipItem::slotSetStartThumb(QImage img)
581 if (!img.isNull() && img.format() == QImage::Format_ARGB32) {
582 QPixmap pix = QPixmap::fromImage(img);
584 QRectF r = sceneBoundingRect();
585 r.setRight(pix.width() + 2);
590 void ClipItem::slotSetEndThumb(QImage img)
592 if (!img.isNull() && img.format() == QImage::Format_ARGB32) {
593 QPixmap pix = QPixmap::fromImage(img);
595 QRectF r = sceneBoundingRect();
596 r.setLeft(r.right() - pix.width() - 2);
601 void ClipItem::slotThumbReady(int frame, QImage img)
603 if (scene() == NULL) return;
604 QRectF r = boundingRect();
605 QPixmap pix = QPixmap::fromImage(img);
606 double width = pix.width() / projectScene()->scale().x();
607 if (m_startThumbRequested && frame == m_speedIndependantInfo.cropStart.frames(m_fps)) {
609 m_startThumbRequested = false;
610 update(r.left(), r.top(), width, pix.height());
611 if (m_clipType == IMAGE || m_clipType == TEXT) {
612 update(r.right() - width, r.top(), width, pix.height());
614 } else if (m_endThumbRequested && frame == (m_speedIndependantInfo.cropStart + m_speedIndependantInfo.cropDuration).frames(m_fps) - 1) {
616 m_endThumbRequested = false;
617 update(r.right() - width, r.top(), width, pix.height());
621 void ClipItem::slotSetStartThumb(const QPixmap pix)
626 void ClipItem::slotSetEndThumb(const QPixmap pix)
631 QPixmap ClipItem::startThumb() const
636 QPixmap ClipItem::endThumb() const
641 void ClipItem::slotGotAudioData()
643 m_audioThumbReady = true;
644 if (m_clipType == AV && !isAudioOnly()) {
645 QRectF r = boundingRect();
646 r.setTop(r.top() + r.height() / 2 - 1);
651 int ClipItem::type() const
656 DocClipBase *ClipItem::baseClip() const
661 QDomElement ClipItem::xml() const
666 QDomElement ClipItem::itemXml() const
668 QDomElement xml = m_clip->toXML();
669 if (m_speed != 1.0) xml.setAttribute("speed", m_speed);
670 if (m_strobe > 1) xml.setAttribute("strobe", m_strobe);
671 if (m_audioOnly) xml.setAttribute("audio_only", 1);
672 else if (m_videoOnly) xml.setAttribute("video_only", 1);
676 int ClipItem::clipType() const
681 QString ClipItem::clipName() const
686 void ClipItem::setClipName(const QString &name)
691 const QString ClipItem::clipProducer() const
696 void ClipItem::flashClip()
698 if (m_timeLine == 0) {
699 m_timeLine = new QTimeLine(750, this);
700 m_timeLine->setUpdateInterval(80);
701 m_timeLine->setCurveShape(QTimeLine::EaseInOutCurve);
702 m_timeLine->setFrameRange(0, 100);
703 connect(m_timeLine, SIGNAL(valueChanged(qreal)), this, SLOT(animate(qreal)));
705 //m_timeLine->start();
708 void ClipItem::animate(qreal /*value*/)
710 QRectF r = boundingRect();
716 void ClipItem::paint(QPainter *painter,
717 const QStyleOptionGraphicsItem *option,
721 if (parentItem()) paintColor = QColor(255, 248, 149);
722 else paintColor = m_baseColor;
723 if (isSelected() || (parentItem() && parentItem()->isSelected())) paintColor = paintColor.darker();
725 painter->setMatrixEnabled(false);
726 const QRectF mapped = painter->matrix().mapRect(rect()).adjusted(0.5, 0, 0.5, 0);
727 const QRectF exposed = option->exposedRect;
728 painter->setClipRect(mapped);
729 painter->fillRect(mapped, paintColor);
732 if (KdenliveSettings::videothumbnails() && !isAudioOnly()) {
733 QPen pen = painter->pen();
734 pen.setColor(QColor(255, 255, 255, 150));
735 painter->setPen(pen);
736 if ((m_clipType == IMAGE || m_clipType == TEXT) && !m_startPix.isNull()) {
737 const QPointF top = mapped.topRight() - QPointF(m_startPix.width() - 1, 0);
738 painter->drawPixmap(top, m_startPix);
739 QLineF l2(top.x(), mapped.top(), top.x(), mapped.bottom());
740 painter->drawLine(l2);
741 } else if (!m_endPix.isNull()) {
742 const QPointF top = mapped.topRight() - QPointF(m_endPix.width() - 1, 0);
743 painter->drawPixmap(top, m_endPix);
744 QLineF l2(top.x(), mapped.top(), top.x(), mapped.bottom());
745 painter->drawLine(l2);
747 if (!m_startPix.isNull()) {
748 painter->drawPixmap(mapped.topLeft(), m_startPix);
749 QLineF l2(mapped.left() + m_startPix.width(), mapped.top(), mapped.left() + m_startPix.width(), mapped.bottom());
750 painter->drawLine(l2);
752 if (painter->matrix().m11() == FRAME_SIZE) {
753 int offset = (m_info.startPos - m_info.cropStart).frames(m_fps);
754 int left = qMax((int) m_info.startPos.frames(m_fps) + 1, (int) mapToScene(exposed.left(), 0).x());
755 int right = qMin((int)(m_info.startPos + m_info.cropDuration).frames(m_fps) - 1, (int) mapToScene(exposed.right(), 0).x());
756 doGetIntraThumbs(painter, mapped.topLeft(), m_info.cropStart.frames(m_fps), left - offset, right - offset);
758 painter->setPen(Qt::black);
761 // draw audio thumbnails
762 if (KdenliveSettings::audiothumbnails() && m_speed == 1.0 && !isVideoOnly() && ((m_clipType == AV && (exposed.bottom() > (rect().height() / 2) || isAudioOnly())) || m_clipType == AUDIO) && m_audioThumbReady) {
764 double startpixel = exposed.left();
767 double endpixel = exposed.right();
770 //kDebug()<<"/// REPAINTING AUDIO THMBS ZONE: "<<startpixel<<"x"<<endpixel;
772 /*QPainterPath path = m_clipType == AV ? roundRectPathLower : resultClipPath;*/
774 if (m_clipType == AV && !isAudioOnly()) {
776 mappedRect.setTop(mappedRect.bottom() - mapped.height() / 2);
777 } else mappedRect = mapped;
779 double scale = painter->matrix().m11();
781 if (isEnabled() && m_clip) channels = m_clip->getProperty("channels").toInt();
782 if (scale != m_framePixelWidth)
783 m_audioThumbCachePic.clear();
784 double cropLeft = m_info.cropStart.frames(m_fps);
785 const int clipStart = mappedRect.x();
786 const int mappedStartPixel = painter->matrix().map(QPointF(startpixel + cropLeft, 0)).x() - clipStart;
787 const int mappedEndPixel = painter->matrix().map(QPointF(endpixel + cropLeft, 0)).x() - clipStart;
788 cropLeft = cropLeft * scale;
792 emit prepareAudioThumb(scale, mappedStartPixel, mappedEndPixel, channels);
795 for (int startCache = mappedStartPixel - (mappedStartPixel) % 100; startCache < mappedEndPixel; startCache += 100) {
796 if (m_audioThumbCachePic.contains(startCache) && !m_audioThumbCachePic[startCache].isNull())
797 painter->drawPixmap(clipStart + startCache - cropLeft, mappedRect.y(), m_audioThumbCachePic[startCache]);
801 // Draw effects names
802 if (!m_effectNames.isEmpty() && mapped.width() > 40) {
803 QRectF txtBounding = painter->boundingRect(mapped, Qt::AlignLeft | Qt::AlignTop, m_effectNames);
805 if (m_timeLine && m_timeLine->state() == QTimeLine::Running) {
806 qreal value = m_timeLine->currentValue();
807 txtBounding.setWidth(txtBounding.width() * value);
808 bgColor.setRgb(50 + 200 *(1.0 - value), 50, 50, 100 + 50 * value);
809 } else bgColor.setRgb(50, 50, 90, 180);
811 QPainterPath rounded;
812 rounded.moveTo(txtBounding.bottomRight());
813 rounded.arcTo(txtBounding.right() - txtBounding.height() - 2, txtBounding.top() - txtBounding.height(), txtBounding.height() * 2, txtBounding.height() * 2, 270, 90);
814 rounded.lineTo(txtBounding.topLeft());
815 rounded.lineTo(txtBounding.bottomLeft());
816 painter->fillPath(rounded, bgColor);
817 painter->setPen(Qt::lightGray);
818 painter->drawText(txtBounding.adjusted(1, 0, 1, 0), Qt::AlignCenter, m_effectNames);
822 QColor frameColor(paintColor.darker());
823 if (isSelected() || (parentItem() && parentItem()->isSelected())) {
824 frameColor = QColor(Qt::red);
826 frameColor.setAlpha(160);
828 const QRectF txtBounding2 = painter->boundingRect(mapped, Qt::AlignHCenter | Qt::AlignVCenter, ' ' + m_clipName + ' ');
829 //painter->fillRect(txtBounding2, frameColor);
830 painter->setBrush(frameColor);
831 painter->setPen(Qt::NoPen);
832 painter->drawRoundedRect(txtBounding2, 3, 3);
833 painter->setBrush(QBrush(Qt::NoBrush));
835 //painter->setPen(QColor(0, 0, 0, 180));
836 //painter->drawText(txtBounding, Qt::AlignCenter, m_clipName);
838 painter->drawPixmap(txtBounding2.topLeft() - QPointF(17, -1), m_videoPix);
839 } else if (m_audioOnly) {
840 painter->drawPixmap(txtBounding2.topLeft() - QPointF(17, -1), m_audioPix);
842 painter->setPen(Qt::white);
843 painter->drawText(txtBounding2, Qt::AlignCenter, m_clipName);
847 if (isEnabled() && m_clip) {
848 QList < CommentedTime > markers = m_clip->commentedSnapMarkers();
849 QList < CommentedTime >::Iterator it = markers.begin();
852 QBrush markerBrush(QColor(120, 120, 0, 140));
853 QPen pen = painter->pen();
854 pen.setColor(QColor(255, 255, 255, 200));
855 pen.setStyle(Qt::DotLine);
857 for (; it != markers.end(); ++it) {
858 pos = GenTime((int)((*it).time().frames(m_fps) / qAbs(m_speed) + 0.5), m_fps) - cropStart();
859 if (pos > GenTime()) {
860 if (pos > cropDuration()) break;
861 QLineF l(rect().x() + pos.frames(m_fps), rect().y(), rect().x() + pos.frames(m_fps), rect().bottom());
862 QLineF l2 = painter->matrix().map(l);
863 painter->setPen(pen);
864 painter->drawLine(l2);
865 if (KdenliveSettings::showmarkers()) {
866 framepos = rect().x() + pos.frames(m_fps);
867 const QRectF r1(framepos + 0.04, 10, rect().width() - framepos - 2, rect().height() - 10);
868 const QRectF r2 = painter->matrix().mapRect(r1);
869 const QRectF txtBounding3 = painter->boundingRect(r2, Qt::AlignLeft | Qt::AlignTop, ' ' + (*it).comment() + ' ');
870 painter->setBrush(markerBrush);
871 painter->setPen(Qt::NoPen);
872 painter->drawRoundedRect(txtBounding3, 3, 3);
873 painter->setBrush(QBrush(Qt::NoBrush));
874 painter->setPen(Qt::white);
875 painter->drawText(txtBounding3, Qt::AlignCenter, (*it).comment());
877 //painter->fillRect(QRect(br.x() + framepos, br.y(), 10, br.height()), QBrush(QColor(0, 0, 0, 150)));
882 // draw start / end fades
885 fades = QBrush(QColor(200, 50, 50, 150));
886 } else fades = QBrush(QColor(200, 200, 200, 200));
888 if (m_startFade != 0) {
889 QPainterPath fadeInPath;
890 fadeInPath.moveTo(0, 0);
891 fadeInPath.lineTo(0, rect().height());
892 fadeInPath.lineTo(m_startFade, 0);
893 fadeInPath.closeSubpath();
894 QPainterPath f1 = painter->matrix().map(fadeInPath);
895 painter->fillPath(f1/*.intersected(resultClipPath)*/, fades);
896 /*if (isSelected()) {
897 QLineF l(m_startFade * scale, 0, 0, itemHeight);
898 painter->drawLine(l);
901 if (m_endFade != 0) {
902 QPainterPath fadeOutPath;
903 fadeOutPath.moveTo(rect().width(), 0);
904 fadeOutPath.lineTo(rect().width(), rect().height());
905 fadeOutPath.lineTo(rect().width() - m_endFade, 0);
906 fadeOutPath.closeSubpath();
907 QPainterPath f1 = painter->matrix().map(fadeOutPath);
908 painter->fillPath(f1/*.intersected(resultClipPath)*/, fades);
909 /*if (isSelected()) {
910 QLineF l(itemWidth - m_endFade * scale, 0, itemWidth, itemHeight);
911 painter->drawLine(l);
916 painter->setPen(QPen(Qt::lightGray));
917 // draw effect or transition keyframes
918 if (mapped.width() > 20) drawKeyFrames(painter, exposed);
920 //painter->setMatrixEnabled(true);
923 // expand clip rect to allow correct painting of clip border
924 QPen pen1(frameColor);
925 painter->setPen(pen1);
926 painter->setClipping(false);
927 painter->drawRect(painter->matrix().mapRect(rect()));
931 OPERATIONTYPE ClipItem::operationMode(QPointF pos)
933 if (isItemLocked()) return NONE;
934 const double scale = projectScene()->scale().x();
935 double maximumOffset = 6 / scale;
936 if (isSelected() || (parentItem() && parentItem()->isSelected())) {
937 int kf = mouseOverKeyFrames(pos, maximumOffset);
939 m_editedKeyframe = kf;
943 QRectF rect = sceneBoundingRect();
944 int addtransitionOffset = 10;
945 // Don't allow add transition if track height is very small
946 if (rect.height() < 30) addtransitionOffset = 0;
948 if (qAbs((int)(pos.x() - (rect.x() + m_startFade))) < maximumOffset && qAbs((int)(pos.y() - rect.y())) < 6) {
950 } else if (pos.x() - rect.x() < maximumOffset && (rect.bottom() - pos.y() > addtransitionOffset)) {
952 } else if (qAbs((int)(pos.x() - (rect.x() + rect.width() - m_endFade))) < maximumOffset && qAbs((int)(pos.y() - rect.y())) < 6) {
954 } else if ((rect.right() - pos.x() < maximumOffset) && (rect.bottom() - pos.y() > addtransitionOffset)) {
956 } else if ((pos.x() - rect.x() < 16 / scale) && (rect.bottom() - pos.y() <= addtransitionOffset)) {
957 return TRANSITIONSTART;
958 } else if ((rect.right() - pos.x() < 16 / scale) && (rect.bottom() - pos.y() <= addtransitionOffset)) {
959 return TRANSITIONEND;
965 QList <GenTime> ClipItem::snapMarkers() const
967 QList < GenTime > snaps;
968 QList < GenTime > markers = baseClip()->snapMarkers();
971 for (int i = 0; i < markers.size(); i++) {
972 pos = GenTime((int)(markers.at(i).frames(m_fps) / qAbs(m_speed) + 0.5), m_fps) - cropStart();
973 if (pos > GenTime()) {
974 if (pos > cropDuration()) break;
975 else snaps.append(pos + startPos());
981 QList <CommentedTime> ClipItem::commentedSnapMarkers() const
983 QList < CommentedTime > snaps;
984 QList < CommentedTime > markers = baseClip()->commentedSnapMarkers();
987 for (int i = 0; i < markers.size(); i++) {
988 pos = GenTime((int)(markers.at(i).time().frames(m_fps) / qAbs(m_speed) + 0.5), m_fps) - cropStart();
989 if (pos > GenTime()) {
990 if (pos > cropDuration()) break;
991 else snaps.append(CommentedTime(pos + startPos(), markers.at(i).comment()));
997 void ClipItem::slotPrepareAudioThumb(double pixelForOneFrame, int startpixel, int endpixel, int channels)
999 QRectF re = sceneBoundingRect();
1000 if (m_clipType == AV && !isAudioOnly()) re.setTop(re.y() + re.height() / 2);
1002 //kDebug() << "// PREP AUDIO THMB FRMO : scale:" << pixelForOneFrame<< ", from: " << startpixel << ", to: " << endpixel;
1003 //if ( (!audioThumbWasDrawn || framePixelWidth!=pixelForOneFrame ) && !baseClip()->audioFrameChache.isEmpty()){
1005 for (int startCache = startpixel - startpixel % 100; startCache < endpixel; startCache += 100) {
1006 //kDebug() << "creating " << startCache;
1007 //if (framePixelWidth!=pixelForOneFrame ||
1008 if (m_framePixelWidth == pixelForOneFrame && m_audioThumbCachePic.contains(startCache))
1010 if (m_audioThumbCachePic[startCache].isNull() || m_framePixelWidth != pixelForOneFrame) {
1011 m_audioThumbCachePic[startCache] = QPixmap(100, (int)(re.height()));
1012 m_audioThumbCachePic[startCache].fill(QColor(180, 180, 200, 140));
1014 bool fullAreaDraw = pixelForOneFrame < 10;
1015 QMap<int, QPainterPath > positiveChannelPaths;
1016 QMap<int, QPainterPath > negativeChannelPaths;
1017 QPainter pixpainter(&m_audioThumbCachePic[startCache]);
1019 audiopen.setWidth(0);
1020 pixpainter.setPen(audiopen);
1021 //pixpainter.setRenderHint(QPainter::Antialiasing,true);
1022 //pixpainter.drawLine(0,0,100,re.height());
1023 // Bail out, if caller provided invalid data
1024 if (channels <= 0) {
1025 kWarning() << "Unable to draw image with " << channels << "number of channels";
1029 int channelHeight = m_audioThumbCachePic[startCache].height() / channels;
1031 for (int i = 0; i < channels; i++) {
1033 positiveChannelPaths[i].moveTo(0, channelHeight*i + channelHeight / 2);
1034 negativeChannelPaths[i].moveTo(0, channelHeight*i + channelHeight / 2);
1037 for (int samples = 0; samples <= 100; samples++) {
1038 double frame = (double)(samples + startCache - 0) / pixelForOneFrame;
1039 int sample = (int)((frame - (int)(frame)) * 20); // AUDIO_FRAME_SIZE
1040 if (frame < 0 || sample < 0 || sample > 19)
1042 QMap<int, QByteArray> frame_channel_data = baseClip()->m_audioFrameCache[(int)frame];
1044 for (int channel = 0; channel < channels && frame_channel_data[channel].size() > 0; channel++) {
1046 int y = channelHeight * channel + channelHeight / 2;
1047 int delta = (int)(frame_channel_data[channel][sample] - 127 / 2) * channelHeight / 64;
1049 positiveChannelPaths[channel].lineTo(samples, 0.1 + y + qAbs(delta));
1050 negativeChannelPaths[channel].lineTo(samples, 0.1 + y - qAbs(delta));
1052 positiveChannelPaths[channel].lineTo(samples, 0.1 + y + delta);
1053 negativeChannelPaths[channel].lineTo(samples, 0.1 + y - delta);
1056 for (int channel = 0; channel < channels ; channel++)
1057 if (fullAreaDraw && samples == 100) {
1058 positiveChannelPaths[channel].lineTo(samples, channelHeight*channel + channelHeight / 2);
1059 negativeChannelPaths[channel].lineTo(samples, channelHeight*channel + channelHeight / 2);
1060 positiveChannelPaths[channel].lineTo(0, channelHeight*channel + channelHeight / 2);
1061 negativeChannelPaths[channel].lineTo(0, channelHeight*channel + channelHeight / 2);
1065 pixpainter.setPen(QPen(QColor(0, 0, 0)));
1066 pixpainter.setBrush(QBrush(QColor(60, 60, 60)));
1068 for (int i = 0; i < channels; i++) {
1070 //pixpainter.fillPath(positiveChannelPaths[i].united(negativeChannelPaths[i]),QBrush(Qt::SolidPattern));//or singleif looks better
1071 pixpainter.drawPath(positiveChannelPaths[i].united(negativeChannelPaths[i]));//or singleif looks better
1073 pixpainter.drawPath(positiveChannelPaths[i]);
1076 //audioThumbWasDrawn=true;
1077 m_framePixelWidth = pixelForOneFrame;
1082 int ClipItem::fadeIn() const
1087 int ClipItem::fadeOut() const
1093 void ClipItem::setFadeIn(int pos)
1095 if (pos == m_startFade) return;
1096 int oldIn = m_startFade;
1097 if (pos < 0) pos = 0;
1098 if (pos > cropDuration().frames(m_fps)) pos = (int)(cropDuration().frames(m_fps));
1100 QRectF rect = boundingRect();
1101 update(rect.x(), rect.y(), qMax(oldIn, pos), rect.height());
1104 void ClipItem::setFadeOut(int pos)
1106 if (pos == m_endFade) return;
1107 int oldOut = m_endFade;
1108 if (pos < 0) pos = 0;
1109 if (pos > cropDuration().frames(m_fps)) pos = (int)(cropDuration().frames(m_fps));
1111 QRectF rect = boundingRect();
1112 update(rect.x() + rect.width() - qMax(oldOut, pos), rect.y(), qMax(oldOut, pos), rect.height());
1116 void ClipItem::setFades(int in, int out)
1124 void ClipItem::hoverEnterEvent(QGraphicsSceneHoverEvent *e)
1126 //if (e->pos().x() < 20) m_hover = true;
1128 if (isItemLocked()) return;
1130 QRectF r = boundingRect();
1131 double width = 35 / projectScene()->scale().x();
1132 double height = r.height() / 2;
1133 //WARNING: seems like it generates a full repaint of the clip, maybe not so good...
1134 update(r.x(), r.y() + height, width, height);
1135 update(r.right() - width, r.y() + height, width, height);
1139 void ClipItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *)
1141 if (isItemLocked()) return;
1143 QRectF r = boundingRect();
1144 double width = 35 / projectScene()->scale().x();
1145 double height = r.height() / 2;
1146 //WARNING: seems like it generates a full repaint of the clip, maybe not so good...
1147 update(r.x(), r.y() + height, width, height);
1148 update(r.right() - width, r.y() + height, width, height);
1152 void ClipItem::resizeStart(int posx, bool /*size*/)
1154 bool sizeLimit = false;
1155 if (clipType() != IMAGE && clipType() != COLOR && clipType() != TEXT) {
1156 const int min = (startPos() - cropStart()).frames(m_fps);
1157 if (posx < min) posx = min;
1161 if (posx == startPos().frames(m_fps)) return;
1162 const int previous = cropStart().frames(m_fps);
1163 AbstractClipItem::resizeStart(posx, sizeLimit);
1165 // set speed independant info
1166 m_speedIndependantInfo = m_info;
1167 m_speedIndependantInfo.cropStart = GenTime((int)(m_info.cropStart.frames(m_fps) * qAbs(m_speed)), m_fps);
1168 m_speedIndependantInfo.cropDuration = GenTime((int)(m_info.cropDuration.frames(m_fps) * qAbs(m_speed)), m_fps);
1170 if ((int) cropStart().frames(m_fps) != previous) {
1171 if (m_hasThumbs && KdenliveSettings::videothumbnails()) {
1172 m_startThumbTimer.start(150);
1177 void ClipItem::resizeEnd(int posx)
1179 const int max = (startPos() - cropStart() + maxDuration()).frames(m_fps);
1180 if (posx > max && maxDuration() != GenTime()) posx = max;
1181 if (posx == endPos().frames(m_fps)) return;
1182 //kDebug() << "// NEW POS: " << posx << ", OLD END: " << endPos().frames(m_fps);
1183 const int previous = cropDuration().frames(m_fps);
1184 AbstractClipItem::resizeEnd(posx);
1186 // set speed independant info
1187 m_speedIndependantInfo = m_info;
1188 m_speedIndependantInfo.cropStart = GenTime((int)(m_info.cropStart.frames(m_fps) * qAbs(m_speed)), m_fps);
1189 m_speedIndependantInfo.cropDuration = GenTime((int)(m_info.cropDuration.frames(m_fps) * qAbs(m_speed)), m_fps);
1191 if ((int) cropDuration().frames(m_fps) != previous) {
1192 if (m_hasThumbs && KdenliveSettings::videothumbnails()) {
1193 m_endThumbTimer.start(150);
1199 bool ClipItem::checkEffectsKeyframesPos(const int previous, const int current, bool fromStart)
1201 bool effModified = false;
1202 for (int i = 0; i < m_effectList.count(); i++) {
1203 QDomElement effect = m_effectList.at(i);
1204 QDomNodeList params = effect.elementsByTagName("parameter");
1205 for (int j = 0; j < params.count(); j++) {
1206 bool modified = false;
1207 QDomElement e = params.item(j).toElement();
1208 if (!e.isNull() && (e.attribute("type") == "keyframe" || e.attribute("type") == "simplekeyframe")) {
1209 // parse keyframes and adjust values
1210 const QStringList keyframes = e.attribute("keyframes").split(';', QString::SkipEmptyParts);
1211 QMap <int, double> kfr;
1214 foreach(const QString &str, keyframes) {
1215 pos = str.section(':', 0, 0).toInt();
1216 val = str.section(':', 1, 1).toDouble();
1217 if (pos == previous) {
1218 // first or last keyframe
1222 if ((fromStart && pos >= current) || (!fromStart && pos <= current)) {
1223 // only keyframes in range
1232 QMap<int, double>::const_iterator k = kfr.constBegin();
1233 while (k != kfr.constEnd()) {
1234 newkfr.append(QString::number(k.key()) + ':' + QString::number(k.value()) + ';');
1237 e.setAttribute("keyframes", newkfr);
1242 if (effModified && m_selectedEffect >= 0) setSelectedEffect(m_selectedEffect);
1247 QVariant ClipItem::itemChange(GraphicsItemChange change, const QVariant &value)
1249 if (change == QGraphicsItem::ItemSelectedChange) {
1250 if (value.toBool()) setZValue(10);
1253 if (change == ItemPositionChange && scene()) {
1254 // calculate new position.
1255 //if (parentItem()) return pos();
1256 QPointF newPos = value.toPointF();
1257 //kDebug() << "/// MOVING CLIP ITEM.------------\n++++++++++";
1258 int xpos = projectScene()->getSnapPointForPos((int) newPos.x(), KdenliveSettings::snaptopoints());
1259 xpos = qMax(xpos, 0);
1261 int newTrack = newPos.y() / KdenliveSettings::trackheight();
1262 newTrack = qMin(newTrack, projectScene()->tracksCount() - 1);
1263 newTrack = qMax(newTrack, 0);
1264 newPos.setY((int)(newTrack * KdenliveSettings::trackheight() + 1));
1265 // Only one clip is moving
1266 QRectF sceneShape = rect();
1267 sceneShape.translate(newPos);
1268 QList<QGraphicsItem*> items;
1269 if (projectScene()->editMode() == NORMALEDIT)
1270 items = scene()->items(sceneShape, Qt::IntersectsItemShape);
1271 items.removeAll(this);
1272 bool forwardMove = newPos.x() > pos().x();
1274 if (!items.isEmpty()) {
1275 for (int i = 0; i < items.count(); i++) {
1276 if (!items.at(i)->isEnabled()) continue;
1277 if (items.at(i)->type() == type()) {
1279 QPointF otherPos = items.at(i)->pos();
1280 if ((int) otherPos.y() != (int) pos().y()) {
1284 offset = qMax(offset, (int)(newPos.x() - (static_cast < AbstractClipItem* >(items.at(i))->startPos() - cropDuration()).frames(m_fps)));
1286 offset = qMax(offset, (int)((static_cast < AbstractClipItem* >(items.at(i))->endPos().frames(m_fps)) - newPos.x()));
1291 sceneShape.translate(QPointF(-offset, 0));
1292 newPos.setX(newPos.x() - offset);
1294 sceneShape.translate(QPointF(offset, 0));
1295 newPos.setX(newPos.x() + offset);
1297 QList<QGraphicsItem*> subitems = scene()->items(sceneShape, Qt::IntersectsItemShape);
1298 subitems.removeAll(this);
1299 for (int j = 0; j < subitems.count(); j++) {
1300 if (!subitems.at(j)->isEnabled()) continue;
1301 if (subitems.at(j)->type() == type()) {
1302 // move was not successful, revert to previous pos
1303 m_info.startPos = GenTime((int) pos().x(), m_fps);
1309 m_info.track = newTrack;
1310 m_info.startPos = GenTime((int) newPos.x(), m_fps);
1316 m_info.track = newTrack;
1317 m_info.startPos = GenTime((int) newPos.x(), m_fps);
1318 //kDebug()<<"// ITEM NEW POS: "<<newPos.x()<<", mapped: "<<mapToScene(newPos.x(), 0).x();
1321 return QGraphicsItem::itemChange(change, value);
1325 /*void ClipItem::mouseMoveEvent(QGraphicsSceneMouseEvent * event) {
1328 int ClipItem::effectsCounter()
1330 return effectsCount() + 1;
1333 int ClipItem::effectsCount()
1335 return m_effectList.count();
1338 int ClipItem::hasEffect(const QString &tag, const QString &id) const
1340 return m_effectList.hasEffect(tag, id);
1343 QStringList ClipItem::effectNames()
1345 return m_effectList.effectNames();
1348 QDomElement ClipItem::effectAt(int ix) const
1350 if (ix > m_effectList.count() - 1 || ix < 0 || m_effectList.at(ix).isNull()) return QDomElement();
1351 return m_effectList.at(ix).cloneNode().toElement();
1354 QDomElement ClipItem::getEffectAt(int ix) const
1356 if (ix > m_effectList.count() - 1 || ix < 0 || m_effectList.at(ix).isNull()) return QDomElement();
1357 return m_effectList.at(ix);
1360 void ClipItem::setEffectAt(int ix, QDomElement effect)
1362 if (ix < 0 || ix > (m_effectList.count() - 1) || effect.isNull()) {
1363 kDebug() << "Invalid effect index: " << ix;
1366 //kDebug() << "CHange EFFECT AT: " << ix << ", CURR: " << m_effectList.at(ix).attribute("tag") << ", NEW: " << effect.attribute("tag");
1367 effect.setAttribute("kdenlive_ix", ix + 1);
1368 m_effectList.replace(ix, effect);
1369 m_effectNames = m_effectList.effectNames().join(" / ");
1370 QString id = effect.attribute("id");
1371 if (id == "fadein" || id == "fadeout" || id == "fade_from_black" || id == "fade_to_black")
1374 QRectF r = boundingRect();
1380 EffectsParameterList ClipItem::addEffect(const QDomElement effect, bool /*animate*/)
1382 bool needRepaint = false;
1384 if (!effect.hasAttribute("kdenlive_ix")) {
1385 ix = effectsCounter();
1386 } else ix = effect.attribute("kdenlive_ix").toInt();
1387 if (!m_effectList.isEmpty() && ix <= m_effectList.count()) {
1389 m_effectList.insert(ix - 1, effect);
1390 for (int i = ix; i < m_effectList.count(); i++) {
1391 int index = m_effectList.item(i).attribute("kdenlive_ix").toInt();
1392 if (index >= ix) m_effectList.item(i).setAttribute("kdenlive_ix", index + 1);
1394 } else m_effectList.append(effect);
1395 EffectsParameterList parameters;
1396 parameters.addParam("tag", effect.attribute("tag"));
1397 parameters.addParam("kdenlive_ix", effect.attribute("kdenlive_ix"));
1398 if (effect.hasAttribute("src")) parameters.addParam("src", effect.attribute("src"));
1399 if (effect.hasAttribute("disable")) parameters.addParam("disable", effect.attribute("disable"));
1401 QString effectId = effect.attribute("id");
1402 if (effectId.isEmpty()) effectId = effect.attribute("tag");
1403 parameters.addParam("id", effectId);
1405 // special case:Â the affine effect needs in / out points
1406 if (effectId == "pan_zoom") {
1407 parameters.addParam("in", QString::number(cropStart().frames(m_fps)));
1408 parameters.addParam("out", QString::number((cropStart() + cropDuration()).frames(m_fps)));
1411 QDomNodeList params = effect.elementsByTagName("parameter");
1413 for (int i = 0; i < params.count(); i++) {
1414 QDomElement e = params.item(i).toElement();
1416 if (e.attribute("type") == "simplekeyframe") {
1417 QStringList values = e.attribute("keyframes").split(";", QString::SkipEmptyParts);
1418 double factor = e.attribute("factor", "1").toDouble();
1420 for (int j = 0; j < values.count(); j++) {
1421 QString pos = values.at(j).section(':', 0, 0);
1422 double val = values.at(j).section(':', 1, 1).toDouble() / factor;
1423 values[j] = pos + "=" + QString::number(val);
1426 parameters.addParam(e.attribute("name"), values.join(";"));
1427 /*parameters.addParam("max", e.attribute("max"));
1428 parameters.addParam("min", e.attribute("min"));
1429 parameters.addParam("factor", );*/
1430 } else if (e.attribute("type") == "keyframe") {
1431 parameters.addParam("keyframes", e.attribute("keyframes"));
1432 parameters.addParam("max", e.attribute("max"));
1433 parameters.addParam("min", e.attribute("min"));
1434 parameters.addParam("factor", e.attribute("factor", "1"));
1435 parameters.addParam("starttag", e.attribute("starttag", "start"));
1436 parameters.addParam("endtag", e.attribute("endtag", "end"));
1437 } else if (e.attribute("factor", "1") == "1") {
1438 parameters.addParam(e.attribute("name"), e.attribute("value"));
1440 // check if it is a fade effect
1441 if (effectId == "fadein") {
1443 if (m_effectList.hasEffect(QString(), "fade_from_black") == -1) {
1444 if (e.attribute("name") == "out") fade += e.attribute("value").toInt();
1445 else if (e.attribute("name") == "in") fade -= e.attribute("value").toInt();
1447 QDomElement fadein = m_effectList.getEffectByTag(QString(), "fade_from_black");
1448 if (fadein.attribute("name") == "out") fade += fadein.attribute("value").toInt();
1449 else if (fadein.attribute("name") == "in") fade -= fadein.attribute("value").toInt();
1451 } else if (effectId == "fade_from_black") {
1453 if (m_effectList.hasEffect(QString(), "fadein") == -1) {
1454 if (e.attribute("name") == "out") fade += e.attribute("value").toInt();
1455 else if (e.attribute("name") == "in") fade -= e.attribute("value").toInt();
1457 QDomElement fadein = m_effectList.getEffectByTag(QString(), "fadein");
1458 if (fadein.attribute("name") == "out") fade += fadein.attribute("value").toInt();
1459 else if (fadein.attribute("name") == "in") fade -= fadein.attribute("value").toInt();
1461 } else if (effectId == "fadeout") {
1463 if (m_effectList.hasEffect(QString(), "fade_to_black") == -1) {
1464 if (e.attribute("name") == "out") fade -= e.attribute("value").toInt();
1465 else if (e.attribute("name") == "in") fade += e.attribute("value").toInt();
1467 QDomElement fadeout = m_effectList.getEffectByTag(QString(), "fade_to_black");
1468 if (fadeout.attribute("name") == "out") fade -= fadeout.attribute("value").toInt();
1469 else if (fadeout.attribute("name") == "in") fade += fadeout.attribute("value").toInt();
1471 } else if (effectId == "fade_to_black") {
1473 if (m_effectList.hasEffect(QString(), "fadeout") == -1) {
1474 if (e.attribute("name") == "out") fade -= e.attribute("value").toInt();
1475 else if (e.attribute("name") == "in") fade += e.attribute("value").toInt();
1477 QDomElement fadeout = m_effectList.getEffectByTag(QString(), "fadeout");
1478 if (fadeout.attribute("name") == "out") fade -= fadeout.attribute("value").toInt();
1479 else if (fadeout.attribute("name") == "in") fade += fadeout.attribute("value").toInt();
1484 if (e.attribute("factor").startsWith('%')) {
1485 fact = ProfilesDialog::getStringEval(projectScene()->profile(), e.attribute("factor"));
1486 } else fact = e.attribute("factor", "1").toDouble();
1487 parameters.addParam(e.attribute("name"), QString::number(e.attribute("value").toDouble() / fact));
1491 m_effectNames = m_effectList.effectNames().join(" / ");
1492 if (fade > 0) m_startFade = fade;
1493 else if (fade < 0) m_endFade = -fade;
1495 if (m_selectedEffect == -1) {
1496 setSelectedEffect(0);
1497 } else if (m_selectedEffect == ix - 1) setSelectedEffect(m_selectedEffect);
1498 if (needRepaint) update(boundingRect());
1502 else { /*if (!needRepaint) */
1503 QRectF r = boundingRect();
1510 void ClipItem::deleteEffect(QString index)
1512 bool needRepaint = false;
1515 for (int i = 0; i < m_effectList.count(); ++i) {
1516 ix = m_effectList.at(i).attribute("kdenlive_ix");
1518 QString effectId = m_effectList.at(i).attribute("id");
1519 if ((effectId == "fadein" && hasEffect(QString(), "fade_from_black") == -1) ||
1520 (effectId == "fade_from_black" && hasEffect(QString(), "fadein") == -1)) {
1523 } else if ((effectId == "fadeout" && hasEffect(QString(), "fade_to_black") == -1) ||
1524 (effectId == "fade_to_black" && hasEffect(QString(), "fadeout") == -1)) {
1527 } else if (EffectsList::hasKeyFrames(m_effectList.at(i))) needRepaint = true;
1528 m_effectList.removeAt(i);
1530 } else if (ix.toInt() > index.toInt()) {
1531 m_effectList.item(i).setAttribute("kdenlive_ix", ix.toInt() - 1);
1534 m_effectNames = m_effectList.effectNames().join(" / ");
1536 if (m_effectList.isEmpty() || m_selectedEffect + 1 == index.toInt()) {
1537 // Current effect was removed
1538 if (index.toInt() > m_effectList.count() - 1) {
1539 setSelectedEffect(m_effectList.count() - 1);
1540 } else setSelectedEffect(index.toInt());
1542 if (needRepaint) update(boundingRect());
1544 QRectF r = boundingRect();
1548 //if (!m_effectList.isEmpty()) flashClip();
1551 double ClipItem::speed() const
1556 int ClipItem::strobe() const
1561 void ClipItem::setSpeed(const double speed, const int strobe)
1564 if (m_speed <= 0 && m_speed > -1)
1567 if (m_speed == 1.0) m_clipName = baseClip()->name();
1568 else m_clipName = baseClip()->name() + " - " + QString::number(speed * 100, 'f', 0) + '%';
1569 m_info.cropStart = GenTime((int)(m_speedIndependantInfo.cropStart.frames(m_fps) / qAbs(m_speed) + 0.5), m_fps);
1570 m_info.cropDuration = GenTime((int)(m_speedIndependantInfo.cropDuration.frames(m_fps) / qAbs(m_speed) + 0.5), m_fps);
1574 GenTime ClipItem::maxDuration() const
1576 return GenTime((int)(m_maxDuration.frames(m_fps) / qAbs(m_speed) + 0.5), m_fps);
1579 GenTime ClipItem::speedIndependantCropStart() const
1581 return m_speedIndependantInfo.cropStart;
1584 GenTime ClipItem::speedIndependantCropDuration() const
1586 return m_speedIndependantInfo.cropDuration;
1590 const ItemInfo ClipItem::speedIndependantInfo() const
1592 return m_speedIndependantInfo;
1596 void ClipItem::dropEvent(QGraphicsSceneDragDropEvent * event)
1598 const QString effects = QString::fromUtf8(event->mimeData()->data("kdenlive/effectslist"));
1600 doc.setContent(effects, true);
1601 const QDomElement e = doc.documentElement();
1602 if (scene() && !scene()->views().isEmpty()) {
1604 CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
1605 if (view) view->slotAddEffect(e, m_info.startPos, track());
1610 void ClipItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
1612 if (isItemLocked()) event->setAccepted(false);
1613 else event->setAccepted(event->mimeData()->hasFormat("kdenlive/effectslist"));
1616 void ClipItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
1621 void ClipItem::addTransition(Transition* t)
1623 m_transitionsList.append(t);
1624 //CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
1626 QDomElement e = doc.documentElement();
1627 //if (view) view->slotAddTransition(this, t->toXML() , t->startPos(), track());
1630 void ClipItem::setVideoOnly(bool force)
1632 m_videoOnly = force;
1635 void ClipItem::setAudioOnly(bool force)
1637 m_audioOnly = force;
1638 if (m_audioOnly) m_baseColor = QColor(141, 215, 166);
1640 if (m_clipType == COLOR) {
1641 QString colour = m_clip->getProperty("colour");
1642 colour = colour.replace(0, 2, "#");
1643 m_baseColor = QColor(colour.left(7));
1644 } else if (m_clipType == AUDIO) m_baseColor = QColor(141, 215, 166);
1645 else m_baseColor = QColor(141, 166, 215);
1647 m_audioThumbCachePic.clear();
1650 bool ClipItem::isAudioOnly() const
1655 bool ClipItem::isVideoOnly() const
1660 void ClipItem::insertKeyframe(QDomElement effect, int pos, int val)
1662 if (effect.attribute("disable") == "1") return;
1663 effect.setAttribute("active_keyframe", pos);
1664 m_editedKeyframe = pos;
1665 QDomNodeList params = effect.elementsByTagName("parameter");
1666 for (int i = 0; i < params.count(); i++) {
1667 QDomElement e = params.item(i).toElement();
1668 if (!e.isNull() && (e.attribute("type") == "keyframe" || e.attribute("type") == "simplekeyframe")) {
1669 QString kfr = e.attribute("keyframes");
1670 const QStringList keyframes = kfr.split(';', QString::SkipEmptyParts);
1673 foreach(const QString &str, keyframes) {
1674 int kpos = str.section(':', 0, 0).toInt();
1675 double newval = str.section(':', 1, 1).toDouble();
1678 } else if (!added) {
1679 if (i == m_visibleParam)
1680 newkfr.append(QString::number(pos) + ":" + QString::number(val));
1682 newkfr.append(QString::number(pos) + ":" + QString::number(newval));
1683 if (kpos > pos) newkfr.append(str);
1685 } else newkfr.append(str);
1688 if (i == m_visibleParam)
1689 newkfr.append(QString::number(pos) + ":" + QString::number(val));
1691 newkfr.append(QString::number(pos) + ":" + e.attribute("default"));
1693 e.setAttribute("keyframes", newkfr.join(";"));
1698 void ClipItem::movedKeyframe(QDomElement effect, int oldpos, int newpos, double value)
1700 if (effect.attribute("disable") == "1") return;
1701 effect.setAttribute("active_keyframe", newpos);
1702 QDomNodeList params = effect.elementsByTagName("parameter");
1703 int start = cropStart().frames(m_fps);
1704 int end = (cropStart() + cropDuration()).frames(m_fps) - 1;
1705 for (int i = 0; i < params.count(); i++) {
1706 QDomElement e = params.item(i).toElement();
1707 if (!e.isNull() && (e.attribute("type") == "keyframe" || e.attribute("type") == "simplekeyframe")) {
1708 QString kfr = e.attribute("keyframes");
1709 const QStringList keyframes = kfr.split(';', QString::SkipEmptyParts);
1711 foreach(const QString &str, keyframes) {
1712 if (str.section(':', 0, 0).toInt() != oldpos) {
1714 } else if (newpos != -1) {
1715 newpos = qMax(newpos, start);
1716 newpos = qMin(newpos, end);
1717 if (i == m_visibleParam)
1718 newkfr.append(QString::number(newpos) + ":" + QString::number(value));
1720 newkfr.append(QString::number(newpos) + ":" + str.section(':', 1, 1));
1723 e.setAttribute("keyframes", newkfr.join(";"));
1727 updateKeyframes(effect);
1731 void ClipItem::updateKeyframes(QDomElement effect)
1733 m_keyframes.clear();
1735 QDomNodeList params = effect.elementsByTagName("parameter");
1736 QDomElement e = params.item(m_visibleParam).toElement();
1737 if (e.attribute("intimeline") != "1") {
1738 setSelectedEffect(m_selectedEffect);
1741 const QStringList keyframes = e.attribute("keyframes").split(';', QString::SkipEmptyParts);
1742 foreach(const QString &str, keyframes) {
1743 int pos = str.section(':', 0, 0).toInt();
1744 double val = str.section(':', 1, 1).toDouble();
1745 m_keyframes[pos] = val;
1747 if (!m_keyframes.contains(m_selectedKeyframe)) m_selectedKeyframe = -1;
1750 void ClipItem::doGetIntraThumbs(QPainter *painter, const QPointF startPos, int offset, int start, int end)
1752 if (!m_clip->thumbProducer() || clipType() == COLOR) return;
1753 if (scene() && scene()->views().isEmpty()) return;
1754 CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
1755 if (view == NULL) return;
1756 const int theight = KdenliveSettings::trackheight();
1757 const int twidth = FRAME_SIZE;
1759 if (clipType() == IMAGE || clipType() == TEXT) {
1760 for (int i = start; i <= end; i++)
1761 painter->drawPixmap(startPos + QPointF(twidth *(i - offset), 0), m_startPix);
1764 for (int i = start; i <= end; i++) {
1765 if (!view->pixmapCache->find(m_clip->fileURL().path() + "%" + QString::number(i), p)) {
1766 p = m_clip->thumbProducer()->extractImage(i, twidth, theight);
1767 view->pixmapCache->insert(m_clip->fileURL().path() + "%" + QString::number(i), p);
1769 painter->drawPixmap(startPos + QPointF(twidth *(i - offset), 0), p);
1773 QList <int> ClipItem::updatePanZoom(int width, int height, int cut)
1775 QList <int> effectPositions;
1776 for (int i = 0; i < m_effectList.count(); i++) {
1777 QDomElement effect = m_effectList.at(i);
1778 QDomNodeList params = effect.elementsByTagName("parameter");
1779 for (int j = 0; j < params.count(); j++) {
1780 QDomElement e = params.item(j).toElement();
1783 if (e.attribute("type") == "geometry" && !e.hasAttribute("fixed")) {
1784 effectPositions << i;
1786 int in = cropStart().frames(fps());
1787 int out = in + cropDuration().frames(fps());
1788 int dur = out - in - 1;
1790 effect.setAttribute("in", in);
1791 effect.setAttribute("out", out);
1793 Mlt::Geometry geometry(e.attribute("value").toUtf8().data(), dur, width, height);
1794 Mlt::GeometryItem item;
1795 bool endFrameAdded = false;
1797 while (!geometry.next_key(&item, dur)) {
1798 if (!endFrameAdded) {
1799 // add keyframe at the end with interpolated value
1802 endFrameAdded = true;
1804 Mlt::GeometryItem endItem;
1805 Mlt::GeometryItem interp;
1806 geometry.fetch(&interp, dur - 1);
1807 endItem.frame(dur - 1);
1808 endItem.x(interp.x());
1809 endItem.y(interp.y());
1810 endItem.w(interp.w());
1811 endItem.h(interp.h());
1812 endItem.mix(interp.mix());
1813 geometry.insert(&endItem);
1815 geometry.remove(item.frame());
1818 Mlt::Geometry origGeometry(e.attribute("value").toUtf8().data(), dur, width, height);
1819 // remove keyframes before cut point
1820 while (!geometry.prev_key(&item, cut - 1) && item.frame() < cut)
1821 geometry.remove(item.frame());
1823 // add a keyframe at new pos 0
1824 origGeometry.fetch(&item, cut);
1826 geometry.insert(&item);
1828 // move exisiting keyframes by -cut
1829 while (!origGeometry.next_key(&item, cut)) {
1830 geometry.remove(item.frame());
1831 origGeometry.remove(item.frame());
1832 item.frame(item.frame() - cut);
1833 geometry.insert(&item);
1838 e.setAttribute("value", geometry.serialise());
1843 return effectPositions;
1846 Mlt::Producer *ClipItem::getProducer(int track, bool trackSpecific)
1849 return m_clip->audioProducer(track);
1850 else if (isVideoOnly())
1851 return m_clip->videoProducer();
1853 return m_clip->producer(trackSpecific ? track : -1);
1856 #include "clipitem.moc"