diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index 78a9df7d..2b43aaa4 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -5,7 +5,8 @@ "includePath": [ "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include", "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/14.0.3/include", - "${workspaceFolder}/**", + "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1", + "${workspaceFolder}", "/usr/local/include", "/usr/local/opt/boost/include", "/usr/local/opt/gdal/include", @@ -18,28 +19,10 @@ "macFrameworkPath": [ "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks" ], - "compilerPath": "/usr/bin/clang", "cStandard": "c17", "cppStandard": "c++17", "intelliSenseMode": "macos-clang-arm64", - "compilerPathInCppPropertiesJson": "/usr/bin/gcc", - "mergeConfigurations": false, - "browse": { - "path": [ - "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include", - "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/14.0.3/include", - "${workspaceFolder}/**", - "/usr/local/include", - "/usr/local/opt/boost/include", - "/usr/local/opt/gdal/include", - "/usr/local/opt/boost/include", - "${workspaceFolder}/BuildTools/macosx/libraries/include", - "${workspaceFolder}/BuildTools/macosx/libraries/include/wx-3.2", - "${workspaceFolder}/BuildTools/macosx/libraries/lib/wx/include/osx_cocoa-unicode-3.2", - "${workspaceFolder}" - ], - "limitSymbolsToIncludedHeaders": true - } + "compilerPath": "/usr/bin/gcc" }, { "name": "Linux", @@ -67,7 +50,6 @@ "-std=gnu++14" ], "cppStandard": "c++17", - "compilerPathInCppPropertiesJson": "/usr/bin/gcc", "cStandard": "c11", "mergeConfigurations": false, "browse": { diff --git a/.vscode/settings.json b/.vscode/settings.json index 4d6e4db9..d849728e 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -22,5 +22,6 @@ "editor.defaultFormatter": "ms-python.autopep8" }, "python.formatting.provider": "none", - "activestate.promptRuntimeCreation": false + "activestate.promptRuntimeCreation": false, + "C_Cpp.default.compilerPath": "/usr/bin/clang++" } diff --git a/DialogTools/ConnectDatasourceDlg.cpp b/DialogTools/ConnectDatasourceDlg.cpp index 5311e21a..8a7717c7 100644 --- a/DialogTools/ConnectDatasourceDlg.cpp +++ b/DialogTools/ConnectDatasourceDlg.cpp @@ -2,7 +2,7 @@ * GeoDa TM, Copyright (C) 2011-2015 by Luc Anselin - all rights reserved * * This file is part of GeoDa. - * + * * GeoDa is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or @@ -17,79 +17,71 @@ * along with this program. If not, see . */ -#include -#include -#include -#include -#include +#include "ConnectDatasourceDlg.h" + +#include +#include +#include +#include +#include +#include #include -#include #include +#include +#include #include -#include #include #include -#include -#include +#include #include -#include #include -#include -#include -#include +#include -#include -#include -#include +#include +#include +#include +#include +#include -#include "../DialogTools/CsvFieldConfDlg.h" #include "../DataViewer/DataSource.h" -#include "../ShapeOperations/OGRDataAdapter.h" -#include "../GenUtils.h" -#include "../logger.h" +#include "../DialogTools/CsvFieldConfDlg.h" +#include "../GdaCartoDB.h" #include "../GdaException.h" +#include "../GenUtils.h" #include "../GeneralWxUtils.h" -#include "../GdaCartoDB.h" #include "../GeoDa.h" #include "../Project.h" +#include "../ShapeOperations/OGRDataAdapter.h" +#include "../logger.h" #include "../rc/GeoDaIcon-16x16.xpm" -#include "ConnectDatasourceDlg.h" -DnDFile::DnDFile(ConnectDatasourceDlg *pOwner) -{ - m_pOwner = pOwner; -} +DnDFile::DnDFile(ConnectDatasourceDlg* pOwner) { m_pOwner = pOwner; } -DnDFile::~DnDFile() -{ - -} +DnDFile::~DnDFile() {} -bool DnDFile::OnDropFiles(wxCoord, wxCoord, const wxArrayString& filenames) -{ - size_t nFiles = filenames.GetCount(); - - if (m_pOwner != NULL && nFiles > 0) - { - wxString fpath = filenames[0]; - wxFileName fn = wxFileName::FileName(fpath); - - // activate file/dir access permission for osx 10.15+ - wxFileName dirname = wxFileName::DirName(fpath); - wxString dir_path = dirname.GetPath(); - wxDir dir; - dir.Open(dir_path); - if ( dir.IsOpened() ) { - wxString filename; - dir.GetFirst(&filename); - } +bool DnDFile::OnDropFiles(wxCoord, wxCoord, const wxArrayString& filenames) { + size_t nFiles = filenames.GetCount(); - m_pOwner->ds_file_path = fn; - wxCommandEvent ev; - m_pOwner->OnOkClick(ev); + if (m_pOwner != NULL && nFiles > 0) { + wxString fpath = filenames[0]; + wxFileName fn = wxFileName::FileName(fpath); + + // activate file/dir access permission for osx 10.15+ + wxFileName dirname = wxFileName::DirName(fpath); + wxString dir_path = dirname.GetPath(); + wxDir dir; + dir.Open(dir_path); + if (dir.IsOpened()) { + wxString filename; + dir.GetFirst(&filename); } - - return true; + + m_pOwner->ds_file_path = fn; + wxCommandEvent ev; + m_pOwner->OnOkClick(ev); + } + + return true; } //////////////////////////////////////////////////////////////////////////////// @@ -98,285 +90,252 @@ bool DnDFile::OnDropFiles(wxCoord, wxCoord, const wxArrayString& filenames) //////////////////////////////////////////////////////////////////////////////// const int RecentDatasource::N_MAX_ITEMS = 10; -const std::string RecentDatasource::KEY_NAME_IN_GDA_HISTORY = "recent_ds"; - -RecentDatasource::RecentDatasource() -{ - n_ds =0; - // get a latest input DB information - std::vector ds_infos = OGRDataAdapter::GetInstance().GetHistory(KEY_NAME_IN_GDA_HISTORY); - - if (ds_infos.size() > 0) { - ds_json_str = ds_infos[0]; - Init(ds_json_str); - } -} +const char RecentDatasource::KEY_NAME_IN_GDA_HISTORY[] = "recent_ds"; + +RecentDatasource::RecentDatasource() { + n_ds = 0; + // get a latest input DB information + std::vector ds_infos = OGRDataAdapter::GetInstance().GetHistory(KEY_NAME_IN_GDA_HISTORY); -RecentDatasource::~RecentDatasource() -{ - + if (ds_infos.size() > 0) { + ds_json_str = ds_infos[0]; + Init(ds_json_str); + } } -void RecentDatasource::Init(wxString json_str_) -{ - if (json_str_.IsEmpty()) - return; - - // "recent_ds" : [{"ds_name":"/data/test.shp", "layer_name":"test", "ds_config":"...", "thumb":"..."}, ] - std::wstring json_str = json_str_.ToStdWstring(); - json_spirit::wValue v; - - try { - if (!json_spirit::read(json_str, v)) { - throw GdaException("Could not parse recent ds string"); - } - - const json_spirit::wArray& ds_list = v.get_array(); - - n_ds = ds_list.size(); - - for (size_t i=0; iname_ == L"ds_name") { - val = i->value_; - ds_name = val.get_str(); - } - else if (i->name_ == "layer_name") { - val = i->value_; - layer_name = val.get_str(); - } - else if (i->name_ == "ds_config") { - val = i->value_; - ds_conf = val.get_str(); - } - else if (i->name_ == "ds_thumb") { - val = i->value_; - ds_thumb = val.get_str(); - } - } - ds_names.push_back(ds_name); - ds_layernames.push_back(layer_name); - ds_confs.push_back(ds_conf); - ds_thumbnails.push_back(ds_thumb); +RecentDatasource::~RecentDatasource() {} + +void RecentDatasource::Init(wxString json_str_) { + if (json_str_.IsEmpty()) return; + + // "recent_ds" : [{"ds_name":"/data/test.shp", "layer_name":"test", "ds_config":"...", "thumb":"..."}, ] + std::wstring json_str = json_str_.ToStdWstring(); + json_spirit::wValue v; + + try { + if (!json_spirit::read(json_str, v)) { + throw GdaException("Could not parse recent ds string"); + } + + const json_spirit::wArray& ds_list = v.get_array(); + + n_ds = ds_list.size(); + + for (size_t i = 0; i < n_ds; i++) { + const json_spirit::wObject& o = ds_list[i].get_obj(); + wxString ds_name, ds_conf, layer_name, ds_thumb; + + for (json_spirit::wObject::const_iterator i = o.begin(); i != o.end(); ++i) { + json_spirit::wValue val; + if (i->name_ == L"ds_name") { + val = i->value_; + ds_name = val.get_str(); + } else if (i->name_ == "layer_name") { + val = i->value_; + layer_name = val.get_str(); + } else if (i->name_ == "ds_config") { + val = i->value_; + ds_conf = val.get_str(); + } else if (i->name_ == "ds_thumb") { + val = i->value_; + ds_thumb = val.get_str(); } - } catch (std::runtime_error e) { - wxString msg; - msg << "Get Latest DB infor: JSON parsing failed: "; - msg << e.what(); - throw GdaException(msg.mb_str()); + } + ds_names.push_back(ds_name); + ds_layernames.push_back(layer_name); + ds_confs.push_back(ds_conf); + ds_thumbnails.push_back(ds_thumb); } + } catch (std::runtime_error e) { + wxString msg; + msg << "Get Latest DB infor: JSON parsing failed: "; + msg << e.what(); + throw GdaException(msg.mb_str()); + } } -void RecentDatasource::Save() -{ - // update ds_json_str from ds_names & ds_values - json_spirit::wArray ds_list_obj; - - for (int i=0; i= 0) { - ds_names.erase(ds_names.begin() + search_idx); - ds_confs.erase(ds_confs.begin() + search_idx); - ds_layernames.erase(ds_layernames.begin() + search_idx); - - wxString thumbnail_name = ds_thumbnails[search_idx]; - wxString file_path_str; - file_path_str << GenUtils::GetSamplesDir() << thumbnail_name; - - if (wxFileExists(file_path_str) ) { - wxRemoveFile(file_path_str); - } - - ds_thumbnails.erase(ds_thumbnails.begin() + search_idx); + } + + if (search_idx >= 0) { + ds_names.erase(ds_names.begin() + search_idx); + ds_confs.erase(ds_confs.begin() + search_idx); + ds_layernames.erase(ds_layernames.begin() + search_idx); + + wxString thumbnail_name = ds_thumbnails[search_idx]; + wxString file_path_str; + file_path_str << GenUtils::GetSamplesDir() << thumbnail_name; + + if (wxFileExists(file_path_str)) { + wxRemoveFile(file_path_str); } - + + ds_thumbnails.erase(ds_thumbnails.begin() + search_idx); + } + + n_ds = ds_names.size(); + + if (n_ds < N_MAX_ITEMS) { + ds_names.push_back(ds_name); + ds_confs.push_back(ds_conf); + ds_layernames.push_back(layer_name); + ds_thumbnails.push_back(ds_thumb); + n_ds = ds_names.size(); - - if (n_ds < N_MAX_ITEMS) { - ds_names.push_back(ds_name); - ds_confs.push_back(ds_conf); - ds_layernames.push_back(layer_name); - ds_thumbnails.push_back(ds_thumb); - - n_ds = ds_names.size(); - } else { - ds_names.erase(ds_names.begin()); - ds_confs.erase(ds_confs.begin()); - ds_layernames.erase(ds_layernames.begin()); - - wxString thumbnail_name = ds_thumbnails[0]; - wxString file_path_str; - file_path_str << GenUtils::GetSamplesDir() << thumbnail_name; - - if (wxFileExists(file_path_str) ) { - wxRemoveFile(file_path_str); - } - - ds_thumbnails.erase(ds_thumbnails.begin()); - - ds_names.push_back(ds_name); - ds_confs.push_back(ds_conf); - ds_layernames.push_back(layer_name); - ds_thumbnails.push_back(ds_thumb); + } else { + ds_names.erase(ds_names.begin()); + ds_confs.erase(ds_confs.begin()); + ds_layernames.erase(ds_layernames.begin()); + + wxString thumbnail_name = ds_thumbnails[0]; + wxString file_path_str; + file_path_str << GenUtils::GetSamplesDir() << thumbnail_name; + + if (wxFileExists(file_path_str)) { + wxRemoveFile(file_path_str); } - - Save(); -} -void RecentDatasource::Add(IDataSource* ds, const wxString& layer_name, wxString ds_thumb) -{ - wxString ds_name = ds->GetOGRConnectStr(); - wxString ds_conf = ds->GetJsonStr(); - - Add(ds_name, ds_conf, layer_name, ds_thumb); + ds_thumbnails.erase(ds_thumbnails.begin()); + + ds_names.push_back(ds_name); + ds_confs.push_back(ds_conf); + ds_layernames.push_back(layer_name); + ds_thumbnails.push_back(ds_thumb); + } + + Save(); } -void RecentDatasource::Delete(int idx) -{ - if (idx >= 0) { - ds_names.erase(ds_names.begin() + idx); - ds_confs.erase(ds_confs.begin() + idx); - ds_layernames.erase(ds_layernames.begin() + idx); - - wxString thumbnail_name = ds_thumbnails[idx]; - wxString file_path_str; - file_path_str << GenUtils::GetSamplesDir() << thumbnail_name; - - if (wxFileExists(file_path_str) ) { - wxRemoveFile(file_path_str); - } - - ds_thumbnails.erase(ds_thumbnails.begin() + idx); - - n_ds = ds_names.size(); - Save(); - } +void RecentDatasource::Add(IDataSource* ds, const wxString& layer_name, wxString ds_thumb) { + wxString ds_name = ds->GetOGRConnectStr(); + wxString ds_conf = ds->GetJsonStr(); + + Add(ds_name, ds_conf, layer_name, ds_thumb); } -void RecentDatasource::DeleteLastRecord() -{ - if (n_ds > 0) { - Delete(n_ds -1); +void RecentDatasource::Delete(int idx) { + if (idx >= 0) { + ds_names.erase(ds_names.begin() + idx); + ds_confs.erase(ds_confs.begin() + idx); + ds_layernames.erase(ds_layernames.begin() + idx); + + wxString thumbnail_name = ds_thumbnails[idx]; + wxString file_path_str; + file_path_str << GenUtils::GetSamplesDir() << thumbnail_name; + + if (wxFileExists(file_path_str)) { + wxRemoveFile(file_path_str); } -} -wxString RecentDatasource::GetLastIndex() -{ - int last_idx = ds_names.size() - 1; - if (last_idx < 0) - last_idx = 0; - wxString str; - str << last_idx; - return str; + ds_thumbnails.erase(ds_thumbnails.begin() + idx); + + n_ds = ds_names.size(); + Save(); + } } -wxString RecentDatasource::GetLastLayerName() -{ - int last_idx = ds_layernames.size() - 1; - if (last_idx < 0) - return ""; - wxString str; - str << ds_layernames[last_idx]; - return str; +void RecentDatasource::DeleteLastRecord() { + if (n_ds > 0) { + Delete(n_ds - 1); + } } -wxString RecentDatasource::GetLastDSName() -{ - int last_idx = ds_names.size() - 1; - if (last_idx < 0) - return ""; - wxString str; - str << ds_names[last_idx]; - return str; +wxString RecentDatasource::GetLastIndex() { + int last_idx = ds_names.size() - 1; + if (last_idx < 0) last_idx = 0; + wxString str; + str << last_idx; + return str; } -void RecentDatasource::UpdateLastThumb(wxString ds_thumb) -{ - int last_idx = ds_names.size() - 1; - if (last_idx >= 0) { - - ds_thumbnails[last_idx] = ds_thumb; - } - Save(); +wxString RecentDatasource::GetLastLayerName() { + int last_idx = ds_layernames.size() - 1; + if (last_idx < 0) return ""; + wxString str; + str << ds_layernames[last_idx]; + return str; } -void RecentDatasource::Clear() -{ - OGRDataAdapter::GetInstance().AddEntry(KEY_NAME_IN_GDA_HISTORY, ""); +wxString RecentDatasource::GetLastDSName() { + int last_idx = ds_names.size() - 1; + if (last_idx < 0) return ""; + wxString str; + str << ds_names[last_idx]; + return str; } -std::vector RecentDatasource::GetList() -{ - return ds_names; +void RecentDatasource::UpdateLastThumb(wxString ds_thumb) { + int last_idx = ds_names.size() - 1; + if (last_idx >= 0) { + ds_thumbnails[last_idx] = ds_thumb; + } + Save(); } -IDataSource* RecentDatasource::GetDatasource(wxString ds_name) -{ - for (int i=0; i RecentDatasource::GetList() { return ds_names; } + +IDataSource* RecentDatasource::GetDatasource(wxString ds_name) { + for (int i = 0; i < n_ds; i++) { + if (ds_names[i] == ds_name) { + wxString ds_conf = ds_confs[i]; + try { + return IDataSource::CreateDataSource(ds_confs[i]); + } catch (GdaException& e) { + return NULL; + } } - return NULL; + } + return NULL; } -wxString RecentDatasource::GetLayerName(wxString ds_name) -{ - for (int i=0; iShow(dialogType == 1); + m_encoding_lbl->Show(dialogType == 1); + } + + if (showRecentPanel) { + RecentDatasource recent_ds; + if (recent_ds.GetRecords() > 0) { + wxBoxSizer* sizer; + sizer = new wxBoxSizer(wxVERTICAL); - if (dialogType == 1) { - // in case of Merge, show encoding choice ctrl - m_encodings->Show(); - m_encoding_lbl->Show(); - } + InitRecentPanel(); + sizer->Add(scrl, 1, wxEXPAND | wxRIGHT, 5); - if (showRecentPanel) { - RecentDatasource recent_ds; - if (recent_ds.GetRecords() > 0) { - wxBoxSizer* sizer; - sizer = new wxBoxSizer( wxVERTICAL ); - - InitRecentPanel(); - sizer->Add( scrl, 1, wxEXPAND | wxRIGHT, 5 ); - - recent_panel->SetSizer( sizer ); - recent_panel->Layout(); - sizer->Fit( recent_panel ); - } - InitSamplePanel(); + recent_panel->SetSizer(sizer); + recent_panel->Layout(); + sizer->Fit(recent_panel); } + InitSamplePanel(); + } - m_dnd = new DnDFile(this); - m_drag_drop_box->SetDropTarget(m_dnd); - - SetIcon(wxIcon(GeoDaIcon_16x16_xpm)); - - Bind(wxEVT_COMMAND_MENU_SELECTED, &ConnectDatasourceDlg::BrowseDataSource, - this, GdaConst::ID_CONNECT_POPUP_MENU, - GdaConst::ID_CONNECT_POPUP_MENU + ds_names.Count()); - - Centre(); - Move(pos); - - GetSizer()->Fit(this); - Restore(); - Raise(); -} + m_dnd = new DnDFile(this); + m_drag_drop_box->SetDropTarget(m_dnd); -ConnectDatasourceDlg::~ConnectDatasourceDlg() -{ - if (datasource) { - delete datasource; - datasource = NULL; - } - if (m_wx_encoding) { - delete m_wx_encoding; - } + SetIcon(wxIcon(GeoDaIcon_16x16_xpm)); + + Bind(wxEVT_COMMAND_MENU_SELECTED, &ConnectDatasourceDlg::BrowseDataSource, this, GdaConst::ID_CONNECT_POPUP_MENU, + GdaConst::ID_CONNECT_POPUP_MENU + ds_names.Count()); + + Centre(); + Move(pos); + + GetSizer()->Fit(this); + Restore(); + Raise(); } +ConnectDatasourceDlg::~ConnectDatasourceDlg() { + if (datasource) { + delete datasource; + datasource = NULL; + } + if (m_wx_encoding) { + delete m_wx_encoding; + } +} +void ConnectDatasourceDlg::AddRecentItem(wxBoxSizer* sizer, wxScrolledWindow* scrl, wxString ds_name, + wxString ds_layername, wxString ds_thumb, int id) { + wxString file_path_str; + if (ds_thumb.IsEmpty()) { + ds_thumb = "no_map.png"; + } + file_path_str = GenUtils::GetSamplesDir() + ds_thumb; -void ConnectDatasourceDlg::AddRecentItem(wxBoxSizer* sizer, wxScrolledWindow* scrl, - wxString ds_name, wxString ds_layername, - wxString ds_thumb, int id) -{ - wxString file_path_str; - if (ds_thumb.IsEmpty()) { - ds_thumb = "no_map.png"; - } - file_path_str = GenUtils::GetSamplesDir() + ds_thumb; - - wxImage img; - if (!wxFileExists(file_path_str)) { + wxImage img; + if (!wxFileExists(file_path_str)) { #ifdef __linux__ - file_path_str = GenUtils::GetUserSamplesDir() + ds_thumb; - if (!wxFileExists(file_path_str)) { - ds_thumb = "no_map.png"; - file_path_str = GenUtils::GetSamplesDir() + ds_thumb; - } + file_path_str = GenUtils::GetUserSamplesDir() + ds_thumb; + if (!wxFileExists(file_path_str)) { + ds_thumb = "no_map.png"; + file_path_str = GenUtils::GetSamplesDir() + ds_thumb; + } #else - ds_thumb = "no_map.png"; - file_path_str = GenUtils::GetSamplesDir() + ds_thumb; + ds_thumb = "no_map.png"; + file_path_str = GenUtils::GetSamplesDir() + ds_thumb; #endif - } + } + img.LoadFile(file_path_str); + if (!img.IsOk()) { + ds_thumb = "no_map.png"; + file_path_str = GenUtils::GetSamplesDir() + ds_thumb; img.LoadFile(file_path_str); - if (!img.IsOk()) { - ds_thumb = "no_map.png"; - file_path_str = GenUtils::GetSamplesDir() + ds_thumb; - img.LoadFile(file_path_str); - } - img.Rescale(100,66,wxIMAGE_QUALITY_HIGH ); - wxBitmap bmp(img); - - wxBitmapButton* thumb; - thumb = new wxBitmapButton(scrl, id, bmp); - thumb->Bind(wxEVT_BUTTON, &ConnectDatasourceDlg::OnRecent, this); - - - wxBoxSizer* text_sizer; - text_sizer = new wxBoxSizer( wxVERTICAL ); - - wxString lbl_ds_layername = ds_layername; - lbl_ds_layername = GenUtils::PadTrim(lbl_ds_layername, 28, false); - - wxBoxSizer* title_sizer; - title_sizer = new wxBoxSizer( wxHORIZONTAL ); - wxStaticText* layername; - layername = new wxStaticText(scrl, wxID_ANY, lbl_ds_layername.Trim()); - layername->SetFont(*GdaConst::medium_font); - layername->SetToolTip(ds_layername); - layername->SetForegroundColour(wxColour(100,100,100)); - + } + img.Rescale(100, 66, wxIMAGE_QUALITY_HIGH); + wxBitmap bmp(img); + + wxBitmapButton* thumb; + thumb = new wxBitmapButton(scrl, id, bmp); + thumb->Bind(wxEVT_BUTTON, &ConnectDatasourceDlg::OnRecent, this); + + wxBoxSizer* text_sizer; + text_sizer = new wxBoxSizer(wxVERTICAL); + + wxString lbl_ds_layername = ds_layername; + lbl_ds_layername = GenUtils::PadTrim(lbl_ds_layername, 28, false); + + wxBoxSizer* title_sizer; + title_sizer = new wxBoxSizer(wxHORIZONTAL); + wxStaticText* layername; + layername = new wxStaticText(scrl, wxID_ANY, lbl_ds_layername.Trim()); + layername->SetFont(*GdaConst::medium_font); + layername->SetToolTip(ds_layername); + layername->SetForegroundColour(wxColour(100, 100, 100)); + #ifdef __WIN32__ - int pad_remove_btn = 10; - wxButton *remove = new wxButton(scrl, id, _("Delete"), wxDefaultPosition, wxSize(36,18), wxBORDER_NONE|wxBU_EXACTFIT); - remove->SetFont(*GdaConst::extra_small_font); + int pad_remove_btn = 10; + wxButton* remove = + new wxButton(scrl, id, _("Delete"), wxDefaultPosition, wxSize(36, 18), wxBORDER_NONE | wxBU_EXACTFIT); + remove->SetFont(*GdaConst::extra_small_font); #else - int pad_remove_btn = 0; - wxBitmap remove_bitmap(GdaConst::delete_icon_xpm); - wxBitmapButton* remove; - remove = new wxBitmapButton(scrl, id, remove_bitmap, wxDefaultPosition, wxDefaultSize, wxBORDER_NONE|wxBU_EXACTFIT); + int pad_remove_btn = 0; + wxBitmap remove_bitmap(GdaConst::delete_icon_xpm); + wxBitmapButton* remove; + remove = new wxBitmapButton(scrl, id, remove_bitmap, wxDefaultPosition, wxDefaultSize, wxBORDER_NONE | wxBU_EXACTFIT); #endif - remove->Bind(wxEVT_BUTTON, &ConnectDatasourceDlg::OnRecentDelete, this); - - title_sizer->Add(layername, 1, wxALIGN_LEFT |wxALIGN_CENTER_VERTICAL | wxRIGHT, pad_remove_btn); - title_sizer->Add(remove, 0, wxALIGN_LEFT |wxALIGN_CENTER_VERTICAL| wxALIGN_TOP | wxLEFT, 5); - - text_sizer->Add(title_sizer, 1, wxALIGN_LEFT | wxALL, 5); - - wxString lbl_ds_name = ds_name; - lbl_ds_name = GenUtils::PadTrim(lbl_ds_name, 50, false); - wxStaticText* filepath; - filepath = new wxStaticText(scrl, wxID_ANY, lbl_ds_name); - filepath->SetFont(*GdaConst::extra_small_font); - filepath->SetToolTip(ds_name); - filepath->SetForegroundColour(wxColour(70,70,70)); - text_sizer->Add(filepath, 1, wxALIGN_LEFT | wxALL, 10); - - - wxBoxSizer* row_sizer; - row_sizer = new wxBoxSizer( wxHORIZONTAL ); - row_sizer->Add(thumb, 0, wxALIGN_CENTER | wxALL, 0); - row_sizer->Add(text_sizer, 1, wxALIGN_LEFT | wxALIGN_TOP | wxEXPAND | wxTOP, 5); - - sizer->Add(row_sizer, 0, wxALIGN_LEFT | wxALL, 2); + remove->Bind(wxEVT_BUTTON, &ConnectDatasourceDlg::OnRecentDelete, this); + + title_sizer->Add(layername, 1, wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL | wxRIGHT, pad_remove_btn); + title_sizer->Add(remove, 0, wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL | wxALIGN_TOP | wxLEFT, 5); + + text_sizer->Add(title_sizer, 1, wxALIGN_LEFT | wxALL, 5); + + wxString lbl_ds_name = ds_name; + lbl_ds_name = GenUtils::PadTrim(lbl_ds_name, 50, false); + wxStaticText* filepath; + filepath = new wxStaticText(scrl, wxID_ANY, lbl_ds_name); + filepath->SetFont(*GdaConst::extra_small_font); + filepath->SetToolTip(ds_name); + filepath->SetForegroundColour(wxColour(70, 70, 70)); + text_sizer->Add(filepath, 1, wxALIGN_LEFT | wxALL, 10); + + wxBoxSizer* row_sizer; + row_sizer = new wxBoxSizer(wxHORIZONTAL); + row_sizer->Add(thumb, 0, wxALIGN_CENTER | wxALL, 0); + row_sizer->Add(text_sizer, 1, wxALIGN_LEFT | wxALIGN_TOP | wxEXPAND | wxTOP, 5); + + sizer->Add(row_sizer, 0, wxALIGN_LEFT | wxALL, 2); } -void ConnectDatasourceDlg::OnRecentDelete(wxCommandEvent& event) -{ - int xrcid = event.GetId(); - int recent_idx = xrcid - base_xrcid_recent_thumb; - - RecentDatasource recent_ds; - recent_ds.Delete(recent_idx); - - InitRecentPanel(); +void ConnectDatasourceDlg::OnRecentDelete(wxCommandEvent& event) { + int xrcid = event.GetId(); + int recent_idx = xrcid - base_xrcid_recent_thumb; + + RecentDatasource recent_ds; + recent_ds.Delete(recent_idx); + + InitRecentPanel(); } -void ConnectDatasourceDlg::OnRecent(wxCommandEvent& event) -{ - int xrcid = event.GetId(); - int recent_idx = xrcid - base_xrcid_recent_thumb; - - RecentDatasource recent_ds; - wxString ds_name = recent_ds.GetDSName(recent_idx); // UTF-8 decoded - - if (ds_name.EndsWith(".gda")) { - GdaFrame* gda_frame = GdaFrame::GetGdaFrame(); - gda_frame->OpenProject(ds_name); - Project* project = gda_frame->GetProject(); - wxString layer_name; - if (project) { - layer_name = project->layername; - } - recent_ds.Add(ds_name, ds_name, layer_name); - EndDialog(wxID_CANCEL); +void ConnectDatasourceDlg::OnRecent(wxCommandEvent& event) { + int xrcid = event.GetId(); + int recent_idx = xrcid - base_xrcid_recent_thumb; + + RecentDatasource recent_ds; + wxString ds_name = recent_ds.GetDSName(recent_idx); // UTF-8 decoded + + if (ds_name.EndsWith(".gda")) { + GdaFrame* gda_frame = GdaFrame::GetGdaFrame(); + gda_frame->OpenProject(ds_name); + Project* project = gda_frame->GetProject(); + wxString layer_name; + if (project) { + layer_name = project->layername; + } + recent_ds.Add(ds_name, ds_name, layer_name); + EndDialog(wxID_CANCEL); + } else { + // For csv file, if no csvt file, + // pop-up a field definition dialog and create a csvt file + if (ds_name.EndsWith("csv") && showCsvConfigure) { + if (wxFileExists(ds_name)) { + CsvFieldConfDlg csvDlg(this, ds_name, m_wx_encoding); + csvDlg.ShowModal(); + } + } + IDataSource* ds = recent_ds.GetDatasource(ds_name); + if (ds == NULL) { + // raise message dialog show can't connect to datasource + wxString msg = _("Can't connect to datasource: ") + ds_name; + wxMessageDialog dlg(this, msg, _("Error"), wxOK | wxICON_ERROR); + dlg.ShowModal(); + return; } else { - // For csv file, if no csvt file, - // pop-up a field definition dialog and create a csvt file - if (ds_name.EndsWith("csv") && showCsvConfigure) { - if (wxFileExists(ds_name)) { - CsvFieldConfDlg csvDlg(this, ds_name, m_wx_encoding); - csvDlg.ShowModal(); - } - } - IDataSource* ds = recent_ds.GetDatasource(ds_name); - if (ds == NULL) { - // raise message dialog show can't connect to datasource - wxString msg = _("Can't connect to datasource: ") + ds_name; - wxMessageDialog dlg (this, msg, _("Error"), wxOK | wxICON_ERROR); - dlg.ShowModal(); - return; - } else { - wxString layername = recent_ds.GetLayerName(ds_name); - SaveRecentDataSource(ds, layername); - layer_name = layername; - datasource = ds; - EndDialog(wxID_OK); - } + wxString layername = recent_ds.GetLayerName(ds_name); + SaveRecentDataSource(ds, layername); + layer_name = layername; + datasource = ds; + EndDialog(wxID_OK); } + } } -void ConnectDatasourceDlg::InitRecentPanel() -{ - if (scrl) - scrl->Destroy(); - - scrl = new wxScrolledWindow(recent_panel, wxID_ANY, wxDefaultPosition, - wxSize(420,200), wxVSCROLL ); - scrl->SetScrollRate( 5, 5 ); +void ConnectDatasourceDlg::InitRecentPanel() { + if (scrl) scrl->Destroy(); + + scrl = new wxScrolledWindow(recent_panel, wxID_ANY, wxDefaultPosition, wxSize(420, 200), wxVSCROLL); + scrl->SetScrollRate(5, 5); #ifdef __WIN32__ - scrl->SetBackgroundColour(*wxWHITE); + scrl->SetBackgroundColour(*wxWHITE); #endif - - wxBoxSizer* sizer; - sizer = new wxBoxSizer( wxVERTICAL ); - - RecentDatasource recent_ds; - int n_records = recent_ds.GetRecords(); - - if (n_records > 0) { - recent_nb->SetSelection(0); - } - for (int i=n_records-1; i>=0; i--) { - wxString ds_name = recent_ds.GetDSName(i); - wxString ds_layername = recent_ds.GetDSLayerName(i); - wxString ds_thumb = recent_ds.GetDSThumbnail(i); - AddRecentItem(sizer, scrl, ds_name, ds_layername, ds_thumb, base_xrcid_recent_thumb+i); - } - - scrl->SetSizer( sizer ); - scrl->Layout(); - sizer->Fit( scrl ); -} - -void ConnectDatasourceDlg::CreateControls() -{ - if (showRecentPanel) { - wxXmlResource::Get()->LoadDialog(this, GetParent(),"IDD_CONNECT_DATASOURCE"); - recent_nb = XRCCTRL(*this, "IDC_DS_LIST", wxNotebook); - recent_nb->SetSelection(1); - recent_panel = XRCCTRL(*this, "dsRecentListSizer", wxPanel); - smaples_panel = XRCCTRL(*this, "dsSampleList", wxPanel); - } else { - wxXmlResource::Get()->LoadDialog(this, GetParent(),"IDD_CONNECT_DATASOURCE_SIMPLE"); - m_encodings = XRCCTRL(*this, "IDC_CDS_ENCODING_CHOICE",wxChoice); - m_encoding_lbl = XRCCTRL(*this, "IDC_CDS_ENCODING_LABEL",wxStaticText); - } - - FindWindow(XRCID("wxID_OK"))->Enable(true); - // init db_table control that is unique in this class - m_drag_drop_box = XRCCTRL(*this, "IDC_DRAG_DROP_BOX",wxStaticBitmap); - m_webservice_url = XRCCTRL(*this, "IDC_CDS_WS_URL",AutoTextCtrl); - m_database_lookup_table = XRCCTRL(*this, "ID_BTN_LOOKUP_TABLE", wxBitmapButton); - m_database_lookup_table->Hide(); - //m_database_lookup_wslayer = XRCCTRL(*this, "ID_BTN_LOOKUP_WSLAYER", wxBitmapButton); - m_database_table = XRCCTRL(*this, "IDC_CDS_DB_TABLE", wxTextCtrl); - m_database_table->Hide(); // don't need this - - XRCCTRL(*this, "IDC_STATIC_DB_TABLE", wxStaticText)->Hide(); - - m_web_choice = XRCCTRL(*this, "ID_CDS_WEB_CHOICE", wxChoice); - - noshow_recent = XRCCTRL(*this, "IDC_NOSHOW_RECENT_SAMPLES", wxCheckBox); - noshow_recent->Bind(wxEVT_CHECKBOX, &ConnectDatasourceDlg::OnNoShowRecent, this); - if (!showRecentPanel) { - noshow_recent->Hide(); - } - - // create controls defined in parent class - DatasourceDlg::CreateControls(); + wxBoxSizer* sizer; + sizer = new wxBoxSizer(wxVERTICAL); + + RecentDatasource recent_ds; + int n_records = recent_ds.GetRecords(); + + if (n_records > 0) { + recent_nb->SetSelection(0); + } + for (int i = n_records - 1; i >= 0; i--) { + wxString ds_name = recent_ds.GetDSName(i); + wxString ds_layername = recent_ds.GetDSLayerName(i); + wxString ds_thumb = recent_ds.GetDSThumbnail(i); + AddRecentItem(sizer, scrl, ds_name, ds_layername, ds_thumb, base_xrcid_recent_thumb + i); + } + + scrl->SetSizer(sizer); + scrl->Layout(); + sizer->Fit(scrl); +} - // setup WSF auto-completion - std::vector ws_url_cands = OGRDataAdapter::GetInstance().GetHistory("ws_url"); - m_webservice_url->SetAutoList(ws_url_cands); +void ConnectDatasourceDlg::CreateControls() { + if (showRecentPanel) { + wxXmlResource::Get()->LoadDialog(this, GetParent(), "IDD_CONNECT_DATASOURCE"); + recent_nb = XRCCTRL(*this, "IDC_DS_LIST", wxNotebook); + recent_nb->SetSelection(1); + recent_panel = XRCCTRL(*this, "dsRecentListSizer", wxPanel); + smaples_panel = XRCCTRL(*this, "dsSampleList", wxPanel); + m_encodings = NULL; + m_encoding_lbl = NULL; + } else { + wxXmlResource::Get()->LoadDialog(this, GetParent(), "IDD_CONNECT_DATASOURCE_SIMPLE"); + m_encodings = XRCCTRL(*this, "IDC_CDS_ENCODING_CHOICE", wxChoice); + m_encoding_lbl = XRCCTRL(*this, "IDC_CDS_ENCODING_LABEL", wxStaticText); + } + + FindWindow(XRCID("wxID_OK"))->Enable(true); + // init db_table control that is unique in this class + m_drag_drop_box = XRCCTRL(*this, "IDC_DRAG_DROP_BOX", wxStaticBitmap); + m_webservice_url = XRCCTRL(*this, "IDC_CDS_WS_URL", AutoTextCtrl); + m_database_lookup_table = XRCCTRL(*this, "ID_BTN_LOOKUP_TABLE", wxBitmapButton); + m_database_lookup_table->Hide(); + // m_database_lookup_wslayer = XRCCTRL(*this, "ID_BTN_LOOKUP_WSLAYER", wxBitmapButton); + m_database_table = XRCCTRL(*this, "IDC_CDS_DB_TABLE", wxTextCtrl); + m_database_table->Hide(); // don't need this + + XRCCTRL(*this, "IDC_STATIC_DB_TABLE", wxStaticText)->Hide(); + + m_web_choice = XRCCTRL(*this, "ID_CDS_WEB_CHOICE", wxChoice); + + noshow_recent = XRCCTRL(*this, "IDC_NOSHOW_RECENT_SAMPLES", wxCheckBox); + noshow_recent->Bind(wxEVT_CHECKBOX, &ConnectDatasourceDlg::OnNoShowRecent, this); + noshow_recent->SetValue(!showRecentPanel); + + // create controls defined in parent class + DatasourceDlg::CreateControls(); + + // setup WSF auto-completion + std::vector ws_url_cands = OGRDataAdapter::GetInstance().GetHistory("ws_url"); + m_webservice_url->SetAutoList(ws_url_cands); } -void ConnectDatasourceDlg::OnNoShowRecent( wxCommandEvent& event) -{ - //recent_nb->Hide(); - //noshow_recent->Hide(); - //GetSizer()->Fit(this); - - showRecentPanel = false; - GdaConst::show_recent_sample_connect_ds_dialog = false; - OGRDataAdapter::GetInstance().AddEntry("show_recent_sample_connect_ds_dialog", "0"); +void ConnectDatasourceDlg::OnNoShowRecent(wxCommandEvent& event) { + // recent_nb->Hide(); + // noshow_recent->Hide(); + // GetSizer()->Fit(this); + + showRecentPanel = !noshow_recent->IsChecked(); + GdaConst::show_recent_sample_connect_ds_dialog = showRecentPanel; + OGRDataAdapter::GetInstance().AddEntry("show_recent_sample_connect_ds_dialog", + showRecentPanel ? GdaConst::gda_config_true : GdaConst::gda_config_false); } /** * This functions handles the event of user click the "lookup" button in * Web Service tab */ -void ConnectDatasourceDlg::OnLookupWSLayerBtn( wxCommandEvent& event ) -{ - //XXX handling invalid wfs url: - //http://walkableneighborhoods.org/geoserver/wfs - OnLookupDSTableBtn(event); +void ConnectDatasourceDlg::OnLookupWSLayerBtn(wxCommandEvent& event) { + // XXX handling invalid wfs url: + // http://walkableneighborhoods.org/geoserver/wfs + OnLookupDSTableBtn(event); } /** @@ -713,40 +656,36 @@ void ConnectDatasourceDlg::OnLookupWSLayerBtn( wxCommandEvent& event ) * When click "lookup" button in Datasource Table, this will call * PromptDSLayers() function */ -void ConnectDatasourceDlg::OnLookupDSTableBtn( wxCommandEvent& event ) -{ - try { - CreateDataSource(); - PromptDSLayers(datasource); - m_database_table->SetValue(layer_name ); - } catch (GdaException& e) { - wxString msg; - msg << e.what(); - wxMessageDialog dlg(this, msg , _("Error"), wxOK | wxICON_ERROR); - dlg.ShowModal(); - if( datasource!= NULL && - msg.StartsWith("Failed to open data source") ) { - if ( datasource->GetType() == GdaConst::ds_oci ){ - wxExecute("open http://geodacenter.github.io/setup-oracle.html"); - } else if ( datasource->GetType() == GdaConst::ds_esri_arc_sde ){ - wxExecute("open http://geodacenter.github.io/etup-arcsde.html"); - } else if ( datasource->GetType() == GdaConst::ds_esri_file_geodb ){ - wxExecute("open http://geodacenter.github.io/setup-esri-fgdb.html"); - } else { - wxExecute("open http://geodacenter.github.io/formats.html"); - } - } - } +void ConnectDatasourceDlg::OnLookupDSTableBtn(wxCommandEvent& event) { + try { + CreateDataSource(); + PromptDSLayers(datasource); + m_database_table->SetValue(layer_name); + } catch (GdaException& e) { + wxString msg; + msg << e.what(); + wxMessageDialog dlg(this, msg, _("Error"), wxOK | wxICON_ERROR); + dlg.ShowModal(); + if (datasource != NULL && msg.StartsWith("Failed to open data source")) { + if (datasource->GetType() == GdaConst::ds_oci) { + wxExecute("open http://geodacenter.github.io/setup-oracle.html"); + } else if (datasource->GetType() == GdaConst::ds_esri_arc_sde) { + wxExecute("open http://geodacenter.github.io/etup-arcsde.html"); + } else if (datasource->GetType() == GdaConst::ds_esri_file_geodb) { + wxExecute("open http://geodacenter.github.io/setup-esri-fgdb.html"); + } else { + wxExecute("open http://geodacenter.github.io/formats.html"); + } + } + } } -void ConnectDatasourceDlg::OnLookupCartoDBTableBtn( wxCommandEvent& event ) -{ - try { - CreateDataSource(); - PromptDSLayers(datasource); - } catch(GdaException& e) { - - } +void ConnectDatasourceDlg::OnLookupCartoDBTableBtn(wxCommandEvent& event) { + try { + CreateDataSource(); + PromptDSLayers(datasource); + } catch (GdaException& e) { + } } /** @@ -755,115 +694,110 @@ void ConnectDatasourceDlg::OnLookupCartoDBTableBtn( wxCommandEvent& event ) * then create a Project() that will be used by the * main program. */ -void ConnectDatasourceDlg::OnOkClick( wxCommandEvent& event ) -{ - wxLogMessage("Entering ConnectDatasourceDlg::OnOkClick"); - try { - // Open GeoDa project file direclty - if (ds_file_path.GetExt().Lower() == "gda") { - if (dialogType == 1) { - // in Merge/Stack (when dialogType == 1), user can't open gda file - wxString msg = _("Please open a data file rather than a project file (*.gda)."); - wxMessageDialog dlg(this, msg, _("Info"), wxOK | wxICON_ERROR); - dlg.ShowModal(); - return; - } - GdaFrame* gda_frame = GdaFrame::GetGdaFrame(); - if (gda_frame) { - gda_frame->OpenProject(ds_file_path.GetFullPath()); - //wxLogMessage("%s", _("Open project file:") + ds_file_path.GetFullPath()); - try { - Project* project = gda_frame->GetProject(); - wxString layer_name; - if (project) layer_name = project->layername; - - RecentDatasource recent_ds; - recent_ds.Add(ds_file_path.GetFullPath(), ds_file_path.GetFullPath(), layer_name); - } catch( GdaException ex) { - wxLogMessage("Open project error."); - } - EndDialog(wxID_CANCEL); - } - return; +void ConnectDatasourceDlg::OnOkClick(wxCommandEvent& event) { + wxLogMessage("Entering ConnectDatasourceDlg::OnOkClick"); + try { + // Open GeoDa project file direclty + if (ds_file_path.GetExt().Lower() == "gda") { + if (dialogType == 1) { + // in Merge/Stack (when dialogType == 1), user can't open gda file + wxString msg = _("Please open a data file rather than a project file (*.gda)."); + wxMessageDialog dlg(this, msg, _("Info"), wxOK | wxICON_ERROR); + dlg.ShowModal(); + return; + } + GdaFrame* gda_frame = GdaFrame::GetGdaFrame(); + if (gda_frame) { + gda_frame->OpenProject(ds_file_path.GetFullPath()); + // wxLogMessage("%s", _("Open project file:") + ds_file_path.GetFullPath()); + try { + Project* project = gda_frame->GetProject(); + wxString layer_name; + if (project) layer_name = project->layername; + + RecentDatasource recent_ds; + recent_ds.Add(ds_file_path.GetFullPath(), ds_file_path.GetFullPath(), layer_name); + } catch (GdaException ex) { + wxLogMessage("Open project error."); } + EndDialog(wxID_CANCEL); + } + return; + } - if (dialogType == 1) { - // case of Merge - m_wx_encoding = GetEncoding(); - } + if (dialogType == 1) { + // case of Merge + m_wx_encoding = GetEncoding(); + } + + // For csv file, if no csvt file, + // pop-up a field definition dialog and create a csvt file + if (ds_file_path.GetExt().Lower() == "csv" && showCsvConfigure) { + wxString csv_path = ds_file_path.GetFullPath(); + CsvFieldConfDlg csvDlg(this, csv_path, m_wx_encoding); + csvDlg.ShowModal(); + } + + CreateDataSource(); - // For csv file, if no csvt file, - // pop-up a field definition dialog and create a csvt file - if (ds_file_path.GetExt().Lower() == "csv" && showCsvConfigure) { - wxString csv_path = ds_file_path.GetFullPath(); - CsvFieldConfDlg csvDlg(this, csv_path, m_wx_encoding); - csvDlg.ShowModal(); + // Check to make sure to get a layer name + wxString layername; + int datasource_type = m_ds_notebook->GetSelection(); + if (datasource_type == 0) { + // File table is selected + + if (wxDirExists(ds_file_path.GetFullPath())) { + // dra-n-drop a directory + PromptDSLayers(datasource); + layername = layer_name; + } else { + if (layer_name.IsEmpty()) { + layername = ds_file_path.GetName(); + } else { + // user may select a layer name from Popup dialog that displays + // all layer names, see PromptDSLayers() + layername = layer_name; } - - CreateDataSource(); - - // Check to make sure to get a layer name - wxString layername; - int datasource_type = m_ds_notebook->GetSelection(); - if (datasource_type == 0) { - // File table is selected - - if ( wxDirExists(ds_file_path.GetFullPath()) ) { - // dra-n-drop a directory - PromptDSLayers(datasource); - layername = layer_name; - } else { - if (layer_name.IsEmpty()) { - layername = ds_file_path.GetName(); - } else { - // user may select a layer name from Popup dialog that displays - // all layer names, see PromptDSLayers() - layername = layer_name; - } - } - - } else if (datasource_type == 1) { - // Database tab is selected - layername = m_database_table->GetValue(); - if (layername.IsEmpty()) PromptDSLayers(datasource); - layername = layer_name; - - } else if (datasource_type == 2) { - // Web Service tab is selected - if (layer_name.IsEmpty()) PromptDSLayers(datasource); - layername = layer_name; - - } else { - // Should never be here - return; - } - - if (layername.IsEmpty()) - return; - - // At this point, there is a valid datasource and layername. - if (layer_name.IsEmpty()) - layer_name = layername; - - //wxLogMessage("%s", _("Open Datasource:") + datasource->ToString()); - //wxLogMessage("%s", _("Open Layer:") + layername); - - SaveRecentDataSource(datasource, layer_name); - - EndDialog(wxID_OK); - - } catch (GdaException& e) { - wxString msg; - msg << e.what(); - wxMessageDialog dlg(this, msg, _("Error"), wxOK | wxICON_ERROR); - dlg.ShowModal(); - - } catch (...) { - wxString msg = _("Unknow exception. Please contact GeoDa support."); - wxMessageDialog dlg(this, msg , _("Error"), wxOK | wxICON_ERROR); - dlg.ShowModal(); - } - wxLogMessage("Exiting ConnectDatasourceDlg::OnOkClick"); + } + + } else if (datasource_type == 1) { + // Database tab is selected + layername = m_database_table->GetValue(); + if (layername.IsEmpty()) PromptDSLayers(datasource); + layername = layer_name; + + } else if (datasource_type == 2) { + // Web Service tab is selected + if (layer_name.IsEmpty()) PromptDSLayers(datasource); + layername = layer_name; + + } else { + // Should never be here + return; + } + + if (layername.IsEmpty()) return; + + // At this point, there is a valid datasource and layername. + if (layer_name.IsEmpty()) layer_name = layername; + + // wxLogMessage("%s", _("Open Datasource:") + datasource->ToString()); + // wxLogMessage("%s", _("Open Layer:") + layername); + + SaveRecentDataSource(datasource, layer_name); + + EndDialog(wxID_OK); + } catch (GdaException& e) { + wxString msg; + msg << e.what(); + wxMessageDialog dlg(this, msg, _("Error"), wxOK | wxICON_ERROR); + dlg.ShowModal(); + } catch (...) { + wxString msg = _("Unknow exception. Please contact GeoDa support."); + wxMessageDialog dlg(this, msg, _("Error"), wxOK | wxICON_ERROR); + dlg.ShowModal(); + } + wxLogMessage("Exiting ConnectDatasourceDlg::OnOkClick"); } /** @@ -871,378 +805,363 @@ void ConnectDatasourceDlg::OnOkClick( wxCommandEvent& event ) * inputs * Throw GdaException() */ -IDataSource* ConnectDatasourceDlg::CreateDataSource() -{ - wxLogMessage("ConnectDatasourceDlg::CreateDataSource()"); - if (datasource) { - delete datasource; - datasource = NULL; - } - - int datasource_type = m_ds_notebook->GetSelection(); - - if (datasource_type == 0) { - // File tab selected - wxString fn = ds_file_path.GetFullPath(); - if (fn.IsEmpty()) { - throw GdaException( - wxString("Please select a datasource file.").mb_str()); - } +IDataSource* ConnectDatasourceDlg::CreateDataSource() { + wxLogMessage("ConnectDatasourceDlg::CreateDataSource()"); + if (datasource) { + delete datasource; + datasource = NULL; + } + + int datasource_type = m_ds_notebook->GetSelection(); + + if (datasource_type == 0) { + // File tab selected + wxString fn = ds_file_path.GetFullPath(); + if (fn.IsEmpty()) { + throw GdaException(wxString("Please select a datasource file.").mb_str()); + } #if defined(_WIN64) || defined(__amd64__) - if (m_ds_filepath_txt->GetValue().StartsWith("PGeo:")) { - fn = "PGeo:" + fn; - } + if (m_ds_filepath_txt->GetValue().StartsWith("PGeo:")) { + fn = "PGeo:" + fn; + } #endif - datasource = new FileDataSource(fn); - - // a special case: sqlite is a file based database, so we need to get - // avalible layers and prompt for user selecting - if (datasource->GetType() == GdaConst::ds_sqlite || - datasource->GetType() == GdaConst::ds_gpkg|| - datasource->GetType() == GdaConst::ds_osm || - datasource->GetType() == GdaConst::ds_esri_personal_gdb|| - datasource->GetType() == GdaConst::ds_esri_file_geodb) - { - PromptDSLayers(datasource); - if (layer_name.IsEmpty()) { - throw GdaException( - wxString("Layer/Table name could not be empty. Please select a layer/table.").mb_str()); - } - } - - } else if (datasource_type == 1) { - // Database tab selected - //int cur_sel = m_database_type->GetSelection(); - wxString cur_sel = m_database_type->GetStringSelection(); - wxString dbname = m_database_name->GetValue().Trim(); - wxString dbhost = m_database_host->GetValue().Trim(); - wxString dbport = m_database_port->GetValue().Trim(); - wxString dbuser = m_database_uname->GetValue().Trim(); - wxString dbpwd = m_database_upwd->GetValue().Trim(); - - GdaConst::DataSourceType ds_type = GdaConst::ds_unknown; - if (cur_sel == DBTYPE_ORACLE) ds_type = GdaConst::ds_oci; - else if (cur_sel == DBTYPE_ARCSDE) ds_type = GdaConst::ds_esri_arc_sde; - else if (cur_sel == DBTYPE_POSTGIS) ds_type = GdaConst::ds_postgresql; - else if (cur_sel == DBTYPE_MYSQL) ds_type = GdaConst::ds_mysql; - //else if (cur_sel == 4) ds_type = GdaConst::ds_ms_sql; - else { - wxString msg = _("The selected database driver is not supported on this platform. Please check GeoDa website for more information about database support and connection."); - throw GdaException(msg.mb_str()); - } - - // save user inputs to history table - OGRDataAdapter& ogr_adapter = OGRDataAdapter::GetInstance(); - if (!dbhost.IsEmpty()) - ogr_adapter.AddHistory("db_host", dbhost.ToStdString()); - - if (!dbname.IsEmpty()) - ogr_adapter.AddHistory("db_name", dbname.ToStdString()); - - if (!dbport.IsEmpty()) - ogr_adapter.AddHistory("db_port", dbport.ToStdString()); - - if (!dbuser.IsEmpty()) - ogr_adapter.AddHistory("db_user", dbuser.ToStdString()); - - // check if empty, prompt user to input - wxRegEx regex; - regex.Compile("[0-9]+"); - if (!regex.Matches( dbport )){ - wxString msg = _("Database port is empty. Please input one."); - throw GdaException(msg.mb_str()); - } - wxString error_msg; - if (dbhost.IsEmpty()) error_msg = _("Please input database host."); - else if (dbname.IsEmpty()) error_msg = _("Please input database name."); - else if (dbport.IsEmpty()) error_msg = _("Please input database port."); - else if (dbuser.IsEmpty()) error_msg = _("Please input user name."); - else if (dbpwd.IsEmpty()) error_msg = _("Please input password."); - if (!error_msg.IsEmpty()) { - throw GdaException(error_msg.mb_str() ); - } - - // save current db info - json_spirit::Object ret_obj; - ret_obj.push_back(json_spirit::Pair("db_host", dbhost.ToStdString())); - ret_obj.push_back(json_spirit::Pair("db_port", dbport.ToStdString())); - ret_obj.push_back(json_spirit::Pair("db_name", dbname.ToStdString())); - ret_obj.push_back(json_spirit::Pair("db_user", dbuser.ToStdString())); - ret_obj.push_back(json_spirit::Pair("db_pwd", dbpwd.ToStdString())); - ret_obj.push_back(json_spirit::Pair("db_type", cur_sel.ToStdString())); - - std::string json_str = json_spirit::write(ret_obj); - OGRDataAdapter::GetInstance().AddEntry("db_info", json_str); - - datasource = new DBDataSource(ds_type, dbname, dbhost, dbport, dbuser, dbpwd); - - } else if ( datasource_type == 2 ) { - // Web Service tab selected - wxString ws_url = m_webservice_url->GetValue().Trim(); - // detect if it's a valid url string - wxRegEx regex; - regex.Compile("^(https|http)://"); - if (!regex.Matches( ws_url )){ - wxString msg = _("Please input a valid url address."); - throw GdaException(msg.mb_str()); - } - if (ws_url.IsEmpty()) { - wxString msg = _("Please input a valid url."); - throw GdaException(msg.mb_str()); - } else { - OGRDataAdapter::GetInstance().AddHistory("ws_url", ws_url.ToStdString()); - } - - if (m_web_choice->GetSelection() == 0 ) { - datasource = new FileDataSource(ws_url); - } else { - if ((!ws_url.StartsWith("WFS:") || !ws_url.StartsWith("wfs:")) - && !ws_url.EndsWith("SERVICE=WFS")) - { - ws_url = "WFS:" + ws_url; - } - datasource = new WebServiceDataSource(GdaConst::ds_wfs, ws_url); - // prompt user to select a layer from WFS - //if (layer_name.IsEmpty()) PromptDSLayers(datasource); - } - } - - return datasource; -} - -void ConnectDatasourceDlg::SaveRecentDataSource(IDataSource* ds, - const wxString& layer_name) -{ - wxLogMessage("Entering ConnectDatasourceDlg::SaveRecentDataSource"); - try { - RecentDatasource recent_ds; - recent_ds.Add(ds, layer_name); - } catch( GdaException ex) { - LOG_MSG(ex.what()); + datasource = new FileDataSource(fn); + + // a special case: sqlite is a file based database, so we need to get + // avalible layers and prompt for user selecting + if (datasource->GetType() == GdaConst::ds_sqlite || datasource->GetType() == GdaConst::ds_gpkg || + datasource->GetType() == GdaConst::ds_osm || datasource->GetType() == GdaConst::ds_esri_personal_gdb || + datasource->GetType() == GdaConst::ds_esri_file_geodb) { + PromptDSLayers(datasource); + if (layer_name.IsEmpty()) { + throw GdaException(wxString("Layer/Table name could not be empty. Please select a layer/table.").mb_str()); + } } - wxLogMessage("Exiting ConnectDatasourceDlg::SaveRecentDataSource"); -} -void ConnectDatasourceDlg::InitSamplePanel() -{ - wxLogMessage("ConnectDatasourceDlg::InitSamplePanel()"); - wxBoxSizer* sizer; - sizer = new wxBoxSizer( wxVERTICAL ); - - wxScrolledWindow* sample_scrl; - sample_scrl = new wxScrolledWindow(smaples_panel, wxID_ANY, - wxDefaultPosition, wxSize(420,200), - wxVSCROLL ); - sample_scrl->SetScrollRate( 5, 5 ); -#ifdef __WIN32__ - sample_scrl->SetBackgroundColour(*wxWHITE); -#endif - { - wxBoxSizer* sizer; - sizer = new wxBoxSizer( wxVERTICAL ); - for (size_t i=0; i < GdaConst::sample_names.size(); i++) { - wxString sample_name = GdaConst::sample_names[i]; - wxString sample_meta_url = GdaConst::sample_meta_urls[i]; - wxString ds_layername = GdaConst::sample_layer_names[i]; - wxString ds_thumb = GdaConst::sample_layer_names[i]; - AddSampleItem(sizer, sample_scrl, sample_name, sample_meta_url, - ds_layername, ds_thumb, base_xrcid_sample_thumb + (int)i); - } - sample_scrl->SetSizer( sizer ); - sample_scrl->Layout(); - sizer->Fit( sample_scrl ); + } else if (datasource_type == 1) { + // Database tab selected + // int cur_sel = m_database_type->GetSelection(); + wxString cur_sel = m_database_type->GetStringSelection(); + wxString dbname = m_database_name->GetValue().Trim(); + wxString dbhost = m_database_host->GetValue().Trim(); + wxString dbport = m_database_port->GetValue().Trim(); + wxString dbuser = m_database_uname->GetValue().Trim(); + wxString dbpwd = m_database_upwd->GetValue().Trim(); + + GdaConst::DataSourceType ds_type = GdaConst::ds_unknown; + if (cur_sel == DBTYPE_ORACLE) { + ds_type = GdaConst::ds_oci; + } else if (cur_sel == DBTYPE_ARCSDE) { + ds_type = GdaConst::ds_esri_arc_sde; + } else if (cur_sel == DBTYPE_POSTGIS) { + ds_type = GdaConst::ds_postgresql; + } else if (cur_sel == DBTYPE_MYSQL) { + ds_type = GdaConst::ds_mysql; + // else if (cur_sel == 4) ds_type = GdaConst::ds_ms_sql; + } else { + wxString msg = + _("The selected database driver is not supported on this platform. Please check GeoDa website for more " + "information about database support and connection."); + throw GdaException(msg.mb_str()); } - - sizer->Add( sample_scrl, 1, wxEXPAND | wxRIGHT, 5 ); - - smaples_panel->SetSizer( sizer ); - smaples_panel->Layout(); - sizer->Fit( smaples_panel ); -} -void ConnectDatasourceDlg::AddSampleItem(wxBoxSizer* sizer, - wxScrolledWindow* scrl, - wxString name, - wxString ds_url, - wxString ds_layername, - wxString ds_thumb, int id) -{ - wxBoxSizer* text_sizer; - text_sizer = new wxBoxSizer( wxVERTICAL ); - - wxString lbl_ds_layername = ds_layername; - lbl_ds_layername = GenUtils::PadTrim(lbl_ds_layername, 30, false); - - wxStaticText* layername; - layername = new wxStaticText(scrl, wxID_ANY, lbl_ds_layername.Trim()); - layername->SetFont(*GdaConst::medium_font); - layername->SetForegroundColour(wxColour(100,100,100)); - layername->SetToolTip(ds_layername); - text_sizer->Add(layername, 1, wxALIGN_LEFT | wxALL | wxEXPAND, 5); - - wxString lbl_name = name; - lbl_name = GenUtils::PadTrim(lbl_name, 60, false); - - wxStaticText* obs_txt; - obs_txt = new wxStaticText(scrl, wxID_ANY, lbl_name); - obs_txt->SetFont(*GdaConst::extra_small_font); - obs_txt->SetForegroundColour(wxColour(70,70,70)); - obs_txt->SetToolTip(name); - text_sizer->Add(obs_txt, 0, wxALIGN_LEFT | wxALL, 5); - - wxString lbl_ds_name = ds_url; - lbl_ds_name = GenUtils::PadTrim(lbl_ds_name, 50, false); - wxHyperlinkCtrl* filepath; - filepath = new wxHyperlinkCtrl(scrl, wxID_ANY, ds_url, ds_url, wxDefaultPosition, wxDefaultSize, wxHL_ALIGN_LEFT); - filepath->SetFont(*GdaConst::extra_small_font); - filepath->SetForegroundColour(wxColour(70,70,70)); - filepath->SetToolTip(ds_url); - text_sizer->Add(filepath, 1, wxALIGN_LEFT | wxALL, 5); - - wxString file_path_str; - ds_thumb.Replace(" ", ""); - file_path_str = GenUtils::GetSamplesDir() + ds_thumb + ".png"; - if (!wxFileExists(file_path_str)) { - file_path_str = GenUtils::GetSamplesDir() + "no_map.png"; + // save user inputs to history table + OGRDataAdapter& ogr_adapter = OGRDataAdapter::GetInstance(); + if (!dbhost.IsEmpty()) ogr_adapter.AddHistory("db_host", dbhost.ToStdString()); + + if (!dbname.IsEmpty()) ogr_adapter.AddHistory("db_name", dbname.ToStdString()); + + if (!dbport.IsEmpty()) ogr_adapter.AddHistory("db_port", dbport.ToStdString()); + + if (!dbuser.IsEmpty()) ogr_adapter.AddHistory("db_user", dbuser.ToStdString()); + + // check if empty, prompt user to input + wxRegEx regex; + regex.Compile("[0-9]+"); + if (!regex.Matches(dbport)) { + wxString msg = _("Database port is empty. Please input one."); + throw GdaException(msg.mb_str()); } - wxImage img(file_path_str); - if (!img.IsOk()) { - file_path_str = GenUtils::GetSamplesDir() + "no_map.png"; - img.LoadFile(file_path_str); + wxString error_msg; + if (dbhost.IsEmpty()) + error_msg = _("Please input database host."); + else if (dbname.IsEmpty()) + error_msg = _("Please input database name."); + else if (dbport.IsEmpty()) + error_msg = _("Please input database port."); + else if (dbuser.IsEmpty()) + error_msg = _("Please input user name."); + else if (dbpwd.IsEmpty()) + error_msg = _("Please input password."); + if (!error_msg.IsEmpty()) { + throw GdaException(error_msg.mb_str()); } - img.Rescale(100,66,wxIMAGE_QUALITY_HIGH ); - wxBitmap bmp(img); - - wxBitmapButton* thumb; - thumb = new wxBitmapButton(scrl, id, bmp); - thumb->Bind(wxEVT_BUTTON, &ConnectDatasourceDlg::OnSample, this); - - wxBoxSizer* row_sizer; - row_sizer = new wxBoxSizer( wxHORIZONTAL ); - row_sizer->Add(thumb, 0, wxALIGN_CENTER | wxALL, 0); - row_sizer->Add(text_sizer, 1, wxALIGN_LEFT | wxALIGN_TOP | wxEXPAND | wxTOP, 5); - - sizer->Add(row_sizer, 0, wxALIGN_LEFT | wxALL, 2); -} -void ConnectDatasourceDlg::OnSample(wxCommandEvent& event) -{ - wxLogMessage("ConnectDatasourceDlg::OnSample()"); - int xrcid = event.GetId(); - int sample_idx = xrcid - base_xrcid_sample_thumb; - - // '{"ds_type":"ds_shapefile", "ds_path": "/test.shp", "db_name":"test"..}' - wxString ds_json; - wxString layername = GdaConst::sample_layer_names[sample_idx]; - wxString ds_name = GdaConst::sample_datasources[sample_idx]; - if (ds_name == "samples.sqlite") { - ds_name = GenUtils::GetSamplesDir() + ds_name; - ds_name.Replace("\\", "\\\\"); - //ds_json = wxString::Format("{\"ds_type\":\"SQLite\", \"ds_path\": \"%s\"}", ds_name); - ds_json = "{\"ds_type\":\"SQLite\", \"ds_path\": \""+ds_name+"\"}"; + // save current db info + json_spirit::Object ret_obj; + ret_obj.push_back(json_spirit::Pair("db_host", dbhost.ToStdString())); + ret_obj.push_back(json_spirit::Pair("db_port", dbport.ToStdString())); + ret_obj.push_back(json_spirit::Pair("db_name", dbname.ToStdString())); + ret_obj.push_back(json_spirit::Pair("db_user", dbuser.ToStdString())); + ret_obj.push_back(json_spirit::Pair("db_pwd", dbpwd.ToStdString())); + ret_obj.push_back(json_spirit::Pair("db_type", cur_sel.ToStdString())); + + std::string json_str = json_spirit::write(ret_obj); + OGRDataAdapter::GetInstance().AddEntry("db_info", json_str); + + datasource = new DBDataSource(ds_type, dbname, dbhost, dbport, dbuser, dbpwd); + + } else if (datasource_type == 2) { + // Web Service tab selected + wxString ws_url = m_webservice_url->GetValue().Trim(); + // detect if it's a valid url string + wxRegEx regex; + regex.Compile("^(https|http)://"); + if (!regex.Matches(ws_url)) { + wxString msg = _("Please input a valid url address."); + throw GdaException(msg.mb_str()); + } + if (ws_url.IsEmpty()) { + wxString msg = _("Please input a valid url."); + throw GdaException(msg.mb_str()); } else { - //ds_json = wxString::Format("{\"ds_type\":\"GeoJSON\", \"ds_path\": \"%s\"}", ds_name); - ds_json = "{\"ds_type\":\"GeoJSON\", \"ds_path\": \""+ds_name+"\"}"; + OGRDataAdapter::GetInstance().AddHistory("ws_url", ws_url.ToStdString()); } - - IDataSource* ds = IDataSource::CreateDataSource(ds_json); - if (ds == NULL) { - // raise message dialog show can't connect to datasource - wxString msg = _("Can't connect to datasource: ") + ds_name; - wxMessageDialog dlg (this, msg, _("Error"), wxOK | wxICON_ERROR); - dlg.ShowModal(); - return; + + if (m_web_choice->GetSelection() == 0) { + datasource = new FileDataSource(ws_url); } else { - datasource = ds; - layer_name = layername; - EndDialog(wxID_OK); + if ((!ws_url.StartsWith("WFS:") || !ws_url.StartsWith("wfs:")) && !ws_url.EndsWith("SERVICE=WFS")) { + ws_url = "WFS:" + ws_url; + } + datasource = new WebServiceDataSource(GdaConst::ds_wfs, ws_url); + // prompt user to select a layer from WFS + // if (layer_name.IsEmpty()) PromptDSLayers(datasource); } + } + + return datasource; +} +void ConnectDatasourceDlg::SaveRecentDataSource(IDataSource* ds, const wxString& layer_name) { + wxLogMessage("Entering ConnectDatasourceDlg::SaveRecentDataSource"); + try { + RecentDatasource recent_ds; + recent_ds.Add(ds, layer_name); + } catch (GdaException ex) { + LOG_MSG(ex.what()); + } + wxLogMessage("Exiting ConnectDatasourceDlg::SaveRecentDataSource"); } -wxCSConv* ConnectDatasourceDlg::GetEncoding() -{ - if (m_encodings && m_encodings->IsShown()) { - - wxFontEncoding encoding_type = wxFONTENCODING_SYSTEM; - int sel = m_encodings->GetSelection(); - wxString encode_str = m_encodings->GetString(sel); - - if (sel == 0) return NULL; - - //encode_str = wxString(encode_str.wc_str(), wxCSConv(wxFONTENCODING_DEFAULT)); - if (sel == 1) { - encoding_type = wxFONTENCODING_UTF8; - } else if (sel == 2) { - encoding_type = wxFONTENCODING_UTF16LE; - - } else if (sel == 3) { - encoding_type = wxFONTENCODING_CP1256; - - } else if (sel == 4 ) { - encoding_type = wxFONTENCODING_ISO8859_2; - - } else if (sel == 5) { - encoding_type = wxFONTENCODING_CP1250; - - } else if (sel == 6) { - encoding_type = wxFONTENCODING_CP852; - - } else if (sel == 7) { - encoding_type = wxFONTENCODING_GB2312; - - } else if (sel == 8) { - encoding_type = wxFONTENCODING_BIG5; - - } else if (sel == 9) { - encoding_type = wxFONTENCODING_ISO8859_5; - - } else if (sel == 10) { - encoding_type = wxFONTENCODING_KOI8; - - } else if (sel == 11) { - encoding_type = wxFONTENCODING_CP1251; - - } else if (sel == 12) { - encoding_type = wxFONTENCODING_CP866; - - } else if (sel == 13 ) { - encoding_type = wxFONTENCODING_ISO8859_7; - - } else if (sel == 14) { - encoding_type = wxFONTENCODING_ISO8859_8; - - } else if (sel == 15) { - encoding_type = wxFONTENCODING_CP1255; - - } else if (sel == 16) { - encoding_type = wxFONTENCODING_SHIFT_JIS; - } else if (sel == 17) { - encoding_type = wxFONTENCODING_EUC_JP; - } else if (sel == 18) { - encoding_type = wxFONTENCODING_EUC_KR; - - } else if (sel == 19 ) { - encoding_type = wxFONTENCODING_ISO8859_10; - - } else if (sel == 20) { - encoding_type = wxFONTENCODING_ISO8859_3; - - } else if (sel == 21) { - encoding_type = wxFONTENCODING_ISO8859_9; - } else if (sel == 22) { - encoding_type = wxFONTENCODING_CP1254; - } else if (sel == 23) { - encoding_type = wxFONTENCODING_CP1258; - } else if (sel == 24) { - encoding_type = wxFONTENCODING_ISO8859_1; - - } else if (sel == 25) { - encoding_type = wxFONTENCODING_ISO8859_15; - } - - if (m_wx_encoding) delete m_wx_encoding; - m_wx_encoding = new wxCSConv(encoding_type); +void ConnectDatasourceDlg::InitSamplePanel() { + wxLogMessage("ConnectDatasourceDlg::InitSamplePanel()"); + wxBoxSizer* sizer; + sizer = new wxBoxSizer(wxVERTICAL); + + wxScrolledWindow* sample_scrl; + sample_scrl = new wxScrolledWindow(smaples_panel, wxID_ANY, wxDefaultPosition, wxSize(420, 200), wxVSCROLL); + sample_scrl->SetScrollRate(5, 5); +#ifdef __WIN32__ + sample_scrl->SetBackgroundColour(*wxWHITE); +#endif + { + wxBoxSizer* sizer; + sizer = new wxBoxSizer(wxVERTICAL); + for (size_t i = 0; i < GdaConst::sample_names.size(); i++) { + wxString sample_name = GdaConst::sample_names[i]; + wxString sample_meta_url = GdaConst::sample_meta_urls[i]; + wxString ds_layername = GdaConst::sample_layer_names[i]; + wxString ds_thumb = GdaConst::sample_layer_names[i]; + AddSampleItem(sizer, sample_scrl, sample_name, sample_meta_url, ds_layername, ds_thumb, + base_xrcid_sample_thumb + static_cast(i)); } - return m_wx_encoding; + sample_scrl->SetSizer(sizer); + sample_scrl->Layout(); + sizer->Fit(sample_scrl); + } + + sizer->Add(sample_scrl, 1, wxEXPAND | wxRIGHT, 5); + + smaples_panel->SetSizer(sizer); + smaples_panel->Layout(); + sizer->Fit(smaples_panel); +} + +void ConnectDatasourceDlg::AddSampleItem(wxBoxSizer* sizer, wxScrolledWindow* scrl, wxString name, wxString ds_url, + wxString ds_layername, wxString ds_thumb, int id) { + wxBoxSizer* text_sizer; + text_sizer = new wxBoxSizer(wxVERTICAL); + + wxString lbl_ds_layername = ds_layername; + lbl_ds_layername = GenUtils::PadTrim(lbl_ds_layername, 30, false); + + wxStaticText* layername; + layername = new wxStaticText(scrl, wxID_ANY, lbl_ds_layername.Trim()); + layername->SetFont(*GdaConst::medium_font); + layername->SetForegroundColour(wxColour(100, 100, 100)); + layername->SetToolTip(ds_layername); + text_sizer->Add(layername, 1, wxALIGN_LEFT | wxALL | wxEXPAND, 5); + + wxString lbl_name = name; + lbl_name = GenUtils::PadTrim(lbl_name, 60, false); + + wxStaticText* obs_txt; + obs_txt = new wxStaticText(scrl, wxID_ANY, lbl_name); + obs_txt->SetFont(*GdaConst::extra_small_font); + obs_txt->SetForegroundColour(wxColour(70, 70, 70)); + obs_txt->SetToolTip(name); + text_sizer->Add(obs_txt, 0, wxALIGN_LEFT | wxALL, 5); + + wxString lbl_ds_name = ds_url; + lbl_ds_name = GenUtils::PadTrim(lbl_ds_name, 50, false); + wxHyperlinkCtrl* filepath; + filepath = new wxHyperlinkCtrl(scrl, wxID_ANY, ds_url, ds_url, wxDefaultPosition, wxDefaultSize, wxHL_ALIGN_LEFT); + filepath->SetFont(*GdaConst::extra_small_font); + filepath->SetForegroundColour(wxColour(70, 70, 70)); + filepath->SetToolTip(ds_url); + text_sizer->Add(filepath, 1, wxALIGN_LEFT | wxALL, 5); + + wxString file_path_str; + ds_thumb.Replace(" ", ""); + file_path_str = GenUtils::GetSamplesDir() + ds_thumb + ".png"; + if (!wxFileExists(file_path_str)) { + file_path_str = GenUtils::GetSamplesDir() + "no_map.png"; + } + wxImage img(file_path_str); + if (!img.IsOk()) { + file_path_str = GenUtils::GetSamplesDir() + "no_map.png"; + img.LoadFile(file_path_str); + } + img.Rescale(100, 66, wxIMAGE_QUALITY_HIGH); + wxBitmap bmp(img); + + wxBitmapButton* thumb; + thumb = new wxBitmapButton(scrl, id, bmp); + thumb->Bind(wxEVT_BUTTON, &ConnectDatasourceDlg::OnSample, this); + + wxBoxSizer* row_sizer; + row_sizer = new wxBoxSizer(wxHORIZONTAL); + row_sizer->Add(thumb, 0, wxALIGN_CENTER | wxALL, 0); + row_sizer->Add(text_sizer, 1, wxALIGN_LEFT | wxALIGN_TOP | wxEXPAND | wxTOP, 5); + + sizer->Add(row_sizer, 0, wxALIGN_LEFT | wxALL, 2); +} + +void ConnectDatasourceDlg::OnSample(wxCommandEvent& event) { + wxLogMessage("ConnectDatasourceDlg::OnSample()"); + int xrcid = event.GetId(); + int sample_idx = xrcid - base_xrcid_sample_thumb; + + // '{"ds_type":"ds_shapefile", "ds_path": "/test.shp", "db_name":"test"..}' + wxString ds_json; + wxString layername = GdaConst::sample_layer_names[sample_idx]; + wxString ds_name = GdaConst::sample_datasources[sample_idx]; + if (ds_name == "samples.sqlite") { + ds_name = GenUtils::GetSamplesDir() + ds_name; + ds_name.Replace("\\", "\\\\"); + // ds_json = wxString::Format("{\"ds_type\":\"SQLite\", \"ds_path\": \"%s\"}", ds_name); + ds_json = "{\"ds_type\":\"SQLite\", \"ds_path\": \"" + ds_name + "\"}"; + } else { + // ds_json = wxString::Format("{\"ds_type\":\"GeoJSON\", \"ds_path\": \"%s\"}", ds_name); + ds_json = "{\"ds_type\":\"GeoJSON\", \"ds_path\": \"" + ds_name + "\"}"; + } + + IDataSource* ds = IDataSource::CreateDataSource(ds_json); + if (ds == NULL) { + // raise message dialog show can't connect to datasource + wxString msg = _("Can't connect to datasource: ") + ds_name; + wxMessageDialog dlg(this, msg, _("Error"), wxOK | wxICON_ERROR); + dlg.ShowModal(); + return; + } else { + datasource = ds; + layer_name = layername; + EndDialog(wxID_OK); + } +} + +wxCSConv* ConnectDatasourceDlg::GetEncoding() { + if (m_encodings && m_encodings->IsShown()) { + wxFontEncoding encoding_type = wxFONTENCODING_SYSTEM; + int sel = m_encodings->GetSelection(); + wxString encode_str = m_encodings->GetString(sel); + + if (sel == 0) return NULL; + + // encode_str = wxString(encode_str.wc_str(), wxCSConv(wxFONTENCODING_DEFAULT)); + if (sel == 1) { + encoding_type = wxFONTENCODING_UTF8; + } else if (sel == 2) { + encoding_type = wxFONTENCODING_UTF16LE; + + } else if (sel == 3) { + encoding_type = wxFONTENCODING_CP1256; + + } else if (sel == 4) { + encoding_type = wxFONTENCODING_ISO8859_2; + + } else if (sel == 5) { + encoding_type = wxFONTENCODING_CP1250; + + } else if (sel == 6) { + encoding_type = wxFONTENCODING_CP852; + + } else if (sel == 7) { + encoding_type = wxFONTENCODING_GB2312; + + } else if (sel == 8) { + encoding_type = wxFONTENCODING_BIG5; + + } else if (sel == 9) { + encoding_type = wxFONTENCODING_ISO8859_5; + + } else if (sel == 10) { + encoding_type = wxFONTENCODING_KOI8; + + } else if (sel == 11) { + encoding_type = wxFONTENCODING_CP1251; + + } else if (sel == 12) { + encoding_type = wxFONTENCODING_CP866; + + } else if (sel == 13) { + encoding_type = wxFONTENCODING_ISO8859_7; + + } else if (sel == 14) { + encoding_type = wxFONTENCODING_ISO8859_8; + + } else if (sel == 15) { + encoding_type = wxFONTENCODING_CP1255; + + } else if (sel == 16) { + encoding_type = wxFONTENCODING_SHIFT_JIS; + } else if (sel == 17) { + encoding_type = wxFONTENCODING_EUC_JP; + } else if (sel == 18) { + encoding_type = wxFONTENCODING_EUC_KR; + + } else if (sel == 19) { + encoding_type = wxFONTENCODING_ISO8859_10; + + } else if (sel == 20) { + encoding_type = wxFONTENCODING_ISO8859_3; + + } else if (sel == 21) { + encoding_type = wxFONTENCODING_ISO8859_9; + } else if (sel == 22) { + encoding_type = wxFONTENCODING_CP1254; + } else if (sel == 23) { + encoding_type = wxFONTENCODING_CP1258; + } else if (sel == 24) { + encoding_type = wxFONTENCODING_ISO8859_1; + + } else if (sel == 25) { + encoding_type = wxFONTENCODING_ISO8859_15; + } + + if (m_wx_encoding) delete m_wx_encoding; + m_wx_encoding = new wxCSConv(encoding_type); + } + return m_wx_encoding; } diff --git a/DialogTools/ConnectDatasourceDlg.h b/DialogTools/ConnectDatasourceDlg.h index 8aacb5af..a82ff260 100644 --- a/DialogTools/ConnectDatasourceDlg.h +++ b/DialogTools/ConnectDatasourceDlg.h @@ -2,7 +2,7 @@ * GeoDa TM, Copyright (C) 2011-2015 by Luc Anselin - all rights reserved * * This file is part of GeoDa. - * + * * GeoDa is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or @@ -20,157 +20,145 @@ #ifndef __GEODA_CENTER_CONNECT_DATASOURCE_DLG_H__ #define __GEODA_CENTER_CONNECT_DATASOURCE_DLG_H__ - -#include -#include #include +#include +#include +#include #include #include -#include -#include +#include + +#include #include "../DataViewer/DataSource.h" #include "AutoCompTextCtrl.h" #include "DatasourceDlg.h" -#include "DatasourceDlg.h" // // Class RecentDatasource // // -class RecentDatasource -{ -public: - RecentDatasource(); - virtual ~RecentDatasource(); - - void Add(wxString ds_name, wxString ds_conf, wxString ds_layername, - wxString ds_thumb = ""); - void Add(IDataSource* ds, const wxString& layer_name, wxString ds_thumb=""); - void Clear(); - void Save(); - void Delete(int idx); - void DeleteLastRecord(); - - int GetRecords() {return n_ds;} - wxString GetLastIndex(); - wxString GetLastLayerName(); - wxString GetLastDSName(); - void UpdateLastThumb(wxString ds_thumb); - std::vector GetList(); - - IDataSource* GetDatasource(wxString ds_name); - wxString GetLayerName(wxString ds_name); - - wxString GetDSName(int idx) {return ds_names[idx];} - wxString GetDSLayerName(int idx) {return ds_layernames[idx];} - wxString GetDSThumbnail(int idx) {return ds_thumbnails[idx];} -protected: - static const int N_MAX_ITEMS; - static const std::string KEY_NAME_IN_GDA_HISTORY; - - int n_ds; - wxString ds_json_str; - - std::vector ds_names; - std::vector ds_layernames; - std::vector ds_confs; - std::vector ds_thumbnails; - - void Init(wxString json_str); -}; +class RecentDatasource { + public: + RecentDatasource(); + virtual ~RecentDatasource(); + + void Add(wxString ds_name, wxString ds_conf, wxString ds_layername, wxString ds_thumb = ""); + void Add(IDataSource* ds, const wxString& layer_name, wxString ds_thumb = ""); + void Clear(); + void Save(); + void Delete(int idx); + void DeleteLastRecord(); + + int GetRecords() { return n_ds; } + wxString GetLastIndex(); + wxString GetLastLayerName(); + wxString GetLastDSName(); + void UpdateLastThumb(wxString ds_thumb); + std::vector GetList(); + + IDataSource* GetDatasource(wxString ds_name); + wxString GetLayerName(wxString ds_name); + wxString GetDSName(int idx) { return ds_names[idx]; } + wxString GetDSLayerName(int idx) { return ds_layernames[idx]; } + wxString GetDSThumbnail(int idx) { return ds_thumbnails[idx]; } + protected: + static const int N_MAX_ITEMS; + static const char KEY_NAME_IN_GDA_HISTORY[]; + + int n_ds; + wxString ds_json_str; + + std::vector ds_names; + std::vector ds_layernames; + std::vector ds_confs; + std::vector ds_thumbnails; + + void Init(wxString json_str); +}; //////////////////////////////////////////////////////////////////////////////// // // Class ConnectDatasourceDlg -// +// //////////////////////////////////////////////////////////////////////////////// class DnDFile; -class ConnectDatasourceDlg: public DatasourceDlg -{ -public: - enum Style { - DEFAULT_STYLE = 0, - SHOW_CSV_CONFIGURE = 1, //0b00000001 - SHOW_RECENT_PANEL = 2, //0b00000010 - ALLOW_PROJECT_FILE = 4 - }; - - ConnectDatasourceDlg(wxWindow* parent, - const wxPoint& pos = wxDefaultPosition, - const wxSize& size = wxDefaultSize, - bool showCsvConfigure=true, - bool showRecentPanel=GdaConst::show_recent_sample_connect_ds_dialog, - int dialogType = 0); - virtual ~ConnectDatasourceDlg(); - virtual void OnOkClick( wxCommandEvent& event ); - - void CreateControls(); - void OnLookupWSLayerBtn( wxCommandEvent& event ); - void OnLookupDSTableBtn( wxCommandEvent& event ); - void OnLookupCartoDBTableBtn( wxCommandEvent& event ); - IDataSource* GetDataSource(){ return datasource; } - wxCSConv* GetEncoding(); - -protected: - int dialogType; - bool showCsvConfigure; - bool showRecentPanel; - wxCSConv* m_wx_encoding; - - wxStaticBitmap* m_drag_drop_box; - wxBitmapButton* m_database_lookup_table; - wxBitmapButton* m_database_lookup_wslayer; - wxTextCtrl* m_database_table; - AutoTextCtrl* m_webservice_url; - IDataSource* datasource; - wxPanel* recent_panel; - wxPanel* smaples_panel; - wxScrolledWindow* scrl; - wxNotebook* recent_nb; - wxCheckBox* noshow_recent; - wxChoice* m_web_choice; - wxChoice* m_encodings; - wxStaticText* m_encoding_lbl; - DnDFile* m_dnd; - - int base_xrcid_recent_thumb; - int base_xrcid_sample_thumb; - void AddRecentItem(wxBoxSizer* sizer, wxScrolledWindow* scrl, - wxString ds_name, wxString ds_layername, - wxString ds_thumb, int id); - void AddSampleItem(wxBoxSizer* sizer, wxScrolledWindow* scrl, - wxString name, - wxString ds_name, wxString ds_layername, - wxString ds_thumb, int id); - void InitRecentPanel(); - void InitSamplePanel(); - IDataSource* CreateDataSource(); - void SaveRecentDataSource(IDataSource* ds, const wxString& layer_name); - - void OnRecent(wxCommandEvent& event); - void OnSample(wxCommandEvent& event); - void OnRecentDelete(wxCommandEvent& event); - - void OnNoShowRecent(wxCommandEvent& event); - - - DECLARE_EVENT_TABLE() +class ConnectDatasourceDlg : public DatasourceDlg { + public: + enum Style { + DEFAULT_STYLE = 0, + SHOW_CSV_CONFIGURE = 1, // 0b00000001 + SHOW_RECENT_PANEL = 2, // 0b00000010 + ALLOW_PROJECT_FILE = 4 + }; + + ConnectDatasourceDlg(wxWindow* parent, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, + bool showCsvConfigure = true, + bool showRecentPanel = GdaConst::show_recent_sample_connect_ds_dialog, int dialogType = 0); + virtual ~ConnectDatasourceDlg(); + virtual void OnOkClick(wxCommandEvent& event); + + void CreateControls(); + void OnLookupWSLayerBtn(wxCommandEvent& event); + void OnLookupDSTableBtn(wxCommandEvent& event); + void OnLookupCartoDBTableBtn(wxCommandEvent& event); + IDataSource* GetDataSource() { return datasource; } + wxCSConv* GetEncoding(); + + protected: + int dialogType; + bool showCsvConfigure; + bool showRecentPanel; + wxCSConv* m_wx_encoding; + + wxStaticBitmap* m_drag_drop_box; + wxBitmapButton* m_database_lookup_table; + wxBitmapButton* m_database_lookup_wslayer; + wxTextCtrl* m_database_table; + AutoTextCtrl* m_webservice_url; + IDataSource* datasource; + wxPanel* recent_panel; + wxPanel* smaples_panel; + wxScrolledWindow* scrl; + wxNotebook* recent_nb; + wxCheckBox* noshow_recent; + wxChoice* m_web_choice; + wxChoice* m_encodings; + wxStaticText* m_encoding_lbl; + DnDFile* m_dnd; + + int base_xrcid_recent_thumb; + int base_xrcid_sample_thumb; + void AddRecentItem(wxBoxSizer* sizer, wxScrolledWindow* scrl, wxString ds_name, wxString ds_layername, + wxString ds_thumb, int id); + void AddSampleItem(wxBoxSizer* sizer, wxScrolledWindow* scrl, wxString name, wxString ds_name, wxString ds_layername, + wxString ds_thumb, int id); + void InitRecentPanel(); + void InitSamplePanel(); + IDataSource* CreateDataSource(); + void SaveRecentDataSource(IDataSource* ds, const wxString& layer_name); + + void OnRecent(wxCommandEvent& event); + void OnSample(wxCommandEvent& event); + void OnRecentDelete(wxCommandEvent& event); + + void OnNoShowRecent(wxCommandEvent& event); + + DECLARE_EVENT_TABLE() }; // Drag and Drop Area -class DnDFile : public wxFileDropTarget -{ -public: - DnDFile(ConnectDatasourceDlg *pOwner = NULL); - ~DnDFile(); - - virtual bool OnDropFiles(wxCoord x, wxCoord y, const wxArrayString& filenames); - -private: - ConnectDatasourceDlg *m_pOwner; +class DnDFile : public wxFileDropTarget { + public: + explicit DnDFile(ConnectDatasourceDlg* pOwner = NULL); + ~DnDFile(); + + virtual bool OnDropFiles(wxCoord x, wxCoord y, const wxArrayString& filenames); + + private: + ConnectDatasourceDlg* m_pOwner; }; #endif diff --git a/GdaConst.cpp b/GdaConst.cpp index aa9bf048..6891e51a 100644 --- a/GdaConst.cpp +++ b/GdaConst.cpp @@ -345,6 +345,8 @@ bool GdaConst::enable_high_dpi_support = true; bool GdaConst::enable_high_dpi_support = false; #endif +const char GdaConst::gda_config_true[] = "1"; +const char GdaConst::gda_config_false[] = "0"; bool GdaConst::show_csv_configure_in_merge = true; bool GdaConst::show_recent_sample_connect_ds_dialog = true; bool GdaConst::use_cross_hatching = false; diff --git a/GdaConst.h b/GdaConst.h index 9fdc94ad..96f2c493 100644 --- a/GdaConst.h +++ b/GdaConst.h @@ -254,6 +254,8 @@ class GdaConst { static const wxString gda_lang_spanish; // Preferences + static const char gda_config_true[]; + static const char gda_config_false[]; static double gda_autoweight_stop; static bool gda_draw_map_labels; static int gda_map_label_font_size; diff --git a/version.h b/version.h index 37411b81..423df06e 100644 --- a/version.h +++ b/version.h @@ -2,10 +2,10 @@ namespace Gda { const int version_major = 1; const int version_minor = 22; const int version_build = 0; - const int version_subbuild = 2; + const int version_subbuild = 4; const int version_year = 2023; - const int version_month = 8; - const int version_day = 18; + const int version_month = 10; + const int version_day = 9; const int version_night = 0; const int version_type = 2; // 0: alpha, 1: beta, 2: release } // namespace Gda