BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
defaulteditorfactory.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/viewmodel/mvvm/editors/defaulteditorfactory.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 
19 #include "mvvm/model/sessionitem.h"
22 
23 using namespace ModelView;
24 
25 namespace {
26 
27 const SessionItem* itemFromIndex(const QModelIndex& index)
28 {
29  auto model = dynamic_cast<const ViewModel*>(index.model());
30  return model ? model->sessionItemFromIndex(index) : nullptr;
31 }
32 
33 } // namespace
34 
35 // ----------------------------------------------------------------------------
36 
39 {
40  m_nameToBuilderMap[name] = std::move(builder);
41 }
42 
44 {
45  auto it = m_nameToBuilderMap.find(name);
46  return it != m_nameToBuilderMap.end() ? it->second : EditorBuilders::builder_t();
47 }
48 
49 // ----------------------------------------------------------------------------
50 
52 {
53  // registering set of builders for given editor types
68 }
69 
70 //! Creates cell editor basing on item role. It is expected that the index belongs to a ViewModel.
71 
72 std::unique_ptr<CustomEditor>
73 RoleDependentEditorFactory::createEditor(const QModelIndex& index) const
74 {
75  auto item = itemFromIndex(index);
76  return item ? createItemEditor(item) : std::unique_ptr<CustomEditor>();
77 }
78 
79 //! Creates cell editor basing on editor type.
80 
81 std::unique_ptr<CustomEditor>
83 {
84  auto builder = findBuilder(item->editorType());
85  return builder ? builder(item) : std::unique_ptr<CustomEditor>();
86 }
87 
88 // ----------------------------------------------------------------------------
89 
91 {
92  // registering set of builders for given variant names
101 }
102 
103 //! Creates cell editor basing on variant name.
104 
105 std::unique_ptr<CustomEditor>
106 VariantDependentEditorFactory::createEditor(const QModelIndex& index) const
107 {
108  auto item = itemFromIndex(index);
109  auto value = item ? item->data<QVariant>() : index.data(Qt::EditRole);
110  auto builder = findBuilder(Utils::VariantName(value));
111  return builder ? builder(item) : std::unique_ptr<CustomEditor>();
112 }
113 
114 // ----------------------------------------------------------------------------
115 
117  : m_roleDependentFactory(std::make_unique<RoleDependentEditorFactory>())
118  , m_variantDependentFactory(std::make_unique<VariantDependentEditorFactory>())
119 {
120 }
121 
122 //! Creates editor for given model index basing either on editorType() or specific variant name.
123 
124 std::unique_ptr<CustomEditor> DefaultEditorFactory::createEditor(const QModelIndex& index) const
125 {
126  // trying to created an editor basing on possibly defined EDITOR role
127  auto editor = m_roleDependentFactory->createEditor(index);
128  // if we do not succeed, then creating editor from variant type
129  return editor ? std::move(editor) : m_variantDependentFactory->createEditor(index);
130 }
EditorBuilders::builder_t findBuilder(const std::string &name) const
std::map< std::string, EditorBuilders::builder_t > m_nameToBuilderMap
void registerBuilder(const std::string &name, EditorBuilders::builder_t builder)
std::unique_ptr< RoleDependentEditorFactory > m_roleDependentFactory
std::unique_ptr< CustomEditor > createEditor(const QModelIndex &index) const override
Creates editor for given model index basing either on editorType() or specific variant name.
std::unique_ptr< VariantDependentEditorFactory > m_variantDependentFactory
Editor factory for cell editors in Qt trees and tables, relies on EDITORTYPE role stored on board of ...
std::unique_ptr< CustomEditor > createEditor(const QModelIndex &index) const override
Creates cell editor basing on item role. It is expected that the index belongs to a ViewModel.
std::unique_ptr< CustomEditor > createItemEditor(const SessionItem *item) const
Creates cell editor basing on editor type.
The main object representing an editable/displayable/serializable entity.
Definition: sessionitem.h:38
std::string editorType() const
Returns editor type.
Editor factory for cell editors in Qt trees and tables, relies on variant type stored as DATA role on...
std::unique_ptr< CustomEditor > createEditor(const QModelIndex &index) const override
Creates cell editor basing on variant name.
Main class to represent content of SessionModel in Qt's trees and tables.
Definition: viewmodel.h:29
SessionItem * sessionItemFromIndex(const QModelIndex &index) const
Definition: viewmodel.cpp:59
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
const std::string SelectableComboPropertyEditorType
const std::string ComboPropertyEditorType
const std::string ExternalPropertyEditorType
const std::string ScientficSpinBoxEditorType
const std::string bool_type_name
const std::string comboproperty_type_name
const std::string double_type_name
const std::string ColorEditorType
const std::string ScientficDoubleEditorType
const std::string extproperty_type_name
const std::string BoolEditorType
const std::string DoubleEditorType
const std::string IntegerEditorType
const std::string qcolor_type_name
const std::string int_type_name
MVVM_VIEWMODEL_EXPORT builder_t ColorEditorBuilder()
Builder for color property editor.
MVVM_VIEWMODEL_EXPORT builder_t BoolEditorBuilder()
Builder for boolean property editor.
MVVM_VIEWMODEL_EXPORT builder_t SelectableComboPropertyEditorBuilder()
Builder for ComboProperty editor with multi-selection functionality.
MVVM_VIEWMODEL_EXPORT builder_t ScientificDoubleEditorBuilder()
Builder for double editor with scientific notation based on simple text field.
MVVM_VIEWMODEL_EXPORT builder_t ComboPropertyEditorBuilder()
Builder for ComboProperty editor.
std::function< editor_t(const SessionItem *)> builder_t
MVVM_VIEWMODEL_EXPORT builder_t ScientificSpinBoxEditorBuilder()
Builder for double editor with scientific notation and spinbox functionality.
MVVM_VIEWMODEL_EXPORT builder_t ExternalPropertyEditorBuilder()
Builder for external property editor.
MVVM_VIEWMODEL_EXPORT builder_t IntegerEditorBuilder()
Builder for integer property editor.
MVVM_VIEWMODEL_EXPORT builder_t DoubleEditorBuilder()
Builder for double editor with limits support.
MVVM_MODEL_EXPORT std::string VariantName(const Variant &variant)
Returns name of variant.
materialitems.h Collection of materials to populate MaterialModel.
QString const & name(EShape k)
Definition: particles.cpp:21
Definition: filesystem.h:81
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?