]> git.sesse.net Git - kdenlive/blob - src/abstractgroupitem.cpp
Krazy fixes: cleanup all headers
[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 "abstractgroupitem.h"
22 #include "abstractclipitem.h"
23 #include "kdenlivesettings.h"
24 #include "customtrackscene.h"
25
26 #include <KDebug>
27
28 #include <QPainter>
29 #include <QStyleOptionGraphicsItem>
30
31
32 AbstractGroupItem::AbstractGroupItem(double fps): QGraphicsItemGroup(), m_fps(fps) {
33     setZValue(2);
34     setFlags(QGraphicsItem::ItemClipsToShape | QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
35 }
36
37 int AbstractGroupItem::type() const {
38     return GROUPWIDGET;
39 }
40
41 const int AbstractGroupItem::track() const {
42     return (int)(scenePos().y() / KdenliveSettings::trackheight());
43 }
44
45 CustomTrackScene* AbstractGroupItem::projectScene() {
46     if (scene()) return static_cast <CustomTrackScene*>(scene());
47     return NULL;
48 }
49
50 QPainterPath AbstractGroupItem::groupShape(QPointF offset) {
51     QPainterPath path;
52     QList<QGraphicsItem *> children = childItems();
53     for (int i = 0; i < children.count(); i++) {
54         if (children.at(i)->type() == AVWIDGET) {
55             QRectF r(children.at(i)->sceneBoundingRect());
56             r.translate(offset);
57             path.addRect(r);
58         }
59     }
60     return path;
61 }
62
63 void AbstractGroupItem::addItem(QGraphicsItem * item) {
64     addToGroup(item);
65     //fixItemRect();
66 }
67
68 void AbstractGroupItem::fixItemRect() {
69     QPointF start = boundingRect().topLeft();
70     if (start != QPointF(0, 0)) {
71         translate(0 - start.x(), 0 - start.y());
72         setPos(start);
73     }
74 }
75
76 // virtual
77 void AbstractGroupItem::paint(QPainter *p, const QStyleOptionGraphicsItem *option, QWidget *) {
78     p->fillRect(option->exposedRect, QColor(200, 100, 100, 100));
79 }
80
81 //virtual
82 QVariant AbstractGroupItem::itemChange(GraphicsItemChange change, const QVariant &value) {
83     if (change == ItemPositionChange && scene()) {
84         // calculate new position.
85         const int trackHeight = KdenliveSettings::trackheight();
86         QPointF newPos = value.toPointF();
87         int xpos = projectScene()->getSnapPointForPos((int) newPos.x(), KdenliveSettings::snaptopoints());
88         xpos = qMax(xpos, 0);
89         newPos.setX(xpos);
90
91         QPointF start = pos();
92         int startTrack = (start.y() + trackHeight / 2) / trackHeight;
93         int newTrack = (newPos.y()) / trackHeight;
94         //kDebug()<<"// GROUP NEW T:"<<newTrack<<",START T:"<<startTrack<<",MAX:"<<projectScene()->tracksCount() - 1;
95         newTrack = qMin(newTrack, projectScene()->tracksCount() - (int)(boundingRect().height() + 5) / trackHeight);
96         newTrack = qMax(newTrack, 0);
97
98         // Check if top item is a clip or a transition
99         int offset = 0;
100         int topTrack = -1;
101         QList<QGraphicsItem *> children = childItems();
102         for (int i = 0; i < children.count(); i++) {
103             int currentTrack = (int)(children.at(i)->scenePos().y() / trackHeight);
104             if (children.at(i)->type() == AVWIDGET) {
105                 if (topTrack == -1 || currentTrack <= topTrack) {
106                     offset = 0;
107                     topTrack = currentTrack;
108                 }
109             } else if (children.at(i)->type() == TRANSITIONWIDGET) {
110                 if (topTrack == -1 || currentTrack < topTrack) {
111                     offset = (int)(trackHeight / 3 * 2 - 1);
112                     topTrack = currentTrack;
113                 }
114             }
115         }
116         newPos.setY((int)((newTrack) * trackHeight) + offset);
117         if (newPos == start) return start;
118
119         if (newPos.x() < 0) {
120             // If group goes below 0, adjust position to 0
121             return QPointF(pos().x() - start.x(), pos().y());
122         }
123
124         QPainterPath shape = groupShape(newPos - pos());
125         QList<QGraphicsItem*> collindingItems = scene()->items(shape, Qt::IntersectsItemShape);
126         for (int i = 0; i < children.count(); i++) {
127             collindingItems.removeAll(children.at(i));
128         }
129         if (collindingItems.isEmpty()) return newPos;
130         else {
131             bool forwardMove = newPos.x() > start.x();
132             int offset = 0;
133             const double width = sceneBoundingRect().width() + 1;
134             for (int i = 0; i < collindingItems.count(); i++) {
135                 QGraphicsItem *collision = collindingItems.at(i);
136                 if (collision->type() == AVWIDGET) {
137                     // Collision
138                     //kDebug()<<"// COLLISION WIT:"<<collision->sceneBoundingRect();
139                     if (newPos.y() != start.y()) {
140                         // Track change results in collision, restore original position
141                         return start;
142                     }
143                     AbstractClipItem *item = static_cast <AbstractClipItem *>(collision);
144                     if (forwardMove) {
145                         // Moving forward, determine best pos
146                         QPainterPath clipPath;
147                         clipPath.addRect(item->sceneBoundingRect());
148                         QPainterPath res = shape.intersected(clipPath);
149                         offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
150                     } else {
151                         // Moving backward, determine best pos
152                         QPainterPath clipPath;
153                         clipPath.addRect(item->sceneBoundingRect());
154                         QPainterPath res = shape.intersected(clipPath);
155                         offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
156                     }
157                 }
158             }
159             if (offset > 0) {
160                 if (forwardMove) {
161                     newPos.setX(newPos.x() - offset);
162                 } else {
163                     newPos.setX(newPos.x() + offset);
164                 }
165                 // If there is still a collision after our position adjust, restore original pos
166                 collindingItems = scene()->items(groupShape(newPos - pos()), Qt::IntersectsItemShape);
167                 for (int i = 0; i < children.count(); i++) {
168                     collindingItems.removeAll(children.at(i));
169                 }
170                 for (int i = 0; i < collindingItems.count(); i++)
171                     if (collindingItems.at(i)->type() == AVWIDGET) return pos();
172             }
173             return newPos;
174         }
175     }
176     return QGraphicsItemGroup::itemChange(change, value);
177 }
178