]> git.sesse.net Git - kdenlive/commitdiff
Fix group move (not perfect but no more crashes)
authorJean-Baptiste Mardelle <jb@kdenlive.org>
Sat, 4 Oct 2008 22:11:52 +0000 (22:11 +0000)
committerJean-Baptiste Mardelle <jb@kdenlive.org>
Sat, 4 Oct 2008 22:11:52 +0000 (22:11 +0000)
svn path=/branches/KDE4/; revision=2431

src/abstractgroupitem.cpp
src/abstractgroupitem.h
src/customtrackview.cpp

index dc5573d5f681fad58b4517c19031291882256cb9..41d05583f01cd96a9d43989161e5f8a4288af24a 100644 (file)
@@ -44,15 +44,16 @@ CustomTrackScene* AbstractGroupItem::projectScene() {
 }
 
 
-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 +80,53 @@ 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 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();
+        int newTrack = (start.y() + newPos.y()) / KdenliveSettings::trackheight();
+        //kDebug()<<"// GROUP NEW TRACK: "<<newTrack<<", START TRACK: "<<startTrack;
+        newTrack = qMin(newTrack, projectScene()->tracksCount() - 1);
+        newTrack = qMax(newTrack, 0);
+        newPos.setY((int)((newTrack - startTrack) * 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 +137,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++) {
index 1945722e5cb5454a186f47c3ca6471d4998517c6..ec4268cf264a57f428cfe4fd00f728ba6b988dc6 100644 (file)
@@ -40,7 +40,7 @@ protected:
     virtual void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *);
 
 private:
-    QPainterPath groupShape(QPointF);
+    QPolygonF groupShape(QPointF);
     void fixItemRect();
     double m_fps;
 };
index be2539f0270097d2bea0e49e5a941b984dae5480..45a1084aab81ab2c2e7787480b688e25b5aff446 100644 (file)
@@ -522,7 +522,10 @@ void CustomTrackView::mousePressEvent(QMouseEvent * event) {
         if (m_dragItem) {
             if (!m_dragItem->isSelected()) {
                 m_scene->clearSelection();
-                if (m_selectionGroup) scene()->destroyItemGroup(m_selectionGroup);
+                if (m_selectionGroup) {
+                    scene()->destroyItemGroup(m_selectionGroup);
+                    m_selectionGroup = NULL;
+                }
                 m_dragItem->setSelected(true);
             }
         }
@@ -536,7 +539,10 @@ void CustomTrackView::mousePressEvent(QMouseEvent * event) {
 
     // No item under click
     if (m_dragItem == NULL) {
-        if (m_selectionGroup) scene()->destroyItemGroup(m_selectionGroup);
+        if (m_selectionGroup) {
+            scene()->destroyItemGroup(m_selectionGroup);
+            m_selectionGroup = NULL;
+        }
         setCursor(Qt::ArrowCursor);
         m_scene->clearSelection();
         setCursorPos((int)(mapToScene(event->x(), 0).x()));
@@ -564,14 +570,14 @@ void CustomTrackView::mousePressEvent(QMouseEvent * event) {
     if (m_dragItem && m_dragItem->type() == AVWIDGET) emit clipItemSelected((ClipItem*) m_dragItem);
     else emit clipItemSelected(NULL);
 
-    if (m_dragItem && m_operationMode == NONE) QGraphicsView::mousePressEvent(event);
-
     if (m_selectionGroup) {
         // delete selection group
         scene()->destroyItemGroup(m_selectionGroup);
         m_selectionGroup = NULL;
     }
 
+    if (m_dragItem && m_operationMode == NONE) QGraphicsView::mousePressEvent(event);
+
     QList<QGraphicsItem *> selection = m_scene->selectedItems();
     if (selection.count() > 1) {
         m_selectionGroup = new AbstractGroupItem(m_document->fps());