]> git.sesse.net Git - kdenlive/blobdiff - src/abstractgroupitem.cpp
Allow to preserve aspect ratio in render dialog resize option:
[kdenlive] / src / abstractgroupitem.cpp
index 788e015bdb9731ef0e515d3c31d87803240fdf38..7d2cc39a1b829bc20f65ad90e05ffd20f6d8b6bf 100644 (file)
@@ -54,6 +54,23 @@ int AbstractGroupItem::track() const
     return (int)(scenePos().y() / KdenliveSettings::trackheight());
 }
 
+void AbstractGroupItem::setItemLocked(bool locked)
+{
+    if (locked) {
+        setSelected(false);
+        setFlag(QGraphicsItem::ItemIsMovable, false);
+        setFlag(QGraphicsItem::ItemIsSelectable, false);
+    } else {
+        setFlag(QGraphicsItem::ItemIsMovable, true);
+        setFlag(QGraphicsItem::ItemIsSelectable, true);
+    }
+}
+
+bool AbstractGroupItem::isItemLocked() const
+{
+    return !(flags() & (QGraphicsItem::ItemIsSelectable));
+}
+
 CustomTrackScene* AbstractGroupItem::projectScene()
 {
     if (scene()) return static_cast <CustomTrackScene*>(scene());
@@ -62,27 +79,32 @@ CustomTrackScene* AbstractGroupItem::projectScene()
 
 QPainterPath AbstractGroupItem::clipGroupShape(QPointF offset) const
 {
-    QPainterPath path;
-    QList<QGraphicsItem *> children = childItems();
-    for (int i = 0; i < children.count(); i++) {
-        if (children.at(i)->type() == AVWIDGET) {
-            QRectF r(children.at(i)->sceneBoundingRect());
-            r.translate(offset);
-            path.addRect(r);
-        }
-    }
-    return path;
+    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<QGraphicsItem *> children = childItems();
     for (int i = 0; i < children.count(); i++) {
-        if (children.at(i)->type() == TRANSITIONWIDGET) {
+        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<QGraphicsItem *> 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;
@@ -130,12 +152,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:"<<start.x()<<", PROPOSED:"<<(int)(start.x() - pos().x() + newPos.x());
         int xpos = projectScene()->getSnapPointForPos((int)(start.x() + newPos.x() - pos().x()), KdenliveSettings::snaptopoints());
 
         xpos = qMax(xpos, 0);
@@ -198,20 +223,27 @@ QVariant AbstractGroupItem::itemChange(GraphicsItemChange change, const QVariant
             return QPointF(pos().x() - start.x(), pos().y());
         }*/
 
-        QList<QGraphicsItem*> collindingItems;
+        QList<QGraphicsItem*> collidingItems;
         QPainterPath shape;
         if (projectScene()->editMode() == NORMALEDIT) {
             shape = clipGroupShape(newPos - pos());
-            collindingItems = scene()->items(shape, Qt::IntersectsItemShape);
+            collidingItems = scene()->items(shape, Qt::IntersectsItemShape);
+            collidingItems.removeAll(this);
             for (int i = 0; i < children.count(); i++) {
-                collindingItems.removeAll(children.at(i));
+                if (children.at(i)->type() == GROUPWIDGET) {
+                    QList<QGraphicsItem *> 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 +273,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);
+                collidingItems = scene()->items(clipGroupShape(newPos - pos()), Qt::IntersectsItemShape);
+                collidingItems.removeAll(this);
                 for (int i = 0; i < children.count(); i++) {
-                    collindingItems.removeAll(children.at(i));
+                    if (children.at(i)->type() == GROUPWIDGET) {
+                        QList<QGraphicsItem *> 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);
+            collidingItems = scene()->items(shape, Qt::IntersectsItemShape);
+            collidingItems.removeAll(this);
             for (int i = 0; i < children.count(); i++) {
-                collindingItems.removeAll(children.at(i));
+                if (children.at(i)->type() == GROUPWIDGET) {
+                    QList<QGraphicsItem *> 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 +338,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);
+                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() == TRANSITIONWIDGET) return pos();
+                for (int i = 0; i < collidingItems.count(); i++)
+                    if (collidingItems.at(i)->type() == TRANSITIONWIDGET) return pos();
             }
         }
         return newPos;