]> git.sesse.net Git - kdenlive/blobdiff - src/abstractgroupitem.cpp
forgot to commit that small change, fixes compilation
[kdenlive] / src / abstractgroupitem.cpp
index dc5573d5f681fad58b4517c19031291882256cb9..5d581c22a5164a583995d5677565adf5a29e65ca 100644 (file)
@@ -39,20 +39,26 @@ int AbstractGroupItem::type() const {
     return GROUPWIDGET;
 }
 
+const int AbstractGroupItem::track() const {
+    return (int)(scenePos().y() / KdenliveSettings::trackheight());
+}
+
 CustomTrackScene* AbstractGroupItem::projectScene() {
-    return static_cast <CustomTrackScene*>(scene());
+    if (scene()) return static_cast <CustomTrackScene*>(scene());
+    return NULL;
 }
 
 
-QPainterPath AbstractGroupItem::groupShape(QPointF offset) {
+QPolygonF AbstractGroupItem::groupShape(QPointF offset) {
     QList<QGraphicsItem *> children = childItems();
-    QPainterPath path;
+    QPolygonF path;
     for (int i = 0; i < children.count(); i++) {
-        QRectF r = children.at(i)->sceneBoundingRect();
-        //kDebug()<<"// GROUP CHild: "<<r;
-        //r.translate(offset);
-        path.addRect(r);
+        if (children.at(i)->type() == AVWIDGET) {
+            QPolygonF r = QPolygonF(children.at(i)->sceneBoundingRect());
+            path = path.united(r);
+        }
     }
+    path.translate(offset);
     return path;
 }
 
@@ -79,27 +85,58 @@ QVariant AbstractGroupItem::itemChange(GraphicsItemChange change, const QVariant
     if (change == ItemPositionChange && scene()) {
         // calculate new position.
         QPointF newPos = value.toPointF();
-        QPainterPath sceneShape = groupShape(newPos);
-        QPointF start = sceneBoundingRect().topLeft();
-        QPointF sc = mapToScene(pos());
+        int xpos = projectScene()->getSnapPointForPos((int) newPos.x(), KdenliveSettings::snaptopoints());
+        xpos = qMax(xpos, 0);
+        newPos.setX(xpos);
+
+        //kDebug()<<"// GRP MOVE: "<<pos().y()<<"->"<<newPos.y();
+        QPointF start = pos();//sceneBoundingRect().topLeft();
         int posx = start.x() + newPos.x(); //projectScene()->getSnapPointForPos(start.x() + sc.x(), KdenliveSettings::snaptopoints());
-        //int startx = projectScene()->getSnapPointForPos(start.x(), false);
-        //int startx = projectScene()->getSnapPointForPos(start.x(), false);
-        kDebug() << "------------------------------------";
-        kDebug() << "BRect: " << start.x() << "diff: " << newPos.x() << ",mapd: " << start.x() - sc.x();
-        return newPos;
-        //kDebug()<<"BR: "<<start.x()<<",NP: "<<newPos.x()<<",MAPD: "<<sc.x()<<",POS: "<<pos().x();
-        if (start.x() <= 0) {
-            //kDebug()<<"/// GOING UNDER 0, POS: "<<posx<<", ADJUSTED: items.at(i)->sceneBoundingRect();
-            return pos();
+
+        int startTrack = (start.y() + KdenliveSettings::trackheight() / 2) / KdenliveSettings::trackheight();
+        int newTrack = (newPos.y()) / KdenliveSettings::trackheight();
+        //kDebug()<<"// GROUP NEW T:"<<newTrack<<",START T:"<<startTrack<<",MAX:"<<projectScene()->tracksCount() - 1;
+        newTrack = qMin(newTrack, projectScene()->tracksCount() - (int)(boundingRect().height() + 5) / KdenliveSettings::trackheight());
+        newTrack = qMax(newTrack, 0);
+        newPos.setY((int)((newTrack) * KdenliveSettings::trackheight()));
+
+        //kDebug() << "------------------------------------GRUOP MOVE";
+
+        if (start.x() + newPos.x() - pos().x() < 0) {
+            // If group goes below 0, adjust position to 0
+            return QPointF(pos().x() - start.x(), pos().y());
+        }
+
+        QPolygonF sceneShape = groupShape(newPos - pos());
+        QList<QGraphicsItem*> collindingItems = scene()->items(sceneShape, Qt::IntersectsItemShape);
+        QList<QGraphicsItem *> children = childItems();
+        for (int i = 0; i < children.count(); i++) {
+            collindingItems.removeAll(children.at(i));
+        }
+        if (collindingItems.isEmpty()) return newPos;
+        else {
+            for (int i = 0; i < collindingItems.count(); i++) {
+                QGraphicsItem *collision = collindingItems.at(i);
+                if (collision->type() == AVWIDGET) {
+                    // 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());
+                    }*/
+                }
+            }
+            return newPos;
         }
+
         //else posx -= startx;
         //posx = qMax(posx, 0);
-        newPos.setX(posx);
+        //newPos.setX(posx);
         //kDebug()<<"Y POS: "<<start.y() + newPos.y()<<"SCN MP: "<<sc;
-        int newTrack = (start.y() + newPos.y()) / KdenliveSettings::trackheight();
+        /*int newTrack = (start.y() + newPos.y()) / KdenliveSettings::trackheight();
         int oldTrack = (start.y() + pos().y()) / KdenliveSettings::trackheight();
-        newPos.setY((newTrack) * KdenliveSettings::trackheight() - start.y() + 1);
+        newPos.setY((newTrack) * KdenliveSettings::trackheight() - start.y() + 1);*/
 
 
         //if (start.y() + newPos.y() < 1)  newTrack = oldTrack;
@@ -110,12 +147,6 @@ QVariant AbstractGroupItem::itemChange(GraphicsItemChange change, const QVariant
 
         QList<QGraphicsItem*> items = scene()->items(sceneShape, Qt::IntersectsItemShape);
 
-        QList<QGraphicsItem *> children = childItems();
-        for (int i = 0; i < children.count(); i++) {
-            items.removeAll(children.at(i));
-        }
-
-
 
         if (!items.isEmpty()) {
             for (int i = 0; i < items.count(); i++) {