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 // qt-mvvm: Model-view-view-model framework for large GUI applications
4 //
5 //! @file mvvm/model/mvvm/model/comboproperty.cpp
6 //! @brief Implements class CLASS?
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2020
11 //! @authors Gennady Pospelov et al, Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
17 #include <sstream>
18 #include <stdexcept>
19 
20 namespace {
21 const std::string value_separator = ";";
22 const std::string selection_separator = ",";
23 const std::string multiple_label = "Multiple";
24 const std::string none_label = "None";
25 
26 template <typename C, typename T> std::string toString(const C& container, const T& delim)
27 {
28  std::stringstream result;
29  for (auto it = container.begin(); it != container.end(); ++it) {
30  result << *it;
31  if (std::distance(it, container.end()) != 1)
32  result << delim;
33  }
34  return result.str();
35 }
36 
37 std::vector<std::string> tokenize(const std::string& str, const std::string& delimeter)
38 {
39  std::vector<std::string> result;
40  size_t start = str.find_first_not_of(delimeter);
41  size_t end = start;
42 
43  while (start != std::string::npos) {
44  // Find next occurence of delimiter
45  end = str.find(delimeter, start);
46  // Push back the token found into vector
47  result.push_back(str.substr(start, end - start));
48  // Skip all occurences of the delimiter to find new start
49  start = str.find_first_not_of(delimeter, end);
50  }
51  return result;
52 }
53 } // namespace
54 
55 using namespace ModelView;
56 
58 
59 ComboProperty::ComboProperty(std::vector<std::string> values) : m_values(std::move(values)) {}
60 
61 ComboProperty ComboProperty::createFrom(const std::vector<std::string>& values,
62  const std::string& current_value)
63 {
64  ComboProperty result(values);
65 
66  if (!current_value.empty())
67  result.setValue(current_value);
68  else
69  result.setCurrentIndex(0);
70 
71  return result;
72 }
73 
74 std::string ComboProperty::value() const
75 {
76  return currentIndex() < 0 ? std::string() : m_values.at(static_cast<size_t>(currentIndex()));
77 }
78 
79 void ComboProperty::setValue(const std::string& name)
80 {
82  throw std::runtime_error("ComboProperty::setValue() -> Error. Combo doesn't contain "
83  "value "
84  + name);
86 }
87 
88 std::vector<std::string> ComboProperty::values() const
89 {
90  return m_values;
91 }
92 
93 //! Sets new list of values. Current value will be preserved, if exists in a new list.
94 
95 void ComboProperty::setValues(const std::vector<std::string>& values)
96 {
97  if (values.empty())
98  return;
99 
100  auto current = value();
101  m_values = values;
103 }
104 
105 //! returns list of tool tips for all values
106 std::vector<std::string> ComboProperty::toolTips() const
107 {
108  return m_tooltips;
109 }
110 
111 void ComboProperty::setToolTips(const std::vector<std::string>& tooltips)
112 {
113  m_tooltips = tooltips;
114 }
115 
117 {
118  return m_selected_indices.empty() ? -1 : m_selected_indices.at(0);
119 }
120 
122 {
123  if (index < 0 || index >= static_cast<int>(m_values.size()))
124  throw std::runtime_error("ComboProperty::setCurrentIndex(int index) -> Error. "
125  "Invalid index");
126  m_selected_indices.clear();
127  m_selected_indices.push_back(index);
128 }
129 
130 ComboProperty& ComboProperty::operator<<(const std::string& str)
131 {
132  m_values.push_back(str);
133  if (currentIndex() == -1)
134  setCurrentIndex(0);
135  return *this;
136 }
137 
138 ComboProperty& ComboProperty::operator<<(const std::vector<std::string>& str)
139 {
140  m_values.insert(m_values.end(), str.begin(), str.end());
141  if (currentIndex() == -1)
142  setCurrentIndex(0);
143  return *this;
144 }
145 
146 bool ComboProperty::operator==(const ComboProperty& other) const
147 {
149  return false;
150  if (m_values != other.m_values)
151  return false;
152  return true;
153 }
154 
155 bool ComboProperty::operator!=(const ComboProperty& other) const
156 {
157  return !(*this == other);
158 }
159 
160 bool ComboProperty::operator<(const ComboProperty& other) const
161 {
162  return m_selected_indices < other.m_selected_indices && m_values < other.m_values;
163 }
164 
165 //! Returns a single string containing values delimited with ';'.
166 
167 std::string ComboProperty::stringOfValues() const
168 {
169  return toString(m_values, value_separator);
170 }
171 
172 //! Sets values from the string containing delimeter ';'.
173 
174 void ComboProperty::setStringOfValues(const std::string& values)
175 {
176  auto current = value();
177  m_values = tokenize(values, value_separator);
179 }
180 
181 //! Returns vector of selected indices.
182 
183 std::vector<int> ComboProperty::selectedIndices() const
184 {
185  return m_selected_indices;
186 }
187 
188 //! Returns list of string with selected values;
189 
190 std::vector<std::string> ComboProperty::selectedValues() const
191 {
192  std::vector<std::string> result;
193  for (auto index : m_selected_indices)
194  result.push_back(m_values.at(static_cast<size_t>(index)));
195  return result;
196 }
197 
198 //! Sets given index selection flag.
199 //! If false, index will be excluded from selection.
200 
201 void ComboProperty::setSelected(int index, bool value)
202 {
203  if (index < 0 || index >= static_cast<int>(m_values.size()))
204  return;
205 
206  auto pos = find(m_selected_indices.begin(), m_selected_indices.end(), index);
207  if (value) {
208  if (pos == m_selected_indices.end())
209  m_selected_indices.push_back(index);
210  } else {
211  if (pos != m_selected_indices.end())
212  m_selected_indices.erase(pos);
213  }
214  std::sort(m_selected_indices.begin(), m_selected_indices.end());
215 }
216 
217 void ComboProperty::setSelected(const std::string& name, bool value)
218 {
220 }
221 
222 //! Return string with coma separated list of selected indices.
223 
225 {
226  std::vector<std::string> text;
227  for (auto index : m_selected_indices)
228  text.push_back(std::to_string(index));
229  return toString(text, selection_separator);
230 }
231 
232 //! Sets selected indices from string.
233 
234 void ComboProperty::setStringOfSelections(const std::string& values)
235 {
236  m_selected_indices.clear();
237  if (values.empty())
238  return;
239 
240  for (const auto& str : tokenize(values, selection_separator)) {
241  int num = std::stoi(str);
242  setSelected(num, true);
243  }
244 }
245 
246 //! Returns the label to show.
247 
248 std::string ComboProperty::label() const
249 {
250  if (m_selected_indices.size() > 1) {
251  return multiple_label;
252  } else if (m_selected_indices.size() == 1) {
253  return value();
254  } else {
255  return none_label;
256  }
257 }
Custom property to define list of string values with multiple selections.
Definition: comboproperty.h:27
std::vector< std::string > toolTips() const
returns list of tool tips for all values
std::vector< int > selectedIndices() const
Returns vector of selected indices.
std::string label() const
Returns the label to show.
std::vector< std::string > m_tooltips
Definition: comboproperty.h:70
void setStringOfSelections(const std::string &values)
Sets selected indices from string.
bool operator==(const ComboProperty &other) const
bool operator<(const ComboProperty &other) const
std::string stringOfValues() const
Returns a single string containing values delimited with ';'.
void setStringOfValues(const std::string &values)
Sets values from the string containing delimeter ';'.
std::vector< std::string > m_values
Definition: comboproperty.h:69
void setSelected(int index, bool value=true)
Sets given index selection flag.
std::string value() const
void setValue(const std::string &name)
ComboProperty & operator<<(const std::string &str)
std::vector< std::string > values() const
void setValues(const std::vector< std::string > &values)
Sets new list of values. Current value will be preserved, if exists in a new list.
std::string stringOfSelections() const
Return string with coma separated list of selected indices.
void setToolTips(const std::vector< std::string > &tooltips)
bool operator!=(const ComboProperty &other) const
std::vector< std::string > selectedValues() const
Returns list of string with selected values;.
static ComboProperty createFrom(const std::vector< std::string > &values, const std::string &current_value={})
std::vector< int > m_selected_indices
Definition: comboproperty.h:71
void setCurrentIndex(int index)
Defines class CLASS?
Defines class CLASS?
bool Contains(const A &container, const B &element)
Returns true if container contains a given element.
int IndexOfItem(It begin, It end, const T &item)
Returns index corresponding to the first occurance of the item in the container.
materialitems.h Collection of materials to populate MaterialModel.
std::string toString(PyObject *obj)
Converts PyObject into string, if possible, or throws exception.
Definition: PyUtils.cpp:24
QString const & name(EShape k)
Definition: particles.cpp:21
Definition: filesystem.h:81