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>
33 #include <mlt++/Mlt.h>
36 #include "customtrackview.h"
39 #include "kdenlivesettings.h"
41 ClipItem::ClipItem(DocClipBase *clip, int track, GenTime startpos, const QRectF & rect, GenTime duration, double fps)
42 : QGraphicsRectItem(rect), m_clip(clip), m_resizeMode(NONE), m_grabPoint(0), m_maxTrack(0), m_track(track), m_startPos(startpos), m_hasThumbs(false), startThumbTimer(NULL), endThumbTimer(NULL), m_startFade(0), m_endFade(0), m_effectsCounter(1), audioThumbWasDrawn(false), m_opacity(1.0), m_timeLine(0), m_thumbsRequested(0), m_fps(fps), m_hover(false) {
44 // kDebug() << "******* CREATING NEW TML CLIP, DUR: " << duration;
45 m_xml = clip->toXML();
46 m_clipName = clip->name();
47 m_producer = clip->getId();
48 m_clipType = clip->clipType();
49 m_cropStart = GenTime();
50 m_maxDuration = duration;
51 if (duration != GenTime()) m_cropDuration = duration;
52 else m_cropDuration = m_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) {
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()));
83 } else if (m_clipType == COLOR) {
84 QString colour = m_xml.attribute("colour");
85 colour = colour.replace(0, 2, "#");
86 setBrush(QColor(colour.left(7)));
87 } else if (m_clipType == IMAGE) {
88 m_startPix = KThumb::getImage(KUrl(m_xml.attribute("resource")), (int)(50 * KdenliveSettings::project_display_ratio()), 50);
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::slotFetchThumbs() {
101 m_thumbsRequested += 2;
102 emit getThumb((int)m_cropStart.frames(m_fps), (int)(m_cropStart + m_cropDuration).frames(m_fps));
105 void ClipItem::slotGetStartThumb() {
107 emit getThumb((int)m_cropStart.frames(m_fps), -1);
110 void ClipItem::slotGetEndThumb() {
112 emit getThumb(-1, (int)(m_cropStart + m_cropDuration).frames(m_fps));
115 void ClipItem::slotThumbReady(int frame, QPixmap pix) {
116 if (m_thumbsRequested == 0) return;
117 if (frame == m_cropStart.frames(m_fps)) m_startPix = pix;
123 void ClipItem::slotGotAudioData() {
124 audioThumbReady = true;
128 int ClipItem::type() const {
132 DocClipBase *ClipItem::baseClip() {
136 QDomElement ClipItem::xml() const {
140 int ClipItem::clipType() {
144 QString ClipItem::clipName() {
148 int ClipItem::clipProducer() {
152 GenTime ClipItem::maxDuration() const {
153 return m_maxDuration;
156 GenTime ClipItem::duration() const {
157 return m_cropDuration;
160 GenTime ClipItem::startPos() const {
164 GenTime ClipItem::cropStart() const {
168 GenTime ClipItem::endPos() const {
169 return m_startPos + m_cropDuration;
172 double ClipItem::fps() const {
176 void ClipItem::flashClip() {
177 if (m_timeLine == 0) {
178 m_timeLine = new QTimeLine(750, this);
179 connect(m_timeLine, SIGNAL(valueChanged(qreal)), this, SLOT(animate(qreal)));
184 void ClipItem::animate(qreal value) {
190 void ClipItem::paint(QPainter *painter,
191 const QStyleOptionGraphicsItem *option,
193 painter->setOpacity(m_opacity);
194 QBrush paintColor = brush();
195 if (isSelected()) paintColor = QBrush(QColor(79, 93, 121));
197 QRect rectInView;//this is the rect that is visible by the user
198 if (scene()->views().size() > 0) {
199 rectInView = scene()->views()[0]->viewport()->rect();
200 rectInView.moveTo(scene()->views()[0]->horizontalScrollBar()->value(), scene()->views()[0]->verticalScrollBar()->value());
201 rectInView.adjust(-10, -10, 10, 10);//make view rect 10 pixel greater on each site, or repaint after scroll event
202 //kDebug() << scene()->views()[0]->viewport()->rect() << " " << scene()->views()[0]->horizontalScrollBar()->value();
204 if (rectInView.isNull())
206 QPainterPath clippath;
207 clippath.addRect(rectInView);
209 int startpixel = (int)(rectInView.x() - rect().x()); //start and endpixel that is viewable from rect()
212 int endpixel = rectInView.width() + rectInView.x();
216 //painter->setRenderHints(QPainter::Antialiasing);
218 QPainterPath roundRectPathUpper, roundRectPathLower;
219 double roundingY = 20;
220 double roundingX = 20;
222 painter->setClipRect(option->exposedRect);
223 if (roundingX > br.width() / 2) roundingX = br.width() / 2;
225 int br_endx = (int)(br.x() + br .width() - offset);
226 int br_startx = (int)(br.x() + offset);
227 int br_starty = (int)(br.y());
228 int br_halfy = (int)(br.y() + br.height() / 2 - offset);
229 int br_endy = (int)(br.y() + br.height());
230 int left_upper = 0, left_lower = 0, right_upper = 0, right_lower = 0;
233 if (!true) /*TRANSITIONSTART to upper clip*/
235 if (!false) /*TRANSITIONSTART to lower clip*/
237 if (!true) /*TRANSITIONEND to upper clip*/
239 if (!false) /*TRANSITIONEND to lower clip*/
243 // build path around clip
244 roundRectPathUpper.moveTo(br_endx - right_upper , br_halfy);
245 roundRectPathUpper.arcTo(br_endx - roundingX - right_upper , br_starty , roundingX, roundingY, 0.0, 90.0);
246 roundRectPathUpper.lineTo(br_startx + roundingX + left_upper, br_starty);
247 roundRectPathUpper.arcTo(br_startx + left_upper, br_starty , roundingX, roundingY, 90.0, 90.0);
248 roundRectPathUpper.lineTo(br_startx + left_upper, br_halfy);
250 roundRectPathLower.moveTo(br_startx + left_lower, br_halfy);
251 roundRectPathLower.arcTo(br_startx + left_lower, br_endy - roundingY , roundingX, roundingY, 180.0, 90.0);
252 roundRectPathLower.lineTo(br_endx - roundingX - right_lower , br_endy);
253 roundRectPathLower.arcTo(br_endx - roundingX - right_lower , br_endy - roundingY, roundingX, roundingY, 270.0, 90.0);
254 roundRectPathLower.lineTo(br_endx - right_lower , br_halfy);
256 painter->setClipPath(roundRectPathUpper.united(roundRectPathLower).intersected(clippath), Qt::IntersectClip);
257 //painter->fillPath(roundRectPath, brush()); //, QBrush(QColor(Qt::red)));
258 painter->fillRect(br.intersected(rectInView), paintColor);
259 //painter->fillRect(QRectF(br.x() + br.width() - m_endPix.width(), br.y(), m_endPix.width(), br.height()), QBrush(QColor(Qt::black)));
262 if (!m_startPix.isNull() && KdenliveSettings::videothumbnails()) {
263 if (m_clipType == IMAGE) {
264 painter->drawPixmap(QPointF(br.x() + br.width() - m_startPix.width(), br.y()), m_startPix);
265 QLineF l(br.x() + br.width() - m_startPix.width(), br.y(), br.x() + br.width() - m_startPix.width(), br.y() + br.height());
266 painter->drawLine(l);
268 painter->drawPixmap(QPointF(br.x() + br.width() - m_endPix.width(), br.y()), m_endPix);
269 QLineF l(br.x() + br.width() - m_endPix.width(), br.y(), br.x() + br.width() - m_endPix.width(), br.y() + br.height());
270 painter->drawLine(l);
273 painter->drawPixmap(QPointF(br.x(), br.y()), m_startPix);
274 QLineF l2(br.x() + m_startPix.width(), br.y(), br.x() + m_startPix.width(), br.y() + br.height());
275 painter->drawLine(l2);
278 // draw audio thumbnails
279 if ((m_clipType == AV || m_clipType == AUDIO) && audioThumbReady && KdenliveSettings::audiothumbnails()) {
281 QPainterPath path = m_clipType == AV ? roundRectPathLower : roundRectPathUpper.united(roundRectPathLower);
282 if (m_clipType == AV) painter->fillPath(path, QBrush(QColor(200, 200, 200, 140)));
285 double pixelForOneFrame = (double)br.width() / duration().frames(m_fps);
286 if (pixelForOneFrame != framePixelWidth)
287 audioThumbCachePic.clear();
288 emit prepareAudioThumb(pixelForOneFrame, path, startpixel, endpixel + 200);//200 more for less missing parts before repaint after scrolling
290 for (int startCache = startpixel - startpixel % 100; startCache < endpixel + 300;startCache += 100) {
291 if (audioThumbCachePic.contains(startCache) && !audioThumbCachePic[startCache].isNull())
292 painter->drawPixmap((int)(roundRectPathUpper.united(roundRectPathLower).boundingRect().x() + startCache), (int)(path.boundingRect().y()), audioThumbCachePic[startCache]);
297 // draw start / end fades
298 double scale = br.width() / m_cropDuration.frames(m_fps);
301 fades = QBrush(QColor(200, 50, 50, 150));
302 } else fades = QBrush(QColor(200, 200, 200, 200));
304 if (m_startFade != 0) {
305 QPainterPath fadeInPath;
306 fadeInPath.moveTo(br.x() - offset, br.y());
307 fadeInPath.lineTo(br.x() - offset, br.y() + br.height());
308 fadeInPath.lineTo(br.x() + m_startFade * scale, br.y());
309 fadeInPath.closeSubpath();
310 painter->fillPath(fadeInPath, fades);
312 QLineF l(br.x() + m_startFade * scale, br.y(), br.x(), br.y() + br.height());
313 painter->drawLine(l);
316 if (m_endFade != 0) {
317 QPainterPath fadeOutPath;
318 fadeOutPath.moveTo(br.x() + br.width(), br.y());
319 fadeOutPath.lineTo(br.x() + br.width(), br.y() + br.height());
320 fadeOutPath.lineTo(br.x() + br.width() - m_endFade * scale, br.y());
321 fadeOutPath.closeSubpath();
322 painter->fillPath(fadeOutPath, fades);
324 QLineF l(br.x() + br.width() - m_endFade * scale, br.y(), br.x() + br.width(), br.y() + br.height());
325 painter->drawLine(l);
329 QPen pen = painter->pen();
330 pen.setColor(Qt::white);
331 //pen.setStyle(Qt::DashDotDotLine); //Qt::DotLine);
333 // Draw effects names
334 QString effects = effectNames().join(" / ");
335 if (!effects.isEmpty()) {
336 painter->setPen(pen);
337 QFont font = painter->font();
338 QFont smallFont = font;
339 smallFont.setPointSize(8);
340 painter->setFont(smallFont);
341 QRectF txtBounding = painter->boundingRect(br, Qt::AlignLeft | Qt::AlignTop, " " + effects + " ");
342 painter->fillRect(txtBounding, QBrush(QColor(0, 0, 0, 150)));
343 painter->drawText(txtBounding, Qt::AlignCenter, effects);
344 pen.setColor(Qt::black);
345 painter->setPen(pen);
346 painter->setFont(font);
349 // For testing puspose only: draw transitions count
351 painter->setPen(pen);
352 QFont font = painter->font();
353 QFont smallFont = font;
354 smallFont.setPointSize(8);
355 painter->setFont(smallFont);
356 QString txt = " Transitions: " + QString::number(m_transitionsList.count()) + " ";
357 QRectF txtBoundin = painter->boundingRect(br, Qt::AlignRight | Qt::AlignTop, txt);
358 painter->fillRect(txtBoundin, QBrush(QColor(0, 0, 0, 150)));
359 painter->drawText(txtBoundin, Qt::AlignCenter, txt);
360 pen.setColor(Qt::black);
361 painter->setPen(pen);
362 painter->setFont(font);
366 QRectF txtBounding = painter->boundingRect(br, Qt::AlignHCenter | Qt::AlignTop, " " + m_clipName + " ");
367 painter->fillRect(txtBounding, QBrush(QColor(255, 255, 255, 150)));
368 painter->drawText(txtBounding, Qt::AlignCenter, m_clipName);
370 // draw frame around clip
371 pen.setColor(Qt::red);
373 if (isSelected()) painter->setPen(pen);
374 painter->setClipRect(option->exposedRect);
375 painter->drawPath(roundRectPathUpper.united(roundRectPathLower).intersected(clippath));
378 //painter->fillRect(startpixel,0,startpixel+endpixel,(int)br.height(), QBrush(QColor(255,255,255,150)));
379 //painter->fillRect(QRect(br.x(), br.y(), roundingX, roundingY), QBrush(QColor(Qt::green)));
381 /*QRectF recta(rect().x(), rect().y(), scale,rect().height());
382 painter->drawRect(recta);
383 painter->drawLine(rect().x() + 1, rect().y(), rect().x() + 1, rect().y() + rect().height());
384 painter->drawLine(rect().x() + rect().width(), rect().y(), rect().x() + rect().width(), rect().y() + rect().height());
385 painter->setPen(QPen(Qt::black, 1.0));
386 painter->drawLine(rect().x(), rect().y(), rect().x() + rect().width(), rect().y());
387 painter->drawLine(rect().x(), rect().y() + rect().height(), rect().x() + rect().width(), rect().y() + rect().height());*/
389 //QGraphicsRectItem::paint(painter, option, widget);
390 //QPen pen(Qt::green, 1.0 / size.x() + 0.5);
391 //painter->setPen(pen);
392 //painter->drawLine(rect().x(), rect().y(), rect().x() + rect().width(), rect().y());
393 //kDebug()<<"ITEM REPAINT RECT: "<<boundingRect().width();
394 //painter->drawText(rect(), Qt::AlignCenter, m_name);
395 // painter->drawRect(boundingRect());
396 //painter->drawRoundRect(-10, -10, 20, 20);
398 painter->setPen(QPen(Qt::black));
399 painter->setBrush(QBrush(Qt::yellow));
400 painter->drawEllipse((int)(br.x() + 10), (int)(br.y() + br.height() / 2 - 5) , 10, 10);
401 painter->drawEllipse((int)(br.x() + br.width() - 20), (int)(br.y() + br.height() / 2 - 5), 10, 10);
406 OPERATIONTYPE ClipItem::operationMode(QPointF pos, double scale) {
407 if (abs((int)(pos.x() - (rect().x() + scale * m_startFade))) < 6 && abs((int)(pos.y() - rect().y())) < 6) return FADEIN;
408 else if (abs((int)(pos.x() - rect().x())) < 6) return RESIZESTART;
409 else if (abs((int)(pos.x() - (rect().x() + rect().width() - scale * m_endFade))) < 6 && abs((int)(pos.y() - rect().y())) < 6) return FADEOUT;
410 else if (abs((int)(pos.x() - (rect().x() + rect().width()))) < 6) return RESIZEEND;
411 else if (abs((int)(pos.x() - (rect().x() + 10))) < 6 && abs((int)(pos.y() - (rect().y() + rect().height() / 2 - 5))) < 6) return TRANSITIONSTART;
412 else if (abs((int)(pos.x() - (rect().x() + rect().width() - 20))) < 6 && abs((int)(pos.y() - (rect().y() + rect().height() / 2 - 5))) < 6) return TRANSITIONEND;
416 void ClipItem::slotPrepareAudioThumb(double pixelForOneFrame, QPainterPath path, int startpixel, int endpixel) {
419 QRectF re = path.boundingRect();
421 //if ( (!audioThumbWasDrawn || framePixelWidth!=pixelForOneFrame ) && !baseClip()->audioFrameChache.isEmpty()){
423 for (int startCache = startpixel - startpixel % 100;startCache + 100 < endpixel ;startCache += 100) {
424 //kDebug() << "creating " << startCache;
425 //if (framePixelWidth!=pixelForOneFrame ||
426 if (framePixelWidth == pixelForOneFrame && audioThumbCachePic.contains(startCache))
428 if (audioThumbCachePic[startCache].isNull() || framePixelWidth != pixelForOneFrame) {
429 audioThumbCachePic[startCache] = QPixmap(100, (int)(re.height()));
430 audioThumbCachePic[startCache].fill(QColor(200, 200, 200, 0));
432 bool fullAreaDraw = pixelForOneFrame < 10;
433 QMap<int, QPainterPath > positiveChannelPaths;
434 QMap<int, QPainterPath > negativeChannelPaths;
435 QPainter pixpainter(&audioThumbCachePic[startCache]);
437 audiopen.setWidth(0);
438 pixpainter.setPen(audiopen);
439 //pixpainter.setRenderHint(QPainter::Antialiasing,true);
440 //pixpainter.drawLine(0,0,100,re.height());
441 int channelHeight = audioThumbCachePic[startCache].height() / channels;
443 for (int i = 0;i < channels;i++) {
445 positiveChannelPaths[i].moveTo(0, channelHeight*i + channelHeight / 2);
446 negativeChannelPaths[i].moveTo(0, channelHeight*i + channelHeight / 2);
449 for (int samples = 0;samples <= 100;samples++) {
450 double frame = (double)(samples + startCache - 0) / pixelForOneFrame;
451 int sample = (int)((frame - (int)(frame)) * 20); // AUDIO_FRAME_SIZE
452 if (frame < 0 || sample < 0 || sample > 19)
454 QMap<int, QByteArray> frame_channel_data = baseClip()->audioFrameChache[(int)frame];
456 for (int channel = 0;channel < channels && frame_channel_data[channel].size() > 0;channel++) {
458 int y = channelHeight * channel + channelHeight / 2;
459 int delta = (int)(frame_channel_data[channel][sample] - 127 / 2) * channelHeight / 64;
461 positiveChannelPaths[channel].lineTo(samples, 0.1 + y + qAbs(delta));
462 negativeChannelPaths[channel].lineTo(samples, 0.1 + y - qAbs(delta));
464 positiveChannelPaths[channel].lineTo(samples, 0.1 + y + delta);
465 negativeChannelPaths[channel].lineTo(samples, 0.1 + y - delta);
468 for (int channel = 0;channel < channels ;channel++)
469 if (fullAreaDraw && samples == 100) {
470 positiveChannelPaths[channel].lineTo(samples, channelHeight*channel + channelHeight / 2);
471 negativeChannelPaths[channel].lineTo(samples, channelHeight*channel + channelHeight / 2);
472 positiveChannelPaths[channel].lineTo(0, channelHeight*channel + channelHeight / 2);
473 negativeChannelPaths[channel].lineTo(0, channelHeight*channel + channelHeight / 2);
477 if (m_clipType != AV) pixpainter.setBrush(QBrush(QColor(200, 200, 100)));
479 pixpainter.setPen(QPen(QColor(0, 0, 0)));
480 pixpainter.setBrush(QBrush(QColor(60, 60, 60)));
482 for (int i = 0;i < channels;i++) {
484 //pixpainter.fillPath(positiveChannelPaths[i].united(negativeChannelPaths[i]),QBrush(Qt::SolidPattern));//or singleif looks better
485 pixpainter.drawPath(positiveChannelPaths[i].united(negativeChannelPaths[i]));//or singleif looks better
487 pixpainter.drawPath(positiveChannelPaths[i]);
490 //audioThumbWasDrawn=true;
491 framePixelWidth = pixelForOneFrame;
496 int ClipItem::fadeIn() const {
500 int ClipItem::fadeOut() const {
504 void ClipItem::setFadeIn(int pos, double scale) {
505 int oldIn = m_startFade;
506 if (pos < 0) pos = 0;
507 if (pos > m_cropDuration.frames(m_fps)) pos = (int)(m_cropDuration.frames(m_fps) / 2);
509 if (oldIn > pos) update(rect().x(), rect().y(), oldIn * scale, rect().height());
510 else update(rect().x(), rect().y(), pos * scale, rect().height());
513 void ClipItem::setFadeOut(int pos, double scale) {
514 int oldOut = m_endFade;
515 if (pos < 0) pos = 0;
516 if (pos > m_cropDuration.frames(m_fps)) pos = (int)(m_cropDuration.frames(m_fps) / 2);
518 if (oldOut > pos) update(rect().x() + rect().width() - pos * scale, rect().y(), pos * scale, rect().height());
519 else update(rect().x() + rect().width() - oldOut * scale, rect().y(), oldOut * scale, rect().height());
525 void ClipItem::mousePressEvent(QGraphicsSceneMouseEvent * event) {
526 /*m_resizeMode = operationMode(event->pos());
527 if (m_resizeMode == MOVE) {
528 m_maxTrack = scene()->sceneRect().height();
529 m_grabPoint = (int) (event->pos().x() - rect().x());
531 QGraphicsRectItem::mousePressEvent(event);
535 void ClipItem::mouseReleaseEvent(QGraphicsSceneMouseEvent * event) {
537 QGraphicsRectItem::mouseReleaseEvent(event);
541 void ClipItem::hoverEnterEvent(QGraphicsSceneHoverEvent *) {
547 void ClipItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *) {
552 void ClipItem::moveTo(int x, double scale, double offset, int newTrack) {
553 double origX = rect().x();
554 double origY = rect().y();
557 setRect(x * scale, origY + offset, rect().width(), rect().height());
558 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
559 if (collisionList.size() == 0) m_track = newTrack;
560 for (int i = 0; i < collisionList.size(); ++i) {
561 QGraphicsItem *item = collisionList.at(i);
562 if (item->type() == 70000) {
564 QRectF other = ((QGraphicsRectItem *)item)->rect();
565 if (x < m_startPos.frames(m_fps)) {
566 kDebug() << "COLLISION, MOVING TO------";
567 m_startPos = ((ClipItem *)item)->endPos() + GenTime(1, m_fps);
568 origX = m_startPos.frames(m_fps) * scale;
570 kDebug() << "COLLISION, MOVING TO+++";
571 m_startPos = ((ClipItem *)item)->startPos() - m_cropDuration;
572 origX = m_startPos.frames(m_fps) * scale;
575 setRect(origX, origY, rect().width(), rect().height());
584 m_startPos = GenTime(x, m_fps);
586 /* QList <QGraphicsItem *> childrenList = QGraphicsItem::children();
587 for (int i = 0; i < childrenList.size(); ++i) {
588 childrenList.at(i)->moveBy(rect().x() - origX , offset);
592 void ClipItem::resizeStart(int posx, double scale) {
593 GenTime durationDiff = GenTime(posx, m_fps) - m_startPos;
594 if (durationDiff == GenTime()) return;
595 //kDebug() << "-- RESCALE: CROP=" << m_cropStart << ", DIFF = " << durationDiff;
596 if (m_cropStart + durationDiff < GenTime()) {
597 durationDiff = GenTime() - m_cropStart;
598 } else if (durationDiff >= m_cropDuration) {
599 durationDiff = m_cropDuration - GenTime(3, m_fps);
601 m_startPos += durationDiff;
602 m_cropStart += durationDiff;
603 m_cropDuration = m_cropDuration - durationDiff;
604 setRect(m_startPos.frames(m_fps) * scale, rect().y(), m_cropDuration.frames(m_fps) * scale, rect().height());
605 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
606 for (int i = 0; i < collisionList.size(); ++i) {
607 QGraphicsItem *item = collisionList.at(i);
608 if (item->type() == 70000) {
609 GenTime diff = ((ClipItem *)item)->endPos() + GenTime(1, m_fps) - m_startPos;
610 setRect((m_startPos + diff).frames(m_fps) * scale, rect().y(), (m_cropDuration - diff).frames(m_fps) * scale, rect().height());
613 m_cropDuration = m_cropDuration - diff;
617 if (m_hasThumbs) startThumbTimer->start(100);
620 void ClipItem::resizeEnd(int posx, double scale) {
621 GenTime durationDiff = GenTime(posx, m_fps) - endPos();
622 if (durationDiff == GenTime()) return;
623 //kDebug() << "-- RESCALE: CROP=" << m_cropStart << ", DIFF = " << durationDiff;
624 if (m_cropDuration + durationDiff <= GenTime()) {
625 durationDiff = GenTime() - (m_cropDuration - GenTime(3, m_fps));
626 } else if (m_cropDuration + durationDiff >= m_maxDuration) {
627 durationDiff = m_maxDuration - m_cropDuration;
629 m_cropDuration += durationDiff;
630 setRect(m_startPos.frames(m_fps) * scale, rect().y(), m_cropDuration.frames(m_fps) * scale, rect().height());
631 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
632 for (int i = 0; i < collisionList.size(); ++i) {
633 QGraphicsItem *item = collisionList.at(i);
634 if (item->type() == 70000) {
635 GenTime diff = ((ClipItem *)item)->startPos() - GenTime(1, m_fps) - startPos();
636 m_cropDuration = diff;
637 setRect(m_startPos.frames(m_fps) * scale, rect().y(), m_cropDuration.frames(m_fps) * scale, rect().height());
641 if (m_hasThumbs) endThumbTimer->start(100);
645 void ClipItem::mouseMoveEvent(QGraphicsSceneMouseEvent * event) {
648 int ClipItem::track() const {
652 void ClipItem::setTrack(int track) {
656 int ClipItem::effectsCounter() {
657 return m_effectsCounter++;
660 int ClipItem::effectsCount() {
661 return m_effectList.size();
664 QStringList ClipItem::effectNames() {
665 return m_effectList.effectNames();
668 QDomElement ClipItem::effectAt(int ix) {
669 return m_effectList.at(ix);
672 void ClipItem::setEffectAt(int ix, QDomElement effect) {
673 kDebug() << "CHange EFFECT AT: " << ix << ", CURR: " << m_effectList.at(ix).attribute("tag") << ", NEW: " << effect.attribute("tag");
674 m_effectList.insert(ix, effect);
675 m_effectList.removeAt(ix + 1);
676 update(boundingRect());
679 QMap <QString, QString> ClipItem::addEffect(QDomElement effect) {
680 QMap <QString, QString> effectParams;
681 m_effectList.append(effect);
682 effectParams["tag"] = effect.attribute("tag");
683 effectParams["kdenlive_ix"] = effect.attribute("kdenlive_ix");
684 QString state = effect.attribute("disabled");
685 if (!state.isEmpty()) effectParams["disabled"] = state;
686 QDomNodeList params = effect.elementsByTagName("parameter");
687 for (int i = 0; i < params.count(); i++) {
688 QDomElement e = params.item(i).toElement();
690 effectParams[e.attribute("name")] = e.attribute("value");
692 if (!e.attribute("factor").isEmpty()) {
693 effectParams[e.attribute("name")] = QString::number(effectParams[e.attribute("name")].toDouble() / e.attribute("factor").toDouble());
697 update(boundingRect());
701 QMap <QString, QString> ClipItem::getEffectArgs(QDomElement effect) {
702 QMap <QString, QString> effectParams;
703 effectParams["tag"] = effect.attribute("tag");
704 effectParams["kdenlive_ix"] = effect.attribute("kdenlive_ix");
705 QString state = effect.attribute("disabled");
706 if (!state.isEmpty()) effectParams["disabled"] = state;
707 QDomNodeList params = effect.elementsByTagName("parameter");
708 for (int i = 0; i < params.count(); i++) {
709 QDomElement e = params.item(i).toElement();
710 if (e.attribute("name").contains(";")) {
711 QString format = e.attribute("format");
712 QStringList separators = format.split("%d", QString::SkipEmptyParts);
713 QStringList values = e.attribute("value").split(QRegExp("[,:;x]"));
715 QTextStream txtNeu(&neu);
716 if (values.size() > 0)
717 txtNeu << (int)values[0].toDouble();
718 for (int i = 0;i < separators.size() && i + 1 < values.size();i++) {
719 txtNeu << separators[i];
720 txtNeu << (int)(values[i+1].toDouble());
722 effectParams["start"] = neu;
725 effectParams[e.attribute("name")] = e.attribute("value");
727 if (!e.attribute("factor").isEmpty()) {
728 effectParams[e.attribute("name")] = QString::number(effectParams[e.attribute("name")].toDouble() / e.attribute("factor").toDouble());
734 void ClipItem::deleteEffect(QString index) {
735 for (int i = 0; i < m_effectList.size(); ++i) {
736 if (m_effectList.at(i).attribute("kdenlive_ix") == index) {
737 m_effectList.removeAt(i);
742 update(boundingRect());
745 void ClipItem::addTransition(Transition tr) {
746 m_transitionsList.append(&tr);
751 void ClipItem::dropEvent(QGraphicsSceneDragDropEvent * event) {
752 QString effects = QString(event->mimeData()->data("kdenlive/effectslist"));
754 doc.setContent(effects, true);
755 QDomElement e = doc.documentElement();
756 CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
757 if (view) view->slotAddEffect(e, m_startPos, m_track);
761 void ClipItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event) {
762 event->setAccepted(event->mimeData()->hasFormat("kdenlive/effectslist"));
765 void ClipItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event) {
771 void CustomTrackView::mousePressEvent ( QMouseEvent * event )
773 int pos = event->x();
774 if (event->modifiers() == Qt::ControlModifier)
775 setDragMode(QGraphicsView::ScrollHandDrag);
776 else if (event->modifiers() == Qt::ShiftModifier)
777 setDragMode(QGraphicsView::RubberBandDrag);
779 QGraphicsItem * item = itemAt(event->pos());
782 else emit cursorMoved((int) mapToScene(event->x(), 0).x());
785 QGraphicsView::mousePressEvent(event);
788 void CustomTrackView::mouseReleaseEvent ( QMouseEvent * event )
790 QGraphicsView::mouseReleaseEvent(event);
791 setDragMode(QGraphicsView::NoDrag);
795 #include "clipitem.moc"