]> git.sesse.net Git - kdenlive/blob - src/clipitem.cpp
progress in timeline widget
[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 <QStyleOptionGraphicsItem>
24 #include <QGraphicsScene>
25 #include <KDebug>
26
27
28 #include "clipitem.h"
29
30 ClipItem::ClipItem(int clipType, QString name, int producer, int maxDuration, const QRectF & rect)
31     : QGraphicsRectItem(rect), m_resizeMode(NONE), m_grabPoint(0), m_clipType(clipType), m_clipName(name), m_producer(producer), m_cropStart(0), m_cropDuration(maxDuration), m_maxDuration(maxDuration), m_maxTrack(0)
32 {
33   setToolTip(name);
34   //setCursor(Qt::SizeHorCursor);
35   setFlags(QGraphicsItem::ItemClipsToShape | QGraphicsItem::ItemClipsChildrenToShape | QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
36   m_label = new LabelItem( name, this);
37   QRectF textRect = m_label->boundingRect();
38   m_textWidth = textRect.width();
39   m_label->setPos(rect.x() + rect.width()/2 - m_textWidth/2, rect.y() + rect.height() / 2 - textRect.height()/2);
40   setBrush(QColor(100, 100, 150));
41 }
42
43 int ClipItem::type () const
44 {
45   return 70000;
46 }
47
48 int ClipItem::clipType()
49 {
50   return m_clipType;
51 }
52
53 QString ClipItem::clipName()
54 {
55   return m_clipName;
56 }
57
58 int ClipItem::clipProducer()
59 {
60   return m_producer;
61 }
62
63 int ClipItem::maxDuration()
64 {
65   return m_maxDuration;
66 }
67
68 // virtual 
69  void ClipItem::paint(QPainter *painter,
70                            const QStyleOptionGraphicsItem *option,
71                            QWidget *widget)
72  {
73
74     //painter->setClipRect( option->exposedRect );
75     /*painter->setRenderHint(QPainter::TextAntialiasing);
76     painter->fillRect(rect(), QColor(200, 50, 50, 150));
77     QPointF pos = option->matrix.map(QPointF(1.0, 1.0));
78     //painter->setPen(QPen(Qt::black, 1.0 / option->levelOfDetail));
79     painter->setPen(QPen(Qt::black, 1.0 / pos.x()));
80     double scale = 1.0 / pos.x();
81     //QPointF size = option->matrix.map(QPointF(1, 1));
82     //double off = painter->pen().width();
83     QRectF br = boundingRect();
84     QRectF recta(rect().x(), rect().y(), scale,rect().height());
85     painter->drawRect(recta);
86     //painter->drawLine(rect().x() + 1, rect().y(), rect().x() + 1, rect().y() + rect().height());
87     painter->drawLine(rect().x() + rect().width(), rect().y(), rect().x() + rect().width(), rect().y() + rect().height());
88     painter->setPen(QPen(Qt::black, 1.0 / pos.y()));
89     painter->drawLine(rect().x(), rect().y(), rect().x() + rect().width(), rect().y());
90     painter->drawLine(rect().x(), rect().y() + rect().height(), rect().x() + rect().width(), rect().y() + rect().height());*/
91
92     QGraphicsRectItem::paint(painter, option, widget);
93     //QPen pen(Qt::green, 1.0 / size.x() + 0.5);
94     //painter->setPen(pen);
95     //painter->drawLine(rect().x(), rect().y(), rect().x() + rect().width(), rect().y());*/
96     //kDebug()<<"ITEM REPAINT RECT: "<<boundingRect().width();
97     //painter->drawText(rect(), Qt::AlignCenter, m_name);
98     // painter->drawRect(boundingRect());
99      //painter->drawRoundRect(-10, -10, 20, 20);
100  }
101
102
103 OPERATIONTYPE ClipItem::operationMode(QPointF pos)
104 {
105     if (abs(pos.x() - rect().x()) < 6) {
106       if (abs(pos.y() - rect().y()) < 6) return FADEIN;
107       return RESIZESTART;
108     }
109     else if (abs(pos.x() - (rect().x() + rect().width())) < 6) {
110       if (abs(pos.y() - rect().y()) < 6) return FADEOUT;
111       return RESIZEEND;
112     }
113     return MOVE;
114 }
115
116 // virtual
117  void ClipItem::mousePressEvent ( QGraphicsSceneMouseEvent * event ) 
118  {
119     m_resizeMode = operationMode(event->pos());
120     if (m_resizeMode == MOVE) {
121       m_maxTrack = scene()->sceneRect().height();
122       m_grabPoint = (int) (event->pos().x() - rect().x());
123     }
124     QGraphicsRectItem::mousePressEvent(event);
125  }
126
127 // virtual
128  void ClipItem::mouseReleaseEvent ( QGraphicsSceneMouseEvent * event ) 
129  {
130     m_resizeMode = NONE;
131     QGraphicsRectItem::mouseReleaseEvent(event);
132  }
133
134  void ClipItem::moveTo(double x, double offset)
135  {
136   double origX = rect().x();
137   double origY = rect().y();
138     setRect(x, origY + offset, rect().width(), rect().height());
139
140     QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
141     for (int i = 0; i < collisionList.size(); ++i) {
142       QGraphicsItem *item = collisionList.at(i);
143       if (item->type() == 70000)
144       {
145         if (offset == 0)
146         {
147           QRectF other = ((QGraphicsRectItem *)item)->rect();
148           if (x < origX) {
149             kDebug()<<"COLLISION, MOVING TO------";
150             origX = other.x() + other.width(); 
151           }
152           else if (x > origX) {
153             kDebug()<<"COLLISION, MOVING TO+++";
154             origX = other.x() - rect().width(); 
155           }
156         }
157         setRect(origX, origY, rect().width(), rect().height());
158         offset = 0;
159         origX = rect().x();
160         break;
161       }
162     }
163
164     QList <QGraphicsItem *> childrenList = children();
165     for (int i = 0; i < childrenList.size(); ++i) {
166       childrenList.at(i)->moveBy(rect().x() - origX , offset);
167     }
168  }
169
170 // virtual
171  void ClipItem::mouseMoveEvent ( QGraphicsSceneMouseEvent * event ) 
172  {
173     double moveX = (int) event->scenePos().x();
174     double originalX = rect().x();
175     double originalWidth = rect().width();
176     if (m_resizeMode == RESIZESTART) {
177       if (m_cropStart - (originalX - moveX) < 0) moveX = originalX - m_cropStart;
178       if (originalX + rect().width() - moveX < 1) moveX = originalX + rect().width() + 2;
179       m_cropStart -= originalX - moveX;
180       kDebug()<<"MOVE CLIP START TO: "<<event->scenePos()<<", CROP: "<<m_cropStart;
181       setRect(moveX, rect().y(), originalX + rect().width() - moveX, rect().height());
182
183       QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
184       for (int i = 0; i < collisionList.size(); ++i) {
185         QGraphicsItem *item = collisionList.at(i);
186           if (item->type() == 70000)
187           {
188             QRectF other = ((QGraphicsRectItem *)item)->rect();
189             int newStart = other.x() + other.width();
190             setRect(newStart, rect().y(), rect().x() + rect().width() - newStart, rect().height());
191             moveX = newStart;
192             break;
193           }
194       }
195       QList <QGraphicsItem *> childrenList = children();
196       for (int i = 0; i < childrenList.size(); ++i) {
197         childrenList.at(i)->moveBy((moveX - originalX) / 2 , 0);
198       }
199       return;
200     }
201     if (m_resizeMode == RESIZEEND) {
202       int newWidth = moveX - originalX;
203       if (newWidth < 1) newWidth = 2;
204       if (newWidth > m_maxDuration) newWidth = m_maxDuration;
205       setRect(originalX, rect().y(), newWidth, rect().height());
206
207       QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
208       for (int i = 0; i < collisionList.size(); ++i) {
209         QGraphicsItem *item = collisionList.at(i);
210           if (item->type() == 70000)
211           {
212             QRectF other = ((QGraphicsRectItem *)item)->rect();
213             newWidth = other.x() - rect().x();
214             setRect(rect().x(), rect().y(), newWidth, rect().height());
215             break;
216           }
217       }
218
219       QList <QGraphicsItem *> childrenList = children();
220       for (int i = 0; i < childrenList.size(); ++i) {
221         childrenList.at(i)->moveBy((newWidth - originalWidth) / 2 , 0);
222       }
223       return;
224     }
225     if (m_resizeMode == MOVE) {
226       kDebug()<<"///////  MOVE CLIP, EVENT Y: "<<event->scenePos().y()<<", SCENE HEIGHT: "<<scene()->sceneRect().height();
227       int moveTrack = (int) event->scenePos().y() / 50;
228       int currentTrack = (int) rect().y() / 50;
229       int offset = moveTrack - currentTrack;
230       if (event->scenePos().y() >= m_maxTrack || event->scenePos().y() < 0) {
231         offset = 0;
232         kDebug()<<"%%%%%%%%%%%%%%%%%%%%%%%   MAX HEIGHT OVERLOOK";
233       }
234       if (offset != 0) offset = 50 * offset;
235       moveTo(moveX - m_grabPoint, offset);
236     }
237  }
238
239
240 // virtual 
241 /*
242 void CustomTrackView::mousePressEvent ( QMouseEvent * event )
243 {
244   int pos = event->x();
245   if (event->modifiers() == Qt::ControlModifier) 
246     setDragMode(QGraphicsView::ScrollHandDrag);
247   else if (event->modifiers() == Qt::ShiftModifier) 
248     setDragMode(QGraphicsView::RubberBandDrag);
249   else {
250     QGraphicsItem * item = itemAt(event->pos());
251     if (item) {
252     }
253     else emit cursorMoved((int) mapToScene(event->x(), 0).x());
254   }
255   kDebug()<<pos;
256   QGraphicsView::mousePressEvent(event);
257 }
258
259 void CustomTrackView::mouseReleaseEvent ( QMouseEvent * event )
260 {
261   QGraphicsView::mouseReleaseEvent(event);
262   setDragMode(QGraphicsView::NoDrag);
263 }
264 */
265
266 #include "clipitem.moc"