Compare commits

...

2 Commits

Author SHA1 Message Date
69a68ce67c Refactor monitor tab GUI setup 2020-07-12 19:06:19 +02:00
afd6de62a0 Refactor GUI setup 2020-07-12 18:55:41 +02:00
2 changed files with 71 additions and 52 deletions

View File

@ -24,60 +24,14 @@ MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent)
m_ui.setupUi(this);
connect(m_ui.monitorBrightnessSlider, &QSlider::valueChanged, [this](int value) {
const auto mappedBrightness = utils::map(value, 0, 100, 0.f, 1.f);
const auto newBrightness = std::clamp(mappedBrightness, 0.f, 1.f);
qDebug(ltr("Overriding brightness with %1").arg(newBrightness));
m_brightness = newBrightness;
});
loadIcons();
if(!QSystemTrayIcon::isSystemTrayAvailable()) {
qWarning(ltr("System tray is not available -> tray icon will be disabled"));
}
else {
qDebug(ltr("System tray is available"));
m_trayIconMenu = new QMenu(this);
m_showHideAction = m_trayIconMenu->addAction(tr("Show"), [this]() {
if(m_visibility) {
m_showHideAction->setText(tr("Show"));
hide();
}
else {
m_showHideAction->setText(tr("Hide"));
show();
}
m_visibility = !m_visibility;
});
m_trayIconMenu->addSeparator();
m_trayIconMenu->addAction(tr("Quit"), this, &QCoreApplication::quit);
qDebug(ltr("Tray icon context menu initialized"));
m_trayIcon = new QSystemTrayIcon(this);
m_trayIcon->setIcon(*m_errorIcon);
m_trayIcon->setContextMenu(m_trayIconMenu);
m_trayIcon->show();
m_trayIcon->setToolTip(tr("AdaptiveBrightness"));
connect(m_trayIcon, &QSystemTrayIcon::activated, this, &MainWindow::trayIconClicked);
qDebug(ltr("Tray icon initialized"));
}
m_timerThread = new QThread(this);
m_timer = new QTimer(nullptr);
m_timer->setInterval(1000);
m_timer->moveToThread(m_timerThread);
m_timerThread->connect(m_timer, &QTimer::timeout, this, &MainWindow::updateState, Qt::DirectConnection);
m_timer->connect(m_timerThread, SIGNAL(started()), SLOT(start()));
m_timerThread->start();
qDebug(ltr("Tray icon update timer started"));
m_sensors = enumerateSensors();
m_monitors = enumerateMonitors();
setupMonitorsTab();
setupTrayIcon();
setupCallbackTimer();
}
MainWindow::~MainWindow()
@ -131,6 +85,67 @@ void MainWindow::loadIcons()
qDebug(ltr("Icons loaded"));
}
void MainWindow::setupMonitorsTab()
{
connect(m_ui.monitorBrightnessSlider, &QSlider::valueChanged, [this](int value) {
const auto mappedBrightness = utils::map(value, 0, 100, 0.f, 1.f);
const auto newBrightness = std::clamp(mappedBrightness, 0.f, 1.f);
qDebug(ltr("Overriding brightness with %1").arg(newBrightness));
m_brightness = newBrightness;
});
}
void MainWindow::setupTrayIcon()
{
loadIcons();
if(!QSystemTrayIcon::isSystemTrayAvailable()) {
qWarning(ltr("System tray is not available -> tray icon will be disabled"));
}
else {
qDebug(ltr("System tray is available"));
m_trayIconMenu = new QMenu(this);
m_showHideAction = m_trayIconMenu->addAction(tr("Show"), [this]() {
if(m_visibility) {
m_showHideAction->setText(tr("Show"));
hide();
}
else {
m_showHideAction->setText(tr("Hide"));
show();
}
m_visibility = !m_visibility;
});
m_trayIconMenu->addSeparator();
m_trayIconMenu->addAction(tr("Quit"), this, &QCoreApplication::quit);
qDebug(ltr("Tray icon context menu initialized"));
m_trayIcon = new QSystemTrayIcon(this);
m_trayIcon->setIcon(*m_errorIcon);
m_trayIcon->setContextMenu(m_trayIconMenu);
m_trayIcon->show();
m_trayIcon->setToolTip(tr("AdaptiveBrightness"));
connect(m_trayIcon, &QSystemTrayIcon::activated, this, &MainWindow::trayIconClicked);
qDebug(ltr("Tray icon initialized"));
}
}
void MainWindow::setupCallbackTimer()
{
m_timerThread = new QThread(this);
m_timer = new QTimer(nullptr);
m_timer->setInterval(1000);
m_timer->moveToThread(m_timerThread);
m_timerThread->connect(m_timer, &QTimer::timeout, this, &MainWindow::updateState, Qt::DirectConnection);
m_timer->connect(m_timerThread, SIGNAL(started()), SLOT(start()));
m_timerThread->start();
qDebug(ltr("Callback timer started"));
}
void MainWindow::updateState()
{
qDebug(ltr("Updating state"));

View File

@ -30,6 +30,10 @@ class MainWindow : public QMainWindow {
void loadIcons();
void setupMonitorsTab();
void setupTrayIcon();
void setupCallbackTimer();
void updateState();
void trayIconClicked(QSystemTrayIcon::ActivationReason reason);