Skip to content

Commit

Permalink
Refs #8849. Move common validating functionality to a method.
Browse files Browse the repository at this point in the history
  • Loading branch information
arturbekasov committed Mar 3, 2014
1 parent d94ee81 commit b929dbb
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 47 deletions.
Expand Up @@ -17,13 +17,18 @@ namespace MuonAnalysisHelper
{

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

/// Sets double validator for specified field
DLLExport void setDoubleValidator(QLineEdit* field);

/// Returns a first period MatrixWorkspace in a run workspace
DLLExport MatrixWorkspace_sptr firstPeriod(Workspace_sptr ws);

/// Validates the field and returns the value
DLLExport double getValidatedDouble(QLineEdit* field, double defaultValue,
const QString& valueDescr, Logger& log);

/// Returns a number of periods in a run workspace
DLLExport size_t numPeriods(Workspace_sptr ws);

Expand Down
54 changes: 7 additions & 47 deletions Code/Mantid/MantidQt/CustomInterfaces/src/MuonAnalysis.cpp
Expand Up @@ -2381,38 +2381,16 @@ void MuonAnalysis::startUpLook()
*/
double MuonAnalysis::timeZero()
{
QString boxText = m_uiForm.timeZeroFront->text();
double timeZero = 0.0;
try
{
timeZero = boost::lexical_cast<double>(boxText.toStdString());
}
catch(boost::bad_lexical_cast&)
{
QMessageBox::warning(this, "MantidPlot - Muon Analysis", "Unable to interpret time zero as number, setting to 0.0");
m_uiForm.timeZeroFront->setText("0.0");
}
return timeZero;
return getValidatedDouble(m_uiForm.timeZeroFront, 0, "time zero", g_log);
}

/**
* Return first good bin as set on the interface.
*/
double MuonAnalysis::firstGoodBin() const
{
QString text = m_uiForm.firstGoodBinFront->text();

bool ok;
double value = text.toDouble(&ok);

if (!ok)
{
g_log.warning("First Good Data is empty or invalid. Reset to default value.");
m_uiForm.firstGoodBinFront->setText(QString::number(FIRST_GOOD_BIN_DEFAULT));
value = FIRST_GOOD_BIN_DEFAULT;
}

return value;
return getValidatedDouble(m_uiForm.firstGoodBinFront, FIRST_GOOD_BIN_DEFAULT, "first good bin",
g_log);
}

/**
Expand All @@ -2433,17 +2411,7 @@ double MuonAnalysis::plotFromTime() const
}
else if (startTimeType == "Custom Value")
{
bool ok;
double customValue = m_uiForm.timeAxisStartAtInput->text().toDouble(&ok);

if (!ok)
{
g_log.warning("Custom start time value is empty or invalid. Reset to zero.");
customValue = 0;
m_uiForm.timeAxisStartAtInput->setText("0.0");
}

return customValue;
return getValidatedDouble(m_uiForm.timeAxisStartAtInput, 0, "custom start time", g_log);
}

// Just in case misspelled type or added a new one
Expand All @@ -2457,17 +2425,9 @@ double MuonAnalysis::plotFromTime() const
*/
double MuonAnalysis::plotToTime() const
{
bool ok;
double value = m_uiForm.timeAxisFinishAtInput->text().toDouble(&ok);

if (!ok)
{
g_log.warning("Custom finish time value is empty or invalid. Reset to default.");
value = plotFromTime() + 1.0;
m_uiForm.timeAxisFinishAtInput->setText(QString::number(value));
}

return value;
double defaultValue = plotFromTime() + 1.0;
return getValidatedDouble(m_uiForm.timeAxisFinishAtInput, defaultValue, "custom finish time",
g_log);
}


Expand Down
26 changes: 26 additions & 0 deletions Code/Mantid/MantidQt/CustomInterfaces/src/MuonAnalysisHelper.cpp
Expand Up @@ -17,6 +17,7 @@ namespace MuonAnalysisHelper
{

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

/**
* Sets double validator for specified field.
Expand Down Expand Up @@ -322,6 +323,31 @@ void WidgetAutoSaver::endGroup()
m_settings.endGroup();
}

/**
* Validates and returns a double value. If it is not invalid, the widget is set to default value,
* appropriate warning is printed and default value is returned.
* @param field :: Field to get value from
* @param defaultValue :: Default value to return/set if field value is invalid
* @param valueDescr :: Description of the value
* @param log :: Log to print warning to in case value is invalid
* @return Value if field is valid, default value otherwise
*/
double getValidatedDouble(QLineEdit* field, double defaultValue, const QString& valueDescr, Logger& log)
{
bool ok;
double value = field->text().toDouble(&ok);

if (!ok)
{
log.warning() << "The value of " << valueDescr.toStdString() << " is empty or invalid. ";
log.warning() << "Reset to default of " << defaultValue << ".\n";
value = defaultValue;
field->setText(QString::number(defaultValue));
}

return value;
}

} // namespace MuonAnalysisHelper
} // namespace CustomInterfaces
} // namespace Mantid

0 comments on commit b929dbb

Please sign in to comment.