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(NONE), 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);
40 setBrush(QColor(100, 100, 150));
43 int ClipItem::type () const
48 int ClipItem::clipType()
53 QString ClipItem::clipName()
58 int ClipItem::clipProducer()
63 int ClipItem::maxDuration()
69 void ClipItem::paint(QPainter *painter,
70 const QStyleOptionGraphicsItem *option,
74 //painter->setClipRect( option->exposedRect );
75 /*painter->setRenderHint(QPainter::TextAntialiasing);
76 painter->fillRect(rect(), QColor(200, 50, 50, 150));
77 QPointF pos = option->matrix.map(QPointF(1.0, 1.0));
78 //painter->setPen(QPen(Qt::black, 1.0 / option->levelOfDetail));
79 painter->setPen(QPen(Qt::black, 1.0 / pos.x()));
80 double scale = 1.0 / pos.x();
81 //QPointF size = option->matrix.map(QPointF(1, 1));
82 //double off = painter->pen().width();
83 QRectF br = boundingRect();
84 QRectF recta(rect().x(), rect().y(), scale,rect().height());
85 painter->drawRect(recta);
86 //painter->drawLine(rect().x() + 1, rect().y(), rect().x() + 1, rect().y() + rect().height());
87 painter->drawLine(rect().x() + rect().width(), rect().y(), rect().x() + rect().width(), rect().y() + rect().height());
88 painter->setPen(QPen(Qt::black, 1.0 / pos.y()));
89 painter->drawLine(rect().x(), rect().y(), rect().x() + rect().width(), rect().y());
90 painter->drawLine(rect().x(), rect().y() + rect().height(), rect().x() + rect().width(), rect().y() + rect().height());*/
92 QGraphicsRectItem::paint(painter, option, widget);
93 //QPen pen(Qt::green, 1.0 / size.x() + 0.5);
94 //painter->setPen(pen);
95 //painter->drawLine(rect().x(), rect().y(), rect().x() + rect().width(), rect().y());*/
96 //kDebug()<<"ITEM REPAINT RECT: "<<boundingRect().width();
97 //painter->drawText(rect(), Qt::AlignCenter, m_name);
98 // painter->drawRect(boundingRect());
99 //painter->drawRoundRect(-10, -10, 20, 20);
103 OPERATIONTYPE ClipItem::operationMode(QPointF pos)
105 if (abs(pos.x() - rect().x()) < 6) {
106 if (abs(pos.y() - rect().y()) < 6) return FADEIN;
109 else if (abs(pos.x() - (rect().x() + rect().width())) < 6) {
110 if (abs(pos.y() - rect().y()) < 6) return FADEOUT;
117 void ClipItem::mousePressEvent ( QGraphicsSceneMouseEvent * event )
119 m_resizeMode = operationMode(event->pos());
120 if (m_resizeMode == MOVE) {
121 m_grabPoint = (int) (event->pos().x() - rect().x());
123 QGraphicsRectItem::mousePressEvent(event);
127 void ClipItem::mouseReleaseEvent ( QGraphicsSceneMouseEvent * event )
130 QGraphicsRectItem::mouseReleaseEvent(event);
133 void ClipItem::moveTo(double x, double offset)
135 double origX = rect().x();
136 double origY = rect().y();
137 setRect(x, origY + offset, rect().width(), rect().height());
139 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
140 for (int i = 0; i < collisionList.size(); ++i) {
141 QGraphicsItem *item = collisionList.at(i);
142 if (item->type() == 70000)
146 QRectF other = ((QGraphicsRectItem *)item)->rect();
148 kDebug()<<"COLLISION, MOVING TO------";
149 origX = other.x() + other.width();
151 else if (x > origX) {
152 kDebug()<<"COLLISION, MOVING TO+++";
153 origX = other.x() - rect().width();
156 setRect(origX, origY, rect().width(), rect().height());
163 QList <QGraphicsItem *> childrenList = children();
164 for (int i = 0; i < childrenList.size(); ++i) {
165 childrenList.at(i)->moveBy(rect().x() - origX , offset);
170 void ClipItem::mouseMoveEvent ( QGraphicsSceneMouseEvent * event )
172 double moveX = (int) event->scenePos().x();
173 double originalX = rect().x();
174 double originalWidth = rect().width();
175 if (m_resizeMode == RESIZESTART) {
176 if (m_cropStart - (originalX - moveX) < 0) moveX = originalX - m_cropStart;
177 if (originalX + rect().width() - moveX < 1) moveX = originalX + rect().width() + 2;
178 m_cropStart -= originalX - moveX;
179 kDebug()<<"MOVE CLIP START TO: "<<event->scenePos()<<", CROP: "<<m_cropStart;
180 setRect(moveX, rect().y(), originalX + rect().width() - moveX, rect().height());
182 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
183 for (int i = 0; i < collisionList.size(); ++i) {
184 QGraphicsItem *item = collisionList.at(i);
185 if (item->type() == 70000)
187 QRectF other = ((QGraphicsRectItem *)item)->rect();
188 int newStart = other.x() + other.width();
189 setRect(newStart, rect().y(), rect().x() + rect().width() - newStart, rect().height());
194 QList <QGraphicsItem *> childrenList = children();
195 for (int i = 0; i < childrenList.size(); ++i) {
196 childrenList.at(i)->moveBy((moveX - originalX) / 2 , 0);
200 if (m_resizeMode == RESIZEEND) {
201 int newWidth = moveX - originalX;
202 if (newWidth < 1) newWidth = 2;
203 if (newWidth > m_maxDuration) newWidth = m_maxDuration;
204 setRect(originalX, rect().y(), newWidth, rect().height());
206 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
207 for (int i = 0; i < collisionList.size(); ++i) {
208 QGraphicsItem *item = collisionList.at(i);
209 if (item->type() == 70000)
211 QRectF other = ((QGraphicsRectItem *)item)->rect();
212 newWidth = other.x() - rect().x();
213 setRect(rect().x(), rect().y(), newWidth, rect().height());
218 QList <QGraphicsItem *> childrenList = children();
219 for (int i = 0; i < childrenList.size(); ++i) {
220 childrenList.at(i)->moveBy((newWidth - originalWidth) / 2 , 0);
224 if (m_resizeMode == MOVE) {
225 int moveTrack = (int) event->scenePos().y() / 50;
226 int currentTrack = (int) rect().y() / 50;
227 int offset = moveTrack - currentTrack;
228 if (offset != 0) offset = 50 * offset;
229 moveTo(moveX - m_grabPoint, offset);
236 void CustomTrackView::mousePressEvent ( QMouseEvent * event )
238 int pos = event->x();
239 if (event->modifiers() == Qt::ControlModifier)
240 setDragMode(QGraphicsView::ScrollHandDrag);
241 else if (event->modifiers() == Qt::ShiftModifier)
242 setDragMode(QGraphicsView::RubberBandDrag);
244 QGraphicsItem * item = itemAt(event->pos());
247 else emit cursorMoved((int) mapToScene(event->x(), 0).x());
250 QGraphicsView::mousePressEvent(event);
253 void CustomTrackView::mouseReleaseEvent ( QMouseEvent * event )
255 QGraphicsView::mouseReleaseEvent(event);
256 setDragMode(QGraphicsView::NoDrag);
260 #include "clipitem.moc"