]> git.sesse.net Git - kdenlive/blobdiff - src/abstractscopewidget.cpp
Color scopes now using the common abstract scope as well. --> Common functions, more...
[kdenlive] / src / abstractscopewidget.cpp
index 75dfa5d659420049ff8440fca7a432768d088cb1..8b42676fa0f8654c4bb6dab4bc5ea100e0d13066 100644 (file)
 #include <QFuture>
 #include <QColor>
 #include <QMenu>
+#include <QMouseEvent>
 #include <QPainter>
 
 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, QWidget *parent) :
-    QWidget(parent),
-    m_projMonitor(projMonitor),
-    m_clipMonitor(clipMonitor),
-    offset(5),
-    m_accelFactorHUD(1),
-    m_accelFactorScope(1),
-    m_accelFactorBackground(1),
-    m_semaphoreHUD(1),
-    m_semaphoreScope(1),
-    m_semaphoreBackground(1),
-    initialDimensionUpdateDone(false)
+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::penLightDots(QBrush(QColor(200, 200, 250, 150)), 1, Qt::DotLine);
+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);
+
+AbstractScopeWidget::AbstractScopeWidget(bool trackMouse, QWidget *parent) :
+        QWidget(parent),
+        m_mousePos(0, 0),
+        m_mouseWithinWidget(false),
+        offset(5),
+        m_accelFactorHUD(1),
+        m_accelFactorScope(1),
+        m_accelFactorBackground(1),
+        m_semaphoreHUD(1),
+        m_semaphoreScope(1),
+        m_semaphoreBackground(1),
+        initialDimensionUpdateDone(false),
+        m_requestForcedUpdate(false)
 
 {
     m_scopePalette = QPalette();
@@ -66,20 +70,20 @@ 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)));
+    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)));
     b &= connect(m_aRealtime, SIGNAL(toggled(bool)), this, SLOT(slotResetRealtimeFactor(bool)));
     b &= connect(m_aAutoRefresh, SIGNAL(toggled(bool)), this, SLOT(slotAutoRefreshToggled(bool)));
     Q_ASSERT(b);
+
+    // Enable mouse tracking if desired.
+    // Causes the mouseMoved signal to be emitted when the mouse moves inside the
+    // widget, even when no mouse button is pressed.
+    this->setMouseTracking(trackMouse);
 }
 
 AbstractScopeWidget::~AbstractScopeWidget()
@@ -115,7 +119,10 @@ void AbstractScopeWidget::writeConfig()
     scopeConfig.sync();
 }
 
-QString AbstractScopeWidget::configName() { return "Scope_" + m_widgetName; }
+QString AbstractScopeWidget::configName()
+{
+    return "Scope_" + m_widgetName;
+}
 
 void AbstractScopeWidget::prodHUDThread()
 {
@@ -139,8 +146,8 @@ void AbstractScopeWidget::prodHUDThread()
 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()) {
+    // 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)) {
 //        qDebug() << "Scope " << m_widgetName << " is not visible. Not calculating scope.";
     } else {
         // Try to acquire the semaphore. This must only succeed if m_threadScope is not running
@@ -156,7 +163,8 @@ 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;
 
@@ -187,7 +195,10 @@ void AbstractScopeWidget::prodBackgroundThread()
 void AbstractScopeWidget::forceUpdate(bool doUpdate)
 {
 //    qDebug() << "Force update called in " << widgetName() << ". Arg: " << doUpdate;
-    if (!doUpdate) { return; }
+    if (!doUpdate) {
+        return;
+    }
+    m_requestForcedUpdate = true;
     m_newHUDUpdates.fetchAndAddRelaxed(1);
     m_newScopeUpdates.fetchAndAddRelaxed(1);
     m_newBackgroundUpdates.fetchAndAddRelaxed(1);
@@ -204,6 +215,7 @@ void AbstractScopeWidget::forceUpdateHUD()
 void AbstractScopeWidget::forceUpdateScope()
 {
     m_newScopeUpdates.fetchAndAddRelaxed(1);
+    m_requestForcedUpdate = true;
     prodScopeThread();
 
 }
@@ -219,6 +231,9 @@ void AbstractScopeWidget::forceUpdateBackground()
 
 void AbstractScopeWidget::mouseReleaseEvent(QMouseEvent *event)
 {
+    if (!m_aAutoRefresh->isChecked()) {
+        m_requestForcedUpdate = true;
+    }
     prodHUDThread();
     prodScopeThread();
     prodBackgroundThread();
@@ -229,7 +244,6 @@ void AbstractScopeWidget::resizeEvent(QResizeEvent *event)
 {
     // Update the dimension of the available rect for painting
     m_scopeRect = scopeRect();
-
     forceUpdate();
 
     QWidget::resizeEvent(event);
@@ -249,14 +263,35 @@ void AbstractScopeWidget::paintEvent(QPaintEvent *)
     davinci.drawImage(m_scopeRect.topLeft(), m_imgHUD);
 }
 
+void AbstractScopeWidget::mouseMoveEvent(QMouseEvent *event)
+{
+    m_mousePos = event->pos();
+    m_mouseWithinWidget = true;
+    emit signalMousePositionChanged();
+}
+void AbstractScopeWidget::leaveEvent(QEvent *)
+{
+    m_mouseWithinWidget = false;
+    emit signalMousePositionChanged();
+}
+
 void AbstractScopeWidget::customContextMenuRequested(const QPoint &pos)
 {
     m_menu->exec(this->mapToGlobal(pos));
 }
 
-uint AbstractScopeWidget::calculateAccelFactorHUD(uint oldMseconds, uint) { return ceil((float)oldMseconds*REALTIME_FPS/1000 ); }
-uint AbstractScopeWidget::calculateAccelFactorScope(uint oldMseconds, uint) { return ceil((float)oldMseconds*REALTIME_FPS/1000 ); }
-uint AbstractScopeWidget::calculateAccelFactorBackground(uint oldMseconds, uint) { return ceil((float)oldMseconds*REALTIME_FPS/1000 ); }
+uint AbstractScopeWidget::calculateAccelFactorHUD(uint oldMseconds, uint)
+{
+    return ceil((float)oldMseconds*REALTIME_FPS / 1000);
+}
+uint AbstractScopeWidget::calculateAccelFactorScope(uint oldMseconds, uint)
+{
+    return ceil((float)oldMseconds*REALTIME_FPS / 1000);
+}
+uint AbstractScopeWidget::calculateAccelFactorBackground(uint oldMseconds, uint)
+{
+    return ceil((float)oldMseconds*REALTIME_FPS / 1000);
+}
 
 
 ///// Slots /////
@@ -279,7 +314,7 @@ void AbstractScopeWidget::slotHUDRenderingFinished(uint mseconds, uint oldFactor
         m_accelFactorHUD = accel;
     }
 
-    if ( (m_newHUDFrames > 0 && m_aAutoRefresh->isChecked()) || m_newHUDUpdates > 0) {
+    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;
         prodHUDThread();;
@@ -313,7 +348,7 @@ void AbstractScopeWidget::slotScopeRenderingFinished(uint mseconds, uint oldFact
         m_accelFactorScope = accel;
     }
 
-    if ( (m_newScopeFrames > 0 && m_aAutoRefresh->isChecked()) || m_newScopeUpdates > 0) {
+    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;
         prodScopeThread();
@@ -338,32 +373,13 @@ void AbstractScopeWidget::slotBackgroundRenderingFinished(uint mseconds, uint ol
         m_accelFactorBackground = accel;
     }
 
-    if ( (m_newBackgroundFrames > 0 && m_aAutoRefresh->isChecked()) || m_newBackgroundUpdates > 0) {
+    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;
         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);
@@ -399,10 +415,19 @@ void AbstractScopeWidget::slotResetRealtimeFactor(bool realtimeChecked)
     }
 }
 
+bool AbstractScopeWidget::autoRefreshEnabled()
+{
+    return m_aAutoRefresh->isChecked();
+}
+
 void AbstractScopeWidget::slotAutoRefreshToggled(bool autoRefresh)
 {
+    if (isVisible()) {
+        emit requestAutoRefresh(autoRefresh);
+    }
     // TODO only if depends on input
     if (autoRefresh) {
-        forceUpdate();
+        //forceUpdate();
+        m_requestForcedUpdate = true;
     }
 }