Skip to content
Snippets Groups Projects
Commit 16a24457 authored by Wuttke, Joachim's avatar Wuttke, Joachim
Browse files

still cannot read, but printing proper error message

parent a48a4453
No related branches found
No related tags found
1 merge request!2331store instruments in file system, not in a "library" of our own making (#896)
...@@ -33,7 +33,7 @@ void custom_terminate_handler() ...@@ -33,7 +33,7 @@ void custom_terminate_handler()
try { try {
std::rethrow_exception(std::current_exception()); std::rethrow_exception(std::current_exception());
} catch (const std::exception& ex) { } catch (const std::exception& ex) {
std::cerr << "terminate called after throwing an instance of 'std::runtime_error'\n" std::cerr << "terminate called after throwing a std exception'\n"
<< "what():" << std::endl; << "what():" << std::endl;
std::cerr << ex.what() << std::endl; std::cerr << ex.what() << std::endl;
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
// ************************************************************************************************ // ************************************************************************************************
#include "GUI/View/Instrument/InstrumentsQListView.h" #include "GUI/View/Instrument/InstrumentsQListView.h"
#include "Base/Util/Assert.h"
#include "GUI/Model/Device/InstrumentLibrary.h" #include "GUI/Model/Device/InstrumentLibrary.h"
#include "GUI/Model/Project/ProjectDocument.h" #include "GUI/Model/Project/ProjectDocument.h"
#include "GUI/View/Instrument/InstrumentLibraryDialog.h" #include "GUI/View/Instrument/InstrumentLibraryDialog.h"
...@@ -20,20 +21,20 @@ ...@@ -20,20 +21,20 @@
#include "GUI/View/Setup/FrameActions.h" #include "GUI/View/Setup/FrameActions.h"
#include "GUI/View/Tool/Globals.h" #include "GUI/View/Tool/Globals.h"
#include <QFile> #include <QFile>
#include <QMessageBox>
#include <QVBoxLayout> #include <QVBoxLayout>
#include "GUI/Model/Descriptor/SelectionProperty.h" #include "GUI/Model/Descriptor/SelectionProperty.h"
#include "GUI/Model/Device/InstrumentItems.h"
#include "GUI/Model/Device/InstrumentItemCatalog.h" #include "GUI/Model/Device/InstrumentItemCatalog.h"
#include "GUI/Model/Device/InstrumentItems.h"
#include "GUI/Support/XML/UtilXML.h" #include "GUI/Support/XML/UtilXML.h"
#include <QMessageBox>
namespace { namespace {
const QString XML_ROOT_TAG = "BornAgain::Instrument"; const QString XML_ROOT_TAG = "BornAgain::Instrument";
const QString XML_VERSION_TAG = "Version"; const QString XML_VERSION_TAG = "Version";
} } // namespace
InstrumentsQListView::InstrumentsQListView() InstrumentsQListView::InstrumentsQListView()
: m_instrument_library(std::make_unique<InstrumentLibrary>()) : m_instrument_library(std::make_unique<InstrumentLibrary>())
...@@ -162,7 +163,7 @@ void InstrumentsQListView::onCopy() ...@@ -162,7 +163,7 @@ void InstrumentsQListView::onCopy()
void InstrumentsQListView::onStoreInLibrary() void InstrumentsQListView::onStoreInLibrary()
{ {
//InstrumentLibraryDialog dlg(GUI::Global::mainWindow, m_instrument_library.get()); // InstrumentLibraryDialog dlg(GUI::Global::mainWindow, m_instrument_library.get());
QFile file("/tmp/instrument.xml"); QFile file("/tmp/instrument.xml");
if (!file.open(QFile::ReadWrite | QIODevice::Truncate | QFile::Text)) if (!file.open(QFile::ReadWrite | QIODevice::Truncate | QFile::Text))
...@@ -191,35 +192,51 @@ void InstrumentsQListView::onStoreInLibrary() ...@@ -191,35 +192,51 @@ void InstrumentsQListView::onStoreInLibrary()
void InstrumentsQListView::onLoadFromLibrary() void InstrumentsQListView::onLoadFromLibrary()
{ {
InstrumentItem* ii = nullptr;
QFile file("/tmp/instrument.xml"); QFile file("/tmp/instrument.xml");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
throw std::runtime_error("Cannot open instrument file for reading"); try {
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
/* throw std::runtime_error("Cannot open instrument file for reading");
InstrumentItem ii; QXmlStreamReader r(&file);
while (!r.atEnd()) {
QXmlStreamReader r(&file); r.readNext();
if (r.isStartElement()) {
while (!r.atEnd()) { if (r.name() == XML_ROOT_TAG) {
r.readNext(); const int foundVersion = r.attributes().value(XML_VERSION_TAG).toString().toInt();
if (r.isStartElement()) { if (foundVersion != 1)
if (r.name() == XML_ROOT_TAG) { throw std::runtime_error("Unsupported version of instrument file");
const int foundVersion =
r.attributes().value(XML_VERSION_TAG).toString().toInt(); const uint selection_version =
if (foundVersion != 1) XML::readUIntAttribute(&r, XML::Attrib::selection_version);
throw std::runtime_error("Unsupported version of instrument file"); if (selection_version != 2)
throw std::runtime_error("Unsupported version of instrument stance");
ii.readFrom(&r);
} const uint typeIndex = XML::readUIntAttribute(&r, XML::Attrib::type);
} const auto type = static_cast<typename InstrumentItemCatalog::Type>(typeIndex);
ii = InstrumentItemCatalog::create(type);
ASSERT(ii);
ii->readFrom(&r);
}
}
}
if (r.hasError())
throw std::runtime_error(QString("Error in instrument file, line %1, column %2: %3")
.arg(r.lineNumber())
.arg(r.columnNumber())
.arg(r.errorString())
.toStdString());
file.close();
} catch (const std::exception& ex) {
QMessageBox msgbox(QMessageBox::Warning, "BornAgain: failed import", QString(ex.what()),
QMessageBox::Ok, nullptr);
msgbox.exec();
return;
} }
if (r.hasError())
throw std::runtime_error("Error while reading instrument file");
QModelIndex idx = m_model->copyInstrument(&ii);
selectionModel()->select(idx, QItemSelectionModel::ClearAndSelect);
*/
file.close();
QModelIndex idx = m_model->copyInstrument(ii);
selectionModel()->select(idx, QItemSelectionModel::ClearAndSelect);
} }
void InstrumentsQListView::updateActions() void InstrumentsQListView::updateActions()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment