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 + ';' + QString::number((cropStart() + cropDuration()).frames(m_fps) - 1) + ':' + 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")) {
393 e.setAttribute("keyframes", keyframes.at(keyframeParams));
394 if (ix == m_selectedEffect && keyframeParams == 0) {
396 double max = e.attribute("max").toDouble();
397 double min = e.attribute("min").toDouble();
398 m_keyframeFactor = 100.0 / (max - min);
399 m_keyframeDefault = e.attribute("default").toDouble();
400 m_selectedKeyframe = 0;
402 const QStringList keyframes = e.attribute("keyframes").split(';', QString::SkipEmptyParts);
403 foreach(const QString &str, keyframes) {
404 int pos = str.section(':', 0, 0).toInt();
405 double val = str.section(':', 1, 1).toDouble();
406 m_keyframes[pos] = val;
408 if (m_keyframes.find(m_editedKeyframe) == m_keyframes.end()) m_editedKeyframe = -1;
409 if (m_keyframes.find(m_editedKeyframe) == m_keyframes.end()) m_editedKeyframe = -1;
418 void ClipItem::setSelectedEffect(const int ix)
420 m_selectedEffect = ix;
421 QDomElement effect = effectAt(m_selectedEffect);
422 if (effect.isNull() == false) {
423 QDomNodeList params = effect.elementsByTagName("parameter");
424 if (effect.attribute("disable") != "1")
425 for (int i = 0; i < params.count(); i++) {
426 QDomElement e = params.item(i).toElement();
427 if (!e.isNull() && (e.attribute("type") == "keyframe" || e.attribute("type") == "simplekeyframe")) {
429 double max = e.attribute("max").toDouble();
430 double min = e.attribute("min").toDouble();
431 m_keyframeFactor = 100.0 / (max - min);
432 m_keyframeDefault = e.attribute("default").toDouble();
433 m_selectedKeyframe = 0;
436 const QStringList keyframes = e.attribute("keyframes").split(';', QString::SkipEmptyParts);
437 foreach(const QString &str, keyframes) {
438 int pos = str.section(':', 0, 0).toInt();
439 double val = str.section(':', 1, 1).toDouble();
440 m_keyframes[pos] = val;
442 if (m_keyframes.find(m_editedKeyframe) == m_keyframes.end()) m_editedKeyframe = -1;
448 if (!m_keyframes.isEmpty()) {
454 QStringList ClipItem::keyframes(const int index)
457 QDomElement effect = effectAt(index);
458 QDomNodeList params = effect.elementsByTagName("parameter");
460 for (int i = 0; i < params.count(); i++) {
461 QDomElement e = params.item(i).toElement();
462 if (!e.isNull() && (e.attribute("type") == "keyframe" || e.attribute("type") == "simplekeyframe"))
463 result.append(e.attribute("keyframes"));
468 void ClipItem::updateKeyframeEffect()
470 // regenerate xml parameter from the clip keyframes
471 QDomElement effect = getEffectAt(m_selectedEffect);
472 if (effect.attribute("disable") == "1") return;
473 QDomNodeList params = effect.elementsByTagName("parameter");
475 for (int i = 0; i < params.count(); i++) {
476 QDomElement e = params.item(i).toElement();
477 if (!e.isNull() && (e.attribute("type") == "keyframe" || e.attribute("type") == "simplekeyframe")) {
479 if (m_keyframes.count() > 0) {
480 QMap<int, int>::const_iterator i = m_keyframes.constBegin();
481 while (i != m_keyframes.constEnd()) {
482 keyframes.append(QString::number(i.key()) + ':' + QString::number(i.value()) + ';');
486 // Effect has a keyframe type parameter, we need to set the values
487 //kDebug() << "::::::::::::::: SETTING EFFECT KEYFRAMES: " << keyframes;
488 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)
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));
532 } else resetThumbs(checkDuration);
535 void ClipItem::slotFetchThumbs()
537 if (scene() == NULL || m_clipType == AUDIO || m_clipType == COLOR) return;
538 if (m_clipType == IMAGE) {
539 if (m_startPix.isNull()) {
540 m_startPix = KThumb::getImage(KUrl(m_clip->getProperty("resource")), (int)(KdenliveSettings::trackheight() * KdenliveSettings::project_display_ratio()), KdenliveSettings::trackheight());
546 if (m_clipType == TEXT) {
547 if (m_startPix.isNull()) slotGetStartThumb();
551 if (m_endPix.isNull() && m_startPix.isNull()) {
552 m_startThumbRequested = true;
553 m_endThumbRequested = true;
554 emit getThumb((int)m_speedIndependantInfo.cropStart.frames(m_fps), (int)(m_speedIndependantInfo.cropStart + m_speedIndependantInfo.cropDuration).frames(m_fps) - 1);
556 if (m_endPix.isNull()) {
559 if (m_startPix.isNull()) {
565 void ClipItem::slotGetStartThumb()
567 m_startThumbRequested = true;
568 emit getThumb((int)m_speedIndependantInfo.cropStart.frames(m_fps), -1);
571 void ClipItem::slotGetEndThumb()
573 m_endThumbRequested = true;
574 emit getThumb(-1, (int)(m_speedIndependantInfo.cropStart + m_speedIndependantInfo.cropDuration).frames(m_fps) - 1);
578 void ClipItem::slotSetStartThumb(QImage img)
580 if (!img.isNull() && img.format() == QImage::Format_ARGB32) {
581 QPixmap pix = QPixmap::fromImage(img);
583 QRectF r = sceneBoundingRect();
584 r.setRight(pix.width() + 2);
589 void ClipItem::slotSetEndThumb(QImage img)
591 if (!img.isNull() && img.format() == QImage::Format_ARGB32) {
592 QPixmap pix = QPixmap::fromImage(img);
594 QRectF r = sceneBoundingRect();
595 r.setLeft(r.right() - pix.width() - 2);
600 void ClipItem::slotThumbReady(int frame, QImage img)
602 if (scene() == NULL) return;
603 QRectF r = boundingRect();
604 QPixmap pix = QPixmap::fromImage(img);
605 double width = pix.width() / projectScene()->scale().x();
606 if (m_startThumbRequested && frame == m_speedIndependantInfo.cropStart.frames(m_fps)) {
608 m_startThumbRequested = false;
609 update(r.left(), r.top(), width, pix.height());
610 if (m_clipType == IMAGE || m_clipType == TEXT) {
611 update(r.right() - width, r.top(), width, pix.height());
613 } else if (m_endThumbRequested && frame == (m_speedIndependantInfo.cropStart + m_speedIndependantInfo.cropDuration).frames(m_fps) - 1) {
615 m_endThumbRequested = false;
616 update(r.right() - width, r.top(), width, pix.height());
620 void ClipItem::slotSetStartThumb(const QPixmap pix)
625 void ClipItem::slotSetEndThumb(const QPixmap pix)
630 QPixmap ClipItem::startThumb() const
635 QPixmap ClipItem::endThumb() const
640 void ClipItem::slotGotAudioData()
642 m_audioThumbReady = true;
643 if (m_clipType == AV && !isAudioOnly()) {
644 QRectF r = boundingRect();
645 r.setTop(r.top() + r.height() / 2 - 1);
650 int ClipItem::type() const
655 DocClipBase *ClipItem::baseClip() const
660 QDomElement ClipItem::xml() const
665 QDomElement ClipItem::itemXml() const
667 QDomElement xml = m_clip->toXML();
668 if (m_speed != 1.0) xml.setAttribute("speed", m_speed);
669 if (m_strobe > 1) xml.setAttribute("strobe", m_strobe);
670 if (m_audioOnly) xml.setAttribute("audio_only", 1);
671 else if (m_videoOnly) xml.setAttribute("video_only", 1);
675 int ClipItem::clipType() const
680 QString ClipItem::clipName() const
685 void ClipItem::setClipName(const QString &name)
690 const QString ClipItem::clipProducer() const
695 void ClipItem::flashClip()
697 if (m_timeLine == 0) {
698 m_timeLine = new QTimeLine(750, this);
699 m_timeLine->setUpdateInterval(80);
700 m_timeLine->setCurveShape(QTimeLine::EaseInOutCurve);
701 m_timeLine->setFrameRange(0, 100);
702 connect(m_timeLine, SIGNAL(valueChanged(qreal)), this, SLOT(animate(qreal)));
704 //m_timeLine->start();
707 void ClipItem::animate(qreal /*value*/)
709 QRectF r = boundingRect();
715 void ClipItem::paint(QPainter *painter,
716 const QStyleOptionGraphicsItem *option,
720 if (parentItem()) paintColor = QColor(255, 248, 149);
721 else paintColor = m_baseColor;
722 if (isSelected() || (parentItem() && parentItem()->isSelected())) paintColor = paintColor.darker();
724 painter->setMatrixEnabled(false);
725 const QRectF mapped = painter->matrix().mapRect(rect()).adjusted(0.5, 0, 0.5, 0);
726 const QRectF exposed = option->exposedRect;
727 painter->setClipRect(mapped);
728 painter->fillRect(mapped, paintColor);
731 if (KdenliveSettings::videothumbnails() && !isAudioOnly()) {
732 QPen pen = painter->pen();
733 pen.setColor(QColor(255, 255, 255, 150));
734 painter->setPen(pen);
735 if ((m_clipType == IMAGE || m_clipType == TEXT) && !m_startPix.isNull()) {
736 const QPointF top = mapped.topRight() - QPointF(m_startPix.width() - 1, 0);
737 painter->drawPixmap(top, m_startPix);
738 QLineF l2(top.x(), mapped.top(), top.x(), mapped.bottom());
739 painter->drawLine(l2);
740 } else if (!m_endPix.isNull()) {
741 const QPointF top = mapped.topRight() - QPointF(m_endPix.width() - 1, 0);
742 painter->drawPixmap(top, m_endPix);
743 QLineF l2(top.x(), mapped.top(), top.x(), mapped.bottom());
744 painter->drawLine(l2);
746 if (!m_startPix.isNull()) {
747 painter->drawPixmap(mapped.topLeft(), m_startPix);
748 QLineF l2(mapped.left() + m_startPix.width(), mapped.top(), mapped.left() + m_startPix.width(), mapped.bottom());
749 painter->drawLine(l2);
751 if (painter->matrix().m11() == FRAME_SIZE) {
752 int offset = (m_info.startPos - m_info.cropStart).frames(m_fps);
753 int left = qMax((int) m_info.startPos.frames(m_fps) + 1, (int) mapToScene(exposed.left(), 0).x());
754 int right = qMin((int)(m_info.startPos + m_info.cropDuration).frames(m_fps) - 1, (int) mapToScene(exposed.right(), 0).x());
755 doGetIntraThumbs(painter, mapped.topLeft(), m_info.cropStart.frames(m_fps), left - offset, right - offset);
757 painter->setPen(Qt::black);
760 // draw audio thumbnails
761 if (KdenliveSettings::audiothumbnails() && m_speed == 1.0 && !isVideoOnly() && ((m_clipType == AV && (exposed.bottom() > (rect().height() / 2) || isAudioOnly())) || m_clipType == AUDIO) && m_audioThumbReady) {
763 double startpixel = exposed.left();
766 double endpixel = exposed.right();
769 //kDebug()<<"/// REPAINTING AUDIO THMBS ZONE: "<<startpixel<<"x"<<endpixel;
771 /*QPainterPath path = m_clipType == AV ? roundRectPathLower : resultClipPath;*/
773 if (m_clipType == AV && !isAudioOnly()) {
775 mappedRect.setTop(mappedRect.bottom() - mapped.height() / 2);
776 } else mappedRect = mapped;
778 double scale = painter->matrix().m11();
780 if (isEnabled() && m_clip) channels = m_clip->getProperty("channels").toInt();
781 if (scale != m_framePixelWidth)
782 m_audioThumbCachePic.clear();
783 double cropLeft = m_info.cropStart.frames(m_fps);
784 const int clipStart = mappedRect.x();
785 const int mappedStartPixel = painter->matrix().map(QPointF(startpixel + cropLeft, 0)).x() - clipStart;
786 const int mappedEndPixel = painter->matrix().map(QPointF(endpixel + cropLeft, 0)).x() - clipStart;
787 cropLeft = cropLeft * scale;
791 emit prepareAudioThumb(scale, mappedStartPixel, mappedEndPixel, channels);
794 for (int startCache = mappedStartPixel - (mappedStartPixel) % 100; startCache < mappedEndPixel; startCache += 100) {
795 if (m_audioThumbCachePic.contains(startCache) && !m_audioThumbCachePic[startCache].isNull())
796 painter->drawPixmap(clipStart + startCache - cropLeft, mappedRect.y(), m_audioThumbCachePic[startCache]);
800 // Draw effects names
801 if (!m_effectNames.isEmpty() && mapped.width() > 40) {
802 QRectF txtBounding = painter->boundingRect(mapped, Qt::AlignLeft | Qt::AlignTop, m_effectNames);
804 if (m_timeLine && m_timeLine->state() == QTimeLine::Running) {
805 qreal value = m_timeLine->currentValue();
806 txtBounding.setWidth(txtBounding.width() * value);
807 bgColor.setRgb(50 + 200 *(1.0 - value), 50, 50, 100 + 50 * value);
808 } else bgColor.setRgb(50, 50, 90, 180);
810 QPainterPath rounded;
811 rounded.moveTo(txtBounding.bottomRight());
812 rounded.arcTo(txtBounding.right() - txtBounding.height() - 2, txtBounding.top() - txtBounding.height(), txtBounding.height() * 2, txtBounding.height() * 2, 270, 90);
813 rounded.lineTo(txtBounding.topLeft());
814 rounded.lineTo(txtBounding.bottomLeft());
815 painter->fillPath(rounded, bgColor);
816 painter->setPen(Qt::lightGray);
817 painter->drawText(txtBounding.adjusted(1, 0, 1, 0), Qt::AlignCenter, m_effectNames);
821 QColor frameColor(paintColor.darker());
822 if (isSelected() || (parentItem() && parentItem()->isSelected())) {
823 frameColor = QColor(Qt::red);
825 frameColor.setAlpha(160);
827 const QRectF txtBounding2 = painter->boundingRect(mapped, Qt::AlignHCenter | Qt::AlignVCenter, ' ' + m_clipName + ' ');
828 //painter->fillRect(txtBounding2, frameColor);
829 painter->setBrush(frameColor);
830 painter->setPen(Qt::NoPen);
831 painter->drawRoundedRect(txtBounding2, 3, 3);
832 painter->setBrush(QBrush(Qt::NoBrush));
834 //painter->setPen(QColor(0, 0, 0, 180));
835 //painter->drawText(txtBounding, Qt::AlignCenter, m_clipName);
837 painter->drawPixmap(txtBounding2.topLeft() - QPointF(17, -1), m_videoPix);
838 } else if (m_audioOnly) {
839 painter->drawPixmap(txtBounding2.topLeft() - QPointF(17, -1), m_audioPix);
841 painter->setPen(Qt::white);
842 painter->drawText(txtBounding2, Qt::AlignCenter, m_clipName);
846 if (isEnabled() && m_clip) {
847 QList < CommentedTime > markers = m_clip->commentedSnapMarkers();
848 QList < CommentedTime >::Iterator it = markers.begin();
851 QBrush markerBrush(QColor(120, 120, 0, 140));
852 QPen pen = painter->pen();
853 pen.setColor(QColor(255, 255, 255, 200));
854 pen.setStyle(Qt::DotLine);
856 for (; it != markers.end(); ++it) {
857 pos = GenTime((int)((*it).time().frames(m_fps) / qAbs(m_speed) + 0.5), m_fps) - cropStart();
858 if (pos > GenTime()) {
859 if (pos > cropDuration()) break;
860 QLineF l(rect().x() + pos.frames(m_fps), rect().y(), rect().x() + pos.frames(m_fps), rect().bottom());
861 QLineF l2 = painter->matrix().map(l);
862 painter->setPen(pen);
863 painter->drawLine(l2);
864 if (KdenliveSettings::showmarkers()) {
865 framepos = rect().x() + pos.frames(m_fps);
866 const QRectF r1(framepos + 0.04, 10, rect().width() - framepos - 2, rect().height() - 10);
867 const QRectF r2 = painter->matrix().mapRect(r1);
868 const QRectF txtBounding3 = painter->boundingRect(r2, Qt::AlignLeft | Qt::AlignTop, ' ' + (*it).comment() + ' ');
869 painter->setBrush(markerBrush);
870 painter->setPen(Qt::NoPen);
871 painter->drawRoundedRect(txtBounding3, 3, 3);
872 painter->setBrush(QBrush(Qt::NoBrush));
873 painter->setPen(Qt::white);
874 painter->drawText(txtBounding3, Qt::AlignCenter, (*it).comment());
876 //painter->fillRect(QRect(br.x() + framepos, br.y(), 10, br.height()), QBrush(QColor(0, 0, 0, 150)));
881 // draw start / end fades
884 fades = QBrush(QColor(200, 50, 50, 150));
885 } else fades = QBrush(QColor(200, 200, 200, 200));
887 if (m_startFade != 0) {
888 QPainterPath fadeInPath;
889 fadeInPath.moveTo(0, 0);
890 fadeInPath.lineTo(0, rect().height());
891 fadeInPath.lineTo(m_startFade, 0);
892 fadeInPath.closeSubpath();
893 QPainterPath f1 = painter->matrix().map(fadeInPath);
894 painter->fillPath(f1/*.intersected(resultClipPath)*/, fades);
895 /*if (isSelected()) {
896 QLineF l(m_startFade * scale, 0, 0, itemHeight);
897 painter->drawLine(l);
900 if (m_endFade != 0) {
901 QPainterPath fadeOutPath;
902 fadeOutPath.moveTo(rect().width(), 0);
903 fadeOutPath.lineTo(rect().width(), rect().height());
904 fadeOutPath.lineTo(rect().width() - m_endFade, 0);
905 fadeOutPath.closeSubpath();
906 QPainterPath f1 = painter->matrix().map(fadeOutPath);
907 painter->fillPath(f1/*.intersected(resultClipPath)*/, fades);
908 /*if (isSelected()) {
909 QLineF l(itemWidth - m_endFade * scale, 0, itemWidth, itemHeight);
910 painter->drawLine(l);
915 painter->setPen(QPen(Qt::lightGray));
916 // draw effect or transition keyframes
917 if (mapped.width() > 20) drawKeyFrames(painter, exposed);
919 //painter->setMatrixEnabled(true);
922 // expand clip rect to allow correct painting of clip border
923 QPen pen1(frameColor);
924 painter->setPen(pen1);
925 painter->setClipping(false);
926 painter->drawRect(painter->matrix().mapRect(rect()));
930 OPERATIONTYPE ClipItem::operationMode(QPointF pos)
932 if (isItemLocked()) return NONE;
933 const double scale = projectScene()->scale().x();
934 double maximumOffset = 6 / scale;
935 if (isSelected() || (parentItem() && parentItem()->isSelected())) {
936 int kf = mouseOverKeyFrames(pos, maximumOffset);
938 m_editedKeyframe = kf;
942 QRectF rect = sceneBoundingRect();
943 int addtransitionOffset = 10;
944 // Don't allow add transition if track height is very small
945 if (rect.height() < 30) addtransitionOffset = 0;
947 if (qAbs((int)(pos.x() - (rect.x() + m_startFade))) < maximumOffset && qAbs((int)(pos.y() - rect.y())) < 6) {
948 if (m_startFade == 0) setToolTip(i18n("Add audio fade"));
949 // xgettext:no-c-format
950 else setToolTip(i18n("Audio fade duration: %1s", GenTime(m_startFade, m_fps).seconds()));
952 } else if (pos.x() - rect.x() < maximumOffset && (rect.bottom() - pos.y() > addtransitionOffset)) {
953 // xgettext:no-c-format
954 setToolTip(i18n("Crop from start: %1s", cropStart().seconds()));
956 } else if (qAbs((int)(pos.x() - (rect.x() + rect.width() - m_endFade))) < maximumOffset && qAbs((int)(pos.y() - rect.y())) < 6) {
957 if (m_endFade == 0) setToolTip(i18n("Add audio fade"));
958 // xgettext:no-c-format
959 else setToolTip(i18n("Audio fade duration: %1s", GenTime(m_endFade, m_fps).seconds()));
961 } else if ((rect.right() - pos.x() < maximumOffset) && (rect.bottom() - pos.y() > addtransitionOffset)) {
962 // xgettext:no-c-format
963 setToolTip(i18n("Clip duration: %1s", cropDuration().seconds()));
965 } else if ((pos.x() - rect.x() < 16 / scale) && (rect.bottom() - pos.y() <= addtransitionOffset)) {
966 setToolTip(i18n("Add transition"));
967 return TRANSITIONSTART;
968 } else if ((rect.right() - pos.x() < 16 / scale) && (rect.bottom() - pos.y() <= addtransitionOffset)) {
969 setToolTip(i18n("Add transition"));
970 return TRANSITIONEND;
972 QString tooltip = "<b>" + m_clipName + "</b>";
973 if (!baseClip()->fileURL().isEmpty())
974 tooltip.append("<br />" + baseClip()->fileURL().path());
975 if (!baseClip()->description().isEmpty())
976 tooltip.append("<br />" + baseClip()->description());
981 QList <GenTime> ClipItem::snapMarkers() const
983 QList < GenTime > snaps;
984 QList < GenTime > markers = baseClip()->snapMarkers();
987 for (int i = 0; i < markers.size(); i++) {
988 pos = GenTime((int)(markers.at(i).frames(m_fps) / qAbs(m_speed) + 0.5), m_fps) - cropStart();
989 if (pos > GenTime()) {
990 if (pos > cropDuration()) break;
991 else snaps.append(pos + startPos());
997 QList <CommentedTime> ClipItem::commentedSnapMarkers() const
999 QList < CommentedTime > snaps;
1000 QList < CommentedTime > markers = baseClip()->commentedSnapMarkers();
1003 for (int i = 0; i < markers.size(); i++) {
1004 pos = GenTime((int)(markers.at(i).time().frames(m_fps) / qAbs(m_speed) + 0.5), m_fps) - cropStart();
1005 if (pos > GenTime()) {
1006 if (pos > cropDuration()) break;
1007 else snaps.append(CommentedTime(pos + startPos(), markers.at(i).comment()));
1013 void ClipItem::slotPrepareAudioThumb(double pixelForOneFrame, int startpixel, int endpixel, int channels)
1015 QRectF re = sceneBoundingRect();
1016 if (m_clipType == AV && !isAudioOnly()) re.setTop(re.y() + re.height() / 2);
1018 //kDebug() << "// PREP AUDIO THMB FRMO : scale:" << pixelForOneFrame<< ", from: " << startpixel << ", to: " << endpixel;
1019 //if ( (!audioThumbWasDrawn || framePixelWidth!=pixelForOneFrame ) && !baseClip()->audioFrameChache.isEmpty()){
1021 for (int startCache = startpixel - startpixel % 100; startCache < endpixel; startCache += 100) {
1022 //kDebug() << "creating " << startCache;
1023 //if (framePixelWidth!=pixelForOneFrame ||
1024 if (m_framePixelWidth == pixelForOneFrame && m_audioThumbCachePic.contains(startCache))
1026 if (m_audioThumbCachePic[startCache].isNull() || m_framePixelWidth != pixelForOneFrame) {
1027 m_audioThumbCachePic[startCache] = QPixmap(100, (int)(re.height()));
1028 m_audioThumbCachePic[startCache].fill(QColor(180, 180, 200, 140));
1030 bool fullAreaDraw = pixelForOneFrame < 10;
1031 QMap<int, QPainterPath > positiveChannelPaths;
1032 QMap<int, QPainterPath > negativeChannelPaths;
1033 QPainter pixpainter(&m_audioThumbCachePic[startCache]);
1035 audiopen.setWidth(0);
1036 pixpainter.setPen(audiopen);
1037 //pixpainter.setRenderHint(QPainter::Antialiasing,true);
1038 //pixpainter.drawLine(0,0,100,re.height());
1039 // Bail out, if caller provided invalid data
1040 if (channels <= 0) {
1041 kWarning() << "Unable to draw image with " << channels << "number of channels";
1045 int channelHeight = m_audioThumbCachePic[startCache].height() / channels;
1047 for (int i = 0; i < channels; i++) {
1049 positiveChannelPaths[i].moveTo(0, channelHeight*i + channelHeight / 2);
1050 negativeChannelPaths[i].moveTo(0, channelHeight*i + channelHeight / 2);
1053 for (int samples = 0; samples <= 100; samples++) {
1054 double frame = (double)(samples + startCache - 0) / pixelForOneFrame;
1055 int sample = (int)((frame - (int)(frame)) * 20); // AUDIO_FRAME_SIZE
1056 if (frame < 0 || sample < 0 || sample > 19)
1058 QMap<int, QByteArray> frame_channel_data = baseClip()->m_audioFrameCache[(int)frame];
1060 for (int channel = 0; channel < channels && frame_channel_data[channel].size() > 0; channel++) {
1062 int y = channelHeight * channel + channelHeight / 2;
1063 int delta = (int)(frame_channel_data[channel][sample] - 127 / 2) * channelHeight / 64;
1065 positiveChannelPaths[channel].lineTo(samples, 0.1 + y + qAbs(delta));
1066 negativeChannelPaths[channel].lineTo(samples, 0.1 + y - qAbs(delta));
1068 positiveChannelPaths[channel].lineTo(samples, 0.1 + y + delta);
1069 negativeChannelPaths[channel].lineTo(samples, 0.1 + y - delta);
1072 for (int channel = 0; channel < channels ; channel++)
1073 if (fullAreaDraw && samples == 100) {
1074 positiveChannelPaths[channel].lineTo(samples, channelHeight*channel + channelHeight / 2);
1075 negativeChannelPaths[channel].lineTo(samples, channelHeight*channel + channelHeight / 2);
1076 positiveChannelPaths[channel].lineTo(0, channelHeight*channel + channelHeight / 2);
1077 negativeChannelPaths[channel].lineTo(0, channelHeight*channel + channelHeight / 2);
1081 pixpainter.setPen(QPen(QColor(0, 0, 0)));
1082 pixpainter.setBrush(QBrush(QColor(60, 60, 60)));
1084 for (int i = 0; i < channels; i++) {
1086 //pixpainter.fillPath(positiveChannelPaths[i].united(negativeChannelPaths[i]),QBrush(Qt::SolidPattern));//or singleif looks better
1087 pixpainter.drawPath(positiveChannelPaths[i].united(negativeChannelPaths[i]));//or singleif looks better
1089 pixpainter.drawPath(positiveChannelPaths[i]);
1092 //audioThumbWasDrawn=true;
1093 m_framePixelWidth = pixelForOneFrame;
1098 int ClipItem::fadeIn() const
1103 int ClipItem::fadeOut() const
1109 void ClipItem::setFadeIn(int pos)
1111 if (pos == m_startFade) return;
1112 int oldIn = m_startFade;
1113 if (pos < 0) pos = 0;
1114 if (pos > cropDuration().frames(m_fps)) pos = (int)(cropDuration().frames(m_fps));
1116 QRectF rect = boundingRect();
1117 update(rect.x(), rect.y(), qMax(oldIn, pos), rect.height());
1120 void ClipItem::setFadeOut(int pos)
1122 if (pos == m_endFade) return;
1123 int oldOut = m_endFade;
1124 if (pos < 0) pos = 0;
1125 if (pos > cropDuration().frames(m_fps)) pos = (int)(cropDuration().frames(m_fps));
1127 QRectF rect = boundingRect();
1128 update(rect.x() + rect.width() - qMax(oldOut, pos), rect.y(), qMax(oldOut, pos), rect.height());
1132 void ClipItem::setFades(int in, int out)
1140 void ClipItem::hoverEnterEvent(QGraphicsSceneHoverEvent *e)
1142 //if (e->pos().x() < 20) m_hover = true;
1144 if (isItemLocked()) return;
1146 QRectF r = boundingRect();
1147 double width = 35 / projectScene()->scale().x();
1148 double height = r.height() / 2;
1149 //WARNING: seems like it generates a full repaint of the clip, maybe not so good...
1150 update(r.x(), r.y() + height, width, height);
1151 update(r.right() - width, r.y() + height, width, height);
1155 void ClipItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *)
1157 if (isItemLocked()) return;
1159 QRectF r = boundingRect();
1160 double width = 35 / projectScene()->scale().x();
1161 double height = r.height() / 2;
1162 //WARNING: seems like it generates a full repaint of the clip, maybe not so good...
1163 update(r.x(), r.y() + height, width, height);
1164 update(r.right() - width, r.y() + height, width, height);
1168 void ClipItem::resizeStart(int posx, bool /*size*/)
1170 bool sizeLimit = false;
1171 if (clipType() != IMAGE && clipType() != COLOR && clipType() != TEXT) {
1172 const int min = (startPos() - cropStart()).frames(m_fps);
1173 if (posx < min) posx = min;
1177 if (posx == startPos().frames(m_fps)) return;
1178 const int previous = cropStart().frames(m_fps);
1179 AbstractClipItem::resizeStart(posx, sizeLimit);
1181 // set speed independant info
1182 m_speedIndependantInfo = m_info;
1183 m_speedIndependantInfo.cropStart = GenTime((int)(m_info.cropStart.frames(m_fps) * qAbs(m_speed)), m_fps);
1184 m_speedIndependantInfo.cropDuration = GenTime((int)(m_info.cropDuration.frames(m_fps) * qAbs(m_speed)), m_fps);
1186 if ((int) cropStart().frames(m_fps) != previous) {
1187 if (m_hasThumbs && KdenliveSettings::videothumbnails()) {
1188 m_startThumbTimer.start(150);
1193 void ClipItem::resizeEnd(int posx)
1195 const int max = (startPos() - cropStart() + maxDuration()).frames(m_fps);
1196 if (posx > max && maxDuration() != GenTime()) posx = max;
1197 if (posx == endPos().frames(m_fps)) return;
1198 //kDebug() << "// NEW POS: " << posx << ", OLD END: " << endPos().frames(m_fps);
1199 const int previous = cropDuration().frames(m_fps);
1200 AbstractClipItem::resizeEnd(posx);
1202 // set speed independant info
1203 m_speedIndependantInfo = m_info;
1204 m_speedIndependantInfo.cropStart = GenTime((int)(m_info.cropStart.frames(m_fps) * qAbs(m_speed)), m_fps);
1205 m_speedIndependantInfo.cropDuration = GenTime((int)(m_info.cropDuration.frames(m_fps) * qAbs(m_speed)), m_fps);
1207 if ((int) cropDuration().frames(m_fps) != previous) {
1208 if (m_hasThumbs && KdenliveSettings::videothumbnails()) {
1209 m_endThumbTimer.start(150);
1215 bool ClipItem::checkEffectsKeyframesPos(const int previous, const int current, bool fromStart)
1217 bool effModified = false;
1218 for (int i = 0; i < m_effectList.count(); i++) {
1219 QDomElement effect = m_effectList.at(i);
1220 QDomNodeList params = effect.elementsByTagName("parameter");
1221 for (int j = 0; j < params.count(); j++) {
1222 bool modified = false;
1223 QDomElement e = params.item(j).toElement();
1226 if (e.attribute("type") == "keyframe" || e.attribute("type") == "simplekeyframe") {
1227 // parse keyframes and adjust values
1228 const QStringList keyframes = e.attribute("keyframes").split(';', QString::SkipEmptyParts);
1229 QMap <int, double> kfr;
1232 foreach(const QString &str, keyframes) {
1233 pos = str.section(':', 0, 0).toInt();
1234 val = str.section(':', 1, 1).toDouble();
1235 if (pos == previous) {
1236 // first or last keyframe
1240 if ((fromStart && pos >= current) || (!fromStart && pos <= current)) {
1241 // only keyframes in range
1250 QMap<int, double>::const_iterator k = kfr.constBegin();
1251 while (k != kfr.constEnd()) {
1252 newkfr.append(QString::number(k.key()) + ':' + QString::number(k.value()) + ';');
1255 e.setAttribute("keyframes", newkfr);
1260 if (effModified && m_selectedEffect >= 0) setSelectedEffect(m_selectedEffect);
1265 QVariant ClipItem::itemChange(GraphicsItemChange change, const QVariant &value)
1267 if (change == QGraphicsItem::ItemSelectedChange) {
1268 if (value.toBool()) setZValue(10);
1271 if (change == ItemPositionChange && scene()) {
1272 // calculate new position.
1273 //if (parentItem()) return pos();
1274 QPointF newPos = value.toPointF();
1275 //kDebug() << "/// MOVING CLIP ITEM.------------\n++++++++++";
1276 int xpos = projectScene()->getSnapPointForPos((int) newPos.x(), KdenliveSettings::snaptopoints());
1277 xpos = qMax(xpos, 0);
1279 int newTrack = newPos.y() / KdenliveSettings::trackheight();
1280 newTrack = qMin(newTrack, projectScene()->tracksCount() - 1);
1281 newTrack = qMax(newTrack, 0);
1282 newPos.setY((int)(newTrack * KdenliveSettings::trackheight() + 1));
1283 // Only one clip is moving
1284 QRectF sceneShape = rect();
1285 sceneShape.translate(newPos);
1286 QList<QGraphicsItem*> items;
1287 if (projectScene()->editMode() == NORMALEDIT)
1288 items = scene()->items(sceneShape, Qt::IntersectsItemShape);
1289 items.removeAll(this);
1290 bool forwardMove = newPos.x() > pos().x();
1292 if (!items.isEmpty()) {
1293 for (int i = 0; i < items.count(); i++) {
1294 if (!items.at(i)->isEnabled()) continue;
1295 if (items.at(i)->type() == type()) {
1297 QPointF otherPos = items.at(i)->pos();
1298 if ((int) otherPos.y() != (int) pos().y()) {
1302 offset = qMax(offset, (int)(newPos.x() - (static_cast < AbstractClipItem* >(items.at(i))->startPos() - cropDuration()).frames(m_fps)));
1304 offset = qMax(offset, (int)((static_cast < AbstractClipItem* >(items.at(i))->endPos().frames(m_fps)) - newPos.x()));
1309 sceneShape.translate(QPointF(-offset, 0));
1310 newPos.setX(newPos.x() - offset);
1312 sceneShape.translate(QPointF(offset, 0));
1313 newPos.setX(newPos.x() + offset);
1315 QList<QGraphicsItem*> subitems = scene()->items(sceneShape, Qt::IntersectsItemShape);
1316 subitems.removeAll(this);
1317 for (int j = 0; j < subitems.count(); j++) {
1318 if (!subitems.at(j)->isEnabled()) continue;
1319 if (subitems.at(j)->type() == type()) {
1320 // move was not successful, revert to previous pos
1321 m_info.startPos = GenTime((int) pos().x(), m_fps);
1327 m_info.track = newTrack;
1328 m_info.startPos = GenTime((int) newPos.x(), m_fps);
1334 m_info.track = newTrack;
1335 m_info.startPos = GenTime((int) newPos.x(), m_fps);
1336 //kDebug()<<"// ITEM NEW POS: "<<newPos.x()<<", mapped: "<<mapToScene(newPos.x(), 0).x();
1339 return QGraphicsItem::itemChange(change, value);
1343 /*void ClipItem::mouseMoveEvent(QGraphicsSceneMouseEvent * event) {
1346 int ClipItem::effectsCounter()
1348 return effectsCount() + 1;
1351 int ClipItem::effectsCount()
1353 return m_effectList.count();
1356 int ClipItem::hasEffect(const QString &tag, const QString &id) const
1358 return m_effectList.hasEffect(tag, id);
1361 QStringList ClipItem::effectNames()
1363 return m_effectList.effectNames();
1366 QDomElement ClipItem::effectAt(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).cloneNode().toElement();
1372 QDomElement ClipItem::getEffectAt(int ix) const
1374 if (ix > m_effectList.count() - 1 || ix < 0 || m_effectList.at(ix).isNull()) return QDomElement();
1375 return m_effectList.at(ix);
1378 void ClipItem::setEffectAt(int ix, QDomElement effect)
1380 if (ix < 0 || ix > (m_effectList.count() - 1) || effect.isNull()) {
1381 kDebug() << "Invalid effect index: " << ix;
1384 //kDebug() << "CHange EFFECT AT: " << ix << ", CURR: " << m_effectList.at(ix).attribute("tag") << ", NEW: " << effect.attribute("tag");
1385 effect.setAttribute("kdenlive_ix", ix + 1);
1386 m_effectList.replace(ix, effect);
1387 m_effectNames = m_effectList.effectNames().join(" / ");
1388 QString id = effect.attribute("id");
1389 if (id == "fadein" || id == "fadeout" || id == "fade_from_black" || id == "fade_to_black")
1392 QRectF r = boundingRect();
1398 EffectsParameterList ClipItem::addEffect(const QDomElement effect, bool /*animate*/)
1400 bool needRepaint = false;
1402 if (!effect.hasAttribute("kdenlive_ix")) {
1403 ix = effectsCounter();
1404 } else ix = effect.attribute("kdenlive_ix").toInt();
1405 if (!m_effectList.isEmpty() && ix <= m_effectList.count()) {
1407 m_effectList.insert(ix - 1, effect);
1408 for (int i = ix; i < m_effectList.count(); i++) {
1409 int index = m_effectList.item(i).attribute("kdenlive_ix").toInt();
1410 if (index >= ix) m_effectList.item(i).setAttribute("kdenlive_ix", index + 1);
1412 } else m_effectList.append(effect);
1413 EffectsParameterList parameters;
1414 parameters.addParam("tag", effect.attribute("tag"));
1415 parameters.addParam("kdenlive_ix", effect.attribute("kdenlive_ix"));
1416 if (effect.hasAttribute("src")) parameters.addParam("src", effect.attribute("src"));
1417 if (effect.hasAttribute("disable")) parameters.addParam("disable", effect.attribute("disable"));
1419 QString effectId = effect.attribute("id");
1420 if (effectId.isEmpty()) effectId = effect.attribute("tag");
1421 parameters.addParam("id", effectId);
1423 // special case:Â the affine effect needs in / out points
1424 if (effectId == "pan_zoom") {
1425 parameters.addParam("in", QString::number(cropStart().frames(m_fps)));
1426 parameters.addParam("out", QString::number((cropStart() + cropDuration()).frames(m_fps)));
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 void ClipItem::deleteEffect(QString index)
1530 bool needRepaint = false;
1533 for (int i = 0; i < m_effectList.count(); ++i) {
1534 ix = m_effectList.at(i).attribute("kdenlive_ix");
1536 QString effectId = m_effectList.at(i).attribute("id");
1537 if ((effectId == "fadein" && hasEffect(QString(), "fade_from_black") == -1) ||
1538 (effectId == "fade_from_black" && hasEffect(QString(), "fadein") == -1)) {
1541 } else if ((effectId == "fadeout" && hasEffect(QString(), "fade_to_black") == -1) ||
1542 (effectId == "fade_to_black" && hasEffect(QString(), "fadeout") == -1)) {
1545 } else if (EffectsList::hasKeyFrames(m_effectList.at(i))) needRepaint = true;
1546 m_effectList.removeAt(i);
1548 } else if (ix.toInt() > index.toInt()) {
1549 m_effectList.item(i).setAttribute("kdenlive_ix", ix.toInt() - 1);
1552 m_effectNames = m_effectList.effectNames().join(" / ");
1554 if (m_effectList.isEmpty() || m_selectedEffect + 1 == index.toInt()) {
1555 // Current effect was removed
1556 if (index.toInt() > m_effectList.count() - 1) {
1557 setSelectedEffect(m_effectList.count() - 1);
1558 } else setSelectedEffect(index.toInt());
1560 if (needRepaint) update(boundingRect());
1562 QRectF r = boundingRect();
1566 //if (!m_effectList.isEmpty()) flashClip();
1569 double ClipItem::speed() const
1574 int ClipItem::strobe() const
1579 void ClipItem::setSpeed(const double speed, const int strobe)
1582 if (m_speed <= 0 && m_speed > -1)
1585 if (m_speed == 1.0) m_clipName = baseClip()->name();
1586 else m_clipName = baseClip()->name() + " - " + QString::number(speed * 100, 'f', 0) + '%';
1587 m_info.cropStart = GenTime((int)(m_speedIndependantInfo.cropStart.frames(m_fps) / qAbs(m_speed) + 0.5), m_fps);
1588 m_info.cropDuration = GenTime((int)(m_speedIndependantInfo.cropDuration.frames(m_fps) / qAbs(m_speed) + 0.5), m_fps);
1592 GenTime ClipItem::maxDuration() const
1594 return GenTime((int)(m_maxDuration.frames(m_fps) / qAbs(m_speed) + 0.5), m_fps);
1597 GenTime ClipItem::speedIndependantCropStart() const
1599 return m_speedIndependantInfo.cropStart;
1602 GenTime ClipItem::speedIndependantCropDuration() const
1604 return m_speedIndependantInfo.cropDuration;
1608 const ItemInfo ClipItem::speedIndependantInfo() const
1610 return m_speedIndependantInfo;
1614 void ClipItem::dropEvent(QGraphicsSceneDragDropEvent * event)
1616 const QString effects = QString(event->mimeData()->data("kdenlive/effectslist"));
1618 doc.setContent(effects, true);
1619 const QDomElement e = doc.documentElement();
1620 if (scene() && !scene()->views().isEmpty()) {
1622 CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
1623 if (view) view->slotAddEffect(e, m_info.startPos, track());
1628 void ClipItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
1630 if (isItemLocked()) event->setAccepted(false);
1631 else event->setAccepted(event->mimeData()->hasFormat("kdenlive/effectslist"));
1634 void ClipItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
1639 void ClipItem::addTransition(Transition* t)
1641 m_transitionsList.append(t);
1642 //CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
1644 QDomElement e = doc.documentElement();
1645 //if (view) view->slotAddTransition(this, t->toXML() , t->startPos(), track());
1648 void ClipItem::setVideoOnly(bool force)
1650 m_videoOnly = force;
1653 void ClipItem::setAudioOnly(bool force)
1655 m_audioOnly = force;
1656 if (m_audioOnly) m_baseColor = QColor(141, 215, 166);
1658 if (m_clipType == COLOR) {
1659 QString colour = m_clip->getProperty("colour");
1660 colour = colour.replace(0, 2, "#");
1661 m_baseColor = QColor(colour.left(7));
1662 } else if (m_clipType == AUDIO) m_baseColor = QColor(141, 215, 166);
1663 else m_baseColor = QColor(141, 166, 215);
1665 m_audioThumbCachePic.clear();
1668 bool ClipItem::isAudioOnly() const
1673 bool ClipItem::isVideoOnly() const
1678 void ClipItem::insertKeyframe(QDomElement effect, int pos, int val)
1680 if (effect.attribute("disable") == "1") return;
1681 effect.setAttribute("active_keyframe", pos);
1682 m_editedKeyframe = pos;
1683 QDomNodeList params = effect.elementsByTagName("parameter");
1684 for (int i = 0; i < params.count(); i++) {
1685 QDomElement e = params.item(i).toElement();
1686 QString kfr = e.attribute("keyframes");
1687 const QStringList keyframes = kfr.split(';', QString::SkipEmptyParts);
1690 foreach(const QString &str, keyframes) {
1691 int kpos = str.section(':', 0, 0).toInt();
1692 double newval = str.section(':', 1, 1).toDouble();
1695 } else if (!added) {
1696 if (i == 0) newkfr.append(QString::number(pos) + ":" + QString::number(val));
1697 else newkfr.append(QString::number(pos) + ":" + QString::number(newval));
1698 if (kpos > pos) newkfr.append(str);
1700 } else newkfr.append(str);
1702 if (!added) newkfr.append(QString::number(pos) + ":" + QString::number(val));
1703 e.setAttribute("keyframes", newkfr.join(";"));
1707 void ClipItem::movedKeyframe(QDomElement effect, int oldpos, int newpos, double value)
1709 if (effect.attribute("disable") == "1") return;
1710 effect.setAttribute("active_keyframe", newpos);
1711 QDomNodeList params = effect.elementsByTagName("parameter");
1712 int start = cropStart().frames(m_fps);
1713 int end = (cropStart() + cropDuration()).frames(m_fps) - 1;
1714 for (int i = 0; i < params.count(); i++) {
1715 QDomElement e = params.item(i).toElement();
1716 QString kfr = e.attribute("keyframes");
1717 const QStringList keyframes = kfr.split(';', QString::SkipEmptyParts);
1719 foreach(const QString &str, keyframes) {
1720 if (str.section(':', 0, 0).toInt() != oldpos) {
1722 } else if (newpos != -1) {
1723 newpos = qMax(newpos, start);
1724 newpos = qMin(newpos, end);
1725 if (i == 0) newkfr.append(QString::number(newpos) + ":" + QString::number(value));
1726 else newkfr.append(QString::number(newpos) + ":" + str.section(':', 1, 1));
1729 e.setAttribute("keyframes", newkfr.join(";"));
1732 updateKeyframes(effect);
1736 void ClipItem::updateKeyframes(QDomElement effect)
1738 m_keyframes.clear();
1740 QDomNodeList params = effect.elementsByTagName("parameter");
1741 QDomElement e = params.item(0).toElement();
1742 const QStringList keyframes = e.attribute("keyframes").split(';', QString::SkipEmptyParts);
1743 foreach(const QString &str, keyframes) {
1744 int pos = str.section(':', 0, 0).toInt();
1745 double val = str.section(':', 1, 1).toDouble();
1746 m_keyframes[pos] = val;
1748 if (!m_keyframes.contains(m_selectedKeyframe)) m_selectedKeyframe = -1;
1751 void ClipItem::doGetIntraThumbs(QPainter *painter, const QPointF startPos, int offset, int start, int end)
1753 if (!m_clip->thumbProducer() || clipType() == COLOR) return;
1754 if (scene() && scene()->views().isEmpty()) return;
1755 CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
1756 if (view == NULL) return;
1757 const int theight = KdenliveSettings::trackheight();
1758 const int twidth = FRAME_SIZE;
1760 if (clipType() == IMAGE || clipType() == TEXT) {
1761 for (int i = start; i <= end; i++)
1762 painter->drawPixmap(startPos + QPointF(twidth *(i - offset), 0), m_startPix);
1765 for (int i = start; i <= end; i++) {
1766 if (!view->pixmapCache->find(m_clip->fileURL().path() + "%" + QString::number(i), p)) {
1767 p = m_clip->thumbProducer()->extractImage(i, twidth, theight);
1768 view->pixmapCache->insert(m_clip->fileURL().path() + "%" + QString::number(i), p);
1770 painter->drawPixmap(startPos + QPointF(twidth *(i - offset), 0), p);
1774 QList <int> ClipItem::updatePanZoom(int width, int height, int cut)
1776 QList <int> effectPositions;
1777 for (int i = 0; i < m_effectList.count(); i++) {
1778 QDomElement effect = m_effectList.at(i);
1779 QDomNodeList params = effect.elementsByTagName("parameter");
1780 for (int j = 0; j < params.count(); j++) {
1781 QDomElement e = params.item(j).toElement();
1784 if (e.attribute("type") == "geometry" && !e.hasAttribute("fixed")) {
1785 effectPositions << i;
1787 int in = cropStart().frames(fps());
1788 int out = in + cropDuration().frames(fps());
1789 int dur = out - in - 1;
1791 effect.setAttribute("in", in);
1792 effect.setAttribute("out", out);
1794 Mlt::Geometry geometry(e.attribute("value").toUtf8().data(), dur, width, height);
1795 Mlt::GeometryItem item;
1796 bool endFrameAdded = false;
1798 while (!geometry.next_key(&item, dur)) {
1799 if (!endFrameAdded) {
1800 // add keyframe at the end with interpolated value
1803 endFrameAdded = true;
1805 Mlt::GeometryItem endItem;
1806 Mlt::GeometryItem interp;
1807 geometry.fetch(&interp, dur - 1);
1808 endItem.frame(dur - 1);
1809 endItem.x(interp.x());
1810 endItem.y(interp.y());
1811 endItem.w(interp.w());
1812 endItem.h(interp.h());
1813 endItem.mix(interp.mix());
1814 geometry.insert(&endItem);
1816 geometry.remove(item.frame());
1819 Mlt::Geometry origGeometry(e.attribute("value").toUtf8().data(), dur, width, height);
1820 // remove keyframes before cut point
1821 while (!geometry.prev_key(&item, cut - 1) && item.frame() < cut)
1822 geometry.remove(item.frame());
1824 // add a keyframe at new pos 0
1825 origGeometry.fetch(&item, cut);
1827 geometry.insert(&item);
1829 // move exisiting keyframes by -cut
1830 while (!origGeometry.next_key(&item, cut)) {
1831 geometry.remove(item.frame());
1832 origGeometry.remove(item.frame());
1833 item.frame(item.frame() - cut);
1834 geometry.insert(&item);
1839 e.setAttribute("value", geometry.serialise());
1844 return effectPositions;
1847 Mlt::Producer *ClipItem::getProducer(int track, bool trackSpecific)
1850 return m_clip->audioProducer(track);
1851 else if (isVideoOnly())
1852 return m_clip->videoProducer();
1854 return m_clip->producer(trackSpecific ? track : -1);
1857 #include "clipitem.moc"