]> git.sesse.net Git - kdenlive/blobdiff - src/onmonitoritems/onmonitorrectitem.cpp
Integrate with the required MLT hooks for getting Movit to work.
[kdenlive] / src / onmonitoritems / onmonitorrectitem.cpp
index ddbb97fc261e5597609908f585ca0d4125ca53c9..fc1b822da8f1c25d4117f97ae93991ba60963cc4 100644 (file)
 #include <QPainter>
 #include <QStyleOptionGraphicsItem>
 #include <QCursor>
+#include <QGraphicsView>
 
-OnMonitorRectItem::OnMonitorRectItem(const QRectF &rect, double dar, MonitorScene *scene, QGraphicsItem* parent) :
-        AbstractOnMonitorItem(scene),
-        QGraphicsRectItem(rect, parent),
-        m_dar(dar)
+OnMonitorRectItem::OnMonitorRectItem(const QRectF &rect, double dar, QGraphicsItem* parent) :
+    QGraphicsRectItem(rect, parent)
+  , m_dar(dar)
+  , m_mode(NoAction)
+  , m_modified(false)
+  , m_view(NULL)
 {
     setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
 
@@ -36,34 +39,40 @@ OnMonitorRectItem::OnMonitorRectItem(const QRectF &rect, double dar, MonitorScen
     framepen.setColor(Qt::yellow);
     setPen(framepen);
     setBrush(Qt::transparent);
+    setAcceptHoverEvents(true);
 }
 
-rectActions OnMonitorRectItem::getMode(QPoint pos)
+rectActions OnMonitorRectItem::getMode(const QPointF &pos)
 {
-    pos = mapFromScene(pos).toPoint();
     // Item mapped coordinates
-    QPolygon pol(rect().normalized().toRect());
+    QPolygonF pol(rect().normalized());
 
-    QPainterPath top(pol.point(0));
-    top.lineTo(pol.point(1));
-    QPainterPath bottom(pol.point(2));
-    bottom.lineTo(pol.point(3));
-    QPainterPath left(pol.point(0));
-    left.lineTo(pol.point(3));
-    QPainterPath right(pol.point(1));
-    right.lineTo(pol.point(2));
+    QPainterPath top(pol.at(0));
+    top.lineTo(pol.at(1));
+    QPainterPath bottom(pol.at(2));
+    bottom.lineTo(pol.at(3));
+    QPainterPath left(pol.at(0));
+    left.lineTo(pol.at(3));
+    QPainterPath right(pol.at(1));
+    right.lineTo(pol.at(2));
 
     QPainterPath mouseArea;
-    mouseArea.addRect(pos.x() - 4, pos.y() - 4, 8, 8);
+    qreal xsize = 12;
+    qreal ysize = 12;
+    if (getView()) {
+        xsize /= m_view->matrix().m11();
+        ysize /= m_view->matrix().m22();
+    }
+    mouseArea.addRect(pos.x() - xsize / 2, pos.y() - ysize / 2, xsize, ysize);
 
     // Check for collisions between the mouse and the borders
-    if (mouseArea.contains(pol.point(0)))
+    if (mouseArea.contains(pol.at(0)))
         return ResizeTopLeft;
-    else if (mouseArea.contains(pol.point(2)))
+    else if (mouseArea.contains(pol.at(2)))
         return ResizeBottomRight;
-    else if (mouseArea.contains(pol.point(1)))
+    else if (mouseArea.contains(pol.at(1)))
         return ResizeTopRight;
-    else if (mouseArea.contains(pol.point(3)))
+    else if (mouseArea.contains(pol.at(3)))
         return ResizeBottomLeft;
     else if (top.intersects(mouseArea))
         return ResizeTop;
@@ -79,32 +88,17 @@ rectActions OnMonitorRectItem::getMode(QPoint pos)
         return NoAction;
 }
 
-/*int OnMonitorRectItem::type() const
-{
-    return Type;
-}*/
-
-void OnMonitorRectItem::slotMousePressed(QGraphicsSceneMouseEvent* event)
+void OnMonitorRectItem::mousePressEvent(QGraphicsSceneMouseEvent* event)
 {
     event->accept();
 
-    if (!isEnabled())
-        return;
-
     m_lastPoint = event->scenePos();
     m_oldRect = rect().normalized();
-    m_mode = getMode(m_lastPoint.toPoint());
+    m_mode = getMode(event->pos());
 }
 
-void OnMonitorRectItem::slotMouseMoved(QGraphicsSceneMouseEvent* event)
+void OnMonitorRectItem::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
 {
-    event->accept();
-
-    if (!isEnabled()) {
-        emit requestCursor(QCursor(Qt::ArrowCursor));
-        return;
-    }
-
     /*if (event->buttons() != Qt::NoButton && (event->screenPos() - m_screenClickPoint).manhattanLength() < QApplication::startDragDistance()) {
      *   event->accept();
      *   return;
@@ -114,7 +108,7 @@ void OnMonitorRectItem::slotMouseMoved(QGraphicsSceneMouseEvent* event)
         QRectF r = rect().normalized();
         QPointF p = pos();
         QPointF mousePos = event->scenePos();
-        QPointF mousePosInRect = mapFromScene(mousePos);
+        QPointF mousePosInRect = event->pos();
         QPointF diff = mousePos - m_lastPoint;
         m_lastPoint = mousePos;
 
@@ -182,7 +176,7 @@ void OnMonitorRectItem::slotMouseMoved(QGraphicsSceneMouseEvent* event)
 
         // Keep aspect ratio
         if (event->modifiers() == Qt::ControlModifier) {
-            // compare to rect during mouse press: 
+            // compare to rect during mouse press:
             // if we subtract rect() we'll get a whole lot of flickering
             // because of diffWidth > diffHeight changing all the time
             int diffWidth = qAbs(r.width() - m_oldRect.width());
@@ -216,53 +210,93 @@ void OnMonitorRectItem::slotMouseMoved(QGraphicsSceneMouseEvent* event)
         }
 
         setRect(r);
-    } else {
-        switch (getMode(event->scenePos().toPoint())) {
-        case ResizeTopLeft:
-        case ResizeBottomRight:
-            emit requestCursor(QCursor(Qt::SizeFDiagCursor));
-            break;
-        case ResizeTopRight:
-        case ResizeBottomLeft:
-            emit requestCursor(QCursor(Qt::SizeBDiagCursor));
-            break;
-        case ResizeTop:
-        case ResizeBottom:
-            emit requestCursor(QCursor(Qt::SizeVerCursor));
-            break;
-        case ResizeLeft:
-        case ResizeRight:
-            emit requestCursor(QCursor(Qt::SizeHorCursor));
-            break;
-        case Move:
-            emit requestCursor(QCursor(Qt::OpenHandCursor));
-            break;
-        default:
-            emit requestCursor(QCursor(Qt::ArrowCursor));
-            break;
+    }
+
+    if (m_modified) {
+        event->accept();
+        if (KdenliveSettings::monitorscene_directupdate()) {
+            emit changed();
+            m_modified = false;
         }
+    } else {
+        event->ignore();
     }
+}
 
-    if (m_modified && KdenliveSettings::monitorscene_directupdate()) {
-        emit actionFinished();
+void OnMonitorRectItem::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
+{
+    if (m_modified) {
         m_modified = false;
+        emit changed();
+    }
+    event->accept();
+}
+
+void OnMonitorRectItem::hoverMoveEvent(QGraphicsSceneHoverEvent* event)
+{
+    switch (getMode(event->pos())) {
+    case ResizeTopLeft:
+    case ResizeBottomRight:
+        setCursor(QCursor(Qt::SizeFDiagCursor));
+        break;
+    case ResizeTopRight:
+    case ResizeBottomLeft:
+        setCursor(QCursor(Qt::SizeBDiagCursor));
+        break;
+    case ResizeTop:
+    case ResizeBottom:
+        setCursor(QCursor(Qt::SizeVerCursor));
+        break;
+    case ResizeLeft:
+    case ResizeRight:
+        setCursor(QCursor(Qt::SizeHorCursor));
+        break;
+    case Move:
+        setCursor(QCursor(Qt::OpenHandCursor));
+        break;
+    default:
+        unsetCursor();
+        break;
     }
 }
 
 void OnMonitorRectItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
 {
-    Q_UNUSED(widget);
+    Q_UNUSED(widget)
+    Q_UNUSED(option)
 
     painter->setPen(pen());
-    painter->drawRect(option->rect);
-
+    //painter->setClipRect(option->rect);
+    const QRectF r = rect();
+    painter->drawRect(r);
+    QRectF handle = painter->worldTransform().inverted().mapRect(QRectF(0, 0, 6, 6));
     if (isEnabled()) {
-        double handleSize = 6 / painter->matrix().m11();
-        double halfHandleSize = handleSize / 2;
-        painter->fillRect(-halfHandleSize, -halfHandleSize, handleSize, handleSize, QColor(Qt::yellow));
-        painter->fillRect(option->rect.width() - halfHandleSize, -halfHandleSize, handleSize, handleSize, QColor(Qt::yellow));
-        painter->fillRect(option->rect.width() - halfHandleSize, option->rect.height() - halfHandleSize, handleSize, handleSize, QColor(Qt::yellow));
-        painter->fillRect(-halfHandleSize, option->rect.height() - halfHandleSize, handleSize, handleSize, QColor(Qt::yellow));
+        handle.moveTopLeft(r.topLeft());
+        painter->fillRect(handle, QColor(Qt::yellow));
+        handle.moveTopRight(r.topRight());
+        painter->fillRect(handle, QColor(Qt::yellow));
+        handle.moveBottomLeft(r.bottomLeft());
+        painter->fillRect(handle, QColor(Qt::yellow));
+        handle.moveBottomRight(r.bottomRight());
+        painter->fillRect(handle, QColor(Qt::yellow));
+    }
+    
+    // Draw cross at center
+    QPointF center = r.center();
+    painter->drawLine(center + QPointF(-handle.width(), 0), center + QPointF(handle.width(), 0));
+    painter->drawLine(center + QPointF(0, handle.height()), center + QPointF(0, -handle.height()));
+}
+
+bool OnMonitorRectItem::getView()
+{
+    if (m_view)
+        return true;
+
+    if (scene() && !scene()->views().isEmpty()) {
+        m_view = scene()->views().first();
+        return true;
+    } else {
+        return false;
     }
 }