Skip to content

Commit

Permalink
Add a utf8Label method to the Unit class.
Browse files Browse the repository at this point in the history
This allows the unit to return a wide string containing unicode character
encodings, giving more flexibility in how units are presented.
Refs #9252
  • Loading branch information
martyngigg committed Apr 3, 2014
1 parent bbb7336 commit 279a4cc
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 4 deletions.
20 changes: 17 additions & 3 deletions Code/Mantid/Framework/Kernel/inc/MantidKernel/Unit.h
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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;
Expand Down
9 changes: 9 additions & 0 deletions Code/Mantid/Framework/Kernel/src/Unit.cpp
Expand Up @@ -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),
Expand Down
64 changes: 63 additions & 1 deletion Code/Mantid/Framework/Kernel/test/UnitTest.h
Expand Up @@ -98,6 +98,7 @@ std::string convert_units_check_range(const Unit &aUnit,std::vector<double> &sam

class UnitTest : public CxxTest::TestSuite
{

class UnitTester : public Unit
{
public:
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -877,7 +919,7 @@ class UnitTest : public CxxTest::TestSuite


//----------------------------------------------------------------------
// Energy transfer tests
// Energy transfer in wavenumber tests
//----------------------------------------------------------------------

void testDeltaEk_unitID()
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 279a4cc

Please sign in to comment.