Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Obico host option with custom printer dialog elements #1

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/libslic3r/PrintConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ static t_config_enum_values s_keys_map_PrintHostType {
{ "flashair", htFlashAir },
{ "astrobox", htAstroBox },
{ "repetier", htRepetier },
{ "mks", htMKS }
{ "mks", htMKS },
{ "obico", htObico }
};
CONFIG_OPTION_ENUM_DEFINE_STATIC_MAPS(PrintHostType)

Expand Down Expand Up @@ -2829,6 +2830,7 @@ def = this->add("filament_loading_speed", coFloats);
def->enum_values.push_back("astrobox");
def->enum_values.push_back("repetier");
def->enum_values.push_back("mks");
def->enum_values.push_back("obico");
def->enum_labels.push_back("PrusaLink");
def->enum_labels.push_back("PrusaConnect");
def->enum_labels.push_back("Octo/Klipper");
Expand All @@ -2837,6 +2839,7 @@ def = this->add("filament_loading_speed", coFloats);
def->enum_labels.push_back("AstroBox");
def->enum_labels.push_back("Repetier");
def->enum_labels.push_back("MKS");
def->enum_labels.push_back("Obico");
def->mode = comAdvanced;
def->cli = ConfigOptionDef::nocli;
def->set_default_value(new ConfigOptionEnum<PrintHostType>(htOctoPrint));
Expand Down
2 changes: 1 addition & 1 deletion src/libslic3r/PrintConfig.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ enum class FuzzySkinType {
};

enum PrintHostType {
htPrusaLink, htPrusaConnect, htOctoPrint, htDuet, htFlashAir, htAstroBox, htRepetier, htMKS
htPrusaLink, htPrusaConnect, htOctoPrint, htDuet, htFlashAir, htAstroBox, htRepetier, htMKS, htObico
};

enum AuthorizationType {
Expand Down
3 changes: 2 additions & 1 deletion src/slic3r/GUI/MainFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3573,7 +3573,8 @@ void MainFrame::load_printer_url()
wxString url = cfg.opt_string("print_host_webui").empty() ? cfg.opt_string("print_host") : cfg.opt_string("print_host_webui");
wxString apikey;
if (cfg.has("printhost_apikey") && (cfg.option<ConfigOptionEnum<PrintHostType>>("host_type")->value == htPrusaLink ||
cfg.option<ConfigOptionEnum<PrintHostType>>("host_type")->value == htPrusaConnect))
cfg.option<ConfigOptionEnum<PrintHostType>>("host_type")->value == htPrusaConnect ||
cfg.option<ConfigOptionEnum<PrintHostType>>("host_type")->value == htObico))
apikey = cfg.opt_string("printhost_apikey");
if (!url.empty()) {
if (!url.Lower().starts_with("http"))
Expand Down
38 changes: 32 additions & 6 deletions src/slic3r/GUI/PhysicalPrinterDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ void PhysicalPrinterDialog::build_printhost_settings(ConfigOptionsGroup* m_optgr
return sizer;
};

auto printhost_browse = [=](wxWindow* parent)
auto printhost_browse = [=](wxWindow* parent)
{
auto sizer = create_sizer_with_btn(parent, &m_printhost_browse_btn, "printer_host_browser", _L("Browse") + " " + dots);
m_printhost_browse_btn->Bind(wxEVT_BUTTON, [=](wxCommandEvent& e) {
Expand Down Expand Up @@ -177,6 +177,21 @@ void PhysicalPrinterDialog::build_printhost_settings(ConfigOptionsGroup* m_optgr
return sizer;
};

auto printhost_apikey_find = [=](wxWindow* parent) {
auto sizer = create_sizer_with_btn(parent, &m_printhost_apikey_get_btn, "printer_host_browser", _L("Get API Key"));

m_printhost_apikey_get_btn->Bind(wxEVT_BUTTON, [this](wxCommandEvent& e) {
std::unique_ptr<PrintHost> host(PrintHost::get_print_host(m_config));
if (!host) {
const wxString text = _L("Could not get a valid Printer Host reference");
show_error(this, text);
return;
}
wxLaunchDefaultBrowser(host->get_apikey_url(), wxBROWSER_NEW_WINDOW);
});
return sizer;
};

auto print_host_printers = [this, create_sizer_with_btn](wxWindow* parent) {
//add_scaled_button(parent, &m_printhost_port_browse_btn, "browse", _(L("Refresh Printers")), wxBU_LEFT | wxBU_EXACTFIT);
auto sizer = create_sizer_with_btn(parent, &m_printhost_port_browse_btn, "monitor_signal_strong", _(L("Refresh Printers")));
Expand All @@ -202,7 +217,9 @@ void PhysicalPrinterDialog::build_printhost_settings(ConfigOptionsGroup* m_optgr

option = m_optgroup->get_option("printhost_apikey");
option.opt.width = Field::def_width_wider();
m_optgroup->append_single_option_line(option);
Line apikey_line = m_optgroup->create_single_option_line(option);
apikey_line.append_widget(printhost_apikey_find);
m_optgroup->append_line(apikey_line);

option = m_optgroup->get_option("printhost_port");
option.opt.width = Field::def_width_wider();
Expand Down Expand Up @@ -245,7 +262,7 @@ void PhysicalPrinterDialog::build_printhost_settings(ConfigOptionsGroup* m_optgr
m_optgroup->append_line(cafile_hint);
}
else {

Line line{ "", "" };
line.full_width = 1;

Expand All @@ -264,7 +281,7 @@ void PhysicalPrinterDialog::build_printhost_settings(ConfigOptionsGroup* m_optgr
m_optgroup->append_line(line);
}

for (const std::string& opt_key : std::vector<std::string>{ "printhost_user", "printhost_password" }) {
for (const std::string& opt_key : std::vector<std::string>{ "printhost_user", "printhost_password" }) {
option = m_optgroup->get_option(opt_key);
option.opt.width = Field::def_width_wider();
m_optgroup->append_single_option_line(option);
Expand Down Expand Up @@ -317,6 +334,7 @@ void PhysicalPrinterDialog::update_printhost_buttons()
if (host) {
m_printhost_test_btn->Enable(!m_config->opt_string("print_host").empty() && host->can_test());
m_printhost_browse_btn->Enable(host->has_auto_discovery());
m_printhost_apikey_get_btn->Enable(!m_config->opt_string("print_host").empty());
}
}

Expand Down Expand Up @@ -405,6 +423,7 @@ void PhysicalPrinterDialog::update(bool printer_change)
const PrinterTechnology tech = Preset::printer_technology(*m_config);
// Only offer the host type selection for FFF, for SLA it's always the SL1 printer (at the moment)
bool supports_multiple_printers = false;
bool supports_apikey_find = false;
if (tech == ptFFF) {
update_host_type(printer_change);
const auto opt = m_config->option<ConfigOptionEnum<PrintHostType>>("host_type");
Expand All @@ -421,13 +440,13 @@ void PhysicalPrinterDialog::update(bool printer_change)
AuthorizationType auth_type = m_config->option<ConfigOptionEnum<AuthorizationType>>("printhost_authorization_type")->value;
m_optgroup->show_field("printhost_apikey", auth_type == AuthorizationType::atKeyPassword);
for (const char* opt_key : { "printhost_user", "printhost_password" })
m_optgroup->show_field(opt_key, auth_type == AuthorizationType::atUserPassword);
m_optgroup->show_field(opt_key, auth_type == AuthorizationType::atUserPassword);
} else {
m_optgroup->hide_field("printhost_authorization_type");
m_optgroup->show_field("printhost_apikey", true);
for (const std::string& opt_key : std::vector<std::string>{ "printhost_user", "printhost_password" })
m_optgroup->hide_field(opt_key);
supports_multiple_printers = opt && opt->value == htRepetier;
supports_multiple_printers = opt && (opt->value == htRepetier || opt->value == htObico);
if (opt->value == htPrusaConnect) { // automatically show default prusaconnect address
if (Field* printhost_field = m_optgroup->get_field("print_host"); printhost_field) {
if (wxTextCtrl* temp = dynamic_cast<wxTextCtrl*>(printhost_field->getWindow()); temp && temp->GetValue().IsEmpty()) {
Expand All @@ -436,6 +455,7 @@ void PhysicalPrinterDialog::update(bool printer_change)
}
}
}
supports_apikey_find = opt && opt->value == htObico;
}
else {
m_optgroup->set_value("host_type", int(PrintHostType::htOctoPrint), false);
Expand All @@ -452,6 +472,7 @@ void PhysicalPrinterDialog::update(bool printer_change)

m_optgroup->show_field("printhost_port", supports_multiple_printers);
m_printhost_port_browse_btn->Show(supports_multiple_printers);
m_printhost_apikey_get_btn->Show(supports_apikey_find);

update_preset_input();

Expand Down Expand Up @@ -530,6 +551,7 @@ void PhysicalPrinterDialog::on_sys_color_changed()
{
m_printhost_browse_btn->sys_color_changed();
m_printhost_test_btn->sys_color_changed();
m_printhost_apikey_get_btn->sys_color_changed();
if (m_printhost_cafile_browse_btn)
m_printhost_cafile_browse_btn->sys_color_changed();
}
Expand All @@ -539,6 +561,10 @@ void PhysicalPrinterDialog::OnOK(wxEvent& event)
{
wxGetApp().get_tab(Preset::TYPE_PRINTER)->save_preset("", false, false, true, m_preset_name );
event.Skip();

// reload tab
auto p_mainframe = wxGetApp().mainframe;
p_mainframe -> load_printer_url();
}

}} // namespace Slic3r::GUI
1 change: 1 addition & 0 deletions src/slic3r/GUI/PhysicalPrinterDialog.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class PhysicalPrinterDialog : public DPIDialog
ScalableButton* m_printhost_cafile_browse_btn {nullptr};
ScalableButton* m_printhost_client_cert_browse_btn {nullptr};
ScalableButton* m_printhost_port_browse_btn {nullptr};
ScalableButton* m_printhost_apikey_get_btn {nullptr};

RoundedRectangle* m_input_area {nullptr};
wxStaticText* m_valid_label {nullptr};
Expand Down
17 changes: 13 additions & 4 deletions src/slic3r/GUI/PrinterWebView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ void PrinterWebView::load_url(wxString& url, wxString apikey)
return;
m_apikey = apikey;
m_apikey_sent = false;

m_browser->LoadURL(url);
//m_browser->SetFocus();
UpdateState();
Expand All @@ -96,10 +96,20 @@ void PrinterWebView::OnClose(wxCloseEvent& evt)

void PrinterWebView::SendAPIKey()
{
if (m_apikey.IsEmpty()) {
m_browser->RemoveAllUserScripts();
}

if (m_apikey_sent || m_apikey.IsEmpty())
return;
m_apikey_sent = true;

m_apikey_sent = true;
wxString script = wxString::Format(R"(
document.addEventListener('DOMContentLoaded', () => {
const data = {'command': 'silent_login', 'token': '%s'};
window.postMessage(data, '*');
});

// Check if window.fetch exists before overriding
if (window.fetch) {
const originalFetch = window.fetch;
Expand All @@ -109,8 +119,7 @@ void PrinterWebView::SendAPIKey()
return originalFetch(input, init);
};
}
)",
m_apikey);
)", m_apikey, m_apikey);
m_browser->RemoveAllUserScripts();

m_browser->AddUserScript(script);
Expand Down
Loading