PrusaSlicer-NonPlainar/xs/src/slic3r/AppControllerWx.cpp

303 lines
7.7 KiB
C++
Raw Normal View History

2018-06-26 13:51:47 +00:00
#include "AppController.hpp"
#include <thread>
#include <future>
2018-06-26 13:51:47 +00:00
#include <slic3r/GUI/GUI.hpp>
#include <slic3r/GUI/ProgressStatusBar.hpp>
2018-06-26 13:51:47 +00:00
#include <wx/app.h>
#include <wx/filedlg.h>
#include <wx/msgdlg.h>
#include <wx/progdlg.h>
#include <wx/gauge.h>
#include <wx/statusbr.h>
#include <wx/event.h>
// This source file implements the UI dependent methods of the AppControllers.
// It will be clear what is needed to be reimplemented in case of a UI framework
// change or a CLI client creation. In this particular case we use wxWidgets to
// implement everything.
2018-06-26 13:51:47 +00:00
namespace Slic3r {
bool AppControllerBoilerplate::supports_asynch() const
{
return true;
}
void AppControllerBoilerplate::process_events()
{
wxSafeYield();
}
2018-06-26 13:51:47 +00:00
AppControllerBoilerplate::PathList
AppControllerBoilerplate::query_destination_paths(
const wxString &title,
2018-06-26 13:51:47 +00:00
const std::string &extensions) const
{
2018-07-02 14:15:21 +00:00
wxFileDialog dlg(wxTheApp->GetTopWindow(), title );
2018-06-26 13:51:47 +00:00
dlg.SetWildcard(extensions);
dlg.ShowModal();
wxArrayString paths;
dlg.GetPaths(paths);
PathList ret(paths.size(), "");
for(auto& p : paths) ret.push_back(p.ToStdString());
return ret;
}
AppControllerBoilerplate::Path
AppControllerBoilerplate::query_destination_path(
const wxString &title,
2018-06-26 13:51:47 +00:00
const std::string &extensions,
const std::string& hint) const
{
wxFileDialog dlg(wxTheApp->GetTopWindow(), title );
dlg.SetWildcard(extensions);
dlg.SetFilename(hint);
Path ret;
if(dlg.ShowModal() == wxID_OK) {
ret = Path(dlg.GetPath());
}
return ret;
}
bool AppControllerBoilerplate::report_issue(IssueType issuetype,
const wxString &description,
const wxString &brief)
2018-06-26 13:51:47 +00:00
{
auto icon = wxICON_INFORMATION;
auto style = wxOK|wxCENTRE;
2018-06-26 13:51:47 +00:00
switch(issuetype) {
case IssueType::INFO: break;
case IssueType::WARN: icon = wxICON_WARNING; break;
case IssueType::WARN_Q: icon = wxICON_WARNING; style |= wxCANCEL; break;
2018-06-26 13:51:47 +00:00
case IssueType::ERR:
case IssueType::FATAL: icon = wxICON_ERROR;
}
auto ret = wxMessageBox(description, brief, icon | style);
return ret != wxCANCEL;
2018-06-26 13:51:47 +00:00
}
2018-07-02 14:15:21 +00:00
bool AppControllerBoilerplate::report_issue(
AppControllerBoilerplate::IssueType issuetype,
const wxString &description)
2018-07-02 14:15:21 +00:00
{
return report_issue(issuetype, description, wxString());
2018-07-02 14:15:21 +00:00
}
2018-06-26 13:51:47 +00:00
wxDEFINE_EVENT(PROGRESS_STATUS_UPDATE_EVENT, wxCommandEvent);
namespace {
/*
* A simple thread safe progress dialog implementation that can be used from
* the main thread as well.
*/
2018-06-26 13:51:47 +00:00
class GuiProgressIndicator:
public ProgressIndicator, public wxEvtHandler {
2018-06-26 13:51:47 +00:00
wxProgressDialog gauge_;
using Base = ProgressIndicator;
2018-06-26 13:51:47 +00:00
wxString message_;
int range_; wxString title_;
bool is_asynch_ = false;
const int id_ = wxWindow::NewControlId();
// status update handler
void _state( wxCommandEvent& evt) {
unsigned st = evt.GetInt();
message_ = evt.GetString();
2018-06-26 13:51:47 +00:00
_state(st);
}
// Status update implementation
2018-06-26 13:51:47 +00:00
void _state( unsigned st) {
2018-07-02 14:15:21 +00:00
if(!gauge_.IsShown()) gauge_.ShowModal();
Base::state(st);
gauge_.Update(static_cast<int>(st), message_);
2018-06-26 13:51:47 +00:00
}
public:
/// Setting whether it will be used from the UI thread or some worker thread
2018-06-26 13:51:47 +00:00
inline void asynch(bool is) { is_asynch_ = is; }
/// Get the mode of parallel operation.
2018-06-26 13:51:47 +00:00
inline bool asynch() const { return is_asynch_; }
inline GuiProgressIndicator(int range, const wxString& title,
const wxString& firstmsg) :
gauge_(title, firstmsg, range, wxTheApp->GetTopWindow(),
wxPD_APP_MODAL | wxPD_AUTO_HIDE),
2018-07-02 14:15:21 +00:00
message_(firstmsg),
range_(range), title_(title)
2018-06-26 13:51:47 +00:00
{
Base::max(static_cast<float>(range));
Base::states(static_cast<unsigned>(range));
Bind(PROGRESS_STATUS_UPDATE_EVENT,
&GuiProgressIndicator::_state,
this, id_);
}
virtual void cancel() override {
update(max(), "Abort");
ProgressIndicator::cancel();
}
2018-06-26 13:51:47 +00:00
virtual void state(float val) override {
2018-07-02 14:15:21 +00:00
state(static_cast<unsigned>(val));
2018-06-26 13:51:47 +00:00
}
2018-06-26 15:02:46 +00:00
void state(unsigned st) {
2018-06-26 13:51:47 +00:00
// send status update event
if(is_asynch_) {
auto evt = new wxCommandEvent(PROGRESS_STATUS_UPDATE_EVENT, id_);
evt->SetInt(st);
evt->SetString(message_);
2018-06-26 13:51:47 +00:00
wxQueueEvent(this, evt);
} else _state(st);
}
virtual void message(const wxString & msg) override {
2018-07-02 14:15:21 +00:00
message_ = msg;
2018-06-26 13:51:47 +00:00
}
virtual void messageFmt(const wxString& fmt, ...) {
2018-06-26 13:51:47 +00:00
va_list arglist;
va_start(arglist, fmt);
2018-07-02 14:15:21 +00:00
message_ = wxString::Format(wxString(fmt), arglist);
2018-06-26 13:51:47 +00:00
va_end(arglist);
}
virtual void title(const wxString & title) override {
2018-07-02 14:15:21 +00:00
title_ = title;
2018-06-26 13:51:47 +00:00
}
};
}
AppControllerBoilerplate::ProgresIndicatorPtr
AppControllerBoilerplate::create_progress_indicator(
unsigned statenum, const wxString& title, const wxString& firstmsg) const
2018-06-26 13:51:47 +00:00
{
auto pri =
std::make_shared<GuiProgressIndicator>(statenum, title, firstmsg);
// We set up the mode of operation depending of the creator thread's
// identity
2018-06-26 13:51:47 +00:00
pri->asynch(!is_main_thread());
return pri;
}
2018-07-02 14:15:21 +00:00
AppControllerBoilerplate::ProgresIndicatorPtr
AppControllerBoilerplate::create_progress_indicator(unsigned statenum,
const wxString &title) const
2018-07-02 14:15:21 +00:00
{
return create_progress_indicator(statenum, title, wxString());
2018-07-02 14:15:21 +00:00
}
2018-06-26 13:51:47 +00:00
namespace {
class Wrapper: public ProgressIndicator, public wxEvtHandler {
ProgressStatusBar *sbar_;
using Base = ProgressIndicator;
2018-06-26 13:51:47 +00:00
std::string message_;
AppControllerBoilerplate& ctl_;
void showProgress(bool show = true) {
sbar_->show_progress(show);
2018-06-26 13:51:47 +00:00
}
void _state(unsigned st) {
if( st <= ProgressIndicator::max() ) {
2018-06-26 13:51:47 +00:00
Base::state(st);
sbar_->set_status_text(message_);
sbar_->set_progress(st);
2018-06-26 13:51:47 +00:00
}
}
// status update handler
void _state( wxCommandEvent& evt) {
unsigned st = evt.GetInt(); _state(st);
}
const int id_ = wxWindow::NewControlId();
public:
inline Wrapper(ProgressStatusBar *sbar,
2018-06-26 13:51:47 +00:00
AppControllerBoilerplate& ctl):
sbar_(sbar), ctl_(ctl)
2018-06-26 13:51:47 +00:00
{
Base::max(static_cast<float>(sbar_->get_range()));
Base::states(static_cast<unsigned>(sbar_->get_range()));
2018-06-26 13:51:47 +00:00
Bind(PROGRESS_STATUS_UPDATE_EVENT,
&Wrapper::_state,
this, id_);
}
virtual void state(float val) override {
state(unsigned(val));
}
virtual void max(float val) override {
if(val > 1.0) {
sbar_->set_range(static_cast<int>(val));
ProgressIndicator::max(val);
}
2018-06-26 13:51:47 +00:00
}
2018-06-26 15:02:46 +00:00
void state(unsigned st) {
2018-06-26 13:51:47 +00:00
if(!ctl_.is_main_thread()) {
auto evt = new wxCommandEvent(PROGRESS_STATUS_UPDATE_EVENT, id_);
evt->SetInt(st);
wxQueueEvent(this, evt);
} else {
_state(st);
}
2018-06-26 13:51:47 +00:00
}
virtual void message(const wxString & msg) override {
2018-06-26 13:51:47 +00:00
message_ = msg;
}
virtual void message_fmt(const wxString& fmt, ...) override {
2018-06-26 13:51:47 +00:00
va_list arglist;
va_start(arglist, fmt);
2018-07-03 08:22:55 +00:00
message_ = wxString::Format(fmt, arglist);
2018-06-26 13:51:47 +00:00
va_end(arglist);
}
virtual void title(const wxString & /*title*/) override {}
2018-06-26 13:51:47 +00:00
virtual void on_cancel(CancelFn fn) override {
sbar_->set_cancel_callback(fn);
Base::on_cancel(fn);
}
2018-06-26 13:51:47 +00:00
};
}
void AppController::set_global_progress_indicator(ProgressStatusBar *prsb)
2018-06-26 13:51:47 +00:00
{
if(prsb) {
global_progress_indicator(std::make_shared<Wrapper>(prsb, *this));
2018-06-26 13:51:47 +00:00
}
}
}