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>
30 #include <mlt++/Mlt.h>
34 #include "kdenlivesettings.h"
36 ClipItem::ClipItem(DocClipBase *clip, int track, int startpos, const QRectF & rect, int duration)
37 : 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(0)
40 kDebug()<<"******* CREATING NEW TML CLIP, DUR: "<<duration;
41 m_xml = clip->toXML();
42 m_clipName = clip->name();
43 m_producer = clip->getId();
44 m_clipType = clip->clipType();
46 m_maxDuration = duration;
47 if (duration != -1) m_cropDuration = duration;
48 else m_cropDuration = m_maxDuration;
51 m_cropStart = xml.attribute("in", 0).toInt();
52 m_maxDuration = xml.attribute("duration", 0).toInt();
53 if (m_maxDuration == 0) m_maxDuration = xml.attribute("out", 0).toInt() - m_cropStart;
55 if (duration != -1) m_cropDuration = duration;
56 else m_cropDuration = m_maxDuration;*/
59 setFlags(QGraphicsItem::ItemClipsToShape | QGraphicsItem::ItemClipsChildrenToShape | QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
61 setBrush(QColor(100, 100, 150));
62 if (m_clipType == VIDEO || m_clipType == AV) {
64 connect(this, SIGNAL(getThumb(int, int)), clip->thumbProducer(), SLOT(extractImage(int, int)));
65 connect(clip->thumbProducer(), SIGNAL(thumbReady(int, QPixmap)), this, SLOT(slotThumbReady(int, QPixmap)));
66 QTimer::singleShot(300, this, SLOT(slotFetchThumbs()));
68 startThumbTimer = new QTimer(this);
69 startThumbTimer->setSingleShot(true);
70 connect(startThumbTimer, SIGNAL(timeout()), this, SLOT(slotGetStartThumb()));
71 endThumbTimer = new QTimer(this);
72 endThumbTimer->setSingleShot(true);
73 connect(endThumbTimer, SIGNAL(timeout()), this, SLOT(slotGetEndThumb()));
76 else if (m_clipType == COLOR) {
77 QString colour = m_xml.attribute("colour");
78 colour = colour.replace(0, 2, "#");
79 setBrush(QColor(colour.left(7)));
81 else if (m_clipType == IMAGE) {
82 m_startPix = KThumb::getImage(KUrl(m_xml.attribute("resource")), 50 * KdenliveSettings::project_display_ratio(), 50);
89 if (startThumbTimer) delete startThumbTimer;
90 if (endThumbTimer) delete endThumbTimer;
93 void ClipItem::slotFetchThumbs()
95 emit getThumb(m_cropStart, m_cropStart + m_cropDuration);
98 void ClipItem::slotGetStartThumb()
100 emit getThumb(m_cropStart, -1);
103 void ClipItem::slotGetEndThumb()
105 emit getThumb(-1, m_cropStart + m_cropDuration);
108 void ClipItem::slotThumbReady(int frame, QPixmap pix)
110 if (frame == m_cropStart) m_startPix = pix;
115 int ClipItem::type () const
120 DocClipBase *ClipItem::baseClip()
125 QDomElement ClipItem::xml() const
130 int ClipItem::clipType()
135 QString ClipItem::clipName()
140 int ClipItem::clipProducer()
145 int ClipItem::maxDuration()
147 return m_maxDuration;
150 int ClipItem::duration()
152 return m_cropDuration;
155 int ClipItem::startPos()
160 int ClipItem::cropStart()
165 int ClipItem::endPos()
167 return m_startPos + m_cropDuration;
171 void ClipItem::paint(QPainter *painter,
172 const QStyleOptionGraphicsItem *option,
176 painter->setRenderHints(QPainter::Antialiasing);
177 QPainterPath roundRectPathUpper,roundRectPathLower;
178 double roundingY = 20;
179 double roundingX = 20;
181 painter->setClipRect(option->exposedRect);
182 if (roundingX > br.width() / 2) roundingX = br.width() / 2;
183 //kDebug()<<"-----PAINTING, SCAL: "<<scale<<", height: "<<br.height();
184 roundRectPathUpper.moveTo(br.x() + br .width() - offset, br.y() + br.height()/2 - offset);
185 roundRectPathUpper.arcTo(br.x() + br .width() - roundingX - offset, br.y(), roundingX, roundingY, 0.0, 90.0);
186 roundRectPathUpper.lineTo(br.x() + roundingX, br.y());
187 roundRectPathUpper.arcTo(br.x() + offset, br.y(), roundingX, roundingY, 90.0, 90.0);
188 roundRectPathUpper.lineTo(br.x() + offset, br.y() + br.height()/2 - offset);
189 roundRectPathUpper.closeSubpath();
191 roundRectPathLower.moveTo(br.x() + offset, br.y() + br.height()/2 - offset);
192 roundRectPathLower.arcTo(br.x() + offset, br.y() + br.height() - roundingY - offset, roundingX, roundingY, 180.0, 90.0);
193 roundRectPathLower.lineTo(br.x() + br .width() - roundingX, br.y() + br.height() - offset);
194 roundRectPathLower.arcTo(br.x() + br .width() - roundingX - offset, br.y() + br.height() - roundingY - offset, roundingX, roundingY, 270.0, 90.0);
195 roundRectPathLower.lineTo(br.x() + br .width() - offset, br.y()+ br.height()/2 - offset);
196 roundRectPathLower.closeSubpath();
198 painter->setClipPath(roundRectPathUpper.united(roundRectPathLower), Qt::IntersectClip);
199 //painter->fillPath(roundRectPath, brush()); //, QBrush(QColor(Qt::red)));
200 painter->fillRect(br, brush());
201 //painter->fillRect(QRectF(br.x() + br.width() - m_endPix.width(), br.y(), m_endPix.width(), br.height()), QBrush(QColor(Qt::black)));
204 if (!m_startPix.isNull()) {
205 if (m_clipType == IMAGE) {
206 painter->drawPixmap(QPointF(br.x() + br.width() - m_startPix.width(), br.y()), m_startPix);
207 QLineF l(br.x() + br.width() - m_startPix.width(), br.y(), br.x() + br.width() - m_startPix.width(), br.y() + br.height());
208 painter->drawLine(l);
210 painter->drawPixmap(QPointF(br.x() + br.width() - m_endPix.width(), br.y()), m_endPix);
211 QLineF l(br.x() + br.width() - m_endPix.width(), br.y(), br.x() + br.width() - m_endPix.width(), br.y() + br.height());
212 painter->drawLine(l);
215 painter->drawPixmap(QPointF(br.x(), br.y()), m_startPix);
216 QLineF l2(br.x() + m_startPix.width(), br.y(), br.x() + m_startPix.width(), br.y() + br.height());
217 painter->drawLine(l2);
219 if (m_clipType == AV || m_clipType==AUDIO ){
220 QPainterPath path= m_clipType==AV ? roundRectPathLower : roundRectPathUpper.united(roundRectPathLower);
221 painter->fillPath(path,QBrush(QColor(200,200,200,127)));
223 double pixelForOneFrame=(double)br.width()/duration();
226 /*for (int frame=m_cropStart;frame<m_cropStart+m_cropDuration && frame< baseClip()->audioFrameChache.size();frame++){
228 QMap<int,QByteArray> frame_channel_data=baseClip()->audioFrameChache[frame];
229 int audio_frame_samples=frame_channel_data[0].size();
231 for (int samples=0;samples<audio_frame_samples;samples++){
233 for (int channel=0;channel<channels && frame_channel_data[channel].size()> 0;channel++){
234 int y=re.y()+re.height()*channel/channels+ (re.height()/channels)/2;
235 int x1=re.x() + ((double)frame*pixelForOneFrame*audio_frame_samples+samples)/pixelForOneFrame;
236 kDebug() <<" frame " << frame << " " << x1 ;
237 painter->drawLine(x1 , y+frame_channel_data[channel][0],x1+1, y+frame_channel_data[channel][0] );
241 QRectF re=path.boundingRect();
242 for (int samples=re.x();samples<re.x()+re.width();samples++){
243 double frame=(double)(samples-re.x())/pixelForOneFrame;
244 int sample=(frame-(int)(frame))*20 ;// AUDIO_FRAME_SIZE
246 if (frame<0 || sample< 0 || sample>19 )
248 QMap<int,QByteArray> frame_channel_data=baseClip()->audioFrameChache[(int)frame];
250 for (int channel=0;channel<channels && frame_channel_data[channel].size()> 0;channel++){
252 int y=re.y()+re.height()*channel/channels+ (re.height()/channels)/2;
253 painter->drawLine(samples , y+frame_channel_data[channel][sample],samples+1, y+frame_channel_data[channel][sample] );
254 //painter->drawLine(samples , y+samples-10,samples+1, y+samples-10 );
259 // draw start / end fades
260 double scale = br.width() / m_cropDuration;
263 fades = QBrush(QColor(200, 50, 50, 150));
265 else fades = QBrush(QColor(200, 200, 200, 200));
267 if (m_startFade != 0) {
268 QPainterPath fadeInPath;
269 fadeInPath.moveTo(br.x() - offset, br.y());
270 fadeInPath.lineTo(br.x() - offset, br.y() + br.height());
271 fadeInPath.lineTo(br.x() + m_startFade * scale, br.y());
272 fadeInPath.closeSubpath();
273 painter->fillPath(fadeInPath, fades);
275 QLineF l(br.x() + m_startFade * scale, br.y(), br.x(), br.y() + br.height());
276 painter->drawLine(l);
279 if (m_endFade != 0) {
280 QPainterPath fadeOutPath;
281 fadeOutPath.moveTo(br.x() + br.width(), br.y());
282 fadeOutPath.lineTo(br.x() + br.width(), br.y() + br.height());
283 fadeOutPath.lineTo(br.x() + br.width() - m_endFade * scale, br.y());
284 fadeOutPath.closeSubpath();
285 painter->fillPath(fadeOutPath, fades);
287 QLineF l(br.x() + br.width() - m_endFade * scale, br.y(), br.x() + br.width(), br.y() + br.height());
288 painter->drawLine(l);
292 QPen pen = painter->pen();
293 pen.setColor(Qt::white);
294 //pen.setStyle(Qt::DashDotDotLine); //Qt::DotLine);
296 QString effects = effectNames().join(" / ");
297 if (!effects.isEmpty()) {
298 painter->setPen(pen);
299 QFont font = painter->font();
300 QFont smallFont = font;
301 smallFont.setPointSize(8);
302 painter->setFont(smallFont);
303 QRectF txtBounding = painter->boundingRect(br, Qt::AlignLeft | Qt::AlignTop, " " + effects + " ");
304 painter->fillRect(txtBounding, QBrush(QColor(0,0,0,150)));
305 painter->drawText(txtBounding, Qt::AlignCenter, effects);
306 pen.setColor(Qt::black);
307 painter->setPen(pen);
308 painter->setFont(font);
311 QRectF txtBounding = painter->boundingRect(br, Qt::AlignCenter, " " + m_clipName + " ");
312 painter->fillRect(txtBounding, QBrush(QColor(255,255,255,150)));
313 painter->drawText(txtBounding, Qt::AlignCenter, m_clipName);
315 pen.setColor(Qt::red);
316 pen.setStyle(Qt::DashDotDotLine); //Qt::DotLine);
317 if (isSelected()) painter->setPen(pen);
318 painter->setClipRect(option->exposedRect);
319 painter->drawPath(roundRectPathUpper.united(roundRectPathLower));
320 //painter->fillRect(QRect(br.x(), br.y(), roundingX, roundingY), QBrush(QColor(Qt::green)));
322 /*QRectF recta(rect().x(), rect().y(), scale,rect().height());
323 painter->drawRect(recta);
324 painter->drawLine(rect().x() + 1, rect().y(), rect().x() + 1, rect().y() + rect().height());
325 painter->drawLine(rect().x() + rect().width(), rect().y(), rect().x() + rect().width(), rect().y() + rect().height());
326 painter->setPen(QPen(Qt::black, 1.0));
327 painter->drawLine(rect().x(), rect().y(), rect().x() + rect().width(), rect().y());
328 painter->drawLine(rect().x(), rect().y() + rect().height(), rect().x() + rect().width(), rect().y() + rect().height());*/
330 //QGraphicsRectItem::paint(painter, option, widget);
331 //QPen pen(Qt::green, 1.0 / size.x() + 0.5);
332 //painter->setPen(pen);
333 //painter->drawLine(rect().x(), rect().y(), rect().x() + rect().width(), rect().y());
334 //kDebug()<<"ITEM REPAINT RECT: "<<boundingRect().width();
335 //painter->drawText(rect(), Qt::AlignCenter, m_name);
336 // painter->drawRect(boundingRect());
337 //painter->drawRoundRect(-10, -10, 20, 20);
341 OPERATIONTYPE ClipItem::operationMode(QPointF pos, double scale)
343 if (abs(pos.x() - (rect().x() + scale * m_startFade)) < 6 && abs(pos.y() - rect().y()) < 6) return FADEIN;
344 else if (abs(pos.x() - rect().x()) < 6) return RESIZESTART;
345 else if (abs(pos.x() - (rect().x() + rect().width() - scale * m_endFade)) < 6 && abs(pos.y() - rect().y()) < 6) return FADEOUT;
346 else if (abs(pos.x() - (rect().x() + rect().width())) < 6) return RESIZEEND;
350 int ClipItem::fadeIn() const
355 int ClipItem::fadeOut() const
360 void ClipItem::setFadeIn(int pos, double scale)
362 int oldIn = m_startFade;
363 if (pos < 0) pos = 0;
364 if (pos > m_cropDuration) pos = m_cropDuration / 2;
366 if (oldIn > pos) update(rect().x(), rect().y(), oldIn * scale, rect().height());
367 else update(rect().x(), rect().y(), pos * scale, rect().height());
370 void ClipItem::setFadeOut(int pos, double scale)
372 int oldOut = m_endFade;
373 if (pos < 0) pos = 0;
374 if (pos > m_cropDuration) pos = m_cropDuration / 2;
376 if (oldOut > pos) update(rect().x() + rect().width() - pos * scale, rect().y(), pos * scale, rect().height());
377 else update(rect().x() + rect().width() - oldOut * scale, rect().y(), oldOut * scale, rect().height());
383 void ClipItem::mousePressEvent ( QGraphicsSceneMouseEvent * event )
385 /*m_resizeMode = operationMode(event->pos());
386 if (m_resizeMode == MOVE) {
387 m_maxTrack = scene()->sceneRect().height();
388 m_grabPoint = (int) (event->pos().x() - rect().x());
390 QGraphicsRectItem::mousePressEvent(event);
394 void ClipItem::mouseReleaseEvent ( QGraphicsSceneMouseEvent * event )
397 QGraphicsRectItem::mouseReleaseEvent(event);
400 void ClipItem::moveTo(int x, double scale, double offset, int newTrack)
402 double origX = rect().x();
403 double origY = rect().y();
406 setRect(x * scale, origY + offset, rect().width(), rect().height());
407 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
408 if (collisionList.size() == 0) m_track = newTrack;
409 for (int i = 0; i < collisionList.size(); ++i) {
410 QGraphicsItem *item = collisionList.at(i);
411 if (item->type() == 70000)
415 QRectF other = ((QGraphicsRectItem *)item)->rect();
416 if (x < m_startPos) {
417 kDebug()<<"COLLISION, MOVING TO------";
418 m_startPos = ((ClipItem *)item)->endPos() + 1;
419 origX = m_startPos * scale;
422 kDebug()<<"COLLISION, MOVING TO+++";
423 m_startPos = ((ClipItem *)item)->startPos() - m_cropDuration;
424 origX = m_startPos * scale;
427 setRect(origX, origY, rect().width(), rect().height());
438 /* QList <QGraphicsItem *> childrenList = QGraphicsItem::children();
439 for (int i = 0; i < childrenList.size(); ++i) {
440 childrenList.at(i)->moveBy(rect().x() - origX , offset);
444 void ClipItem::resizeStart(int posx, double scale)
446 int durationDiff = posx - m_startPos;
447 if (durationDiff == 0) return;
448 kDebug()<<"-- RESCALE: CROP="<<m_cropStart<<", DIFF = "<<durationDiff;
449 if (m_cropStart + durationDiff < 0) {
450 durationDiff = -m_cropStart;
452 else if (durationDiff >= m_cropDuration) {
453 durationDiff = m_cropDuration - 3;
455 m_startPos += durationDiff;
456 m_cropStart += durationDiff;
457 m_cropDuration -= durationDiff;
458 setRect(m_startPos * scale, rect().y(), m_cropDuration * scale, rect().height());
459 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
460 for (int i = 0; i < collisionList.size(); ++i) {
461 QGraphicsItem *item = collisionList.at(i);
462 if (item->type() == 70000)
464 int diff = ((ClipItem *)item)->endPos() + 1 - m_startPos;
465 setRect((m_startPos + diff) * scale, rect().y(), (m_cropDuration - diff) * scale, rect().height());
468 m_cropDuration -= diff;
472 if (m_hasThumbs) startThumbTimer->start(100);
475 void ClipItem::resizeEnd(int posx, double scale)
477 int durationDiff = posx - endPos();
478 if (durationDiff == 0) return;
479 kDebug()<<"-- RESCALE: CROP="<<m_cropStart<<", DIFF = "<<durationDiff;
480 if (m_cropDuration + durationDiff <= 0) {
481 durationDiff = - (m_cropDuration - 3);
483 else if (m_cropDuration + durationDiff >= m_maxDuration) {
484 durationDiff = m_maxDuration - m_cropDuration;
486 m_cropDuration += durationDiff;
487 setRect(m_startPos * scale, rect().y(), m_cropDuration * scale, rect().height());
488 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
489 for (int i = 0; i < collisionList.size(); ++i) {
490 QGraphicsItem *item = collisionList.at(i);
491 if (item->type() == 70000)
493 int diff = ((ClipItem *)item)->startPos() - 1 - startPos();
494 m_cropDuration = diff;
495 setRect(m_startPos * scale, rect().y(), m_cropDuration * scale, rect().height());
499 if (m_hasThumbs) endThumbTimer->start(100);
503 void ClipItem::mouseMoveEvent ( QGraphicsSceneMouseEvent * event )
507 int ClipItem::track()
512 void ClipItem::setTrack(int track)
517 int ClipItem::effectsCounter()
519 return m_effectsCounter++;
522 int ClipItem::effectsCount()
524 return m_effectList.size();
527 QStringList ClipItem::effectNames()
529 return m_effectList.effectNames();
532 QDomElement ClipItem::effectAt(int ix)
534 return m_effectList.at(ix);
537 void ClipItem::setEffectAt(int ix, QDomElement effect)
539 kDebug()<<"CHange EFFECT AT: "<<ix<<", CURR: "<<m_effectList.at(ix).attribute("tag")<<", NEW: "<<effect.attribute("tag");
540 m_effectList.insert(ix, effect);
541 m_effectList.removeAt(ix + 1);
542 update(boundingRect());
545 QMap <QString, QString> ClipItem::addEffect(QDomElement effect)
547 QMap <QString, QString> effectParams;
548 m_effectList.append(effect);
549 effectParams["tag"] = effect.attribute("tag");
550 effectParams["kdenlive_ix"] = effect.attribute("kdenlive_ix");
551 QDomNodeList params = effect.elementsByTagName("parameter");
552 for (int i = 0; i < params.count(); i++) {
553 QDomElement e = params.item(i).toElement();
555 effectParams[e.attribute("name")] = e.attribute("value");
557 update(boundingRect());
561 QMap <QString, QString> ClipItem::getEffectArgs(QDomElement effect)
563 QMap <QString, QString> effectParams;
564 effectParams["tag"] = effect.attribute("tag");
565 effectParams["kdenlive_ix"] = effect.attribute("kdenlive_ix");
566 QDomNodeList params = effect.elementsByTagName("parameter");
567 for (int i = 0; i < params.count(); i++) {
568 QDomElement e = params.item(i).toElement();
570 effectParams[e.attribute("name")] = e.attribute("value");
575 void ClipItem::deleteEffect(QString index)
577 for (int i = 0; i < m_effectList.size(); ++i) {
578 if (m_effectList.at(i).attribute("kdenlive_ix") == index) {
579 m_effectList.removeAt(i);
583 update(boundingRect());
589 void CustomTrackView::mousePressEvent ( QMouseEvent * event )
591 int pos = event->x();
592 if (event->modifiers() == Qt::ControlModifier)
593 setDragMode(QGraphicsView::ScrollHandDrag);
594 else if (event->modifiers() == Qt::ShiftModifier)
595 setDragMode(QGraphicsView::RubberBandDrag);
597 QGraphicsItem * item = itemAt(event->pos());
600 else emit cursorMoved((int) mapToScene(event->x(), 0).x());
603 QGraphicsView::mousePressEvent(event);
606 void CustomTrackView::mouseReleaseEvent ( QMouseEvent * event )
608 QGraphicsView::mouseReleaseEvent(event);
609 setDragMode(QGraphicsView::NoDrag);
613 #include "clipitem.moc"