BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
defaulteditorfactory.test.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/tests/testviewmodel/defaulteditorfactory.test.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 
15 #include "google_test.h"
30 #include "mvvm/model/sessionitem.h"
32 #include "mvvm/utils/reallimits.h"
35 #include "widgetbasedtest.h"
36 #include <QSpinBox>
37 #include <QStandardItemModel>
38 #include <limits>
39 
40 using namespace ModelView;
41 
43 public:
44  DefaultEditorFactoryTest() : m_factory(std::make_unique<DefaultEditorFactory>()) {}
46 
47  //! Helper function to build temporary model and create editor for cell.
48  std::unique_ptr<CustomEditor> createEditor(const QVariant& variant,
50  const std::string& editor_type = {})
51  {
52  // populating model with data
53  SessionModel model;
54  auto propertyItem = model.insertItem<PropertyItem>();
55  propertyItem->setData(variant);
56  if (limits.hasLowerLimit() || limits.hasUpperLimit())
57  propertyItem->setLimits(limits);
58  if (!editor_type.empty())
59  propertyItem->setEditorType(editor_type);
60 
61  // create view model and use index of data cell to create an editor
62  DefaultViewModel viewModel(&model);
63  return m_factory->createEditor(viewModel.index(0, 1));
64  }
65 
66 protected:
67  std::unique_ptr<DefaultEditorFactory> m_factory;
68 };
69 
71 
72 //! Tests editor creation on bool property.
73 
75 {
76  auto editor = createEditor(QVariant::fromValue(true));
77  EXPECT_TRUE(dynamic_cast<BoolEditor*>(editor.get()));
78 }
79 
80 //! Tests editor creation on integer property.
81 
83 {
84  auto editor = createEditor(QVariant::fromValue(42));
85  EXPECT_TRUE(dynamic_cast<IntegerEditor*>(editor.get()));
86 
87  auto spin_box = editor->findChild<QSpinBox*>();
88 
89  ASSERT_TRUE(spin_box != nullptr);
90  EXPECT_EQ(spin_box->minimum(), -65536);
91  EXPECT_EQ(spin_box->maximum(), 65536);
92 }
93 
94 //! Tests editor creation on integer property with limits.
95 
96 TEST_F(DefaultEditorFactoryTest, integerPropertyWithLimits)
97 {
98  auto editor = createEditor(QVariant::fromValue(42), RealLimits::limited(-1, 1));
99  EXPECT_TRUE(dynamic_cast<IntegerEditor*>(editor.get()));
100 
101  auto spin_box = editor->findChild<QSpinBox*>();
102  ASSERT_TRUE(spin_box != nullptr);
103  EXPECT_EQ(spin_box->minimum(), -1);
104  EXPECT_EQ(spin_box->maximum(), 1);
105 }
106 
107 //! Tests editor creation on double property.
108 
110 {
111  auto editor = createEditor(QVariant::fromValue(42.42));
112  EXPECT_TRUE(dynamic_cast<ScientificSpinBoxEditor*>(editor.get()));
113 
114  auto spin_box = editor->findChild<ScientificSpinBox*>();
115  ASSERT_TRUE(spin_box != nullptr);
116  EXPECT_FLOAT_EQ(spin_box->minimum(), -std::numeric_limits<double>::max());
117  EXPECT_FLOAT_EQ(spin_box->maximum(), std::numeric_limits<double>::max());
118 }
119 
120 //! Tests editor creation on double property with limits.
121 
122 TEST_F(DefaultEditorFactoryTest, doublePropertyWithLimits)
123 {
124  auto editor = createEditor(QVariant::fromValue(42.42), RealLimits::limited(41, 43));
125  EXPECT_TRUE(dynamic_cast<ScientificSpinBoxEditor*>(editor.get()));
126 
127  auto spin_box = editor->findChild<ScientificSpinBox*>();
128  ASSERT_TRUE(spin_box != nullptr);
129  EXPECT_FLOAT_EQ(spin_box->minimum(), 41);
130  EXPECT_FLOAT_EQ(spin_box->maximum(), 43);
131 }
132 
133 //! Tests editor creation on color property.
134 
136 {
137  auto editor = createEditor(QVariant::fromValue(QColor(Qt::green)));
138  EXPECT_TRUE(dynamic_cast<ColorEditor*>(editor.get()));
139 }
140 
141 //! Tests editor creation on combo property.
142 
144 {
145  auto editor = createEditor(QVariant::fromValue(ComboProperty()));
146  EXPECT_TRUE(dynamic_cast<ComboPropertyEditor*>(editor.get()));
147 }
148 
149 //! Tests editor creation on combo property.
150 
152 {
153  auto editor = createEditor(QVariant::fromValue(ExternalProperty()));
154  EXPECT_TRUE(dynamic_cast<ExternalPropertyEditor*>(editor.get()));
155 }
156 
157 //! Tests editor creation on some unsupported property.
158 
159 TEST_F(DefaultEditorFactoryTest, unsupportedProperty)
160 {
161  // no dedicated editor for std::string yet
162  auto editor = createEditor(QVariant::fromValue(std::string("text")));
163  EXPECT_EQ(editor.get(), nullptr);
164 
165  // no editor for RealLimits
166  editor = createEditor(QVariant::fromValue(RealLimits::limited(1.0, 2.0)));
167  EXPECT_EQ(editor.get(), nullptr);
168 
169  // no editor for invalid variant
170  editor = createEditor(QVariant());
171  EXPECT_EQ(editor.get(), nullptr);
172 
173  // special case of invalid index
174  EXPECT_EQ(m_factory->createEditor(QModelIndex()), nullptr);
175 }
176 
177 //! Create test editor using EDITOR role.
178 
180 {
181  auto editor = createEditor(QVariant::fromValue(ComboProperty()), RealLimits(),
183  EXPECT_TRUE(dynamic_cast<SelectableComboBoxEditor*>(editor.get()));
184 }
185 
186 //! Tests editor creation on combo property in QStandardItemModel with our variant.
187 
188 TEST_F(DefaultEditorFactoryTest, comboPropertyInStandardModel)
189 {
190  QStandardItemModel model;
191  auto parent = model.invisibleRootItem();
192  QList<QStandardItem*> children{new QStandardItem};
193  parent->appendRow(children);
194 
195  auto item = model.item(0, 0);
196  item->setData(QVariant::fromValue(ComboProperty()), Qt::EditRole);
197 
198  auto editor = m_factory->createEditor(model.index(0, 0));
199  EXPECT_TRUE(dynamic_cast<ComboPropertyEditor*>(editor.get()));
200 }
Defines class CLASS?
std::unique_ptr< DefaultEditorFactory > m_factory
std::unique_ptr< CustomEditor > createEditor(const QVariant &variant, RealLimits limits=RealLimits::limitless(), const std::string &editor_type={})
Helper function to build temporary model and create editor for cell.
Custom editor for QVariant based on bool values.
Definition: booleditor.h:26
Custom editor for QVariant based on QColor.
Definition: coloreditor.h:28
Custom editor for QVariant based on ComboProperty.
Custom property to define list of string values with multiple selections.
Definition: comboproperty.h:27
Default editor factory for cell editors in Qt trees and tables.
View model to show content of SessionModel in Qt widgets: two column tree with label/data.
Custom editor for QVariant based on ExternalProperty.
Property to carry text, color and identifier.
Custom editor for QVariant based on integer with possibility to set limits.
Definition: integereditor.h:26
Item to carry concrete editable entity (e.g.
Definition: propertyitem.h:27
Limits for double.
Definition: reallimits.h:25
static RealLimits limitless()
Creates an object withoud bounds (default)
Definition: reallimits.cpp:66
static RealLimits limited(double left_bound_value, double right_bound_value)
Creates an object bounded from the left and right.
Definition: reallimits.cpp:61
Custom editor for QVariant based on double with scientific notation support.
Adds multi-selection capabilities to QComboBox.
bool setData(const T &value, int role=ItemDataRole::DATA, bool direct=false)
Sets data for a given role.
Definition: sessionitem.h:141
Main class to hold hierarchy of SessionItem objects.
Definition: sessionmodel.h:37
bool setData(SessionItem *item, const Variant &value, int role)
Sets the data for given item.
T * insertItem(SessionItem *parent=nullptr, const TagRow &tagrow={})
Inserts item into given parent under given tagrow.
Definition: sessionmodel.h:104
Convenience class to setup QApplication for tests involving QWidget creation.
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
TEST_F(DefaultEditorFactoryTest, boolProperty)
Tests editor creation on bool property.
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
ExternalProperty colorProperty(const QColor &color)
Constructs color property from given color.
const std::string SelectableComboPropertyEditorType
materialitems.h Collection of materials to populate MaterialModel.
Definition: filesystem.h:81
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?