diff --git a/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/LatticePresenter.h b/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/LatticePresenter.h index 04ce99534b227a1838bdc2f6b4fafad2b3bcae8c..2b894fc036d9eb0ae107225191499be0d0bd1045 100644 --- a/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/LatticePresenter.h +++ b/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/LatticePresenter.h @@ -9,7 +9,34 @@ namespace MantidQt { class LatticeView; class LoanedMemento; - class LatticePresenter : public Updateable + + + /** Presenter of MVP type for controlling interaction of lattice view with WorkspaceMementos. + + @author Owen Arnold, RAL ISIS + @date 06/Oct/2011 + + Copyright © 2010-11 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://svn.mantidproject.org/mantid/trunk/Code/Mantid>. + Code Documentation is available at: <http://doxygen.mantidproject.org> + */ + class DLLExport LatticePresenter : public Updateable { public: LatticePresenter(LoanedMemento& memento); @@ -17,7 +44,7 @@ namespace MantidQt void update(); void acceptView(LatticeView* view); private: - void checkInput(double a1, double a2, double a3, double b1, double b2, double b3); + bool checkInput(double a1, double a2, double a3, double b1, double b2, double b3); LatticeView* m_view; LoanedMemento& m_WsMemento; }; diff --git a/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/LatticeView.h b/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/LatticeView.h index 20e3dea36abe3744e21bd8e05e1cc7769b83d0ca..7a8a8cdfd9f5a65a8c39c4eceb2813febd85e486 100644 --- a/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/LatticeView.h +++ b/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/LatticeView.h @@ -5,6 +5,31 @@ namespace MantidQt { namespace CustomInterfaces { + /** Abstract lattice view. + + @author Owen Arnold, RAL ISIS + @date 06/Oct/2011 + + Copyright © 2010-11 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://svn.mantidproject.org/mantid/trunk/Code/Mantid>. + Code Documentation is available at: <http://doxygen.mantidproject.org> + */ class LatticeView { public: diff --git a/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/ParameterisedLatticeView.h b/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/ParameterisedLatticeView.h index 568b7b3e1a2e24c50a4a288f268496400119fb9a..29e889d2c7ca57a84714c99783d9bf3bc6575112 100644 --- a/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/ParameterisedLatticeView.h +++ b/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/ParameterisedLatticeView.h @@ -14,6 +14,32 @@ namespace MantidQt namespace CustomInterfaces { class LatticePresenter; + + /** Concrete LatticeView as a QtWidget + + @author Owen Arnold, RAL ISIS + @date 06/Oct/2011 + + Copyright © 2010-11 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://svn.mantidproject.org/mantid/trunk/Code/Mantid>. + Code Documentation is available at: <http://doxygen.mantidproject.org> + */ class ParameterisedLatticeView : public QWidget, public LatticeView { Q_OBJECT diff --git a/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/Updateable.h b/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/Updateable.h index d91622d1a42c91fe12942cd9e64e6730510d13b3..90f32fc26875d9a82e358528c1e29cbbae40e13f 100644 --- a/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/Updateable.h +++ b/Code/Mantid/MantidQt/CustomInterfaces/inc/MantidQtCustomInterfaces/Updateable.h @@ -1,11 +1,43 @@ +#ifndef MANTIDQTCUSTOMINTERFACES_UPDATABLE_H +#define MANTIDQTCUSTOMINTERFACES_UPDATABLE_H + +#include "MantidKernel/System.h" + namespace MantidQt { namespace CustomInterfaces { - class Updateable + /** Abstraction of an updateable item, i.e. a MVP presenter or Qt MVC Model. + + @author Owen Arnold, RAL ISIS + @date 06/Oct/2011 + + Copyright © 2010-11 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://svn.mantidproject.org/mantid/trunk/Code/Mantid>. + Code Documentation is available at: <http://doxygen.mantidproject.org> + */ + class DLLExport Updateable { public: virtual void update() = 0; }; } -} \ No newline at end of file +} + +#endif \ No newline at end of file diff --git a/Code/Mantid/MantidQt/CustomInterfaces/src/LatticePresenter.cpp b/Code/Mantid/MantidQt/CustomInterfaces/src/LatticePresenter.cpp index ee34b70315ffbe51401382b0e9b8d0f6668a595c..bea03f7e92e29ba6f421bc573cb72a23c9fa8ac6 100644 --- a/Code/Mantid/MantidQt/CustomInterfaces/src/LatticePresenter.cpp +++ b/Code/Mantid/MantidQt/CustomInterfaces/src/LatticePresenter.cpp @@ -9,16 +9,21 @@ namespace MantidQt { namespace CustomInterfaces { + /// Constructor LatticePresenter::LatticePresenter(LoanedMemento& memento) : m_WsMemento(memento) { } + /// Destructor LatticePresenter::~LatticePresenter() { //Explicitly DO NOT manage/release view as view owns presenter. Presenter is view visitor. } + /** + Accept the lattice view and configure it. + */ void LatticePresenter::acceptView(LatticeView* view) { m_view = view; @@ -34,18 +39,34 @@ namespace MantidQt m_view->initalize(a1, a2, a3, b1, b2, b3); } - void LatticePresenter::checkInput(double a1, double a2, double a3, double b1, double b2, double b3) + /** + Check that the inputs are okay. If not then pass up to view and return false. + @param a1: Lattice parameter indicating component in x + @param a2: Lattice parameter indicating component in y + @param a3: Lattice parameter indicating component in z + @param b1: Lattice parameter giving alpha angle + @param b2: Lattice parameter giving beta angle + @param b3: Lattice parameter giving gamma angle + @return false if invalid unit cell. + */ + bool LatticePresenter::checkInput(double a1, double a2, double a3, double b1, double b2, double b3) { + bool parametersOk = false; try { Mantid::Geometry::OrientedLattice lattice(a1, a2, a3, b1, b2, b3); + parametersOk = true; } catch(std::range_error&) { m_view->indicateInvalid(); } + return parametersOk; } + /** + Update method, externally triggered. + */ void LatticePresenter::update() { double a1 = m_view->getA1(); @@ -62,15 +83,18 @@ namespace MantidQt m_WsMemento->getItem(8)->setValue(b2); m_WsMemento->getItem(9)->setValue(b3); - if(m_WsMemento->hasChanged()) + if(checkInput(a1, a2, a3, b1, b2, b3)) { - m_view->indicateModified(); + if(m_WsMemento->hasChanged()) + { + m_view->indicateModified(); + } + else + { + m_view->indicateDefault(); + } } - else - { - m_view->indicateDefault(); - } - checkInput(a1, a2, a3, b1, b2, b3); + } } } \ No newline at end of file diff --git a/Code/Mantid/MantidQt/CustomInterfaces/src/ParameterisedLatticeView.cpp b/Code/Mantid/MantidQt/CustomInterfaces/src/ParameterisedLatticeView.cpp index aa52d09c096c14452e72a9eb6b3daeccfa30c49c..278cf37ce76383777a562910b6fb185f33742130 100644 --- a/Code/Mantid/MantidQt/CustomInterfaces/src/ParameterisedLatticeView.cpp +++ b/Code/Mantid/MantidQt/CustomInterfaces/src/ParameterisedLatticeView.cpp @@ -18,6 +18,16 @@ namespace MantidQt m_presenter->acceptView(this); } + /** + initalize the view with model data. + @param a1: Lattice parameter indicating component in x + @param a2: Lattice parameter indicating component in y + @param a3: Lattice parameter indicating component in z + @param b1: Lattice parameter giving alpha angle + @param b2: Lattice parameter giving beta angle + @param b3: Lattice parameter giving gamma angle + @return false if invalid unit cell. + */ void ParameterisedLatticeView::initalize(double a1, double a2, double a3, double b1, double b2, double b3) { QGridLayout* layout = new QGridLayout(); @@ -47,6 +57,9 @@ namespace MantidQt this->setLayout(layout); } + /** + Create a standard edit box for each component. + */ QLineEdit* ParameterisedLatticeView::createEditBox(double value) { QLineEdit* box = new QLineEdit(); @@ -64,41 +77,67 @@ namespace MantidQt this->layout(); } + /** + Getter for a1 + @return a1 + */ double ParameterisedLatticeView::getA1() const { return m_a1->text().toDouble(); } + /** + Getter for a2 + @return a2 + */ double ParameterisedLatticeView::getA2() const { return m_a2->text().toDouble(); } + /** + Getter for a3 + @return a3 + */ double ParameterisedLatticeView::getA3() const { return m_a3->text().toDouble(); } + /** + Getter for b1 + @return b1 + */ double ParameterisedLatticeView::getB1() const { return m_b1->text().toDouble(); } + /** + Getter for b2 + @return b2 + */ double ParameterisedLatticeView::getB2() const { return m_b2->text().toDouble(); } + /** + Getter for b3 + @return a3 + */ double ParameterisedLatticeView::getB3() const { return m_b3->text().toDouble(); } + /// Slot for edit box edited. void ParameterisedLatticeView::edited() { - m_presenter->update(); + m_presenter->update(); // Feedback to the presenter } + /// Indicate that a modification has been made void ParameterisedLatticeView::indicateModified() { QPalette pal = this->palette(); @@ -107,12 +146,14 @@ namespace MantidQt this->setPalette(pal); } + /// Indicate that there are no pending modifications void ParameterisedLatticeView::indicateDefault() { this->setAutoFillBackground(true); this->setPalette(m_pal); } + /// Indicate that the modifications/input are invalid. void ParameterisedLatticeView::indicateInvalid() { QPalette pal = this->palette(); diff --git a/Code/Mantid/MantidQt/CustomInterfaces/test/LatticePresenterTest.h b/Code/Mantid/MantidQt/CustomInterfaces/test/LatticePresenterTest.h index 37977201523a3b6df980450a1c447bfd1a7243c5..4c93db294bc4b5fc6f5f1a9ce4c569653a14abdb 100644 --- a/Code/Mantid/MantidQt/CustomInterfaces/test/LatticePresenterTest.h +++ b/Code/Mantid/MantidQt/CustomInterfaces/test/LatticePresenterTest.h @@ -5,22 +5,236 @@ #include <gmock/gmock.h> #include <gtest/gtest.h> -//#include "MantidQtCustomInterfaces/WorkspaceMementoCollection.h" -//using namespace MantidQt::CustomInterfaces; -//using namespace testing; +#include "MantidDataObjects/MementoTableWorkspace.h" +#include "MantidAPI/TableRow.h" +#include "MantidQtCustomInterfaces/WorkspaceMemento.h" +#include "MantidQtCustomInterfaces/WorkspaceMementoItem.h" +#include "MantidQtCustomInterfaces/LoanedMemento.h" +#include "MantidQtCustomInterfaces/LatticePresenter.h" +#include "MantidQtCustomInterfaces/LatticeView.h" + +using namespace Mantid::API; +using namespace Mantid::DataObjects; +using namespace MantidQt::CustomInterfaces; +using namespace testing; class LatticePresenterTest : public CxxTest::TestSuite { +private: + + /// Helper mock class for the MVP view (LatticeView) + class MockLatticeView : public LatticeView + { + public: + MOCK_CONST_METHOD0(getA1, + double()); + MOCK_CONST_METHOD0(getA2, + double()); + MOCK_CONST_METHOD0(getA3, + double()); + MOCK_CONST_METHOD0(getB1, + double()); + MOCK_CONST_METHOD0(getB2, + double()); + MOCK_CONST_METHOD0(getB3, + double()); + MOCK_METHOD0(indicateModified, + void()); + MOCK_METHOD0(indicateDefault, + void()); + MOCK_METHOD0(indicateInvalid, + void()); + MOCK_METHOD6(initalize, + void(double a1, double a2, double a3, double b1, double b2, double b3)); + }; + + // Helper method to generate a workspace memento; + static WorkspaceMemento* makeMemento() + { + TableWorkspace_sptr ws(new MementoTableWorkspace(1)); + TableRow row = ws->getRow(0); + row << "TestWSRow" << "CNCS" << 1 << "SampleXML" << 1.0 << 1.0 << 1.0 << 90.0 << 90.0 << 90.0 << "Not Ready"; + int rowIndex = 0; + WorkspaceMemento* memento = new WorkspaceMemento(ws, "TestWSRow"); + memento->addItem(new WorkspaceMementoItem<0, std::string>(ws, rowIndex)); + memento->addItem(new WorkspaceMementoItem<1, std::string>(ws, rowIndex)); + memento->addItem(new WorkspaceMementoItem<2, int>(ws, rowIndex)); + memento->addItem(new WorkspaceMementoItem<3, std::string>(ws, rowIndex)); + memento->addItem(new WorkspaceMementoItem<4, double>(ws, rowIndex)); + memento->addItem(new WorkspaceMementoItem<5, double>(ws, rowIndex)); + memento->addItem(new WorkspaceMementoItem<6, double>(ws, rowIndex)); + memento->addItem(new WorkspaceMementoItem<7, double>(ws, rowIndex)); + memento->addItem(new WorkspaceMementoItem<8, double>(ws, rowIndex)); + memento->addItem(new WorkspaceMementoItem<9, double>(ws, rowIndex)); + memento->addItem(new WorkspaceMementoItem<10, std::string>(ws, rowIndex)); + return memento; + } + + public: //===================================================================================== // Functional tests //===================================================================================== - void testsTODO() + void testConstruction() + { + //Create a view to drive. + MockLatticeView* view = new MockLatticeView(); + WorkspaceMemento* wsMemento = makeMemento(); + + { + EXPECT_CALL(*view, initalize(_, _, _, _, _, _)).Times(1); //Presenter will initalize view. + //EXPECT_CALL(*view, indicateValid()).Times(1); //Because we're going to provide valid lattice data. + + //Make some input data to give to the presenter. + + LoanedMemento loanedMemento(wsMemento); + + //Create the presenter and give it the view. + LatticePresenter presenter(loanedMemento); + presenter.acceptView(view); + + TS_ASSERT(Mock::VerifyAndClearExpectations(view)); + } + + delete wsMemento; + delete view; + } + + void testConstructionWithInvalidLattice() + { + MockLatticeView* view = new MockLatticeView(); + WorkspaceMemento* wsMemento = makeMemento(); + + //Now overrite the lattice portion with junk/invalid numbers. + std::vector<double> vars(6, 0); //All six variables initalized to zero! + wsMemento->getItem(4)->setValue(vars[0]); //a1 + wsMemento->getItem(5)->setValue(vars[1]); //a2 + wsMemento->getItem(6)->setValue(vars[2]); //a3 + wsMemento->getItem(7)->setValue(vars[3]); //b1 + wsMemento->getItem(8)->setValue(vars[4]); //b2 + wsMemento->getItem(9)->setValue(vars[5]); //b3 + + { + EXPECT_CALL(*view, initalize(_, _, _, _, _, _)).Times(1); //Presenter will initalize view. + EXPECT_CALL(*view, indicateInvalid()).Times(1); //Because we're going to provide bad lattice data. + + LoanedMemento loanedMemento(wsMemento); + + //Create the presenter and give it the view. + LatticePresenter presenter(loanedMemento); + presenter.acceptView(view); + + TS_ASSERT(Mock::VerifyAndClearExpectations(view)); + } + + delete wsMemento; + delete view; + } + + void testNothingChanged() { - //TSM_ASSERT("No tests!", false); + //Create a view to drive. + MockLatticeView* view = new MockLatticeView(); + WorkspaceMemento* wsMemento = makeMemento(); + + { + EXPECT_CALL(*view, initalize(_, _, _, _, _, _)).Times(1); //Presenter will initalize view. + // --- Region These getteres will return the same values as provided originally. see makeMemento()! + EXPECT_CALL(*view, getA1()).WillOnce(Return(1)); + EXPECT_CALL(*view, getA2()).WillOnce(Return(1)); + EXPECT_CALL(*view, getA3()).WillOnce(Return(1)); + EXPECT_CALL(*view, getB1()).WillOnce(Return(90)); + EXPECT_CALL(*view, getB2()).WillOnce(Return(90)); + EXPECT_CALL(*view, getB3()).WillOnce(Return(90)); + // -- End Region + EXPECT_CALL(*view, indicateDefault()).Times(1); //Because Nothing is going to change! VIEW WILL BE TOLD THIS. + + //Make some input data to give to the presenter. + + LoanedMemento loanedMemento(wsMemento); + + //Create the presenter and give it the view. + LatticePresenter presenter(loanedMemento); + presenter.acceptView(view); + presenter.update(); // Update, but nothing has changed, should now go ahead and tell view this! + + TS_ASSERT(Mock::VerifyAndClearExpectations(view)); + } + + delete wsMemento; + delete view; + } + + void testChangedButValid() + { + //Create a view to drive. + MockLatticeView* view = new MockLatticeView(); + WorkspaceMemento* wsMemento = makeMemento(); + + { + EXPECT_CALL(*view, initalize(_, _, _, _, _, _)).Times(1); //Presenter will initalize view. + // --- Region These getteres will return different values from those provided originally. see makeMemento()! + EXPECT_CALL(*view, getA1()).WillOnce(Return(2)); + EXPECT_CALL(*view, getA2()).WillOnce(Return(2)); + EXPECT_CALL(*view, getA3()).WillOnce(Return(2)); + EXPECT_CALL(*view, getB1()).WillOnce(Return(90)); + EXPECT_CALL(*view, getB2()).WillOnce(Return(90)); + EXPECT_CALL(*view, getB3()).WillOnce(Return(90)); + // -- End Region + EXPECT_CALL(*view, indicateModified()).Times(1); //Because stuff is going to change! VIEW WILL BE TOLD THIS. + + //Make some input data to give to the presenter. + + LoanedMemento loanedMemento(wsMemento); + + //Create the presenter and give it the view. + LatticePresenter presenter(loanedMemento); + presenter.acceptView(view); + presenter.update(); // Update, but now stuff has changed, should now go ahead and tell view this! + + TS_ASSERT(Mock::VerifyAndClearExpectations(view)); + } + + delete wsMemento; + delete view; } + + void testChangedButNotValid() + { + //Create a view to drive. + MockLatticeView* view = new MockLatticeView(); + WorkspaceMemento* wsMemento = makeMemento(); + + { + EXPECT_CALL(*view, initalize(_, _, _, _, _, _)).Times(1); //Presenter will initalize view. + // --- Region These getteres will return different/invalid values from those provided originally. see makeMemento()! + EXPECT_CALL(*view, getA1()).WillOnce(Return(0)); + EXPECT_CALL(*view, getA2()).WillOnce(Return(0)); + EXPECT_CALL(*view, getA3()).WillOnce(Return(0)); + EXPECT_CALL(*view, getB1()).WillOnce(Return(0)); + EXPECT_CALL(*view, getB2()).WillOnce(Return(0)); + EXPECT_CALL(*view, getB3()).WillOnce(Return(0)); + // -- End Region + EXPECT_CALL(*view, indicateInvalid()).Times(1); //Because stuff is going to change for the worse! VIEW WILL BE TOLD THIS. + + //Make some input data to give to the presenter. + + LoanedMemento loanedMemento(wsMemento); + + //Create the presenter and give it the view. + LatticePresenter presenter(loanedMemento); + presenter.acceptView(view); + presenter.update(); // Update, but now stuff has changed for the worse, should now go ahead and tell view this! + + TS_ASSERT(Mock::VerifyAndClearExpectations(view)); + } + + delete wsMemento; + delete view; + } + }; #endif \ No newline at end of file