]> git.sesse.net Git - nageru/commitdiff
Do the UI changes always in the main thread.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Sun, 8 Nov 2015 16:01:51 +0000 (17:01 +0100)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Sun, 8 Nov 2015 16:01:51 +0000 (17:01 +0100)
mainwindow.cpp
post_to_main_thread.h [new file with mode: 0644]

index 01baef5c903d2461a8960328a379368cfc332161..d4e4d62d3fab6cb87fafb089dd458f2937ac7905 100644 (file)
@@ -18,6 +18,7 @@
 #include "glwidget.h"
 #include "lrameter.h"
 #include "mixer.h"
+#include "post_to_main_thread.h"
 #include "ui_display.h"
 #include "ui_mainwindow.h"
 #include "vumeter.h"
@@ -91,21 +92,23 @@ void MainWindow::mixer_created(Mixer *mixer)
        }
 
        mixer->set_audio_level_callback([this](float level_lufs, float peak_db, float global_level_lufs, float range_low_lufs, float range_high_lufs, float auto_gain_staging_db){
-               ui->vu_meter->set_level(level_lufs);
-               ui->lra_meter->set_levels(global_level_lufs, range_low_lufs, range_high_lufs);
-
-               char buf[256];
-               snprintf(buf, sizeof(buf), "%.1f", peak_db);
-               ui->peak_display->setText(buf);
-               if (peak_db > -0.1f) {  // -0.1 dBFS is EBU peak limit.
-                       ui->peak_display->setStyleSheet("QLabel { background-color: red; color: white; }");
-               } else {
-                       ui->peak_display->setStyleSheet("");
-               }
-
-               ui->gainstaging_knob->setValue(lrintf(auto_gain_staging_db * 10.0f));
-               snprintf(buf, sizeof(buf), "%+.1f dB", auto_gain_staging_db);
-               ui->gainstaging_db_display->setText(buf);
+               post_to_main_thread([=]() {
+                       ui->vu_meter->set_level(level_lufs);
+                       ui->lra_meter->set_levels(global_level_lufs, range_low_lufs, range_high_lufs);
+
+                       char buf[256];
+                       snprintf(buf, sizeof(buf), "%.1f", peak_db);
+                       ui->peak_display->setText(buf);
+                       if (peak_db > -0.1f) {  // -0.1 dBFS is EBU peak limit.
+                               ui->peak_display->setStyleSheet("QLabel { background-color: red; color: white; }");
+                       } else {
+                               ui->peak_display->setStyleSheet("");
+                       }
+
+                       ui->gainstaging_knob->setValue(lrintf(auto_gain_staging_db * 10.0f));
+                       snprintf(buf, sizeof(buf), "%+.1f dB", auto_gain_staging_db);
+                       ui->gainstaging_db_display->setText(buf);
+               });
        });
 }
 
diff --git a/post_to_main_thread.h b/post_to_main_thread.h
new file mode 100644 (file)
index 0000000..0462c7b
--- /dev/null
@@ -0,0 +1,16 @@
+#ifndef _POST_TO_MAIN_THREAD_H
+#define _POST_TO_MAIN_THREAD_H 1
+
+#include <QApplication>
+#include <QObject>
+#include <memory>
+
+// http://stackoverflow.com/questions/21646467/how-to-execute-a-functor-in-a-given-thread-in-qt-gcd-style
+template<typename F>
+static inline void post_to_main_thread(F &&fun)
+{
+       QObject signalSource;
+       QObject::connect(&signalSource, &QObject::destroyed, qApp, std::move(fun));
+}
+
+#endif  // !defined(_POST_TO_MAIN_THREAD_H)