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

refactor instrument copy

parent ebfce4f8
Branches
Tags
1 merge request!304Add instrument library
......@@ -71,6 +71,33 @@ void InstrumentModel::readFrom(QXmlStreamReader* reader, MessageService* message
emit instrumentAddedOrRemoved();
}
InstrumentItem* InstrumentModel::insertCopy(const InstrumentItem& source)
{
InstrumentItem* copy = static_cast<InstrumentItem*>(insertNewItem(source.modelType()));
const QString idBackup = copy->id(); // will be overwritten by the following
for (auto child : source.children()) {
auto tag = source.tagFromItem(child);
copyItem(child, copy, tag);
}
copy->setId(idBackup);
// copying non-uniform axis data
if (auto specularSource = dynamic_cast<const SpecularInstrumentItem*>(&source)) {
auto axis_group = specularSource->beamItem()->inclinationAxisGroup();
auto donor_axis = axis_group->itemOfType<PointwiseAxisItem>();
if (donor_axis->containsNonXMLData()) {
auto acceptor_axis = dynamic_cast<SpecularInstrumentItem*>(copy)
->beamItem()
->inclinationAxisGroup()
->itemOfType<PointwiseAxisItem>();
acceptor_axis->init(*donor_axis->axis(), donor_axis->getUnitsLabel());
}
}
return copy;
}
QVector<InstrumentItem*> InstrumentModel::instrumentItems() const
{
return topItems<InstrumentItem>();
......
......@@ -31,6 +31,11 @@ public:
QVector<SessionItem*> nonXMLItems() const override;
virtual void readFrom(QXmlStreamReader* reader, MessageService* messageService = 0) override;
//! Inserts a deep copy (also of any non xml data in a pointwise axis)
//! The id will not be copied, but a new unique one will created
//! Returns the newly created instrument.
InstrumentItem* insertCopy(const InstrumentItem& instrument);
QVector<InstrumentItem*> instrumentItems() const;
QVector<Instrument2DItem*> instrument2DItems() const;
......
......@@ -25,7 +25,7 @@
#include <QVBoxLayout>
InstrumentSelectorWidget::InstrumentSelectorWidget(InstrumentModel* model, QWidget* parent)
: ItemSelectorWidget(parent)
: ItemSelectorWidget(parent), m_instrumentModel(model)
{
setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);
......@@ -156,36 +156,18 @@ void InstrumentSelectorWidget::onRemove()
void InstrumentSelectorWidget::onCopy()
{
QModelIndex index = selectionModel()->selectedIndexes().front();
if (!index.isValid())
return;
if (index.isValid()) {
SessionItem* item = m_model->itemForIndex(index);
QString nameOfCopy = suggestInstrumentName(item->itemName());
InstrumentItem* copy =
static_cast<InstrumentItem*>(m_model->insertNewItem(item->modelType()));
const QString idBackup = copy->id(); // will be overwritten by the following
for (auto child : item->children()) {
auto tag = item->tagFromItem(child);
m_model->copyItem(child, copy, tag);
}
copy->setId(idBackup);
copy->setItemName(nameOfCopy);
// copying non-uniform axis data
if (auto instrument = dynamic_cast<SpecularInstrumentItem*>(item)) {
auto axis_group = instrument->beamItem()->inclinationAxisGroup();
auto donor_axis = axis_group->itemOfType<PointwiseAxisItem>();
if (!donor_axis->containsNonXMLData())
return;
auto acceptor_axis = dynamic_cast<SpecularInstrumentItem*>(copy)
->beamItem()
->inclinationAxisGroup()
->itemOfType<PointwiseAxisItem>();
acceptor_axis->init(*donor_axis->axis(), donor_axis->getUnitsLabel());
}
selectionModel()->select(m_model->indexOfItem(copy), QItemSelectionModel::ClearAndSelect);
}
const InstrumentItem* toBeCopied = dynamic_cast<InstrumentItem*>(m_model->itemForIndex(index));
if (toBeCopied == nullptr)
return;
const auto newName = suggestInstrumentName(toBeCopied->instrumentName());
InstrumentItem* copy = m_instrumentModel->insertCopy(*toBeCopied);
copy->setItemName(newName);
selectionModel()->select(m_model->indexOfItem(copy), QItemSelectionModel::ClearAndSelect);
}
void InstrumentSelectorWidget::updateActions()
......
......@@ -48,6 +48,8 @@ private:
QMap<QString, int> mapOfNames();
template <class Instrument> void newInstrument();
InstrumentModel* m_instrumentModel;
public:
QAction* m_action_new_gisas;
QAction* m_action_new_offspecular;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment