1 /***************************************************************************
2 * Copyright (C) 2008 by Marco Gittler (g.marco@freenet.de) *
3 * Copyright (C) 2008 by Jean-Baptiste Mardelle (jb@kdenlive.org) *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
21 #include <QGraphicsScene>
22 #include <QGraphicsView>
29 #include "abstractgroupitem.h"
30 #include "abstractclipitem.h"
31 #include "kdenlivesettings.h"
32 #include "customtrackscene.h"
34 AbstractGroupItem::AbstractGroupItem(double fps): QGraphicsItemGroup(), m_fps(fps) {
35 setFlags(QGraphicsItem::ItemClipsToShape | QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
38 int AbstractGroupItem::type() const {
42 CustomTrackScene* AbstractGroupItem::projectScene() {
43 return static_cast <CustomTrackScene*>(scene());
47 QPolygonF AbstractGroupItem::groupShape(QPointF offset) {
48 QList<QGraphicsItem *> children = childItems();
50 for (int i = 0; i < children.count(); i++) {
51 if (children.at(i)->type() == AVWIDGET) {
52 QPolygonF r = QPolygonF(children.at(i)->sceneBoundingRect());
53 path = path.united(r);
56 path.translate(offset);
60 void AbstractGroupItem::addItem(QGraphicsItem * item) {
65 void AbstractGroupItem::fixItemRect() {
66 QPointF start = boundingRect().topLeft();
67 if (start != QPointF(0, 0)) {
68 translate(0 - start.x(), 0 - start.y());
74 void AbstractGroupItem::paint(QPainter *p, const QStyleOptionGraphicsItem *, QWidget *) {
75 p->fillRect(boundingRect(), QColor(200, 100, 100, 100));
79 QVariant AbstractGroupItem::itemChange(GraphicsItemChange change, const QVariant &value) {
80 if (change == ItemPositionChange && scene()) {
81 // calculate new position.
82 QPointF newPos = value.toPointF();
83 QPointF start = sceneBoundingRect().topLeft();
84 int posx = start.x() + newPos.x(); //projectScene()->getSnapPointForPos(start.x() + sc.x(), KdenliveSettings::snaptopoints());
86 int startTrack = (start.y()) / KdenliveSettings::trackheight();
87 int newTrack = (start.y() + newPos.y()) / KdenliveSettings::trackheight();
88 //kDebug()<<"// GROUP NEW TRACK: "<<newTrack<<", START TRACK: "<<startTrack;
89 newTrack = qMin(newTrack, projectScene()->tracksCount() - 1);
90 newTrack = qMax(newTrack, 0);
91 newPos.setY((int)((newTrack - startTrack) * KdenliveSettings::trackheight()));
93 //kDebug() << "------------------------------------GRUOP MOVE";
95 if (start.x() + newPos.x() - pos().x() < 0) {
96 // If group goes below 0, adjust position to 0
97 return QPointF(pos().x() - start.x(), pos().y());
100 QPolygonF sceneShape = groupShape(newPos - pos());
101 QList<QGraphicsItem*> collindingItems = scene()->items(sceneShape, Qt::IntersectsItemShape);
102 QList<QGraphicsItem *> children = childItems();
103 for (int i = 0; i < children.count(); i++) {
104 collindingItems.removeAll(children.at(i));
106 if (collindingItems.isEmpty()) return newPos;
108 for (int i = 0; i < collindingItems.count(); i++) {
109 QGraphicsItem *collision = collindingItems.at(i);
110 if (collision->type() == AVWIDGET) {
113 //TODO: improve movement when collision happens
114 /*if (startTrack != newTrack) return pos();
115 if (collision->pos().x() > pos().x()) {
116 return QPointF(collision->sceneBoundingRect().x() - sceneBoundingRect().width() + pos().x() - start.x() - 1, newPos.y());
123 //else posx -= startx;
124 //posx = qMax(posx, 0);
126 //kDebug()<<"Y POS: "<<start.y() + newPos.y()<<"SCN MP: "<<sc;
127 /*int newTrack = (start.y() + newPos.y()) / KdenliveSettings::trackheight();
128 int oldTrack = (start.y() + pos().y()) / KdenliveSettings::trackheight();
129 newPos.setY((newTrack) * KdenliveSettings::trackheight() - start.y() + 1);*/
132 //if (start.y() + newPos.y() < 1) newTrack = oldTrack;
136 // Only one clip is moving
138 QList<QGraphicsItem*> items = scene()->items(sceneShape, Qt::IntersectsItemShape);
141 if (!items.isEmpty()) {
142 for (int i = 0; i < items.count(); i++) {
143 if (items.at(i)->type() == AVWIDGET) {
145 //kDebug()<<"/// COLLISION WITH ITEM: "<<items.at(i)->sceneBoundingRect();
147 QPointF otherPos = items.at(i)->pos();
148 if ((int) otherPos.y() != (int) pos().y()) return pos();
149 if (pos().x() < otherPos.x()) {
150 // move clip just before colliding clip
151 int npos = (static_cast < AbstractClipItem* >(items.at(i))->startPos()).frames(m_fps) - sceneBoundingRect().width();
154 // get pos just after colliding clip
155 int npos = static_cast < AbstractClipItem* >(items.at(i))->endPos().frames(m_fps);
164 return QGraphicsItem::itemChange(change, value);