Skip to content

Commit

Permalink
update fix of #1898
Browse files Browse the repository at this point in the history
Also fix another bug that when edit cell in table (only when using sqlite data source), the double precision value will be rounded to an integer number in the editing box.
  • Loading branch information
lixun910 committed Jun 17, 2019
1 parent 1bec812 commit 6cc7169
Show file tree
Hide file tree
Showing 11 changed files with 3,320 additions and 3,327 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*.gal
*.pyc

.vscode/
deps/

swig/*.gwt
Expand Down
121 changes: 44 additions & 77 deletions DataViewer/OGRColumn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <string.h>
#include <istream>
#include <time.h>
#include <sstream>
Expand All @@ -30,7 +31,7 @@
#include <wx/numformatter.h>

#include "../GenUtils.h"
#include "../GeoDa.h"
//#include "../GeoDa.h"
#include "../logger.h"
#include "../ShapeOperations/OGRDataAdapter.h"
#include "../GdaException.h"
Expand Down Expand Up @@ -789,125 +790,91 @@ OGRColumnString::~OGRColumnString()
// This column -> vector<double>
void OGRColumnString::FillData(vector<double>& data)
{
const char* thousand_sep = CPLGetConfigOption("GEODA_LOCALE_SEPARATOR", ",");
const char* decimal_sep = CPLGetConfigOption("GEODA_LOCALE_DECIMAL", ".");
bool use_custom_locale = false;
if ((strlen(thousand_sep) > 0 && strcmp(thousand_sep, ",") != 0) ||
(strlen(decimal_sep) > 0 && strcmp(decimal_sep, ".") != 0)) {
// customized locale numeric
use_custom_locale = true;
}

if (is_new) {
for (int i=0; i<rows; ++i) {
double val = 0.0;
if (!wxNumberFormatter::FromString(new_data[i], &val)) {
undef_markers[i] = true;
if (use_custom_locale) {
new_data[i].Replace(thousand_sep, "");
new_data[i].Replace(decimal_sep, ".");
}
wxNumberFormatter::FromString(new_data[i], &val);
data[i] = val;
}

} else {
int col_idx = GetColIndex();
wxString tmp;
char *old_locale, *saved_locale = 0;

for (int i=0; i<rows; ++i) {
if ( undef_markers[i] == true) {
data[i] = 0.0;
continue;
}
tmp = wxString(ogr_layer->data[i]->GetFieldAsString(col_idx));
double val;
if (tmp.IsEmpty()) {
data[i] = 0.0;
undef_markers[i] = true;
} else if (wxNumberFormatter::FromString(tmp, &val)) {
data[i] = val;
} else {
// try to use different locale
if (i==0) {
// get name of current locale
old_locale = setlocale(LC_NUMERIC, NULL);
// Copy the name so it won’t be clobbered by setlocale
saved_locale = strdup (old_locale);
// try comma as decimal point
setlocale(LC_NUMERIC, "de_DE");
}
double _val;
if (wxNumberFormatter::FromString(tmp, &_val)) {
data[i] = _val;
} else {
data[i] = 0.0;
undef_markers[i] = true;
}

if (use_custom_locale) {
tmp.Replace(thousand_sep, "");
tmp.Replace(decimal_sep, ".");
}
}
if (saved_locale) {
// restore locale
setlocale(LC_NUMERIC, saved_locale);
free(saved_locale);

double val = 0.0;
wxNumberFormatter::FromString(tmp, &val);
data[i] = val;
}
}
}

// This column -> vector<wxInt64>
void OGRColumnString::FillData(vector<wxInt64> &data)
{
const char* thousand_sep = CPLGetConfigOption("GEODA_LOCALE_SEPARATOR", ",");
const char* decimal_sep = CPLGetConfigOption("GEODA_LOCALE_DECIMAL", ".");
bool use_custom_locale = false;
if ((strlen(thousand_sep) > 0 && strcmp(thousand_sep, ",") != 0) ||
(strlen(decimal_sep) > 0 && strcmp(decimal_sep, ".") != 0)) {
// customized locale numeric
use_custom_locale = true;
}

if (is_new) {
for (int i=0; i<rows; ++i) {
wxInt64 val = 0;
if (!wxNumberFormatter::FromString(new_data[i], &val)) {
undef_markers[i] = true;
if (use_custom_locale) {
new_data[i].Replace(thousand_sep, "");
new_data[i].Replace(decimal_sep, ".");
}
wxNumberFormatter::FromString(new_data[i], &val);
data[i] = val;
}
} else {
int col_idx = GetColIndex();
bool conv_success = true;
wxString tmp;
char *old_locale, *saved_locale = 0;

for (int i=0; i<rows; ++i) {
if ( undef_markers[i] == true) {
data[i] = 0;
continue;
}
tmp = wxString(ogr_layer->data[i]->GetFieldAsString(col_idx));
wxInt64 val;
double val_d;

if (tmp.IsEmpty()) {
undef_markers[i] = true;
data[i] = 0;

} else if (wxNumberFormatter::FromString(tmp, &val)) {
data[i] = val;

} else if (wxNumberFormatter::FromString(tmp, &val_d)) {
val = static_cast<wxInt64>(val_d);
data[i] = val;

} else {
// try to use different locale
if (i==0) {
// get name of current locale
old_locale = setlocale(LC_NUMERIC, NULL);
// Copy the name so it won’t be clobbered by setlocale
saved_locale = strdup (old_locale);
// try comma as decimal point
setlocale(LC_NUMERIC, "de_DE");
}
wxInt64 val_;
double val_d_;
if (wxNumberFormatter::FromString(tmp, &val_)) {
data[i] = val_;

} else if (wxNumberFormatter::FromString(tmp, &val_d_)) {
val_ = static_cast<wxInt64>(val_d_);
data[i] = val_;

} else {
data[i] = 0;
undef_markers[i] = true;
}
wxInt64 val = 0;

if (use_custom_locale) {
tmp.Replace(thousand_sep, "");
tmp.Replace(decimal_sep, ".");
}
}
if (saved_locale) {
// restore locale
setlocale(LC_NUMERIC, saved_locale);
free(saved_locale);

wxNumberFormatter::FromString(tmp, &val);
data[i] = val;
}
}
}
Expand Down
20 changes: 16 additions & 4 deletions DataViewer/OGRTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1189,7 +1189,18 @@ bool OGRTable::RenameSimpleCol(int col, int time, const wxString& new_name)
return true;
}

wxString OGRTable::GetCellString(int row, int col, int time)
int OGRTable::GetCellStringLength(int row, int col, bool use_disp_decimal)
{
int str_length = 0;
int ts = GetColTimeSteps(col);
for (int t=0; t<ts; ++t) {
wxString val = GetCellString(row, col, t, use_disp_decimal);
if (val.length() > str_length) str_length = val.length();
}
return str_length;
}

wxString OGRTable::GetCellString(int row, int col, int time, bool use_disp_decimal)
{
// NOTE: if called from wxGrid, must use row_order[row] to permute
if (row < 0 || row >= rows) return wxEmptyString;
Expand All @@ -1198,15 +1209,16 @@ wxString OGRTable::GetCellString(int row, int col, int time)
cur_type == GdaConst::unknown_type) {
return wxEmptyString;
}
// get display number of decimals
int disp_dec = GetColDispDecimals(col);

// mapping col+time to underneath OGR col
OGRColumn* ogr_col = FindOGRColumn(col, time);
if (ogr_col == NULL) {
return "";
}
return ogr_col->GetValueAt(row, disp_dec, m_wx_encoding);
// get display number of decimals
int col_dec = GetColDecimals(col, time);
if (use_disp_decimal) col_dec = GetColDispDecimals(col);
return ogr_col->GetValueAt(row, col_dec, m_wx_encoding);
}


Expand Down
5 changes: 3 additions & 2 deletions DataViewer/OGRTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,9 @@ class OGRTable : public TableInterface, TableStateObserver
virtual bool ColChangeDisplayedDecimals(int col, int new_disp_dec);
virtual bool RenameGroup(int col, const wxString& new_name);
virtual bool RenameSimpleCol(int col, int time, const wxString& new_name);
virtual wxString GetCellString(int row, int col, int time=0);
virtual bool SetCellFromString(int row, int col, int time,
virtual wxString GetCellString(int row, int col, int time=0, bool use_disp_decimal=false);
virtual int GetCellStringLength(int row, int col, bool use_disp_decimal=true);
virtual bool SetCellFromString(int row, int col, int time,
const wxString &value);
virtual int InsertCol(GdaConst::FieldType type, const wxString& name,
int pos=-1, int time_steps=1,
Expand Down
29 changes: 10 additions & 19 deletions DataViewer/TableFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,27 +113,18 @@ TableFrame::TableFrame(wxFrame *parent, Project* project,
for (int i=0, iend=table_base->GetNumberCols(); i<iend; i++) {
double cur_col_size = grid->GetColSize(i);
double cur_lbl_len = grid->GetColLabelValue(i).length();
double max_cell_len = 0;
double max_cell_len = cur_lbl_len;
for (int j=0; j<sample-1; ++j) {
wxString cv = grid->GetCellValue(j, i);
cv.Trim(true);
cv.Trim(false);
if (cv.length() > max_cell_len) max_cell_len = cv.length();
}
if (max_cell_len > cur_lbl_len &&
max_cell_len >= 1 && cur_lbl_len >= 1) {
// attempt to scale up col width based on cur_col_size
double fac = max_cell_len / cur_lbl_len;
if (fac < 1) fac = 1;
if (fac > 5) fac = 5;
fac = fac * 1.2;
grid->SetColMinimalWidth(i, cur_col_size);
grid->SetColSize(i, cur_col_size * fac);
} else {
// add a few pixels of buffer to current label
grid->SetColMinimalWidth(i, cur_col_size+6);
grid->SetColSize(i, cur_col_size+6);
//wxString cv = grid->GetCellValue(j, i);
int cv_length = table_int->GetCellStringLength(j,i,true);
if (cv_length > max_cell_len) max_cell_len = cv_length;
}
// width (pixel) per number
double pw = 10;
grid->SetColMinimalWidth(i, cur_lbl_len * pw + 8);
// attempt to scale up col width based on cur_col_size
//double fac = 1.2;
grid->SetColSize(i, max_cell_len * pw);
}
grid->ForceRefresh();

Expand Down
6 changes: 4 additions & 2 deletions DataViewer/TableInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,10 @@ class TableInterface
const wxString& new_name) = 0;
/** wxGrid will call this function to fill data in displayed part
automatically. Returns formated string (e.g. nummeric numbers) */
virtual wxString GetCellString(int row, int col, int time=0) = 0;
/** Attempts to set the wxGrid cell from a user-entered value. Returns
virtual wxString GetCellString(int row, int col, int time=0, bool use_disp_decimal = false) = 0;
virtual int GetCellStringLength(int row, int col, bool use_disp_decimal=true) = 0;

/** Attempts to set the wxGrid cell from a user-entered value. Returns
true on success. If failured, then IsCellFromStringFail() returns false
and GetSetCellFromStringFailMsg() retuns a meaningful failure message
that can be displayed to the user. */
Expand Down
19 changes: 11 additions & 8 deletions DialogTools/LocaleSetupDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,15 @@ LocaleSetupDlg::LocaleSetupDlg(wxWindow* parent,
m_txt_thousands->SetMaxLength(1);
m_txt_decimal->SetMaxLength(1);

struct lconv *poLconv = localeconv();
wxString thousands_sep = poLconv->thousands_sep;
wxString decimal_point = poLconv->decimal_point;
const char* thousands_sep = CPLGetConfigOption("GEODA_LOCALE_SEPARATOR", ",");
const char* decimal_sep = CPLGetConfigOption("GEODA_LOCALE_DECIMAL", ".");

//struct lconv *poLconv = localeconv();
//wxString thousands_sep = poLconv->thousands_sep;
//wxString decimal_point = poLconv->decimal_point;

m_txt_thousands->SetValue(thousands_sep);
m_txt_decimal->SetValue(decimal_point);
m_txt_decimal->SetValue(decimal_sep);

SetParent(parent);
SetPosition(pos);
Expand All @@ -83,7 +86,7 @@ void LocaleSetupDlg::OnResetSysLocale( wxCommandEvent& event )
m_txt_thousands->SetValue(thousands_sep);
m_txt_decimal->SetValue(decimal_point);

wxString msg = _("Reset to system locale successfully. Please re-open current project with system locale.");
wxString msg = _("Reset to system locale successfully.");
wxMessageDialog msg_dlg(this, msg,
_("Reset to system locale information"),
wxOK | wxOK_DEFAULT | wxICON_INFORMATION);
Expand All @@ -98,14 +101,14 @@ void LocaleSetupDlg::OnOkClick( wxCommandEvent& event )
wxString thousands_sep = m_txt_thousands->GetValue();
wxString decimal_point = m_txt_decimal->GetValue();

CPLSetConfigOption("GDAL_LOCALE_SEPARATOR",
CPLSetConfigOption("GEODA_LOCALE_SEPARATOR",
(const char*)thousands_sep.mb_str());
if ( !decimal_point.IsEmpty() )
CPLSetConfigOption("GDAL_LOCALE_DECIMAL",
CPLSetConfigOption("GEODA_LOCALE_DECIMAL",
(const char*)decimal_point.mb_str());

if (need_reopen) {
wxString msg = _("Locale for numbers has been setup successfully. Please re-open current project to enable this locale.");
wxString msg = _("Locale for numbers has been setup successfully.");
wxMessageDialog msg_dlg(this, msg,
"Setup locale",
wxOK | wxOK_DEFAULT | wxICON_INFORMATION);
Expand Down
7 changes: 7 additions & 0 deletions ShapeOperations/OGRFieldProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,21 +73,28 @@ OGRFieldProxy::OGRFieldProxy(OGRFieldDefn *field_defn)

if (ogr_type == OFTString){
type = GdaConst::string_type;
if (length == 0) length = GdaConst::max_dbf_string_len;
}
else if (ogr_type == OFTInteger64 || ogr_type == OFTInteger) {
type = GdaConst::long64_type;
if (length == 0) length = GdaConst::max_dbf_long_len;
}
else if (ogr_type == OFTReal) {
type = GdaConst::double_type;
if (length == 0) length = GdaConst::max_dbf_long_len;
if (decimals == 0) decimals = GdaConst::max_dbf_double_decimals;
}
else if (ogr_type == OFTDate ) {
type = GdaConst::date_type;
if (length == 0) length = GdaConst::default_dbf_string_len;

} else if (ogr_type == OFTTime) {
type = GdaConst::time_type;
if (length == 0) length = GdaConst::default_dbf_string_len;

} else if (ogr_type == OFTDateTime) {
type = GdaConst::datetime_type;
if (length == 0) length = GdaConst::default_dbf_string_len;
}
}

Expand Down
Loading

0 comments on commit 6cc7169

Please sign in to comment.