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