PrusaSlicer-NonPlainar/src/slic3r/AppControllerWx.cpp

341 lines
8.3 KiB
C++
Raw Normal View History

2018-06-26 13:51:47 +00:00
#include "AppController.hpp"
2018-09-19 11:43:15 +00:00
#include <wx/stdstream.h>
#include <wx/wfstream.h>
#include <wx/zipstrm.h>
2018-06-26 13:51:47 +00:00
#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 AppControllerGui::supports_asynch() const
{
return true;
}
void AppControllerGui::process_events()
{
2018-09-17 13:12:13 +00:00
wxYieldIfNeeded();
}
FilePathList AppControllerGui::query_destination_paths(
2018-09-17 13:12:13 +00:00
const std::string &title,
const std::string &extensions,
const std::string &/*functionid*/,
const std::string& hint) const
2018-06-26 13:51:47 +00:00
{
2018-09-17 13:12:13 +00:00
wxFileDialog dlg(wxTheApp->GetTopWindow(), _(title) );
2018-06-26 13:51:47 +00:00
dlg.SetWildcard(extensions);
dlg.SetFilename(hint);
2018-06-26 13:51:47 +00:00
FilePathList ret;
2018-06-26 13:51:47 +00:00
if(dlg.ShowModal() == wxID_OK) {
wxArrayString paths;
dlg.GetPaths(paths);
for(auto& p : paths) ret.push_back(p.ToStdString());
}
2018-06-26 13:51:47 +00:00
return ret;
}
FilePath AppControllerGui::query_destination_path(
2018-09-17 13:12:13 +00:00
const std::string &title,
2018-06-26 13:51:47 +00:00
const std::string &extensions,
const std::string &/*functionid*/,
2018-06-26 13:51:47 +00:00
const std::string& hint) const
{
2018-09-17 13:12:13 +00:00
wxFileDialog dlg(wxTheApp->GetTopWindow(), _(title) );
2018-06-26 13:51:47 +00:00
dlg.SetWildcard(extensions);
dlg.SetFilename(hint);
FilePath ret;
2018-06-26 13:51:47 +00:00
if(dlg.ShowModal() == wxID_OK) {
ret = FilePath(dlg.GetPath());
2018-06-26 13:51:47 +00:00
}
return ret;
}
bool AppControllerGui::report_issue(IssueType issuetype,
2018-09-17 13:12:13 +00:00
const std::string &description,
const std::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;
}
2018-09-17 13:12:13 +00:00
auto ret = wxMessageBox(_(description), _(brief), icon | style);
return ret != wxCANCEL;
2018-06-26 13:51:47 +00:00
}
wxDEFINE_EVENT(PROGRESS_STATUS_UPDATE_EVENT, wxCommandEvent);
2018-09-19 11:43:15 +00:00
struct Zipper::Impl {
wxFileName fpath;
wxFFileOutputStream zipfile;
wxZipOutputStream zipstream;
wxStdOutputStream pngstream;
2018-09-19 11:43:15 +00:00
Impl(const std::string& zipfile_path):
fpath(zipfile_path),
zipfile(zipfile_path),
zipstream(zipfile),
pngstream(zipstream)
2018-09-19 11:43:15 +00:00
{
if(!zipfile.IsOk())
2018-09-19 11:43:15 +00:00
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->zipstream.PutNextEntry(fname);
2018-09-19 11:43:15 +00:00
}
std::string Zipper::get_name() const
{
return m_impl->fpath.GetName().ToStdString();
2018-09-19 11:43:15 +00:00
}
std::ostream &Zipper::stream()
{
return m_impl->pngstream;
2018-09-19 11:43:15 +00:00
}
void Zipper::close()
{
m_impl->zipstream.Close();
m_impl->zipfile.Close();
2018-09-19 11:43:15 +00:00
}
2018-06-26 13:51:47 +00:00
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 m_gauge;
using Base = ProgressIndicator;
wxString m_message;
int m_range; wxString m_title;
bool m_is_asynch = false;
2018-06-26 13:51:47 +00:00
const int m_id = wxWindow::NewControlId();
2018-06-26 13:51:47 +00:00
// status update handler
void _state( wxCommandEvent& evt) {
unsigned st = evt.GetInt();
m_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) {
if(!m_gauge.IsShown()) m_gauge.ShowModal();
2018-07-02 14:15:21 +00:00
Base::state(st);
if(!m_gauge.Update(static_cast<int>(st), m_message)) {
2018-09-17 13:12:13 +00:00
cancel();
}
2018-06-26 13:51:47 +00:00
}
public:
/// Setting whether it will be used from the UI thread or some worker thread
inline void asynch(bool is) { m_is_asynch = is; }
/// Get the mode of parallel operation.
inline bool asynch() const { return m_is_asynch; }
2018-06-26 13:51:47 +00:00
inline GuiProgressIndicator(int range, const wxString& title,
const wxString& firstmsg) :
m_gauge(title, firstmsg, range, wxTheApp->GetTopWindow(),
2018-09-17 13:12:13 +00:00
wxPD_APP_MODAL | wxPD_AUTO_HIDE | wxPD_CAN_ABORT),
m_message(firstmsg),
m_range(range), m_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, m_id);
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(m_is_asynch) {
auto evt = new wxCommandEvent(PROGRESS_STATUS_UPDATE_EVENT, m_id);
2018-06-26 13:51:47 +00:00
evt->SetInt(st);
evt->SetString(m_message);
2018-06-26 13:51:47 +00:00
wxQueueEvent(this, evt);
} else _state(st);
}
2018-09-17 13:12:13 +00:00
virtual void message(const std::string & msg) override {
m_message = _(msg);
2018-06-26 13:51:47 +00:00
}
2018-09-17 13:12:13 +00:00
virtual void messageFmt(const std::string& fmt, ...) {
2018-06-26 13:51:47 +00:00
va_list arglist;
va_start(arglist, fmt);
m_message = wxString::Format(_(fmt), arglist);
2018-06-26 13:51:47 +00:00
va_end(arglist);
}
2018-09-17 13:12:13 +00:00
virtual void title(const std::string & title) override {
m_title = _(title);
2018-06-26 13:51:47 +00:00
}
};
}
ProgresIndicatorPtr AppControllerGui::create_progress_indicator(
2018-09-17 13:12:13 +00:00
unsigned statenum,
const std::string& title,
const std::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;
}
namespace {
class Wrapper: public ProgressIndicator, public wxEvtHandler {
ProgressStatusBar *m_sbar;
using Base = ProgressIndicator;
wxString m_message;
AppControllerBase& m_ctl;
2018-06-26 13:51:47 +00:00
void showProgress(bool show = true) {
m_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);
m_sbar->set_status_text(m_message);
m_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,
AppControllerBase& ctl):
m_sbar(sbar), m_ctl(ctl)
2018-06-26 13:51:47 +00:00
{
Base::max(static_cast<float>(m_sbar->get_range()));
Base::states(static_cast<unsigned>(m_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) {
m_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) {
if(!m_ctl.is_main_thread()) {
2018-06-26 13:51:47 +00:00
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-09-17 13:12:13 +00:00
virtual void message(const std::string & msg) override {
m_message = _(msg);
2018-06-26 13:51:47 +00:00
}
2018-09-17 13:12:13 +00:00
virtual void message_fmt(const std::string& fmt, ...) override {
2018-06-26 13:51:47 +00:00
va_list arglist;
va_start(arglist, fmt);
m_message = wxString::Format(_(fmt), arglist);
2018-06-26 13:51:47 +00:00
va_end(arglist);
}
2018-09-17 13:12:13 +00:00
virtual void title(const std::string & /*title*/) override {}
2018-06-26 13:51:47 +00:00
virtual void on_cancel(CancelFn fn) override {
m_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) {
auto ctl = GUI::get_appctl();
ctl->global_progress_indicator(std::make_shared<Wrapper>(prsb, *ctl));
2018-06-26 13:51:47 +00:00
}
}
}