mirror of
https://git.mirrors.martin98.com/https://github.com/slic3r/Slic3r.git
synced 2025-08-01 22:00:41 +08:00
Fixed UI_Slider so that tests pass.
Also fixed the assumptions so that the test was doing what was intended.
This commit is contained in:
parent
dfb9536ad0
commit
fef97086fb
@ -5,7 +5,7 @@ namespace Slic3r { namespace GUI {
|
|||||||
|
|
||||||
UI_Slider::UI_Slider(wxWindow* parent, Slic3r::ConfigOptionDef _opt, size_t scale) : UI_Sizer(parent, _opt), _scale(scale) {
|
UI_Slider::UI_Slider(wxWindow* parent, Slic3r::ConfigOptionDef _opt, size_t scale) : UI_Sizer(parent, _opt), _scale(scale) {
|
||||||
double default_value {0.0};
|
double default_value {0.0};
|
||||||
|
if (_opt.default_value != nullptr) { default_value = _opt.default_value->getFloat(); }
|
||||||
sizer = new wxBoxSizer(wxHORIZONTAL);
|
sizer = new wxBoxSizer(wxHORIZONTAL);
|
||||||
_slider = new wxSlider(parent, wxID_ANY,
|
_slider = new wxSlider(parent, wxID_ANY,
|
||||||
(default_value < _opt.min ? _opt.min : default_value) * this->_scale,
|
(default_value < _opt.min ? _opt.min : default_value) * this->_scale,
|
||||||
@ -15,9 +15,10 @@ UI_Slider::UI_Slider(wxWindow* parent, Slic3r::ConfigOptionDef _opt, size_t scal
|
|||||||
wxSize(_opt.width, _opt.height));
|
wxSize(_opt.width, _opt.height));
|
||||||
|
|
||||||
_textctrl = new wxTextCtrl(parent, wxID_ANY,
|
_textctrl = new wxTextCtrl(parent, wxID_ANY,
|
||||||
static_cast<double>(_slider->GetValue()) / this->_scale,
|
trim_zeroes(std::to_string(static_cast<double>(_slider->GetValue()) / this->_scale)),
|
||||||
wxDefaultPosition,
|
wxDefaultPosition,
|
||||||
wxSize(50, -1));
|
wxSize(50, -1),
|
||||||
|
wxTE_PROCESS_ENTER);
|
||||||
|
|
||||||
|
|
||||||
sizer->Add(_slider, 1, wxALIGN_CENTER_VERTICAL, 0);
|
sizer->Add(_slider, 1, wxALIGN_CENTER_VERTICAL, 0);
|
||||||
@ -92,9 +93,21 @@ void UI_Slider::_update_textctrl() {
|
|||||||
this->_textctrl->SetInsertionPointEnd();
|
this->_textctrl->SetInsertionPointEnd();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void UI_Slider::disable() {
|
||||||
|
this->_slider->Disable();
|
||||||
|
this->_textctrl->Disable();
|
||||||
|
this->_textctrl->SetEditable(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
void UI_Slider::enable() {
|
||||||
|
this->_slider->Enable();
|
||||||
|
this->_textctrl->Enable();
|
||||||
|
this->_textctrl->SetEditable(true);
|
||||||
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
void UI_Slider::set_range(T min, T max) {
|
void UI_Slider::set_range(T min, T max) {
|
||||||
this->_slider->SetRange(static_cast<int>(min * static_cast<int>(self->_scale)), static_cast<int>(max * static_cast<int>(self->_scale)));
|
this->_slider->SetRange(static_cast<int>(min * static_cast<int>(this->_scale)), static_cast<int>(max * static_cast<int>(this->_scale)));
|
||||||
}
|
}
|
||||||
|
|
||||||
} } // Namespace Slic3r::GUI
|
} } // Namespace Slic3r::GUI
|
||||||
|
@ -19,29 +19,40 @@ SCENARIO( "UI_Slider: Defaults, Min/max handling, accessors.") {
|
|||||||
wxUIActionSimulator sim;
|
wxUIActionSimulator sim;
|
||||||
wxMilliSleep(500);
|
wxMilliSleep(500);
|
||||||
auto simple_option {ConfigOptionDef()};
|
auto simple_option {ConfigOptionDef()};
|
||||||
auto* default_color {new ConfigOptionString("30")};
|
auto* default_color {new ConfigOptionFloat(30.0)};
|
||||||
simple_option.min = 0;
|
simple_option.min = 0;
|
||||||
simple_option.max = 60;
|
simple_option.max = 60;
|
||||||
|
simple_option.default_value = default_color;
|
||||||
GIVEN("A UI Slider with default scale") {
|
GIVEN("A UI Slider with default scale") {
|
||||||
auto event_count {0};
|
auto event_count {0};
|
||||||
auto test_field {Slic3r::GUI::UI_Slider(wxTheApp->GetTopWindow(), Slic3r::ConfigOptionDef())};
|
|
||||||
auto changefunc {[&event_count] (const std::string& opt_id, const double& color) { event_count++; }};
|
auto changefunc {[&event_count] (const std::string& opt_id, const double& color) { event_count++; }};
|
||||||
WHEN("Option min is 0") {
|
WHEN("Option min is 0") {
|
||||||
WHEN("default of 0 is used.") {
|
simple_option.default_value = default_color;
|
||||||
|
auto test_field {Slic3r::GUI::UI_Slider(wxTheApp->GetTopWindow(), simple_option)};
|
||||||
|
THEN("default of 0 is used.") {
|
||||||
REQUIRE(test_field.slider()->GetMin() == 0);
|
REQUIRE(test_field.slider()->GetMin() == 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
WHEN("Option max is 0") {
|
WHEN("Option max is 0") {
|
||||||
WHEN("default of 100*10(scale) is used.") {
|
simple_option.max = 0;
|
||||||
|
simple_option.default_value = default_color;
|
||||||
|
auto test_field {Slic3r::GUI::UI_Slider(wxTheApp->GetTopWindow(), simple_option)};
|
||||||
|
THEN("default of 100*10(scale) is used.") {
|
||||||
REQUIRE(test_field.slider()->GetMax() == 1000);
|
REQUIRE(test_field.slider()->GetMax() == 1000);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
WHEN("Default value is used.") {
|
WHEN("Default value is used.") {
|
||||||
|
simple_option.default_value = default_color;
|
||||||
|
auto test_field {Slic3r::GUI::UI_Slider(wxTheApp->GetTopWindow(), simple_option)};
|
||||||
THEN("Raw slider value is 30 * scale (10) = 300") {
|
THEN("Raw slider value is 30 * scale (10) = 300") {
|
||||||
REQUIRE(test_field.slider()->GetValue() == 300);
|
REQUIRE(test_field.slider()->GetValue() == 300);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
WHEN("set_scale is called with 25 for argument") {
|
WHEN("set_scale is called with 25 for argument") {
|
||||||
|
simple_option.default_value = default_color;
|
||||||
|
simple_option.max = 100;
|
||||||
|
auto test_field {Slic3r::GUI::UI_Slider(wxTheApp->GetTopWindow(), simple_option)};
|
||||||
|
test_field.set_scale(25);
|
||||||
THEN("Slider min is 0") {
|
THEN("Slider min is 0") {
|
||||||
REQUIRE(test_field.slider()->GetMin() == 0);
|
REQUIRE(test_field.slider()->GetMin() == 0);
|
||||||
}
|
}
|
||||||
@ -69,7 +80,7 @@ SCENARIO( "UI_Slider: Defaults, Min/max handling, accessors.") {
|
|||||||
GIVEN("A UI Slider with default scale") {
|
GIVEN("A UI Slider with default scale") {
|
||||||
WHEN("No default value is given from config.") {
|
WHEN("No default value is given from config.") {
|
||||||
simple_option.default_value = nullptr;
|
simple_option.default_value = nullptr;
|
||||||
auto test_field {Slic3r::GUI::UI_Slider(wxTheApp->GetTopWindow(), Slic3r::ConfigOptionDef())};
|
auto test_field {Slic3r::GUI::UI_Slider(wxTheApp->GetTopWindow(), simple_option)};
|
||||||
THEN("Initial value of slider is 0.") {
|
THEN("Initial value of slider is 0.") {
|
||||||
REQUIRE(test_field.get_int() == 0);
|
REQUIRE(test_field.get_int() == 0);
|
||||||
REQUIRE(test_field.get_double() == 0);
|
REQUIRE(test_field.get_double() == 0);
|
||||||
@ -78,6 +89,8 @@ SCENARIO( "UI_Slider: Defaults, Min/max handling, accessors.") {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
WHEN("disable() is called") {
|
WHEN("disable() is called") {
|
||||||
|
simple_option.default_value = default_color;
|
||||||
|
auto test_field {Slic3r::GUI::UI_Slider(wxTheApp->GetTopWindow(), simple_option)};
|
||||||
test_field.slider()->Enable();
|
test_field.slider()->Enable();
|
||||||
test_field.textctrl()->Enable();
|
test_field.textctrl()->Enable();
|
||||||
test_field.textctrl()->SetEditable(true);
|
test_field.textctrl()->SetEditable(true);
|
||||||
@ -92,6 +105,8 @@ SCENARIO( "UI_Slider: Defaults, Min/max handling, accessors.") {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
WHEN("enable() is called") {
|
WHEN("enable() is called") {
|
||||||
|
simple_option.default_value = default_color;
|
||||||
|
auto test_field {Slic3r::GUI::UI_Slider(wxTheApp->GetTopWindow(), simple_option)};
|
||||||
test_field.slider()->Disable();
|
test_field.slider()->Disable();
|
||||||
test_field.textctrl()->Disable();
|
test_field.textctrl()->Disable();
|
||||||
test_field.textctrl()->SetEditable(false);
|
test_field.textctrl()->SetEditable(false);
|
||||||
@ -108,18 +123,25 @@ SCENARIO( "UI_Slider: Defaults, Min/max handling, accessors.") {
|
|||||||
}
|
}
|
||||||
|
|
||||||
GIVEN("A UI Slider with scale of 1") {
|
GIVEN("A UI Slider with scale of 1") {
|
||||||
auto test_field {Slic3r::GUI::UI_Slider(wxTheApp->GetTopWindow(), Slic3r::ConfigOptionDef(), 1)};
|
|
||||||
WHEN("Option min is 0") {
|
WHEN("Option min is 0") {
|
||||||
|
simple_option.default_value = default_color;
|
||||||
|
simple_option.min = 0;
|
||||||
|
auto test_field {Slic3r::GUI::UI_Slider(wxTheApp->GetTopWindow(), simple_option, 1)};
|
||||||
WHEN("default of 0 is used.") {
|
WHEN("default of 0 is used.") {
|
||||||
REQUIRE(test_field.slider()->GetMin() == 0);
|
REQUIRE(test_field.slider()->GetMin() == 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
WHEN("Option max is 0") {
|
WHEN("Option max is 0") {
|
||||||
WHEN("default of 100 is used.") {
|
simple_option.default_value = default_color;
|
||||||
|
simple_option.max = 0;
|
||||||
|
auto test_field {Slic3r::GUI::UI_Slider(wxTheApp->GetTopWindow(), simple_option, 1)};
|
||||||
|
WHEN("default of 10 * 100 = 1000 is used.") {
|
||||||
REQUIRE(test_field.slider()->GetMax() == 100);
|
REQUIRE(test_field.slider()->GetMax() == 100);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
WHEN("Default value is used.") {
|
WHEN("Default value is used.") {
|
||||||
|
simple_option.default_value = default_color;
|
||||||
|
auto test_field {Slic3r::GUI::UI_Slider(wxTheApp->GetTopWindow(), simple_option, 1)};
|
||||||
THEN("Raw slider value is 30 * scale (1)") {
|
THEN("Raw slider value is 30 * scale (1)") {
|
||||||
REQUIRE(test_field.slider()->GetValue() == 30);
|
REQUIRE(test_field.slider()->GetValue() == 30);
|
||||||
}
|
}
|
||||||
@ -142,7 +164,7 @@ SCENARIO( "UI_Slider: Event handlers") {
|
|||||||
auto changefunc {[&event_count] (const std::string& opt_id, const double& color) { event_count++; }};
|
auto changefunc {[&event_count] (const std::string& opt_id, const double& color) { event_count++; }};
|
||||||
auto killfunc {[&event_count](const std::string& opt_id) { event_count += 1; }};
|
auto killfunc {[&event_count](const std::string& opt_id) { event_count += 1; }};
|
||||||
GIVEN("A UI Slider") {
|
GIVEN("A UI Slider") {
|
||||||
auto test_field {Slic3r::GUI::UI_Slider(wxTheApp->GetTopWindow(), Slic3r::ConfigOptionDef())};
|
auto test_field {Slic3r::GUI::UI_Slider(wxTheApp->GetTopWindow(), simple_option)};
|
||||||
test_field.on_change = changefunc;
|
test_field.on_change = changefunc;
|
||||||
test_field.on_kill_focus = killfunc;
|
test_field.on_kill_focus = killfunc;
|
||||||
WHEN("UI Slider receives a text change event (enter is pressed).") {
|
WHEN("UI Slider receives a text change event (enter is pressed).") {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user