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