Skip to content

Commit

Permalink
rafactored utils, use namespace, removed KDE specific code, use custo…
Browse files Browse the repository at this point in the history
…m icons everywhere
  • Loading branch information
Odizinne committed Sep 21, 2024
1 parent 38eea31 commit 7ec5bf4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 82 deletions.
10 changes: 6 additions & 4 deletions src/HeadsetControlQt/headsetcontrolqt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include <QLocale>
#include <QTranslator>

using namespace Utils;

#ifdef _WIN32
const QString HeadsetControlQt::headsetcontrolExecutable = "dependencies/headsetcontrol.exe";
const QString HeadsetControlQt::settingsFile = QStandardPaths::writableLocation(
Expand Down Expand Up @@ -353,23 +355,23 @@ void HeadsetControlQt::updateUIWithHeadsetInfo(const QJsonObject &headsetInfo)
ui->themeComboBox->currentIndex();

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

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

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

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

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

}
ui->ledBox->setEnabled(capabilities.contains("lights"));
Expand All @@ -388,7 +390,7 @@ void HeadsetControlQt::noDeviceFound()
trayIcon->setToolTip(tr("No Device Found"));
QString iconPath = getBatteryIconPath(0, false, true, ui->themeComboBox->currentIndex());

trayIcon->setIcon(getBatteryIcon(iconPath));
trayIcon->setIcon(QIcon(iconPath));

}

Expand Down
81 changes: 8 additions & 73 deletions src/Utils/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ bool isDarkMode(const QColor &color) {
return brightness < 127;
}

void setFrameColorBasedOnWindow(QWidget *window, QFrame *frame) {
void Utils::setFrameColorBasedOnWindow(QWidget *window, QFrame *frame) {
QColor main_bg_color = window->palette().color(QPalette::Window);
QColor frame_bg_color;

Expand All @@ -51,28 +51,12 @@ void setFrameColorBasedOnWindow(QWidget *window, QFrame *frame) {
frame->setPalette(palette);
}

QString getBatteryIconPath(int batteryLevel, bool charging, bool missing, int themeIndex)
QString Utils::getBatteryIconPath(int batteryLevel, bool charging, bool missing, int themeIndex)
{
QString theme;

if (themeIndex == 0) {
#ifdef _WIN32
theme = getTheme();
#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")) {
theme = "symbolic";
} else {
theme = "light";
}
} else if (desktop.contains("GNOME", Qt::CaseInsensitive)) {
theme = "light";
} else {
theme = "dark";
}
#endif
} else if (themeIndex == 1) {
theme = "dark";
} else if (themeIndex == 2) {
Expand All @@ -92,20 +76,15 @@ QString getBatteryIconPath(int batteryLevel, bool charging, bool missing, int th
else if (batteryLevel >= 0) iconName = "battery-020-" + theme;
}

QString iconPath;
if (theme == "symbolic") {
iconPath = iconName;
} else {
iconPath = QString(":/icons/%1.png").arg(iconName);
}

return iconPath;
return QString(":/icons/%1.png").arg(iconName);;
}


#ifdef _WIN32
QString getTheme()
QString Utils::getTheme()
{
#ifdef __linux__
return "light"
#elif _WIN32
// Determine the theme based on registry value
QSettings settings(
"HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize",
Expand All @@ -114,49 +93,5 @@ QString getTheme()

// Return the opposite to match icon (dark icon on light theme)
return (value == 0) ? "light" : "dark";
}
#endif

#ifdef __linux__
QString getKDEPlasmaVersion() {
QProcess process;
process.start("plasmashell", QStringList() << "--version");
process.waitForFinished();

QString output = process.readAllStandardOutput();
QStringList lines = output.split('\n');
QString versionLine;

// Find the line containing the version info
for (const QString &line : lines) {
if (line.contains("plasmashell")) {
versionLine = line;
break;
}
}

// Extract version number from the line
QString version = versionLine.split(' ').last().trimmed();
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
}
11 changes: 6 additions & 5 deletions src/Utils/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
#include <QString>
#include <QFrame>

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);
namespace Utils {
QString getBatteryIconPath(int batteryLevel, bool charging, bool missing, int themeIndex);
QString getTheme();
void setFrameColorBasedOnWindow(QWidget *window, QFrame *frame);
}

#endif // UTILS_H

0 comments on commit 7ec5bf4

Please sign in to comment.