X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=src%2Fabstractgroupitem.cpp;h=2b74e01e108e7459cfe7c80d1ea605b060b818de;hb=56aee6aedeeed3efd10ada8fe3c229eddc01ef05;hp=268bb930654776b8ab3abd17f03b507e4d1eac99;hpb=43c74f884965987e0906d94a43d95d426d392fc5;p=kdenlive diff --git a/src/abstractgroupitem.cpp b/src/abstractgroupitem.cpp index 268bb930..2b74e01e 100644 --- a/src/abstractgroupitem.cpp +++ b/src/abstractgroupitem.cpp @@ -18,52 +18,156 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * ***************************************************************************/ -#include -#include -#include -#include - -#include -#include - #include "abstractgroupitem.h" #include "abstractclipitem.h" #include "kdenlivesettings.h" #include "customtrackscene.h" +#include "customtrackview.h" + +#include + +#include +#include +#include +#include +#include -AbstractGroupItem::AbstractGroupItem(double fps): QGraphicsItemGroup(), m_fps(fps) { - setFlags(QGraphicsItem::ItemClipsToShape | QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable); + +AbstractGroupItem::AbstractGroupItem(double /* fps */) : + QObject(), + QGraphicsItemGroup() +{ + 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 { - return GROUPWIDGET; +int AbstractGroupItem::type() const +{ + return GroupWidget; } -CustomTrackScene* AbstractGroupItem::projectScene() { +int AbstractGroupItem::track() const +{ + //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() +{ if (scene()) return static_cast (scene()); return NULL; } +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() == (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; +} -QPolygonF AbstractGroupItem::groupShape(QPointF offset) { +QPainterPath AbstractGroupItem::spacerGroupShape(GraphicsRectItem type, const QPointF &offset) const +{ + QPainterPath path; QList children = childItems(); - QPolygonF path; - for (int i = 0; i < children.count(); i++) { - if (children.at(i)->type() == AVWIDGET) { - QPolygonF r = QPolygonF(children.at(i)->sceneBoundingRect()); - path = path.united(r); + 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); + } + } } } - path.translate(offset); return path; } -void AbstractGroupItem::addItem(QGraphicsItem * item) { +void AbstractGroupItem::addItem(QGraphicsItem * item) +{ addToGroup(item); - //fixItemRect(); + item->setFlag(QGraphicsItem::ItemIsMovable, false); +} + +void AbstractGroupItem::removeItem(QGraphicsItem * item) +{ + removeFromGroup(item); } -void AbstractGroupItem::fixItemRect() { +void AbstractGroupItem::fixItemRect() +{ QPointF start = boundingRect().topLeft(); if (start != QPointF(0, 0)) { translate(0 - start.x(), 0 - start.y()); @@ -71,97 +175,384 @@ void AbstractGroupItem::fixItemRect() { } } +/*ItemInfo AbstractGroupItem::info() const { + ItemInfo itemInfo; + itemInfo.startPos = m_startPos; + itemInfo.track = m_track; + return itemInfo; +}*/ + // virtual -void AbstractGroupItem::paint(QPainter *p, const QStyleOptionGraphicsItem *, QWidget *) { - p->fillRect(boundingRect(), QColor(200, 100, 100, 100)); +void AbstractGroupItem::paint(QPainter *p, const QStyleOptionGraphicsItem *option, QWidget *) +{ + QColor bgcolor(100, 100, 200, 100); + QRectF bound = option->exposedRect.adjusted(0, 0, 1, 1); + p->setClipRect(bound); + p->setBrush(bgcolor); + QPen pen = p->pen(); + pen.setColor(QColor(200, 90, 90)); + pen.setStyle(Qt::DashLine); + pen.setWidthF(0.0); + p->setPen(pen); + p->drawRoundedRect(boundingRect().adjusted(0, 0, -1, 0), 3, 3); } //virtual -QVariant AbstractGroupItem::itemChange(GraphicsItemChange change, const QVariant &value) { - if (change == ItemPositionChange && scene()) { +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. - QPointF newPos = value.toPointF(); + const int trackHeight = KdenliveSettings::trackheight(); QPointF start = sceneBoundingRect().topLeft(); - int posx = start.x() + newPos.x(); //projectScene()->getSnapPointForPos(start.x() + sc.x(), KdenliveSettings::snaptopoints()); - - int startTrack = (start.y()) / KdenliveSettings::trackheight(); - int newTrack = (start.y() + newPos.y()) / KdenliveSettings::trackheight(); - //kDebug()<<"// GROUP NEW TRACK: "<tracksCount() - 1); - newTrack = qMax(newTrack, 0); - newPos.setY((int)((newTrack - startTrack) * KdenliveSettings::trackheight())); + QPointF newPos = value.toPointF(); + int xpos = projectScene()->getSnapPointForPos((int)(start.x() + newPos.x() - pos().x()), KdenliveSettings::snaptopoints()); - //kDebug() << "------------------------------------GRUOP MOVE"; + xpos = qMax(xpos, 0); + //kDebug()<<"GRP XPOS:"< groupTracks; + QList children = childItems(); + 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; + topTrack = currentTrack; + } + } else if (children.at(i)->type() == TransitionWidget) { + if (topTrack == -1 || currentTrack < topTrack) { + offset = (int)(trackHeight / 3 * 2 - 1); + topTrack = currentTrack; + } + } else if (children.at(i)->type() == GroupWidget) { + QList subchildren = children.at(i)->childItems(); + bool clipGroup = false; + for (int j = 0; j < subchildren.count(); j++) { + 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; + } + } + if (clipGroup) { + if (topTrack == -1 || currentTrack <= topTrack) { + offset = 0; + topTrack = currentTrack; + } + } else { + if (topTrack == -1 || currentTrack < topTrack) { + offset = (int)(trackHeight / 3 * 2 - 1); + topTrack = currentTrack; + } + } + } + } + // 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; - if (start.x() + newPos.x() - pos().x() < 0) { + /*if (newPos.x() < 0) { // If group goes below 0, adjust position to 0 return QPointF(pos().x() - start.x(), pos().y()); + }*/ + + 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(); + } } - QPolygonF sceneShape = groupShape(newPos - pos()); - QList collindingItems = scene()->items(sceneShape, Qt::IntersectsItemShape); - QList children = childItems(); - for (int i = 0; i < children.count(); i++) { - collindingItems.removeAll(children.at(i)); + 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 (collindingItems.isEmpty()) return newPos; + if (collidingItems.isEmpty()) return newPos; else { - for (int i = 0; i < collindingItems.count(); i++) { - QGraphicsItem *collision = collindingItems.at(i); - if (collision->type() == AVWIDGET) { + bool forwardMove = xpos > start.x(); + int offset = 0; + for (int i = 0; i < collidingItems.count(); ++i) { + QGraphicsItem *collision = collidingItems.at(i); + if (collision->type() == TransitionWidget) { // Collision - return pos(); - //TODO: improve movement when collision happens - /*if (startTrack != newTrack) return pos(); - if (collision->pos().x() > pos().x()) { - return QPointF(collision->sceneBoundingRect().x() - sceneBoundingRect().width() + pos().x() - start.x() - 1, newPos.y()); - }*/ + 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(transitionGroupShape(newPos - pos()), Qt::IntersectsItemShape); + for (int i = 0; i < children.count(); ++i) { + collidingItems.removeAll(children.at(i)); } + 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); +} - //else posx -= startx; - //posx = qMax(posx, 0); - //newPos.setX(posx); - //kDebug()<<"Y POS: "<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]; + 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 +void AbstractGroupItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event) +{ + event->setAccepted(event->mimeData()->hasFormat("kdenlive/effectslist")); +} +void AbstractGroupItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event) +{ + Q_UNUSED(event) +} - //if (start.y() + newPos.y() < 1) newTrack = oldTrack; +// virtual +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 { + 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(); + } +} - return newPos; +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); +} - // Only one clip is moving +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 items = scene()->items(sceneShape, Qt::IntersectsItemShape); +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 (); +} - if (!items.isEmpty()) { - for (int i = 0; i < items.count(); i++) { - if (items.at(i)->type() == AVWIDGET) { - // Collision! - //kDebug()<<"/// COLLISION WITH ITEM: "<sceneBoundingRect(); - return pos(); - QPointF otherPos = items.at(i)->pos(); - if ((int) otherPos.y() != (int) pos().y()) return pos(); - if (pos().x() < otherPos.x()) { - // move clip just before colliding clip - int npos = (static_cast < AbstractClipItem* >(items.at(i))->startPos()).frames(m_fps) - sceneBoundingRect().width(); - newPos.setX(npos); - } else { - // get pos just after colliding clip - int npos = static_cast < AbstractClipItem* >(items.at(i))->endPos().frames(m_fps); - newPos.setX(npos); - } - return newPos; - } +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 newPos; } - return QGraphicsItem::itemChange(change, value); + return end - start; } +#include "abstractgroupitem.moc"