Skip to content

Commit

Permalink
refs #5167. Disable features automatically.
Browse files Browse the repository at this point in the history
Do not allow the user to have access to these features unless everything has been setup properly.
  • Loading branch information
OwenArnold committed Nov 29, 2012
1 parent 49acde5 commit 610fa94
Show file tree
Hide file tree
Showing 14 changed files with 171 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace MantidQt
virtual ~ConcretePeaksPresenter();
virtual void update();
virtual void updateWithSlicePoint(const double& slicePoint);
virtual void changeShownDim();
virtual bool changeShownDim();
virtual bool isLabelOfFreeAxis(const std::string& label) const;
private:
/// Peak overlay views.
Expand All @@ -37,7 +37,11 @@ namespace MantidQt
/// Peak transformer
PeakTransform m_transform;
/// Configurre peak transformations
void configureMappingTransform();
bool configureMappingTransform();
/// Hide all views
void hideAll();
/// Show all views
void showAll();
};

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace MantidQt
public:
virtual void update(){}
virtual void updateWithSlicePoint(const double&){}
virtual void changeShownDim(){}
virtual bool changeShownDim(){return false;}
virtual bool isLabelOfFreeAxis(const std::string&) const {return false;}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ namespace SliceViewer
virtual void setSlicePoint(const double& point);
/// Hide the view.
virtual void hideView();
/// Show the view.
virtual void showView();
/// Update the view.
virtual void updateView();
/// Get the origin. md x, md y
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ namespace MantidQt
virtual void updateView() = 0;
/// Hide the view.
virtual void hideView() = 0;
/// Show the view.
virtual void showView() = 0;
/// Move the peak overlay to a new position.
virtual void movePosition(const PeakTransform& peakTransform) = 0;
/// Destructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ namespace MantidQt
{
namespace SliceViewer
{
/**
@class PeakTransform
Used to remap coordinates into a form consistent with an axis reordering.
*/
class DLLExport PeakTransform
{
public:
Expand All @@ -27,6 +31,18 @@ namespace MantidQt
boost::regex m_KRegex;
boost::regex m_LRegex;
};

/**
@class PeakTransformException
Exceptions occuring when PeakTransformations cannot be formed.
*/
class PeakTransformException : public std::exception
{
public:
PeakTransformException(const std::string& msg) : std::exception(msg.c_str())
{
}
};

}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace SliceViewer
public:
virtual void update() = 0;
virtual void updateWithSlicePoint(const double&) = 0;
virtual void changeShownDim() = 0;
virtual bool changeShownDim() = 0;
virtual bool isLabelOfFreeAxis(const std::string& label) const = 0;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,10 @@ public slots:

void findRangeFull();
void findRangeSlice();

// Peak overlay methods.
void updatePeaksWithSlicePoint();
void enablePeakOverlaysIfAppropriate();


private:
Expand Down
75 changes: 61 additions & 14 deletions Code/Mantid/MantidQt/SliceViewer/src/ConcretePeaksPresenter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace SliceViewer
throw std::invalid_argument("PeaksWorkspace does not contain integrated peaks."); // We might consider drawing these in the future anyway.
}

this->configureMappingTransform();
const bool transformSucceeded = this->configureMappingTransform();
// Extract the integration radius from the workspace.
const double peakIntegrationRadius = boost::lexical_cast<double>(peaksWS->run().getProperty("PeakRadius")->value());
factory->setRadius(peakIntegrationRadius);
Expand All @@ -48,6 +48,10 @@ namespace SliceViewer
PeakOverlayView_sptr view = boost::shared_ptr<PeakOverlayView>( m_factory->createView(m_transform.transform(position)) );
m_viewPeaks[i] = view;
}
if(!transformSucceeded)
{
hideAll();
}
}

/**
Expand Down Expand Up @@ -78,37 +82,50 @@ namespace SliceViewer
*/
ConcretePeaksPresenter::~ConcretePeaksPresenter()
{
for(VecPeakOverlayView::iterator it = m_viewPeaks.begin(); it != m_viewPeaks.end(); ++it)
{
(*it)->hideView();
}
hideAll();
}

/**
Respond to changes in the shown dimension.
@ return True only if this succeeds.
*/
void ConcretePeaksPresenter::changeShownDim()
bool ConcretePeaksPresenter::changeShownDim()
{
// Reconfigure the mapping tranform.
this->configureMappingTransform();
const bool transformSucceeded = this->configureMappingTransform();
// Apply the mapping tranform to move each peak overlay object.

for(VecPeakOverlayView::iterator it = m_viewPeaks.begin(); it != m_viewPeaks.end(); ++it)
if(transformSucceeded)
{
(*it)->movePosition(m_transform);
for(VecPeakOverlayView::iterator it = m_viewPeaks.begin(); it != m_viewPeaks.end(); ++it)
{
(*it)->movePosition(m_transform);
}
}
return transformSucceeded;
}

/**
This method looks at the plotted dimensions (XY) , and work out what indexes into the vector HKL, these XYZ dimensions correpond to.
The indexes can then be used for any future transformation, where the user changes the chosen dimensions to plot.
@return True if the mapping has succeeded.
*/
void ConcretePeaksPresenter::configureMappingTransform()
bool ConcretePeaksPresenter::configureMappingTransform()
{
std::string xLabel = m_factory->getPlotXLabel();
std::string yLabel = m_factory->getPlotYLabel();
m_transform = PeakTransform(xLabel, yLabel);
bool transformSucceeded = false;
try
{
std::string xLabel = m_factory->getPlotXLabel();
std::string yLabel = m_factory->getPlotYLabel();
m_transform = PeakTransform(xLabel, yLabel);
showAll();
transformSucceeded = true;
}
catch(PeakTransformException&)
{
hideAll();
}
return transformSucceeded;
}

/**
Expand All @@ -121,5 +138,35 @@ namespace SliceViewer
return boost::regex_match(label, m_transform.getFreePeakAxisRegex());
}

/**
Request that each owned view makes iteself visible.
*/
void ConcretePeaksPresenter::showAll()
{
// Show all views.
for(VecPeakOverlayView::iterator it = m_viewPeaks.begin(); it != m_viewPeaks.end(); ++it)
{
if((*it) != NULL)
{
(*it)->showView();
}
}
}

/**
Request that each owned view makes iteself NOT visible.
*/
void ConcretePeaksPresenter::hideAll()
{
// Hide all views.
for(VecPeakOverlayView::iterator it = m_viewPeaks.begin(); it != m_viewPeaks.end(); ++it)
{
if((*it) != NULL)
{
(*it)->hideView();
}
}
}

}
}
5 changes: 5 additions & 0 deletions Code/Mantid/MantidQt/SliceViewer/src/PeakOverlay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ namespace SliceViewer
this->hide();
}

void PeakOverlay::showView()
{
this->show();
}

void PeakOverlay::movePosition(const PeakTransform& transform)
{
// Will have the plots x, y, and z aligned to the correct h, k, l value.
Expand Down
4 changes: 2 additions & 2 deletions Code/Mantid/MantidQt/SliceViewer/src/PeakTransform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ namespace MantidQt
}
else
{
throw std::runtime_error("Could not process mapped dimensions.");
throw PeakTransformException("Could not process mapped dimensions");
}
}

Expand Down Expand Up @@ -105,7 +105,7 @@ namespace MantidQt
return m_HRegex;
case 1:
return m_KRegex;
case 2:
default:
return m_LRegex;
}
}
Expand Down
48 changes: 44 additions & 4 deletions Code/Mantid/MantidQt/SliceViewer/src/SliceViewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,6 @@ SliceViewer::SliceViewer(QWidget *parent)
m_overlayWSOutline->setShowHandles(false);
m_overlayWSOutline->setShowLine(false);
m_overlayWSOutline->setShown(false);

ui.btnPeakOverlay->setEnabled(true);
}

//------------------------------------------------------------------------------------
Expand Down Expand Up @@ -602,9 +600,11 @@ void SliceViewer::setWorkspace(Mantid::API::IMDWorkspace_sptr ws)
}
}

// Enable peaks overlays according to the dimensionality and the displayed dimensions.
enablePeakOverlaysIfAppropriate();

// Send out a signal
emit changedShownDim(m_dimX, m_dimY);

}


Expand Down Expand Up @@ -1472,8 +1472,11 @@ void SliceViewer::changedShownDim(int index, int dim, int oldDim)
}
}


// Show the new slice. This finds m_dimX and m_dimY
this->updateDisplay();
// The dimensionality has changed. It might no longer be possible to plot peaks. We need updateDisplay called first, because we read axis off the plot.
enablePeakOverlaysIfAppropriate();
// Transform the peak overlays according to the new plotting.
m_peaksPresenter->changeShownDim();
// Send out a signal
Expand Down Expand Up @@ -2073,6 +2076,12 @@ void SliceViewer::dynamicRebinComplete(bool error)

/**
Event handler for selection/de-selection of peak overlays.
Allow user to choose a suitable input peaks workspace
Create a factory for fabricating new views 'PeakOverlays'
Create a proper peaks presenter to manage the views and bind them against the PeaksWorkspace and the SliceViewer
Update the views with the current slice point. to ensure they are shown.
@param checked : True if peak overlay option is checked.
*/
void SliceViewer::peakOverlay_toggled(bool checked)
Expand All @@ -2095,7 +2104,7 @@ void SliceViewer::peakOverlay_toggled(bool checked)
}
else
{
m_peaksPresenter = PeaksPresenter_sptr(new NullPeaksPresenter);
m_peaksPresenter = boost::make_shared<NullPeaksPresenter>();
}
}

Expand All @@ -2118,6 +2127,37 @@ void SliceViewer::updatePeaksWithSlicePoint()
}
}

/**
Decide whether to enable peak overlays, then reflect the ui controls to indicate this.
1) Check the dimensionality of the workspace.
2) Check that the currently displayed plot x and y correspond to a valid peak transform (H, K, L) etc.
*/
void SliceViewer::enablePeakOverlaysIfAppropriate()
{
bool enablePeakOverlays = false;
if(m_ws->getNumDims() >= 2)
{
try
{
const std::string xDim = m_plot->axisTitle(QwtPlot::xBottom).text().toStdString();
const std::string yDim = m_plot->axisTitle(QwtPlot::yLeft).text().toStdString();
PeakTransform(xDim, yDim); // This will throw if the mapped dimensions are not hkl.
enablePeakOverlays = true;
}
catch(PeakTransformException&)
{
}
}
m_syncPeakOverlay->setEnabled(enablePeakOverlays);
if(! enablePeakOverlays)
{
ui.btnPeakOverlay->setChecked(false); // Don't leave the button depressed.
m_peaksPresenter = boost::make_shared<NullPeaksPresenter>(); // Reset the presenter
}
}

} //namespace
}

25 changes: 25 additions & 0 deletions Code/Mantid/MantidQt/SliceViewer/test/ConcretePeaksPresenterTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class ConcretePeaksPresenterTest : public CxxTest::TestSuite
MOCK_METHOD0(updateView, void());
MOCK_METHOD1(setSlicePoint, void(const double&));
MOCK_METHOD0(hideView, void());
MOCK_METHOD0(showView, void());
MOCK_METHOD1(movePosition, void(const PeakTransform&));
~MockPeakOverlayView(){}
};
Expand Down Expand Up @@ -180,6 +181,30 @@ class ConcretePeaksPresenterTest : public CxxTest::TestSuite

TSM_ASSERT("MockView not used as expected.", Mock::VerifyAndClearExpectations(pMockView));
}

void test_handle_non_hkl_xy_mappings()
{
// Create a widget factory mock
auto mockViewFactory = new MockPeakOverlayFactory;

const int expectedNumberPeaks = 1;

// Create a mock view object that will be returned by the mock factory.
auto pMockView = new NiceMock<MockPeakOverlayView>;
EXPECT_CALL(*pMockView, hideView()).Times(expectedNumberPeaks); // This will be called automatically because the presenter won't be able to map Qx (below).
auto mockView = boost::shared_ptr<NiceMock<MockPeakOverlayView> >(pMockView);

EXPECT_CALL(*mockViewFactory, setRadius(_)).Times(1);
EXPECT_CALL(*mockViewFactory, createView(_)).WillRepeatedly(Return(mockView));
EXPECT_CALL(*mockViewFactory, getPlotXLabel()).WillOnce(Return("Qx")); // Not either H, K or L
EXPECT_CALL(*mockViewFactory, getPlotYLabel()).WillOnce(Return("K"));
Mantid::API::IPeaksWorkspace_sptr peaksWS = createPeaksWorkspace(expectedNumberPeaks);

ConcretePeaksPresenter presenter(mockViewFactory, peaksWS);
TSM_ASSERT("MockView not used as expected.", Mock::VerifyAndClearExpectations(pMockView));
}



};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class NullPeaksPresenterTest : public CxxTest::TestSuite
void test_changeShownDim_does_nothing()
{
NullPeaksPresenter presenter;
TS_ASSERT_THROWS_NOTHING(presenter.changeShownDim());
TS_ASSERT(!presenter.changeShownDim());
}

void test_isLabelOfFreeAxis_always_returns_false()
Expand Down

0 comments on commit 610fa94

Please sign in to comment.