X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=src%2Fabstractgroupitem.cpp;h=e91d1f81a310108101e4efb937ab80000424e1dc;hb=c3302003093710ee247ad84c0fe2ef3c579d417f;hp=788e015bdb9731ef0e515d3c31d87803240fdf38;hpb=d7a6993a720e685d921bbf9d715789307e79effe;p=kdenlive diff --git a/src/abstractgroupitem.cpp b/src/abstractgroupitem.cpp index 788e015b..e91d1f81 100644 --- a/src/abstractgroupitem.cpp +++ b/src/abstractgroupitem.cpp @@ -32,9 +32,10 @@ #include #include + AbstractGroupItem::AbstractGroupItem(double /* fps */) : - QObject(), - QGraphicsItemGroup() + QObject(), + QGraphicsItemGroup() { setZValue(1); setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable); @@ -42,6 +43,7 @@ AbstractGroupItem::AbstractGroupItem(double /* fps */) : setFlag(QGraphicsItem::ItemSendsGeometryChanges, true); #endif setAcceptDrops(true); + m_resizeInfos = QList (); } int AbstractGroupItem::type() const @@ -51,7 +53,37 @@ int AbstractGroupItem::type() const int AbstractGroupItem::track() const { - return (int)(scenePos().y() / KdenliveSettings::trackheight()); + //return (int)(scenePos().y() / KdenliveSettings::trackheight()); + int topTrack = -1; + QList children = childItems(); + for (int i = 0; i < children.count(); ++i) { + if (children.at(i)->type() == GROUPWIDGET) { + children.append(children.at(i)->childItems()); + continue; + } + AbstractClipItem *item = static_cast (children.at(i)); + if (item && (topTrack == -1 || topTrack > item->track())) { + topTrack = item->track(); + } + } + return topTrack; +} + +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() @@ -60,29 +92,64 @@ CustomTrackScene* AbstractGroupItem::projectScene() return NULL; } -QPainterPath AbstractGroupItem::clipGroupShape(QPointF offset) const +QPainterPath AbstractGroupItem::clipGroupSpacerShape(const QPointF &offset) const +{ + return spacerGroupShape(AVWIDGET, offset); +} + +QPainterPath AbstractGroupItem::clipGroupShape(const QPointF &offset) const +{ + return groupShape(AVWIDGET, offset); +} + +QPainterPath AbstractGroupItem::transitionGroupShape(const QPointF &offset) const +{ + return groupShape(TRANSITIONWIDGET, offset); +} + +QPainterPath AbstractGroupItem::groupShape(GRAPHICSRECTITEM type, const QPointF &offset) const { QPainterPath path; QList children = childItems(); - for (int i = 0; i < children.count(); i++) { - if (children.at(i)->type() == AVWIDGET) { + for (int i = 0; i < children.count(); ++i) { + 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; } -QPainterPath AbstractGroupItem::transitionGroupShape(QPointF offset) const +QPainterPath AbstractGroupItem::spacerGroupShape(GRAPHICSRECTITEM type, const QPointF &offset) const { QPainterPath path; QList children = childItems(); - for (int i = 0; i < children.count(); i++) { - if (children.at(i)->type() == TRANSITIONWIDGET) { + for (int i = 0; i < children.count(); ++i) { + if (children.at(i)->type() == (int)type) { QRectF r(children.at(i)->sceneBoundingRect()); r.translate(offset); + r.setRight(scene()->width()); 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); + r.setRight(scene()->width()); + path.addRect(r); + } + } } } return path; @@ -91,7 +158,12 @@ QPainterPath AbstractGroupItem::transitionGroupShape(QPointF offset) const void AbstractGroupItem::addItem(QGraphicsItem * item) { addToGroup(item); - //fixItemRect(); + item->setFlag(QGraphicsItem::ItemIsMovable, false); +} + +void AbstractGroupItem::removeItem(QGraphicsItem * item) +{ + removeFromGroup(item); } void AbstractGroupItem::fixItemRect() @@ -113,51 +185,52 @@ void AbstractGroupItem::fixItemRect() // virtual void AbstractGroupItem::paint(QPainter *p, const QStyleOptionGraphicsItem *option, QWidget *) { - const double scale = option->matrix.m11(); QColor bgcolor(100, 100, 200, 100); QRectF bound = option->exposedRect.adjusted(0, 0, 1, 1); p->setClipRect(bound); - p->fillRect(option->exposedRect, bgcolor); + p->setBrush(bgcolor); QPen pen = p->pen(); pen.setColor(QColor(200, 90, 90)); pen.setStyle(Qt::DashLine); pen.setWidthF(0.0); - //pen.setCosmetic(true); p->setPen(pen); - p->drawRect(boundingRect().adjusted(0, 0, - 1 / scale, 0)); + p->drawRoundedRect(boundingRect().adjusted(0, 0, -1, 0), 3, 3); } //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); - //kDebug()<<"GRP XPOS:"<tracksCount() - (int)(boundingRect().height() + 5) / trackHeight); - correctedTrack = qMax(correctedTrack, 0); - - proposedTrack += (correctedTrack - realTrack); - + QStringList lockedTracks = property("locked_tracks").toStringList(); + int proposedTrack = (property("y_absolute").toInt() + newPos.y()) / trackHeight; // Check if top item is a clip or a transition int offset = 0; int topTrack = -1; + QList groupTracks; QList children = childItems(); - for (int i = 0; i < children.count(); i++) { - int currentTrack = (int)(children.at(i)->scenePos().y() / trackHeight); + for (int i = 0; i < children.count(); ++i) { + int currentTrack = 0; + if (children.at(i)->type() == AVWIDGET || children.at(i)->type() == TRANSITIONWIDGET) { + currentTrack = static_cast (children.at(i))->track(); + if (!groupTracks.contains(currentTrack)) groupTracks.append(currentTrack); + } + else if (children.at(i)->type() == GROUPWIDGET) { + currentTrack = static_cast (children.at(i))->track(); + } + else continue; if (children.at(i)->type() == AVWIDGET) { if (topTrack == -1 || currentTrack <= topTrack) { offset = 0; @@ -172,9 +245,10 @@ QVariant AbstractGroupItem::itemChange(GraphicsItemChange change, const QVariant QList subchildren = children.at(i)->childItems(); bool clipGroup = false; for (int j = 0; j < subchildren.count(); j++) { - if (subchildren.at(j)->type() == AVWIDGET) { + if (subchildren.at(j)->type() == AVWIDGET || subchildren.at(j)->type() == TRANSITIONWIDGET) { + int subTrack = static_cast (subchildren.at(j))->track(); + if (!groupTracks.contains(subTrack)) groupTracks.append(subTrack); clipGroup = true; - break; } } if (clipGroup) { @@ -190,6 +264,24 @@ QVariant AbstractGroupItem::itemChange(GraphicsItemChange change, const QVariant } } } + // Check no clip in the group goes outside of existing tracks + int maximumTrack = projectScene()->tracksCount() - 1; + int groupHeight = 0; + for (int i = 0; i < groupTracks.count(); ++i) { + int offset = groupTracks.at(i) - topTrack; + if (offset > groupHeight) groupHeight = offset; + } + maximumTrack -= groupHeight; + proposedTrack = qMin(proposedTrack, maximumTrack); + proposedTrack = qMax(proposedTrack, 0); + int groupOffset = proposedTrack - topTrack; + if (!lockedTracks.isEmpty()) { + for (int i = 0; i < groupTracks.count(); ++i) { + if (lockedTracks.contains(QString::number(groupTracks.at(i) + groupOffset))) { + return pos(); + } + } + } newPos.setY((int)((proposedTrack) * trackHeight) + offset); //if (newPos == start) return start; @@ -198,20 +290,27 @@ QVariant AbstractGroupItem::itemChange(GraphicsItemChange change, const QVariant return QPointF(pos().x() - start.x(), pos().y()); }*/ - QList collindingItems; + QList collidingItems; QPainterPath shape; if (projectScene()->editMode() == NORMALEDIT) { shape = clipGroupShape(newPos - pos()); - collindingItems = scene()->items(shape, Qt::IntersectsItemShape); - for (int i = 0; i < children.count(); i++) { - collindingItems.removeAll(children.at(i)); + 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 (!collindingItems.isEmpty()) { + if (!collidingItems.isEmpty()) { bool forwardMove = xpos > start.x(); int offset = 0; - for (int i = 0; i < collindingItems.count(); i++) { - QGraphicsItem *collision = collindingItems.at(i); + for (int i = 0; i < collidingItems.count(); ++i) { + QGraphicsItem *collision = collidingItems.at(i); if (collision->type() == AVWIDGET) { // Collision if (newPos.y() != pos().y()) { @@ -241,28 +340,42 @@ 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(clipGroupShape(newPos - pos()), Qt::IntersectsItemShape); - for (int i = 0; i < children.count(); i++) { - collindingItems.removeAll(children.at(i)); + 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 < collindingItems.count(); i++) - if (collindingItems.at(i)->type() == AVWIDGET) return pos(); + for (int i = 0; i < collidingItems.count(); ++i) + if (collidingItems.at(i)->type() == AVWIDGET) return pos(); } } if (projectScene()->editMode() == NORMALEDIT) { shape = transitionGroupShape(newPos - pos()); - collindingItems = scene()->items(shape, Qt::IntersectsItemShape); - for (int i = 0; i < children.count(); i++) { - collindingItems.removeAll(children.at(i)); + 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 (collindingItems.isEmpty()) return newPos; + 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); + for (int i = 0; i < collidingItems.count(); ++i) { + QGraphicsItem *collision = collidingItems.at(i); if (collision->type() == TRANSITIONWIDGET) { // Collision if (newPos.y() != pos().y()) { @@ -292,12 +405,12 @@ 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(transitionGroupShape(newPos - pos()), Qt::IntersectsItemShape); - for (int i = 0; i < children.count(); i++) { - collindingItems.removeAll(children.at(i)); + collidingItems = scene()->items(transitionGroupShape(newPos - pos()), Qt::IntersectsItemShape); + for (int i = 0; i < children.count(); ++i) { + collidingItems.removeAll(children.at(i)); } - for (int i = 0; i < collindingItems.count(); i++) - if (collindingItems.at(i)->type() == TRANSITIONWIDGET) return pos(); + for (int i = 0; i < collidingItems.count(); ++i) + if (collidingItems.at(i)->type() == TRANSITIONWIDGET) return pos(); } } return newPos; @@ -308,12 +421,22 @@ QVariant AbstractGroupItem::itemChange(GraphicsItemChange change, const QVariant //virtual void AbstractGroupItem::dropEvent(QGraphicsSceneDragDropEvent * event) { - QString effects = QString(event->mimeData()->data("kdenlive/effectslist")); + QString effects = QString::fromUtf8(event->mimeData()->data("kdenlive/effectslist")); QDomDocument doc; doc.setContent(effects, true); QDomElement e = doc.documentElement(); + e.setAttribute("kdenlive_ix", 0); CustomTrackView *view = (CustomTrackView *) scene()->views()[0]; - if (view) view->slotAddGroupEffect(e, this); + QPointF dropPos = event->scenePos(); + QList selection = scene()->items(dropPos); + AbstractClipItem *dropChild = NULL; + for (int i = 0; i < selection.count(); ++i) { + if (selection.at(i)->type() == AVWIDGET) { + dropChild = (AbstractClipItem *) selection.at(i); + break; + } + } + if (view) view->slotAddGroupEffect(e, this, dropChild); } //virtual @@ -324,7 +447,7 @@ void AbstractGroupItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event) void AbstractGroupItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event) { - Q_UNUSED(event); + Q_UNUSED(event) } // virtual @@ -333,5 +456,103 @@ void AbstractGroupItem::mousePressEvent(QGraphicsSceneMouseEvent * event) if (event->modifiers() & Qt::ShiftModifier) { // User want to do a rectangle selection, so ignore the event to pass it to the view event->ignore(); - } else QGraphicsItem::mousePressEvent(event); + } else { + QList list = scene()->items(event->scenePos()); + // only allow group move if we click over an item in the group + foreach(const QGraphicsItem *item, list) { + if (item->type() == TRANSITIONWIDGET || item->type() == AVWIDGET) { + QGraphicsItem::mousePressEvent(event); + return; + } + } + event->ignore(); + } +} + +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; +} + +#include "abstractgroupitem.moc"