]> git.sesse.net Git - kdenlive/blob - src/abstractgroupitem.cpp
Fix timeline corruption on group move and spacer tool
[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 #include <QGraphicsSceneMouseEvent>
34
35 AbstractGroupItem::AbstractGroupItem(double /* fps */) :
36         QObject(),
37         QGraphicsItemGroup()
38 {
39     setZValue(1);
40     setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
41     setAcceptDrops(true);
42 }
43
44 int AbstractGroupItem::type() const
45 {
46     return GROUPWIDGET;
47 }
48
49 int AbstractGroupItem::track() const
50 {
51     return (int)(scenePos().y() / KdenliveSettings::trackheight());
52 }
53
54 CustomTrackScene* AbstractGroupItem::projectScene()
55 {
56     if (scene()) return static_cast <CustomTrackScene*>(scene());
57     return NULL;
58 }
59
60 QPainterPath AbstractGroupItem::groupShape(QPointF offset)
61 {
62     QPainterPath path;
63     QList<QGraphicsItem *> children = childItems();
64     for (int i = 0; i < children.count(); i++) {
65         if (children.at(i)->type() == AVWIDGET) {
66             QRectF r(children.at(i)->sceneBoundingRect());
67             r.translate(offset);
68             path.addRect(r);
69         }
70     }
71     return path;
72 }
73
74 void AbstractGroupItem::addItem(QGraphicsItem * item)
75 {
76     addToGroup(item);
77     //fixItemRect();
78 }
79
80 void AbstractGroupItem::fixItemRect()
81 {
82     QPointF start = boundingRect().topLeft();
83     if (start != QPointF(0, 0)) {
84         translate(0 - start.x(), 0 - start.y());
85         setPos(start);
86     }
87 }
88
89 /*ItemInfo AbstractGroupItem::info() const {
90     ItemInfo itemInfo;
91     itemInfo.startPos = m_startPos;
92     itemInfo.track = m_track;
93     return itemInfo;
94 }*/
95
96 // virtual
97 void AbstractGroupItem::paint(QPainter *p, const QStyleOptionGraphicsItem *option, QWidget *)
98 {
99     const double scale = option->matrix.m11();
100     QColor bgcolor(100, 100, 200, 100);
101     QRectF bound = option->exposedRect.adjusted(0, 0, 1, 1);
102     p->setClipRect(bound);
103     p->fillRect(option->exposedRect, bgcolor);
104     QPen pen = p->pen();
105     pen.setColor(QColor(200, 90, 90));
106     pen.setStyle(Qt::DashLine);
107     pen.setWidthF(0.0);
108     //pen.setCosmetic(true);
109     p->setPen(pen);
110     p->drawRect(boundingRect().adjusted(0, 0, - 1 / scale, 0));
111 }
112
113 //virtual
114 QVariant AbstractGroupItem::itemChange(GraphicsItemChange change, const QVariant &value)
115 {
116     if (change == ItemPositionChange && scene() && parentItem() == 0) {
117         // calculate new position.
118         const int trackHeight = KdenliveSettings::trackheight();
119         QPointF start = sceneBoundingRect().topLeft();
120         QPointF newPos = value.toPointF();
121         //kDebug()<<"REAL:"<<start.x()<<", PROPOSED:"<<(int)(start.x() - pos().x() + newPos.x());
122         int xpos = projectScene()->getSnapPointForPos((int)(start.x() + newPos.x() - pos().x()), KdenliveSettings::snaptopoints());
123
124         xpos = qMax(xpos, 0);
125         //kDebug()<<"GRP XPOS:"<<xpos<<", START:"<<start.x()<<",NEW:"<<newPos.x()<<"; SCENE:"<<scenePos().x()<<",POS:"<<pos().x();
126         newPos.setX((int)(pos().x() + xpos - (int) start.x()));
127
128         //int startTrack = (start.y() + trackHeight / 2) / trackHeight;
129
130         int realTrack = (start.y() + newPos.y() - pos().y()) / trackHeight;
131         int proposedTrack = newPos.y() / trackHeight;
132
133         int correctedTrack = qMin(realTrack, projectScene()->tracksCount() - (int)(boundingRect().height() + 5) / trackHeight);
134         correctedTrack = qMax(correctedTrack, 0);
135
136         proposedTrack += (correctedTrack - realTrack);
137
138         // Check if top item is a clip or a transition
139         int offset = 0;
140         int topTrack = -1;
141         QList<QGraphicsItem *> children = childItems();
142         for (int i = 0; i < children.count(); i++) {
143             int currentTrack = (int)(children.at(i)->scenePos().y() / trackHeight);
144             if (children.at(i)->type() == AVWIDGET) {
145                 if (topTrack == -1 || currentTrack <= topTrack) {
146                     offset = 0;
147                     topTrack = currentTrack;
148                 }
149             } else if (children.at(i)->type() == TRANSITIONWIDGET) {
150                 if (topTrack == -1 || currentTrack < topTrack) {
151                     offset = (int)(trackHeight / 3 * 2 - 1);
152                     topTrack = currentTrack;
153                 }
154             } else if (children.at(i)->type() == GROUPWIDGET) {
155                 QList<QGraphicsItem *> subchildren = children.at(i)->childItems();
156                 bool clipGroup = false;
157                 for (int j = 0; j < subchildren.count(); j++) {
158                     if (subchildren.at(j)->type() == AVWIDGET) {
159                         clipGroup = true;
160                         break;
161                     }
162                 }
163                 if (clipGroup) {
164                     if (topTrack == -1 || currentTrack <= topTrack) {
165                         offset = 0;
166                         topTrack = currentTrack;
167                     }
168                 } else {
169                     if (topTrack == -1 || currentTrack < topTrack) {
170                         offset = (int)(trackHeight / 3 * 2 - 1);
171                         topTrack = currentTrack;
172                     }
173                 }
174             }
175         }
176         newPos.setY((int)((proposedTrack) * trackHeight) + offset);
177         //if (newPos == start) return start;
178
179         /*if (newPos.x() < 0) {
180             // If group goes below 0, adjust position to 0
181             return QPointF(pos().x() - start.x(), pos().y());
182         }*/
183
184         QPainterPath shape = groupShape(newPos - pos());
185         QList<QGraphicsItem*> collindingItems = scene()->items(shape, Qt::IntersectsItemShape);
186         for (int i = 0; i < children.count(); i++) {
187             collindingItems.removeAll(children.at(i));
188         }
189
190         if (collindingItems.isEmpty()) return newPos;
191         else {
192             bool forwardMove = xpos > start.x();
193             int offset = 0;
194             for (int i = 0; i < collindingItems.count(); i++) {
195                 QGraphicsItem *collision = collindingItems.at(i);
196                 if (collision->type() == AVWIDGET) {
197                     // Collision
198                     if (newPos.y() != pos().y()) {
199                         // Track change results in collision, restore original position
200                         return pos();
201                     }
202                     AbstractClipItem *item = static_cast <AbstractClipItem *>(collision);
203                     if (forwardMove) {
204                         // Moving forward, determine best pos
205                         QPainterPath clipPath;
206                         clipPath.addRect(item->sceneBoundingRect());
207                         QPainterPath res = shape.intersected(clipPath);
208                         offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
209                     } else {
210                         // Moving backward, determine best pos
211                         QPainterPath clipPath;
212                         clipPath.addRect(item->sceneBoundingRect());
213                         QPainterPath res = shape.intersected(clipPath);
214                         offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
215                     }
216                 }
217             }
218             if (offset > 0) {
219                 if (forwardMove) {
220                     newPos.setX(newPos.x() - offset);
221                 } else {
222                     newPos.setX(newPos.x() + offset);
223                 }
224                 // If there is still a collision after our position adjust, restore original pos
225                 collindingItems = scene()->items(groupShape(newPos - pos()), Qt::IntersectsItemShape);
226                 for (int i = 0; i < children.count(); i++) {
227                     collindingItems.removeAll(children.at(i));
228                 }
229                 for (int i = 0; i < collindingItems.count(); i++)
230                     if (collindingItems.at(i)->type() == AVWIDGET) return pos();
231             }
232             return newPos;
233         }
234     }
235     return QGraphicsItemGroup::itemChange(change, value);
236 }
237
238 //virtual
239 void AbstractGroupItem::dropEvent(QGraphicsSceneDragDropEvent * event)
240 {
241     QString effects = QString(event->mimeData()->data("kdenlive/effectslist"));
242     QDomDocument doc;
243     doc.setContent(effects, true);
244     QDomElement e = doc.documentElement();
245     CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
246     if (view) view->slotAddGroupEffect(e, this);
247 }
248
249 //virtual
250 void AbstractGroupItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
251 {
252     event->setAccepted(event->mimeData()->hasFormat("kdenlive/effectslist"));
253 }
254
255 void AbstractGroupItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
256 {
257     Q_UNUSED(event);
258 }
259
260 // virtual
261 void AbstractGroupItem::mousePressEvent(QGraphicsSceneMouseEvent * event)
262 {
263     if (event->modifiers() & Qt::ShiftModifier) {
264         // User want to do a rectangle selection, so ignore the event to pass it to the view
265         event->ignore();
266     } else QGraphicsItem::mousePressEvent(event);
267 }