]> git.sesse.net Git - kdenlive/blob - src/abstractgroupitem.cpp
This will hopefully fix most clip & group move issues. Should solve points b) and...
[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 <QStyleOptionGraphicsItem>
25 #include <QToolTip>
26
27 #include <KDebug>
28 #include <KLocale>
29
30 #include "abstractgroupitem.h"
31 #include "abstractclipitem.h"
32 #include "kdenlivesettings.h"
33 #include "customtrackscene.h"
34
35 AbstractGroupItem::AbstractGroupItem(double fps): QGraphicsItemGroup(), m_fps(fps) {
36     setZValue(2);
37     setFlags(QGraphicsItem::ItemClipsToShape | QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
38 }
39
40 int AbstractGroupItem::type() const {
41     return GROUPWIDGET;
42 }
43
44 const int AbstractGroupItem::track() const {
45     return (int)(scenePos().y() / KdenliveSettings::trackheight());
46 }
47
48 CustomTrackScene* AbstractGroupItem::projectScene() {
49     if (scene()) return static_cast <CustomTrackScene*>(scene());
50     return NULL;
51 }
52
53 QPainterPath AbstractGroupItem::groupShape(QPointF offset) {
54     QPainterPath path;
55     QList<QGraphicsItem *> children = childItems();
56     for (int i = 0; i < children.count(); i++) {
57         if (children.at(i)->type() == AVWIDGET) {
58             QRectF r(children.at(i)->sceneBoundingRect());
59             r.translate(offset);
60             path.addRect(r);
61         }
62     }
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 *option, QWidget *) {
81     p->fillRect(option->exposedRect, 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         QPointF start = pos();
95         int startTrack = (start.y() + trackHeight / 2) / trackHeight;
96         int newTrack = (newPos.y()) / trackHeight;
97         //kDebug()<<"// GROUP NEW T:"<<newTrack<<",START T:"<<startTrack<<",MAX:"<<projectScene()->tracksCount() - 1;
98         newTrack = qMin(newTrack, projectScene()->tracksCount() - (int)(boundingRect().height() + 5) / trackHeight);
99         newTrack = qMax(newTrack, 0);
100
101         // Check if top item is a clip or a transition
102         int offset = 0;
103         int topTrack = -1;
104         QList<QGraphicsItem *> children = childItems();
105         for (int i = 0; i < children.count(); i++) {
106             int currentTrack = (int)(children.at(i)->scenePos().y() / trackHeight);
107             if (children.at(i)->type() == AVWIDGET) {
108                 if (topTrack == -1 || currentTrack <= topTrack) {
109                     offset = 0;
110                     topTrack = currentTrack;
111                 }
112             } else if (children.at(i)->type() == TRANSITIONWIDGET) {
113                 if (topTrack == -1 || currentTrack < topTrack) {
114                     offset = (int)(trackHeight / 3 * 2 - 1);
115                     topTrack = currentTrack;
116                 }
117             }
118         }
119         newPos.setY((int)((newTrack) * trackHeight) + offset);
120         if (newPos == start) return start;
121
122         if (newPos.x() < 0) {
123             // If group goes below 0, adjust position to 0
124             return QPointF(pos().x() - start.x(), pos().y());
125         }
126
127         QPainterPath shape = groupShape(newPos - pos());
128         QList<QGraphicsItem*> collindingItems = scene()->items(shape, Qt::IntersectsItemShape);
129         for (int i = 0; i < children.count(); i++) {
130             collindingItems.removeAll(children.at(i));
131         }
132         if (collindingItems.isEmpty()) return newPos;
133         else {
134             bool forwardMove = newPos.x() > start.x();
135             int offset = 0;
136             const double width = sceneBoundingRect().width() + 1;
137             for (int i = 0; i < collindingItems.count(); i++) {
138                 QGraphicsItem *collision = collindingItems.at(i);
139                 if (collision->type() == AVWIDGET) {
140                     // Collision
141                     //kDebug()<<"// COLLISION WIT:"<<collision->sceneBoundingRect();
142                     if (newPos.y() != start.y()) {
143                         // Track change results in collision, restore original position
144                         return start;
145                     }
146                     AbstractClipItem *item = static_cast <AbstractClipItem *>(collision);
147                     if (forwardMove) {
148                         // Moving forward, determine best pos
149                         QPainterPath clipPath;
150                         clipPath.addRect(item->sceneBoundingRect());
151                         QPainterPath res = shape.intersected(clipPath);
152                         offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
153                     } else {
154                         // Moving backward, determine best pos
155                         QPainterPath clipPath;
156                         clipPath.addRect(item->sceneBoundingRect());
157                         QPainterPath res = shape.intersected(clipPath);
158                         offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
159                     }
160                 }
161             }
162             if (offset > 0) {
163                 if (forwardMove) {
164                     newPos.setX(newPos.x() - offset);
165                 } else {
166                     newPos.setX(newPos.x() + offset);
167                 }
168                 // If there is still a collision after our position adjust, restore original pos
169                 collindingItems = scene()->items(groupShape(newPos - pos()), Qt::IntersectsItemShape);
170                 for (int i = 0; i < children.count(); i++) {
171                     collindingItems.removeAll(children.at(i));
172                 }
173                 for (int i = 0; i < collindingItems.count(); i++)
174                     if (collindingItems.at(i)->type() == AVWIDGET) return pos();
175             }
176             return newPos;
177         }
178     }
179     return QGraphicsItem::itemChange(change, value);
180 }
181