From badeb2f64ce7039493ec1746cffc6655d53b8f5a Mon Sep 17 00:00:00 2001 From: YuSanka Date: Mon, 12 Feb 2018 08:57:32 +0100 Subject: [PATCH] Save language preset. * Added global variable g_local_dir to get "localization" directory. * Chage/Set language works correctly now. * Probably, fixed work on Linux --- lib/Slic3r.pm | 1 + lib/Slic3r/GUI.pm | 1 - xs/src/libslic3r/Utils.hpp | 5 ++- xs/src/libslic3r/utils.cpp | 11 ++++-- xs/src/slic3r/GUI/GUI.cpp | 72 +++++++++++++++++++++----------------- xs/src/slic3r/GUI/GUI.hpp | 2 +- xs/xsp/XS.xsp | 6 ++++ 7 files changed, 60 insertions(+), 38 deletions(-) diff --git a/lib/Slic3r.pm b/lib/Slic3r.pm index 66039ddf0..523770f3f 100644 --- a/lib/Slic3r.pm +++ b/lib/Slic3r.pm @@ -43,6 +43,7 @@ use FindBin; # Let the XS module know where the GUI resources reside. set_resources_dir(decode_path($FindBin::Bin) . (($^O eq 'darwin') ? '/../Resources' : '/resources')); set_var_dir(resources_dir() . "/icons"); +set_local_dir(resources_dir() . "/localization/"); use Moo 1.003001; diff --git a/lib/Slic3r/GUI.pm b/lib/Slic3r/GUI.pm index e585a6967..8510c7ba4 100644 --- a/lib/Slic3r/GUI.pm +++ b/lib/Slic3r/GUI.pm @@ -150,7 +150,6 @@ sub OnInit { sub recreate_GUI{ my ($self) = @_; - print "Inside recreate_GUI \n"; my $topwindow = $self->GetTopWindow(); $self->{mainframe} = my $frame = Slic3r::GUI::MainFrame->new( # If set, the "Controller" tab for the control of the printer over serial line and the serial port settings are hidden. diff --git a/xs/src/libslic3r/Utils.hpp b/xs/src/libslic3r/Utils.hpp index d49995f95..27e7fad6b 100644 --- a/xs/src/libslic3r/Utils.hpp +++ b/xs/src/libslic3r/Utils.hpp @@ -19,8 +19,11 @@ std::string var(const std::string &file_name); void set_resources_dir(const std::string &path); // Return a full path to the resources directory. const std::string& resources_dir(); + +// Set a path with GUI localization files. +void set_local_dir(const std::string &path); // Return a full path to the localization directory. -std::string localization_dir(); +const std::string& localization_dir(); // Set a path with preset files. void set_data_dir(const std::string &path); diff --git a/xs/src/libslic3r/utils.cpp b/xs/src/libslic3r/utils.cpp index 7cd82f931..34b9eaa9f 100644 --- a/xs/src/libslic3r/utils.cpp +++ b/xs/src/libslic3r/utils.cpp @@ -103,9 +103,16 @@ const std::string& resources_dir() return g_resources_dir; } -std::string localization_dir() +static std::string g_local_dir; + +void set_local_dir(const std::string &dir) { - return resources_dir() + "\\localization\\"; + g_local_dir = dir; +} + +const std::string& localization_dir() +{ + return g_local_dir; } static std::string g_data_dir; diff --git a/xs/src/slic3r/GUI/GUI.cpp b/xs/src/slic3r/GUI/GUI.cpp index 3b825ad92..02f5a1df3 100644 --- a/xs/src/slic3r/GUI/GUI.cpp +++ b/xs/src/slic3r/GUI/GUI.cpp @@ -35,7 +35,7 @@ #include "Tab.hpp" #include "TabIface.hpp" #include "AppConfig.hpp" -//#include +#include #include #include #include "Utils.hpp" @@ -168,8 +168,7 @@ wxNotebook *g_wxTabPanel = nullptr; std::vector g_tabs_list; -wxLocale* m_Locale; -std::string m_local_dir; +wxLocale* g_wxLocale; void set_wxapp(wxApp *app) { @@ -211,17 +210,27 @@ bool select_language(wxArrayString & names, { wxCHECK_MSG(names.Count() == identifiers.Count(), false, _L("Array of language names and identifiers should have the same size.")); - long index = wxGetSingleChoiceIndex(_L("Select the language"), - _L("Language"), names); + int init_selection = 0; + long current_language = g_wxLocale ? g_wxLocale->GetLanguage() : wxLANGUAGE_UNKNOWN; + for (auto lang : identifiers){ + if (lang == current_language) + break; + else + ++init_selection; + } + if (init_selection == identifiers.size()) + init_selection = 0; + long index = wxGetSingleChoiceIndex(_L("Select the language"), _L("Language"), + names, init_selection); if (index != -1) { - m_Locale = new wxLocale; - m_Locale->Init(identifiers[index]); - m_Locale->AddCatalogLookupPathPrefix(wxPathOnly(m_local_dir)); + g_wxLocale = new wxLocale; + g_wxLocale->Init(identifiers[index]); + g_wxLocale->AddCatalogLookupPathPrefix(wxPathOnly(localization_dir())); wxLogTrace(wxTraceMask(), _L("Slic3rPE: Path Prefix = \"%s\""), - wxPathOnly(m_local_dir).GetData()); - m_Locale->AddCatalog(g_wxApp->GetAppName()); + wxPathOnly(localization_dir()).GetData()); + g_wxLocale->AddCatalog(g_wxApp->GetAppName()); wxLogTrace(wxTraceMask(), _L("Slic3rPE: Catalog Name = \"%s\""), g_wxApp->GetAppName().GetData()); @@ -232,44 +241,41 @@ bool select_language(wxArrayString & names, bool load_language() { -// wxConfig config(g_wxApp->GetAppName()); + wxConfig config(g_wxApp->GetAppName()); long language; -// if (!config.Read(wxT("wxTranslation_Language"), -// &language, wxLANGUAGE_UNKNOWN)) + if (!config.Read(wxT("wxTranslation_Language"), + &language, wxLANGUAGE_UNKNOWN)) { - language = wxLANGUAGE_ENGLISH_US;// wxLANGUAGE_UKRAINIAN;// wxLANGUAGE_UNKNOWN; + language = wxLANGUAGE_UNKNOWN; } - if (language == wxLANGUAGE_UNKNOWN) return false; - wxArrayString names; - wxArrayLong identifiers; + if (language == wxLANGUAGE_UNKNOWN) + return false; + wxArrayString names; + wxArrayLong identifiers; get_installed_languages(names, identifiers); for (size_t i = 0; i < identifiers.Count(); i++) { if (identifiers[i] == language) { - m_Locale = new wxLocale; - m_Locale->Init(identifiers[i]); - m_Locale->AddCatalogLookupPathPrefix(wxPathOnly(m_local_dir)); - m_Locale->AddCatalog(g_wxApp->GetAppName()); + g_wxLocale = new wxLocale; + g_wxLocale->Init(identifiers[i]); + g_wxLocale->AddCatalogLookupPathPrefix(wxPathOnly(localization_dir())); + g_wxLocale->AddCatalog(g_wxApp->GetAppName()); return true; } } return false; } -void save_language(bool bReset) +void save_language() { -// wxConfig config(g_wxApp->GetAppName()); + wxConfig config(g_wxApp->GetAppName()); long language = wxLANGUAGE_UNKNOWN; - if (!bReset) - { - if (m_Locale) - { - language = m_Locale->GetLanguage(); - } + if (g_wxLocale) { + language = g_wxLocale->GetLanguage(); } -// config.Write(wxT("wxTranslation_Language"), language); -// config.Flush(); + config.Write(wxT("wxTranslation_Language"), language); + config.Flush(); } void get_installed_languages(wxArrayString & names, @@ -277,9 +283,8 @@ void get_installed_languages(wxArrayString & names, { names.Clear(); identifiers.Clear(); - m_local_dir = localization_dir(); - wxDir dir(wxPathOnly(m_local_dir)); + wxDir dir(wxPathOnly(localization_dir())); wxString filename; const wxLanguageInfo * langinfo; wxString name = wxLocale::GetLanguageName(wxLANGUAGE_DEFAULT); @@ -319,6 +324,7 @@ void add_debug_menu(wxMenuBar *menu, int event_language_change) wxArrayLong identifiers; get_installed_languages(names, identifiers); if (select_language(names, identifiers)){ + save_language(); show_info(g_wxTabPanel, "Application will be restarted", "Attention!"); if (event_language_change > 0) { wxCommandEvent event(event_language_change); diff --git a/xs/src/slic3r/GUI/GUI.hpp b/xs/src/slic3r/GUI/GUI.hpp index d3db707fa..d2b8e82d9 100644 --- a/xs/src/slic3r/GUI/GUI.hpp +++ b/xs/src/slic3r/GUI/GUI.hpp @@ -77,7 +77,7 @@ void show_info(wxWindow* parent, wxString message, wxString title); // load language saved at application config bool load_language(); // save language at application config -void save_language(bool bReset); +void save_language(); // get list of installed languages void get_installed_languages(wxArrayString & names, wxArrayLong & identifiers); // select language from the list of installed languages diff --git a/xs/xsp/XS.xsp b/xs/xsp/XS.xsp index e31570ca7..e900532aa 100644 --- a/xs/xsp/XS.xsp +++ b/xs/xsp/XS.xsp @@ -54,6 +54,12 @@ set_var_dir(dir) CODE: Slic3r::set_var_dir(dir); +void +set_local_dir(dir) + char *dir; + CODE: + Slic3r::set_local_dir(dir); + char* var_dir() CODE: