Refactored Slic3r::Config to a composition style instead of direct inheritance of Slic3r::DynamicPrintConfig.

This commit is contained in:
Joseph Lenox 2018-07-14 21:48:22 -05:00
parent db43d27be7
commit 70cd35af31
4 changed files with 55 additions and 26 deletions

View File

@ -483,7 +483,7 @@ void Plater::arrange() {
GetFrame()->statusbar->SetStatusText(_("Nothing to arrange."));
return;
}
bool success {this->model->arrange_objects(this->config->min_object_distance(), &bb)};
bool success {this->model->arrange_objects(this->config->config().min_object_distance(), &bb)};
if (success) {
GetFrame()->statusbar->SetStatusText(_("Objects were arranged."));

View File

@ -386,7 +386,7 @@ void Plate2D::update_bed_size() {
const auto& canvas_h {canvas_size.GetHeight()};
if (canvas_w == 0) return; // Abort early if we haven't drawn canvas yet.
this->bed_polygon = Slic3r::Polygon(scale(dynamic_cast<ConfigOptionPoints*>(config->optptr("bed_shape"))->values));
this->bed_polygon = Slic3r::Polygon(scale(config->get<ConfigOptionPoints>("bed_shape").values));
const auto& polygon = bed_polygon;

View File

@ -26,7 +26,7 @@ Config::new_from_defaults(t_config_option_keys init)
for (auto& opt_key : init) {
if (print_config_def.has(opt_key)) {
const std::string value { print_config_def.get(opt_key)->default_value->serialize() };
my_config->set_deserialize(opt_key, value);
my_config->_config.set_deserialize(opt_key, value);
}
}
@ -52,7 +52,7 @@ bool
Config::validate()
{
// general validation
for (auto k : this->keys()) {
for (auto k : this->_config.keys()) {
if (print_config_def.options.count(k) == 0) continue; // skip over keys that aren't in the master list
const auto& opt {print_config_def.options.at(k)};
if (opt.cli == "" || std::regex_search(opt.cli, _match_info, _cli_pattern) == false) continue;
@ -95,24 +95,24 @@ Config::set(const t_config_option_key& opt_key, const std::string& value)
switch (def.type) {
case coInt:
{
auto* ptr {dynamic_cast<ConfigOptionInt*>(this->optptr(opt_key, true))};
auto* ptr {dynamic_cast<ConfigOptionInt*>(this->_config.optptr(opt_key, true))};
ptr->setInt(std::stoi(value));
} break;
case coInts:
{
auto* ptr {dynamic_cast<ConfigOptionInts*>(this->optptr(opt_key, true))};
auto* ptr {dynamic_cast<ConfigOptionInts*>(this->_config.optptr(opt_key, true))};
if (!ptr->deserialize(value, true) ) {
throw InvalidOptionValue(std::string(opt_key) + std::string(" set with invalid value."));
}
} break;
case coFloat:
{
auto* ptr {dynamic_cast<ConfigOptionFloat*>(this->optptr(opt_key, true))};
auto* ptr {dynamic_cast<ConfigOptionFloat*>(this->_config.optptr(opt_key, true))};
ptr->setFloat(std::stod(value));
} break;
case coFloatOrPercent:
{
auto* ptr {dynamic_cast<ConfigOptionFloatOrPercent*>(this->optptr(opt_key, true))};
auto* ptr {dynamic_cast<ConfigOptionFloatOrPercent*>(this->_config.optptr(opt_key, true))};
const size_t perc = value.find("%");
ptr->percent = (perc != std::string::npos);
if (ptr->percent)
@ -122,14 +122,14 @@ Config::set(const t_config_option_key& opt_key, const std::string& value)
} break;
case coFloats:
{
auto* ptr {dynamic_cast<ConfigOptionFloats*>(this->optptr(opt_key, true))};
auto* ptr {dynamic_cast<ConfigOptionFloats*>(this->_config.optptr(opt_key, true))};
if (!ptr->deserialize(value, true) ) {
throw InvalidOptionValue(std::string(opt_key) + std::string(" set with invalid value."));
}
} break;
case coString:
{
auto* ptr {dynamic_cast<ConfigOptionString*>(this->optptr(opt_key, true))};
auto* ptr {dynamic_cast<ConfigOptionString*>(this->_config.optptr(opt_key, true))};
if (!ptr->deserialize(value) ) {
throw InvalidOptionValue(std::string(opt_key) + std::string(" set with invalid value."));
}
@ -152,32 +152,32 @@ Config::set(const t_config_option_key& opt_key, const int value)
switch (def.type) {
case coInt:
{
auto* ptr {dynamic_cast<ConfigOptionInt*>(this->optptr(opt_key, true))};
auto* ptr {dynamic_cast<ConfigOptionInt*>(this->_config.optptr(opt_key, true))};
ptr->setInt(value);
} break;
case coInts:
{
auto* ptr {dynamic_cast<ConfigOptionInts*>(this->optptr(opt_key, true))};
auto* ptr {dynamic_cast<ConfigOptionInts*>(this->_config.optptr(opt_key, true))};
ptr->deserialize(std::to_string(value), true);
} break;
case coFloat:
{
auto* ptr {dynamic_cast<ConfigOptionFloat*>(this->optptr(opt_key, true))};
auto* ptr {dynamic_cast<ConfigOptionFloat*>(this->_config.optptr(opt_key, true))};
ptr->setFloat(value);
} break;
case coFloatOrPercent:
{
auto* ptr {dynamic_cast<ConfigOptionFloatOrPercent*>(this->optptr(opt_key, true))};
auto* ptr {dynamic_cast<ConfigOptionFloatOrPercent*>(this->_config.optptr(opt_key, true))};
ptr->setFloat(value);
} break;
case coFloats:
{
auto* ptr {dynamic_cast<ConfigOptionFloats*>(this->optptr(opt_key, true))};
auto* ptr {dynamic_cast<ConfigOptionFloats*>(this->_config.optptr(opt_key, true))};
ptr->deserialize(std::to_string(value), true);
} break;
case coString:
{
auto* ptr {dynamic_cast<ConfigOptionString*>(this->optptr(opt_key, true))};
auto* ptr {dynamic_cast<ConfigOptionString*>(this->_config.optptr(opt_key, true))};
if (!ptr->deserialize(std::to_string(value)) ) {
throw InvalidOptionValue(std::string(opt_key) + std::string(" set with invalid value."));
}
@ -199,32 +199,32 @@ Config::set(const t_config_option_key& opt_key, const double value)
switch (def.type) {
case coInt:
{
auto* ptr {dynamic_cast<ConfigOptionInt*>(this->optptr(opt_key, true))};
auto* ptr {dynamic_cast<ConfigOptionInt*>(this->_config.optptr(opt_key, true))};
ptr->setInt(std::round(value));
} break;
case coInts:
{
auto* ptr {dynamic_cast<ConfigOptionInts*>(this->optptr(opt_key, true))};
auto* ptr {dynamic_cast<ConfigOptionInts*>(this->_config.optptr(opt_key, true))};
ptr->deserialize(std::to_string(std::round(value)), true);
} break;
case coFloat:
{
auto* ptr {dynamic_cast<ConfigOptionFloat*>(this->optptr(opt_key, true))};
auto* ptr {dynamic_cast<ConfigOptionFloat*>(this->_config.optptr(opt_key, true))};
ptr->setFloat(value);
} break;
case coFloatOrPercent:
{
auto* ptr {dynamic_cast<ConfigOptionFloatOrPercent*>(this->optptr(opt_key, true))};
auto* ptr {dynamic_cast<ConfigOptionFloatOrPercent*>(this->_config.optptr(opt_key, true))};
ptr->setFloat(value);
} break;
case coFloats:
{
auto* ptr {dynamic_cast<ConfigOptionFloats*>(this->optptr(opt_key, true))};
auto* ptr {dynamic_cast<ConfigOptionFloats*>(this->_config.optptr(opt_key, true))};
ptr->deserialize(std::to_string(value), true);
} break;
case coString:
{
auto* ptr {dynamic_cast<ConfigOptionString*>(this->optptr(opt_key, true))};
auto* ptr {dynamic_cast<ConfigOptionString*>(this->_config.optptr(opt_key, true))};
if (!ptr->deserialize(std::to_string(value)) ) {
throw InvalidOptionValue(std::string(opt_key) + std::string(" set with invalid value."));
}
@ -283,7 +283,7 @@ is_valid_float(const std::string& type, const ConfigOptionDef& opt, const std::s
return result;
}
Config::Config() : _config(DynamicPrintConfig()) {};
} // namespace Slic3r

View File

@ -34,7 +34,7 @@ public:
class Config;
using config_ptr = std::shared_ptr<Config>;
class Config : public DynamicPrintConfig {
class Config {
public:
/// Factory method to construct a Config with all default values loaded.
@ -63,14 +63,29 @@ public:
template <class T>
T& get(const t_config_option_key& opt_key, bool create=false) {
if (print_config_def.options.count(opt_key) == 0) throw InvalidOptionType(opt_key + std::string(" is an invalid option."));
return *(dynamic_cast<T*>(this->optptr(opt_key, create)));
return *(dynamic_cast<T*>(this->_config.optptr(opt_key, create)));
}
double getFloat(const t_config_option_key& opt_key, bool create=false) {
if (print_config_def.options.count(opt_key) == 0) throw InvalidOptionType(opt_key + std::string(" is an invalid option."));
return (dynamic_cast<ConfigOption*>(this->_config.optptr(opt_key, create)))->getFloat();
}
int getInt(const t_config_option_key& opt_key, bool create=false) {
if (print_config_def.options.count(opt_key) == 0) throw InvalidOptionType(opt_key + std::string(" is an invalid option."));
return (dynamic_cast<ConfigOption*>(this->_config.optptr(opt_key, create)))->getInt();
}
std::string getString(const t_config_option_key& opt_key, bool create=false) {
if (print_config_def.options.count(opt_key) == 0) throw InvalidOptionType(opt_key + std::string(" is an invalid option."));
return (dynamic_cast<ConfigOption*>(this->_config.optptr(opt_key, create)))->getString();
}
/// Template function to dynamic cast and leave it in pointer form.
template <class T>
T* get_ptr(const t_config_option_key& opt_key, bool create=false) {
if (print_config_def.options.count(opt_key) == 0) throw InvalidOptionType(opt_key + std::string(" is an invalid option."));
return dynamic_cast<T*>(this->optptr(opt_key, create));
return dynamic_cast<T*>(this->_config.optptr(opt_key, create));
}
/// Function to parse value from a string to whatever opt_key is.
@ -90,9 +105,23 @@ public:
/// It will throw InvalidConfigOption exceptions on failure.
bool validate();
const DynamicPrintConfig& config() const { return _config; }
bool empty() const { return _config.empty(); }
/// Pass-through of apply()
void apply(const config_ptr& other) { _config.apply(other->config()); }
void apply(const Slic3r::Config& other) { _config.apply(other.config()); }
Config();
private:
std::regex _cli_pattern {"=(.+)$"};
std::smatch _match_info {};
/// Underlying configuration store.
DynamicPrintConfig _config {};
};
bool is_valid_int(const std::string& type, const ConfigOptionDef& opt, const std::string& ser_value);