diff --git a/AdaptiveBrightness/main_window.cpp b/AdaptiveBrightness/main_window.cpp index 506222b..93d33e4 100644 --- a/AdaptiveBrightness/main_window.cpp +++ b/AdaptiveBrightness/main_window.cpp @@ -111,25 +111,11 @@ void MainWindow::setupMonitorsTab() { connect(m_ui.monitorDropdown, QOverload::of(&QComboBox::currentIndexChanged), this, &MainWindow::updateCurrentMonitorGUI); - m_ui.monitorBrightnessSlider->setDisabled(true); - m_ui.monitorOverallBrightnessSlider->setDisabled(true); - - m_ui.monitorOverrideCheck->setChecked(false); - connect(m_ui.monitorOverrideCheck, &QCheckBox::stateChanged, - [this](int state) { m_ui.monitorBrightnessSlider->setEnabled(state == Qt::CheckState::Checked); }); - - m_ui.monitorOverallOverrideCheck->setChecked(false); - connect(m_ui.monitorOverallOverrideCheck, &QCheckBox::stateChanged, - [this](int state) { m_ui.monitorOverallBrightnessSlider->setEnabled(state == Qt::CheckState::Checked); }); - - connect(m_ui.monitorBrightnessSlider, &QSlider::valueChanged, [this](int value) { - const auto newBrightness = utils::map(value, 0, 100, 0.f, 1.f); - qDebug(ltr("Overriding brightness with %1").arg(newBrightness)); - m_brightness = newBrightness; - }); - setupLimitsSlider(); setupOverallLimitsSlider(); + + setupBrightness(); + setupOverallBrightness(); } void MainWindow::setupLimitsSlider() @@ -176,6 +162,50 @@ void MainWindow::setupOverallLimitsSlider() }); } +void MainWindow::setupBrightness() +{ + m_ui.monitorBrightnessSlider->setDisabled(true); + + connect(m_ui.monitorBrightnessSlider, &QSlider::valueChanged, [this](int value) { + m_ui.monitorBrightnessValueLabel->setText(tr("%1%").arg(value, 3)); + const auto newBrightness = utils::map(value, 0, 100, 0.f, 1.f); + qDebug(ltr("Overriding brightness with %1").arg(newBrightness)); + const auto curIdx = m_ui.monitorDropdown->currentIndex(); + m_monitors[curIdx].brightness = newBrightness; + }); + + m_ui.monitorOverrideCheck->setChecked(false); + connect(m_ui.monitorOverrideCheck, &QCheckBox::stateChanged, [this](int state) { + m_ui.monitorBrightnessSlider->setEnabled(state == Qt::CheckState::Checked); + const auto curIdx = m_ui.monitorDropdown->currentIndex(); + m_monitors[curIdx].manualOverride = state == Qt::CheckState::Checked; + }); +} + +void MainWindow::setupOverallBrightness() +{ + m_ui.monitorOverallBrightnessSlider->setDisabled(true); + + connect(m_ui.monitorOverallBrightnessSlider, &QSlider::valueChanged, [this](int value) { + m_ui.monitorOverallBrightnessValueLabel->setText(tr("%1%").arg(value, 3)); + const auto newBrightness = utils::map(value, 0, 100, 0.f, 1.f); + qDebug(ltr("Overriding overall brightness with %1").arg(newBrightness)); + for(auto& monitor: m_monitors) { + monitor.brightness = newBrightness; + } + updateCurrentMonitorGUI(m_ui.monitorDropdown->currentIndex()); + }); + + m_ui.monitorOverallOverrideCheck->setChecked(false); + connect(m_ui.monitorOverallOverrideCheck, &QCheckBox::stateChanged, [this](int state) { + m_ui.monitorOverallBrightnessSlider->setEnabled(state == Qt::CheckState::Checked); + for(auto& monitor: m_monitors) { + monitor.manualOverride = state == Qt::CheckState::Checked; + } + updateCurrentMonitorGUI(m_ui.monitorDropdown->currentIndex()); + }); +} + void MainWindow::setupTrayIcon() { loadIcons(); @@ -229,14 +259,9 @@ void MainWindow::setupCallbackTimer() void MainWindow::updateCurrentMonitorGUI(int index) { - const auto brightness = utils::map(m_monitors[index].brightness, 0.f, 1.f, 0, 100); - m_ui.monitorMinBrightnessSlider->setValue(utils::map(m_monitors[index].minBrightness, 0.f, 1.f, 0, 100)); m_ui.monitorMaxBrightnessSlider->setValue(utils::map(m_monitors[index].maxBrightness, 0.f, 1.f, 0, 100)); - - m_ui.monitorBrightnessSlider->setValue(brightness); - m_ui.monitorBrightnessValueLabel->setText(QString("%1%").arg(brightness, 3)); - + m_ui.monitorBrightnessSlider->setValue(utils::map(m_monitors[index].brightness, 0.f, 1.f, 0, 100)); m_ui.monitorOverrideCheck->setChecked(m_monitors[index].manualOverride); } @@ -273,22 +298,25 @@ void MainWindow::updateState() bool errorOccurred = false; for(auto& monitor: m_monitors) { - if(!monitor.driver.setBrightness(brightness)) { + monitor.brightness = monitor.manualOverride ? monitor.brightness : brightness; + if(!monitor.driver.setBrightness(monitor.brightness)) { errorOccurred = true; } } return !errorOccurred; }; - if(!getAverageSensorValue(m_brightness)) { + auto brightness = 0.5f; + + if(!getAverageSensorValue(brightness)) { enumSensors(); qInfo(ltr("At least one sensor became invalid, re-enumerating sensors found %1 valid sensor(s)").arg(m_sensors.size())); sensorError = true; } - const auto iconBrightness = utils::map(m_brightness, 0.f, 1.f, size_t{0}, size_t{8}); + const auto iconBrightness = utils::map(brightness, 0.f, 1.f, size_t{0}, size_t{8}); - if(!setAllMonitorsBrightness(m_brightness)) { + if(!setAllMonitorsBrightness(brightness)) { enumMonitors(); qInfo(ltr("At least one monitor became invalid, re-enumerating monitors found %1 valid monitor(s)").arg(m_monitors.size())); monitorError = true; @@ -305,10 +333,8 @@ void MainWindow::updateState() } if(m_trayIcon) { - m_trayIcon->setToolTip(tr("AdaptiveBrightness - %1%").arg(m_brightness * 100, 0, 'f', 0)); + m_trayIcon->setToolTip(tr("AdaptiveBrightness - %1%").arg(brightness * 100, 0, 'f', 0)); } - - m_ui.monitorBrightnessSlider->setValue(utils::map(m_brightness, 0.f, 1.f, 0, 100)); } void MainWindow::trayIconClicked(QSystemTrayIcon::ActivationReason reason) diff --git a/AdaptiveBrightness/main_window.hpp b/AdaptiveBrightness/main_window.hpp index 0ff66cf..1048d70 100644 --- a/AdaptiveBrightness/main_window.hpp +++ b/AdaptiveBrightness/main_window.hpp @@ -36,6 +36,8 @@ class MainWindow : public QMainWindow { void setupMonitorsTab(); void setupLimitsSlider(); void setupOverallLimitsSlider(); + void setupBrightness(); + void setupOverallBrightness(); void setupTrayIcon(); void setupCallbackTimer(); @@ -60,8 +62,6 @@ class MainWindow : public QMainWindow { QThread* m_timerThread = nullptr; QTimer* m_timer = nullptr; - float m_brightness = 0.5f; - struct SensorData { Sensor driver; };