BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
JobItem.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/Model/Job/JobItem.cpp
6 //! @brief Implements class JobItem
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 
15 #include "GUI/Model/Job/JobItem.h"
16 #include "Base/Util/Assert.h"
30 #include "GUI/Util/Error.h"
31 
32 namespace {
33 namespace Tags {
34 
35 const QString SimulationOptions("SimulationOptions");
36 const QString Materials("Materials");
37 const QString ParameterContainer("ParameterContainer");
38 const QString Sample("Sample");
39 const QString Instrument("Instrument");
40 
41 } // namespace Tags
42 } // namespace
43 
45  : SessionItem(M_TYPE)
46 {
48  addProperty(P_IDENTIFIER, QString());
49  addProperty(P_INSTRUMENT_NAME, QString())->setEditable(false);
51 
53 
54  addProperty(P_BEGIN_TIME, QString())->setEditable(false);
55  addProperty(P_END_TIME, QString())->setEditable(false);
56 
57  addProperty(P_COMMENTS, QString());
59  addProperty(P_PRESENTATION_TYPE, QVariant::Type::Invalid);
60 
64 
66 }
67 
69 {
70  emit jobDestroyed();
71 }
72 
73 QString JobItem::getIdentifier() const
74 {
75  return getItemValue(P_IDENTIFIER).toString();
76 }
77 
78 void JobItem::setIdentifier(const QString& identifier)
79 {
80  setItemValue(P_IDENTIFIER, identifier);
81  emit jobIdentifierChanged(identifier);
82 }
83 
84 QString JobItem::jobName() const
85 {
86  return itemName();
87 }
88 
89 void JobItem::setJobName(const QString& name)
90 {
93  emit jobNameChanged(name);
94 }
95 
97 {
98  return dynamic_cast<IntensityDataItem*>(getItem(T_OUTPUT));
99 }
100 
102 {
103  return dynamic_cast<DataItem*>(getItem(T_OUTPUT));
104 }
105 
107 {
109 }
110 
111 void JobItem::setStatus(const JobStatus status)
112 {
114  if (status == JobStatus::Failed) {
115  if (DataItem* intensityItem = dataItem()) {
116  if (intensityItem->getDatafield())
117  intensityItem->getDatafield()->setAllTo(0.0);
118  emit intensityItem->emitDataChanged();
119  }
120  }
121  emit jobStatusChanged(status);
122 }
123 
124 bool JobItem::isIdle() const
125 {
126  return getStatus() == JobStatus::Idle;
127 }
128 
129 bool JobItem::isRunning() const
130 {
131  return getStatus() == JobStatus::Running;
132 }
133 
135 {
136  return getStatus() == JobStatus::Completed;
137 }
138 
140 {
141  return getStatus() == JobStatus::Canceled;
142 }
143 
144 bool JobItem::isFailed() const
145 {
146  return getStatus() == JobStatus::Failed;
147 }
148 
149 bool JobItem::isFitting() const
150 {
151  return getStatus() == JobStatus::Fitting;
152 }
153 
155 {
156  return isTag(T_REALDATA) && getItem(T_REALDATA);
157 }
158 
159 QDateTime JobItem::beginTime() const
160 {
161  return QDateTime::fromString(getItemValue(P_BEGIN_TIME).toString(), Qt::ISODateWithMs);
162 }
163 
164 void JobItem::setBeginTime(const QDateTime& begin_time)
165 {
166  setItemValue(P_BEGIN_TIME, begin_time.toString(Qt::ISODateWithMs));
167  emit jobBeginTimeChanged(begin_time);
168 }
169 
170 QDateTime JobItem::endTime() const
171 {
172  return QDateTime::fromString(getItemValue(P_END_TIME).toString(), Qt::ISODateWithMs);
173 }
174 
175 void JobItem::setEndTime(const QDateTime& end_time)
176 {
177  setItemValue(P_END_TIME, end_time.toString(Qt::ISODateWithMs));
178  emit jobEndTimeChanged(end_time);
179 }
180 
181 std::optional<size_t> JobItem::duration() const
182 {
183  QDateTime begin_time = beginTime();
184  QDateTime end_time = endTime();
185  if (begin_time.isValid() && end_time.isValid() && begin_time < end_time)
186  return begin_time.msecsTo(end_time);
187  return std::nullopt;
188 }
189 
190 QString JobItem::getComments() const
191 {
192  return getItemValue(P_COMMENTS).toString();
193 }
194 
195 void JobItem::setComments(const QString& comments)
196 {
197  setItemValue(P_COMMENTS, comments);
198  emit jobCommentsChanged(comments);
199 }
200 
202 {
203  return getItemValue(P_PROGRESS).toInt();
204 }
205 
206 void JobItem::setProgress(int progress)
207 {
208  setItemValue(P_PROGRESS, progress);
209  emit jobProgressChanged(progress);
210 }
211 
213 {
215 }
216 
218 {
220 }
221 
223 {
224  return &m_sampleItem;
225 }
226 
228 {
229  m_sampleItem.initFrom(sample);
230  return &m_sampleItem;
231 }
232 
234 {
235  return m_instrument.get();
236 }
237 
239 {
240  auto* result = dynamic_cast<Instrument2DItem*>(m_instrument.get());
241  ASSERT(result);
242  return result;
243 }
244 
246 {
247  m_instrument.reset(instrument->createCopy());
248  return m_instrument.get();
249 }
250 
251 void JobItem::setResults(const SimulationResult& result)
252 {
255 }
256 
258 {
259  return dynamic_cast<FitSuiteItem*>(getItem(T_FIT_SUITE));
260 }
261 
263 {
264  if (getItem(T_FIT_SUITE))
265  throw Error("JobItem::createFitSuiteItem() -> Error. Attempt to create "
266  "a second FitSuiteItem.");
267 
268  return model()->insertItem<FitSuiteItem>(this, -1, T_FIT_SUITE);
269 }
270 
272 {
273  return const_cast<ParameterContainerItem*>(
274  static_cast<const JobItem*>(this)->parameterContainerItem());
275 }
276 
278 {
279  return &m_parameterContainer;
280 }
281 
283 {
284  if (FitSuiteItem* item = fitSuiteItem())
285  return item->fitParameterContainerItem();
286 
287  return nullptr;
288 }
289 
291 {
292  return dynamic_cast<RealDataItem*>(getItem(JobItem::T_REALDATA));
293 }
294 
296 {
297  return model()->copyItem(real_data, this, T_REALDATA);
298 }
299 
301 {
302  return model()->insertItem<RealDataItem>(this, -1, T_REALDATA);
303 }
304 
306 {
307  return dynamic_cast<Data1DViewItem*>(getItem(JobItem::T_DATAVIEW));
308 }
309 
311 {
312  insertChild(-1, data_view, T_DATAVIEW);
313 }
314 
316 {
317  return model()->insertItem<Data1DViewItem>(this, -1, T_DATAVIEW);
318 }
319 
320 QString JobItem::sampleName() const
321 {
322  return m_sampleItem.sampleName();
323 }
324 
325 QString JobItem::instrumentName() const
326 {
327  return getItemValue(P_INSTRUMENT_NAME).toString();
328 }
329 
330 void JobItem::setInstrumentName(const QString& name)
331 {
334 }
335 
337 {
338  return getItemValue(P_PRESENTATION_TYPE).toString();
339 }
340 
341 void JobItem::setPresentationType(const QString& type)
342 {
344  emit jobPresentationTypeChanged(type);
345 }
346 
347 void JobItem::writeNonSessionItems(QXmlStreamWriter* writer) const
348 {
350 
351  writer->writeStartElement(Tags::SimulationOptions);
353  writer->writeEndElement();
354 
355  Streamer(writer).write<InstrumentItemCatalog>(Tags::Instrument, m_instrument.get());
356 
357  writer->writeStartElement(Tags::Sample);
359  writer->writeEndElement();
360 
361  writer->writeStartElement(Tags::ParameterContainer);
363  writer->writeEndElement();
364 }
365 
366 void JobItem::readNonSessionItems(QXmlStreamReader* reader)
367 {
368  const int version = reader->attributes().value(GUI::Session::XML::Version).toInt();
369 
370  if (version < 1)
372 
373  if (version > 1)
375 
376  while (reader->readNextStartElement()) {
377  if (reader->name() == Tags::SimulationOptions) {
379  GUI::Session::XML::gotoEndElementOfTag(reader, Tags::SimulationOptions);
380  } else if (reader->name() == Tags::Instrument) {
381  InstrumentItem* p = nullptr;
382  Streamer(reader).read<InstrumentItemCatalog>(Tags::Instrument, p);
383  m_instrument.reset(p);
384  } else if (reader->name() == Tags::ParameterContainer) {
386  GUI::Session::XML::gotoEndElementOfTag(reader, Tags::ParameterContainer);
387  } else if (reader->name() == Tags::Sample) {
389  GUI::Session::XML::gotoEndElementOfTag(reader, Tags::Sample);
390  }
391  }
392 }
393 
395 {
397 }
398 
399 //! Updates the name of file to store intensity data.
400 
402 {
403  if (DataItem* item = dataItem())
405 
406  if (RealDataItem* realItem = realDataItem()) {
407  if (DataItem* item = realItem->dataItem())
409 
410  if (DataItem* item = realItem->nativeData())
412  }
413 }
414 
416 {
418 }
419 
421 {
422  m_simulationOptionsItem = options;
423 }
Defines class Data1DViewItem.
Defines class DeserializationException.
Defines error class.
Defines class FitSuiteItem.
Defines class InstrumentItemCatalog.
Defines class InstrumentItem and all its children.
Defines class IntensityDataItem.
Defines namespace GUI::Model::FilenameUtils.
Defines abstract item with a material property.
Defines namespace GUI::Model::JobItemUtils.
Defines class JobItem.
QString jobStatusToString(JobStatus status)
get a string representation of the status
Definition: JobStatus.cpp:32
JobStatus jobStatusFromString(const QString &name)
get status value for given string representation
Definition: JobStatus.cpp:39
JobStatus
The JobStatus enum lists the possible states of a job.
Definition: JobStatus.h:22
@ Completed
the job was successfully completed
@ Canceled
the job was stopped by the user
@ Running
the job is busy calculating
@ Failed
the job aborted because it hit an error
@ Idle
the job has not been started yet
@ Fitting
the job is busy fitting
Defines class RealDataItem.
Defines class SpecularDataItem.
Defines class Streamer.
Defines.
View model for 1D DataItem. Can represent several items at once. In current implementation the first ...
static constexpr auto M_TYPE
Abstract base class for IntensityDataItem and SpecularDataItem. Owns one simulated data set of type D...
Definition: DataItem.h:34
static DeserializationException tooOld()
static DeserializationException tooNew()
The FitParameterContainerItem class is a collection of all defined fit parameters in JobItem.
static constexpr auto M_TYPE
Definition: FitSuiteItem.h:32
Abstract base class for instrument-specific item classes.
InstrumentItem * createCopy() const
Creates an exact copy; also ID is the same!
bool is() const
Assembles beam, detector and their relative positions with respect to the sample.
Definition: Instrument.h:26
static constexpr auto M_TYPE
void jobStatusChanged(const JobStatus status)
void setComments(const QString &comments)
Definition: JobItem.cpp:195
QString getComments() const
Definition: JobItem.cpp:190
void jobCommentsChanged(const QString &comments)
void setEndTime(const QDateTime &end_time)
Definition: JobItem.cpp:175
QString instrumentName() const
Definition: JobItem.cpp:325
void jobEndTimeChanged(const QDateTime &end_time)
static constexpr auto P_END_TIME
Definition: JobItem.h:51
static constexpr auto M_TYPE
Definition: JobItem.h:62
const SimulationOptionsItem & simulationOptionsItem() const
Definition: JobItem.cpp:415
void setJobName(const QString &name)
Definition: JobItem.cpp:89
static constexpr auto T_REALDATA
Definition: JobItem.h:57
void readNonSessionItems(QXmlStreamReader *reader) override
Definition: JobItem.cpp:366
QDateTime endTime() const
Definition: JobItem.cpp:170
ParameterContainerItem m_parameterContainer
Definition: JobItem.h:165
void writeNonSessionItems(QXmlStreamWriter *writer) const override
Definition: JobItem.cpp:347
void jobBeginTimeChanged(const QDateTime &begin_time)
bool isIdle() const
Definition: JobItem.cpp:124
QString sampleName() const
Definition: JobItem.cpp:320
Instrument2DItem * instrument2DItem()
Definition: JobItem.cpp:238
static constexpr auto P_BEGIN_TIME
Definition: JobItem.h:50
void setStatus(JobStatus status)
Definition: JobItem.cpp:111
bool isFailed() const
Definition: JobItem.cpp:144
static constexpr auto P_INSTRUMENT_NAME
Definition: JobItem.h:47
void jobProgressChanged(int progress)
static constexpr auto P_STATUS
Definition: JobItem.h:49
void setResults(const SimulationResult &result)
Definition: JobItem.cpp:251
bool runImmediately() const
Definition: JobItem.cpp:212
void setPresentationType(const QString &type)
Definition: JobItem.cpp:341
SimulationOptionsItem m_simulationOptionsItem
Definition: JobItem.h:164
void jobNameChanged(const QString &name)
MultiLayerItem * sampleItem()
Definition: JobItem.cpp:222
void setProgress(int progress)
Definition: JobItem.cpp:206
void jobDestroyed()
void copySimulationOptionsIntoJob(const SimulationOptionsItem &options)
Definition: JobItem.cpp:420
void addDataViewItem(Data1DViewItem *data_view)
Definition: JobItem.cpp:310
FitParameterContainerItem * fitParameterContainerItem()
Definition: JobItem.cpp:282
void updateIntensityDataFileName()
Updates the name of file to store intensity data.
Definition: JobItem.cpp:401
void jobPresentationTypeChanged(const QString &type)
Data1DViewItem * dataItemView()
Definition: JobItem.cpp:305
void jobIdentifierChanged(const QString &identifier)
int getProgress() const
Definition: JobItem.cpp:201
static constexpr auto T_DATAVIEW
Definition: JobItem.h:58
QString jobName() const
Definition: JobItem.cpp:84
void setInstrumentName(const QString &name)
Definition: JobItem.cpp:330
~JobItem()
Definition: JobItem.cpp:68
static constexpr auto T_OUTPUT
Definition: JobItem.h:56
FitSuiteItem * fitSuiteItem()
Definition: JobItem.cpp:257
bool isCompleted() const
Definition: JobItem.cpp:134
InstrumentItem * instrumentItem() const
Definition: JobItem.cpp:233
QString presentationType() const
Definition: JobItem.cpp:336
DataItem * dataItem()
Definition: JobItem.cpp:101
void setIdentifier(const QString &identifier)
Definition: JobItem.cpp:78
RealDataItem * realDataItem()
Definition: JobItem.cpp:290
static constexpr auto T_FIT_SUITE
Definition: JobItem.h:59
bool isValidForFitting()
Definition: JobItem.cpp:154
RealDataItem * copyRealDataIntoJob(const RealDataItem *real_data)
Definition: JobItem.cpp:295
void jobInstrumentNameChanged(const QString &name)
static constexpr auto P_IDENTIFIER
Definition: JobItem.h:46
QString getIdentifier() const
Definition: JobItem.cpp:73
FitSuiteItem * createFitSuiteItem()
Definition: JobItem.cpp:262
bool isRunning() const
Definition: JobItem.cpp:129
JobItem()
Definition: JobItem.cpp:44
QDateTime beginTime() const
Definition: JobItem.cpp:159
MultiLayerItem * copySampleIntoJob(const MultiLayerItem *sample)
Definition: JobItem.cpp:227
ParameterContainerItem * parameterContainerItem()
Definition: JobItem.cpp:271
bool isSpecularJob() const
Definition: JobItem.cpp:394
bool isCanceled() const
Definition: JobItem.cpp:139
static constexpr auto P_PROGRESS
Definition: JobItem.h:53
IntensityDataItem * intensityDataItem()
Definition: JobItem.cpp:96
bool runInBackground() const
Definition: JobItem.cpp:217
MultiLayerItem m_sampleItem
Definition: JobItem.h:166
JobStatus getStatus() const
Definition: JobItem.cpp:106
std::unique_ptr< InstrumentItem > m_instrument
Definition: JobItem.h:167
RealDataItem * createRealDataItem()
Definition: JobItem.cpp:300
InstrumentItem * copyInstrumentIntoJob(const InstrumentItem *instrument)
Definition: JobItem.cpp:245
static constexpr auto P_PRESENTATION_TYPE
Definition: JobItem.h:54
void setBeginTime(const QDateTime &begin_time)
Definition: JobItem.cpp:164
bool isFitting() const
Definition: JobItem.cpp:149
std::optional< size_t > duration() const
if begin and end time are both available the duration in ms, otherwise empty
Definition: JobItem.cpp:181
static constexpr auto P_COMMENTS
Definition: JobItem.h:52
static constexpr auto P_WITH_FITTING
Definition: JobItem.h:48
Data1DViewItem * createDataViewItem()
Definition: JobItem.cpp:315
QString sampleName() const
void initFrom(const MultiLayerItem *other)
void writeContentTo(QXmlStreamWriter *writer) const
void readContentFrom(QXmlStreamReader *reader)
The ParameterContainerItem is a top item to hold all ParameterItem, represents an entry point to para...
void readContentFrom(QXmlStreamReader *reader)
void writeContentTo(QXmlStreamWriter *writer) const
Provides access to experimental data, for display and fitting. Owns an AbstractDataLoader.
Definition: RealDataItem.h:33
static constexpr auto M_TYPE
Definition: RealDataItem.h:45
Base class for a GUI data item.
Definition: SessionItem.h:204
bool isTag(const QString &name) const
Returns true if tag is available.
QString itemName() const
Get item name, return display name if no name is set.
void setItemName(const QString &name)
Set item name, add property if necessary.
SessionItem * addProperty(const QString &name, const QVariant &variant)
Add new property item and register new tag. name is the tag name and the display name....
bool registerTag(const QString &name, int min=0, int max=-1, QStringList modelTypes={})
Add new tag to this item with given name, min, max and types. max = -1 -> unlimited,...
void insertChild(int row, SessionItem *item, const QString &tag="")
Insert item into given tag into given row.
QVariant getItemValue(const QString &tag) const
Directly access value of item under given tag.
void setItemValue(const QString &tag, const QVariant &variant) const
Directly set value of item under given tag.
SessionModel * model() const
Returns model of this item.
Definition: SessionItem.cpp:60
T * item(const QString &tag) const
Definition: SessionItem.h:353
void setEditable(bool enabled)
SessionItem * getItem(const QString &tag="", int row=0) const
Returns item in given row of given tag.
T * insertItem(SessionItem *parent=nullptr, int row=-1, QString tag="")
Definition: SessionModel.h:137
T * copyItem(const T *item_to_copy, SessionItem *new_parent=nullptr, const QString &tag="")
Definition: SessionModel.h:149
The SimulationOptionsItem class holds simulation status (run policy, number of threads,...
void writeContentTo(QXmlStreamWriter *writer) const
void readContentFrom(QXmlStreamReader *reader)
static constexpr auto M_TYPE
Supports serialization to or deserialization from QXmlStream.
Definition: Streamer.h:36
void write(const QString &tag, typename Catalog::CatalogedType *p)
Definition: Streamer.h:82
void read(const QString &tag, typename Catalog::CatalogedType *&p, Args... argsForConstructor)
Definition: Streamer.h:93
QString jobReferenceFileName(const QString &itemName)
Constructs the name of the file with reference data.
QString jobResultsFileName(const QString &itemName)
Constructs the name of the file with simulated intensities.
QString jobNativeDataFileName(const QString &id)
void setResults(DataItem *intensityItem, const SimulationResult &result)
Sets simulation results into the DataItem.
QString const & name(EShape k)
Definition: particles.cpp:20
constexpr auto Version("Version")
void writeAttribute(QXmlStreamWriter *writer, const QString &attributeName, const QVariant &variant)
Write the variant's value as an attribute.
Definition: UtilXML.cpp:65
void gotoEndElementOfTag(QXmlStreamReader *reader, const QString &tag)
Definition: UtilXML.cpp:49
QString toString(const QModelIndex &index)
Provides string representation of index data.
Definition: JobItem.cpp:33