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.
This commit is contained in:
Vojtech Bubnik 2021-11-30 08:42:25 +01:00
parent f23c53ba17
commit 6d906fe993
10 changed files with 47 additions and 58 deletions

View File

@ -23,7 +23,6 @@
#include "GUI_App.hpp" #include "GUI_App.hpp"
#include "MsgDialog.hpp" #include "MsgDialog.hpp"
#include "I18N.hpp" #include "I18N.hpp"
#include "../Utils/PrintHost.hpp"
#include "MainFrame.hpp" #include "MainFrame.hpp"
#include "libslic3r/AppConfig.hpp" #include "libslic3r/AppConfig.hpp"
#include "NotificationManager.hpp" #include "NotificationManager.hpp"
@ -37,7 +36,7 @@ namespace GUI {
static const char *CONFIG_KEY_PATH = "printhost_path"; static const char *CONFIG_KEY_PATH = "printhost_path";
static const char *CONFIG_KEY_GROUP = "printhost_group"; static const char *CONFIG_KEY_GROUP = "printhost_group";
PrintHostSendDialog::PrintHostSendDialog(const fs::path &path, std::set<PrintHostPostUploadAction> post_actions, const wxArrayString &groups) PrintHostSendDialog::PrintHostSendDialog(const fs::path &path, PrintHostPostUploadActions post_actions, const wxArrayString &groups)
: MsgDialog(static_cast<wxWindow*>(wxGetApp().mainframe), _L("Send G-Code to printer host"), _L("Upload to Printer Host with the following filename:")) : MsgDialog(static_cast<wxWindow*>(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)) , txt_filename(new wxTextCtrl(this, wxID_ANY))
, combo_groups(!groups.IsEmpty() ? new wxComboBox(this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, groups, wxCB_READONLY) : nullptr) , combo_groups(!groups.IsEmpty() ? new wxComboBox(this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, groups, wxCB_READONLY) : nullptr)
@ -77,54 +76,46 @@ PrintHostSendDialog::PrintHostSendDialog(const fs::path &path, std::set<PrintHos
txt_filename->SetValue(recent_path); txt_filename->SetValue(recent_path);
txt_filename->SetFocus(); 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")); auto* btn_print = add_button(wxID_YES, false, _L("Upload and Print"));
btn_print->Bind(wxEVT_BUTTON, [this, suffix](wxCommandEvent&) { btn_print->Bind(wxEVT_BUTTON, [this, validate_path](wxCommandEvent&) {
wxString path = txt_filename->GetValue(); if (validate_path(txt_filename->GetValue())) {
// .gcode suffix control post_upload_action = PrintHostPostUploadAction::StartPrint;
if (!path.Lower().EndsWith(suffix.Lower())) EndDialog(wxID_OK);
{
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::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")); auto* btn_print = add_button(wxID_YES, false, _L("Upload and Simulate"));
btn_print->Bind(wxEVT_BUTTON, [this, suffix](wxCommandEvent&) { btn_print->Bind(wxEVT_BUTTON, [this, validate_path](wxCommandEvent&) {
wxString path = txt_filename->GetValue(); if (validate_path(txt_filename->GetValue())) {
// .gcode suffix control post_upload_action = PrintHostPostUploadAction::StartSimulation;
if (!path.Lower().EndsWith(suffix.Lower())) EndDialog(wxID_OK);
{
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);
});
} }
add_button(wxID_CANCEL); add_button(wxID_CANCEL);
if (auto* btn_ok = get_button(wxID_OK); btn_ok != NULL) { if (auto* btn_ok = get_button(wxID_OK); btn_ok != NULL) {
btn_ok->SetLabel(_L("Upload")); btn_ok->SetLabel(_L("Upload"));
btn_ok->Bind(wxEVT_BUTTON, [this, suffix](wxCommandEvent&) { btn_ok->Bind(wxEVT_BUTTON, [this, validate_path](wxCommandEvent&) {
wxString path = txt_filename->GetValue(); if (validate_path(txt_filename->GetValue())) {
// .gcode suffix control post_upload_action = PrintHostPostUploadAction::None;
if (!path.Lower().EndsWith(suffix.Lower())) EndDialog(wxID_OK);
{
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;
} }
EndDialog(wxID_OK);
}); });
} }
finalize(); finalize();

View File

@ -11,6 +11,7 @@
#include "GUI_Utils.hpp" #include "GUI_Utils.hpp"
#include "MsgDialog.hpp" #include "MsgDialog.hpp"
#include "../Utils/PrintHost.hpp"
class wxButton; class wxButton;
class wxTextCtrl; class wxTextCtrl;
@ -18,19 +19,14 @@ class wxChoice;
class wxComboBox; class wxComboBox;
class wxDataViewListCtrl; class wxDataViewListCtrl;
namespace Slic3r { namespace Slic3r {
struct PrintHostJob;
enum class PrintHostPostUploadAction;
namespace GUI { namespace GUI {
class PrintHostSendDialog : public GUI::MsgDialog class PrintHostSendDialog : public GUI::MsgDialog
{ {
public: public:
PrintHostSendDialog(const boost::filesystem::path &path, std::set<PrintHostPostUploadAction> post_actions, const wxArrayString& groups); PrintHostSendDialog(const boost::filesystem::path &path, PrintHostPostUploadActions post_actions, const wxArrayString& groups);
boost::filesystem::path filename() const; boost::filesystem::path filename() const;
PrintHostPostUploadAction post_action() const; PrintHostPostUploadAction post_action() const;
std::string group() const; std::string group() const;
@ -40,6 +36,7 @@ private:
wxTextCtrl *txt_filename; wxTextCtrl *txt_filename;
wxComboBox *combo_groups; wxComboBox *combo_groups;
PrintHostPostUploadAction post_upload_action; PrintHostPostUploadAction post_upload_action;
wxString m_valid_suffix;
}; };

View File

@ -26,7 +26,7 @@ public:
bool upload(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn) const override; bool upload(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn) const override;
bool has_auto_discovery() const override { return true; } bool has_auto_discovery() const override { return true; }
bool can_test() const override { return true; } bool can_test() const override { return true; }
std::set<PrintHostPostUploadAction> 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; } std::string get_host() const override { return host; }
protected: protected:

View File

@ -25,7 +25,7 @@ public:
bool upload(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn) const override; bool upload(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn) const override;
bool has_auto_discovery() const override { return false; } bool has_auto_discovery() const override { return false; }
bool can_test() const override { return true; } bool can_test() const override { return true; }
std::set<PrintHostPostUploadAction> 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; } std::string get_host() const override { return host; }
private: private:

View File

@ -26,7 +26,7 @@ public:
bool upload(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn) const override; bool upload(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn) const override;
bool has_auto_discovery() const override { return false; } bool has_auto_discovery() const override { return false; }
bool can_test() const override { return true; } bool can_test() const override { return true; }
std::set<PrintHostPostUploadAction> get_post_upload_actions() const {return std::set<PrintHostPostUploadAction>();} PrintHostPostUploadActions get_post_upload_actions() const { return PrintHostPostUploadAction::StartPrint; }
std::string get_host() const override { return host; } std::string get_host() const override { return host; }
private: private:

View File

@ -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%") BOOST_LOG_TRIVIAL(info) << boost::format("MKS: Uploading file %1%, filepath: %2%, print: %3%, command: %4%")
% upload_data.source_path % upload_data.source_path
% upload_data.upload_path % upload_data.upload_path
% upload_data.start_print % (upload_data.post_action == PrintHostPostUploadAction::StartPrint)
% upload_cmd; % upload_cmd;
auto http = Http::post(std::move(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)); error_fn(format_error(body, L("Unknown error occured"), 0));
res = false; res = false;
} }
else if (upload_data.start_print) { else if (upload_data.post_action == PrintHostPostUploadAction::StartPrint) {
wxString errormsg; wxString errormsg;
res = start_print(errormsg, upload_data.upload_path.string()); res = start_print(errormsg, upload_data.upload_path.string());
if (!res) { if (!res) {

View File

@ -25,7 +25,7 @@ public:
bool upload(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn) const override; bool upload(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn) const override;
bool has_auto_discovery() const override { return false; } bool has_auto_discovery() const override { return false; }
bool can_test() const override { return true; } 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; } std::string get_host() const override { return m_host; }
private: private:

View File

@ -28,7 +28,7 @@ public:
bool upload(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn) const override; bool upload(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn) const override;
bool has_auto_discovery() const override { return true; } bool has_auto_discovery() const override { return true; }
bool can_test() const override { return true; } bool can_test() const override { return true; }
std::set<PrintHostPostUploadAction> 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; } std::string get_host() const override { return m_host; }
const std::string& get_apikey() const { return m_apikey; } const std::string& get_apikey() const { return m_apikey; }
const std::string& get_cafile() const { return m_cafile; } const std::string& get_cafile() const { return m_cafile; }
@ -57,7 +57,7 @@ public:
wxString get_test_ok_msg() const override; wxString get_test_ok_msg() const override;
wxString get_test_failed_msg(wxString &msg) const override; wxString get_test_failed_msg(wxString &msg) const override;
std::set<PrintHostPostUploadAction> get_post_upload_actions() const override {return std::set<PrintHostPostUploadAction>();} PrintHostPostUploadActions get_post_upload_actions() const { return {}; }
protected: protected:
bool validate_version_text(const boost::optional<std::string> &version_text) const override; bool validate_version_text(const boost::optional<std::string> &version_text) const override;
@ -82,7 +82,7 @@ public:
wxString get_test_ok_msg() const override; wxString get_test_ok_msg() const override;
wxString get_test_failed_msg(wxString& msg) const override; wxString get_test_failed_msg(wxString& msg) const override;
std::set<PrintHostPostUploadAction> get_post_upload_actions() const {return std::set({PrintHostPostUploadAction::StartPrint});} PrintHostPostUploadActions get_post_upload_actions() const { return PrintHostPostUploadAction::StartPrint; }
protected: protected:
bool validate_version_text(const boost::optional<std::string>& version_text) const override; bool validate_version_text(const boost::optional<std::string>& version_text) const override;

View File

@ -9,6 +9,7 @@
#include <wx/string.h> #include <wx/string.h>
#include <libslic3r/enum_bitmask.hpp>
#include "Http.hpp" #include "Http.hpp"
class wxArrayString; class wxArrayString;
@ -22,6 +23,8 @@ enum class PrintHostPostUploadAction {
StartPrint, StartPrint,
StartSimulation StartSimulation
}; };
using PrintHostPostUploadActions = enum_bitmask<PrintHostPostUploadAction>;
ENABLE_ENUM_BITMASK_OPERATORS(PrintHostPostUploadAction);
struct PrintHostUpload struct PrintHostUpload
{ {
@ -30,10 +33,9 @@ struct PrintHostUpload
std::string group; std::string group;
PrintHostPostUploadAction post_action = PrintHostPostUploadAction::None; PrintHostPostUploadAction post_action { PrintHostPostUploadAction::None };
}; };
class PrintHost class PrintHost
{ {
public: public:
@ -50,7 +52,7 @@ public:
virtual bool upload(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn) const = 0; virtual bool upload(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn) const = 0;
virtual bool has_auto_discovery() const = 0; virtual bool has_auto_discovery() const = 0;
virtual bool can_test() const = 0; virtual bool can_test() const = 0;
virtual std::set<PrintHostPostUploadAction> 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. // A print host usually does not support multiple printers, with the exception of Repetier server.
virtual bool supports_multiple_printers() const { return false; } virtual bool supports_multiple_printers() const { return false; }
virtual std::string get_host() const = 0; virtual std::string get_host() const = 0;

View File

@ -7,7 +7,6 @@
#include "PrintHost.hpp" #include "PrintHost.hpp"
namespace Slic3r { namespace Slic3r {
class DynamicPrintConfig; class DynamicPrintConfig;
@ -27,7 +26,7 @@ public:
bool upload(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn) const override; bool upload(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn) const override;
bool has_auto_discovery() const override { return false; } bool has_auto_discovery() const override { return false; }
bool can_test() const override { return true; } bool can_test() const override { return true; }
std::set<PrintHostPostUploadAction> 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; } bool supports_multiple_printers() const override { return true; }
std::string get_host() const override { return host; } std::string get_host() const override { return host; }