BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
JobModel.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/Model/Model/JobModel.cpp
6 //! @brief Implements class JobModel
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
20 #include "GUI/Model/Job/JobItem.h"
27 
28 JobModel::JobModel(QObject* parent)
29  : SessionModel(GUI::Session::XML::JobModelTag, parent)
30  , m_queue_data(nullptr)
31 {
32  m_queue_data = new JobQueueData(this);
35  setObjectName(GUI::Session::XML::JobModelTag);
36 }
38 {
39  delete m_queue_data;
40 }
41 
42 JobItem* JobModel::getJobItemForIdentifier(const QString& identifier)
43 {
44  for (auto* jobItem : jobItems())
45  if (jobItem->getIdentifier() == identifier)
46  return jobItem;
47 
48  return nullptr;
49 }
50 
51 //! Main method to add a job
52 JobItem* JobModel::addJob(const MultiLayerItem* sampleItem, const InstrumentItem* instrumentItem,
53  const RealDataItem* realDataItem, const SimulationOptionsItem& optionItem)
54 {
55  ASSERT(sampleItem);
56  ASSERT(instrumentItem);
57 
58  auto* jobItem = insertItem<JobItem>();
59  jobItem->setItemName(generateJobName());
60  jobItem->setIdentifier(QUuid::createUuid().toString());
61 
62  jobItem->copySampleIntoJob(sampleItem);
63  GUI::Model::JobFunctions::setupJobItemInstrument(jobItem, instrumentItem);
64 
65  jobItem->copySimulationOptionsIntoJob(optionItem);
66 
67  ParameterTreeBuilder(jobItem, true).build();
68 
70 
71  if (realDataItem)
72  GUI::Model::JobFunctions::setupJobItemForFit(jobItem, realDataItem);
73 
74  return jobItem;
75 }
76 
77 QVector<JobItem*> JobModel::jobItems() const
78 {
79  return topItems<JobItem>();
80 }
81 
82 //! restore instrument and sample model from backup for given JobItem
83 void JobModel::restore(JobItem* jobItem)
84 {
86 }
87 
89 {
91  return true;
92 
93  for (auto* jobItem : jobItems())
94  if (jobItem->getStatus() == JobStatus::Fitting)
95  return true;
96 
97  return false;
98 }
99 
101 {
102  for (auto* job : jobItems())
103  removeJob(job);
104 
106 }
107 
108 QVector<SessionItem*> JobModel::nonXMLItems() const
109 {
110  QVector<SessionItem*> result;
111 
112  for (auto* jobItem : topItems<JobItem>()) {
113  if (auto* intensityItem = jobItem->dataItem())
114  result.push_back(intensityItem);
115 
116  if (auto* real_data = dynamic_cast<RealDataItem*>(jobItem->realDataItem())) {
117  if (auto* data_item = real_data->dataItem())
118  result.push_back(data_item);
119  if (auto* native_data = real_data->nativeData())
120  result.push_back(native_data);
121  }
122  }
123 
124  return result;
125 }
126 
127 void JobModel::readFrom(QXmlStreamReader* reader, MessageService* messageService /*= 0*/)
128 {
129  SessionModel::readFrom(reader, messageService);
130 
131  // Set & check instrument links. JobItem and realDataItem have to reference the same instrument
132  // (which is the one contained in this job item, not contained in the instrument model)
133  for (auto* jobItem : jobItems()) {
134  if (RealDataItem* refItem = jobItem->realDataItem())
135  refItem->linkToInstrument(jobItem->instrumentItem());
136 
137  // Create the not stored parameter tuning tree
138  ParameterTreeBuilder(jobItem, false).build();
139  }
140 }
141 
142 void JobModel::runJob(JobItem* jobItem)
143 {
144  m_queue_data->runJob(jobItem);
145 }
146 
148 {
149  m_queue_data->cancelJob(jobItem->getIdentifier());
150 }
151 
153 {
154  ASSERT(jobItem);
155  m_queue_data->removeJob(jobItem->getIdentifier());
156 
157  removeRows(jobItem->index().row(), 1, QModelIndex());
158 }
159 
161 {
162  int maxJobIndex = 0;
163  for (const auto* jobItem : jobItems()) {
164  if (jobItem->itemName().startsWith("job"))
165  maxJobIndex = std::max(maxJobIndex, jobItem->itemName().mid(3).toInt());
166  }
167  return QString("job%1").arg(maxJobIndex + 1);
168 }
Defines class InstrumentItem and all its children.
Defines class IntensityDataItem.
Defines abstract item with a material property.
Defines auxiliary functions in namespace GUI::Model::JobModelFunctions.
Defines class JobItem.
Defines class JobModel.
Defines class JobQueueData.
@ Fitting
the job is busy fitting
Defines class MultiLayerItem.
Defines namespace GUI::Model::ParameterTreeUtils.
Defines pointwise axis item.
Defines class RealDataItem.
Defines reader and writer classes for SessionModel.
Abstract base class for instrument-specific item classes.
QString getIdentifier() const
Definition: JobItem.cpp:73
ParameterContainerItem * parameterContainerItem()
Definition: JobItem.cpp:271
void focusRequest(JobItem *item)
void globalProgress(int)
QVector< JobItem * > jobItems() const
Definition: JobModel.cpp:77
void runJob(JobItem *jobItem)
Definition: JobModel.cpp:142
~JobModel() override
Definition: JobModel.cpp:37
void removeJob(JobItem *jobItem)
Definition: JobModel.cpp:152
void restore(JobItem *jobItem)
restore instrument and sample model from backup for given JobItem
Definition: JobModel.cpp:83
bool hasUnfinishedJobs()
Definition: JobModel.cpp:88
JobQueueData * m_queue_data
Definition: JobModel.h:60
JobModel(QObject *parent=nullptr)
Definition: JobModel.cpp:28
JobItem * getJobItemForIdentifier(const QString &identifier)
Definition: JobModel.cpp:42
void clear() override
Definition: JobModel.cpp:100
QString generateJobName()
generates numbered job name with new/unused number
Definition: JobModel.cpp:160
void cancelJob(JobItem *jobItem)
Definition: JobModel.cpp:147
void readFrom(QXmlStreamReader *reader, MessageService *messageService=nullptr) override
Definition: JobModel.cpp:127
QVector< SessionItem * > nonXMLItems() const override
Definition: JobModel.cpp:108
JobItem * addJob(const MultiLayerItem *sampleItem, const InstrumentItem *instrumentItem, const RealDataItem *realDataItem, const SimulationOptionsItem &optionItem)
Main method to add a job.
Definition: JobModel.cpp:52
The JobQueueData class holds all objects/logic to run simulation in a thread.
Definition: JobQueueData.h:28
bool hasUnfinishedJobs()
void focusRequest(JobItem *jobItem)
void globalProgress(int)
void removeJob(const QString &identifier)
Remove job from list completely.
void cancelJob(const QString &identifier)
Cancels running job.
void runJob(JobItem *jobItem)
Submits job and run it in a thread.
The service to collect messages from different senders.
The ParameterTreeBuilder contains helper functions to create container with ParameterItems....
Provides access to experimental data, for display and fitting. Owns an AbstractDataLoader.
Definition: RealDataItem.h:33
QModelIndex index() const
Returns model index of this item.
Definition: SessionItem.cpp:74
Base class for a GUI data collection. A collection is e.g. all real data (RealDataModel)....
Definition: SessionModel.h:42
virtual void clear()
virtual void readFrom(QXmlStreamReader *reader, MessageService *messageService=nullptr)
bool removeRows(int row, int count, const QModelIndex &parent) override
The SimulationOptionsItem class holds simulation status (run policy, number of threads,...
void setupJobItemOutput(JobItem *jobItem)
Setup items intended for storing results of the job.
void setupJobItemForFit(JobItem *jobItem, const RealDataItem *realDataItem)
Setups JobItem for fit.
void setupJobItemInstrument(JobItem *jobItem, const InstrumentItem *from)
Properly copies instrument into job item.
constexpr auto JobModelTag("JobModel")
QString toString(const QModelIndex &index)
Provides string representation of index data.
Definition: def.cpp:20
Definition: Streamer.h:23