BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
SessionItemTags.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/Support/Data/SessionItemTags.cpp
6 //! @brief Implement class SessionItemTags
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 "Base/Util/Assert.h"
17 
18 //! Register tag with given parameters. Returns true in case of success. Returns
19 //! false if parameters are invalid or such tag was already registered.
20 
21 bool SessionItemTags::registerTag(const QString& name, int min, int max,
22  const QStringList& modelTypes)
23 {
24  if (min < 0 || (min > max && max >= 0))
25  return false;
26  if (name.isEmpty() || isValid(name))
27  return false;
28  m_tags.push_back({name, min, max, 0, modelTypes});
29  return true;
30 }
31 
32 //! Returns true if there is a registered tag with such name.
33 //! If modelType is not empty, than an additional check is performed if tag is
34 //! intended for the given modelType.
35 
36 bool SessionItemTags::isValid(const QString& tagName, const QString& modelType) const
37 {
38  for (const auto& tag : m_tags) {
39  if (tag.name == tagName) {
40  if (modelType.isEmpty())
41  return true;
42  return tag.modelTypes.isEmpty() ? true : tag.modelTypes.contains(modelType);
43  }
44  }
45  return false;
46 }
47 
48 //! Returns list of modelTypes the given tagName is intended for.
49 
50 QStringList SessionItemTags::modelTypesForTag(const QString& tagName) const
51 {
52  return isValid(tagName) ? tagInfo(tagName).modelTypes : QStringList();
53 }
54 
55 //! Returns start index of given tagName corresponding to the index of SessionItem's m_children.
56 
57 int SessionItemTags::tagStartIndex(const QString& tagName) const
58 {
59  int index(0);
60  for (const auto& tag : m_tags) {
61  if (tag.name == tagName)
62  return index;
63  index += tag.childCount;
64  }
65  ASSERT(0);
66 }
67 
68 //! Returns index in SessionItem's m_children corresponding to given row in tagName.
69 
70 int SessionItemTags::indexFromTagRow(const QString& tagName, int row) const
71 {
72  ASSERT(row >= 0 && row < tagInfo(tagName).childCount);
73  return tagStartIndex(tagName) + row;
74 }
75 
76 //! Returns index in SessionItem's m_children to insert new item.
77 //! If number of items for given tagName exceeds maximum allowed, returns -1;
78 
79 int SessionItemTags::insertIndexFromTagRow(const QString& tagName, int row)
80 {
81  if (maximumReached(tagName))
82  return -1;
83  auto& tag = tagInfo(tagName);
84  if (row > tag.childCount)
85  return -1;
86  if (row < 0)
87  row = tag.childCount;
88  return tagStartIndex(tagName) + row;
89 }
90 
91 QString SessionItemTags::tagFromIndex(int index) const
92 {
93  if (index < 0)
94  return "";
95  for (const auto& tag : m_tags) {
96  if (index < tag.childCount)
97  return tag.name;
98  index -= tag.childCount;
99  }
100  return "";
101 }
102 
103 int SessionItemTags::childCount(const QString& tagName)
104 {
105  return tagInfo(tagName).childCount;
106 }
107 
108 int SessionItemTags::childMax(const QString& tagName)
109 {
110  return tagInfo(tagName).max;
111 }
112 
113 void SessionItemTags::addChild(const QString& tagName)
114 {
115  ASSERT(!maximumReached(tagName));
116  tagInfo(tagName).childCount++;
117 }
118 
119 void SessionItemTags::removeChild(const QString& tagName)
120 {
121  auto& tag = tagInfo(tagName);
122  ASSERT(tag.childCount > 0);
123  tag.childCount--;
124 }
125 
126 bool SessionItemTags::isSingleItemTag(const QString& tagName) const
127 {
128  if (!isValid(tagName))
129  return false;
130  const auto& tag = tagInfo(tagName);
131  return tag.min == 1 && tag.max == 1 && tag.childCount == 1;
132 }
133 
135 {
136  return const_cast<TagInfo&>(static_cast<const SessionItemTags*>(this)->tagInfo(tagName));
137 }
138 
139 const SessionItemTags::TagInfo& SessionItemTags::tagInfo(const QString& tagName) const
140 {
141  for (const auto& tag : m_tags)
142  if (tag.name == tagName)
143  return tag;
144  ASSERT(0);
145 }
146 
147 bool SessionItemTags::maximumReached(const QString& tagName) const
148 {
149  const auto& tag = tagInfo(tagName);
150  return tag.max != -1 && tag.max == tag.childCount;
151 }
Defines class SessionItemTags.
Holds all tag info for SessionItem.
TagInfo & tagInfo(const QString &tagName)
int childMax(const QString &tagName)
bool registerTag(const QString &name, int min, int max, const QStringList &modelTypes)
Register tag with given parameters. Returns true in case of success. Returns false if parameters are ...
QString tagFromIndex(int index) const
int indexFromTagRow(const QString &tagName, int row) const
Returns index in SessionItem's m_children corresponding to given row in tagName.
QVector< TagInfo > m_tags
bool isSingleItemTag(const QString &tagName) const
void addChild(const QString &tagName)
void removeChild(const QString &tagName)
int insertIndexFromTagRow(const QString &tagName, int row)
Returns index in SessionItem's m_children to insert new item. If number of items for given tagName ex...
int childCount(const QString &tagName)
int tagStartIndex(const QString &tagName) const
Returns start index of given tagName corresponding to the index of SessionItem's m_children.
bool isValid(const QString &tagName, const QString &modelType="") const
Returns true if there is a registered tag with such name. If modelType is not empty,...
QStringList modelTypesForTag(const QString &tagName) const
Returns list of modelTypes the given tagName is intended for.
bool maximumReached(const QString &tagName) const
QString const & name(EShape k)
Definition: particles.cpp:20