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()
213 int endpixel = rectInView.width() + rectInView.x();
217 //painter->setRenderHints(QPainter::Antialiasing);
219 QPainterPath roundRectPathUpper, roundRectPathLower;
220 double roundingY = 20;
221 double roundingX = 20;
223 painter->setClipRect(option->exposedRect);
224 if (roundingX > br.width() / 2) roundingX = br.width() / 2;
226 int br_endx = (int)(br.x() + br .width() - offset);
227 int br_startx = (int)(br.x() + offset);
228 int br_starty = (int)(br.y());
229 int br_halfy = (int)(br.y() + br.height() / 2 - offset);
230 int br_endy = (int)(br.y() + br.height());
231 int left_upper = 0, left_lower = 0, right_upper = 0, right_lower = 0;
233 if (m_hover && false) {
234 if (!true) /*TRANSITIONSTART to upper clip*/
236 if (!false) /*TRANSITIONSTART to lower clip*/
238 if (!true) /*TRANSITIONEND to upper clip*/
240 if (!false) /*TRANSITIONEND to lower clip*/
243 QList<QPainterPath> transitionPath;
244 foreach(Transition* t, m_transitionsList) {
246 //t.addRect(br_startx,br.y()+br.height()/2,br.x() + /*t->transitionDuration().frames(m_fps) *pixelForOneFrame*/5 ,br.y()+br.height()*2);
248 t.moveTo(br_startx + twidth , br_endy);
249 t.lineTo(br_startx + twidth , br_halfy + roundingY);
251 t.arcTo(br_startx + twidth - roundingX , br_halfy , roundingX, roundingY, 0.0, 90.0);
252 t.lineTo(br_startx + roundingX , br_halfy);
253 t.arcTo(br_startx , br_halfy, roundingX , roundingY, 90.0, 90.0);
254 t.lineTo(br_startx , br_endy);
256 transitionPath.append(t);
258 // build path around clip
259 roundRectPathUpper.moveTo(br_endx - right_upper , br_halfy);
260 roundRectPathUpper.arcTo(br_endx - roundingX - right_upper , br_starty , roundingX, roundingY, 0.0, 90.0);
261 roundRectPathUpper.lineTo(br_startx + roundingX + left_upper, br_starty);
262 roundRectPathUpper.arcTo(br_startx + left_upper, br_starty , roundingX, roundingY, 90.0, 90.0);
263 roundRectPathUpper.lineTo(br_startx + left_upper, br_halfy);
265 roundRectPathLower.moveTo(br_startx + left_lower, br_halfy);
266 roundRectPathLower.arcTo(br_startx + left_lower, br_endy - roundingY , roundingX, roundingY, 180.0, 90.0);
267 roundRectPathLower.lineTo(br_endx - roundingX - right_lower , br_endy);
268 roundRectPathLower.arcTo(br_endx - roundingX - right_lower , br_endy - roundingY, roundingX, roundingY, 270.0, 90.0);
269 roundRectPathLower.lineTo(br_endx - right_lower , br_halfy);
271 QPainterPath resultClipPath = roundRectPathUpper.united(roundRectPathLower);
272 foreach(QPainterPath p, transitionPath) {
273 resultClipPath = resultClipPath.united(p);
275 painter->setClipPath(resultClipPath.intersected(clippath), Qt::IntersectClip);
276 //painter->fillPath(roundRectPath, brush()); //, QBrush(QColor(Qt::red)));
277 painter->fillRect(br.intersected(rectInView), paintColor);
278 //painter->fillRect(QRectF(br.x() + br.width() - m_endPix.width(), br.y(), m_endPix.width(), br.height()), QBrush(QColor(Qt::black)));
281 if (!m_startPix.isNull() && KdenliveSettings::videothumbnails()) {
282 if (m_clipType == IMAGE) {
283 painter->drawPixmap(QPointF(br.x() + br.width() - m_startPix.width(), br.y()), m_startPix);
284 QLineF l(br.x() + br.width() - m_startPix.width(), br.y(), br.x() + br.width() - m_startPix.width(), br.y() + br.height());
285 painter->drawLine(l);
287 painter->drawPixmap(QPointF(br.x() + br.width() - m_endPix.width(), br.y()), m_endPix);
288 QLineF l(br.x() + br.width() - m_endPix.width(), br.y(), br.x() + br.width() - m_endPix.width(), br.y() + br.height());
289 painter->drawLine(l);
292 painter->drawPixmap(QPointF(br.x(), br.y()), m_startPix);
293 QLineF l2(br.x() + m_startPix.width(), br.y(), br.x() + m_startPix.width(), br.y() + br.height());
294 painter->drawLine(l2);
297 double scale = br.width() / m_cropDuration.frames(m_fps);
298 // draw audio thumbnails
299 if ((m_clipType == AV || m_clipType == AUDIO) && audioThumbReady && KdenliveSettings::audiothumbnails()) {
301 QPainterPath path = m_clipType == AV ? roundRectPathLower : roundRectPathUpper.united(roundRectPathLower);
302 if (m_clipType == AV) painter->fillPath(path, QBrush(QColor(200, 200, 200, 140)));
305 if (scale != framePixelWidth)
306 audioThumbCachePic.clear();
307 emit prepareAudioThumb(scale, path, startpixel, endpixel + 200);//200 more for less missing parts before repaint after scrolling
308 int cropLeft = (m_cropStart).frames(m_fps) * scale;
309 for (int startCache = startpixel - startpixel % 100; startCache < endpixel + 300;startCache += 100) {
310 if (audioThumbCachePic.contains(startCache) && !audioThumbCachePic[startCache].isNull())
311 painter->drawPixmap((int)(roundRectPathUpper.united(roundRectPathLower).boundingRect().x() + startCache - cropLeft), (int)(path.boundingRect().y()), audioThumbCachePic[startCache]);
316 // draw start / end fades
319 fades = QBrush(QColor(200, 50, 50, 150));
320 } else fades = QBrush(QColor(200, 200, 200, 200));
322 if (m_startFade != 0) {
323 QPainterPath fadeInPath;
324 fadeInPath.moveTo(br.x() - offset, br.y());
325 fadeInPath.lineTo(br.x() - offset, br.y() + br.height());
326 fadeInPath.lineTo(br.x() + m_startFade * scale, br.y());
327 fadeInPath.closeSubpath();
328 painter->fillPath(fadeInPath, fades);
330 QLineF l(br.x() + m_startFade * scale, br.y(), br.x(), br.y() + br.height());
331 painter->drawLine(l);
334 if (m_endFade != 0) {
335 QPainterPath fadeOutPath;
336 fadeOutPath.moveTo(br.x() + br.width(), br.y());
337 fadeOutPath.lineTo(br.x() + br.width(), br.y() + br.height());
338 fadeOutPath.lineTo(br.x() + br.width() - m_endFade * scale, br.y());
339 fadeOutPath.closeSubpath();
340 painter->fillPath(fadeOutPath, fades);
342 QLineF l(br.x() + br.width() - m_endFade * scale, br.y(), br.x() + br.width(), br.y() + br.height());
343 painter->drawLine(l);
347 QPen pen = painter->pen();
348 pen.setColor(Qt::white);
349 //pen.setStyle(Qt::DashDotDotLine); //Qt::DotLine);
351 // Draw effects names
352 QString effects = effectNames().join(" / ");
353 if (!effects.isEmpty()) {
354 painter->setPen(pen);
355 QFont font = painter->font();
356 QFont smallFont = font;
357 smallFont.setPointSize(8);
358 painter->setFont(smallFont);
359 QRectF txtBounding = painter->boundingRect(br, Qt::AlignLeft | Qt::AlignTop, " " + effects + " ");
360 painter->fillRect(txtBounding, QBrush(QColor(0, 0, 0, 150)));
361 painter->drawText(txtBounding, Qt::AlignCenter, effects);
362 pen.setColor(Qt::black);
363 painter->setPen(pen);
364 painter->setFont(font);
367 // For testing puspose only: draw transitions count
369 painter->setPen(pen);
370 QFont font = painter->font();
371 QFont smallFont = font;
372 smallFont.setPointSize(8);
373 painter->setFont(smallFont);
374 QString txt = " Transitions: " + QString::number(m_transitionsList.count()) + " ";
375 QRectF txtBoundin = painter->boundingRect(br, Qt::AlignRight | Qt::AlignTop, txt);
376 painter->fillRect(txtBoundin, QBrush(QColor(0, 0, 0, 150)));
377 painter->drawText(txtBoundin, Qt::AlignCenter, txt);
378 pen.setColor(Qt::black);
379 painter->setPen(pen);
380 painter->setFont(font);
384 QRectF txtBounding = painter->boundingRect(br, Qt::AlignHCenter | Qt::AlignTop, " " + m_clipName + " ");
385 painter->fillRect(txtBounding, QBrush(QColor(255, 255, 255, 150)));
386 painter->drawText(txtBounding, Qt::AlignCenter, m_clipName);
388 // draw frame around clip
389 pen.setColor(Qt::red);
391 if (isSelected()) painter->setPen(pen);
392 painter->setClipRect(option->exposedRect);
393 painter->drawPath(resultClipPath.intersected(clippath));
394 foreach(QPainterPath p, transitionPath) {
396 painter->fillPath(p, QBrush(QColor(255, 255, 0, 100)));
397 painter->drawPath(p);
400 //painter->fillRect(startpixel,0,startpixel+endpixel,(int)br.height(), QBrush(QColor(255,255,255,150)));
401 //painter->fillRect(QRect(br.x(), br.y(), roundingX, roundingY), QBrush(QColor(Qt::green)));
403 /*QRectF recta(rect().x(), rect().y(), scale,rect().height());
404 painter->drawRect(recta);
405 painter->drawLine(rect().x() + 1, rect().y(), rect().x() + 1, rect().y() + rect().height());
406 painter->drawLine(rect().x() + rect().width(), rect().y(), rect().x() + rect().width(), rect().y() + rect().height());
407 painter->setPen(QPen(Qt::black, 1.0));
408 painter->drawLine(rect().x(), rect().y(), rect().x() + rect().width(), rect().y());
409 painter->drawLine(rect().x(), rect().y() + rect().height(), rect().x() + rect().width(), rect().y() + rect().height());*/
411 //QGraphicsRectItem::paint(painter, option, widget);
412 //QPen pen(Qt::green, 1.0 / size.x() + 0.5);
413 //painter->setPen(pen);
414 //painter->drawLine(rect().x(), rect().y(), rect().x() + rect().width(), rect().y());
415 //kDebug()<<"ITEM REPAINT RECT: "<<boundingRect().width();
416 //painter->drawText(rect(), Qt::AlignCenter, m_name);
417 // painter->drawRect(boundingRect());
418 //painter->drawRoundRect(-10, -10, 20, 20);
420 painter->setPen(QPen(Qt::black));
421 painter->setBrush(QBrush(Qt::yellow));
422 painter->drawEllipse((int)(br.x() + 10), (int)(br.y() + br.height() / 2 - 5) , 10, 10);
423 painter->drawEllipse((int)(br.x() + br.width() - 20), (int)(br.y() + br.height() / 2 - 5), 10, 10);
428 OPERATIONTYPE ClipItem::operationMode(QPointF pos, double scale) {
429 if (abs((int)(pos.x() - (rect().x() + scale * m_startFade))) < 6 && abs((int)(pos.y() - rect().y())) < 6) return FADEIN;
430 else if (abs((int)(pos.x() - rect().x())) < 6) return RESIZESTART;
431 else if (abs((int)(pos.x() - (rect().x() + rect().width() - scale * m_endFade))) < 6 && abs((int)(pos.y() - rect().y())) < 6) return FADEOUT;
432 else if (abs((int)(pos.x() - (rect().x() + rect().width()))) < 6) return RESIZEEND;
433 else if (abs((int)(pos.x() - (rect().x() + 10))) < 6 && abs((int)(pos.y() - (rect().y() + rect().height() / 2 - 5))) < 6) return TRANSITIONSTART;
434 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;
438 void ClipItem::slotPrepareAudioThumb(double pixelForOneFrame, QPainterPath path, int startpixel, int endpixel) {
441 QRectF re = path.boundingRect();
443 //if ( (!audioThumbWasDrawn || framePixelWidth!=pixelForOneFrame ) && !baseClip()->audioFrameChache.isEmpty()){
445 for (int startCache = startpixel - startpixel % 100;startCache + 100 < endpixel ;startCache += 100) {
446 //kDebug() << "creating " << startCache;
447 //if (framePixelWidth!=pixelForOneFrame ||
448 if (framePixelWidth == pixelForOneFrame && audioThumbCachePic.contains(startCache))
450 if (audioThumbCachePic[startCache].isNull() || framePixelWidth != pixelForOneFrame) {
451 audioThumbCachePic[startCache] = QPixmap(100, (int)(re.height()));
452 audioThumbCachePic[startCache].fill(QColor(200, 200, 200, 0));
454 bool fullAreaDraw = pixelForOneFrame < 10;
455 QMap<int, QPainterPath > positiveChannelPaths;
456 QMap<int, QPainterPath > negativeChannelPaths;
457 QPainter pixpainter(&audioThumbCachePic[startCache]);
459 audiopen.setWidth(0);
460 pixpainter.setPen(audiopen);
461 //pixpainter.setRenderHint(QPainter::Antialiasing,true);
462 //pixpainter.drawLine(0,0,100,re.height());
463 int channelHeight = audioThumbCachePic[startCache].height() / channels;
465 for (int i = 0;i < channels;i++) {
467 positiveChannelPaths[i].moveTo(0, channelHeight*i + channelHeight / 2);
468 negativeChannelPaths[i].moveTo(0, channelHeight*i + channelHeight / 2);
471 for (int samples = 0;samples <= 100;samples++) {
472 double frame = (double)(samples + startCache - 0) / pixelForOneFrame;
473 int sample = (int)((frame - (int)(frame)) * 20); // AUDIO_FRAME_SIZE
474 if (frame < 0 || sample < 0 || sample > 19)
476 QMap<int, QByteArray> frame_channel_data = baseClip()->audioFrameChache[(int)frame];
478 for (int channel = 0;channel < channels && frame_channel_data[channel].size() > 0;channel++) {
480 int y = channelHeight * channel + channelHeight / 2;
481 int delta = (int)(frame_channel_data[channel][sample] - 127 / 2) * channelHeight / 64;
483 positiveChannelPaths[channel].lineTo(samples, 0.1 + y + qAbs(delta));
484 negativeChannelPaths[channel].lineTo(samples, 0.1 + y - qAbs(delta));
486 positiveChannelPaths[channel].lineTo(samples, 0.1 + y + delta);
487 negativeChannelPaths[channel].lineTo(samples, 0.1 + y - delta);
490 for (int channel = 0;channel < channels ;channel++)
491 if (fullAreaDraw && samples == 100) {
492 positiveChannelPaths[channel].lineTo(samples, channelHeight*channel + channelHeight / 2);
493 negativeChannelPaths[channel].lineTo(samples, channelHeight*channel + channelHeight / 2);
494 positiveChannelPaths[channel].lineTo(0, channelHeight*channel + channelHeight / 2);
495 negativeChannelPaths[channel].lineTo(0, channelHeight*channel + channelHeight / 2);
499 if (m_clipType != AV) pixpainter.setBrush(QBrush(QColor(200, 200, 100)));
501 pixpainter.setPen(QPen(QColor(0, 0, 0)));
502 pixpainter.setBrush(QBrush(QColor(60, 60, 60)));
504 for (int i = 0;i < channels;i++) {
506 //pixpainter.fillPath(positiveChannelPaths[i].united(negativeChannelPaths[i]),QBrush(Qt::SolidPattern));//or singleif looks better
507 pixpainter.drawPath(positiveChannelPaths[i].united(negativeChannelPaths[i]));//or singleif looks better
509 pixpainter.drawPath(positiveChannelPaths[i]);
512 //audioThumbWasDrawn=true;
513 framePixelWidth = pixelForOneFrame;
518 int ClipItem::fadeIn() const {
522 int ClipItem::fadeOut() const {
526 void ClipItem::setFadeIn(int pos, double scale) {
527 int oldIn = m_startFade;
528 if (pos < 0) pos = 0;
529 if (pos > m_cropDuration.frames(m_fps)) pos = (int)(m_cropDuration.frames(m_fps) / 2);
531 if (oldIn > pos) update(rect().x(), rect().y(), oldIn * scale, rect().height());
532 else update(rect().x(), rect().y(), pos * scale, rect().height());
535 void ClipItem::setFadeOut(int pos, double scale) {
536 int oldOut = m_endFade;
537 if (pos < 0) pos = 0;
538 if (pos > m_cropDuration.frames(m_fps)) pos = (int)(m_cropDuration.frames(m_fps) / 2);
540 if (oldOut > pos) update(rect().x() + rect().width() - pos * scale, rect().y(), pos * scale, rect().height());
541 else update(rect().x() + rect().width() - oldOut * scale, rect().y(), oldOut * scale, rect().height());
547 void ClipItem::mousePressEvent(QGraphicsSceneMouseEvent * event) {
548 /*m_resizeMode = operationMode(event->pos());
549 if (m_resizeMode == MOVE) {
550 m_maxTrack = scene()->sceneRect().height();
551 m_grabPoint = (int) (event->pos().x() - rect().x());
553 QGraphicsRectItem::mousePressEvent(event);
557 void ClipItem::mouseReleaseEvent(QGraphicsSceneMouseEvent * event) {
559 QGraphicsRectItem::mouseReleaseEvent(event);
563 void ClipItem::hoverEnterEvent(QGraphicsSceneHoverEvent *) {
569 void ClipItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *) {
574 void ClipItem::moveTo(int x, double scale, double offset, int newTrack) {
575 double origX = rect().x();
576 double origY = rect().y();
579 setRect(x * scale, origY + offset, rect().width(), rect().height());
580 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
581 if (collisionList.size() == 0) m_track = newTrack;
582 for (int i = 0; i < collisionList.size(); ++i) {
583 QGraphicsItem *item = collisionList.at(i);
584 if (item->type() == 70000) {
586 QRectF other = ((QGraphicsRectItem *)item)->rect();
587 if (x < m_startPos.frames(m_fps)) {
588 kDebug() << "COLLISION, MOVING TO------";
589 m_startPos = ((ClipItem *)item)->endPos() + GenTime(1, m_fps);
590 origX = m_startPos.frames(m_fps) * scale;
592 kDebug() << "COLLISION, MOVING TO+++";
593 m_startPos = ((ClipItem *)item)->startPos() - m_cropDuration;
594 origX = m_startPos.frames(m_fps) * scale;
597 setRect(origX, origY, rect().width(), rect().height());
606 m_startPos = GenTime(x, m_fps);
608 /* QList <QGraphicsItem *> childrenList = QGraphicsItem::children();
609 for (int i = 0; i < childrenList.size(); ++i) {
610 childrenList.at(i)->moveBy(rect().x() - origX , offset);
614 void ClipItem::resizeStart(int posx, double scale) {
615 GenTime durationDiff = GenTime(posx, m_fps) - m_startPos;
616 if (durationDiff == GenTime()) return;
617 //kDebug() << "-- RESCALE: CROP=" << m_cropStart << ", DIFF = " << durationDiff;
618 if (m_cropStart + durationDiff < GenTime()) {
619 durationDiff = GenTime() - m_cropStart;
620 } else if (durationDiff >= m_cropDuration) {
621 durationDiff = m_cropDuration - GenTime(3, m_fps);
623 m_startPos += durationDiff;
624 m_cropStart += durationDiff;
625 m_cropDuration = m_cropDuration - durationDiff;
626 setRect(m_startPos.frames(m_fps) * scale, rect().y(), m_cropDuration.frames(m_fps) * scale, rect().height());
627 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
628 for (int i = 0; i < collisionList.size(); ++i) {
629 QGraphicsItem *item = collisionList.at(i);
630 if (item->type() == 70000) {
631 GenTime diff = ((ClipItem *)item)->endPos() + GenTime(1, m_fps) - m_startPos;
632 setRect((m_startPos + diff).frames(m_fps) * scale, rect().y(), (m_cropDuration - diff).frames(m_fps) * scale, rect().height());
635 m_cropDuration = m_cropDuration - diff;
639 if (m_hasThumbs) startThumbTimer->start(100);
642 void ClipItem::resizeEnd(int posx, double scale) {
643 GenTime durationDiff = GenTime(posx, m_fps) - endPos();
644 if (durationDiff == GenTime()) return;
645 //kDebug() << "-- RESCALE: CROP=" << m_cropStart << ", DIFF = " << durationDiff;
646 if (m_cropDuration + durationDiff <= GenTime()) {
647 durationDiff = GenTime() - (m_cropDuration - GenTime(3, m_fps));
648 } else if (m_cropDuration + durationDiff >= m_maxDuration) {
649 durationDiff = m_maxDuration - m_cropDuration;
651 m_cropDuration += durationDiff;
652 setRect(m_startPos.frames(m_fps) * scale, rect().y(), m_cropDuration.frames(m_fps) * scale, rect().height());
653 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
654 for (int i = 0; i < collisionList.size(); ++i) {
655 QGraphicsItem *item = collisionList.at(i);
656 if (item->type() == 70000) {
657 GenTime diff = ((ClipItem *)item)->startPos() - GenTime(1, m_fps) - startPos();
658 m_cropDuration = diff;
659 setRect(m_startPos.frames(m_fps) * scale, rect().y(), m_cropDuration.frames(m_fps) * scale, rect().height());
663 if (m_hasThumbs) endThumbTimer->start(100);
667 void ClipItem::mouseMoveEvent(QGraphicsSceneMouseEvent * event) {
670 int ClipItem::track() const {
674 void ClipItem::setTrack(int track) {
678 int ClipItem::effectsCounter() {
679 return m_effectsCounter++;
682 int ClipItem::effectsCount() {
683 return m_effectList.size();
686 QStringList ClipItem::effectNames() {
687 return m_effectList.effectNames();
690 QDomElement ClipItem::effectAt(int ix) {
691 return m_effectList.at(ix);
694 void ClipItem::setEffectAt(int ix, QDomElement effect) {
695 kDebug() << "CHange EFFECT AT: " << ix << ", CURR: " << m_effectList.at(ix).attribute("tag") << ", NEW: " << effect.attribute("tag");
696 m_effectList.insert(ix, effect);
697 m_effectList.removeAt(ix + 1);
698 update(boundingRect());
701 QMap <QString, QString> ClipItem::addEffect(QDomElement effect) {
702 QMap <QString, QString> effectParams;
703 m_effectList.append(effect);
704 effectParams["tag"] = effect.attribute("tag");
705 effectParams["kdenlive_ix"] = effect.attribute("kdenlive_ix");
706 QString state = effect.attribute("disabled");
707 if (!state.isEmpty()) effectParams["disabled"] = state;
708 QDomNodeList params = effect.elementsByTagName("parameter");
709 for (int i = 0; i < params.count(); i++) {
710 QDomElement e = params.item(i).toElement();
712 effectParams[e.attribute("name")] = e.attribute("value");
714 if (!e.attribute("factor").isEmpty()) {
715 effectParams[e.attribute("name")] = QString::number(effectParams[e.attribute("name")].toDouble() / e.attribute("factor").toDouble());
719 update(boundingRect());
723 QMap <QString, QString> ClipItem::getEffectArgs(QDomElement effect) {
724 QMap <QString, QString> effectParams;
725 effectParams["tag"] = effect.attribute("tag");
726 effectParams["kdenlive_ix"] = effect.attribute("kdenlive_ix");
727 QString state = effect.attribute("disabled");
728 if (!state.isEmpty()) effectParams["disabled"] = state;
729 QDomNodeList params = effect.elementsByTagName("parameter");
730 for (int i = 0; i < params.count(); i++) {
731 QDomElement e = params.item(i).toElement();
732 if (e.attribute("name").contains(";")) {
733 QString format = e.attribute("format");
734 QStringList separators = format.split("%d", QString::SkipEmptyParts);
735 QStringList values = e.attribute("value").split(QRegExp("[,:;x]"));
737 QTextStream txtNeu(&neu);
738 if (values.size() > 0)
739 txtNeu << (int)values[0].toDouble();
740 for (int i = 0;i < separators.size() && i + 1 < values.size();i++) {
741 txtNeu << separators[i];
742 txtNeu << (int)(values[i+1].toDouble());
744 effectParams["start"] = neu;
747 effectParams[e.attribute("name")] = e.attribute("value");
749 if (!e.attribute("factor").isEmpty()) {
750 effectParams[e.attribute("name")] = QString::number(effectParams[e.attribute("name")].toDouble() / e.attribute("factor").toDouble());
756 void ClipItem::deleteEffect(QString index) {
757 for (int i = 0; i < m_effectList.size(); ++i) {
758 if (m_effectList.at(i).attribute("kdenlive_ix") == index) {
759 m_effectList.removeAt(i);
764 update(boundingRect());
767 void ClipItem::addTransition(Transition tr) {
768 m_transitionsList.append(&tr);
773 void ClipItem::dropEvent(QGraphicsSceneDragDropEvent * event) {
774 QString effects = QString(event->mimeData()->data("kdenlive/effectslist"));
776 doc.setContent(effects, true);
777 QDomElement e = doc.documentElement();
778 CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
779 if (view) view->slotAddEffect(e, m_startPos, m_track);
783 void ClipItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event) {
784 event->setAccepted(event->mimeData()->hasFormat("kdenlive/effectslist"));
787 void ClipItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event) {
793 void CustomTrackView::mousePressEvent ( QMouseEvent * event )
795 int pos = event->x();
796 if (event->modifiers() == Qt::ControlModifier)
797 setDragMode(QGraphicsView::ScrollHandDrag);
798 else if (event->modifiers() == Qt::ShiftModifier)
799 setDragMode(QGraphicsView::RubberBandDrag);
801 QGraphicsItem * item = itemAt(event->pos());
804 else emit cursorMoved((int) mapToScene(event->x(), 0).x());
807 QGraphicsView::mousePressEvent(event);
810 void CustomTrackView::mouseReleaseEvent ( QMouseEvent * event )
812 QGraphicsView::mouseReleaseEvent(event);
813 setDragMode(QGraphicsView::NoDrag);
817 #include "clipitem.moc"