From fe57a7f321e96db9cb5a9eada32addc8c5d04c36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ole-Andr=C3=A9=20Rodlie?= Date: Sun, 4 Feb 2024 14:17:14 +0100 Subject: [PATCH] Update dialog (wip) --- src/powerkit_dialog.cpp | 240 +++++++++++++++++++++++++++++----------- src/powerkit_dialog.h | 19 +++- 2 files changed, 196 insertions(+), 63 deletions(-) diff --git a/src/powerkit_dialog.cpp b/src/powerkit_dialog.cpp index ba10d7c..0cab6af 100644 --- a/src/powerkit_dialog.cpp +++ b/src/powerkit_dialog.cpp @@ -12,6 +12,7 @@ #include "powerkit_settings.h" #include "powerkit_backlight.h" #include "powerkit_client.h" +#include "powerkit_cpu.h" #include @@ -22,6 +23,7 @@ using namespace PowerKit; Dialog::Dialog(QWidget *parent, bool quitOnClose) : QDialog(parent) + , cpuTimer(nullptr) , dbus(nullptr) , lidActionBattery(nullptr) , lidActionAC(nullptr) @@ -47,11 +49,25 @@ Dialog::Dialog(QWidget *parent, , batteryBacklightLabel(nullptr) , acBacklightLabel(nullptr) , backlightMouseWheel(nullptr) + , batteryStatusLabel(nullptr) + , cpuFreqLabel(nullptr) + , cpuTempLabel(nullptr) + , hasCpuCoreTemp(false) + , hasBattery(false) { // setup dialog if (quitOnClose) { setAttribute(Qt::WA_QuitOnClose, true); } setWindowTitle(tr("Power Manager")); - setMinimumSize(QSize(390, 310)); + setMinimumSize(QSize(390, 125)); + + // setup cpu timer + cpuTimer = new QTimer(this); + cpuTimer->setInterval(1000); + connect(cpuTimer, + SIGNAL(timeout()), + this, + SLOT(drawCpu())); + cpuTimer->start(); // setup dbus QDBusConnection session = QDBusConnection::sessionBus(); @@ -62,70 +78,23 @@ Dialog::Dialog(QWidget *parent, if (!dbus->isValid()) { QMessageBox::warning(this, tr("powerkit not running"), - tr("powerkit is not running, please start powerkit before running settings.")); + tr("powerkit is not running, please start powerkit before running this application.")); QTimer::singleShot(100, qApp, SLOT(quit())); return; } + // detect device changes + connect(dbus, SIGNAL(UpdatedDevices()), + this, SLOT(handleUpdatedDevices())); + // trigger generation of powerkit.conf if not exists Settings::getConf(); - // setup theme - Theme::setAppTheme(); - Theme::setIconTheme(); - setWindowIcon(QIcon::fromTheme(DEFAULT_AC_ICON)); - - setupWidgets(); // setup widgets - populate(); // populate boxes - loadSettings(); // load settings - - // connect widgets - connect(lidActionBattery, SIGNAL(currentIndexChanged(int)), - this, SLOT(handleLidActionBattery(int))); - connect(lidActionAC, SIGNAL(currentIndexChanged(int)), - this, SLOT(handleLidActionAC(int))); - connect(criticalActionBattery, SIGNAL(currentIndexChanged(int)), - this, SLOT(handleCriticalAction(int))); - connect(criticalBattery, SIGNAL(valueChanged(int)), - this, SLOT(handleCriticalBattery(int))); - connect(autoSleepBattery, SIGNAL(valueChanged(int)), - this, SLOT(handleAutoSleepBattery(int))); - connect(autoSleepAC, SIGNAL(valueChanged(int)), - this, SLOT(handleAutoSleepAC(int))); - connect(showNotifications, SIGNAL(toggled(bool)), - this, SLOT(handleShowNotifications(bool))); - connect(showSystemTray, SIGNAL(toggled(bool)), - this, SLOT(handleShowSystemTray(bool))); - connect(disableLidAction, SIGNAL(toggled(bool)), - this, SLOT(handleDisableLidAction(bool))); - connect(autoSleepBatteryAction, SIGNAL(currentIndexChanged(int)), - this, SLOT(handleAutoSleepBatteryAction(int))); - connect(autoSleepACAction, SIGNAL(currentIndexChanged(int)), - this, SLOT(handleAutoSleepACAction(int))); - connect(backlightBatteryCheck, SIGNAL(toggled(bool)), - this, SLOT(handleBacklightBatteryCheck(bool))); - connect(backlightACCheck, SIGNAL(toggled(bool)), - this, SLOT(handleBacklightACCheck(bool))); - connect(backlightSliderBattery, SIGNAL(valueChanged(int)), - this, SLOT(handleBacklightBatterySlider(int))); - connect(backlightSliderAC, SIGNAL(valueChanged(int)), - this, SLOT(handleBacklightACSlider(int))); - connect(backlightBatteryLowerCheck, SIGNAL(toggled(bool)), - this, SLOT(handleBacklightBatteryCheckLower(bool))); - connect(backlightACHigherCheck, SIGNAL(toggled(bool)), - this, SLOT(handleBacklightACCheckHigher(bool))); - connect(warnOnLowBattery, SIGNAL(toggled(bool)), - this, SLOT(handleWarnOnLowBattery(bool))); - connect(warnOnVeryLowBattery, SIGNAL(toggled(bool)), - this, SLOT(handleWarnOnVeryLowBattery(bool))); - connect(notifyOnBattery, SIGNAL(toggled(bool)), - this, SLOT(handleNotifyBattery(bool))); - connect(notifyOnAC, SIGNAL(toggled(bool)), - this, SLOT(handleNotifyAC(bool))); - connect(notifyNewInhibitor, SIGNAL(toggled(bool)), - this, SLOT(handleNotifyNewInhibitor(bool))); - connect(backlightMouseWheel, SIGNAL(toggled(bool)), - this, SLOT(handleBacklightMouseWheel(bool))); + setupWidgets(); + populateWidgets(); + loadSettings(); + connectWidgets(); + handleUpdatedDevices(); } Dialog::~Dialog() @@ -135,11 +104,42 @@ Dialog::~Dialog() void Dialog::setupWidgets() { + // setup theme + Theme::setAppTheme(); + Theme::setIconTheme(); + setWindowIcon(QIcon::fromTheme(DEFAULT_AC_ICON)); + // setup widgets QVBoxLayout *layout = new QVBoxLayout(this); layout->setMargin(5); layout->setSpacing(0); + // status widgets + + const auto statusWidget = new QGroupBox(this); + statusWidget->setTitle(tr("Status")); + statusWidget->setSizePolicy(QSizePolicy::Expanding, + QSizePolicy::Expanding); + const auto statusLayout = new QHBoxLayout(statusWidget); + //statusLayout->setMargin(0); + //statusLayout->setSpacing(0); + + batteryStatusLabel = new QLabel(this); + batteryStatusLabel->setMaximumSize(64, 64); + batteryStatusLabel->setMinimumSize(64, 64); + + cpuFreqLabel = new QLabel(this); + cpuFreqLabel->setMaximumSize(64, 64); + cpuFreqLabel->setMinimumSize(64, 64); + + cpuTempLabel = new QLabel(this); + cpuTempLabel->setMaximumSize(64, 64); + cpuTempLabel->setMinimumSize(64, 64); + + statusLayout->addWidget(batteryStatusLabel); + statusLayout->addWidget(cpuFreqLabel); + statusLayout->addWidget(cpuTempLabel); + // battery QGroupBox *batteryContainer = new QGroupBox(this); batteryContainer->setTitle(tr("On Battery")); @@ -282,8 +282,8 @@ void Dialog::setupWidgets() batteryBacklightLabel->setToolTip(tr("Override brightness when switched to battery power.")); batteryBacklightContainerLayout->addWidget(batteryBacklightIcon); batteryBacklightContainerLayout->addWidget(batteryBacklightLabel); - batteryBacklightContainerLayout->addWidget(backlightBatteryCheck); batteryBacklightContainerLayout->addStretch(); + batteryBacklightContainerLayout->addWidget(backlightBatteryCheck); batteryBacklightContainerLayout->addWidget(batteryBacklightOptContainer); // add battery widgets to container @@ -402,14 +402,14 @@ void Dialog::setupWidgets() acBacklightLabel->setToolTip(tr("Override brightness when switched to AC power.")); acBacklightContainerLayout->addWidget(acBacklightIcon); acBacklightContainerLayout->addWidget(acBacklightLabel); - acBacklightContainerLayout->addWidget(backlightACCheck); acBacklightContainerLayout->addStretch(); + acBacklightContainerLayout->addWidget(backlightACCheck); acBacklightContainerLayout->addWidget(acBacklightOptContainer); // add widgets to ac acContainerLayout->addWidget(sleepACContainer); - acContainerLayout->addWidget(acBacklightContainer); acContainerLayout->addStretch(); + acContainerLayout->addWidget(acBacklightContainer); // common QGroupBox *daemonContainer = new QGroupBox(this); @@ -496,6 +496,7 @@ void Dialog::setupWidgets() settingsContainerArea->setWidget(settingsWidget); // add widgets to settings + settingsLayout->addWidget(statusWidget); settingsLayout->addWidget(batteryContainer); settingsLayout->addWidget(acContainer); settingsLayout->addWidget(daemonContainer); @@ -504,10 +505,12 @@ void Dialog::setupWidgets() settingsLayout->addStretch(); layout->addWidget(settingsContainerArea); + + handleUpdatedDevices(); } // populate widgets with default values -void Dialog::populate() +void Dialog::populateWidgets() { lidActionBattery->clear(); lidActionBattery->addItem(QIcon::fromTheme(DEFAULT_NONE_ICON), @@ -572,6 +575,56 @@ void Dialog::populate() tr("Hybrid Sleep"), suspendHybrid); } +void Dialog::connectWidgets() +{ + connect(lidActionBattery, SIGNAL(currentIndexChanged(int)), + this, SLOT(handleLidActionBattery(int))); + connect(lidActionAC, SIGNAL(currentIndexChanged(int)), + this, SLOT(handleLidActionAC(int))); + connect(criticalActionBattery, SIGNAL(currentIndexChanged(int)), + this, SLOT(handleCriticalAction(int))); + connect(criticalBattery, SIGNAL(valueChanged(int)), + this, SLOT(handleCriticalBattery(int))); + connect(autoSleepBattery, SIGNAL(valueChanged(int)), + this, SLOT(handleAutoSleepBattery(int))); + connect(autoSleepAC, SIGNAL(valueChanged(int)), + this, SLOT(handleAutoSleepAC(int))); + connect(showNotifications, SIGNAL(toggled(bool)), + this, SLOT(handleShowNotifications(bool))); + connect(showSystemTray, SIGNAL(toggled(bool)), + this, SLOT(handleShowSystemTray(bool))); + connect(disableLidAction, SIGNAL(toggled(bool)), + this, SLOT(handleDisableLidAction(bool))); + connect(autoSleepBatteryAction, SIGNAL(currentIndexChanged(int)), + this, SLOT(handleAutoSleepBatteryAction(int))); + connect(autoSleepACAction, SIGNAL(currentIndexChanged(int)), + this, SLOT(handleAutoSleepACAction(int))); + connect(backlightBatteryCheck, SIGNAL(toggled(bool)), + this, SLOT(handleBacklightBatteryCheck(bool))); + connect(backlightACCheck, SIGNAL(toggled(bool)), + this, SLOT(handleBacklightACCheck(bool))); + connect(backlightSliderBattery, SIGNAL(valueChanged(int)), + this, SLOT(handleBacklightBatterySlider(int))); + connect(backlightSliderAC, SIGNAL(valueChanged(int)), + this, SLOT(handleBacklightACSlider(int))); + connect(backlightBatteryLowerCheck, SIGNAL(toggled(bool)), + this, SLOT(handleBacklightBatteryCheckLower(bool))); + connect(backlightACHigherCheck, SIGNAL(toggled(bool)), + this, SLOT(handleBacklightACCheckHigher(bool))); + connect(warnOnLowBattery, SIGNAL(toggled(bool)), + this, SLOT(handleWarnOnLowBattery(bool))); + connect(warnOnVeryLowBattery, SIGNAL(toggled(bool)), + this, SLOT(handleWarnOnVeryLowBattery(bool))); + connect(notifyOnBattery, SIGNAL(toggled(bool)), + this, SLOT(handleNotifyBattery(bool))); + connect(notifyOnAC, SIGNAL(toggled(bool)), + this, SLOT(handleNotifyAC(bool))); + connect(notifyNewInhibitor, SIGNAL(toggled(bool)), + this, SLOT(handleNotifyNewInhibitor(bool))); + connect(backlightMouseWheel, SIGNAL(toggled(bool)), + this, SLOT(handleBacklightMouseWheel(bool))); +} + // load settings and set defaults void Dialog::loadSettings() { @@ -724,6 +777,8 @@ void Dialog::loadSettings() enableBacklight(true); enableLid(Client::lidIsPresent(dbus)); + hasCpuCoreTemp = Cpu::hasCoreTemp(); + if (!hasCpuCoreTemp) { cpuTempLabel->setVisible(false); } } void Dialog::saveSettings() @@ -806,6 +861,67 @@ void Dialog::setDefaultAction(QComboBox *box, QString value) } } +void Dialog::drawBattery() +{ + if (!hasBattery) { return; } + const auto battery = Client::getBatteryLeft(dbus); + const auto onBattery = Client::onBattery(dbus); + QString batteryTime = QDateTime::fromTime_t(onBattery ? Client::timeToEmpty(dbus) : Client::timeToFull(dbus)).toUTC().toString("hh:mm"); + QColor color = Qt::green; + if (onBattery) { + if (battery >= 76) { + color = Qt::gray; + } else if (battery >= 51) { + color = Qt::yellow; + } else if (battery >= 26) { + color = QColor("orange"); + } else { + color = Qt::red; + } + } + batteryStatusLabel->setPixmap(Theme::drawCircleProgress(battery, + 64, + 4, + 4, + true, + QString("%1%\n%2").arg(QString::number(battery), batteryTime), + color, + Qt::white)); +} + +void Dialog::drawCpu() +{ + if (cpuFreqLabel) { + const auto freq = Cpu::getCpuFreqLabel(); + cpuFreqLabel->setPixmap(Theme::drawCircleProgress(freq.first, + 64, + 4, + 4, + true, + freq.second, + Qt::gray, + Qt::white)); + } + if (cpuTempLabel && hasCpuCoreTemp) { + const auto temp = Cpu::getCpuTempLabel(); + cpuTempLabel->setPixmap(Theme::drawCircleProgress(temp.first, + 64, + 4, + 4, + true, + temp.second, + Qt::gray, + Qt::white)); + } +} + +void Dialog::handleUpdatedDevices() +{ + hasBattery = Client::hasBattery(dbus); + batteryStatusLabel->setVisible(hasBattery); + drawBattery(); +} + // save current value and update power manager void Dialog::handleLidActionBattery(int index) { diff --git a/src/powerkit_dialog.h b/src/powerkit_dialog.h index fbbf948..3c1e89d 100644 --- a/src/powerkit_dialog.h +++ b/src/powerkit_dialog.h @@ -27,6 +27,7 @@ #include #include #include +#include namespace PowerKit { @@ -40,6 +41,7 @@ namespace PowerKit ~Dialog(); private: + QTimer *cpuTimer; QDBusInterface *dbus; QComboBox *lidActionBattery; QComboBox *lidActionAC; @@ -71,14 +73,29 @@ namespace PowerKit QLabel *acBacklightLabel; QCheckBox *backlightMouseWheel; + QLabel *batteryStatusLabel; + QLabel *cpuFreqLabel; + QLabel *cpuTempLabel; + + bool hasCpuCoreTemp; + bool hasBattery; + private slots: void setupWidgets(); - void populate(); + void populateWidgets(); + void connectWidgets(); void loadSettings(); void saveSettings(); + void setDefaultAction(QComboBox *box, int action); void setDefaultAction(QSpinBox *box, int action); void setDefaultAction(QComboBox *box, QString value); + + void drawBattery(); + void drawCpu(); + + void handleUpdatedDevices(); + void handleLidActionBattery(int index); void handleLidActionAC(int index); void handleCriticalAction(int index);