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