Skip to content
Snippets Groups Projects
Commit c2b0c4ca authored by Pospelov, Gennady's avatar Pospelov, Gennady
Browse files

New functionality in SessionModel to copy (clone) parts of the model.

parent 130662eb
No related branches found
No related tags found
No related merge requests found
...@@ -5,3 +5,11 @@ InstrumentModel::InstrumentModel(QObject *parent) ...@@ -5,3 +5,11 @@ InstrumentModel::InstrumentModel(QObject *parent)
{ {
} }
InstrumentModel *InstrumentModel::createCopy(ParameterizedItem *parent)
{
InstrumentModel *result = new InstrumentModel();
result->initFrom(this, parent);
return result;
}
...@@ -11,6 +11,8 @@ public: ...@@ -11,6 +11,8 @@ public:
explicit InstrumentModel(QObject *parent = 0); explicit InstrumentModel(QObject *parent = 0);
~InstrumentModel(){} ~InstrumentModel(){}
virtual InstrumentModel *createCopy(ParameterizedItem *parent=0);
}; };
......
...@@ -12,6 +12,13 @@ SampleModel::SampleModel(QObject *parent) ...@@ -12,6 +12,13 @@ SampleModel::SampleModel(QObject *parent)
} }
SampleModel *SampleModel::createCopy(ParameterizedItem *parent)
{
SampleModel *result = new SampleModel();
result->initFrom(this, parent);
return result;
}
void SampleModel::onMaterialModelChanged(const QModelIndex &first, const QModelIndex & /* second */) void SampleModel::onMaterialModelChanged(const QModelIndex &first, const QModelIndex & /* second */)
{ {
MaterialModel *materialModel = qobject_cast<MaterialModel *>(sender()); MaterialModel *materialModel = qobject_cast<MaterialModel *>(sender());
......
...@@ -12,6 +12,8 @@ public: ...@@ -12,6 +12,8 @@ public:
explicit SampleModel(QObject *parent = 0); explicit SampleModel(QObject *parent = 0);
~SampleModel(){} ~SampleModel(){}
SampleModel *createCopy(ParameterizedItem *parent = 0);
public slots: public slots:
void onMaterialModelChanged(const QModelIndex &first, const QModelIndex &second); void onMaterialModelChanged(const QModelIndex &first, const QModelIndex &second);
......
...@@ -333,8 +333,10 @@ void SessionModel::readFrom(QXmlStreamReader *reader) ...@@ -333,8 +333,10 @@ void SessionModel::readFrom(QXmlStreamReader *reader)
{ {
Q_ASSERT(reader); Q_ASSERT(reader);
qDebug() << "SessionModel::readFrom()" << m_model_tag << reader->name() << m_root_item;
if(reader->name() != m_model_tag) { if(reader->name() != m_model_tag) {
throw GUIHelpers::Error("SessioneModel::readFrom() -> Format error in p1"); throw GUIHelpers::Error("SessionModel::readFrom() -> Format error in p1");
} }
beginResetModel(); beginResetModel();
...@@ -353,13 +355,14 @@ void SessionModel::readFrom(QXmlStreamReader *reader) ...@@ -353,13 +355,14 @@ void SessionModel::readFrom(QXmlStreamReader *reader)
} }
void SessionModel::writeTo(QXmlStreamWriter *writer) void SessionModel::writeTo(QXmlStreamWriter *writer, ParameterizedItem *parent)
{ {
writer->writeStartElement(m_model_tag); writer->writeStartElement(m_model_tag);
writer->writeAttribute(SessionXML::ModelNameAttribute, m_name); writer->writeAttribute(SessionXML::ModelNameAttribute, m_name);
qDebug() << "SessionModel::writeTo"; qDebug() << "SessionModel::writeTo";
writeItemAndChildItems(writer, m_root_item); if(!parent) parent = m_root_item;
writeItemAndChildItems(writer, parent);
writer->writeEndElement(); // m_model_tag writer->writeEndElement(); // m_model_tag
} }
...@@ -404,55 +407,12 @@ void SessionModel::moveParameterizedItem(ParameterizedItem *item, ParameterizedI ...@@ -404,55 +407,12 @@ void SessionModel::moveParameterizedItem(ParameterizedItem *item, ParameterizedI
} }
//ParameterizedItem *SessionModel::createNewItem(QString model_type, SessionModel *SessionModel::createCopy(ParameterizedItem *parent)
// ParameterizedItem *parent, int row) {
//{ throw GUIHelpers::Error("SessionModel::createCopy() -> Error. Not implemented.");
// if (!m_root_item) { }
// m_root_item = ItemFactory::createEmptyItem();
// }
// if (!parent) parent = m_root_item;
// if (row == -1) row = parent->childItemCount();
// if (row < 0 || row > parent->childItemCount()) return 0;
// if (parent != m_root_item) {
// if (!parent->acceptsAsChild(model_type))
// return 0;
// }
// return ItemFactory::createItem(model_type);
//}
//void SessionModel::insertNewItem(ParameterizedItem *new_item,
// ParameterizedItem *parent,
// int row,
// ParameterizedItem::PortInfo::Keys port)
//{
// if(!new_item)
// throw GUIHelpers::Error("SessionModel::insertNewItem() ->Attempt to insert zero item");
//// Q_ASSERT(new_item);
//// if (!m_root_item) {
//// m_root_item = ItemFactory::createEmptyItem();
//// }
// if (!parent) parent = m_root_item;
// if (row == -1) row = parent->childItemCount();
// if (row < 0 || row > parent->childItemCount()) return;
//// if (parent != m_root_item) {
//// if (!parent->acceptsAsChild(model_type))
//// return 0;
//// }
//// ParameterizedItem *new_item = ItemFactory::createItem(model_type);
// if(port != ParameterizedItem::PortInfo::PortDef)
// new_item->setItemPort(port);
// connect(new_item, SIGNAL(propertyChanged(const QString &)), this, SLOT(onItemPropertyChange(const QString &)));
// parent->insertChildItem(row, new_item);
//// return new_item;
//}
ParameterizedItem *SessionModel::insertNewItem(QString model_type, ParameterizedItem *SessionModel::insertNewItem(QString model_type,
ParameterizedItem *parent, ParameterizedItem *parent,
...@@ -800,6 +760,28 @@ void SessionModel::onItemPropertyChange(const QString & name ) ...@@ -800,6 +760,28 @@ void SessionModel::onItemPropertyChange(const QString & name )
emit dataChanged(itemIndex, itemIndex); emit dataChanged(itemIndex, itemIndex);
} }
void SessionModel::initFrom(SessionModel *model, ParameterizedItem *parent)
{
qDebug() << "SessionModel::initFrom() -> " << model->getModelTag() << parent;
QByteArray byte_array;
QXmlStreamWriter writer(&byte_array);
writer.setAutoFormatting(true);
model->writeTo(&writer, parent);
QXmlStreamReader reader(byte_array);
while (!reader.atEnd()) {
reader.readNext();
if (reader.isStartElement()) {
readFrom(&reader);
}
}
}
void SessionModel::cleanItem(const QModelIndex &parent, int first, int /* last */) void SessionModel::cleanItem(const QModelIndex &parent, int first, int /* last */)
{ {
ParameterizedItem *parentItem = itemForIndex(parent); ParameterizedItem *parentItem = itemForIndex(parent);
......
...@@ -93,6 +93,7 @@ public: ...@@ -93,6 +93,7 @@ public:
QString getModelTag() const { return m_model_tag; } QString getModelTag() const { return m_model_tag; }
QString getModelName() const { return m_name; } QString getModelName() const { return m_name; }
void setModelName(const QString &name) { m_name = name; }
QList<QString> getAcceptableChildItems(const QModelIndex &parent) const; QList<QString> getAcceptableChildItems(const QModelIndex &parent) const;
...@@ -108,7 +109,7 @@ public: ...@@ -108,7 +109,7 @@ public:
ParameterizedItem *itemForIndex(const QModelIndex &index) const; ParameterizedItem *itemForIndex(const QModelIndex &index) const;
void readFrom(QXmlStreamReader *reader); void readFrom(QXmlStreamReader *reader);
void writeTo(QXmlStreamWriter *writer); void writeTo(QXmlStreamWriter *writer, ParameterizedItem *parent = 0);
void moveParameterizedItem(ParameterizedItem *item, ParameterizedItem *new_parent = 0, int row = -1); void moveParameterizedItem(ParameterizedItem *item, ParameterizedItem *new_parent = 0, int row = -1);
...@@ -121,9 +122,15 @@ public: ...@@ -121,9 +122,15 @@ public:
// int row; // int row;
// }; // };
virtual SessionModel *createCopy(ParameterizedItem *parent=0);
public slots: public slots:
void onItemPropertyChange(const QString &name); void onItemPropertyChange(const QString &name);
protected:
virtual void initFrom(SessionModel *model, ParameterizedItem *parent);
private: private:
// ParameterizedItem *createNewItem(QString model_type, ParameterizedItem *parent, int row = -1); // ParameterizedItem *createNewItem(QString model_type, ParameterizedItem *parent, int row = -1);
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include "TestParticleItems.h" #include "TestParticleItems.h"
#include "TestLayerRoughnessItems.h" #include "TestLayerRoughnessItems.h"
#include "TestParaCrystalItems.h" #include "TestParaCrystalItems.h"
#include "TestSessionModel.h"
int main(int argc, char** argv) { int main(int argc, char** argv) {
QCoreApplication app(argc, argv); QCoreApplication app(argc, argv);
...@@ -19,11 +20,13 @@ int main(int argc, char** argv) { ...@@ -19,11 +20,13 @@ int main(int argc, char** argv) {
TestParticleItems testParticleItems; TestParticleItems testParticleItems;
TestLayerRoughnessItems testLayerRoughnessItems; TestLayerRoughnessItems testLayerRoughnessItems;
TestParaCrystalItems testParaCrystalItems; TestParaCrystalItems testParaCrystalItems;
TestSessionModel testSessionModel;
return QTest::qExec(&testFormFactorItems, argc, argv) | return QTest::qExec(&testFormFactorItems, argc, argv) |
QTest::qExec(&testFTDistributionItems, argc, argv) | QTest::qExec(&testFTDistributionItems, argc, argv) |
QTest::qExec(&testParameterizedItem, argc, argv) | QTest::qExec(&testParameterizedItem, argc, argv) |
QTest::qExec(&testParticleItems, argc, argv) | QTest::qExec(&testParticleItems, argc, argv) |
QTest::qExec(&testLayerRoughnessItems, argc, argv) | QTest::qExec(&testLayerRoughnessItems, argc, argv) |
QTest::qExec(&testParaCrystalItems, argc, argv); QTest::qExec(&testParaCrystalItems, argc, argv) |
QTest::qExec(&testSessionModel, argc, argv);
} }
#ifndef TESTSESSIONMODEL_H
#define TESTSESSIONMODEL_H
#include <QtTest>
#include "SessionModel.h"
#include "SampleModel.h"
#include "MaterialModel.h"
#include "InstrumentModel.h"
#include "MaterialEditor.h"
#include <QXmlStreamWriter>
class TestSessionModel : public QObject {
Q_OBJECT
private slots:
void test_SampleModel_CreateCopy();
void test_SampleModel_CreatePartialCopy();
void test_InstrumentModel_CreateCopy();
void test_InstrumentModel_CreatePartialCopy();
};
inline void TestSessionModel::test_SampleModel_CreateCopy()
{
boost::scoped_ptr<MaterialModel> materialModel(new MaterialModel());
boost::scoped_ptr<MaterialEditor> materialEditor(new MaterialEditor(materialModel.get()));
SampleModel *model1 = new SampleModel();
ParameterizedItem *multilayer = model1->insertNewItem(Constants::MultiLayerType);
multilayer->setItemName("multilayer");
model1->insertNewItem(Constants::LayerType, model1->indexOfItem(multilayer));
ParameterizedItem *multilayer2 = model1->insertNewItem(Constants::MultiLayerType);
multilayer2->setItemName("multilayer2");
QString buffer1;
QXmlStreamWriter writer1(&buffer1);
model1->writeTo(&writer1);
SampleModel *model2 = model1->createCopy();
QString buffer2;
QXmlStreamWriter writer2(&buffer2);
model2->writeTo(&writer2);
QCOMPARE(buffer1, buffer2);
delete model1;
delete model2;
}
inline void TestSessionModel::test_SampleModel_CreatePartialCopy()
{
boost::scoped_ptr<MaterialModel> materialModel(new MaterialModel());
boost::scoped_ptr<MaterialEditor> materialEditor(new MaterialEditor(materialModel.get()));
SampleModel *model1 = new SampleModel();
ParameterizedItem *multilayer1 = model1->insertNewItem(Constants::MultiLayerType);
multilayer1->setItemName("multilayer1");
model1->insertNewItem(Constants::LayerType, model1->indexOfItem(multilayer1));
ParameterizedItem *multilayer2 = model1->insertNewItem(Constants::MultiLayerType);
multilayer2->setItemName("multilayer2");
SampleModel *model2 = model1->createCopy(multilayer1);
ParameterizedItem *result = model2->itemForIndex(model2->index(0,0,QModelIndex()));
QCOMPARE(result->itemName(), multilayer1->itemName());
QCOMPARE(result->modelType(), multilayer1->modelType());
delete model1;
delete model2;
}
inline void TestSessionModel::test_InstrumentModel_CreateCopy()
{
InstrumentModel *model1 = new InstrumentModel();
ParameterizedItem *instrument1 = model1->insertNewItem(Constants::InstrumentType);
instrument1->setItemName("instrument1");
model1->insertNewItem(Constants::DetectorType, model1->indexOfItem(instrument1));
model1->insertNewItem(Constants::BeamType, model1->indexOfItem(instrument1));
ParameterizedItem *instrument2 = model1->insertNewItem(Constants::InstrumentType);
instrument2->setItemName("instrument2");
model1->insertNewItem(Constants::DetectorType, model1->indexOfItem(instrument2));
model1->insertNewItem(Constants::BeamType, model1->indexOfItem(instrument2));
QString buffer1;
QXmlStreamWriter writer1(&buffer1);
model1->writeTo(&writer1);
InstrumentModel *model2 = model1->createCopy();
QString buffer2;
QXmlStreamWriter writer2(&buffer2);
model2->writeTo(&writer2);
QCOMPARE(buffer1, buffer2);
delete model1;
delete model2;
}
inline void TestSessionModel::test_InstrumentModel_CreatePartialCopy()
{
InstrumentModel *model1 = new InstrumentModel();
ParameterizedItem *instrument1 = model1->insertNewItem(Constants::InstrumentType);
instrument1->setItemName("instrument1");
model1->insertNewItem(Constants::DetectorType, model1->indexOfItem(instrument1));
model1->insertNewItem(Constants::BeamType, model1->indexOfItem(instrument1));
ParameterizedItem *instrument2 = model1->insertNewItem(Constants::InstrumentType);
instrument2->setItemName("instrument2");
model1->insertNewItem(Constants::DetectorType, model1->indexOfItem(instrument2));
model1->insertNewItem(Constants::BeamType, model1->indexOfItem(instrument2));
InstrumentModel *model2 = model1->createCopy(instrument2);
ParameterizedItem *result = model2->itemForIndex(model2->index(0,0,QModelIndex()));
QCOMPARE(result->itemName(), instrument2->itemName());
QCOMPARE(result->modelType(), instrument2->modelType());
delete model1;
delete model2;
}
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment