]> git.sesse.net Git - kdenlive/blob - src/clipitem.cpp
e901c8f225a066efc6b2f966cf73292d6d637944
[kdenlive] / src / clipitem.cpp
1 /***************************************************************************
2  *   Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
3  *                                                                         *
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.                                   *
8  *                                                                         *
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.                          *
13  *                                                                         *
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  ***************************************************************************/
19
20
21
22 #include <QPainter>
23 #include <QTimer>
24 #include <QStyleOptionGraphicsItem>
25 #include <QGraphicsScene>
26
27
28 #include <KDebug>
29
30 #include <mlt++/Mlt.h>
31
32 #include "clipitem.h"
33 #include "renderer.h"
34 #include "kdenlivesettings.h"
35
36 ClipItem::ClipItem(QDomElement xml, int track, int startpos, const QRectF & rect, int duration)
37     : QGraphicsRectItem(rect), m_xml(xml), m_resizeMode(NONE), m_grabPoint(0), m_maxTrack(0), m_track(track), m_startPos(startpos), m_thumbProd(NULL), startThumbTimer(NULL), endThumbTimer(NULL)
38 {
39   //setToolTip(name);
40
41   m_clipName = xml.attribute("name");
42   if (m_clipName.isEmpty()) m_clipName = KUrl(xml.attribute("resource")).fileName();
43
44   m_producer = xml.attribute("id").toInt();
45
46   m_clipType = (CLIPTYPE) xml.attribute("type").toInt();
47
48   m_cropStart = xml.attribute("in", 0).toInt();
49   m_maxDuration = xml.attribute("duration", 0).toInt();
50   if (m_maxDuration == 0) m_maxDuration = xml.attribute("out", 0).toInt() - m_cropStart;
51
52   if (duration != -1) m_cropDuration = duration;
53   else m_cropDuration = m_maxDuration;
54
55   setFlags(QGraphicsItem::ItemClipsToShape | QGraphicsItem::ItemClipsChildrenToShape | QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
56
57   setBrush(QColor(100, 100, 150));
58   if (m_clipType == VIDEO || m_clipType == AV) {
59     m_thumbProd = new KThumb(KUrl(xml.attribute("resource")));
60     connect(this, SIGNAL(getThumb(int, int, int, int)), m_thumbProd, SLOT(extractImage(int, int, int, int)));
61     connect(m_thumbProd, SIGNAL(thumbReady(int, QPixmap)), this, SLOT(slotThumbReady(int, QPixmap)));
62     QTimer::singleShot(300, this, SLOT(slotFetchThumbs()));
63
64     startThumbTimer = new QTimer(this);
65     startThumbTimer->setSingleShot(true);
66     connect(startThumbTimer, SIGNAL(timeout()), this, SLOT(slotGetStartThumb()));
67     endThumbTimer = new QTimer(this);
68     endThumbTimer->setSingleShot(true);
69     connect(endThumbTimer, SIGNAL(timeout()), this, SLOT(slotGetEndThumb()));
70
71   }
72   else if (m_clipType == COLOR) {
73     QString colour = xml.attribute("colour");
74     colour = colour.replace(0, 2, "#");
75     setBrush(QColor(colour.left(7)));
76   }
77   //m_startPix.load("/home/one/Desktop/thumb.jpg");
78 }
79
80 ClipItem::~ClipItem()
81 {
82   if (startThumbTimer) delete startThumbTimer;
83   if (endThumbTimer) delete endThumbTimer;
84   if (m_thumbProd) delete m_thumbProd;
85 }
86
87 void ClipItem::slotFetchThumbs()
88 {
89   emit getThumb(m_cropStart, m_cropStart + m_cropDuration, 50 * KdenliveSettings::project_display_ratio(), 50);
90 }
91
92 void ClipItem::slotGetStartThumb()
93 {
94   emit getThumb(m_cropStart, -1, 50 * KdenliveSettings::project_display_ratio(), 50);
95 }
96
97 void ClipItem::slotGetEndThumb()
98 {
99   emit getThumb(-1, m_cropStart + m_cropDuration, 50 * KdenliveSettings::project_display_ratio(), 50);
100 }
101
102 void ClipItem::slotThumbReady(int frame, QPixmap pix)
103 {
104   if (frame == m_cropStart) m_startPix = pix;
105   else m_endPix = pix;
106   update();
107 }
108
109 int ClipItem::type () const
110 {
111   return 70000;
112 }
113
114 QDomElement ClipItem::xml() const
115 {
116   return m_xml;
117 }
118
119 int ClipItem::clipType()
120 {
121   return m_clipType;
122 }
123
124 QString ClipItem::clipName()
125 {
126   return m_clipName;
127 }
128
129 int ClipItem::clipProducer()
130 {
131   return m_producer;
132 }
133
134 int ClipItem::maxDuration()
135 {
136   return m_maxDuration;
137 }
138
139 int ClipItem::duration()
140 {
141   return m_cropDuration;
142 }
143
144 int ClipItem::startPos()
145 {
146   return m_startPos;
147 }
148
149 int ClipItem::endPos()
150 {
151   return m_startPos + m_cropDuration;
152 }
153
154 // virtual 
155  void ClipItem::paint(QPainter *painter,
156                            const QStyleOptionGraphicsItem *option,
157                            QWidget *widget)
158  {
159     QRectF br = rect();
160     painter->setRenderHints(QPainter::Antialiasing);
161     QPainterPath roundRectPath;
162     double roundingY = 20;
163     double roundingX = 20;
164     double offset = 1;
165     painter->setClipRect(option->exposedRect);
166     if (roundingX > br.width() / 2) roundingX = br.width() / 2;
167     //kDebug()<<"-----PAINTING, SCAL: "<<scale<<", height: "<<br.height();
168     roundRectPath.moveTo(br.x() + br .width() - offset, br.y() + roundingY);
169     roundRectPath.arcTo(br.x() + br .width() - roundingX - offset, br.y(), roundingX, roundingY, 0.0, 90.0);
170     roundRectPath.lineTo(br.x() + roundingX, br.y());
171     roundRectPath.arcTo(br.x() + offset, br.y(), roundingX, roundingY, 90.0, 90.0);
172     roundRectPath.lineTo(br.x() + offset, br.y() + br.height() - roundingY);
173     roundRectPath.arcTo(br.x() + offset, br.y() + br.height() - roundingY - offset, roundingX, roundingY, 180.0, 90.0);
174     roundRectPath.lineTo(br.x() + br .width() - roundingX, br.y() + br.height() - offset);
175     roundRectPath.arcTo(br.x() + br .width() - roundingX - offset, br.y() + br.height() - roundingY - offset, roundingX, roundingY, 270.0, 90.0);
176     roundRectPath.closeSubpath();
177     painter->setClipPath(roundRectPath, Qt::IntersectClip);
178     //painter->fillPath(roundRectPath, brush()); //, QBrush(QColor(Qt::red)));
179     painter->fillRect(br, brush());
180     //painter->fillRect(QRectF(br.x() + br.width() - m_endPix.width(), br.y(), m_endPix.width(), br.height()), QBrush(QColor(Qt::black)));
181     if (!m_startPix.isNull()) {
182       painter->drawPixmap(QPointF(br.x() + br.width() - m_endPix.width(), br.y()), m_endPix);
183       QLineF l(br.x() + br.width() - m_endPix.width(), br.y(), br.x() + br.width() - m_endPix.width(), br.y() + br.height());
184       painter->drawLine(l);
185
186       painter->drawPixmap(QPointF(br.x(), br.y()), m_startPix);
187       QLineF l2(br.x() + m_startPix.width(), br.y(), br.x() + m_startPix.width(), br.y() + br.height());
188       painter->drawLine(l2);
189     }
190     painter->setClipRect(option->exposedRect);
191     QPen pen = painter->pen();
192     pen.setColor(Qt::red);
193     pen.setStyle(Qt::DashDotDotLine); //Qt::DotLine);
194     
195     QRectF txtBounding = painter->boundingRect(br, Qt::AlignCenter, " " + m_clipName + " ");
196     painter->fillRect(txtBounding, QBrush(QColor(255,255,255,150)));
197     painter->drawText(txtBounding, Qt::AlignCenter, m_clipName);
198
199     if (isSelected()) painter->setPen(pen);
200     painter->drawPath(roundRectPath);
201     //painter->fillRect(QRect(br.x(), br.y(), roundingX, roundingY), QBrush(QColor(Qt::green)));
202
203     /*QRectF recta(rect().x(), rect().y(), scale,rect().height());
204     painter->drawRect(recta);
205     painter->drawLine(rect().x() + 1, rect().y(), rect().x() + 1, rect().y() + rect().height());
206     painter->drawLine(rect().x() + rect().width(), rect().y(), rect().x() + rect().width(), rect().y() + rect().height());
207     painter->setPen(QPen(Qt::black, 1.0));
208     painter->drawLine(rect().x(), rect().y(), rect().x() + rect().width(), rect().y());
209     painter->drawLine(rect().x(), rect().y() + rect().height(), rect().x() + rect().width(), rect().y() + rect().height());*/
210
211     //QGraphicsRectItem::paint(painter, option, widget);
212     //QPen pen(Qt::green, 1.0 / size.x() + 0.5);
213     //painter->setPen(pen);
214     //painter->drawLine(rect().x(), rect().y(), rect().x() + rect().width(), rect().y());
215     //kDebug()<<"ITEM REPAINT RECT: "<<boundingRect().width();
216     //painter->drawText(rect(), Qt::AlignCenter, m_name);
217     // painter->drawRect(boundingRect());
218      //painter->drawRoundRect(-10, -10, 20, 20);
219  }
220
221
222 OPERATIONTYPE ClipItem::operationMode(QPointF pos)
223 {
224     if (abs(pos.x() - rect().x()) < 6) {
225       if (abs(pos.y() - rect().y()) < 6) return FADEIN;
226       return RESIZESTART;
227     }
228     else if (abs(pos.x() - (rect().x() + rect().width())) < 6) {
229       if (abs(pos.y() - rect().y()) < 6) return FADEOUT;
230       return RESIZEEND;
231     }
232     return MOVE;
233 }
234
235
236 // virtual
237  void ClipItem::mousePressEvent ( QGraphicsSceneMouseEvent * event ) 
238  {
239     m_resizeMode = operationMode(event->pos());
240     if (m_resizeMode == MOVE) {
241       m_maxTrack = scene()->sceneRect().height();
242       m_grabPoint = (int) (event->pos().x() - rect().x());
243     }
244     QGraphicsRectItem::mousePressEvent(event);
245  }
246
247 // virtual
248  void ClipItem::mouseReleaseEvent ( QGraphicsSceneMouseEvent * event ) 
249  {
250     m_resizeMode = NONE;
251     QGraphicsRectItem::mouseReleaseEvent(event);
252  }
253
254  void ClipItem::moveTo(int x, double scale, double offset, int newTrack)
255  {
256   double origX = rect().x();
257   double origY = rect().y();
258   bool success = true;
259   setRect(x * scale, origY + offset, rect().width(), rect().height());
260   QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
261   if (collisionList.size() == 0) m_track = newTrack;
262   for (int i = 0; i < collisionList.size(); ++i) {
263     QGraphicsItem *item = collisionList.at(i);
264     if (item->type() == 70000)
265     {
266         if (offset == 0)
267         {
268           QRectF other = ((QGraphicsRectItem *)item)->rect();
269           if (x < m_startPos) {
270             kDebug()<<"COLLISION, MOVING TO------";
271             m_startPos = ((ClipItem *)item)->endPos() + 1;
272             origX = m_startPos * scale; 
273           }
274           else {
275             kDebug()<<"COLLISION, MOVING TO+++";
276             m_startPos = ((ClipItem *)item)->startPos() - m_cropDuration;
277             origX = m_startPos * scale; 
278           }
279         }
280         setRect(origX, origY, rect().width(), rect().height());
281         offset = 0;
282         origX = rect().x();
283         success = false;
284         break;
285       }
286     }
287     if (success) {
288         m_track = newTrack;
289         m_startPos = x;
290     }
291 /*    QList <QGraphicsItem *> childrenList = QGraphicsItem::children();
292     for (int i = 0; i < childrenList.size(); ++i) {
293       childrenList.at(i)->moveBy(rect().x() - origX , offset);
294     }*/
295  }
296
297 void ClipItem::resizeStart(int posx, double scale)
298 {
299     int durationDiff = posx - m_startPos;
300     if (durationDiff == 0) return;
301     kDebug()<<"-- RESCALE: CROP="<<m_cropStart<<", DIFF = "<<durationDiff;
302     if (m_cropStart + durationDiff < 0) {
303       durationDiff = -m_cropStart;
304     }
305     else if (durationDiff >= m_cropDuration) {
306       durationDiff = m_cropDuration - 3;
307     }
308     m_startPos += durationDiff;
309     m_cropStart += durationDiff;
310     m_cropDuration -= durationDiff;
311     setRect(m_startPos * scale, rect().y(), m_cropDuration * scale, rect().height());
312     QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
313     for (int i = 0; i < collisionList.size(); ++i) {
314       QGraphicsItem *item = collisionList.at(i);
315       if (item->type() == 70000)
316       {
317         int diff = ((ClipItem *)item)->endPos() + 1 - m_startPos;
318         setRect((m_startPos + diff) * scale, rect().y(), (m_cropDuration - diff) * scale, rect().height());
319         m_startPos += diff;
320         m_cropStart += diff;
321         m_cropDuration -= diff;
322         break;
323       }
324     }
325     if (m_thumbProd) startThumbTimer->start(100);
326 }
327
328 void ClipItem::resizeEnd(int posx, double scale)
329 {
330     int durationDiff = posx - endPos();
331     if (durationDiff == 0) return;
332     kDebug()<<"-- RESCALE: CROP="<<m_cropStart<<", DIFF = "<<durationDiff;
333     if (m_cropDuration + durationDiff <= 0) {
334       durationDiff = - (m_cropDuration - 3);
335     }
336     else if (m_cropDuration + durationDiff >= m_maxDuration) {
337       durationDiff = m_maxDuration - m_cropDuration;
338     }
339     m_cropDuration += durationDiff;
340     setRect(m_startPos * scale, rect().y(), m_cropDuration * scale, rect().height());
341     QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
342     for (int i = 0; i < collisionList.size(); ++i) {
343       QGraphicsItem *item = collisionList.at(i);
344       if (item->type() == 70000)
345       {
346         int diff = ((ClipItem *)item)->startPos() - 1 - startPos();
347         m_cropDuration = diff;
348         setRect(m_startPos * scale, rect().y(), m_cropDuration * scale, rect().height());
349         break;
350       }
351     }
352     if (m_thumbProd) endThumbTimer->start(100);
353 }
354
355 // virtual
356  void ClipItem::mouseMoveEvent ( QGraphicsSceneMouseEvent * event ) 
357  {
358  }
359
360 int ClipItem::track()
361 {
362   return  m_track;
363 }
364
365 void ClipItem::setTrack(int track)
366 {
367   m_track = track;
368 }
369
370
371 // virtual 
372 /*
373 void CustomTrackView::mousePressEvent ( QMouseEvent * event )
374 {
375   int pos = event->x();
376   if (event->modifiers() == Qt::ControlModifier) 
377     setDragMode(QGraphicsView::ScrollHandDrag);
378   else if (event->modifiers() == Qt::ShiftModifier) 
379     setDragMode(QGraphicsView::RubberBandDrag);
380   else {
381     QGraphicsItem * item = itemAt(event->pos());
382     if (item) {
383     }
384     else emit cursorMoved((int) mapToScene(event->x(), 0).x());
385   }
386   kDebug()<<pos;
387   QGraphicsView::mousePressEvent(event);
388 }
389
390 void CustomTrackView::mouseReleaseEvent ( QMouseEvent * event )
391 {
392   QGraphicsView::mouseReleaseEvent(event);
393   setDragMode(QGraphicsView::NoDrag);
394 }
395 */
396
397 #include "clipitem.moc"