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