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

429 lines
12 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/PngExportDialog.hpp>
#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(
2018-07-02 14:15:21 +00:00
const string &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(
2018-07-02 14:15:21 +00:00
const string &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,
2018-07-02 14:15:21 +00:00
const string &description,
const string &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 string &description)
{
return report_issue(issuetype, description, string());
}
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 IProgressIndicator, public wxEvtHandler {
wxProgressDialog gauge_;
2018-06-26 13:51:47 +00:00
using Base = IProgressIndicator;
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_; }
2018-07-02 14:15:21 +00:00
inline GuiProgressIndicator(int range, const string& title,
const string& 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");
IProgressIndicator::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);
}
2018-07-02 14:15:21 +00:00
virtual void message(const string & msg) override {
message_ = msg;
2018-06-26 13:51:47 +00:00
}
2018-07-02 14:15:21 +00:00
virtual void messageFmt(const string& 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);
}
2018-07-02 14:15:21 +00:00
virtual void title(const string & title) override {
title_ = title;
2018-06-26 13:51:47 +00:00
}
};
}
AppControllerBoilerplate::ProgresIndicatorPtr
AppControllerBoilerplate::create_progress_indicator(
2018-07-02 14:15:21 +00:00
unsigned statenum, const string& title, const string& 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 string &title) const
{
return create_progress_indicator(statenum, title, string());
}
2018-06-26 13:51:47 +00:00
namespace {
// A wrapper progress indicator class around the statusbar created in perl.
2018-06-26 13:51:47 +00:00
class Wrapper: public IProgressIndicator, public wxEvtHandler {
wxGauge *gauge_;
wxStatusBar *stbar_;
using Base = IProgressIndicator;
std::string message_;
AppControllerBoilerplate& ctl_;
void showProgress(bool show = true) {
gauge_->Show(show);
}
void _state(unsigned st) {
if( st <= IProgressIndicator::max() ) {
2018-06-26 13:51:47 +00:00
Base::state(st);
if(!gauge_->IsShown()) showProgress(true);
stbar_->SetStatusText(message_);
if(static_cast<long>(st) == gauge_->GetRange()) {
2018-06-26 13:51:47 +00:00
gauge_->SetValue(0);
showProgress(false);
} else {
gauge_->SetValue(static_cast<int>(st));
}
}
}
// status update handler
void _state( wxCommandEvent& evt) {
unsigned st = evt.GetInt(); _state(st);
}
const int id_ = wxWindow::NewControlId();
public:
inline Wrapper(wxGauge *gauge, wxStatusBar *stbar,
AppControllerBoilerplate& ctl):
gauge_(gauge), stbar_(stbar), ctl_(ctl)
{
Base::max(static_cast<float>(gauge->GetRange()));
Base::states(static_cast<unsigned>(gauge->GetRange()));
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) {
gauge_->SetRange(static_cast<int>(val));
IProgressIndicator::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
}
2018-07-02 14:15:21 +00:00
virtual void message(const string & msg) override {
2018-06-26 13:51:47 +00:00
message_ = msg;
}
2018-07-02 14:15:21 +00:00
virtual void message_fmt(const string& 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);
}
2018-07-02 14:15:21 +00:00
virtual void title(const string & /*title*/) override {}
2018-06-26 13:51:47 +00:00
};
}
2018-06-26 15:02:46 +00:00
void AppController::set_global_progress_indicator(
2018-06-26 13:51:47 +00:00
unsigned gid,
unsigned sid)
{
wxGauge* gauge = dynamic_cast<wxGauge*>(wxWindow::FindWindowById(gid));
wxStatusBar* sb = dynamic_cast<wxStatusBar*>(wxWindow::FindWindowById(sid));
if(gauge && sb) {
global_progress_indicator(std::make_shared<Wrapper>(gauge, sb, *this));
2018-06-26 13:51:47 +00:00
}
}
PrintController::PngExportData PrintController::query_png_export_data()
{
// Implement the logic of the PngExportDialog
2018-06-26 13:51:47 +00:00
class PngExportView: public PngExportDialog {
double ratio_, bs_ratio_;
PrintController& ctl_;
2018-06-26 13:51:47 +00:00
public:
PngExportView(PrintController& ctl):
PngExportDialog(wxTheApp->GetTopWindow()), ctl_(ctl)
{
2018-06-26 13:51:47 +00:00
ratio_ = double(spin_reso_width_->GetValue()) /
spin_reso_height_->GetValue();
bs_ratio_ = bed_width_spin_->GetValue() /
bed_height_spin_->GetValue();
}
PngExportData export_data() const {
PrintController::PngExportData ret;
ret.zippath = filepick_ctl_->GetPath();
ret.width_px = spin_reso_width_->GetValue();
ret.height_px = spin_reso_height_->GetValue();
ret.width_mm = bed_width_spin_->GetValue();
ret.height_mm = bed_height_spin_->GetValue();
ret.exp_time_s = exptime_spin_->GetValue();
ret.exp_time_first_s = exptime_first_spin_->GetValue();
ret.corr_x = corr_spin_x_->GetValue();
ret.corr_y = corr_spin_y_->GetValue();
ret.corr_z = corr_spin_z_->GetValue();
2018-06-26 13:51:47 +00:00
return ret;
}
void prefill(const PngExportData& data) {
filepick_ctl_->SetPath(data.zippath);
spin_reso_width_->SetValue(data.width_px);
spin_reso_height_->SetValue(data.height_px);
bed_width_spin_->SetValue(data.width_mm);
bed_height_spin_->SetValue(data.height_mm);
exptime_spin_->SetValue(data.exp_time_s);
exptime_first_spin_->SetValue(data.exp_time_first_s);
corr_spin_x_->SetValue(data.corr_x);
corr_spin_y_->SetValue(data.corr_y);
corr_spin_z_->SetValue(data.corr_z);
if(data.zippath.empty()) export_btn_->Disable();
else export_btn_->Enable();
2018-06-26 13:51:47 +00:00
}
virtual void ResoLock( wxCommandEvent& /*event*/ ) override {
ratio_ = double(spin_reso_width_->GetValue()) /
double(spin_reso_height_->GetValue());
}
virtual void BedsizeLock( wxCommandEvent& /*event*/ ) override {
bs_ratio_ = bed_width_spin_->GetValue() /
bed_height_spin_->GetValue();
}
virtual void EvalResoSpin( wxCommandEvent& event ) override {
if(reso_lock_btn_->GetValue()) {
if(event.GetId() == spin_reso_width_->GetId()) {
spin_reso_height_->SetValue(
std::round(spin_reso_width_->GetValue()/ratio_));
spin_reso_height_->Update();
2018-06-26 13:51:47 +00:00
} else {
spin_reso_width_->SetValue(
std::round(spin_reso_height_->GetValue()*ratio_));
spin_reso_width_->Update();
2018-06-26 13:51:47 +00:00
}
}
}
virtual void EvalBedSpin( wxCommandEvent& event ) override {
if(bedsize_lock_btn_->GetValue()) {
if(event.GetId() == bed_width_spin_->GetId()) {
bed_height_spin_->SetValue(
std::round(bed_width_spin_->GetValue()/bs_ratio_));
bed_height_spin_->Update();
2018-06-26 13:51:47 +00:00
} else {
bed_width_spin_->SetValue(
std::round(bed_height_spin_->GetValue()*bs_ratio_));
bed_width_spin_->Update();
2018-06-26 13:51:47 +00:00
}
}
}
virtual void onFileChanged( wxFileDirPickerEvent& event ) {
if(filepick_ctl_->GetPath().IsEmpty()) export_btn_->Disable();
else export_btn_->Enable();
}
virtual void Close( wxCommandEvent& /*event*/ ) {
auto ret = wxID_OK;
if(wxFileName(filepick_ctl_->GetPath()).Exists())
if(!ctl_.report_issue(PrintController::IssueType::WARN_Q,
2018-07-02 14:15:21 +00:00
_(L("File already exists. Overwrite?")),
_(L("Warning")))) ret = wxID_CANCEL;
EndModal(ret);
}
2018-06-26 13:51:47 +00:00
};
PngExportView exdlg(*this);
2018-06-26 13:51:47 +00:00
exdlg.prefill(prev_expdata_);
auto r = exdlg.ShowModal();
auto ret = exdlg.export_data();
prev_expdata_ = ret;
if(r != wxID_OK) ret.zippath.clear();
return ret;
}
}