]> git.sesse.net Git - kdenlive/blobdiff - src/abstractgroupitem.cpp
Various fixes to improve general stability in Qt 4.5.2
[kdenlive] / src / abstractgroupitem.cpp
index f2c9e90151eb9fe15300eded166303f98e7f4f93..a54cd93f3a334c458b76da7da9caade6506b8704 100644 (file)
 #include <QStyleOptionGraphicsItem>
 #include <QDomDocument>
 #include <QMimeData>
-
-
-AbstractGroupItem::AbstractGroupItem(double fps): QGraphicsItemGroup(), m_fps(fps) {
-    setZValue(2);
-    setFlags(QGraphicsItem::ItemClipsToShape | QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
+#include <QGraphicsSceneMouseEvent>
+
+AbstractGroupItem::AbstractGroupItem(double /* fps */) :
+        QObject(),
+        QGraphicsItemGroup()
+{
+    setZValue(1);
+    setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
     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() {
+CustomTrackScene* AbstractGroupItem::projectScene()
+{
     if (scene()) return static_cast <CustomTrackScene*>(scene());
     return NULL;
 }
 
-QPainterPath AbstractGroupItem::groupShape(QPointF offset) {
+QPainterPath AbstractGroupItem::groupShape(QPointF offset)
+{
     QPainterPath path;
     QList<QGraphicsItem *> children = childItems();
     for (int i = 0; i < children.count(); i++) {
@@ -64,12 +71,14 @@ QPainterPath AbstractGroupItem::groupShape(QPointF offset) {
     return path;
 }
 
-void AbstractGroupItem::addItem(QGraphicsItem * item) {
+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,41 +94,49 @@ 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);
+    p->fillRect(option->exposedRect, bgcolor);
+    p->setClipping(false);
     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) {
+QVariant AbstractGroupItem::itemChange(GraphicsItemChange change, const QVariant &value)
+{
     if (change == ItemPositionChange && scene()) {
         // calculate new position.
         const int trackHeight = KdenliveSettings::trackheight();
+        QPointF start = sceneBoundingRect().topLeft();
         QPointF newPos = value.toPointF();
-        int xpos = projectScene()->getSnapPointForPos((int) newPos.x(), KdenliveSettings::snaptopoints());
+        //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);
-        newPos.setX(xpos);
-        QPointF start = pos();
-        kDebug() << "// GRP MOVE, NEW:" << newPos << ", CURR:" << start;
+        //kDebug()<<"GRP XPOS:"<<xpos<<", START:"<<start.x()<<",NEW:"<<newPos.x()<<"; SCENE:"<<scenePos().x()<<",POS:"<<pos().x();
+        newPos.setX((int)(pos().x() + xpos - (int) start.x()));
+
         //int startTrack = (start.y() + trackHeight / 2) / trackHeight;
-        int newTrack = (newPos.y()) / trackHeight;
 
-        //kDebug()<<"// GROUP NEW T:"<<newTrack<<",START T:"<<startTrack<<",MAX:"<<projectScene()->tracksCount() - 1;
-        newTrack = qMin(newTrack, projectScene()->tracksCount() - (int)(boundingRect().height() + 5) / trackHeight);
-        newTrack = qMax(newTrack, 0);
+        int realTrack = (start.y() + newPos.y() - pos().y()) / trackHeight;
+        int proposedTrack = newPos.y() / trackHeight;
+
+        int correctedTrack = qMin(realTrack, projectScene()->tracksCount() - (int)(boundingRect().height() + 5) / trackHeight);
+        correctedTrack = qMax(correctedTrack, 0);
+
+        proposedTrack += (correctedTrack - realTrack);
 
         // Check if top item is a clip or a transition
         int offset = 0;
         int topTrack = -1;
-        kDebug() << "// CHG GRP 1";
         QList<QGraphicsItem *> children = childItems();
         for (int i = 0; i < children.count(); i++) {
             int currentTrack = (int)(children.at(i)->scenePos().y() / trackHeight);
@@ -135,35 +152,31 @@ QVariant AbstractGroupItem::itemChange(GraphicsItemChange change, const QVariant
                 }
             }
         }
-        kDebug() << "// CHG GRP 2";
-        newPos.setY((int)((newTrack) * trackHeight) + offset);
-        if (newPos == start) return start;
+        newPos.setY((int)((proposedTrack) * trackHeight) + offset);
+        //if (newPos == start) return start;
 
-        if (newPos.x() < 0) {
+        /*if (newPos.x() < 0) {
             // If group goes below 0, adjust position to 0
             return QPointF(pos().x() - start.x(), pos().y());
-        }
-        kDebug() << "// CHG GRP 3";
+        }*/
         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));
         }
 
-        kDebug() << "// CHG GRP 4" << newPos;
         if (collindingItems.isEmpty()) return newPos;
         else {
-            bool forwardMove = newPos.x() > start.x();
+            bool forwardMove = xpos > start.x();
             int offset = 0;
-            kDebug() << "// CHG GRP 5";
             for (int i = 0; i < collindingItems.count(); i++) {
                 QGraphicsItem *collision = collindingItems.at(i);
                 if (collision->type() == AVWIDGET) {
                     // Collision
                     //kDebug()<<"// COLLISION WIT:"<<collision->sceneBoundingRect();
-                    if (newPos.y() != start.y()) {
+                    if (newPos.y() != pos().y()) {
                         // Track change results in collision, restore original position
-                        return start;
+                        return pos();
                     }
                     AbstractClipItem *item = static_cast <AbstractClipItem *>(collision);
                     if (forwardMove) {
@@ -202,7 +215,8 @@ QVariant AbstractGroupItem::itemChange(GraphicsItemChange change, const QVariant
 }
 
 //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);
@@ -212,10 +226,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);
+}