Skip to content

Commit

Permalink
Add different error messages for file loading errors
Browse files Browse the repository at this point in the history
  • Loading branch information
davidborland committed Dec 11, 2019
1 parent 44ebc4d commit c851fcd
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 34 deletions.
127 changes: 116 additions & 11 deletions MainWindow.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,23 @@ void MainWindow::on_actionOpen_Image_File_triggered() {
SetDefaultDirectory(defaultImageDirectoryKey, fileName);

// Load data
if (!visualizationContainer->OpenImageFile(fileName.toStdString())) {
VisualizationContainer::FileErrorCode errorCode = visualizationContainer->OpenImageFile(fileName.toStdString());

if (errorCode != VisualizationContainer::Success) {
QMessageBox errorMessage;
errorMessage.setIcon(QMessageBox::Warning);
errorMessage.setText("Could not open file.");
errorMessage.exec();

switch (errorCode) {
case VisualizationContainer::WrongFileType:
errorMessage.setInformativeText("Wrong file type.");
errorMessage.exec();
break;

default:
errorMessage.setInformativeText("Unknown error.");
errorMessage.exec();
}
}
}

Expand All @@ -108,6 +121,16 @@ void MainWindow::on_actionOpen_Image_Stack_triggered() {
QDir directory = fileInfo.absoluteDir();
QFileInfoList fileInfoList = fileInfo.absoluteDir().entryInfoList(QDir::Files, QDir::Name);

// Check for files
if (fileInfoList.length() == 0) {
QMessageBox errorMessage;
errorMessage.setIcon(QMessageBox::Warning);
errorMessage.setText("Could not open files.");
errorMessage.setInformativeText("No files present.");
errorMessage.exec();
return;
}

// Get file names
std::vector<std::string> fileNames;

Expand All @@ -116,10 +139,23 @@ void MainWindow::on_actionOpen_Image_Stack_triggered() {
}

// Load data
if (!visualizationContainer->OpenImageStack(fileNames)) {
VisualizationContainer::FileErrorCode errorCode = visualizationContainer->OpenImageFile(fileName.toStdString());

if (errorCode != VisualizationContainer::Success) {
QMessageBox errorMessage;
errorMessage.setIcon(QMessageBox::Warning);
errorMessage.setText("Could not open files.");
errorMessage.exec();

switch (errorCode) {
case VisualizationContainer::WrongFileType:
errorMessage.setInformativeText("Wrong file type.");
errorMessage.exec();
break;

default:
errorMessage.setInformativeText("Unknown error.");
errorMessage.exec();
}
}
}

Expand All @@ -138,10 +174,33 @@ void MainWindow::on_actionOpen_Segmentation_File_triggered() {
SetDefaultDirectory(defaultSegmentationDirectoryKey, fileName);

// Load segmentation data
if (!visualizationContainer->OpenSegmentationFile(fileName.toStdString())) {
VisualizationContainer::FileErrorCode errorCode = visualizationContainer->OpenSegmentationFile(fileName.toStdString());

if (errorCode != VisualizationContainer::Success) {
QMessageBox errorMessage;
errorMessage.setIcon(QMessageBox::Warning);
errorMessage.setText("Could not open file.");
errorMessage.exec();

switch (errorCode) {
case VisualizationContainer::WrongFileType:
errorMessage.setInformativeText("Wrong file type.");
errorMessage.exec();
break;

case VisualizationContainer::NoImageData:
errorMessage.setInformativeText("Load image data first.");
errorMessage.exec();
break;

case VisualizationContainer::VolumeMismatch:
errorMessage.setInformativeText("Segmentation data volume does not match loaded image data volume.");
errorMessage.exec();
break;

default:
errorMessage.setInformativeText("Unknown error.");
errorMessage.exec();
}
}
}

Expand All @@ -164,6 +223,16 @@ void MainWindow::on_actionOpen_Segmentation_Stack_triggered() {
QDir directory = fileInfo.absoluteDir();
QFileInfoList fileInfoList = fileInfo.absoluteDir().entryInfoList(QDir::Files, QDir::Name);

// Check for files
if (fileInfoList.length() == 0) {
QMessageBox errorMessage;
errorMessage.setIcon(QMessageBox::Warning);
errorMessage.setText("Could not open files.");
errorMessage.setInformativeText("No files present.");
errorMessage.exec();
return;
}

// Get file names
std::vector<std::string> fileNames;

Expand All @@ -172,10 +241,33 @@ void MainWindow::on_actionOpen_Segmentation_Stack_triggered() {
}

// Load segmentation data
if (!visualizationContainer->OpenSegmentationStack(fileNames)) {
VisualizationContainer::FileErrorCode errorCode = visualizationContainer->OpenSegmentationStack(fileNames);

if (errorCode != VisualizationContainer::Success) {
QMessageBox errorMessage;
errorMessage.setIcon(QMessageBox::Warning);
errorMessage.setText("Could not open files.");
errorMessage.exec();

switch (errorCode) {
case VisualizationContainer::WrongFileType:
errorMessage.setInformativeText("Wrong file type.");
errorMessage.exec();
break;

case VisualizationContainer::NoImageData:
errorMessage.setInformativeText("Load image data first.");
errorMessage.exec();
break;

case VisualizationContainer::VolumeMismatch:
errorMessage.setInformativeText("Segmentation data volume does not match loaded image data volume.");
errorMessage.exec();
break;

default:
errorMessage.setInformativeText("Unknown error.");
errorMessage.exec();
}
}
}

Expand All @@ -194,10 +286,23 @@ void MainWindow::on_actionSave_Segmentation_Data_triggered() {
SetDefaultDirectory(defaultSegmentationDirectoryKey, fileName);

// Save segmentation data
if (!visualizationContainer->SaveSegmentationData(fileName.toStdString())) {
VisualizationContainer::FileErrorCode errorCode = visualizationContainer->SaveSegmentationData(fileName.toStdString());

if (errorCode != VisualizationContainer::Success) {
QMessageBox errorMessage;
errorMessage.setText("Could not save file.");
errorMessage.exec();
errorMessage.setIcon(QMessageBox::Warning);
errorMessage.setText("Could not save data.");

switch (errorCode) {
case VisualizationContainer::WrongFileType:
errorMessage.setInformativeText("Wrong file type.");
errorMessage.exec();
break;

default:
errorMessage.setInformativeText("Unknown error.");
errorMessage.exec();
}
}
}

Expand Down
47 changes: 29 additions & 18 deletions VisualizationContainer.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ VisualizationContainer::~VisualizationContainer() {
delete regions;
}

bool VisualizationContainer::OpenImageFile(const std::string& fileName) {
VisualizationContainer::FileErrorCode VisualizationContainer::OpenImageFile(const std::string& fileName) {
// Get the file extension
std::string extension = fileName.substr(fileName.find_last_of(".") + 1);

Expand All @@ -138,7 +138,7 @@ bool VisualizationContainer::OpenImageFile(const std::string& fileName) {

SetImageData(reader->GetOutput());

return true;
return Success;
}
else if (extension == "nii") {
vtkSmartPointer<vtkNIFTIImageReader> reader = vtkSmartPointer<vtkNIFTIImageReader>::New();
Expand All @@ -147,13 +147,13 @@ bool VisualizationContainer::OpenImageFile(const std::string& fileName) {

SetImageData(reader->GetOutput());

return true;
return Success;
}

return false;
return WrongFileType;
}

bool VisualizationContainer::OpenImageStack(const std::vector<std::string>& fileNames) {
VisualizationContainer::FileErrorCode VisualizationContainer::OpenImageStack(const std::vector<std::string>& fileNames) {
// Get the file extension
std::string extension = fileNames[0].substr(fileNames[0].find_last_of(".") + 1);

Expand All @@ -173,13 +173,15 @@ bool VisualizationContainer::OpenImageStack(const std::vector<std::string>& file

SetImageData(reader->GetOutput());

return true;
return Success;
}

return false;
return WrongFileType;
}

bool VisualizationContainer::OpenSegmentationFile(const std::string& fileName) {
VisualizationContainer::FileErrorCode VisualizationContainer::OpenSegmentationFile(const std::string& fileName) {
if (data == nullptr) return NoImageData;

// Get the file extension
std::string extension = fileName.substr(fileName.find_last_of(".") + 1);

Expand All @@ -194,7 +196,10 @@ bool VisualizationContainer::OpenSegmentationFile(const std::string& fileName) {

qtWindow->UpdateRegionTable(regions);

return true;
return Success;
}
else {
return VolumeMismatch;
}
}
else if (extension == "nii") {
Expand All @@ -207,15 +212,18 @@ bool VisualizationContainer::OpenSegmentationFile(const std::string& fileName) {

qtWindow->UpdateRegionTable(regions);

return true;
return Success;
}
else {
return VolumeMismatch;
}
}

return false;
return WrongFileType;
}

bool VisualizationContainer::OpenSegmentationStack(const std::vector<std::string>& fileNames) {
if (fileNames.size() == 0) return false;
VisualizationContainer::FileErrorCode VisualizationContainer::OpenSegmentationStack(const std::vector<std::string>& fileNames) {
if (data == nullptr) return NoImageData;

// Get the file extension
std::string extension = fileNames[0].substr(fileNames[0].find_last_of(".") + 1);
Expand All @@ -239,14 +247,17 @@ bool VisualizationContainer::OpenSegmentationStack(const std::vector<std::string

qtWindow->UpdateRegionTable(regions);

return true;
return Success;
}
else {
return VolumeMismatch;
}
}

return false;
return WrongFileType;
}

bool VisualizationContainer::SaveSegmentationData(const std::string& fileName) {
VisualizationContainer::FileErrorCode VisualizationContainer::SaveSegmentationData(const std::string& fileName) {
std::string extension = fileName.substr(fileName.find_last_of(".") + 1);

if (extension == "vti") {
Expand All @@ -272,12 +283,12 @@ bool VisualizationContainer::SaveSegmentationData(const std::string& fileName) {
writer->Update();
}
else {
return false;
return WrongFileType;
}

SaveRegionMetadata(fileName + ".json");

return true;
return Success;
}

void VisualizationContainer::SegmentVolume() {
Expand Down
17 changes: 12 additions & 5 deletions VisualizationContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,21 @@ class VisualizationContainer {
MainWindow* mainWindow);
~VisualizationContainer();

bool OpenImageFile(const std::string& fileName);
bool OpenImageStack(const std::vector<std::string>& fileNames);
enum FileErrorCode {
Success = 1,
WrongFileType,
NoImageData,
VolumeMismatch
};

bool OpenSegmentationFile(const std::string& fileName);
bool OpenSegmentationStack(const std::vector<std::string>& fileNames);
FileErrorCode OpenImageFile(const std::string& fileName);
FileErrorCode OpenImageStack(const std::vector<std::string>& fileNames);

FileErrorCode OpenSegmentationFile(const std::string& fileName);
FileErrorCode OpenSegmentationStack(const std::vector<std::string>& fileNames);

void SegmentVolume();
bool SaveSegmentationData(const std::string& fileName);
FileErrorCode SaveSegmentationData(const std::string& fileName);

void ToggleInteractionMode();

Expand Down

0 comments on commit c851fcd

Please sign in to comment.