Example #1
0
/** Set a value for a widget.
 *
 * The function needs to know about the types of widgets
 * that are being used. Currently it knows about QComboBox, QLineEdit and
 * QCheckBox
 * @param widget :: A pointer to the widget
 * @param propName :: The property name
 */
void AlgorithmDialog::setPreviousValue(QWidget *widget,
                                       const QString &propName) {
  // If is called from a script, check if we have such property
  if (isForScript() && !getAlgorithmProperty(propName))
    return;

  QString value = getPreviousValue(propName);

  Mantid::Kernel::Property *property = getAlgorithmProperty(propName);

  // Do the right thing for the widget type
  if (QComboBox *opts = qobject_cast<QComboBox *>(widget)) {
    if (property && value.isEmpty()) {
      value = QString::fromStdString(property->value());
    }
    int index = opts->findText(value);
    if (index >= 0) {
      opts->setCurrentIndex(index);
    }
    return;
  }
  if (QAbstractButton *checker = qobject_cast<QAbstractButton *>(widget)) {
    if (value.isEmpty() &&
        dynamic_cast<Mantid::Kernel::PropertyWithValue<bool> *>(property))
      value = QString::fromStdString(property->value());
    checker->setChecked(value != "0");
    return;
  }
  if (QDateTimeEdit *dateEdit = qobject_cast<QDateTimeEdit *>(widget)) {
    // String in ISO8601 format
    DateAndTime t = DateAndTime::getCurrentTime();
    try {
      t.setFromISO8601(verifyAndSanitizeISO8601(value.toStdString()));
    } catch (std::exception &) {
    }
    dateEdit->setDate(QDate(t.year(), t.month(), t.day()));
    dateEdit->setTime(QTime(t.hour(), t.minute(), t.second(), 0));
    return;
  }

  QLineEdit *textfield = qobject_cast<QLineEdit *>(widget);
  MantidWidget *mtdwidget = qobject_cast<MantidWidget *>(widget);
  if (textfield || mtdwidget) {
    if (!isForScript()) {
      if (textfield)
        textfield->setText(value);
      else
        mtdwidget->setUserInput(value);
    } else {
      // Need to check if this is the default value as we don't fill them in if
      // they are
      if (m_python_arguments.contains(propName) || !property->isDefault()) {
        if (textfield)
          textfield->setText(value);
        else
          mtdwidget->setUserInput(value);
      }
    }
    return;
  }

  PropertyWidget *propWidget = qobject_cast<PropertyWidget *>(widget);
  if (propWidget) {
    propWidget->setPreviousValue(value);

    return;
  }

  // Reaching here means we have a widget type we don't understand. Tell the
  // developer
  QMessageBox::warning(
      this, windowTitle(),
      QString("Cannot set value for ") + widget->metaObject()->className() +
          ". Update AlgorithmDialog::setValue() to cope with this widget.");
}