FIX: save page crash

1. auto pa calibrate : while save, vector overflow crash
2. adjust save name length limit

Change-Id: Iac901c9fcf90d2ac65211a913b4ab3425591c03a
This commit is contained in:
liz.li 2023-07-05 20:17:09 +08:00 committed by Lane.Wei
parent 0edb4476cf
commit bcae2a7298
2 changed files with 38 additions and 31 deletions

View file

@ -150,6 +150,10 @@ void CaliPASaveAutoPanel::sync_cali_result(const std::vector<PACalibResult>& cal
{ {
m_history_results = history_result; m_history_results = history_result;
m_calib_results.clear(); m_calib_results.clear();
for (auto& item : cali_result) {
if (item.confidence == 0)
m_calib_results[item.tray_id] = item;
}
m_grid_panel->DestroyChildren(); m_grid_panel->DestroyChildren();
auto grid_sizer = new wxBoxSizer(wxHORIZONTAL); auto grid_sizer = new wxBoxSizer(wxHORIZONTAL);
const int COLUMN_GAP = FromDIP(50); const int COLUMN_GAP = FromDIP(50);
@ -296,7 +300,7 @@ void CaliPASaveAutoPanel::sync_cali_result(const std::vector<PACalibResult>& cal
Layout(); Layout();
} }
void CaliPASaveAutoPanel::save_to_result_from_widgets(wxWindow* window, bool* out_is_valid) { void CaliPASaveAutoPanel::save_to_result_from_widgets(wxWindow* window, bool* out_is_valid, wxString* out_msg) {
if (!window) if (!window)
return; return;
@ -307,8 +311,7 @@ void CaliPASaveAutoPanel::save_to_result_from_widgets(wxWindow* window, bool* ou
if (input->get_type() == GridTextInputType::K) { if (input->get_type() == GridTextInputType::K) {
float k = 0.0f; float k = 0.0f;
if (!CalibUtils::validate_input_k_value(input->GetTextCtrl()->GetValue(), &k)) { if (!CalibUtils::validate_input_k_value(input->GetTextCtrl()->GetValue(), &k)) {
MessageDialog msg_dlg(nullptr, _L("Please input a valid value (K in 0~0.5)"), wxEmptyString, wxICON_WARNING | wxOK); *out_msg = _L("Please input a valid value (K in 0~0.5)");
msg_dlg.ShowModal();
*out_is_valid = false; *out_is_valid = false;
} }
m_calib_results[tray_id].k_value = k; m_calib_results[tray_id].k_value = k;
@ -322,28 +325,27 @@ void CaliPASaveAutoPanel::save_to_result_from_widgets(wxWindow* window, bool* ou
int tray_id = comboBox->get_col_idx(); int tray_id = comboBox->get_col_idx();
wxString name = comboBox->GetTextCtrl()->GetValue().ToStdString(); wxString name = comboBox->GetTextCtrl()->GetValue().ToStdString();
if (name.IsEmpty()) { if (name.IsEmpty()) {
MessageDialog msg_dlg(nullptr, _L("Please enter the name you want to save to printer."), wxEmptyString, wxICON_WARNING | wxOK); *out_msg = _L("Please enter the name you want to save to printer.");
msg_dlg.ShowModal(); *out_is_valid = false;
}
else if (name.Length() > 40) {
*out_msg = _L("The name cannot exceed 40 characters.");
*out_is_valid = false; *out_is_valid = false;
} }
//else if (name.Length() > 20) {
// MessageDialog msg_dlg(nullptr, _L("The name cannot exceed 20 characters."), wxEmptyString, wxICON_WARNING | wxOK);
// msg_dlg.ShowModal();
// *out_is_valid = false;
//}
m_calib_results[tray_id].name = name.ToStdString(); m_calib_results[tray_id].name = name.ToStdString();
} }
auto childern = window->GetChildren(); auto childern = window->GetChildren();
for (auto child : childern) { for (auto child : childern) {
save_to_result_from_widgets(child, out_is_valid); save_to_result_from_widgets(child, out_is_valid, out_msg);
} }
}; };
bool CaliPASaveAutoPanel::get_result(std::vector<PACalibResult>& out_result) { bool CaliPASaveAutoPanel::get_result(std::vector<PACalibResult>& out_result) {
bool is_valid = true; bool is_valid = true;
wxString err_msg;
// Check if the input value is valid and save to m_calib_results // Check if the input value is valid and save to m_calib_results
save_to_result_from_widgets(m_grid_panel, &is_valid); save_to_result_from_widgets(m_grid_panel, &is_valid, &err_msg);
if (is_valid) { if (is_valid) {
// Check for duplicate names // Check for duplicate names
struct PACalibResult { struct PACalibResult {
@ -353,8 +355,8 @@ bool CaliPASaveAutoPanel::get_result(std::vector<PACalibResult>& out_result) {
}; };
std::unordered_set<std::pair<std::string, std::string>, PACalibResult> set; std::unordered_set<std::pair<std::string, std::string>, PACalibResult> set;
for (auto& result : m_calib_results) { for (auto& result : m_calib_results) {
if (!set.insert({ result.name, result.filament_id }).second) { if (!set.insert({ result.second.name, result.second.filament_id }).second) {
MessageDialog msg_dlg(nullptr, _L("Only one of the results with the same name will be saved. Are you sure you want to overrides the other results?"), wxEmptyString, wxICON_WARNING | wxID_YES | wxID_NO); MessageDialog msg_dlg(nullptr, _L("Only one of the results with the same name will be saved. Are you sure you want to overrides the other results?"), wxEmptyString, wxICON_WARNING | wxYES_NO);
if (msg_dlg.ShowModal() != wxID_YES) { if (msg_dlg.ShowModal() != wxID_YES) {
return false; return false;
} }
@ -366,17 +368,21 @@ bool CaliPASaveAutoPanel::get_result(std::vector<PACalibResult>& out_result) {
// Check for duplicate names from history // Check for duplicate names from history
for (auto& result : m_history_results) { for (auto& result : m_history_results) {
if (!set.insert({ result.name, result.filament_id }).second) { if (!set.insert({ result.name, result.filament_id }).second) {
MessageDialog msg_dlg(nullptr, wxString::Format(_L("There is already a historical calibration result with the same name: %s. Only one of the results with the same name is saved. Are you sure you want to overrides the historical result?"), result.name), wxEmptyString, wxICON_WARNING | wxID_YES | wxID_NO); MessageDialog msg_dlg(nullptr, wxString::Format(_L("There is already a historical calibration result with the same name: %s. Only one of the results with the same name is saved. Are you sure you want to overrides the historical result?"), result.name), wxEmptyString, wxICON_WARNING | wxYES_NO);
if (msg_dlg.ShowModal() != wxID_YES) { if (msg_dlg.ShowModal() != wxID_YES) {
return false; return false;
} }
} }
} }
out_result = m_calib_results; for (auto& result : m_calib_results) {
out_result.push_back(result.second);
}
return true; return true;
} }
else { else {
MessageDialog msg_dlg(nullptr, err_msg, wxEmptyString, wxICON_WARNING | wxOK);
msg_dlg.ShowModal();
return false; return false;
} }
} }
@ -468,11 +474,11 @@ bool CaliPASaveManualPanel::get_result(PACalibResult& out_result) {
msg_dlg.ShowModal(); msg_dlg.ShowModal();
return false; return false;
} }
//else if (name.Length() > 20) { else if (name.Length() > 40) {
// MessageDialog msg_dlg(nullptr, _L("The name cannot exceed 20 characters."), wxEmptyString, wxICON_WARNING | wxOK); MessageDialog msg_dlg(nullptr, _L("The name cannot exceed 40 characters."), wxEmptyString, wxICON_WARNING | wxOK);
// msg_dlg.ShowModal(); msg_dlg.ShowModal();
// return false; return false;
//} }
out_result.k_value = k; out_result.k_value = k;
out_result.name = name.ToStdString(); out_result.name = name.ToStdString();
@ -943,7 +949,7 @@ void CalibrationFlowX1SavePage::sync_cali_result(const std::vector<FlowRatioCali
Layout(); Layout();
} }
void CalibrationFlowX1SavePage::save_to_result_from_widgets(wxWindow* window, bool* out_is_valid) void CalibrationFlowX1SavePage::save_to_result_from_widgets(wxWindow* window, bool* out_is_valid, wxString* out_msg)
{ {
if (!window) if (!window)
return; return;
@ -955,16 +961,14 @@ void CalibrationFlowX1SavePage::save_to_result_from_widgets(wxWindow* window, bo
if (input->get_type() == GridTextInputType::FlowRatio) { if (input->get_type() == GridTextInputType::FlowRatio) {
float flow_ratio = 0.0f; float flow_ratio = 0.0f;
if (!CalibUtils::validate_input_flow_ratio(input->GetTextCtrl()->GetValue(), &flow_ratio)) { if (!CalibUtils::validate_input_flow_ratio(input->GetTextCtrl()->GetValue(), &flow_ratio)) {
MessageDialog msg_dlg(nullptr, _L("Please input a valid value (0.0 < flow ratio < 2.0)"), wxEmptyString, wxICON_WARNING | wxOK); *out_msg = _L("Please input a valid value (0.0 < flow ratio < 2.0)");
msg_dlg.ShowModal();
*out_is_valid = false; *out_is_valid = false;
} }
m_save_results[tray_id].second = flow_ratio; m_save_results[tray_id].second = flow_ratio;
} }
else if (input->get_type() == GridTextInputType::Name) { else if (input->get_type() == GridTextInputType::Name) {
if (input->GetTextCtrl()->GetValue().IsEmpty()) { if (input->GetTextCtrl()->GetValue().IsEmpty()) {
MessageDialog msg_dlg(nullptr, _L("Please enter the name of the preset you want to save."), wxEmptyString, wxICON_WARNING | wxOK); *out_msg = _L("Please enter the name of the preset you want to save.");
msg_dlg.ShowModal();
*out_is_valid = false; *out_is_valid = false;
} }
m_save_results[tray_id].first = input->GetTextCtrl()->GetValue().ToStdString(); m_save_results[tray_id].first = input->GetTextCtrl()->GetValue().ToStdString();
@ -973,15 +977,16 @@ void CalibrationFlowX1SavePage::save_to_result_from_widgets(wxWindow* window, bo
auto childern = window->GetChildren(); auto childern = window->GetChildren();
for (auto child : childern) { for (auto child : childern) {
save_to_result_from_widgets(child, out_is_valid); save_to_result_from_widgets(child, out_is_valid, out_msg);
} }
} }
bool CalibrationFlowX1SavePage::get_result(std::vector<std::pair<wxString, float>>& out_results) bool CalibrationFlowX1SavePage::get_result(std::vector<std::pair<wxString, float>>& out_results)
{ {
bool is_valid = true; bool is_valid = true;
wxString err_msg;
// Check if the value is valid and save to m_calib_results // Check if the value is valid and save to m_calib_results
save_to_result_from_widgets(m_grid_panel, &is_valid); save_to_result_from_widgets(m_grid_panel, &is_valid, &err_msg);
if (is_valid) { if (is_valid) {
// obj->cali_result contain failure results, so use m_save_results to record value // obj->cali_result contain failure results, so use m_save_results to record value
for (auto& item : m_save_results) { for (auto& item : m_save_results) {
@ -990,6 +995,8 @@ bool CalibrationFlowX1SavePage::get_result(std::vector<std::pair<wxString, float
return true; return true;
} }
else { else {
MessageDialog msg_dlg(nullptr, err_msg, wxEmptyString, wxICON_WARNING | wxOK);
msg_dlg.ShowModal();
return false; return false;
} }
} }

View file

@ -92,7 +92,7 @@ public:
void set_machine_obj(MachineObject* obj) { m_obj = obj; } void set_machine_obj(MachineObject* obj) { m_obj = obj; }
void sync_cali_result(const std::vector<PACalibResult>& cali_result, const std::vector<PACalibResult>& history_result); void sync_cali_result(const std::vector<PACalibResult>& cali_result, const std::vector<PACalibResult>& history_result);
void save_to_result_from_widgets(wxWindow* window, bool* out_is_valid); void save_to_result_from_widgets(wxWindow* window, bool* out_is_valid, wxString* out_msg);
bool get_result(std::vector<PACalibResult>& out_result); bool get_result(std::vector<PACalibResult>& out_result);
bool is_all_failed() { return m_is_all_failed; } bool is_all_failed() { return m_is_all_failed; }
@ -101,7 +101,7 @@ protected:
wxPanel* m_complete_text_panel; wxPanel* m_complete_text_panel;
wxPanel* m_part_failed_panel; wxPanel* m_part_failed_panel;
wxPanel* m_grid_panel{ nullptr }; wxPanel* m_grid_panel{ nullptr };
std::vector<PACalibResult> m_calib_results; std::map<int, PACalibResult> m_calib_results;// map<tray_id, PACalibResult>
std::vector<PACalibResult> m_history_results; std::vector<PACalibResult> m_history_results;
bool m_is_all_failed{ true }; bool m_is_all_failed{ true };
MachineObject* m_obj; MachineObject* m_obj;
@ -198,7 +198,7 @@ public:
// sync widget value from cali flow rate result // sync widget value from cali flow rate result
void sync_cali_result(const std::vector<FlowRatioCalibResult>& cali_result); void sync_cali_result(const std::vector<FlowRatioCalibResult>& cali_result);
void save_to_result_from_widgets(wxWindow* window, bool* out_is_valid); void save_to_result_from_widgets(wxWindow* window, bool* out_is_valid, wxString* out_msg);
bool get_result(std::vector<std::pair<wxString, float>>& out_results); bool get_result(std::vector<std::pair<wxString, float>>& out_results);
bool is_all_failed() { return m_is_all_failed; } bool is_all_failed() { return m_is_all_failed; }