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 m_maxDuration = GenTime(10000, m_fps);
85 QString colour = m_xml.attribute("colour");
86 colour = colour.replace(0, 2, "#");
87 setBrush(QColor(colour.left(7)));
88 } else if (m_clipType == IMAGE) {
89 m_maxDuration = GenTime(10000, m_fps);
90 m_startPix = KThumb::getImage(KUrl(m_xml.attribute("resource")), (int)(50 * KdenliveSettings::project_display_ratio()), 50);
91 } else if (m_clipType == AUDIO) {
92 connect(clip, SIGNAL(gotAudioData()), this, SLOT(slotGotAudioData()));
97 ClipItem::~ClipItem() {
98 if (startThumbTimer) delete startThumbTimer;
99 if (endThumbTimer) delete endThumbTimer;
102 void ClipItem::slotFetchThumbs() {
103 m_thumbsRequested += 2;
104 emit getThumb((int)m_cropStart.frames(m_fps), (int)(m_cropStart + m_cropDuration).frames(m_fps));
107 void ClipItem::slotGetStartThumb() {
109 emit getThumb((int)m_cropStart.frames(m_fps), -1);
112 void ClipItem::slotGetEndThumb() {
114 emit getThumb(-1, (int)(m_cropStart + m_cropDuration).frames(m_fps));
117 void ClipItem::slotThumbReady(int frame, QPixmap pix) {
118 if (m_thumbsRequested == 0) return;
119 if (frame == m_cropStart.frames(m_fps)) m_startPix = pix;
125 void ClipItem::slotGotAudioData() {
126 audioThumbReady = true;
130 int ClipItem::type() const {
134 DocClipBase *ClipItem::baseClip() {
138 QDomElement ClipItem::xml() const {
142 int ClipItem::clipType() {
146 QString ClipItem::clipName() {
150 int ClipItem::clipProducer() {
154 GenTime ClipItem::maxDuration() const {
155 return m_maxDuration;
158 GenTime ClipItem::duration() const {
159 return m_cropDuration;
162 GenTime ClipItem::startPos() const {
166 GenTime ClipItem::cropStart() const {
170 GenTime ClipItem::endPos() const {
171 return m_startPos + m_cropDuration;
174 double ClipItem::fps() const {
178 void ClipItem::flashClip() {
179 if (m_timeLine == 0) {
180 m_timeLine = new QTimeLine(750, this);
181 connect(m_timeLine, SIGNAL(valueChanged(qreal)), this, SLOT(animate(qreal)));
186 void ClipItem::animate(qreal value) {
192 void ClipItem::paint(QPainter *painter,
193 const QStyleOptionGraphicsItem *option,
195 painter->setOpacity(m_opacity);
196 QBrush paintColor = brush();
197 if (isSelected()) paintColor = QBrush(QColor(79, 93, 121));
199 QRect rectInView;//this is the rect that is visible by the user
200 if (scene()->views().size() > 0) {
201 rectInView = scene()->views()[0]->viewport()->rect();
202 rectInView.moveTo(scene()->views()[0]->horizontalScrollBar()->value(), scene()->views()[0]->verticalScrollBar()->value());
203 rectInView.adjust(-10, -10, 10, 10);//make view rect 10 pixel greater on each site, or repaint after scroll event
204 //kDebug() << scene()->views()[0]->viewport()->rect() << " " << scene()->views()[0]->horizontalScrollBar()->value();
206 if (rectInView.isNull())
208 QPainterPath clippath;
209 clippath.addRect(rectInView);
211 int startpixel = (int)(rectInView.x() - rect().x()); //start and endpixel that is viewable from rect()
215 int endpixel = rectInView.width() + rectInView.x();
219 //painter->setRenderHints(QPainter::Antialiasing);
221 QPainterPath roundRectPathUpper, roundRectPathLower;
222 double roundingY = 20;
223 double roundingX = 20;
225 painter->setClipRect(option->exposedRect);
226 if (roundingX > br.width() / 2) roundingX = br.width() / 2;
228 int br_endx = (int)(br.x() + br .width() - offset);
229 int br_startx = (int)(br.x() + offset);
230 int br_starty = (int)(br.y());
231 int br_halfy = (int)(br.y() + br.height() / 2 - offset);
232 int br_endy = (int)(br.y() + br.height());
233 int left_upper = 0, left_lower = 0, right_upper = 0, right_lower = 0;
235 if (m_hover && false) {
236 if (!true) /*TRANSITIONSTART to upper clip*/
238 if (!false) /*TRANSITIONSTART to lower clip*/
240 if (!true) /*TRANSITIONEND to upper clip*/
242 if (!false) /*TRANSITIONEND to lower clip*/
245 QList<QPainterPath> transitionPath;
246 foreach(Transition* t, m_transitionsList) {
248 //t.addRect(br_startx,br.y()+br.height()/2,br.x() + /*t->transitionDuration().frames(m_fps) *pixelForOneFrame*/5 ,br.y()+br.height()*2);
250 t.moveTo(br_startx + twidth , br_endy);
251 t.lineTo(br_startx + twidth , br_halfy + roundingY);
253 t.arcTo(br_startx + twidth - roundingX , br_halfy , roundingX, roundingY, 0.0, 90.0);
254 t.lineTo(br_startx + roundingX , br_halfy);
255 t.arcTo(br_startx , br_halfy, roundingX , roundingY, 90.0, 90.0);
256 t.lineTo(br_startx , br_endy);
258 transitionPath.append(t);
260 // build path around clip
261 roundRectPathUpper.moveTo(br_endx - right_upper , br_halfy);
262 roundRectPathUpper.arcTo(br_endx - roundingX - right_upper , br_starty , roundingX, roundingY, 0.0, 90.0);
263 roundRectPathUpper.lineTo(br_startx + roundingX + left_upper, br_starty);
264 roundRectPathUpper.arcTo(br_startx + left_upper, br_starty , roundingX, roundingY, 90.0, 90.0);
265 roundRectPathUpper.lineTo(br_startx + left_upper, br_halfy);
267 roundRectPathLower.moveTo(br_startx + left_lower, br_halfy);
268 roundRectPathLower.arcTo(br_startx + left_lower, br_endy - roundingY , roundingX, roundingY, 180.0, 90.0);
269 roundRectPathLower.lineTo(br_endx - roundingX - right_lower , br_endy);
270 roundRectPathLower.arcTo(br_endx - roundingX - right_lower , br_endy - roundingY, roundingX, roundingY, 270.0, 90.0);
271 roundRectPathLower.lineTo(br_endx - right_lower , br_halfy);
273 QPainterPath resultClipPath = roundRectPathUpper.united(roundRectPathLower);
274 foreach(QPainterPath p, transitionPath) {
275 resultClipPath = resultClipPath.united(p);
277 painter->setClipPath(resultClipPath.intersected(clippath), Qt::IntersectClip);
278 //painter->fillPath(roundRectPath, brush()); //, QBrush(QColor(Qt::red)));
279 painter->fillRect(br.intersected(rectInView), paintColor);
280 //painter->fillRect(QRectF(br.x() + br.width() - m_endPix.width(), br.y(), m_endPix.width(), br.height()), QBrush(QColor(Qt::black)));
283 if (!m_startPix.isNull() && KdenliveSettings::videothumbnails()) {
284 if (m_clipType == IMAGE) {
285 painter->drawPixmap(QPointF(br.x() + br.width() - m_startPix.width(), br.y()), m_startPix);
286 QLineF l(br.x() + br.width() - m_startPix.width(), br.y(), br.x() + br.width() - m_startPix.width(), br.y() + br.height());
287 painter->drawLine(l);
289 painter->drawPixmap(QPointF(br.x() + br.width() - m_endPix.width(), br.y()), m_endPix);
290 QLineF l(br.x() + br.width() - m_endPix.width(), br.y(), br.x() + br.width() - m_endPix.width(), br.y() + br.height());
291 painter->drawLine(l);
294 painter->drawPixmap(QPointF(br.x(), br.y()), m_startPix);
295 QLineF l2(br.x() + m_startPix.width(), br.y(), br.x() + m_startPix.width(), br.y() + br.height());
296 painter->drawLine(l2);
299 double scale = br.width() / m_cropDuration.frames(m_fps);
300 // draw audio thumbnails
301 if ((m_clipType == AV || m_clipType == AUDIO) && audioThumbReady && KdenliveSettings::audiothumbnails()) {
303 QPainterPath path = m_clipType == AV ? roundRectPathLower : roundRectPathUpper.united(roundRectPathLower);
304 if (m_clipType == AV) painter->fillPath(path, QBrush(QColor(200, 200, 200, 140)));
307 if (scale != framePixelWidth)
308 audioThumbCachePic.clear();
309 emit prepareAudioThumb(scale, path, startpixel, endpixel + 200);//200 more for less missing parts before repaint after scrolling
310 int cropLeft = (m_cropStart).frames(m_fps) * scale;
311 for (int startCache = startpixel - startpixel % 100; startCache < endpixel + 300;startCache += 100) {
312 if (audioThumbCachePic.contains(startCache) && !audioThumbCachePic[startCache].isNull())
313 painter->drawPixmap((int)(roundRectPathUpper.united(roundRectPathLower).boundingRect().x() + startCache - cropLeft), (int)(path.boundingRect().y()), audioThumbCachePic[startCache]);
318 // draw start / end fades
321 fades = QBrush(QColor(200, 50, 50, 150));
322 } else fades = QBrush(QColor(200, 200, 200, 200));
324 if (m_startFade != 0) {
325 QPainterPath fadeInPath;
326 fadeInPath.moveTo(br.x() - offset, br.y());
327 fadeInPath.lineTo(br.x() - offset, br.y() + br.height());
328 fadeInPath.lineTo(br.x() + m_startFade * scale, br.y());
329 fadeInPath.closeSubpath();
330 painter->fillPath(fadeInPath, fades);
332 QLineF l(br.x() + m_startFade * scale, br.y(), br.x(), br.y() + br.height());
333 painter->drawLine(l);
336 if (m_endFade != 0) {
337 QPainterPath fadeOutPath;
338 fadeOutPath.moveTo(br.x() + br.width(), br.y());
339 fadeOutPath.lineTo(br.x() + br.width(), br.y() + br.height());
340 fadeOutPath.lineTo(br.x() + br.width() - m_endFade * scale, br.y());
341 fadeOutPath.closeSubpath();
342 painter->fillPath(fadeOutPath, fades);
344 QLineF l(br.x() + br.width() - m_endFade * scale, br.y(), br.x() + br.width(), br.y() + br.height());
345 painter->drawLine(l);
349 QPen pen = painter->pen();
350 pen.setColor(Qt::white);
351 //pen.setStyle(Qt::DashDotDotLine); //Qt::DotLine);
353 // Draw effects names
354 QString effects = effectNames().join(" / ");
355 if (!effects.isEmpty()) {
356 painter->setPen(pen);
357 QFont font = painter->font();
358 QFont smallFont = font;
359 smallFont.setPointSize(8);
360 painter->setFont(smallFont);
361 QRectF txtBounding = painter->boundingRect(br, Qt::AlignLeft | Qt::AlignTop, " " + effects + " ");
362 painter->fillRect(txtBounding, QBrush(QColor(0, 0, 0, 150)));
363 painter->drawText(txtBounding, Qt::AlignCenter, effects);
364 pen.setColor(Qt::black);
365 painter->setPen(pen);
366 painter->setFont(font);
369 // For testing puspose only: draw transitions count
371 painter->setPen(pen);
372 QFont font = painter->font();
373 QFont smallFont = font;
374 smallFont.setPointSize(8);
375 painter->setFont(smallFont);
376 QString txt = " Transitions: " + QString::number(m_transitionsList.count()) + " ";
377 QRectF txtBoundin = painter->boundingRect(br, Qt::AlignRight | Qt::AlignTop, txt);
378 painter->fillRect(txtBoundin, QBrush(QColor(0, 0, 0, 150)));
379 painter->drawText(txtBoundin, Qt::AlignCenter, txt);
380 pen.setColor(Qt::black);
381 painter->setPen(pen);
382 painter->setFont(font);
386 QRectF txtBounding = painter->boundingRect(br, Qt::AlignHCenter | Qt::AlignTop, " " + m_clipName + " ");
387 painter->fillRect(txtBounding, QBrush(QColor(255, 255, 255, 150)));
388 painter->drawText(txtBounding, Qt::AlignCenter, m_clipName);
390 // draw frame around clip
391 pen.setColor(Qt::red);
393 if (isSelected()) painter->setPen(pen);
394 painter->setClipRect(option->exposedRect);
395 painter->drawPath(resultClipPath.intersected(clippath));
396 foreach(QPainterPath p, transitionPath) {
398 painter->fillPath(p, QBrush(QColor(255, 255, 0, 100)));
399 painter->drawPath(p);
402 //painter->fillRect(startpixel,0,startpixel+endpixel,(int)br.height(), QBrush(QColor(255,255,255,150)));
403 //painter->fillRect(QRect(br.x(), br.y(), roundingX, roundingY), QBrush(QColor(Qt::green)));
405 /*QRectF recta(rect().x(), rect().y(), scale,rect().height());
406 painter->drawRect(recta);
407 painter->drawLine(rect().x() + 1, rect().y(), rect().x() + 1, rect().y() + rect().height());
408 painter->drawLine(rect().x() + rect().width(), rect().y(), rect().x() + rect().width(), rect().y() + rect().height());
409 painter->setPen(QPen(Qt::black, 1.0));
410 painter->drawLine(rect().x(), rect().y(), rect().x() + rect().width(), rect().y());
411 painter->drawLine(rect().x(), rect().y() + rect().height(), rect().x() + rect().width(), rect().y() + rect().height());*/
413 //QGraphicsRectItem::paint(painter, option, widget);
414 //QPen pen(Qt::green, 1.0 / size.x() + 0.5);
415 //painter->setPen(pen);
416 //painter->drawLine(rect().x(), rect().y(), rect().x() + rect().width(), rect().y());
417 //kDebug()<<"ITEM REPAINT RECT: "<<boundingRect().width();
418 //painter->drawText(rect(), Qt::AlignCenter, m_name);
419 // painter->drawRect(boundingRect());
420 //painter->drawRoundRect(-10, -10, 20, 20);
422 painter->setPen(QPen(Qt::black));
423 painter->setBrush(QBrush(Qt::yellow));
424 painter->drawEllipse((int)(br.x() + 10), (int)(br.y() + br.height() / 2 - 5) , 10, 10);
425 painter->drawEllipse((int)(br.x() + br.width() - 20), (int)(br.y() + br.height() / 2 - 5), 10, 10);
430 OPERATIONTYPE ClipItem::operationMode(QPointF pos, double scale) {
431 if (abs((int)(pos.x() - (rect().x() + scale * m_startFade))) < 6 && abs((int)(pos.y() - rect().y())) < 6) return FADEIN;
432 else if (abs((int)(pos.x() - rect().x())) < 6) return RESIZESTART;
433 else if (abs((int)(pos.x() - (rect().x() + rect().width() - scale * m_endFade))) < 6 && abs((int)(pos.y() - rect().y())) < 6) return FADEOUT;
434 else if (abs((int)(pos.x() - (rect().x() + rect().width()))) < 6) return RESIZEEND;
435 else if (abs((int)(pos.x() - (rect().x() + 10))) < 6 && abs((int)(pos.y() - (rect().y() + rect().height() / 2 - 5))) < 6) return TRANSITIONSTART;
436 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;
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 int ClipItem::fadeIn() const {
524 int ClipItem::fadeOut() const {
528 void ClipItem::setFadeIn(int pos, double scale) {
529 int oldIn = m_startFade;
530 if (pos < 0) pos = 0;
531 if (pos > m_cropDuration.frames(m_fps)) pos = (int)(m_cropDuration.frames(m_fps) / 2);
533 if (oldIn > pos) update(rect().x(), rect().y(), oldIn * scale, rect().height());
534 else update(rect().x(), rect().y(), 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 if (oldOut > pos) update(rect().x() + rect().width() - pos * scale, rect().y(), pos * scale, rect().height());
543 else update(rect().x() + rect().width() - oldOut * scale, rect().y(), oldOut * scale, rect().height());
549 void ClipItem::mousePressEvent(QGraphicsSceneMouseEvent * event) {
550 /*m_resizeMode = operationMode(event->pos());
551 if (m_resizeMode == MOVE) {
552 m_maxTrack = scene()->sceneRect().height();
553 m_grabPoint = (int) (event->pos().x() - rect().x());
555 QGraphicsRectItem::mousePressEvent(event);
559 void ClipItem::mouseReleaseEvent(QGraphicsSceneMouseEvent * event) {
561 QGraphicsRectItem::mouseReleaseEvent(event);
565 void ClipItem::hoverEnterEvent(QGraphicsSceneHoverEvent *) {
571 void ClipItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *) {
576 void ClipItem::moveTo(int x, double scale, double offset, int newTrack) {
577 double origX = rect().x();
578 double origY = rect().y();
581 setRect(x * scale, origY + offset, rect().width(), rect().height());
582 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
583 if (collisionList.size() == 0) m_track = newTrack;
584 for (int i = 0; i < collisionList.size(); ++i) {
585 QGraphicsItem *item = collisionList.at(i);
586 if (item->type() == 70000) {
588 QRectF other = ((QGraphicsRectItem *)item)->rect();
589 if (x < m_startPos.frames(m_fps)) {
590 kDebug() << "COLLISION, MOVING TO------";
591 m_startPos = ((ClipItem *)item)->endPos() + GenTime(1, m_fps);
592 origX = m_startPos.frames(m_fps) * scale;
594 kDebug() << "COLLISION, MOVING TO+++";
595 m_startPos = ((ClipItem *)item)->startPos() - m_cropDuration;
596 origX = m_startPos.frames(m_fps) * scale;
599 setRect(origX, origY, rect().width(), rect().height());
608 m_startPos = GenTime(x, m_fps);
610 /* QList <QGraphicsItem *> childrenList = QGraphicsItem::children();
611 for (int i = 0; i < childrenList.size(); ++i) {
612 childrenList.at(i)->moveBy(rect().x() - origX , offset);
616 void ClipItem::resizeStart(int posx, double scale) {
617 GenTime durationDiff = GenTime(posx, m_fps) - m_startPos;
618 if (durationDiff == GenTime()) return;
619 //kDebug() << "-- RESCALE: CROP=" << m_cropStart << ", DIFF = " << durationDiff;
620 if (m_cropStart + durationDiff < GenTime()) {
621 durationDiff = GenTime() - m_cropStart;
622 } else if (durationDiff >= m_cropDuration) {
623 durationDiff = m_cropDuration - GenTime(3, m_fps);
625 m_startPos += durationDiff;
626 m_cropStart += durationDiff;
627 m_cropDuration = m_cropDuration - durationDiff;
628 setRect(m_startPos.frames(m_fps) * scale, rect().y(), m_cropDuration.frames(m_fps) * scale, rect().height());
629 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
630 for (int i = 0; i < collisionList.size(); ++i) {
631 QGraphicsItem *item = collisionList.at(i);
632 if (item->type() == 70000) {
633 GenTime diff = ((ClipItem *)item)->endPos() + GenTime(1, m_fps) - m_startPos;
634 setRect((m_startPos + diff).frames(m_fps) * scale, rect().y(), (m_cropDuration - diff).frames(m_fps) * scale, rect().height());
637 m_cropDuration = m_cropDuration - diff;
641 if (m_hasThumbs) startThumbTimer->start(100);
644 void ClipItem::resizeEnd(int posx, double scale) {
645 GenTime durationDiff = GenTime(posx, m_fps) - endPos();
646 if (durationDiff == GenTime()) return;
647 //kDebug() << "-- RESCALE: CROP=" << m_cropStart << ", DIFF = " << durationDiff;
648 if (m_cropDuration + durationDiff <= GenTime()) {
649 durationDiff = GenTime() - (m_cropDuration - GenTime(3, m_fps));
650 } else if (m_cropStart + m_cropDuration + durationDiff >= m_maxDuration) {
651 durationDiff = m_maxDuration - m_cropDuration - m_cropStart;
653 m_cropDuration += durationDiff;
654 setRect(m_startPos.frames(m_fps) * scale, rect().y(), m_cropDuration.frames(m_fps) * scale, rect().height());
655 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
656 for (int i = 0; i < collisionList.size(); ++i) {
657 QGraphicsItem *item = collisionList.at(i);
658 if (item->type() == 70000) {
659 GenTime diff = ((ClipItem *)item)->startPos() - GenTime(1, m_fps) - startPos();
660 m_cropDuration = diff;
661 setRect(m_startPos.frames(m_fps) * scale, rect().y(), m_cropDuration.frames(m_fps) * scale, rect().height());
665 if (m_hasThumbs) endThumbTimer->start(100);
669 void ClipItem::mouseMoveEvent(QGraphicsSceneMouseEvent * event) {
672 int ClipItem::track() const {
676 void ClipItem::setTrack(int track) {
680 int ClipItem::effectsCounter() {
681 return m_effectsCounter++;
684 int ClipItem::effectsCount() {
685 return m_effectList.size();
688 QStringList ClipItem::effectNames() {
689 return m_effectList.effectNames();
692 QDomElement ClipItem::effectAt(int ix) {
693 return m_effectList.at(ix);
696 void ClipItem::setEffectAt(int ix, QDomElement effect) {
697 kDebug() << "CHange EFFECT AT: " << ix << ", CURR: " << m_effectList.at(ix).attribute("tag") << ", NEW: " << effect.attribute("tag");
698 m_effectList.insert(ix, effect);
699 m_effectList.removeAt(ix + 1);
700 update(boundingRect());
703 QMap <QString, QString> ClipItem::addEffect(QDomElement effect) {
704 QMap <QString, QString> effectParams;
705 m_effectList.append(effect);
706 effectParams["tag"] = effect.attribute("tag");
707 effectParams["kdenlive_ix"] = effect.attribute("kdenlive_ix");
708 QString state = effect.attribute("disabled");
709 if (!state.isEmpty()) effectParams["disabled"] = state;
710 QDomNodeList params = effect.elementsByTagName("parameter");
711 for (int i = 0; i < params.count(); i++) {
712 QDomElement e = params.item(i).toElement();
714 effectParams[e.attribute("name")] = e.attribute("value");
716 if (!e.attribute("factor").isEmpty()) {
717 effectParams[e.attribute("name")] = QString::number(effectParams[e.attribute("name")].toDouble() / e.attribute("factor").toDouble());
721 update(boundingRect());
725 QMap <QString, QString> ClipItem::getEffectArgs(QDomElement effect) {
726 QMap <QString, QString> effectParams;
727 effectParams["tag"] = effect.attribute("tag");
728 effectParams["kdenlive_ix"] = effect.attribute("kdenlive_ix");
729 QString state = effect.attribute("disabled");
730 if (!state.isEmpty()) effectParams["disabled"] = state;
731 QDomNodeList params = effect.elementsByTagName("parameter");
732 for (int i = 0; i < params.count(); i++) {
733 QDomElement e = params.item(i).toElement();
734 if (e.attribute("name").contains(";")) {
735 QString format = e.attribute("format");
736 QStringList separators = format.split("%d", QString::SkipEmptyParts);
737 QStringList values = e.attribute("value").split(QRegExp("[,:;x]"));
739 QTextStream txtNeu(&neu);
740 if (values.size() > 0)
741 txtNeu << (int)values[0].toDouble();
742 for (int i = 0;i < separators.size() && i + 1 < values.size();i++) {
743 txtNeu << separators[i];
744 txtNeu << (int)(values[i+1].toDouble());
746 effectParams["start"] = neu;
749 effectParams[e.attribute("name")] = e.attribute("value");
751 if (!e.attribute("factor").isEmpty()) {
752 effectParams[e.attribute("name")] = QString::number(effectParams[e.attribute("name")].toDouble() / e.attribute("factor").toDouble());
758 void ClipItem::deleteEffect(QString index) {
759 for (int i = 0; i < m_effectList.size(); ++i) {
760 if (m_effectList.at(i).attribute("kdenlive_ix") == index) {
761 m_effectList.removeAt(i);
766 update(boundingRect());
769 void ClipItem::addTransition(Transition tr) {
770 m_transitionsList.append(&tr);
775 void ClipItem::dropEvent(QGraphicsSceneDragDropEvent * event) {
776 QString effects = QString(event->mimeData()->data("kdenlive/effectslist"));
778 doc.setContent(effects, true);
779 QDomElement e = doc.documentElement();
780 CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
781 if (view) view->slotAddEffect(e, m_startPos, m_track);
785 void ClipItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event) {
786 event->setAccepted(event->mimeData()->hasFormat("kdenlive/effectslist"));
789 void ClipItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event) {
795 void CustomTrackView::mousePressEvent ( QMouseEvent * event )
797 int pos = event->x();
798 if (event->modifiers() == Qt::ControlModifier)
799 setDragMode(QGraphicsView::ScrollHandDrag);
800 else if (event->modifiers() == Qt::ShiftModifier)
801 setDragMode(QGraphicsView::RubberBandDrag);
803 QGraphicsItem * item = itemAt(event->pos());
806 else emit cursorMoved((int) mapToScene(event->x(), 0).x());
809 QGraphicsView::mousePressEvent(event);
812 void CustomTrackView::mouseReleaseEvent ( QMouseEvent * event )
814 QGraphicsView::mouseReleaseEvent(event);
815 setDragMode(QGraphicsView::NoDrag);
819 #include "clipitem.moc"