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 <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) : 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");
Preset::Preset(std::string load_dir, std::string filename, preset_t p) : group(p), _file(wxFileName(load_dir, filename)) {
this->name = this->_file.GetName();
this->_dirty_config = std::make_shared<Slic3r::Config>();
this->_config = std::make_shared<Slic3r::Config>();
}
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;
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 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);});
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());
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;
}
return dirty;
}
bool Preset::dirty() const {
return this->dirty_options().size() > 0;
}
bool Preset::dirty() const {
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;
}
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;
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() };
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);
if (this->default_preset) {
this->_config = Slic3r::Config::new_from_defaults(keys);
} else if (this->_file.HasName() > 0) {
config_ptr config = Slic3r::Config::new_from_defaults(keys);
if (this->file_exists()) {
auto external_config { Slic3r::Config::new_from_ini(this->_file.GetFullPath().ToStdString()) };
// 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;
// For extra_keys don't populate defaults.
if (extra_keys.size() > 0 && !this->external){
config->apply(external_config, extra_keys);
}
}
this->_dirty_config->apply(this->_config);
return this->_dirty_config;
this->_config = 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();
}
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();
}
}
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

@ -11,6 +11,7 @@
#include <wx/wx.h>
#include <wx/string.h>
#include <wx/filefn.h>
#include <wx/filename.h>
#endif
namespace Slic3r { namespace GUI {
@ -37,7 +38,7 @@ class Preset {
public:
friend class PresetEditor;
preset_t group;
std::string name {""};
wxString name {""};
bool external {false};
/// Preset
@ -46,12 +47,13 @@ public:
/// Search the compatible_printers config option list for this preset name.
/// Assume that Printer configs are compatible with other Printer configs
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));}
/// 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);
@ -101,10 +103,7 @@ private:
config_ptr _dirty_config { nullptr };
/// Underlying filename for this preset config
std::string file {""};
/// dirname for the file.
std::string dir {""};
wxFileName _file {};
/// All the options owned by the corresponding editor
t_config_option_keys _group_keys() const;