From 279a4ccd4047bbdbd645e2d32b80ac2163de0be3 Mon Sep 17 00:00:00 2001 From: Martyn Gigg Date: Thu, 3 Apr 2014 13:16:30 +0100 Subject: [PATCH] Add a utf8Label method to the Unit class. This allows the unit to return a wide string containing unicode character encodings, giving more flexibility in how units are presented. Refs #9252 --- .../Framework/Kernel/inc/MantidKernel/Unit.h | 20 +++++- Code/Mantid/Framework/Kernel/src/Unit.cpp | 9 +++ Code/Mantid/Framework/Kernel/test/UnitTest.h | 64 ++++++++++++++++++- 3 files changed, 89 insertions(+), 4 deletions(-) diff --git a/Code/Mantid/Framework/Kernel/inc/MantidKernel/Unit.h b/Code/Mantid/Framework/Kernel/inc/MantidKernel/Unit.h index 451e6b6f062d..cd640be0e1d6 100644 --- a/Code/Mantid/Framework/Kernel/inc/MantidKernel/Unit.h +++ b/Code/Mantid/Framework/Kernel/inc/MantidKernel/Unit.h @@ -60,6 +60,8 @@ class MANTID_KERNEL_DLL Unit /// A label for the unit to be printed on axes /// @return The unit label virtual const std::string label() const = 0; + /// A label string that can contain utf-8 character encodings. + virtual const std::wstring utf8Label() const; // Check whether the unit can be converted to another via a simple factor bool quickConversion(const Unit& destination, double& factor, double& power) const; @@ -282,7 +284,8 @@ class MANTID_KERNEL_DLL TOF : public Unit const std::string unitID() const; ///< "TOF" const std::string caption() const { return "Time-of-flight"; } const std::string label() const {return "microsecond"; } - + const std::wstring utf8Label() const { return L"\u03bcs"; } + virtual double singleToTOF(const double x) const; virtual double singleFromTOF(const double tof) const; virtual void init(); @@ -307,6 +310,7 @@ class MANTID_KERNEL_DLL Wavelength : public Unit const std::string unitID() const; ///< "Wavelength" const std::string caption() const { return "Wavelength"; } const std::string label() const {return "Angstrom"; } + const std::wstring utf8Label() const { return L"\u212b"; } virtual double singleToTOF(const double x) const; virtual double singleFromTOF(const double tof) const; @@ -364,6 +368,7 @@ class MANTID_KERNEL_DLL Energy_inWavenumber : public Unit const std::string unitID() const; ///< "Energy_inWavenumber" const std::string caption() const { return "Energy"; } const std::string label() const {return "1/cm"; } + const std::wstring utf8Label() const { return L"cm\u207b\u00b9"; } virtual double singleToTOF(const double x) const; virtual double singleFromTOF(const double tof) const; @@ -390,6 +395,7 @@ class MANTID_KERNEL_DLL dSpacing : public Unit const std::string unitID() const; ///< "dSpacing" const std::string caption() const { return "d-Spacing"; } const std::string label() const {return "Angstrom"; } + const std::wstring utf8Label() const { return L"\u212b"; } virtual double singleToTOF(const double x) const; virtual double singleFromTOF(const double tof) const; @@ -416,6 +422,7 @@ class MANTID_KERNEL_DLL MomentumTransfer : public Unit const std::string unitID() const; ///< "MomentumTransfer" const std::string caption() const { return "q"; } const std::string label() const {return "1/Angstrom"; } + const std::wstring utf8Label() const { return L"\u212b\u207b\u00b9"; } virtual double singleToTOF(const double x) const; virtual double singleFromTOF(const double tof) const; @@ -441,6 +448,7 @@ class MANTID_KERNEL_DLL QSquared : public Unit const std::string unitID() const; ///< "QSquared" const std::string caption() const { return "Q2"; } const std::string label() const {return "Angstrom^-2"; } + const std::wstring utf8Label() const { return L"\u212b\u207b\u00b2"; } virtual double singleToTOF(const double x) const; virtual double singleFromTOF(const double tof) const; @@ -497,6 +505,8 @@ class MANTID_KERNEL_DLL DeltaE_inWavenumber : public DeltaE const std::string unitID() const; ///< "DeltaE_inWavenumber" const std::string caption() const { return "Energy transfer"; } const std::string label() const {return "1/cm"; } + const std::wstring utf8Label() const { return L"cm\u207b\u00b9"; } + virtual void init(); virtual Unit * clone() const; @@ -544,6 +554,7 @@ class MANTID_KERNEL_DLL Momentum : public Unit const std::string unitID() const; ///< "Momentum" const std::string caption() const { return "Momentum"; } const std::string label() const {return "Angstrom^-1"; } + const std::wstring utf8Label() const { return L"\u212b\u207b\u00b9"; } virtual double singleToTOF(const double x) const; virtual double singleFromTOF(const double tof) const; @@ -573,7 +584,8 @@ class MANTID_KERNEL_DLL SpinEchoLength : public Wavelength const std::string unitID() const; ///< "SpinEchoLength" const std::string caption() const { return "Spin Echo Length"; } const std::string label() const {return "nm"; } - + const std::wstring utf8Label() const { return L"nm"; } + virtual double singleToTOF(const double x) const; virtual double singleFromTOF(const double tof) const; virtual void init(); @@ -596,7 +608,8 @@ class MANTID_KERNEL_DLL SpinEchoTime : public Wavelength const std::string unitID() const; ///< "SpinEchoTime" const std::string caption() const { return "Spin Echo Time"; } const std::string label() const {return "ns"; } - + const std::wstring utf8Label() const { return L"ns"; } + virtual double singleToTOF(const double x) const; virtual double singleFromTOF(const double tof) const; virtual void init(); @@ -620,6 +633,7 @@ class MANTID_KERNEL_DLL Time : public Unit const std::string unitID() const; ///< "Time" const std::string caption() const { return "t"; } const std::string label() const {return "Second"; } + const std::wstring utf8Label() const { return L"s"; } virtual double singleToTOF(const double x) const; virtual double singleFromTOF(const double tof) const; diff --git a/Code/Mantid/Framework/Kernel/src/Unit.cpp b/Code/Mantid/Framework/Kernel/src/Unit.cpp index 3784922264b4..708364e566ef 100644 --- a/Code/Mantid/Framework/Kernel/src/Unit.cpp +++ b/Code/Mantid/Framework/Kernel/src/Unit.cpp @@ -13,6 +13,15 @@ namespace Mantid namespace Kernel { +/** + * @return A label string that can contain utf-8 character encodings. + * Default returns contents of label() + */ +const std::wstring Unit::utf8Label() const +{ + auto lbl = this->label(); + return std::wstring(lbl.begin(), lbl.end()); +} /// Copy Constructor Unit::Unit(const Unit & other) : initialized(false), diff --git a/Code/Mantid/Framework/Kernel/test/UnitTest.h b/Code/Mantid/Framework/Kernel/test/UnitTest.h index 928462808eb5..af1e462d17e3 100644 --- a/Code/Mantid/Framework/Kernel/test/UnitTest.h +++ b/Code/Mantid/Framework/Kernel/test/UnitTest.h @@ -98,6 +98,7 @@ std::string convert_units_check_range(const Unit &aUnit,std::vector &sam class UnitTest : public CxxTest::TestSuite { + class UnitTester : public Unit { public: @@ -249,6 +250,11 @@ class UnitTest : public CxxTest::TestSuite TS_ASSERT_EQUALS( tof.label(), "microsecond" ) } + void testTOF_utf8Label() + { + TS_ASSERT_EQUALS( tof.utf8Label(), L"\u03bcs" ) + } + void testTOF_cast() { Unit *u = NULL; @@ -309,6 +315,11 @@ class UnitTest : public CxxTest::TestSuite TS_ASSERT_EQUALS( lambda.label(), "Angstrom" ) } + void testWavelength_utf8Label() + { + TS_ASSERT_EQUALS( lambda.utf8Label(), L"\u212b" ) + } + void testWavelength_cast() { Unit *u = NULL; @@ -391,6 +402,12 @@ class UnitTest : public CxxTest::TestSuite TS_ASSERT_EQUALS( energy.label(), "meV" ) } + void testEnergy_utf8Label() + { + TS_ASSERT_EQUALS( energy.utf8Label(), L"meV" ) + } + + void testEnergy_cast() { Unit *u = NULL; @@ -475,6 +492,11 @@ class UnitTest : public CxxTest::TestSuite TS_ASSERT_EQUALS( energyk.label(), "1/cm" ) } + void testEnergy_inWavenumber_utf8Label() + { + TS_ASSERT_EQUALS( energyk.utf8Label(), L"cm\u207b\u00b9" ) + } + void testEnergy_inWavenumber_cast() { Unit *u = NULL; @@ -540,6 +562,11 @@ class UnitTest : public CxxTest::TestSuite TS_ASSERT_EQUALS( d.label(), "Angstrom" ) } + void testdSpacing_utf8Label() + { + TS_ASSERT_EQUALS( d.utf8Label(), L"\u212b" ) + } + void testdSpacing_cast() { Unit *u = NULL; @@ -628,6 +655,11 @@ class UnitTest : public CxxTest::TestSuite TS_ASSERT_EQUALS( q.label(), "1/Angstrom" ) } + void testQTransfer_utf8Label() + { + TS_ASSERT_EQUALS( q.utf8Label(), L"\u212b\u207b\u00b9" ) + } + void testQTransfer_cast() { Unit *u = NULL; @@ -716,6 +748,11 @@ class UnitTest : public CxxTest::TestSuite TS_ASSERT_EQUALS( q2.label(), "Angstrom^-2" ) } + void testQ2_utf8Label() + { + TS_ASSERT_EQUALS( q2.utf8Label(), L"\u212b\u207b\u00b2" ) + } + void testQ2_cast() { Unit *u = NULL; @@ -805,6 +842,11 @@ class UnitTest : public CxxTest::TestSuite TS_ASSERT_EQUALS( dE.label(), "meV" ) } + void testDeltaE_utf8Label() + { + TS_ASSERT_EQUALS( dE.utf8Label(), L"meV" ) + } + void testDeltaE_cast() { Unit *u = NULL; @@ -877,7 +919,7 @@ class UnitTest : public CxxTest::TestSuite //---------------------------------------------------------------------- - // Energy transfer tests + // Energy transfer in wavenumber tests //---------------------------------------------------------------------- void testDeltaEk_unitID() @@ -895,6 +937,11 @@ class UnitTest : public CxxTest::TestSuite TS_ASSERT_EQUALS( dEk.label(), "1/cm" ) } + void testDeltaEk_utf8Label() + { + TS_ASSERT_EQUALS( dEk.utf8Label(), L"cm\u207b\u00b9" ) + } + void testDeltaEk_cast() { Unit *u = NULL; @@ -982,6 +1029,11 @@ class UnitTest : public CxxTest::TestSuite TS_ASSERT_EQUALS( k_i.label(), "Angstrom^-1" ) } + void testMomentum_utf8Label() + { + TS_ASSERT_EQUALS( k_i.utf8Label(), L"\u212b\u207b\u00b9" ) + } + void testMomentum_cast() { Unit *u = NULL; @@ -1113,6 +1165,11 @@ class UnitTest : public CxxTest::TestSuite TS_ASSERT_EQUALS( delta.label(), "nm" ) } + void testSpinEchoLength_utf8Label() + { + TS_ASSERT_EQUALS( delta.utf8Label(), L"nm" ) + } + void testSpinEchoLength_cast() { Unit *u = NULL; @@ -1198,6 +1255,11 @@ class UnitTest : public CxxTest::TestSuite TS_ASSERT_EQUALS( tau.label(), "ns" ) } + void testSpinEchoTime_utf8Llabel() + { + TS_ASSERT_EQUALS( tau.utf8Label(), L"ns" ) + } + void testSpinEchoTime_cast() { Unit *u = NULL;