Load presets into the new C++ class.

This commit is contained in:
bubnikv 2017-10-02 17:35:00 +02:00
parent b1e3b0cdf9
commit 84d4bf8fdb
6 changed files with 57 additions and 3 deletions

View File

@ -163,6 +163,7 @@ sub thread_cleanup {
*Slic3r::Surface::Collection::DESTROY = sub {}; *Slic3r::Surface::Collection::DESTROY = sub {};
*Slic3r::Print::SupportMaterial2::DESTROY = sub {}; *Slic3r::Print::SupportMaterial2::DESTROY = sub {};
*Slic3r::TriangleMesh::DESTROY = sub {}; *Slic3r::TriangleMesh::DESTROY = sub {};
*Slic3r::GUI::PresetBundle::DESTROY = sub {};
return undef; # this prevents a "Scalars leaked" warning return undef; # this prevents a "Scalars leaked" warning
} }

View File

@ -172,6 +172,7 @@ sub OnInit {
} }
} }
$self->{mainframe}->config_wizard if $run_wizard; $self->{mainframe}->config_wizard if $run_wizard;
eval { $self->{preset_bundle}->load_presets($datadir) };
# $self->check_version # $self->check_version
# if $self->have_version_check # if $self->have_version_check

View File

@ -188,7 +188,8 @@ void ConfigBase::apply(const ConfigBase &other, const t_config_option_keys &keys
continue; continue;
} }
// not the most efficient way, but easier than casting pointers to subclasses // not the most efficient way, but easier than casting pointers to subclasses
if (! my_opt->deserialize(other.option(key)->serialize())) const ConfigOption *other_opt = other.option(key);
if (other_opt != nullptr && ! my_opt->deserialize(other_opt->serialize()))
CONFESS((std::string("Unexpected failure when deserializing serialized value for ") + key).c_str()); CONFESS((std::string("Unexpected failure when deserializing serialized value for ") + key).c_str());
} }
} }

View File

@ -1,11 +1,20 @@
#include "Preset.hpp" #include "Preset.hpp"
#include <fstream> #include <fstream>
#include <boost/filesystem.hpp>
#include <boost/algorithm/string/predicate.hpp> #include <boost/algorithm/string/predicate.hpp>
#include <wx/image.h> #include <wx/image.h>
#include <wx/bmpcbox.h> #include <wx/bmpcbox.h>
#if 0
#define DEBUG
#define _DEBUG
#undef NDEBUG
#endif
#include <assert.h>
namespace Slic3r { namespace Slic3r {
// Load a config file, return a C++ class Slic3r::DynamicPrintConfig with $keys initialized from the config file. // Load a config file, return a C++ class Slic3r::DynamicPrintConfig with $keys initialized from the config file.
@ -67,6 +76,28 @@ PresetCollection::PresetCollection(Preset::Type type, const std::vector<std::str
m_presets.front().load(keys); m_presets.front().load(keys);
} }
// Load all presets found in dir_path.
// Throws an exception on error.
void PresetCollection::load_presets(const std::string &dir_path, const std::string &subdir)
{
m_presets.erase(m_presets.begin()+1, m_presets.end());
t_config_option_keys keys = this->default_preset().config.keys();
for (auto &file : boost::filesystem::directory_iterator(boost::filesystem::canonical(boost::filesystem::path(dir_path) / subdir).make_preferred()))
if (boost::filesystem::is_regular_file(file.status()) && boost::algorithm::iends_with(file.path().filename().string(), ".ini")) {
std::string name = file.path().filename().string();
// Remove the .ini suffix.
name.erase(name.size() - 4);
try {
Preset preset(m_type, name, false);
preset.file = file.path().string();
preset.load(keys);
m_presets.emplace_back(preset);
} catch (const boost::filesystem::filesystem_error &err) {
}
}
}
void PresetCollection::set_default_suppressed(bool default_suppressed) void PresetCollection::set_default_suppressed(bool default_suppressed)
{ {
if (m_default_suppressed != default_suppressed) { if (m_default_suppressed != default_suppressed) {
@ -162,12 +193,23 @@ PresetBundle::PresetBundle() :
PresetBundle::~PresetBundle() PresetBundle::~PresetBundle()
{ {
assert(m_bitmapCompatible != nullptr);
assert(m_bitmapIncompatible != nullptr);
delete m_bitmapCompatible; delete m_bitmapCompatible;
m_bitmapCompatible = nullptr;
delete m_bitmapIncompatible; delete m_bitmapIncompatible;
m_bitmapIncompatible = nullptr;
for (std::pair<const std::string, wxBitmap*> &bitmap : m_mapColorToBitmap) for (std::pair<const std::string, wxBitmap*> &bitmap : m_mapColorToBitmap)
delete bitmap.second; delete bitmap.second;
} }
void PresetBundle::load_presets(const std::string &dir_path)
{
this->prints.load_presets(dir_path, "print");
this->prints.load_presets(dir_path, "filament");
this->prints.load_presets(dir_path, "printer");
}
bool PresetBundle::load_bitmaps(const std::string &path_bitmap_compatible, const std::string &path_bitmap_incompatible) bool PresetBundle::load_bitmaps(const std::string &path_bitmap_compatible, const std::string &path_bitmap_incompatible)
{ {
bool loaded_compatible = m_bitmapCompatible ->LoadFile(wxString::FromUTF8(path_bitmap_compatible.c_str())); bool loaded_compatible = m_bitmapCompatible ->LoadFile(wxString::FromUTF8(path_bitmap_compatible.c_str()));

View File

@ -66,6 +66,9 @@ public:
// Initialize the PresetCollection with the "- default -" preset. // Initialize the PresetCollection with the "- default -" preset.
PresetCollection(Preset::Type type, const std::vector<std::string> &keys); PresetCollection(Preset::Type type, const std::vector<std::string> &keys);
// Load ini files of the particular type from the provided directory path.
void load_presets(const std::string &dir_path, const std::string &subdir);
// Compatible & incompatible marks, to be placed at the wxBitmapComboBox items. // Compatible & incompatible marks, to be placed at the wxBitmapComboBox items.
void set_bitmap_compatible (const wxBitmap *bmp) { m_bitmap_compatible = bmp; } void set_bitmap_compatible (const wxBitmap *bmp) { m_bitmap_compatible = bmp; }
void set_bitmap_incompatible(const wxBitmap *bmp) { m_bitmap_incompatible = bmp; } void set_bitmap_incompatible(const wxBitmap *bmp) { m_bitmap_incompatible = bmp; }
@ -122,6 +125,9 @@ public:
bool load_bitmaps(const std::string &path_bitmap_compatible, const std::string &path_bitmap_incompatible); bool load_bitmaps(const std::string &path_bitmap_compatible, const std::string &path_bitmap_incompatible);
// Load ini files of all types (print, filament, printer) from the provided directory path.
void load_presets(const std::string &dir_path);
PresetCollection prints; PresetCollection prints;
PresetCollection filaments; PresetCollection filaments;
PresetCollection printers; PresetCollection printers;

View File

@ -51,6 +51,9 @@ PresetCollection::arrayref()
PresetBundle(); PresetBundle();
~PresetBundle(); ~PresetBundle();
void load_bitmaps(std::string path_bitmap_compatible, std::string path_bitmap_incompatible);
void load_presets(std::string dir_path);
Ref<PresetCollection> prints() %code%{ RETVAL = &THIS->prints; %}; Ref<PresetCollection> prints() %code%{ RETVAL = &THIS->prints; %};
Ref<PresetCollection> filaments() %code%{ RETVAL = &THIS->filaments; %}; Ref<PresetCollection> filaments() %code%{ RETVAL = &THIS->filaments; %};
Ref<PresetCollection> printers() %code%{ RETVAL = &THIS->printers; %}; Ref<PresetCollection> printers() %code%{ RETVAL = &THIS->printers; %};