33 #include <QFileDialog>
34 #include <QMessageBox>
38 #include <itksys/SystemTools.hxx>
42 struct QmitkIOUtil::Impl
44 struct ReaderOptionsDialogFunctor :
public ReaderOptionsFunctorBase
46 virtual bool operator()(LoadInfo &loadInfo)
override
49 if (dialog.exec() == QDialog::Accepted)
51 return !dialog.ReuseOptions();
55 loadInfo.m_Cancel =
true;
61 struct WriterOptionsDialogFunctor :
public WriterOptionsFunctorBase
63 virtual bool operator()(SaveInfo &saveInfo)
override
66 if (dialog.exec() == QDialog::Accepted)
68 return !dialog.ReuseOptions();
72 saveInfo.m_Cancel =
true;
79 struct MimeTypeComparison :
public std::unary_function<mitk::MimeType, bool>
81 MimeTypeComparison(
const std::string &mimeTypeName) : m_Name(mimeTypeName) {}
83 const std::string m_Name;
91 std::vector<std::string> categories = mimeTypeProvider->
GetCategories();
92 for (std::vector<std::string>::iterator cat = categories.begin(); cat != categories.end(); ++cat)
94 QSet<QString> filterExtensions;
96 for (std::vector<mitk::MimeType>::iterator mt = mimeTypes.begin(); mt != mimeTypes.end(); ++mt)
98 std::vector<std::string> extensions = mt->GetExtensions();
99 for (std::vector<std::string>::iterator ext = extensions.begin(); ext != extensions.end(); ++ext)
101 filterExtensions << QString::fromStdString(*ext);
105 QString filter = QString::fromStdString(*cat) +
" (";
106 foreach (
const QString &extension, filterExtensions)
108 filter +=
"*." + extension +
" ";
110 filter = filter.replace(filter.size() - 1, 1,
')');
111 filters +=
";;" + filter;
113 filters.prepend(
"All (*)");
119 std::vector<LoadInfo> loadInfos;
120 foreach (
const QString &file, paths)
122 loadInfos.push_back(
LoadInfo(file.toStdString()));
125 Impl::ReaderOptionsDialogFunctor optionsCallback;
126 std::string errMsg =
Load(loadInfos, NULL, NULL, &optionsCallback);
129 QMessageBox::warning(parent,
"Error reading files", QString::fromStdString(errMsg));
133 QList<mitk::BaseData::Pointer> qResult;
134 for (std::vector<LoadInfo>::const_iterator iter = loadInfos.begin(), iterEnd = loadInfos.end(); iter != iterEnd;
137 for (
const auto &elem : iter->m_Output)
149 std::vector<LoadInfo> loadInfos;
150 foreach (
const QString &file, paths)
152 loadInfos.push_back(
LoadInfo(QFile::encodeName(file).constData()));
156 Impl::ReaderOptionsDialogFunctor optionsCallback;
157 std::string errMsg =
Load(loadInfos, nodeResult, &storage, &optionsCallback);
160 QMessageBox::warning(parent,
"Error reading files", QString::fromStdString(errMsg));
169 return Load(paths, parent);
178 return Load(paths, storage, parent);
182 const QString &defaultBaseName,
183 const QString &defaultPath,
186 std::vector<const mitk::BaseData *> dataVector;
187 dataVector.push_back(data);
188 QStringList defaultBaseNames;
189 defaultBaseNames.push_back(defaultBaseName);
190 return Save(dataVector, defaultBaseNames, defaultPath, parent).back();
194 const QStringList &defaultBaseNames,
195 const QString &defaultPath,
198 QStringList fileNames;
199 QString currentPath = defaultPath;
201 std::vector<SaveInfo> saveInfos;
204 for (std::vector<const mitk::BaseData *>::const_iterator dataIter = data.begin(), dataIterEnd = data.end();
205 dataIter != dataIterEnd;
206 ++dataIter, ++counter)
213 if (filters.
Size() < 2)
215 QMessageBox::warning(
217 "Saving not possible",
218 QString(
"No writer available for type \"%1\"").arg(QString::fromStdString((*dataIter)->GetNameOfClass())));
223 QString filterString = filters.
ToString();
225 QString fileName = currentPath;
226 QString dialogTitle =
"Save " + QString::fromStdString((*dataIter)->GetNameOfClass());
227 if (counter < defaultBaseNames.size())
229 dialogTitle +=
" \"" + defaultBaseNames[counter] +
"\"";
230 fileName += QDir::separator() + defaultBaseNames[counter];
244 QString nextName = QFileDialog::getSaveFileName(parent, dialogTitle, fileName, filterString, &selectedFilter);
246 if (nextName.isEmpty())
254 std::string stdFileName = QFile::encodeName(fileName).constData();
255 QFileInfo fileInfo(fileName);
256 currentPath = fileInfo.absolutePath();
257 QString suffix = fileInfo.completeSuffix();
261 if (fileInfo.exists() && !fileInfo.isFile())
263 QMessageBox::warning(parent,
"Saving not possible", QString(
"The path \"%1\" is not a file").arg(fileName));
270 std::vector<mitk::MimeType> filterMimeTypes = filters.
GetMimeTypes();
271 for (std::vector<mitk::MimeType>::const_iterator mimeTypeIter = filterMimeTypes.begin(),
272 mimeTypeIterEnd = filterMimeTypes.end();
273 mimeTypeIter != mimeTypeIterEnd;
276 if (mimeTypeIter->MatchesExtension(stdFileName))
278 selectedMimeType = *mimeTypeIter;
283 if (!selectedMimeType.
IsValid())
294 if (!fileInfo.exists())
298 if (suffix.isEmpty())
306 selectedMimeType = filterMimeType;
309 if (selectedMimeType.
IsValid())
311 suffix = QString::fromStdString(selectedMimeType.
GetExtensions().front());
312 fileName +=
"." + suffix;
313 stdFileName = QFile::encodeName(fileName).constData();
316 fileInfo = QFileInfo(fileName);
317 if (fileInfo.exists())
319 if (!fileInfo.isFile())
321 QMessageBox::warning(
322 parent,
"Saving not possible", QString(
"The path \"%1\" is not a file").arg(fileName));
325 if (QMessageBox::question(
328 QString(
"A file named \"%1\" already exists. Do you want to replace it?").arg(fileName)) ==
338 if (!selectedMimeType.
IsValid())
341 QMessageBox::warning(
342 parent,
"Saving not possible", QString(
"No mime-type available which can handle \"%1\".").arg(fileName));
346 if (!QFileInfo(fileInfo.absolutePath()).isWritable())
348 QMessageBox::warning(parent,
"Saving not possible", QString(
"The path \"%1\" is not writable").arg(fileName));
352 fileNames.push_back(fileName);
353 saveInfo.
m_Path = stdFileName;
357 saveInfos.push_back(saveInfo);
360 if (!saveInfos.empty())
362 Impl::WriterOptionsDialogFunctor optionsCallback;
363 std::string errMsg =
Save(saveInfos, &optionsCallback);
366 QMessageBox::warning(parent,
"Error writing files", QString::fromStdString(errMsg));
376 Save(data, fileName);
381 Save(surface, fileName);
386 Save(image, fileName);
391 Save(pointset, fileName);
394 struct QmitkIOUtil::SaveFilter::Impl
400 m_FilterStrings.push_back(
"All (*.*)");
406 for (std::vector<mitk::MimeType>::const_reverse_iterator iter = mimeTypes.rbegin(), iterEnd = mimeTypes.rend();
410 QList<QString> filterExtensions;
412 std::vector<std::string> extensions = mimeType.
GetExtensions();
413 for (
auto &extension : extensions)
415 filterExtensions << QString::fromStdString(extension);
417 if (m_DefaultExtension.isEmpty())
419 m_DefaultExtension = QString::fromStdString(extensions.front());
422 QString filter = QString::fromStdString(mimeType.
GetComment()) +
" (";
423 foreach (
const QString &extension, filterExtensions)
425 filter +=
"*." + extension +
" ";
427 filter = filter.replace(filter.size() - 1, 1,
')');
428 m_MimeTypes.push_back(mimeType);
429 m_FilterStrings.push_back(filter);
434 std::vector<mitk::MimeType> m_MimeTypes;
435 QStringList m_FilterStrings;
436 QString m_DefaultExtension;
455 d.reset(
new Impl(*other.d));
461 return d->m_MimeTypes;
466 std::vector<mitk::MimeType>::const_iterator iter =
467 std::find_if(d->m_MimeTypes.begin(), d->m_MimeTypes.end(), MimeTypeComparison(mimeType));
468 if (iter == d->m_MimeTypes.end())
472 int index =
static_cast<int>(iter - d->m_MimeTypes.begin());
473 if (index < 0 || index >= d->m_FilterStrings.size())
477 return d->m_FilterStrings[index];
482 int index = d->m_FilterStrings.indexOf(filter);
487 return d->m_MimeTypes[index];
492 if (d->m_FilterStrings.size() > 1)
494 return d->m_FilterStrings.at(1);
496 else if (d->m_FilterStrings.size() > 0)
498 return d->m_FilterStrings.front();
505 return d->m_DefaultExtension;
510 if (d->m_MimeTypes.size() > 1)
512 return d->m_MimeTypes.at(1);
514 else if (d->m_MimeTypes.size() > 0)
516 return d->m_MimeTypes.front();
523 return d->m_FilterStrings.join(
";;");
528 return d->m_FilterStrings.size();
533 return d->m_FilterStrings.isEmpty();
538 return std::find_if(d->m_MimeTypes.begin(), d->m_MimeTypes.end(), MimeTypeComparison(mimeType)) !=
539 d->m_MimeTypes.end();
QString GetDefaultExtension() const
Data management class that handles 'was created by' relations.
itk::SmartPointer< Self > Pointer
FileWriterSelector m_WriterSelector
Contains a set of IFileWriter objects.
static IMimeTypeProvider * GetMimeTypeProvider(us::ModuleContext *context=us::GetModuleContext())
Get an IMimeTypeProvider instance.
static void SavePointSetWithDialog(mitk::PointSet::Pointer pointset, std::string fileName="", QWidget *parent=NULL)
SavePointSetWithDialog Convenience method to save a pointset with a Qt dialog.
Base of all data objects.
static mitk::MimeType ALL_MIMETYPE()
bool Select(const std::string &mimeType)
QString GetFilterForMimeType(const std::string &mimeType) const
bool ContainsMimeType(const std::string &mimeType)
std::vector< std::string > GetExtensions() const
std::string GetName() const
The CustomMimeType class represents a custom mime-type which may be registered as a service object...
MimeType m_MimeType
The selected mime-type, used to restrict results from FileWriterSelector.
static QString GetFileOpenFilterString()
GetFilterString.
virtual std::vector< MimeType > GetMimeTypesForCategory(const std::string &category) const =0
mitk::MimeType GetMimeTypeForFilter(const QString &filter) const
virtual std::vector< std::string > GetCategories() const =0
Get a sorted and unique list of mime-type categories.
static void SaveImageWithDialog(mitk::Image::Pointer image, std::string fileName="", QWidget *parent=NULL)
SaveImageWithDialog Convenience method to save an image with a Qt dialog.
static QList< mitk::BaseData::Pointer > Load(const QStringList &paths, QWidget *parent=NULL)
Loads the specified files.
std::vector< MimeType > GetMimeTypes() const
static void SaveBaseDataWithDialog(mitk::BaseData *data, std::string fileName, QWidget *parent=NULL)
SaveBaseDataWithDialog Convenience method to save any data with a Qt dialog.
SaveFilter & operator=(const SaveFilter &other)
std::vector< mitk::MimeType > GetMimeTypes() const
The MimeType class represens a registered mime-type. It is an immutable wrapper for mitk::CustomMimeT...
QString GetDefaultFilter() const
SaveFilter(const SaveFilter &other)
static void SaveSurfaceWithDialog(mitk::Surface::Pointer surface, std::string fileName="", QWidget *parent=NULL)
SaveSurfaceWithDialog Convenience method to save a surface with a Qt dialog.
mitk::MimeType GetDefaultMimeType() const
std::string m_Path
The path to write the BaseData object to.
static QString Save(const mitk::BaseData *data, const QString &defaultBaseName, const QString &defaultPath=QString(), QWidget *parent=NULL)
std::string GetComment() const
A RAII helper class for core service objects.
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.