]> git.sesse.net Git - kdenlive/blobdiff - src/abstractscopewidget.cpp
First step for implementation of proxy editing (for testing purpose only)
[kdenlive] / src / abstractscopewidget.cpp
index bbc5761a773e0de1d478db76b5782af8a310b019..a3b328cd9357485094f40cc1916595ae925e5613 100644 (file)
 #include <QMouseEvent>
 #include <QPainter>
 
+// Uncomment for Scope debugging.
+//#define DEBUG_ASW
+
+#ifdef DEBUG_ASW
+#include <QDebug>
+#endif
+
 const int REALTIME_FPS = 30;
 
 const QColor light(250, 238, 226, 255);
 const QColor dark(40,  40,  39, 255);
 const QColor dark2(25,  25,  23, 255);
-
-const QPen AbstractScopeWidget::penThick(QBrush(QColor(250, 250, 250)),     2, Qt::SolidLine);
-const QPen AbstractScopeWidget::penThin(QBrush(QColor(250, 250, 250)),     1, Qt::SolidLine);
-const QPen AbstractScopeWidget::penLight(QBrush(QColor(200, 200, 250, 150)), 1, Qt::SolidLine);
-const QPen AbstractScopeWidget::penDark(QBrush(QColor(0, 0, 20, 250)),      1, Qt::SolidLine);
-
-AbstractScopeWidget::AbstractScopeWidget(Monitor *projMonitor, Monitor *clipMonitor, bool trackMouse, QWidget *parent) :
+const QColor AbstractScopeWidget::colHighlightLight(18, 130, 255, 255);
+const QColor AbstractScopeWidget::colHighlightDark(255, 64, 19, 255);
+const QColor AbstractScopeWidget::colDarkWhite(250, 250, 250);
+
+const QPen AbstractScopeWidget::penThick(QBrush(AbstractScopeWidget::colDarkWhite.rgb()),           2, Qt::SolidLine);
+const QPen AbstractScopeWidget::penThin(QBrush(AbstractScopeWidget::colDarkWhite.rgb()),            1, Qt::SolidLine);
+const QPen AbstractScopeWidget::penLight(QBrush(QColor(200, 200, 250, 150)),                        1, Qt::SolidLine);
+const QPen AbstractScopeWidget::penLightDots(QBrush(QColor(200, 200, 250, 150)),                    1, Qt::DotLine);
+const QPen AbstractScopeWidget::penLighter(QBrush(QColor(225, 225, 250, 225)),                      1, Qt::SolidLine);
+const QPen AbstractScopeWidget::penDark(QBrush(QColor(0, 0, 20, 250)),                              1, Qt::SolidLine);
+const QPen AbstractScopeWidget::penDarkDots(QBrush(QColor(0, 0, 20, 250)),                          1, Qt::DotLine);
+const QPen AbstractScopeWidget::penBackground(QBrush(dark2),                                        1, Qt::SolidLine);
+
+const QString AbstractScopeWidget::directions[] =  {"North", "Northeast", "East", "Southeast"};
+
+AbstractScopeWidget::AbstractScopeWidget(bool trackMouse, QWidget *parent) :
         QWidget(parent),
-        m_projMonitor(projMonitor),
-        m_clipMonitor(clipMonitor),
         m_mousePos(0, 0),
         m_mouseWithinWidget(false),
         offset(5),
@@ -44,7 +58,10 @@ AbstractScopeWidget::AbstractScopeWidget(Monitor *projMonitor, Monitor *clipMoni
         m_semaphoreHUD(1),
         m_semaphoreScope(1),
         m_semaphoreBackground(1),
-        initialDimensionUpdateDone(false)
+        initialDimensionUpdateDone(false),
+        m_requestForcedUpdate(false),
+        m_rescaleMinDist(4),
+        m_rescaleVerticalThreshold(2.0f)
 
 {
     m_scopePalette = QPalette();
@@ -69,14 +86,9 @@ AbstractScopeWidget::AbstractScopeWidget(Monitor *projMonitor, Monitor *clipMoni
 
     this->setContextMenuPolicy(Qt::CustomContextMenu);
 
-    m_activeRender = (m_clipMonitor->isActive()) ? m_clipMonitor->render : m_projMonitor->render;
-
     bool b = true;
     b &= connect(this, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(customContextMenuRequested(QPoint)));
 
-    //b &= connect(m_activeRender, SIGNAL(rendererPosition(int)), this, SLOT(slotRenderZoneUpdated()));
-    b &= connect(m_activeRender, SIGNAL(frameUpdated(QImage)), this, SLOT(slotRenderZoneUpdated(QImage)));
-
     b &= connect(this, SIGNAL(signalHUDRenderingFinished(uint, uint)), this, SLOT(slotHUDRenderingFinished(uint, uint)));
     b &= connect(this, SIGNAL(signalScopeRenderingFinished(uint, uint)), this, SLOT(slotScopeRenderingFinished(uint, uint)));
     b &= connect(this, SIGNAL(signalBackgroundRenderingFinished(uint, uint)), this, SLOT(slotBackgroundRenderingFinished(uint, uint)));
@@ -131,7 +143,9 @@ QString AbstractScopeWidget::configName()
 void AbstractScopeWidget::prodHUDThread()
 {
     if (this->visibleRegion().isEmpty()) {
-//        qDebug() << "Scope " << m_widgetName << " is not visible. Not calculating HUD.";
+#ifdef DEBUG_ASW
+        qDebug() << "Scope " << m_widgetName << " is not visible. Not calculating HUD.";
+#endif
     } else {
         if (m_semaphoreHUD.tryAcquire(1)) {
             Q_ASSERT(!m_threadHUD.isRunning());
@@ -139,20 +153,27 @@ void AbstractScopeWidget::prodHUDThread()
             m_newHUDFrames.fetchAndStoreRelaxed(0);
             m_newHUDUpdates.fetchAndStoreRelaxed(0);
             m_threadHUD = QtConcurrent::run(this, &AbstractScopeWidget::renderHUD, m_accelFactorHUD);
-//            qDebug() << "HUD thread started in " << m_widgetName;
+#ifdef DEBUG_ASW
+            qDebug() << "HUD thread started in " << m_widgetName;
+#endif
 
-        } else {
-//            qDebug() << "HUD semaphore locked, not prodding in " << m_widgetName << ". Thread running: " << m_threadHUD.isRunning();
         }
+#ifdef DEBUG_ASW
+        else {
+            qDebug() << "HUD semaphore locked, not prodding in " << m_widgetName << ". Thread running: " << m_threadHUD.isRunning();
+        }
+#endif
     }
 }
 
 void AbstractScopeWidget::prodScopeThread()
 {
     // Only start a new thread if the scope is actually visible
-    // and not hidden by another widget on the stack.
-    if (this->visibleRegion().isEmpty()) {
-//        qDebug() << "Scope " << m_widgetName << " is not visible. Not calculating scope.";
+    // and not hidden by another widget on the stack and if user want the scope to update.
+    if (this->visibleRegion().isEmpty() || (!m_aAutoRefresh->isChecked() && !m_requestForcedUpdate)) {
+#ifdef DEBUG_ASW
+        qDebug() << "Scope " << m_widgetName << " is not visible. Not calculating scope.";
+#endif
     } else {
         // Try to acquire the semaphore. This must only succeed if m_threadScope is not running
         // anymore. Therefore the semaphore must NOT be released before m_threadScope ends.
@@ -167,19 +188,26 @@ void AbstractScopeWidget::prodScopeThread()
 
             // See http://doc.qt.nokia.com/latest/qtconcurrentrun.html#run about
             // running member functions in a thread
-            m_threadScope = QtConcurrent::run(this, &AbstractScopeWidget::renderScope, m_accelFactorScope, m_scopeImage);
+            m_threadScope = QtConcurrent::run(this, &AbstractScopeWidget::renderScope, m_accelFactorScope);
+            m_requestForcedUpdate = false;
 
-//            qDebug() << "Scope thread started in " << m_widgetName;
+#ifdef DEBUG_ASW
+            qDebug() << "Scope thread started in " << m_widgetName;
+#endif
 
         } else {
-//            qDebug() << "Scope semaphore locked, not prodding in " << m_widgetName << ". Thread running: " << m_threadScope.isRunning();
+#ifdef DEBUG_ASW
+            qDebug() << "Scope semaphore locked, not prodding in " << m_widgetName << ". Thread running: " << m_threadScope.isRunning();
+#endif
         }
     }
 }
 void AbstractScopeWidget::prodBackgroundThread()
 {
     if (this->visibleRegion().isEmpty()) {
-//        qDebug() << "Scope " << m_widgetName << " is not visible. Not calculating background.";
+#ifdef DEBUG_ASW
+        qDebug() << "Scope " << m_widgetName << " is not visible. Not calculating background.";
+#endif
     } else {
         if (m_semaphoreBackground.tryAcquire(1)) {
             Q_ASSERT(!m_threadBackground.isRunning());
@@ -187,20 +215,29 @@ void AbstractScopeWidget::prodBackgroundThread()
             m_newBackgroundFrames.fetchAndStoreRelaxed(0);
             m_newBackgroundUpdates.fetchAndStoreRelaxed(0);
             m_threadBackground = QtConcurrent::run(this, &AbstractScopeWidget::renderBackground, m_accelFactorBackground);
-//            qDebug() << "Background thread started in " << m_widgetName;
+
+#ifdef DEBUG_ASW
+            qDebug() << "Background thread started in " << m_widgetName;
+#endif
 
         } else {
-//            qDebug() << "Background semaphore locked, not prodding in " << m_widgetName << ". Thread running: " << m_threadBackground.isRunning();
+#ifdef DEBUG_ASW
+            qDebug() << "Background semaphore locked, not prodding in " << m_widgetName << ". Thread running: " << m_threadBackground.isRunning();
+#endif
         }
     }
 }
 
 void AbstractScopeWidget::forceUpdate(bool doUpdate)
 {
-//    qDebug() << "Force update called in " << widgetName() << ". Arg: " << doUpdate;
+#ifdef DEBUG_ASW
+    qDebug() << "Forced update called in " << widgetName() << ". Arg: " << doUpdate;
+#endif
+
     if (!doUpdate) {
         return;
     }
+    m_requestForcedUpdate = true;
     m_newHUDUpdates.fetchAndAddRelaxed(1);
     m_newScopeUpdates.fetchAndAddRelaxed(1);
     m_newBackgroundUpdates.fetchAndAddRelaxed(1);
@@ -217,6 +254,7 @@ void AbstractScopeWidget::forceUpdateHUD()
 void AbstractScopeWidget::forceUpdateScope()
 {
     m_newScopeUpdates.fetchAndAddRelaxed(1);
+    m_requestForcedUpdate = true;
     prodScopeThread();
 
 }
@@ -230,20 +268,11 @@ void AbstractScopeWidget::forceUpdateBackground()
 
 ///// Events /////
 
-void AbstractScopeWidget::mouseReleaseEvent(QMouseEvent *event)
-{
-    if (!m_aAutoRefresh->isChecked()) m_activeRender->sendFrameUpdate();
-    prodHUDThread();
-    prodScopeThread();
-    prodBackgroundThread();
-    QWidget::mouseReleaseEvent(event);
-}
 
 void AbstractScopeWidget::resizeEvent(QResizeEvent *event)
 {
     // Update the dimension of the available rect for painting
     m_scopeRect = scopeRect();
-
     forceUpdate();
 
     QWidget::resizeEvent(event);
@@ -263,12 +292,88 @@ void AbstractScopeWidget::paintEvent(QPaintEvent *)
     davinci.drawImage(m_scopeRect.topLeft(), m_imgHUD);
 }
 
+void AbstractScopeWidget::mousePressEvent(QMouseEvent *event)
+{
+    if (event->button() == Qt::LeftButton) {
+        // Rescaling mode starts
+        m_rescaleActive = true;
+        m_rescalePropertiesLocked = false;
+        m_rescaleFirstRescaleDone = false;
+        m_rescaleStartPoint = event->pos();
+        m_rescaleModifiers = event->modifiers();
+    }
+}
+void AbstractScopeWidget::mouseReleaseEvent(QMouseEvent *event)
+{
+    m_rescaleActive = false;
+    m_rescalePropertiesLocked = false;
+
+    if (!m_aAutoRefresh->isChecked()) {
+        m_requestForcedUpdate = true;
+    }
+    prodHUDThread();
+    prodScopeThread();
+    prodBackgroundThread();
+    QWidget::mouseReleaseEvent(event);
+}
 void AbstractScopeWidget::mouseMoveEvent(QMouseEvent *event)
 {
     m_mousePos = event->pos();
     m_mouseWithinWidget = true;
     emit signalMousePositionChanged();
+
+    QPoint movement = event->pos()-m_rescaleStartPoint;
+
+    if (m_rescaleActive) {
+        if (m_rescalePropertiesLocked) {
+            // Direction is known, now adjust parameters
+
+            // Reset the starting point to make the next moveEvent relative to the current one
+            m_rescaleStartPoint = event->pos();
+
+
+            if (!m_rescaleFirstRescaleDone) {
+                // We have just learned the desired direction; Normalize the movement to one pixel
+                // to avoid a jump by m_rescaleMinDist
+
+                if (movement.x() != 0) {
+                    movement.setX(movement.x() / abs(movement.x()));
+                }
+                if (movement.y() != 0) {
+                    movement.setY(movement.y() / abs(movement.y()));
+                }
+
+                m_rescaleFirstRescaleDone = true;
+            }
+
+            handleMouseDrag(movement, m_rescaleDirection, m_rescaleModifiers);
+
+
+
+        } else {
+            // Detect the movement direction here.
+            // This algorithm relies on the aspect ratio of dy/dx (size and signum).
+            if (movement.manhattanLength() > m_rescaleMinDist) {
+                float diff = ((float) movement.y())/movement.x();
+
+                if (abs(diff) > m_rescaleVerticalThreshold || movement.x() == 0) {
+                    m_rescaleDirection = North;
+                } else if (abs(diff) < 1/m_rescaleVerticalThreshold) {
+                    m_rescaleDirection = East;
+                } else if (diff < 0) {
+                    m_rescaleDirection = Northeast;
+                } else {
+                    m_rescaleDirection = Southeast;
+                }
+#ifdef DEBUG_ASW
+                qDebug() << "Diff is " << diff << "; chose " << directions[m_rescaleDirection] << " as direction";
+#endif
+                m_rescalePropertiesLocked = true;
+            }
+        }
+    }
 }
+
 void AbstractScopeWidget::leaveEvent(QEvent *)
 {
     m_mouseWithinWidget = false;
@@ -298,7 +403,9 @@ uint AbstractScopeWidget::calculateAccelFactorBackground(uint oldMseconds, uint)
 
 void AbstractScopeWidget::slotHUDRenderingFinished(uint mseconds, uint oldFactor)
 {
-//    qDebug() << "HUD rendering has finished, waiting for termination in " << m_widgetName;
+#ifdef DEBUG_ASW
+    qDebug() << "HUD rendering has finished in " << mseconds << " ms, waiting for termination in " << m_widgetName;
+#endif
     m_threadHUD.waitForFinished();
     m_imgHUD = m_threadHUD.result();
 
@@ -315,8 +422,10 @@ void AbstractScopeWidget::slotHUDRenderingFinished(uint mseconds, uint oldFactor
     }
 
     if ((m_newHUDFrames > 0 && m_aAutoRefresh->isChecked()) || m_newHUDUpdates > 0) {
-//        qDebug() << "Trying to start a new HUD thread for " << m_widgetName
-//                << ". New frames/updates: " << m_newHUDFrames << "/" << m_newHUDUpdates;
+#ifdef DEBUG_ASW
+        qDebug() << "Trying to start a new HUD thread for " << m_widgetName
+                << ". New frames/updates: " << m_newHUDFrames << "/" << m_newHUDUpdates;
+#endif
         prodHUDThread();;
     }
 }
@@ -325,7 +434,9 @@ void AbstractScopeWidget::slotScopeRenderingFinished(uint mseconds, uint oldFact
 {
     // The signal can be received before the thread has really finished. So we
     // need to wait until it has really finished before starting a new thread.
-//    qDebug() << "Scope rendering has finished, waiting for termination in " << m_widgetName;
+#ifdef DEBUG_ASW
+    qDebug() << "Scope rendering has finished in " << mseconds << " ms, waiting for termination in " << m_widgetName;
+#endif
     m_threadScope.waitForFinished();
     m_imgScope = m_threadScope.result();
 
@@ -349,15 +460,19 @@ void AbstractScopeWidget::slotScopeRenderingFinished(uint mseconds, uint oldFact
     }
 
     if ((m_newScopeFrames > 0 && m_aAutoRefresh->isChecked()) || m_newScopeUpdates > 0) {
-//        qDebug() << "Trying to start a new scope thread for " << m_widgetName
-//                << ". New frames/updates: " << m_newScopeFrames << "/" << m_newScopeUpdates;
+#ifdef DEBUG_ASW
+        qDebug() << "Trying to start a new scope thread for " << m_widgetName
+                << ". New frames/updates: " << m_newScopeFrames << "/" << m_newScopeUpdates;
+#endif
         prodScopeThread();
     }
 }
 
 void AbstractScopeWidget::slotBackgroundRenderingFinished(uint mseconds, uint oldFactor)
 {
-//    qDebug() << "Background rendering has finished, waiting for termination in " << m_widgetName;
+#ifdef DEBUG_ASW
+    qDebug() << "Background rendering has finished in " << mseconds << " ms, waiting for termination in " << m_widgetName;
+#endif
     m_threadBackground.waitForFinished();
     m_imgBackground = m_threadBackground.result();
 
@@ -374,42 +489,29 @@ void AbstractScopeWidget::slotBackgroundRenderingFinished(uint mseconds, uint ol
     }
 
     if ((m_newBackgroundFrames > 0 && m_aAutoRefresh->isChecked()) || m_newBackgroundUpdates > 0) {
-//        qDebug() << "Trying to start a new background thread for " << m_widgetName
-//                << ". New frames/updates: " << m_newBackgroundFrames << "/" << m_newBackgroundUpdates;
+#ifdef DEBUG_ASW
+        qDebug() << "Trying to start a new background thread for " << m_widgetName
+                << ". New frames/updates: " << m_newBackgroundFrames << "/" << m_newBackgroundUpdates;
+#endif
         prodBackgroundThread();;
     }
 }
 
-void AbstractScopeWidget::slotActiveMonitorChanged(bool isClipMonitor)
-{
-//    qDebug() << "Active monitor has changed in " << m_widgetName << ". Is the clip monitor active now? " << isClipMonitor;
-
-    bool b = m_activeRender->disconnect(this);
-    Q_ASSERT(b);
-
-    m_activeRender = (isClipMonitor) ? m_clipMonitor->render : m_projMonitor->render;
-
-    //b &= connect(m_activeRender, SIGNAL(rendererPosition(int)), this, SLOT(slotRenderZoneUpdated()));
-    b &= connect(m_activeRender, SIGNAL(frameUpdated(QImage)), this, SLOT(slotRenderZoneUpdated(QImage)));
-    Q_ASSERT(b);
-
-    // Update the scope for the new monitor.
-    prodHUDThread();
-    prodScopeThread();
-    prodBackgroundThread();
-}
-
 void AbstractScopeWidget::slotRenderZoneUpdated()
 {
     m_newHUDFrames.fetchAndAddRelaxed(1);
     m_newScopeFrames.fetchAndAddRelaxed(1);
     m_newBackgroundFrames.fetchAndAddRelaxed(1);
 
-//    qDebug() << "Monitor incoming. New frames total HUD/Scope/Background: " << m_newHUDFrames
-//            << "/" << m_newScopeFrames << "/" << m_newBackgroundFrames;
+#ifdef DEBUG_ASW
+    qDebug() << "Data incoming at " << widgetName() << ". New frames total HUD/Scope/Background: " << m_newHUDFrames
+            << "/" << m_newScopeFrames << "/" << m_newBackgroundFrames;
+#endif
 
     if (this->visibleRegion().isEmpty()) {
-//        qDebug() << "Scope of widget " << m_widgetName << " is not at the top, not rendering.";
+#ifdef DEBUG_ASW
+        qDebug() << "Scope of widget " << m_widgetName << " is not at the top, not rendering.";
+#endif
     } else {
         if (m_aAutoRefresh->isChecked()) {
             prodHUDThread();
@@ -419,12 +521,6 @@ void AbstractScopeWidget::slotRenderZoneUpdated()
     }
 }
 
-void AbstractScopeWidget::slotRenderZoneUpdated(QImage frame)
-{
-    m_scopeImage = frame;
-    slotRenderZoneUpdated();
-}
-
 void AbstractScopeWidget::slotResetRealtimeFactor(bool realtimeChecked)
 {
     if (!realtimeChecked) {
@@ -441,9 +537,24 @@ bool AbstractScopeWidget::autoRefreshEnabled()
 
 void AbstractScopeWidget::slotAutoRefreshToggled(bool autoRefresh)
 {
-    if (isVisible()) emit requestAutoRefresh(autoRefresh);
+#ifdef DEBUG_ASW
+    qDebug() << "Auto-refresh switched to " << autoRefresh << " in " << widgetName()
+            << " (Visible: " << isVisible() << "/" << this->visibleRegion().isEmpty() << ")";
+#endif
+    if (isVisible()) {
+        // Notify listeners whether we accept new frames now
+        emit requestAutoRefresh(autoRefresh);
+    }
     // TODO only if depends on input
     if (autoRefresh) {
-        forceUpdate();
+        //forceUpdate();
+        m_requestForcedUpdate = true;
     }
 }
+
+void AbstractScopeWidget::handleMouseDrag(const QPoint, const RescaleDirection, const Qt::KeyboardModifiers) { }
+
+
+#ifdef DEBUG_ASW
+#undef DEBUG_ASW
+#endif