]> git.sesse.net Git - kdenlive/blob - src/clipitem.cpp
5e16fce15b6810d08afc7a65dbd1f781885c3f64
[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(0), 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 }
41
42 int ClipItem::type () const
43 {
44   return 70000;
45 }
46
47 int ClipItem::clipType()
48 {
49   return m_clipType;
50 }
51
52 QString ClipItem::clipName()
53 {
54   return m_clipName;
55 }
56
57 int ClipItem::clipProducer()
58 {
59   return m_producer;
60 }
61
62 int ClipItem::maxDuration()
63 {
64   return m_maxDuration;
65 }
66
67 // virtual 
68  void ClipItem::paint(QPainter *painter,
69                            const QStyleOptionGraphicsItem *option,
70                            QWidget *widget)
71  {
72     painter->setClipRect( option->exposedRect );
73     painter->fillRect(rect(), QColor(200, 50, 50, 150));
74     //kDebug()<<"ITEM REPAINT RECT: "<<boundingRect().width();
75     //painter->drawText(rect(), Qt::AlignCenter, m_name);
76     painter->drawRect(rect());
77      //painter->drawRoundRect(-10, -10, 20, 20);
78  }
79
80
81 int ClipItem::operationMode(QPointF pos)
82 {
83     if (abs(pos.x() - rect().x()) < 6)
84       return 1;
85     else if (abs(pos.x() - (rect().x() + rect().width())) < 6)
86       return 2;
87     return 0;
88 }
89
90 // virtual
91  void ClipItem::mousePressEvent ( QGraphicsSceneMouseEvent * event ) 
92  {
93     m_resizeMode = operationMode(event->pos());
94     if (m_resizeMode == 0) {
95       m_grabPoint = (int) (event->pos().x() - rect().x());
96     }
97     QGraphicsRectItem::mousePressEvent(event);
98  }
99
100 // virtual
101  void ClipItem::mouseReleaseEvent ( QGraphicsSceneMouseEvent * event ) 
102  {
103     m_resizeMode = 0;
104     QGraphicsRectItem::mouseReleaseEvent(event);
105  }
106
107  void ClipItem::moveTo(double x, double offset)
108  {
109   double origX = rect().x();
110   double origY = rect().y();
111     setRect(x, origY + offset, rect().width(), rect().height());
112
113     QList <QGraphicsItem *> collisionList = collidingItems();
114     for (int i = 0; i < collisionList.size(); ++i) {
115       QGraphicsItem *item = collisionList.at(i);
116       if (item->type() == 70000)
117       {
118         if (offset == 0)
119         {
120           QRectF other = ((QGraphicsRectItem *)item)->rect();
121           if (x < origX) {
122             kDebug()<<"COLLISION, MOVING TO------";
123             origX = other.x() + other.width(); 
124           }
125           else if (x > origX) {
126             kDebug()<<"COLLISION, MOVING TO+++";
127             origX = other.x() - rect().width(); 
128           }
129         }
130         setRect(origX, origY, rect().width(), rect().height());
131         offset = 0;
132         origX = rect().x();
133         break;
134       }
135     }
136
137     QList <QGraphicsItem *> childrenList = children();
138     for (int i = 0; i < childrenList.size(); ++i) {
139       childrenList.at(i)->moveBy(rect().x() - origX , offset);
140     }
141  }
142
143 // virtual
144  void ClipItem::mouseMoveEvent ( QGraphicsSceneMouseEvent * event ) 
145  {
146     double moveX = (int) event->scenePos().x();
147     double originalX = rect().x();
148     double originalWidth = rect().width();
149     if (m_resizeMode == 1) {
150       if (m_cropStart - (originalX - moveX) < 0) moveX = originalX - m_cropStart;
151       if (originalX + rect().width() - moveX < 1) moveX = originalX + rect().width() + 2;
152       m_cropStart -= originalX - moveX;
153       kDebug()<<"MOVE CLIP START TO: "<<event->scenePos()<<", CROP: "<<m_cropStart;
154       setRect(moveX, rect().y(), originalX + rect().width() - moveX, rect().height());
155
156       QList <QGraphicsItem *> collisionList = collidingItems();
157       for (int i = 0; i < collisionList.size(); ++i) {
158         QGraphicsItem *item = collisionList.at(i);
159           if (item->type() == 70000)
160           {
161             QRectF other = ((QGraphicsRectItem *)item)->rect();
162             int newStart = other.x() + other.width();
163             setRect(newStart, rect().y(), rect().x() + rect().width() - newStart, rect().height());
164             moveX = newStart;
165             break;
166           }
167       }
168       QList <QGraphicsItem *> childrenList = children();
169       for (int i = 0; i < childrenList.size(); ++i) {
170         childrenList.at(i)->moveBy((moveX - originalX) / 2 , 0);
171       }
172       return;
173     }
174     if (m_resizeMode == 2) {
175       int newWidth = moveX - originalX;
176       if (newWidth < 1) newWidth = 2;
177       if (newWidth > m_maxDuration) newWidth = m_maxDuration;
178       setRect(originalX, rect().y(), newWidth, rect().height());
179
180       QList <QGraphicsItem *> collisionList = collidingItems();
181       for (int i = 0; i < collisionList.size(); ++i) {
182         QGraphicsItem *item = collisionList.at(i);
183           if (item->type() == 70000)
184           {
185             QRectF other = ((QGraphicsRectItem *)item)->rect();
186             newWidth = other.x() - rect().x();
187             setRect(rect().x(), rect().y(), newWidth, rect().height());
188             break;
189           }
190       }
191
192       QList <QGraphicsItem *> childrenList = children();
193       for (int i = 0; i < childrenList.size(); ++i) {
194         childrenList.at(i)->moveBy((newWidth - originalWidth) / 2 , 0);
195       }
196       return;
197     }
198     int moveTrack = (int) event->scenePos().y() / 50;
199     int currentTrack = (int) rect().y() / 50;
200     int offset = moveTrack - currentTrack;
201     if (offset != 0) offset = 50 * offset;
202     moveTo(moveX - m_grabPoint, offset);
203     
204  }
205
206
207 // virtual 
208 /*
209 void CustomTrackView::mousePressEvent ( QMouseEvent * event )
210 {
211   int pos = event->x();
212   if (event->modifiers() == Qt::ControlModifier) 
213     setDragMode(QGraphicsView::ScrollHandDrag);
214   else if (event->modifiers() == Qt::ShiftModifier) 
215     setDragMode(QGraphicsView::RubberBandDrag);
216   else {
217     QGraphicsItem * item = itemAt(event->pos());
218     if (item) {
219     }
220     else emit cursorMoved((int) mapToScene(event->x(), 0).x());
221   }
222   kDebug()<<pos;
223   QGraphicsView::mousePressEvent(event);
224 }
225
226 void CustomTrackView::mouseReleaseEvent ( QMouseEvent * event )
227 {
228   QGraphicsView::mouseReleaseEvent(event);
229   setDragMode(QGraphicsView::NoDrag);
230 }
231 */
232
233 #include "clipitem.moc"