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 connect(clip, SIGNAL (gotAudioData()), this, SLOT (slotGotAudioData()));
67 QTimer::singleShot(300, this, SLOT(slotFetchThumbs()));
69 startThumbTimer = new QTimer(this);
70 startThumbTimer->setSingleShot(true);
71 connect(startThumbTimer, SIGNAL(timeout()), this, SLOT(slotGetStartThumb()));
72 endThumbTimer = new QTimer(this);
73 endThumbTimer->setSingleShot(true);
74 connect(endThumbTimer, SIGNAL(timeout()), this, SLOT(slotGetEndThumb()));
77 else if (m_clipType == COLOR) {
78 QString colour = m_xml.attribute("colour");
79 colour = colour.replace(0, 2, "#");
80 setBrush(QColor(colour.left(7)));
82 else if (m_clipType == IMAGE) {
83 m_startPix = KThumb::getImage(KUrl(m_xml.attribute("resource")), 50 * KdenliveSettings::project_display_ratio(), 50);
90 if (startThumbTimer) delete startThumbTimer;
91 if (endThumbTimer) delete endThumbTimer;
94 void ClipItem::slotFetchThumbs()
96 emit getThumb(m_cropStart, m_cropStart + m_cropDuration);
99 void ClipItem::slotGetStartThumb()
101 emit getThumb(m_cropStart, -1);
104 void ClipItem::slotGetEndThumb()
106 emit getThumb(-1, m_cropStart + m_cropDuration);
109 void ClipItem::slotThumbReady(int frame, QPixmap pix)
111 if (frame == m_cropStart) m_startPix = pix;
116 void ClipItem::slotGotAudioData(){
120 int ClipItem::type () const
125 DocClipBase *ClipItem::baseClip()
130 QDomElement ClipItem::xml() const
135 int ClipItem::clipType()
140 QString ClipItem::clipName()
145 int ClipItem::clipProducer()
150 int ClipItem::maxDuration()
152 return m_maxDuration;
155 int ClipItem::duration()
157 return m_cropDuration;
160 int ClipItem::startPos()
165 int ClipItem::cropStart()
170 int ClipItem::endPos()
172 return m_startPos + m_cropDuration;
176 void ClipItem::paint(QPainter *painter,
177 const QStyleOptionGraphicsItem *option,
181 painter->setRenderHints(QPainter::Antialiasing);
182 QPainterPath roundRectPathUpper,roundRectPathLower;
183 double roundingY = 20;
184 double roundingX = 20;
186 painter->setClipRect(option->exposedRect);
187 if (roundingX > br.width() / 2) roundingX = br.width() / 2;
188 //kDebug()<<"-----PAINTING, SCAL: "<<scale<<", height: "<<br.height();
189 roundRectPathUpper.moveTo(br.x() + br .width() - offset, br.y() + br.height()/2 - offset);
190 roundRectPathUpper.arcTo(br.x() + br .width() - roundingX - offset, br.y(), roundingX, roundingY, 0.0, 90.0);
191 roundRectPathUpper.lineTo(br.x() + roundingX, br.y());
192 roundRectPathUpper.arcTo(br.x() + offset, br.y(), roundingX, roundingY, 90.0, 90.0);
193 roundRectPathUpper.lineTo(br.x() + offset, br.y() + br.height()/2 - offset);
194 roundRectPathUpper.closeSubpath();
196 roundRectPathLower.moveTo(br.x() + offset, br.y() + br.height()/2 - offset);
197 roundRectPathLower.arcTo(br.x() + offset, br.y() + br.height() - roundingY - offset, roundingX, roundingY, 180.0, 90.0);
198 roundRectPathLower.lineTo(br.x() + br .width() - roundingX, br.y() + br.height() - offset);
199 roundRectPathLower.arcTo(br.x() + br .width() - roundingX - offset, br.y() + br.height() - roundingY - offset, roundingX, roundingY, 270.0, 90.0);
200 roundRectPathLower.lineTo(br.x() + br .width() - offset, br.y()+ br.height()/2 - offset);
201 roundRectPathLower.closeSubpath();
203 painter->setClipPath(roundRectPathUpper.united(roundRectPathLower), Qt::IntersectClip);
204 //painter->fillPath(roundRectPath, brush()); //, QBrush(QColor(Qt::red)));
205 painter->fillRect(br, brush());
206 //painter->fillRect(QRectF(br.x() + br.width() - m_endPix.width(), br.y(), m_endPix.width(), br.height()), QBrush(QColor(Qt::black)));
209 if (!m_startPix.isNull()) {
210 if (m_clipType == IMAGE) {
211 painter->drawPixmap(QPointF(br.x() + br.width() - m_startPix.width(), br.y()), m_startPix);
212 QLineF l(br.x() + br.width() - m_startPix.width(), br.y(), br.x() + br.width() - m_startPix.width(), br.y() + br.height());
213 painter->drawLine(l);
215 painter->drawPixmap(QPointF(br.x() + br.width() - m_endPix.width(), br.y()), m_endPix);
216 QLineF l(br.x() + br.width() - m_endPix.width(), br.y(), br.x() + br.width() - m_endPix.width(), br.y() + br.height());
217 painter->drawLine(l);
220 painter->drawPixmap(QPointF(br.x(), br.y()), m_startPix);
221 QLineF l2(br.x() + m_startPix.width(), br.y(), br.x() + m_startPix.width(), br.y() + br.height());
222 painter->drawLine(l2);
224 if (m_clipType == AV || m_clipType==AUDIO ){
225 QPainterPath path= m_clipType==AV ? roundRectPathLower : roundRectPathUpper.united(roundRectPathLower);
226 painter->fillPath(path,QBrush(QColor(200,200,200,127)));
228 double pixelForOneFrame=(double)br.width()/duration();
231 /*for (int frame=m_cropStart;frame<m_cropStart+m_cropDuration && frame< baseClip()->audioFrameChache.size();frame++){
233 QMap<int,QByteArray> frame_channel_data=baseClip()->audioFrameChache[frame];
234 int audio_frame_samples=frame_channel_data[0].size();
236 for (int samples=0;samples<audio_frame_samples;samples++){
238 for (int channel=0;channel<channels && frame_channel_data[channel].size()> 0;channel++){
239 int y=re.y()+re.height()*channel/channels+ (re.height()/channels)/2;
240 int x1=re.x() + ((double)frame*pixelForOneFrame*audio_frame_samples+samples)/pixelForOneFrame;
241 kDebug() <<" frame " << frame << " " << x1 ;
242 painter->drawLine(x1 , y+frame_channel_data[channel][0],x1+1, y+frame_channel_data[channel][0] );
247 audiopen.setWidth(0);
248 painter->setPen(audiopen);
249 QRectF re=path.boundingRect();
251 QMap<int,QPainterPath > channelPaths;
252 QMap<int,QPainterPath > negativeChannelPaths;
253 for (int i=0;i<channels;i++){
254 channelPaths[i].moveTo(re.x(),re.y()+re.height()*i/channels+ (re.height()/channels)/2);
255 negativeChannelPaths[i].moveTo(re.x(),re.y()+re.height()*i/channels+ (re.height()/channels)/2);
258 for (int samples=re.x();samples<re.x()+re.width();samples++){
259 double frame=(double)(samples-re.x())/pixelForOneFrame;
260 int sample=(frame-(int)(frame))*20 ;// AUDIO_FRAME_SIZE
261 if (frame<0 || sample< 0 || sample>19 )
263 QMap<int,QByteArray> frame_channel_data=baseClip()->audioFrameChache[(int)frame];
265 for (int channel=0;channel<channels && frame_channel_data[channel].size()> 0;channel++){
267 int y=re.y()+re.height()*channel/channels+ (re.height()/channels)/2;
268 channelPaths[channel].lineTo(samples,y+( (int)frame_channel_data[channel][sample] -127/2 ) * (re.height()/channels) / 64 );
269 negativeChannelPaths[channel].lineTo(samples,y-( (int)frame_channel_data[channel][sample] -127/2 ) * (re.height()/channels) / 64 );
272 for (int i=0;i<channels;i++){
273 //painter->drawPath(channelPaths[i].united(negativeChannelPaths[i]));//or singleif looks better
274 painter->drawPath(channelPaths[i]);
279 // draw start / end fades
280 double scale = br.width() / m_cropDuration;
283 fades = QBrush(QColor(200, 50, 50, 150));
285 else fades = QBrush(QColor(200, 200, 200, 200));
287 if (m_startFade != 0) {
288 QPainterPath fadeInPath;
289 fadeInPath.moveTo(br.x() - offset, br.y());
290 fadeInPath.lineTo(br.x() - offset, br.y() + br.height());
291 fadeInPath.lineTo(br.x() + m_startFade * scale, br.y());
292 fadeInPath.closeSubpath();
293 painter->fillPath(fadeInPath, fades);
295 QLineF l(br.x() + m_startFade * scale, br.y(), br.x(), br.y() + br.height());
296 painter->drawLine(l);
299 if (m_endFade != 0) {
300 QPainterPath fadeOutPath;
301 fadeOutPath.moveTo(br.x() + br.width(), br.y());
302 fadeOutPath.lineTo(br.x() + br.width(), br.y() + br.height());
303 fadeOutPath.lineTo(br.x() + br.width() - m_endFade * scale, br.y());
304 fadeOutPath.closeSubpath();
305 painter->fillPath(fadeOutPath, fades);
307 QLineF l(br.x() + br.width() - m_endFade * scale, br.y(), br.x() + br.width(), br.y() + br.height());
308 painter->drawLine(l);
312 QPen pen = painter->pen();
313 pen.setColor(Qt::white);
314 //pen.setStyle(Qt::DashDotDotLine); //Qt::DotLine);
316 QString effects = effectNames().join(" / ");
317 if (!effects.isEmpty()) {
318 painter->setPen(pen);
319 QFont font = painter->font();
320 QFont smallFont = font;
321 smallFont.setPointSize(8);
322 painter->setFont(smallFont);
323 QRectF txtBounding = painter->boundingRect(br, Qt::AlignLeft | Qt::AlignTop, " " + effects + " ");
324 painter->fillRect(txtBounding, QBrush(QColor(0,0,0,150)));
325 painter->drawText(txtBounding, Qt::AlignCenter, effects);
326 pen.setColor(Qt::black);
327 painter->setPen(pen);
328 painter->setFont(font);
331 QRectF txtBounding = painter->boundingRect(br, Qt::AlignCenter, " " + m_clipName + " ");
332 painter->fillRect(txtBounding, QBrush(QColor(255,255,255,150)));
333 painter->drawText(txtBounding, Qt::AlignCenter, m_clipName);
335 pen.setColor(Qt::red);
336 pen.setStyle(Qt::DashDotDotLine); //Qt::DotLine);
337 if (isSelected()) painter->setPen(pen);
338 painter->setClipRect(option->exposedRect);
339 painter->drawPath(roundRectPathUpper.united(roundRectPathLower));
340 //painter->fillRect(QRect(br.x(), br.y(), roundingX, roundingY), QBrush(QColor(Qt::green)));
342 /*QRectF recta(rect().x(), rect().y(), scale,rect().height());
343 painter->drawRect(recta);
344 painter->drawLine(rect().x() + 1, rect().y(), rect().x() + 1, rect().y() + rect().height());
345 painter->drawLine(rect().x() + rect().width(), rect().y(), rect().x() + rect().width(), rect().y() + rect().height());
346 painter->setPen(QPen(Qt::black, 1.0));
347 painter->drawLine(rect().x(), rect().y(), rect().x() + rect().width(), rect().y());
348 painter->drawLine(rect().x(), rect().y() + rect().height(), rect().x() + rect().width(), rect().y() + rect().height());*/
350 //QGraphicsRectItem::paint(painter, option, widget);
351 //QPen pen(Qt::green, 1.0 / size.x() + 0.5);
352 //painter->setPen(pen);
353 //painter->drawLine(rect().x(), rect().y(), rect().x() + rect().width(), rect().y());
354 //kDebug()<<"ITEM REPAINT RECT: "<<boundingRect().width();
355 //painter->drawText(rect(), Qt::AlignCenter, m_name);
356 // painter->drawRect(boundingRect());
357 //painter->drawRoundRect(-10, -10, 20, 20);
361 OPERATIONTYPE ClipItem::operationMode(QPointF pos, double scale)
363 if (abs(pos.x() - (rect().x() + scale * m_startFade)) < 6 && abs(pos.y() - rect().y()) < 6) return FADEIN;
364 else if (abs(pos.x() - rect().x()) < 6) return RESIZESTART;
365 else if (abs(pos.x() - (rect().x() + rect().width() - scale * m_endFade)) < 6 && abs(pos.y() - rect().y()) < 6) return FADEOUT;
366 else if (abs(pos.x() - (rect().x() + rect().width())) < 6) return RESIZEEND;
370 int ClipItem::fadeIn() const
375 int ClipItem::fadeOut() const
380 void ClipItem::setFadeIn(int pos, double scale)
382 int oldIn = m_startFade;
383 if (pos < 0) pos = 0;
384 if (pos > m_cropDuration) pos = m_cropDuration / 2;
386 if (oldIn > pos) update(rect().x(), rect().y(), oldIn * scale, rect().height());
387 else update(rect().x(), rect().y(), pos * scale, rect().height());
390 void ClipItem::setFadeOut(int pos, double scale)
392 int oldOut = m_endFade;
393 if (pos < 0) pos = 0;
394 if (pos > m_cropDuration) pos = m_cropDuration / 2;
396 if (oldOut > pos) update(rect().x() + rect().width() - pos * scale, rect().y(), pos * scale, rect().height());
397 else update(rect().x() + rect().width() - oldOut * scale, rect().y(), oldOut * scale, rect().height());
403 void ClipItem::mousePressEvent ( QGraphicsSceneMouseEvent * event )
405 /*m_resizeMode = operationMode(event->pos());
406 if (m_resizeMode == MOVE) {
407 m_maxTrack = scene()->sceneRect().height();
408 m_grabPoint = (int) (event->pos().x() - rect().x());
410 QGraphicsRectItem::mousePressEvent(event);
414 void ClipItem::mouseReleaseEvent ( QGraphicsSceneMouseEvent * event )
417 QGraphicsRectItem::mouseReleaseEvent(event);
420 void ClipItem::moveTo(int x, double scale, double offset, int newTrack)
422 double origX = rect().x();
423 double origY = rect().y();
426 setRect(x * scale, origY + offset, rect().width(), rect().height());
427 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
428 if (collisionList.size() == 0) m_track = newTrack;
429 for (int i = 0; i < collisionList.size(); ++i) {
430 QGraphicsItem *item = collisionList.at(i);
431 if (item->type() == 70000)
435 QRectF other = ((QGraphicsRectItem *)item)->rect();
436 if (x < m_startPos) {
437 kDebug()<<"COLLISION, MOVING TO------";
438 m_startPos = ((ClipItem *)item)->endPos() + 1;
439 origX = m_startPos * scale;
442 kDebug()<<"COLLISION, MOVING TO+++";
443 m_startPos = ((ClipItem *)item)->startPos() - m_cropDuration;
444 origX = m_startPos * scale;
447 setRect(origX, origY, rect().width(), rect().height());
458 /* QList <QGraphicsItem *> childrenList = QGraphicsItem::children();
459 for (int i = 0; i < childrenList.size(); ++i) {
460 childrenList.at(i)->moveBy(rect().x() - origX , offset);
464 void ClipItem::resizeStart(int posx, double scale)
466 int durationDiff = posx - m_startPos;
467 if (durationDiff == 0) return;
468 kDebug()<<"-- RESCALE: CROP="<<m_cropStart<<", DIFF = "<<durationDiff;
469 if (m_cropStart + durationDiff < 0) {
470 durationDiff = -m_cropStart;
472 else if (durationDiff >= m_cropDuration) {
473 durationDiff = m_cropDuration - 3;
475 m_startPos += durationDiff;
476 m_cropStart += durationDiff;
477 m_cropDuration -= durationDiff;
478 setRect(m_startPos * scale, rect().y(), m_cropDuration * scale, rect().height());
479 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
480 for (int i = 0; i < collisionList.size(); ++i) {
481 QGraphicsItem *item = collisionList.at(i);
482 if (item->type() == 70000)
484 int diff = ((ClipItem *)item)->endPos() + 1 - m_startPos;
485 setRect((m_startPos + diff) * scale, rect().y(), (m_cropDuration - diff) * scale, rect().height());
488 m_cropDuration -= diff;
492 if (m_hasThumbs) startThumbTimer->start(100);
495 void ClipItem::resizeEnd(int posx, double scale)
497 int durationDiff = posx - endPos();
498 if (durationDiff == 0) return;
499 kDebug()<<"-- RESCALE: CROP="<<m_cropStart<<", DIFF = "<<durationDiff;
500 if (m_cropDuration + durationDiff <= 0) {
501 durationDiff = - (m_cropDuration - 3);
503 else if (m_cropDuration + durationDiff >= m_maxDuration) {
504 durationDiff = m_maxDuration - m_cropDuration;
506 m_cropDuration += durationDiff;
507 setRect(m_startPos * scale, rect().y(), m_cropDuration * scale, rect().height());
508 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
509 for (int i = 0; i < collisionList.size(); ++i) {
510 QGraphicsItem *item = collisionList.at(i);
511 if (item->type() == 70000)
513 int diff = ((ClipItem *)item)->startPos() - 1 - startPos();
514 m_cropDuration = diff;
515 setRect(m_startPos * scale, rect().y(), m_cropDuration * scale, rect().height());
519 if (m_hasThumbs) endThumbTimer->start(100);
523 void ClipItem::mouseMoveEvent ( QGraphicsSceneMouseEvent * event )
527 int ClipItem::track()
532 void ClipItem::setTrack(int track)
537 int ClipItem::effectsCounter()
539 return m_effectsCounter++;
542 int ClipItem::effectsCount()
544 return m_effectList.size();
547 QStringList ClipItem::effectNames()
549 return m_effectList.effectNames();
552 QDomElement ClipItem::effectAt(int ix)
554 return m_effectList.at(ix);
557 void ClipItem::setEffectAt(int ix, QDomElement effect)
559 kDebug()<<"CHange EFFECT AT: "<<ix<<", CURR: "<<m_effectList.at(ix).attribute("tag")<<", NEW: "<<effect.attribute("tag");
560 m_effectList.insert(ix, effect);
561 m_effectList.removeAt(ix + 1);
562 update(boundingRect());
565 QMap <QString, QString> ClipItem::addEffect(QDomElement effect)
567 QMap <QString, QString> effectParams;
568 m_effectList.append(effect);
569 effectParams["tag"] = effect.attribute("tag");
570 effectParams["kdenlive_ix"] = effect.attribute("kdenlive_ix");
571 QDomNodeList params = effect.elementsByTagName("parameter");
572 for (int i = 0; i < params.count(); i++) {
573 QDomElement e = params.item(i).toElement();
575 effectParams[e.attribute("name")] = e.attribute("value");
577 update(boundingRect());
581 QMap <QString, QString> ClipItem::getEffectArgs(QDomElement effect)
583 QMap <QString, QString> effectParams;
584 effectParams["tag"] = effect.attribute("tag");
585 effectParams["kdenlive_ix"] = effect.attribute("kdenlive_ix");
586 QDomNodeList params = effect.elementsByTagName("parameter");
587 for (int i = 0; i < params.count(); i++) {
588 QDomElement e = params.item(i).toElement();
590 effectParams[e.attribute("name")] = e.attribute("value");
595 void ClipItem::deleteEffect(QString index)
597 for (int i = 0; i < m_effectList.size(); ++i) {
598 if (m_effectList.at(i).attribute("kdenlive_ix") == index) {
599 m_effectList.removeAt(i);
603 update(boundingRect());
609 void CustomTrackView::mousePressEvent ( QMouseEvent * event )
611 int pos = event->x();
612 if (event->modifiers() == Qt::ControlModifier)
613 setDragMode(QGraphicsView::ScrollHandDrag);
614 else if (event->modifiers() == Qt::ShiftModifier)
615 setDragMode(QGraphicsView::RubberBandDrag);
617 QGraphicsItem * item = itemAt(event->pos());
620 else emit cursorMoved((int) mapToScene(event->x(), 0).x());
623 QGraphicsView::mousePressEvent(event);
626 void CustomTrackView::mouseReleaseEvent ( QMouseEvent * event )
628 QGraphicsView::mouseReleaseEvent(event);
629 setDragMode(QGraphicsView::NoDrag);
633 #include "clipitem.moc"