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