]> git.sesse.net Git - kdenlive/blobdiff - src/graphicsscenerectmove.cpp
[PATCH by Ray Lehtiniem] Delete some unused variables
[kdenlive] / src / graphicsscenerectmove.cpp
index 658871fe4e8ee332c2efc3bf054167bfe3a10cdc..7126a93c85dd24f5dc698e6d51536f57f5a85632 100644 (file)
@@ -1,3 +1,24 @@
+/***************************************************************************
+ *   copyright (C) 2008 by Marco Gittler                                   *
+ *   Copyright (C) 2008 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ *   This program is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+ *   GNU General Public License for more details.                          *
+ *                                                                         *
+ *   You should have received a copy of the GNU General Public License     *
+ *   along with this program; if not, write to the                         *
+ *   Free Software Foundation, Inc.,                                       *
+ *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
+ ***************************************************************************/
+
+#include "graphicsscenerectmove.h"
 
 #include <KDebug>
 #include <QGraphicsSceneMouseEvent>
 #include <QGraphicsSvgItem>
 #include <QGraphicsView>
 #include <QCursor>
+#include <QTextCursor>
 #include <QList>
 #include <QKeyEvent>
 #include <QApplication>
+#include <QTextBlock>
 
-#include "graphicsscenerectmove.h"
 
 GraphicsSceneRectMove::GraphicsSceneRectMove(QObject *parent): QGraphicsScene(parent), m_selectedItem(NULL), resizeMode(NoResize), m_tool(TITLE_RECTANGLE) {
     //grabMouse();
     zoom = 1.0;
-    setBackgroundBrush(QBrush(QColor(0, 0, 0, 0)));
+    setBackgroundBrush(QBrush(Qt::transparent));
 }
 
 void GraphicsSceneRectMove::setSelectedItem(QGraphicsItem *item) {
@@ -49,6 +71,13 @@ void GraphicsSceneRectMove::keyPressEvent(QKeyEvent * keyEvent) {
         return;
     }
     int diff = 1;
+    if (m_selectedItem->type() == 8) {
+        QGraphicsTextItem *t = static_cast<QGraphicsTextItem *>(m_selectedItem);
+        if (t->textInteractionFlags() & Qt::TextEditorInteraction) {
+            QGraphicsScene::keyPressEvent(keyEvent);
+            return;
+        }
+    }
     if (keyEvent->modifiers() & Qt::ControlModifier) diff = 10;
     switch (keyEvent->key()) {
     case Qt::Key_Left:
@@ -76,6 +105,7 @@ void GraphicsSceneRectMove::keyPressEvent(QKeyEvent * keyEvent) {
     default:
         QGraphicsScene::keyPressEvent(keyEvent);
     }
+    emit actionFinished();
 }
 
 //virtual
@@ -88,6 +118,7 @@ void GraphicsSceneRectMove::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* e) {
     if (g) {
         if (g->type() == 8) {
             QGraphicsTextItem *t = static_cast<QGraphicsTextItem *>(g);
+            m_selectedItem = g;
             t->setTextInteractionFlags(Qt::TextEditorInteraction);
         }
     }
@@ -96,15 +127,16 @@ void GraphicsSceneRectMove::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* e) {
 
 void GraphicsSceneRectMove::mouseReleaseEvent(QGraphicsSceneMouseEvent *e) {
     if (m_tool == TITLE_RECTANGLE && m_selectedItem) setSelectedItem(m_selectedItem);
-    emit actionFinished();
     QGraphicsScene::mouseReleaseEvent(e);
+    emit actionFinished();
 }
 
 void GraphicsSceneRectMove::mousePressEvent(QGraphicsSceneMouseEvent* e) {
+    m_clickPoint = e->screenPos();
     QPointF p = e->scenePos();
     p += QPoint(-2, -2);
     resizeMode = NoResize;
-    QList <QGraphicsItem *> list = items(QRectF(p , QSizeF(4, 4)).toRect());
+    const QList <QGraphicsItem *> list = items(QRectF(p , QSizeF(4, 4)).toRect());
     QGraphicsItem *item = NULL;
     bool hasSelected = false;
 
@@ -122,6 +154,9 @@ void GraphicsSceneRectMove::mousePressEvent(QGraphicsSceneMouseEvent* e) {
             if (m_selectedItem && m_selectedItem->type() == 8) {
                 // disable text editing
                 QGraphicsTextItem *t = static_cast<QGraphicsTextItem *>(m_selectedItem);
+                t->textCursor().setPosition(0);
+                QTextBlock cur = t->textCursor().block();
+                t->setTextCursor(QTextCursor(cur));
                 t->setTextInteractionFlags(Qt::NoTextInteraction);
             }
             m_selectedItem = NULL;
@@ -133,7 +168,7 @@ void GraphicsSceneRectMove::mousePressEvent(QGraphicsSceneMouseEvent* e) {
             }
         }
         if (item != NULL) {
-            m_clickPoint = e->scenePos();
+            m_sceneClickPoint = e->scenePos();
             m_selectedItem = item;
             kDebug() << "/////////  ITEM TYPE: " << item->type();
             if (item->type() == 8) {
@@ -145,7 +180,11 @@ void GraphicsSceneRectMove::mousePressEvent(QGraphicsSceneMouseEvent* e) {
                 t->setTextInteractionFlags(Qt::NoTextInteraction);
                 setCursor(Qt::ClosedHandCursor);
             } else if (item->type() == 3 || item->type() == 13 || item->type() == 7) {
-                QRectF r = item->boundingRect();
+                QRectF r;
+                if (m_selectedItem->type() == 3) {
+                    r = ((QGraphicsRectItem*)m_selectedItem)->rect();
+                } else r = m_selectedItem->boundingRect();
+
                 r.translate(item->scenePos());
                 if ((r.toRect().topLeft() - e->scenePos().toPoint()).manhattanLength() < 6 / zoom) {
                     resizeMode = TopLeft;
@@ -168,7 +207,7 @@ void GraphicsSceneRectMove::mousePressEvent(QGraphicsSceneMouseEvent* e) {
         }
         QGraphicsScene::mousePressEvent(e);
     } else if (m_tool == TITLE_RECTANGLE) {
-        m_clickPoint = e->scenePos();
+        m_sceneClickPoint = e->scenePos();
         m_selectedItem = NULL;
     } else if (m_tool == TITLE_TEXT) {
         m_selectedItem = addText(QString());
@@ -183,13 +222,32 @@ void GraphicsSceneRectMove::mousePressEvent(QGraphicsSceneMouseEvent* e) {
 
 }
 
+void GraphicsSceneRectMove::clearTextSelection() {
+    if (m_selectedItem && m_selectedItem->type() == 8) {
+        // disable text editing
+        QGraphicsTextItem *t = static_cast<QGraphicsTextItem *>(m_selectedItem);
+        t->textCursor().setPosition(0);
+        QTextBlock cur = t->textCursor().block();
+        t->setTextCursor(QTextCursor(cur));
+        t->setTextInteractionFlags(Qt::NoTextInteraction);
+    }
+    m_selectedItem = NULL;
+    clearSelection();
+}
 
 //virtual
 void GraphicsSceneRectMove::mouseMoveEvent(QGraphicsSceneMouseEvent* e) {
-
+    if ((e->screenPos() - m_clickPoint).manhattanLength() < QApplication::startDragDistance()) {
+        e->accept();
+        return;
+    }
     if (m_selectedItem && e->buttons() & Qt::LeftButton) {
         if (m_selectedItem->type() == 3 || m_selectedItem->type() == 13 || m_selectedItem->type() == 7) {
-            QRectF newrect = m_selectedItem->boundingRect();
+            QRectF newrect;
+            if (m_selectedItem->type() == 3) {
+                newrect = ((QGraphicsRectItem*)m_selectedItem)->rect();
+            } else newrect = m_selectedItem->boundingRect();
+
             QPointF newpoint = e->scenePos();
             //newpoint -= m_selectedItem->scenePos();
             switch (resizeMode) {
@@ -223,12 +281,12 @@ void GraphicsSceneRectMove::mouseMoveEvent(QGraphicsSceneMouseEvent* e) {
                 newrect.setBottom(newpoint.y() - m_selectedItem->pos().y());
                 break;
             default:
-                QPointF diff = e->scenePos() - m_clickPoint;
-                m_clickPoint = e->scenePos();
+                QPointF diff = e->scenePos() - m_sceneClickPoint;
+                m_sceneClickPoint = e->scenePos();
                 m_selectedItem->moveBy(diff.x(), diff.y());
                 break;
             }
-            if (m_selectedItem->type() == 3) {
+            if (m_selectedItem->type() == 3 && resizeMode != NoResize) {
                 QGraphicsRectItem *gi = (QGraphicsRectItem*)m_selectedItem;
                 gi->setRect(newrect);
             }
@@ -250,13 +308,12 @@ void GraphicsSceneRectMove::mouseMoveEvent(QGraphicsSceneMouseEvent* e) {
                 QGraphicsScene::mouseMoveEvent(e);
                 return;
             }
-            QPointF diff = e->scenePos() - m_clickPoint;
-            m_clickPoint = e->scenePos();
+            QPointF diff = e->scenePos() - m_sceneClickPoint;
+            m_sceneClickPoint = e->scenePos();
             m_selectedItem->moveBy(diff.x(), diff.y());
         }
         emit itemMoved();
     } else if (m_tool == TITLE_SELECT) {
-
         QPointF p = e->scenePos();
         p += QPoint(-2, -2);
         resizeMode = NoResize;
@@ -267,7 +324,7 @@ void GraphicsSceneRectMove::mouseMoveEvent(QGraphicsSceneMouseEvent* e) {
                 setCursor(Qt::OpenHandCursor);
                 break;
             } else if (g->type() == 3 && g->zValue() > -1000) {
-                QRectF r = g->boundingRect();
+                QRectF r = ((QGraphicsRectItem*)g)->rect();
                 r.translate(g->scenePos());
                 itemFound = true;
                 if ((r.toRect().topLeft() - e->scenePos().toPoint()).manhattanLength() < 6 / zoom) {
@@ -293,12 +350,12 @@ void GraphicsSceneRectMove::mouseMoveEvent(QGraphicsSceneMouseEvent* e) {
         }
         QGraphicsScene::mouseMoveEvent(e);
     } else if (m_tool == TITLE_RECTANGLE && e->buttons() & Qt::LeftButton) {
-        if (m_selectedItem == NULL && (m_clickPoint.toPoint() - e->scenePos().toPoint()).manhattanLength() >= QApplication::startDragDistance()) {
+        if (m_selectedItem == NULL && (m_clickPoint - e->screenPos()).manhattanLength() >= QApplication::startDragDistance()) {
             // create new rect item
-            m_selectedItem = addRect(0, 0, e->scenePos().x() - m_clickPoint.x(), e->scenePos().y() - m_clickPoint.y());
+            m_selectedItem = addRect(0, 0, e->scenePos().x() - m_sceneClickPoint.x(), e->scenePos().y() - m_sceneClickPoint.y());
             emit newRect((QGraphicsRectItem *) m_selectedItem);
             m_selectedItem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
-            m_selectedItem->setPos(m_clickPoint);
+            m_selectedItem->setPos(m_sceneClickPoint);
             resizeMode = BottomRight;
             QGraphicsScene::mouseMoveEvent(e);
         }
@@ -308,7 +365,6 @@ void GraphicsSceneRectMove::mouseMoveEvent(QGraphicsSceneMouseEvent* e) {
 void GraphicsSceneRectMove::wheelEvent(QGraphicsSceneWheelEvent * wheelEvent) {
     QList<QGraphicsView*> viewlist = views();
     //kDebug() << wheelEvent->delta() << " " << zoom;
-    double scale = 1.0;
     if (viewlist.size() > 0) {
         if (wheelEvent->delta() < 0) emit sceneZoom(true);
         else emit sceneZoom(false);
@@ -338,7 +394,7 @@ void GraphicsSceneRectMove::setZoom(double s) {
 }
 
 void GraphicsSceneRectMove::setCursor(QCursor c) {
-    QList<QGraphicsView*> l = views();
+    const QList<QGraphicsView*> l = views();
     foreach(QGraphicsView* v, l) {
         v->setCursor(c);
     }