]> git.sesse.net Git - kdenlive/blob - src/abstractgroupitem.cpp
Should solve most problems with group move:
[kdenlive] / src / abstractgroupitem.cpp
1 /***************************************************************************
2  *   Copyright (C) 2008 by Marco Gittler (g.marco@freenet.de)              *
3  *   Copyright (C) 2008 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
4  *                                                                         *
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.                                   *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #include <QGraphicsScene>
22 #include <QGraphicsView>
23 #include <QScrollBar>
24 #include <QToolTip>
25
26 #include <KDebug>
27 #include <KLocale>
28
29 #include "abstractgroupitem.h"
30 #include "abstractclipitem.h"
31 #include "kdenlivesettings.h"
32 #include "customtrackscene.h"
33
34 AbstractGroupItem::AbstractGroupItem(double fps): QGraphicsItemGroup(), m_fps(fps) {
35     setZValue(2);
36     setFlags(QGraphicsItem::ItemClipsToShape | QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
37 }
38
39 int AbstractGroupItem::type() const {
40     return GROUPWIDGET;
41 }
42
43 const int AbstractGroupItem::track() const {
44     return (int)(scenePos().y() / KdenliveSettings::trackheight());
45 }
46
47 CustomTrackScene* AbstractGroupItem::projectScene() {
48     if (scene()) return static_cast <CustomTrackScene*>(scene());
49     return NULL;
50 }
51
52
53 QPolygonF AbstractGroupItem::groupShape(QPointF offset) {
54     QList<QGraphicsItem *> children = childItems();
55     QPolygonF path;
56     for (int i = 0; i < children.count(); i++) {
57         if (children.at(i)->type() == AVWIDGET) {
58             QPolygonF r = QPolygonF(children.at(i)->sceneBoundingRect());
59             path = path.united(r);
60         }
61     }
62     path.translate(offset);
63     return path;
64 }
65
66 void AbstractGroupItem::addItem(QGraphicsItem * item) {
67     addToGroup(item);
68     //fixItemRect();
69 }
70
71 void AbstractGroupItem::fixItemRect() {
72     QPointF start = boundingRect().topLeft();
73     if (start != QPointF(0, 0)) {
74         translate(0 - start.x(), 0 - start.y());
75         setPos(start);
76     }
77 }
78
79 // virtual
80 void AbstractGroupItem::paint(QPainter *p, const QStyleOptionGraphicsItem *, QWidget *) {
81     p->fillRect(boundingRect(), QColor(200, 100, 100, 100));
82 }
83
84 //virtual
85 QVariant AbstractGroupItem::itemChange(GraphicsItemChange change, const QVariant &value) {
86     if (change == ItemPositionChange && scene()) {
87         // calculate new position.
88         const int trackHeight = KdenliveSettings::trackheight();
89         QPointF newPos = value.toPointF();
90         int xpos = projectScene()->getSnapPointForPos((int) newPos.x(), KdenliveSettings::snaptopoints());
91         xpos = qMax(xpos, 0);
92         newPos.setX(xpos);
93
94
95         //kDebug()<<"// GRP MOVE: "<<pos().y()<<"->"<<newPos.y();
96         QPointF start = pos();//sceneBoundingRect().topLeft();
97         int posx = start.x() + newPos.x(); //projectScene()->getSnapPointForPos(start.x() + sc.x(), KdenliveSettings::snaptopoints());
98
99         int startTrack = (start.y() + trackHeight / 2) / trackHeight;
100         int newTrack = (newPos.y()) / trackHeight;
101         //kDebug()<<"// GROUP NEW T:"<<newTrack<<",START T:"<<startTrack<<",MAX:"<<projectScene()->tracksCount() - 1;
102         newTrack = qMin(newTrack, projectScene()->tracksCount() - (int)(boundingRect().height() + 5) / trackHeight);
103         newTrack = qMax(newTrack, 0);
104
105         // Check if top item is a clip or a transition
106         int offset = 0;
107         int topTrack = -1;
108         QList<QGraphicsItem *> children = childItems();
109         for (int i = 0; i < children.count(); i++) {
110             int currentTrack = (int)(children.at(i)->scenePos().y() / trackHeight);
111             if (children.at(i)->type() == AVWIDGET) {
112                 if (topTrack == -1 || currentTrack <= topTrack) {
113                     offset = 0;
114                     topTrack = currentTrack;
115                 }
116             } else if (children.at(i)->type() == TRANSITIONWIDGET) {
117                 if (topTrack == -1 || currentTrack < topTrack) {
118                     offset = (int)(trackHeight / 3 * 2 - 1);
119                     topTrack = currentTrack;
120                 }
121             }
122         }
123
124         newPos.setY((int)((newTrack) * trackHeight) + offset);
125
126         //kDebug() << "------------------------------------GRUOP MOVE";
127
128         if (start.x() + newPos.x() - pos().x() < 0) {
129             // If group goes below 0, adjust position to 0
130             return QPointF(pos().x() - start.x(), pos().y());
131         }
132
133         QPolygonF sceneShape = groupShape(newPos - pos());
134         QList<QGraphicsItem*> collindingItems = scene()->items(sceneShape, Qt::IntersectsItemShape);
135         for (int i = 0; i < children.count(); i++) {
136             collindingItems.removeAll(children.at(i));
137         }
138         if (collindingItems.isEmpty()) return newPos;
139         else {
140             for (int i = 0; i < collindingItems.count(); i++) {
141                 QGraphicsItem *collision = collindingItems.at(i);
142                 if (collision->type() == AVWIDGET) {
143                     // Collision
144                     return pos();
145                     //TODO: improve movement when collision happens
146                     /*if (startTrack != newTrack) return pos();
147                     if (collision->pos().x() > pos().x()) {
148                     return QPointF(collision->sceneBoundingRect().x() - sceneBoundingRect().width() + pos().x() - start.x() - 1, newPos.y());
149                     }*/
150                 }
151             }
152             return newPos;
153         }
154
155         //else posx -= startx;
156         //posx = qMax(posx, 0);
157         //newPos.setX(posx);
158         //kDebug()<<"Y POS: "<<start.y() + newPos.y()<<"SCN MP: "<<sc;
159         /*int newTrack = (start.y() + newPos.y()) / KdenliveSettings::trackheight();
160         int oldTrack = (start.y() + pos().y()) / KdenliveSettings::trackheight();
161         newPos.setY((newTrack) * KdenliveSettings::trackheight() - start.y() + 1);*/
162
163
164         //if (start.y() + newPos.y() < 1)  newTrack = oldTrack;
165
166         return newPos;
167
168         // Only one clip is moving
169
170         QList<QGraphicsItem*> items = scene()->items(sceneShape, Qt::IntersectsItemShape);
171
172
173         if (!items.isEmpty()) {
174             for (int i = 0; i < items.count(); i++) {
175                 if (items.at(i)->type() == AVWIDGET) {
176                     // Collision!
177                     //kDebug()<<"/// COLLISION WITH ITEM: "<<items.at(i)->sceneBoundingRect();
178                     return pos();
179                     QPointF otherPos = items.at(i)->pos();
180                     if ((int) otherPos.y() != (int) pos().y()) return pos();
181                     if (pos().x() < otherPos.x()) {
182                         // move clip just before colliding clip
183                         int npos = (static_cast < AbstractClipItem* >(items.at(i))->startPos()).frames(m_fps) - sceneBoundingRect().width();
184                         newPos.setX(npos);
185                     } else {
186                         // get pos just after colliding clip
187                         int npos = static_cast < AbstractClipItem* >(items.at(i))->endPos().frames(m_fps);
188                         newPos.setX(npos);
189                     }
190                     return newPos;
191                 }
192             }
193         }
194         return newPos;
195     }
196     return QGraphicsItem::itemChange(change, value);
197 }
198