Implement more of Preset class.

This commit is contained in:
Joseph Lenox 2018-11-10 22:27:42 -06:00 committed by Joseph Lenox
parent 81888af2cd
commit 844071073f
2 changed files with 112 additions and 10 deletions

View File

@ -1,19 +1,106 @@
#include "Preset.hpp"
#include "Config.hpp"
#include <regex>
#include <algorithm>
#include "Dialogs/PresetEditor.hpp"
#include <boost/filesystem.hpp>
using namespace std::literals::string_literals;
using namespace boost;
namespace Slic3r { namespace GUI {
Preset::Preset(std::string load_dir, std::string filename, preset_t p) : dir(load_dir), file(filename), group(p) {
Preset::Preset(std::string load_dir, std::string filename, preset_t p) : group(p), file(filename), dir(load_dir) {
// find last .ini at the end of the filename.
std::regex ini (".ini[ ]*$");
this->name = std::regex_replace(filename, ini, "$1");
this->_dirty_config = std::make_shared<Slic3r::Config>();
this->_config = std::make_shared<Slic3r::Config>();
}
t_config_option_keys Preset::dirty_options() const {
t_config_option_keys dirty;
auto diff_config = this->_config->diff(this->_dirty_config);
std::move(diff_config.begin(), diff_config.end(), std::back_inserter(dirty));
auto extra = this->_group_overrides();
std::copy_if(extra.cbegin(), extra.cend(), std::back_inserter(dirty),
[this](const std::string x) { return !this->_config->has(x) && this->_dirty_config->has(x);});
dirty.erase(std::remove_if(dirty.begin(), dirty.end(),
[this](const std::string x) { return this->_config->has(x) && !this->_dirty_config->has(x);}),
dirty.end());
return dirty;
}
bool Preset::dirty() const {
return false;
return this->dirty_options().size() > 0;
}
Slic3r::Config Preset::dirty_config() {
if (!this->loaded()) load_config();
Slic3r::Config result { Slic3r::Config(*(this->_dirty_config)) };
return result;
}
config_ptr Preset::load_config() {
if (this->loaded()) return this->_dirty_config;
t_config_option_keys keys { this->_group_keys() };
t_config_option_keys extra_keys { this->_group_overrides() };
if (this->default_preset) {
this->_config = Slic3r::Config::new_from_defaults(keys);
} else if (this->file.size() > 0) {
filesystem::path full_path { this->dir + "/"s + this->file };
config_ptr config = Slic3r::Config::new_from_defaults(keys);
if (filesystem::exists(full_path)) {
auto external_config { Slic3r::Config::new_from_ini(full_path.string()) };
// Apply preset values on top of defaults
config = Slic3r::Config::new_from_defaults(keys);
config->apply_with_defaults(external_config, keys);
// For extra_keys don't populate defaults.
if (extra_keys.size() > 0 && !this->external){
config->apply(external_config, extra_keys);
}
this->_config = config;
}
}
this->_dirty_config->apply(this->_config);
return this->_dirty_config;
}
t_config_option_keys Preset::_group_keys() const {
switch (this->group) {
case preset_t::Print:
return PrintEditor::options();
case preset_t::Material:
return MaterialEditor::options();
case preset_t::Printer:
return PrinterEditor::options();
default:
return t_config_option_keys();
}
}
t_config_option_keys Preset::_group_overrides() const {
switch (this->group) {
case preset_t::Print:
return PrintEditor::overriding_options();
case preset_t::Material:
return MaterialEditor::overriding_options();
case preset_t::Printer:
return PrinterEditor::overriding_options();
default:
return t_config_option_keys();
}
}
}} // namespace Slic3r::GUI

View File

@ -1,9 +1,11 @@
#ifndef PRESET_HPP
#define PRESET_HPP
// Libslic3r
#include "PrintConfig.hpp"
#include "Config.hpp"
// wx
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
#include <wx/wx.h>
@ -12,6 +14,7 @@
namespace Slic3r { namespace GUI {
/// Preset types list. We assign numbers to permit static_casts and use as preset tab indices.
/// Don't skip numbers in the enumeration, we use this as an index into vectors (instead of using std::map).
enum class preset_t : uint8_t {
@ -27,8 +30,11 @@ class Preset;
using Presets = std::vector<Preset>;
class PresetEditor;
class Preset {
public:
friend class PresetEditor;
preset_t group;
std::string name {""};
@ -57,21 +63,24 @@ public:
void delete_preset();
/// Returns list of options that have been modified from the config.
t_config_option_keys dirty_options();
t_config_option_keys dirty_options() const;
/// Returns whether or not this config is different from its modified state.
bool dirty() const;
/// Loads the selected config from file and return a shared_ptr to that config
/// Loads the selected config from file and return a shared_ptr to the dirty config
config_ptr load_config();
/// Retrieve a copy of the loaded version of the configuration with any options applied.
Slic3r::Config dirty_config();
/// Pass-through to Slic3r::Config, returns whether or not a config was loaded.
bool loaded() { return !this->config.empty(); }
bool loaded() { return !this->_config->empty(); }
/// Clear the dirty config.
void dismiss_changes();
void apply_dirty(const Slic3r::Config& other) { this->dirty_config.apply(other); }
void apply_dirty(const Slic3r::Config& other) { this->_dirty_config->apply(other); }
void apply_dirty(const config_ptr& other) { this->apply_dirty(*other); }
bool operator==(const wxString& _name) const { return this->operator==(_name.ToStdString()); }
bool operator==(const std::string& _name) const { return _name.compare(this->name) == 0; }
@ -81,10 +90,14 @@ private:
bool external {false};
/// store to keep config options for this preset
Slic3r::Config config { Slic3r::Config() };
/// This is intented to be a "pristine" copy from the underlying
/// file store.
config_ptr _config { nullptr };
/// Alternative config store for a modified configuration.
Slic3r::Config dirty_config { Slic3r::Config() };
/// This is the config reference that the rest of the system gets
/// from load_config
config_ptr _dirty_config { nullptr };
/// Underlying filename for this preset config
std::string file {""};
@ -92,8 +105,10 @@ private:
/// dirname for the file.
std::string dir {""};
/// reach through to the appropriate material type
t_config_option_keys _group_class();
/// All the options owned by the corresponding editor
t_config_option_keys _group_keys() const;
/// All the override options owned by the corresponding editor
t_config_option_keys _group_overrides() const;
};