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 ***************************************************************************/
24 #include <QStyleOptionGraphicsItem>
25 #include <QGraphicsScene>
26 #include <QGraphicsView>
29 #include <QApplication>
34 #include "customtrackview.h"
36 #include "docclipbase.h"
37 #include "transition.h"
39 #include "kdenlivesettings.h"
42 ClipItem::ClipItem(DocClipBase *clip, ItemInfo info, GenTime cropStart, double scale, double fps)
43 : AbstractClipItem(info, QRectF(), fps), m_clip(clip), m_resizeMode(NONE), m_grabPoint(0), m_maxTrack(0), m_hasThumbs(false), startThumbTimer(NULL), endThumbTimer(NULL), m_effectsCounter(1), audioThumbWasDrawn(false), m_opacity(1.0), m_timeLine(0), m_thumbsRequested(0), m_startFade(0), m_endFade(0), m_hover(false) {
44 QRectF rect((double) info.startPos.frames(fps) * scale, (double)(info.track * KdenliveSettings::trackheight() + 1), (double)(info.endPos - info.startPos).frames(fps) * scale, (double)(KdenliveSettings::trackheight() - 1));
47 m_clipName = clip->name();
48 m_producer = clip->getId();
49 m_clipType = clip->clipType();
50 m_cropStart = cropStart;
52 m_maxDuration = clip->maxDuration();
54 audioThumbReady = clip->audioThumbCreated();
56 m_cropStart = xml.attribute("in", 0).toInt();
57 m_maxDuration = xml.attribute("duration", 0).toInt();
58 if (m_maxDuration == 0) m_maxDuration = xml.attribute("out", 0).toInt() - m_cropStart;
60 if (duration != -1) m_cropDuration = duration;
61 else m_cropDuration = m_maxDuration;*/
64 setFlags(QGraphicsItem::ItemClipsToShape | QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
65 setAcceptsHoverEvents(true);
66 connect(this , SIGNAL(prepareAudioThumb(double, QPainterPath, int, int)) , this, SLOT(slotPrepareAudioThumb(double, QPainterPath, int, int)));
68 setBrush(QColor(141, 166, 215));
69 if (m_clipType == VIDEO || m_clipType == AV || m_clipType == SLIDESHOW) {
71 connect(this, SIGNAL(getThumb(int, int)), clip->thumbProducer(), SLOT(extractImage(int, int)));
72 connect(clip->thumbProducer(), SIGNAL(thumbReady(int, QPixmap)), this, SLOT(slotThumbReady(int, QPixmap)));
73 connect(clip, SIGNAL(gotAudioData()), this, SLOT(slotGotAudioData()));
74 QTimer::singleShot(300, this, SLOT(slotFetchThumbs()));
76 startThumbTimer = new QTimer(this);
77 startThumbTimer->setSingleShot(true);
78 connect(startThumbTimer, SIGNAL(timeout()), this, SLOT(slotGetStartThumb()));
79 endThumbTimer = new QTimer(this);
80 endThumbTimer->setSingleShot(true);
81 connect(endThumbTimer, SIGNAL(timeout()), this, SLOT(slotGetEndThumb()));
82 } else if (m_clipType == COLOR) {
83 QString colour = clip->getProperty("colour");
84 colour = colour.replace(0, 2, "#");
85 setBrush(QColor(colour.left(7)));
86 } else if (m_clipType == IMAGE || m_clipType == TEXT) {
87 m_startPix = KThumb::getImage(KUrl(clip->getProperty("resource")), (int)(50 * KdenliveSettings::project_display_ratio()), 50);
88 m_endPix = m_startPix;
89 } else if (m_clipType == AUDIO) {
90 connect(clip, SIGNAL(gotAudioData()), this, SLOT(slotGotAudioData()));
95 ClipItem::~ClipItem() {
96 if (startThumbTimer) delete startThumbTimer;
97 if (endThumbTimer) delete endThumbTimer;
100 void ClipItem::resetThumbs() {
102 audioThumbCachePic.clear();
106 void ClipItem::refreshClip() {
107 m_maxDuration = m_clip->maxDuration();
108 if (m_clipType == VIDEO || m_clipType == AV || m_clipType == SLIDESHOW) slotFetchThumbs();
109 else if (m_clipType == COLOR) {
110 QString colour = m_clip->getProperty("colour");
111 colour = colour.replace(0, 2, "#");
112 setBrush(QColor(colour.left(7)));
113 } else if (m_clipType == IMAGE || m_clipType == TEXT) {
114 m_startPix = KThumb::getImage(KUrl(m_clip->getProperty("resource")), (int)(50 * KdenliveSettings::project_display_ratio()), 50);
115 m_endPix = m_startPix;
119 void ClipItem::slotFetchThumbs() {
120 m_thumbsRequested += 2;
121 emit getThumb((int)m_cropStart.frames(m_fps), (int)(m_cropStart + m_cropDuration).frames(m_fps));
124 void ClipItem::slotGetStartThumb() {
126 emit getThumb((int)m_cropStart.frames(m_fps), -1);
129 void ClipItem::slotGetEndThumb() {
131 emit getThumb(-1, (int)(m_cropStart + m_cropDuration).frames(m_fps));
134 void ClipItem::slotThumbReady(int frame, QPixmap pix) {
135 if (m_thumbsRequested == 0) return;
136 if (frame == m_cropStart.frames(m_fps)) {
138 QRectF r = boundingRect();
139 r.setRight(pix.width() + 2);
144 QRectF r = boundingRect();
145 r.setLeft(r.right() - pix.width() - 2);
151 void ClipItem::slotGotAudioData() {
152 audioThumbReady = true;
153 if (m_clipType == AV) {
154 QRectF r = boundingRect();
155 r.setTop(r.top() + r.height() / 2 - 1);
161 int ClipItem::type() const {
165 DocClipBase *ClipItem::baseClip() {
169 QDomElement ClipItem::xml() const {
170 return m_clip->toXML();
173 int ClipItem::clipType() {
177 QString ClipItem::clipName() {
181 int ClipItem::clipProducer() {
185 void ClipItem::flashClip() {
186 if (m_timeLine == 0) {
187 m_timeLine = new QTimeLine(750, this);
188 m_timeLine->setCurveShape(QTimeLine::EaseInOutCurve);
189 connect(m_timeLine, SIGNAL(valueChanged(qreal)), this, SLOT(animate(qreal)));
194 void ClipItem::animate(qreal value) {
195 QRectF r = boundingRect();
201 void ClipItem::paint(QPainter *painter,
202 const QStyleOptionGraphicsItem *option,
204 painter->setOpacity(m_opacity);
205 QBrush paintColor = brush();
206 if (isSelected()) paintColor = QBrush(QColor(79, 93, 121));
208 double scale = br.width() / m_cropDuration.frames(m_fps);
210 // kDebug()<<"/// EXPOSED RECT: "<<option->exposedRect.x()<<" X "<<option->exposedRect.right();
211 painter->setClipRect(option->exposedRect);
212 int startpixel = (int)option->exposedRect.x() - rect().x();
216 int endpixel = (int)option->exposedRect.right() - rect().x();
220 //painter->setRenderHints(QPainter::Antialiasing);
222 QPainterPath roundRectPathUpper = upperRectPart(br), roundRectPathLower = lowerRectPart(br);
225 // build path around clip
226 QPainterPath resultClipPath = roundRectPathUpper.united(roundRectPathLower);
227 painter->fillPath(resultClipPath, paintColor);
230 if (!m_startPix.isNull() && KdenliveSettings::videothumbnails()) {
231 if (m_clipType == IMAGE) {
232 painter->drawPixmap(QPointF(br.x() + br.width() - m_startPix.width(), br.y()), m_startPix);
233 QLineF l(br.x() + br.width() - m_startPix.width(), br.y(), br.x() + br.width() - m_startPix.width(), br.y() + br.height());
234 painter->drawLine(l);
236 painter->drawPixmap(QPointF(br.x() + br.width() - m_endPix.width(), br.y()), m_endPix);
237 QLineF l(br.x() + br.width() - m_endPix.width(), br.y(), br.x() + br.width() - m_endPix.width(), br.y() + br.height());
238 painter->drawLine(l);
241 painter->drawPixmap(QPointF(br.x(), br.y()), m_startPix);
242 QLineF l2(br.x() + m_startPix.width(), br.y(), br.x() + m_startPix.width(), br.y() + br.height());
243 painter->drawLine(l2);
246 // draw audio thumbnails
247 if (KdenliveSettings::audiothumbnails() && ((m_clipType == AV && option->exposedRect.height() > br.height() / 2) || m_clipType == AUDIO) && audioThumbReady) {
249 QPainterPath path = m_clipType == AV ? roundRectPathLower : resultClipPath;
250 if (m_clipType == AV) painter->fillPath(path, QBrush(QColor(200, 200, 200, 140)));
253 if (scale != framePixelWidth)
254 audioThumbCachePic.clear();
255 emit prepareAudioThumb(scale, path, startpixel, endpixel + 200);//200 more for less missing parts before repaint after scrolling
256 int cropLeft = (int)((m_cropStart).frames(m_fps) * scale);
257 for (int startCache = startpixel - startpixel % 100; startCache < endpixel + 300;startCache += 100) {
258 if (audioThumbCachePic.contains(startCache) && !audioThumbCachePic[startCache].isNull())
259 painter->drawPixmap((int)(roundRectPathUpper.united(roundRectPathLower).boundingRect().x() + startCache - cropLeft), (int)(path.boundingRect().y()), audioThumbCachePic[startCache]);
264 QList < CommentedTime > markers = baseClip()->commentedSnapMarkers();
265 QList < CommentedTime >::Iterator it = markers.begin();
268 const int markerwidth = 4;
270 markerBrush = QBrush(QColor(120, 120, 0, 100));
271 QPen pen = painter->pen();
272 pen.setColor(QColor(255, 255, 255, 200));
273 pen.setStyle(Qt::DotLine);
274 painter->setPen(pen);
275 for (; it != markers.end(); ++it) {
276 pos = (*it).time() - cropStart();
277 if (pos > GenTime()) {
278 if (pos > duration()) break;
279 framepos = scale * pos.frames(m_fps);
280 QLineF l(br.x() + framepos, br.y() + 5, br.x() + framepos, br.y() + br.height() - 5);
281 painter->drawLine(l);
282 if (KdenliveSettings::showmarkers()) {
283 const QRectF txtBounding = painter->boundingRect(br.x() + framepos + 1, br.y() + 10, br.width() - framepos - 2, br.height() - 10, Qt::AlignLeft | Qt::AlignTop, " " + (*it).comment() + " ");
285 path.addRoundedRect(txtBounding, 3, 3);
286 painter->fillPath(path, markerBrush);
287 painter->drawText(txtBounding, Qt::AlignCenter, (*it).comment());
289 //painter->fillRect(QRect(br.x() + framepos, br.y(), 10, br.height()), QBrush(QColor(0, 0, 0, 150)));
292 pen.setColor(Qt::black);
293 pen.setStyle(Qt::SolidLine);
294 painter->setPen(pen);
297 // draw start / end fades
300 fades = QBrush(QColor(200, 50, 50, 150));
301 } else fades = QBrush(QColor(200, 200, 200, 200));
303 if (m_startFade != 0) {
304 QPainterPath fadeInPath;
305 fadeInPath.moveTo(br.x() , br.y());
306 fadeInPath.lineTo(br.x() , br.bottom());
307 fadeInPath.lineTo(br.x() + m_startFade * scale, br.y());
308 fadeInPath.closeSubpath();
309 painter->fillPath(fadeInPath.intersected(resultClipPath), fades);
311 QLineF l(br.x() + m_startFade * scale, br.y(), br.x(), br.bottom());
312 painter->drawLine(l);
315 if (m_endFade != 0) {
316 QPainterPath fadeOutPath;
317 fadeOutPath.moveTo(br.right(), br.y());
318 fadeOutPath.lineTo(br.right(), br.bottom());
319 fadeOutPath.lineTo(br.right() - m_endFade * scale, br.y());
320 fadeOutPath.closeSubpath();
321 painter->fillPath(fadeOutPath.intersected(resultClipPath), fades);
323 QLineF l(br.right() - m_endFade * scale, br.y(), br.x() + br.width(), br.bottom());
324 painter->drawLine(l);
328 // Draw effects names
329 if (!m_effectNames.isEmpty() && br.width() > 30) {
330 QRectF txtBounding = painter->boundingRect(br, Qt::AlignLeft | Qt::AlignTop, m_effectNames);
331 txtBounding.setRight(txtBounding.right() + 15);
332 painter->setPen(Qt::white);
333 QBrush markerBrush(Qt::SolidPattern);
334 if (m_timeLine && m_timeLine->state() == QTimeLine::Running) {
335 qreal value = m_timeLine->currentValue();
336 txtBounding.setWidth(txtBounding.width() * value);
337 markerBrush.setColor(QColor(50 + 200 * (1.0 - value), 50, 50, 100 + 50 * value));
338 } else markerBrush.setColor(QColor(50, 50, 50, 150));
340 path.addRoundedRect(txtBounding, 4, 4);
341 painter->fillPath(path.intersected(resultClipPath), markerBrush);
342 painter->drawText(txtBounding, Qt::AlignCenter, m_effectNames);
343 painter->setPen(Qt::black);
347 QRectF txtBounding = painter->boundingRect(br, Qt::AlignHCenter | Qt::AlignTop, " " + m_clipName + " ");
348 //painter->fillRect(txtBounding, QBrush(QColor(255, 255, 255, 150)));
349 painter->setPen(QColor(0, 0, 0, 180));
350 painter->drawText(txtBounding, Qt::AlignCenter, m_clipName);
351 txtBounding.translate(QPointF(1, 1));
352 painter->setPen(QColor(255, 255, 255, 255));
353 painter->drawText(txtBounding, Qt::AlignCenter, m_clipName);
354 // draw frame around clip
356 pen.setColor(Qt::red);
359 pen.setColor(Qt::black);
362 painter->setPen(pen);
363 //painter->setClipRect(option->exposedRect);
364 painter->drawPath(resultClipPath);
366 if (m_hover && br.width() > 30) {
367 painter->setBrush(QColor(180, 180, 50, 180)); //gradient);
369 // draw transitions handles
370 QPainterPath transitionHandle;
371 const int handle_size = 4;
372 transitionHandle.moveTo(0, 0);
373 transitionHandle.lineTo(handle_size, handle_size);
374 transitionHandle.lineTo(handle_size * 2, 0);
375 transitionHandle.lineTo(handle_size * 3, handle_size);
376 transitionHandle.lineTo(handle_size * 2, handle_size * 2);
377 transitionHandle.lineTo(handle_size * 3, handle_size * 3);
378 transitionHandle.lineTo(0, handle_size * 3);
379 transitionHandle.closeSubpath();
380 int pointy = (int)(br.y() + br.height() / 2);
381 int pointx1 = (int)(br.x() + 10);
382 int pointx2 = (int)(br.x() + br.width() - (10 + handle_size * 3));
384 painter->setPen(QPen(Qt::black));
385 painter->setBrush(QBrush(QColor(50, 50, 0)));
387 /*QRadialGradient gradient(pointx1 + 5, pointy + 5 , 5, 2, 2);
388 gradient.setColorAt(0.2, Qt::white);
389 gradient.setColorAt(0.8, Qt::yellow);
390 gradient.setColorAt(1, Qt::black);*/
393 painter->translate(pointx1, pointy);
394 painter->drawPath(transitionHandle); //Ellipse(0, 0 , 10, 10);
395 painter->translate(-pointx1, -pointy);
397 /* QRadialGradient gradient1(pointx2 + 5, pointy + 5 , 5, 2, 2);
398 gradient1.setColorAt(0.2, Qt::white);
399 gradient1.setColorAt(0.8, Qt::yellow);
400 gradient1.setColorAt(1, Qt::black);
401 painter->setBrush(gradient1);*/
402 painter->translate(pointx2, pointy);
405 //painter->setMatrix(m);
406 painter->drawPath(transitionHandle); // Ellipse(0, 0, 10, 10);
407 //painter->setMatrix(m);
408 painter->translate(- pointx2, -pointy);
413 OPERATIONTYPE ClipItem::operationMode(QPointF pos, double scale) {
414 if (qAbs((int)(pos.x() - (rect().x() + scale * m_startFade))) < 6 && qAbs((int)(pos.y() - rect().y())) < 6) return FADEIN;
415 else if (qAbs((int)(pos.x() - rect().x())) < 6) return RESIZESTART;
416 else if (qAbs((int)(pos.x() - (rect().x() + rect().width() - scale * m_endFade))) < 6 && qAbs((int)(pos.y() - rect().y())) < 6) return FADEOUT;
417 else if (qAbs((int)(pos.x() - (rect().x() + rect().width()))) < 6) return RESIZEEND;
418 else if (qAbs((int)(pos.x() - (rect().x() + 16))) < 10 && qAbs((int)(pos.y() - (rect().y() + rect().height() / 2 + 5))) < 8) return TRANSITIONSTART;
419 else if (qAbs((int)(pos.x() - (rect().x() + rect().width() - 21))) < 10 && qAbs((int)(pos.y() - (rect().y() + rect().height() / 2 + 5))) < 8) return TRANSITIONEND;
424 QList <GenTime> ClipItem::snapMarkers() {
425 QList < GenTime > snaps;
426 QList < GenTime > markers = baseClip()->snapMarkers();
430 for (int i = 0; i < markers.size(); i++) {
431 pos = markers.at(i) - cropStart();
432 if (pos > GenTime()) {
433 if (pos > duration()) break;
434 else snaps.append(pos + startPos());
440 void ClipItem::slotPrepareAudioThumb(double pixelForOneFrame, QPainterPath path, int startpixel, int endpixel) {
443 QRectF re = path.boundingRect();
445 //if ( (!audioThumbWasDrawn || framePixelWidth!=pixelForOneFrame ) && !baseClip()->audioFrameChache.isEmpty()){
447 for (int startCache = startpixel - startpixel % 100;startCache + 100 < endpixel ;startCache += 100) {
448 //kDebug() << "creating " << startCache;
449 //if (framePixelWidth!=pixelForOneFrame ||
450 if (framePixelWidth == pixelForOneFrame && audioThumbCachePic.contains(startCache))
452 if (audioThumbCachePic[startCache].isNull() || framePixelWidth != pixelForOneFrame) {
453 audioThumbCachePic[startCache] = QPixmap(100, (int)(re.height()));
454 audioThumbCachePic[startCache].fill(QColor(200, 200, 200, 0));
456 bool fullAreaDraw = pixelForOneFrame < 10;
457 QMap<int, QPainterPath > positiveChannelPaths;
458 QMap<int, QPainterPath > negativeChannelPaths;
459 QPainter pixpainter(&audioThumbCachePic[startCache]);
461 audiopen.setWidth(0);
462 pixpainter.setPen(audiopen);
463 //pixpainter.setRenderHint(QPainter::Antialiasing,true);
464 //pixpainter.drawLine(0,0,100,re.height());
465 int channelHeight = audioThumbCachePic[startCache].height() / channels;
467 for (int i = 0;i < channels;i++) {
469 positiveChannelPaths[i].moveTo(0, channelHeight*i + channelHeight / 2);
470 negativeChannelPaths[i].moveTo(0, channelHeight*i + channelHeight / 2);
473 for (int samples = 0;samples <= 100;samples++) {
474 double frame = (double)(samples + startCache - 0) / pixelForOneFrame;
475 int sample = (int)((frame - (int)(frame)) * 20); // AUDIO_FRAME_SIZE
476 if (frame < 0 || sample < 0 || sample > 19)
478 QMap<int, QByteArray> frame_channel_data = baseClip()->audioFrameChache[(int)frame];
480 for (int channel = 0;channel < channels && frame_channel_data[channel].size() > 0;channel++) {
482 int y = channelHeight * channel + channelHeight / 2;
483 int delta = (int)(frame_channel_data[channel][sample] - 127 / 2) * channelHeight / 64;
485 positiveChannelPaths[channel].lineTo(samples, 0.1 + y + qAbs(delta));
486 negativeChannelPaths[channel].lineTo(samples, 0.1 + y - qAbs(delta));
488 positiveChannelPaths[channel].lineTo(samples, 0.1 + y + delta);
489 negativeChannelPaths[channel].lineTo(samples, 0.1 + y - delta);
492 for (int channel = 0;channel < channels ;channel++)
493 if (fullAreaDraw && samples == 100) {
494 positiveChannelPaths[channel].lineTo(samples, channelHeight*channel + channelHeight / 2);
495 negativeChannelPaths[channel].lineTo(samples, channelHeight*channel + channelHeight / 2);
496 positiveChannelPaths[channel].lineTo(0, channelHeight*channel + channelHeight / 2);
497 negativeChannelPaths[channel].lineTo(0, channelHeight*channel + channelHeight / 2);
501 if (m_clipType != AV) pixpainter.setBrush(QBrush(QColor(200, 200, 100)));
503 pixpainter.setPen(QPen(QColor(0, 0, 0)));
504 pixpainter.setBrush(QBrush(QColor(60, 60, 60)));
506 for (int i = 0;i < channels;i++) {
508 //pixpainter.fillPath(positiveChannelPaths[i].united(negativeChannelPaths[i]),QBrush(Qt::SolidPattern));//or singleif looks better
509 pixpainter.drawPath(positiveChannelPaths[i].united(negativeChannelPaths[i]));//or singleif looks better
511 pixpainter.drawPath(positiveChannelPaths[i]);
514 //audioThumbWasDrawn=true;
515 framePixelWidth = pixelForOneFrame;
520 uint ClipItem::fadeIn() const {
524 uint ClipItem::fadeOut() const {
529 void ClipItem::setFadeIn(int pos, double scale) {
530 int oldIn = m_startFade;
531 if (pos < 0) pos = 0;
532 if (pos > m_cropDuration.frames(m_fps)) pos = (int)(m_cropDuration.frames(m_fps) / 2);
534 update(rect().x(), rect().y(), qMax(oldIn, pos) * scale, rect().height());
537 void ClipItem::setFadeOut(int pos, double scale) {
538 int oldOut = m_endFade;
539 if (pos < 0) pos = 0;
540 if (pos > m_cropDuration.frames(m_fps)) pos = (int)(m_cropDuration.frames(m_fps) / 2);
542 update(rect().x() + rect().width() - qMax(oldOut, pos) * scale, rect().y(), pos * scale, rect().height());
547 void ClipItem::mousePressEvent(QGraphicsSceneMouseEvent * event) {
548 /*m_resizeMode = operationMode(event->pos());
549 if (m_resizeMode == MOVE) {
550 m_maxTrack = scene()->sceneRect().height();
551 m_grabPoint = (int) (event->pos().x() - rect().x());
553 QGraphicsRectItem::mousePressEvent(event);
557 void ClipItem::mouseReleaseEvent(QGraphicsSceneMouseEvent * event) {
559 QGraphicsRectItem::mouseReleaseEvent(event);
563 void ClipItem::hoverEnterEvent(QGraphicsSceneHoverEvent *) {
565 QRectF r = boundingRect();
566 qreal width = qMin(25.0, r.width());
567 update(r.x(), r.y(), width, r.height());
568 update(r.right() - width, r.y(), width, r.height());
572 void ClipItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *) {
574 QRectF r = boundingRect();
575 qreal width = qMin(25.0, r.width());
576 update(r.x(), r.y(), width, r.height());
577 update(r.right() - width, r.y(), width, r.height());
580 void ClipItem::resizeStart(int posx, double scale) {
581 AbstractClipItem::resizeStart(posx, scale);
582 if (m_hasThumbs) startThumbTimer->start(100);
585 void ClipItem::resizeEnd(int posx, double scale) {
586 AbstractClipItem::resizeEnd(posx, scale);
587 if (m_hasThumbs) endThumbTimer->start(100);
591 void ClipItem::mouseMoveEvent(QGraphicsSceneMouseEvent * event) {
594 int ClipItem::effectsCounter() {
595 return m_effectsCounter++;
598 int ClipItem::effectsCount() {
599 return m_effectList.size();
602 QStringList ClipItem::effectNames() {
603 return m_effectList.effectNames();
606 QDomElement ClipItem::effectAt(int ix) {
607 return m_effectList.at(ix);
610 void ClipItem::setEffectAt(int ix, QDomElement effect) {
611 kDebug() << "CHange EFFECT AT: " << ix << ", CURR: " << m_effectList.at(ix).attribute("tag") << ", NEW: " << effect.attribute("tag");
612 m_effectList.insert(ix, effect);
613 m_effectList.removeAt(ix + 1);
614 m_effectNames = m_effectList.effectNames().join(" / ");
615 if (effect.attribute("id") == "fadein" || effect.attribute("id") == "fadeout") update(boundingRect());
617 QRectF r = boundingRect();
623 QMap <QString, QString> ClipItem::addEffect(QDomElement effect, bool animate) {
624 QMap <QString, QString> effectParams;
625 bool needRepaint = false;
627 doc.appendChild(doc.importNode(effect, true));
628 kDebug() << "/////// CLIP ADD EFFECT: "<< doc.toString();*/
629 m_effectList.append(effect);
630 effectParams["tag"] = effect.attribute("tag");
631 QString effectId = effect.attribute("id");
632 if (effectId.isEmpty()) effectId = effect.attribute("tag");
633 effectParams["id"] = effectId;
634 effectParams["kdenlive_ix"] = effect.attribute("kdenlive_ix");
635 QString state = effect.attribute("disabled");
636 if (!state.isEmpty()) effectParams["disabled"] = state;
637 QDomNodeList params = effect.elementsByTagName("parameter");
639 for (int i = 0; i < params.count(); i++) {
640 QDomElement e = params.item(i).toElement();
642 if (e.attribute("factor").isEmpty()) {
643 effectParams[e.attribute("name")] = e.attribute("value");
644 // check if it is a fade effect
645 if (effectId == "fadein") {
647 if (e.attribute("name") == "out") fade += e.attribute("value").toInt();
648 else if (e.attribute("name") == "in") fade -= e.attribute("value").toInt();
649 } else if (effectId == "fadeout") {
651 if (e.attribute("name") == "out") fade -= e.attribute("value").toInt();
652 else if (e.attribute("name") == "in") fade += e.attribute("value").toInt();
655 effectParams[e.attribute("name")] = QString::number(effectParams[e.attribute("name")].toDouble() / e.attribute("factor").toDouble());
659 m_effectNames = m_effectList.effectNames().join(" / ");
660 if (fade > 0) m_startFade = fade;
661 else if (fade < 0) m_endFade = -fade;
662 if (needRepaint) update(boundingRect());
665 } else if (!needRepaint) {
666 QRectF r = boundingRect();
673 QMap <QString, QString> ClipItem::getEffectArgs(QDomElement effect) {
674 QMap <QString, QString> effectParams;
675 effectParams["tag"] = effect.attribute("tag");
676 effectParams["kdenlive_ix"] = effect.attribute("kdenlive_ix");
677 effectParams["id"] = effect.attribute("id");
678 QString state = effect.attribute("disabled");
679 if (!state.isEmpty()) effectParams["disabled"] = state;
680 QDomNodeList params = effect.elementsByTagName("parameter");
681 for (int i = 0; i < params.count(); i++) {
682 QDomElement e = params.item(i).toElement();
683 if (e.attribute("namedesc").contains(";")) {
684 QString format = e.attribute("format");
685 QStringList separators = format.split("%d", QString::SkipEmptyParts);
686 QStringList values = e.attribute("value").split(QRegExp("[,:;x]"));
688 QTextStream txtNeu(&neu);
689 if (values.size() > 0)
690 txtNeu << (int)values[0].toDouble();
691 for (int i = 0;i < separators.size() && i + 1 < values.size();i++) {
692 txtNeu << separators[i];
693 txtNeu << (int)(values[i+1].toDouble());
695 effectParams["start"] = neu;
696 } else if (!e.isNull()) {
697 if (!e.attribute("factor").isEmpty())
698 effectParams[e.attribute("name")] = QString::number(effectParams[e.attribute("name")].toDouble() / e.attribute("factor").toDouble());
699 else effectParams[e.attribute("name")] = e.attribute("value");
705 void ClipItem::deleteEffect(QString index) {
706 bool needRepaint = false;
707 for (int i = 0; i < m_effectList.size(); ++i) {
708 if (m_effectList.at(i).attribute("kdenlive_ix") == index) {
709 if (m_effectList.at(i).attribute("id") == "fadein") {
713 else if (m_effectList.at(i).attribute("id") == "fadeout") {
717 m_effectList.removeAt(i);
721 m_effectNames = m_effectList.effectNames().join(" / ");
722 if (needRepaint) update(boundingRect());
727 void ClipItem::dropEvent(QGraphicsSceneDragDropEvent * event) {
728 QString effects = QString(event->mimeData()->data("kdenlive/effectslist"));
730 doc.setContent(effects, true);
731 QDomElement e = doc.documentElement();
732 CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
733 if (view) view->slotAddEffect(e, m_startPos, track());
737 void ClipItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event) {
738 event->setAccepted(event->mimeData()->hasFormat("kdenlive/effectslist"));
741 void ClipItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event) {
744 void ClipItem::addTransition(Transition* t) {
745 m_transitionsList.append(t);
746 CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
748 QDomElement e = doc.documentElement();
749 //if (view) view->slotAddTransition(this, t->toXML() , t->startPos(), track());
753 void CustomTrackView::mousePressEvent ( QMouseEvent * event )
755 int pos = event->x();
756 if (event->modifiers() == Qt::ControlModifier)
757 setDragMode(QGraphicsView::ScrollHandDrag);
758 else if (event->modifiers() == Qt::ShiftModifier)
759 setDragMode(QGraphicsView::RubberBandDrag);
761 QGraphicsItem * item = itemAt(event->pos());
764 else emit cursorMoved((int) mapToScene(event->x(), 0).x());
767 QGraphicsView::mousePressEvent(event);
770 void CustomTrackView::mouseReleaseEvent ( QMouseEvent * event )
772 QGraphicsView::mouseReleaseEvent(event);
773 setDragMode(QGraphicsView::NoDrag);
777 #include "clipitem.moc"