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