/** The Apply button is only enable when Selected / Unselected values are valid (only when checked), and at least one checkbox is selected. The Target Field is not empty, but has not been checked for validity. */ void SaveSelectionDlg::OnApplySaveClick( wxCommandEvent& event ) { int write_col = col_id_map[m_save_field_choice->GetSelection()]; bool sel_checked = m_sel_check_box->GetValue() == 1; bool unsel_checked = m_unsel_check_box->GetValue() == 1; double sel_c = 1; if (sel_checked) { wxString sel_c_str = m_sel_val_text->GetValue(); sel_c_str.Trim(false); sel_c_str.Trim(true); sel_c_str.ToDouble(&sel_c); } double unsel_c = 0; if (unsel_checked) { wxString unsel_c_str = m_unsel_val_text->GetValue(); unsel_c_str.Trim(false); unsel_c_str.Trim(true); unsel_c_str.ToDouble(&unsel_c); } int sf_tm = 0; if (grid_base->col_data[write_col]->time_steps > 1 && m_save_field_choice_tm) { sf_tm = m_save_field_choice_tm->GetSelection(); } std::vector<bool>& h = project->highlight_state->GetHighlight(); // write_col now refers to a valid field in grid base, so write out // results to that field. int obs = h.size(); DbfColContainer& cd = *grid_base->col_data[write_col]; std::vector<bool> undefined; if (cd.type == GeoDaConst::long64_type) { wxInt64 sel_c_i = sel_c; wxInt64 unsel_c_i = unsel_c; std::vector<wxInt64> t(grid_base->GetNumberRows()); cd.GetVec(t, sf_tm); cd.GetUndefined(undefined, sf_tm); if (sel_checked) { for (int i=0; i<obs; i++) { if (h[i]) { t[i] = sel_c_i; undefined[i] = false; } } } if (unsel_checked) { for (int i=0; i<obs; i++) { if (!h[i]) { t[i] = unsel_c_i; undefined[i] = false; } } } cd.SetFromVec(t, sf_tm); cd.SetUndefined(undefined, sf_tm); } else if (cd.type == GeoDaConst::double_type) { std::vector<double> t(grid_base->GetNumberRows()); cd.GetVec(t, sf_tm); cd.GetUndefined(undefined, sf_tm); if (sel_checked) { for (int i=0; i<obs; i++) { if (h[i]) { t[i] = sel_c; undefined[i] = false; } } } if (unsel_checked) { for (int i=0; i<obs; i++) { if (!h[i]) { t[i] = unsel_c; undefined[i] = false; } } } cd.SetFromVec(t, sf_tm); cd.SetUndefined(undefined, sf_tm); } else { wxString msg = "Chosen field is not a numeric type. This is likely "; msg << "a bug. Please report this."; wxMessageDialog dlg(this, msg, "Error", wxOK | wxICON_ERROR ); dlg.ShowModal(); return; } if (grid_base->GetView()) grid_base->GetView()->Refresh(); wxString msg = "Values assigned to target field successfully."; wxMessageDialog dlg(this, msg, "Success", wxOK | wxICON_INFORMATION ); dlg.ShowModal(); OnCancelClick(event); }
/** The Apply button is only enable when Selected / Unselected values are valid (only when checked), and at least one checkbox is selected. The Target Variable is not empty, but has not been checked for validity. */ void SaveSelectionDlg::OnApplySaveClick( wxCommandEvent& event ) { wxLogMessage("Click SaveSelectionDlg::OnApplySaveClick"); wxString field_name = m_save_sel_var_name->GetValue(); if (field_name.empty()) { wxMessageDialog dlg(this, _("Variable name can't be empty."), _("Error"), wxOK | wxICON_ERROR ); dlg.ShowModal(); return; } int col = table_int->FindColId(field_name); if ( col == wxNOT_FOUND) { // create a new integer field int col_insert_pos = table_int->GetNumberCols(); int time_steps = 1; int m_length_val = GdaConst::default_dbf_long_len; int m_decimals_val = 0; col = table_int->InsertCol(GdaConst::long64_type, field_name, col_insert_pos, time_steps, m_length_val, m_decimals_val); } if (col <= 0) { return; } int write_col = col; bool sel_checked = m_sel_check_box->GetValue() == 1; bool unsel_checked = m_unsel_check_box->GetValue() == 1; double sel_c = 1; if (sel_checked) { wxString sel_c_str = m_sel_val_text->GetValue(); sel_c_str.Trim(false); sel_c_str.Trim(true); sel_c_str.ToDouble(&sel_c); } double unsel_c = 0; if (unsel_checked) { wxString unsel_c_str = m_unsel_val_text->GetValue(); unsel_c_str.Trim(false); unsel_c_str.Trim(true); unsel_c_str.ToDouble(&unsel_c); } int sf_tm = 0; if (table_int->GetColTimeSteps(write_col) > 1 && m_save_field_choice_tm) { sf_tm = m_save_field_choice_tm->GetSelection(); } std::vector<bool>& h = project->GetHighlightState()->GetHighlight(); // write_col now refers to a valid field in grid base, so write out // results to that field. int obs = h.size(); std::vector<bool> undefined; if (table_int->GetColType(write_col) == GdaConst::long64_type) { wxInt64 sel_c_i = sel_c; wxInt64 unsel_c_i = unsel_c; std::vector<wxInt64> t(table_int->GetNumberRows()); table_int->GetColData(write_col, sf_tm, t); table_int->GetColUndefined(write_col, sf_tm, undefined); if (sel_checked) { for (int i=0; i<obs; i++) { if (h[i]) { t[i] = sel_c_i; undefined[i] = false; } } } if (unsel_checked) { for (int i=0; i<obs; i++) { if (!h[i]) { t[i] = unsel_c_i; undefined[i] = false; } } } table_int->SetColData(write_col, sf_tm, t); table_int->SetColUndefined(write_col, sf_tm, undefined); } else if (table_int->GetColType(write_col) == GdaConst::double_type) { std::vector<double> t(table_int->GetNumberRows()); table_int->GetColData(write_col, sf_tm, t); table_int->GetColUndefined(write_col, sf_tm, undefined); if (sel_checked) { for (int i=0; i<obs; i++) { if (h[i]) { t[i] = sel_c; undefined[i] = false; } } } if (unsel_checked) { for (int i=0; i<obs; i++) { if (!h[i]) { t[i] = unsel_c; undefined[i] = false; } } } table_int->SetColData(write_col, sf_tm, t); table_int->SetColUndefined(write_col, sf_tm, undefined); } else { wxString msg = _("Chosen field is not a numeric type. Please select a numeric type field."); wxMessageDialog dlg(this, msg, _("Error"), wxOK | wxICON_ERROR ); dlg.ShowModal(); return; } wxString msg = _("Values assigned to target field successfully."); wxMessageDialog dlg(this, msg, "Success", wxOK | wxICON_INFORMATION ); dlg.ShowModal(); OnCancelClick(event); }