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:
//! Process the file contents. Can be called more than once, e.g. if the import settings have
//! changed.
//! Return an error string in case of a fatal error (item should not be kept). Any non fatal
//! error which might be solvable by changes in import settings or by selecting a different
//! 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;
//! Any error has to be stored in the loader (see numErrors()).
virtual void processContents() = 0;
//! 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
......
......@@ -18,6 +18,7 @@
#include "GUI/coregui/DataLoaders/AutomaticDataLoader1DResultModel.h"
#include "GUI/coregui/Models/RealDataItem.h"
#include "GUI/coregui/Models/SpecularDataItem.h"
#include "GUI/coregui/utils/DeserializationException.h"
#include "GUI/coregui/utils/ImportDataInfo.h"
#include "qcustomplot.h"
......@@ -38,6 +39,40 @@ AbstractDataLoader* AutomaticDataLoader1D::clone() const
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)
{
m_fileContent = fileContent;
......@@ -48,7 +83,7 @@ QByteArray AutomaticDataLoader1D::fileContent() const
return m_fileContent;
}
QString AutomaticDataLoader1D::processContents()
void AutomaticDataLoader1D::processContents()
{
ASSERT(m_item != nullptr);
ASSERT(m_item->isSpecularData());
......@@ -59,20 +94,24 @@ QString AutomaticDataLoader1D::processContents()
ImportDataInfo importInfo(std::move(*oData), Axes::Units::QSPACE);
m_item->setImportData(std::move(importInfo));
emit contentsProcessed();
return QString();
m_error.clear();
} catch (std::runtime_error& ex) {
m_item->removeNativeData();
m_item->specularDataItem()->setOutputData(nullptr);
emit contentsProcessed();
return QString::fromStdString(
ex.what()); // #baimport ++ this error gets lost if file is added
// and the loader is switched (not even red icon!)
m_error = QString::fromStdString(ex.what());
if (m_error.isEmpty())
m_error = "Unspecified error";
}
emit contentsProcessed();
}
AbstractDataLoaderResultModel* AutomaticDataLoader1D::createResultModel() const
{
return new AutomaticDataLoader1DResultModel(m_item);
}
int AutomaticDataLoader1D::numErrors() const
{
return m_error.isEmpty() ? 0 : 1;
}
......@@ -24,27 +24,17 @@ public:
virtual QString name() const override;
virtual QString persistentClassName() 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 QByteArray fileContent() const override;
virtual QString processContents() override;
virtual void processContents() override;
virtual AbstractDataLoaderResultModel* createResultModel() const override;
virtual int numErrors() const override;
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;
QString m_error;
};
#endif // BORNAGAIN_GUI_COREGUI_DATALOADERS_AUTOMATICDATALOADER1D_H
......@@ -217,7 +217,7 @@ AbstractDataLoader* QREDataLoader::clone() const
return loader;
}
QString QREDataLoader::processContents()
void QREDataLoader::processContents()
{
AutoEmit emitter([this] { contentsProcessed(); }); // automatic signal calling in any return
......@@ -259,7 +259,7 @@ QString QREDataLoader::processContents()
if (!m_importResult.errors.isEmpty()) {
invalidateItemData();
return QString(); // empty string => no fatal error (can be cured by changed settings)
return;
}
if (creationOfOutputDataIsNecessary) {
......@@ -271,8 +271,6 @@ QString QREDataLoader::processContents()
invalidateItemData();
}
}
return QString(); // empty string => no fatal error (can be cured by changed settings)
}
int QREDataLoader::numErrors() const
......
......@@ -38,7 +38,7 @@ public:
virtual QByteArray fileContent() const override;
virtual void setFileContents(const QByteArray& fileContent) override;
virtual void guessSettings() override;
virtual QString processContents() override;
virtual void processContents() override;
virtual int numErrors() const override;
virtual AbstractDataLoaderResultModel* createResultModel() const override;
......
......@@ -70,7 +70,7 @@ void UserDefinedDataLoader1D::guessSettings()
m_wrappedLoader->guessSettings();
}
QString UserDefinedDataLoader1D::processContents()
void UserDefinedDataLoader1D::processContents()
{
return m_wrappedLoader->processContents();
m_wrappedLoader->processContents();
}
......@@ -34,7 +34,7 @@ public:
virtual AbstractDataLoader* clone() const override;
virtual void setFileContents(const QByteArray& fileContent) override;
virtual void guessSettings() override;
virtual QString processContents() override;
virtual void processContents() override;
private:
QString m_name;
......
......@@ -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);
const bool fileCouldBeOpened = file.open(QFile::ReadOnly | QIODevice::Text);
......@@ -146,12 +147,9 @@ QString ImportDataUtils::Import1dData(RealDataItem* realDataItem,
QApplication::setOverrideCursor(Qt::WaitCursor);
loader->setFileContents(fileContent);
loader->guessSettings();
const QString errorText = loader->processContents();
loader->processContents();
QApplication::restoreOverrideCursor();
if (!errorText.isEmpty())
return errorText;
return QString();
}
......
......@@ -217,14 +217,9 @@ void SpecularDataImportWidget::onFormatSelectionChanged()
m_loader->setRealDataItem(realDataItem());
QApplication::setOverrideCursor(Qt::WaitCursor);
m_loader->guessSettings();
const QString errorText = m_loader->processContents();
m_loader->processContents();
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();
updatePreview();
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