]> git.sesse.net Git - kdenlive/blob - src/abstractgroupitem.cpp
Cleanup some timeline operations
[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::removeItem(QGraphicsItem * item)
122 {
123     removeFromGroup(item);
124 }
125
126 void AbstractGroupItem::fixItemRect()
127 {
128     QPointF start = boundingRect().topLeft();
129     if (start != QPointF(0, 0)) {
130         translate(0 - start.x(), 0 - start.y());
131         setPos(start);
132     }
133 }
134
135 /*ItemInfo AbstractGroupItem::info() const {
136     ItemInfo itemInfo;
137     itemInfo.startPos = m_startPos;
138     itemInfo.track = m_track;
139     return itemInfo;
140 }*/
141
142 // virtual
143 void AbstractGroupItem::paint(QPainter *p, const QStyleOptionGraphicsItem *option, QWidget *)
144 {
145     QColor bgcolor(100, 100, 200, 100);
146     QRectF bound = option->exposedRect.adjusted(0, 0, 1, 1);
147     p->setClipRect(bound);
148     const QRectF mapped = p->worldTransform().mapRect(option->exposedRect);
149     p->setWorldMatrixEnabled(false);
150     p->setBrush(bgcolor);
151     QPen pen = p->pen();
152     pen.setColor(QColor(200, 90, 90));
153     pen.setStyle(Qt::DashLine);
154     pen.setWidthF(0.0);
155     p->setPen(pen);
156     p->drawRoundedRect(mapped, 3, 3);
157 }
158
159 //virtual
160 QVariant AbstractGroupItem::itemChange(GraphicsItemChange change, const QVariant &value)
161 {
162     if (change == QGraphicsItem::ItemSelectedChange) {
163         if (value.toBool()) setZValue(10);
164         else setZValue(1);
165     }
166     if (change == ItemPositionChange && scene() && parentItem() == 0) {
167         // calculate new position.
168         const int trackHeight = KdenliveSettings::trackheight();
169         QPointF start = sceneBoundingRect().topLeft();
170         QPointF newPos = value.toPointF();
171         int xpos = projectScene()->getSnapPointForPos((int)(start.x() + newPos.x() - pos().x()), KdenliveSettings::snaptopoints());
172
173         xpos = qMax(xpos, 0);
174         //kDebug()<<"GRP XPOS:"<<xpos<<", START:"<<start.x()<<",NEW:"<<newPos.x()<<"; SCENE:"<<scenePos().x()<<",POS:"<<pos().x();
175         newPos.setX((int)(pos().x() + xpos - (int) start.x()));
176
177         int yOffset = property("y_absolute").toInt() + newPos.y();
178         int proposedTrack = yOffset / trackHeight;
179
180         // Check if top item is a clip or a transition
181         int offset = 0;
182         int topTrack = -1;
183         QList<QGraphicsItem *> children = childItems();
184         for (int i = 0; i < children.count(); i++) {
185             int currentTrack = 0;
186             if (children.at(i)->type() == AVWIDGET || children.at(i)->type() == TRANSITIONWIDGET) currentTrack = static_cast <AbstractClipItem*> (children.at(i))->track();
187             else if (children.at(i)->type() == GROUPWIDGET) currentTrack = static_cast <AbstractGroupItem*> (children.at(i))->track();
188             else continue;
189             if (children.at(i)->type() == AVWIDGET) {
190                 if (topTrack == -1 || currentTrack <= topTrack) {
191                     offset = 0;
192                     topTrack = currentTrack;
193                 }
194             } else if (children.at(i)->type() == TRANSITIONWIDGET) {
195                 if (topTrack == -1 || currentTrack < topTrack) {
196                     offset = (int)(trackHeight / 3 * 2 - 1);
197                     topTrack = currentTrack;
198                 }
199             } else if (children.at(i)->type() == GROUPWIDGET) {
200                 QList<QGraphicsItem *> subchildren = children.at(i)->childItems();
201                 bool clipGroup = false;
202                 for (int j = 0; j < subchildren.count(); j++) {
203                     if (subchildren.at(j)->type() == AVWIDGET) {
204                         clipGroup = true;
205                         break;
206                     }
207                 }
208                 if (clipGroup) {
209                     if (topTrack == -1 || currentTrack <= topTrack) {
210                         offset = 0;
211                         topTrack = currentTrack;
212                     }
213                 } else {
214                     if (topTrack == -1 || currentTrack < topTrack) {
215                         offset = (int)(trackHeight / 3 * 2 - 1);
216                         topTrack = currentTrack;
217                     }
218                 }
219             }
220         }
221         newPos.setY((int)((proposedTrack) * trackHeight) + offset);
222         //if (newPos == start) return start;
223
224         /*if (newPos.x() < 0) {
225             // If group goes below 0, adjust position to 0
226             return QPointF(pos().x() - start.x(), pos().y());
227         }*/
228
229         QList<QGraphicsItem*> collidingItems;
230         QPainterPath shape;
231         if (projectScene()->editMode() == NORMALEDIT) {
232             shape = clipGroupShape(newPos - pos());
233             collidingItems = scene()->items(shape, Qt::IntersectsItemShape);
234             collidingItems.removeAll(this);
235             for (int i = 0; i < children.count(); i++) {
236                 if (children.at(i)->type() == GROUPWIDGET) {
237                     QList<QGraphicsItem *> subchildren = children.at(i)->childItems();
238                     for (int j = 0; j < subchildren.count(); j++) {
239                         collidingItems.removeAll(subchildren.at(j));
240                     }
241                 }
242                 collidingItems.removeAll(children.at(i));
243             }
244         }
245         if (!collidingItems.isEmpty()) {
246             bool forwardMove = xpos > start.x();
247             int offset = 0;
248             for (int i = 0; i < collidingItems.count(); i++) {
249                 QGraphicsItem *collision = collidingItems.at(i);
250                 if (collision->type() == AVWIDGET) {
251                     // Collision
252                     if (newPos.y() != pos().y()) {
253                         // Track change results in collision, restore original position
254                         return pos();
255                     }
256                     AbstractClipItem *item = static_cast <AbstractClipItem *>(collision);
257                     if (forwardMove) {
258                         // Moving forward, determine best pos
259                         QPainterPath clipPath;
260                         clipPath.addRect(item->sceneBoundingRect());
261                         QPainterPath res = shape.intersected(clipPath);
262                         offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
263                     } else {
264                         // Moving backward, determine best pos
265                         QPainterPath clipPath;
266                         clipPath.addRect(item->sceneBoundingRect());
267                         QPainterPath res = shape.intersected(clipPath);
268                         offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
269                     }
270                 }
271             }
272             if (offset > 0) {
273                 if (forwardMove) {
274                     newPos.setX(newPos.x() - offset);
275                 } else {
276                     newPos.setX(newPos.x() + offset);
277                 }
278                 // If there is still a collision after our position adjust, restore original pos
279                 collidingItems = scene()->items(clipGroupShape(newPos - pos()), Qt::IntersectsItemShape);
280                 collidingItems.removeAll(this);
281                 for (int i = 0; i < children.count(); i++) {
282                     if (children.at(i)->type() == GROUPWIDGET) {
283                         QList<QGraphicsItem *> subchildren = children.at(i)->childItems();
284                         for (int j = 0; j < subchildren.count(); j++) {
285                             collidingItems.removeAll(subchildren.at(j));
286                         }
287                     }
288                     collidingItems.removeAll(children.at(i));
289                 }
290                 for (int i = 0; i < collidingItems.count(); i++)
291                     if (collidingItems.at(i)->type() == AVWIDGET) return pos();
292             }
293         }
294
295         if (projectScene()->editMode() == NORMALEDIT) {
296             shape = transitionGroupShape(newPos - pos());
297             collidingItems = scene()->items(shape, Qt::IntersectsItemShape);
298             collidingItems.removeAll(this);
299             for (int i = 0; i < children.count(); i++) {
300                 if (children.at(i)->type() == GROUPWIDGET) {
301                     QList<QGraphicsItem *> subchildren = children.at(i)->childItems();
302                     for (int j = 0; j < subchildren.count(); j++) {
303                         collidingItems.removeAll(subchildren.at(j));
304                     }
305                 }
306                 collidingItems.removeAll(children.at(i));
307             }
308         }
309         if (collidingItems.isEmpty()) return newPos;
310         else {
311             bool forwardMove = xpos > start.x();
312             int offset = 0;
313             for (int i = 0; i < collidingItems.count(); i++) {
314                 QGraphicsItem *collision = collidingItems.at(i);
315                 if (collision->type() == TRANSITIONWIDGET) {
316                     // Collision
317                     if (newPos.y() != pos().y()) {
318                         // Track change results in collision, restore original position
319                         return pos();
320                     }
321                     AbstractClipItem *item = static_cast <AbstractClipItem *>(collision);
322                     if (forwardMove) {
323                         // Moving forward, determine best pos
324                         QPainterPath clipPath;
325                         clipPath.addRect(item->sceneBoundingRect());
326                         QPainterPath res = shape.intersected(clipPath);
327                         offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
328                     } else {
329                         // Moving backward, determine best pos
330                         QPainterPath clipPath;
331                         clipPath.addRect(item->sceneBoundingRect());
332                         QPainterPath res = shape.intersected(clipPath);
333                         offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
334                     }
335                 }
336             }
337             if (offset > 0) {
338                 if (forwardMove) {
339                     newPos.setX(newPos.x() - offset);
340                 } else {
341                     newPos.setX(newPos.x() + offset);
342                 }
343                 // If there is still a collision after our position adjust, restore original pos
344                 collidingItems = scene()->items(transitionGroupShape(newPos - pos()), Qt::IntersectsItemShape);
345                 for (int i = 0; i < children.count(); i++) {
346                     collidingItems.removeAll(children.at(i));
347                 }
348                 for (int i = 0; i < collidingItems.count(); i++)
349                     if (collidingItems.at(i)->type() == TRANSITIONWIDGET) return pos();
350             }
351         }
352         return newPos;
353     }
354     return QGraphicsItemGroup::itemChange(change, value);
355 }
356
357 //virtual
358 void AbstractGroupItem::dropEvent(QGraphicsSceneDragDropEvent * event)
359 {
360     QString effects = QString::fromUtf8(event->mimeData()->data("kdenlive/effectslist"));
361     QDomDocument doc;
362     doc.setContent(effects, true);
363     QDomElement e = doc.documentElement();
364     e.setAttribute("kdenlive_ix", 0);
365     CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
366     QPointF dropPos = event->scenePos();
367     QList<QGraphicsItem *> selection = scene()->items(dropPos);
368     AbstractClipItem *dropChild = NULL;
369     for (int i = 0; i < selection.count(); i++) {
370         if (selection.at(i)->type() == AVWIDGET) {
371             dropChild = (AbstractClipItem *) selection.at(i);
372             break;
373         }
374     }           
375     if (view) view->slotAddGroupEffect(e, this, dropChild);
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 {
396         QList <QGraphicsItem *>list = scene()->items(event->scenePos());
397         // only allow group move if we click over an item in the group
398         foreach(const QGraphicsItem *item, list) {
399             if (item->type() == TRANSITIONWIDGET || item->type() == AVWIDGET) {
400                 QGraphicsItem::mousePressEvent(event);
401                 return;
402             }
403         }
404         event->ignore();
405     }
406 }
407
408 void AbstractGroupItem::resizeStart(int diff)
409 {
410     bool info = false;
411     if (m_resizeInfos.isEmpty())
412         info = true;
413     int maximum = diff;
414     QList <QGraphicsItem *> children = childItems();
415     QList <AbstractClipItem *> items;
416     int itemcount = 0;
417     for (int i = 0; i < children.count(); ++i) {
418         AbstractClipItem *item = static_cast <AbstractClipItem *>(children.at(i));
419         if (item && item->type() == AVWIDGET) {
420             items << item;
421             if (info)
422                 m_resizeInfos << item->info();
423             item->resizeStart((int)(m_resizeInfos.at(itemcount).startPos.frames(item->fps())) + diff);
424             int itemdiff = (int)(item->startPos() - m_resizeInfos.at(itemcount).startPos).frames(item->fps());
425             if (qAbs(itemdiff) < qAbs(maximum))
426                 maximum = itemdiff;
427             ++itemcount;
428         }
429     }
430     
431     for (int i = 0; i < items.count(); ++i)
432         items.at(i)->resizeStart((int)(m_resizeInfos.at(i).startPos.frames(items.at(i)->fps())) + maximum);
433 }
434
435 void AbstractGroupItem::resizeEnd(int diff)
436 {
437     bool info = false;
438     if (m_resizeInfos.isEmpty())
439         info = true;
440     int maximum = diff;
441     QList <QGraphicsItem *> children = childItems();
442     QList <AbstractClipItem *> items;
443     int itemcount = 0;
444     for (int i = 0; i < children.count(); ++i) {
445         AbstractClipItem *item = static_cast <AbstractClipItem *>(children.at(i));
446         if (item && item->type() == AVWIDGET) {
447             items << item;
448             if (info)
449                 m_resizeInfos << item->info();
450             item->resizeEnd((int)(m_resizeInfos.at(itemcount).endPos.frames(item->fps())) + diff);
451             int itemdiff = (int)(item->endPos() - m_resizeInfos.at(itemcount).endPos).frames(item->fps());
452             if (qAbs(itemdiff) < qAbs(maximum))
453                 maximum = itemdiff;
454             ++itemcount;
455         }
456     }
457
458     for (int i = 0; i < items.count(); ++i)
459         items.at(i)->resizeEnd((int)(m_resizeInfos.at(i).endPos.frames(items.at(i)->fps())) + maximum);
460 }
461
462 QList< ItemInfo > AbstractGroupItem::resizeInfos()
463 {
464     return m_resizeInfos;
465 }
466
467 void AbstractGroupItem::clearResizeInfos()
468 {
469     // m_resizeInfos.clear() will crash in some cases for unknown reasons - ttill
470     m_resizeInfos = QList <ItemInfo>();
471 }
472
473 GenTime AbstractGroupItem::duration()
474 {
475     QList <QGraphicsItem *> children = childItems();
476     GenTime start = GenTime(-1.0);
477     GenTime end = GenTime();
478     for (int i = 0; i < children.count(); ++i) {
479         if (children.at(i)->type() != GROUPWIDGET) {
480             AbstractClipItem *item = static_cast <AbstractClipItem *>(children.at(i));
481             if (item) {
482                 if (start < GenTime() || item->startPos() < start)
483                     start = item->startPos();
484                 if (item->endPos() > end)
485                     end = item->endPos();
486             }
487         } else {
488             children << children.at(i)->childItems();
489         }
490     }
491     return end - start;
492 }