Skip to content

Commit

Permalink
Add a PlotAxis class.
Browse files Browse the repository at this point in the history
It will handle creating axis titles for a given workspace. This keeps
information in a single place.
Refs #9252
  • Loading branch information
martyngigg committed Apr 7, 2014
1 parent 200e4ca commit 8286acd
Show file tree
Hide file tree
Showing 4 changed files with 240 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Code/Mantid/MantidQt/API/CMakeLists.txt
Expand Up @@ -17,6 +17,7 @@ set ( SRC_FILES
src/MantidWidget.cpp
src/Message.cpp
src/OptionsPropertyWidget.cpp
src/PlotAxis.cpp
src/PropertyWidget.cpp
src/PropertyWidgetFactory.cpp
src/PythonRunner.cpp
Expand Down Expand Up @@ -67,6 +68,7 @@ set ( INC_FILES
inc/MantidQtAPI/HelpWindow.h
inc/MantidQtAPI/InterfaceFactory.h
inc/MantidQtAPI/InterfaceManager.h
inc/MantidQtAPI/PlotAxis.h
inc/MantidQtAPI/MantidColorMap.h
inc/MantidQtAPI/MantidQwtIMDWorkspaceData.h
inc/MantidQtAPI/MantidQwtMatrixWorkspaceData.h
Expand All @@ -85,6 +87,7 @@ set( TEST_FILES
InterfaceManagerTest.h
MantidColorMapTest.h
MantidQwtMatrixWorkspaceDataTest.h
PlotAxisTest.h
SelectionNotificationServiceTest.h
)

Expand Down
64 changes: 64 additions & 0 deletions Code/Mantid/MantidQt/API/inc/MantidQtAPI/PlotAxis.h
@@ -0,0 +1,64 @@
#ifndef MANTIDQT_API_PLOTAXISLABEL_H_
#define MANTIDQT_API_PLOTAXISLABEL_H_

#include "MantidQtAPI/DllOption.h"
#include "MantidAPI/MatrixWorkspace.h"

#include <QString>

namespace MantidQt
{
namespace API
{

/**
Deals with formatting a label for a plot axis for a given type of workspace
Copyright &copy; 2014 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National Laboratory
This file is part of Mantid.
Mantid is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
Mantid is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
File change history is stored at: <https://github.com/mantidproject/mantid>
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
class EXPORT_OPT_MANTIDQT_API PlotAxis
{
public:
/// Constructor with workspace & axis index
PlotAxis(const Mantid::API::MatrixWorkspace_const_sptr & workspace,
const size_t index);

/// Create a new axis title
QString title() const;

private:
DISABLE_DEFAULT_CONSTRUCT(PlotAxis);

/// Creates a title suitable for an axis attached to the given index
void titleFromIndex(const Mantid::API::MatrixWorkspace_const_sptr & workspace,
const size_t index);

/// Creates a title suitable for the Y axis
void initYAxisTitle(const Mantid::API::MatrixWorkspace_const_sptr & workspace);

/// Title
QString m_title;
};

} // namespace API
} // namespace MantidQt

#endif /* MANTIDQT_API_PLOTAXISLABEL_H_ */
79 changes: 79 additions & 0 deletions Code/Mantid/MantidQt/API/src/PlotAxis.cpp
@@ -0,0 +1,79 @@
#include "MantidQtAPI/PlotAxis.h"
#include <QStringBuilder>

namespace MantidQt
{
namespace API
{

/**
* @param workspace A pointer to a MatrixWorkspace object
* @param index Index of the axis in the workspace to inspect
*/
PlotAxis::PlotAxis(const Mantid::API::MatrixWorkspace_const_sptr &workspace,
const size_t index)
: m_title()
{
if (index == 0 || index == 1) titleFromIndex(workspace, index);
else
throw std::invalid_argument("PlotAxis() - Unknown axis index: '" + \
boost::lexical_cast<std::string>(index) + "'");
}

/**
* @return A new title for this axis
*/
QString PlotAxis::title() const
{
return m_title;
}

/**
* @param workspace A pointer to a MatrixWorkspace object
* @param index Index of the axis in the workspace to inspect
*/
void PlotAxis::titleFromIndex(const Mantid::API::MatrixWorkspace_const_sptr &workspace,
const size_t index)
{
// Deal with axis names
Mantid::API::Axis* ax = workspace->getAxis(index);
m_title = "";
if ( ax->isSpectra() ) m_title = "Spectrum Number";
else if (ax->unit() && ax->unit()->unitID() != "Empty" )
{
m_title = QString::fromStdString(ax->unit()->caption());
const auto lbl = ax->unit()->label();
if ( !lbl.utf8().empty() )
{
m_title += " (" + QString::fromStdWString(lbl.utf8()) + ")";
}
}
else if (!ax->title().empty())
{
m_title = QString::fromStdString(ax->title());
}
else
{
m_title = (index == 0) ? "X axis" : "Y axis";
}
}

/**
* @param workspace A pointer to a MatrixWorkspace object
*/
void PlotAxis::initYAxisTitle(const Mantid::API::MatrixWorkspace_const_sptr &workspace)
{
m_title = QString::fromStdString(workspace->YUnitLabel());
Mantid::API::Axis* ax = workspace->getAxis(0);
if (workspace->isDistribution() && ax->unit())
{
const std::string unitID = ax->unit()->unitID();
if (unitID != "" || unitID != "Empty")
{
m_title = m_title % " / " % ax->unit()->label().ascii().c_str();
}
}
}

} // namespace API
} // namespace MantidQt
94 changes: 94 additions & 0 deletions Code/Mantid/MantidQt/API/test/PlotAxisTest.h
@@ -0,0 +1,94 @@
#ifndef MANTID_API_PLOTAXISLABELTEST_H_
#define MANTID_API_AXISLABELTEST_H_

#include <cxxtest/TestSuite.h>

#include "MantidQtAPI/PlotAxis.h"
#include "MantidAPI/NumericAxis.h"
#include "MantidAPI/SpectraAxis.h"
#include "MantidTestHelpers/WorkspaceCreationHelper.h"

class PlotAxisTest : public CxxTest::TestSuite
{
public:
// This pair of boilerplate methods prevent the suite being created statically
// This means the constructor isn't called when running other tests
static PlotAxisTest *createSuite() { return new PlotAxisTest(); }
static void destroySuite( PlotAxisTest *suite ) { delete suite; }

void test_NoUnit_On_Indexed_Axis_Prints_Default()
{
using MantidQt::API::PlotAxis;
auto ws = WorkspaceCreationHelper::Create2DWorkspace(1,1);
ws->replaceAxis(1, new Mantid::API::NumericAxis(1));

TS_ASSERT_EQUALS("X axis", PlotAxis(ws, 0).title());
TS_ASSERT_EQUALS("Y axis", PlotAxis(ws, 1).title());
}

void test_Empty_Unit_And_Empty_Axis_Title_On_Indexed_Axis_Prints_Default()
{
using MantidQt::API::PlotAxis;
auto ws = WorkspaceCreationHelper::Create2DWorkspace(1,1);
ws->getAxis(0)->setUnit("Empty");
ws->replaceAxis(1, new Mantid::API::NumericAxis(1));
ws->getAxis(1)->setUnit("Empty");

TS_ASSERT_EQUALS("X axis", PlotAxis(ws, 0).title());
TS_ASSERT_EQUALS("Y axis", PlotAxis(ws, 1).title());
}

void test_Empty_Unit_And_Non_Empty_Title_On_Indexed_Axis_Prints_Title()
{
using MantidQt::API::PlotAxis;
auto ws = WorkspaceCreationHelper::Create2DWorkspace(1,1);
ws->getAxis(0)->setUnit("Empty");
auto * ax0 = ws->getAxis(0);
ax0->setUnit("Empty");
ax0->title() = "Custom title 1";
ws->replaceAxis(1, new Mantid::API::NumericAxis(1));
auto * ax1 = ws->getAxis(1);
ax1->setUnit("Empty");
ax1->title() = "Custom title 2";

TS_ASSERT_EQUALS("Custom title 1", PlotAxis(ws, 0).title());
TS_ASSERT_EQUALS("Custom title 2", PlotAxis(ws, 1).title());
}

void test_Axis_With_Unit_Has_Label_In_Parentheses()
{
using MantidQt::API::PlotAxis;
auto ws = WorkspaceCreationHelper::Create2DWorkspace(1,1);
ws->getAxis(0)->setUnit("TOF");
ws->replaceAxis(1, new Mantid::API::NumericAxis(1));
ws->getAxis(1)->setUnit("TOF");

QString expected = QString::fromUtf8("Time-of-flight (\u03bcs)");
TS_ASSERT_EQUALS(expected, PlotAxis(ws, 0).title());
TS_ASSERT_EQUALS(expected, PlotAxis(ws, 1).title());
}

void test_SpectraAxis_Gives_Standard_Text()
{
using MantidQt::API::PlotAxis;
auto ws = WorkspaceCreationHelper::Create2DWorkspace(1,1);
ws->replaceAxis(0, new Mantid::API::SpectraAxis(ws.get()));

TS_ASSERT_EQUALS("Spectrum Number", PlotAxis(ws, 0).title());
TS_ASSERT_EQUALS("Spectrum Number", PlotAxis(ws, 1).title());
}

//---------------------- Failure cases -------------------------------

void test_Index_Greater_Than_1_Or_Less_Than_Zero_Throws_Invalid_Argument()
{
using MantidQt::API::PlotAxis;
auto ws = WorkspaceCreationHelper::Create2DWorkspace(1,1);

TS_ASSERT_THROWS(PlotAxis(ws, 2), std::invalid_argument);
TS_ASSERT_THROWS(PlotAxis(ws, -1), std::invalid_argument);
}
};


#endif /* MANTID_API_AXISLABELTEST_H_ */

0 comments on commit 8286acd

Please sign in to comment.