Open alxbilger opened 1 month ago
[ci-build][with-all-tests]
@damienmarchal I managed to return a common string for all types deriving from SelectableItem
when calling Data::getValueTypeString
. Now, the problem is with the dynamic_cast
:
struct TestSelectableItem final : sofa::helper::SelectableItem<TestSelectableItem> {};
TestSelectableItem item;
sofa::Data<TestSelectableItem> data;
BaseData* baseData = &data;
Obviously dynamic_cast<BaseSelectableItem*>(&item) != nullptr
, but dynamic_cast<Data<BaseSelectableItem>*>(&baseData)==nullptr
.
The widget mechanism relies on dynamic_cast
to go from a BaseData
to a Data<T>
(https://github.com/sofa-framework/sofa/blob/cda8e2306915cb2ae768ff6a283e7931dfa637db/Sofa/GUI/Qt/src/sofa/gui/qt/DataWidget.h#L69). Any idea?
@alxbilger
I was trying to find a solution but looking at the code in DataWidget.cpp makes me notice:
DataWidget *DataWidget::CreateDataWidget(const DataWidget::CreatorArgument &dwarg)
{
DataWidget *datawidget_ = nullptr;
const std::string &widgetName=dwarg.data->getWidget();
if (widgetName.empty())
datawidget_ = DataWidgetFactory::CreateAnyObject(dwarg);
else
datawidget_ = DataWidgetFactory::CreateObject(widgetName, dwarg);
return datawidget_;
}
And wonder if you try the getWidget() to short cut the dynamic_cast ?
@damienmarchal I believe that the dynamic_cast
still happens in https://github.com/sofa-framework/sofa/blob/master/Sofa/GUI/Qt/src/sofa/gui/qt/DataWidget.h#L180
The type is now supported in both Qt and ImGui:
SelectableItem
is a new type that can replaceOptionsGroup
in some cases. The idea is the same: a list of keys + a selection of a key among the list. InOptionsGroup
, everything is dynamic, whereasSelectableItem
is designed to be static.Disadvantages of
OptionsGroup
:In
SelectableItem
:The consequences:
constexpr
:It allows to use the type in a
constexpr
context. For example:There is compile-time check that any of the
ResolutionMethod("ProjectedGaussSeidel"), ResolutionMethod("NonsmoothNonlinearConjugateGradient"), ResolutionMethod("UnbuiltGaussSeidel")
exist. If it does not exist, it does not compile.It is preferable to force the
constexpr
context:The compiler may not choose to use
constexpr
in:Therefore, no
constexpr
check ifNonsmoothNonlinearConjugateGradient
is not in the list. There is a runtime check though.The type allows to deprecate a key if desired (I am thinking about the Data
method
in the FEM force fields).There is also a description of each key. And the whole list (key + description) can be added easily in the description of the Data, hence in the documentation.
The major problem is about the GUI. More particularly, when a Data is read to be displayed in the GUI. Here, each of the
SelectableItem
is a new strong type. So there is noDataTypeName
,DataTypeInfo
etc for the new type. I tried to factorize with a common base class (BaseSelectableItem
) but without success. I tried to defineDataTypeName
,DataTypeInfo
etc forSelectedItem
but without success. The consequence is that I cannot specialize a widget for this type of Data.By submitting this pull request, I acknowledge that
I have read, understand, and agree SOFA Developer Certificate of Origin (DCO).
Reviewers will merge this pull-request only if