From 2056f4c3365b6b9c3eb89d3746882983ad7b42a8 Mon Sep 17 00:00:00 2001 From: tamasmeszaros Date: Wed, 19 Sep 2018 13:43:15 +0200 Subject: [PATCH] Zipper concept clarified. --- xs/src/libslic3r/PrintExport.hpp | 61 ++++++++----------------------- xs/src/slic3r/AppController.cpp | 48 +++++++----------------- xs/src/slic3r/AppController.hpp | 17 +++++++++ xs/src/slic3r/AppControllerWx.cpp | 49 +++++++++++++++++++++++++ 4 files changed, 94 insertions(+), 81 deletions(-) diff --git a/xs/src/libslic3r/PrintExport.hpp b/xs/src/libslic3r/PrintExport.hpp index c3c59fc30..cd6d16f33 100644 --- a/xs/src/libslic3r/PrintExport.hpp +++ b/xs/src/libslic3r/PrintExport.hpp @@ -73,19 +73,21 @@ template struct VeryFalse { static const bool value = false; }; // This has to be explicitly implemented in the gui layer or a default zlib // based implementation is needed. -template class Zipper { +template class LayerWriter { public: - Zipper(const std::string& /*zipfile_path*/) { + LayerWriter(const std::string& /*zipfile_path*/) { static_assert(VeryFalse::value, - "No zipper implementation provided!"); + "No layer writer implementation provided!"); } void next_entry(const std::string& /*fname*/) {} std::string get_name() { return ""; } - template Zipper& operator<<(const T& /*arg*/) { + bool is_ok() { return false; } + + template LayerWriter& operator<<(const T& /*arg*/) { return *this; } @@ -203,62 +205,29 @@ public: inline void save(const std::string& path) { try { - Zipper zipper(path); + LayerWriter writer(path); - std::string project = zipper.get_name(); + std::string project = writer.get_name(); - zipper.next_entry(project); - zipper << createIniContent(project); + writer.next_entry("config.ini"); + writer << createIniContent(project); for(unsigned i = 0; i < layers_rst_.size(); i++) { if(layers_rst_[i].second.rdbuf()->in_avail() > 0) { char lyrnum[6]; std::sprintf(lyrnum, "%.5d", i); auto zfilename = project + lyrnum + ".png"; - zipper.next_entry(zfilename); - zipper << layers_rst_[i].second.rdbuf(); + writer.next_entry(zfilename); + writer << layers_rst_[i].second.rdbuf(); layers_rst_[i].second.str(""); } } - zipper.close(); - } catch(std::exception&) { - BOOST_LOG_TRIVIAL(error) << "Can't create zip file for layers! " - << path; + writer.close(); + } catch(std::exception& e) { + BOOST_LOG_TRIVIAL(error) << e.what(); return; } - -// wxFileName filepath(path); - -// wxFFileOutputStream zipfile(path); - -// std::string project = filepath.GetName().ToStdString(); - -// if(!zipfile.IsOk()) { -// BOOST_LOG_TRIVIAL(error) << "Can't create zip file for layers! " -// << path; -// return; -// } - -// wxZipOutputStream zipstream(zipfile); -// wxStdOutputStream pngstream(zipstream); - -// zipstream.PutNextEntry("config.ini"); -// pngstream << createIniContent(project); - -// for(unsigned i = 0; i < layers_rst_.size(); i++) { -// if(layers_rst_[i].second.rdbuf()->in_avail() > 0) { -// char lyrnum[6]; -// std::sprintf(lyrnum, "%.5d", i); -// auto zfilename = project + lyrnum + ".png"; -// zipstream.PutNextEntry(zfilename); -// pngstream << layers_rst_[i].second.rdbuf(); -// layers_rst_[i].second.str(""); -// } -// } - -// zipstream.Close(); -// zipfile.Close(); } void saveLayer(unsigned lyr, const std::string& path) { diff --git a/xs/src/slic3r/AppController.cpp b/xs/src/slic3r/AppController.cpp index 6728c6c00..b5c9702f1 100644 --- a/xs/src/slic3r/AppController.cpp +++ b/xs/src/slic3r/AppController.cpp @@ -18,9 +18,6 @@ #include #include -#include -#include -#include namespace Slic3r { @@ -290,41 +287,21 @@ void PrintController::slice() slice(pri); } -struct wxZipper {}; - -template<> class Zipper { - wxFileName m_fpath; - wxFFileOutputStream m_zipfile; - wxZipOutputStream m_zipstream; - wxStdOutputStream m_pngstream; +template<> class LayerWriter { + Zipper m_zip; public: - Zipper(const std::string& zipfile_path): - m_fpath(zipfile_path), - m_zipfile(zipfile_path), - m_zipstream(m_zipfile), - m_pngstream(m_zipstream) - { - if(!m_zipfile.IsOk()) - throw std::runtime_error(L("Cannot create zip file.")); + inline LayerWriter(const std::string& zipfile_path): m_zip(zipfile_path) {} + + inline void next_entry(const std::string& fname) { m_zip.next_entry(fname); } + + inline std::string get_name() const { return m_zip.get_name(); } + + template inline LayerWriter& operator<<(const T& arg) { + m_zip.stream() << arg; return *this; } - void next_entry(const std::string& fname) { - m_zipstream.PutNextEntry(fname); - } - - std::string get_name() { - return m_fpath.GetName().ToStdString(); - } - - template Zipper& operator<<(const T& arg) { - m_pngstream << arg; return *this; - } - - void close() { - m_zipstream.Close(); - m_zipfile.Close(); - } + inline void close() { m_zip.close(); } }; void PrintController::slice_to_png() @@ -428,7 +405,7 @@ void PrintController::slice_to_png() }); try { - print_to( *print, exd.zippath, + print_to( *print, exd.zippath, exd.width_mm, exd.height_mm, exd.width_px, exd.height_px, exd.exp_time_s, exd.exp_time_first_s); @@ -439,6 +416,7 @@ void PrintController::slice_to_png() scale_back(); if(print->canceled()) print->restart(); + print->set_status_default(); } const PrintConfig &PrintController::config() const diff --git a/xs/src/slic3r/AppController.hpp b/xs/src/slic3r/AppController.hpp index 88d1f0eca..c9ab6f15d 100644 --- a/xs/src/slic3r/AppController.hpp +++ b/xs/src/slic3r/AppController.hpp @@ -165,6 +165,23 @@ protected: ProgresIndicatorPtr global_progressind_; }; +class Zipper { + struct Impl; + std::unique_ptr m_impl; +public: + + Zipper(const std::string& zipfilepath); + ~Zipper(); + + void next_entry(const std::string& fname); + + std::string get_name() const; + + std::ostream& stream(); + + void close(); +}; + /** * @brief Implementation of the printing logic. */ diff --git a/xs/src/slic3r/AppControllerWx.cpp b/xs/src/slic3r/AppControllerWx.cpp index 4d67d5f66..7de2a424f 100644 --- a/xs/src/slic3r/AppControllerWx.cpp +++ b/xs/src/slic3r/AppControllerWx.cpp @@ -1,5 +1,9 @@ #include "AppController.hpp" +#include +#include +#include + #include #include @@ -98,6 +102,51 @@ bool AppControllerBoilerplate::report_issue( wxDEFINE_EVENT(PROGRESS_STATUS_UPDATE_EVENT, wxCommandEvent); +struct Zipper::Impl { + wxFileName m_fpath; + wxFFileOutputStream m_zipfile; + wxZipOutputStream m_zipstream; + wxStdOutputStream m_pngstream; + + Impl(const std::string& zipfile_path): + m_fpath(zipfile_path), + m_zipfile(zipfile_path), + m_zipstream(m_zipfile), + m_pngstream(m_zipstream) + { + if(!m_zipfile.IsOk()) + throw std::runtime_error(L("Cannot create zip file.")); + } +}; + +Zipper::Zipper(const std::string &zipfilepath) +{ + m_impl.reset(new Impl(zipfilepath)); +} + +Zipper::~Zipper() {} + +void Zipper::next_entry(const std::string &fname) +{ + m_impl->m_zipstream.PutNextEntry(fname); +} + +std::string Zipper::get_name() const +{ + return m_impl->m_fpath.GetName().ToStdString(); +} + +std::ostream &Zipper::stream() +{ + return m_impl->m_pngstream; +} + +void Zipper::close() +{ + m_impl->m_zipstream.Close(); + m_impl->m_zipfile.Close(); +} + namespace { /*