Skip to content

Commit

Permalink
Refs #9213. Add log selection combo-box and auto-populate it
Browse files Browse the repository at this point in the history
  • Loading branch information
arturbekasov committed Apr 24, 2014
1 parent 1a7cf1a commit e5f3b24
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 16 deletions.
Expand Up @@ -51,6 +51,9 @@ namespace CustomInterfaces
/// Load new data and update the view accordingly
void load();

/// Updates the list of logs user can choose from
void updateAvailableLogs();

private:
/// View which the object works with
IALCDataLoadingView* const m_view;
Expand Down
Expand Up @@ -46,19 +46,19 @@ namespace CustomInterfaces
public:
ALCDataLoadingView(QWidget* widget);

// -- IALCDataLoadingView interface ------------------------------------------------------------

void initialize();

/// @see IALCDataLoadingView::firstRun
std::string firstRun() const;
/// @see IALCDataLoadingView::lastRun
std::string lastRun() const;
/// @see IALCDataLoadingView::log
std::string log() const;

/// @see IALCDataLoadingView::displayData
void setDataCurve(const QwtData& data);
/// @see IALCDataLoadingView::displayError
void displayError(const std::string &error);
void setAvailableLogs(const std::vector<std::string> &logs);

// -- End of IALCDataLoadingView interface -----------------------------------------------------

private:
/// UI form
Expand Down
Expand Up @@ -51,13 +51,6 @@
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QLineEdit" name="log">
<property name="text">
<string>sample_magn_field</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="MantidQt::MantidWidgets::MWRunFiles" name="firstRun" native="true">
<property name="label" stdset="0">
Expand All @@ -78,6 +71,13 @@
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QComboBox" name="log">
<property name="editable">
<bool>false</bool>
</property>
</widget>
</item>
</layout>
</widget>
</item>
Expand Down
Expand Up @@ -67,9 +67,16 @@ namespace CustomInterfaces
/// @param error :: Error message to display
virtual void displayError(const std::string& error) = 0;

/// Update the list of logs user can select
/// @param logs :: New list of log names
virtual void setAvailableLogs(const std::vector<std::string>& logs) = 0;

signals:
/// Request to load data
void loadRequested();

/// User has selected the first run
void firstRunSelected();
};

} // namespace CustomInterfaces
Expand Down
Expand Up @@ -3,11 +3,13 @@
#include "MantidAPI/AlgorithmManager.h"

#include "MantidQtCustomInterfaces/Muon/ALCHelper.h"
#include "MantidQtCustomInterfaces/Muon/MuonAnalysisHelper.h"

#include <Poco/ActiveResult.h>

#include <QApplication>

using namespace Mantid::Kernel;
using namespace Mantid::API;

namespace MantidQt
Expand All @@ -23,6 +25,7 @@ namespace CustomInterfaces
m_view->initialize();

connect(m_view, SIGNAL(loadRequested()), SLOT(load()));
connect(m_view, SIGNAL(firstRunSelected()), SLOT(updateAvailableLogs()));
}

void ALCDataLoadingPresenter::load()
Expand All @@ -34,7 +37,7 @@ namespace CustomInterfaces
alg->setProperty("FirstRun", m_view->firstRun());
alg->setProperty("LastRun", m_view->lastRun());
alg->setProperty("LogValue", m_view->log());
alg->setPropertyValue("OutputWorkspace", "__NotUsed__");
alg->setPropertyValue("OutputWorkspace", "__NotUsed");

Poco::ActiveResult<bool> result(alg->executeAsync());

Expand All @@ -52,5 +55,41 @@ namespace CustomInterfaces
m_view->displayError(e.what());
}
}

void ALCDataLoadingPresenter::updateAvailableLogs()
{
Workspace_sptr loadedWs;

try //... to load the first run
{
IAlgorithm_sptr load = AlgorithmManager::Instance().create("LoadMuonNexus");
load->setChild(true); // Don't want workspaces in the ADS
load->setProperty("Filename", m_view->firstRun());
// Don't load any data - we need logs only
load->setPropertyValue("SpectrumMin","0");
load->setPropertyValue("SpectrumMax","0");
load->setPropertyValue("OutputWorkspace", "__NotUsed");
load->execute();

loadedWs = load->getProperty("OutputWorkspace");
}
catch(std::exception& e)
{
m_view->setAvailableLogs(std::vector<std::string>()); // Empty logs list
return;
}

MatrixWorkspace_const_sptr ws = MuonAnalysisHelper::firstPeriod(loadedWs);
std::vector<std::string> logs;

const auto& properties = ws->run().getProperties();
for(auto it = properties.begin(); it != properties.end(); ++it)
{
logs.push_back((*it)->name());
}

m_view->setAvailableLogs(logs);
}

} // namespace CustomInterfaces
} // namespace MantidQt
Expand Up @@ -15,7 +15,8 @@ namespace CustomInterfaces
void ALCDataLoadingView::initialize()
{
m_ui.setupUi(m_widget);
connect(m_ui.load, SIGNAL(clicked()), this, SIGNAL(loadRequested()));
connect(m_ui.load, SIGNAL(clicked()), SIGNAL(loadRequested()));
connect(m_ui.firstRun, SIGNAL(fileFindingFinished()), SIGNAL(firstRunSelected()));

m_ui.dataPlot->setCanvasBackground(Qt::white);
m_ui.dataPlot->setAxisFont(QwtPlot::xBottom, m_widget->font());
Expand All @@ -39,7 +40,7 @@ namespace CustomInterfaces

std::string ALCDataLoadingView::log() const
{
return m_ui.log->text().toStdString();
return m_ui.log->currentText().toStdString();
}

void ALCDataLoadingView::setDataCurve(const QwtData& data)
Expand All @@ -53,5 +54,17 @@ namespace CustomInterfaces
QMessageBox::critical(m_widget, "Loading error", QString::fromStdString(error));
}

void ALCDataLoadingView::setAvailableLogs(const std::vector<std::string>& logs)
{
// Clear previous log list
m_ui.log->clear();

// Add new items
for (auto it = logs.begin(); it != logs.end(); ++it)
{
m_ui.log->addItem(QString::fromStdString(*it));
}
}

} // namespace CustomInterfaces
} // namespace MantidQt
Expand Up @@ -16,14 +16,17 @@ using namespace testing;
class MockALCDataLoadingView : public IALCDataLoadingView
{
public:
MOCK_METHOD0(initialize, void());
MOCK_CONST_METHOD0(firstRun, std::string());
MOCK_CONST_METHOD0(lastRun, std::string());
MOCK_CONST_METHOD0(log, std::string());

MOCK_METHOD0(initialize, void());
MOCK_METHOD1(setDataCurve, void(const QwtData&));
MOCK_METHOD1(displayError, void(const std::string&));
MOCK_METHOD1(setAvailableLogs, void(const std::vector<std::string>&));

void requestLoading() { emit loadRequested(); }
void selectFirstRun() { emit firstRunSelected(); }
};

MATCHER_P3(QwtDataX, i, value, delta, "") { return fabs(arg.x(i) - value) < delta; }
Expand Down Expand Up @@ -83,6 +86,23 @@ class ALCDataLoadingPresenterTest : public CxxTest::TestSuite
m_view->requestLoading();
}

void test_updateAvailableLogs()
{
EXPECT_CALL(*m_view, firstRun()).WillRepeatedly(Return("MUSR00015189.nxs"));
EXPECT_CALL(*m_view, setAvailableLogs(AllOf(Property(&std::vector<std::string>::size, 33),
Contains("run_number"),
Contains("sample_magn_field"),
Contains("Field_Danfysik"))));
m_view->selectFirstRun();
}

void test_updateAvailableLogs_invalidFirstRun()
{
EXPECT_CALL(*m_view, firstRun()).WillRepeatedly(Return("LOQ49886.nxs")); // XXX: not a Muon file
EXPECT_CALL(*m_view, setAvailableLogs(ElementsAre())); // Empty array expectedB
TS_ASSERT_THROWS_NOTHING(m_view->selectFirstRun());
}

void test_load_error()
{
// TODO: with algorithm being executed asynchronously, check that errors are caught propertly
Expand Down

0 comments on commit e5f3b24

Please sign in to comment.