SEARCH
TOOLBOX
LANGUAGES
Option Group

Option Group

From SOFAWiki

Jump to: navigation, search
Basic instructions to create a new Plug-in

Contents

Motivation

If a component have several options for configuring itself, it's needed to show all the options list in order that users can see all possible choices and takes the appropriated one. Using sofa::helper::OptionsGroup class allows developers implement this mechanism easily. These options will automatically appear in the GUI and can easily give the option informations for the setting of the component in its implementation.

Declaration

In a file my_component.h

#include <sofa/core/objectmodel/Data.h>
#include <sofa/helper/OptionsGroup.h>
 
classe my_component
{
    Data< sofa::helper::OptionsGroup > m_options;
};


Initialization - setting

sofa::helper::OptionsGroup class have a default constructor doing nothing. After constructed, this variable must be setted for the interest use. Assumed there are 4 choices, and the second one is selected if user doesn't set it.

So in a file my_component.inl or my_component.cpp :

#include "my_component.h"
 
my_component::my_component()
: m_options(initData(&m_options, "m_options", "Briefly explain here the meaning of yours options"))
{
	m_options.beginEdit()->setNames(4,"Choice0","Choice1","Choice2","BadChoice");
        m_options.beginEdit()->setSelectedItem(1); //or equivalently by  m_options.beginEdit()->setSelectedItem("Choice1");
        m_options.endEdit();
};

Use for implementation

#include "my_component.h"
 
my_component::my_myfunction()
{
	if (m_options.getValue().getSelectedItem() =="Choice0"){
               do_the_thing_with_the_choice0();
        }
	if (m_options.getValue().getSelectedItem() =="Choice1"){
               do_the_thing_with_the_choice1();
        }
	if (m_options.getValue().getSelectedItem() =="Choice2"){
               do_the_thing_with_the_choice2();
        }
	if (m_options.getValue().getSelectedItem() =="BadChoice"){
               do_the_thing_with_the_badchoice();
        }
};

Use for SOFA GUI

The user non-developer can only active the appropriated choice by two ways : double-click on the component in the GUI Graph, and select the choice in the Combo Box. Or in the other way in the scene fille m_file.scn :

< my_component  m_options="Choice2" />

If user do nothing, the default "Choice1" is selected by its implementation.