Skip to content
Snippets Groups Projects
Commit 440214d3 authored by Matthias Puchner's avatar Matthias Puchner
Browse files

refactor & reformat LinkManager

parent 5a06fc03
No related branches found
No related tags found
1 merge request!302Simplify instrument linking
Pipeline #44216 failed
...@@ -22,17 +22,33 @@ ...@@ -22,17 +22,33 @@
#include <QMessageBox> #include <QMessageBox>
#include <QPushButton> #include <QPushButton>
using namespace GUI::View;
namespace { namespace {
bool QuestionOnInstrumentReshaping(const QString& message); bool QuestionOnInstrumentReshaping(const QString& message)
{
QMessageBox msgBox;
msgBox.setText("Instrument description conflicts with the experimental data.");
QString informative;
informative.append(message);
informative.append("\n\nDo you want to adjust the instrument to the experimental data?\n\n");
msgBox.setInformativeText(informative);
QPushButton* modifyInstrumentButton =
msgBox.addButton("Yes, please modify instrument", QMessageBox::YesRole);
msgBox.addButton("No, leave as it is", QMessageBox::NoRole);
msgBox.exec();
return msgBox.clickedButton() == modifyInstrumentButton;
}
} // namespace } // namespace
LinkInstrumentManager::LinkInstrumentManager(InstrumentModel* instrumentModel, LinkInstrumentManager::LinkInstrumentManager(InstrumentModel* instrumentModel,
RealDataModel* realDataModel) RealDataModel* realDataModel)
: m_instrumentModel(instrumentModel), m_realDataModel(realDataModel) : m_instrumentModel(instrumentModel), m_realDataModel(realDataModel)
{ {
ASSERT(m_instrumentModel != nullptr);
ASSERT(m_realDataModel != nullptr);
connect(m_instrumentModel, &InstrumentModel::instrumentAddedOrRemoved, this, connect(m_instrumentModel, &InstrumentModel::instrumentAddedOrRemoved, this,
&LinkInstrumentManager::onInstrumentAddedOrRemoved); &LinkInstrumentManager::onInstrumentAddedOrRemoved);
...@@ -40,9 +56,6 @@ LinkInstrumentManager::LinkInstrumentManager(InstrumentModel* instrumentModel, ...@@ -40,9 +56,6 @@ LinkInstrumentManager::LinkInstrumentManager(InstrumentModel* instrumentModel,
&LinkInstrumentManager::onInstrumentChanged); &LinkInstrumentManager::onInstrumentChanged);
} }
//! Returns true if RealDataItem can be linked to the instrument (same number of bins).
//! Also offers dialog to adjust instrument to match shape of real data.
bool LinkInstrumentManager::canLinkDataToInstrument(const RealDataItem* realDataItem, bool LinkInstrumentManager::canLinkDataToInstrument(const RealDataItem* realDataItem,
const QString& identifier, const QString& identifier,
bool quiet /*= false*/) bool quiet /*= false*/)
...@@ -53,9 +66,7 @@ bool LinkInstrumentManager::canLinkDataToInstrument(const RealDataItem* realData ...@@ -53,9 +66,7 @@ bool LinkInstrumentManager::canLinkDataToInstrument(const RealDataItem* realData
if (!instrumentItem) if (!instrumentItem)
return true; return true;
const bool isCompatible = if (!ImportDataUtils::Compatible(*instrumentItem, *realDataItem)) {
GUI::View::ImportDataUtils::Compatible(*instrumentItem, *realDataItem);
if (!isCompatible) {
if (!quiet) if (!quiet)
QMessageBox::warning(baWin, "Can't link to instrument", QMessageBox::warning(baWin, "Can't link to instrument",
"Can't link, data is incompatible with the instrument."); "Can't link, data is incompatible with the instrument.");
...@@ -74,8 +85,7 @@ bool LinkInstrumentManager::canLinkDataToInstrument(const RealDataItem* realData ...@@ -74,8 +85,7 @@ bool LinkInstrumentManager::canLinkDataToInstrument(const RealDataItem* realData
QString message = QString message =
realDataItem->holdsDimensionalData() realDataItem->holdsDimensionalData()
? "Experimental data carries information on the range/points of measurement." ? "Experimental data carries information on the range/points of measurement."
: GUI::View::ImportDataUtils::printShapeMessage(instrumentItem->shape(), : ImportDataUtils::printShapeMessage(instrumentItem->shape(), realDataItem->shape());
realDataItem->shape());
if (!QuestionOnInstrumentReshaping(message)) if (!QuestionOnInstrumentReshaping(message))
return false; return false;
...@@ -85,61 +95,21 @@ bool LinkInstrumentManager::canLinkDataToInstrument(const RealDataItem* realData ...@@ -85,61 +95,21 @@ bool LinkInstrumentManager::canLinkDataToInstrument(const RealDataItem* realData
void LinkInstrumentManager::onInstrumentChanged(const InstrumentItem* instrument) void LinkInstrumentManager::onInstrumentChanged(const InstrumentItem* instrument)
{ {
ASSERT(instrument != nullptr);
// Run through all RealDataItem and refresh linking to match possible change in detector // Run through all RealDataItem and refresh linking to match possible change in detector
// axes definition. // axes definition.
for (auto realDataItem : linkedRealDataItems(instrument)) for (auto realDataItem : m_realDataModel->realDataItems())
if (!instrument->alignedWith(realDataItem)) if (realDataItem->instrumentId() == instrument->id()) {
realDataItem->clearInstrumentId(); if (!instrument->alignedWith(realDataItem))
else realDataItem->clearInstrumentId();
realDataItem->updateToInstrument(instrument); else
realDataItem->updateToInstrument(instrument);
}
} }
void LinkInstrumentManager::onInstrumentAddedOrRemoved() void LinkInstrumentManager::onInstrumentAddedOrRemoved()
{ {
// remove links in realDataItems (in case of a linked instrument was removed) // remove links in realDataItems (in case of a linked instrument was removed)
for (auto realDataItem : m_realDataModel->realDataItems()) { for (auto realDataItem : m_realDataModel->realDataItems())
if (!m_instrumentModel->instrumentExists(realDataItem->instrumentId())) if (!m_instrumentModel->instrumentExists(realDataItem->instrumentId()))
realDataItem->clearInstrumentId(); realDataItem->clearInstrumentId();
}
}
//! Returns list of RealDataItem's linked to given instrument.
QList<RealDataItem*>
LinkInstrumentManager::linkedRealDataItems(const InstrumentItem* instrumentItem)
{
ASSERT(instrumentItem != nullptr);
QList<RealDataItem*> result;
for (auto realDataItem : m_realDataModel->realDataItems()) {
const QString linkedIdentifier = realDataItem->instrumentId();
const QString instrumentIdentifier = instrumentItem->id();
if (linkedIdentifier == instrumentIdentifier)
result.append(realDataItem);
}
return result;
}
namespace {
bool QuestionOnInstrumentReshaping(const QString& message)
{
QMessageBox msgBox;
msgBox.setText("Instrument description conflicts with the experimental data.");
QString informative;
informative.append(message);
informative.append("\n\nDo you want to adjust the instrument to the experimental data?\n\n");
msgBox.setInformativeText(informative);
QPushButton* modifyInstrumentButton =
msgBox.addButton("Yes, please modify instrument", QMessageBox::YesRole);
msgBox.addButton("No, leave as it is", QMessageBox::NoRole);
msgBox.exec();
return msgBox.clickedButton() == modifyInstrumentButton;
} }
} // namespace
...@@ -24,7 +24,6 @@ ...@@ -24,7 +24,6 @@
class InstrumentModel; class InstrumentModel;
class RealDataModel; class RealDataModel;
class SessionItem;
class InstrumentItem; class InstrumentItem;
class RealDataItem; class RealDataItem;
...@@ -36,17 +35,17 @@ class LinkInstrumentManager : public QObject { ...@@ -36,17 +35,17 @@ class LinkInstrumentManager : public QObject {
Q_OBJECT Q_OBJECT
public: public:
explicit LinkInstrumentManager(InstrumentModel* instrumentModel, RealDataModel* realDataModel); LinkInstrumentManager(InstrumentModel* instrumentModel, RealDataModel* realDataModel);
//! Returns true if RealDataItem can be linked to the instrument (same number of bins).
//! Also offers dialog to adjust instrument to match shape of real data.
//! quiet defines whether a "not possible" message box is shown if link is not possible. Use //! quiet defines whether a "not possible" message box is shown if link is not possible. Use
//! this e.g. for unit tests. The question for adjusting the instrument is not suppressed by //! this e.g. for unit tests. The question for adjusting the instrument is not suppressed by
//! this flag. //! this flag.
bool canLinkDataToInstrument(const RealDataItem* realDataItem, const QString& identifier, bool canLinkDataToInstrument(const RealDataItem* realDataItem, const QString& identifier,
bool quiet = false); bool quiet = false);
QList<RealDataItem*> linkedRealDataItems(const InstrumentItem* instrumentItem); private:
private slots:
void onInstrumentChanged(const InstrumentItem* instrument); void onInstrumentChanged(const InstrumentItem* instrument);
void onInstrumentAddedOrRemoved(); void onInstrumentAddedOrRemoved();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment