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::ItemClipsChildrenToShape | 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(100, 100, 150));
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() {
153 return m_maxDuration;
156 GenTime ClipItem::duration() {
157 return m_cropDuration;
160 GenTime ClipItem::startPos() {
164 GenTime ClipItem::cropStart() {
168 GenTime ClipItem::endPos() {
169 return m_startPos + m_cropDuration;
172 void ClipItem::flashClip() {
173 if (m_timeLine == 0) {
174 m_timeLine = new QTimeLine(750, this);
175 connect(m_timeLine, SIGNAL(valueChanged(qreal)), this, SLOT(animate(qreal)));
180 void ClipItem::animate(qreal value) {
186 void ClipItem::paint(QPainter *painter,
187 const QStyleOptionGraphicsItem *option,
189 painter->setOpacity(m_opacity);
190 QBrush paintColor = brush();
191 if (isSelected()) paintColor = QBrush(QColor(150, 50, 100));
193 QRect rectInView;//this is the rect that is visible by the user
194 if (scene()->views().size() > 0) {
195 rectInView = scene()->views()[0]->viewport()->rect();
196 rectInView.moveTo(scene()->views()[0]->horizontalScrollBar()->value(), scene()->views()[0]->verticalScrollBar()->value());
197 rectInView.adjust(-10, -10, 10, 10);//make view rect 10 pixel greater on each site, or repaint after scroll event
198 //kDebug() << scene()->views()[0]->viewport()->rect() << " " << scene()->views()[0]->horizontalScrollBar()->value();
200 if (rectInView.isNull())
202 QPainterPath clippath;
203 clippath.addRect(rectInView);
204 int startpixel = (int)(rectInView.x() - rect().x()); //start and endpixel that is viewable from rect()
207 int endpixel = rectInView.width() + rectInView.x();
211 painter->setRenderHints(QPainter::Antialiasing);
212 QPainterPath roundRectPathUpper, roundRectPathLower;
213 double roundingY = 20;
214 double roundingX = 20;
216 painter->setClipRect(option->exposedRect);
217 if (roundingX > br.width() / 2) roundingX = br.width() / 2;
218 //kDebug()<<"-----PAINTING, SCAL: "<<scale<<", height: "<<br.height();
219 int br_endx = (int)(br.x() + br .width() - offset);
220 int br_startx = (int)(br.x() + offset);
221 int br_starty = (int)(br.y());
222 int br_halfy = (int)(br.y() + br.height() / 2 - offset);
223 int br_endy = (int)(br.y() + br.height());
224 int left_upper = 0, left_lower = 0, right_upper = 0, right_lower = 0;
226 if (!true) /*TRANSITIONSTART to upper clip*/
228 if (!false) /*TRANSITIONSTART to lower clip*/
230 if (!true) /*TRANSITIONEND to upper clip*/
232 if (!false) /*TRANSITIONEND to lower clip*/
235 roundRectPathUpper.moveTo(br_endx - right_upper , br_halfy);
236 roundRectPathUpper.arcTo(br_endx - roundingX - right_upper , br_starty , roundingX, roundingY, 0.0, 90.0);
237 roundRectPathUpper.lineTo(br_startx + roundingX + left_upper, br_starty);
238 roundRectPathUpper.arcTo(br_startx + left_upper, br_starty , roundingX, roundingY, 90.0, 90.0);
239 roundRectPathUpper.lineTo(br_startx + left_upper, br_halfy);
240 roundRectPathUpper.closeSubpath();
242 roundRectPathLower.moveTo(br_startx + left_lower, br_halfy);
243 roundRectPathLower.arcTo(br_startx + left_lower, br_endy - roundingY , roundingX, roundingY, 180.0, 90.0);
244 roundRectPathLower.lineTo(br_endx - roundingX - right_lower , br_endy);
245 roundRectPathLower.arcTo(br_endx - roundingX - right_lower , br_endy - roundingY, roundingX, roundingY, 270.0, 90.0);
246 roundRectPathLower.lineTo(br_endx - right_lower , br_halfy);
247 roundRectPathLower.closeSubpath();
249 painter->setClipPath(roundRectPathUpper.united(roundRectPathLower).intersected(clippath), Qt::IntersectClip);
250 //painter->fillPath(roundRectPath, brush()); //, QBrush(QColor(Qt::red)));
251 painter->fillRect(br.intersected(rectInView), paintColor);
252 //painter->fillRect(QRectF(br.x() + br.width() - m_endPix.width(), br.y(), m_endPix.width(), br.height()), QBrush(QColor(Qt::black)));
255 if (!m_startPix.isNull() && KdenliveSettings::videothumbnails()) {
256 if (m_clipType == IMAGE) {
257 painter->drawPixmap(QPointF(br.x() + br.width() - m_startPix.width(), br.y()), m_startPix);
258 QLineF l(br.x() + br.width() - m_startPix.width(), br.y(), br.x() + br.width() - m_startPix.width(), br.y() + br.height());
259 painter->drawLine(l);
261 painter->drawPixmap(QPointF(br.x() + br.width() - m_endPix.width(), br.y()), m_endPix);
262 QLineF l(br.x() + br.width() - m_endPix.width(), br.y(), br.x() + br.width() - m_endPix.width(), br.y() + br.height());
263 painter->drawLine(l);
266 painter->drawPixmap(QPointF(br.x(), br.y()), m_startPix);
267 QLineF l2(br.x() + m_startPix.width(), br.y(), br.x() + m_startPix.width(), br.y() + br.height());
268 painter->drawLine(l2);
270 if ((m_clipType == AV || m_clipType == AUDIO) && audioThumbReady && KdenliveSettings::audiothumbnails()) {
272 QPainterPath path = m_clipType == AV ? roundRectPathLower : roundRectPathUpper.united(roundRectPathLower);
273 painter->fillPath(path, QBrush(QColor(200, 200, 200, 127)));
276 double pixelForOneFrame = (double)br.width() / duration().frames(m_fps);
277 if (pixelForOneFrame != framePixelWidth)
278 audioThumbCachePic.clear();
279 emit prepareAudioThumb(pixelForOneFrame, path, startpixel, endpixel + 200);//200 more for less missing parts before repaint after scrolling
281 for (int startCache = startpixel - startpixel % 100; startCache < endpixel + 300;startCache += 100) {
282 if (audioThumbCachePic.contains(startCache) && !audioThumbCachePic[startCache].isNull())
283 painter->drawPixmap((int)(roundRectPathUpper.united(roundRectPathLower).boundingRect().x() + startCache), (int)(path.boundingRect().y()), audioThumbCachePic[startCache]);
288 // draw start / end fades
289 double scale = br.width() / m_cropDuration.frames(m_fps);
292 fades = QBrush(QColor(200, 50, 50, 150));
293 } else fades = QBrush(QColor(200, 200, 200, 200));
295 if (m_startFade != 0) {
296 QPainterPath fadeInPath;
297 fadeInPath.moveTo(br.x() - offset, br.y());
298 fadeInPath.lineTo(br.x() - offset, br.y() + br.height());
299 fadeInPath.lineTo(br.x() + m_startFade * scale, br.y());
300 fadeInPath.closeSubpath();
301 painter->fillPath(fadeInPath, fades);
303 QLineF l(br.x() + m_startFade * scale, br.y(), br.x(), br.y() + br.height());
304 painter->drawLine(l);
307 if (m_endFade != 0) {
308 QPainterPath fadeOutPath;
309 fadeOutPath.moveTo(br.x() + br.width(), br.y());
310 fadeOutPath.lineTo(br.x() + br.width(), br.y() + br.height());
311 fadeOutPath.lineTo(br.x() + br.width() - m_endFade * scale, br.y());
312 fadeOutPath.closeSubpath();
313 painter->fillPath(fadeOutPath, fades);
315 QLineF l(br.x() + br.width() - m_endFade * scale, br.y(), br.x() + br.width(), br.y() + br.height());
316 painter->drawLine(l);
320 QPen pen = painter->pen();
321 pen.setColor(Qt::white);
322 //pen.setStyle(Qt::DashDotDotLine); //Qt::DotLine);
324 QString effects = effectNames().join(" / ");
325 if (!effects.isEmpty()) {
326 painter->setPen(pen);
327 QFont font = painter->font();
328 QFont smallFont = font;
329 smallFont.setPointSize(8);
330 painter->setFont(smallFont);
331 QRectF txtBounding = painter->boundingRect(br, Qt::AlignLeft | Qt::AlignTop, " " + effects + " ");
332 painter->fillRect(txtBounding, QBrush(QColor(0, 0, 0, 150)));
333 painter->drawText(txtBounding, Qt::AlignCenter, effects);
334 pen.setColor(Qt::black);
335 painter->setPen(pen);
336 painter->setFont(font);
339 pen.setColor(Qt::red);
340 //pen.setStyle(Qt::DashDotDotLine); //Qt::DotLine);
341 if (isSelected()) painter->setPen(pen);
342 painter->setClipRect(option->exposedRect);
343 painter->drawPath(roundRectPathUpper.united(roundRectPathLower).intersected(clippath));
345 QRectF txtBounding = painter->boundingRect(br, Qt::AlignCenter, " " + m_clipName + " ");
346 painter->fillRect(txtBounding, QBrush(QColor(255, 255, 255, 150)));
347 painter->drawText(txtBounding, Qt::AlignCenter, m_clipName);
350 //painter->fillRect(startpixel,0,startpixel+endpixel,(int)br.height(), QBrush(QColor(255,255,255,150)));
351 //painter->fillRect(QRect(br.x(), br.y(), roundingX, roundingY), QBrush(QColor(Qt::green)));
353 /*QRectF recta(rect().x(), rect().y(), scale,rect().height());
354 painter->drawRect(recta);
355 painter->drawLine(rect().x() + 1, rect().y(), rect().x() + 1, rect().y() + rect().height());
356 painter->drawLine(rect().x() + rect().width(), rect().y(), rect().x() + rect().width(), rect().y() + rect().height());
357 painter->setPen(QPen(Qt::black, 1.0));
358 painter->drawLine(rect().x(), rect().y(), rect().x() + rect().width(), rect().y());
359 painter->drawLine(rect().x(), rect().y() + rect().height(), rect().x() + rect().width(), rect().y() + rect().height());*/
361 //QGraphicsRectItem::paint(painter, option, widget);
362 //QPen pen(Qt::green, 1.0 / size.x() + 0.5);
363 //painter->setPen(pen);
364 //painter->drawLine(rect().x(), rect().y(), rect().x() + rect().width(), rect().y());
365 //kDebug()<<"ITEM REPAINT RECT: "<<boundingRect().width();
366 //painter->drawText(rect(), Qt::AlignCenter, m_name);
367 // painter->drawRect(boundingRect());
368 //painter->drawRoundRect(-10, -10, 20, 20);
370 painter->setPen(QPen(Qt::black));
371 painter->setBrush(QBrush(Qt::yellow));
372 painter->drawEllipse((int)(br.x() + 10), (int)(br.y() + br.height() / 2 - 5) , 10, 10);
373 painter->drawEllipse((int)(br.x() + br.width() - 20), (int)(br.y() + br.height() / 2 - 5), 10, 10);
378 OPERATIONTYPE ClipItem::operationMode(QPointF pos, double scale) {
379 if (abs((int)(pos.x() - (rect().x() + scale * m_startFade))) < 6 && abs((int)(pos.y() - rect().y())) < 6) return FADEIN;
380 else if (abs((int)(pos.x() - rect().x())) < 6) return RESIZESTART;
381 else if (abs((int)(pos.x() - (rect().x() + rect().width() - scale * m_endFade))) < 6 && abs((int)(pos.y() - rect().y())) < 6) return FADEOUT;
382 else if (abs((int)(pos.x() - (rect().x() + rect().width()))) < 6) return RESIZEEND;
383 else if (abs((int)(pos.x() - (rect().x() + 10))) < 6 && abs((int)(pos.y() - (rect().y() + rect().height() / 2 - 5))) < 6) return TRANSITIONSTART;
384 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;
388 void ClipItem::slotPrepareAudioThumb(double pixelForOneFrame, QPainterPath path, int startpixel, int endpixel) {
391 QRectF re = path.boundingRect();
393 //if ( (!audioThumbWasDrawn || framePixelWidth!=pixelForOneFrame ) && !baseClip()->audioFrameChache.isEmpty()){
395 for (int startCache = startpixel - startpixel % 100;startCache + 100 < endpixel ;startCache += 100) {
396 //kDebug() << "creating " << startCache;
397 //if (framePixelWidth!=pixelForOneFrame ||
398 if (framePixelWidth == pixelForOneFrame && audioThumbCachePic.contains(startCache))
400 if (audioThumbCachePic[startCache].isNull() || framePixelWidth != pixelForOneFrame) {
401 audioThumbCachePic[startCache] = QPixmap(100, (int)(re.height()));
402 audioThumbCachePic[startCache].fill(QColor(200, 200, 200, 127));
404 bool fullAreaDraw = pixelForOneFrame < 10;
405 QMap<int, QPainterPath > positiveChannelPaths;
406 QMap<int, QPainterPath > negativeChannelPaths;
407 QPainter pixpainter(&audioThumbCachePic[startCache]);
409 audiopen.setWidth(0);
410 pixpainter.setPen(audiopen);
411 //pixpainter.setRenderHint(QPainter::Antialiasing,true);
412 //pixpainter.drawLine(0,0,100,re.height());
413 int channelHeight = audioThumbCachePic[startCache].height() / channels;
415 for (int i = 0;i < channels;i++) {
417 positiveChannelPaths[i].moveTo(0, channelHeight*i + channelHeight / 2);
418 negativeChannelPaths[i].moveTo(0, channelHeight*i + channelHeight / 2);
421 for (int samples = 0;samples <= 100;samples++) {
422 double frame = (double)(samples + startCache - 0) / pixelForOneFrame;
423 int sample = (int)((frame - (int)(frame)) * 20); // AUDIO_FRAME_SIZE
424 if (frame < 0 || sample < 0 || sample > 19)
426 QMap<int, QByteArray> frame_channel_data = baseClip()->audioFrameChache[(int)frame];
428 for (int channel = 0;channel < channels && frame_channel_data[channel].size() > 0;channel++) {
430 int y = channelHeight * channel + channelHeight / 2;
431 int delta = (int)(frame_channel_data[channel][sample] - 127 / 2) * channelHeight / 64;
433 positiveChannelPaths[channel].lineTo(samples, 0.1 + y + qAbs(delta));
434 negativeChannelPaths[channel].lineTo(samples, 0.1 + y - qAbs(delta));
436 positiveChannelPaths[channel].lineTo(samples, 0.1 + y + delta);
437 negativeChannelPaths[channel].lineTo(samples, 0.1 + y - delta);
440 for (int channel = 0;channel < channels ;channel++)
441 if (fullAreaDraw && samples == 100) {
442 positiveChannelPaths[channel].lineTo(samples, channelHeight*channel + channelHeight / 2);
443 negativeChannelPaths[channel].lineTo(samples, channelHeight*channel + channelHeight / 2);
444 positiveChannelPaths[channel].lineTo(0, channelHeight*channel + channelHeight / 2);
445 negativeChannelPaths[channel].lineTo(0, channelHeight*channel + channelHeight / 2);
449 for (int i = 0;i < channels;i++) {
451 //pixpainter.fillPath(positiveChannelPaths[i].united(negativeChannelPaths[i]),QBrush(Qt::SolidPattern));//or singleif looks better
452 pixpainter.setBrush(QBrush(QColor(200, 200, 100, 200)));
453 pixpainter.drawPath(positiveChannelPaths[i].united(negativeChannelPaths[i]));//or singleif looks better
455 pixpainter.drawPath(positiveChannelPaths[i]);
458 //audioThumbWasDrawn=true;
459 framePixelWidth = pixelForOneFrame;
464 int ClipItem::fadeIn() const {
468 int ClipItem::fadeOut() const {
472 void ClipItem::setFadeIn(int pos, double scale) {
473 int oldIn = m_startFade;
474 if (pos < 0) pos = 0;
475 if (pos > m_cropDuration.frames(m_fps)) pos = (int)(m_cropDuration.frames(m_fps) / 2);
477 if (oldIn > pos) update(rect().x(), rect().y(), oldIn * scale, rect().height());
478 else update(rect().x(), rect().y(), pos * scale, rect().height());
481 void ClipItem::setFadeOut(int pos, double scale) {
482 int oldOut = m_endFade;
483 if (pos < 0) pos = 0;
484 if (pos > m_cropDuration.frames(m_fps)) pos = (int)(m_cropDuration.frames(m_fps) / 2);
486 if (oldOut > pos) update(rect().x() + rect().width() - pos * scale, rect().y(), pos * scale, rect().height());
487 else update(rect().x() + rect().width() - oldOut * scale, rect().y(), oldOut * scale, rect().height());
493 void ClipItem::mousePressEvent(QGraphicsSceneMouseEvent * event) {
494 /*m_resizeMode = operationMode(event->pos());
495 if (m_resizeMode == MOVE) {
496 m_maxTrack = scene()->sceneRect().height();
497 m_grabPoint = (int) (event->pos().x() - rect().x());
499 QGraphicsRectItem::mousePressEvent(event);
503 void ClipItem::mouseReleaseEvent(QGraphicsSceneMouseEvent * event) {
505 QGraphicsRectItem::mouseReleaseEvent(event);
509 void ClipItem::hoverEnterEvent(QGraphicsSceneHoverEvent *) {
515 void ClipItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *) {
520 void ClipItem::moveTo(int x, double scale, double offset, int newTrack) {
521 double origX = rect().x();
522 double origY = rect().y();
525 setRect(x * scale, origY + offset, rect().width(), rect().height());
526 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
527 if (collisionList.size() == 0) m_track = newTrack;
528 for (int i = 0; i < collisionList.size(); ++i) {
529 QGraphicsItem *item = collisionList.at(i);
530 if (item->type() == 70000) {
532 QRectF other = ((QGraphicsRectItem *)item)->rect();
533 if (x < m_startPos.frames(m_fps)) {
534 kDebug() << "COLLISION, MOVING TO------";
535 m_startPos = ((ClipItem *)item)->endPos() + GenTime(1, m_fps);
536 origX = m_startPos.frames(m_fps) * scale;
538 kDebug() << "COLLISION, MOVING TO+++";
539 m_startPos = ((ClipItem *)item)->startPos() - m_cropDuration;
540 origX = m_startPos.frames(m_fps) * scale;
543 setRect(origX, origY, rect().width(), rect().height());
552 m_startPos = GenTime(x, m_fps);
554 /* QList <QGraphicsItem *> childrenList = QGraphicsItem::children();
555 for (int i = 0; i < childrenList.size(); ++i) {
556 childrenList.at(i)->moveBy(rect().x() - origX , offset);
560 void ClipItem::resizeStart(int posx, double scale) {
561 GenTime durationDiff = GenTime(posx, m_fps) - m_startPos;
562 if (durationDiff == GenTime()) return;
563 //kDebug() << "-- RESCALE: CROP=" << m_cropStart << ", DIFF = " << durationDiff;
564 if (m_cropStart + durationDiff < GenTime()) {
565 durationDiff = GenTime() - m_cropStart;
566 } else if (durationDiff >= m_cropDuration) {
567 durationDiff = m_cropDuration - GenTime(3, m_fps);
569 m_startPos += durationDiff;
570 m_cropStart += durationDiff;
571 m_cropDuration = m_cropDuration - durationDiff;
572 setRect(m_startPos.frames(m_fps) * scale, rect().y(), m_cropDuration.frames(m_fps) * scale, rect().height());
573 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
574 for (int i = 0; i < collisionList.size(); ++i) {
575 QGraphicsItem *item = collisionList.at(i);
576 if (item->type() == 70000) {
577 GenTime diff = ((ClipItem *)item)->endPos() + GenTime(1, m_fps) - m_startPos;
578 setRect((m_startPos + diff).frames(m_fps) * scale, rect().y(), (m_cropDuration - diff).frames(m_fps) * scale, rect().height());
581 m_cropDuration = m_cropDuration - diff;
585 if (m_hasThumbs) startThumbTimer->start(100);
588 void ClipItem::resizeEnd(int posx, double scale) {
589 GenTime durationDiff = GenTime(posx, m_fps) - endPos();
590 if (durationDiff == GenTime()) return;
591 //kDebug() << "-- RESCALE: CROP=" << m_cropStart << ", DIFF = " << durationDiff;
592 if (m_cropDuration + durationDiff <= GenTime()) {
593 durationDiff = GenTime() - (m_cropDuration - GenTime(3, m_fps));
594 } else if (m_cropDuration + durationDiff >= m_maxDuration) {
595 durationDiff = m_maxDuration - m_cropDuration;
597 m_cropDuration += durationDiff;
598 setRect(m_startPos.frames(m_fps) * scale, rect().y(), m_cropDuration.frames(m_fps) * scale, rect().height());
599 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
600 for (int i = 0; i < collisionList.size(); ++i) {
601 QGraphicsItem *item = collisionList.at(i);
602 if (item->type() == 70000) {
603 GenTime diff = ((ClipItem *)item)->startPos() - GenTime(1, m_fps) - startPos();
604 m_cropDuration = diff;
605 setRect(m_startPos.frames(m_fps) * scale, rect().y(), m_cropDuration.frames(m_fps) * scale, rect().height());
609 if (m_hasThumbs) endThumbTimer->start(100);
613 void ClipItem::mouseMoveEvent(QGraphicsSceneMouseEvent * event) {
616 int ClipItem::track() {
620 void ClipItem::setTrack(int track) {
624 int ClipItem::effectsCounter() {
625 return m_effectsCounter++;
628 int ClipItem::effectsCount() {
629 return m_effectList.size();
632 QStringList ClipItem::effectNames() {
633 return m_effectList.effectNames();
636 QDomElement ClipItem::effectAt(int ix) {
637 return m_effectList.at(ix);
640 void ClipItem::setEffectAt(int ix, QDomElement effect) {
641 kDebug() << "CHange EFFECT AT: " << ix << ", CURR: " << m_effectList.at(ix).attribute("tag") << ", NEW: " << effect.attribute("tag");
642 m_effectList.insert(ix, effect);
643 m_effectList.removeAt(ix + 1);
644 update(boundingRect());
647 QMap <QString, QString> ClipItem::addEffect(QDomElement effect) {
648 QMap <QString, QString> effectParams;
649 m_effectList.append(effect);
650 effectParams["tag"] = effect.attribute("tag");
651 effectParams["kdenlive_ix"] = effect.attribute("kdenlive_ix");
652 QString state = effect.attribute("disabled");
653 if (!state.isEmpty()) effectParams["disabled"] = state;
654 QDomNodeList params = effect.elementsByTagName("parameter");
655 for (int i = 0; i < params.count(); i++) {
656 QDomElement e = params.item(i).toElement();
658 effectParams[e.attribute("name")] = e.attribute("value");
660 if (!e.attribute("factor").isEmpty()) {
661 effectParams[e.attribute("name")] = QString::number(effectParams[e.attribute("name")].toDouble() / e.attribute("factor").toDouble());
665 update(boundingRect());
669 QMap <QString, QString> ClipItem::getEffectArgs(QDomElement effect) {
670 QMap <QString, QString> effectParams;
671 effectParams["tag"] = effect.attribute("tag");
672 effectParams["kdenlive_ix"] = effect.attribute("kdenlive_ix");
673 QString state = effect.attribute("disabled");
674 if (!state.isEmpty()) effectParams["disabled"] = state;
675 QDomNodeList params = effect.elementsByTagName("parameter");
676 for (int i = 0; i < params.count(); i++) {
677 QDomElement e = params.item(i).toElement();
678 if (e.attribute("name").contains(";")) {
679 QString format = e.attribute("format");
680 QStringList separators = format.split("%d", QString::SkipEmptyParts);
681 QStringList values = e.attribute("value").split(QRegExp("[,:;x]"));
683 QTextStream txtNeu(&neu);
684 if (values.size() > 0)
685 txtNeu << (int)values[0].toDouble();
686 for (int i = 0;i < separators.size() && i + 1 < values.size();i++) {
687 txtNeu << separators[i];
688 txtNeu << (int)(values[i+1].toDouble());
690 effectParams["start"] = neu;
693 effectParams[e.attribute("name")] = e.attribute("value");
695 if (!e.attribute("factor").isEmpty()) {
696 effectParams[e.attribute("name")] = QString::number(effectParams[e.attribute("name")].toDouble() / e.attribute("factor").toDouble());
702 void ClipItem::deleteEffect(QString index) {
703 for (int i = 0; i < m_effectList.size(); ++i) {
704 if (m_effectList.at(i).attribute("kdenlive_ix") == index) {
705 m_effectList.removeAt(i);
710 update(boundingRect());
714 void ClipItem::dropEvent(QGraphicsSceneDragDropEvent * event) {
715 QString effects = QString(event->mimeData()->data("kdenlive/effectslist"));
717 doc.setContent(effects, true);
718 QDomElement e = doc.documentElement();
719 CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
720 if (view) view->slotAddEffect(e, m_startPos, m_track);
724 void ClipItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event) {
725 event->setAccepted(event->mimeData()->hasFormat("kdenlive/effectslist"));
728 void ClipItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event) {
734 void CustomTrackView::mousePressEvent ( QMouseEvent * event )
736 int pos = event->x();
737 if (event->modifiers() == Qt::ControlModifier)
738 setDragMode(QGraphicsView::ScrollHandDrag);
739 else if (event->modifiers() == Qt::ShiftModifier)
740 setDragMode(QGraphicsView::RubberBandDrag);
742 QGraphicsItem * item = itemAt(event->pos());
745 else emit cursorMoved((int) mapToScene(event->x(), 0).x());
748 QGraphicsView::mousePressEvent(event);
751 void CustomTrackView::mouseReleaseEvent ( QMouseEvent * event )
753 QGraphicsView::mouseReleaseEvent(event);
754 setDragMode(QGraphicsView::NoDrag);
758 #include "clipitem.moc"