From 0e55433cb82254e8a5d60c07c44411c1a65872c0 Mon Sep 17 00:00:00 2001 From: Mikhail Svechnikov <m.svechnikov@fz-juelich.de> Date: Fri, 21 Apr 2023 18:31:38 +0200 Subject: [PATCH] cleanup --- GUI/Model/Data/DataItem.cpp | 10 +++-- GUI/Model/Data/DataItemUtil.cpp | 28 ++++---------- GUI/Model/Data/DataItemUtil.h | 7 ++-- GUI/Model/Data/IntensityDataItem.cpp | 2 - GUI/Model/Data/SpecularDataItem.cpp | 2 - GUI/Model/Device/RealItem.cpp | 37 ++++++------------- GUI/Model/Device/RealItem.h | 7 +--- GUI/Model/Job/JobItem.cpp | 26 +++++-------- GUI/Model/Model/RealModel.cpp | 1 - GUI/View/Common/DataPropertyWidget.cpp | 9 +++-- .../Common/IntensityDataPropertyWidget.cpp | 22 +++++------ GUI/View/Loaders/QREDataLoader.cpp | 5 +-- GUI/View/Numeric/NumWidgetUtil.cpp | 8 ++-- GUI/View/Numeric/NumWidgetUtil.h | 5 ++- .../SpecularDataPropertyWidget.cpp | 22 +++++------ 15 files changed, 75 insertions(+), 116 deletions(-) diff --git a/GUI/Model/Data/DataItem.cpp b/GUI/Model/Data/DataItem.cpp index f5275c44012..ad8525c11c7 100644 --- a/GUI/Model/Data/DataItem.cpp +++ b/GUI/Model/Data/DataItem.cpp @@ -302,7 +302,7 @@ void DataItem::setAxesRangeToData() void DataItem::writeTo(QXmlStreamWriter* w) const { - XML::writeAttribute(w, XML::Attrib::version, uint(1)); + XML::writeAttribute(w, XML::Attrib::version, uint(2)); // file name w->writeStartElement(Tag::FileName); @@ -328,7 +328,6 @@ void DataItem::writeTo(QXmlStreamWriter* w) const void DataItem::readFrom(QXmlStreamReader* r) { const uint version = XML::readUIntAttribute(r, XML::Attrib::version); - Q_UNUSED(version) while (r->readNextStartElement()) { QString tag = r->name().toString(); @@ -341,7 +340,12 @@ void DataItem::readFrom(QXmlStreamReader* r) // axes units } else if (tag == Tag::AxesUnits) { QString axes_units; - XML::readAttribute(r, XML::Attrib::value, &axes_units); + if (version == 1) { + XML::readAttribute(r, XML::Attrib::name, &axes_units); + if (axes_units.isEmpty()) + axes_units = GUI::Util::CoordName::nameFromCoord(Coords::NBINS); + } else + XML::readAttribute(r, XML::Attrib::value, &axes_units); m_currentCoord = GUI::Util::CoordName::coordFromName(axes_units); XML::gotoEndElementOfTag(r, tag); diff --git a/GUI/Model/Data/DataItemUtil.cpp b/GUI/Model/Data/DataItemUtil.cpp index 956caa93327..ba7ba19e479 100644 --- a/GUI/Model/Data/DataItemUtil.cpp +++ b/GUI/Model/Data/DataItemUtil.cpp @@ -20,8 +20,8 @@ #include "GUI/Model/Data/DataItem.h" #include "GUI/Support/Util/CoordName.h" -//! Updates axes' titles -void GUI::Model::DataItemUtil::updateAxesTitle(DataItem* intensityItem, const ICoordSystem& converter, Coords units) +void GUI::Model::DataItemUtil::updateAxesTitle(DataItem* intensityItem, + const ICoordSystem& converter, Coords units) { intensityItem->setXaxisTitle(QString::fromStdString(converter.nameOfAxis(0, units))); if (converter.rank() > 1) @@ -59,34 +59,22 @@ void GUI::Model::DataItemUtil::updateDataAxes(DataItem* dataItem, const ICoordSy updateAxesTitle(dataItem, converter, axes_units); } -//void GUI::Model::DataItemUtil::setDataItemCoords(DataItem* dataItem, const ICoordSystem& converter) -//{ -// ComboProperty combo = availableUnits(converter); -// dataItem->setAxesUnitsCombo(combo); -//} - void GUI::Model::DataItemUtil::createDefaultDetectorMap(DataItem* dataItem, const ICoordSystem& converter) { - // do not substitute units -// auto output_data = std::make_unique<Datafield>(converter.defaultAxes()); - auto output_data = std::make_unique<Datafield>(converter.convertedAxes(dataItem->currentCoord())); + auto current_coord = dataItem->currentCoord(); + auto output_data = std::make_unique<Datafield>(converter.convertedAxes(current_coord)); dataItem->setDatafield(output_data.release()); - // do not substitute units -// setDataItemCoords(dataItem, converter); - updateAxesTitle(dataItem, converter, dataItem->currentCoord()); -// updateAxesTitle(dataItem, converter, converter.defaultUnits()); + updateAxesTitle(dataItem, converter, current_coord); } void GUI::Model::DataItemUtil::setResults(DataItem* dataItem, const SimulationResult& result) { + auto current_coord = dataItem->currentCoord(); if (dataItem->c_field() == nullptr) { const auto& converter = result.converter(); - // do not substitute units -// GUI::Model::DataItemUtil::setDataItemCoords(dataItem, converter); - updateAxesTitle(dataItem, converter, dataItem->currentCoord() /*converter.defaultUnits()*/); + updateAxesTitle(dataItem, converter, current_coord); } - auto selected_units = GUI::Util::CoordName::coordFromName(dataItem->currentAxesUnits()); dataItem->setDatafield( - new Datafield(result.converter().convertedAxes(selected_units), result.flatVector())); + new Datafield(result.converter().convertedAxes(current_coord), result.flatVector())); } diff --git a/GUI/Model/Data/DataItemUtil.h b/GUI/Model/Data/DataItemUtil.h index 1b75f40fd11..609744f1c0a 100644 --- a/GUI/Model/Data/DataItemUtil.h +++ b/GUI/Model/Data/DataItemUtil.h @@ -27,15 +27,16 @@ class SimulationResult; namespace GUI::Model::DataItemUtil { +//! Updates axes' titles void updateAxesTitle(DataItem* intensityItem, const ICoordSystem& converter, Coords units); -//! updates axes of Datafield in IntensityData item +//! Updates axes of Datafield in IntensityData item void updateDataAxes(DataItem* dataItem, const ICoordSystem& converter); +//! Available units for coordinate system QStringList availableUnits(const ICoordSystem& converter); -//void setDataItemCoords(DataItem* dataItem, const ICoordSystem& converter); - +//! Creates zero-value intensity map with given coordinate system void createDefaultDetectorMap(DataItem* dataItem, const ICoordSystem& converter); //! Sets simulation results into the DataItem diff --git a/GUI/Model/Data/IntensityDataItem.cpp b/GUI/Model/Data/IntensityDataItem.cpp index fb1d07aba95..3387827c12a 100644 --- a/GUI/Model/Data/IntensityDataItem.cpp +++ b/GUI/Model/Data/IntensityDataItem.cpp @@ -224,8 +224,6 @@ std::vector<int> IntensityDataItem::shape() const void IntensityDataItem::reset(ImportDataInfo data) { ASSERT(data.unitsLabel() == GUI::Util::CoordName::nameFromCoord(Coords::NBINS)); -// ComboProperty combo = ComboProperty() << data.unitsLabel(); -// setAxesUnitsCombo(combo); setCurrentAxesUnits(data.unitsLabel()); setXaxisTitle(data.axisLabel(0)); diff --git a/GUI/Model/Data/SpecularDataItem.cpp b/GUI/Model/Data/SpecularDataItem.cpp index 83f812ac3ab..822ae7beb6c 100644 --- a/GUI/Model/Data/SpecularDataItem.cpp +++ b/GUI/Model/Data/SpecularDataItem.cpp @@ -122,8 +122,6 @@ std::vector<int> SpecularDataItem::shape() const void SpecularDataItem::reset(ImportDataInfo data) { -// ComboProperty combo = ComboProperty() << data.unitsLabel(); -// setAxesUnitsCombo(combo); setCurrentAxesUnits(data.unitsLabel()); setXaxisTitle(data.axisLabel(0)); diff --git a/GUI/Model/Device/RealItem.cpp b/GUI/Model/Device/RealItem.cpp index 44fc35afd72..9d84ccf4a87 100644 --- a/GUI/Model/Device/RealItem.cpp +++ b/GUI/Model/Device/RealItem.cpp @@ -21,7 +21,6 @@ #include "GUI/Model/Data/ProjectionItems.h" #include "GUI/Model/Data/SpecularDataItem.h" #include "GUI/Model/Device/InstrumentItems.h" -#include "GUI/Model/Device/InstrumentModel.h" #include "GUI/Support/IO/AbstractDataLoader1D.h" #include "GUI/Support/IO/DataLoaders1D.h" #include "GUI/Support/IO/ImportDataInfo.h" @@ -220,6 +219,11 @@ QString RealItem::instrumentId() const return m_instrumentId; } +void RealItem::setInstrumentId(const QString& id) +{ + m_instrumentId = id; +} + void RealItem::linkToInstrument(const InstrumentItem* instrument) { if (instrument) { @@ -235,11 +239,6 @@ void RealItem::unlinkFromInstrument() updateToInstrument(nullptr); } -void RealItem::setInstrumentModel(InstrumentModel* instrumentModel) -{ - m_instrumentModel = instrumentModel; -} - bool RealItem::hasImportErrors() const { return (dataLoader() != nullptr) ? dataLoader()->numErrors() > 0 : false; @@ -413,7 +412,7 @@ void RealItem::writeTo(QXmlStreamWriter* w) const } } -void RealItem::readFrom(QXmlStreamReader* r, InstrumentItem* jobInstrument) +void RealItem::readFrom(QXmlStreamReader* r) { const uint version = XML::readUIntAttribute(r, XML::Attrib::version); Q_UNUSED(version) @@ -452,25 +451,11 @@ void RealItem::readFrom(QXmlStreamReader* r, InstrumentItem* jobInstrument) QString type; XML::readAttribute(r, XML::Attrib::type, &type); initFromType(type); - - // create axes units list from job's instrument or linked instrument -// if (jobInstrument) { -// const auto converter = jobInstrument->createCoordSystem(); -// GUI::Model::DataItemUtil::setDataItemCoords(m_dataItem.get(), *converter); -// } else if (!m_instrumentId.isEmpty()) { // read 'm_instrumentId' before -// ASSERT(m_instrumentModel); -// auto* instrument = m_instrumentModel->findInstrumentItemById(m_instrumentId); -// ASSERT(instrument); -// const auto converter = instrument->createCoordSystem(); -// GUI::Model::DataItemUtil::setDataItemCoords(m_dataItem.get(), *converter); -// } - m_dataItem->readFrom(r); XML::gotoEndElementOfTag(r, tag); // native data } else if (tag == Tag::NativeData) { - // TODO: do we need to create coordinates here? ASSERT(m_dataItem); // read 'm_dataItem' before initNativeData()->readFrom(r); XML::gotoEndElementOfTag(r, tag); @@ -517,7 +502,6 @@ QString RealItem::readDataFiles(const QString& projectDir, MessageService* messa void RealItem::copyTo(RealItem* const realdata_dst) const { - realdata_dst->setInstrumentModel(m_instrumentModel); GUI::Util::copyContents(this, realdata_dst); if (m_dataItem) @@ -577,10 +561,11 @@ void RealItem::updateToInstrument(const InstrumentItem* instrument) return; if (instrument) { -// const auto converter = instrument->createCoordSystem(); -// GUI::Model::DataItemUtil::setDataItemCoords(data_item, *converter); -// dataItem()->setCurrentCoord(Coords::NBINS); -// dataItem()->updateCoords(*converter); + // To keep the same units after linking, just comment lines. + // To switch to certain units on linking, uncomment lines. + // dataItem()->setCurrentCoord(Coords::DEGREES); + // const auto converter = instrument->createCoordSystem(); + // dataItem()->updateCoords(*converter); return; } diff --git a/GUI/Model/Device/RealItem.h b/GUI/Model/Device/RealItem.h index 05d3140a81a..86e9c703f76 100644 --- a/GUI/Model/Device/RealItem.h +++ b/GUI/Model/Device/RealItem.h @@ -22,7 +22,6 @@ class Datafield; class DataItem; class ImportDataInfo; class InstrumentItem; -class InstrumentModel; class IntensityDataItem; class MaskContainerItem; class MessageService; @@ -87,11 +86,10 @@ public: // instrument QString instrumentId() const; + void setInstrumentId(const QString& id); void linkToInstrument(const InstrumentItem* instrument); void unlinkFromInstrument(); - void setInstrumentModel(InstrumentModel* instrumentModel); - // loader bool hasImportErrors() const; @@ -112,7 +110,7 @@ public: void deserializeBinaryData(const QByteArray& data); void writeTo(QXmlStreamWriter* w) const; - void readFrom(QXmlStreamReader* r, InstrumentItem* jobInstrument = nullptr); + void readFrom(QXmlStreamReader* r); void writeDataFiles(const QString& projectDir) const; QString readDataFiles(const QString& projectDir, MessageService* messageService); @@ -144,7 +142,6 @@ private: QString m_name; QString m_nativeDataUnits = "nbins"; QString m_presentationType; - InstrumentModel* m_instrumentModel = nullptr; // DataItem can be `IntensityDataItem` and `SpecularDataItem` (default `IntensityDataItem`). std::unique_ptr<DataItem> m_dataItem; diff --git a/GUI/Model/Job/JobItem.cpp b/GUI/Model/Job/JobItem.cpp index 8ff8e5feaf6..17769a51ea1 100644 --- a/GUI/Model/Job/JobItem.cpp +++ b/GUI/Model/Job/JobItem.cpp @@ -370,18 +370,15 @@ RealItem* JobItem::realItem() void JobItem::adjustReaDataToJobInstrument() { - // Link RealItem to the JobItem's instrument. - // (re-)Linking is necessary because of following reason - // 1) Copying of RealItem from RealModel on board of JobItem requires relink to the copied - // instrument - // 2) During relink all masks (if exists) will be converted to the default units of current - // detector - realItem()->linkToInstrument(instrumentItem()); + // update stored instrument id without updating rest of real data + realItem()->setInstrumentId(instrumentItem()->id()); if (instrumentItem()->is<GISASInstrumentItem>()) { - // Temporary real units conversion to degrees before copying masks to instrument. + // Temporary conversion of real units to degrees before copying masks to instrument. // It is not clear why we need to do this, but otherwise the result is incorrect. - // Degrees are hardcoded somewhere in detector, so we should use the same units here. + // Seems that degrees are default units in detector, so we should use the same units here, + // but if we change 'converter->defaultUnits()' to, for example, radians, here we still + // should use 'Coords::DEGREES'. So the reason lies deeper. Coords backup_coord = m_realItem->dataItem()->currentCoord(); m_realItem->dataItem()->setCurrentCoord(Coords::DEGREES); const auto converter = instrumentItem()->createCoordSystem(); @@ -389,11 +386,11 @@ void JobItem::adjustReaDataToJobInstrument() importMasksFromRealItem(); // Copy masks and ROI from RealItem on board of instrument. - // convert real units back + // convert units back m_realItem->dataItem()->setCurrentCoord(backup_coord); realItem()->dataItem()->updateCoords(*converter); - cropRealData(); // Crop RealItem to the region of interest. + cropRealData(); // Crop RealItem to the region of interest. } } @@ -615,9 +612,8 @@ void JobItem::readFrom(QXmlStreamReader* r) // real item } else if (tag == Tag::RealItem) { - ASSERT(instrumentItem()); // read 'm_instrument' before createRealItem(); - m_realItem->readFrom(r, instrumentItem()); + m_realItem->readFrom(r); createDiffDataItem()->copyXYRangesFromItem(m_realItem->dataItem()); if (isIntensityJob()) @@ -629,11 +625,7 @@ void JobItem::readFrom(QXmlStreamReader* r) // simulated data } else if (tag == Tag::SimulatedData) { - ASSERT(instrumentItem()); // read 'm_instrument' before createSimulatedDataItem(); - // create axes units list in data item before reading selected units -// const auto converter = instrumentItem()->createCoordSystem(); -// GUI::Model::DataItemUtil::setDataItemCoords(simulatedDataItem(), *converter); m_simulatedDataItem->readFrom(r); XML::gotoEndElementOfTag(r, tag); diff --git a/GUI/Model/Model/RealModel.cpp b/GUI/Model/Model/RealModel.cpp index 39759a3f74f..6d6df155f7c 100644 --- a/GUI/Model/Model/RealModel.cpp +++ b/GUI/Model/Model/RealModel.cpp @@ -130,7 +130,6 @@ void RealModel::readDataFiles(const QString& projectDir, MessageService* message RealItem* RealModel::createRealItem() { auto realItem = std::make_unique<RealItem>(); - realItem->setInstrumentModel(m_instrumentModel); m_realItems.push_back(std::move(realItem)); return m_realItems.back().get(); } diff --git a/GUI/View/Common/DataPropertyWidget.cpp b/GUI/View/Common/DataPropertyWidget.cpp index 9a059a5e92a..0c1337871f9 100644 --- a/GUI/View/Common/DataPropertyWidget.cpp +++ b/GUI/View/Common/DataPropertyWidget.cpp @@ -61,13 +61,14 @@ void DataPropertyWidget::updateUIValues() updater(); } -InstrumentItem *DataPropertyWidget::instrumentItem() +InstrumentItem* DataPropertyWidget::instrumentItem() { if (jobItem()) return jobItem()->instrumentItem(); else if (realItem()) { ASSERT(gProjectDocument.has_value()); - return gProjectDocument.value()->instrumentModel()->findInstrumentItemById(realItem()->instrumentId()); + return gProjectDocument.value()->instrumentModel()->findInstrumentItemById( + realItem()->instrumentId()); } else ASSERT(false); return nullptr; @@ -76,7 +77,7 @@ InstrumentItem *DataPropertyWidget::instrumentItem() QStringList DataPropertyWidget::axesUnitsList() { QStringList list; - if(!instrumentItem()) { + if (!instrumentItem()) { ASSERT(realItem()); list << realItem()->dataItem()->currentAxesUnits(); } else { @@ -84,5 +85,5 @@ QStringList DataPropertyWidget::axesUnitsList() ASSERT(converter); list = GUI::Model::DataItemUtil::availableUnits(*converter); } - return list; + return list; } diff --git a/GUI/View/Common/IntensityDataPropertyWidget.cpp b/GUI/View/Common/IntensityDataPropertyWidget.cpp index 24cf7838c3b..2eeba28ba48 100644 --- a/GUI/View/Common/IntensityDataPropertyWidget.cpp +++ b/GUI/View/Common/IntensityDataPropertyWidget.cpp @@ -50,17 +50,17 @@ void IntensityDataPropertyWidget::createPanelElements() GUI::Util::Layout::clearLayout(m_mainLayout); m_updaters.clear(); - m_mainLayout->addRow( - "Axes units:", - GUI::Util::createUnitsComboBox( axesUnitsList(), - [=] { return currentIntensityDataItem()->currentAxesUnits(); }, - [=](const QString& newVal) { - for (auto item : allIntensityDataItems()) - item->setCurrentAxesUnits(newVal); - emit axesRangeResetRequested(); - gProjectDocument.value()->setModified(); - }, - &m_updaters)); + m_mainLayout->addRow("Axes units:", + GUI::Util::createUnitsComboBox( + axesUnitsList(), + [=] { return currentIntensityDataItem()->currentAxesUnits(); }, + [=](const QString& newVal) { + for (auto item : allIntensityDataItems()) + item->setCurrentAxesUnits(newVal); + emit axesRangeResetRequested(); + gProjectDocument.value()->setModified(); + }, + &m_updaters)); m_mainLayout->addRow( "Color scheme:", diff --git a/GUI/View/Loaders/QREDataLoader.cpp b/GUI/View/Loaders/QREDataLoader.cpp index 0a85e0caa0f..a6c803ac21d 100644 --- a/GUI/View/Loaders/QREDataLoader.cpp +++ b/GUI/View/Loaders/QREDataLoader.cpp @@ -570,11 +570,8 @@ void QREDataLoader::datafieldFromParsingResult(RealItem* item) const // -- Replacement of specularItem->reset(std::move(data)); SpecularDataItem* specularItem = item->specularDataItem(); specularItem->setCurrentCoord(coord); -// ComboProperty combo = ComboProperty() << units_name; -// specularItem->setAxesUnitsCombo(combo); - const auto xAxisTitle = - QString::fromStdString(AngularReflectometryCoords::nameOfAxis0(coord)); + const auto xAxisTitle = QString::fromStdString(AngularReflectometryCoords::nameOfAxis0(coord)); const auto* const yAxisTitle = "Signal [a.u.]"; // taken from ImportDataInfo::axisLabel specularItem->setXaxisTitle(xAxisTitle); diff --git a/GUI/View/Numeric/NumWidgetUtil.cpp b/GUI/View/Numeric/NumWidgetUtil.cpp index cf041b4d94b..6e6e5f846ba 100644 --- a/GUI/View/Numeric/NumWidgetUtil.cpp +++ b/GUI/View/Numeric/NumWidgetUtil.cpp @@ -62,11 +62,9 @@ QComboBox* GUI::Util::createSafeComboBox(function<ComboProperty()> comboFunction return createComboBox(comboFunction, slot, updaters, tooltip, false); } -QComboBox *GUI::Util::createUnitsComboBox(const QStringList& list, - function<QString()> currentUnits, - function<void (const QString &)> slot, - QList<std::function<void ()> > *updaters, - bool isScrollable) +QComboBox* GUI::Util::createUnitsComboBox(const QStringList& list, function<QString()> currentUnits, + function<void(const QString&)> slot, + QList<std::function<void()>>* updaters, bool isScrollable) { QComboBox* combo = new QComboBox; combo->addItems(list); diff --git a/GUI/View/Numeric/NumWidgetUtil.h b/GUI/View/Numeric/NumWidgetUtil.h index a28dc7c438c..dcf32758810 100644 --- a/GUI/View/Numeric/NumWidgetUtil.h +++ b/GUI/View/Numeric/NumWidgetUtil.h @@ -51,9 +51,10 @@ QComboBox* createSafeComboBox(std::function<ComboProperty()> comboFunction, QList<std::function<void()>>* updaters = nullptr, QString tooltip = ""); -QComboBox* createUnitsComboBox(const QStringList &list, std::function<QString ()> currentUnits, +QComboBox* createUnitsComboBox(const QStringList& list, std::function<QString()> currentUnits, std::function<void(const QString&)> slot, - QList<std::function<void()>>* updaters = nullptr, bool isScrollable = true); + QList<std::function<void()>>* updaters = nullptr, + bool isScrollable = true); //! Create a combo box with the information found in a selection property. //! diff --git a/GUI/View/PlotSpecular/SpecularDataPropertyWidget.cpp b/GUI/View/PlotSpecular/SpecularDataPropertyWidget.cpp index 760544b9f4a..9f99f20ecbf 100644 --- a/GUI/View/PlotSpecular/SpecularDataPropertyWidget.cpp +++ b/GUI/View/PlotSpecular/SpecularDataPropertyWidget.cpp @@ -42,17 +42,17 @@ void SpecularDataPropertyWidget::createPanelElements() GUI::Util::Layout::clearLayout(m_mainLayout); m_updaters.clear(); - m_mainLayout->addRow( - "Axes units:", - GUI::Util::createUnitsComboBox( axesUnitsList(), - [=] { return currentSpecularDataItem()->currentAxesUnits(); }, - [=](const QString& newVal) { - for (auto item : allSpecularDataItems()) - item->setCurrentAxesUnits(newVal); - emit axesRangeResetRequested(); - gProjectDocument.value()->setModified(); - }, - &m_updaters)); + m_mainLayout->addRow("Axes units:", + GUI::Util::createUnitsComboBox( + axesUnitsList(), + [=] { return currentSpecularDataItem()->currentAxesUnits(); }, + [=](const QString& newVal) { + for (auto item : allSpecularDataItems()) + item->setCurrentAxesUnits(newVal); + emit axesRangeResetRequested(); + gProjectDocument.value()->setModified(); + }, + &m_updaters)); // -- x-axis auto* xGroup = new QGroupBox("X axis", this); -- GitLab