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