]> git.sesse.net Git - kdenlive/blob - src/clipitem.cpp
89e8c49521f706361c205e1b52f67148704cdb74
[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
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)
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_grabPoint = (int) (event->pos().x() - rect().x());
122     }
123     QGraphicsRectItem::mousePressEvent(event);
124  }
125
126 // virtual
127  void ClipItem::mouseReleaseEvent ( QGraphicsSceneMouseEvent * event ) 
128  {
129     m_resizeMode = NONE;
130     QGraphicsRectItem::mouseReleaseEvent(event);
131  }
132
133  void ClipItem::moveTo(double x, double offset)
134  {
135   double origX = rect().x();
136   double origY = rect().y();
137     setRect(x, origY + offset, rect().width(), rect().height());
138
139     QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
140     for (int i = 0; i < collisionList.size(); ++i) {
141       QGraphicsItem *item = collisionList.at(i);
142       if (item->type() == 70000)
143       {
144         if (offset == 0)
145         {
146           QRectF other = ((QGraphicsRectItem *)item)->rect();
147           if (x < origX) {
148             kDebug()<<"COLLISION, MOVING TO------";
149             origX = other.x() + other.width(); 
150           }
151           else if (x > origX) {
152             kDebug()<<"COLLISION, MOVING TO+++";
153             origX = other.x() - rect().width(); 
154           }
155         }
156         setRect(origX, origY, rect().width(), rect().height());
157         offset = 0;
158         origX = rect().x();
159         break;
160       }
161     }
162
163     QList <QGraphicsItem *> childrenList = children();
164     for (int i = 0; i < childrenList.size(); ++i) {
165       childrenList.at(i)->moveBy(rect().x() - origX , offset);
166     }
167  }
168
169 // virtual
170  void ClipItem::mouseMoveEvent ( QGraphicsSceneMouseEvent * event ) 
171  {
172     double moveX = (int) event->scenePos().x();
173     double originalX = rect().x();
174     double originalWidth = rect().width();
175     if (m_resizeMode == RESIZESTART) {
176       if (m_cropStart - (originalX - moveX) < 0) moveX = originalX - m_cropStart;
177       if (originalX + rect().width() - moveX < 1) moveX = originalX + rect().width() + 2;
178       m_cropStart -= originalX - moveX;
179       kDebug()<<"MOVE CLIP START TO: "<<event->scenePos()<<", CROP: "<<m_cropStart;
180       setRect(moveX, rect().y(), originalX + rect().width() - moveX, rect().height());
181
182       QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
183       for (int i = 0; i < collisionList.size(); ++i) {
184         QGraphicsItem *item = collisionList.at(i);
185           if (item->type() == 70000)
186           {
187             QRectF other = ((QGraphicsRectItem *)item)->rect();
188             int newStart = other.x() + other.width();
189             setRect(newStart, rect().y(), rect().x() + rect().width() - newStart, rect().height());
190             moveX = newStart;
191             break;
192           }
193       }
194       QList <QGraphicsItem *> childrenList = children();
195       for (int i = 0; i < childrenList.size(); ++i) {
196         childrenList.at(i)->moveBy((moveX - originalX) / 2 , 0);
197       }
198       return;
199     }
200     if (m_resizeMode == RESIZEEND) {
201       int newWidth = moveX - originalX;
202       if (newWidth < 1) newWidth = 2;
203       if (newWidth > m_maxDuration) newWidth = m_maxDuration;
204       setRect(originalX, rect().y(), newWidth, rect().height());
205
206       QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
207       for (int i = 0; i < collisionList.size(); ++i) {
208         QGraphicsItem *item = collisionList.at(i);
209           if (item->type() == 70000)
210           {
211             QRectF other = ((QGraphicsRectItem *)item)->rect();
212             newWidth = other.x() - rect().x();
213             setRect(rect().x(), rect().y(), newWidth, rect().height());
214             break;
215           }
216       }
217
218       QList <QGraphicsItem *> childrenList = children();
219       for (int i = 0; i < childrenList.size(); ++i) {
220         childrenList.at(i)->moveBy((newWidth - originalWidth) / 2 , 0);
221       }
222       return;
223     }
224     if (m_resizeMode == MOVE) {
225       int moveTrack = (int) event->scenePos().y() / 50;
226       int currentTrack = (int) rect().y() / 50;
227       int offset = moveTrack - currentTrack;
228       if (offset != 0) offset = 50 * offset;
229       moveTo(moveX - m_grabPoint, offset);
230     }
231  }
232
233
234 // virtual 
235 /*
236 void CustomTrackView::mousePressEvent ( QMouseEvent * event )
237 {
238   int pos = event->x();
239   if (event->modifiers() == Qt::ControlModifier) 
240     setDragMode(QGraphicsView::ScrollHandDrag);
241   else if (event->modifiers() == Qt::ShiftModifier) 
242     setDragMode(QGraphicsView::RubberBandDrag);
243   else {
244     QGraphicsItem * item = itemAt(event->pos());
245     if (item) {
246     }
247     else emit cursorMoved((int) mapToScene(event->x(), 0).x());
248   }
249   kDebug()<<pos;
250   QGraphicsView::mousePressEvent(event);
251 }
252
253 void CustomTrackView::mouseReleaseEvent ( QMouseEvent * event )
254 {
255   QGraphicsView::mouseReleaseEvent(event);
256   setDragMode(QGraphicsView::NoDrag);
257 }
258 */
259
260 #include "clipitem.moc"