]> git.sesse.net Git - kdenlive/blob - src/abstractgroupitem.cpp
Fix broken group move when dragging an empty group zone
[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
36 AbstractGroupItem::AbstractGroupItem(double /* fps */) :
37         QObject(),
38         QGraphicsItemGroup()
39 {
40     setZValue(1);
41     setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
42 #if QT_VERSION >= 0x040600
43     setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
44 #endif
45     setAcceptDrops(true);
46     m_resizeInfos = QList <ItemInfo>();
47 }
48
49 int AbstractGroupItem::type() const
50 {
51     return GROUPWIDGET;
52 }
53
54 int AbstractGroupItem::track() const
55 {
56     return (int)(scenePos().y() / KdenliveSettings::trackheight());
57 }
58
59 void AbstractGroupItem::setItemLocked(bool locked)
60 {
61     if (locked)
62         setSelected(false);
63
64     setFlag(QGraphicsItem::ItemIsMovable, !locked);
65     setFlag(QGraphicsItem::ItemIsSelectable, !locked);
66
67     foreach (QGraphicsItem *child, childItems())
68         ((AbstractClipItem *)child)->setItemLocked(locked);
69 }
70
71 bool AbstractGroupItem::isItemLocked() const
72 {
73     return !(flags() & (QGraphicsItem::ItemIsSelectable));
74 }
75
76 CustomTrackScene* AbstractGroupItem::projectScene()
77 {
78     if (scene()) return static_cast <CustomTrackScene*>(scene());
79     return NULL;
80 }
81
82 QPainterPath AbstractGroupItem::clipGroupShape(QPointF offset) const
83 {
84     return groupShape(AVWIDGET, offset);
85 }
86
87 QPainterPath AbstractGroupItem::transitionGroupShape(QPointF offset) const
88 {
89     return groupShape(TRANSITIONWIDGET, offset);
90 }
91
92 QPainterPath AbstractGroupItem::groupShape(GRAPHICSRECTITEM type, QPointF offset) const
93 {
94     QPainterPath path;
95     QList<QGraphicsItem *> children = childItems();
96     for (int i = 0; i < children.count(); i++) {
97         if (children.at(i)->type() == (int)type) {
98             QRectF r(children.at(i)->sceneBoundingRect());
99             r.translate(offset);
100             path.addRect(r);
101         } else if (children.at(i)->type() == GROUPWIDGET) {
102             QList<QGraphicsItem *> subchildren = children.at(i)->childItems();
103             for (int j = 0; j < subchildren.count(); j++) {
104                 if (subchildren.at(j)->type() == (int)type) {
105                     QRectF r(subchildren.at(j)->sceneBoundingRect());
106                     r.translate(offset);
107                     path.addRect(r);
108                 }
109             }
110         }
111     }
112     return path;
113 }
114
115 void AbstractGroupItem::addItem(QGraphicsItem * item)
116 {
117     addToGroup(item);
118     item->setFlag(QGraphicsItem::ItemIsMovable, false);
119 }
120
121 void AbstractGroupItem::fixItemRect()
122 {
123     QPointF start = boundingRect().topLeft();
124     if (start != QPointF(0, 0)) {
125         translate(0 - start.x(), 0 - start.y());
126         setPos(start);
127     }
128 }
129
130 /*ItemInfo AbstractGroupItem::info() const {
131     ItemInfo itemInfo;
132     itemInfo.startPos = m_startPos;
133     itemInfo.track = m_track;
134     return itemInfo;
135 }*/
136
137 // virtual
138 void AbstractGroupItem::paint(QPainter *p, const QStyleOptionGraphicsItem *option, QWidget *)
139 {
140     QColor bgcolor(100, 100, 200, 100);
141     QRectF bound = option->exposedRect.adjusted(0, 0, 1, 1);
142     p->setClipRect(bound);
143     const QRectF mapped = p->worldTransform().mapRect(option->exposedRect);
144     p->setWorldMatrixEnabled(false);
145     p->setBrush(bgcolor);
146     QPen pen = p->pen();
147     pen.setColor(QColor(200, 90, 90));
148     pen.setStyle(Qt::DashLine);
149     pen.setWidthF(0.0);
150     p->setPen(pen);
151     p->drawRoundedRect(mapped, 3, 3);
152 }
153
154 //virtual
155 QVariant AbstractGroupItem::itemChange(GraphicsItemChange change, const QVariant &value)
156 {
157     if (change == QGraphicsItem::ItemSelectedChange) {
158         if (value.toBool()) setZValue(10);
159         else setZValue(1);
160     }
161     if (change == ItemPositionChange && scene() && parentItem() == 0) {
162         // calculate new position.
163         const int trackHeight = KdenliveSettings::trackheight();
164         QPointF start = sceneBoundingRect().topLeft();
165         QPointF newPos = value.toPointF();
166         int xpos = projectScene()->getSnapPointForPos((int)(start.x() + newPos.x() - pos().x()), KdenliveSettings::snaptopoints());
167
168         xpos = qMax(xpos, 0);
169         //kDebug()<<"GRP XPOS:"<<xpos<<", START:"<<start.x()<<",NEW:"<<newPos.x()<<"; SCENE:"<<scenePos().x()<<",POS:"<<pos().x();
170         newPos.setX((int)(pos().x() + xpos - (int) start.x()));
171
172         int yOffset = property("y_absolute").toInt() + newPos.y();
173         int proposedTrack = yOffset / trackHeight;
174
175         // Check if top item is a clip or a transition
176         int offset = 0;
177         int topTrack = -1;
178         QList<QGraphicsItem *> children = childItems();
179         for (int i = 0; i < children.count(); i++) {
180             int currentTrack = 0;
181             if (children.at(i)->type() == AVWIDGET || children.at(i)->type() == TRANSITIONWIDGET) currentTrack = static_cast <AbstractClipItem*> (children.at(i))->track();
182             else if (children.at(i)->type() == GROUPWIDGET) currentTrack = static_cast <AbstractGroupItem*> (children.at(i))->track();
183             else continue;
184             if (children.at(i)->type() == AVWIDGET) {
185                 if (topTrack == -1 || currentTrack <= topTrack) {
186                     offset = 0;
187                     topTrack = currentTrack;
188                 }
189             } else if (children.at(i)->type() == TRANSITIONWIDGET) {
190                 if (topTrack == -1 || currentTrack < topTrack) {
191                     offset = (int)(trackHeight / 3 * 2 - 1);
192                     topTrack = currentTrack;
193                 }
194             } else if (children.at(i)->type() == GROUPWIDGET) {
195                 QList<QGraphicsItem *> subchildren = children.at(i)->childItems();
196                 bool clipGroup = false;
197                 for (int j = 0; j < subchildren.count(); j++) {
198                     if (subchildren.at(j)->type() == AVWIDGET) {
199                         clipGroup = true;
200                         break;
201                     }
202                 }
203                 if (clipGroup) {
204                     if (topTrack == -1 || currentTrack <= topTrack) {
205                         offset = 0;
206                         topTrack = currentTrack;
207                     }
208                 } else {
209                     if (topTrack == -1 || currentTrack < topTrack) {
210                         offset = (int)(trackHeight / 3 * 2 - 1);
211                         topTrack = currentTrack;
212                     }
213                 }
214             }
215         }
216         newPos.setY((int)((proposedTrack) * trackHeight) + offset);
217         //if (newPos == start) return start;
218
219         /*if (newPos.x() < 0) {
220             // If group goes below 0, adjust position to 0
221             return QPointF(pos().x() - start.x(), pos().y());
222         }*/
223
224         QList<QGraphicsItem*> collidingItems;
225         QPainterPath shape;
226         if (projectScene()->editMode() == NORMALEDIT) {
227             shape = clipGroupShape(newPos - pos());
228             collidingItems = scene()->items(shape, Qt::IntersectsItemShape);
229             collidingItems.removeAll(this);
230             for (int i = 0; i < children.count(); i++) {
231                 if (children.at(i)->type() == GROUPWIDGET) {
232                     QList<QGraphicsItem *> subchildren = children.at(i)->childItems();
233                     for (int j = 0; j < subchildren.count(); j++) {
234                         collidingItems.removeAll(subchildren.at(j));
235                     }
236                 }
237                 collidingItems.removeAll(children.at(i));
238             }
239         }
240         if (!collidingItems.isEmpty()) {
241             bool forwardMove = xpos > start.x();
242             int offset = 0;
243             for (int i = 0; i < collidingItems.count(); i++) {
244                 QGraphicsItem *collision = collidingItems.at(i);
245                 if (collision->type() == AVWIDGET) {
246                     // Collision
247                     if (newPos.y() != pos().y()) {
248                         // Track change results in collision, restore original position
249                         return pos();
250                     }
251                     AbstractClipItem *item = static_cast <AbstractClipItem *>(collision);
252                     if (forwardMove) {
253                         // Moving forward, determine best pos
254                         QPainterPath clipPath;
255                         clipPath.addRect(item->sceneBoundingRect());
256                         QPainterPath res = shape.intersected(clipPath);
257                         offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
258                     } else {
259                         // Moving backward, determine best pos
260                         QPainterPath clipPath;
261                         clipPath.addRect(item->sceneBoundingRect());
262                         QPainterPath res = shape.intersected(clipPath);
263                         offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
264                     }
265                 }
266             }
267             if (offset > 0) {
268                 if (forwardMove) {
269                     newPos.setX(newPos.x() - offset);
270                 } else {
271                     newPos.setX(newPos.x() + offset);
272                 }
273                 // If there is still a collision after our position adjust, restore original pos
274                 collidingItems = scene()->items(clipGroupShape(newPos - pos()), Qt::IntersectsItemShape);
275                 collidingItems.removeAll(this);
276                 for (int i = 0; i < children.count(); i++) {
277                     if (children.at(i)->type() == GROUPWIDGET) {
278                         QList<QGraphicsItem *> subchildren = children.at(i)->childItems();
279                         for (int j = 0; j < subchildren.count(); j++) {
280                             collidingItems.removeAll(subchildren.at(j));
281                         }
282                     }
283                     collidingItems.removeAll(children.at(i));
284                 }
285                 for (int i = 0; i < collidingItems.count(); i++)
286                     if (collidingItems.at(i)->type() == AVWIDGET) return pos();
287             }
288         }
289
290         if (projectScene()->editMode() == NORMALEDIT) {
291             shape = transitionGroupShape(newPos - pos());
292             collidingItems = scene()->items(shape, Qt::IntersectsItemShape);
293             collidingItems.removeAll(this);
294             for (int i = 0; i < children.count(); i++) {
295                 if (children.at(i)->type() == GROUPWIDGET) {
296                     QList<QGraphicsItem *> subchildren = children.at(i)->childItems();
297                     for (int j = 0; j < subchildren.count(); j++) {
298                         collidingItems.removeAll(subchildren.at(j));
299                     }
300                 }
301                 collidingItems.removeAll(children.at(i));
302             }
303         }
304         if (collidingItems.isEmpty()) return newPos;
305         else {
306             bool forwardMove = xpos > start.x();
307             int offset = 0;
308             for (int i = 0; i < collidingItems.count(); i++) {
309                 QGraphicsItem *collision = collidingItems.at(i);
310                 if (collision->type() == TRANSITIONWIDGET) {
311                     // Collision
312                     if (newPos.y() != pos().y()) {
313                         // Track change results in collision, restore original position
314                         return pos();
315                     }
316                     AbstractClipItem *item = static_cast <AbstractClipItem *>(collision);
317                     if (forwardMove) {
318                         // Moving forward, determine best pos
319                         QPainterPath clipPath;
320                         clipPath.addRect(item->sceneBoundingRect());
321                         QPainterPath res = shape.intersected(clipPath);
322                         offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
323                     } else {
324                         // Moving backward, determine best pos
325                         QPainterPath clipPath;
326                         clipPath.addRect(item->sceneBoundingRect());
327                         QPainterPath res = shape.intersected(clipPath);
328                         offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
329                     }
330                 }
331             }
332             if (offset > 0) {
333                 if (forwardMove) {
334                     newPos.setX(newPos.x() - offset);
335                 } else {
336                     newPos.setX(newPos.x() + offset);
337                 }
338                 // If there is still a collision after our position adjust, restore original pos
339                 collidingItems = scene()->items(transitionGroupShape(newPos - pos()), Qt::IntersectsItemShape);
340                 for (int i = 0; i < children.count(); i++) {
341                     collidingItems.removeAll(children.at(i));
342                 }
343                 for (int i = 0; i < collidingItems.count(); i++)
344                     if (collidingItems.at(i)->type() == TRANSITIONWIDGET) return pos();
345             }
346         }
347         return newPos;
348     }
349     return QGraphicsItemGroup::itemChange(change, value);
350 }
351
352 //virtual
353 void AbstractGroupItem::dropEvent(QGraphicsSceneDragDropEvent * event)
354 {
355     QString effects = QString::fromUtf8(event->mimeData()->data("kdenlive/effectslist"));
356     QDomDocument doc;
357     doc.setContent(effects, true);
358     QDomElement e = doc.documentElement();
359     e.setAttribute("kdenlive_ix", 0);
360     CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
361     QPointF dropPos = event->scenePos();
362     QList<QGraphicsItem *> selection = scene()->items(dropPos);
363     AbstractClipItem *dropChild = NULL;
364     for (int i = 0; i < selection.count(); i++) {
365         if (selection.at(i)->type() == AVWIDGET) {
366             dropChild = (AbstractClipItem *) selection.at(i);
367             break;
368         }
369     }           
370     if (view) view->slotAddGroupEffect(e, this, dropChild);
371 }
372
373 //virtual
374 void AbstractGroupItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
375 {
376     event->setAccepted(event->mimeData()->hasFormat("kdenlive/effectslist"));
377 }
378
379 void AbstractGroupItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
380 {
381     Q_UNUSED(event)
382 }
383
384 // virtual
385 void AbstractGroupItem::mousePressEvent(QGraphicsSceneMouseEvent * event)
386 {
387     if (event->modifiers() & Qt::ShiftModifier) {
388         // User want to do a rectangle selection, so ignore the event to pass it to the view
389         event->ignore();
390     } else {
391         QList <QGraphicsItem *>list = scene()->items(event->scenePos());
392         // only allow group move if we click over an item in the group
393         foreach(const QGraphicsItem *item, list) {
394             if (item->type() == TRANSITIONWIDGET || item->type() == AVWIDGET) {
395                 QGraphicsItem::mousePressEvent(event);
396                 return;
397             }
398         }
399         event->ignore();
400     }
401 }
402
403 void AbstractGroupItem::resizeStart(int diff)
404 {
405     bool info = false;
406     if (m_resizeInfos.isEmpty())
407         info = true;
408     int maximum = diff;
409     QList <QGraphicsItem *> children = childItems();
410     QList <AbstractClipItem *> items;
411     int itemcount = 0;
412     for (int i = 0; i < children.count(); ++i) {
413         AbstractClipItem *item = static_cast <AbstractClipItem *>(children.at(i));
414         if (item && item->type() == AVWIDGET) {
415             items << item;
416             if (info)
417                 m_resizeInfos << item->info();
418             item->resizeStart((int)(m_resizeInfos.at(itemcount).startPos.frames(item->fps())) + diff);
419             int itemdiff = (int)(item->startPos() - m_resizeInfos.at(itemcount).startPos).frames(item->fps());
420             if (qAbs(itemdiff) < qAbs(maximum))
421                 maximum = itemdiff;
422             ++itemcount;
423         }
424     }
425     
426     for (int i = 0; i < items.count(); ++i)
427         items.at(i)->resizeStart((int)(m_resizeInfos.at(i).startPos.frames(items.at(i)->fps())) + maximum);
428 }
429
430 void AbstractGroupItem::resizeEnd(int diff)
431 {
432     bool info = false;
433     if (m_resizeInfos.isEmpty())
434         info = true;
435     int maximum = diff;
436     QList <QGraphicsItem *> children = childItems();
437     QList <AbstractClipItem *> items;
438     int itemcount = 0;
439     for (int i = 0; i < children.count(); ++i) {
440         AbstractClipItem *item = static_cast <AbstractClipItem *>(children.at(i));
441         if (item && item->type() == AVWIDGET) {
442             items << item;
443             if (info)
444                 m_resizeInfos << item->info();
445             item->resizeEnd((int)(m_resizeInfos.at(itemcount).endPos.frames(item->fps())) + diff);
446             int itemdiff = (int)(item->endPos() - m_resizeInfos.at(itemcount).endPos).frames(item->fps());
447             if (qAbs(itemdiff) < qAbs(maximum))
448                 maximum = itemdiff;
449             ++itemcount;
450         }
451     }
452
453     for (int i = 0; i < items.count(); ++i)
454         items.at(i)->resizeEnd((int)(m_resizeInfos.at(i).endPos.frames(items.at(i)->fps())) + maximum);
455 }
456
457 QList< ItemInfo > AbstractGroupItem::resizeInfos()
458 {
459     return m_resizeInfos;
460 }
461
462 void AbstractGroupItem::clearResizeInfos()
463 {
464     // m_resizeInfos.clear() will crash in some cases for unknown reasons - ttill
465     m_resizeInfos = QList <ItemInfo>();
466 }
467
468 GenTime AbstractGroupItem::duration()
469 {
470     QList <QGraphicsItem *> children = childItems();
471     GenTime start = GenTime(-1.0);
472     GenTime end = GenTime();
473     for (int i = 0; i < children.count(); ++i) {
474         if (children.at(i)->type() != GROUPWIDGET) {
475             AbstractClipItem *item = static_cast <AbstractClipItem *>(children.at(i));
476             if (item) {
477                 if (start < GenTime() || item->startPos() < start)
478                     start = item->startPos();
479                 if (item->endPos() > end)
480                     end = item->endPos();
481             }
482         } else {
483             children << children.at(i)->childItems();
484         }
485     }
486     return end - start;
487 }