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, const QRectF & rect)
31 : QGraphicsRectItem(rect), m_resizeMode(0), m_grabPoint(0), m_producer(producer)
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);
43 int ClipItem::type () const
49 void ClipItem::paint(QPainter *painter,
50 const QStyleOptionGraphicsItem *option,
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);
62 void ClipItem::mousePressEvent ( QGraphicsSceneMouseEvent * event )
64 if (abs(event->pos().x() - rect().x()) < 6)
66 else if (abs(event->pos().x() - (rect().x() + rect().width())) < 6)
70 m_grabPoint = event->pos().x() - rect().x();
72 QGraphicsRectItem::mousePressEvent(event);
76 void ClipItem::mouseReleaseEvent ( QGraphicsSceneMouseEvent * event )
79 QGraphicsRectItem::mouseReleaseEvent(event);
82 void ClipItem::moveTo(double x, double offset)
84 double origX = rect().x();
85 double origY = rect().y();
86 setRect(x, origY + offset, rect().width(), rect().height());
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)
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();
99 kDebug()<<"COLLISION, MOVING TO------";
100 origX = other.x() + other.width();
102 else if (x > origX) {
103 kDebug()<<"COLLISION, MOVING TO+++";
104 origX = other.x() - rect().width();
107 setRect(origX, origY, rect().width(), rect().height());
114 QList <QGraphicsItem *> childrenList = children();
115 for (int i = 0; i < childrenList.size(); ++i) {
116 childrenList.at(i)->moveBy(rect().x() - origX , offset);
121 void ClipItem::mouseMoveEvent ( QGraphicsSceneMouseEvent * event )
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());
129 if (m_resizeMode == 2) {
130 setRect(originalX, rect().y(), moveX - originalX, rect().height());
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);
144 void CustomTrackView::mousePressEvent ( QMouseEvent * event )
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);
152 QGraphicsItem * item = itemAt(event->pos());
155 else emit cursorMoved((int) mapToScene(event->x(), 0).x());
158 QGraphicsView::mousePressEvent(event);
161 void CustomTrackView::mouseReleaseEvent ( QMouseEvent * event )
163 QGraphicsView::mouseReleaseEvent(event);
164 setDragMode(QGraphicsView::NoDrag);
168 #include "clipitem.moc"