]> git.sesse.net Git - kdenlive/blobdiff - src/abstractgroupitem.cpp
Fix issues with DVD wizard, allow to loop menu movie
[kdenlive] / src / abstractgroupitem.cpp
index 2390bcd178cc498c33580d2083f6bb817e1b27bb..d4e9210481b241386a17f504de906f9d4fa61c32 100644 (file)
 #include <QStyleOptionGraphicsItem>
 #include <QDomDocument>
 #include <QMimeData>
+#include <QGraphicsSceneMouseEvent>
 
-
-AbstractGroupItem::AbstractGroupItem(double fps): QGraphicsItemGroup(), m_fps(fps) {
-    setZValue(2);
-    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);
 }
 
-int AbstractGroupItem::type() const {
+int AbstractGroupItem::type() const
+{
     return GROUPWIDGET;
 }
 
-int AbstractGroupItem::track() const {
+int AbstractGroupItem::track() const
+{
     return (int)(scenePos().y() / KdenliveSettings::trackheight());
 }
 
-CustomTrackScene* AbstractGroupItem::projectScene() {
+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());
     return NULL;
 }
 
-QPainterPath AbstractGroupItem::groupShape(QPointF offset) {
+QPainterPath AbstractGroupItem::clipGroupShape(QPointF offset) const
+{
     QPainterPath path;
     QList<QGraphicsItem *> children = childItems();
     for (int i = 0; i < children.count(); i++) {
@@ -64,12 +91,28 @@ QPainterPath AbstractGroupItem::groupShape(QPointF offset) {
     return path;
 }
 
-void AbstractGroupItem::addItem(QGraphicsItem * item) {
+QPainterPath AbstractGroupItem::transitionGroupShape(QPointF offset) const
+{
+    QPainterPath path;
+    QList<QGraphicsItem *> children = childItems();
+    for (int i = 0; i < children.count(); i++) {
+        if (children.at(i)->type() == TRANSITIONWIDGET) {
+            QRectF r(children.at(i)->sceneBoundingRect());
+            r.translate(offset);
+            path.addRect(r);
+        }
+    }
+    return path;
+}
+
+void AbstractGroupItem::addItem(QGraphicsItem * item)
+{
     addToGroup(item);
     //fixItemRect();
 }
 
-void AbstractGroupItem::fixItemRect() {
+void AbstractGroupItem::fixItemRect()
+{
     QPointF start = boundingRect().topLeft();
     if (start != QPointF(0, 0)) {
         translate(0 - start.x(), 0 - start.y());
@@ -85,22 +128,30 @@ void AbstractGroupItem::fixItemRect() {
 }*/
 
 // virtual
-void AbstractGroupItem::paint(QPainter *p, const QStyleOptionGraphicsItem *option, QWidget *) {
+void AbstractGroupItem::paint(QPainter *p, const QStyleOptionGraphicsItem *option, QWidget *)
+{
     const double scale = option->matrix.m11();
-    QRect clipRect = option->exposedRect.toRect();
-    clipRect.adjust(0, 0, 1 / scale + 0.5, 1);
-    p->fillRect(option->exposedRect, QColor(100, 100, 200, 100));
-    p->setClipRect(clipRect);
+    QColor bgcolor(100, 100, 200, 100);
+    QRectF bound = option->exposedRect.adjusted(0, 0, 1, 1);
+    p->setClipRect(bound);
+    p->fillRect(option->exposedRect, 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());
+    p->drawRect(boundingRect().adjusted(0, 0, - 1 / scale, 0));
 }
 
 //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.
         const int trackHeight = KdenliveSettings::trackheight();
         QPointF start = sceneBoundingRect().topLeft();
@@ -138,6 +189,26 @@ QVariant AbstractGroupItem::itemChange(GraphicsItemChange change, const QVariant
                     offset = (int)(trackHeight / 3 * 2 - 1);
                     topTrack = currentTrack;
                 }
+            } else if (children.at(i)->type() == GROUPWIDGET) {
+                QList<QGraphicsItem *> subchildren = children.at(i)->childItems();
+                bool clipGroup = false;
+                for (int j = 0; j < subchildren.count(); j++) {
+                    if (subchildren.at(j)->type() == AVWIDGET) {
+                        clipGroup = true;
+                        break;
+                    }
+                }
+                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;
+                    }
+                }
             }
         }
         newPos.setY((int)((proposedTrack) * trackHeight) + offset);
@@ -147,21 +218,74 @@ QVariant AbstractGroupItem::itemChange(GraphicsItemChange change, const QVariant
             // If group goes below 0, adjust position to 0
             return QPointF(pos().x() - start.x(), pos().y());
         }*/
-        QPainterPath shape = groupShape(newPos - pos());
-        QList<QGraphicsItem*> collindingItems = scene()->items(shape, Qt::IntersectsItemShape);
-        for (int i = 0; i < children.count(); i++) {
-            collindingItems.removeAll(children.at(i));
+
+        QList<QGraphicsItem*> collindingItems;
+        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));
+            }
+        }
+        if (!collindingItems.isEmpty()) {
+            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) {
+                    // Collision
+                    if (newPos.y() != pos().y()) {
+                        // Track change results in collision, restore original position
+                        return pos();
+                    }
+                    AbstractClipItem *item = static_cast <AbstractClipItem *>(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
+                collindingItems = scene()->items(clipGroupShape(newPos - pos()), Qt::IntersectsItemShape);
+                for (int i = 0; i < children.count(); i++) {
+                    collindingItems.removeAll(children.at(i));
+                }
+                for (int i = 0; i < collindingItems.count(); i++)
+                    if (collindingItems.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));
+            }
+        }
         if (collindingItems.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) {
+                if (collision->type() == TRANSITIONWIDGET) {
                     // Collision
-                    //kDebug()<<"// COLLISION WIT:"<<collision->sceneBoundingRect();
                     if (newPos.y() != pos().y()) {
                         // Track change results in collision, restore original position
                         return pos();
@@ -189,21 +313,22 @@ 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);
+                collindingItems = scene()->items(transitionGroupShape(newPos - pos()), Qt::IntersectsItemShape);
                 for (int i = 0; i < children.count(); i++) {
                     collindingItems.removeAll(children.at(i));
                 }
                 for (int i = 0; i < collindingItems.count(); i++)
-                    if (collindingItems.at(i)->type() == AVWIDGET) return pos();
+                    if (collindingItems.at(i)->type() == TRANSITIONWIDGET) return pos();
             }
-            return newPos;
         }
+        return newPos;
     }
     return QGraphicsItemGroup::itemChange(change, value);
 }
 
 //virtual
-void AbstractGroupItem::dropEvent(QGraphicsSceneDragDropEvent * event) {
+void AbstractGroupItem::dropEvent(QGraphicsSceneDragDropEvent * event)
+{
     QString effects = QString(event->mimeData()->data("kdenlive/effectslist"));
     QDomDocument doc;
     doc.setContent(effects, true);
@@ -213,10 +338,21 @@ void AbstractGroupItem::dropEvent(QGraphicsSceneDragDropEvent * event) {
 }
 
 //virtual
-void AbstractGroupItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event) {
+void AbstractGroupItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
+{
     event->setAccepted(event->mimeData()->hasFormat("kdenlive/effectslist"));
 }
 
-void AbstractGroupItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event) {
+void AbstractGroupItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
+{
     Q_UNUSED(event);
 }
+
+// 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 QGraphicsItem::mousePressEvent(event);
+}