Skip to content

Commit

Permalink
Add validation to sample transmission UI
Browse files Browse the repository at this point in the history
Refs #11072
  • Loading branch information
DanNixon committed Feb 16, 2015
1 parent b788d91 commit ceb9a8d
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 27 deletions.
Expand Up @@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>492</width>
<height>245</height>
<height>247</height>
</rect>
</property>
<property name="windowTitle">
Expand Down Expand Up @@ -92,6 +92,13 @@
<item>
<widget class="QLineEdit" name="leMultiple"/>
</item>
<item>
<widget class="QLabel" name="valMultiple">
<property name="text">
<string/>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
Expand All @@ -105,36 +112,47 @@
<string>Sample Details</string>
</property>
<layout class="QGridLayout" name="loSampleDetails">
<item row="2" column="0">
<widget class="QLabel" name="lbThickness">
<item row="1" column="0">
<widget class="QLabel" name="lbNumberDensity">
<property name="text">
<string>Thickness</string>
<string>Number Density</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="lbNumberDensity">
<item row="2" column="1">
<widget class="QDoubleSpinBox" name="spThickness"/>
</item>
<item row="1" column="1">
<widget class="QDoubleSpinBox" name="spNumberDensity"/>
</item>
<item row="2" column="0">
<widget class="QLabel" name="lbThickness">
<property name="text">
<string>Number Density</string>
<string>Thickness</string>
</property>
</widget>
</item>
<item row="0" column="1">
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QLineEdit" name="leChemicalFormula"/>
</item>
<item>
<widget class="QLabel" name="valChemicalFormula">
<property name="text">
<string/>
</property>
</widget>
</item>
</layout>
</item>
<item row="0" column="0">
<widget class="QLabel" name="lbChemicalFormula">
<property name="text">
<string>Chemical Formula</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="leChemicalFormula"/>
</item>
<item row="1" column="1">
<widget class="QDoubleSpinBox" name="spNumberDensity"/>
</item>
<item row="2" column="1">
<widget class="QDoubleSpinBox" name="spThickness"/>
</item>
</layout>
</widget>
</item>
Expand Down
Expand Up @@ -80,8 +80,12 @@ namespace MantidQt
bool isAllInputValid();

private:
/// Sets a validation label
void setErrorLabel(QLabel * errorLabel, bool valid);

/// Any raised error messages.
QStringList m_errorMessages;

};
}
}
Expand Down
52 changes: 46 additions & 6 deletions Code/Mantid/MantidQt/CustomInterfaces/src/SampleTransmission.cpp
Expand Up @@ -4,6 +4,7 @@
#include "MantidQtCustomInterfaces/SampleTransmission.h"

#include "MantidAPI/AlgorithmManager.h"
#include "MantidQtCustomInterfaces/UserInputValidator.h"


namespace
Expand Down Expand Up @@ -54,8 +55,37 @@ void SampleTransmission::initLayout()
*/
bool SampleTransmission::validate()
{
//TODO
return false;
UserInputValidator uiv;

// Valudate input binning
int wavelengthBinning = m_uiForm.cbBinningType->currentIndex();
switch(wavelengthBinning)
{
// Single
case 0:
uiv.checkBins(m_uiForm.spSingleLow->value(),
m_uiForm.spSingleWidth->value(),
m_uiForm.spSingleHigh->value());
break;

// Multiple
case 1:
uiv.checkFieldIsNotEmpty("Multiple binning",
m_uiForm.leMultiple,
m_uiForm.valMultiple);
break;
}

// Validate chemical formula
uiv.checkFieldIsNotEmpty("Chemical Formula",
m_uiForm.leChemicalFormula,
m_uiForm.valChemicalFormula);

// Give error message
if(!uiv.isAllInputValid())
g_log.error(uiv.generateErrorMessage().toStdString());

return uiv.isAllInputValid();
}


Expand All @@ -64,6 +94,10 @@ bool SampleTransmission::validate()
*/
void SampleTransmission::calculate()
{
// Do not try to run with invalid input
if(!validate())
return;

// Create the transmission calculation algorithm
IAlgorithm_sptr transCalcAlg = AlgorithmManager::Instance().create("CalculateSampleTransmission");
transCalcAlg->initialize();
Expand All @@ -72,13 +106,19 @@ void SampleTransmission::calculate()
int wavelengthBinning = m_uiForm.cbBinningType->currentIndex();
switch(wavelengthBinning)
{
// Multiple
// Single
case 0:
//TODO
transCalcAlg->setProperty("WavelengthRange", "");
{
QStringList params;
params << m_uiForm.spSingleLow->text()
<< m_uiForm.spSingleWidth->text()
<< m_uiForm.spSingleHigh->text();
QString binString = params.join(",");
transCalcAlg->setProperty("WavelengthRange", binString.toStdString());
break;
}

// Single
// Multiple
case 1:
transCalcAlg->setProperty("WavelengthRange", m_uiForm.leMultiple->text().toStdString());
break;
Expand Down
41 changes: 36 additions & 5 deletions Code/Mantid/MantidQt/CustomInterfaces/src/UserInputValidator.cpp
Expand Up @@ -45,13 +45,13 @@ namespace MantidQt
{
if(field->text() == "")
{
errorLabel->setText("*");
setErrorLabel(errorLabel, false);
m_errorMessages.append(name + " has been left blank.");
return false;
}
else
{
errorLabel->setText("");
setErrorLabel(errorLabel, true);
return true;
}
}
Expand All @@ -72,12 +72,12 @@ namespace MantidQt

if( fieldState == QValidator::Acceptable )
{
errorLabel->setText("");
setErrorLabel(errorLabel, true);
return true;
}
else
{
errorLabel->setText("*");
setErrorLabel(errorLabel, false);
m_errorMessages.append(errorMessage);
return false;
}
Expand Down Expand Up @@ -273,7 +273,7 @@ namespace MantidQt
if( m_errorMessages.isEmpty() )
return "";

return "Please correct the following:\n\n" + m_errorMessages.join("\n");
return "Please correct the following:\n" + m_errorMessages.join("\n");
}

/**
Expand All @@ -285,5 +285,36 @@ namespace MantidQt
{
return m_errorMessages.isEmpty();
}

/**
* Sets a validation label that is displyed next to the widget on the UI.
*
* @param errorLabel Label to change
* @param valid If the input was valid
*/
void UserInputValidator::setErrorLabel(QLabel * errorLabel, bool valid)
{
// Do nothing if no error label was provided
if(errorLabel == NULL)
return;

if(!valid)
{
// Set the label to be red
QPalette palette = errorLabel->palette();
palette.setColor(errorLabel->foregroundRole(), Qt::red);
errorLabel->setPalette(palette);

errorLabel->setText("*");
}
else
{
errorLabel->setText("");
}

// Only show the label if input is invalid
errorLabel->setVisible(!valid);
}

}
}

0 comments on commit ceb9a8d

Please sign in to comment.