BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
MaskItems.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/Model/Device/MaskItems.cpp
6 //! @brief Implements MaskItems classes
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 
16 #include "Device/Mask/Ellipse.h"
17 #include "Device/Mask/InfinitePlane.h"
18 #include "Device/Mask/Line.h"
19 #include "Device/Mask/Polygon.h"
20 #include "Device/Mask/Rectangle.h"
25 
27  : SessionItem(M_TYPE)
28 {
29  const QString T_MASKS = "Mask Tag";
30  static const QStringList allowedMasks = {RectangleItem::M_TYPE, PolygonItem::M_TYPE,
34  registerTag(T_MASKS, 0, -1, allowedMasks);
35  setDefaultTag(T_MASKS);
36 }
37 
38 QVector<MaskItem*> MaskContainerItem::maskItems() const
39 {
40  QVector<MaskItem*> result;
41  for (auto* child : children())
42  if (auto* mask = dynamic_cast<MaskItem*>(child))
43  result.append(mask);
44 
45  return result;
46 }
47 
48 void MaskContainerItem::insertMask(int row, MaskItem* maskItem)
49 {
50  insertChild(row, maskItem);
51 }
52 
54 {
55  insertChild(-1, maskItem);
56 }
57 
59 {
60  return firstChildOfType<RegionOfInterestItem>();
61 }
62 
64 {
66 }
67 
68 QString MaskItem::maskName() const
69 {
70  return itemName();
71 }
72 
73 void MaskItem::setMaskName(const QString& name)
74 {
76 }
77 
78 /* ------------------------------------------------------------------------- */
79 
80 bool MaskItem::maskValue() const
81 {
82  return getItemValue(P_MASK_VALUE).toBool();
83 }
84 
85 void MaskItem::setMaskValue(const bool mask_value)
86 {
87  setItemValue(P_MASK_VALUE, mask_value);
88 }
89 
91 {
92  return name == P_MASK_VALUE;
93 }
94 
96 {
97  return getItemValue(P_IS_VISIBLE).toBool();
98 }
99 
100 void MaskItem::setIsVisibleValue(const bool visible)
101 {
102  setItemValue(P_IS_VISIBLE, visible);
103 }
104 
106 {
107  return name == P_IS_VISIBLE;
108 }
109 
110 MaskItem::MaskItem(const QString& name)
111  : SessionItem(name)
112 {
113  addProperty(P_MASK_VALUE, true);
114  addProperty(P_IS_VISIBLE, true);
115 }
116 
118 {
119  return getItem(P_MASK_VALUE);
120 }
121 
122 /* ------------------------------------------------------------------------- */
123 
125  : RectangleItem(M_TYPE)
126 {
127 }
128 
129 RectangleItem::RectangleItem(const QString& modelType)
130  : MaskItem(modelType)
131 {
133  addProperty(P_XLOW, 0.0)->setLimits(RealLimits::limitless());
134  addProperty(P_YLOW, 0.0)->setLimits(RealLimits::limitless());
135  addProperty(P_XUP, 0.0)->setLimits(RealLimits::limitless());
136  addProperty(P_YUP, 0.0)->setLimits(RealLimits::limitless());
137 }
138 
139 std::unique_ptr<IShape2D> RectangleItem::createShape(double scale) const
140 {
141  double xlow = scale * getItemValue(P_XLOW).toDouble();
142  double ylow = scale * getItemValue(P_YLOW).toDouble();
143  double xup = scale * getItemValue(P_XUP).toDouble();
144  double yup = scale * getItemValue(P_YUP).toDouble();
145  return std::make_unique<Rectangle>(xlow, ylow, xup, yup);
146 }
147 
149 {
151 }
152 
153 void RectangleItem::setXLow(const double x_low)
154 {
155  setItemValue(P_XLOW, x_low);
156 }
157 
159 {
161 }
162 
163 void RectangleItem::setYLow(const double y_low)
164 {
165  setItemValue(P_YLOW, y_low);
166 }
167 
169 {
171 }
172 
173 void RectangleItem::setXUp(const double x_up)
174 {
175  setItemValue(P_XUP, x_up);
176 }
177 
179 {
181 }
182 
183 void RectangleItem::setYUp(const double y_up)
184 {
185  setItemValue(P_YUP, y_up);
186 }
187 
189 {
190  s.assertVersion(0);
191  Serialize::rwSessionItem<bool>(s, "maskValue", getItem(P_MASK_VALUE));
192  Serialize::rwSessionItem<bool>(s, "visible", getItem(P_IS_VISIBLE));
193  Serialize::rwSessionItem<double>(s, "xlow", getItem(P_XLOW));
194  Serialize::rwSessionItem<double>(s, "ylow", getItem(P_YLOW));
195  Serialize::rwSessionItem<double>(s, "xup", getItem(P_XUP));
196  Serialize::rwSessionItem<double>(s, "yup", getItem(P_YUP));
197 }
198 
199 /* ------------------------------------------------------------------------- */
200 
202  : RectangleItem(M_TYPE)
203 {
204  setMaskValue(false);
205 }
206 
207 std::unique_ptr<IShape2D> RegionOfInterestItem::createShape(double scale) const
208 {
209  auto shape = RectangleItem::createShape(scale);
210  dynamic_cast<Rectangle*>(shape.get())->setInverted(true);
211  return shape;
212 }
213 
214 /* ------------------------------------------------------------------------- */
215 
217  : SessionItem(M_TYPE)
218 {
220  addProperty(P_POSX, 0.0)->setLimits(RealLimits::limitless());
221  addProperty(P_POSY, 0.0)->setLimits(RealLimits::limitless());
222 }
223 
225 {
226  return getItemValue(P_POSX).toDouble();
227 }
228 
229 void PolygonPointItem::setPosX(const double pos_x)
230 {
231  setItemValue(P_POSX, pos_x);
232 }
233 
235 {
236  return getItemValue(P_POSY).toDouble();
237 }
238 
239 void PolygonPointItem::setPosY(const double pos_y)
240 {
241  setItemValue(P_POSY, pos_y);
242 }
243 
245 {
246  s.assertVersion(0);
247  Serialize::rwSessionItem<double>(s, "x", getItem(P_POSX));
248  Serialize::rwSessionItem<double>(s, "y", getItem(P_POSY));
249 }
250 
251 /* ------------------------------------------------------------------------- */
252 
254  : MaskItem(M_TYPE)
255 {
257  const QString T_POINTS = "Point tag";
258  registerTag(T_POINTS, 0, -1, {PolygonPointItem::M_TYPE});
259  setDefaultTag(T_POINTS);
260  addProperty(P_ISCLOSED, false);
261 }
262 
263 std::unique_ptr<IShape2D> PolygonItem::createShape(double scale) const
264 {
265  std::vector<double> x, y;
266  for (auto* item : this->childrenOfType<PolygonPointItem>()) {
267  x.push_back(scale * item->posX());
268  y.push_back(scale * item->posY());
269  }
270  return std::make_unique<Polygon>(x, y);
271 }
272 
274 {
275  return getItemValue(P_ISCLOSED).toBool();
276 }
277 
278 void PolygonItem::setIsClosed(bool closed)
279 {
280  setItemValue(P_ISCLOSED, closed);
281 }
282 
283 
284 QVector<PolygonPointItem*> PolygonItem::points() const
285 {
286  return childrenOfType<PolygonPointItem>();
287 }
288 
289 void PolygonItem::addPoint(double x, double y)
290 {
291  auto* pointItem = new PolygonPointItem();
292  pointItem->setPosX(x);
293  pointItem->setPosY(y);
294  insertChild(-1, pointItem);
295 }
296 
298 {
299  s.assertVersion(0);
300  Serialize::rwSessionItem<bool>(s, "maskValue", getItem(P_MASK_VALUE));
301  Serialize::rwSessionItem<bool>(s, "visible", getItem(P_IS_VISIBLE));
302  Serialize::rwSessionItem<bool>(s, "closed", getItem(P_ISCLOSED));
303 
304  if (s.xmlReader()) {
305  QVector<PolygonPointItem*> pts;
306  Serialize::rwVector(s, "points", pts);
307  for (const auto* point : pts)
308  addPoint(point->posX(), point->posY());
309  qDeleteAll(pts);
310  } else {
311  auto p = points();
312  Serialize::rwVector(s, "points", p);
313  }
314 }
315 
316 /* ------------------------------------------------------------------------- */
317 
319  : MaskItem(M_TYPE)
320 {
322  addProperty(P_POSX, 0.0)->setLimits(RealLimits::limitless());
323 }
324 
325 std::unique_ptr<IShape2D> VerticalLineItem::createShape(double scale) const
326 {
327  return std::make_unique<VerticalLine>(scale
328  * getItemValue(VerticalLineItem::P_POSX).toDouble());
329 }
330 
332 {
334 }
335 
336 void VerticalLineItem::setPosX(double pos_x)
337 {
338  setItemValue(P_POSX, pos_x);
339 }
340 
342 {
343  s.assertVersion(0);
344  Serialize::rwSessionItem<bool>(s, "maskValue", getItem(P_MASK_VALUE));
345  Serialize::rwSessionItem<bool>(s, "visible", getItem(P_IS_VISIBLE));
346  Serialize::rwSessionItem<double>(s, "x", getItem(P_POSX));
347 }
348 
349 /* ------------------------------------------------------------------------- */
350 
352  : MaskItem(M_TYPE)
353 {
355  addProperty(P_POSY, 0.0)->setLimits(RealLimits::limitless());
356 }
357 
358 std::unique_ptr<IShape2D> HorizontalLineItem::createShape(double scale) const
359 {
360  return std::make_unique<HorizontalLine>(scale
362 }
363 
365 {
367 }
368 
369 void HorizontalLineItem::setPosY(double pos_y)
370 {
371  setItemValue(P_POSY, pos_y);
372 }
373 
375 {
376  s.assertVersion(0);
377  Serialize::rwSessionItem<bool>(s, "maskValue", getItem(P_MASK_VALUE));
378  Serialize::rwSessionItem<bool>(s, "visible", getItem(P_IS_VISIBLE));
379  Serialize::rwSessionItem<double>(s, "y", getItem(P_POSY));
380 }
381 
382 /* ------------------------------------------------------------------------- */
383 
385  : MaskItem(M_TYPE)
386 {
388  addProperty(P_XCENTER, 0.0)->setLimits(RealLimits::limitless());
389  addProperty(P_YCENTER, 0.0)->setLimits(RealLimits::limitless());
390  addProperty(P_XRADIUS, 0.0);
391  addProperty(P_YRADIUS, 0.0);
392  addProperty(P_ANGLE, 0.0)->setLimits(RealLimits::limitless());
393 }
394 
395 std::unique_ptr<IShape2D> EllipseItem::createShape(double scale) const
396 {
397  double xcenter = scale * getItemValue(EllipseItem::P_XCENTER).toDouble();
398  double ycenter = scale * getItemValue(EllipseItem::P_YCENTER).toDouble();
399  double xradius = scale * getItemValue(EllipseItem::P_XRADIUS).toDouble();
400  double yradius = scale * getItemValue(EllipseItem::P_YRADIUS).toDouble();
401  double angle = scale * getItemValue(EllipseItem::P_ANGLE).toDouble();
402 
403  return std::make_unique<Ellipse>(xcenter, ycenter, xradius, yradius, angle);
404 }
405 
407 {
409 }
410 
411 void EllipseItem::setXCenter(const double x_center)
412 {
413  setItemValue(P_XCENTER, x_center);
414 }
415 
417 {
419 }
420 
421 void EllipseItem::setYCenter(const double y_center)
422 {
423  setItemValue(P_YCENTER, y_center);
424 }
425 
427 {
429 }
430 
431 void EllipseItem::setXRadius(const double x_radius)
432 {
433  setItemValue(P_XRADIUS, x_radius);
434 }
435 
437 {
439 }
440 
441 void EllipseItem::setYRadius(const double y_radius)
442 {
443  setItemValue(P_YRADIUS, y_radius);
444 }
445 
447 {
449 }
450 
451 void EllipseItem::setAngle(const double angle)
452 {
454 }
455 
457 {
458  s.assertVersion(0);
459  Serialize::rwSessionItem<bool>(s, "maskValue", getItem(P_MASK_VALUE));
460  Serialize::rwSessionItem<bool>(s, "visible", getItem(P_IS_VISIBLE));
461  Serialize::rwSessionItem<double>(s, "x", getItem(P_XCENTER));
462  Serialize::rwSessionItem<double>(s, "y", getItem(P_YCENTER));
463  Serialize::rwSessionItem<double>(s, "xr", getItem(P_XRADIUS));
464  Serialize::rwSessionItem<double>(s, "yr", getItem(P_YRADIUS));
465  Serialize::rwSessionItem<double>(s, "angle", getItem(P_ANGLE));
466 }
467 
468 /* ------------------------------------------------------------------------- */
469 
471  : MaskItem(M_TYPE)
472 {
474  maskValueItem()->setEnabled(false);
475 }
476 
477 std::unique_ptr<IShape2D> MaskAllItem::createShape(double) const
478 {
479  return std::make_unique<InfinitePlane>();
480 }
481 
483 {
484  s.assertVersion(0);
485  Serialize::rwSessionItem<bool>(s, "maskValue", getItem(P_MASK_VALUE));
486  Serialize::rwSessionItem<bool>(s, "visible", getItem(P_IS_VISIBLE));
487 }
488 
489 /* ------------------------------------------------------------------------- */
490 
492  : SessionModel("MaskItems")
493 {
494  m_maskContainer = insertItem<MaskContainerItem>();
495 }
496 
497 void MaskItems::insertMask(int index, MaskItem* maskItem)
498 {
499  m_maskContainer->insertChild(index, maskItem);
500 }
501 
502 QVector<MaskItem*> MaskItems::maskItems() const
503 {
504  return m_maskContainer->maskItems();
505 }
506 
507 void MaskItems::copy(const MaskContainerItem* maskContainer)
508 {
510  if (maskContainer)
511  for (auto mask : maskContainer->maskItems())
512  copyItem(mask, m_maskContainer);
513 }
514 
516 {
518  m_maskContainer = insertItem<MaskContainerItem>();
519 }
520 
522 {
523  QVector<MaskItem*> items;
524  if (!s.xmlReader())
525  items = maskItems();
526 
527  s.assertVersion(0);
528  Serialize::rwCatalogized<MaskItemCatalog>(s, "maskItems", items);
529 
530  if (s.xmlReader())
531  for (auto m : items)
533 }
Defines class DoubleDescriptor.
Defines class MaskItemCatalog.
Defines MaskItems classes.
Defines class Streamer.
Defines class SessionModel.
@ other
The unit has no enum value defined in here (e.g. when defined as an explicit string)
@ degree
Describes properties of a double value which are necessary to allow GUI representation,...
static constexpr auto P_XCENTER
Definition: MaskItems.h:160
std::unique_ptr< IShape2D > createShape(double scale) const override
Definition: MaskItems.cpp:395
static constexpr auto P_XRADIUS
Definition: MaskItems.h:162
void setAngle(double angle)
Definition: MaskItems.cpp:451
DoubleDescriptor yRadius() const
Definition: MaskItems.cpp:436
DoubleDescriptor xRadius() const
Definition: MaskItems.cpp:426
static constexpr auto P_YCENTER
Definition: MaskItems.h:161
static constexpr auto P_ANGLE
Definition: MaskItems.h:164
void setYRadius(double y_radius)
Definition: MaskItems.cpp:441
DoubleDescriptor xCenter() const
Definition: MaskItems.cpp:406
void serialize(Streamer &s) override
Definition: MaskItems.cpp:456
DoubleDescriptor angle() const
Definition: MaskItems.cpp:446
void setYCenter(double y_center)
Definition: MaskItems.cpp:421
void setXCenter(double x_center)
Definition: MaskItems.cpp:411
void setXRadius(double x_radius)
Definition: MaskItems.cpp:431
DoubleDescriptor yCenter() const
Definition: MaskItems.cpp:416
static constexpr auto M_TYPE
Definition: MaskItems.h:167
static constexpr auto P_YRADIUS
Definition: MaskItems.h:163
void setPosY(double pos_y)
Definition: MaskItems.cpp:369
static constexpr auto M_TYPE
Definition: MaskItems.h:147
void serialize(Streamer &s) override
Definition: MaskItems.cpp:374
static constexpr auto P_POSY
Definition: MaskItems.h:144
std::unique_ptr< IShape2D > createShape(double scale) const override
Definition: MaskItems.cpp:358
DoubleDescriptor posY() const
Definition: MaskItems.cpp:364
void serialize(Streamer &s) override
Definition: MaskItems.cpp:482
std::unique_ptr< IShape2D > createShape(double scale) const override
Definition: MaskItems.cpp:477
static constexpr auto M_TYPE
Definition: MaskItems.h:192
Container holding various masks as children.
Definition: MaskItems.h:202
void clear()
Remove all masks.
Definition: MaskItems.cpp:63
QVector< MaskItem * > maskItems() const
Definition: MaskItems.cpp:38
RegionOfInterestItem * regionOfInterestItem() const
Can be nullptr.
Definition: MaskItems.cpp:58
void addMask(MaskItem *maskItem)
Definition: MaskItems.cpp:53
void insertMask(int row, MaskItem *maskItem)
Insert mask at given row.
Definition: MaskItems.cpp:48
A base class for all mask items.
Definition: MaskItems.h:27
static bool isIsVisiblePropertyName(const QString &name)
Definition: MaskItems.cpp:105
MaskItem(const QString &name)
Definition: MaskItems.cpp:110
static bool isMaskValuePropertyName(const QString &name)
Definition: MaskItems.cpp:90
static constexpr auto P_IS_VISIBLE
Definition: MaskItems.h:30
QString maskName() const
Definition: MaskItems.cpp:68
void setMaskValue(bool mask_value)
Definition: MaskItems.cpp:85
void setMaskName(const QString &name)
Definition: MaskItems.cpp:73
void setIsVisibleValue(bool visible)
Definition: MaskItems.cpp:100
static constexpr auto P_MASK_VALUE
Definition: MaskItems.h:29
bool isVisibleValue() const
Definition: MaskItems.cpp:95
bool maskValue() const
Definition: MaskItems.cpp:80
SessionItem * maskValueItem() const
Definition: MaskItems.cpp:117
MaskContainerItem * m_maskContainer
Definition: MaskItems.h:252
void clear()
Removes all maskItems.
Definition: MaskItems.cpp:515
void copy(const MaskContainerItem *maskContainer)
Copy the mask items stored in the given maskContainer.
Definition: MaskItems.cpp:507
void insertMask(int index, MaskItem *maskItem)
Takes ownership of maskItem.
Definition: MaskItems.cpp:497
void serialize(Streamer &s)
Definition: MaskItems.cpp:521
QVector< MaskItem * > maskItems() const
Definition: MaskItems.cpp:502
static constexpr auto P_ISCLOSED
Definition: MaskItems.h:109
void setIsClosed(bool closed)
Definition: MaskItems.cpp:278
void addPoint(double x, double y)
Definition: MaskItems.cpp:289
static constexpr auto M_TYPE
Definition: MaskItems.h:112
QVector< PolygonPointItem * > points() const
Definition: MaskItems.cpp:284
std::unique_ptr< IShape2D > createShape(double scale) const override
Definition: MaskItems.cpp:263
void serialize(Streamer &s) override
Definition: MaskItems.cpp:297
bool isClosed() const
Definition: MaskItems.cpp:273
void serialize(Streamer &s)
Definition: MaskItems.cpp:244
void setPosY(double pos_y)
Definition: MaskItems.cpp:239
static constexpr auto P_POSY
Definition: MaskItems.h:92
void setPosX(double pos_x)
Definition: MaskItems.cpp:229
static constexpr auto P_POSX
Definition: MaskItems.h:91
static constexpr auto M_TYPE
Definition: MaskItems.h:95
double posX() const
Definition: MaskItems.cpp:224
double posY() const
Definition: MaskItems.cpp:234
void serialize(Streamer &s) override
Definition: MaskItems.cpp:188
DoubleDescriptor xLow() const
Definition: MaskItems.cpp:148
static constexpr auto P_XUP
Definition: MaskItems.h:57
DoubleDescriptor yUp() const
Definition: MaskItems.cpp:178
static constexpr auto P_YUP
Definition: MaskItems.h:58
void setXLow(double x_low)
Definition: MaskItems.cpp:153
std::unique_ptr< IShape2D > createShape(double scale) const override
Definition: MaskItems.cpp:139
DoubleDescriptor yLow() const
Definition: MaskItems.cpp:158
DoubleDescriptor xUp() const
Definition: MaskItems.cpp:168
void setXUp(double x_up)
Definition: MaskItems.cpp:173
void setYLow(double y_low)
Definition: MaskItems.cpp:163
void setYUp(double y_up)
Definition: MaskItems.cpp:183
static constexpr auto P_YLOW
Definition: MaskItems.h:56
static constexpr auto P_XLOW
Definition: MaskItems.h:55
static constexpr auto M_TYPE
Definition: MaskItems.h:61
static constexpr auto M_TYPE
Definition: MaskItems.h:83
std::unique_ptr< IShape2D > createShape(double scale) const override
Definition: MaskItems.cpp:207
Base class for a GUI data item.
Definition: SessionItem.h:204
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,...
int numberOfChildren() const
Returns total number of children.
Definition: SessionItem.cpp:88
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.
void setDefaultTag(const QString &tag)
Set default tag.
SessionModel * model() const
Returns model of this item.
Definition: SessionItem.cpp:60
T * item(const QString &tag) const
Definition: SessionItem.h:353
QVector< SessionItem * > children() const
Returns vector of all children.
Definition: SessionItem.cpp:95
QString modelType() const
Get model type.
void setEnabled(bool enabled)
Flags accessors.
QModelIndex index() const
Returns model index of this item.
Definition: SessionItem.cpp:74
SessionItem * getItem(const QString &tag="", int row=0) const
Returns item in given row of given tag.
SessionItem & setLimits(const RealLimits &value)
Base class for a GUI data collection. A collection is e.g. all real data (RealDataModel)....
Definition: SessionModel.h:42
virtual void clear()
QModelIndex index(int row, int column, const QModelIndex &parent) const override
bool removeRows(int row, int count, const QModelIndex &parent) override
T * copyItem(const T *item_to_copy, SessionItem *new_parent=nullptr, const QString &tag="")
Definition: SessionModel.h:149
Supports serialization to or deserialization from QXmlStream.
Definition: Streamer.h:36
QXmlStreamReader * xmlReader()
Returns stream reader or nullptr.
Definition: Streamer.h:48
void assertVersion(unsigned expectedVersion) const
As reader, throws DeserializationException unless the expected version is read. As writer,...
Definition: Streamer.cpp:26
static constexpr auto M_TYPE
Definition: MaskItems.h:131
void serialize(Streamer &s) override
Definition: MaskItems.cpp:341
std::unique_ptr< IShape2D > createShape(double scale) const override
Definition: MaskItems.cpp:325
void setPosX(double pos_x)
Definition: MaskItems.cpp:336
static constexpr auto P_POSX
Definition: MaskItems.h:128
DoubleDescriptor posX() const
Definition: MaskItems.cpp:331
QString const & name(EShape k)
Definition: particles.cpp:20
void rwVector(Streamer &s, const QString &tag, QVector< T > &vec, Args... argsForConstructor)
Serializes a list of items of known and fixed type. Passes optional arguments to the constructor.
Definition: Serialize.h:93