Ejemplo n.º 1
0
bool mitk::QmitkIOUtil::SaveToFileWriter( mitk::FileWriterWithInformation::Pointer fileWriter, mitk::BaseData::Pointer data,   const std::string fileName )
{
    // Ensure a valid filename
    QString qFileName(QString::fromStdString(fileName));
    QString qProposedFileName(qFileName);
    if(qFileName.isEmpty())
    {
        qProposedFileName.append(fileWriter->GetDefaultFilename());
    }
    qProposedFileName.append(fileWriter->GetDefaultExtension());
    qFileName = GetFileNameWithQDialog("Save file", qProposedFileName,
                                       QString::fromAscii(fileWriter->GetFileDialogPattern()));
    //do nothing if the user presses cancel
    if ( qFileName.isEmpty() )
      return false;

    // Check if an extension exists already and if not, append the default extension
    if ( !qFileName.contains( QRegExp("\\.\\w+$") ) )
    {
        qFileName.append( fileWriter->GetDefaultExtension() );
    }
    else
    {
        std::string extension = itksys::SystemTools::GetFilenameLastExtension( qFileName.toLocal8Bit().constData() );
        if (!fileWriter->IsExtensionValid(extension))
        {
            return false;
        }
    }
    //Write only if a valid name was set
    if(qFileName.isEmpty() == false)
    {
        fileWriter->SetFileName( qFileName.toLocal8Bit().constData() );
        fileWriter->DoWrite( data );
    }

    return true;
}
Ejemplo n.º 2
0
void CommonFunctionality::SaveToFileWriter( mitk::FileWriterWithInformation::Pointer fileWriter, mitk::BaseData::Pointer data, const char* aFileName, const char* propFileName)
{ 
   // Check if desired format is supported
  if (! fileWriter->CanWriteBaseDataType(data) ) {
    QMessageBox::critical(NULL,"ERROR","Could not write file. Invalid data type for file writer.");
    return;
  }

  // Ensure a valid filename
  QString fileName;
  if (aFileName == NULL)
  {
    QString proposedName("");
    if(propFileName == NULL)
    {
      proposedName.append(fileWriter->GetDefaultFilename());
    }
    else
    {
      proposedName.append(propFileName).append(fileWriter->GetDefaultExtension());
    }
    fileName = GetSaveFileNameStartingInLastDirectory("Save file", proposedName,QString::fromAscii(fileWriter->GetFileDialogPattern()));

    // Check if an extension exists already and if not, append the default extension
    if ( !fileName.contains( QRegExp("\\.\\w+$") ) )
    {
      fileName.append( fileWriter->GetDefaultExtension() );
    }
    else
    {
      std::string extension = itksys::SystemTools::GetFilenameLastExtension( fileName.toLocal8Bit().constData() );
      if (!fileWriter->IsExtensionValid(extension))
      {
        QString message;
        message.append("File extension not suitable for writing given data. Choose one extension of this list: ");
        message.append(fileWriter->GetPossibleFileExtensionsAsString().c_str());
        QMessageBox::critical(NULL,"ERROR",message);
        return;
      }
    }
  }
  else
    fileName = aFileName;


  // Write
  if (fileName.isEmpty() == false )
  {
    fileWriter->SetFileName( fileName.toLocal8Bit().constData() );
    fileWriter->DoWrite( data );
  }
}