示例#1
0
/**
  * A helper function to create the simple API
  * @param algName :: A string giving the name of the algorithm
  * @param path :: The path to the .m file that we should create
  */
void CreateSimpleAPIHelper(const std::string &algName,
                           const std::string &path) {
  IAlgorithm *alg;
  try {
    alg = FrameworkManager::Instance().createAlgorithm(algName);
  } catch (std::exception &) {
    std::string err = "An error occurred while writing the ";
    err += algName + " function definition.\n";
    mexErrMsgTxt(err.c_str());
    return;
  }
  std::string fullpath(path + algName + ".m");
  std::ofstream mfile(fullpath.c_str());

  typedef std::vector<Mantid::Kernel::Property *> PropertyVector;
  // parameter list
  mfile << "function res = " << algName << "(varargin)\n";
  // help string
  PropertyVector orderedProperties(alg->getProperties());
  std::sort(orderedProperties.begin(), orderedProperties.end(),
            PropertyOrdering());
  PropertyVector::const_iterator pIter = orderedProperties.begin();
  PropertyVector::const_iterator pEnd = orderedProperties.end();
  mfile << "%\t" << algName << "(";
  for (; pIter != pEnd;) {
    mfile << (*pIter)->name();
    if (++pIter != pEnd)
      mfile << ", ";
  }
  mfile << ")\n";
  mfile << "%\t\tArgument description:\n";
  pIter = orderedProperties.begin();
  unsigned int iOpt(0);
  for (; pIter != pEnd; ++pIter) {
    Mantid::Kernel::Property *prop = *pIter;
    mfile << "%\t\tName: " << prop->name() << ", Optional: ";
    if (prop->isValid() == "") {
      ++iOpt;
      mfile << "Yes, Default value: " << santizePropertyValue(prop->value());
    } else
      mfile << "No";
    mfile << ", Direction: "
          << Mantid::Kernel::Direction::asText(prop->direction()); // << ", ";
    auto allowed = prop->allowedValues();
    if (!allowed.empty()) {
      mfile << ", Allowed values: ";
      auto sIter = allowed.begin();
      auto sEnd = allowed.end();
      for (; sIter != sEnd;) {
        mfile << (*sIter);
        if (++sIter != sEnd)
          mfile << ", ";
      }
    }
    mfile << "\n";
  }
  mfile << "%\n%\tNote: All string arguments must be wrapped in single quotes "
           "''.\n";

  // The function definition
  mfile << "if nargin < " << (orderedProperties.size() - iOpt) << "\n"
        << "\tfprintf('All mandatory arguments have not been supplied, type "
           "\"help " << algName << "\" for more information\\n');\n"
        << "\treturn\n"
        << "end\n";

  mfile << "alg = MantidAlgorithm('" << algName << "');\n"
        << "argstring = '';\n";
  // Build arguments list
  mfile << "for i = 1:nargin\n"
        << "\targstring = strcat(argstring,varargin{i});\n"
        << "\tif i < nargin\n"
        << "\t\targstring = strcat(argstring, ';');\n"
        << "\tend\n"
        << "end\n";
  // Run the algorithm
  mfile << "res = run(alg, argstring);\n";
  mfile.close();
}
示例#2
0
    /**
    * Create the dynamic widgets for the concrete loader
    */
    void LoadDialog::createDynamicLayout()
    {
      // Disable the layout so that a widget cannot be interacted with while it may be being deleted
      m_form.propertyLayout->setEnabled(false);

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

      if( !m_form.fileWidget->isValid() ) return;
      // First step is the get the specific loader that is responsible
      IAlgorithm *loadAlg = getAlgorithm();
      const QString filenames = m_form.fileWidget->getUserInput().asString();
      if( filenames == m_currentFiles ) return;
      m_currentFiles = filenames;
      removeOldInputWidgets(m_form.propertyLayout); // The new file might be invalid
      try
      {
        loadAlg->setPropertyValue("Filename", filenames.toStdString());
      }
      catch(std::exception & exc)
      {
        m_form.fileWidget->setFileProblem(QString::fromStdString(exc.what()));
        m_form.propertyLayout->setEnabled(true);
        m_form.propertyLayout->activate();
        this->resize(this->width(), m_initialHeight + 15);
        
        // Reset the algorithm pointer so that the base class re-reads the properties and drops links from
        // old widgets meaning they are safe to remove
        setAlgorithm(loadAlg);
        tieStaticWidgets(false); //The ties are cleared when resetting the algorithm

        return;
      }
      // Reset the algorithm pointer so that the base class re-reads the properties and drops links from
      // old widgets meaning they are safe to remove
      setAlgorithm(loadAlg);
      tieStaticWidgets(false); //The ties are cleared when resetting the algorithm
      // Add the new ones
      const std::vector<Property*> & inputProps = loadAlg->getProperties();
      int dialogHeight = m_initialHeight;
      for( size_t i = 0; i < inputProps.size(); ++i )
      {
        const Property* prop = inputProps[i];
        const QString propName = QString::fromStdString(prop->name());
        if( propName == "OutputWorkspace" || propName == "Filename" ) continue;
        if( requiresUserInput(propName) )
        {
          dialogHeight += createWidgetsForProperty(prop, m_form.propertyLayout, m_form.scrollAreaWidgetContents);
        }
      }
      // Re-enable and recompute the size of the layout
      m_form.propertyLayout->setEnabled(true);
      m_form.propertyLayout->activate();

      const int screenHeight = QApplication::desktop()->height();
      // If the thing won't end up too big compared to the screen height,
      // resize the scroll area so we don't get a scroll bar
      if ( dialogHeight < 0.8*screenHeight ) this->resize(this->width(),dialogHeight + 20);

      // Make sure the OutputWorkspace value has been stored so that the validator is cleared appropriately
      QString wsName(m_form.workspaceEdit->text());
      if(!wsName.isEmpty()) storePropertyValue("OutputWorkspace",wsName);
      setPropertyValues(QStringList("Filename"));
    }