From c9c8e20387d0afec242235e693dbc6b2756de4c1 Mon Sep 17 00:00:00 2001 From: Swiftb0y <12380386+Swiftb0y@users.noreply.github.com> Date: Mon, 12 Feb 2024 18:09:53 +0100 Subject: [PATCH] refactor(preferences): avoid static_casts for preferences::constants --- CMakeLists.txt | 1 + src/preferences/constants.cpp | 4 +++ src/preferences/constants.h | 10 ++++++ src/preferences/dialog/dlgprefinterface.cpp | 36 +++++++++++---------- 4 files changed, 34 insertions(+), 17 deletions(-) create mode 100644 src/preferences/constants.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index f5db15cdb67..396a547d489 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1042,6 +1042,7 @@ add_library(mixxx-lib STATIC EXCLUDE_FROM_ALL src/preferences/colorpalettesettings.cpp src/preferences/colorpalettesettings.cpp src/preferences/configobject.cpp + src/preferences/constants.cpp src/preferences/dialog/dlgprefautodj.cpp src/preferences/dialog/dlgprefautodjdlg.ui src/preferences/dialog/dlgprefbeats.cpp diff --git a/src/preferences/constants.cpp b/src/preferences/constants.cpp new file mode 100644 index 00000000000..b1a902a8160 --- /dev/null +++ b/src/preferences/constants.cpp @@ -0,0 +1,4 @@ +// just a stub so the MOC file can be included somewhere +#include "constants.h" + +#include "moc_constants.cpp" diff --git a/src/preferences/constants.h b/src/preferences/constants.h index 099d2177369..7daab422f9c 100644 --- a/src/preferences/constants.h +++ b/src/preferences/constants.h @@ -1,10 +1,17 @@ #pragma once +// required for Qt-Macros +#include + namespace mixxx { namespace preferences { namespace constants { +Q_NAMESPACE + +// In order for this Q_NAMESPACE to work, all members of the namespace must +// be declared here. see QTBUG-68611 // Don't change these constants since they are stored in user configuration // files. @@ -13,6 +20,7 @@ enum class Tooltips { On = 1, OnlyInLibrary = 2, }; +Q_ENUM_NS(Tooltips); // Settings to enable or disable the prevention to run the screensaver. enum class ScreenSaver { @@ -20,6 +28,7 @@ enum class ScreenSaver { On = 1, OnPlay = 2 }; +Q_ENUM_NS(ScreenSaver); enum class MultiSamplingMode { Disabled = 0, @@ -28,6 +37,7 @@ enum class MultiSamplingMode { Eight = 8, Sixteen = 16 }; +Q_ENUM_NS(MultiSamplingMode); } // namespace constants } // namespace preferences diff --git a/src/preferences/dialog/dlgprefinterface.cpp b/src/preferences/dialog/dlgprefinterface.cpp index dfe8bf21733..107b7b197c4 100644 --- a/src/preferences/dialog/dlgprefinterface.cpp +++ b/src/preferences/dialog/dlgprefinterface.cpp @@ -11,6 +11,7 @@ #include "control/controlproxy.h" #include "defs_urls.h" #include "moc_dlgprefinterface.cpp" +#include "preferences/constants.h" #include "preferences/usersettings.h" #include "skin/legacy/legacyskinparser.h" #include "skin/skin.h" @@ -180,14 +181,14 @@ DlgPrefInterface::DlgPrefInterface( // Screensaver mode comboBoxScreensaver->clear(); comboBoxScreensaver->addItem(tr("Allow screensaver to run"), - static_cast(constants::ScreenSaver::Off)); + QVariant::fromValue(constants::ScreenSaver::Off)); comboBoxScreensaver->addItem(tr("Prevent screensaver from running"), - static_cast(constants::ScreenSaver::On)); + QVariant::fromValue((constants::ScreenSaver::On))); comboBoxScreensaver->addItem(tr("Prevent screensaver while playing"), - static_cast(constants::ScreenSaver::OnPlay)); + QVariant::fromValue(constants::ScreenSaver::OnPlay)); - int inhibitsettings = static_cast(m_pScreensaverManager->status()); - comboBoxScreensaver->setCurrentIndex(comboBoxScreensaver->findData(inhibitsettings)); + comboBoxScreensaver->setCurrentIndex(comboBoxScreensaver->findData( + QVariant::fromValue(m_pScreensaverManager->status()))); // Multi-Sampling #ifdef MIXXX_USE_QML @@ -208,7 +209,7 @@ DlgPrefInterface::DlgPrefInterface( ConfigKey(kPreferencesGroup, kMultiSamplingKey), constants::MultiSamplingMode::Four); int multiSamplingIndex = multiSamplingComboBox->findData( - static_cast(m_multiSampling)); + QVariant::fromValue((m_multiSampling))); if (multiSamplingIndex != -1) { multiSamplingComboBox->setCurrentIndex(multiSamplingIndex); } else { @@ -325,8 +326,8 @@ void DlgPrefInterface::slotUpdate() { loadTooltipPreferenceFromConfig(); - int inhibitsettings = static_cast(m_pScreensaverManager->status()); - comboBoxScreensaver->setCurrentIndex(comboBoxScreensaver->findData(inhibitsettings)); + comboBoxScreensaver->setCurrentIndex(comboBoxScreensaver->findData( + QVariant::fromValue(m_pScreensaverManager->status()))); } void DlgPrefInterface::slotResetToDefaults() { @@ -353,11 +354,12 @@ void DlgPrefInterface::slotResetToDefaults() { // Inhibit the screensaver comboBoxScreensaver->setCurrentIndex(comboBoxScreensaver->findData( - static_cast(constants::ScreenSaver::On))); + QVariant::fromValue(constants::ScreenSaver::On))); #ifdef MIXXX_USE_QML - multiSamplingComboBox->setCurrentIndex(multiSamplingComboBox->findData( - static_cast(constants::MultiSamplingMode::Four))); // 4x MSAA + multiSamplingComboBox->setCurrentIndex( + multiSamplingComboBox->findData(QVariant::fromValue( + constants::MultiSamplingMode::Four))); // 4x MSAA #endif #ifdef Q_OS_IOS @@ -471,16 +473,16 @@ void DlgPrefInterface::slotApply() { ConfigValue(checkBoxHideMenuBar->isChecked())); emit menuBarAutoHideChanged(); - m_pConfig->set(ConfigKey(kControlsGroup, kTooltipsKey), - ConfigValue(static_cast(m_tooltipMode))); + m_pConfig->setValue(ConfigKey(kControlsGroup, kTooltipsKey), + m_tooltipMode); emit tooltipModeChanged(m_tooltipMode); // screensaver mode update - int screensaverComboBoxState = comboBoxScreensaver->currentData().toInt(); - int screensaverConfiguredState = static_cast(m_pScreensaverManager->status()); + const auto screensaverComboBoxState = + comboBoxScreensaver->currentData().value(); + const auto screensaverConfiguredState = m_pScreensaverManager->status(); if (screensaverComboBoxState != screensaverConfiguredState) { - m_pScreensaverManager->setStatus( - static_cast(screensaverComboBoxState)); + m_pScreensaverManager->setStatus(screensaverComboBoxState); } #ifdef MIXXX_USE_QML