]> git.sesse.net Git - kdenlive/blob - src/abstractgroupitem.cpp
initial support for clip grouping (still a bit buggy)
[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 #include "customtrackview.h"
26
27 #include <KDebug>
28
29 #include <QPainter>
30 #include <QStyleOptionGraphicsItem>
31 #include <QDomDocument>
32 #include <QMimeData>
33
34
35 AbstractGroupItem::AbstractGroupItem(double fps): QGraphicsItemGroup(), m_fps(fps) {
36     setZValue(2);
37     setFlags(QGraphicsItem::ItemClipsToShape | QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
38     setAcceptDrops(true);
39 }
40
41 int AbstractGroupItem::type() const {
42     return GROUPWIDGET;
43 }
44
45 int AbstractGroupItem::track() const {
46     return (int)(scenePos().y() / KdenliveSettings::trackheight());
47 }
48
49 CustomTrackScene* AbstractGroupItem::projectScene() {
50     if (scene()) return static_cast <CustomTrackScene*>(scene());
51     return NULL;
52 }
53
54 QPainterPath AbstractGroupItem::groupShape(QPointF offset) {
55     QPainterPath path;
56     QList<QGraphicsItem *> children = childItems();
57     for (int i = 0; i < children.count(); i++) {
58         if (children.at(i)->type() == AVWIDGET) {
59             QRectF r(children.at(i)->sceneBoundingRect());
60             r.translate(offset);
61             path.addRect(r);
62         }
63     }
64     return path;
65 }
66
67 void AbstractGroupItem::addItem(QGraphicsItem * item) {
68     addToGroup(item);
69     //fixItemRect();
70 }
71
72 void AbstractGroupItem::fixItemRect() {
73     QPointF start = boundingRect().topLeft();
74     if (start != QPointF(0, 0)) {
75         translate(0 - start.x(), 0 - start.y());
76         setPos(start);
77     }
78 }
79
80 /*ItemInfo AbstractGroupItem::info() const {
81     ItemInfo itemInfo;
82     itemInfo.startPos = m_startPos;
83     itemInfo.track = m_track;
84     return itemInfo;
85 }*/
86
87 // virtual
88 void AbstractGroupItem::paint(QPainter *p, const QStyleOptionGraphicsItem *option, QWidget *) {
89     const double scale = option->matrix.m11();
90     QRect clipRect = option->exposedRect.toRect();
91     clipRect.adjust(0, 0, 1 / scale + 0.5, 1);
92     p->fillRect(option->exposedRect, QColor(100, 100, 200, 100));
93     p->setClipRect(clipRect);
94     QPen pen = p->pen();
95     pen.setColor(QColor(200, 90, 90));
96     pen.setStyle(Qt::DashLine);
97     p->setPen(pen);
98     p->drawRect(boundingRect());
99 }
100
101 //virtual
102 QVariant AbstractGroupItem::itemChange(GraphicsItemChange change, const QVariant &value) {
103     if (change == ItemPositionChange && scene()) {
104         // calculate new position.
105         const int trackHeight = KdenliveSettings::trackheight();
106         QPointF newPos = value.toPointF();
107         int xpos = projectScene()->getSnapPointForPos((int) newPos.x(), KdenliveSettings::snaptopoints());
108         xpos = qMax(xpos, 0);
109         newPos.setX(xpos);
110         QPointF start = pos();
111         kDebug() << "// GRP MOVE, NEW:" << newPos << ", CURR:" << start;
112         //int startTrack = (start.y() + trackHeight / 2) / trackHeight;
113         int newTrack = (newPos.y()) / trackHeight;
114
115         //kDebug()<<"// GROUP NEW T:"<<newTrack<<",START T:"<<startTrack<<",MAX:"<<projectScene()->tracksCount() - 1;
116         newTrack = qMin(newTrack, projectScene()->tracksCount() - (int)(boundingRect().height() + 5) / trackHeight);
117         newTrack = qMax(newTrack, 0);
118
119         // Check if top item is a clip or a transition
120         int offset = 0;
121         int topTrack = -1;
122         kDebug() << "// CHG GRP 1";
123         QList<QGraphicsItem *> children = childItems();
124         for (int i = 0; i < children.count(); i++) {
125             int currentTrack = (int)(children.at(i)->scenePos().y() / trackHeight);
126             if (children.at(i)->type() == AVWIDGET) {
127                 if (topTrack == -1 || currentTrack <= topTrack) {
128                     offset = 0;
129                     topTrack = currentTrack;
130                 }
131             } else if (children.at(i)->type() == TRANSITIONWIDGET) {
132                 if (topTrack == -1 || currentTrack < topTrack) {
133                     offset = (int)(trackHeight / 3 * 2 - 1);
134                     topTrack = currentTrack;
135                 }
136             }
137         }
138         kDebug() << "// CHG GRP 2";
139         newPos.setY((int)((newTrack) * trackHeight) + offset);
140         if (newPos == start) return start;
141
142         if (newPos.x() < 0) {
143             // If group goes below 0, adjust position to 0
144             return QPointF(pos().x() - start.x(), pos().y());
145         }
146         kDebug() << "// CHG GRP 3";
147         QPainterPath shape = groupShape(newPos - pos());
148         QList<QGraphicsItem*> collindingItems = scene()->items(shape, Qt::IntersectsItemShape);
149         for (int i = 0; i < children.count(); i++) {
150             collindingItems.removeAll(children.at(i));
151         }
152
153         kDebug() << "// CHG GRP 4" << newPos;
154         if (collindingItems.isEmpty()) return newPos;
155         else {
156             bool forwardMove = newPos.x() > start.x();
157             int offset = 0;
158             kDebug() << "// CHG GRP 5";
159             for (int i = 0; i < collindingItems.count(); i++) {
160                 QGraphicsItem *collision = collindingItems.at(i);
161                 if (collision->type() == AVWIDGET) {
162                     // Collision
163                     //kDebug()<<"// COLLISION WIT:"<<collision->sceneBoundingRect();
164                     if (newPos.y() != start.y()) {
165                         // Track change results in collision, restore original position
166                         return start;
167                     }
168                     AbstractClipItem *item = static_cast <AbstractClipItem *>(collision);
169                     if (forwardMove) {
170                         // Moving forward, determine best pos
171                         QPainterPath clipPath;
172                         clipPath.addRect(item->sceneBoundingRect());
173                         QPainterPath res = shape.intersected(clipPath);
174                         offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
175                     } else {
176                         // Moving backward, determine best pos
177                         QPainterPath clipPath;
178                         clipPath.addRect(item->sceneBoundingRect());
179                         QPainterPath res = shape.intersected(clipPath);
180                         offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
181                     }
182                 }
183             }
184             if (offset > 0) {
185                 if (forwardMove) {
186                     newPos.setX(newPos.x() - offset);
187                 } else {
188                     newPos.setX(newPos.x() + offset);
189                 }
190                 // If there is still a collision after our position adjust, restore original pos
191                 collindingItems = scene()->items(groupShape(newPos - pos()), Qt::IntersectsItemShape);
192                 for (int i = 0; i < children.count(); i++) {
193                     collindingItems.removeAll(children.at(i));
194                 }
195                 for (int i = 0; i < collindingItems.count(); i++)
196                     if (collindingItems.at(i)->type() == AVWIDGET) return pos();
197             }
198             return newPos;
199         }
200     }
201     return QGraphicsItemGroup::itemChange(change, value);
202 }
203
204 //virtual
205 void AbstractGroupItem::dropEvent(QGraphicsSceneDragDropEvent * event) {
206     QString effects = QString(event->mimeData()->data("kdenlive/effectslist"));
207     QDomDocument doc;
208     doc.setContent(effects, true);
209     QDomElement e = doc.documentElement();
210     CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
211     if (view) view->slotAddGroupEffect(e, this);
212 }
213
214 //virtual
215 void AbstractGroupItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event) {
216     event->setAccepted(event->mimeData()->hasFormat("kdenlive/effectslist"));
217 }
218
219 void AbstractGroupItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event) {
220     Q_UNUSED(event);
221 }