]> git.sesse.net Git - kdenlive/blob - src/abstractgroupitem.cpp
Fix crash with copied clips
[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     setFlags(QGraphicsItem::ItemClipsToShape | QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
36 }
37
38 int AbstractGroupItem::type() const {
39     return GROUPWIDGET;
40 }
41
42 CustomTrackScene* AbstractGroupItem::projectScene() {
43     if (scene()) return static_cast <CustomTrackScene*>(scene());
44     return NULL;
45 }
46
47
48 QPolygonF AbstractGroupItem::groupShape(QPointF offset) {
49     QList<QGraphicsItem *> children = childItems();
50     QPolygonF path;
51     for (int i = 0; i < children.count(); i++) {
52         if (children.at(i)->type() == AVWIDGET) {
53             QPolygonF r = QPolygonF(children.at(i)->sceneBoundingRect());
54             path = path.united(r);
55         }
56     }
57     path.translate(offset);
58     return path;
59 }
60
61 void AbstractGroupItem::addItem(QGraphicsItem * item) {
62     addToGroup(item);
63     //fixItemRect();
64 }
65
66 void AbstractGroupItem::fixItemRect() {
67     QPointF start = boundingRect().topLeft();
68     if (start != QPointF(0, 0)) {
69         translate(0 - start.x(), 0 - start.y());
70         setPos(start);
71     }
72 }
73
74 // virtual
75 void AbstractGroupItem::paint(QPainter *p, const QStyleOptionGraphicsItem *, QWidget *) {
76     p->fillRect(boundingRect(), QColor(200, 100, 100, 100));
77 }
78
79 //virtual
80 QVariant AbstractGroupItem::itemChange(GraphicsItemChange change, const QVariant &value) {
81     if (change == ItemPositionChange && scene()) {
82         // calculate new position.
83         QPointF newPos = value.toPointF();
84         QPointF start = sceneBoundingRect().topLeft();
85         int posx = start.x() + newPos.x(); //projectScene()->getSnapPointForPos(start.x() + sc.x(), KdenliveSettings::snaptopoints());
86
87         int startTrack = (start.y()) / KdenliveSettings::trackheight();
88         int newTrack = (start.y() + newPos.y()) / KdenliveSettings::trackheight();
89         //kDebug()<<"// GROUP NEW TRACK: "<<newTrack<<", START TRACK: "<<startTrack;
90         newTrack = qMin(newTrack, projectScene()->tracksCount() - 1);
91         newTrack = qMax(newTrack, 0);
92         newPos.setY((int)((newTrack - startTrack) * KdenliveSettings::trackheight()));
93
94         //kDebug() << "------------------------------------GRUOP MOVE";
95
96         if (start.x() + newPos.x() - pos().x() < 0) {
97             // If group goes below 0, adjust position to 0
98             return QPointF(pos().x() - start.x(), pos().y());
99         }
100
101         QPolygonF sceneShape = groupShape(newPos - pos());
102         QList<QGraphicsItem*> collindingItems = scene()->items(sceneShape, Qt::IntersectsItemShape);
103         QList<QGraphicsItem *> children = childItems();
104         for (int i = 0; i < children.count(); i++) {
105             collindingItems.removeAll(children.at(i));
106         }
107         if (collindingItems.isEmpty()) return newPos;
108         else {
109             for (int i = 0; i < collindingItems.count(); i++) {
110                 QGraphicsItem *collision = collindingItems.at(i);
111                 if (collision->type() == AVWIDGET) {
112                     // Collision
113                     return pos();
114                     //TODO: improve movement when collision happens
115                     /*if (startTrack != newTrack) return pos();
116                     if (collision->pos().x() > pos().x()) {
117                     return QPointF(collision->sceneBoundingRect().x() - sceneBoundingRect().width() + pos().x() - start.x() - 1, newPos.y());
118                     }*/
119                 }
120             }
121             return newPos;
122         }
123
124         //else posx -= startx;
125         //posx = qMax(posx, 0);
126         //newPos.setX(posx);
127         //kDebug()<<"Y POS: "<<start.y() + newPos.y()<<"SCN MP: "<<sc;
128         /*int newTrack = (start.y() + newPos.y()) / KdenliveSettings::trackheight();
129         int oldTrack = (start.y() + pos().y()) / KdenliveSettings::trackheight();
130         newPos.setY((newTrack) * KdenliveSettings::trackheight() - start.y() + 1);*/
131
132
133         //if (start.y() + newPos.y() < 1)  newTrack = oldTrack;
134
135         return newPos;
136
137         // Only one clip is moving
138
139         QList<QGraphicsItem*> items = scene()->items(sceneShape, Qt::IntersectsItemShape);
140
141
142         if (!items.isEmpty()) {
143             for (int i = 0; i < items.count(); i++) {
144                 if (items.at(i)->type() == AVWIDGET) {
145                     // Collision!
146                     //kDebug()<<"/// COLLISION WITH ITEM: "<<items.at(i)->sceneBoundingRect();
147                     return pos();
148                     QPointF otherPos = items.at(i)->pos();
149                     if ((int) otherPos.y() != (int) pos().y()) return pos();
150                     if (pos().x() < otherPos.x()) {
151                         // move clip just before colliding clip
152                         int npos = (static_cast < AbstractClipItem* >(items.at(i))->startPos()).frames(m_fps) - sceneBoundingRect().width();
153                         newPos.setX(npos);
154                     } else {
155                         // get pos just after colliding clip
156                         int npos = static_cast < AbstractClipItem* >(items.at(i))->endPos().frames(m_fps);
157                         newPos.setX(npos);
158                     }
159                     return newPos;
160                 }
161             }
162         }
163         return newPos;
164     }
165     return QGraphicsItem::itemChange(change, value);
166 }
167