Refactor to use wxFileName instead of boost::filesystem

This commit is contained in:
Joseph Lenox 2018-11-11 00:17:33 -06:00 committed by Joseph Lenox
parent 6e80e73a3a
commit af791ccd97
2 changed files with 75 additions and 81 deletions

View File

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

View File

@ -11,6 +11,7 @@
#include <wx/wx.h> #include <wx/wx.h>
#include <wx/string.h> #include <wx/string.h>
#include <wx/filefn.h> #include <wx/filefn.h>
#include <wx/filename.h>
#endif #endif
namespace Slic3r { namespace GUI { namespace Slic3r { namespace GUI {
@ -37,7 +38,7 @@ class Preset {
public: public:
friend class PresetEditor; friend class PresetEditor;
preset_t group; preset_t group;
std::string name {""}; wxString name {""};
bool external {false}; bool external {false};
/// Preset /// Preset
@ -46,12 +47,13 @@ public:
/// Search the compatible_printers config option list for this preset name. /// Search the compatible_printers config option list for this preset name.
/// Assume that Printer configs are compatible with other Printer configs /// Assume that Printer configs are compatible with other Printer configs
bool compatible(std::string printer_name) { return true; } bool compatible(std::string printer_name) { return true; }
bool compatible(const wxString& printer_name) { if (group == preset_t::Printer) return true; return true; }
bool compatible(const Preset& other) {return (this->group == preset_t::Printer || (compatible(other.name) && other.group == preset_t::Printer));} bool compatible(const Preset& other) {return (this->group == preset_t::Printer || (compatible(other.name) && other.group == preset_t::Printer));}
/// Format the name appropriately. /// Format the name appropriately.
wxString dropdown_name() { return (this->dirty() ? wxString(this->name) << " " << _("(modified)") : wxString(this->name)); } wxString dropdown_name() { return (this->dirty() ? this->name << " " << _("(modified)") : this->name); }
bool file_exists() const { return wxFileExists(this->name); } bool file_exists() const { return this->_file.IsFileReadable(); }
bool prompt_unsaved_changes(wxWindow* parent); bool prompt_unsaved_changes(wxWindow* parent);
@ -101,10 +103,7 @@ private:
config_ptr _dirty_config { nullptr }; config_ptr _dirty_config { nullptr };
/// Underlying filename for this preset config /// Underlying filename for this preset config
std::string file {""}; wxFileName _file {};
/// dirname for the file.
std::string dir {""};
/// All the options owned by the corresponding editor /// All the options owned by the corresponding editor
t_config_option_keys _group_keys() const; t_config_option_keys _group_keys() const;