diff --git a/src/slic3r/GUI/BackgroundSlicingProcess.cpp b/src/slic3r/GUI/BackgroundSlicingProcess.cpp index 99997e390..aa6c922aa 100644 --- a/src/slic3r/GUI/BackgroundSlicingProcess.cpp +++ b/src/slic3r/GUI/BackgroundSlicingProcess.cpp @@ -1,5 +1,5 @@ #include "BackgroundSlicingProcess.hpp" -#include "GUI.hpp" +#include "GUI_App.hpp" #include #include @@ -19,10 +19,6 @@ namespace Slic3r { -namespace GUI { - extern wxPanel *g_wxPlater; -}; - BackgroundSlicingProcess::BackgroundSlicingProcess() { m_temp_output_path = wxStandardPaths::Get().GetTempDir().utf8_str().data(); @@ -59,7 +55,7 @@ void BackgroundSlicingProcess::thread_proc() assert(m_print != nullptr); m_print->process(); if (! m_print->canceled()) { - wxQueueEvent(GUI::g_wxPlater, new wxCommandEvent(m_event_sliced_id)); + wxQueueEvent(GUI::wxGetApp().mainframe->m_plater, new wxCommandEvent(m_event_sliced_id)); m_print->export_gcode(m_temp_output_path, m_gcode_preview_data); if (! m_print->canceled() && ! m_output_path.empty()) { if (copy_file(m_temp_output_path, m_output_path) != 0) @@ -81,7 +77,7 @@ void BackgroundSlicingProcess::thread_proc() wxCommandEvent evt(m_event_finished_id); evt.SetString(error); evt.SetInt(m_print->canceled() ? -1 : (error.empty() ? 1 : 0)); - wxQueueEvent(GUI::g_wxPlater, evt.Clone()); + wxQueueEvent(GUI::wxGetApp().mainframe->m_plater, evt.Clone()); m_print->restart(); lck.unlock(); // Let the UI thread wake up if it is waiting for the background task to finish. diff --git a/src/slic3r/GUI/GUI.cpp b/src/slic3r/GUI/GUI.cpp index 1a4fbb833..8313f3bda 100644 --- a/src/slic3r/GUI/GUI.cpp +++ b/src/slic3r/GUI/GUI.cpp @@ -116,21 +116,9 @@ void break_to_debugger() #endif /* _WIN32 */ } -// Passing the wxWidgets GUI classes instantiated by the Perl part to C++. -wxApp *g_wxApp = nullptr; -wxFrame *g_wxMainFrame = nullptr; -ProgressStatusBar *g_progress_status_bar = nullptr; -wxNotebook *g_wxTabPanel = nullptr; -wxPanel *g_wxPlater = nullptr; -AppConfig *g_AppConfig = nullptr; -PresetBundle *g_PresetBundle= nullptr; -PresetUpdater *g_PresetUpdater = nullptr; - // #ys_FIXME_for_delete std::vector g_tabs_list; -wxLocale* g_wxLocale {nullptr}; - std::vector > m_optgroups; double m_brim_width = 0.0; size_t m_label_width = 100; @@ -148,54 +136,6 @@ bool g_show_manifold_warning_icon = false; PreviewIface* g_preview = nullptr; -// static std::string libslic3r_translate_callback(const char *s) { return wxGetTranslation(wxString(s, wxConvUTF8)).utf8_str().data(); } - -void set_wxapp(wxApp *app) -{ - g_wxApp = app; - // Let the libslic3r know the callback, which will translate messages on demand. -// Slic3r::I18N::set_translate_callback(libslic3r_translate_callback); -} - -void set_main_frame(wxFrame *main_frame) -{ - g_wxMainFrame = main_frame; -} - -wxFrame* get_main_frame() { return g_wxMainFrame; } - -void set_progress_status_bar(ProgressStatusBar *prsb) -{ - g_progress_status_bar = prsb; -} - -ProgressStatusBar* get_progress_status_bar() { return g_progress_status_bar; } - -void set_tab_panel(wxNotebook *tab_panel) -{ - g_wxTabPanel = tab_panel; -} - -void set_plater(wxPanel *plater) -{ - g_wxPlater = plater; -} - -void set_app_config(AppConfig *app_config) -{ - g_AppConfig = app_config; -} - -void set_preset_bundle(PresetBundle *preset_bundle) -{ - g_PresetBundle = preset_bundle; -} - -void set_preset_updater(PresetUpdater *updater) -{ - g_PresetUpdater = updater; -} - enum ActionButtons { abExportGCode, @@ -252,245 +192,12 @@ void set_objects_list_sizer(wxBoxSizer *objects_list_sizer){ g_object_list_sizer = objects_list_sizer; } -bool select_language(wxArrayString & names, - wxArrayLong & identifiers) -{ - wxCHECK_MSG(names.Count() == identifiers.Count(), false, - _(L("Array of language names and identifiers should have the same size."))); - 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) - { - g_wxLocale = new wxLocale; - g_wxLocale->Init(identifiers[index]); - g_wxLocale->AddCatalogLookupPathPrefix(wxPathOnly(localization_dir())); - g_wxLocale->AddCatalog(g_wxApp->GetAppName()); - wxSetlocale(LC_NUMERIC, "C"); - Preset::update_suffix_modified(); - return true; - } - return false; -} - -wxLocale* get_locale() { - return g_wxLocale; -} - -// #ys_FIXME_for_delete -bool load_language() -{ - wxString language = wxEmptyString; - if (g_AppConfig->has("translation_language")) - language = g_AppConfig->get("translation_language"); - - if (language.IsEmpty()) - return false; - wxArrayString names; - wxArrayLong identifiers; - get_installed_languages(names, identifiers); - for (size_t i = 0; i < identifiers.Count(); i++) - { - if (wxLocale::GetLanguageCanonicalName(identifiers[i]) == language) - { - g_wxLocale = new wxLocale; - g_wxLocale->Init(identifiers[i]); - g_wxLocale->AddCatalogLookupPathPrefix(wxPathOnly(localization_dir())); - g_wxLocale->AddCatalog(g_wxApp->GetAppName()); - wxSetlocale(LC_NUMERIC, "C"); - Preset::update_suffix_modified(); - return true; - } - } - return false; -} - -void save_language() -{ - wxString language = wxEmptyString; - if (g_wxLocale) - language = g_wxLocale->GetCanonicalName(); - - g_AppConfig->set("translation_language", language.ToStdString()); - g_AppConfig->save(); -} - -void get_installed_languages(wxArrayString & names, - wxArrayLong & identifiers) -{ - names.Clear(); - identifiers.Clear(); - - wxDir dir(wxPathOnly(localization_dir())); - wxString filename; - const wxLanguageInfo * langinfo; - wxString name = wxLocale::GetLanguageName(wxLANGUAGE_DEFAULT); - if (!name.IsEmpty()) - { - names.Add(_(L("Default"))); - identifiers.Add(wxLANGUAGE_DEFAULT); - } - for (bool cont = dir.GetFirst(&filename, wxEmptyString, wxDIR_DIRS); - cont; cont = dir.GetNext(&filename)) - { - langinfo = wxLocale::FindLanguageInfo(filename); - if (langinfo != NULL) - { - auto full_file_name = dir.GetName() + wxFileName::GetPathSeparator() + - filename + wxFileName::GetPathSeparator() + - g_wxApp->GetAppName() + wxT(".mo"); - if (wxFileExists(full_file_name)) - { - names.Add(langinfo->Description); - identifiers.Add(langinfo->Language); - } - } - } -} - -// enum ConfigMenuIDs { -// ConfigMenuWizard, -// ConfigMenuSnapshots, -// ConfigMenuTakeSnapshot, -// ConfigMenuUpdate, -// ConfigMenuPreferences, -// ConfigMenuModeSimple, -// ConfigMenuModeExpert, -// ConfigMenuLanguage, -// ConfigMenuFlashFirmware, -// ConfigMenuCnt, -// }; - -// #ys_FIXME_for_delete -// ConfigMenuIDs get_view_mode() -// { -// if (!g_AppConfig->has("view_mode")) -// return ConfigMenuModeSimple; -// -// const auto mode = g_AppConfig->get("view_mode"); -// return mode == "expert" ? ConfigMenuModeExpert : ConfigMenuModeSimple; -// } - static wxString dots("…", wxConvUTF8); -// #ys_FIXME_for_delete -/* -void add_config_menu(wxMenuBar *menu, int event_preferences_changed, int event_language_change) -{ - auto local_menu = new wxMenu(); - wxWindowID config_id_base = wxWindow::NewControlId((int)ConfigMenuCnt); - const auto config_wizard_name = _(ConfigWizard::name().wx_str()); - const auto config_wizard_tooltip = wxString::Format(_(L("Run %s")), config_wizard_name); - // Cmd+, is standard on OS X - what about other operating systems? - local_menu->Append(config_id_base + ConfigMenuWizard, config_wizard_name + dots, config_wizard_tooltip); - local_menu->Append(config_id_base + ConfigMenuSnapshots, _(L("Configuration Snapshots"))+dots, _(L("Inspect / activate configuration snapshots"))); - local_menu->Append(config_id_base + ConfigMenuTakeSnapshot, _(L("Take Configuration Snapshot")), _(L("Capture a configuration snapshot"))); -// local_menu->Append(config_id_base + ConfigMenuUpdate, _(L("Check for updates")), _(L("Check for configuration updates"))); - local_menu->AppendSeparator(); - local_menu->Append(config_id_base + ConfigMenuPreferences, _(L("Preferences"))+dots+"\tCtrl+,", _(L("Application preferences"))); - local_menu->AppendSeparator(); - auto mode_menu = new wxMenu(); - mode_menu->AppendRadioItem(config_id_base + ConfigMenuModeSimple, _(L("&Simple")), _(L("Simple View Mode"))); - mode_menu->AppendRadioItem(config_id_base + ConfigMenuModeExpert, _(L("&Expert")), _(L("Expert View Mode"))); - mode_menu->Check(config_id_base + get_view_mode(), true); - local_menu->AppendSubMenu(mode_menu, _(L("&Mode")), _(L("Slic3r View Mode"))); - local_menu->AppendSeparator(); - local_menu->Append(config_id_base + ConfigMenuLanguage, _(L("Change Application Language"))); - local_menu->AppendSeparator(); - local_menu->Append(config_id_base + ConfigMenuFlashFirmware, _(L("Flash printer firmware")), _(L("Upload a firmware image into an Arduino based printer"))); - // TODO: for when we're able to flash dictionaries - // local_menu->Append(config_id_base + FirmwareMenuDict, _(L("Flash language file")), _(L("Upload a language dictionary file into a Prusa printer"))); - - local_menu->Bind(wxEVT_MENU, [config_id_base, event_language_change, event_preferences_changed](wxEvent &event){ - switch (event.GetId() - config_id_base) { - case ConfigMenuWizard: - config_wizard(ConfigWizard::RR_USER); - break; - case ConfigMenuTakeSnapshot: - // Take a configuration snapshot. - if (check_unsaved_changes()) { - wxTextEntryDialog dlg(nullptr, _(L("Taking configuration snapshot")), _(L("Snapshot name"))); - if (dlg.ShowModal() == wxID_OK) - g_AppConfig->set("on_snapshot", - Slic3r::GUI::Config::SnapshotDB::singleton().take_snapshot( - *g_AppConfig, Slic3r::GUI::Config::Snapshot::SNAPSHOT_USER, dlg.GetValue().ToUTF8().data()).id); - } - break; - case ConfigMenuSnapshots: - if (check_unsaved_changes()) { - std::string on_snapshot; - if (Config::SnapshotDB::singleton().is_on_snapshot(*g_AppConfig)) - on_snapshot = g_AppConfig->get("on_snapshot"); - ConfigSnapshotDialog dlg(Slic3r::GUI::Config::SnapshotDB::singleton(), on_snapshot); - dlg.ShowModal(); - if (! dlg.snapshot_to_activate().empty()) { - if (! Config::SnapshotDB::singleton().is_on_snapshot(*g_AppConfig)) - Config::SnapshotDB::singleton().take_snapshot(*g_AppConfig, Config::Snapshot::SNAPSHOT_BEFORE_ROLLBACK); - g_AppConfig->set("on_snapshot", - Config::SnapshotDB::singleton().restore_snapshot(dlg.snapshot_to_activate(), *g_AppConfig).id); - g_PresetBundle->load_presets(*g_AppConfig); - // Load the currently selected preset into the GUI, update the preset selection box. - load_current_presets(); - } - } - break; - case ConfigMenuPreferences: - { - PreferencesDialog dlg(g_wxMainFrame, event_preferences_changed); - dlg.ShowModal(); - break; - } - case ConfigMenuLanguage: - { - wxArrayString names; - wxArrayLong identifiers; - get_installed_languages(names, identifiers); - if (select_language(names, identifiers)) { - save_language(); - show_info(g_wxTabPanel, _(L("Application will be restarted")), _(L("Attention!"))); - if (event_language_change > 0) { - _3DScene::remove_all_canvases();// remove all canvas before recreate GUI - wxCommandEvent event(event_language_change); - g_wxApp->ProcessEvent(event); - } - } - break; - } - case ConfigMenuFlashFirmware: - FirmwareDialog::run(g_wxMainFrame); - break; - default: - break; - } - }); - mode_menu->Bind(wxEVT_MENU, [config_id_base](wxEvent& event) { - std::string mode = event.GetId() - config_id_base == ConfigMenuModeExpert ? - "expert" : "simple"; - g_AppConfig->set("view_mode", mode); - g_AppConfig->save(); - update_mode(); - }); - menu->Append(local_menu, _(L("&Configuration"))); -} - -void add_menus(wxMenuBar *menu, int event_preferences_changed, int event_language_change) -{ - add_config_menu(menu, event_preferences_changed, event_language_change); -} -*/ void open_model(wxWindow *parent, wxArrayString& input_files){ auto dialog = new wxFileDialog(parent /*? parent : GetTopWindow()*/, _(L("Choose one or more files (STL/OBJ/AMF/3MF/PRUSA):")), - g_AppConfig->get_last_dir(), "", + get_app_config()->get_last_dir(), "", MODEL_WILDCARD, wxFD_OPEN | wxFD_MULTIPLE | wxFD_FILE_MUST_EXIST); if (dialog->ShowModal() != wxID_OK) { dialog->Destroy(); @@ -501,36 +208,12 @@ void open_model(wxWindow *parent, wxArrayString& input_files){ dialog->Destroy(); } -// This is called when closing the application, when loading a config file or when starting the config wizard -// to notify the user whether he is aware that some preset changes will be lost. -// #ys_FIXME_for_delete -/* -bool check_unsaved_changes() -{ - std::string dirty; - for (Tab *tab : g_tabs_list) - if (tab->current_preset_is_dirty()) - if (dirty.empty()) - dirty = tab->name(); - else - dirty += std::string(", ") + tab->name(); - if (dirty.empty()) - // No changes, the application may close or reload presets. - return true; - // Ask the user. - auto dialog = new wxMessageDialog(g_wxMainFrame, - _(L("You have unsaved changes ")) + dirty + _(L(". Discard changes and continue anyway?")), - _(L("Unsaved Presets")), - wxICON_QUESTION | wxYES_NO | wxNO_DEFAULT); - return dialog->ShowModal() == wxID_YES; -} -*/ bool config_wizard_startup(bool app_config_exists) { - if (! app_config_exists || g_PresetBundle->printers.size() <= 1) { + if (!app_config_exists || wxGetApp().preset_bundle->printers.size() <= 1) { config_wizard(ConfigWizard::RR_DATA_EMPTY); return true; - } else if (g_AppConfig->legacy_datadir()) { + } else if (get_app_config()->legacy_datadir()) { // Looks like user has legacy pre-vendorbundle data directory, // explain what this is and run the wizard @@ -551,7 +234,7 @@ void config_wizard(int reason) try { ConfigWizard wizard(nullptr, static_cast(reason)); - wizard.run(g_PresetBundle, g_PresetUpdater); + wizard.run(wxGetApp().preset_bundle, wxGetApp().preset_updater); } catch (const std::exception &e) { show_error(nullptr, e.what()); @@ -560,24 +243,7 @@ void config_wizard(int reason) // Load the currently selected preset into the GUI, update the preset selection box. wxGetApp().load_current_presets(); } - -void open_preferences_dialog(int event_preferences) -{ - auto dlg = new PreferencesDialog(g_wxMainFrame, event_preferences); - dlg->ShowModal(); -} - // #ys_FIXME_for_delete -/* -void create_preset_tabs(int event_value_change, int event_presets_changed) -{ - update_label_colours_from_appconfig(); - add_created_tab(new TabPrint (g_wxTabPanel), event_value_change, event_presets_changed); - add_created_tab(new TabFilament (g_wxTabPanel), event_value_change, event_presets_changed); - add_created_tab(new TabSLAMaterial (g_wxTabPanel), event_value_change, event_presets_changed); - add_created_tab(new TabPrinter (g_wxTabPanel), event_value_change, event_presets_changed); -} -*/ std::vector preset_tabs = { { "print", nullptr, ptFFF }, { "filament", nullptr, ptFFF }, @@ -599,8 +265,8 @@ TabIface* get_preset_tab_iface(char *name) Tab* tab = get_tab(name); if (tab) return new TabIface(tab); - for (size_t i = 0; i < g_wxTabPanel->GetPageCount(); ++ i) { - Tab *tab = dynamic_cast(g_wxTabPanel->GetPage(i)); + for (size_t i = 0; i < wxGetApp().tab_panel()->GetPageCount(); ++i) { + Tab *tab = dynamic_cast(wxGetApp().tab_panel()->GetPage(i)); if (! tab) continue; if (tab->name() == name) { @@ -759,22 +425,14 @@ void set_print_callback_event(Print *print, int id) wxCommandEvent event(id); event.SetInt(percent); event.SetString(message); - wxQueueEvent(g_wxMainFrame, event.Clone()); + wxQueueEvent(wxGetApp().mainframe, event.Clone()); }); } -wxApp* get_app(){ - return g_wxApp; -} - wxWindow* get_right_panel(){ return g_right_panel; } -wxNotebook * get_tab_panel() { - return g_wxTabPanel; -} - const size_t& label_width(){ return m_label_width; } @@ -833,7 +491,7 @@ int combochecklist_get_flags(wxComboCtrl* comboCtrl) AppConfig* get_app_config() { - return g_AppConfig; + return wxGetApp().app_config; } wxString L_str(const std::string &str) @@ -875,7 +533,7 @@ void set_model_events_from_perl(Model &model, void add_frequently_changed_parameters(wxWindow* parent, wxBoxSizer* sizer, wxFlexGridSizer* preset_sizer) { - DynamicPrintConfig* config = &g_PresetBundle->prints.get_edited_preset().config; + DynamicPrintConfig* config = &wxGetApp().preset_bundle->prints.get_edited_preset().config; std::shared_ptr optgroup = std::make_shared(parent, "", config); const wxArrayInt& ar = preset_sizer->GetColWidths(); m_label_width = ar.IsEmpty() ? 100 : ar.front()-4; @@ -884,8 +542,8 @@ void add_frequently_changed_parameters(wxWindow* parent, wxBoxSizer* sizer, wxFl //Frequently changed parameters optgroup->m_on_change = [config](t_config_option_key opt_key, boost::any value){ TabPrint* tab_print = nullptr; - for (size_t i = 0; i < g_wxTabPanel->GetPageCount(); ++i) { - Tab *tab = dynamic_cast(g_wxTabPanel->GetPage(i)); + for (size_t i = 0; i < wxGetApp().tab_panel()->GetPageCount(); ++i) { + Tab *tab = dynamic_cast(wxGetApp().tab_panel()->GetPage(i)); if (!tab) continue; if (tab->name() == "print"){ @@ -976,7 +634,7 @@ void add_frequently_changed_parameters(wxWindow* parent, wxBoxSizer* sizer, wxFl sizer->Add(g_wiping_dialog_button); g_wiping_dialog_button->Bind(wxEVT_BUTTON, ([parent](wxCommandEvent& e) { - auto &config = g_PresetBundle->project_config; + auto &config = wxGetApp().preset_bundle->project_config; const std::vector &init_matrix = (config.option("wiping_volumes_matrix"))->values; const std::vector &init_extruders = (config.option("wiping_volumes_extruders"))->values; @@ -1010,8 +668,8 @@ void show_frequently_changed_parameters(bool show) g_frequently_changed_parameters_sizer->Show(show); if (!show) return; - for (size_t i = 0; i < g_wxTabPanel->GetPageCount(); ++i) { - Tab *tab = dynamic_cast(g_wxTabPanel->GetPage(i)); + for (size_t i = 0; i < wxGetApp().tab_panel()->GetPageCount(); ++i) { + Tab *tab = dynamic_cast(wxGetApp().tab_panel()->GetPage(i)); if (!tab) continue; tab->update_wiping_button_visibility(); @@ -1022,11 +680,11 @@ void show_frequently_changed_parameters(bool show) void show_buttons(bool show) { g_buttons[abReslice]->Show(show); - for (size_t i = 0; i < g_wxTabPanel->GetPageCount(); ++i) { - TabPrinter *tab = dynamic_cast(g_wxTabPanel->GetPage(i)); + for (size_t i = 0; i < wxGetApp().tab_panel()->GetPageCount(); ++i) { + TabPrinter *tab = dynamic_cast(wxGetApp().tab_panel()->GetPage(i)); if (!tab) continue; - if (g_PresetBundle->printers.get_selected_preset().printer_technology() == ptFFF) { + if (wxGetApp().preset_bundle->printers.get_selected_preset().printer_technology() == ptFFF) { g_buttons[abPrint]->Show(show && !tab->m_config->opt_string("serial_port").empty()); g_buttons[abSendGCode]->Show(show && !tab->m_config->opt_string("print_host").empty()); } @@ -1154,8 +812,8 @@ void save_window_size(wxTopLevelWindow *window, const std::string &name) const wxPoint pos = window->GetPosition(); const auto maximized = window->IsMaximized() ? "1" : "0"; - g_AppConfig->set((boost::format("window_%1%_size") % name).str(), (boost::format("%1%;%2%") % size.GetWidth() % size.GetHeight()).str()); - g_AppConfig->set((boost::format("window_%1%_maximized") % name).str(), maximized); + get_app_config()->set((boost::format("window_%1%_size") % name).str(), (boost::format("%1%;%2%") % size.GetWidth() % size.GetHeight()).str()); + get_app_config()->set((boost::format("window_%1%_maximized") % name).str(), maximized); } void restore_window_size(wxTopLevelWindow *window, const std::string &name) @@ -1174,8 +832,8 @@ void restore_window_size(wxTopLevelWindow *window, const std::string &name) try { const auto key_size = (boost::format("window_%1%_size") % name).str(); - if (g_AppConfig->has(key_size)) { - if (unescape_strings_cstyle(g_AppConfig->get(key_size), pair) && pair.size() == 2) { + if (get_app_config()->has(key_size)) { + if (unescape_strings_cstyle(get_app_config()->get(key_size), pair) && pair.size() == 2) { auto width = boost::lexical_cast(pair[0]); auto height = boost::lexical_cast(pair[1]); @@ -1187,7 +845,7 @@ void restore_window_size(wxTopLevelWindow *window, const std::string &name) // Maximizing should be the last thing to do. // This ensure the size and position are sane when the user un-maximizes the window. const auto key_maximized = (boost::format("window_%1%_maximized") % name).str(); - if (g_AppConfig->get(key_maximized) == "1") { + if (get_app_config()->get(key_maximized) == "1") { window->Maximize(true); } } diff --git a/src/slic3r/GUI/GUI.hpp b/src/slic3r/GUI/GUI.hpp index 83f954248..215a72b60 100644 --- a/src/slic3r/GUI/GUI.hpp +++ b/src/slic3r/GUI/GUI.hpp @@ -104,32 +104,11 @@ void enable_screensaver(); bool debugged(); void break_to_debugger(); -// Passing the wxWidgets GUI classes instantiated by the Perl part to C++. -void set_wxapp(wxApp *app); -void set_main_frame(wxFrame *main_frame); -void set_progress_status_bar(ProgressStatusBar *prsb); -void set_tab_panel(wxNotebook *tab_panel); -void set_plater(wxPanel *plater); -void set_app_config(AppConfig *app_config); -void set_preset_bundle(PresetBundle *preset_bundle); -void set_preset_updater(PresetUpdater *updater); -void set_objects_from_perl( wxWindow* parent, - wxBoxSizer *frequently_changed_parameters_sizer, - wxBoxSizer *info_sizer, - wxButton *btn_export_gcode, - wxButton *btn_reslice, - wxButton *btn_print, - wxButton *btn_send_gcode, - wxStaticBitmap *manifold_warning_icon); void set_show_print_info(bool show); void set_show_manifold_warning_icon(bool show); void set_objects_list_sizer(wxBoxSizer *objects_list_sizer); AppConfig* get_app_config(); -wxApp* get_app(); -wxFrame* get_main_frame(); -ProgressStatusBar* get_progress_status_bar(); -wxNotebook* get_tab_panel(); AppControllerPtr get_appctl(); void set_cli_appctl(); @@ -145,10 +124,6 @@ std::vector* get_preset_tabs(); extern void add_menus(wxMenuBar *menu, int event_preferences_changed, int event_language_change); -// This is called when closing the application, when loading a config file or when starting the config wizard -// to notify the user whether he is aware that some preset changes will be lost. -// extern bool check_unsaved_changes(); - // Checks if configuration wizard needs to run, calls config_wizard if so. // Returns whether the Wizard ran. extern bool config_wizard_startup(bool app_config_exists); @@ -157,11 +132,6 @@ extern bool config_wizard_startup(bool app_config_exists); // The run_reason argument is actually ConfigWizard::RunReason, but int is used here because of Perl. extern void config_wizard(int run_reason); -// Create "Preferences" dialog after selecting menu "Preferences" in Perl part -extern void open_preferences_dialog(int event_preferences); - -// Create a new preset tab (print, filament and printer), -// void create_preset_tabs(int event_value_change, int event_presets_changed); TabIface* get_preset_tab_iface(char *name); PreviewIface* create_preview_iface(wxNotebook* notebook, DynamicPrintConfig* config, Print* print, GCodePreviewData* gcode_preview_data); @@ -180,15 +150,6 @@ void warning_catcher(wxWindow* parent, const wxString& message); // to deliver a progress status message. void set_print_callback_event(Print *print, int id); -// load language saved at application config -wxLocale* get_locale(); -bool load_language(); -// save language at application config -void save_language(); -// get list of installed languages -void get_installed_languages(wxArrayString & names, wxArrayLong & identifiers); -// select language from the list of installed languages -bool select_language(wxArrayString & names, wxArrayLong & identifiers); // update right panel of the Plater according to view mode void update_mode(); diff --git a/src/slic3r/GUI/GUI_App.cpp b/src/slic3r/GUI/GUI_App.cpp index 6bca60568..4407c44c1 100644 --- a/src/slic3r/GUI/GUI_App.cpp +++ b/src/slic3r/GUI/GUI_App.cpp @@ -10,10 +10,10 @@ #include #include #include +#include #include "Utils.hpp" #include "GUI.hpp" -#include "MainFrame.hpp" #include "AppConfig.hpp" #include "PresetBundle.hpp" #include "3DScene.hpp" @@ -48,9 +48,7 @@ bool GUI_App::OnInit() set_data_dir(wxStandardPaths::Get().GetUserDataDir().ToUTF8().data()); app_config = new AppConfig(); - set_app_config(app_config); preset_bundle = new PresetBundle(); - set_preset_bundle(preset_bundle); // just checking for existence of Slic3r::data_dir is not enough : it may be an empty directory // supplied as argument to --datadir; in that case we should still run the wizard @@ -68,7 +66,6 @@ bool GUI_App::OnInit() app_config->save(); preset_updater = new PresetUpdater(); - set_preset_updater(preset_updater); load_language(); @@ -92,74 +89,56 @@ bool GUI_App::OnInit() std::cerr << "Creating main frame..." << std::endl; // wxImage::FindHandlerType(wxBITMAP_TYPE_PNG) || wxImage::AddHandler(new wxPNGHandler()); - mainframe = new Slic3r::GUI::MainFrame(no_plater, false); + mainframe = new MainFrame(no_plater, false); SetTopWindow(mainframe); // This makes CallAfter() work - // /*mainframe->*/Bind(wxEVT_IDLE, -// [this](wxIdleEvent& event) -// { -// std::function cur_cb{ nullptr }; -// // try to get the mutex. If we can't, just skip this idle event and get the next one. -// if (!callback_register.try_lock()) return; -// // pop callback -// if (m_cb.size() != 0){ -// cur_cb = m_cb.top(); -// m_cb.pop(); -// } -// // unlock mutex -// this->callback_register.unlock(); -// -// try { // call the function if it's not nullptr; -// if (cur_cb != nullptr) cur_cb(); -// } -// catch (std::exception& e) { -// // Slic3r::Log::error(LogChannel, LOG_WSTRING("Exception thrown: " << e.what())); // #ys_FIXME -// } -// -// if (app_config->dirty()) -// app_config->save(); -// } - ;// #ys_FIXME - // ); + Bind(wxEVT_IDLE, [this](wxIdleEvent& event) + { + std::function cur_cb{ nullptr }; + // try to get the mutex. If we can't, just skip this idle event and get the next one. + if (!callback_register.try_lock()) return; + // pop callback + if (m_cb.size() != 0){ + cur_cb = m_cb.top(); + m_cb.pop(); + } + // unlock mutex + this->callback_register.unlock(); + + try { // call the function if it's not nullptr; + if (cur_cb != nullptr) cur_cb(); + } + catch (std::exception& e) { + std::cerr << "Exception thrown: " << e.what() << std::endl; + } + + if (app_config->dirty()) + app_config->save(); + }); // On OS X the UI tends to freeze in weird ways if modal dialogs(config wizard, update notifications, ...) // are shown before or in the same event callback with the main frame creation. // Therefore we schedule them for later using CallAfter. -// CallAfter([this](){ -// // eval{ -// if (!preset_updater->config_update()) -// mainframe->Close(); -// // }; -// // if ($@) { -// // show_error(undef, $@); -// // mainframe->Close(); -// // } -// }); -// -// CallAfter([this](){ -// if (!Slic3r::GUI::config_wizard_startup(app_conf_exists)) { -// // Only notify if there was not wizard so as not to bother too much ... -// preset_updater->slic3r_update_notify(); -// } -// preset_updater->sync(preset_bundle); -// }); -// + CallAfter([this](){ + // eval{ + if (!preset_updater->config_update()) + mainframe->Close(); + // }; + // if ($@) { + // show_error(undef, $@); + // mainframe->Close(); + // } + }); + + CallAfter([this](){ + if (!config_wizard_startup(app_conf_exists)) { + // Only notify if there was not wizard so as not to bother too much ... + preset_updater->slic3r_update_notify(); + } + preset_updater->sync(preset_bundle); + }); - // #ys_FIXME All of this should to be removed - // # The following event is emited by the C++ menu implementation of preferences change. - // EVT_COMMAND($self, -1, $PREFERENCES_EVENT, sub{ - // $self->update_ui_from_settings; - // }); - // - // # The following event is emited by PresetUpdater(C++) to inform about - // # the newer Slic3r application version avaiable online. - // EVT_COMMAND($self, -1, $VERSION_ONLINE_EVENT, sub { - // my($self, $event) = @_; - // my $version = $event->GetString; - // $self->{app_config}->set('version_online', $version); - // $self->{app_config}->save; - // }); mainframe->Show(true); return true; @@ -238,7 +217,7 @@ void GUI_App::recreate_GUI() std::cerr << "recreate_GUI" << std::endl; auto topwindow = GetTopWindow(); - mainframe = new Slic3r::GUI::MainFrame(no_plater,false); + mainframe = new MainFrame(no_plater,false); if (topwindow) { SetTopWindow(mainframe); @@ -249,7 +228,7 @@ void GUI_App::recreate_GUI() // before the UI was up and running. CallAfter([](){ // Run the config wizard, don't offer the "reset user profile" checkbox. - Slic3r::GUI::config_wizard_startup(true); + config_wizard_startup(true); }); } @@ -374,7 +353,8 @@ void GUI_App::save_window_pos(wxTopLevelWindow* window, const std::string& name) app_config->save(); } -void GUI_App::restore_window_pos(wxTopLevelWindow* window, const std::string& name){ +void GUI_App::restore_window_pos(wxTopLevelWindow* window, const std::string& name) +{ if (!app_config->has(name + "_pos")) return; @@ -397,6 +377,37 @@ void GUI_App::restore_window_pos(wxTopLevelWindow* window, const std::string& na window->Maximize(); } +// select language from the list of installed languages +bool GUI_App::select_language( wxArrayString & names, + wxArrayLong & identifiers) +{ + wxCHECK_MSG(names.Count() == identifiers.Count(), false, + _(L("Array of language names and identifiers should have the same size."))); + int init_selection = 0; + long current_language = m_wxLocale ? m_wxLocale->GetLanguage() : wxLANGUAGE_UNKNOWN; + for (auto lang : identifiers){ + if (lang == current_language) + break; + ++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_wxLocale = new wxLocale; + m_wxLocale->Init(identifiers[index]); + m_wxLocale->AddCatalogLookupPathPrefix(localization_dir()); + m_wxLocale->AddCatalog(GetAppName()); + wxSetlocale(LC_NUMERIC, "C"); + Preset::update_suffix_modified(); + return true; + } + return false; +} + +// load language saved at application config bool GUI_App::load_language() { wxString language = wxEmptyString; @@ -412,11 +423,10 @@ bool GUI_App::load_language() { if (wxLocale::GetLanguageCanonicalName(identifiers[i]) == language) { - auto locale = get_locale(); - locale = new wxLocale; - locale->Init(identifiers[i]); - locale->AddCatalogLookupPathPrefix(wxPathOnly(localization_dir())); - locale->AddCatalog(GetAppName()); + m_wxLocale = new wxLocale; + m_wxLocale->Init(identifiers[i]); + m_wxLocale->AddCatalogLookupPathPrefix(localization_dir()); + m_wxLocale->AddCatalog(GetAppName()); wxSetlocale(LC_NUMERIC, "C"); Preset::update_suffix_modified(); return true; @@ -425,6 +435,50 @@ bool GUI_App::load_language() return false; } +// save language at application config +void GUI_App::save_language() +{ + wxString language = wxEmptyString; + if (m_wxLocale) + language = m_wxLocale->GetCanonicalName(); + + app_config->set("translation_language", language.ToStdString()); + app_config->save(); +} + +// get list of installed languages +void GUI_App::get_installed_languages(wxArrayString & names, wxArrayLong & identifiers) +{ + names.Clear(); + identifiers.Clear(); + + wxDir dir(localization_dir()); + wxString filename; + const wxLanguageInfo * langinfo; + wxString name = wxLocale::GetLanguageName(wxLANGUAGE_DEFAULT); + if (!name.IsEmpty()) + { + names.Add(_(L("Default"))); + identifiers.Add(wxLANGUAGE_DEFAULT); + } + for (bool cont = dir.GetFirst(&filename, wxEmptyString, wxDIR_DIRS); + cont; cont = dir.GetNext(&filename)) + { + langinfo = wxLocale::FindLanguageInfo(filename); + if (langinfo != NULL) + { + auto full_file_name = dir.GetName() + wxFileName::GetPathSeparator() + + filename + wxFileName::GetPathSeparator() + + GetAppName() + wxT(".mo"); + if (wxFileExists(full_file_name)) + { + names.Add(langinfo->Description); + identifiers.Add(langinfo->Language); + } + } + } +} + ConfigMenuIDs GUI_App::get_view_mode() { if (!app_config->has("view_mode")) @@ -498,8 +552,8 @@ void GUI_App::add_config_menu(wxMenuBar *menu) break; case ConfigMenuPreferences: { -// PreferencesDialog dlg(mainframe, event_preferences_changed); -// dlg.ShowModal(); + PreferencesDialog dlg(mainframe); + dlg.ShowModal(); break; } case ConfigMenuLanguage: diff --git a/src/slic3r/GUI/GUI_App.hpp b/src/slic3r/GUI/GUI_App.hpp index a6e2cdc7a..d6d085166 100644 --- a/src/slic3r/GUI/GUI_App.hpp +++ b/src/slic3r/GUI/GUI_App.hpp @@ -4,6 +4,7 @@ #include // #include #include "PrintConfig.hpp" +#include "MainFrame.hpp" // #include "../../libslic3r/Utils.hpp" // #include "GUI.hpp" @@ -39,7 +40,6 @@ enum ConfigMenuIDs { ConfigMenuCnt, }; -class MainFrame; class Tab; class GUI_App : public wxApp @@ -61,7 +61,7 @@ class GUI_App : public wxApp // #ys_FIXME // std::vector g_tabs_list; -// wxLocale* g_wxLocale{ nullptr }; + wxLocale* m_wxLocale{ nullptr }; public: bool OnInit() override; @@ -105,8 +105,14 @@ public: const std::string& icon); void save_window_pos(wxTopLevelWindow* window, const std::string& name); void restore_window_pos(wxTopLevelWindow* window, const std::string& name); + + bool select_language(wxArrayString & names, wxArrayLong & identifiers); bool load_language(); + void save_language(); + void get_installed_languages(wxArrayString & names, wxArrayLong & identifiers); + ConfigMenuIDs get_view_mode(); + void add_config_menu(wxMenuBar *menu); bool check_unsaved_changes(); bool checked_tab(Tab* tab); diff --git a/src/slic3r/GUI/GUI_ObjectParts.cpp b/src/slic3r/GUI/GUI_ObjectParts.cpp index d27494617..fc36e7fdb 100644 --- a/src/slic3r/GUI/GUI_ObjectParts.cpp +++ b/src/slic3r/GUI/GUI_ObjectParts.cpp @@ -758,9 +758,8 @@ void remove() if (m_objects_model->GetParent(item) == wxDataViewItem(0)) { if (m_event_remove_object > 0) { wxCommandEvent event(m_event_remove_object); - get_main_frame()->ProcessWindowEvent(event); +// get_main_frame()->ProcessWindowEvent(event); // #ys_FIXME } -// delete_object_from_list(); } else on_btn_del(); @@ -785,7 +784,7 @@ void object_ctrl_selection_changed() else event.SetInt(vol_idx); } - get_main_frame()->ProcessWindowEvent(event); +// get_main_frame()->ProcessWindowEvent(event); // #ys_FIXME } #ifdef __WXOSX__ @@ -1194,8 +1193,8 @@ void show_context_menu() const auto menu = m_objects_model->GetParent(item) == wxDataViewItem(0) ? create_add_part_popupmenu() : create_part_settings_popupmenu(); - get_tab_panel()->GetPage(0)->PopupMenu(menu); - } + wxGetApp().tab_panel()->GetPage(0)->PopupMenu(menu); + } } // ****** @@ -1203,7 +1202,7 @@ void show_context_menu() void load_part( ModelObject* model_object, wxArrayString& part_names, const bool is_modifier) { - wxWindow* parent = get_tab_panel()->GetPage(0); + wxWindow* parent = wxGetApp().tab_panel()->GetPage(0); wxArrayString input_files; open_model(parent, input_files); @@ -1550,7 +1549,7 @@ void parts_changed(int obj_idx) is_parts_changed() ? 1 : 0, is_part_settings_changed() ? 1 : 0); e.SetString(event_str); - get_main_frame()->ProcessWindowEvent(e); +// get_main_frame()->ProcessWindowEvent(e); // #ys_FIXME } void update_settings_value() @@ -1741,7 +1740,7 @@ void update_extruder_in_config(const wxString& selection) if (m_event_update_scene > 0) { wxCommandEvent e(m_event_update_scene); - get_main_frame()->ProcessWindowEvent(e); +// get_main_frame()->ProcessWindowEvent(e); // #ys_FIXME } } diff --git a/src/slic3r/GUI/MainFrame.cpp b/src/slic3r/GUI/MainFrame.cpp index 1aba0f7c7..23e7e0793 100644 --- a/src/slic3r/GUI/MainFrame.cpp +++ b/src/slic3r/GUI/MainFrame.cpp @@ -30,7 +30,6 @@ wxFrame(NULL, wxID_ANY, SLIC3R_BUILD, wxDefaultPosition, wxDefaultSize, wxDEFAUL m_no_plater(no_plater), m_loaded(loaded) { -// Slic3r::GUI::set_main_frame(this); m_appController = new Slic3r::AppController(); // Load the icon either from the exe, or from the ico file. @@ -54,7 +53,7 @@ wxFrame(NULL, wxID_ANY, SLIC3R_BUILD, wxDefaultPosition, wxDefaultSize, wxDEFAUL wxToolTip::SetAutoPop(32767); // initialize status bar - m_statusbar = new Slic3r::GUI::ProgressStatusBar(this); + m_statusbar = new ProgressStatusBar(this); m_statusbar->embed(this); m_statusbar->set_status_text(_(L("Version ")) + SLIC3R_VERSION + @@ -102,8 +101,8 @@ wxFrame(NULL, wxID_ANY, SLIC3R_BUILD, wxDefaultPosition, wxDefaultSize, wxDEFAUL event.Skip(); }); -// update_ui_from_settings(); -// + update_ui_from_settings(); + // Slic3r::GUI::update_mode(); return; @@ -112,15 +111,13 @@ wxFrame(NULL, wxID_ANY, SLIC3R_BUILD, wxDefaultPosition, wxDefaultSize, wxDEFAUL void MainFrame::init_tabpanel() { m_tabpanel = new wxNotebook(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxNB_TOP | wxTAB_TRAVERSAL); -// Slic3r::GUI::set_tab_panel(m_tabpanel); m_tabpanel->Bind(wxEVT_NOTEBOOK_PAGE_CHANGED, [this](wxEvent&){ auto panel = m_tabpanel->GetCurrentPage(); // panel->OnActivate(); if panel->can('OnActivate'); - if (panel == nullptr) { + if (panel == nullptr) return; - } for (auto& tab_name : { "print", "filament", "printer" }) { if (tab_name == panel->GetName()) { @@ -136,12 +133,6 @@ void MainFrame::init_tabpanel() if (!m_no_plater) { m_plater = new Slic3r::GUI::Plater(m_tabpanel, this); - // m_plater = new Slic3r::GUI::Plater(m_tabpanel, - // event_object_selection_changed = > $OBJECT_SELECTION_CHANGED_EVENT, - // event_object_settings_changed = > $OBJECT_SETTINGS_CHANGED_EVENT, - // event_remove_object = > $OBJECT_REMOVE_EVENT, - // event_update_scene = > $UPDATE_SCENE_EVENT, - // ), L("Plater") m_tabpanel->AddPage(m_plater, _(L("Plater"))); } diff --git a/src/slic3r/GUI/Preferences.cpp b/src/slic3r/GUI/Preferences.cpp index 89a8ead92..2037572da 100644 --- a/src/slic3r/GUI/Preferences.cpp +++ b/src/slic3r/GUI/Preferences.cpp @@ -5,11 +5,10 @@ namespace Slic3r { namespace GUI { -PreferencesDialog::PreferencesDialog(wxWindow* parent, int event_preferences) : - wxDialog(parent, wxID_ANY, _(L("Preferences")), wxDefaultPosition, wxDefaultSize), - m_event_preferences(event_preferences) { - build(); - } +PreferencesDialog::PreferencesDialog(wxWindow* parent) : + wxDialog(parent, wxID_ANY, _(L("Preferences")), wxDefaultPosition, wxDefaultSize) { + build(); +} void PreferencesDialog::build() { @@ -124,10 +123,7 @@ void PreferencesDialog::accept() Close(); // needed on Linux // Nothify the UI to update itself from the ini file. - if (m_event_preferences > 0) { - wxCommandEvent event(m_event_preferences); - get_app()->ProcessEvent(event); - } + wxGetApp().update_ui_from_settings(); } } // GUI diff --git a/src/slic3r/GUI/Preferences.hpp b/src/slic3r/GUI/Preferences.hpp index d01d78b70..0b1dd5730 100644 --- a/src/slic3r/GUI/Preferences.hpp +++ b/src/slic3r/GUI/Preferences.hpp @@ -15,9 +15,8 @@ class PreferencesDialog : public wxDialog { std::map m_values; std::shared_ptr m_optgroup; - int m_event_preferences; public: - PreferencesDialog(wxWindow* parent, int event_preferences); + PreferencesDialog(wxWindow* parent); ~PreferencesDialog(){ } void build(); diff --git a/src/slic3r/GUI/ProgressStatusBar.cpp b/src/slic3r/GUI/ProgressStatusBar.cpp index d92b63cfe..45c3aaa76 100644 --- a/src/slic3r/GUI/ProgressStatusBar.cpp +++ b/src/slic3r/GUI/ProgressStatusBar.cpp @@ -5,14 +5,14 @@ #include #include #include -#include "GUI.hpp" +#include "GUI_App.hpp" #include namespace Slic3r { ProgressStatusBar::ProgressStatusBar(wxWindow *parent, int id): - self(new wxStatusBar(parent ? parent : GUI::get_main_frame(), + self(new wxStatusBar(parent ? parent : GUI::wxGetApp().mainframe, id == -1? wxID_ANY : id)), m_timer(new wxTimer(self)), m_prog (new wxGauge(self, @@ -130,7 +130,7 @@ void ProgressStatusBar::run(int rate) void ProgressStatusBar::embed(wxFrame *frame) { - wxFrame* mf = frame ? frame : GUI::get_main_frame(); + wxFrame* mf = frame ? frame : GUI::wxGetApp().mainframe; mf->SetStatusBar(self); } diff --git a/src/slic3r/GUI/Tab.cpp b/src/slic3r/GUI/Tab.cpp index e4a1f5933..c83fd6df5 100644 --- a/src/slic3r/GUI/Tab.cpp +++ b/src/slic3r/GUI/Tab.cpp @@ -662,7 +662,7 @@ void Tab::load_key_value(const std::string& opt_key, const boost::any& value, bo update(); } -extern wxFrame *g_wxMainFrame; +// extern wxFrame *g_wxMainFrame; // #ys_FIXME void Tab::on_value_change(const std::string& opt_key, const boost::any& value) { @@ -680,11 +680,11 @@ void Tab::on_value_change(const std::string& opt_key, const boost::any& value) { int val = boost::any_cast(value); event.SetInt(val); - g_wxMainFrame->ProcessWindowEvent(event); +// g_wxMainFrame->ProcessWindowEvent(event);// #ys_FIXME return; } - g_wxMainFrame->ProcessWindowEvent(event); +// g_wxMainFrame->ProcessWindowEvent(event);// #ys_FIXME } if (opt_key == "fill_density") { @@ -737,7 +737,7 @@ void Tab::on_presets_changed() if (m_event_presets_changed > 0) { wxCommandEvent event(m_event_presets_changed); event.SetString(m_name); - g_wxMainFrame->ProcessWindowEvent(event); +// g_wxMainFrame->ProcessWindowEvent(event); // #ys_FIXME } update_preset_description_line(); }