Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow controller setting integer to have a negative range #13656

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/controllers/legacycontrollersettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,23 @@
#include "controllers/legacycontrollersettingslayout.h"
#include "util/parented_ptr.h"

namespace {
template<class T>
bool valid_range(T min, T max, T step) {
// Detecting overflow
VERIFY_OR_DEBUG_ASSERT(max + step > max) {
return false;
}
// Detecting overflow
VERIFY_OR_DEBUG_ASSERT(min - step < min) {
return false;
}
acolombier marked this conversation as resolved.
Show resolved Hide resolved

return (min <= 0 && min + step <= max) ||
(max >= 0 && max - step >= min);
acolombier marked this conversation as resolved.
Show resolved Hide resolved
}
} // namespace

class QSpinBox;
class QDoubleSpinBox;

Expand Down Expand Up @@ -263,7 +280,7 @@ class LegacyControllerNumberSetting
m_defaultValue >= m_minValue && m_savedValue >= m_minValue &&
m_editedValue >= m_minValue && m_defaultValue <= m_maxValue &&
m_savedValue <= m_maxValue && m_editedValue <= m_maxValue &&
m_stepValue > 0 && m_stepValue < m_maxValue;
m_stepValue > 0 && valid_range(m_minValue, m_maxValue, m_stepValue);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may face the condition that the default "default" = 0 in case of a missing attribute is not valid. Do we want to improve that as well for the negative range?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the usecase at the de-serialisation step.

}

static AbstractLegacyControllerSetting* createFrom(const QDomElement& element) {
Expand Down
35 changes: 35 additions & 0 deletions src/test/controller_mapping_settings_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -511,3 +511,38 @@ TEST_F(LegacyControllerMappingSettingsTest, discardDuplicateSettings) {
ASSERT_EQ(settings.at(1)->value().toNumber(), 50);
ASSERT_EQ(settings.at(2)->value().toString(), "myOptionValue1");
}

TEST_F(LegacyControllerMappingSettingsTest, handleNumberWithNegativeRange) {
QDomDocument doc;
QString dom;
QTextStream(&dom)
<< QString(kValidInteger).arg("0", "-500", "0", "1");
doc.setContent(
QString("<?xml version=\"1.0\" "
"encoding=\"utf-8\"?><MixxxControllerPreset><settings>%1</"
"settings></MixxxControllerPreset>")
.arg(dom));

auto pMapping = LegacyDummyMappingFileHandler::loadDummyMapping(
doc.documentElement(), "/fake/path");

ASSERT_EQ(pMapping->getSettings().size(), 1);
ASSERT_EQ(pMapping->getSettings().at(0)->variableName(), "myInteger1");
ASSERT_EQ(pMapping->getSettings().at(0)->value().toNumber(), 0);

dom.clear();
QTextStream(&dom)
<< QString(kValidInteger).arg("20", "-100", "100", "50");
doc.setContent(
QString("<?xml version=\"1.0\" "
"encoding=\"utf-8\"?><MixxxControllerPreset><settings>%1</"
"settings></MixxxControllerPreset>")
.arg(dom));

pMapping = LegacyDummyMappingFileHandler::loadDummyMapping(
doc.documentElement(), "/fake/path");

ASSERT_EQ(pMapping->getSettings().size(), 1);
ASSERT_EQ(pMapping->getSettings().at(0)->variableName(), "myInteger1");
ASSERT_EQ(pMapping->getSettings().at(0)->value().toNumber(), 20);
}
Loading