Пример #1
0
/**
 * Validate user input.
 * Outputs any warnings to the results log at warning level.
 *
 * @param silent If the results should not be logged
 * @return Result of validation
 */
bool SampleTransmission::validate(bool silent) {
  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);

  // Ensure number density is not zero
  uiv.setErrorLabel(
      m_uiForm.valNumberDensity,
      uiv.checkNotEqual("Number Density", m_uiForm.spNumberDensity->value()));

  // Ensure thickness is not zero
  uiv.setErrorLabel(
      m_uiForm.valThickness,
      uiv.checkNotEqual("Thickness", m_uiForm.spThickness->value()));

  // Give error message
  if (!silent && !uiv.isAllInputValid())
    showInformationBox(uiv.generateErrorMessage());

  return uiv.isAllInputValid();
}
Пример #2
0
/**
 * Does validation on the user input.
 *
 * @param silent Set to true to avoid creating an error message
 * @return True if all user input is valid
 */
bool CalculatePaalmanPings::doValidation(bool silent) {
  UserInputValidator uiv;

  uiv.checkDataSelectorIsValid("Sample", m_uiForm.dsSample);
  const auto sampleWsName = m_uiForm.dsSample->getCurrentDataName();
  const auto sampleWsNameStr = sampleWsName.toStdString();
  bool sampleExists =
      AnalysisDataService::Instance().doesExist(sampleWsNameStr);

  if (sampleExists &&
      !AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(
          sampleWsNameStr)) {
    uiv.addErrorMessage(
        "Invalid sample workspace. Ensure a MatrixWorkspace is provided.");
  }

  // Validate chemical formula
  if (uiv.checkFieldIsNotEmpty("Sample Chemical Formula",
                               m_uiForm.leSampleChemicalFormula,
                               m_uiForm.valSampleChemicalFormula))
    uiv.checkFieldIsValid("Sample Chemical Formula",
                          m_uiForm.leSampleChemicalFormula,
                          m_uiForm.valSampleChemicalFormula);
  const auto sampleChem =
      m_uiForm.leSampleChemicalFormula->text().toStdString();
  try {
    Mantid::Kernel::Material::parseChemicalFormula(sampleChem);
  } catch (std::runtime_error &ex) {
    UNUSED_ARG(ex);
    uiv.addErrorMessage("Chemical Formula for Sample was not recognised.");
    uiv.setErrorLabel(m_uiForm.valSampleChemicalFormula, false);
  }

  if (m_uiForm.ckUseCan->isChecked()) {
    uiv.checkDataSelectorIsValid("Can", m_uiForm.dsContainer);

    // Validate chemical formula
    if (uiv.checkFieldIsNotEmpty("Can Chemical Formula",
                                 m_uiForm.leCanChemicalFormula,
                                 m_uiForm.valCanChemicalFormula))
      uiv.checkFieldIsValid("Can Chemical Formula",
                            m_uiForm.leCanChemicalFormula,
                            m_uiForm.valCanChemicalFormula);

    const auto containerChem =
        m_uiForm.leCanChemicalFormula->text().toStdString();
    try {
      Mantid::Kernel::Material::parseChemicalFormula(containerChem);
    } catch (std::runtime_error &ex) {
      UNUSED_ARG(ex);
      uiv.addErrorMessage("Chemical Formula for Container was not recognised.");
      uiv.setErrorLabel(m_uiForm.valCanChemicalFormula, false);
    }

    const auto containerWsName = m_uiForm.dsContainer->getCurrentDataName();
    const auto containerWsNameStr = containerWsName.toStdString();
    bool containerExists =
        AnalysisDataService::Instance().doesExist(containerWsNameStr);

    if (containerExists &&
        !AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(
            containerWsNameStr)) {
      uiv.addErrorMessage(
          "Invalid container workspace. Ensure a MatrixWorkspace is provided.");
    }

    // Ensure sample and container are the same kind of data
    const auto sampleType = sampleWsName.right(sampleWsName.length() -
                                               sampleWsName.lastIndexOf("_"));
    const auto containerType = containerWsName.right(
        containerWsName.length() - containerWsName.lastIndexOf("_"));

    g_log.debug() << "Sample type is: " << sampleType.toStdString() << '\n';
    g_log.debug() << "Can type is: " << containerType.toStdString() << '\n';

    if (containerType != sampleType)
      uiv.addErrorMessage(
          "Sample and can workspaces must contain the same type of data.");

    // Shape validation

    const auto shape = m_uiForm.cbSampleShape->currentIndex();
    if (shape == 1 && m_uiForm.ckUseCan->isChecked()) {
      auto sampleRadius = m_uiForm.spCylSampleOuterRadius->value();
      auto containerRadius = m_uiForm.spCylCanOuterRadius->value();
      if (containerRadius <= sampleRadius) {
        uiv.addErrorMessage(
            "Container radius must be bigger than sample radius");
      }
    }
    if (shape == 2) {
      auto sampleInnerRadius = m_uiForm.spAnnSampleInnerRadius->value();
      auto sampleOuterRadius = m_uiForm.spAnnSampleOuterRadius->value();
      if (sampleOuterRadius <= sampleInnerRadius) {
        uiv.addErrorMessage(
            "Sample outer radius must be bigger than sample inner radius");
      }
      if (m_uiForm.ckUseCan->isChecked()) {
        auto containerRadius = m_uiForm.spAnnCanOuterRadius->value();
        if (containerRadius <= sampleOuterRadius) {
          uiv.addErrorMessage(
              "Container outer radius must be bigger than sample outer radius");
        }
      }
    }
  }

  // Show error message if needed
  if (!uiv.isAllInputValid() && !silent)
    emit showMessageBox(uiv.generateErrorMessage());

  return uiv.isAllInputValid();
}