From afd6de62a0ba61e3c0199eb0c6dff77649f83b2e Mon Sep 17 00:00:00 2001 From: BlackMark Date: Sun, 12 Jul 2020 18:55:41 +0200 Subject: [PATCH] Refactor GUI setup --- AdaptiveBrightness/main_window.cpp | 96 +++++++++++++++++------------- AdaptiveBrightness/main_window.hpp | 2 + 2 files changed, 55 insertions(+), 43 deletions(-) diff --git a/AdaptiveBrightness/main_window.cpp b/AdaptiveBrightness/main_window.cpp index cecfd7a..d0fd6e8 100644 --- a/AdaptiveBrightness/main_window.cpp +++ b/AdaptiveBrightness/main_window.cpp @@ -31,50 +31,9 @@ MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent) m_brightness = newBrightness; }); - loadIcons(); + setupTrayIcon(); - 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")); + setupCallbackTimer(); m_sensors = enumerateSensors(); m_monitors = enumerateMonitors(); @@ -131,6 +90,57 @@ void MainWindow::loadIcons() qDebug(ltr("Icons loaded")); } +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")); diff --git a/AdaptiveBrightness/main_window.hpp b/AdaptiveBrightness/main_window.hpp index d87ee2e..04006d5 100644 --- a/AdaptiveBrightness/main_window.hpp +++ b/AdaptiveBrightness/main_window.hpp @@ -29,6 +29,8 @@ class MainWindow : public QMainWindow { void closeEvent(QCloseEvent* event) override; void loadIcons(); + void setupTrayIcon(); + void setupCallbackTimer(); void updateState();