FIX: add load model process dialog

Change-Id: I3cf9f5a535236f87425a0314bde283e4549ca5e7
This commit is contained in:
chunmao.guo 2023-05-25 12:29:02 +08:00 committed by Lane.Wei
parent 0d9c0c1c12
commit ddd2c544c8
5 changed files with 96 additions and 36 deletions

View file

@ -8,6 +8,7 @@
#include "Widgets/Label.hpp" #include "Widgets/Label.hpp"
#include "Printer/PrinterFileSystem.h" #include "Printer/PrinterFileSystem.h"
#include "MsgDialog.hpp" #include "MsgDialog.hpp"
#include "Widgets/ProgressDialog.hpp"
#include <libslic3r/Model.hpp> #include <libslic3r/Model.hpp>
#include <libslic3r/Format/bbs_3mf.hpp> #include <libslic3r/Format/bbs_3mf.hpp>
@ -446,6 +447,15 @@ void MediaFilePanel::fetchUrl(boost::weak_ptr<PrinterFileSystem> wfs)
} }
} }
struct MediaProgressDialog : ProgressDialog
{
MediaProgressDialog(wxString title, wxWindow * parent, std::function<void()> cancel)
: ProgressDialog(title, "", 100, parent, wxPD_NO_PROGRESS | wxPD_APP_MODAL | wxPD_CAN_ABORT)
, m_cancel(cancel) {}
void OnCancel() override{m_cancel(); }
std::function<void()> m_cancel;
};
void Slic3r::GUI::MediaFilePanel::doAction(size_t index, int action) void Slic3r::GUI::MediaFilePanel::doAction(size_t index, int action)
{ {
auto fs = m_image_grid->GetFileSystem(); auto fs = m_image_grid->GetFileSystem();
@ -467,19 +477,30 @@ void Slic3r::GUI::MediaFilePanel::doAction(size_t index, int action)
} else if (action == 1) { } else if (action == 1) {
if (fs->GetFileType() == PrinterFileSystem::F_MODEL) { if (fs->GetFileType() == PrinterFileSystem::F_MODEL) {
if (index != -1) { if (index != -1) {
fs->FetchModel(index, [fs,index](std::string const & data) { auto dlg = new MediaProgressDialog(_L("Print"), this, [fs] { fs->FetchModelCancel(); });
dlg->Update(0, _L("Fetching model infomations ..."));
fs->FetchModel(index, [this, fs, dlg, index](int result, std::string const &data) {
dlg->Destroy();
if (result == PrinterFileSystem::ERROR_CANCEL)
return;
if (result != 0)
MessageDialog(this,
_L("Failed to fetching model infomations from printer."),
_L("Error"), wxOK).ShowModal();
Slic3r::DynamicPrintConfig config; Slic3r::DynamicPrintConfig config;
Slic3r::Model model; Slic3r::Model model;
Slic3r::PlateDataPtrs plate_data_list; Slic3r::PlateDataPtrs plate_data_list;
Slic3r::Semver file_version; Slic3r::Semver file_version;
std::istringstream is(data); std::istringstream is(data);
if (!Slic3r::load_gcode_3mf_from_stream(is, &config, &model, &plate_data_list, &file_version)) if (!Slic3r::load_gcode_3mf_from_stream(is, &config, &model, &plate_data_list, &file_version)) {
MessageDialog(this,
_L("Failed to parse model infomations."),
_L("Error"), wxOK).ShowModal();
return; return;
// TODO: print gcode 3mf }
auto &file = fs->GetFile(index); auto &file = fs->GetFile(index);
Slic3r::GUI::wxGetApp().plater()->update_print_required_data(config, model, plate_data_list, from_u8(file.name).ToStdString()); Slic3r::GUI::wxGetApp().plater()->update_print_required_data(config, model, plate_data_list, from_u8(file.name).ToStdString());
wxPostEvent(Slic3r::GUI::wxGetApp().plater(), SimpleEvent(EVT_PRINT_FROM_SDCARD_VIEW)); wxPostEvent(Slic3r::GUI::wxGetApp().plater(), SimpleEvent(EVT_PRINT_FROM_SDCARD_VIEW));
}); });
} }
return; return;

View file

@ -275,8 +275,10 @@ void PrinterFileSystem::DownloadCancel(size_t index)
file.flags &= ~FF_DOWNLOAD; file.flags &= ~FF_DOWNLOAD;
} }
void PrinterFileSystem::FetchModel(size_t index, std::function<void(std::string const &)> callback) void PrinterFileSystem::FetchModel(size_t index, std::function<void(int, std::string const &)> callback)
{ {
if (m_task_flags & FF_FETCH_MODEL)
return;
json req; json req;
json arr; json arr;
if (index == (size_t) -1) return; if (index == (size_t) -1) return;
@ -293,7 +295,8 @@ void PrinterFileSystem::FetchModel(size_t index, std::function<void(std::string
} }
req["paths"] = arr; req["paths"] = arr;
req["zip"] = true; req["zip"] = true;
SendRequest<File>( m_task_flags |= FF_FETCH_MODEL;
m_fetch_model_seq = SendRequest<File>(
SUB_FILE, req, SUB_FILE, req,
[](json const &resp, File &file, unsigned char const *data) -> int { [](json const &resp, File &file, unsigned char const *data) -> int {
// in work thread, continue recv // in work thread, continue recv
@ -304,11 +307,18 @@ void PrinterFileSystem::FetchModel(size_t index, std::function<void(std::string
} }
return 0; return 0;
}, },
[callback](int, File const &file) { [this, callback](int result, File const &file) {
callback(file.local_path); m_task_flags &= ~FF_FETCH_MODEL;
callback(result, file.local_path);
}); });
} }
void PrinterFileSystem::FetchModelCancel()
{
if ((m_task_flags & FF_FETCH_MODEL) == 0) return;
CancelRequests2({m_fetch_model_seq});
}
size_t PrinterFileSystem::GetCount() const size_t PrinterFileSystem::GetCount() const
{ {
if (m_group_mode == G_NONE) if (m_group_mode == G_NONE)
@ -715,7 +725,7 @@ bool PrinterFileSystem::ParseThumbnail(File &file, std::istream &is)
for (auto &plate : plate_data_list) { for (auto &plate : plate_data_list) {
time += atof(plate->gcode_prediction.c_str()); time += atof(plate->gcode_prediction.c_str());
weight += atof(plate->gcode_weight.c_str()); weight += atof(plate->gcode_weight.c_str());
if (!plate->thumbnail_file.empty()) if (!plate->gcode_file.empty() && !plate->thumbnail_file.empty())
file.metadata.emplace("plate_thumbnail_" + std::to_string(plate->plate_index), plate->thumbnail_file); file.metadata.emplace("plate_thumbnail_" + std::to_string(plate->plate_index), plate->thumbnail_file);
} }
file.metadata.emplace("Title", model.model_info->model_name); file.metadata.emplace("Title", model.model_info->model_name);
@ -937,18 +947,29 @@ void PrinterFileSystem::InstallNotify(int type, callback_t2 const &callback)
m_notifies[type] = callback; m_notifies[type] = callback;
} }
void PrinterFileSystem::CancelRequest(boost::uint32_t seq) void PrinterFileSystem::CancelRequest(boost::uint32_t seq) { CancelRequests({seq}); }
void PrinterFileSystem::CancelRequests(std::vector<boost::uint32_t> const &seqs)
{ {
json req; json req;
json arr; json arr;
for (auto seq : seqs)
arr.push_back(seq); arr.push_back(seq);
req["tasks"] = arr; req["tasks"] = arr;
SendRequest(TASK_CANCEL, req, [this](int result, json const &resp, unsigned char const *) { SendRequest(TASK_CANCEL, req, [this](int result, json const &resp, unsigned char const *) {
if (result != 0) return; if (result != 0) return;
json tasks = resp["tasks"]; json tasks = resp["tasks"];
std::vector<boost::uint32_t> seqs;
for (auto &f : tasks) seqs.push_back(f);
CancelRequests2(seqs);
});
}
void PrinterFileSystem::CancelRequests2(std::vector<boost::uint32_t> const &seqs)
{
std::deque<callback_t2> callbacks; std::deque<callback_t2> callbacks;
boost::unique_lock l(m_mutex); boost::unique_lock l(m_mutex);
for (auto &f : tasks) { for (auto &f : seqs) {
boost::uint32_t seq = f; boost::uint32_t seq = f;
seq -= m_sequence; seq -= m_sequence;
if (size_t(seq) >= m_callbacks.size()) continue; if (size_t(seq) >= m_callbacks.size()) continue;
@ -963,7 +984,6 @@ void PrinterFileSystem::CancelRequest(boost::uint32_t seq)
} }
l.unlock(); l.unlock();
for (auto &c : callbacks) c(ERROR_CANCEL, json(), nullptr); for (auto &c : callbacks) c(ERROR_CANCEL, json(), nullptr);
});
} }
void PrinterFileSystem::RecvMessageThread() void PrinterFileSystem::RecvMessageThread()

View file

@ -37,6 +37,7 @@ class PrinterFileSystem : public wxEvtHandler, public boost::enable_shared_from_
TASK_CANCEL = 0x1000 TASK_CANCEL = 0x1000
}; };
public:
enum { enum {
SUCCESS = 0, SUCCESS = 0,
CONTINUE = 1, CONTINUE = 1,
@ -89,6 +90,7 @@ public:
FF_THUMNAIL = 2, // Thumbnail ready FF_THUMNAIL = 2, // Thumbnail ready
FF_DOWNLOAD = 4, // Request download FF_DOWNLOAD = 4, // Request download
FF_DELETED = 8, // Request delete FF_DELETED = 8, // Request delete
FF_FETCH_MODEL = 16,// Request model
}; };
struct File struct File
@ -133,7 +135,9 @@ public:
void DownloadCancel(size_t index); void DownloadCancel(size_t index);
void FetchModel(size_t index, std::function<void(std::string const &)> callback); void FetchModel(size_t index, std::function<void(int, std::string const &)> callback);
void FetchModelCancel();
size_t GetCount() const; size_t GetCount() const;
@ -256,6 +260,10 @@ private:
void CancelRequest(boost::uint32_t seq); void CancelRequest(boost::uint32_t seq);
void CancelRequests(std::vector<boost::uint32_t> const &seqs);
void CancelRequests2(std::vector<boost::uint32_t> const & seqs);
void RecvMessageThread(); void RecvMessageThread();
void HandleResponse(boost::unique_lock<boost::mutex> &l, Bambu_Sample const &sample); void HandleResponse(boost::unique_lock<boost::mutex> &l, Bambu_Sample const &sample);
@ -297,6 +305,7 @@ private:
Session m_session; Session m_session;
boost::uint32_t m_sequence = 0; boost::uint32_t m_sequence = 0;
boost::uint32_t m_download_seq = 0; boost::uint32_t m_download_seq = 0;
boost::uint32_t m_fetch_model_seq = 0;
std::deque<std::string> m_messages; std::deque<std::string> m_messages;
std::deque<callback_t2> m_callbacks; std::deque<callback_t2> m_callbacks;
std::deque<callback_t2> m_notifies; std::deque<callback_t2> m_notifies;

View file

@ -223,9 +223,11 @@ bool ProgressDialog::Create(const wxString &title, const wxString &message, int
maximum /= m_factor; maximum /= m_factor;
#endif #endif
if (!HasPDFlag(wxPD_NO_PROGRESS)) {
m_gauge = new wxGauge(this, wxID_ANY, maximum, wxDefaultPosition, PROGRESSDIALOG_GAUGE_SIZE, gauge_style); m_gauge = new wxGauge(this, wxID_ANY, maximum, wxDefaultPosition, PROGRESSDIALOG_GAUGE_SIZE, gauge_style);
m_gauge->SetValue(0); m_gauge->SetValue(0);
m_sizer_main->Add(m_gauge, 0, wxEXPAND | wxLEFT | wxRIGHT, FromDIP(28)); m_sizer_main->Add(m_gauge, 0, wxEXPAND | wxLEFT | wxRIGHT, FromDIP(28));
}
#ifdef __WXMSW__ #ifdef __WXMSW__
//m_block_left = new wxWindow(m_gauge, wxID_ANY, wxPoint(0, 0), wxSize(FromDIP(2), PROGRESSDIALOG_GAUGE_SIZE.y * 2)); //m_block_left = new wxWindow(m_gauge, wxID_ANY, wxPoint(0, 0), wxSize(FromDIP(2), PROGRESSDIALOG_GAUGE_SIZE.y * 2));
@ -250,6 +252,7 @@ bool ProgressDialog::Create(const wxString &title, const wxString &message, int
DisableAbort(); DisableAbort();
m_button_cancel->Enable(false); m_button_cancel->Enable(false);
DisableSkip(); DisableSkip();
OnCancel();
m_timeStop = wxGetCurrentTime(); m_timeStop = wxGetCurrentTime();
} }
}); });
@ -509,13 +512,14 @@ bool ProgressDialog::Update(int value, const wxString &newmsg, bool *skip)
{ {
if (!DoBeforeUpdate(skip)) return false; if (!DoBeforeUpdate(skip)) return false;
wxCHECK_MSG(m_gauge, false, "dialog should be fully created"); wxCHECK_MSG(m_msg || m_gauge, false, "dialog should be fully created");
#ifdef __WXMSW__ #ifdef __WXMSW__
value /= m_factor; value /= m_factor;
#endif // __WXMSW__ #endif // __WXMSW__
wxASSERT_MSG(value <= m_maximum, wxT("invalid progress value")); wxASSERT_MSG(value <= m_maximum, wxT("invalid progress value"));
if (m_gauge)
m_gauge->SetValue(value); m_gauge->SetValue(value);
UpdateMessage(newmsg); UpdateMessage(newmsg);
@ -588,10 +592,10 @@ bool ProgressDialog::Pulse(const wxString &newmsg, bool *skip)
{ {
if (!DoBeforeUpdate(skip)) return false; if (!DoBeforeUpdate(skip)) return false;
wxCHECK_MSG(m_gauge, false, "dialog should be fully created"); wxCHECK_MSG(m_msg || m_gauge, false, "dialog should be fully created");
// show a bit of progress // show a bit of progress
m_gauge->Pulse(); if (m_gauge) m_gauge->Pulse();
UpdateMessage(newmsg); UpdateMessage(newmsg);
@ -732,6 +736,8 @@ void ProgressDialog::OnCancel(wxCommandEvent &event)
DisableAbort(); DisableAbort();
DisableSkip(); DisableSkip();
OnCancel();
// save the time when the dialog was stopped // save the time when the dialog was stopped
m_timeStop = wxGetCurrentTime(); m_timeStop = wxGetCurrentTime();
} }
@ -756,6 +762,7 @@ void ProgressDialog::OnClose(wxCloseEvent &event)
m_state = Canceled; m_state = Canceled;
DisableAbort(); DisableAbort();
DisableSkip(); DisableSkip();
OnCancel();
m_timeStop = wxGetCurrentTime(); m_timeStop = wxGetCurrentTime();
} }

View file

@ -19,6 +19,7 @@ class WXDLLIMPEXP_FWD_CORE wxWindowDisabler;
#define PROGRESSDIALOG_DEF_BK wxColour(255,255,255) #define PROGRESSDIALOG_DEF_BK wxColour(255,255,255)
#define PROGRESSDIALOG_GREY_700 wxColour(107,107,107) #define PROGRESSDIALOG_GREY_700 wxColour(107,107,107)
#define wxPD_NO_PROGRESS 0x0100
namespace Slic3r { namespace GUI { namespace Slic3r { namespace GUI {
@ -51,6 +52,8 @@ public:
virtual bool WasCancelled() const; virtual bool WasCancelled() const;
virtual bool WasSkipped() const; virtual bool WasSkipped() const;
virtual void OnCancel() {};
// Must provide overload to avoid hiding it (and warnings about it) // Must provide overload to avoid hiding it (and warnings about it)
virtual void Update() wxOVERRIDE { wxDialog::Update(); } virtual void Update() wxOVERRIDE { wxDialog::Update(); }