]> git.sesse.net Git - kdenlive/blobdiff - src/abstractscopewidget.cpp
Fix resize box in geometry widget
[kdenlive] / src / abstractscopewidget.cpp
index 998cc725a1b25d08c91af0b1dd3e53893692eb23..760c15ac8e3273033edb2a93c0d5135b50d40d1b 100644 (file)
@@ -16,7 +16,6 @@
 
 #include <QFuture>
 #include <QColor>
-#include <QDebug>
 #include <QMenu>
 #include <QPainter>
 
@@ -68,7 +67,7 @@ AbstractScopeWidget::AbstractScopeWidget(Monitor *projMonitor, Monitor *clipMoni
     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(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)));
@@ -80,26 +79,55 @@ AbstractScopeWidget::AbstractScopeWidget(Monitor *projMonitor, Monitor *clipMoni
 
 AbstractScopeWidget::~AbstractScopeWidget()
 {
+    writeConfig();
+
     delete m_menu;
     delete m_aAutoRefresh;
     delete m_aRealtime;
 }
 
+void AbstractScopeWidget::init()
+{
+    m_widgetName = widgetName();
+    readConfig();
+}
+
+void AbstractScopeWidget::readConfig()
+{
+    KSharedConfigPtr config = KGlobal::config();
+    KConfigGroup scopeConfig(config, configName());
+    m_aAutoRefresh->setChecked(scopeConfig.readEntry("autoRefresh", true));
+    m_aRealtime->setChecked(scopeConfig.readEntry("realtime", false));
+    scopeConfig.sync();
+}
+
+void AbstractScopeWidget::writeConfig()
+{
+    KSharedConfigPtr config = KGlobal::config();
+    KConfigGroup scopeConfig(config, configName());
+    scopeConfig.writeEntry("autoRefresh", m_aAutoRefresh->isChecked());
+    scopeConfig.writeEntry("realtime", m_aRealtime->isChecked());
+    scopeConfig.sync();
+}
+
+QString AbstractScopeWidget::configName() { return "Scope_" + m_widgetName; }
+
 void AbstractScopeWidget::prodHUDThread()
 {
     if (this->visibleRegion().isEmpty()) {
-        qDebug() << "Scope " << widgetName() << " is not visible. Not calculating HUD.";
-    }
-    if (m_semaphoreHUD.tryAcquire(1)) {
-        Q_ASSERT(!m_threadHUD.isRunning());
+//        qDebug() << "Scope " << m_widgetName << " is not visible. Not calculating HUD.";
+    } else {
+        if (m_semaphoreHUD.tryAcquire(1)) {
+            Q_ASSERT(!m_threadHUD.isRunning());
 
-        m_newHUDFrames.fetchAndStoreRelaxed(0);
-        m_newHUDUpdates.fetchAndStoreRelaxed(0);
-        m_threadHUD = QtConcurrent::run(this, &AbstractScopeWidget::renderHUD, m_accelFactorHUD);
-        qDebug() << "HUD thread started in " << widgetName();
+            m_newHUDFrames.fetchAndStoreRelaxed(0);
+            m_newHUDUpdates.fetchAndStoreRelaxed(0);
+            m_threadHUD = QtConcurrent::run(this, &AbstractScopeWidget::renderHUD, m_accelFactorHUD);
+//            qDebug() << "HUD thread started in " << m_widgetName;
 
-    } else {
-        qDebug() << "HUD semaphore locked, not prodding in " << widgetName() << ". Thread running: " << m_threadHUD.isRunning();
+        } else {
+//            qDebug() << "HUD semaphore locked, not prodding in " << m_widgetName << ". Thread running: " << m_threadHUD.isRunning();
+        }
     }
 }
 
@@ -108,47 +136,53 @@ 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 " << widgetName() << " is not visible. Not calculating scope.";
-    }
-    // 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.
-    // If acquiring the semaphore fails, the thread is still running.
-    if (m_semaphoreScope.tryAcquire(1)) {
-        Q_ASSERT(!m_threadScope.isRunning());
+//        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
+        // anymore. Therefore the semaphore must NOT be released before m_threadScope ends.
+        // If acquiring the semaphore fails, the thread is still running.
+        if (m_semaphoreScope.tryAcquire(1)) {
+            Q_ASSERT(!m_threadScope.isRunning());
 
-        m_newScopeFrames.fetchAndStoreRelaxed(0);
-        m_newScopeUpdates.fetchAndStoreRelaxed(0);
+            m_newScopeFrames.fetchAndStoreRelaxed(0);
+            m_newScopeUpdates.fetchAndStoreRelaxed(0);
 
-        // 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);
+            Q_ASSERT(m_accelFactorScope > 0);
 
-        qDebug() << "Scope thread started in " << widgetName();
+            // 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);
 
-    } else {
-        qDebug() << "Scope semaphore locked, not prodding in " << widgetName() << ". Thread running: " << m_threadScope.isRunning();
+//            qDebug() << "Scope thread started in " << m_widgetName;
+
+        } else {
+//            qDebug() << "Scope semaphore locked, not prodding in " << m_widgetName << ". Thread running: " << m_threadScope.isRunning();
+        }
     }
 }
 void AbstractScopeWidget::prodBackgroundThread()
 {
     if (this->visibleRegion().isEmpty()) {
-        qDebug() << "Scope " << widgetName() << " is not visible. Not calculating background.";
-    }
-    if (m_semaphoreBackground.tryAcquire(1)) {
-        Q_ASSERT(!m_threadBackground.isRunning());
+//        qDebug() << "Scope " << m_widgetName << " is not visible. Not calculating background.";
+    } else {
+        if (m_semaphoreBackground.tryAcquire(1)) {
+            Q_ASSERT(!m_threadBackground.isRunning());
 
-        m_newBackgroundFrames.fetchAndStoreRelaxed(0);
-        m_newBackgroundUpdates.fetchAndStoreRelaxed(0);
-        m_threadBackground = QtConcurrent::run(this, &AbstractScopeWidget::renderBackground, m_accelFactorBackground);
-        qDebug() << "Background thread started in " << widgetName();
+            m_newBackgroundFrames.fetchAndStoreRelaxed(0);
+            m_newBackgroundUpdates.fetchAndStoreRelaxed(0);
+            m_threadBackground = QtConcurrent::run(this, &AbstractScopeWidget::renderBackground, m_accelFactorBackground);
+//            qDebug() << "Background thread started in " << m_widgetName;
 
-    } else {
-        qDebug() << "Background semaphore locked, not prodding in " << widgetName() << ". Thread running: " << m_threadBackground.isRunning();
+        } else {
+//            qDebug() << "Background semaphore locked, not prodding in " << m_widgetName << ". Thread running: " << m_threadBackground.isRunning();
+        }
     }
 }
 
-void AbstractScopeWidget::forceUpdate()
+void AbstractScopeWidget::forceUpdate(bool doUpdate)
 {
+//    qDebug() << "Force update called in " << widgetName() << ". Arg: " << doUpdate;
+    if (!doUpdate) { return; }
     m_newHUDUpdates.fetchAndAddRelaxed(1);
     m_newScopeUpdates.fetchAndAddRelaxed(1);
     m_newBackgroundUpdates.fetchAndAddRelaxed(1);
@@ -196,28 +230,14 @@ void AbstractScopeWidget::resizeEvent(QResizeEvent *event)
     QWidget::resizeEvent(event);
 }
 
-void AbstractScopeWidget::raise()
+void AbstractScopeWidget::showEvent(QShowEvent *event)
 {
-    // Widget has been brought to the top of a widget stack,
-    // layers need to be updated.
-    QWidget::raise();
-    forceUpdate();
+    QWidget::showEvent(event);
+    m_scopeRect = scopeRect();
 }
 
 void AbstractScopeWidget::paintEvent(QPaintEvent *)
 {
-//    qDebug() << "Painting now on scope " << widgetName();
-    if (!initialDimensionUpdateDone) {
-        // This is a workaround.
-        // When updating the dimensions in the constructor, the size
-        // of the control items at the top are simply ignored! So do
-        // it here instead.
-        m_scopeRect = scopeRect();
-        initialDimensionUpdateDone = true;
-    }
-
-    qDebug() << "Drawing top/left at " << m_scopeRect.topLeft().x() << "/" << m_scopeRect.topLeft().y();
-
     QPainter davinci(this);
     davinci.drawImage(m_scopeRect.topLeft(), m_imgBackground);
     davinci.drawImage(m_scopeRect.topLeft(), m_imgScope);
@@ -238,7 +258,7 @@ uint AbstractScopeWidget::calculateAccelFactorBackground(uint oldMseconds, uint)
 
 void AbstractScopeWidget::slotHUDRenderingFinished(uint mseconds, uint oldFactor)
 {
-    qDebug() << "HUD rendering has finished, waiting for termination in " << widgetName();
+//    qDebug() << "HUD rendering has finished, waiting for termination in " << m_widgetName;
     m_threadHUD.waitForFinished();
     m_imgHUD = m_threadHUD.result();
 
@@ -255,8 +275,8 @@ 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 " << widgetName()
-                << ". New frames/updates: " << m_newHUDFrames << "/" << m_newHUDUpdates;
+//        qDebug() << "Trying to start a new HUD thread for " << m_widgetName
+//                << ". New frames/updates: " << m_newHUDFrames << "/" << m_newHUDUpdates;
         prodHUDThread();;
     }
 }
@@ -265,7 +285,7 @@ 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 " << widgetName();
+//    qDebug() << "Scope rendering has finished, waiting for termination in " << m_widgetName;
     m_threadScope.waitForFinished();
     m_imgScope = m_threadScope.result();
 
@@ -278,7 +298,7 @@ void AbstractScopeWidget::slotScopeRenderingFinished(uint mseconds, uint oldFact
     int accel;
     if (m_aRealtime->isChecked()) {
         accel = calculateAccelFactorScope(mseconds, oldFactor);
-        if (m_accelFactorScope < 1) {
+        if (accel < 1) {
             // If mseconds happens to be 0.
             accel = 1;
         }
@@ -289,15 +309,15 @@ 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 " << widgetName()
-                << ". New frames/updates: " << m_newScopeFrames << "/" << m_newScopeUpdates;
+//        qDebug() << "Trying to start a new scope thread for " << m_widgetName
+//                << ". New frames/updates: " << m_newScopeFrames << "/" << m_newScopeUpdates;
         prodScopeThread();
     }
 }
 
 void AbstractScopeWidget::slotBackgroundRenderingFinished(uint mseconds, uint oldFactor)
 {
-    qDebug() << "Background rendering has finished, waiting for termination in " << widgetName();
+//    qDebug() << "Background rendering has finished, waiting for termination in " << m_widgetName;
     m_threadBackground.waitForFinished();
     m_imgBackground = m_threadBackground.result();
 
@@ -314,24 +334,23 @@ 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 " << widgetName()
-                << ". New frames/updates: " << m_newBackgroundFrames << "/" << m_newBackgroundUpdates;
+//        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 " << widgetName() << ". Is the clip monitor active now? " << isClipMonitor;
+//    qDebug() << "Active monitor has changed in " << m_widgetName << ". Is the clip monitor active now? " << isClipMonitor;
 
-    bool disconnected = m_activeRender->disconnect(this);
-    Q_ASSERT(disconnected);
+    bool b = m_activeRender->disconnect(this);
+    Q_ASSERT(b);
 
     m_activeRender = (isClipMonitor) ? m_clipMonitor->render : m_projMonitor->render;
 
-    bool b = true;
     b &= connect(m_activeRender, SIGNAL(rendererPosition(int)), this, SLOT(slotRenderZoneUpdated()));
-    b &= connect(m_activeRender, SIGNAL(frameUpdated(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.
@@ -346,11 +365,11 @@ void AbstractScopeWidget::slotRenderZoneUpdated()
     m_newScopeFrames.fetchAndAddRelaxed(1);
     m_newBackgroundFrames.fetchAndAddRelaxed(1);
 
-    qDebug() << "Monitor incoming. New frames total HUD/Scope/Background: " << m_newHUDFrames
-            << "/" << m_newScopeFrames << "/" << m_newBackgroundFrames;
+//    qDebug() << "Monitor incoming. New frames total HUD/Scope/Background: " << m_newHUDFrames
+//            << "/" << m_newScopeFrames << "/" << m_newBackgroundFrames;
 
     if (this->visibleRegion().isEmpty()) {
-        qDebug() << "Scope of widget " << widgetName() << " is not at the top, not rendering.";
+//        qDebug() << "Scope of widget " << m_widgetName << " is not at the top, not rendering.";
     } else {
         if (m_aAutoRefresh->isChecked()) {
             prodHUDThread();
@@ -360,6 +379,12 @@ void AbstractScopeWidget::slotRenderZoneUpdated()
     }
 }
 
+void AbstractScopeWidget::slotRenderZoneUpdated(QImage frame)
+{
+    m_scopeImage = frame;
+    slotRenderZoneUpdated();
+}
+
 void AbstractScopeWidget::slotResetRealtimeFactor(bool realtimeChecked)
 {
     if (!realtimeChecked) {