Commit 19939b4f authored by Matthias Puchner's avatar Matthias Puchner
Browse files

improve error handling for legacy loader

parent 869f7056
Pipeline #33400 passed with stage
in 25 minutes and 28 seconds
...@@ -87,11 +87,8 @@ public: ...@@ -87,11 +87,8 @@ public:
//! Process the file contents. Can be called more than once, e.g. if the import settings have //! Process the file contents. Can be called more than once, e.g. if the import settings have
//! changed. //! changed.
//! Return an error string in case of a fatal error (item should not be kept). Any non fatal //! Any error has to be stored in the loader (see numErrors()).
//! error which might be solvable by changes in import settings or by selecting a different virtual void processContents() = 0;
//! loader is not to be delivered here but has to be a processing error / warning, so that the
//! item can be kept and its import settings can be changed.
virtual QString processContents() = 0;
//! Number of errors found while processing the content. An error means that either a particular //! Number of errors found while processing the content. An error means that either a particular
//! content (line) can't be used or may be suspicious (line related error), or that the whole //! content (line) can't be used or may be suspicious (line related error), or that the whole
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
#include "GUI/coregui/DataLoaders/AutomaticDataLoader1DResultModel.h" #include "GUI/coregui/DataLoaders/AutomaticDataLoader1DResultModel.h"
#include "GUI/coregui/Models/RealDataItem.h" #include "GUI/coregui/Models/RealDataItem.h"
#include "GUI/coregui/Models/SpecularDataItem.h" #include "GUI/coregui/Models/SpecularDataItem.h"
#include "GUI/coregui/utils/DeserializationException.h"
#include "GUI/coregui/utils/ImportDataInfo.h" #include "GUI/coregui/utils/ImportDataInfo.h"
#include "qcustomplot.h" #include "qcustomplot.h"
...@@ -38,6 +39,40 @@ AbstractDataLoader* AutomaticDataLoader1D::clone() const ...@@ -38,6 +39,40 @@ AbstractDataLoader* AutomaticDataLoader1D::clone() const
return loader; return loader;
} }
QByteArray AutomaticDataLoader1D::serialize() const
{
QByteArray a;
QDataStream s(&a, QIODevice::WriteOnly);
s.setVersion(QDataStream::Qt_5_12);
s << (quint8)1; // version
s << m_fileContent;
s << m_error;
return a;
}
void AutomaticDataLoader1D::deserialize(const QByteArray& data)
{
m_fileContent.clear();
m_error.clear();
QDataStream s(data);
s.setVersion(QDataStream::Qt_5_12);
quint8 version;
s >> version;
if (version == 1) {
s >> m_fileContent;
s >> m_error;
} else
throw DeserializationException::tooNew();
if (s.status() != QDataStream::Ok)
throw DeserializationException::streamError();
}
void AutomaticDataLoader1D::setFileContents(const QByteArray& fileContent) void AutomaticDataLoader1D::setFileContents(const QByteArray& fileContent)
{ {
m_fileContent = fileContent; m_fileContent = fileContent;
...@@ -48,7 +83,7 @@ QByteArray AutomaticDataLoader1D::fileContent() const ...@@ -48,7 +83,7 @@ QByteArray AutomaticDataLoader1D::fileContent() const
return m_fileContent; return m_fileContent;
} }
QString AutomaticDataLoader1D::processContents() void AutomaticDataLoader1D::processContents()
{ {
ASSERT(m_item != nullptr); ASSERT(m_item != nullptr);
ASSERT(m_item->isSpecularData()); ASSERT(m_item->isSpecularData());
...@@ -59,20 +94,24 @@ QString AutomaticDataLoader1D::processContents() ...@@ -59,20 +94,24 @@ QString AutomaticDataLoader1D::processContents()
ImportDataInfo importInfo(std::move(*oData), Axes::Units::QSPACE); ImportDataInfo importInfo(std::move(*oData), Axes::Units::QSPACE);
m_item->setImportData(std::move(importInfo)); m_item->setImportData(std::move(importInfo));
m_error.clear();
emit contentsProcessed();
return QString();
} catch (std::runtime_error& ex) { } catch (std::runtime_error& ex) {
m_item->removeNativeData(); m_item->removeNativeData();
m_item->specularDataItem()->setOutputData(nullptr); m_item->specularDataItem()->setOutputData(nullptr);
emit contentsProcessed(); m_error = QString::fromStdString(ex.what());
return QString::fromStdString( if (m_error.isEmpty())
ex.what()); // #baimport ++ this error gets lost if file is added m_error = "Unspecified error";
// and the loader is switched (not even red icon!)
} }
emit contentsProcessed();
} }
AbstractDataLoaderResultModel* AutomaticDataLoader1D::createResultModel() const AbstractDataLoaderResultModel* AutomaticDataLoader1D::createResultModel() const
{ {
return new AutomaticDataLoader1DResultModel(m_item); return new AutomaticDataLoader1DResultModel(m_item);
} }
int AutomaticDataLoader1D::numErrors() const
{
return m_error.isEmpty() ? 0 : 1;
}
...@@ -24,27 +24,17 @@ public: ...@@ -24,27 +24,17 @@ public:
virtual QString name() const override; virtual QString name() const override;
virtual QString persistentClassName() const override; virtual QString persistentClassName() const override;
virtual AbstractDataLoader* clone() const override; virtual AbstractDataLoader* clone() const override;
virtual QByteArray serialize() const override;
virtual void deserialize(const QByteArray& data) override;
virtual void setFileContents(const QByteArray& fileContent) override; virtual void setFileContents(const QByteArray& fileContent) override;
virtual QByteArray fileContent() const override; virtual QByteArray fileContent() const override;
virtual QString processContents() override; virtual void processContents() override;
virtual AbstractDataLoaderResultModel* createResultModel() const override; virtual AbstractDataLoaderResultModel* createResultModel() const override;
virtual int numErrors() const override;
private: private:
//! Result of the file parsing.
struct ParsingResult {
void clear();
void clearCalculatedValues();
QVector<QString> lines;
QVector<QPair<int, QVector<double>>> originalEntriesAsDouble;
QVector<QPair<int, double>> qValues;
QVector<QPair<int, double>> rValues;
int maxColumnCount;
QByteArray hashOfFile;
QMap<int, QString> calculationErrors; // calculation error per line
};
mutable ParsingResult m_importResult;
QByteArray m_fileContent; QByteArray m_fileContent;
QString m_error;
}; };
#endif // BORNAGAIN_GUI_COREGUI_DATALOADERS_AUTOMATICDATALOADER1D_H #endif // BORNAGAIN_GUI_COREGUI_DATALOADERS_AUTOMATICDATALOADER1D_H
...@@ -217,7 +217,7 @@ AbstractDataLoader* QREDataLoader::clone() const ...@@ -217,7 +217,7 @@ AbstractDataLoader* QREDataLoader::clone() const
return loader; return loader;
} }
QString QREDataLoader::processContents() void QREDataLoader::processContents()
{ {
AutoEmit emitter([this] { contentsProcessed(); }); // automatic signal calling in any return AutoEmit emitter([this] { contentsProcessed(); }); // automatic signal calling in any return
...@@ -259,7 +259,7 @@ QString QREDataLoader::processContents() ...@@ -259,7 +259,7 @@ QString QREDataLoader::processContents()
if (!m_importResult.errors.isEmpty()) { if (!m_importResult.errors.isEmpty()) {
invalidateItemData(); invalidateItemData();
return QString(); // empty string => no fatal error (can be cured by changed settings) return;
} }
if (creationOfOutputDataIsNecessary) { if (creationOfOutputDataIsNecessary) {
...@@ -271,8 +271,6 @@ QString QREDataLoader::processContents() ...@@ -271,8 +271,6 @@ QString QREDataLoader::processContents()
invalidateItemData(); invalidateItemData();
} }
} }
return QString(); // empty string => no fatal error (can be cured by changed settings)
} }
int QREDataLoader::numErrors() const int QREDataLoader::numErrors() const
......
...@@ -38,7 +38,7 @@ public: ...@@ -38,7 +38,7 @@ public:
virtual QByteArray fileContent() const override; virtual QByteArray fileContent() const override;
virtual void setFileContents(const QByteArray& fileContent) override; virtual void setFileContents(const QByteArray& fileContent) override;
virtual void guessSettings() override; virtual void guessSettings() override;
virtual QString processContents() override; virtual void processContents() override;
virtual int numErrors() const override; virtual int numErrors() const override;
virtual AbstractDataLoaderResultModel* createResultModel() const override; virtual AbstractDataLoaderResultModel* createResultModel() const override;
......
...@@ -70,7 +70,7 @@ void UserDefinedDataLoader1D::guessSettings() ...@@ -70,7 +70,7 @@ void UserDefinedDataLoader1D::guessSettings()
m_wrappedLoader->guessSettings(); m_wrappedLoader->guessSettings();
} }
QString UserDefinedDataLoader1D::processContents() void UserDefinedDataLoader1D::processContents()
{ {
return m_wrappedLoader->processContents(); m_wrappedLoader->processContents();
} }
...@@ -34,7 +34,7 @@ public: ...@@ -34,7 +34,7 @@ public:
virtual AbstractDataLoader* clone() const override; virtual AbstractDataLoader* clone() const override;
virtual void setFileContents(const QByteArray& fileContent) override; virtual void setFileContents(const QByteArray& fileContent) override;
virtual void guessSettings() override; virtual void guessSettings() override;
virtual QString processContents() override; virtual void processContents() override;
private: private:
QString m_name; QString m_name;
......
...@@ -118,7 +118,8 @@ QString ImportDataUtils::Import1dData(RealDataItem* realDataItem, ...@@ -118,7 +118,8 @@ QString ImportDataUtils::Import1dData(RealDataItem* realDataItem,
} }
} }
// try with CSV loader // -- try with selected CSV loader. If none selected, try with QRE loader
QFile file(fileName); QFile file(fileName);
const bool fileCouldBeOpened = file.open(QFile::ReadOnly | QIODevice::Text); const bool fileCouldBeOpened = file.open(QFile::ReadOnly | QIODevice::Text);
...@@ -146,12 +147,9 @@ QString ImportDataUtils::Import1dData(RealDataItem* realDataItem, ...@@ -146,12 +147,9 @@ QString ImportDataUtils::Import1dData(RealDataItem* realDataItem,
QApplication::setOverrideCursor(Qt::WaitCursor); QApplication::setOverrideCursor(Qt::WaitCursor);
loader->setFileContents(fileContent); loader->setFileContents(fileContent);
loader->guessSettings(); loader->guessSettings();
const QString errorText = loader->processContents(); loader->processContents();
QApplication::restoreOverrideCursor(); QApplication::restoreOverrideCursor();
if (!errorText.isEmpty())
return errorText;
return QString(); return QString();
} }
......
...@@ -217,14 +217,9 @@ void SpecularDataImportWidget::onFormatSelectionChanged() ...@@ -217,14 +217,9 @@ void SpecularDataImportWidget::onFormatSelectionChanged()
m_loader->setRealDataItem(realDataItem()); m_loader->setRealDataItem(realDataItem());
QApplication::setOverrideCursor(Qt::WaitCursor); QApplication::setOverrideCursor(Qt::WaitCursor);
m_loader->guessSettings(); m_loader->guessSettings();
const QString errorText = m_loader->processContents(); m_loader->processContents();
QApplication::restoreOverrideCursor(); QApplication::restoreOverrideCursor();
if (!errorText.isEmpty()) {
// #baimport + use old loader again (make a clone before adding the new to realDataItem)??
// Test with QRE -> automatic show errorText
}
updatePropertiesEdits(); updatePropertiesEdits();
updatePreview(); updatePreview();
connect(m_loader, &AbstractDataLoader::importSettingsChanged, this, connect(m_loader, &AbstractDataLoader::importSettingsChanged, this,
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment