diff --git a/AdaptiveBrightness/main_window.cpp b/AdaptiveBrightness/main_window.cpp index 8277944..53fbfb6 100644 --- a/AdaptiveBrightness/main_window.cpp +++ b/AdaptiveBrightness/main_window.cpp @@ -24,8 +24,8 @@ MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent) m_ui.setupUi(this); - m_sensors = enumerateSensors(); - m_monitors = enumerateMonitors(); + enumSensors(); + enumMonitors(); setupMonitorsTab(); @@ -50,6 +50,28 @@ void MainWindow::closeEvent(QCloseEvent* event) event->ignore(); } +void MainWindow::enumSensors() +{ + auto sensors = enumerateSensors(); + + for(auto& sensor: sensors) { + m_sensors.push_back({std::move(sensor)}); + } +} + +void MainWindow::enumMonitors() +{ + m_ui.monitorDropdown->clear(); + + auto monitors = enumerateMonitors(); + + for(auto& monitor: monitors) { + const auto currentBrightness = monitor.getBrightness(); + m_ui.monitorDropdown->addItem(monitor.getName()); + m_monitors.push_back({std::move(monitor), 0.f, 1.f, currentBrightness, false}); + } +} + void MainWindow::loadIcons() { qDebug(ltr("Loading icons")); @@ -87,6 +109,8 @@ void MainWindow::loadIcons() void MainWindow::setupMonitorsTab() { + connect(m_ui.monitorDropdown, QOverload::of(&QComboBox::currentIndexChanged), this, &MainWindow::updateCurrentMonitorGUI); + m_ui.monitorBrightnessSlider->setDisabled(true); m_ui.monitorOverallBrightnessSlider->setDisabled(true); @@ -157,6 +181,24 @@ void MainWindow::setupCallbackTimer() qDebug(ltr("Callback timer started")); } +void MainWindow::updateCurrentMonitorGUI(int index) +{ + const auto brightness = std::clamp(utils::map(m_monitors[index].brightness, 0.f, 1.f, 0, 100), 0, 100); + const auto minimum = std::clamp(utils::map(m_monitors[index].minBrightness, 0.f, 1.f, 0, 100), 0, 100); + const auto maximum = std::clamp(utils::map(m_monitors[index].maxBrightness, 0.f, 1.f, 0, 100), 0, 100); + + m_ui.monitorMinBrightnessSlider->setValue(minimum); + m_ui.monitorMinBrightnessValueLabel->setText(QString("%1%").arg(minimum, 3, 10)); + + m_ui.monitorMaxBrightnessSlider->setValue(maximum); + m_ui.monitorMaxBrightnessValueLabel->setText(QString("%1%").arg(maximum, 3, 10)); + + m_ui.monitorBrightnessSlider->setValue(brightness); + m_ui.monitorBrightnessValueLabel->setText(QString("%1%").arg(brightness, 3, 10)); + + m_ui.monitorOverrideCheck->setChecked(m_monitors[index].manualOverride); +} + void MainWindow::updateState() { qDebug(ltr("Updating state")); @@ -173,8 +215,8 @@ void MainWindow::updateState() std::vector values; for(auto& sensor: m_sensors) { - const auto sensorValues = sensor.readValues(); - if(!sensor) { + const auto sensorValues = sensor.driver.readValues(); + if(!sensor.driver) { return false; } values.insert(values.end(), sensorValues.begin(), sensorValues.end()); @@ -191,7 +233,7 @@ void MainWindow::updateState() bool errorOccurred = false; for(auto& monitor: m_monitors) { - if(!monitor.setBrightness(brightness)) { + if(!monitor.driver.setBrightness(brightness)) { errorOccurred = true; } } @@ -199,7 +241,7 @@ void MainWindow::updateState() }; if(!getAverageSensorValue(m_brightness)) { - m_sensors = enumerateSensors(); + enumSensors(); qInfo(ltr("At least one sensor became invalid, re-enumerating sensors found %1 valid sensor(s)").arg(m_sensors.size())); sensorError = true; } @@ -208,7 +250,7 @@ void MainWindow::updateState() iconBrightness = std::clamp(roundedBrightness, 0, 8); if(!setAllMonitorsBrightness(m_brightness)) { - m_monitors = enumerateMonitors(); + enumMonitors(); qInfo(ltr("At least one monitor became invalid, re-enumerating monitors found %1 valid monitor(s)").arg(m_monitors.size())); monitorError = true; } diff --git a/AdaptiveBrightness/main_window.hpp b/AdaptiveBrightness/main_window.hpp index 3665cc9..d392462 100644 --- a/AdaptiveBrightness/main_window.hpp +++ b/AdaptiveBrightness/main_window.hpp @@ -28,12 +28,17 @@ class MainWindow : public QMainWindow { private: void closeEvent(QCloseEvent* event) override; + void enumSensors(); + void enumMonitors(); + void loadIcons(); void setupMonitorsTab(); void setupTrayIcon(); void setupCallbackTimer(); + void updateCurrentMonitorGUI(int index); + void updateState(); void trayIconClicked(QSystemTrayIcon::ActivationReason reason); @@ -54,6 +59,18 @@ class MainWindow : public QMainWindow { float m_brightness = 0.5f; - std::vector m_sensors; - std::vector m_monitors; + struct SensorData { + Sensor driver; + }; + + struct MonitorData { + Monitor driver; + float minBrightness; + float maxBrightness; + float brightness; + bool manualOverride; + }; + + std::vector m_sensors; + std::vector m_monitors; };