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;
51 m_maxDuration = clip->maxDuration();
53 audioThumbReady = clip->audioThumbCreated();
55 m_cropStart = xml.attribute("in", 0).toInt();
56 m_maxDuration = xml.attribute("duration", 0).toInt();
57 if (m_maxDuration == 0) m_maxDuration = xml.attribute("out", 0).toInt() - m_cropStart;
59 if (duration != -1) m_cropDuration = duration;
60 else m_cropDuration = m_maxDuration;*/
63 setFlags(QGraphicsItem::ItemClipsToShape | QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
64 setAcceptsHoverEvents(true);
65 connect(this , SIGNAL(prepareAudioThumb(double, QPainterPath, int, int)) , this, SLOT(slotPrepareAudioThumb(double, QPainterPath, int, int)));
67 setBrush(QColor(141, 166, 215));
68 if (m_clipType == VIDEO || m_clipType == AV || m_clipType == SLIDESHOW) {
70 connect(this, SIGNAL(getThumb(int, int)), clip->thumbProducer(), SLOT(extractImage(int, int)));
71 connect(clip->thumbProducer(), SIGNAL(thumbReady(int, QPixmap)), this, SLOT(slotThumbReady(int, QPixmap)));
72 connect(clip, SIGNAL(gotAudioData()), this, SLOT(slotGotAudioData()));
73 QTimer::singleShot(300, this, SLOT(slotFetchThumbs()));
75 startThumbTimer = new QTimer(this);
76 startThumbTimer->setSingleShot(true);
77 connect(startThumbTimer, SIGNAL(timeout()), this, SLOT(slotGetStartThumb()));
78 endThumbTimer = new QTimer(this);
79 endThumbTimer->setSingleShot(true);
80 connect(endThumbTimer, SIGNAL(timeout()), this, SLOT(slotGetEndThumb()));
81 } else if (m_clipType == COLOR) {
82 QString colour = clip->getProperty("colour");
83 colour = colour.replace(0, 2, "#");
84 setBrush(QColor(colour.left(7)));
85 } else if (m_clipType == IMAGE || m_clipType == TEXT) {
86 m_startPix = KThumb::getImage(KUrl(clip->getProperty("resource")), (int)(50 * KdenliveSettings::project_display_ratio()), 50);
87 m_endPix = m_startPix;
88 } else if (m_clipType == AUDIO) {
89 connect(clip, SIGNAL(gotAudioData()), this, SLOT(slotGotAudioData()));
94 ClipItem::~ClipItem() {
95 if (startThumbTimer) delete startThumbTimer;
96 if (endThumbTimer) delete endThumbTimer;
99 void ClipItem::resetThumbs() {
101 audioThumbCachePic.clear();
105 void ClipItem::refreshClip() {
106 m_maxDuration = m_clip->maxDuration();
107 if (m_clipType == VIDEO || m_clipType == AV || m_clipType == SLIDESHOW) slotFetchThumbs();
108 else if (m_clipType == COLOR) {
109 QString colour = m_clip->getProperty("colour");
110 colour = colour.replace(0, 2, "#");
111 setBrush(QColor(colour.left(7)));
112 } else if (m_clipType == IMAGE || m_clipType == TEXT) {
113 m_startPix = KThumb::getImage(KUrl(m_clip->getProperty("resource")), (int)(50 * KdenliveSettings::project_display_ratio()), 50);
114 m_endPix = m_startPix;
118 void ClipItem::slotFetchThumbs() {
119 m_thumbsRequested += 2;
120 emit getThumb((int)m_cropStart.frames(m_fps), (int)(m_cropStart + m_cropDuration).frames(m_fps));
123 void ClipItem::slotGetStartThumb() {
125 emit getThumb((int)m_cropStart.frames(m_fps), -1);
128 void ClipItem::slotGetEndThumb() {
130 emit getThumb(-1, (int)(m_cropStart + m_cropDuration).frames(m_fps));
133 void ClipItem::slotThumbReady(int frame, QPixmap pix) {
134 if (m_thumbsRequested == 0) return;
135 if (frame == m_cropStart.frames(m_fps)) {
137 QRectF r = boundingRect();
138 r.setRight(pix.width() + 2);
142 QRectF r = boundingRect();
143 r.setLeft(r.right() - pix.width() - 2);
149 void ClipItem::slotGotAudioData() {
150 audioThumbReady = true;
151 if (m_clipType == AV) {
152 QRectF r = boundingRect();
153 r.setTop(r.top() + r.height() / 2 - 1);
158 int ClipItem::type() const {
162 DocClipBase *ClipItem::baseClip() {
166 QDomElement ClipItem::xml() const {
167 return m_clip->toXML();
170 int ClipItem::clipType() {
174 QString ClipItem::clipName() {
178 int ClipItem::clipProducer() {
182 void ClipItem::flashClip() {
183 if (m_timeLine == 0) {
184 m_timeLine = new QTimeLine(750, this);
185 m_timeLine->setCurveShape(QTimeLine::EaseInOutCurve);
186 connect(m_timeLine, SIGNAL(valueChanged(qreal)), this, SLOT(animate(qreal)));
191 void ClipItem::animate(qreal value) {
192 QRectF r = boundingRect();
198 void ClipItem::paint(QPainter *painter,
199 const QStyleOptionGraphicsItem *option,
201 painter->setOpacity(m_opacity);
202 QBrush paintColor = brush();
203 if (isSelected()) paintColor = QBrush(QColor(79, 93, 121));
205 double scale = br.width() / m_cropDuration.frames(m_fps);
207 // kDebug()<<"/// EXPOSED RECT: "<<option->exposedRect.x()<<" X "<<option->exposedRect.right();
208 painter->setClipRect(option->exposedRect);
209 int startpixel = (int)option->exposedRect.x() - rect().x();
213 int endpixel = (int)option->exposedRect.right() - rect().x();
217 //painter->setRenderHints(QPainter::Antialiasing);
219 QPainterPath roundRectPathUpper = upperRectPart(br), roundRectPathLower = lowerRectPart(br);
222 // build path around clip
223 QPainterPath resultClipPath = roundRectPathUpper.united(roundRectPathLower);
224 painter->fillPath(resultClipPath, paintColor);
227 if (!m_startPix.isNull() && KdenliveSettings::videothumbnails()) {
228 if (m_clipType == IMAGE) {
229 painter->drawPixmap(QPointF(br.x() + br.width() - m_startPix.width(), br.y()), m_startPix);
230 QLineF l(br.x() + br.width() - m_startPix.width(), br.y(), br.x() + br.width() - m_startPix.width(), br.y() + br.height());
231 painter->drawLine(l);
233 painter->drawPixmap(QPointF(br.x() + br.width() - m_endPix.width(), br.y()), m_endPix);
234 QLineF l(br.x() + br.width() - m_endPix.width(), br.y(), br.x() + br.width() - m_endPix.width(), br.y() + br.height());
235 painter->drawLine(l);
238 painter->drawPixmap(QPointF(br.x(), br.y()), m_startPix);
239 QLineF l2(br.x() + m_startPix.width(), br.y(), br.x() + m_startPix.width(), br.y() + br.height());
240 painter->drawLine(l2);
243 // draw audio thumbnails
244 if (KdenliveSettings::audiothumbnails() && ((m_clipType == AV && option->exposedRect.bottom() > br.height() / 2) || m_clipType == AUDIO) && audioThumbReady) {
246 QPainterPath path = m_clipType == AV ? roundRectPathLower : resultClipPath;
247 if (m_clipType == AV) painter->fillPath(path, QBrush(QColor(200, 200, 200, 140)));
250 if (scale != framePixelWidth)
251 audioThumbCachePic.clear();
252 emit prepareAudioThumb(scale, path, startpixel, endpixel + 200);//200 more for less missing parts before repaint after scrolling
253 int cropLeft = (int)((m_cropStart).frames(m_fps) * scale);
254 for (int startCache = startpixel - startpixel % 100; startCache < endpixel + 300;startCache += 100) {
255 if (audioThumbCachePic.contains(startCache) && !audioThumbCachePic[startCache].isNull())
256 painter->drawPixmap((int)(roundRectPathUpper.united(roundRectPathLower).boundingRect().x() + startCache - cropLeft), (int)(path.boundingRect().y()), audioThumbCachePic[startCache]);
261 QList < CommentedTime > markers = baseClip()->commentedSnapMarkers();
262 QList < CommentedTime >::Iterator it = markers.begin();
265 const int markerwidth = 4;
267 markerBrush = QBrush(QColor(120, 120, 0, 100));
268 QPen pen = painter->pen();
269 pen.setColor(QColor(255, 255, 255, 200));
270 pen.setStyle(Qt::DotLine);
271 painter->setPen(pen);
272 for (; it != markers.end(); ++it) {
273 pos = (*it).time() - cropStart();
274 if (pos > GenTime()) {
275 if (pos > duration()) break;
276 framepos = scale * pos.frames(m_fps);
277 QLineF l(br.x() + framepos, br.y() + 5, br.x() + framepos, br.y() + br.height() - 5);
278 painter->drawLine(l);
279 if (KdenliveSettings::showmarkers()) {
280 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() + " ");
282 path.addRoundedRect(txtBounding, 3, 3);
283 painter->fillPath(path, markerBrush);
284 painter->drawText(txtBounding, Qt::AlignCenter, (*it).comment());
286 //painter->fillRect(QRect(br.x() + framepos, br.y(), 10, br.height()), QBrush(QColor(0, 0, 0, 150)));
289 pen.setColor(Qt::black);
290 pen.setStyle(Qt::SolidLine);
291 painter->setPen(pen);
293 // draw start / end fades
296 fades = QBrush(QColor(200, 50, 50, 150));
297 } else fades = QBrush(QColor(200, 200, 200, 200));
299 if (m_startFade != 0) {
300 QPainterPath fadeInPath;
301 fadeInPath.moveTo(br.x() , br.y());
302 fadeInPath.lineTo(br.x() , br.bottom());
303 fadeInPath.lineTo(br.x() + m_startFade * scale, br.y());
304 fadeInPath.closeSubpath();
305 painter->fillPath(fadeInPath.intersected(resultClipPath), fades);
307 QLineF l(br.x() + m_startFade * scale, br.y(), br.x(), br.bottom());
308 painter->drawLine(l);
311 if (m_endFade != 0) {
312 QPainterPath fadeOutPath;
313 fadeOutPath.moveTo(br.right(), br.y());
314 fadeOutPath.lineTo(br.right(), br.bottom());
315 fadeOutPath.lineTo(br.right() - m_endFade * scale, br.y());
316 fadeOutPath.closeSubpath();
317 painter->fillPath(fadeOutPath.intersected(resultClipPath), fades);
319 QLineF l(br.right() - m_endFade * scale, br.y(), br.x() + br.width(), br.bottom());
320 painter->drawLine(l);
324 // Draw effects names
325 if (!m_effectNames.isEmpty() && br.width() > 30) {
326 QRectF txtBounding = painter->boundingRect(br, Qt::AlignLeft | Qt::AlignTop, m_effectNames);
327 txtBounding.setRight(txtBounding.right() + 15);
328 painter->setPen(Qt::white);
329 QBrush markerBrush(Qt::SolidPattern);
330 if (m_timeLine && m_timeLine->state() == QTimeLine::Running) {
331 qreal value = m_timeLine->currentValue();
332 txtBounding.setWidth(txtBounding.width() * value);
333 markerBrush.setColor(QColor(50 + 200 * (1.0 - value), 50, 50, 100 + 50 * value));
334 } else markerBrush.setColor(QColor(50, 50, 50, 150));
336 path.addRoundedRect(txtBounding, 4, 4);
337 painter->fillPath(path.intersected(resultClipPath), markerBrush);
338 painter->drawText(txtBounding, Qt::AlignCenter, m_effectNames);
339 painter->setPen(Qt::black);
343 QRectF txtBounding = painter->boundingRect(br, Qt::AlignHCenter | Qt::AlignTop, " " + m_clipName + " ");
344 //painter->fillRect(txtBounding, QBrush(QColor(255, 255, 255, 150)));
345 painter->setPen(QColor(0, 0, 0, 180));
346 painter->drawText(txtBounding, Qt::AlignCenter, m_clipName);
347 txtBounding.translate(QPointF(1, 1));
348 painter->setPen(QColor(255, 255, 255, 255));
349 painter->drawText(txtBounding, Qt::AlignCenter, m_clipName);
350 // draw frame around clip
352 pen.setColor(Qt::red);
355 pen.setColor(Qt::black);
358 painter->setPen(pen);
359 //painter->setClipRect(option->exposedRect);
360 painter->drawPath(resultClipPath);
362 if (m_hover && br.width() > 30) {
363 painter->setBrush(QColor(180, 180, 50, 180)); //gradient);
365 // draw transitions handles
366 QPainterPath transitionHandle;
367 const int handle_size = 4;
368 transitionHandle.moveTo(0, 0);
369 transitionHandle.lineTo(handle_size, handle_size);
370 transitionHandle.lineTo(handle_size * 2, 0);
371 transitionHandle.lineTo(handle_size * 3, handle_size);
372 transitionHandle.lineTo(handle_size * 2, handle_size * 2);
373 transitionHandle.lineTo(handle_size * 3, handle_size * 3);
374 transitionHandle.lineTo(0, handle_size * 3);
375 transitionHandle.closeSubpath();
376 int pointy = (int)(br.y() + br.height() / 2);
377 int pointx1 = (int)(br.x() + 10);
378 int pointx2 = (int)(br.x() + br.width() - (10 + handle_size * 3));
380 painter->setPen(QPen(Qt::black));
381 painter->setBrush(QBrush(QColor(50, 50, 0)));
383 /*QRadialGradient gradient(pointx1 + 5, pointy + 5 , 5, 2, 2);
384 gradient.setColorAt(0.2, Qt::white);
385 gradient.setColorAt(0.8, Qt::yellow);
386 gradient.setColorAt(1, Qt::black);*/
389 painter->translate(pointx1, pointy);
390 painter->drawPath(transitionHandle); //Ellipse(0, 0 , 10, 10);
391 painter->translate(-pointx1, -pointy);
393 /* QRadialGradient gradient1(pointx2 + 5, pointy + 5 , 5, 2, 2);
394 gradient1.setColorAt(0.2, Qt::white);
395 gradient1.setColorAt(0.8, Qt::yellow);
396 gradient1.setColorAt(1, Qt::black);
397 painter->setBrush(gradient1);*/
398 painter->translate(pointx2, pointy);
401 //painter->setMatrix(m);
402 painter->drawPath(transitionHandle); // Ellipse(0, 0, 10, 10);
403 //painter->setMatrix(m);
404 painter->translate(- pointx2, -pointy);
409 OPERATIONTYPE ClipItem::operationMode(QPointF pos, double scale) {
410 if (qAbs((int)(pos.x() - (rect().x() + scale * m_startFade))) < 6 && qAbs((int)(pos.y() - rect().y())) < 6) return FADEIN;
411 else if (qAbs((int)(pos.x() - rect().x())) < 6) return RESIZESTART;
412 else if (qAbs((int)(pos.x() - (rect().x() + rect().width() - scale * m_endFade))) < 6 && qAbs((int)(pos.y() - rect().y())) < 6) return FADEOUT;
413 else if (qAbs((int)(pos.x() - (rect().x() + rect().width()))) < 6) return RESIZEEND;
414 else if (qAbs((int)(pos.x() - (rect().x() + 16))) < 10 && qAbs((int)(pos.y() - (rect().y() + rect().height() / 2 + 5))) < 8) return TRANSITIONSTART;
415 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;
420 QList <GenTime> ClipItem::snapMarkers() {
421 QList < GenTime > snaps;
422 QList < GenTime > markers = baseClip()->snapMarkers();
426 for (int i = 0; i < markers.size(); i++) {
427 pos = markers.at(i) - cropStart();
428 if (pos > GenTime()) {
429 if (pos > duration()) break;
430 else snaps.append(pos + startPos());
436 void ClipItem::slotPrepareAudioThumb(double pixelForOneFrame, QPainterPath path, int startpixel, int endpixel) {
439 QRectF re = path.boundingRect();
441 //if ( (!audioThumbWasDrawn || framePixelWidth!=pixelForOneFrame ) && !baseClip()->audioFrameChache.isEmpty()){
443 for (int startCache = startpixel - startpixel % 100;startCache + 100 < endpixel ;startCache += 100) {
444 //kDebug() << "creating " << startCache;
445 //if (framePixelWidth!=pixelForOneFrame ||
446 if (framePixelWidth == pixelForOneFrame && audioThumbCachePic.contains(startCache))
448 if (audioThumbCachePic[startCache].isNull() || framePixelWidth != pixelForOneFrame) {
449 audioThumbCachePic[startCache] = QPixmap(100, (int)(re.height()));
450 audioThumbCachePic[startCache].fill(QColor(200, 200, 200, 0));
452 bool fullAreaDraw = pixelForOneFrame < 10;
453 QMap<int, QPainterPath > positiveChannelPaths;
454 QMap<int, QPainterPath > negativeChannelPaths;
455 QPainter pixpainter(&audioThumbCachePic[startCache]);
457 audiopen.setWidth(0);
458 pixpainter.setPen(audiopen);
459 //pixpainter.setRenderHint(QPainter::Antialiasing,true);
460 //pixpainter.drawLine(0,0,100,re.height());
461 int channelHeight = audioThumbCachePic[startCache].height() / channels;
463 for (int i = 0;i < channels;i++) {
465 positiveChannelPaths[i].moveTo(0, channelHeight*i + channelHeight / 2);
466 negativeChannelPaths[i].moveTo(0, channelHeight*i + channelHeight / 2);
469 for (int samples = 0;samples <= 100;samples++) {
470 double frame = (double)(samples + startCache - 0) / pixelForOneFrame;
471 int sample = (int)((frame - (int)(frame)) * 20); // AUDIO_FRAME_SIZE
472 if (frame < 0 || sample < 0 || sample > 19)
474 QMap<int, QByteArray> frame_channel_data = baseClip()->audioFrameChache[(int)frame];
476 for (int channel = 0;channel < channels && frame_channel_data[channel].size() > 0;channel++) {
478 int y = channelHeight * channel + channelHeight / 2;
479 int delta = (int)(frame_channel_data[channel][sample] - 127 / 2) * channelHeight / 64;
481 positiveChannelPaths[channel].lineTo(samples, 0.1 + y + qAbs(delta));
482 negativeChannelPaths[channel].lineTo(samples, 0.1 + y - qAbs(delta));
484 positiveChannelPaths[channel].lineTo(samples, 0.1 + y + delta);
485 negativeChannelPaths[channel].lineTo(samples, 0.1 + y - delta);
488 for (int channel = 0;channel < channels ;channel++)
489 if (fullAreaDraw && samples == 100) {
490 positiveChannelPaths[channel].lineTo(samples, channelHeight*channel + channelHeight / 2);
491 negativeChannelPaths[channel].lineTo(samples, channelHeight*channel + channelHeight / 2);
492 positiveChannelPaths[channel].lineTo(0, channelHeight*channel + channelHeight / 2);
493 negativeChannelPaths[channel].lineTo(0, channelHeight*channel + channelHeight / 2);
497 if (m_clipType != AV) pixpainter.setBrush(QBrush(QColor(200, 200, 100)));
499 pixpainter.setPen(QPen(QColor(0, 0, 0)));
500 pixpainter.setBrush(QBrush(QColor(60, 60, 60)));
502 for (int i = 0;i < channels;i++) {
504 //pixpainter.fillPath(positiveChannelPaths[i].united(negativeChannelPaths[i]),QBrush(Qt::SolidPattern));//or singleif looks better
505 pixpainter.drawPath(positiveChannelPaths[i].united(negativeChannelPaths[i]));//or singleif looks better
507 pixpainter.drawPath(positiveChannelPaths[i]);
510 //audioThumbWasDrawn=true;
511 framePixelWidth = pixelForOneFrame;
516 uint ClipItem::fadeIn() const {
520 uint ClipItem::fadeOut() const {
525 void ClipItem::setFadeIn(int pos, double scale) {
526 int oldIn = m_startFade;
527 if (pos < 0) pos = 0;
528 if (pos > m_cropDuration.frames(m_fps)) pos = (int)(m_cropDuration.frames(m_fps) / 2);
530 update(rect().x(), rect().y(), qMax(oldIn, pos) * scale, rect().height());
533 void ClipItem::setFadeOut(int pos, double scale) {
534 int oldOut = m_endFade;
535 if (pos < 0) pos = 0;
536 if (pos > m_cropDuration.frames(m_fps)) pos = (int)(m_cropDuration.frames(m_fps) / 2);
538 update(rect().x() + rect().width() - qMax(oldOut, pos) * scale, rect().y(), pos * scale, rect().height());
543 void ClipItem::mousePressEvent(QGraphicsSceneMouseEvent * event) {
544 /*m_resizeMode = operationMode(event->pos());
545 if (m_resizeMode == MOVE) {
546 m_maxTrack = scene()->sceneRect().height();
547 m_grabPoint = (int) (event->pos().x() - rect().x());
549 QGraphicsRectItem::mousePressEvent(event);
553 void ClipItem::mouseReleaseEvent(QGraphicsSceneMouseEvent * event) {
555 QGraphicsRectItem::mouseReleaseEvent(event);
559 void ClipItem::hoverEnterEvent(QGraphicsSceneHoverEvent *) {
561 QRectF r = boundingRect();
562 qreal width = qMin(25.0, r.width());
563 update(r.x(), r.y(), width, r.height());
564 update(r.right() - width, r.y(), width, r.height());
568 void ClipItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *) {
570 QRectF r = boundingRect();
571 qreal width = qMin(25.0, r.width());
572 update(r.x(), r.y(), width, r.height());
573 update(r.right() - width, r.y(), width, r.height());
576 void ClipItem::resizeStart(int posx, double scale) {
577 AbstractClipItem::resizeStart(posx, scale);
578 if (m_hasThumbs) startThumbTimer->start(100);
581 void ClipItem::resizeEnd(int posx, double scale) {
582 AbstractClipItem::resizeEnd(posx, scale);
583 if (m_hasThumbs) endThumbTimer->start(100);
587 void ClipItem::mouseMoveEvent(QGraphicsSceneMouseEvent * event) {
590 int ClipItem::effectsCounter() {
591 return m_effectsCounter++;
594 int ClipItem::effectsCount() {
595 return m_effectList.size();
598 QStringList ClipItem::effectNames() {
599 return m_effectList.effectNames();
602 QDomElement ClipItem::effectAt(int ix) {
603 return m_effectList.at(ix);
606 void ClipItem::setEffectAt(int ix, QDomElement effect) {
607 kDebug() << "CHange EFFECT AT: " << ix << ", CURR: " << m_effectList.at(ix).attribute("tag") << ", NEW: " << effect.attribute("tag");
608 m_effectList.insert(ix, effect);
609 m_effectList.removeAt(ix + 1);
610 m_effectNames = m_effectList.effectNames().join(" / ");
611 if (effect.attribute("id") == "fadein" || effect.attribute("id") == "fadeout") update(boundingRect());
613 QRectF r = boundingRect();
619 QMap <QString, QString> ClipItem::addEffect(QDomElement effect, bool animate) {
620 QMap <QString, QString> effectParams;
621 bool needRepaint = false;
623 doc.appendChild(doc.importNode(effect, true));
624 kDebug() << "/////// CLIP ADD EFFECT: "<< doc.toString();*/
625 m_effectList.append(effect);
626 effectParams["tag"] = effect.attribute("tag");
627 QString effectId = effect.attribute("id");
628 if (effectId.isEmpty()) effectId = effect.attribute("tag");
629 effectParams["id"] = effectId;
630 effectParams["kdenlive_ix"] = effect.attribute("kdenlive_ix");
631 QString state = effect.attribute("disabled");
632 if (!state.isEmpty()) effectParams["disabled"] = state;
633 QDomNodeList params = effect.elementsByTagName("parameter");
635 for (int i = 0; i < params.count(); i++) {
636 QDomElement e = params.item(i).toElement();
638 if (e.attribute("factor").isEmpty()) {
639 effectParams[e.attribute("name")] = e.attribute("value");
640 // check if it is a fade effect
641 if (effectId == "fadein") {
643 if (e.attribute("name") == "out") fade += e.attribute("value").toInt();
644 else if (e.attribute("name") == "in") fade -= e.attribute("value").toInt();
645 } else if (effectId == "fadeout") {
647 if (e.attribute("name") == "out") fade -= e.attribute("value").toInt();
648 else if (e.attribute("name") == "in") fade += e.attribute("value").toInt();
651 effectParams[e.attribute("name")] = QString::number(effectParams[e.attribute("name")].toDouble() / e.attribute("factor").toDouble());
655 m_effectNames = m_effectList.effectNames().join(" / ");
656 if (fade > 0) m_startFade = fade;
657 else if (fade < 0) m_endFade = -fade;
658 if (needRepaint) update(boundingRect());
661 } else if (!needRepaint) {
662 QRectF r = boundingRect();
669 QMap <QString, QString> ClipItem::getEffectArgs(QDomElement effect) {
670 QMap <QString, QString> effectParams;
671 effectParams["tag"] = effect.attribute("tag");
672 effectParams["kdenlive_ix"] = effect.attribute("kdenlive_ix");
673 effectParams["id"] = effect.attribute("id");
674 QString state = effect.attribute("disabled");
675 if (!state.isEmpty()) effectParams["disabled"] = state;
676 QDomNodeList params = effect.elementsByTagName("parameter");
677 for (int i = 0; i < params.count(); i++) {
678 QDomElement e = params.item(i).toElement();
679 if (e.attribute("namedesc").contains(";")) {
680 QString format = e.attribute("format");
681 QStringList separators = format.split("%d", QString::SkipEmptyParts);
682 QStringList values = e.attribute("value").split(QRegExp("[,:;x]"));
684 QTextStream txtNeu(&neu);
685 if (values.size() > 0)
686 txtNeu << (int)values[0].toDouble();
687 for (int i = 0;i < separators.size() && i + 1 < values.size();i++) {
688 txtNeu << separators[i];
689 txtNeu << (int)(values[i+1].toDouble());
691 effectParams["start"] = neu;
692 } else if (!e.isNull()) {
693 if (!e.attribute("factor").isEmpty())
694 effectParams[e.attribute("name")] = QString::number(effectParams[e.attribute("name")].toDouble() / e.attribute("factor").toDouble());
695 else effectParams[e.attribute("name")] = e.attribute("value");
701 void ClipItem::deleteEffect(QString index) {
702 bool needRepaint = false;
703 for (int i = 0; i < m_effectList.size(); ++i) {
704 if (m_effectList.at(i).attribute("kdenlive_ix") == index) {
705 if (m_effectList.at(i).attribute("id") == "fadein") {
708 } else if (m_effectList.at(i).attribute("id") == "fadeout") {
712 m_effectList.removeAt(i);
716 m_effectNames = m_effectList.effectNames().join(" / ");
717 if (needRepaint) update(boundingRect());
722 void ClipItem::dropEvent(QGraphicsSceneDragDropEvent * event) {
723 QString effects = QString(event->mimeData()->data("kdenlive/effectslist"));
725 doc.setContent(effects, true);
726 QDomElement e = doc.documentElement();
727 CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
728 if (view) view->slotAddEffect(e, m_startPos, track());
732 void ClipItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event) {
733 event->setAccepted(event->mimeData()->hasFormat("kdenlive/effectslist"));
736 void ClipItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event) {
739 void ClipItem::addTransition(Transition* t) {
740 m_transitionsList.append(t);
741 CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
743 QDomElement e = doc.documentElement();
744 //if (view) view->slotAddTransition(this, t->toXML() , t->startPos(), track());
748 void CustomTrackView::mousePressEvent ( QMouseEvent * event )
750 int pos = event->x();
751 if (event->modifiers() == Qt::ControlModifier)
752 setDragMode(QGraphicsView::ScrollHandDrag);
753 else if (event->modifiers() == Qt::ShiftModifier)
754 setDragMode(QGraphicsView::RubberBandDrag);
756 QGraphicsItem * item = itemAt(event->pos());
759 else emit cursorMoved((int) mapToScene(event->x(), 0).x());
762 QGraphicsView::mousePressEvent(event);
765 void CustomTrackView::mouseReleaseEvent ( QMouseEvent * event )
767 QGraphicsView::mouseReleaseEvent(event);
768 setDragMode(QGraphicsView::NoDrag);
772 #include "clipitem.moc"