]> git.sesse.net Git - kdenlive/blob - src/abstractgroupitem.cpp
Spacer should not move locked tracks
[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 #if QT_VERSION >= 0x040600
42     setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
43 #endif
44     setAcceptDrops(true);
45 }
46
47 int AbstractGroupItem::type() const
48 {
49     return GROUPWIDGET;
50 }
51
52 int AbstractGroupItem::track() const
53 {
54     return (int)(scenePos().y() / KdenliveSettings::trackheight());
55 }
56
57 void AbstractGroupItem::setItemLocked(bool locked)
58 {
59     if (locked) {
60         setSelected(false);
61         setFlag(QGraphicsItem::ItemIsMovable, false);
62         setFlag(QGraphicsItem::ItemIsSelectable, false);
63     } else {
64         setFlag(QGraphicsItem::ItemIsMovable, true);
65         setFlag(QGraphicsItem::ItemIsSelectable, true);
66     }
67 }
68
69 bool AbstractGroupItem::isItemLocked() const
70 {
71     return !(flags() & (QGraphicsItem::ItemIsSelectable));
72 }
73
74 CustomTrackScene* AbstractGroupItem::projectScene()
75 {
76     if (scene()) return static_cast <CustomTrackScene*>(scene());
77     return NULL;
78 }
79
80 QPainterPath AbstractGroupItem::clipGroupShape(QPointF offset) const
81 {
82     QPainterPath path;
83     QList<QGraphicsItem *> children = childItems();
84     for (int i = 0; i < children.count(); i++) {
85         if (children.at(i)->type() == AVWIDGET) {
86             QRectF r(children.at(i)->sceneBoundingRect());
87             r.translate(offset);
88             path.addRect(r);
89         }
90     }
91     return path;
92 }
93
94 QPainterPath AbstractGroupItem::transitionGroupShape(QPointF offset) const
95 {
96     QPainterPath path;
97     QList<QGraphicsItem *> children = childItems();
98     for (int i = 0; i < children.count(); i++) {
99         if (children.at(i)->type() == TRANSITIONWIDGET) {
100             QRectF r(children.at(i)->sceneBoundingRect());
101             r.translate(offset);
102             path.addRect(r);
103         }
104     }
105     return path;
106 }
107
108 void AbstractGroupItem::addItem(QGraphicsItem * item)
109 {
110     addToGroup(item);
111     //fixItemRect();
112 }
113
114 void AbstractGroupItem::fixItemRect()
115 {
116     QPointF start = boundingRect().topLeft();
117     if (start != QPointF(0, 0)) {
118         translate(0 - start.x(), 0 - start.y());
119         setPos(start);
120     }
121 }
122
123 /*ItemInfo AbstractGroupItem::info() const {
124     ItemInfo itemInfo;
125     itemInfo.startPos = m_startPos;
126     itemInfo.track = m_track;
127     return itemInfo;
128 }*/
129
130 // virtual
131 void AbstractGroupItem::paint(QPainter *p, const QStyleOptionGraphicsItem *option, QWidget *)
132 {
133     const double scale = option->matrix.m11();
134     QColor bgcolor(100, 100, 200, 100);
135     QRectF bound = option->exposedRect.adjusted(0, 0, 1, 1);
136     p->setClipRect(bound);
137     p->fillRect(option->exposedRect, bgcolor);
138     QPen pen = p->pen();
139     pen.setColor(QColor(200, 90, 90));
140     pen.setStyle(Qt::DashLine);
141     pen.setWidthF(0.0);
142     //pen.setCosmetic(true);
143     p->setPen(pen);
144     p->drawRect(boundingRect().adjusted(0, 0, - 1 / scale, 0));
145 }
146
147 //virtual
148 QVariant AbstractGroupItem::itemChange(GraphicsItemChange change, const QVariant &value)
149 {
150     if (change == ItemPositionChange && scene() && parentItem() == 0) {
151         // calculate new position.
152         const int trackHeight = KdenliveSettings::trackheight();
153         QPointF start = sceneBoundingRect().topLeft();
154         QPointF newPos = value.toPointF();
155         //kDebug()<<"REAL:"<<start.x()<<", PROPOSED:"<<(int)(start.x() - pos().x() + newPos.x());
156         int xpos = projectScene()->getSnapPointForPos((int)(start.x() + newPos.x() - pos().x()), KdenliveSettings::snaptopoints());
157
158         xpos = qMax(xpos, 0);
159         //kDebug()<<"GRP XPOS:"<<xpos<<", START:"<<start.x()<<",NEW:"<<newPos.x()<<"; SCENE:"<<scenePos().x()<<",POS:"<<pos().x();
160         newPos.setX((int)(pos().x() + xpos - (int) start.x()));
161
162         //int startTrack = (start.y() + trackHeight / 2) / trackHeight;
163
164         int realTrack = (start.y() + newPos.y() - pos().y()) / trackHeight;
165         int proposedTrack = newPos.y() / trackHeight;
166
167         int correctedTrack = qMin(realTrack, projectScene()->tracksCount() - (int)(boundingRect().height() + 5) / trackHeight);
168         correctedTrack = qMax(correctedTrack, 0);
169
170         proposedTrack += (correctedTrack - realTrack);
171
172         // Check if top item is a clip or a transition
173         int offset = 0;
174         int topTrack = -1;
175         QList<QGraphicsItem *> children = childItems();
176         for (int i = 0; i < children.count(); i++) {
177             int currentTrack = (int)(children.at(i)->scenePos().y() / trackHeight);
178             if (children.at(i)->type() == AVWIDGET) {
179                 if (topTrack == -1 || currentTrack <= topTrack) {
180                     offset = 0;
181                     topTrack = currentTrack;
182                 }
183             } else if (children.at(i)->type() == TRANSITIONWIDGET) {
184                 if (topTrack == -1 || currentTrack < topTrack) {
185                     offset = (int)(trackHeight / 3 * 2 - 1);
186                     topTrack = currentTrack;
187                 }
188             } else if (children.at(i)->type() == GROUPWIDGET) {
189                 QList<QGraphicsItem *> subchildren = children.at(i)->childItems();
190                 bool clipGroup = false;
191                 for (int j = 0; j < subchildren.count(); j++) {
192                     if (subchildren.at(j)->type() == AVWIDGET) {
193                         clipGroup = true;
194                         break;
195                     }
196                 }
197                 if (clipGroup) {
198                     if (topTrack == -1 || currentTrack <= topTrack) {
199                         offset = 0;
200                         topTrack = currentTrack;
201                     }
202                 } else {
203                     if (topTrack == -1 || currentTrack < topTrack) {
204                         offset = (int)(trackHeight / 3 * 2 - 1);
205                         topTrack = currentTrack;
206                     }
207                 }
208             }
209         }
210         newPos.setY((int)((proposedTrack) * trackHeight) + offset);
211         //if (newPos == start) return start;
212
213         /*if (newPos.x() < 0) {
214             // If group goes below 0, adjust position to 0
215             return QPointF(pos().x() - start.x(), pos().y());
216         }*/
217
218         QList<QGraphicsItem*> collindingItems;
219         QPainterPath shape;
220         if (projectScene()->editMode() == NORMALEDIT) {
221             shape = clipGroupShape(newPos - pos());
222             collindingItems = scene()->items(shape, Qt::IntersectsItemShape);
223             for (int i = 0; i < children.count(); i++) {
224                 collindingItems.removeAll(children.at(i));
225             }
226         }
227         if (!collindingItems.isEmpty()) {
228             bool forwardMove = xpos > start.x();
229             int offset = 0;
230             for (int i = 0; i < collindingItems.count(); i++) {
231                 QGraphicsItem *collision = collindingItems.at(i);
232                 if (collision->type() == AVWIDGET) {
233                     // Collision
234                     if (newPos.y() != pos().y()) {
235                         // Track change results in collision, restore original position
236                         return pos();
237                     }
238                     AbstractClipItem *item = static_cast <AbstractClipItem *>(collision);
239                     if (forwardMove) {
240                         // Moving forward, determine best pos
241                         QPainterPath clipPath;
242                         clipPath.addRect(item->sceneBoundingRect());
243                         QPainterPath res = shape.intersected(clipPath);
244                         offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
245                     } else {
246                         // Moving backward, determine best pos
247                         QPainterPath clipPath;
248                         clipPath.addRect(item->sceneBoundingRect());
249                         QPainterPath res = shape.intersected(clipPath);
250                         offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
251                     }
252                 }
253             }
254             if (offset > 0) {
255                 if (forwardMove) {
256                     newPos.setX(newPos.x() - offset);
257                 } else {
258                     newPos.setX(newPos.x() + offset);
259                 }
260                 // If there is still a collision after our position adjust, restore original pos
261                 collindingItems = scene()->items(clipGroupShape(newPos - pos()), Qt::IntersectsItemShape);
262                 for (int i = 0; i < children.count(); i++) {
263                     collindingItems.removeAll(children.at(i));
264                 }
265                 for (int i = 0; i < collindingItems.count(); i++)
266                     if (collindingItems.at(i)->type() == AVWIDGET) return pos();
267             }
268         }
269
270         if (projectScene()->editMode() == NORMALEDIT) {
271             shape = transitionGroupShape(newPos - pos());
272             collindingItems = scene()->items(shape, Qt::IntersectsItemShape);
273             for (int i = 0; i < children.count(); i++) {
274                 collindingItems.removeAll(children.at(i));
275             }
276         }
277         if (collindingItems.isEmpty()) return newPos;
278         else {
279             bool forwardMove = xpos > start.x();
280             int offset = 0;
281             for (int i = 0; i < collindingItems.count(); i++) {
282                 QGraphicsItem *collision = collindingItems.at(i);
283                 if (collision->type() == TRANSITIONWIDGET) {
284                     // Collision
285                     if (newPos.y() != pos().y()) {
286                         // Track change results in collision, restore original position
287                         return pos();
288                     }
289                     AbstractClipItem *item = static_cast <AbstractClipItem *>(collision);
290                     if (forwardMove) {
291                         // Moving forward, determine best pos
292                         QPainterPath clipPath;
293                         clipPath.addRect(item->sceneBoundingRect());
294                         QPainterPath res = shape.intersected(clipPath);
295                         offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
296                     } else {
297                         // Moving backward, determine best pos
298                         QPainterPath clipPath;
299                         clipPath.addRect(item->sceneBoundingRect());
300                         QPainterPath res = shape.intersected(clipPath);
301                         offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
302                     }
303                 }
304             }
305             if (offset > 0) {
306                 if (forwardMove) {
307                     newPos.setX(newPos.x() - offset);
308                 } else {
309                     newPos.setX(newPos.x() + offset);
310                 }
311                 // If there is still a collision after our position adjust, restore original pos
312                 collindingItems = scene()->items(transitionGroupShape(newPos - pos()), Qt::IntersectsItemShape);
313                 for (int i = 0; i < children.count(); i++) {
314                     collindingItems.removeAll(children.at(i));
315                 }
316                 for (int i = 0; i < collindingItems.count(); i++)
317                     if (collindingItems.at(i)->type() == TRANSITIONWIDGET) return pos();
318             }
319         }
320         return newPos;
321     }
322     return QGraphicsItemGroup::itemChange(change, value);
323 }
324
325 //virtual
326 void AbstractGroupItem::dropEvent(QGraphicsSceneDragDropEvent * event)
327 {
328     QString effects = QString(event->mimeData()->data("kdenlive/effectslist"));
329     QDomDocument doc;
330     doc.setContent(effects, true);
331     QDomElement e = doc.documentElement();
332     CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
333     if (view) view->slotAddGroupEffect(e, this);
334 }
335
336 //virtual
337 void AbstractGroupItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
338 {
339     event->setAccepted(event->mimeData()->hasFormat("kdenlive/effectslist"));
340 }
341
342 void AbstractGroupItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
343 {
344     Q_UNUSED(event);
345 }
346
347 // virtual
348 void AbstractGroupItem::mousePressEvent(QGraphicsSceneMouseEvent * event)
349 {
350     if (event->modifiers() & Qt::ShiftModifier) {
351         // User want to do a rectangle selection, so ignore the event to pass it to the view
352         event->ignore();
353     } else QGraphicsItem::mousePressEvent(event);
354 }