]> git.sesse.net Git - kdenlive/blob - src/clipitem.cpp
28c8e9435fedf361c3d85860bafad53302b12ff5
[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, const QRectF & rect)
31     : QGraphicsRectItem(rect), m_resizeMode(0), m_grabPoint(0), m_clipType(clipType), m_clipName(name), m_producer(producer)
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 // virtual 
63  void ClipItem::paint(QPainter *painter,
64                            const QStyleOptionGraphicsItem *option,
65                            QWidget *widget)
66  {
67     painter->setClipRect( option->exposedRect );
68     painter->fillRect(rect(), QColor(200, 50, 50, 150));
69     //kDebug()<<"ITEM REPAINT RECT: "<<boundingRect().width();
70     //painter->drawText(rect(), Qt::AlignCenter, m_name);
71     painter->drawRect(rect());
72      //painter->drawRoundRect(-10, -10, 20, 20);
73  }
74
75
76 int ClipItem::operationMode(QPointF pos)
77 {
78     if (abs(pos.x() - rect().x()) < 6)
79       return 1;
80     else if (abs(pos.x() - (rect().x() + rect().width())) < 6)
81       return 2;
82     return 0;
83 }
84
85 // virtual
86  void ClipItem::mousePressEvent ( QGraphicsSceneMouseEvent * event ) 
87  {
88     m_resizeMode = operationMode(event->pos());
89     if (m_resizeMode == 0) {
90       m_grabPoint = (int) (event->pos().x() - rect().x());
91     }
92     QGraphicsRectItem::mousePressEvent(event);
93  }
94
95 // virtual
96  void ClipItem::mouseReleaseEvent ( QGraphicsSceneMouseEvent * event ) 
97  {
98     m_resizeMode = 0;
99     QGraphicsRectItem::mouseReleaseEvent(event);
100  }
101
102  void ClipItem::moveTo(double x, double offset)
103  {
104   double origX = rect().x();
105   double origY = rect().y();
106     setRect(x, origY + offset, rect().width(), rect().height());
107
108     QList <QGraphicsItem *> collisionList = collidingItems();
109     for (int i = 0; i < collisionList.size(); ++i) {
110       QGraphicsItem *item = collisionList.at(i);
111       if (item->type() == 70000)
112       {
113         if (offset == 0)
114         {
115           QRectF other = ((QGraphicsRectItem *)item)->rect();
116           QPointF pos = mapFromItem(item, other.x()+other.width(), 0);
117           //mapToItem(collisionList.at(i), collisionList.at(i)->boundingRect()).boundingRect();
118           if (x < origX) {
119             kDebug()<<"COLLISION, MOVING TO------";
120             origX = other.x() + other.width(); 
121           }
122           else if (x > origX) {
123             kDebug()<<"COLLISION, MOVING TO+++";
124             origX = other.x() - rect().width(); 
125           }
126         }
127         setRect(origX, origY, rect().width(), rect().height());
128         offset = 0;
129         origX = rect().x();
130         break;
131       }
132     }
133
134     QList <QGraphicsItem *> childrenList = children();
135     for (int i = 0; i < childrenList.size(); ++i) {
136       childrenList.at(i)->moveBy(rect().x() - origX , offset);
137     }
138  }
139
140 // virtual
141  void ClipItem::mouseMoveEvent ( QGraphicsSceneMouseEvent * event ) 
142  {
143     double moveX = (int) event->scenePos().x();
144     double originalX = rect().x();
145     double originalWidth = rect().width();
146     if (m_resizeMode == 1) {
147       kDebug()<<"MOVE CLIP START TO: "<<event->scenePos();
148       setRect(moveX, rect().y(), originalX + rect().width() - moveX, rect().height());
149       QList <QGraphicsItem *> childrenList = children();
150       for (int i = 0; i < childrenList.size(); ++i) {
151         childrenList.at(i)->moveBy((moveX - originalX) / 2 , 0);
152       }
153       return;
154     }
155     if (m_resizeMode == 2) {
156       setRect(originalX, rect().y(), moveX - originalX, rect().height());
157       QList <QGraphicsItem *> childrenList = children();
158       for (int i = 0; i < childrenList.size(); ++i) {
159         childrenList.at(i)->moveBy((moveX - originalX - originalWidth) / 2 , 0);
160       }
161       return;
162     }
163     int moveTrack = (int) event->scenePos().y() / 50;
164     int currentTrack = (int) rect().y() / 50;
165     int offset = moveTrack - currentTrack;
166     if (offset != 0) offset = 50 * offset;
167     moveTo(moveX - m_grabPoint, offset);
168     
169  }
170
171
172 // virtual 
173 /*
174 void CustomTrackView::mousePressEvent ( QMouseEvent * event )
175 {
176   int pos = event->x();
177   if (event->modifiers() == Qt::ControlModifier) 
178     setDragMode(QGraphicsView::ScrollHandDrag);
179   else if (event->modifiers() == Qt::ShiftModifier) 
180     setDragMode(QGraphicsView::RubberBandDrag);
181   else {
182     QGraphicsItem * item = itemAt(event->pos());
183     if (item) {
184     }
185     else emit cursorMoved((int) mapToScene(event->x(), 0).x());
186   }
187   kDebug()<<pos;
188   QGraphicsView::mousePressEvent(event);
189 }
190
191 void CustomTrackView::mouseReleaseEvent ( QMouseEvent * event )
192 {
193   QGraphicsView::mouseReleaseEvent(event);
194   setDragMode(QGraphicsView::NoDrag);
195 }
196 */
197
198 #include "clipitem.moc"