Prusa Slicer 2.6.0
Loading...
Searching...
No Matches
Slic3r::BonjourDialog Class Reference

#include <src/slic3r/GUI/BonjourDialog.hpp>

+ Inheritance diagram for Slic3r::BonjourDialog:
+ Collaboration diagram for Slic3r::BonjourDialog:

Public Member Functions

 BonjourDialog (wxWindow *parent, Slic3r::PrinterTechnology)
 
 BonjourDialog (BonjourDialog &&)=delete
 
 BonjourDialog (const BonjourDialog &)=delete
 
BonjourDialogoperator= (BonjourDialog &&)=delete
 
BonjourDialogoperator= (const BonjourDialog &)=delete
 
 ~BonjourDialog ()
 
bool show_and_lookup ()
 
wxString get_selected () const
 

Private Member Functions

virtual void on_reply (BonjourReplyEvent &)
 
void on_timer (wxTimerEvent &)
 
void on_timer_process ()
 

Private Attributes

wxListView * list
 
std::unique_ptr< ReplySetreplies
 
wxStaticText * label
 
std::shared_ptr< Bonjourbonjour
 
std::unique_ptr< wxTimer > timer
 
unsigned timer_state
 
Slic3r::PrinterTechnology tech
 

Detailed Description

Constructor & Destructor Documentation

◆ BonjourDialog() [1/3]

Slic3r::BonjourDialog::BonjourDialog ( wxWindow *  parent,
Slic3r::PrinterTechnology  tech 
)
58 : wxDialog(parent, wxID_ANY, _(L("Network lookup")), wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER)
59 , list(new wxListView(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT|wxSIMPLE_BORDER))
60 , replies(new ReplySet)
61 , label(new wxStaticText(this, wxID_ANY, ""))
62 , timer(new wxTimer())
63 , timer_state(0)
64 , tech(tech)
65{
66 const int em = GUI::wxGetApp().em_unit();
67 list->SetMinSize(wxSize(80 * em, 30 * em));
68
69 wxBoxSizer *vsizer = new wxBoxSizer(wxVERTICAL);
70
71 vsizer->Add(label, 0, wxEXPAND | wxTOP | wxLEFT | wxRIGHT, em);
72
73 list->SetSingleStyle(wxLC_SINGLE_SEL);
74 list->SetSingleStyle(wxLC_SORT_DESCENDING);
75 list->AppendColumn(_(L("Address")), wxLIST_FORMAT_LEFT, 5 * em);
76 list->AppendColumn(_(L("Hostname")), wxLIST_FORMAT_LEFT, 10 * em);
77 list->AppendColumn(_(L("Service name")), wxLIST_FORMAT_LEFT, 20 * em);
78 if (tech == ptFFF) {
79 list->AppendColumn(_(L("OctoPrint version")), wxLIST_FORMAT_LEFT, 5 * em);
80 }
81
82 vsizer->Add(list, 1, wxEXPAND | wxALL, em);
83
84 wxBoxSizer *button_sizer = new wxBoxSizer(wxHORIZONTAL);
85 button_sizer->Add(new wxButton(this, wxID_OK, "OK"), 0, wxALL, em);
86 button_sizer->Add(new wxButton(this, wxID_CANCEL, "Cancel"), 0, wxALL, em);
87 // ^ Note: The Ok/Cancel labels are translated by wxWidgets
88
89 vsizer->Add(button_sizer, 0, wxALIGN_CENTER);
90 SetSizerAndFit(vsizer);
91
92 Bind(EVT_BONJOUR_REPLY, &BonjourDialog::on_reply, this);
93
94 Bind(EVT_BONJOUR_COMPLETE, [this](wxCommandEvent &) {
95 this->timer_state = 0;
96 });
97
98 Bind(wxEVT_TIMER, &BonjourDialog::on_timer, this);
99 GUI::wxGetApp().UpdateDlgDarkUI(this);
100}
void on_timer(wxTimerEvent &)
Definition BonjourDialog.cpp:218
Slic3r::PrinterTechnology tech
Definition BonjourDialog.hpp:46
std::unique_ptr< ReplySet > replies
Definition BonjourDialog.hpp:41
std::unique_ptr< wxTimer > timer
Definition BonjourDialog.hpp:44
wxStaticText * label
Definition BonjourDialog.hpp:42
virtual void on_reply(BonjourReplyEvent &)
Definition BonjourDialog.cpp:166
wxListView * list
Definition BonjourDialog.hpp:40
unsigned timer_state
Definition BonjourDialog.hpp:45
#define _(msgid)
Definition getopt.c:87
@ ptFFF
Definition Config.hpp:207
#define L(s)
Definition I18N.hpp:18

References _, L, label, list, on_reply(), on_timer(), Slic3r::ptFFF, tech, and timer_state.

+ Here is the call graph for this function:

◆ BonjourDialog() [2/3]

Slic3r::BonjourDialog::BonjourDialog ( BonjourDialog &&  )
delete

◆ BonjourDialog() [3/3]

Slic3r::BonjourDialog::BonjourDialog ( const BonjourDialog )
delete

◆ ~BonjourDialog()

Slic3r::BonjourDialog::~BonjourDialog ( )
103{
104 // Needed bacuse of forward defs
105}

Member Function Documentation

◆ get_selected()

wxString Slic3r::BonjourDialog::get_selected ( ) const
158{
159 auto sel = list->GetFirstSelected();
160 return sel >= 0 ? list->GetItemText(sel) : wxString();
161}

References list.

Referenced by Slic3r::GUI::PhysicalPrinterDialog::build_printhost_settings(), and on_reply().

+ Here is the caller graph for this function:

◆ on_reply()

void Slic3r::BonjourDialog::on_reply ( BonjourReplyEvent e)
privatevirtual
167{
168 if (replies->find(e.reply) != replies->end()) {
169 // We already have this reply
170 return;
171 }
172
173 // Filter replies based on selected technology
174 const auto model = e.reply.txt_data.find("model");
175 const bool sl1 = model != e.reply.txt_data.end() && model->second == "SL1";
176 if ((tech == ptFFF && sl1) || (tech == ptSLA && !sl1)) {
177 return;
178 }
179
180 replies->insert(std::move(e.reply));
181
182 auto selected = get_selected();
183
184 wxWindowUpdateLocker freeze_guard(this);
185 (void)freeze_guard;
186
187 list->DeleteAllItems();
188
189 // The whole list is recreated so that we benefit from it already being sorted in the set.
190 // (And also because wxListView's sorting API is bananas.)
191 for (const auto &reply : *replies) {
192 auto item = list->InsertItem(0, reply.full_address);
193 list->SetItem(item, 1, reply.hostname);
194 list->SetItem(item, 2, reply.service_name);
195
196 if (tech == ptFFF) {
197 const auto it = reply.txt_data.find("version");
198 if (it != reply.txt_data.end()) {
199 list->SetItem(item, 3, GUI::from_u8(it->second));
200 }
201 }
202 }
203
204 const int em = GUI::wxGetApp().em_unit();
205
206 for (int i = 0; i < list->GetColumnCount(); i++) {
207 list->SetColumnWidth(i, wxLIST_AUTOSIZE);
208 if (list->GetColumnWidth(i) < 10 * em) { list->SetColumnWidth(i, 10 * em); }
209 }
210
211 if (!selected.IsEmpty()) {
212 // Attempt to preserve selection
213 auto hit = list->FindItem(-1, selected);
214 if (hit >= 0) { list->SetItemState(hit, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED); }
215 }
216}
wxString get_selected() const
Definition BonjourDialog.cpp:157
typedef void(GLAPIENTRYP _GLUfuncptr)(void)
wxString from_u8(const std::string &str)
Definition GUI.cpp:437
@ ptSLA
Definition Config.hpp:209

References Slic3r::GUI::from_u8(), get_selected(), list, Slic3r::ptFFF, Slic3r::ptSLA, replies, Slic3r::BonjourReplyEvent::reply, tech, Slic3r::BonjourReply::txt_data, and void().

Referenced by BonjourDialog().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ on_timer()

void Slic3r::BonjourDialog::on_timer ( wxTimerEvent &  )
private
219{
221}
void on_timer_process()
Definition BonjourDialog.cpp:225

References on_timer_process().

Referenced by BonjourDialog().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ on_timer_process()

void Slic3r::BonjourDialog::on_timer_process ( )
private
226{
227 const auto search_str = _L("Searching for devices");
228
229 if (timer_state > 0) {
230 const std::string dots(timer_state, '.');
231 label->SetLabel(search_str + dots);
232 timer_state = (timer_state) % 3 + 1;
233 } else {
234 label->SetLabel(search_str + ": " + _L("Finished") + ".");
235 timer->Stop();
236 }
237}
#define _L(s)
Definition I18N.hpp:3

References _L, label, timer, and timer_state.

Referenced by on_timer(), and show_and_lookup().

+ Here is the caller graph for this function:

◆ operator=() [1/2]

BonjourDialog & Slic3r::BonjourDialog::operator= ( BonjourDialog &&  )
delete

◆ operator=() [2/2]

BonjourDialog & Slic3r::BonjourDialog::operator= ( const BonjourDialog )
delete

◆ show_and_lookup()

bool Slic3r::BonjourDialog::show_and_lookup ( )
108{
109 Show(); // Because we need GetId() to work before ShowModal()
110
111 timer->Stop();
112 timer->SetOwner(this);
113 timer_state = 1;
114 timer->Start(1000);
116
117 // The background thread needs to queue messages for this dialog
118 // and for that it needs a valid pointer to it (mandated by the wxWidgets API).
119 // Here we put the pointer under a shared_ptr and protect it by a mutex,
120 // so that both threads can access it safely.
121 auto dguard = std::make_shared<LifetimeGuard>(this);
122
123 // Note: More can be done here when we support discovery of hosts other than Octoprint and SL1
124 Bonjour::TxtKeys txt_keys { "version", "model" };
125
126 bonjour = Bonjour("octoprint")
127 .set_txt_keys(std::move(txt_keys))
128 .set_retries(3)
129 .set_timeout(4)
130 .on_reply([dguard](BonjourReply &&reply) {
131 std::lock_guard<std::mutex> lock_guard(dguard->mutex);
132 auto dialog = dguard->dialog;
133 if (dialog != nullptr) {
134 auto evt = new BonjourReplyEvent(EVT_BONJOUR_REPLY, dialog->GetId(), std::move(reply));
135 wxQueueEvent(dialog, evt);
136 }
137 })
138 .on_complete([dguard]() {
139 std::lock_guard<std::mutex> lock_guard(dguard->mutex);
140 auto dialog = dguard->dialog;
141 if (dialog != nullptr) {
142 auto evt = new wxCommandEvent(EVT_BONJOUR_COMPLETE, dialog->GetId());
143 wxQueueEvent(dialog, evt);
144 }
145 })
146 .lookup();
147
148 bool res = ShowModal() == wxID_OK && list->GetFirstSelected() >= 0;
149 {
150 // Tell the background thread the dialog is going away...
151 std::lock_guard<std::mutex> lock_guard(dguard->mutex);
152 dguard->dialog = nullptr;
153 }
154 return res;
155}
std::shared_ptr< Bonjour > bonjour
Definition BonjourDialog.hpp:43
std::set< std::string > TxtKeys
Definition Bonjour.hpp:57

References bonjour, list, Slic3r::Bonjour::on_reply(), on_timer_process(), Slic3r::Bonjour::set_retries(), Slic3r::Bonjour::set_timeout(), Slic3r::Bonjour::set_txt_keys(), timer, and timer_state.

Referenced by Slic3r::GUI::PhysicalPrinterDialog::build_printhost_settings().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Member Data Documentation

◆ bonjour

std::shared_ptr<Bonjour> Slic3r::BonjourDialog::bonjour
private

Referenced by show_and_lookup().

◆ label

wxStaticText* Slic3r::BonjourDialog::label
private

Referenced by BonjourDialog(), and on_timer_process().

◆ list

wxListView* Slic3r::BonjourDialog::list
private

◆ replies

std::unique_ptr<ReplySet> Slic3r::BonjourDialog::replies
private

Referenced by on_reply().

◆ tech

Slic3r::PrinterTechnology Slic3r::BonjourDialog::tech
private

Referenced by BonjourDialog(), and on_reply().

◆ timer

std::unique_ptr<wxTimer> Slic3r::BonjourDialog::timer
private

◆ timer_state

unsigned Slic3r::BonjourDialog::timer_state
private

The documentation for this class was generated from the following files: