1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-12-27 16:31:31 +00:00

Ensures proper thread confinement for updateStatusBarText.

This commit is contained in:
Thomas Harte 2020-07-27 20:25:52 -04:00
parent a2db6ddea5
commit 7c05b1788e
2 changed files with 11 additions and 6 deletions

View File

@ -1338,24 +1338,25 @@ void MainWindow::addActivityObserver() {
}
void MainWindow::register_led(const std::string &name) {
std::lock_guard guard(ledStatusesLock);
ledStatuses[name] = false;
updateStatusBarText();
QMetaObject::invokeMethod(this, "updateStatusBarText");
}
void MainWindow::set_led_status(const std::string &name, bool isLit) {
std::lock_guard guard(ledStatusesLock);
ledStatuses[name] = isLit;
updateStatusBarText(); // Assumption here: Qt's attempt at automatic thread confinement will work here.
QMetaObject::invokeMethod(this, "updateStatusBarText");
}
void MainWindow::updateStatusBarText() {
QString fullText;
bool isFirst = true;
std::lock_guard guard(ledStatusesLock);
for(const auto &pair: ledStatuses) {
if(!isFirst) fullText += " | ";
if(!fullText.isEmpty()) fullText += " | ";
fullText += QString::fromStdString(pair.first);
fullText += " ";
fullText += pair.second ? "" : "";
isFirst = false;
}
statusBar()->showMessage(fullText);
}

View File

@ -5,6 +5,7 @@
#include <QMainWindow>
#include <memory>
#include <mutex>
#include <optional>
#include "audiobuffer.h"
@ -80,6 +81,7 @@ class MainWindow : public QMainWindow, public Outputs::Speaker::Speaker::Delegat
private slots:
void startMachine();
void updateStatusBarText();
private:
void start_appleII();
@ -144,9 +146,11 @@ class MainWindow : public QMainWindow, public Outputs::Speaker::Speaker::Delegat
void register_led(const std::string &) override;
void set_led_status(const std::string &, bool) override;
std::recursive_mutex ledStatusesLock;
std::map<std::string, bool> ledStatuses;
void addActivityObserver();
void updateStatusBarText();
};
#endif // MAINWINDOW_H