6 #include "uiPreferences/action/SPreferencesConfiguration.hpp" 8 #include <fwCom/Signal.hpp> 9 #include <fwCom/Signal.hxx> 11 #include <fwData/Composite.hpp> 12 #include <fwData/Integer.hpp> 13 #include <fwData/location/Folder.hpp> 14 #include <fwData/String.hpp> 16 #include <fwGui/dialog/LocationDialog.hpp> 18 #include <fwPreferences/helper.hpp> 20 #include <fwServices/macros.hpp> 21 #include <fwServices/registry/ObjectService.hpp> 23 #include <boost/lexical_cast.hpp> 24 #include <boost/tokenizer.hpp> 27 #include <QDoubleValidator> 28 #include <QGridLayout> 29 #include <QHBoxLayout> 30 #include <QIntValidator> 32 #include <QPushButton> 40 const ::fwCom::Signals::SignalKeyType SPreferencesConfiguration::s_PARAMETERS_MODIFIED_SIG =
"parametersModified";
48 m_sigParametersModified = newSignal< ParametersModifiedSignalType >(s_PARAMETERS_MODIFIED_SIG);
64 ::fwData::Composite::sptr prefs = ::fwPreferences::getPreferences();
67 for(PreferenceElt& pref : m_preferences)
69 pref.m_dataPreference = ::fwData::String::New(pref.m_defaultValue);
70 ::fwData::Composite::IteratorType iterPref = prefs->find( pref.m_preferenceKey );
71 if ( iterPref != prefs->end() )
73 pref.m_dataPreference = ::fwData::String::dynamicCast(iterPref->second);
77 (*prefs)[pref.m_preferenceKey] = pref.m_dataPreference;
101 ConfigurationType typeCfg = elt->findConfigurationElement(
"type");
102 SLM_ASSERT(
"element 'type' is missing.", typeCfg);
103 if(typeCfg->getValue() ==
"checkbox")
105 pref.m_type = PreferenceType::CHECKBOX;
107 else if (typeCfg->getValue() ==
"text" )
109 pref.m_type = PreferenceType::TEXT;
111 else if (typeCfg->getValue() ==
"number")
113 FW_DEPRECATED_MSG(
"'number' configuration element will be deprecated in further version please use 'int'",
116 pref.m_type = PreferenceType::U_INT;
118 else if(typeCfg->getValue() ==
"path")
120 pref.m_type = PreferenceType::PATH;
122 else if(typeCfg->getValue() ==
"combobox")
124 pref.m_type = PreferenceType::COMBOBOX;
126 else if(typeCfg->getValue() ==
"double")
128 pref.m_type = PreferenceType::DOUBLE;
130 else if(typeCfg->getValue() ==
"int")
132 pref.m_type = PreferenceType::U_INT;
136 OSLM_ERROR(
"Preference type "<<typeCfg->getValue()<<
" is not implemented");
139 ConfigurationType nameCfg = elt->findConfigurationElement(
"name");
140 SLM_ASSERT(
"element 'name' is missing.", nameCfg);
141 pref.m_name = nameCfg->getValue();
143 ConfigurationType keyCfg = elt->findConfigurationElement(
"key");
144 SLM_ASSERT(
"element 'key' is missing.", keyCfg);
145 pref.m_preferenceKey = keyCfg->getValue();
147 ConfigurationType defaultValueCfg = elt->findConfigurationElement(
"default_value");
148 SLM_ASSERT(
"element 'default_value' is missing.", defaultValueCfg);
149 pref.m_defaultValue = defaultValueCfg->getValue();
151 if(pref.m_type == PreferenceType::TEXT || pref.m_type == PreferenceType::PATH)
153 pref.m_lineEdit =
new QLineEdit(QString::fromStdString(pref.m_defaultValue));
155 else if(pref.m_type == PreferenceType::CHECKBOX)
157 pref.m_checkBox =
new QCheckBox();
158 pref.m_checkBox->setChecked(pref.m_defaultValue ==
"true");
160 else if(pref.m_type == PreferenceType::U_INT)
162 pref.m_lineEdit =
new QLineEdit(QString::fromStdString(pref.m_defaultValue));
163 pref.m_lineEdit->setValidator(
new QIntValidator( 0, 999999));
165 else if(pref.m_type == PreferenceType::DOUBLE)
167 pref.m_lineEdit =
new QLineEdit(QString::fromStdString(pref.m_defaultValue));
168 pref.m_lineEdit->setValidator(
new QDoubleValidator( -1000000.0, 1000000.0, 6));
170 else if(pref.m_type == PreferenceType::COMBOBOX)
172 ConfigurationType valuesCfg = elt->findConfigurationElement(
"values");
173 SLM_ASSERT(
"element 'values' is missing.", valuesCfg);
175 const ::boost::char_separator<char> sep(
", ;");
176 const std::string s = valuesCfg->getValue();
177 const ::boost::tokenizer< ::boost::char_separator<char> > tokens {s, sep};
179 pref.m_comboBox =
new QComboBox();
180 for(
const std::string& value : tokens)
182 pref.m_comboBox->addItem(QString::fromStdString(value));
185 m_preferences.push_back(pref);
193 QPointer<QDialog> dialog =
new QDialog();
194 QPointer<QGridLayout> layout =
new QGridLayout();
197 for(PreferenceElt& pref : m_preferences)
199 QPointer<QLabel> label =
new QLabel(QString::fromStdString(pref.m_name));
200 layout->addWidget(label, index, 0);
202 if(pref.m_type == PreferenceType::TEXT)
204 pref.m_lineEdit->setText(QString::fromStdString(pref.m_dataPreference->value()));
205 layout->addWidget(pref.m_lineEdit, index, 1);
207 else if(pref.m_type == PreferenceType::CHECKBOX)
209 pref.m_checkBox->setChecked(pref.m_dataPreference->value() ==
"true");
210 layout->addWidget(pref.m_checkBox, index, 1);
212 else if(pref.m_type == PreferenceType::U_INT || pref.m_type == PreferenceType::DOUBLE)
214 pref.m_lineEdit->setText(QString::fromStdString(pref.m_dataPreference->value()));
215 layout->addWidget(pref.m_lineEdit, index, 1);
217 else if(pref.m_type == PreferenceType::PATH)
219 pref.m_lineEdit->setText(QString::fromStdString(pref.m_dataPreference->value()));
220 layout->addWidget(pref.m_lineEdit, index, 1);
221 QPointer<QPushButton> directorySelector =
new QPushButton(
"...");
222 layout->addWidget(directorySelector, index, 2);
223 QObject::connect(directorySelector.data(), &QPushButton::clicked, [
this, pref]()
225 this->onSelectDir(pref.m_lineEdit);
228 else if(pref.m_type == PreferenceType::COMBOBOX)
230 const int currentIndex = pref.m_comboBox->findText(QString::fromStdString(pref.m_dataPreference->value()));
233 SLM_WARN(
"Preference '" + pref.m_dataPreference->value() +
234 "' can't be find in combobox. The first one is selected.");
235 pref.m_comboBox->setCurrentIndex(0);
239 pref.m_comboBox->setCurrentIndex(currentIndex);
241 layout->addWidget(pref.m_comboBox, index, 1);
247 QPointer<QPushButton> cancelButton =
new QPushButton(
"Cancel");
248 QPointer<QPushButton> okButton =
new QPushButton(
"OK");
249 okButton->setDefault(
true);
251 QPointer<QHBoxLayout> buttonLayout =
new QHBoxLayout();
252 buttonLayout->addWidget(cancelButton);
253 buttonLayout->addWidget(okButton);
255 layout->addLayout(buttonLayout, index, 1, 4, 2 );
257 QObject::connect(cancelButton.data(), &QPushButton::clicked, dialog.data(), &QDialog::reject);
258 QObject::connect(okButton.data(), &QPushButton::clicked, dialog.data(), &QDialog::accept);
260 dialog->setLayout(layout);
262 if (dialog->exec() == QDialog::Accepted)
264 for(PreferenceElt& pref : m_preferences)
266 if((pref.m_type == PreferenceType::TEXT || pref.m_type == PreferenceType::PATH) &&
267 !pref.m_lineEdit->text().isEmpty())
269 pref.m_dataPreference->value() = pref.m_lineEdit->text().toStdString();
271 else if(pref.m_type == PreferenceType::CHECKBOX)
273 pref.m_dataPreference->value() = pref.m_checkBox->isChecked() ?
"true" :
"false";
275 else if(pref.m_type == PreferenceType::U_INT || pref.m_type == PreferenceType::DOUBLE)
277 pref.m_dataPreference->value() = pref.m_lineEdit->text().toStdString();
279 else if(pref.m_type == PreferenceType::COMBOBOX)
281 pref.m_dataPreference->value() = pref.m_comboBox->currentText().toStdString();
284 m_sigParametersModified->asyncEmit();
290 void SPreferencesConfiguration::onSelectDir(QPointer<QLineEdit> lineEdit)
292 static ::boost::filesystem::path _sDefaultPath;
295 dialogFile.
setTitle(
"Select Storage directory");
297 dialogFile.
setOption(::fwGui::dialog::ILocationDialog::WRITE);
298 dialogFile.
setType(::fwGui::dialog::ILocationDialog::FOLDER);
300 ::fwData::location::Folder::sptr result;
301 result = ::fwData::location::Folder::dynamicCast( dialogFile.
show() );
304 _sDefaultPath = result->getFolder();
305 lineEdit->setText( QString::fromStdString(result->getFolder().string()) );
virtual void info(std::ostream &_sstream) override
Overrides.
virtual void starting() override
Start the action. Gets the preference composite.
FWGUI_API void actionServiceStarting()
Method called when the action service is starting.
This action shows a dialog to configure preferences of an application.
FWGUI_API void actionServiceStopping()
Method called when the action service is stopping.
virtual FWGUI_API void setDefaultLocation(::fwData::location::ILocation::sptr loc) override
Set the initial location for the dialog.
FWGUI_API::fwGui::dialog::ILocationDialog & setOption(::fwGui::dialog::ILocationDialog::Options option) override
allow to set option to the file dialog mode=READ/WRITE, check=FILE_MUST_EXIST
FWGUI_API::fwData::location::ILocation::sptr show() override
Display the dialog.
#define FW_DEPRECATED_MSG(message, version)
Use this macro when deprecating a function to warn the developer.
virtual UIPREFERENCES_API ~SPreferencesConfiguration() noexcept
Destructor. Do nothing.
#define SLM_WARN(message)
Defines the service interface managing the menu items.
UIPREFERENCES_API SPreferencesConfiguration() noexcept
Constructor. Do nothing.
#define OSLM_ERROR(message)
virtual void swapping() override
Does nothing.
virtual void configuring() override
Configures the service.
#define SLM_ASSERT(message, cond)
work like 'assert' from 'cassert', with in addition a message logged by spylog (with FATAL loglevel) ...
FWGUI_API void initialize()
Initialize the action.
Base class for each data object.
::fwRuntime::ConfigurationElement::sptr m_configuration
Configuration element used to configure service internal state using a generic XML like structure TOD...
virtual void stopping() override
Does nothing.
FWGUI_API void setTitle(const std::string &title) override
Set the title for the dialog.
Defines the generic file/folder selector dialog for IHM.
Defines the generic configuration element container class.
FWRUNTIME_API const Container & getElements() const
Returns the configuration element container.
The namespace uiPreferences contains editors to manage the preferences configuration.
FWGUI_API void setType(::fwGui::dialog::ILocationDialog::Types type) override
Set the type of location for the dialog (SINGLE_FILE, FORLDER, MULTI_FILES)
FWGUI_API void saveDefaultLocation(::fwData::location::ILocation::sptr loc) override
Save the specified default location for the dialog in preferences (if available)
virtual void updating() override
Shows a dialog to configure preferences declared in xml.