BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
ModelView::ComboProperty Class Reference

Custom property to define list of string values with multiple selections. More...

Public Member Functions

 ComboProperty ()
 
int currentIndex () const
 
std::string label () const
 Returns the label to show. More...
 
bool operator!= (const ComboProperty &other) const
 
bool operator< (const ComboProperty &other) const
 
ComboPropertyoperator<< (const std::string &str)
 
ComboPropertyoperator<< (const std::vector< std::string > &str)
 
bool operator== (const ComboProperty &other) const
 
std::vector< int > selectedIndices () const
 Returns vector of selected indices. More...
 
std::vector< std::string > selectedValues () const
 Returns list of string with selected values;. More...
 
void setCurrentIndex (int index)
 
void setSelected (const std::string &name, bool value=true)
 
void setSelected (int index, bool value=true)
 Sets given index selection flag. More...
 
void setStringOfSelections (const std::string &values)
 Sets selected indices from string. More...
 
void setStringOfValues (const std::string &values)
 Sets values from the string containing delimeter ';'. More...
 
void setToolTips (const std::vector< std::string > &tooltips)
 
void setValue (const std::string &name)
 
void setValues (const std::vector< std::string > &values)
 Sets new list of values. Current value will be preserved, if exists in a new list. More...
 
std::string stringOfSelections () const
 Return string with coma separated list of selected indices. More...
 
std::string stringOfValues () const
 Returns a single string containing values delimited with ';'. More...
 
std::vector< std::string > toolTips () const
 returns list of tool tips for all values More...
 
std::string value () const
 
std::vector< std::string > values () const
 

Static Public Member Functions

static ComboProperty createFrom (const std::vector< std::string > &values, const std::string &current_value={})
 

Private Member Functions

 ComboProperty (std::vector< std::string > values)
 

Private Attributes

std::vector< int > m_selected_indices
 
std::vector< std::string > m_tooltips
 
std::vector< std::string > m_values
 

Detailed Description

Custom property to define list of string values with multiple selections.

Definition at line 27 of file comboproperty.h.

Constructor & Destructor Documentation

◆ ComboProperty() [1/2]

ComboProperty::ComboProperty ( )
default

◆ ComboProperty() [2/2]

ComboProperty::ComboProperty ( std::vector< std::string >  values)
private

Definition at line 59 of file comboproperty.cpp.

59 : m_values(std::move(values)) {}
std::vector< std::string > m_values
Definition: comboproperty.h:69
std::vector< std::string > values() const

Member Function Documentation

◆ createFrom()

ComboProperty ComboProperty::createFrom ( const std::vector< std::string > &  values,
const std::string &  current_value = {} 
)
static

Definition at line 61 of file comboproperty.cpp.

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 }
Custom property to define list of string values with multiple selections.
Definition: comboproperty.h:27

References setCurrentIndex(), setValue(), and values().

Referenced by ToyItems::LatticeItem::LatticeItem(), and TEST_F().

Here is the call graph for this function:

◆ currentIndex()

int ComboProperty::currentIndex ( ) const

Definition at line 116 of file comboproperty.cpp.

117 {
118  return m_selected_indices.empty() ? -1 : m_selected_indices.at(0);
119 }
std::vector< int > m_selected_indices
Definition: comboproperty.h:71

References m_selected_indices.

Referenced by ModelView::ComboPropertyEditor::internIndex(), operator<<(), TEST_F(), and value().

◆ label()

std::string ComboProperty::label ( ) const

Returns the label to show.

Definition at line 248 of file comboproperty.cpp.

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 }
std::string value() const

References m_selected_indices, and value().

Referenced by TEST_F(), and ModelView::SelectableComboBoxEditor::updateBoxLabel().

Here is the call graph for this function:

◆ operator!=()

bool ComboProperty::operator!= ( const ComboProperty other) const

Definition at line 155 of file comboproperty.cpp.

156 {
157  return !(*this == other);
158 }

◆ operator<()

bool ComboProperty::operator< ( const ComboProperty other) const

Definition at line 160 of file comboproperty.cpp.

161 {
162  return m_selected_indices < other.m_selected_indices && m_values < other.m_values;
163 }

References m_selected_indices, and m_values.

◆ operator<<() [1/2]

ComboProperty & ComboProperty::operator<< ( const std::string &  str)

Definition at line 130 of file comboproperty.cpp.

131 {
132  m_values.push_back(str);
133  if (currentIndex() == -1)
134  setCurrentIndex(0);
135  return *this;
136 }
void setCurrentIndex(int index)

References currentIndex(), m_values, and setCurrentIndex().

Here is the call graph for this function:

◆ operator<<() [2/2]

ComboProperty & ComboProperty::operator<< ( const std::vector< std::string > &  str)

Definition at line 138 of file comboproperty.cpp.

139 {
140  m_values.insert(m_values.end(), str.begin(), str.end());
141  if (currentIndex() == -1)
142  setCurrentIndex(0);
143  return *this;
144 }

References currentIndex(), m_values, and setCurrentIndex().

Here is the call graph for this function:

◆ operator==()

bool ComboProperty::operator== ( const ComboProperty other) const

Definition at line 146 of file comboproperty.cpp.

147 {
149  return false;
150  if (m_values != other.m_values)
151  return false;
152  return true;
153 }

References m_selected_indices, and m_values.

◆ selectedIndices()

std::vector< int > ComboProperty::selectedIndices ( ) const

Returns vector of selected indices.

Definition at line 183 of file comboproperty.cpp.

184 {
185  return m_selected_indices;
186 }

References m_selected_indices.

Referenced by TEST_F().

◆ selectedValues()

std::vector< std::string > ComboProperty::selectedValues ( ) const

Returns list of string with selected values;.

Definition at line 190 of file comboproperty.cpp.

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 }

References m_selected_indices, and m_values.

Referenced by TEST_F().

◆ setCurrentIndex()

void ComboProperty::setCurrentIndex ( int  index)

Definition at line 121 of file comboproperty.cpp.

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 }

References m_selected_indices, and m_values.

Referenced by createFrom(), ModelView::GroupItem::init_group(), ModelView::ComboPropertyEditor::onIndexChanged(), operator<<(), ModelView::PenItem::setSelected(), setStringOfValues(), setValue(), setValues(), and TEST_F().

◆ setSelected() [1/2]

void ComboProperty::setSelected ( const std::string &  name,
bool  value = true 
)

Definition at line 217 of file comboproperty.cpp.

218 {
220 }
void setSelected(int index, bool value=true)
Sets given index selection flag.
int IndexOfItem(It begin, It end, const T &item)
Returns index corresponding to the first occurance of the item in the container.
QString const & name(EShape k)
Definition: particles.cpp:21

References ModelView::Utils::IndexOfItem(), m_values, RealSpace::Particles::name(), setSelected(), and value().

Here is the call graph for this function:

◆ setSelected() [2/2]

void ComboProperty::setSelected ( int  index,
bool  value = true 
)

Sets given index selection flag.

If false, index will be excluded from selection.

Definition at line 201 of file comboproperty.cpp.

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 }

References m_selected_indices, m_values, and value().

Referenced by ModelView::SelectableComboBoxEditor::onModelDataChanged(), setSelected(), setStringOfSelections(), and TEST_F().

Here is the call graph for this function:

◆ setStringOfSelections()

void ComboProperty::setStringOfSelections ( const std::string &  values)

Sets selected indices from string.

Definition at line 234 of file comboproperty.cpp.

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 }

References m_selected_indices, setSelected(), and values().

Referenced by TEST_F().

Here is the call graph for this function:

◆ setStringOfValues()

void ComboProperty::setStringOfValues ( const std::string &  values)

Sets values from the string containing delimeter ';'.

Definition at line 174 of file comboproperty.cpp.

175 {
176  auto current = value();
177  m_values = tokenize(values, value_separator);
179 }
bool Contains(const A &container, const B &element)
Returns true if container contains a given element.

References ModelView::Utils::Contains(), ModelView::Utils::IndexOfItem(), m_values, setCurrentIndex(), value(), and values().

Referenced by TEST_F().

Here is the call graph for this function:

◆ setToolTips()

void ComboProperty::setToolTips ( const std::vector< std::string > &  tooltips)

Definition at line 111 of file comboproperty.cpp.

112 {
113  m_tooltips = tooltips;
114 }
std::vector< std::string > m_tooltips
Definition: comboproperty.h:70

References m_tooltips.

◆ setValue()

void ComboProperty::setValue ( const std::string &  name)

Definition at line 79 of file comboproperty.cpp.

80 {
82  throw std::runtime_error("ComboProperty::setValue() -> Error. Combo doesn't contain "
83  "value "
84  + name);
86 }

References ModelView::Utils::Contains(), ModelView::Utils::IndexOfItem(), m_values, RealSpace::Particles::name(), and setCurrentIndex().

Referenced by createFrom(), and TEST_F().

Here is the call graph for this function:

◆ setValues()

void ComboProperty::setValues ( const std::vector< std::string > &  values)

Sets new list of values. Current value will be preserved, if exists in a new list.

Definition at line 95 of file comboproperty.cpp.

96 {
97  if (values.empty())
98  return;
99 
100  auto current = value();
101  m_values = values;
103 }

References ModelView::Utils::Contains(), ModelView::Utils::IndexOfItem(), m_values, setCurrentIndex(), value(), and values().

Referenced by ModelView::GroupItem::init_group(), and TEST_F().

Here is the call graph for this function:

◆ stringOfSelections()

std::string ComboProperty::stringOfSelections ( ) const

Return string with coma separated list of selected indices.

Definition at line 224 of file comboproperty.cpp.

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 }
std::string toString(PyObject *obj)
Converts PyObject into string, if possible, or throws exception.
Definition: PyUtils.cpp:24

References m_selected_indices, and PyUtils::toString().

Referenced by TEST_F().

Here is the call graph for this function:

◆ stringOfValues()

std::string ComboProperty::stringOfValues ( ) const

Returns a single string containing values delimited with ';'.

Definition at line 167 of file comboproperty.cpp.

168 {
169  return toString(m_values, value_separator);
170 }

References m_values, and PyUtils::toString().

Referenced by TEST_F().

Here is the call graph for this function:

◆ toolTips()

std::vector< std::string > ComboProperty::toolTips ( ) const

returns list of tool tips for all values

Definition at line 106 of file comboproperty.cpp.

107 {
108  return m_tooltips;
109 }

References m_tooltips.

Referenced by TEST_F().

◆ value()

std::string ComboProperty::value ( ) const

Definition at line 74 of file comboproperty.cpp.

75 {
76  return currentIndex() < 0 ? std::string() : m_values.at(static_cast<size_t>(currentIndex()));
77 }

References currentIndex(), and m_values.

Referenced by ModelView::DefaultCellDecorator::cellText(), label(), setSelected(), setStringOfValues(), setValues(), and TEST_F().

Here is the call graph for this function:

◆ values()

std::vector< std::string > ComboProperty::values ( ) const

Member Data Documentation

◆ m_selected_indices

std::vector<int> ModelView::ComboProperty::m_selected_indices
private

◆ m_tooltips

std::vector<std::string> ModelView::ComboProperty::m_tooltips
private

Definition at line 70 of file comboproperty.h.

Referenced by setToolTips(), and toolTips().

◆ m_values

std::vector<std::string> ModelView::ComboProperty::m_values
private

The documentation for this class was generated from the following files: