BornAgain  1.19.0
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/coregui/Views/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 
23 #include <QLabel>
24 #include <QLineEdit>
25 #include <QSpinBox>
26 #include <limits>
27 
28 namespace {
29 QWidget* createCustomStringEditor(const SessionItem& item);
30 double getStep(double val);
31 
32 bool isDoubleProperty(const QVariant& variant)
33 {
34  return variant.type() == QVariant::Double;
35 }
36 
37 bool isIntProperty(const QVariant& variant)
38 {
39  return variant.type() == QVariant::Int;
40 }
41 
42 bool isExternalProperty(const QVariant& variant)
43 {
44  return variant.canConvert<ExternalProperty>();
45 }
46 
47 bool isComboProperty(const QVariant& variant)
48 {
49  return variant.canConvert<ComboProperty>();
50 }
51 
52 bool isStringProperty(const QVariant& variant)
53 {
54  return variant.type() == QVariant::String;
55 }
56 
57 bool isBoolProperty(const QVariant& variant)
58 {
59  return variant.type() == QVariant::Bool;
60 }
61 
62 } // namespace
63 
64 bool PropertyEditorFactory::hasStringRepresentation(const QModelIndex& index)
65 {
66  auto variant = index.data();
67  if (isExternalProperty(variant))
68  return true;
69  if (isComboProperty(variant))
70  return true;
71  if (isBoolProperty(variant))
72  return true;
73  if (isDoubleProperty(variant) && index.internalPointer())
74  return true;
75 
76  return false;
77 }
78 
79 QString PropertyEditorFactory::toString(const QModelIndex& index)
80 {
81  auto variant = index.data();
82  if (isExternalProperty(variant))
83  return variant.value<ExternalProperty>().text();
84  if (isComboProperty(variant))
85  return variant.value<ComboProperty>().label();
86  if (isBoolProperty(variant))
87  return variant.toBool() ? "True" : "False";
88 
89  if (isDoubleProperty(variant) && index.internalPointer()) {
90  auto item = static_cast<SessionItem*>(index.internalPointer());
91  return item->editorType() == "ScientificDouble"
92  ? QString::number(item->value().toDouble(), 'g')
93  : item->editorType() == "ScientificSpinBox"
94  ? ScientificSpinBox::toString(item->value().toDouble(), item->decimals())
95  : QString::number(item->value().toDouble(), 'f', item->decimals());
96  }
97 
98  return "";
99 }
100 
101 QWidget* PropertyEditorFactory::CreateEditor(const SessionItem& item, QWidget* parent)
102 {
103  QWidget* result(nullptr);
104 
105  if (isDoubleProperty(item.value())) {
106  if (item.editorType() == "ScientificDouble") {
107  auto editor = new ScientificDoublePropertyEditor;
108  auto limits = item.limits();
109  editor->setLimits(limits);
110  result = editor;
111  } else if (item.editorType() == "ScientificSpinBox") {
112  auto editor = new ScientificSpinBoxEditor;
113  auto limits = item.limits();
114  editor->setLimits(limits);
115  editor->setDecimals(item.decimals());
116  editor->setSingleStep(getStep(item.roleProperty(Qt::EditRole).toDouble()));
117  result = editor;
118  } else {
119  auto editor = new DoubleEditor;
120  editor->setLimits(item.limits());
121  editor->setDecimals(item.decimals());
122  result = editor;
123  }
124  } else if (isIntProperty(item.value())) {
125  auto editor = new IntEditor;
126  editor->setLimits(item.limits());
127  result = editor;
128  } else if (isBoolProperty(item.value())) {
129  auto editor = new BoolEditor;
130  result = editor;
131  } else if (isStringProperty(item.value())) {
132  result = createCustomStringEditor(item);
133  } else if (isExternalProperty(item.value())) {
134  auto editor = new ExternalPropertyEditor;
135  if (item.editorType() != "Default")
136  editor->setExternalDialogType(item.editorType());
137  result = editor;
138  } else if (isComboProperty(item.value())) {
139  if (item.editorType() == "Default") {
140  auto editor = new ComboPropertyEditor;
141  result = editor;
142  } else if (item.editorType() == "MultiSelectionComboEditor") {
143  auto editor = new MultiComboPropertyEditor;
144  result = editor;
145  }
146  }
147  if (parent && result)
148  result->setParent(parent);
149 
150  return result;
151 }
152 
153 namespace {
154 
155 QWidget* createCustomStringEditor(const SessionItem& item)
156 {
157  QWidget* result(nullptr);
158 
159  if (item.isEditable()) {
160  auto editor = new QLineEdit;
161  editor->setText(item.value().toString());
162  result = editor;
163  } else {
164  auto editor = new QLabel;
165  editor->setText(item.value().toString());
166  result = editor;
167  }
168 
169  return result;
170 }
171 
172 double getStep(double val)
173 {
174  return val == 0.0 ? 1.0 : val / 100.;
175 }
176 
177 } // namespace
Defines class ComboProperty.
Defines classes releted to event filtering.
Defines class ExternalProperty.
Defines class GroupItemController.
Defines MultiComboPropertyEditor class.
Defines PropertyEditorFactory namespace.
Defines class ScientificSpinBox.
Defines class SessionItem.
Editor for ComboProperty variant.
Definition: CustomEditors.h:72
Custom property to define list of string values with multiple selections.
Definition: ComboProperty.h:25
Editor for Double variant.
void setLimits(const RealLimits &limits)
Editor for ExternalProperty variant.
Definition: CustomEditors.h:50
void setExternalDialogType(const QString &dialogType)
The ExternalProperty class defines custom QVariant property to carry the text, color and an identifie...
Editor for Int variant.
void setLimits(const RealLimits &limits)
Provides custom editor for ComboProperty with multi-select option.
void setLimits(double xmin, double xmax)
Sets lower and upper limits.
Definition: RealLimits.cpp:84
Editor for ScientificDoubleProperty variant.
Definition: CustomEditors.h:95
Editor for Double variant using ScientificSpinBox.
static QString toString(double val, int decimal_points)
int decimals() const
QVariant value() const
Get value.
QString editorType() const
bool isEditable() const
QVariant roleProperty(int role) const
Returns corresponding variant under given role, invalid variant when role is not present.
RealLimits limits() const
bool hasStringRepresentation(const QModelIndex &index)
Returns true if the index data has known (custom) convertion to string.
QString toString(const QModelIndex &index)
Provides string representation of index data.
QWidget * CreateEditor(const SessionItem &item, QWidget *parent=nullptr)
Creates an editor suitable for editing of item.value()