From 8dc3a6184595c787051a697b9b6767335282cde6 Mon Sep 17 00:00:00 2001
From: Samuel Jackson <samueljackson@outlook.com>
Date: Fri, 15 Sep 2017 15:15:50 +0100
Subject: [PATCH] fix locale issues

---
 Framework/API/inc/MantidAPI/Column.h          |  2 +-
 .../inc/MantidDataObjects/PeakColumn.h        |  2 +-
 .../inc/MantidDataObjects/TableColumn.h       | 19 ++++++++++++++++---
 .../inc/MantidDataObjects/VectorColumn.h      |  2 +-
 Framework/DataObjects/src/PeakColumn.cpp      |  3 ++-
 MantidPlot/src/ConfigDialog.cpp               |  3 ++-
 MantidPlot/src/Mantid/MantidTable.cpp         | 11 +++++++----
 7 files changed, 30 insertions(+), 12 deletions(-)

diff --git a/Framework/API/inc/MantidAPI/Column.h b/Framework/API/inc/MantidAPI/Column.h
index d5ea4e097c2..15129617522 100644
--- a/Framework/API/inc/MantidAPI/Column.h
+++ b/Framework/API/inc/MantidAPI/Column.h
@@ -92,7 +92,7 @@ public:
   }
 
   /// Read in from stream and set the value at the given index
-  virtual void read(const size_t index, std::istream &in) {
+  virtual void read(const size_t index, std::istringstream &in) {
     UNUSED_ARG(index)
     UNUSED_ARG(in)
   }
diff --git a/Framework/DataObjects/inc/MantidDataObjects/PeakColumn.h b/Framework/DataObjects/inc/MantidDataObjects/PeakColumn.h
index 33fd493ebb0..95384de57ac 100644
--- a/Framework/DataObjects/inc/MantidDataObjects/PeakColumn.h
+++ b/Framework/DataObjects/inc/MantidDataObjects/PeakColumn.h
@@ -42,7 +42,7 @@ public:
   void read(size_t index, const std::string &text) override;
 
   /// Sets item from a stream
-  void read(const size_t index, std::istream &in) override;
+  void read(const size_t index, std::istringstream &in) override;
 
   /// Specialized type check
   bool isBool() const override;
diff --git a/Framework/DataObjects/inc/MantidDataObjects/TableColumn.h b/Framework/DataObjects/inc/MantidDataObjects/TableColumn.h
index 5d711872528..eee53c5d06d 100644
--- a/Framework/DataObjects/inc/MantidDataObjects/TableColumn.h
+++ b/Framework/DataObjects/inc/MantidDataObjects/TableColumn.h
@@ -123,7 +123,7 @@ public:
   /// Read in a string and set the value at the given index
   void read(size_t index, const std::string &text) override;
   /// Read in from stream and set the value at the given index
-  void read(const size_t index, std::istream &in) override;
+  void read(const size_t index, std::istringstream &in) override;
   /// Type check
   bool isBool() const override { return typeid(Type) == typeid(API::Boolean); }
   /// Memory used by the column
@@ -245,6 +245,17 @@ inline void TableColumn<std::string>::read(size_t index,
   m_data[index] = text;
 }
 
+/// Template specialization for strings so they can contain spaces
+template <>
+inline void TableColumn<std::string>::read(size_t index,
+                                           std::istringstream &text) {
+  /* As opposed to other types, assigning strings via a stream does not work if
+   * it contains a whitespace character, so instead the assignment operator is
+   * used.
+   */
+  m_data[index] = text.str();
+}
+
 /// Read in a string and set the value at the given index
 template <typename Type>
 void TableColumn<Type>::read(size_t index, const std::string &text) {
@@ -254,8 +265,10 @@ void TableColumn<Type>::read(size_t index, const std::string &text) {
 
 /// Read in from stream and set the value at the given index
 template <typename Type>
-void TableColumn<Type>::read(size_t index, std::istream &in) {
-  in >> m_data[index];
+void TableColumn<Type>::read(size_t index, std::istringstream &in) {
+  Type t;
+  in >> t;
+  m_data[index] = t;
 }
 
 namespace {
diff --git a/Framework/DataObjects/inc/MantidDataObjects/VectorColumn.h b/Framework/DataObjects/inc/MantidDataObjects/VectorColumn.h
index cb957ad03f0..5a28e0496f7 100644
--- a/Framework/DataObjects/inc/MantidDataObjects/VectorColumn.h
+++ b/Framework/DataObjects/inc/MantidDataObjects/VectorColumn.h
@@ -93,7 +93,7 @@ public:
   }
 
   /// Set item from a stream
-  void read(const size_t index, std::istream &in) override {
+  void read(const size_t index, std::istringstream &in) override {
     std::string s;
     in >> s;
     read(index, s);
diff --git a/Framework/DataObjects/src/PeakColumn.cpp b/Framework/DataObjects/src/PeakColumn.cpp
index 3fada157e20..1ca1c4224ea 100644
--- a/Framework/DataObjects/src/PeakColumn.cpp
+++ b/Framework/DataObjects/src/PeakColumn.cpp
@@ -5,6 +5,7 @@
 #include "MantidKernel/Exception.h"
 #include "MantidKernel/MultiThreaded.h"
 
+#include <boost/algorithm/string.hpp>
 #include <boost/variant/get.hpp>
 
 using namespace Mantid::Kernel;
@@ -184,7 +185,7 @@ void PeakColumn::read(size_t index, const std::string &text) {
  * @param index :: index of the peak to modify
  * @param in :: input stream
  */
-void PeakColumn::read(const size_t index, std::istream &in) {
+void PeakColumn::read(const size_t index, std::istringstream &in) {
   if (this->getReadOnly() || index >= m_peaks.size())
     return;
 
diff --git a/MantidPlot/src/ConfigDialog.cpp b/MantidPlot/src/ConfigDialog.cpp
index d2a57e3a9d6..4add1578d8b 100644
--- a/MantidPlot/src/ConfigDialog.cpp
+++ b/MantidPlot/src/ConfigDialog.cpp
@@ -2591,9 +2591,10 @@ void ConfigDialog::apply() {
     QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
     QList<MdiSubWindow *> windows = app->windowsList();
     foreach (MdiSubWindow *w, windows) {
+      auto oldLocale = w->locale();
       w->setLocale(locale);
       if (auto table = dynamic_cast<Table *>(w))
-        table->updateDecimalSeparators();
+        table->updateDecimalSeparators(oldLocale);
       else if (auto matrix = dynamic_cast<Matrix *>(w))
         matrix->resetView();
     }
diff --git a/MantidPlot/src/Mantid/MantidTable.cpp b/MantidPlot/src/Mantid/MantidTable.cpp
index 57b0de391e2..68c593f6985 100644
--- a/MantidPlot/src/Mantid/MantidTable.cpp
+++ b/MantidPlot/src/Mantid/MantidTable.cpp
@@ -105,6 +105,9 @@ void MantidTable::fillTable() {
     Mantid::API::Column_sptr c = m_ws->getColumn(static_cast<int>(i));
     QString colName = QString::fromStdString(c->name());
     setColName(i, colName);
+    if (c->type() == "str") {
+      setColumnType(i, ColType::Text);
+    }
     // Make columns of ITableWorkspaces read only, if specified
     setReadOnlyColumn(i, c->getReadOnly());
 
@@ -294,14 +297,14 @@ void MantidTable::cellEdited(int row, int col) {
   // Have the column convert the text to a value internally
   int index = row;
   std::istringstream textStream(text);
-  const std::locale systemLocale("");
-  textStream.imbue(systemLocale);
-  c->read(index, textStream.str());
+  const std::locale applicationLocale(locale().name().toStdString());
+  textStream.imbue(applicationLocale);
+  c->read(index, textStream);
 
   // Set the table view to be the same text after editing.
   // That way, if the string was stupid, it will be reset to the old value.
   std::ostringstream s;
-  s.imbue(systemLocale);
+  s.imbue(applicationLocale);
 
   // Avoid losing precision for numeric data
   if (c->type() == "double") {
-- 
GitLab