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