]> git.sesse.net Git - kdenlive/blob - src/clipitem.cpp
Start porting timeline to QGraphicsView
[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_producer(producer)
32 {
33   setToolTip(name);
34   //setCursor(Qt::SizeHorCursor);
35   setFlags(QGraphicsItem::ItemClipsToShape | QGraphicsItem::ItemClipsChildrenToShape | QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
36   //producer = "sjis isjisjsi sij ssao sa sao ";
37   m_label = new LabelItem( name, this);
38   QRectF textRect = m_label->boundingRect();
39   m_textWidth = textRect.width();
40   m_label->setPos(rect.x() + rect.width()/2 - m_textWidth/2, rect.y() + rect.height() / 2 - textRect.height()/2);
41 }
42
43 int ClipItem::type () const
44 {
45   return 70000;
46 }
47
48 // virtual 
49  void ClipItem::paint(QPainter *painter,
50                            const QStyleOptionGraphicsItem *option,
51                            QWidget *widget)
52  {
53     painter->setClipRect( option->exposedRect );
54     painter->fillRect(rect(), Qt::red);
55     //kDebug()<<"ITEM REPAINT RECT: "<<boundingRect().width();
56     //painter->drawText(rect(), Qt::AlignCenter, m_name);
57     painter->drawRect(rect());
58      //painter->drawRoundRect(-10, -10, 20, 20);
59  }
60
61 // virtual
62  void ClipItem::mousePressEvent ( QGraphicsSceneMouseEvent * event ) 
63  {
64     if (abs(event->pos().x() - rect().x()) < 6)
65       m_resizeMode = 1;
66     else if (abs(event->pos().x() - (rect().x() + rect().width())) < 6)
67       m_resizeMode = 2;
68     else  {
69       m_resizeMode = 0;
70       m_grabPoint = event->pos().x() - rect().x();
71     }
72     QGraphicsRectItem::mousePressEvent(event);
73  }
74
75 // virtual
76  void ClipItem::mouseReleaseEvent ( QGraphicsSceneMouseEvent * event ) 
77  {
78     m_resizeMode = 0;
79     QGraphicsRectItem::mouseReleaseEvent(event);
80  }
81
82  void ClipItem::moveTo(double x, double offset)
83  {
84   double origX = rect().x();
85   double origY = rect().y();
86     setRect(x, origY + offset, rect().width(), rect().height());
87
88     QList <QGraphicsItem *> collisionList = collidingItems();
89     for (int i = 0; i < collisionList.size(); ++i) {
90       QGraphicsItem *item = collisionList.at(i);
91       if (item->type() == 70000)
92       {
93         if (offset == 0)
94         {
95           QRectF other = ((QGraphicsRectItem *)item)->rect();
96           QPointF pos = mapFromItem(item, other.x()+other.width(), 0);
97           //mapToItem(collisionList.at(i), collisionList.at(i)->boundingRect()).boundingRect();
98           if (x < origX) {
99             kDebug()<<"COLLISION, MOVING TO------";
100             origX = other.x() + other.width(); 
101           }
102           else if (x > origX) {
103             kDebug()<<"COLLISION, MOVING TO+++";
104             origX = other.x() - rect().width(); 
105           }
106         }
107         setRect(origX, origY, rect().width(), rect().height());
108         offset = 0;
109         origX = rect().x();
110         break;
111       }
112     }
113
114     QList <QGraphicsItem *> childrenList = children();
115     for (int i = 0; i < childrenList.size(); ++i) {
116       childrenList.at(i)->moveBy(rect().x() - origX , offset);
117     }
118  }
119
120 // virtual
121  void ClipItem::mouseMoveEvent ( QGraphicsSceneMouseEvent * event ) 
122  {
123     double moveX = event->scenePos().x();
124     double originalX = rect().x();
125     if (m_resizeMode == 1) {
126       setRect(moveX, rect().y(), originalX + rect().width() - moveX, rect().height());
127       return;
128     }
129     if (m_resizeMode == 2) {
130       setRect(originalX, rect().y(), moveX - originalX, rect().height());
131       return;
132     }
133     int moveTrack = (int) event->scenePos().y() / 50;
134     int currentTrack = (int) rect().y() / 50;
135     int offset = moveTrack - currentTrack;
136     if (offset != 0) offset = 50 * offset;
137     moveTo(moveX - m_grabPoint, offset);
138     
139  }
140
141
142 // virtual 
143 /*
144 void CustomTrackView::mousePressEvent ( QMouseEvent * event )
145 {
146   int pos = event->x();
147   if (event->modifiers() == Qt::ControlModifier) 
148     setDragMode(QGraphicsView::ScrollHandDrag);
149   else if (event->modifiers() == Qt::ShiftModifier) 
150     setDragMode(QGraphicsView::RubberBandDrag);
151   else {
152     QGraphicsItem * item = itemAt(event->pos());
153     if (item) {
154     }
155     else emit cursorMoved((int) mapToScene(event->x(), 0).x());
156   }
157   kDebug()<<pos;
158   QGraphicsView::mousePressEvent(event);
159 }
160
161 void CustomTrackView::mouseReleaseEvent ( QMouseEvent * event )
162 {
163   QGraphicsView::mouseReleaseEvent(event);
164   setDragMode(QGraphicsView::NoDrag);
165 }
166 */
167
168 #include "clipitem.moc"