1 /***************************************************************************
2 * Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org) *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
22 #include "customtrackview.h"
23 #include "customtrackscene.h"
25 #include "docclipbase.h"
26 #include "transition.h"
27 #include "kdenlivesettings.h"
29 #include "profilesdialog.h"
36 #include <QStyleOptionGraphicsItem>
37 #include <QGraphicsScene>
40 ClipItem::ClipItem(DocClipBase *clip, ItemInfo info, double fps, double speed, int strobe, bool generateThumbs) :
41 AbstractClipItem(info, QRectF(), fps),
47 m_startPix(QPixmap()),
52 m_startThumbRequested(false),
53 m_endThumbRequested(false),
60 setRect(0, 0, (info.endPos - info.startPos).frames(fps) - 0.02, (double)(KdenliveSettings::trackheight() - 2));
61 setPos(info.startPos.frames(fps), (double)(info.track * KdenliveSettings::trackheight()) + 1);
63 // set speed independant info
64 if (m_speed <= 0 && m_speed > -1) m_speed = 1.0;
65 m_speedIndependantInfo = m_info;
66 m_speedIndependantInfo.cropStart = GenTime((int)(m_info.cropStart.frames(m_fps) * qAbs(m_speed)), m_fps);
67 m_speedIndependantInfo.cropDuration = GenTime((int)(m_info.cropDuration.frames(m_fps) * qAbs(m_speed)), m_fps);
69 m_videoPix = KIcon("kdenlive-show-video").pixmap(QSize(16, 16));
70 m_audioPix = KIcon("kdenlive-show-audio").pixmap(QSize(16, 16));
72 if (m_speed == 1.0) m_clipName = m_clip->name();
74 m_clipName = m_clip->name() + " - " + QString::number(m_speed * 100, 'f', 0) + '%';
76 m_producer = m_clip->getId();
77 m_clipType = m_clip->clipType();
78 //m_cropStart = info.cropStart;
79 m_maxDuration = m_clip->maxDuration();
81 m_audioThumbReady = m_clip->audioThumbCreated();
82 //setAcceptsHoverEvents(true);
83 connect(this , SIGNAL(prepareAudioThumb(double, int, int, int)) , this, SLOT(slotPrepareAudioThumb(double, int, int, int)));
85 if (m_clipType == VIDEO || m_clipType == AV || m_clipType == SLIDESHOW || m_clipType == PLAYLIST) {
86 m_baseColor = QColor(141, 166, 215);
87 if (!m_clip->isPlaceHolder()) {
89 m_startThumbTimer.setSingleShot(true);
90 connect(&m_startThumbTimer, SIGNAL(timeout()), this, SLOT(slotGetStartThumb()));
91 m_endThumbTimer.setSingleShot(true);
92 connect(&m_endThumbTimer, SIGNAL(timeout()), this, SLOT(slotGetEndThumb()));
94 connect(this, SIGNAL(getThumb(int, int)), m_clip->thumbProducer(), SLOT(extractImage(int, int)));
96 connect(m_clip->thumbProducer(), SIGNAL(thumbReady(int, QImage)), this, SLOT(slotThumbReady(int, QImage)));
97 connect(m_clip, SIGNAL(gotAudioData()), this, SLOT(slotGotAudioData()));
98 if (generateThumbs) QTimer::singleShot(200, this, SLOT(slotFetchThumbs()));
101 } else if (m_clipType == COLOR) {
102 QString colour = m_clip->getProperty("colour");
103 colour = colour.replace(0, 2, "#");
104 m_baseColor = QColor(colour.left(7));
105 } else if (m_clipType == IMAGE || m_clipType == TEXT) {
106 m_baseColor = QColor(141, 166, 215);
107 if (m_clipType == TEXT) {
108 connect(this, SIGNAL(getThumb(int, int)), m_clip->thumbProducer(), SLOT(extractImage(int, int)));
109 connect(m_clip->thumbProducer(), SIGNAL(thumbReady(int, QImage)), this, SLOT(slotThumbReady(int, QImage)));
111 //m_startPix = KThumb::getImage(KUrl(clip->getProperty("resource")), (int)(KdenliveSettings::trackheight() * KdenliveSettings::project_display_ratio()), KdenliveSettings::trackheight());
112 } else if (m_clipType == AUDIO) {
113 m_baseColor = QColor(141, 215, 166);
114 connect(m_clip, SIGNAL(gotAudioData()), this, SLOT(slotGotAudioData()));
119 ClipItem::~ClipItem()
122 if (scene()) scene()->removeItem(this);
123 if (m_clipType == VIDEO || m_clipType == AV || m_clipType == SLIDESHOW || m_clipType == PLAYLIST) {
124 //disconnect(m_clip->thumbProducer(), SIGNAL(thumbReady(int, QImage)), this, SLOT(slotThumbReady(int, QImage)));
125 //disconnect(m_clip, SIGNAL(gotAudioData()), this, SLOT(slotGotAudioData()));
130 ClipItem *ClipItem::clone(ItemInfo info) const
132 ClipItem *duplicate = new ClipItem(m_clip, info, m_fps, m_speed, m_strobe);
133 if (m_clipType == IMAGE || m_clipType == TEXT) duplicate->slotSetStartThumb(m_startPix);
134 else if (m_clipType != COLOR) {
135 if (info.cropStart == m_info.cropStart) duplicate->slotSetStartThumb(m_startPix);
136 if (info.cropStart + (info.endPos - info.startPos) == m_info.cropStart + (m_info.endPos - m_info.startPos)) duplicate->slotSetEndThumb(m_endPix);
138 //kDebug() << "// CLoning clip: " << (info.cropStart + (info.endPos - info.startPos)).frames(m_fps) << ", CURRENT end: " << (cropStart() + duration()).frames(m_fps);
139 duplicate->setEffectList(m_effectList);
140 duplicate->setVideoOnly(m_videoOnly);
141 duplicate->setAudioOnly(m_audioOnly);
142 duplicate->setFades(fadeIn(), fadeOut());
143 //duplicate->setSpeed(m_speed);
147 void ClipItem::setEffectList(const EffectsList effectList)
149 m_effectList.clone(effectList);
150 m_effectNames = m_effectList.effectNames().join(" / ");
151 if (!m_effectList.isEmpty()) {
152 for (int i = 0; i < m_effectList.count(); i++) {
153 QString effectId = m_effectList.item(i).attribute("id");
154 // check if it is a fade effect
155 QDomNodeList params = m_effectList.item(i).elementsByTagName("parameter");
157 for (int j = 0; j < params.count(); j++) {
158 QDomElement e = params.item(j).toElement();
160 if (effectId == "fadein") {
161 if (m_effectList.hasEffect(QString(), "fade_from_black") == -1) {
162 if (e.attribute("name") == "out") fade += e.attribute("value").toInt();
163 else if (e.attribute("name") == "in") fade -= e.attribute("value").toInt();
165 QDomElement fadein = m_effectList.getEffectByTag(QString(), "fade_from_black");
166 if (fadein.attribute("name") == "out") fade += fadein.attribute("value").toInt();
167 else if (fadein.attribute("name") == "in") fade -= fadein.attribute("value").toInt();
169 } else if (effectId == "fade_from_black") {
170 if (m_effectList.hasEffect(QString(), "fadein") == -1) {
171 if (e.attribute("name") == "out") fade += e.attribute("value").toInt();
172 else if (e.attribute("name") == "in") fade -= e.attribute("value").toInt();
174 QDomElement fadein = m_effectList.getEffectByTag(QString(), "fadein");
175 if (fadein.attribute("name") == "out") fade += fadein.attribute("value").toInt();
176 else if (fadein.attribute("name") == "in") fade -= fadein.attribute("value").toInt();
178 } else if (effectId == "fadeout") {
179 if (m_effectList.hasEffect(QString(), "fade_to_black") == -1) {
180 if (e.attribute("name") == "out") fade -= e.attribute("value").toInt();
181 else if (e.attribute("name") == "in") fade += e.attribute("value").toInt();
183 QDomElement fadeout = m_effectList.getEffectByTag(QString(), "fade_to_black");
184 if (fadeout.attribute("name") == "out") fade -= fadeout.attribute("value").toInt();
185 else if (fadeout.attribute("name") == "in") fade += fadeout.attribute("value").toInt();
187 } else if (effectId == "fade_to_black") {
188 if (m_effectList.hasEffect(QString(), "fadeout") == -1) {
189 if (e.attribute("name") == "out") fade -= e.attribute("value").toInt();
190 else if (e.attribute("name") == "in") fade += e.attribute("value").toInt();
192 QDomElement fadeout = m_effectList.getEffectByTag(QString(), "fadeout");
193 if (fadeout.attribute("name") == "out") fade -= fadeout.attribute("value").toInt();
194 else if (fadeout.attribute("name") == "in") fade += fadeout.attribute("value").toInt();
199 if (fade > 0) m_startFade = fade;
200 else if (fade < 0) m_endFade = -fade;
202 setSelectedEffect(0);
206 const EffectsList ClipItem::effectList() const
211 int ClipItem::selectedEffectIndex() const
213 return m_selectedEffect;
216 void ClipItem::initEffect(QDomElement effect, int diff)
218 // the kdenlive_ix int is used to identify an effect in mlt's playlist, should
220 if (effect.attribute("kdenlive_ix").toInt() == 0)
221 effect.setAttribute("kdenlive_ix", QString::number(effectsCounter()));
223 if (effect.attribute("id") == "freeze" && diff > 0) {
224 EffectsList::setParameter(effect, "frame", QString::number(diff));
227 // Init parameter value & keyframes if required
228 QDomNodeList params = effect.elementsByTagName("parameter");
229 for (int i = 0; i < params.count(); i++) {
230 QDomElement e = params.item(i).toElement();
231 kDebug() << "// init eff: " << e.attribute("name");
233 // Check if this effect has a variable parameter
234 if (e.attribute("default").startsWith('%')) {
235 double evaluatedValue = ProfilesDialog::getStringEval(projectScene()->profile(), e.attribute("default"));
236 e.setAttribute("default", evaluatedValue);
237 if (e.hasAttribute("value") && e.attribute("value").startsWith('%')) {
238 e.setAttribute("value", evaluatedValue);
242 if (!e.isNull() && (e.attribute("type") == "keyframe" || e.attribute("type") == "simplekeyframe")) {
243 QString def = e.attribute("default");
244 // Effect has a keyframe type parameter, we need to set the values
245 if (e.attribute("keyframes").isEmpty()) {
246 e.setAttribute("keyframes", QString::number(cropStart().frames(m_fps)) + ':' + def + ';' + QString::number((cropStart() + cropDuration()).frames(m_fps) - 1) + ':' + def);
247 kDebug() << "///// EFFECT KEYFRAMES INITED: " << e.attribute("keyframes");
252 if (effect.attribute("tag") == "volume" || effect.attribute("tag") == "brightness") {
253 if (effect.attribute("id") == "fadeout" || effect.attribute("id") == "fade_to_black") {
254 int end = (cropDuration() + cropStart()).frames(m_fps);
256 if (effect.attribute("id") == "fadeout") {
257 if (m_effectList.hasEffect(QString(), "fade_to_black") == -1) {
258 int effectDuration = EffectsList::parameter(effect, "in").toInt();
259 if (effectDuration > cropDuration().frames(m_fps)) {
260 effectDuration = cropDuration().frames(m_fps) / 2;
262 start -= effectDuration;
264 QDomElement fadeout = m_effectList.getEffectByTag(QString(), "fade_to_black");
265 start -= EffectsList::parameter(fadeout, "out").toInt() - EffectsList::parameter(fadeout, "in").toInt();
267 } else if (effect.attribute("id") == "fade_to_black") {
268 if (m_effectList.hasEffect(QString(), "fadeout") == -1) {
269 int effectDuration = EffectsList::parameter(effect, "in").toInt();
270 if (effectDuration > cropDuration().frames(m_fps)) {
271 effectDuration = cropDuration().frames(m_fps) / 2;
273 start -= effectDuration;
275 QDomElement fadeout = m_effectList.getEffectByTag(QString(), "fadeout");
276 start -= EffectsList::parameter(fadeout, "out").toInt() - EffectsList::parameter(fadeout, "in").toInt();
279 EffectsList::setParameter(effect, "in", QString::number(start));
280 EffectsList::setParameter(effect, "out", QString::number(end));
281 } else if (effect.attribute("id") == "fadein" || effect.attribute("id") == "fade_from_black") {
282 int start = cropStart().frames(m_fps);
284 if (effect.attribute("id") == "fadein") {
285 if (m_effectList.hasEffect(QString(), "fade_from_black") == -1) {
286 int effectDuration = EffectsList::parameter(effect, "out").toInt();
287 if (effectDuration > cropDuration().frames(m_fps)) {
288 effectDuration = cropDuration().frames(m_fps) / 2;
290 end += effectDuration;
292 end += EffectsList::parameter(m_effectList.getEffectByTag(QString(), "fade_from_black"), "out").toInt();
293 } else if (effect.attribute("id") == "fade_from_black") {
294 if (m_effectList.hasEffect(QString(), "fadein") == -1) {
295 int effectDuration = EffectsList::parameter(effect, "out").toInt();
296 if (effectDuration > cropDuration().frames(m_fps)) {
297 effectDuration = cropDuration().frames(m_fps) / 2;
299 end += effectDuration;
301 end += EffectsList::parameter(m_effectList.getEffectByTag(QString(), "fadein"), "out").toInt();
303 EffectsList::setParameter(effect, "in", QString::number(start));
304 EffectsList::setParameter(effect, "out", QString::number(end));
309 bool ClipItem::checkKeyFrames()
311 bool clipEffectsModified = false;
312 for (int ix = 0; ix < m_effectList.count(); ix ++) {
313 QString kfr = keyframes(ix);
314 if (!kfr.isEmpty()) {
315 const QStringList keyframes = kfr.split(';', QString::SkipEmptyParts);
316 QStringList newKeyFrames;
317 bool cutKeyFrame = false;
318 bool modified = false;
320 double lastValue = -1;
321 int start = cropStart().frames(m_fps);
322 int end = (cropStart() + cropDuration()).frames(m_fps);
323 foreach(const QString &str, keyframes) {
324 int pos = str.section(':', 0, 0).toInt();
325 double val = str.section(':', 1, 1).toDouble();
326 if (pos - start < 0) {
327 // a keyframe is defined before the start of the clip
329 } else if (cutKeyFrame) {
330 // create new keyframe at clip start, calculate interpolated value
332 int diff = pos - lastPos;
333 double ratio = (double)(start - lastPos) / diff;
334 double newValue = lastValue + (val - lastValue) * ratio;
335 newKeyFrames.append(QString::number(start) + ':' + QString::number(newValue));
342 // create new keyframe at clip end, calculate interpolated value
343 int diff = pos - lastPos;
345 double ratio = (double)(end - lastPos) / diff;
346 double newValue = lastValue + (val - lastValue) * ratio;
347 newKeyFrames.append(QString::number(end) + ':' + QString::number(newValue));
352 newKeyFrames.append(QString::number(pos) + ':' + QString::number(val));
360 setKeyframes(ix, newKeyFrames.join(";"));
361 clipEffectsModified = true;
365 return clipEffectsModified;
368 void ClipItem::setKeyframes(const int ix, const QString keyframes)
370 QDomElement effect = getEffectAt(ix);
371 if (effect.attribute("disable") == "1") return;
372 QDomNodeList params = effect.elementsByTagName("parameter");
373 for (int i = 0; i < params.count(); i++) {
374 QDomElement e = params.item(i).toElement();
375 if (!e.isNull() && (e.attribute("type") == "keyframe" || e.attribute("type") == "simplekeyframe")) {
376 e.setAttribute("keyframes", keyframes);
377 if (ix == m_selectedEffect) {
379 double max = e.attribute("max").toDouble();
380 double min = e.attribute("min").toDouble();
381 m_keyframeFactor = 100.0 / (max - min);
382 m_keyframeDefault = e.attribute("default").toDouble();
383 m_selectedKeyframe = 0;
385 const QStringList keyframes = e.attribute("keyframes").split(';', QString::SkipEmptyParts);
386 foreach(const QString &str, keyframes) {
387 int pos = str.section(':', 0, 0).toInt();
388 double val = str.section(':', 1, 1).toDouble();
389 m_keyframes[pos] = val;
391 if (m_keyframes.find(m_editedKeyframe) == m_keyframes.end()) m_editedKeyframe = -1;
392 if (m_keyframes.find(m_editedKeyframe) == m_keyframes.end()) m_editedKeyframe = -1;
402 void ClipItem::setSelectedEffect(const int ix)
404 m_selectedEffect = ix;
405 QDomElement effect = effectAt(m_selectedEffect);
406 if (effect.isNull() == false) {
407 QDomNodeList params = effect.elementsByTagName("parameter");
408 if (effect.attribute("disable") != "1")
409 for (int i = 0; i < params.count(); i++) {
410 QDomElement e = params.item(i).toElement();
411 if (!e.isNull() && (e.attribute("type") == "keyframe" || e.attribute("type") == "simplekeyframe")) {
413 double max = e.attribute("max").toDouble();
414 double min = e.attribute("min").toDouble();
415 m_keyframeFactor = 100.0 / (max - min);
416 m_keyframeDefault = e.attribute("default").toDouble();
417 m_selectedKeyframe = 0;
420 const QStringList keyframes = e.attribute("keyframes").split(';', QString::SkipEmptyParts);
421 foreach(const QString &str, keyframes) {
422 int pos = str.section(':', 0, 0).toInt();
423 double val = str.section(':', 1, 1).toDouble();
424 m_keyframes[pos] = val;
426 if (m_keyframes.find(m_editedKeyframe) == m_keyframes.end()) m_editedKeyframe = -1;
432 if (!m_keyframes.isEmpty()) {
438 QString ClipItem::keyframes(const int index)
441 QDomElement effect = effectAt(index);
442 QDomNodeList params = effect.elementsByTagName("parameter");
444 for (int i = 0; i < params.count(); i++) {
445 QDomElement e = params.item(i).toElement();
446 if (!e.isNull() && (e.attribute("type") == "keyframe" || e.attribute("type") == "simplekeyframe")) {
447 result = e.attribute("keyframes");
454 void ClipItem::updateKeyframeEffect()
456 // regenerate xml parameter from the clip keyframes
457 QDomElement effect = getEffectAt(m_selectedEffect);
458 if (effect.attribute("disable") == "1") return;
459 QDomNodeList params = effect.elementsByTagName("parameter");
461 for (int i = 0; i < params.count(); i++) {
462 QDomElement e = params.item(i).toElement();
463 if (!e.isNull() && (e.attribute("type") == "keyframe" || e.attribute("type") == "simplekeyframe")) {
465 if (m_keyframes.count() > 0) {
466 QMap<int, int>::const_iterator i = m_keyframes.constBegin();
467 while (i != m_keyframes.constEnd()) {
468 keyframes.append(QString::number(i.key()) + ':' + QString::number(i.value()) + ';');
472 // Effect has a keyframe type parameter, we need to set the values
473 //kDebug() << "::::::::::::::: SETTING EFFECT KEYFRAMES: " << keyframes;
474 e.setAttribute("keyframes", keyframes);
480 QDomElement ClipItem::selectedEffect()
482 if (m_selectedEffect == -1 || m_effectList.isEmpty()) return QDomElement();
483 return effectAt(m_selectedEffect);
486 void ClipItem::resetThumbs(bool clearExistingThumbs)
488 if (clearExistingThumbs) {
489 m_startPix = QPixmap();
490 m_endPix = QPixmap();
491 m_audioThumbCachePic.clear();
497 void ClipItem::refreshClip(bool checkDuration)
499 if (checkDuration && (m_maxDuration != m_clip->maxDuration())) {
500 m_maxDuration = m_clip->maxDuration();
501 if (m_clipType != IMAGE && m_clipType != TEXT && m_clipType != COLOR) {
502 if (m_maxDuration != GenTime() && m_info.cropStart + m_info.cropDuration > m_maxDuration) {
503 // Clip duration changed, make sure to stay in correct range
504 if (m_info.cropStart > m_maxDuration) {
505 m_info.cropStart = GenTime();
506 m_info.cropDuration = qMin(m_info.cropDuration, m_maxDuration);
507 updateRectGeometry();
509 m_info.cropDuration = m_maxDuration;
510 updateRectGeometry();
515 if (m_clipType == COLOR) {
516 QString colour = m_clip->getProperty("colour");
517 colour = colour.replace(0, 2, "#");
518 m_baseColor = QColor(colour.left(7));
519 } else resetThumbs(checkDuration);
522 void ClipItem::slotFetchThumbs()
524 if (scene() == NULL || m_clipType == AUDIO || m_clipType == COLOR) return;
525 if (m_clipType == IMAGE) {
526 if (m_startPix.isNull()) {
527 m_startPix = KThumb::getImage(KUrl(m_clip->getProperty("resource")), (int)(KdenliveSettings::trackheight() * KdenliveSettings::project_display_ratio()), KdenliveSettings::trackheight());
533 if (m_clipType == TEXT) {
534 if (m_startPix.isNull()) slotGetStartThumb();
538 if (m_endPix.isNull() && m_startPix.isNull()) {
539 m_startThumbRequested = true;
540 m_endThumbRequested = true;
541 emit getThumb((int)m_speedIndependantInfo.cropStart.frames(m_fps), (int)(m_speedIndependantInfo.cropStart + m_speedIndependantInfo.cropDuration).frames(m_fps) - 1);
543 if (m_endPix.isNull()) {
546 if (m_startPix.isNull()) {
552 void ClipItem::slotGetStartThumb()
554 m_startThumbRequested = true;
555 emit getThumb((int)m_speedIndependantInfo.cropStart.frames(m_fps), -1);
558 void ClipItem::slotGetEndThumb()
560 m_endThumbRequested = true;
561 emit getThumb(-1, (int)(m_speedIndependantInfo.cropStart + m_speedIndependantInfo.cropDuration).frames(m_fps) - 1);
565 void ClipItem::slotSetStartThumb(QImage img)
567 if (!img.isNull() && img.format() == QImage::Format_ARGB32) {
568 QPixmap pix = QPixmap::fromImage(img);
570 QRectF r = sceneBoundingRect();
571 r.setRight(pix.width() + 2);
576 void ClipItem::slotSetEndThumb(QImage img)
578 if (!img.isNull() && img.format() == QImage::Format_ARGB32) {
579 QPixmap pix = QPixmap::fromImage(img);
581 QRectF r = sceneBoundingRect();
582 r.setLeft(r.right() - pix.width() - 2);
587 void ClipItem::slotThumbReady(int frame, QImage img)
589 if (scene() == NULL) return;
590 QRectF r = boundingRect();
591 QPixmap pix = QPixmap::fromImage(img);
592 double width = pix.width() / projectScene()->scale().x();
593 if (m_startThumbRequested && frame == m_speedIndependantInfo.cropStart.frames(m_fps)) {
595 m_startThumbRequested = false;
596 update(r.left(), r.top(), width, pix.height());
597 if (m_clipType == IMAGE || m_clipType == TEXT) {
598 update(r.right() - width, r.top(), width, pix.height());
600 } else if (m_endThumbRequested && frame == (m_speedIndependantInfo.cropStart + m_speedIndependantInfo.cropDuration).frames(m_fps) - 1) {
602 m_endThumbRequested = false;
603 update(r.right() - width, r.top(), width, pix.height());
607 void ClipItem::slotSetStartThumb(const QPixmap pix)
612 void ClipItem::slotSetEndThumb(const QPixmap pix)
617 QPixmap ClipItem::startThumb() const
622 QPixmap ClipItem::endThumb() const
627 void ClipItem::slotGotAudioData()
629 m_audioThumbReady = true;
630 if (m_clipType == AV && !isAudioOnly()) {
631 QRectF r = boundingRect();
632 r.setTop(r.top() + r.height() / 2 - 1);
637 int ClipItem::type() const
642 DocClipBase *ClipItem::baseClip() const
647 QDomElement ClipItem::xml() const
649 QDomElement xml = m_clip->toXML();
650 if (m_speed != 1.0) xml.setAttribute("speed", m_speed);
651 if (m_strobe > 1) xml.setAttribute("strobe", m_strobe);
652 if (m_audioOnly) xml.setAttribute("audio_only", 1);
653 else if (m_videoOnly) xml.setAttribute("video_only", 1);
657 QDomElement ClipItem::itemXml() const
659 QDomElement xml = m_clip->toXML();
660 if (m_speed != 1.0) xml.setAttribute("speed", m_speed);
661 if (m_strobe > 1) xml.setAttribute("strobe", m_strobe);
662 if (m_audioOnly) xml.setAttribute("audio_only", 1);
663 else if (m_videoOnly) xml.setAttribute("video_only", 1);
667 int ClipItem::clipType() const
672 QString ClipItem::clipName() const
677 void ClipItem::setClipName(const QString &name)
682 const QString ClipItem::clipProducer() const
687 void ClipItem::flashClip()
689 if (m_timeLine == 0) {
690 m_timeLine = new QTimeLine(750, this);
691 m_timeLine->setUpdateInterval(80);
692 m_timeLine->setCurveShape(QTimeLine::EaseInOutCurve);
693 m_timeLine->setFrameRange(0, 100);
694 connect(m_timeLine, SIGNAL(valueChanged(qreal)), this, SLOT(animate(qreal)));
696 //m_timeLine->start();
699 void ClipItem::animate(qreal /*value*/)
701 QRectF r = boundingRect();
707 void ClipItem::paint(QPainter *painter,
708 const QStyleOptionGraphicsItem *option,
712 if (parentItem()) paintColor = QColor(255, 248, 149);
713 else paintColor = m_baseColor;
714 if (isSelected() || (parentItem() && parentItem()->isSelected())) paintColor = paintColor.darker();
716 painter->setMatrixEnabled(false);
717 const QRectF mapped = painter->matrix().mapRect(rect()).adjusted(0.5, 0, 0.5, 0);
718 const QRectF exposed = option->exposedRect;
719 painter->setClipRect(mapped);
720 painter->fillRect(mapped, paintColor);
723 if (KdenliveSettings::videothumbnails() && !isAudioOnly()) {
724 QPen pen = painter->pen();
725 pen.setColor(QColor(255, 255, 255, 150));
726 painter->setPen(pen);
727 if ((m_clipType == IMAGE || m_clipType == TEXT) && !m_startPix.isNull()) {
728 const QPointF top = mapped.topRight() - QPointF(m_startPix.width() - 1, 0);
729 painter->drawPixmap(top, m_startPix);
730 QLineF l2(top.x(), mapped.top(), top.x(), mapped.bottom());
731 painter->drawLine(l2);
732 } else if (!m_endPix.isNull()) {
733 const QPointF top = mapped.topRight() - QPointF(m_endPix.width() - 1, 0);
734 painter->drawPixmap(top, m_endPix);
735 QLineF l2(top.x(), mapped.top(), top.x(), mapped.bottom());
736 painter->drawLine(l2);
738 if (!m_startPix.isNull()) {
739 painter->drawPixmap(mapped.topLeft(), m_startPix);
740 QLineF l2(mapped.left() + m_startPix.width(), mapped.top(), mapped.left() + m_startPix.width(), mapped.bottom());
741 painter->drawLine(l2);
743 if (painter->matrix().m11() == FRAME_SIZE) {
744 int offset = (m_info.startPos - m_info.cropStart).frames(m_fps);
745 int left = qMax((int) m_info.startPos.frames(m_fps) + 1, (int) mapToScene(exposed.left(), 0).x());
746 int right = qMin((int)(m_info.startPos + m_info.cropDuration).frames(m_fps) - 1, (int) mapToScene(exposed.right(), 0).x());
747 doGetIntraThumbs(painter, mapped.topLeft(), m_info.cropStart.frames(m_fps), left - offset, right - offset);
749 painter->setPen(Qt::black);
752 // draw audio thumbnails
753 if (KdenliveSettings::audiothumbnails() && m_speed == 1.0 && !isVideoOnly() && ((m_clipType == AV && (exposed.bottom() > (rect().height() / 2) || isAudioOnly())) || m_clipType == AUDIO) && m_audioThumbReady) {
755 double startpixel = exposed.left();
758 double endpixel = exposed.right();
761 //kDebug()<<"/// REPAINTING AUDIO THMBS ZONE: "<<startpixel<<"x"<<endpixel;
763 /*QPainterPath path = m_clipType == AV ? roundRectPathLower : resultClipPath;*/
765 if (m_clipType == AV && !isAudioOnly()) {
767 mappedRect.setTop(mappedRect.bottom() - mapped.height() / 2);
768 } else mappedRect = mapped;
770 double scale = painter->matrix().m11();
772 if (isEnabled() && m_clip) channels = m_clip->getProperty("channels").toInt();
773 if (scale != m_framePixelWidth)
774 m_audioThumbCachePic.clear();
775 double cropLeft = m_info.cropStart.frames(m_fps);
776 const int clipStart = mappedRect.x();
777 const int mappedStartPixel = painter->matrix().map(QPointF(startpixel + cropLeft, 0)).x() - clipStart;
778 const int mappedEndPixel = painter->matrix().map(QPointF(endpixel + cropLeft, 0)).x() - clipStart;
779 cropLeft = cropLeft * scale;
783 emit prepareAudioThumb(scale, mappedStartPixel, mappedEndPixel, channels);
786 for (int startCache = mappedStartPixel - (mappedStartPixel) % 100; startCache < mappedEndPixel; startCache += 100) {
787 if (m_audioThumbCachePic.contains(startCache) && !m_audioThumbCachePic[startCache].isNull())
788 painter->drawPixmap(clipStart + startCache - cropLeft, mappedRect.y(), m_audioThumbCachePic[startCache]);
792 // Draw effects names
793 if (!m_effectNames.isEmpty() && mapped.width() > 40) {
794 QRectF txtBounding = painter->boundingRect(mapped, Qt::AlignLeft | Qt::AlignTop, m_effectNames);
796 if (m_timeLine && m_timeLine->state() == QTimeLine::Running) {
797 qreal value = m_timeLine->currentValue();
798 txtBounding.setWidth(txtBounding.width() * value);
799 bgColor.setRgb(50 + 200 *(1.0 - value), 50, 50, 100 + 50 * value);
800 } else bgColor.setRgb(50, 50, 90, 180);
802 QPainterPath rounded;
803 rounded.moveTo(txtBounding.bottomRight());
804 rounded.arcTo(txtBounding.right() - txtBounding.height() - 2, txtBounding.top() - txtBounding.height(), txtBounding.height() * 2, txtBounding.height() * 2, 270, 90);
805 rounded.lineTo(txtBounding.topLeft());
806 rounded.lineTo(txtBounding.bottomLeft());
807 painter->fillPath(rounded, bgColor);
808 painter->setPen(Qt::lightGray);
809 painter->drawText(txtBounding.adjusted(1, 0, 1, 0), Qt::AlignCenter, m_effectNames);
813 QColor frameColor(paintColor.darker());
814 if (isSelected() || (parentItem() && parentItem()->isSelected())) {
815 frameColor = QColor(Qt::red);
817 frameColor.setAlpha(160);
819 const QRectF txtBounding2 = painter->boundingRect(mapped, Qt::AlignHCenter | Qt::AlignVCenter, ' ' + m_clipName + ' ');
820 //painter->fillRect(txtBounding2, frameColor);
821 painter->setBrush(frameColor);
822 painter->setPen(Qt::NoPen);
823 painter->drawRoundedRect(txtBounding2, 3, 3);
824 painter->setBrush(QBrush(Qt::NoBrush));
826 //painter->setPen(QColor(0, 0, 0, 180));
827 //painter->drawText(txtBounding, Qt::AlignCenter, m_clipName);
829 painter->drawPixmap(txtBounding2.topLeft() - QPointF(17, -1), m_videoPix);
830 } else if (m_audioOnly) {
831 painter->drawPixmap(txtBounding2.topLeft() - QPointF(17, -1), m_audioPix);
833 painter->setPen(Qt::white);
834 painter->drawText(txtBounding2, Qt::AlignCenter, m_clipName);
838 if (isEnabled() && m_clip) {
839 QList < CommentedTime > markers = m_clip->commentedSnapMarkers();
840 QList < CommentedTime >::Iterator it = markers.begin();
843 QBrush markerBrush(QColor(120, 120, 0, 140));
844 QPen pen = painter->pen();
845 pen.setColor(QColor(255, 255, 255, 200));
846 pen.setStyle(Qt::DotLine);
848 for (; it != markers.end(); ++it) {
849 pos = GenTime((int)((*it).time().frames(m_fps) / qAbs(m_speed) + 0.5), m_fps) - cropStart();
850 if (pos > GenTime()) {
851 if (pos > cropDuration()) break;
852 QLineF l(rect().x() + pos.frames(m_fps), rect().y(), rect().x() + pos.frames(m_fps), rect().bottom());
853 QLineF l2 = painter->matrix().map(l);
854 painter->setPen(pen);
855 painter->drawLine(l2);
856 if (KdenliveSettings::showmarkers()) {
857 framepos = rect().x() + pos.frames(m_fps);
858 const QRectF r1(framepos + 0.04, 10, rect().width() - framepos - 2, rect().height() - 10);
859 const QRectF r2 = painter->matrix().mapRect(r1);
860 const QRectF txtBounding3 = painter->boundingRect(r2, Qt::AlignLeft | Qt::AlignTop, ' ' + (*it).comment() + ' ');
861 painter->setBrush(markerBrush);
862 painter->setPen(Qt::NoPen);
863 painter->drawRoundedRect(txtBounding3, 3, 3);
864 painter->setBrush(QBrush(Qt::NoBrush));
865 painter->setPen(Qt::white);
866 painter->drawText(txtBounding3, Qt::AlignCenter, (*it).comment());
868 //painter->fillRect(QRect(br.x() + framepos, br.y(), 10, br.height()), QBrush(QColor(0, 0, 0, 150)));
873 // draw start / end fades
876 fades = QBrush(QColor(200, 50, 50, 150));
877 } else fades = QBrush(QColor(200, 200, 200, 200));
879 if (m_startFade != 0) {
880 QPainterPath fadeInPath;
881 fadeInPath.moveTo(0, 0);
882 fadeInPath.lineTo(0, rect().height());
883 fadeInPath.lineTo(m_startFade, 0);
884 fadeInPath.closeSubpath();
885 QPainterPath f1 = painter->matrix().map(fadeInPath);
886 painter->fillPath(f1/*.intersected(resultClipPath)*/, fades);
887 /*if (isSelected()) {
888 QLineF l(m_startFade * scale, 0, 0, itemHeight);
889 painter->drawLine(l);
892 if (m_endFade != 0) {
893 QPainterPath fadeOutPath;
894 fadeOutPath.moveTo(rect().width(), 0);
895 fadeOutPath.lineTo(rect().width(), rect().height());
896 fadeOutPath.lineTo(rect().width() - m_endFade, 0);
897 fadeOutPath.closeSubpath();
898 QPainterPath f1 = painter->matrix().map(fadeOutPath);
899 painter->fillPath(f1/*.intersected(resultClipPath)*/, fades);
900 /*if (isSelected()) {
901 QLineF l(itemWidth - m_endFade * scale, 0, itemWidth, itemHeight);
902 painter->drawLine(l);
907 painter->setPen(QPen(Qt::lightGray));
908 // draw effect or transition keyframes
909 if (mapped.width() > 20) drawKeyFrames(painter, exposed);
911 //painter->setMatrixEnabled(true);
914 // expand clip rect to allow correct painting of clip border
915 QPen pen1(frameColor);
916 painter->setPen(pen1);
917 painter->setClipping(false);
918 painter->drawRect(painter->matrix().mapRect(rect()));
922 OPERATIONTYPE ClipItem::operationMode(QPointF pos)
924 if (isItemLocked()) return NONE;
925 const double scale = projectScene()->scale().x();
926 double maximumOffset = 6 / scale;
927 if (isSelected() || (parentItem() && parentItem()->isSelected())) {
928 int kf = mouseOverKeyFrames(pos, maximumOffset);
930 m_editedKeyframe = kf;
934 QRectF rect = sceneBoundingRect();
935 int addtransitionOffset = 10;
936 // Don't allow add transition if track height is very small
937 if (rect.height() < 30) addtransitionOffset = 0;
939 if (qAbs((int)(pos.x() - (rect.x() + m_startFade))) < maximumOffset && qAbs((int)(pos.y() - rect.y())) < 6) {
940 if (m_startFade == 0) setToolTip(i18n("Add audio fade"));
941 // xgettext:no-c-format
942 else setToolTip(i18n("Audio fade duration: %1s", GenTime(m_startFade, m_fps).seconds()));
944 } else if (pos.x() - rect.x() < maximumOffset && (rect.bottom() - pos.y() > addtransitionOffset)) {
945 // xgettext:no-c-format
946 setToolTip(i18n("Crop from start: %1s", cropStart().seconds()));
948 } else if (qAbs((int)(pos.x() - (rect.x() + rect.width() - m_endFade))) < maximumOffset && qAbs((int)(pos.y() - rect.y())) < 6) {
949 if (m_endFade == 0) setToolTip(i18n("Add audio fade"));
950 // xgettext:no-c-format
951 else setToolTip(i18n("Audio fade duration: %1s", GenTime(m_endFade, m_fps).seconds()));
953 } else if ((rect.right() - pos.x() < maximumOffset) && (rect.bottom() - pos.y() > addtransitionOffset)) {
954 // xgettext:no-c-format
955 setToolTip(i18n("Clip duration: %1s", cropDuration().seconds()));
957 } else if ((pos.x() - rect.x() < 16 / scale) && (rect.bottom() - pos.y() <= addtransitionOffset)) {
958 setToolTip(i18n("Add transition"));
959 return TRANSITIONSTART;
960 } else if ((rect.right() - pos.x() < 16 / scale) && (rect.bottom() - pos.y() <= addtransitionOffset)) {
961 setToolTip(i18n("Add transition"));
962 return TRANSITIONEND;
964 QString tooltip = "<b>" + m_clipName + "</b>";
965 if (!baseClip()->fileURL().isEmpty())
966 tooltip.append("<br />" + baseClip()->fileURL().path());
967 if (!baseClip()->description().isEmpty())
968 tooltip.append("<br />" + baseClip()->description());
973 QList <GenTime> ClipItem::snapMarkers() const
975 QList < GenTime > snaps;
976 QList < GenTime > markers = baseClip()->snapMarkers();
979 for (int i = 0; i < markers.size(); i++) {
981 pos = GenTime((int)(markers.at(i).frames(m_fps) / qAbs(m_speed) + 0.5), m_fps) - cropStart();
982 if (pos > GenTime()) {
983 if (pos > cropDuration()) break;
984 else snaps.append(pos + startPos());
990 QList <CommentedTime> ClipItem::commentedSnapMarkers() const
992 QList < CommentedTime > snaps;
993 QList < CommentedTime > markers = baseClip()->commentedSnapMarkers();
996 for (int i = 0; i < markers.size(); i++) {
997 pos = GenTime((int)(markers.at(i).time().frames(m_fps) / qAbs(m_speed) + 0.5), m_fps) - cropStart();
998 if (pos > GenTime()) {
999 if (pos > cropDuration()) break;
1000 else snaps.append(CommentedTime(pos + startPos(), markers.at(i).comment()));
1006 void ClipItem::slotPrepareAudioThumb(double pixelForOneFrame, int startpixel, int endpixel, int channels)
1008 QRectF re = sceneBoundingRect();
1009 if (m_clipType == AV && !isAudioOnly()) re.setTop(re.y() + re.height() / 2);
1011 //kDebug() << "// PREP AUDIO THMB FRMO : scale:" << pixelForOneFrame<< ", from: " << startpixel << ", to: " << endpixel;
1012 //if ( (!audioThumbWasDrawn || framePixelWidth!=pixelForOneFrame ) && !baseClip()->audioFrameChache.isEmpty()){
1014 for (int startCache = startpixel - startpixel % 100; startCache < endpixel; startCache += 100) {
1015 //kDebug() << "creating " << startCache;
1016 //if (framePixelWidth!=pixelForOneFrame ||
1017 if (m_framePixelWidth == pixelForOneFrame && m_audioThumbCachePic.contains(startCache))
1019 if (m_audioThumbCachePic[startCache].isNull() || m_framePixelWidth != pixelForOneFrame) {
1020 m_audioThumbCachePic[startCache] = QPixmap(100, (int)(re.height()));
1021 m_audioThumbCachePic[startCache].fill(QColor(180, 180, 200, 140));
1023 bool fullAreaDraw = pixelForOneFrame < 10;
1024 QMap<int, QPainterPath > positiveChannelPaths;
1025 QMap<int, QPainterPath > negativeChannelPaths;
1026 QPainter pixpainter(&m_audioThumbCachePic[startCache]);
1028 audiopen.setWidth(0);
1029 pixpainter.setPen(audiopen);
1030 //pixpainter.setRenderHint(QPainter::Antialiasing,true);
1031 //pixpainter.drawLine(0,0,100,re.height());
1032 // Bail out, if caller provided invalid data
1033 if (channels <= 0) {
1034 kWarning() << "Unable to draw image with " << channels << "number of channels";
1038 int channelHeight = m_audioThumbCachePic[startCache].height() / channels;
1040 for (int i = 0; i < channels; i++) {
1042 positiveChannelPaths[i].moveTo(0, channelHeight*i + channelHeight / 2);
1043 negativeChannelPaths[i].moveTo(0, channelHeight*i + channelHeight / 2);
1046 for (int samples = 0; samples <= 100; samples++) {
1047 double frame = (double)(samples + startCache - 0) / pixelForOneFrame;
1048 int sample = (int)((frame - (int)(frame)) * 20); // AUDIO_FRAME_SIZE
1049 if (frame < 0 || sample < 0 || sample > 19)
1051 QMap<int, QByteArray> frame_channel_data = baseClip()->m_audioFrameCache[(int)frame];
1053 for (int channel = 0; channel < channels && frame_channel_data[channel].size() > 0; channel++) {
1055 int y = channelHeight * channel + channelHeight / 2;
1056 int delta = (int)(frame_channel_data[channel][sample] - 127 / 2) * channelHeight / 64;
1058 positiveChannelPaths[channel].lineTo(samples, 0.1 + y + qAbs(delta));
1059 negativeChannelPaths[channel].lineTo(samples, 0.1 + y - qAbs(delta));
1061 positiveChannelPaths[channel].lineTo(samples, 0.1 + y + delta);
1062 negativeChannelPaths[channel].lineTo(samples, 0.1 + y - delta);
1065 for (int channel = 0; channel < channels ; channel++)
1066 if (fullAreaDraw && samples == 100) {
1067 positiveChannelPaths[channel].lineTo(samples, channelHeight*channel + channelHeight / 2);
1068 negativeChannelPaths[channel].lineTo(samples, channelHeight*channel + channelHeight / 2);
1069 positiveChannelPaths[channel].lineTo(0, channelHeight*channel + channelHeight / 2);
1070 negativeChannelPaths[channel].lineTo(0, channelHeight*channel + channelHeight / 2);
1074 pixpainter.setPen(QPen(QColor(0, 0, 0)));
1075 pixpainter.setBrush(QBrush(QColor(60, 60, 60)));
1077 for (int i = 0; i < channels; i++) {
1079 //pixpainter.fillPath(positiveChannelPaths[i].united(negativeChannelPaths[i]),QBrush(Qt::SolidPattern));//or singleif looks better
1080 pixpainter.drawPath(positiveChannelPaths[i].united(negativeChannelPaths[i]));//or singleif looks better
1082 pixpainter.drawPath(positiveChannelPaths[i]);
1085 //audioThumbWasDrawn=true;
1086 m_framePixelWidth = pixelForOneFrame;
1091 int ClipItem::fadeIn() const
1096 int ClipItem::fadeOut() const
1102 void ClipItem::setFadeIn(int pos)
1104 if (pos == m_startFade) return;
1105 int oldIn = m_startFade;
1106 if (pos < 0) pos = 0;
1107 if (pos > cropDuration().frames(m_fps)) pos = (int)(cropDuration().frames(m_fps));
1109 QRectF rect = boundingRect();
1110 update(rect.x(), rect.y(), qMax(oldIn, pos), rect.height());
1113 void ClipItem::setFadeOut(int pos)
1115 if (pos == m_endFade) return;
1116 int oldOut = m_endFade;
1117 if (pos < 0) pos = 0;
1118 if (pos > cropDuration().frames(m_fps)) pos = (int)(cropDuration().frames(m_fps));
1120 QRectF rect = boundingRect();
1121 update(rect.x() + rect.width() - qMax(oldOut, pos), rect.y(), qMax(oldOut, pos), rect.height());
1125 void ClipItem::setFades(int in, int out)
1133 void ClipItem::hoverEnterEvent(QGraphicsSceneHoverEvent *e)
1135 //if (e->pos().x() < 20) m_hover = true;
1137 if (isItemLocked()) return;
1139 QRectF r = boundingRect();
1140 double width = 35 / projectScene()->scale().x();
1141 double height = r.height() / 2;
1142 //WARNING: seems like it generates a full repaint of the clip, maybe not so good...
1143 update(r.x(), r.y() + height, width, height);
1144 update(r.right() - width, r.y() + height, width, height);
1148 void ClipItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *)
1150 if (isItemLocked()) return;
1152 QRectF r = boundingRect();
1153 double width = 35 / projectScene()->scale().x();
1154 double height = r.height() / 2;
1155 //WARNING: seems like it generates a full repaint of the clip, maybe not so good...
1156 update(r.x(), r.y() + height, width, height);
1157 update(r.right() - width, r.y() + height, width, height);
1161 void ClipItem::resizeStart(int posx, bool)
1163 bool sizeLimit = false;
1164 if (clipType() != IMAGE && clipType() != COLOR && clipType() != TEXT) {
1165 const int min = (startPos() - cropStart()).frames(m_fps);
1166 if (posx < min) posx = min;
1170 if (posx == startPos().frames(m_fps)) return;
1171 const int previous = cropStart().frames(m_fps);
1172 AbstractClipItem::resizeStart(posx, sizeLimit);
1174 // set speed independant info
1175 m_speedIndependantInfo = m_info;
1176 m_speedIndependantInfo.cropStart = GenTime((int)(m_info.cropStart.frames(m_fps) * qAbs(m_speed)), m_fps);
1177 m_speedIndependantInfo.cropDuration = GenTime((int)(m_info.cropDuration.frames(m_fps) * qAbs(m_speed)), m_fps);
1179 if ((int) cropStart().frames(m_fps) != previous) {
1180 if (m_hasThumbs && KdenliveSettings::videothumbnails()) {
1181 m_startThumbTimer.start(150);
1186 void ClipItem::resizeEnd(int posx)
1188 const int max = (startPos() - cropStart() + maxDuration()).frames(m_fps);
1189 if (posx > max && maxDuration() != GenTime()) posx = max;
1190 if (posx == endPos().frames(m_fps)) return;
1191 //kDebug() << "// NEW POS: " << posx << ", OLD END: " << endPos().frames(m_fps);
1192 const int previous = cropDuration().frames(m_fps);
1193 AbstractClipItem::resizeEnd(posx);
1195 // set speed independant info
1196 m_speedIndependantInfo = m_info;
1197 m_speedIndependantInfo.cropStart = GenTime((int)(m_info.cropStart.frames(m_fps) * qAbs(m_speed)), m_fps);
1198 m_speedIndependantInfo.cropDuration = GenTime((int)(m_info.cropDuration.frames(m_fps) * qAbs(m_speed)), m_fps);
1200 if ((int) cropDuration().frames(m_fps) != previous) {
1201 if (m_hasThumbs && KdenliveSettings::videothumbnails()) {
1202 m_endThumbTimer.start(150);
1208 bool ClipItem::checkEffectsKeyframesPos(const int previous, const int current, bool fromStart)
1210 bool modified = false;
1211 for (int i = 0; i < m_effectList.count(); i++) {
1212 QDomElement effect = m_effectList.at(i);
1213 QDomNodeList params = effect.elementsByTagName("parameter");
1214 for (int j = 0; j < params.count(); j++) {
1215 QDomElement e = params.item(i).toElement();
1216 if (!e.isNull() && (e.attribute("type") == "keyframe" || e.attribute("type") == "simplekeyframe")) {
1217 // parse keyframes and adjust values
1218 const QStringList keyframes = e.attribute("keyframes").split(';', QString::SkipEmptyParts);
1219 QMap <int, double> kfr;
1222 foreach(const QString &str, keyframes) {
1223 pos = str.section(':', 0, 0).toInt();
1224 val = str.section(':', 1, 1).toDouble();
1225 if (pos == previous) {
1229 if ((fromStart && pos >= current) || (!fromStart && pos <= current)) {
1237 QMap<int, double>::const_iterator k = kfr.constBegin();
1238 while (k != kfr.constEnd()) {
1239 newkfr.append(QString::number(k.key()) + ':' + QString::number(k.value()) + ';');
1242 e.setAttribute("keyframes", newkfr);
1248 if (modified && m_selectedEffect >= 0) setSelectedEffect(m_selectedEffect);
1253 QVariant ClipItem::itemChange(GraphicsItemChange change, const QVariant &value)
1255 if (change == QGraphicsItem::ItemSelectedChange) {
1256 if (value.toBool()) setZValue(10);
1259 if (change == ItemPositionChange && scene()) {
1260 // calculate new position.
1261 //if (parentItem()) return pos();
1262 QPointF newPos = value.toPointF();
1263 //kDebug() << "/// MOVING CLIP ITEM.------------\n++++++++++";
1264 int xpos = projectScene()->getSnapPointForPos((int) newPos.x(), KdenliveSettings::snaptopoints());
1265 xpos = qMax(xpos, 0);
1267 int newTrack = newPos.y() / KdenliveSettings::trackheight();
1268 newTrack = qMin(newTrack, projectScene()->tracksCount() - 1);
1269 newTrack = qMax(newTrack, 0);
1270 newPos.setY((int)(newTrack * KdenliveSettings::trackheight() + 1));
1271 // Only one clip is moving
1272 QRectF sceneShape = rect();
1273 sceneShape.translate(newPos);
1274 QList<QGraphicsItem*> items;
1275 if (projectScene()->editMode() == NORMALEDIT)
1276 items = scene()->items(sceneShape, Qt::IntersectsItemShape);
1277 items.removeAll(this);
1278 bool forwardMove = newPos.x() > pos().x();
1280 if (!items.isEmpty()) {
1281 for (int i = 0; i < items.count(); i++) {
1282 if (!items.at(i)->isEnabled()) continue;
1283 if (items.at(i)->type() == type()) {
1285 QPointF otherPos = items.at(i)->pos();
1286 if ((int) otherPos.y() != (int) pos().y()) {
1290 offset = qMax(offset, (int)(newPos.x() - (static_cast < AbstractClipItem* >(items.at(i))->startPos() - cropDuration()).frames(m_fps)));
1292 offset = qMax(offset, (int)((static_cast < AbstractClipItem* >(items.at(i))->endPos().frames(m_fps)) - newPos.x()));
1297 sceneShape.translate(QPointF(-offset, 0));
1298 newPos.setX(newPos.x() - offset);
1300 sceneShape.translate(QPointF(offset, 0));
1301 newPos.setX(newPos.x() + offset);
1303 QList<QGraphicsItem*> subitems = scene()->items(sceneShape, Qt::IntersectsItemShape);
1304 subitems.removeAll(this);
1305 for (int j = 0; j < subitems.count(); j++) {
1306 if (!subitems.at(j)->isEnabled()) continue;
1307 if (subitems.at(j)->type() == type()) {
1308 // move was not successful, revert to previous pos
1309 m_info.startPos = GenTime((int) pos().x(), m_fps);
1315 m_info.track = newTrack;
1316 m_info.startPos = GenTime((int) newPos.x(), m_fps);
1322 m_info.track = newTrack;
1323 m_info.startPos = GenTime((int) newPos.x(), m_fps);
1324 //kDebug()<<"// ITEM NEW POS: "<<newPos.x()<<", mapped: "<<mapToScene(newPos.x(), 0).x();
1327 return QGraphicsItem::itemChange(change, value);
1331 /*void ClipItem::mouseMoveEvent(QGraphicsSceneMouseEvent * event) {
1334 int ClipItem::effectsCounter()
1336 return effectsCount() + 1;
1339 int ClipItem::effectsCount()
1341 return m_effectList.count();
1344 int ClipItem::hasEffect(const QString &tag, const QString &id) const
1346 return m_effectList.hasEffect(tag, id);
1349 QStringList ClipItem::effectNames()
1351 return m_effectList.effectNames();
1354 QDomElement ClipItem::effectAt(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).cloneNode().toElement();
1360 QDomElement ClipItem::getEffectAt(int ix) const
1362 if (ix > m_effectList.count() - 1 || ix < 0 || m_effectList.at(ix).isNull()) return QDomElement();
1363 return m_effectList.at(ix);
1366 void ClipItem::setEffectAt(int ix, QDomElement effect)
1368 if (ix < 0 || ix > (m_effectList.count() - 1) || effect.isNull()) {
1369 kDebug() << "Invalid effect index: " << ix;
1372 //kDebug() << "CHange EFFECT AT: " << ix << ", CURR: " << m_effectList.at(ix).attribute("tag") << ", NEW: " << effect.attribute("tag");
1373 effect.setAttribute("kdenlive_ix", ix + 1);
1374 m_effectList.replace(ix, effect);
1375 m_effectNames = m_effectList.effectNames().join(" / ");
1376 QString id = effect.attribute("id");
1377 if (id == "fadein" || id == "fadeout" || id == "fade_from_black" || id == "fade_to_black")
1380 QRectF r = boundingRect();
1386 EffectsParameterList ClipItem::addEffect(const QDomElement effect, bool /*animate*/)
1388 bool needRepaint = false;
1390 if (!effect.hasAttribute("kdenlive_ix")) {
1391 ix = effectsCounter();
1392 } else ix = effect.attribute("kdenlive_ix").toInt();
1393 if (!m_effectList.isEmpty() && ix <= m_effectList.count()) {
1395 m_effectList.insert(ix - 1, effect);
1396 for (int i = ix; i < m_effectList.count(); i++) {
1397 int index = m_effectList.item(i).attribute("kdenlive_ix").toInt();
1398 if (index >= ix) m_effectList.item(i).setAttribute("kdenlive_ix", index + 1);
1400 } else m_effectList.append(effect);
1401 EffectsParameterList parameters;
1402 parameters.addParam("tag", effect.attribute("tag"));
1403 parameters.addParam("kdenlive_ix", effect.attribute("kdenlive_ix"));
1404 if (effect.hasAttribute("src")) parameters.addParam("src", effect.attribute("src"));
1405 if (effect.hasAttribute("disable")) parameters.addParam("disable", effect.attribute("disable"));
1408 QString effectId = effect.attribute("id");
1409 if (effectId.isEmpty()) effectId = effect.attribute("tag");
1410 parameters.addParam("id", effectId);
1412 QDomNodeList params = effect.elementsByTagName("parameter");
1414 for (int i = 0; i < params.count(); i++) {
1415 QDomElement e = params.item(i).toElement();
1417 if (e.attribute("type") == "simplekeyframe") {
1418 QStringList values = e.attribute("keyframes").split(";", QString::SkipEmptyParts);
1419 double factor = e.attribute("factor", "1").toDouble();
1421 for (int j = 0; j < values.count(); j++) {
1422 QString pos = values.at(j).section(":", 0, 0);
1423 double val = values.at(j).section(":", 1, 1).toDouble() / factor;
1424 values[j] = pos + "=" + QString::number(val);
1427 parameters.addParam(e.attribute("name"), values.join(";"));
1428 /*parameters.addParam("max", e.attribute("max"));
1429 parameters.addParam("min", e.attribute("min"));
1430 parameters.addParam("factor", );*/
1431 } else if (e.attribute("type") == "keyframe") {
1432 parameters.addParam("keyframes", e.attribute("keyframes"));
1433 parameters.addParam("max", e.attribute("max"));
1434 parameters.addParam("min", e.attribute("min"));
1435 parameters.addParam("factor", e.attribute("factor", "1"));
1436 parameters.addParam("starttag", e.attribute("starttag", "start"));
1437 parameters.addParam("endtag", e.attribute("endtag", "end"));
1438 } else if (e.attribute("factor", "1") == "1") {
1439 parameters.addParam(e.attribute("name"), e.attribute("value"));
1441 // check if it is a fade effect
1442 if (effectId == "fadein") {
1444 if (m_effectList.hasEffect(QString(), "fade_from_black") == -1) {
1445 if (e.attribute("name") == "out") fade += e.attribute("value").toInt();
1446 else if (e.attribute("name") == "in") fade -= e.attribute("value").toInt();
1448 QDomElement fadein = m_effectList.getEffectByTag(QString(), "fade_from_black");
1449 if (fadein.attribute("name") == "out") fade += fadein.attribute("value").toInt();
1450 else if (fadein.attribute("name") == "in") fade -= fadein.attribute("value").toInt();
1452 } else if (effectId == "fade_from_black") {
1454 if (m_effectList.hasEffect(QString(), "fadein") == -1) {
1455 if (e.attribute("name") == "out") fade += e.attribute("value").toInt();
1456 else if (e.attribute("name") == "in") fade -= e.attribute("value").toInt();
1458 QDomElement fadein = m_effectList.getEffectByTag(QString(), "fadein");
1459 if (fadein.attribute("name") == "out") fade += fadein.attribute("value").toInt();
1460 else if (fadein.attribute("name") == "in") fade -= fadein.attribute("value").toInt();
1462 } else if (effectId == "fadeout") {
1464 if (m_effectList.hasEffect(QString(), "fade_to_black") == -1) {
1465 if (e.attribute("name") == "out") fade -= e.attribute("value").toInt();
1466 else if (e.attribute("name") == "in") fade += e.attribute("value").toInt();
1468 QDomElement fadeout = m_effectList.getEffectByTag(QString(), "fade_to_black");
1469 if (fadeout.attribute("name") == "out") fade -= fadeout.attribute("value").toInt();
1470 else if (fadeout.attribute("name") == "in") fade += fadeout.attribute("value").toInt();
1472 } else if (effectId == "fade_to_black") {
1474 if (m_effectList.hasEffect(QString(), "fadeout") == -1) {
1475 if (e.attribute("name") == "out") fade -= e.attribute("value").toInt();
1476 else if (e.attribute("name") == "in") fade += e.attribute("value").toInt();
1478 QDomElement fadeout = m_effectList.getEffectByTag(QString(), "fadeout");
1479 if (fadeout.attribute("name") == "out") fade -= fadeout.attribute("value").toInt();
1480 else if (fadeout.attribute("name") == "in") fade += fadeout.attribute("value").toInt();
1485 if (e.attribute("factor").startsWith('%')) {
1486 fact = ProfilesDialog::getStringEval(projectScene()->profile(), e.attribute("factor"));
1487 } else fact = e.attribute("factor", "1").toDouble();
1488 parameters.addParam(e.attribute("name"), QString::number(e.attribute("value").toDouble() / fact));
1492 m_effectNames = m_effectList.effectNames().join(" / ");
1493 if (fade > 0) m_startFade = fade;
1494 else if (fade < 0) m_endFade = -fade;
1496 if (m_selectedEffect == -1) {
1497 setSelectedEffect(0);
1498 } else if (m_selectedEffect == ix - 1) setSelectedEffect(m_selectedEffect);
1499 if (needRepaint) update(boundingRect());
1503 else { /*if (!needRepaint) */
1504 QRectF r = boundingRect();
1511 EffectsParameterList ClipItem::getEffectArgs(const QDomElement effect)
1513 EffectsParameterList parameters;
1514 parameters.addParam("tag", effect.attribute("tag"));
1515 parameters.addParam("kdenlive_ix", effect.attribute("kdenlive_ix"));
1516 parameters.addParam("id", effect.attribute("id"));
1517 if (effect.hasAttribute("src")) parameters.addParam("src", effect.attribute("src"));
1518 if (effect.hasAttribute("disable")) parameters.addParam("disable", effect.attribute("disable"));
1520 QDomNodeList params = effect.elementsByTagName("parameter");
1521 for (int i = 0; i < params.count(); i++) {
1522 QDomElement e = params.item(i).toElement();
1523 //kDebug() << "/ / / /SENDING EFFECT PARAM: " << e.attribute("type") << ", NAME_ " << e.attribute("tag");
1524 if (e.attribute("type") == "simplekeyframe") {
1526 QStringList values = e.attribute("keyframes").split(";", QString::SkipEmptyParts);
1527 double factor = e.attribute("factor", "1").toDouble();
1528 for (int j = 0; j < values.count(); j++) {
1529 QString pos = values.at(j).section(":", 0, 0);
1530 double val = values.at(j).section(":", 1, 1).toDouble() / factor;
1531 values[j] = pos + "=" + QString::number(val);
1533 // kDebug() << "/ / / /SENDING KEYFR:" << values;
1534 parameters.addParam(e.attribute("name"), values.join(";"));
1535 /*parameters.addParam(e.attribute("name"), e.attribute("keyframes").replace(":", "="));
1536 parameters.addParam("max", e.attribute("max"));
1537 parameters.addParam("min", e.attribute("min"));
1538 parameters.addParam("factor", e.attribute("factor", "1"));*/
1539 } else if (e.attribute("type") == "keyframe") {
1540 kDebug() << "/ / / /SENDING KEYFR EFFECT TYPE";
1541 parameters.addParam("keyframes", e.attribute("keyframes"));
1542 parameters.addParam("max", e.attribute("max"));
1543 parameters.addParam("min", e.attribute("min"));
1544 parameters.addParam("factor", e.attribute("factor", "1"));
1545 parameters.addParam("starttag", e.attribute("starttag", "start"));
1546 parameters.addParam("endtag", e.attribute("endtag", "end"));
1547 } else if (e.attribute("namedesc").contains(';')) {
1548 QString format = e.attribute("format");
1549 QStringList separators = format.split("%d", QString::SkipEmptyParts);
1550 QStringList values = e.attribute("value").split(QRegExp("[,:;x]"));
1552 QTextStream txtNeu(&neu);
1553 if (values.size() > 0)
1554 txtNeu << (int)values[0].toDouble();
1555 for (int i = 0; i < separators.size() && i + 1 < values.size(); i++) {
1556 txtNeu << separators[i];
1557 txtNeu << (int)(values[i+1].toDouble());
1559 parameters.addParam("start", neu);
1561 if (e.attribute("factor", "1") != "1") {
1563 if (e.attribute("factor").startsWith('%')) {
1564 fact = ProfilesDialog::getStringEval(projectScene()->profile(), e.attribute("factor"));
1565 } else fact = e.attribute("factor", "1").toDouble();
1566 parameters.addParam(e.attribute("name"), QString::number(e.attribute("value").toDouble() / fact));
1568 parameters.addParam(e.attribute("name"), e.attribute("value"));
1575 void ClipItem::deleteEffect(QString index)
1577 bool needRepaint = false;
1580 for (int i = 0; i < m_effectList.count(); ++i) {
1581 ix = m_effectList.at(i).attribute("kdenlive_ix");
1583 QString effectId = m_effectList.at(i).attribute("id");
1584 if ((effectId == "fadein" && hasEffect(QString(), "fade_from_black") == -1) ||
1585 (effectId == "fade_from_black" && hasEffect(QString(), "fadein") == -1)) {
1588 } else if ((effectId == "fadeout" && hasEffect(QString(), "fade_to_black") == -1) ||
1589 (effectId == "fade_to_black" && hasEffect(QString(), "fadeout") == -1)) {
1592 } else if (EffectsList::hasKeyFrames(m_effectList.at(i))) needRepaint = true;
1593 m_effectList.removeAt(i);
1595 } else if (ix.toInt() > index.toInt()) {
1596 m_effectList.item(i).setAttribute("kdenlive_ix", ix.toInt() - 1);
1599 m_effectNames = m_effectList.effectNames().join(" / ");
1601 if (m_effectList.isEmpty() || m_selectedEffect + 1 == index.toInt()) {
1602 // Current effect was removed
1603 if (index.toInt() > m_effectList.count() - 1) {
1604 setSelectedEffect(m_effectList.count() - 1);
1605 } else setSelectedEffect(index.toInt());
1607 if (needRepaint) update(boundingRect());
1609 QRectF r = boundingRect();
1613 //if (!m_effectList.isEmpty()) flashClip();
1616 double ClipItem::speed() const
1621 int ClipItem::strobe() const
1626 void ClipItem::setSpeed(const double speed, const int strobe)
1629 if (m_speed <= 0 && m_speed > -1)
1632 if (m_speed == 1.0) m_clipName = baseClip()->name();
1633 else m_clipName = baseClip()->name() + " - " + QString::number(speed * 100, 'f', 0) + '%';
1634 m_info.cropStart = GenTime((int)(m_speedIndependantInfo.cropStart.frames(m_fps) / qAbs(m_speed) + 0.5), m_fps);
1635 m_info.cropDuration = GenTime((int)(m_speedIndependantInfo.cropDuration.frames(m_fps) / qAbs(m_speed) + 0.5), m_fps);
1639 GenTime ClipItem::maxDuration() const
1641 return GenTime((int)(m_maxDuration.frames(m_fps) / qAbs(m_speed) + 0.5), m_fps);
1644 GenTime ClipItem::speedIndependantCropStart() const
1646 return m_speedIndependantInfo.cropStart;
1649 GenTime ClipItem::speedIndependantCropDuration() const
1651 return m_speedIndependantInfo.cropDuration;
1655 const ItemInfo ClipItem::speedIndependantInfo() const
1657 return m_speedIndependantInfo;
1661 void ClipItem::dropEvent(QGraphicsSceneDragDropEvent * event)
1663 const QString effects = QString(event->mimeData()->data("kdenlive/effectslist"));
1665 doc.setContent(effects, true);
1666 const QDomElement e = doc.documentElement();
1667 if (scene() && !scene()->views().isEmpty()) {
1669 CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
1670 if (view) view->slotAddEffect(e, m_info.startPos, track());
1675 void ClipItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
1677 if (isItemLocked()) event->setAccepted(false);
1678 else event->setAccepted(event->mimeData()->hasFormat("kdenlive/effectslist"));
1681 void ClipItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
1686 void ClipItem::addTransition(Transition* t)
1688 m_transitionsList.append(t);
1689 //CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
1691 QDomElement e = doc.documentElement();
1692 //if (view) view->slotAddTransition(this, t->toXML() , t->startPos(), track());
1695 void ClipItem::setVideoOnly(bool force)
1697 m_videoOnly = force;
1700 void ClipItem::setAudioOnly(bool force)
1702 m_audioOnly = force;
1703 if (m_audioOnly) m_baseColor = QColor(141, 215, 166);
1705 if (m_clipType == COLOR) {
1706 QString colour = m_clip->getProperty("colour");
1707 colour = colour.replace(0, 2, "#");
1708 m_baseColor = QColor(colour.left(7));
1709 } else if (m_clipType == AUDIO) m_baseColor = QColor(141, 215, 166);
1710 else m_baseColor = QColor(141, 166, 215);
1712 m_audioThumbCachePic.clear();
1715 bool ClipItem::isAudioOnly() const
1720 bool ClipItem::isVideoOnly() const
1725 void ClipItem::insertKeyframe(QDomElement effect, int pos, int val)
1727 if (effect.attribute("disable") == "1") return;
1728 effect.setAttribute("active_keyframe", pos);
1729 m_editedKeyframe = pos;
1730 QDomNodeList params = effect.elementsByTagName("parameter");
1731 for (int i = 0; i < params.count(); i++) {
1732 QDomElement e = params.item(i).toElement();
1733 QString kfr = e.attribute("keyframes");
1734 const QStringList keyframes = kfr.split(';', QString::SkipEmptyParts);
1737 foreach(const QString &str, keyframes) {
1738 int kpos = str.section(':', 0, 0).toInt();
1739 double newval = str.section(':', 1, 1).toDouble();
1742 } else if (!added) {
1743 if (i == 0) newkfr.append(QString::number(pos) + ":" + QString::number(val));
1744 else newkfr.append(QString::number(pos) + ":" + QString::number(newval));
1745 if (kpos > pos) newkfr.append(str);
1747 } else newkfr.append(str);
1749 if (!added) newkfr.append(QString::number(pos) + ":" + QString::number(val));
1750 e.setAttribute("keyframes", newkfr.join(";"));
1754 void ClipItem::movedKeyframe(QDomElement effect, int oldpos, int newpos, double value)
1756 if (effect.attribute("disable") == "1") return;
1757 effect.setAttribute("active_keyframe", newpos);
1758 QDomNodeList params = effect.elementsByTagName("parameter");
1759 int start = cropStart().frames(m_fps);
1760 int end = (cropStart() + cropDuration()).frames(m_fps) - 1;
1761 for (int i = 0; i < params.count(); i++) {
1762 QDomElement e = params.item(i).toElement();
1763 QString kfr = e.attribute("keyframes");
1764 const QStringList keyframes = kfr.split(';', QString::SkipEmptyParts);
1766 foreach(const QString &str, keyframes) {
1767 if (str.section(':', 0, 0).toInt() != oldpos) {
1769 } else if (newpos != -1) {
1770 newpos = qMax(newpos, start);
1771 newpos = qMin(newpos, end);
1772 if (i == 0) newkfr.append(QString::number(newpos) + ":" + QString::number(value));
1773 else newkfr.append(QString::number(newpos) + ":" + str.section(':', 1, 1));
1776 e.setAttribute("keyframes", newkfr.join(";"));
1779 updateKeyframes(effect);
1783 void ClipItem::updateKeyframes(QDomElement effect)
1785 m_keyframes.clear();
1787 QDomNodeList params = effect.elementsByTagName("parameter");
1788 QDomElement e = params.item(0).toElement();
1789 const QStringList keyframes = e.attribute("keyframes").split(';', QString::SkipEmptyParts);
1790 foreach(const QString &str, keyframes) {
1791 int pos = str.section(':', 0, 0).toInt();
1792 double val = str.section(':', 1, 1).toDouble();
1793 m_keyframes[pos] = val;
1795 if (!m_keyframes.contains(m_selectedKeyframe)) m_selectedKeyframe = -1;
1798 void ClipItem::doGetIntraThumbs(QPainter *painter, const QPointF startPos, int offset, int start, int end)
1800 if (!m_clip->thumbProducer() || clipType() == COLOR) return;
1801 if (scene() && scene()->views().isEmpty()) return;
1802 CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
1803 if (view == NULL) return;
1804 const int theight = KdenliveSettings::trackheight();
1805 const int twidth = FRAME_SIZE;
1807 if (clipType() == IMAGE || clipType() == TEXT) {
1808 for (int i = start; i <= end; i++)
1809 painter->drawPixmap(startPos + QPointF(twidth *(i - offset), 0), m_startPix);
1812 for (int i = start; i <= end; i++) {
1813 if (!view->pixmapCache->find(m_clip->fileURL().path() + "%" + QString::number(i), p)) {
1814 p = m_clip->thumbProducer()->extractImage(i, twidth, theight);
1815 view->pixmapCache->insert(m_clip->fileURL().path() + "%" + QString::number(i), p);
1817 painter->drawPixmap(startPos + QPointF(twidth *(i - offset), 0), p);
1822 #include "clipitem.moc"