From 478dd2a93fa66a3204461520a10023f6ae886f2b Mon Sep 17 00:00:00 2001
From: tamasmeszaros <meszaros.q@gmail.com>
Date: Mon, 2 Jul 2018 16:15:21 +0200
Subject: [PATCH] Proper localization of AppController

---
 xs/CMakeLists.txt                    |   1 +
 xs/src/slic3r/AppController.cpp      | 124 ++++++++++++++-------------
 xs/src/slic3r/AppController.hpp      |  32 ++++---
 xs/src/slic3r/AppControllerWx.cpp    |  79 ++++++++---------
 xs/src/slic3r/IProgressIndicator.hpp |   9 +-
 xs/src/slic3r/Strings.hpp            |  10 +++
 6 files changed, 140 insertions(+), 115 deletions(-)
 create mode 100644 xs/src/slic3r/Strings.hpp

diff --git a/xs/CMakeLists.txt b/xs/CMakeLists.txt
index a00c19a81..e4c4b8f46 100644
--- a/xs/CMakeLists.txt
+++ b/xs/CMakeLists.txt
@@ -270,6 +270,7 @@ add_library(libslic3r_gui STATIC
     ${LIBDIR}/slic3r/AppController.hpp
     ${LIBDIR}/slic3r/AppController.cpp
     ${LIBDIR}/slic3r/AppControllerWx.cpp
+    ${LIBDIR}/slic3r/Strings.hpp
 )
 
 add_library(admesh STATIC
diff --git a/xs/src/slic3r/AppController.cpp b/xs/src/slic3r/AppController.cpp
index 6b925c714..884481fc3 100644
--- a/xs/src/slic3r/AppController.cpp
+++ b/xs/src/slic3r/AppController.cpp
@@ -61,12 +61,21 @@ void AppControllerBoilerplate::progress_indicator(
 }
 
 void AppControllerBoilerplate::progress_indicator(unsigned statenum,
-                                                  const std::string &title,
-                                                  const std::string &firstmsg)
+                                                  const string &title,
+                                                  const string &firstmsg)
 {
     progressind_->m.lock();
     progressind_->store[std::this_thread::get_id()] =
-            create_progress_indicator(statenum, title, firstmsg);;
+            create_progress_indicator(statenum, title, firstmsg);
+    progressind_->m.unlock();
+}
+
+void AppControllerBoilerplate::progress_indicator(unsigned statenum,
+                                                  const string &title)
+{
+    progressind_->m.lock();
+    progressind_->store[std::this_thread::get_id()] =
+            create_progress_indicator(statenum, title);
     progressind_->m.unlock();
 }
 
@@ -173,8 +182,8 @@ void PrintController::slice(PrintObject *pobj)
 
     if(pobj->layers.empty())
         report_issue(IssueType::ERR,
-                     "No layers were detected. You might want to repair your "
-                     "STL file(s) or check their size or thickness and retry"
+                     _(L("No layers were detected. You might want to repair your "
+                     "STL file(s) or check their size or thickness and retry"))
                      );
 
     pobj->state.set_done(STEP_SLICE);
@@ -237,37 +246,35 @@ void PrintController::slice(AppControllerBoilerplate::ProgresIndicatorPtr pri)
 
     Slic3r::trace(3, "Starting the slicing process.");
 
-    pri->update(st+20, "Generating perimeters");
+    pri->update(st+20, _(L("Generating perimeters")));
     for(auto obj : print_->objects) make_perimeters(obj);
 
-    pri->update(st+60, "Infilling layers");
+    pri->update(st+60, _(L("Infilling layers")));
     for(auto obj : print_->objects) infill(obj);
 
-    pri->update(st+70, "Generating support material");
+    pri->update(st+70, _(L("Generating support material")));
     for(auto obj : print_->objects) gen_support_material(obj);
 
-    pri->message_fmt("Weight: %.1fg, Cost: %.1f",
-                                    print_->total_weight,
-                                    print_->total_cost);
-
+    pri->message_fmt(_(L("Weight: %.1fg, Cost: %.1f")),
+                     print_->total_weight, print_->total_cost);
     pri->state(st+85);
 
 
-    pri->update(st+88, "Generating skirt");
+    pri->update(st+88, _(L("Generating skirt")));
     make_skirt();
 
 
-    pri->update(st+90, "Generating brim");
+    pri->update(st+90, _(L("Generating brim")));
     make_brim();
 
-    pri->update(st+95, "Generating wipe tower");
+    pri->update(st+95, _(L("Generating wipe tower")));
     make_wipe_tower();
 
-    pri->update(st+100, "Done");
+    pri->update(st+100, _(L("Done")));
 
     // time to make some statistics..
 
-    Slic3r::trace(3, "Slicing process finished.");
+    Slic3r::trace(3, _(L("Slicing process finished.")));
 }
 
 
@@ -322,60 +329,57 @@ void PrintController::slice_to_png()
             unscale(print_bb.size().y) > exd.height_mm) {
         std::stringstream ss;
 
-        ss << _("Print will not fit and will be truncated!") << "\n"
-           << _("Width needed: ") << unscale(print_bb.size().x) << " mm\n"
-           << _("Height needed: ") << unscale(print_bb.size().y) << " mm\n";
+        ss << _(L("Print will not fit and will be truncated!")) << "\n"
+           << _(L("Width needed: ")) << unscale(print_bb.size().x) << " mm\n"
+           << _(L("Height needed: ")) << unscale(print_bb.size().y) << " mm\n";
 
         report_issue(IssueType::WARN, ss.str(), "Warning");
     }
 
-//    std::async(supports_asynch()? std::launch::async : std::launch::deferred,
-//               [this, exd, correction]()
-//    {
-        auto pri = create_progress_indicator(200, "Slicing to zipped png files...");
+    auto pri = create_progress_indicator(
+                200, _(L("Slicing to zipped png files...")));
 
-        try {
-            pri->update(0, "Slicing...");
-            slice(pri);
-        } catch (std::exception& e) {
-            report_issue(IssueType::ERR, e.what(), "Exception");
-            pri->cancel();
-            return;
+    try {
+        pri->update(0, _(L("Slicing...")));
+        slice(pri);
+    } catch (std::exception& e) {
+        report_issue(IssueType::ERR, e.what(), _(L("Exception occured")));
+        pri->cancel();
+        return;
+    }
+
+    auto pbak = print_->progressindicator;
+    print_->progressindicator = pri;
+
+    try {
+        print_to<FilePrinterFormat::PNG>( *print_, exd.zippath,
+                    exd.width_mm, exd.height_mm,
+                    exd.width_px, exd.height_px,
+                    exd.exp_time_s, exd.exp_time_first_s);
+
+    } catch (std::exception& e) {
+        report_issue(IssueType::ERR, e.what(), _(L("Exception occured")));
+        pri->cancel();
+    }
+
+    if(correction) { // scale the model back
+        print_->invalidate_all_steps();
+        for(auto po : print_->objects) {
+            po->model_object()->scale(
+                Pointf3(1.0/exd.corr_x, 1.0/exd.corr_y, 1.0/exd.corr_z)
+            );
+            po->model_object()->invalidate_bounding_box();
+            po->reload_model_instances();
+            po->invalidate_all_steps();
         }
+    }
 
-        auto pbak = print_->progressindicator;
-        print_->progressindicator = pri;
-
-        try {
-            print_to<FilePrinterFormat::PNG>( *print_, exd.zippath,
-                        exd.width_mm, exd.height_mm,
-                        exd.width_px, exd.height_px,
-                        exd.exp_time_s, exd.exp_time_first_s);
-
-        } catch (std::exception& e) {
-            report_issue(IssueType::ERR, e.what(), "Exception");
-            pri->cancel();
-        }
-
-        if(correction) { // scale the model back
-            print_->invalidate_all_steps();
-            for(auto po : print_->objects) {
-                po->model_object()->scale(
-                    Pointf3(1.0/exd.corr_x, 1.0/exd.corr_y, 1.0/exd.corr_z)
-                );
-                po->model_object()->invalidate_bounding_box();
-                po->reload_model_instances();
-                po->invalidate_all_steps();
-            }
-        }
-
-        print_->progressindicator = pbak;
-//    });
+    print_->progressindicator = pbak;
 
 }
 
 void IProgressIndicator::message_fmt(
-        const std::string &fmtstr, ...) {
+        const string &fmtstr, ...) {
     std::stringstream ss;
     va_list args;
     va_start(args, fmtstr);
diff --git a/xs/src/slic3r/AppController.hpp b/xs/src/slic3r/AppController.hpp
index 39e02a1a0..d37445599 100644
--- a/xs/src/slic3r/AppController.hpp
+++ b/xs/src/slic3r/AppController.hpp
@@ -44,7 +44,7 @@ public:
     AppControllerBoilerplate();
     ~AppControllerBoilerplate();
 
-    using Path = std::string;
+    using Path = string;
     using PathList = std::vector<Path>;
 
     /// Common runtime issue types
@@ -65,20 +65,20 @@ public:
      * @return Returns a list of paths choosed by the user.
      */
     PathList query_destination_paths(
-            const std::string& title,
+            const string& title,
             const std::string& extensions) const;
 
     /**
      * @brief Same as query_destination_paths but works for directories only.
      */
     PathList query_destination_dirs(
-            const std::string& title) const;
+            const string& title) const;
 
     /**
      * @brief Same as query_destination_paths but returns only one path.
      */
     Path query_destination_path(
-            const std::string& title,
+            const string& title,
             const std::string& extensions,
             const std::string& hint = "") const;
 
@@ -93,8 +93,11 @@ public:
      * title.
      */
     bool report_issue(IssueType issuetype,
-                      const std::string& description,
-                      const std::string& brief = "");
+                      const string& description,
+                      const string& brief);
+
+    bool report_issue(IssueType issuetype,
+                      const string& description);
 
     /**
      * @brief Set up a progress indicator for the current thread.
@@ -109,8 +112,11 @@ public:
      * @param firstmsg The message for the first subtask to be displayed.
      */
     void progress_indicator(unsigned statenum,
-                            const std::string& title,
-                            const std::string& firstmsg = "");
+                            const string& title,
+                            const string& firstmsg);
+
+    void progress_indicator(unsigned statenum,
+                            const string& title);
 
     /**
      * @brief Return the progress indicator set up for the current thread. This
@@ -160,8 +166,12 @@ protected:
      */
     ProgresIndicatorPtr create_progress_indicator(
             unsigned statenum,
-            const std::string& title,
-            const std::string& firstmsg = "") const;
+            const string& title,
+            const string& firstmsg) const;
+
+    ProgresIndicatorPtr create_progress_indicator(
+            unsigned statenum,
+            const string& title) const;
 
     // This is a global progress indicator placeholder. In the Slic3r UI it can
     // contain the progress indicator on the statusbar.
@@ -234,8 +244,6 @@ public:
      */
     void slice_to_png();
 
-
-    void slice_to_png(std::string dirpath);
 };
 
 /**
diff --git a/xs/src/slic3r/AppControllerWx.cpp b/xs/src/slic3r/AppControllerWx.cpp
index 2d3dc0fd3..e9368c531 100644
--- a/xs/src/slic3r/AppControllerWx.cpp
+++ b/xs/src/slic3r/AppControllerWx.cpp
@@ -33,11 +33,11 @@ void AppControllerBoilerplate::process_events()
 
 AppControllerBoilerplate::PathList
 AppControllerBoilerplate::query_destination_paths(
-        const std::string &title,
+        const string &title,
         const std::string &extensions) const
 {
 
-    wxFileDialog dlg(wxTheApp->GetTopWindow(), wxString(title) );
+    wxFileDialog dlg(wxTheApp->GetTopWindow(), title );
     dlg.SetWildcard(extensions);
 
     dlg.ShowModal();
@@ -53,7 +53,7 @@ AppControllerBoilerplate::query_destination_paths(
 
 AppControllerBoilerplate::Path
 AppControllerBoilerplate::query_destination_path(
-        const std::string &title,
+        const string &title,
         const std::string &extensions,
         const std::string& hint) const
 {
@@ -72,8 +72,8 @@ AppControllerBoilerplate::query_destination_path(
 }
 
 bool AppControllerBoilerplate::report_issue(IssueType issuetype,
-                                 const std::string &description,
-                                 const std::string &brief)
+                                 const string &description,
+                                 const string &brief)
 {
     auto icon = wxICON_INFORMATION;
     auto style = wxOK|wxCENTRE;
@@ -89,6 +89,13 @@ bool AppControllerBoilerplate::report_issue(IssueType issuetype,
     return ret != wxCANCEL;
 }
 
+bool AppControllerBoilerplate::report_issue(
+        AppControllerBoilerplate::IssueType issuetype,
+        const string &description)
+{
+    return report_issue(issuetype, description, string());
+}
+
 wxDEFINE_EVENT(PROGRESS_STATUS_UPDATE_EVENT, wxCommandEvent);
 
 namespace  {
@@ -104,7 +111,6 @@ class GuiProgressIndicator:
     using Base = IProgressIndicator;
     wxString message_;
     int range_; wxString title_;
-//    unsigned prc_ = 0;
     bool is_asynch_ = false;
 
     const int id_ = wxWindow::NewControlId();
@@ -118,20 +124,9 @@ class GuiProgressIndicator:
 
     // Status update implementation
     void _state( unsigned st) {
-//        if(st < max()) {
-            if(!gauge_.IsShown()) gauge_.ShowModal();
-            Base::state(st);
-            gauge_.Update(static_cast<int>(st), message_);
-//        }
-
-//        if(st == max()) {
-//            prc_++;
-//            if(prc_ == Base::procedure_count())  {
-//                //gauge_.reset();
-//                gauge_.Update(static_cast<int>(st), message_);
-//                prc_ = 0;
-//            }
-//        }
+        if(!gauge_.IsShown()) gauge_.ShowModal();
+        Base::state(st);
+        gauge_.Update(static_cast<int>(st), message_);
     }
 
 public:
@@ -142,12 +137,12 @@ public:
     /// Get the mode of parallel operation.
     inline bool asynch() const { return is_asynch_; }
 
-    inline GuiProgressIndicator(int range, const std::string& title,
-                                const std::string& firstmsg) :
+    inline GuiProgressIndicator(int range, const string& title,
+                                const string& firstmsg) :
         gauge_(title, firstmsg, range, wxTheApp->GetTopWindow(),
                wxPD_APP_MODAL | wxPD_AUTO_HIDE),
-        message_(_(firstmsg)),
-        range_(range), title_(_(title))
+        message_(firstmsg),
+        range_(range), title_(title)
     {
         Base::max(static_cast<float>(range));
         Base::states(static_cast<unsigned>(range));
@@ -163,7 +158,7 @@ public:
     }
 
     virtual void state(float val) override {
-        /*if( val >= 1.0) */state(static_cast<unsigned>(val));
+        state(static_cast<unsigned>(val));
     }
 
     void state(unsigned st) {
@@ -176,27 +171,26 @@ public:
         } else _state(st);
     }
 
-    virtual void message(const std::string & msg) override {
-        message_ = _(msg);
+    virtual void message(const string & msg) override {
+        message_ = msg;
     }
 
-    virtual void messageFmt(const std::string& fmt, ...) {
+    virtual void messageFmt(const string& fmt, ...) {
         va_list arglist;
         va_start(arglist, fmt);
-        message_ = wxString::Format(_(fmt), arglist);
+        message_ = wxString::Format(wxString(fmt), arglist);
         va_end(arglist);
     }
 
-    virtual void title(const std::string & title) override {
-        title_ = _(title);
+    virtual void title(const string & title) override {
+        title_ = title;
     }
 };
 }
 
 AppControllerBoilerplate::ProgresIndicatorPtr
 AppControllerBoilerplate::create_progress_indicator(
-        unsigned statenum, const std::string& title,
-        const std::string& firstmsg) const
+        unsigned statenum, const string& title, const string& firstmsg) const
 {
     auto pri =
             std::make_shared<GuiProgressIndicator>(statenum, title, firstmsg);
@@ -208,6 +202,13 @@ AppControllerBoilerplate::create_progress_indicator(
     return pri;
 }
 
+AppControllerBoilerplate::ProgresIndicatorPtr
+AppControllerBoilerplate::create_progress_indicator(unsigned statenum,
+                                                    const string &title) const
+{
+    return create_progress_indicator(statenum, title, string());
+}
+
 namespace {
 
 // A wrapper progress indicator class around the statusbar created in perl.
@@ -271,18 +272,18 @@ public:
         } else _state(st);
     }
 
-    virtual void message(const std::string & msg) override {
+    virtual void message(const string & msg) override {
         message_ = msg;
     }
 
-    virtual void message_fmt(const std::string& fmt, ...) override {
+    virtual void message_fmt(const string& fmt, ...) override {
         va_list arglist;
         va_start(arglist, fmt);
-        message_ = wxString::Format(_(fmt), arglist);
+        message_ = wxString::Format(wxString(fmt), arglist);
         va_end(arglist);
     }
 
-    virtual void title(const std::string & /*title*/) override {}
+    virtual void title(const string & /*title*/) override {}
 
 };
 }
@@ -396,8 +397,8 @@ PrintController::PngExportData PrintController::query_png_export_data()
 
             if(wxFileName(filepick_ctl_->GetPath()).Exists())
                 if(!ctl_.report_issue(PrintController::IssueType::WARN_Q,
-                                  "File already exists. Overwrite?",
-                                  "Warning")) ret = wxID_CANCEL;
+                                  _(L("File already exists. Overwrite?")),
+                                  _(L("Warning")))) ret = wxID_CANCEL;
             EndModal(ret);
         }
     };
diff --git a/xs/src/slic3r/IProgressIndicator.hpp b/xs/src/slic3r/IProgressIndicator.hpp
index ee68b3d2b..704931574 100644
--- a/xs/src/slic3r/IProgressIndicator.hpp
+++ b/xs/src/slic3r/IProgressIndicator.hpp
@@ -3,6 +3,7 @@
 
 #include <string>
 #include <functional>
+#include "Strings.hpp"
 
 namespace Slic3r {
 
@@ -42,13 +43,13 @@ public:
     }
 
     /// Message shown on the next status update.
-    virtual void message(const std::string&) = 0;
+    virtual void message(const string&) = 0;
 
     /// Title of the operaton.
-    virtual void title(const std::string&) = 0;
+    virtual void title(const string&) = 0;
 
     /// Formatted message for the next status update. Works just like sprinf.
-    virtual void message_fmt(const std::string& fmt, ...);
+    virtual void message_fmt(const string& fmt, ...);
 
     /// Set up a cancel callback for the operation if feasible.
     inline void on_cancel(CancelFn func) { cancelfunc_ = func; }
@@ -60,7 +61,7 @@ public:
     virtual void cancel() { cancelfunc_(); }
 
     /// Convinience function to call message and status update in one function.
-    void update(float st, const std::string& msg) {
+    void update(float st, const string& msg) {
         message(msg); state(st);
     }
 };
diff --git a/xs/src/slic3r/Strings.hpp b/xs/src/slic3r/Strings.hpp
new file mode 100644
index 000000000..b267fe064
--- /dev/null
+++ b/xs/src/slic3r/Strings.hpp
@@ -0,0 +1,10 @@
+#ifndef STRINGS_HPP
+#define STRINGS_HPP
+
+#include "GUI/GUI.hpp"
+
+namespace Slic3r {
+using string = wxString;
+}
+
+#endif // STRINGS_HPP