Implement monitor and overall brightness slider

This commit is contained in:
BlackMark 2020-07-17 16:52:47 +02:00
parent 756bb9b6a7
commit fc49f1d779
2 changed files with 58 additions and 32 deletions

View File

@ -111,25 +111,11 @@ void MainWindow::setupMonitorsTab()
{ {
connect(m_ui.monitorDropdown, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &MainWindow::updateCurrentMonitorGUI); connect(m_ui.monitorDropdown, QOverload<int>::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(); setupLimitsSlider();
setupOverallLimitsSlider(); setupOverallLimitsSlider();
setupBrightness();
setupOverallBrightness();
} }
void MainWindow::setupLimitsSlider() 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() void MainWindow::setupTrayIcon()
{ {
loadIcons(); loadIcons();
@ -229,14 +259,9 @@ void MainWindow::setupCallbackTimer()
void MainWindow::updateCurrentMonitorGUI(int index) 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.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.monitorMaxBrightnessSlider->setValue(utils::map(m_monitors[index].maxBrightness, 0.f, 1.f, 0, 100));
m_ui.monitorBrightnessSlider->setValue(utils::map(m_monitors[index].brightness, 0.f, 1.f, 0, 100));
m_ui.monitorBrightnessSlider->setValue(brightness);
m_ui.monitorBrightnessValueLabel->setText(QString("%1%").arg(brightness, 3));
m_ui.monitorOverrideCheck->setChecked(m_monitors[index].manualOverride); m_ui.monitorOverrideCheck->setChecked(m_monitors[index].manualOverride);
} }
@ -273,22 +298,25 @@ void MainWindow::updateState()
bool errorOccurred = false; bool errorOccurred = false;
for(auto& monitor: m_monitors) { 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; errorOccurred = true;
} }
} }
return !errorOccurred; return !errorOccurred;
}; };
if(!getAverageSensorValue(m_brightness)) { auto brightness = 0.5f;
if(!getAverageSensorValue(brightness)) {
enumSensors(); enumSensors();
qInfo(ltr("At least one sensor became invalid, re-enumerating sensors found %1 valid sensor(s)").arg(m_sensors.size())); qInfo(ltr("At least one sensor became invalid, re-enumerating sensors found %1 valid sensor(s)").arg(m_sensors.size()));
sensorError = true; 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(); enumMonitors();
qInfo(ltr("At least one monitor became invalid, re-enumerating monitors found %1 valid monitor(s)").arg(m_monitors.size())); qInfo(ltr("At least one monitor became invalid, re-enumerating monitors found %1 valid monitor(s)").arg(m_monitors.size()));
monitorError = true; monitorError = true;
@ -305,10 +333,8 @@ void MainWindow::updateState()
} }
if(m_trayIcon) { 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) void MainWindow::trayIconClicked(QSystemTrayIcon::ActivationReason reason)

View File

@ -36,6 +36,8 @@ class MainWindow : public QMainWindow {
void setupMonitorsTab(); void setupMonitorsTab();
void setupLimitsSlider(); void setupLimitsSlider();
void setupOverallLimitsSlider(); void setupOverallLimitsSlider();
void setupBrightness();
void setupOverallBrightness();
void setupTrayIcon(); void setupTrayIcon();
void setupCallbackTimer(); void setupCallbackTimer();
@ -60,8 +62,6 @@ class MainWindow : public QMainWindow {
QThread* m_timerThread = nullptr; QThread* m_timerThread = nullptr;
QTimer* m_timer = nullptr; QTimer* m_timer = nullptr;
float m_brightness = 0.5f;
struct SensorData { struct SensorData {
Sensor driver; Sensor driver;
}; };