diff --git a/src/application.cpp b/src/application.cpp index e0bd954..6724c6f 100644 --- a/src/application.cpp +++ b/src/application.cpp @@ -1,6 +1,6 @@ /* VolTrayke - Volume tray widget. - Copyright (C) 2021 Andrea Zanellato + Copyright (C) 2021-2023 Andrea Zanellato This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -13,7 +13,7 @@ You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., - 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. SPDX-License-Identifier: GPL-2.0-only */ @@ -33,19 +33,17 @@ #endif #include -#include -#include +#include #include #include #include -#include -#include -#include +#include #include -Qtilities::Application::Application(int& argc, char* argv[]) +Qtilities::Application::Application(int argc, char* argv[]) : QApplication(argc, argv) + , trayIcon_(new QSystemTrayIcon(this)) , engine_(nullptr) , channel_(nullptr) { @@ -58,53 +56,6 @@ Qtilities::Application::Application(int& argc, char* argv[]) initLocale(); initUi(); - - connect(this, &QApplication::aboutToQuit, mnuVolume_, &QObject::deleteLater); - connect(this, &QApplication::aboutToQuit, dlgAbout_, &QObject::deleteLater); - connect(this, &QApplication::aboutToQuit, dlgPrefs_, &QObject::deleteLater); - connect(this, &QApplication::aboutToQuit, trayIcon_, &QObject::deleteLater); - connect(this, &QApplication::aboutToQuit, this, &Application::onAboutToQuit); - - connect(dlgPrefs_, &DialogPrefs::sigEngineChanged, - this, &Application::onAudioEngineChanged); - - connect(dlgPrefs_, &DialogPrefs::sigChannelChanged, - this, &Application::onAudioDeviceChanged); - - connect(dlgPrefs_, &DialogPrefs::sigPrefsChanged, - this, [=] { -#if USE_ALSA - engine_->setNormalized(settings_.isNormalized()); - AlsaEngine* alsa = qobject_cast(engine_); - AlsaDevice* dev = qobject_cast(channel_); - if (alsa && dev) - alsa->updateDevice(dev); -#endif - mnuVolume_->setPageStep(settings_.pageStep()); - mnuVolume_->setSingleStep(settings_.singleStep()); - }); - - connect(mnuVolume_, &MenuVolume::sigRunMixer, this, &Application::runMixer); - connect(mnuVolume_, &MenuVolume::sigMuteToggled, this, [=](bool muted) { - if (!channel_) - return; - - channel_->setMute(muted); - updateTrayIcon(); - }); - connect(mnuVolume_, &MenuVolume::sigVolumeChanged, this, [=](int volume) { - if (!channel_) - return; - - channel_->setVolume(volume); - updateTrayIcon(); - }); - connect(trayIcon_, &QSystemTrayIcon::activated, this, &Application::onTrayIconActivated); -} - -Qtilities::Application::~Application() -{ - qDebug() << "Destroyed VolTrayke"; } void Qtilities::Application::initLocale() @@ -115,14 +66,14 @@ void Qtilities::Application::initLocale() QLocale locale(QLocale("it")); QLocale::setDefault(locale); #endif - // install the translations built-into Qt itself - if (qtTranslator.load(QStringLiteral("qt_") + locale.name(), + // install the translations built into Qt itself + if (qtTranslator_.load(QStringLiteral("qt_") + locale.name(), #if QT_VERSION < 0x060000 - QLibraryInfo::location(QLibraryInfo::TranslationsPath))) + QLibraryInfo::location(QLibraryInfo::TranslationsPath))) #else - QLibraryInfo::path(QLibraryInfo::TranslationsPath))) + QLibraryInfo::path(QLibraryInfo::TranslationsPath))) #endif - installTranslator(&qtTranslator); + installTranslator(&qtTranslator_); // E.g. "_en" QString translationsFileName = QCoreApplication::applicationName().toLower() + '_' + locale.name(); @@ -130,46 +81,52 @@ void Qtilities::Application::initLocale() // otherwise read from system data QString translationsPath = QCoreApplication::applicationDirPath(); - bool isLoaded = translator.load(translationsFileName, translationsPath); + bool isLoaded = translator_.load(translationsFileName, translationsPath); if (!isLoaded) { // "/usr/share//translations - isLoaded = translator.load(translationsFileName, + isLoaded = translator_.load(translationsFileName, QStringLiteral(PROJECT_DATA_DIR) + QStringLiteral("/translations")); } if (isLoaded) - installTranslator(&translator); + installTranslator(&translator_); } void Qtilities::Application::initUi() { + settings_.load(); + actAutoStart_ = new QAction(tr("Auto&start"), this); - trayIcon_ = new QSystemTrayIcon(QIcon::fromTheme("audio-volume-medium"), this); - dlgAbout_ = new DialogAbout; - dlgPrefs_ = new DialogPrefs; mnuVolume_ = new MenuVolume; + mnuVolume_->loadSettings(); + mnuVolume_->show(); // FIXME: crash without this + mnuVolume_->hide(); - settings_.load(); - dlgPrefs_->loadSettings(); -#if 1 onAudioEngineChanged(settings_.engineId()); onAudioDeviceChanged(settings_.channelId()); - onAudioDeviceListChanged(); + updateDeviceList(); updateTrayIcon(); - if (channel_) - mnuVolume_->setVolume(channel_->volume()); -#endif - centerOnScreen(dlgPrefs_); - centerOnScreen(dlgAbout_); + if(channel_) { + channel_->setMute(settings_.isMuted()); + int volume = settings_.volume(); + if(volume >= 0 && volume <= 100) + channel_->setVolume(volume); + else + mnuVolume_->setVolume(channel_->volume()); + } actAutoStart_->setCheckable(true); actAutoStart_->setChecked(settings_.useAutostart()); - QAction* actAbout = new QAction(QIcon::fromTheme("help-about", QIcon(":/help-about")), tr("&About"), this); - QAction* actPrefs = new QAction(QIcon::fromTheme("preferences-system", QIcon(":/preferences-system")), tr("&Preferences"), this); - QAction* actQuit = new QAction(QIcon::fromTheme("application-exit", QIcon(":/application-exit")), tr("&Quit"), this); + QAction *actAbout + = new QAction(QIcon::fromTheme("help-about", QIcon(":/help-about")), tr("&About"), this); + QAction *actPrefs + = new QAction(QIcon::fromTheme("preferences-system", QIcon(":/preferences-system")), + tr("&Preferences"), this); + QAction *actQuit = new QAction(QIcon::fromTheme("application-exit", QIcon(":/application-exit")), + tr("&Quit"), this); - QMenu* mnuActions = new QMenu(); + QMenu* mnuActions = new QMenu(mnuVolume_); mnuActions->addAction(actAutoStart_); mnuActions->addAction(actPrefs); mnuActions->addAction(actAbout); @@ -178,13 +135,62 @@ void Qtilities::Application::initUi() trayIcon_->setContextMenu(mnuActions); trayIcon_->show(); - connect(actAbout, &QAction::triggered, - this, [=] { if (dlgAbout_->isHidden()) dlgAbout_->show(); }); + connect(actAbout, &QAction::triggered, this, &Application::about); + connect(actPrefs, &QAction::triggered, this, &Application::preferences); + connect(actQuit, &QAction::triggered, this, &Application::quit); - connect(actPrefs, &QAction::triggered, - this, [=] { if (dlgPrefs_->isHidden()) dlgPrefs_->show(); }); + connect(this, &QApplication::aboutToQuit, mnuVolume_, &QObject::deleteLater); + connect(this, &QApplication::aboutToQuit, trayIcon_, &QObject::deleteLater); + connect(this, &QApplication::aboutToQuit, this, &Application::onAboutToQuit); + + connect(mnuVolume_, &MenuVolume::sigRunMixer, this, &Application::runMixer); + connect(mnuVolume_, &MenuVolume::sigMuteToggled, this, [this](bool muted) { + if (!channel_) + return; - connect(actQuit, &QAction::triggered, qApp, &QCoreApplication::quit); + channel_->setMute(muted); + updateTrayIcon(); + }); + connect(mnuVolume_, &MenuVolume::sigVolumeChanged, this, &Application::onVolumeChanged); + connect(trayIcon_, &QSystemTrayIcon::activated, this, &Application::onTrayIconActivated); +} + +void Qtilities::Application::about() +{ + DialogAbout dlg(mnuVolume_); + centerOnScreen(&dlg); + dlg.exec(); +} + +void Qtilities::Application::preferences() +{ + DialogPrefs prefs(mnuVolume_); + prefs.loadSettings(); + prefs.setDeviceList(deviceList_); + + centerOnScreen(&prefs); + + connect(&prefs, &QDialog::accepted, this, &Application::onPrefsChanged); + connect(engine_, &AudioEngine::sinkListChanged, this, [&prefs, this] { + updateDeviceList(); + prefs.setDeviceList(deviceList_); + }); + prefs.exec(); +} + +void Qtilities::Application::onPrefsChanged() +{ +#if USE_ALSA + engine_->setNormalized(settings_.isNormalized()); + AlsaEngine* alsa = qobject_cast(engine_); + AlsaDevice* dev = qobject_cast(channel_); + if (alsa && dev) + alsa->updateDevice(dev); +#endif + mnuVolume_->loadSettings(); + + onAudioDeviceChanged(settings_.channelId()); + onAudioEngineChanged(settings_.engineId()); } void Qtilities::Application::onAudioEngineChanged(int engineId) @@ -197,8 +203,6 @@ void Qtilities::Application::onAudioEngineChanged(int engineId) disconnect(channel_, nullptr, this, nullptr); channel_ = nullptr; } - disconnect(engine_, &AudioEngine::sinkListChanged, - this, &Application::onAudioDeviceListChanged); } switch (engineId) { #if USE_ALSA @@ -219,9 +223,6 @@ void Qtilities::Application::onAudioEngineChanged(int engineId) engine_->setIgnoreMaxVolume(settings_.ignoreMaxVolume()); #endif engine_->setNormalized(settings_.isNormalized()); - - connect(engine_, &AudioEngine::sinkListChanged, - this, &Application::onAudioDeviceListChanged); } void Qtilities::Application::onAudioDeviceChanged(int deviceId) @@ -234,34 +235,28 @@ void Qtilities::Application::onAudioDeviceChanged(int deviceId) channel_ = engine_->sinks().at(deviceId); - connect(channel_, &AudioDevice::muteChanged, this, [=](bool muted) { + connect(channel_, &AudioDevice::muteChanged, this, [this](bool muted) { mnuVolume_->setMute(muted); + settings_.setMuted(muted); updateTrayIcon(); }); - connect(channel_, &AudioDevice::volumeChanged, this, [=](int volume) { - mnuVolume_->setVolume(volume); - updateTrayIcon(); - }); + connect(channel_, &AudioDevice::volumeChanged, this, &Application::onVolumeChanged); } -void Qtilities::Application::onAudioDeviceListChanged() +void Qtilities::Application::onVolumeChanged(int volume) { - if (engine_) { - QStringList list; - for (const AudioDevice* dev : engine_->sinks()) - list.append(dev->description()); + if (!channel_) + return; - dlgPrefs_->setDeviceList(list); - } + channel_->setVolume(volume); + settings_.setVolume(volume); + updateTrayIcon(); } void Qtilities::Application::onAboutToQuit() { - dlgPrefs_->saveSettings(); - settings_.setUseAutostart(actAutoStart_->isChecked()); - settings_.useAutostart() ? createAutostartFile() - : deleteAutostartFile(); + settings_.useAutostart() ? createAutostartFile() : deleteAutostartFile(); settings_.save(); } @@ -271,9 +266,7 @@ void Qtilities::Application::onTrayIconActivated(QSystemTrayIcon::ActivationReas mnuVolume_->show(); mnuVolume_->adjustSize(); mnuVolume_->popUp(); - } else if (channel_ - && settings_.muteOnMiddleClick() - && reason == QSystemTrayIcon::MiddleClick) { + } else if (channel_ && settings_.muteOnMiddleClick() && reason == QSystemTrayIcon::MiddleClick) { channel_->toggleMute(); } } @@ -281,6 +274,9 @@ void Qtilities::Application::onTrayIconActivated(QSystemTrayIcon::ActivationReas void Qtilities::Application::runMixer() { QString command = settings_.mixerCommand(); + if (command.isEmpty()) + return; + #if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)) QStringList args = QProcess::splitCommand(command); QProcess::startDetached(args.takeFirst(), args); @@ -289,6 +285,16 @@ void Qtilities::Application::runMixer() #endif } +void Qtilities::Application::updateDeviceList() +{ + if (!engine_) + return; + + deviceList_.erase(deviceList_.begin(), deviceList_.end()); + for (const AudioDevice *dev : engine_->sinks()) + deviceList_.append(dev->description()); +} + void Qtilities::Application::updateTrayIcon() { QString iconName; @@ -301,7 +307,9 @@ void Qtilities::Application::updateTrayIcon() else iconName = QLatin1String("audio-volume-high"); - trayIcon_->setIcon(QIcon::fromTheme(iconName)); + QString fallbackIconName = QStringLiteral(":/") + iconName; + + trayIcon_->setIcon(QIcon::fromTheme(iconName, QIcon(fallbackIconName))); } int main(int argc, char* argv[]) diff --git a/src/application.hpp b/src/application.hpp index 4271cb1..d210782 100644 --- a/src/application.hpp +++ b/src/application.hpp @@ -1,6 +1,6 @@ /* VolTrayke - Volume tray widget. - Copyright (C) 2021 Andrea Zanellato + Copyright (C) 2021-2023 Andrea Zanellato This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -13,7 +13,7 @@ You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., - 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. SPDX-License-Identifier: GPL-2.0-only */ @@ -22,8 +22,8 @@ #include "settings.hpp" #include -#include #include +#include class AudioDevice; class AudioEngine; @@ -32,41 +32,39 @@ class QAction; namespace Qtilities { -class DialogAbout; -class DialogPrefs; class MenuVolume; - class Application : public QApplication { Q_OBJECT public: - Application(int&, char**); - ~Application(); - - Settings& settings() { return settings_; } + Application(int argc, char *argv[]); + void about(); + void preferences(); + Settings &settings() { return settings_; } private: void initLocale(); void initUi(); void runMixer(); + void updateDeviceList(); void updateTrayIcon(); void onAboutToQuit(); - void onAudioEngineChanged(int); void onAudioDeviceChanged(int); - void onAudioDeviceListChanged(); + void onAudioEngineChanged(int); + void onPrefsChanged(); void onTrayIconActivated(QSystemTrayIcon::ActivationReason); + void onVolumeChanged(int); - QTranslator qtTranslator, translator; - QAction* actAutoStart_; - QSystemTrayIcon* trayIcon_; - DialogAbout* dlgAbout_; - DialogPrefs* dlgPrefs_; - MenuVolume* mnuVolume_; + QStringList deviceList_; + QTranslator qtTranslator_, translator_; Settings settings_; - AudioEngine* engine_; - AudioDevice* channel_; + QSystemTrayIcon *trayIcon_; + QAction *actAutoStart_; + MenuVolume *mnuVolume_; + AudioEngine *engine_; + AudioDevice *channel_; }; -} // namespace azd +} // namespace Qtilities diff --git a/src/dialogprefs.cpp b/src/dialogprefs.cpp index bd5011c..5dcffeb 100644 --- a/src/dialogprefs.cpp +++ b/src/dialogprefs.cpp @@ -1,6 +1,6 @@ /* VolTrayke - Volume tray widget. - Copyright (C) 2021 Andrea Zanellato + Copyright (C) 2021-2023 Andrea Zanellato This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -13,69 +13,34 @@ You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., - 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. SPDX-License-Identifier: GPL-2.0-only */ -#include "application.hpp" -#include "audio/engineid.hpp" - #include "dialogprefs.hpp" #include "ui_dialogprefs.h" - -#include -#include +#include "application.hpp" +#include "audio/engineid.hpp" +#include "settings.hpp" #include -Qtilities::DialogPrefs::DialogPrefs(QWidget* parent) +Qtilities::DialogPrefs::DialogPrefs(QWidget *parent) : QDialog(parent) , ui(new Qtilities::Ui::DialogPrefs) { ui->setupUi(this); + setupUi(); -#if USE_ALSA - ui->cbxEngine->addItem("ALSA"); -#endif -#if USE_PULSEAUDIO - ui->cbxEngine->addItem("PulseAudio"); -#endif - // TODO: LXQt volume plugin has these features that I don't use, - // here only as reminder if someone ever request to enable them. -#if 0 - ui->chkIgnoreMax->setEnabled(ui->cbxEngine->currentText() == "PulseAudio"); -#else - ui->tbwPrefs->removeTab(2); // Notifications - ui->chkIgnoreMax->setVisible(false); - ui->chkShowOnClick->setVisible(false); -#endif - connect(ui->cbxEngine, QOverload::of(&QComboBox::currentIndexChanged), - this, [=](int id) { - ui->chkIgnoreMax->setEnabled(id == EngineId::PulseAudio); - ui->chkNormalize->setEnabled(id == EngineId::Alsa); - }); - connect(ui->buttonBox->button(QDialogButtonBox::Ok), &QPushButton::clicked, - this, &DialogPrefs::onPrefsChanged); - - connect(ui->buttonBox->button(QDialogButtonBox::Cancel), &QPushButton::clicked, - this, &QDialog::hide); + connect(ui->buttonBox, &QDialogButtonBox::accepted, this, &DialogPrefs::accept); + connect(ui->buttonBox, &QDialogButtonBox::rejected, this, &DialogPrefs::reject); } -Qtilities::DialogPrefs::~DialogPrefs() -{ - delete ui; - qDebug() << "Destroyed DialogPrefs"; -} - -void Qtilities::DialogPrefs::closeEvent(QCloseEvent* event) -{ - hide(); - event->ignore(); -} +Qtilities::DialogPrefs::~DialogPrefs() { delete ui; } void Qtilities::DialogPrefs::loadSettings() { - Settings& settings = static_cast(qApp)->settings(); + Settings &settings = static_cast(qApp)->settings(); ui->cbxEngine->setCurrentIndex(settings.engineId()); ui->cbxChannel->setCurrentIndex(settings.channelId()); ui->chkNormalize->setChecked(settings.isNormalized()); @@ -89,9 +54,9 @@ void Qtilities::DialogPrefs::loadSettings() #endif } -void Qtilities::DialogPrefs::saveSettings() +void Qtilities::DialogPrefs::accept() { - Settings& settings = static_cast(qApp)->settings(); + Settings &settings = static_cast(qApp)->settings(); settings.setEngineId(ui->cbxEngine->currentIndex()); settings.setChannelId(ui->cbxChannel->currentIndex()); settings.setNormalized(ui->chkNormalize->isChecked()); @@ -103,6 +68,7 @@ void Qtilities::DialogPrefs::saveSettings() settings.setIgnoreMaxVolume(ui->chkIgnoreMax->isChecked()); settings.setShowOnLeftClick(ui->chkShowOnClick->isChecked()); #endif + QDialog::accept(); } void Qtilities::DialogPrefs::setDeviceList(const QStringList& list) @@ -111,23 +77,26 @@ void Qtilities::DialogPrefs::setDeviceList(const QStringList& list) ui->cbxChannel->addItems(list); } -void Qtilities::DialogPrefs::onPrefsChanged() +void Qtilities::DialogPrefs::setupUi() { - Settings& settings = static_cast(qApp)->settings(); - int previousBackendId = settings.engineId(); - int previousChannelId = settings.channelId(); - - saveSettings(); - emit sigPrefsChanged(); - - int currentBackendId = settings.engineId(); - int currentChannelId = settings.channelId(); - - if (currentBackendId != previousBackendId) - emit sigEngineChanged(currentBackendId); - - if (currentChannelId != previousChannelId) - emit sigChannelChanged(currentChannelId); - - hide(); +#if USE_ALSA + ui->cbxEngine->addItem("ALSA"); +#endif +#if USE_PULSEAUDIO + ui->cbxEngine->addItem("PulseAudio"); +#endif + // TODO: LXQt volume plugin has these features that I don't use, + // here only as reminder if someone ever request to enable them. +#if 0 + ui->chkIgnoreMax->setEnabled(ui->cbxEngine->currentText() == "PulseAudio"); +#else + ui->tbwPrefs->removeTab(2); // Notifications + ui->chkIgnoreMax->setVisible(false); + ui->chkShowOnClick->setVisible(false); +#endif + connect(ui->cbxEngine, QOverload::of(&QComboBox::currentIndexChanged), + this, [this](int id) { + ui->chkIgnoreMax->setEnabled(id == EngineId::PulseAudio); + ui->chkNormalize->setEnabled(id == EngineId::Alsa); + }); } diff --git a/src/dialogprefs.hpp b/src/dialogprefs.hpp index 937e341..fa8919d 100644 --- a/src/dialogprefs.hpp +++ b/src/dialogprefs.hpp @@ -1,6 +1,6 @@ /* VolTrayke - Volume tray widget. - Copyright (C) 2021 Andrea Zanellato + Copyright (C) 2021-2023 Andrea Zanellato This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -13,7 +13,7 @@ You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., - 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. SPDX-License-Identifier: GPL-2.0-only */ @@ -35,19 +35,11 @@ class DialogPrefs : public QDialog { ~DialogPrefs(); void loadSettings(); - void saveSettings(); void setDeviceList(const QStringList&); -signals: - void sigPrefsChanged(); - void sigEngineChanged(int); - void sigChannelChanged(int); - private: - void closeEvent(QCloseEvent*) override; - - void onAudioEngineChanged(int); - void onPrefsChanged(); + void accept() override; + void setupUi(); Qtilities::Ui::DialogPrefs *ui; }; diff --git a/src/menuvolume.cpp b/src/menuvolume.cpp index d3cab0d..53adf1d 100644 --- a/src/menuvolume.cpp +++ b/src/menuvolume.cpp @@ -1,6 +1,6 @@ /* VolTrayke - Volume tray widget. - Copyright (C) 2021 Andrea Zanellato + Copyright (C) 2021-2023 Andrea Zanellato This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -13,12 +13,14 @@ You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., - 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. SPDX-License-Identifier: GPL-2.0-only */ #include "menuvolume.hpp" +#include "application.hpp" #include "qtilities.hpp" +#include "settings.hpp" #include #include @@ -83,9 +85,15 @@ Qtilities::MenuVolume::MenuVolume(QWidget* parent) }); } -Qtilities::MenuVolume::~MenuVolume() +void Qtilities::MenuVolume::loadSettings() { - qDebug() << "Destroyed MenuVolume"; + Settings &settings = static_cast(qApp)->settings(); + sldVolume_->setPageStep(settings.pageStep()); + sldVolume_->setSingleStep(settings.singleStep()); + + int volume = settings.volume(); + if(volume >= 0 && volume <= 100) + sldVolume_->setValue(volume); } void Qtilities::MenuVolume::popUp() @@ -123,13 +131,3 @@ void Qtilities::MenuVolume::setVolume(int volume) lblVolume_->setText(QString::number(volume)); sldVolume_->blockSignals(false); } - -void Qtilities::MenuVolume::setPageStep(double step) -{ - sldVolume_->setPageStep(step); -} - -void Qtilities::MenuVolume::setSingleStep(double step) -{ - sldVolume_->setSingleStep(step); -} diff --git a/src/menuvolume.hpp b/src/menuvolume.hpp index 1afa654..2475768 100644 --- a/src/menuvolume.hpp +++ b/src/menuvolume.hpp @@ -1,6 +1,6 @@ /* VolTrayke - Volume tray widget. - Copyright (C) 2021 Andrea Zanellato + Copyright (C) 2021-2023 Andrea Zanellato This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -13,7 +13,7 @@ You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., - 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. SPDX-License-Identifier: GPL-2.0-only */ @@ -32,13 +32,11 @@ class MenuVolume : public QMenu { public: MenuVolume(QWidget* parent = nullptr); - ~MenuVolume(); + void loadSettings(); void popUp(); void setMute(bool); void setVolume(int); - void setPageStep(double); // TODO: See how to manage double units in slider - void setSingleStep(double); signals: void sigRunMixer(); @@ -46,8 +44,8 @@ class MenuVolume : public QMenu { void sigVolumeChanged(int); private: - QCheckBox* chkMute_; - QLabel* lblVolume_; - QSlider* sldVolume_; + QCheckBox *chkMute_; + QLabel *lblVolume_; + QSlider *sldVolume_; }; } // namespace Qtilities diff --git a/src/settings.cpp b/src/settings.cpp index 3bd8477..dd00c73 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -1,6 +1,6 @@ /* VolTrayke - Volume tray widget. - Copyright (C) 2021 Andrea Zanellato + Copyright (C) 2021-2023 Andrea Zanellato This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -13,7 +13,7 @@ You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., - 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. SPDX-License-Identifier: GPL-2.0-only */ @@ -24,30 +24,16 @@ #include #include -namespace Qtilities { -namespace Default { - const bool isNormalized = true; - const bool muteOnMiddleClick = true; - const bool useAutostart = false; - const double pageStep = 2.00; - const double singleStep = 1.00; -#if 0 - const bool ignoreMaxVolume = false; - const bool showAlwaysNotifications = false; - const bool showKeyboardNotifications = true; - const bool showOnLeftClick = true; -#endif -} // namespace Default -} // namespace Qtilities - Qtilities::Settings::Settings() : engineId_(-1) , channelId_(-1) , pageStep_(Default::pageStep) , singleStep_(Default::singleStep) + , isMuted_(Default::isMuted) , isNormalized_(Default::isNormalized) , muteOnMiddleClick_(Default::muteOnMiddleClick) , useAutostart_(Default::useAutostart) + , volume_(Default::volume) , mixerCommand_() #if 0 , ignoreMaxVolume_(Default::ignoreMaxVolume) @@ -58,11 +44,6 @@ Qtilities::Settings::Settings() { } -Qtilities::Settings::~Settings() -{ - qDebug() << "Destroyed Settings"; -} - void Qtilities::Settings::load() { QSettings settings(QSettings::IniFormat, QSettings::UserScope, @@ -73,12 +54,16 @@ void Qtilities::Settings::load() if (engineId < EngineId::EngineMax) engineId_ = engineId; - channelId_ = settings.value(QStringLiteral("ChannelId"), -1).toInt(); + int volume = settings.value(QStringLiteral("Volume"), Default::volume).toInt(); + if(volume >= 0 && volume <= 100) + volume_ = volume; - mixerCommand_ = settings.value(QStringLiteral("MixerCommand"), QString()).toString(); + useAutostart_ = settings.value(QStringLiteral("Autostart"), Default::useAutostart).toBool(); + channelId_ = settings.value(QStringLiteral("ChannelId"), -1).toInt(); + isMuted_ = settings.value(QStringLiteral("IsMuted"), Default::isMuted).toBool(); isNormalized_ = settings.value(QStringLiteral("IsNormalized"), Default::isNormalized).toBool(); + mixerCommand_ = settings.value(QStringLiteral("MixerCommand"), QString()).toString(); muteOnMiddleClick_ = settings.value(QStringLiteral("MuteOnMiddleClick"), Default::muteOnMiddleClick).toBool(); - useAutostart_ = settings.value(QStringLiteral("Autostart"), Default::useAutostart).toBool(); pageStep_ = settings.value(QStringLiteral("PageStep"), Default::pageStep).toDouble(); singleStep_ = settings.value(QStringLiteral("SingleStep"), Default::singleStep).toDouble(); #if 0 @@ -95,14 +80,16 @@ void Qtilities::Settings::save() QApplication::organizationName(), QApplication::applicationDisplayName()); - settings.setValue(QStringLiteral("EngineId"), engineId_); + settings.setValue(QStringLiteral("Autostart"), useAutostart_); settings.setValue(QStringLiteral("ChannelId"), channelId_); - settings.setValue(QStringLiteral("PageStep"), pageStep_); - settings.setValue(QStringLiteral("SingleStep"), singleStep_); + settings.setValue(QStringLiteral("EngineId"), engineId_); + settings.setValue(QStringLiteral("IsMuted"), isMuted_); settings.setValue(QStringLiteral("IsNormalized"), isNormalized_); - settings.setValue(QStringLiteral("MuteOnMiddleClick"), muteOnMiddleClick_); - settings.setValue(QStringLiteral("Autostart"), useAutostart_); settings.setValue(QStringLiteral("MixerCommand"), mixerCommand_); + settings.setValue(QStringLiteral("MuteOnMiddleClick"), muteOnMiddleClick_); + settings.setValue(QStringLiteral("PageStep"), pageStep_); + settings.setValue(QStringLiteral("SingleStep"), singleStep_); + settings.setValue(QStringLiteral("Volume"), volume_); #if 0 settings.setValue(QStringLiteral("IgnoreMaxVolume"), ignoreMaxVolume_); settings.setValue(QStringLiteral("ShowAlwaysNotifications"), showAlwaysNotifications_); diff --git a/src/settings.hpp b/src/settings.hpp index fddc903..7c20aaf 100644 --- a/src/settings.hpp +++ b/src/settings.hpp @@ -1,6 +1,6 @@ /* VolTrayke - Volume tray widget. - Copyright (C) 2021 Andrea Zanellato + Copyright (C) 2021-2023 Andrea Zanellato This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -13,7 +13,7 @@ You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., - 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. SPDX-License-Identifier: GPL-2.0-only */ @@ -23,10 +23,25 @@ namespace Qtilities { +namespace Default { + static constexpr bool isMuted = false; + static constexpr bool isNormalized = true; + static constexpr bool muteOnMiddleClick = true; + static constexpr bool useAutostart = false; + static constexpr double pageStep = 2.00; + static constexpr double singleStep = 1.00; + static constexpr int volume = -1; +#if 0 + const bool ignoreMaxVolume = false; + const bool showAlwaysNotifications = false; + const bool showKeyboardNotifications = true; + const bool showOnLeftClick = true; +#endif +} // namespace Default + class Settings { public: Settings(); - ~Settings(); void load(); void save(); @@ -37,6 +52,9 @@ class Settings { int channelId() const { return channelId_; } void setChannelId(int id) { channelId_ = id; } + int volume() const { return volume_; } + void setVolume(int volume) { volume_ = volume; } + double pageStep() { return pageStep_; } void setPageStep(double step) { pageStep_ = step; } @@ -46,6 +64,9 @@ class Settings { bool isNormalized() const { return isNormalized_; } void setNormalized(bool normalized) { isNormalized_ = normalized; } + bool isMuted() const { return isMuted_; } + void setMuted(bool isMuted) { isMuted_ = isMuted; } + bool muteOnMiddleClick() const { return muteOnMiddleClick_; } void setMuteOnMiddleClick(bool mute) { muteOnMiddleClick_ = mute; } @@ -71,8 +92,10 @@ class Settings { private: int engineId_; int channelId_; + int volume_; double pageStep_; double singleStep_; + bool isMuted_; bool isNormalized_; bool muteOnMiddleClick_; #if 0