From 6737506b14e669f45e746466af91afb195971659 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tam=C3=A1s=20M=C3=A9sz=C3=A1ros?= Date: Mon, 25 Jun 2018 12:40:01 +0200 Subject: [PATCH] Separating wx and the app controller. --- xs/CMakeLists.txt | 1 + xs/src/slic3r/AppController.cpp | 203 ++------------------------- xs/src/slic3r/AppController.hpp | 28 ++-- xs/src/slic3r/appcontrollerwx.cpp | 220 ++++++++++++++++++++++++++++++ 4 files changed, 247 insertions(+), 205 deletions(-) create mode 100644 xs/src/slic3r/appcontrollerwx.cpp diff --git a/xs/CMakeLists.txt b/xs/CMakeLists.txt index 6dff3a469..171fe4836 100644 --- a/xs/CMakeLists.txt +++ b/xs/CMakeLists.txt @@ -247,6 +247,7 @@ add_library(libslic3r_gui STATIC ${LIBDIR}/slic3r/Utils/Time.hpp ${LIBDIR}/slic3r/AppController.hpp ${LIBDIR}/slic3r/AppController.cpp + ${LIBDIR}/slic3r/AppControllerWx.cpp ) add_library(admesh STATIC diff --git a/xs/src/slic3r/AppController.cpp b/xs/src/slic3r/AppController.cpp index c64b21d15..02378f138 100644 --- a/xs/src/slic3r/AppController.cpp +++ b/xs/src/slic3r/AppController.cpp @@ -5,24 +5,20 @@ #include #include -#include +//#include #include -#include #include #include #include #include -#include -#include -#include -#include -#include -#include - namespace Slic3r { +namespace GUI { +PresetBundle* get_preset_bundle(); +} + static const PrintObjectStep STEP_SLICE = posSlice; static const PrintObjectStep STEP_PERIMETERS = posPerimeters; static const PrintObjectStep STEP_PREPARE_INFILL = posPrepareInfill; @@ -32,114 +28,6 @@ static const PrintStep STEP_SKIRT = psSkirt; static const PrintStep STEP_BRIM = psBrim; static const PrintStep STEP_WIPE_TOWER = psWipeTower; -AppControllerBoilerplate::PathList -AppControllerBoilerplate::query_destination_paths( - const std::string &title, - const std::string &extensions) const -{ - - wxFileDialog dlg(wxTheApp->GetTopWindow(), wxString(title) ); - dlg.SetWildcard(extensions); - - dlg.ShowModal(); - - wxArrayString paths; - dlg.GetFilenames(paths); - - PathList ret(paths.size(), ""); - for(auto& p : paths) ret.push_back(p.ToStdString()); - - return ret; -} - -AppControllerBoilerplate::Path -AppControllerBoilerplate::query_destination_path( - const std::string &title, - const std::string &extensions) const -{ - wxFileDialog dlg(wxTheApp->GetTopWindow(), title ); - dlg.SetWildcard(extensions); - - dlg.ShowModal(); - - Path ret(dlg.GetFilename()); - - return ret; -} - -void AppControllerBoilerplate::report_issue(IssueType issuetype, - const std::string &description, - const std::string &brief) -{ - auto icon = wxICON_INFORMATION; - switch(issuetype) { - case IssueType::INFO: break; - case IssueType::WARN: icon = wxICON_WARNING; break; - case IssueType::ERR: - case IssueType::FATAL: icon = wxICON_ERROR; - } - - wxString str = _("Proba szoveg"); - wxMessageBox(str + _(description), _(brief), icon); -} - -AppControllerBoilerplate::ProgresIndicatorPtr -AppControllerBoilerplate::createProgressIndicator(unsigned statenum, - const std::string& title, - const std::string& firstmsg) const -{ - class GuiProgressIndicator: public ProgressIndicator { - wxProgressDialog gauge_; - using Base = ProgressIndicator; - wxString message_; - public: - - inline GuiProgressIndicator(int range, const std::string& title, - const std::string& firstmsg): - gauge_(_(title), _(firstmsg), range, wxTheApp->GetTopWindow()) - { - gauge_.Show(false); - Base::max(static_cast(range)); - Base::states(static_cast(range)); - } - - virtual void state(float val) override { - if( val <= max() && val >= 1.0) { - Base::state(val); - gauge_.Update(static_cast(val), message_); - } - } - - virtual void state(unsigned st) override { - if( st <= max() ) { - if(!gauge_.IsShown()) gauge_.ShowModal(); - Base::state(st); - gauge_.Update(static_cast(st), message_); - } - } - - virtual void message(const std::string & msg) override { - message_ = _(msg); - } - - virtual void messageFmt(const std::string& fmt, ...) { - va_list arglist; - va_start(arglist, fmt); - message_ = wxString::Format(_(fmt), arglist); - va_end(arglist); - } - - virtual void title(const std::string & title) override { - gauge_.SetTitle(_(title)); - } - }; - - auto pri = - std::make_shared(statenum, title, firstmsg); - - return pri; -} - void PrintController::make_skirt() { assert(print_ != nullptr); @@ -345,88 +233,15 @@ void PrintController::slice_to_png() conf.validate(); - auto bak = progressIndicator(); +// auto bak = progressIndicator(); progressIndicator(100, "Slicing to zipped png files..."); - std::async(std::launch::async, [this, &bak](){ +// std::async(std::launch::async, [this, bak](){ slice(); - progressIndicator(bak); - }); +// progressIndicator(bak); +// }); } -void AppController::set_global_progress_indicator_id( - unsigned gid, - unsigned sid) -{ - - class Wrapper: public ProgressIndicator { - wxGauge *gauge_; - wxStatusBar *stbar_; - using Base = ProgressIndicator; - std::string message_; - - void showProgress(bool show = true) { - gauge_->Show(show); - gauge_->Pulse(); - } - public: - - inline Wrapper(wxGauge *gauge, wxStatusBar *stbar): - gauge_(gauge), stbar_(stbar) - { - Base::max(static_cast(gauge->GetRange())); - Base::states(static_cast(gauge->GetRange())); - } - - virtual void state(float val) override { - if( val <= max() && val >= 1.0) { - Base::state(val); - stbar_->SetStatusText(message_); - gauge_->SetValue(static_cast(val)); - } - } - - virtual void state(unsigned st) override { - if( st <= max() ) { - Base::state(st); - - if(!gauge_->IsShown()) showProgress(true); - - stbar_->SetStatusText(message_); - if(st == gauge_->GetRange()) { - gauge_->SetValue(0); - showProgress(false); - } else { - gauge_->SetValue(static_cast(st)); - } - } - } - - virtual void message(const std::string & msg) override { - message_ = msg; - } - - virtual void messageFmt(const std::string& fmt, ...) { - va_list arglist; - va_start(arglist, fmt); - message_ = wxString::Format(_(fmt), arglist); - va_end(arglist); - } - - virtual void title(const std::string & /*title*/) override {} - - }; - - wxGauge* gauge = dynamic_cast(wxWindow::FindWindowById(gid)); - wxStatusBar* sb = dynamic_cast(wxWindow::FindWindowById(sid)); - - if(gauge && sb) { - auto&& progind = std::make_shared(gauge, sb); - progressIndicator(progind); - if(printctl) printctl->progressIndicator(progind); - } -} - void AppControllerBoilerplate::ProgressIndicator::messageFmt( const std::string &fmtstr, ...) { std::stringstream ss; diff --git a/xs/src/slic3r/AppController.hpp b/xs/src/slic3r/AppController.hpp index 4bd18b8fb..b505f576e 100644 --- a/xs/src/slic3r/AppController.hpp +++ b/xs/src/slic3r/AppController.hpp @@ -4,6 +4,9 @@ #include #include #include +#include +#include +#include namespace Slic3r { @@ -68,22 +71,25 @@ public: using ProgresIndicatorPtr = std::shared_ptr; - inline void progressIndicator(ProgresIndicatorPtr progrind) { - progressind_ = progrind; + progressind_[std::this_thread::get_id()] = progrind; } inline void progressIndicator(unsigned statenum, const std::string& title, const std::string& firstmsg = "") { - progressind_ = createProgressIndicator(statenum, title, firstmsg); + progressind_[std::this_thread::get_id()] = + createProgressIndicator(statenum, title, firstmsg); } inline ProgresIndicatorPtr progressIndicator() { - if(!progressind_) - progressind_ = createProgressIndicator(100, "Progress"); - return progressind_; + ProgresIndicatorPtr ret; + if( progressind_.find(std::this_thread::get_id()) == progressind_.end()) + progressind_[std::this_thread::get_id()] = ret = + = createProgressIndicator(100, "Progress"); + + return ret; } protected: @@ -94,7 +100,7 @@ protected: const std::string& firstmsg = "") const; private: - ProgresIndicatorPtr progressind_; + std::unordered_map progressind_; }; class PrintController: public AppControllerBoilerplate { @@ -105,6 +111,10 @@ protected: void make_brim(); void make_wipe_tower(); + void make_perimeters(PrintObject *pobj); + void infill(PrintObject *pobj); + void gen_support_material(PrintObject *pobj); + public: using Ptr = std::unique_ptr; @@ -116,13 +126,9 @@ public: } void slice(PrintObject *pobj); - void make_perimeters(PrintObject *pobj); - void infill(PrintObject *pobj); - void gen_support_material(PrintObject *pobj); void slice(); void slice_to_png(); - }; class AppController: protected AppControllerBoilerplate { diff --git a/xs/src/slic3r/appcontrollerwx.cpp b/xs/src/slic3r/appcontrollerwx.cpp new file mode 100644 index 000000000..8a56f6e4e --- /dev/null +++ b/xs/src/slic3r/appcontrollerwx.cpp @@ -0,0 +1,220 @@ +#include "AppController.hpp" + + +#include + +#include +#include +#include +#include +#include +#include +#include + +namespace Slic3r { + +AppControllerBoilerplate::PathList +AppControllerBoilerplate::query_destination_paths( + const std::string &title, + const std::string &extensions) const +{ + + wxFileDialog dlg(wxTheApp->GetTopWindow(), wxString(title) ); + dlg.SetWildcard(extensions); + + dlg.ShowModal(); + + wxArrayString paths; + dlg.GetFilenames(paths); + + PathList ret(paths.size(), ""); + for(auto& p : paths) ret.push_back(p.ToStdString()); + + return ret; +} + +AppControllerBoilerplate::Path +AppControllerBoilerplate::query_destination_path( + const std::string &title, + const std::string &extensions) const +{ + wxFileDialog dlg(wxTheApp->GetTopWindow(), title ); + dlg.SetWildcard(extensions); + + dlg.ShowModal(); + + Path ret(dlg.GetFilename()); + + return ret; +} + +void AppControllerBoilerplate::report_issue(IssueType issuetype, + const std::string &description, + const std::string &brief) +{ + auto icon = wxICON_INFORMATION; + switch(issuetype) { + case IssueType::INFO: break; + case IssueType::WARN: icon = wxICON_WARNING; break; + case IssueType::ERR: + case IssueType::FATAL: icon = wxICON_ERROR; + } + + wxString str = _("Proba szoveg"); + wxMessageBox(str + _(description), _(brief), icon); +} + +wxDEFINE_EVENT(PROGRESS_STATUS_UPDATE_EVENT, wxCommandEvent); + +AppControllerBoilerplate::ProgresIndicatorPtr +AppControllerBoilerplate::createProgressIndicator(unsigned statenum, + const std::string& title, + const std::string& firstmsg) const +{ + class GuiProgressIndicator: + public ProgressIndicator, public wxEvtHandler { + + std::shared_ptr gauge_; + using Base = ProgressIndicator; + wxString message_; + int range_; wxString title_; + + // status update handler + void _state( wxCommandEvent& evt) { + unsigned st = evt.GetInt(); + if( st < max() ) { + + if(!gauge_) gauge_ = std::make_shared( + title_, message_, range_, wxTheApp->GetTopWindow() + ); + + if(!gauge_->IsShown()) gauge_->ShowModal(); + Base::state(st); + gauge_->Update(static_cast(st), message_); + + } else { + gauge_.reset(); + } + } + + public: + + inline GuiProgressIndicator(int range, const std::string& title, + const std::string& firstmsg) : + range_(range), message_(_(firstmsg)), title_(_(title)) + { + Base::max(static_cast(range)); + Base::states(static_cast(range)); + + Bind(PROGRESS_STATUS_UPDATE_EVENT, + &GuiProgressIndicator::_state, + this); + } + + virtual void state(float val) override { + if( val >= 1.0) state(static_cast(val)); + } + + virtual void state(unsigned st) override { + // send status update event + auto evt = new wxCommandEvent(PROGRESS_STATUS_UPDATE_EVENT); + evt->SetInt(st); + wxQueueEvent(this, evt); + } + + virtual void message(const std::string & msg) override { + message_ = _(msg); + } + + virtual void messageFmt(const std::string& fmt, ...) { + va_list arglist; + va_start(arglist, fmt); + message_ = wxString::Format(_(fmt), arglist); + va_end(arglist); + } + + virtual void title(const std::string & title) override { + title_ = _(title); + } + }; + + auto pri = + std::make_shared(statenum, title, firstmsg); + + return pri; +} + +void AppController::set_global_progress_indicator_id( + unsigned gid, + unsigned sid) +{ + + class Wrapper: public ProgressIndicator { + wxGauge *gauge_; + wxStatusBar *stbar_; + using Base = ProgressIndicator; + std::string message_; + + void showProgress(bool show = true) { + gauge_->Show(show); + gauge_->Pulse(); + } + public: + + inline Wrapper(wxGauge *gauge, wxStatusBar *stbar): + gauge_(gauge), stbar_(stbar) + { + Base::max(static_cast(gauge->GetRange())); + Base::states(static_cast(gauge->GetRange())); + } + + virtual void state(float val) override { + if( val <= max() && val >= 1.0) { + Base::state(val); + stbar_->SetStatusText(message_); + gauge_->SetValue(static_cast(val)); + } + } + + virtual void state(unsigned st) override { + if( st <= max() ) { + Base::state(st); + + if(!gauge_->IsShown()) showProgress(true); + + stbar_->SetStatusText(message_); + if(st == gauge_->GetRange()) { + gauge_->SetValue(0); + showProgress(false); + } else { + gauge_->SetValue(static_cast(st)); + } + } + } + + virtual void message(const std::string & msg) override { + message_ = msg; + } + + virtual void messageFmt(const std::string& fmt, ...) { + va_list arglist; + va_start(arglist, fmt); + message_ = wxString::Format(_(fmt), arglist); + va_end(arglist); + } + + virtual void title(const std::string & /*title*/) override {} + + }; + + wxGauge* gauge = dynamic_cast(wxWindow::FindWindowById(gid)); + wxStatusBar* sb = dynamic_cast(wxWindow::FindWindowById(sid)); + + if(gauge && sb) { + auto&& progind = std::make_shared(gauge, sb); + progressIndicator(progind); + if(printctl) printctl->progressIndicator(progind); + } +} + +}