From 6d906fe993ec1750c50b75da17b625e30aaddcb6 Mon Sep 17 00:00:00 2001 From: Vojtech Bubnik Date: Tue, 30 Nov 2021 08:42:25 +0100 Subject: [PATCH] Follow up to RepRapFirmware simulation: Use enum_bitmask instead of std::set for get_post_upload_actions(), a bit of clean-up to PrintHostDialog button callbacks. --- src/slic3r/GUI/PrintHostDialogs.cpp | 67 +++++++++++++---------------- src/slic3r/GUI/PrintHostDialogs.hpp | 9 ++-- src/slic3r/Utils/AstroBox.hpp | 2 +- src/slic3r/Utils/Duet.hpp | 2 +- src/slic3r/Utils/FlashAir.hpp | 2 +- src/slic3r/Utils/MKS.cpp | 4 +- src/slic3r/Utils/MKS.hpp | 2 +- src/slic3r/Utils/OctoPrint.hpp | 6 +-- src/slic3r/Utils/PrintHost.hpp | 8 ++-- src/slic3r/Utils/Repetier.hpp | 3 +- 10 files changed, 47 insertions(+), 58 deletions(-) diff --git a/src/slic3r/GUI/PrintHostDialogs.cpp b/src/slic3r/GUI/PrintHostDialogs.cpp index 82f7aeea8e..2cea580be3 100644 --- a/src/slic3r/GUI/PrintHostDialogs.cpp +++ b/src/slic3r/GUI/PrintHostDialogs.cpp @@ -23,7 +23,6 @@ #include "GUI_App.hpp" #include "MsgDialog.hpp" #include "I18N.hpp" -#include "../Utils/PrintHost.hpp" #include "MainFrame.hpp" #include "libslic3r/AppConfig.hpp" #include "NotificationManager.hpp" @@ -37,7 +36,7 @@ namespace GUI { static const char *CONFIG_KEY_PATH = "printhost_path"; static const char *CONFIG_KEY_GROUP = "printhost_group"; -PrintHostSendDialog::PrintHostSendDialog(const fs::path &path, std::set post_actions, const wxArrayString &groups) +PrintHostSendDialog::PrintHostSendDialog(const fs::path &path, PrintHostPostUploadActions post_actions, const wxArrayString &groups) : MsgDialog(static_cast(wxGetApp().mainframe), _L("Send G-Code to printer host"), _L("Upload to Printer Host with the following filename:")) , txt_filename(new wxTextCtrl(this, wxID_ANY)) , combo_groups(!groups.IsEmpty() ? new wxComboBox(this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, groups, wxCB_READONLY) : nullptr) @@ -77,55 +76,47 @@ PrintHostSendDialog::PrintHostSendDialog(const fs::path &path, std::setSetValue(recent_path); txt_filename->SetFocus(); - wxString suffix = recent_path.substr(recent_path.find_last_of('.')); + m_valid_suffix = recent_path.substr(recent_path.find_last_of('.')); + // .gcode suffix control + auto validate_path = [this](const wxString &path) -> bool { + if (! path.Lower().EndsWith(m_valid_suffix.Lower())) { + MessageDialog msg_wingow(this, wxString::Format(_L("Upload filename doesn't end with \"%s\". Do you wish to continue?"), m_valid_suffix), wxString(SLIC3R_APP_NAME), wxYES | wxNO); + if (msg_wingow.ShowModal() == wxID_NO) + return false; + } + return true; + }; - if (post_actions.find(PrintHostPostUploadAction::StartPrint) != post_actions.end()) { + if (post_actions.has(PrintHostPostUploadAction::StartPrint)) { auto* btn_print = add_button(wxID_YES, false, _L("Upload and Print")); - btn_print->Bind(wxEVT_BUTTON, [this, suffix](wxCommandEvent&) { - wxString path = txt_filename->GetValue(); - // .gcode suffix control - if (!path.Lower().EndsWith(suffix.Lower())) - { - MessageDialog msg_wingow(this, wxString::Format(_L("Upload filename doesn't end with \"%s\". Do you wish to continue?"), suffix), wxString(SLIC3R_APP_NAME), wxYES | wxNO); - if (msg_wingow.ShowModal() == wxID_NO) - return; + btn_print->Bind(wxEVT_BUTTON, [this, validate_path](wxCommandEvent&) { + if (validate_path(txt_filename->GetValue())) { + post_upload_action = PrintHostPostUploadAction::StartPrint; + EndDialog(wxID_OK); } - post_upload_action = PrintHostPostUploadAction::StartPrint; - EndDialog(wxID_OK); - }); + }); } - if (post_actions.find(PrintHostPostUploadAction::StartSimulation) != post_actions.end()) { + if (post_actions.has(PrintHostPostUploadAction::StartSimulation)) { auto* btn_print = add_button(wxID_YES, false, _L("Upload and Simulate")); - btn_print->Bind(wxEVT_BUTTON, [this, suffix](wxCommandEvent&) { - wxString path = txt_filename->GetValue(); - // .gcode suffix control - if (!path.Lower().EndsWith(suffix.Lower())) - { - MessageDialog msg_wingow(this, wxString::Format(_L("Upload filename doesn't end with \"%s\". Do you wish to continue?"), suffix), wxString(SLIC3R_APP_NAME), wxYES | wxNO); - if (msg_wingow.ShowModal() == wxID_NO) - return; - } - post_upload_action = PrintHostPostUploadAction::StartSimulation; - EndDialog(wxID_OK); - }); + btn_print->Bind(wxEVT_BUTTON, [this, validate_path](wxCommandEvent&) { + if (validate_path(txt_filename->GetValue())) { + post_upload_action = PrintHostPostUploadAction::StartSimulation; + EndDialog(wxID_OK); + } + }); } add_button(wxID_CANCEL); if (auto* btn_ok = get_button(wxID_OK); btn_ok != NULL) { btn_ok->SetLabel(_L("Upload")); - btn_ok->Bind(wxEVT_BUTTON, [this, suffix](wxCommandEvent&) { - wxString path = txt_filename->GetValue(); - // .gcode suffix control - if (!path.Lower().EndsWith(suffix.Lower())) - { - MessageDialog msg_wingow(this, wxString::Format(_L("Upload filename doesn't end with \"%s\". Do you wish to continue?"), suffix), wxString(SLIC3R_APP_NAME), wxYES | wxNO); - if (msg_wingow.ShowModal() == wxID_NO) - return; + btn_ok->Bind(wxEVT_BUTTON, [this, validate_path](wxCommandEvent&) { + if (validate_path(txt_filename->GetValue())) { + post_upload_action = PrintHostPostUploadAction::None; + EndDialog(wxID_OK); } - EndDialog(wxID_OK); - }); + }); } finalize(); diff --git a/src/slic3r/GUI/PrintHostDialogs.hpp b/src/slic3r/GUI/PrintHostDialogs.hpp index 2e167997f1..ff3eb60125 100644 --- a/src/slic3r/GUI/PrintHostDialogs.hpp +++ b/src/slic3r/GUI/PrintHostDialogs.hpp @@ -11,6 +11,7 @@ #include "GUI_Utils.hpp" #include "MsgDialog.hpp" +#include "../Utils/PrintHost.hpp" class wxButton; class wxTextCtrl; @@ -18,19 +19,14 @@ class wxChoice; class wxComboBox; class wxDataViewListCtrl; - namespace Slic3r { -struct PrintHostJob; -enum class PrintHostPostUploadAction; - namespace GUI { - class PrintHostSendDialog : public GUI::MsgDialog { public: - PrintHostSendDialog(const boost::filesystem::path &path, std::set post_actions, const wxArrayString& groups); + PrintHostSendDialog(const boost::filesystem::path &path, PrintHostPostUploadActions post_actions, const wxArrayString& groups); boost::filesystem::path filename() const; PrintHostPostUploadAction post_action() const; std::string group() const; @@ -40,6 +36,7 @@ private: wxTextCtrl *txt_filename; wxComboBox *combo_groups; PrintHostPostUploadAction post_upload_action; + wxString m_valid_suffix; }; diff --git a/src/slic3r/Utils/AstroBox.hpp b/src/slic3r/Utils/AstroBox.hpp index 1dbf59843c..ad656c4940 100644 --- a/src/slic3r/Utils/AstroBox.hpp +++ b/src/slic3r/Utils/AstroBox.hpp @@ -26,7 +26,7 @@ public: bool upload(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn) const override; bool has_auto_discovery() const override { return true; } bool can_test() const override { return true; } - std::set get_post_upload_actions() const {return std::set({PrintHostPostUploadAction::StartPrint});} + PrintHostPostUploadActions get_post_upload_actions() const { return PrintHostPostUploadAction::StartPrint; } std::string get_host() const override { return host; } protected: diff --git a/src/slic3r/Utils/Duet.hpp b/src/slic3r/Utils/Duet.hpp index 2b9bd209a1..ea007006c7 100644 --- a/src/slic3r/Utils/Duet.hpp +++ b/src/slic3r/Utils/Duet.hpp @@ -25,7 +25,7 @@ public: bool upload(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn) const override; bool has_auto_discovery() const override { return false; } bool can_test() const override { return true; } - std::set get_post_upload_actions() const {return std::set({PrintHostPostUploadAction::StartPrint, PrintHostPostUploadAction::StartSimulation});} + PrintHostPostUploadActions get_post_upload_actions() const { return PrintHostPostUploadAction::StartPrint | PrintHostPostUploadAction::StartSimulation; } std::string get_host() const override { return host; } private: diff --git a/src/slic3r/Utils/FlashAir.hpp b/src/slic3r/Utils/FlashAir.hpp index 5081db4064..b03a2c9e58 100644 --- a/src/slic3r/Utils/FlashAir.hpp +++ b/src/slic3r/Utils/FlashAir.hpp @@ -26,7 +26,7 @@ public: bool upload(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn) const override; bool has_auto_discovery() const override { return false; } bool can_test() const override { return true; } - std::set get_post_upload_actions() const {return std::set();} + PrintHostPostUploadActions get_post_upload_actions() const { return PrintHostPostUploadAction::StartPrint; } std::string get_host() const override { return host; } private: diff --git a/src/slic3r/Utils/MKS.cpp b/src/slic3r/Utils/MKS.cpp index 2188a8f682..80a79537d5 100644 --- a/src/slic3r/Utils/MKS.cpp +++ b/src/slic3r/Utils/MKS.cpp @@ -70,7 +70,7 @@ bool MKS::upload(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn er BOOST_LOG_TRIVIAL(info) << boost::format("MKS: Uploading file %1%, filepath: %2%, print: %3%, command: %4%") % upload_data.source_path % upload_data.upload_path - % upload_data.start_print + % (upload_data.post_action == PrintHostPostUploadAction::StartPrint) % upload_cmd; auto http = Http::post(std::move(upload_cmd)); @@ -85,7 +85,7 @@ bool MKS::upload(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn er error_fn(format_error(body, L("Unknown error occured"), 0)); res = false; } - else if (upload_data.start_print) { + else if (upload_data.post_action == PrintHostPostUploadAction::StartPrint) { wxString errormsg; res = start_print(errormsg, upload_data.upload_path.string()); if (!res) { diff --git a/src/slic3r/Utils/MKS.hpp b/src/slic3r/Utils/MKS.hpp index 7564b7f81b..37021dc6d0 100644 --- a/src/slic3r/Utils/MKS.hpp +++ b/src/slic3r/Utils/MKS.hpp @@ -25,7 +25,7 @@ public: bool upload(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn) const override; bool has_auto_discovery() const override { return false; } bool can_test() const override { return true; } - bool can_start_print() const override { return true; } + PrintHostPostUploadActions get_post_upload_actions() const { return PrintHostPostUploadAction::StartPrint; } std::string get_host() const override { return m_host; } private: diff --git a/src/slic3r/Utils/OctoPrint.hpp b/src/slic3r/Utils/OctoPrint.hpp index ffcda34a6d..14d53118a2 100644 --- a/src/slic3r/Utils/OctoPrint.hpp +++ b/src/slic3r/Utils/OctoPrint.hpp @@ -28,7 +28,7 @@ public: bool upload(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn) const override; bool has_auto_discovery() const override { return true; } bool can_test() const override { return true; } - std::set get_post_upload_actions() const {return std::set({PrintHostPostUploadAction::StartPrint});} + PrintHostPostUploadActions get_post_upload_actions() const { return PrintHostPostUploadAction::StartPrint; } std::string get_host() const override { return m_host; } const std::string& get_apikey() const { return m_apikey; } const std::string& get_cafile() const { return m_cafile; } @@ -57,7 +57,7 @@ public: wxString get_test_ok_msg() const override; wxString get_test_failed_msg(wxString &msg) const override; - std::set get_post_upload_actions() const override {return std::set();} + PrintHostPostUploadActions get_post_upload_actions() const { return {}; } protected: bool validate_version_text(const boost::optional &version_text) const override; @@ -82,7 +82,7 @@ public: wxString get_test_ok_msg() const override; wxString get_test_failed_msg(wxString& msg) const override; - std::set get_post_upload_actions() const {return std::set({PrintHostPostUploadAction::StartPrint});} + PrintHostPostUploadActions get_post_upload_actions() const { return PrintHostPostUploadAction::StartPrint; } protected: bool validate_version_text(const boost::optional& version_text) const override; diff --git a/src/slic3r/Utils/PrintHost.hpp b/src/slic3r/Utils/PrintHost.hpp index b71690478d..dd22e60b7d 100644 --- a/src/slic3r/Utils/PrintHost.hpp +++ b/src/slic3r/Utils/PrintHost.hpp @@ -9,6 +9,7 @@ #include +#include #include "Http.hpp" class wxArrayString; @@ -22,6 +23,8 @@ enum class PrintHostPostUploadAction { StartPrint, StartSimulation }; +using PrintHostPostUploadActions = enum_bitmask; +ENABLE_ENUM_BITMASK_OPERATORS(PrintHostPostUploadAction); struct PrintHostUpload { @@ -30,10 +33,9 @@ struct PrintHostUpload std::string group; - PrintHostPostUploadAction post_action = PrintHostPostUploadAction::None; + PrintHostPostUploadAction post_action { PrintHostPostUploadAction::None }; }; - class PrintHost { public: @@ -50,7 +52,7 @@ public: virtual bool upload(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn) const = 0; virtual bool has_auto_discovery() const = 0; virtual bool can_test() const = 0; - virtual std::set get_post_upload_actions() const = 0; + virtual PrintHostPostUploadActions get_post_upload_actions() const = 0; // A print host usually does not support multiple printers, with the exception of Repetier server. virtual bool supports_multiple_printers() const { return false; } virtual std::string get_host() const = 0; diff --git a/src/slic3r/Utils/Repetier.hpp b/src/slic3r/Utils/Repetier.hpp index 82ccb1f371..8b95b019dd 100644 --- a/src/slic3r/Utils/Repetier.hpp +++ b/src/slic3r/Utils/Repetier.hpp @@ -7,7 +7,6 @@ #include "PrintHost.hpp" - namespace Slic3r { class DynamicPrintConfig; @@ -27,7 +26,7 @@ public: bool upload(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn) const override; bool has_auto_discovery() const override { return false; } bool can_test() const override { return true; } - std::set get_post_upload_actions() const {return std::set({PrintHostPostUploadAction::StartPrint});} + PrintHostPostUploadActions get_post_upload_actions() const { return PrintHostPostUploadAction::StartPrint; } bool supports_multiple_printers() const override { return true; } std::string get_host() const override { return host; }