X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=src%2Fabstractgroupitem.cpp;h=40923afafe1276fdad0850ebb838ab65e89cf706;hb=7ba3c22ce1c353cacc6b1f8f0d5501b16519f0f8;hp=817db9739b1ccf54edfdc5c15a14d0112cdc15a4;hpb=a28acc6947691dca02f1ea7b7ea1d4d3679fc6f5;p=kdenlive diff --git a/src/abstractgroupitem.cpp b/src/abstractgroupitem.cpp index 817db973..40923afa 100644 --- a/src/abstractgroupitem.cpp +++ b/src/abstractgroupitem.cpp @@ -38,7 +38,11 @@ AbstractGroupItem::AbstractGroupItem(double /* fps */) : { setZValue(1); setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable); +#if QT_VERSION >= 0x040600 + setFlag(QGraphicsItem::ItemSendsGeometryChanges, true); +#endif setAcceptDrops(true); + m_resizeInfos = QList (); } int AbstractGroupItem::type() const @@ -51,21 +55,57 @@ int AbstractGroupItem::track() const return (int)(scenePos().y() / KdenliveSettings::trackheight()); } +void AbstractGroupItem::setItemLocked(bool locked) +{ + if (locked) + setSelected(false); + + setFlag(QGraphicsItem::ItemIsMovable, !locked); + setFlag(QGraphicsItem::ItemIsSelectable, !locked); + + foreach (QGraphicsItem *child, childItems()) + ((AbstractClipItem *)child)->setItemLocked(locked); +} + +bool AbstractGroupItem::isItemLocked() const +{ + return !(flags() & (QGraphicsItem::ItemIsSelectable)); +} + CustomTrackScene* AbstractGroupItem::projectScene() { if (scene()) return static_cast (scene()); return NULL; } -QPainterPath AbstractGroupItem::groupShape(QPointF offset) +QPainterPath AbstractGroupItem::clipGroupShape(QPointF offset) const +{ + return groupShape(AVWIDGET, offset); +} + +QPainterPath AbstractGroupItem::transitionGroupShape(QPointF offset) const +{ + return groupShape(TRANSITIONWIDGET, offset); +} + +QPainterPath AbstractGroupItem::groupShape(GRAPHICSRECTITEM type, QPointF offset) const { QPainterPath path; QList children = childItems(); for (int i = 0; i < children.count(); i++) { - if (children.at(i)->type() == AVWIDGET) { + if (children.at(i)->type() == (int)type) { QRectF r(children.at(i)->sceneBoundingRect()); r.translate(offset); path.addRect(r); + } else if (children.at(i)->type() == GROUPWIDGET) { + QList subchildren = children.at(i)->childItems(); + for (int j = 0; j < subchildren.count(); j++) { + if (subchildren.at(j)->type() == (int)type) { + QRectF r(subchildren.at(j)->sceneBoundingRect()); + r.translate(offset); + path.addRect(r); + } + } } } return path; @@ -113,12 +153,15 @@ void AbstractGroupItem::paint(QPainter *p, const QStyleOptionGraphicsItem *optio //virtual QVariant AbstractGroupItem::itemChange(GraphicsItemChange change, const QVariant &value) { + if (change == QGraphicsItem::ItemSelectedChange) { + if (value.toBool()) setZValue(10); + else setZValue(1); + } if (change == ItemPositionChange && scene() && parentItem() == 0) { // calculate new position. const int trackHeight = KdenliveSettings::trackheight(); QPointF start = sceneBoundingRect().topLeft(); QPointF newPos = value.toPointF(); - //kDebug()<<"REAL:"<getSnapPointForPos((int)(start.x() + newPos.x() - pos().x()), KdenliveSettings::snaptopoints()); xpos = qMax(xpos, 0); @@ -181,19 +224,93 @@ QVariant AbstractGroupItem::itemChange(GraphicsItemChange change, const QVariant return QPointF(pos().x() - start.x(), pos().y()); }*/ - QPainterPath shape = groupShape(newPos - pos()); - QList collindingItems = scene()->items(shape, Qt::IntersectsItemShape); - for (int i = 0; i < children.count(); i++) { - collindingItems.removeAll(children.at(i)); + QList collidingItems; + QPainterPath shape; + if (projectScene()->editMode() == NORMALEDIT) { + shape = clipGroupShape(newPos - pos()); + collidingItems = scene()->items(shape, Qt::IntersectsItemShape); + collidingItems.removeAll(this); + for (int i = 0; i < children.count(); i++) { + if (children.at(i)->type() == GROUPWIDGET) { + QList subchildren = children.at(i)->childItems(); + for (int j = 0; j < subchildren.count(); j++) { + collidingItems.removeAll(subchildren.at(j)); + } + } + collidingItems.removeAll(children.at(i)); + } + } + if (!collidingItems.isEmpty()) { + bool forwardMove = xpos > start.x(); + int offset = 0; + for (int i = 0; i < collidingItems.count(); i++) { + QGraphicsItem *collision = collidingItems.at(i); + if (collision->type() == AVWIDGET) { + // Collision + if (newPos.y() != pos().y()) { + // Track change results in collision, restore original position + return pos(); + } + AbstractClipItem *item = static_cast (collision); + if (forwardMove) { + // Moving forward, determine best pos + QPainterPath clipPath; + clipPath.addRect(item->sceneBoundingRect()); + QPainterPath res = shape.intersected(clipPath); + offset = qMax(offset, (int)(res.boundingRect().width() + 0.5)); + } else { + // Moving backward, determine best pos + QPainterPath clipPath; + clipPath.addRect(item->sceneBoundingRect()); + QPainterPath res = shape.intersected(clipPath); + offset = qMax(offset, (int)(res.boundingRect().width() + 0.5)); + } + } + } + if (offset > 0) { + if (forwardMove) { + newPos.setX(newPos.x() - offset); + } else { + newPos.setX(newPos.x() + offset); + } + // If there is still a collision after our position adjust, restore original pos + collidingItems = scene()->items(clipGroupShape(newPos - pos()), Qt::IntersectsItemShape); + collidingItems.removeAll(this); + for (int i = 0; i < children.count(); i++) { + if (children.at(i)->type() == GROUPWIDGET) { + QList subchildren = children.at(i)->childItems(); + for (int j = 0; j < subchildren.count(); j++) { + collidingItems.removeAll(subchildren.at(j)); + } + } + collidingItems.removeAll(children.at(i)); + } + for (int i = 0; i < collidingItems.count(); i++) + if (collidingItems.at(i)->type() == AVWIDGET) return pos(); + } } - if (collindingItems.isEmpty()) return newPos; + if (projectScene()->editMode() == NORMALEDIT) { + shape = transitionGroupShape(newPos - pos()); + collidingItems = scene()->items(shape, Qt::IntersectsItemShape); + collidingItems.removeAll(this); + for (int i = 0; i < children.count(); i++) { + if (children.at(i)->type() == GROUPWIDGET) { + QList subchildren = children.at(i)->childItems(); + for (int j = 0; j < subchildren.count(); j++) { + collidingItems.removeAll(subchildren.at(j)); + } + } + collidingItems.removeAll(children.at(i)); + } + } + if (collidingItems.isEmpty()) return newPos; else { bool forwardMove = xpos > start.x(); int offset = 0; - for (int i = 0; i < collindingItems.count(); i++) { - QGraphicsItem *collision = collindingItems.at(i); - if (collision->type() == AVWIDGET) { + for (int i = 0; i < collidingItems.count(); i++) { + QGraphicsItem *collision = collidingItems.at(i); + if (collision->type() == TRANSITIONWIDGET) { // Collision if (newPos.y() != pos().y()) { // Track change results in collision, restore original position @@ -222,15 +339,15 @@ QVariant AbstractGroupItem::itemChange(GraphicsItemChange change, const QVariant newPos.setX(newPos.x() + offset); } // If there is still a collision after our position adjust, restore original pos - collindingItems = scene()->items(groupShape(newPos - pos()), Qt::IntersectsItemShape); + collidingItems = scene()->items(transitionGroupShape(newPos - pos()), Qt::IntersectsItemShape); for (int i = 0; i < children.count(); i++) { - collindingItems.removeAll(children.at(i)); + collidingItems.removeAll(children.at(i)); } - for (int i = 0; i < collindingItems.count(); i++) - if (collindingItems.at(i)->type() == AVWIDGET) return pos(); + for (int i = 0; i < collidingItems.count(); i++) + if (collidingItems.at(i)->type() == TRANSITIONWIDGET) return pos(); } - return newPos; } + return newPos; } return QGraphicsItemGroup::itemChange(change, value); } @@ -265,3 +382,89 @@ void AbstractGroupItem::mousePressEvent(QGraphicsSceneMouseEvent * event) event->ignore(); } else QGraphicsItem::mousePressEvent(event); } + +void AbstractGroupItem::resizeStart(int diff) +{ + bool info = false; + if (m_resizeInfos.isEmpty()) + info = true; + int maximum = diff; + QList children = childItems(); + QList items; + int itemcount = 0; + for (int i = 0; i < children.count(); ++i) { + AbstractClipItem *item = static_cast (children.at(i)); + if (item && item->type() == AVWIDGET) { + items << item; + if (info) + m_resizeInfos << item->info(); + item->resizeStart((int)(m_resizeInfos.at(itemcount).startPos.frames(item->fps())) + diff); + int itemdiff = (int)(item->startPos() - m_resizeInfos.at(itemcount).startPos).frames(item->fps()); + if (qAbs(itemdiff) < qAbs(maximum)) + maximum = itemdiff; + ++itemcount; + } + } + + for (int i = 0; i < items.count(); ++i) + items.at(i)->resizeStart((int)(m_resizeInfos.at(i).startPos.frames(items.at(i)->fps())) + maximum); +} + +void AbstractGroupItem::resizeEnd(int diff) +{ + bool info = false; + if (m_resizeInfos.isEmpty()) + info = true; + int maximum = diff; + QList children = childItems(); + QList items; + int itemcount = 0; + for (int i = 0; i < children.count(); ++i) { + AbstractClipItem *item = static_cast (children.at(i)); + if (item && item->type() == AVWIDGET) { + items << item; + if (info) + m_resizeInfos << item->info(); + item->resizeEnd((int)(m_resizeInfos.at(itemcount).endPos.frames(item->fps())) + diff); + int itemdiff = (int)(item->endPos() - m_resizeInfos.at(itemcount).endPos).frames(item->fps()); + if (qAbs(itemdiff) < qAbs(maximum)) + maximum = itemdiff; + ++itemcount; + } + } + + for (int i = 0; i < items.count(); ++i) + items.at(i)->resizeEnd((int)(m_resizeInfos.at(i).endPos.frames(items.at(i)->fps())) + maximum); +} + +QList< ItemInfo > AbstractGroupItem::resizeInfos() +{ + return m_resizeInfos; +} + +void AbstractGroupItem::clearResizeInfos() +{ + // m_resizeInfos.clear() will crash in some cases for unknown reasons - ttill + m_resizeInfos = QList (); +} + +GenTime AbstractGroupItem::duration() +{ + QList children = childItems(); + GenTime start = GenTime(-1.0); + GenTime end = GenTime(); + for (int i = 0; i < children.count(); ++i) { + if (children.at(i)->type() != GROUPWIDGET) { + AbstractClipItem *item = static_cast (children.at(i)); + if (item) { + if (start < GenTime() || item->startPos() < start) + start = item->startPos(); + if (item->endPos() > end) + end = item->endPos(); + } + } else { + children << children.at(i)->childItems(); + } + } + return end - start; +}