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