1 /***************************************************************************
2 * Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org) *
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. *
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. *
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 ***************************************************************************/
23 #include <QStyleOptionGraphicsItem>
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)
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);
42 int ClipItem::type () const
47 int ClipItem::clipType()
52 QString ClipItem::clipName()
57 int ClipItem::clipProducer()
62 int ClipItem::maxDuration()
68 void ClipItem::paint(QPainter *painter,
69 const QStyleOptionGraphicsItem *option,
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);
81 int ClipItem::operationMode(QPointF pos)
83 if (abs(pos.x() - rect().x()) < 6)
85 else if (abs(pos.x() - (rect().x() + rect().width())) < 6)
91 void ClipItem::mousePressEvent ( QGraphicsSceneMouseEvent * event )
93 m_resizeMode = operationMode(event->pos());
94 if (m_resizeMode == 0) {
95 m_grabPoint = (int) (event->pos().x() - rect().x());
97 QGraphicsRectItem::mousePressEvent(event);
101 void ClipItem::mouseReleaseEvent ( QGraphicsSceneMouseEvent * event )
104 QGraphicsRectItem::mouseReleaseEvent(event);
107 void ClipItem::moveTo(double x, double offset)
109 double origX = rect().x();
110 double origY = rect().y();
111 setRect(x, origY + offset, rect().width(), rect().height());
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)
120 QRectF other = ((QGraphicsRectItem *)item)->rect();
121 QPointF pos = mapFromItem(item, other.x()+other.width(), 0);
122 //mapToItem(collisionList.at(i), collisionList.at(i)->boundingRect()).boundingRect();
124 kDebug()<<"COLLISION, MOVING TO------";
125 origX = other.x() + other.width();
127 else if (x > origX) {
128 kDebug()<<"COLLISION, MOVING TO+++";
129 origX = other.x() - rect().width();
132 setRect(origX, origY, rect().width(), rect().height());
139 QList <QGraphicsItem *> childrenList = children();
140 for (int i = 0; i < childrenList.size(); ++i) {
141 childrenList.at(i)->moveBy(rect().x() - origX , offset);
146 void ClipItem::mouseMoveEvent ( QGraphicsSceneMouseEvent * event )
148 double moveX = (int) event->scenePos().x();
149 double originalX = rect().x();
150 double originalWidth = rect().width();
151 if (m_resizeMode == 1) {
152 if (m_cropStart - (originalX - moveX) < 0) moveX = originalX - m_cropStart;
153 if (originalX + rect().width() - moveX < 1) moveX = originalX + rect().width() + 2;
154 m_cropStart -= originalX - moveX;
155 kDebug()<<"MOVE CLIP START TO: "<<event->scenePos()<<", CROP: "<<m_cropStart;
156 setRect(moveX, rect().y(), originalX + rect().width() - moveX, rect().height());
157 QList <QGraphicsItem *> childrenList = children();
158 for (int i = 0; i < childrenList.size(); ++i) {
159 childrenList.at(i)->moveBy((moveX - originalX) / 2 , 0);
163 if (m_resizeMode == 2) {
164 int newWidth = moveX - originalX;
165 if (newWidth < 1) newWidth = 2;
166 if (newWidth > m_maxDuration) newWidth = m_maxDuration;
167 setRect(originalX, rect().y(), newWidth, rect().height());
168 QList <QGraphicsItem *> childrenList = children();
169 for (int i = 0; i < childrenList.size(); ++i) {
170 childrenList.at(i)->moveBy((newWidth - originalWidth) / 2 , 0);
174 int moveTrack = (int) event->scenePos().y() / 50;
175 int currentTrack = (int) rect().y() / 50;
176 int offset = moveTrack - currentTrack;
177 if (offset != 0) offset = 50 * offset;
178 moveTo(moveX - m_grabPoint, offset);
185 void CustomTrackView::mousePressEvent ( QMouseEvent * event )
187 int pos = event->x();
188 if (event->modifiers() == Qt::ControlModifier)
189 setDragMode(QGraphicsView::ScrollHandDrag);
190 else if (event->modifiers() == Qt::ShiftModifier)
191 setDragMode(QGraphicsView::RubberBandDrag);
193 QGraphicsItem * item = itemAt(event->pos());
196 else emit cursorMoved((int) mapToScene(event->x(), 0).x());
199 QGraphicsView::mousePressEvent(event);
202 void CustomTrackView::mouseReleaseEvent ( QMouseEvent * event )
204 QGraphicsView::mouseReleaseEvent(event);
205 setDragMode(QGraphicsView::NoDrag);
209 #include "clipitem.moc"