Skip to content

Commit

Permalink
Removed translation in main, added language hotswap in ui, updated al…
Browse files Browse the repository at this point in the history
…l ts files
  • Loading branch information
Odizinne committed Sep 15, 2024
1 parent 823d4b3 commit 316c6a7
Show file tree
Hide file tree
Showing 10 changed files with 296 additions and 209 deletions.
64 changes: 58 additions & 6 deletions src/HeadsetControlQt/headsetcontrolqt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@ HeadsetControlQt::HeadsetControlQt(QWidget *parent)
{
ui->setupUi(this);
setWindowIcon(QIcon(":/icons/icon.png"));
loadSettings();
initUI();
createTrayIcon();
createTrayMenu();
loadSettings();
setupUIConnections();
updateHeadsetInfo();

connect(worker, &Worker::workRequested, worker, &Worker::doWork);
connect(worker, &Worker::sendHeadsetInfo, this, &::HeadsetControlQt::handleHeadsetInfo);

connect(timer, &QTimer::timeout, worker, &Worker::requestWork);

worker->moveToThread(&workerThread);
Expand Down Expand Up @@ -92,7 +94,6 @@ void HeadsetControlQt::initUI()
setFrameColorBasedOnWindow(this, ui->frame_2);
populateComboBoxes();
checkStartupCheckbox();
setupUIConnections();
}

void HeadsetControlQt::setupUIConnections()
Expand All @@ -105,13 +106,21 @@ void HeadsetControlQt::setupUIConnections()
connect(ui->themeComboBox, &QComboBox::currentIndexChanged, this, &HeadsetControlQt::onThemeComboBoxCurrentIndexChanged);
connect(ui->lowBatteryThresholdSpinBox, &QSpinBox::valueChanged, this, &HeadsetControlQt::saveSettings);
connect(ui->soundBatteryCheckBox, &QCheckBox::stateChanged, this, &HeadsetControlQt::saveSettings);
connect(ui->languageComboBox, &QComboBox::currentIndexChanged, this, &HeadsetControlQt::changeApplicationLanguage);
}

void HeadsetControlQt::populateComboBoxes()
{
ui->themeComboBox->addItem(tr("System"));
ui->themeComboBox->addItem(tr("Dark"));
ui->themeComboBox->addItem(tr("Light"));
ui->languageComboBox->addItem("System");
ui->languageComboBox->addItem("english");
ui->languageComboBox->addItem("français");
ui->languageComboBox->addItem("deutsch");
ui->languageComboBox->addItem("español");
ui->languageComboBox->addItem("italiano");
ui->languageComboBox->addItem("magyar");
}

void HeadsetControlQt::checkStartupCheckbox()
Expand All @@ -126,6 +135,12 @@ void HeadsetControlQt::checkStartupCheckbox()
void HeadsetControlQt::createTrayIcon()
{
trayIcon->setIcon(QIcon(":/icons/icon.png"));
trayIcon->show();
connect(trayIcon, &QSystemTrayIcon::activated, this, &HeadsetControlQt::trayIconActivated);
}

void HeadsetControlQt::createTrayMenu()
{
QMenu *trayMenu = new QMenu(this);
QAction *showAction = new QAction(tr("Show"), this);
connect(showAction, &QAction::triggered, this, &HeadsetControlQt::toggleWindow);
Expand All @@ -134,8 +149,6 @@ void HeadsetControlQt::createTrayIcon()
connect(exitAction, &QAction::triggered, this, &QApplication::quit);
trayMenu->addAction(exitAction);
trayIcon->setContextMenu(trayMenu);
trayIcon->show();
connect(trayIcon, &QSystemTrayIcon::activated, this, &HeadsetControlQt::trayIconActivated);
}

void HeadsetControlQt::createDefaultSettings()
Expand Down Expand Up @@ -181,6 +194,8 @@ void HeadsetControlQt::applySettings()
ui->themeComboBox->setCurrentIndex(settings.value("theme").toInt());
ui->lowBatteryThresholdSpinBox->setValue(settings.value("low_battery_threshold").toInt());
ui->soundBatteryCheckBox->setChecked(settings.value("sound_low_battery").toBool());
ui->languageComboBox->setCurrentIndex(settings.value("language").toInt());
changeApplicationLanguage();
setSidetone();
toggleLED(ui->ledBox->isChecked());
}
Expand All @@ -194,6 +209,7 @@ void HeadsetControlQt::saveSettings()
settings["theme"] = ui->themeComboBox->currentIndex();
settings["low_battery_threshold"] = ui->lowBatteryThresholdSpinBox->value();
settings["sound_low_battery"] = ui->soundBatteryCheckBox->isChecked();
settings["language"] = ui->languageComboBox->currentIndex();

QFile file(settingsFile);
if (file.open(QIODevice::WriteOnly)) {
Expand Down Expand Up @@ -284,7 +300,6 @@ void HeadsetControlQt::sendNotificationBasedOnBattery(const QJsonObject &headset
void HeadsetControlQt::sendSoundNotificationBasedOnBattery(const QJsonObject &headsetInfo)
{
QJsonObject batteryInfo = headsetInfo["battery"].toObject();
QString headsetName = headsetInfo["device"].toString();
int batteryLevel = batteryInfo["level"].toInt();
QString batteryStatus = batteryInfo["status"].toString();
bool available = (batteryStatus == "BATTERY_AVAILABLE");
Expand Down Expand Up @@ -519,3 +534,40 @@ void HeadsetControlQt::sendFirstMinimizeNotification()
firstRun = false;
}
}

void HeadsetControlQt::changeApplicationLanguage()
{
QString language = ui->languageComboBox->currentText().toLower();
QTranslator translator;

if (qApp->removeTranslator(&translator)) {
}

QString languageCode;
if (language == "system") {
QLocale systemLocale;
languageCode = systemLocale.name().left(2);
} else {
QMap<QString, QString> languageCodes;
languageCodes["english"] = "en";
languageCodes["français"] = "fr";
languageCodes["deutsch"] = "de";
languageCodes["español"] = "es";
languageCodes["italiano"] = "it";
languageCodes["magyar"] = "hu";

languageCode = languageCodes.value(language, "en");
}

QString translationFile = QString(":/translations/tr/HeadsetControl-Qt_%1.qm").arg(languageCode);
if (translator.load(translationFile)) {
qApp->installTranslator(&translator);
} else {
qWarning() << "Failed to load translation file:" << translationFile;
}

ui->retranslateUi(this);
delete trayIcon->contextMenu();
createTrayMenu();
saveSettings();
}
2 changes: 2 additions & 0 deletions src/HeadsetControlQt/headsetcontrolqt.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ private slots:
void trayIconActivated(QSystemTrayIcon::ActivationReason reason);
void onStartupCheckBoxStateChanged();
void handleHeadsetInfo(const QJsonObject &headsetInfo);
void changeApplicationLanguage();

private:
void initUI();
void setupUIConnections();
void populateComboBoxes();
void checkStartupCheckbox();
void createTrayIcon();
void createTrayMenu();
void loadSettings();
void applySettings();
void saveSettings();
Expand Down
89 changes: 56 additions & 33 deletions src/HeadsetControlQt/headsetcontrolqt.ui
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>388</width>
<height>429</height>
<width>380</width>
<height>460</height>
</rect>
</property>
<property name="sizePolicy">
Expand Down Expand Up @@ -70,67 +70,64 @@
<enum>QFrame::Shadow::Raised</enum>
</property>
<layout class="QGridLayout" name="gridLayout_5">
<item row="0" column="0">
<widget class="QLabel" name="startupLabel">
<item row="1" column="0">
<widget class="QLabel" name="label_2">
<property name="minimumSize">
<size>
<width>0</width>
<height>25</height>
</size>
</property>
<property name="text">
<string>Run at startup</string>
<string>Low battery threshold</string>
</property>
</widget>
</item>
<item row="0" column="2">
<widget class="QCheckBox" name="startupCheckbox">
<item row="1" column="2">
<widget class="QSpinBox" name="lowBatteryThresholdSpinBox">
<property name="minimumSize">
<size>
<width>0</width>
<height>25</height>
</size>
</property>
<property name="layoutDirection">
<enum>Qt::LayoutDirection::RightToLeft</enum>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>16777215</height>
</size>
</property>
<property name="text">
<string/>
<property name="minimum">
<number>0</number>
</property>
<property name="maximum">
<number>100</number>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_2">
<item row="2" column="2">
<widget class="QComboBox" name="themeComboBox">
<property name="minimumSize">
<size>
<width>0</width>
<height>25</height>
</size>
</property>
<property name="text">
<string>Low battery threshold</string>
<property name="sizeAdjustPolicy">
<enum>QComboBox::SizeAdjustPolicy::AdjustToContents</enum>
</property>
</widget>
</item>
<item row="1" column="2">
<widget class="QSpinBox" name="lowBatteryThresholdSpinBox">
<item row="0" column="0">
<widget class="QLabel" name="startupLabel">
<property name="minimumSize">
<size>
<width>0</width>
<height>25</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>16777215</height>
</size>
</property>
<property name="minimum">
<number>0</number>
</property>
<property name="maximum">
<number>100</number>
<property name="text">
<string>Run at startup</string>
</property>
</widget>
</item>
Expand All @@ -147,6 +144,22 @@
</property>
</widget>
</item>
<item row="0" column="2">
<widget class="QCheckBox" name="startupCheckbox">
<property name="minimumSize">
<size>
<width>0</width>
<height>25</height>
</size>
</property>
<property name="layoutDirection">
<enum>Qt::LayoutDirection::RightToLeft</enum>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
<item row="2" column="1">
<spacer name="horizontalSpacer">
<property name="orientation">
Expand All @@ -160,16 +173,26 @@
</property>
</spacer>
</item>
<item row="2" column="2">
<widget class="QComboBox" name="themeComboBox">
<item row="3" column="0">
<widget class="QLabel" name="label_3">
<property name="minimumSize">
<size>
<width>0</width>
<height>25</height>
</size>
</property>
<property name="sizeAdjustPolicy">
<enum>QComboBox::SizeAdjustPolicy::AdjustToContents</enum>
<property name="text">
<string>Language</string>
</property>
</widget>
</item>
<item row="3" column="2">
<widget class="QComboBox" name="languageComboBox">
<property name="minimumSize">
<size>
<width>0</width>
<height>25</height>
</size>
</property>
</widget>
</item>
Expand All @@ -190,7 +213,7 @@
</font>
</property>
<property name="text">
<string>device</string>
<string notr="true">device</string>
</property>
</widget>
</item>
Expand Down
Loading

0 comments on commit 316c6a7

Please sign in to comment.