BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
ComboProperty.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/Util/ComboProperty.cpp
6 //! @brief Implements class ComboProperty
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/Util/ComboProperty.h"
16 #include "Base/Util/Assert.h"
17 #include "GUI/Util/Error.h"
18 
19 namespace {
20 
21 const QString value_separator = ";";
22 const QString selection_separator = ",";
23 
24 } // namespace
25 
27 
28 ComboProperty::ComboProperty(QStringList values)
29  : m_values(std::move(values))
30 {
31 }
32 
33 ComboProperty ComboProperty::fromList(const QStringList& values, const QString& current_value)
34 {
35  ComboProperty result(values);
36 
37  if (!current_value.isEmpty())
38  result.setValue(current_value);
39 
40  return result;
41 }
42 
43 QString ComboProperty::getValue() const
44 {
45  return currentIndex() < 0 ? QString() : m_values.at(currentIndex());
46 }
47 
48 void ComboProperty::setValue(const QString& name)
49 {
50  if (!m_values.contains(name))
51  throw Error("ComboProperty::setValue() -> Error. Combo doesn't contain "
52  "value "
53  + name);
54  setCurrentIndex(m_values.indexOf(name));
55 }
56 
57 QStringList ComboProperty::getValues() const
58 {
59  return m_values;
60 }
61 
62 //! Sets new list of values. Current value will be preserved, if exists in a new list.
63 
64 void ComboProperty::setValues(const QStringList& values)
65 {
66  ASSERT(values.size());
67  QString current = getValue();
68  m_values = values;
69  setCurrentIndex(m_values.contains(current) ? m_values.indexOf(current) : 0);
70 }
71 
72 //! Returns list of tool tips for all values
73 QStringList ComboProperty::toolTips() const
74 {
75  return m_tooltips;
76 }
77 
78 void ComboProperty::setToolTips(const QStringList& tooltips)
79 {
80  m_tooltips = tooltips;
81 }
82 
84 {
85  return m_selected_indices.empty() ? -1 : m_selected_indices.at(0);
86 }
87 
89 {
90  if (index < 0 || index >= m_values.size())
91  throw Error("ComboProperty::setCurrentIndex(int index) -> Error. "
92  "Invalid index");
93  m_selected_indices.clear();
94  m_selected_indices.push_back(index);
95 }
96 
98 {
99  m_values.append(str);
100  if (!m_values.empty())
101  setCurrentIndex(0);
102  return *this;
103 }
104 
105 ComboProperty& ComboProperty::operator<<(const QStringList& str)
106 {
107  m_values.append(str);
108  if (!m_values.empty())
109  setCurrentIndex(0);
110  return *this;
111 }
112 
113 bool ComboProperty::operator==(const ComboProperty& other) const
114 {
115  if (m_selected_indices != other.m_selected_indices)
116  return false;
117  if (m_values != other.m_values)
118  return false;
119  return true;
120 }
121 
122 bool ComboProperty::operator!=(const ComboProperty& other) const
123 {
124  return !(*this == other);
125 }
126 
127 bool ComboProperty::operator<(const ComboProperty& other) const
128 {
129  return m_selected_indices.size() < other.m_selected_indices.size()
130  && m_values.size() < other.m_values.size();
131 }
132 
133 //! Returns a single string containing values delimited with ';'.
134 
136 {
137  return m_values.join(value_separator);
138 }
139 
140 //! Sets values from the string containing delimeter ';'.
141 
142 void ComboProperty::setStringOfValues(const QString& values)
143 {
144  QString current = getValue();
145  m_values = values.split(value_separator);
146  setCurrentIndex(m_values.contains(current) ? m_values.indexOf(current) : 0);
147 }
148 
149 //! Constructs variant enclosing given ComboProperty.
150 
151 QVariant ComboProperty::variant() const
152 {
153  QVariant result;
154  result.setValue(*this);
155  return result;
156 }
157 
158 //! Returns vector of selected indices.
159 
160 QVector<int> ComboProperty::selectedIndices() const
161 {
162  return m_selected_indices;
163 }
164 
165 //! Returns list of string with selected values;
166 
167 QStringList ComboProperty::selectedValues() const
168 {
169  QStringList result;
170  for (auto index : m_selected_indices)
171  result.append(m_values.at(index));
172  return result;
173 }
174 
175 //! Sets given index selection flag.
176 //! If false, index will be excluded from selection.
177 
178 void ComboProperty::setSelected(int index, bool value)
179 {
180  if (index < 0 || index >= m_values.size())
181  return;
182 
183  if (value) {
184  if (!m_selected_indices.contains(index))
185  m_selected_indices.push_back(index);
186  } else {
187  m_selected_indices.removeAll(index);
188  }
189  std::sort(m_selected_indices.begin(), m_selected_indices.end());
190 }
191 
192 void ComboProperty::setSelected(const QString& name, bool value)
193 {
194  setSelected(m_values.indexOf(name), value);
195 }
196 
197 //! Return string with coma separated list of selected indices.
198 
200 {
201  QStringList text;
202  for (auto index : m_selected_indices)
203  text.append(QString::number(index));
204  return text.join(selection_separator);
205 }
206 
207 //! Sets selected indices from string.
208 
209 void ComboProperty::setStringOfSelections(const QString& values)
210 {
211  m_selected_indices.clear();
212  if (values.isEmpty())
213  return;
214 
215  for (const auto& str : values.split(selection_separator)) {
216  bool success(false);
217  int num = str.toInt(&success);
218  if (success)
219  setSelected(num, true);
220  }
221 }
222 
223 //! Returns the label to show
224 
225 QString ComboProperty::label() const
226 {
227  if (m_selected_indices.size() > 1)
228  return "Multiple";
229  if (m_selected_indices.size() == 1)
230  return getValue();
231  return "None";
232 }
Defines class ComboProperty.
Defines error class.
@ other
The unit has no enum value defined in here (e.g. when defined as an explicit string)
Custom property to define list of string values with multiple selections. Intended for QVariant.
Definition: ComboProperty.h:25
void setStringOfValues(const QString &values)
Sets values from the string containing delimeter ';'.
QString stringOfSelections() const
Return string with coma separated list of selected indices.
ComboProperty & operator<<(const QString &str)
QStringList m_values
Definition: ComboProperty.h:68
QStringList getValues() const
bool operator==(const ComboProperty &other) const
void setStringOfSelections(const QString &values)
Sets selected indices from string.
bool operator<(const ComboProperty &other) const
QVector< int > selectedIndices() const
Returns vector of selected indices.
static ComboProperty fromList(const QStringList &values, const QString &current_value="")
int currentIndex() const
QVector< int > m_selected_indices
Definition: ComboProperty.h:70
QString stringOfValues() const
Returns a single string containing values delimited with ';'.
QVariant variant() const
Constructs variant enclosing given ComboProperty.
void setSelected(int index, bool value=true)
Sets given index selection flag. If false, index will be excluded from selection.
void setValue(const QString &name)
QString label() const
Returns the label to show.
QString getValue() const
void setToolTips(const QStringList &tooltips)
QStringList m_tooltips
Definition: ComboProperty.h:69
QStringList selectedValues() const
Returns list of string with selected values;.
bool operator!=(const ComboProperty &other) const
QStringList toolTips() const
Returns list of tool tips for all values.
void setCurrentIndex(int index)
void setValues(const QStringList &values)
Sets new list of values. Current value will be preserved, if exists in a new list.
QString const & name(EShape k)
Definition: particles.cpp:20