Skip to content

Commit

Permalink
Refactor getBatteryIcon
Browse files Browse the repository at this point in the history
  • Loading branch information
bbacskay authored and Odizinne committed Sep 17, 2024
1 parent bc5a901 commit 9b05907
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 70 deletions.
82 changes: 14 additions & 68 deletions src/HeadsetControlQt/headsetcontrolqt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,67 +351,26 @@ void HeadsetControlQt::updateUIWithHeadsetInfo(const QJsonObject &headsetInfo)
ui->batteryBar->setFormat(QString::number(batteryLevel) + "%");
trayIcon->setToolTip(QString("%1: %2%").arg(deviceName).arg(batteryLevel));
ui->themeComboBox->currentIndex();
QString iconPath = getBatteryIcon(batteryLevel, false, false, ui->themeComboBox->currentIndex());
#ifdef _WIN32
trayIcon->setIcon(QIcon(iconPath));
#elif __linux__
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
QString desktop = env.value("XDG_CURRENT_DESKTOP");
if (desktop.contains("KDE", Qt::CaseInsensitive)) {
QString kdeVersion = getKDEPlasmaVersion();
if (kdeVersion.startsWith("5")) {
trayIcon->setIcon(QIcon(iconPath));
} else if (kdeVersion.startsWith("6")) {
trayIcon->setIcon(QIcon::fromTheme(iconPath));
}
} else {
trayIcon->setIcon(QIcon(iconPath));
}
#endif

QString iconPath = getBatteryIconPath(batteryLevel, false, false, ui->themeComboBox->currentIndex());
trayIcon->setIcon(getBatteryIcon(iconPath));

} else if (batteryStatus == "BATTERY_CHARGING") {
ui->batteryBar->setValue(0);
ui->batteryBar->setFormat("Charging");
trayIcon->setToolTip(QString("%1: Charging").arg(deviceName));

QString iconPath = getBatteryIcon(batteryLevel, true, false, ui->themeComboBox->currentIndex());
#ifdef _WIN32
trayIcon->setIcon(QIcon(iconPath));
#elif __linux__
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
QString desktop = env.value("XDG_CURRENT_DESKTOP");
if (desktop.contains("KDE", Qt::CaseInsensitive)) {
QString kdeVersion = getKDEPlasmaVersion();
if (kdeVersion.startsWith("5")) {
trayIcon->setIcon(QIcon(iconPath));
} else if (kdeVersion.startsWith("6")) {
trayIcon->setIcon(QIcon::fromTheme(iconPath));
}
} else {
trayIcon->setIcon(QIcon(iconPath));
}
#endif
QString iconPath = getBatteryIconPath(batteryLevel, true, false, ui->themeComboBox->currentIndex());
trayIcon->setIcon(getBatteryIcon(iconPath));

} else {
ui->batteryBar->setValue(0);
ui->batteryBar->setFormat(tr("Off"));
trayIcon->setToolTip(tr("No headset connected"));

QString iconPath = getBatteryIcon(batteryLevel, false, true, ui->themeComboBox->currentIndex());
#ifdef _WIN32
trayIcon->setIcon(QIcon(iconPath));
#elif __linux__
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
QString desktop = env.value("XDG_CURRENT_DESKTOP");
if (desktop.contains("KDE", Qt::CaseInsensitive)) {
QString kdeVersion = getKDEPlasmaVersion();
if (kdeVersion.startsWith("5")) {
trayIcon->setIcon(QIcon(iconPath));
} else if (kdeVersion.startsWith("6")) {
trayIcon->setIcon(QIcon::fromTheme(iconPath));
}
} else {
trayIcon->setIcon(QIcon(iconPath));
}
#endif
QString iconPath = getBatteryIconPath(batteryLevel, false, true, ui->themeComboBox->currentIndex());
trayIcon->setIcon(getBatteryIcon(iconPath));

}
ui->ledBox->setEnabled(capabilities.contains("lights"));
ui->ledLabel->setEnabled(capabilities.contains("lights"));
Expand All @@ -427,23 +386,10 @@ void HeadsetControlQt::noDeviceFound()
{
toggleUIElements(false);
trayIcon->setToolTip(tr("No Device Found"));
QString iconPath = getBatteryIcon(0, false, true, ui->themeComboBox->currentIndex());
#ifdef _WIN32
trayIcon->setIcon(QIcon(iconPath));
#elif __linux__
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
QString desktop = env.value("XDG_CURRENT_DESKTOP");
if (desktop.contains("KDE", Qt::CaseInsensitive)) {
QString kdeVersion = getKDEPlasmaVersion();
if (kdeVersion.startsWith("5")) {
trayIcon->setIcon(QIcon(iconPath));
} else if (kdeVersion.startsWith("6")) {
trayIcon->setIcon(QIcon::fromTheme(iconPath));
}
} else {
trayIcon->setIcon(QIcon(iconPath));
}
#endif
QString iconPath = getBatteryIconPath(0, false, true, ui->themeComboBox->currentIndex());

trayIcon->setIcon(getBatteryIcon(iconPath));

}

void HeadsetControlQt::toggleUIElements(bool show)
Expand Down
22 changes: 21 additions & 1 deletion src/Utils/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void setFrameColorBasedOnWindow(QWidget *window, QFrame *frame) {
frame->setPalette(palette);
}

QString getBatteryIcon(int batteryLevel, bool charging, bool missing, int themeIndex)
QString getBatteryIconPath(int batteryLevel, bool charging, bool missing, int themeIndex)
{
QString theme;
if (themeIndex == 0) {
Expand Down Expand Up @@ -140,3 +140,23 @@ QString getKDEPlasmaVersion() {
return version;
}
#endif

QIcon getBatteryIcon(const QString &BatteryIconPath)
{
#ifdef _WIN32
return QIcon(BatteryIconPath);
#elif __linux__
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
QString desktop = env.value("XDG_CURRENT_DESKTOP");
if (desktop.contains("KDE", Qt::CaseInsensitive)) {
QString kdeVersion = getKDEPlasmaVersion();
if (kdeVersion.startsWith("6")) {
return QIcon::fromTheme(BatteryIconPath);
} else {
return QIcon(BatteryIconPath);
}
} else {
return QIcon(BatteryIconPath);
}
#endif
}
3 changes: 2 additions & 1 deletion src/Utils/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
#include <QString>
#include <QFrame>

QString getBatteryIcon(int batteryLevel, bool charging, bool missing, int themeIndex);
QString getBatteryIconPath(int batteryLevel, bool charging, bool missing, int themeIndex);
QIcon getBatteryIcon(const QString &BatteryIconPath);
QString getTheme();
QString getKDEPlasmaVersion();
void setFrameColorBasedOnWindow(QWidget *window, QFrame *frame);
Expand Down

0 comments on commit 9b05907

Please sign in to comment.