BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
PropertyEditorFactory.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/View/PropertyEditor/PropertyEditorFactory.cpp
6 //! @brief Implements PropertyEditorFactory namespace
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 
17 #include "GUI/Util/ComboProperty.h"
20 #include <QModelIndex>
21 #include <QSpinBox>
22 
23 namespace {
24 
25 double getStep(double val)
26 {
27  return val == 0.0 ? 1.0 : val / 100.;
28 }
29 
30 bool isDoubleProperty(const QVariant& variant)
31 {
32  return variant.type() == QVariant::Double;
33 }
34 
35 bool isComboProperty(const QVariant& variant)
36 {
37  return variant.canConvert<ComboProperty>();
38 }
39 
40 } // namespace
41 
42 
44 {
45  auto variant = index.data();
46  if (isComboProperty(variant))
47  return true;
48  if (isDoubleProperty(variant) && index.internalPointer())
49  return true;
50 
51  return false;
52 }
53 
54 QString GUI::View::PropertyEditorFactory::toString(const QModelIndex& index)
55 {
56  auto variant = index.data();
57  if (isComboProperty(variant))
58  return variant.value<ComboProperty>().label();
59 
60  if (isDoubleProperty(variant) && index.internalPointer()) {
61  auto* item = static_cast<SessionItem*>(index.internalPointer());
62  // only "Scientific SpinBoxes" in Fit-Window
63  return ScientificSpinBox::toString(item->value().toDouble(), item->decimals());
64  }
65 
66  return "";
67 }
68 
69 QWidget* GUI::View::PropertyEditorFactory::CreateEditor(const SessionItem& item, QWidget* parent)
70 {
71  QWidget* result(nullptr);
72 
73  if (isDoubleProperty(item.value())) {
74  // only Scientific SpinBoxes in Fit-Window
75  auto* editor = new ScientificSpinBoxEditor;
76  editor->setLimits(item.limits());
77  editor->setDecimals(item.decimals());
78  editor->setSingleStep(getStep(item.roleProperty(Qt::EditRole).toDouble()));
79  result = editor;
80  } else if (isComboProperty(item.value()))
81  result = new ComboPropertyEditor;
82 
83  if (parent && result)
84  result->setParent(parent);
85 
86  return result;
87 }
Defines class ComboProperty.
Defines CustomEditors classes.
Defines namespace GUI::View::PropertyEditorFactory.
Defines class ScientificSpinBox.
Defines class SessionItem.
Editor for ComboProperty variant.
Definition: CustomEditors.h:54
Custom property to define list of string values with multiple selections. Intended for QVariant.
Definition: ComboProperty.h:25
Editor for Double variant using ScientificSpinBox.
Definition: CustomEditors.h:77
void setLimits(const RealLimits &limits)
static QString toString(double val, int decimal_points)
Base class for a GUI data item.
Definition: SessionItem.h:204
int decimals() const
QVariant value() const
Get value.
QVariant roleProperty(int role) const
Returns corresponding variant under given role, invalid variant when role is not present.
RealLimits limits() const
QWidget * CreateEditor(const SessionItem &item, QWidget *parent=nullptr)
Creates an editor suitable for editing of item.value()
QString toString(const QModelIndex &index)
Provides string representation of index data.
bool hasStringRepresentation(const QModelIndex &index)
Returns true if the index data has known (custom) conversion to string.