Refactor GUI setup

This commit is contained in:
BlackMark 2020-07-12 18:55:41 +02:00
parent cb9a57a24a
commit afd6de62a0
2 changed files with 55 additions and 43 deletions

View File

@ -31,50 +31,9 @@ MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent)
m_brightness = newBrightness; m_brightness = newBrightness;
}); });
loadIcons(); setupTrayIcon();
if(!QSystemTrayIcon::isSystemTrayAvailable()) { setupCallbackTimer();
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_sensors = enumerateSensors();
m_monitors = enumerateMonitors(); m_monitors = enumerateMonitors();
@ -131,6 +90,57 @@ void MainWindow::loadIcons()
qDebug(ltr("Icons loaded")); 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() void MainWindow::updateState()
{ {
qDebug(ltr("Updating state")); qDebug(ltr("Updating state"));

View File

@ -29,6 +29,8 @@ class MainWindow : public QMainWindow {
void closeEvent(QCloseEvent* event) override; void closeEvent(QCloseEvent* event) override;
void loadIcons(); void loadIcons();
void setupTrayIcon();
void setupCallbackTimer();
void updateState(); void updateState();