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