mirror of
https://git.mirrors.martin98.com/https://github.com/slic3r/Slic3r.git
synced 2025-07-13 18:31:48 +08:00
Refactoring in PlaceholderParser
This commit is contained in:
parent
a16dda0885
commit
bf541a1fed
@ -26,8 +26,14 @@ class ConfigOption {
|
|||||||
virtual void setInt(int val) {};
|
virtual void setInt(int val) {};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class ConfigOptionVectorBase : public ConfigOption {
|
||||||
|
public:
|
||||||
|
virtual ~ConfigOptionVectorBase() {};
|
||||||
|
virtual std::vector<std::string> vserialize() const = 0;
|
||||||
|
};
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
class ConfigOptionVector
|
class ConfigOptionVector : public ConfigOptionVectorBase
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
virtual ~ConfigOptionVector() {};
|
virtual ~ConfigOptionVector() {};
|
||||||
@ -63,7 +69,7 @@ class ConfigOptionFloat : public ConfigOption
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
class ConfigOptionFloats : public ConfigOption, public ConfigOptionVector<double>
|
class ConfigOptionFloats : public ConfigOptionVector<double>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
@ -76,6 +82,16 @@ class ConfigOptionFloats : public ConfigOption, public ConfigOptionVector<double
|
|||||||
return ss.str();
|
return ss.str();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
std::vector<std::string> vserialize() const {
|
||||||
|
std::vector<std::string> vv;
|
||||||
|
for (std::vector<double>::const_iterator it = this->values.begin(); it != this->values.end(); ++it) {
|
||||||
|
std::ostringstream ss;
|
||||||
|
ss << *it;
|
||||||
|
vv.push_back(ss.str());
|
||||||
|
}
|
||||||
|
return vv;
|
||||||
|
};
|
||||||
|
|
||||||
bool deserialize(std::string str) {
|
bool deserialize(std::string str) {
|
||||||
this->values.clear();
|
this->values.clear();
|
||||||
std::istringstream is(str);
|
std::istringstream is(str);
|
||||||
@ -112,7 +128,7 @@ class ConfigOptionInt : public ConfigOption
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
class ConfigOptionInts : public ConfigOption, public ConfigOptionVector<int>
|
class ConfigOptionInts : public ConfigOptionVector<int>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
@ -125,6 +141,16 @@ class ConfigOptionInts : public ConfigOption, public ConfigOptionVector<int>
|
|||||||
return ss.str();
|
return ss.str();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
std::vector<std::string> vserialize() const {
|
||||||
|
std::vector<std::string> vv;
|
||||||
|
for (std::vector<int>::const_iterator it = this->values.begin(); it != this->values.end(); ++it) {
|
||||||
|
std::ostringstream ss;
|
||||||
|
ss << *it;
|
||||||
|
vv.push_back(ss.str());
|
||||||
|
}
|
||||||
|
return vv;
|
||||||
|
};
|
||||||
|
|
||||||
bool deserialize(std::string str) {
|
bool deserialize(std::string str) {
|
||||||
this->values.clear();
|
this->values.clear();
|
||||||
std::istringstream is(str);
|
std::istringstream is(str);
|
||||||
@ -174,7 +200,7 @@ class ConfigOptionString : public ConfigOption
|
|||||||
};
|
};
|
||||||
|
|
||||||
// semicolon-separated strings
|
// semicolon-separated strings
|
||||||
class ConfigOptionStrings : public ConfigOption, public ConfigOptionVector<std::string>
|
class ConfigOptionStrings : public ConfigOptionVector<std::string>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
@ -187,6 +213,10 @@ class ConfigOptionStrings : public ConfigOption, public ConfigOptionVector<std::
|
|||||||
return ss.str();
|
return ss.str();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
std::vector<std::string> vserialize() const {
|
||||||
|
return this->values;
|
||||||
|
};
|
||||||
|
|
||||||
bool deserialize(std::string str) {
|
bool deserialize(std::string str) {
|
||||||
this->values.clear();
|
this->values.clear();
|
||||||
std::istringstream is(str);
|
std::istringstream is(str);
|
||||||
@ -279,7 +309,7 @@ class ConfigOptionPoint : public ConfigOption
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
class ConfigOptionPoints : public ConfigOption, public ConfigOptionVector<Pointf>
|
class ConfigOptionPoints : public ConfigOptionVector<Pointf>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
@ -294,6 +324,16 @@ class ConfigOptionPoints : public ConfigOption, public ConfigOptionVector<Pointf
|
|||||||
return ss.str();
|
return ss.str();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
std::vector<std::string> vserialize() const {
|
||||||
|
std::vector<std::string> vv;
|
||||||
|
for (Pointfs::const_iterator it = this->values.begin(); it != this->values.end(); ++it) {
|
||||||
|
std::ostringstream ss;
|
||||||
|
ss << *it;
|
||||||
|
vv.push_back(ss.str());
|
||||||
|
}
|
||||||
|
return vv;
|
||||||
|
};
|
||||||
|
|
||||||
bool deserialize(std::string str) {
|
bool deserialize(std::string str) {
|
||||||
this->values.clear();
|
this->values.clear();
|
||||||
std::istringstream is(str);
|
std::istringstream is(str);
|
||||||
@ -332,7 +372,7 @@ class ConfigOptionBool : public ConfigOption
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
class ConfigOptionBools : public ConfigOption, public ConfigOptionVector<bool>
|
class ConfigOptionBools : public ConfigOptionVector<bool>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
@ -345,6 +385,16 @@ class ConfigOptionBools : public ConfigOption, public ConfigOptionVector<bool>
|
|||||||
return ss.str();
|
return ss.str();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
std::vector<std::string> vserialize() const {
|
||||||
|
std::vector<std::string> vv;
|
||||||
|
for (std::vector<bool>::const_iterator it = this->values.begin(); it != this->values.end(); ++it) {
|
||||||
|
std::ostringstream ss;
|
||||||
|
ss << (*it ? "1" : "0");
|
||||||
|
vv.push_back(ss.str());
|
||||||
|
}
|
||||||
|
return vv;
|
||||||
|
};
|
||||||
|
|
||||||
bool deserialize(std::string str) {
|
bool deserialize(std::string str) {
|
||||||
this->values.clear();
|
this->values.clear();
|
||||||
std::istringstream is(str);
|
std::istringstream is(str);
|
||||||
|
@ -29,22 +29,14 @@ PlaceholderParser::update_timestamp()
|
|||||||
ss << std::setw(2) << std::setfill('0') << timeinfo->tm_hour;
|
ss << std::setw(2) << std::setfill('0') << timeinfo->tm_hour;
|
||||||
ss << std::setw(2) << std::setfill('0') << timeinfo->tm_min;
|
ss << std::setw(2) << std::setfill('0') << timeinfo->tm_min;
|
||||||
ss << std::setw(2) << std::setfill('0') << timeinfo->tm_sec;
|
ss << std::setw(2) << std::setfill('0') << timeinfo->tm_sec;
|
||||||
this->_single["timestamp"] = ss.str();
|
this->set("timestamp", ss.str());
|
||||||
}
|
}
|
||||||
this->_single["year"] = this->_int_to_string(1900 + timeinfo->tm_year);
|
this->set("year", 1900 + timeinfo->tm_year);
|
||||||
this->_single["month"] = this->_int_to_string(1 + timeinfo->tm_mon);
|
this->set("month", 1 + timeinfo->tm_mon);
|
||||||
this->_single["day"] = this->_int_to_string(timeinfo->tm_mday);
|
this->set("day", timeinfo->tm_mday);
|
||||||
this->_single["hour"] = this->_int_to_string(timeinfo->tm_hour);
|
this->set("hour", timeinfo->tm_hour);
|
||||||
this->_single["minute"] = this->_int_to_string(timeinfo->tm_min);
|
this->set("minute", timeinfo->tm_min);
|
||||||
this->_single["second"] = this->_int_to_string(timeinfo->tm_sec);
|
this->set("second", timeinfo->tm_sec);
|
||||||
}
|
|
||||||
|
|
||||||
std::string
|
|
||||||
PlaceholderParser::_int_to_string(int value) const
|
|
||||||
{
|
|
||||||
std::ostringstream ss;
|
|
||||||
ss << value;
|
|
||||||
return ss.str();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void PlaceholderParser::apply_config(DynamicPrintConfig &config)
|
void PlaceholderParser::apply_config(DynamicPrintConfig &config)
|
||||||
@ -66,53 +58,20 @@ void PlaceholderParser::apply_config(DynamicPrintConfig &config)
|
|||||||
i != opt_keys.end(); ++i)
|
i != opt_keys.end(); ++i)
|
||||||
{
|
{
|
||||||
const t_config_option_key &key = *i;
|
const t_config_option_key &key = *i;
|
||||||
|
const ConfigOption* opt = config.option(key);
|
||||||
|
|
||||||
// set placeholders for options with multiple values
|
if (const ConfigOptionVectorBase* optv = dynamic_cast<const ConfigOptionVectorBase*>(opt)) {
|
||||||
const ConfigOptionDef &def = (*config.def)[key];
|
// set placeholders for options with multiple values
|
||||||
switch (def.type) {
|
this->set(key, optv->vserialize());
|
||||||
case coFloats:
|
} else if (const ConfigOptionPoint* optp = dynamic_cast<const ConfigOptionPoint*>(opt)) {
|
||||||
this->set_multiple_from_vector(key,
|
this->_single[key] = optp->serialize();
|
||||||
*(ConfigOptionFloats*)config.option(key));
|
|
||||||
break;
|
|
||||||
|
|
||||||
case coInts:
|
Pointf val = *optp;
|
||||||
this->set_multiple_from_vector(key,
|
this->_multiple[key + "_X"] = val.x;
|
||||||
*(ConfigOptionInts*)config.option(key));
|
this->_multiple[key + "_Y"] = val.y;
|
||||||
break;
|
} else {
|
||||||
|
|
||||||
case coStrings:
|
|
||||||
this->set_multiple_from_vector(key,
|
|
||||||
*(ConfigOptionStrings*)config.option(key));
|
|
||||||
break;
|
|
||||||
|
|
||||||
case coPoints:
|
|
||||||
this->set_multiple_from_vector(key,
|
|
||||||
*(ConfigOptionPoints*)config.option(key));
|
|
||||||
break;
|
|
||||||
|
|
||||||
case coBools:
|
|
||||||
this->set_multiple_from_vector(key,
|
|
||||||
*(ConfigOptionBools*)config.option(key));
|
|
||||||
break;
|
|
||||||
|
|
||||||
case coPoint:
|
|
||||||
{
|
|
||||||
const ConfigOptionPoint &opt =
|
|
||||||
*(ConfigOptionPoint*)config.option(key);
|
|
||||||
|
|
||||||
this->_single[key] = opt.serialize();
|
|
||||||
|
|
||||||
Pointf val = opt;
|
|
||||||
this->_multiple[key + "_X"] = val.x;
|
|
||||||
this->_multiple[key + "_Y"] = val.y;
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
// set single-value placeholders
|
// set single-value placeholders
|
||||||
this->_single[key] = config.serialize(key);
|
this->_single[key] = opt->serialize();
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -121,34 +80,30 @@ void
|
|||||||
PlaceholderParser::set(const std::string &key, const std::string &value)
|
PlaceholderParser::set(const std::string &key, const std::string &value)
|
||||||
{
|
{
|
||||||
this->_single[key] = value;
|
this->_single[key] = value;
|
||||||
|
this->_multiple.erase(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::ostream& operator<<(std::ostream &stm, const Pointf &pointf)
|
void
|
||||||
|
PlaceholderParser::set(const std::string &key, int value)
|
||||||
{
|
{
|
||||||
return stm << pointf.x << "," << pointf.y;
|
std::ostringstream ss;
|
||||||
|
ss << value;
|
||||||
|
this->set(key, ss.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class T>
|
void
|
||||||
void PlaceholderParser::set_multiple_from_vector(const std::string &key,
|
PlaceholderParser::set(const std::string &key, const std::vector<std::string> &values)
|
||||||
ConfigOptionVector<T> &opt)
|
|
||||||
{
|
{
|
||||||
const std::vector<T> &vals = opt.values;
|
for (std::vector<std::string>::const_iterator v = values.begin(); v != values.end(); ++v) {
|
||||||
|
std::stringstream ss;
|
||||||
|
ss << key << "_" << (v - values.begin());
|
||||||
|
|
||||||
for (size_t i = 0; i < vals.size(); ++i) {
|
this->_multiple[ ss.str() ] = *v;
|
||||||
std::stringstream multikey_stm;
|
if (v == values.begin()) {
|
||||||
multikey_stm << key << "_" << i;
|
this->_multiple[key] = *v;
|
||||||
|
}
|
||||||
std::stringstream val_stm;
|
|
||||||
val_stm << vals[i];
|
|
||||||
|
|
||||||
this->_multiple[multikey_stm.str()] = val_stm.str();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (vals.size() > 0) {
|
|
||||||
std::stringstream val_stm;
|
|
||||||
val_stm << vals[0];
|
|
||||||
this->_multiple[key] = val_stm.str();
|
|
||||||
}
|
}
|
||||||
|
this->_single.erase(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef SLIC3RXS
|
#ifdef SLIC3RXS
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
#include <myinit.h>
|
#include <myinit.h>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
#include "PrintConfig.hpp"
|
#include "PrintConfig.hpp"
|
||||||
|
|
||||||
|
|
||||||
@ -20,12 +21,8 @@ class PlaceholderParser
|
|||||||
void update_timestamp();
|
void update_timestamp();
|
||||||
void apply_config(DynamicPrintConfig &config);
|
void apply_config(DynamicPrintConfig &config);
|
||||||
void set(const std::string &key, const std::string &value);
|
void set(const std::string &key, const std::string &value);
|
||||||
|
void set(const std::string &key, int value);
|
||||||
private:
|
void set(const std::string &key, const std::vector<std::string> &values);
|
||||||
template<class T>
|
|
||||||
void set_multiple_from_vector(
|
|
||||||
const std::string &key, ConfigOptionVector<T> &opt);
|
|
||||||
std::string _int_to_string(int value) const;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,6 @@
|
|||||||
#include "MultiPoint.hpp"
|
#include "MultiPoint.hpp"
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <sstream>
|
|
||||||
|
|
||||||
namespace Slic3r {
|
namespace Slic3r {
|
||||||
|
|
||||||
@ -340,6 +339,12 @@ REGISTER_CLASS(Point3, "Point3");
|
|||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
std::ostream&
|
||||||
|
operator<<(std::ostream &stm, const Pointf &pointf)
|
||||||
|
{
|
||||||
|
return stm << pointf.x << "," << pointf.y;
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
Pointf::scale(double factor)
|
Pointf::scale(double factor)
|
||||||
{
|
{
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
namespace Slic3r {
|
namespace Slic3r {
|
||||||
|
|
||||||
@ -77,6 +78,8 @@ class Point3 : public Point
|
|||||||
explicit Point3(coord_t _x = 0, coord_t _y = 0, coord_t _z = 0): Point(_x, _y), z(_z) {};
|
explicit Point3(coord_t _x = 0, coord_t _y = 0, coord_t _z = 0): Point(_x, _y), z(_z) {};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
std::ostream& operator<<(std::ostream &stm, const Pointf &pointf);
|
||||||
|
|
||||||
class Pointf
|
class Pointf
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user