Пример #1
0
/** Open the file dialog for a given property
 *
 * @param baseProp :: Property pointer
 * @return full path to the file(s) to load/save
 */
QString FilePropertyWidget::openFileDialog(Mantid::Kernel::Property *baseProp) {
  Mantid::API::FileProperty *prop =
      dynamic_cast<Mantid::API::FileProperty *>(baseProp);
  if (!prop)
    return "";

  /* MG 20/07/09: Static functions such as these that use native Windows and MAC
     dialogs
     in those environments are alot faster. This is unforunately at the expense
     of
     shell-like pattern matching, i.e. [0-9].
  */
  QString filename;
  if (prop->isLoadProperty()) {
    const auto filter = FileDialogHandler::getFilter(baseProp);
    const auto caption = FileDialogHandler::getCaption("Open file", baseProp);
    filename = QFileDialog::getOpenFileName(
        nullptr, caption,
        AlgorithmInputHistory::Instance().getPreviousDirectory(), filter);
  } else if (prop->isSaveProperty()) {
    filename = FileDialogHandler::getSaveFileName(nullptr, prop);
  } else if (prop->isDirectoryProperty()) {
    const auto caption =
        FileDialogHandler::getCaption("Choose a Directory", baseProp);
    filename = QFileDialog::getExistingDirectory(
        nullptr, caption,
        AlgorithmInputHistory::Instance().getPreviousDirectory());
  } else {
    throw std::runtime_error(
        "Invalid type of file property! This should not happen.");
  }

  if (!filename.isEmpty()) {
    AlgorithmInputHistory::Instance().setPreviousDirectory(
        QFileInfo(filename).absoluteDir().path());
  }
  return filename;
}
Пример #2
0
  /** Open the file dialog for a given property
   *
   * @param baseProp :: Property pointer
   * @return full path to the file(s) to load/save
   */
  QString FilePropertyWidget::openFileDialog(Mantid::Kernel::Property * baseProp)
  {
    Mantid::API::FileProperty* prop =
      dynamic_cast< Mantid::API::FileProperty* >( baseProp );
    if( !prop ) return "";

    //The allowed values in this context are file extensions
    std::set<std::string> exts = prop->allowedValues();
    std::string defaultExt = prop->getDefaultExt();

    /* MG 20/07/09: Static functions such as these that use native Windows and MAC dialogs
       in those environments are alot faster. This is unforunately at the expense of
       shell-like pattern matching, i.e. [0-9].
    */
    QString filename;
    if( prop->isLoadProperty() )
    {
      QString filter = getFileDialogFilter(exts, defaultExt);
      filename = QFileDialog::getOpenFileName(NULL, "Open file", AlgorithmInputHistory::Instance().getPreviousDirectory(), filter);
    }
    else if ( prop->isSaveProperty() )
    {
      // --------- Save a File -------------
      //Have each filter on a separate line with the default as the first
      QString filter;
      if( !defaultExt.empty() )
      {
        filter = "*" + QString::fromStdString(defaultExt) + ";;";
      }
      std::set<std::string>::const_iterator iend = exts.end();
      for( std::set<std::string>::const_iterator itr = exts.begin(); itr != iend; ++itr)
      {
        if( (*itr) != defaultExt )
        {
          filter.append("*"+QString::fromStdString(*itr) + ";;");
        }
      }
      //Remove last two semi-colons or else we get an extra empty option in the box
      filter.chop(2);
      // Prepend the default filter
      QString selectedFilter;
      filename = MantidQt::API::FileDialogHandler::getSaveFileName(NULL, "Save file", AlgorithmInputHistory::Instance().getPreviousDirectory(), filter, &selectedFilter);

      //Check the filename and append the selected filter if necessary
      if( QFileInfo(filename).completeSuffix().isEmpty() )
      {
        // Hack off the first star that the filter returns
        QString ext = selectedFilter;

        if( selectedFilter.startsWith("*.") )
        {
          // 1 character from the start
          ext = ext.remove(0,1);
        }
        else
        {
          ext = "";
        }

        if( filename.endsWith(".") && ext.startsWith(".") )
        {
          ext = ext.remove(0,1);
        }

        // Construct the full file name
        filename += ext;
      }
    }
    else if ( prop->isDirectoryProperty() )
    {
      filename = QFileDialog::getExistingDirectory(NULL, "Choose a Directory", AlgorithmInputHistory::Instance().getPreviousDirectory() );
    }
    else
    {
      throw std::runtime_error("Invalid type of file property! This should not happen.");
    }

    if( !filename.isEmpty() )
    {
      AlgorithmInputHistory::Instance().setPreviousDirectory(QFileInfo(filename).absoluteDir().path());
    }
    return filename;
  }