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();
198 if (isSelected()) paintColor = QBrush(QColor(79, 93, 121));
200 double scale = br.width() / m_cropDuration.frames(m_fps);
201 QRect rectInView;//this is the rect that is visible by the user
202 if (scene()->views().size() > 0) {
203 rectInView = scene()->views()[0]->viewport()->rect();
204 rectInView.moveTo(scene()->views()[0]->horizontalScrollBar()->value(), scene()->views()[0]->verticalScrollBar()->value());
205 rectInView.adjust(-10, -10, 10, 10);//make view rect 10 pixel greater on each site, or repaint after scroll event
206 //kDebug() << scene()->views()[0]->viewport()->rect() << " " << scene()->views()[0]->horizontalScrollBar()->value();
208 if (rectInView.isNull())
210 QPainterPath clippath;
211 clippath.addRect(rectInView);
213 int startpixel = (int)(rectInView.x() - rect().x()); //start and endpixel that is viewable from rect()
217 int endpixel = rectInView.width() + rectInView.x();
221 //painter->setRenderHints(QPainter::Antialiasing);
223 QPainterPath roundRectPathUpper, roundRectPathLower;
224 double roundingY = 20;
225 double roundingX = 20;
227 painter->setClipRect(option->exposedRect);
228 if (roundingX > br.width() / 2) roundingX = br.width() / 2;
230 int br_endx = (int)(br.x() + br .width() - offset);
231 int br_startx = (int)(br.x() + offset);
232 int br_starty = (int)(br.y());
233 int br_halfy = (int)(br.y() + br.height() / 2 - offset);
234 int br_endy = (int)(br.y() + br.height());
235 int left_upper = 0, left_lower = 0, right_upper = 0, right_lower = 0;
237 if (m_hover && false) {
238 if (!true) /*TRANSITIONSTART to upper clip*/
240 if (!false) /*TRANSITIONSTART to lower clip*/
242 if (!true) /*TRANSITIONEND to upper clip*/
244 if (!false) /*TRANSITIONEND to lower clip*/
249 QList<QPainterPath> transitionPath;
250 foreach(Transition transition, m_transitionsList) {
252 //t.addRect(br_startx,br.y()+br.height()/2,br.x() + /*t->transitionDuration().frames(m_fps) *pixelForOneFrame*/5 ,br.y()+br.height()*2);
253 int twidth = br_startx + transition.transitionDuration().frames(m_fps) * scale;
254 t.moveTo(twidth , br_endy);
255 t.lineTo(twidth , br_halfy + roundingY);
257 t.arcTo(twidth - roundingX , br_halfy , roundingX, roundingY, 0.0, 90.0);
258 t.lineTo(br_startx + roundingX , br_halfy);
259 t.arcTo(br_startx , br_halfy, roundingX , roundingY, 90.0, 90.0);
260 t.lineTo(br_startx , br_endy);
262 transitionPath.append(t);
265 // build path around clip
266 roundRectPathUpper.moveTo(br_endx - right_upper , br_halfy);
267 roundRectPathUpper.arcTo(br_endx - roundingX - right_upper , br_starty , roundingX, roundingY, 0.0, 90.0);
268 roundRectPathUpper.lineTo(br_startx + roundingX + left_upper, br_starty);
269 roundRectPathUpper.arcTo(br_startx + left_upper, br_starty , roundingX, roundingY, 90.0, 90.0);
270 roundRectPathUpper.lineTo(br_startx + left_upper, br_halfy);
272 roundRectPathLower.moveTo(br_startx + left_lower, br_halfy);
273 roundRectPathLower.arcTo(br_startx + left_lower, br_endy - roundingY , roundingX, roundingY, 180.0, 90.0);
274 roundRectPathLower.lineTo(br_endx - roundingX - right_lower , br_endy);
275 roundRectPathLower.arcTo(br_endx - roundingX - right_lower , br_endy - roundingY, roundingX, roundingY, 270.0, 90.0);
276 roundRectPathLower.lineTo(br_endx - right_lower , br_halfy);
278 QPainterPath resultClipPath = roundRectPathUpper.united(roundRectPathLower);
279 foreach(QPainterPath p, transitionPath) {
280 resultClipPath = resultClipPath.united(p);
282 painter->setClipPath(resultClipPath.intersected(clippath), Qt::IntersectClip);
283 //painter->fillPath(roundRectPath, brush()); //, QBrush(QColor(Qt::red)));
284 painter->fillRect(br.intersected(rectInView), paintColor);
285 //painter->fillRect(QRectF(br.x() + br.width() - m_endPix.width(), br.y(), m_endPix.width(), br.height()), QBrush(QColor(Qt::black)));
288 if (!m_startPix.isNull() && KdenliveSettings::videothumbnails()) {
289 if (m_clipType == IMAGE) {
290 painter->drawPixmap(QPointF(br.x() + br.width() - m_startPix.width(), br.y()), m_startPix);
291 QLineF l(br.x() + br.width() - m_startPix.width(), br.y(), br.x() + br.width() - m_startPix.width(), br.y() + br.height());
292 painter->drawLine(l);
294 painter->drawPixmap(QPointF(br.x() + br.width() - m_endPix.width(), br.y()), m_endPix);
295 QLineF l(br.x() + br.width() - m_endPix.width(), br.y(), br.x() + br.width() - m_endPix.width(), br.y() + br.height());
296 painter->drawLine(l);
299 painter->drawPixmap(QPointF(br.x(), br.y()), m_startPix);
300 QLineF l2(br.x() + m_startPix.width(), br.y(), br.x() + m_startPix.width(), br.y() + br.height());
301 painter->drawLine(l2);
304 // draw audio thumbnails
305 if ((m_clipType == AV || m_clipType == AUDIO) && audioThumbReady && KdenliveSettings::audiothumbnails()) {
307 QPainterPath path = m_clipType == AV ? roundRectPathLower : roundRectPathUpper.united(roundRectPathLower);
308 if (m_clipType == AV) painter->fillPath(path, QBrush(QColor(200, 200, 200, 140)));
311 if (scale != framePixelWidth)
312 audioThumbCachePic.clear();
313 emit prepareAudioThumb(scale, path, startpixel, endpixel + 200);//200 more for less missing parts before repaint after scrolling
314 int cropLeft = (m_cropStart).frames(m_fps) * scale;
315 for (int startCache = startpixel - startpixel % 100; startCache < endpixel + 300;startCache += 100) {
316 if (audioThumbCachePic.contains(startCache) && !audioThumbCachePic[startCache].isNull())
317 painter->drawPixmap((int)(roundRectPathUpper.united(roundRectPathLower).boundingRect().x() + startCache - cropLeft), (int)(path.boundingRect().y()), audioThumbCachePic[startCache]);
322 // draw start / end fades
325 fades = QBrush(QColor(200, 50, 50, 150));
326 } else fades = QBrush(QColor(200, 200, 200, 200));
328 if (m_startFade != 0) {
329 QPainterPath fadeInPath;
330 fadeInPath.moveTo(br.x() - offset, br.y());
331 fadeInPath.lineTo(br.x() - offset, br.y() + br.height());
332 fadeInPath.lineTo(br.x() + m_startFade * scale, br.y());
333 fadeInPath.closeSubpath();
334 painter->fillPath(fadeInPath, fades);
336 QLineF l(br.x() + m_startFade * scale, br.y(), br.x(), br.y() + br.height());
337 painter->drawLine(l);
340 if (m_endFade != 0) {
341 QPainterPath fadeOutPath;
342 fadeOutPath.moveTo(br.x() + br.width(), br.y());
343 fadeOutPath.lineTo(br.x() + br.width(), br.y() + br.height());
344 fadeOutPath.lineTo(br.x() + br.width() - m_endFade * scale, br.y());
345 fadeOutPath.closeSubpath();
346 painter->fillPath(fadeOutPath, fades);
348 QLineF l(br.x() + br.width() - m_endFade * scale, br.y(), br.x() + br.width(), br.y() + br.height());
349 painter->drawLine(l);
353 QPen pen = painter->pen();
354 pen.setColor(Qt::white);
355 //pen.setStyle(Qt::DashDotDotLine); //Qt::DotLine);
357 // Draw effects names
358 QString effects = effectNames().join(" / ");
359 if (!effects.isEmpty()) {
360 painter->setPen(pen);
361 QFont font = painter->font();
362 QFont smallFont = font;
363 smallFont.setPointSize(8);
364 painter->setFont(smallFont);
365 QRectF txtBounding = painter->boundingRect(br, Qt::AlignLeft | Qt::AlignTop, " " + effects + " ");
366 painter->fillRect(txtBounding, QBrush(QColor(0, 0, 0, 150)));
367 painter->drawText(txtBounding, Qt::AlignCenter, effects);
368 pen.setColor(Qt::black);
369 painter->setPen(pen);
370 painter->setFont(font);
373 // For testing puspose only: draw transitions count
375 painter->setPen(pen);
376 QFont font = painter->font();
377 QFont smallFont = font;
378 smallFont.setPointSize(8);
379 painter->setFont(smallFont);
380 QString txt = " Transitions: " + QString::number(m_transitionsList.count()) + " ";
381 QRectF txtBoundin = painter->boundingRect(br, Qt::AlignRight | Qt::AlignTop, txt);
382 painter->fillRect(txtBoundin, QBrush(QColor(0, 0, 0, 150)));
383 painter->drawText(txtBoundin, Qt::AlignCenter, txt);
384 pen.setColor(Qt::black);
385 painter->setPen(pen);
386 painter->setFont(font);
390 QRectF txtBounding = painter->boundingRect(br, Qt::AlignHCenter | Qt::AlignTop, " " + m_clipName + " ");
391 painter->fillRect(txtBounding, QBrush(QColor(255, 255, 255, 150)));
392 painter->drawText(txtBounding, Qt::AlignCenter, m_clipName);
394 // draw frame around clip
395 pen.setColor(Qt::red);
397 if (isSelected()) painter->setPen(pen);
398 painter->setClipRect(option->exposedRect);
399 painter->drawPath(resultClipPath.intersected(clippath));
400 foreach(QPainterPath p, transitionPath) {
402 painter->fillPath(p, QBrush(QColor(255, 255, 0, 100)));
403 painter->drawPath(p);
406 //painter->fillRect(startpixel,0,startpixel+endpixel,(int)br.height(), QBrush(QColor(255,255,255,150)));
407 //painter->fillRect(QRect(br.x(), br.y(), roundingX, roundingY), QBrush(QColor(Qt::green)));
409 /*QRectF recta(rect().x(), rect().y(), scale,rect().height());
410 painter->drawRect(recta);
411 painter->drawLine(rect().x() + 1, rect().y(), rect().x() + 1, rect().y() + rect().height());
412 painter->drawLine(rect().x() + rect().width(), rect().y(), rect().x() + rect().width(), rect().y() + rect().height());
413 painter->setPen(QPen(Qt::black, 1.0));
414 painter->drawLine(rect().x(), rect().y(), rect().x() + rect().width(), rect().y());
415 painter->drawLine(rect().x(), rect().y() + rect().height(), rect().x() + rect().width(), rect().y() + rect().height());*/
417 //QGraphicsRectItem::paint(painter, option, widget);
418 //QPen pen(Qt::green, 1.0 / size.x() + 0.5);
419 //painter->setPen(pen);
420 //painter->drawLine(rect().x(), rect().y(), rect().x() + rect().width(), rect().y());
421 //kDebug()<<"ITEM REPAINT RECT: "<<boundingRect().width();
422 //painter->drawText(rect(), Qt::AlignCenter, m_name);
423 // painter->drawRect(boundingRect());
424 //painter->drawRoundRect(-10, -10, 20, 20);
426 painter->setPen(QPen(Qt::black));
427 painter->setBrush(QBrush(Qt::yellow));
428 painter->drawEllipse((int)(br.x() + 10), (int)(br.y() + br.height() / 2 - 5) , 10, 10);
429 painter->drawEllipse((int)(br.x() + br.width() - 20), (int)(br.y() + br.height() / 2 - 5), 10, 10);
434 OPERATIONTYPE ClipItem::operationMode(QPointF pos, double scale) {
435 if (abs((int)(pos.x() - (rect().x() + scale * m_startFade))) < 6 && abs((int)(pos.y() - rect().y())) < 6) return FADEIN;
436 else if (abs((int)(pos.x() - rect().x())) < 6) return RESIZESTART;
437 else if (abs((int)(pos.x() - (rect().x() + rect().width() - scale * m_endFade))) < 6 && abs((int)(pos.y() - rect().y())) < 6) return FADEOUT;
438 else if (abs((int)(pos.x() - (rect().x() + rect().width()))) < 6) return RESIZEEND;
439 else if (abs((int)(pos.x() - (rect().x() + 10))) < 6 && abs((int)(pos.y() - (rect().y() + rect().height() / 2 - 5))) < 6) return TRANSITIONSTART;
440 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;
444 void ClipItem::slotPrepareAudioThumb(double pixelForOneFrame, QPainterPath path, int startpixel, int endpixel) {
447 QRectF re = path.boundingRect();
449 //if ( (!audioThumbWasDrawn || framePixelWidth!=pixelForOneFrame ) && !baseClip()->audioFrameChache.isEmpty()){
451 for (int startCache = startpixel - startpixel % 100;startCache + 100 < endpixel ;startCache += 100) {
452 //kDebug() << "creating " << startCache;
453 //if (framePixelWidth!=pixelForOneFrame ||
454 if (framePixelWidth == pixelForOneFrame && audioThumbCachePic.contains(startCache))
456 if (audioThumbCachePic[startCache].isNull() || framePixelWidth != pixelForOneFrame) {
457 audioThumbCachePic[startCache] = QPixmap(100, (int)(re.height()));
458 audioThumbCachePic[startCache].fill(QColor(200, 200, 200, 0));
460 bool fullAreaDraw = pixelForOneFrame < 10;
461 QMap<int, QPainterPath > positiveChannelPaths;
462 QMap<int, QPainterPath > negativeChannelPaths;
463 QPainter pixpainter(&audioThumbCachePic[startCache]);
465 audiopen.setWidth(0);
466 pixpainter.setPen(audiopen);
467 //pixpainter.setRenderHint(QPainter::Antialiasing,true);
468 //pixpainter.drawLine(0,0,100,re.height());
469 int channelHeight = audioThumbCachePic[startCache].height() / channels;
471 for (int i = 0;i < channels;i++) {
473 positiveChannelPaths[i].moveTo(0, channelHeight*i + channelHeight / 2);
474 negativeChannelPaths[i].moveTo(0, channelHeight*i + channelHeight / 2);
477 for (int samples = 0;samples <= 100;samples++) {
478 double frame = (double)(samples + startCache - 0) / pixelForOneFrame;
479 int sample = (int)((frame - (int)(frame)) * 20); // AUDIO_FRAME_SIZE
480 if (frame < 0 || sample < 0 || sample > 19)
482 QMap<int, QByteArray> frame_channel_data = baseClip()->audioFrameChache[(int)frame];
484 for (int channel = 0;channel < channels && frame_channel_data[channel].size() > 0;channel++) {
486 int y = channelHeight * channel + channelHeight / 2;
487 int delta = (int)(frame_channel_data[channel][sample] - 127 / 2) * channelHeight / 64;
489 positiveChannelPaths[channel].lineTo(samples, 0.1 + y + qAbs(delta));
490 negativeChannelPaths[channel].lineTo(samples, 0.1 + y - qAbs(delta));
492 positiveChannelPaths[channel].lineTo(samples, 0.1 + y + delta);
493 negativeChannelPaths[channel].lineTo(samples, 0.1 + y - delta);
496 for (int channel = 0;channel < channels ;channel++)
497 if (fullAreaDraw && samples == 100) {
498 positiveChannelPaths[channel].lineTo(samples, channelHeight*channel + channelHeight / 2);
499 negativeChannelPaths[channel].lineTo(samples, channelHeight*channel + channelHeight / 2);
500 positiveChannelPaths[channel].lineTo(0, channelHeight*channel + channelHeight / 2);
501 negativeChannelPaths[channel].lineTo(0, channelHeight*channel + channelHeight / 2);
505 if (m_clipType != AV) pixpainter.setBrush(QBrush(QColor(200, 200, 100)));
507 pixpainter.setPen(QPen(QColor(0, 0, 0)));
508 pixpainter.setBrush(QBrush(QColor(60, 60, 60)));
510 for (int i = 0;i < channels;i++) {
512 //pixpainter.fillPath(positiveChannelPaths[i].united(negativeChannelPaths[i]),QBrush(Qt::SolidPattern));//or singleif looks better
513 pixpainter.drawPath(positiveChannelPaths[i].united(negativeChannelPaths[i]));//or singleif looks better
515 pixpainter.drawPath(positiveChannelPaths[i]);
518 //audioThumbWasDrawn=true;
519 framePixelWidth = pixelForOneFrame;
524 int ClipItem::fadeIn() const {
528 int ClipItem::fadeOut() const {
532 void ClipItem::setFadeIn(int pos, double scale) {
533 int oldIn = m_startFade;
534 if (pos < 0) pos = 0;
535 if (pos > m_cropDuration.frames(m_fps)) pos = (int)(m_cropDuration.frames(m_fps) / 2);
537 if (oldIn > pos) update(rect().x(), rect().y(), oldIn * scale, rect().height());
538 else update(rect().x(), rect().y(), pos * scale, rect().height());
541 void ClipItem::setFadeOut(int pos, double scale) {
542 int oldOut = m_endFade;
543 if (pos < 0) pos = 0;
544 if (pos > m_cropDuration.frames(m_fps)) pos = (int)(m_cropDuration.frames(m_fps) / 2);
546 if (oldOut > pos) update(rect().x() + rect().width() - pos * scale, rect().y(), pos * scale, rect().height());
547 else update(rect().x() + rect().width() - oldOut * scale, rect().y(), oldOut * scale, rect().height());
553 void ClipItem::mousePressEvent(QGraphicsSceneMouseEvent * event) {
554 /*m_resizeMode = operationMode(event->pos());
555 if (m_resizeMode == MOVE) {
556 m_maxTrack = scene()->sceneRect().height();
557 m_grabPoint = (int) (event->pos().x() - rect().x());
559 QGraphicsRectItem::mousePressEvent(event);
563 void ClipItem::mouseReleaseEvent(QGraphicsSceneMouseEvent * event) {
565 QGraphicsRectItem::mouseReleaseEvent(event);
569 void ClipItem::hoverEnterEvent(QGraphicsSceneHoverEvent *) {
575 void ClipItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *) {
580 void ClipItem::moveTo(int x, double scale, double offset, int newTrack) {
581 double origX = rect().x();
582 double origY = rect().y();
585 setRect(x * scale, origY + offset, rect().width(), rect().height());
586 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
587 if (collisionList.size() == 0) m_track = newTrack;
588 for (int i = 0; i < collisionList.size(); ++i) {
589 QGraphicsItem *item = collisionList.at(i);
590 if (item->type() == 70000) {
592 QRectF other = ((QGraphicsRectItem *)item)->rect();
593 if (x < m_startPos.frames(m_fps)) {
594 kDebug() << "COLLISION, MOVING TO------";
595 m_startPos = ((ClipItem *)item)->endPos() + GenTime(1, m_fps);
596 origX = m_startPos.frames(m_fps) * scale;
598 kDebug() << "COLLISION, MOVING TO+++";
599 m_startPos = ((ClipItem *)item)->startPos() - m_cropDuration;
600 origX = m_startPos.frames(m_fps) * scale;
603 setRect(origX, origY, rect().width(), rect().height());
612 m_startPos = GenTime(x, m_fps);
614 /* QList <QGraphicsItem *> childrenList = QGraphicsItem::children();
615 for (int i = 0; i < childrenList.size(); ++i) {
616 childrenList.at(i)->moveBy(rect().x() - origX , offset);
620 void ClipItem::resizeStart(int posx, double scale) {
621 GenTime durationDiff = GenTime(posx, m_fps) - m_startPos;
622 if (durationDiff == GenTime()) return;
623 //kDebug() << "-- RESCALE: CROP=" << m_cropStart << ", DIFF = " << durationDiff;
624 if (m_cropStart + durationDiff < GenTime()) {
625 durationDiff = GenTime() - m_cropStart;
626 } else if (durationDiff >= m_cropDuration) {
627 durationDiff = m_cropDuration - GenTime(3, m_fps);
629 m_startPos += durationDiff;
630 m_cropStart += durationDiff;
631 m_cropDuration = m_cropDuration - durationDiff;
632 setRect(m_startPos.frames(m_fps) * scale, rect().y(), m_cropDuration.frames(m_fps) * scale, rect().height());
633 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
634 for (int i = 0; i < collisionList.size(); ++i) {
635 QGraphicsItem *item = collisionList.at(i);
636 if (item->type() == 70000) {
637 GenTime diff = ((ClipItem *)item)->endPos() + GenTime(1, m_fps) - m_startPos;
638 setRect((m_startPos + diff).frames(m_fps) * scale, rect().y(), (m_cropDuration - diff).frames(m_fps) * scale, rect().height());
641 m_cropDuration = m_cropDuration - diff;
645 if (m_hasThumbs) startThumbTimer->start(100);
648 void ClipItem::resizeEnd(int posx, double scale) {
649 GenTime durationDiff = GenTime(posx, m_fps) - endPos();
650 if (durationDiff == GenTime()) return;
651 //kDebug() << "-- RESCALE: CROP=" << m_cropStart << ", DIFF = " << durationDiff;
652 if (m_cropDuration + durationDiff <= GenTime()) {
653 durationDiff = GenTime() - (m_cropDuration - GenTime(3, m_fps));
654 } else if (m_cropStart + m_cropDuration + durationDiff >= m_maxDuration) {
655 durationDiff = m_maxDuration - m_cropDuration - m_cropStart;
657 m_cropDuration += durationDiff;
658 setRect(m_startPos.frames(m_fps) * scale, rect().y(), m_cropDuration.frames(m_fps) * scale, rect().height());
659 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
660 for (int i = 0; i < collisionList.size(); ++i) {
661 QGraphicsItem *item = collisionList.at(i);
662 if (item->type() == 70000) {
663 GenTime diff = ((ClipItem *)item)->startPos() - GenTime(1, m_fps) - startPos();
664 m_cropDuration = diff;
665 setRect(m_startPos.frames(m_fps) * scale, rect().y(), m_cropDuration.frames(m_fps) * scale, rect().height());
669 if (m_hasThumbs) endThumbTimer->start(100);
673 void ClipItem::mouseMoveEvent(QGraphicsSceneMouseEvent * event) {
676 int ClipItem::track() const {
680 void ClipItem::setTrack(int track) {
684 int ClipItem::effectsCounter() {
685 return m_effectsCounter++;
688 int ClipItem::effectsCount() {
689 return m_effectList.size();
692 QStringList ClipItem::effectNames() {
693 return m_effectList.effectNames();
696 QDomElement ClipItem::effectAt(int ix) {
697 return m_effectList.at(ix);
700 void ClipItem::setEffectAt(int ix, QDomElement effect) {
701 kDebug() << "CHange EFFECT AT: " << ix << ", CURR: " << m_effectList.at(ix).attribute("tag") << ", NEW: " << effect.attribute("tag");
702 m_effectList.insert(ix, effect);
703 m_effectList.removeAt(ix + 1);
704 update(boundingRect());
707 QMap <QString, QString> ClipItem::addEffect(QDomElement effect) {
708 QMap <QString, QString> effectParams;
709 m_effectList.append(effect);
710 effectParams["tag"] = effect.attribute("tag");
711 effectParams["kdenlive_ix"] = effect.attribute("kdenlive_ix");
712 QString state = effect.attribute("disabled");
713 if (!state.isEmpty()) effectParams["disabled"] = state;
714 QDomNodeList params = effect.elementsByTagName("parameter");
715 for (int i = 0; i < params.count(); i++) {
716 QDomElement e = params.item(i).toElement();
718 effectParams[e.attribute("name")] = e.attribute("value");
720 if (!e.attribute("factor").isEmpty()) {
721 effectParams[e.attribute("name")] = QString::number(effectParams[e.attribute("name")].toDouble() / e.attribute("factor").toDouble());
725 update(boundingRect());
729 QMap <QString, QString> ClipItem::getEffectArgs(QDomElement effect) {
730 QMap <QString, QString> effectParams;
731 effectParams["tag"] = effect.attribute("tag");
732 effectParams["kdenlive_ix"] = effect.attribute("kdenlive_ix");
733 QString state = effect.attribute("disabled");
734 if (!state.isEmpty()) effectParams["disabled"] = state;
735 QDomNodeList params = effect.elementsByTagName("parameter");
736 for (int i = 0; i < params.count(); i++) {
737 QDomElement e = params.item(i).toElement();
738 if (e.attribute("name").contains(";")) {
739 QString format = e.attribute("format");
740 QStringList separators = format.split("%d", QString::SkipEmptyParts);
741 QStringList values = e.attribute("value").split(QRegExp("[,:;x]"));
743 QTextStream txtNeu(&neu);
744 if (values.size() > 0)
745 txtNeu << (int)values[0].toDouble();
746 for (int i = 0;i < separators.size() && i + 1 < values.size();i++) {
747 txtNeu << separators[i];
748 txtNeu << (int)(values[i+1].toDouble());
750 effectParams["start"] = neu;
753 effectParams[e.attribute("name")] = e.attribute("value");
755 if (!e.attribute("factor").isEmpty()) {
756 effectParams[e.attribute("name")] = QString::number(effectParams[e.attribute("name")].toDouble() / e.attribute("factor").toDouble());
762 void ClipItem::deleteEffect(QString index) {
763 for (int i = 0; i < m_effectList.size(); ++i) {
764 if (m_effectList.at(i).attribute("kdenlive_ix") == index) {
765 m_effectList.removeAt(i);
770 update(boundingRect());
773 void ClipItem::addTransition(Transition tr) {
774 m_transitionsList.append(tr);
779 void ClipItem::dropEvent(QGraphicsSceneDragDropEvent * event) {
780 QString effects = QString(event->mimeData()->data("kdenlive/effectslist"));
782 doc.setContent(effects, true);
783 QDomElement e = doc.documentElement();
784 CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
785 if (view) view->slotAddEffect(e, m_startPos, m_track);
789 void ClipItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event) {
790 event->setAccepted(event->mimeData()->hasFormat("kdenlive/effectslist"));
793 void ClipItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event) {
799 void CustomTrackView::mousePressEvent ( QMouseEvent * event )
801 int pos = event->x();
802 if (event->modifiers() == Qt::ControlModifier)
803 setDragMode(QGraphicsView::ScrollHandDrag);
804 else if (event->modifiers() == Qt::ShiftModifier)
805 setDragMode(QGraphicsView::RubberBandDrag);
807 QGraphicsItem * item = itemAt(event->pos());
810 else emit cursorMoved((int) mapToScene(event->x(), 0).x());
813 QGraphicsView::mousePressEvent(event);
816 void CustomTrackView::mouseReleaseEvent ( QMouseEvent * event )
818 QGraphicsView::mouseReleaseEvent(event);
819 setDragMode(QGraphicsView::NoDrag);
823 #include "clipitem.moc"