1 // Copyright (C) 2014-2022 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "ModuleBase_Preferences.h"
21 //#include "ModuleBase_Constants.h"
23 #include <Config_PropManager.h>
25 #include <TopAbs_ShapeEnum.hxx>
27 #include <SUIT_ResourceMgr.h>
28 #include <SUIT_PreferenceMgr.h>
32 #include <QApplication>
33 #include <QDialogButtonBox>
34 #include <QPushButton>
37 #pragma warning(disable : 4456) // for nested foreach
40 const QString ModuleBase_Preferences::VIEWER_SECTION = "Viewer";
41 const QString ModuleBase_Preferences::MENU_SECTION = "Menu";
42 const QString ModuleBase_Preferences::GENERAL_SECTION = "General";
44 SUIT_ResourceMgr* ModuleBase_Preferences::myResourceMgr = 0;
46 SUIT_ResourceMgr* ModuleBase_Preferences::resourceMgr()
49 myResourceMgr = new SUIT_ResourceMgr("SHAPER");
50 myResourceMgr->setCurrentFormat("xml");
55 bool ModuleBase_Preferences::editPreferences(ModuleBase_Prefs& theModified)
57 ModuleBase_PreferencesDlg aDlg(resourceMgr(), QApplication::activeWindow());
59 if (aDlg.isChanged()) {
60 aDlg.modified(theModified);
61 resourceMgr()->save();
67 void ModuleBase_Preferences::updateConfigByResources()
69 Config_Properties aProps = Config_PropManager::getProperties();
70 Config_Properties::iterator aIt;
71 for (aIt = aProps.begin(); aIt != aProps.end(); ++aIt) {
72 Config_Prop* aProp = (*aIt);
73 QString aVal = myResourceMgr->stringValue(QString(aProp->section().c_str()),
74 QString(aProp->name().c_str()));
75 if (!aVal.isEmpty()) {
76 aProp->setValue(aVal.toStdString());
81 void ModuleBase_Preferences::updateResourcesByConfig()
83 Config_Properties aProps = Config_PropManager::getProperties();
84 Config_Properties::iterator aIt;
85 for (aIt = aProps.begin(); aIt != aProps.end(); ++aIt) {
86 Config_Prop* aProp = (*aIt);
87 myResourceMgr->setValue(QString(aProp->section().c_str()), QString(aProp->name().c_str()),
88 QString(aProp->value().c_str()));
92 void ModuleBase_Preferences::resetConfig()
94 Config_Properties aProps = Config_PropManager::getProperties();
95 Config_Properties::iterator aIt;
96 for (aIt = aProps.begin(); aIt != aProps.end(); ++aIt) {
97 Config_Prop* aProp = (*aIt);
98 aProp->setValue(aProp->defaultValue());
102 void ModuleBase_Preferences::loadCustomProps()
106 QStringList aSections = myResourceMgr->sections();
107 foreach (QString aSection, aSections)
109 QStringList aParams = myResourceMgr->parameters(aSection);
110 foreach (QString aParam, aParams)
112 Config_Prop* aProp = Config_PropManager::registerProp(aSection.toStdString(),
113 aParam.toStdString(), "", Config_Prop::Disabled);
114 aProp->setValue(myResourceMgr->stringValue(aSection, aParam).toStdString());
120 void ModuleBase_Preferences::createEditContent(ModuleBase_IPrefMgr* thePref, int thePage)
122 thePref->prefMgr()->setItemIcon(thePage, QIcon(":pictures/module.png"));
123 createGeneralTab(thePref, thePage);
124 createCustomPage(thePref, thePage);
125 updateSketchTab(thePref, thePage);
128 void ModuleBase_Preferences::resetResourcePreferences(SUIT_PreferenceMgr* thePref)
133 QtxResourceMgr::WorkingMode aPrev =
134 thePref->resourceMgr()->setWorkingMode(QtxResourceMgr::IgnoreUserValues);
136 thePref->resourceMgr()->setWorkingMode(aPrev);
139 void ModuleBase_Preferences::resetConfigPropPreferences(SUIT_PreferenceMgr* thePref)
142 updateResourcesByConfig();
144 // retrieve the reset resource values to the preferences items
145 Config_Properties aProps = Config_PropManager::getProperties();
146 Config_Properties::iterator aIt;
148 QStringList aSections;
149 for (aIt = aProps.begin(); aIt != aProps.end(); ++aIt) {
150 Config_Prop* aProp = (*aIt);
151 aValues.append(QString(aProp->name().c_str()));
152 if (!aSections.contains(aProp->section().c_str()))
153 aSections.append(aProp->section().c_str());
154 QtxPreferenceItem* anItem = thePref->findItem(QString(aProp->title().c_str()), true);
160 void ModuleBase_Preferences::createGeneralTab(ModuleBase_IPrefMgr* thePref, int thePageId)
162 int generalTab = thePref->addPreference(QObject::tr("General"), thePageId,
163 SUIT_PreferenceMgr::Auto, QString(), QString());
164 thePref->setItemProperty("columns", 2, generalTab);
166 QStringList actItemList;
167 actItemList << QObject::tr("Last part")
168 << QObject::tr("All parts")
169 << QObject::tr("No activation");
171 QList<QVariant> actIdList;
172 actIdList << 0 << 1 << 2;
174 // Group related to creation of a study
175 int group = thePref->addPreference(QObject::tr("Creation a study"), generalTab,
176 SUIT_PreferenceMgr::Auto, QString(), QString());
178 int actId = thePref->addPreference(QObject::tr("Create new part"), group, SUIT_PreferenceMgr::Bool,
179 ModuleBase_Preferences::GENERAL_SECTION, "create_init_part");
181 // Group related to running a python script
182 group = thePref->addPreference(QObject::tr("Launching a python script"), generalTab,
183 SUIT_PreferenceMgr::Auto, QString(), QString());
185 QStringList visuItemList;
186 visuItemList << QObject::tr("Last item in each folder")
187 << QObject::tr("All items")
188 << QObject::tr("No visualization");
190 QList<QVariant> visuIdList;
191 visuIdList << 0 << 1 << 2;
193 int visuId = thePref->addPreference(QObject::tr("Display"), group, SUIT_PreferenceMgr::Selector,
194 ModuleBase_Preferences::GENERAL_SECTION,
195 "part_visualization_script");
196 thePref->setItemProperty("strings", visuItemList, visuId);
197 thePref->setItemProperty("indexes", visuIdList, visuId);
199 // Group related to opening a study
200 group = thePref->addPreference(QObject::tr("Opening a study"), generalTab,
201 SUIT_PreferenceMgr::Auto, QString(), QString());
203 actId = thePref->addPreference(QObject::tr("Activate"), group, SUIT_PreferenceMgr::Selector,
204 ModuleBase_Preferences::GENERAL_SECTION,
205 "part_activation_study");
206 thePref->setItemProperty("strings", actItemList, actId);
207 thePref->setItemProperty("indexes", actIdList, actId);
209 visuItemList.clear();
210 visuItemList << QObject::tr("As stored in HDF")
211 << QObject::tr("Last item in each folder")
212 << QObject::tr("All items")
213 << QObject::tr("No visualization");
216 visuIdList << 0 << 1 << 2 << 3;
218 visuId = thePref->addPreference(QObject::tr("Display"), group, SUIT_PreferenceMgr::Selector,
219 ModuleBase_Preferences::GENERAL_SECTION,
220 "part_visualization_study");
221 thePref->setItemProperty("strings", visuItemList, visuId);
222 thePref->setItemProperty("indexes", visuIdList, visuId);
225 void ModuleBase_Preferences::updateSketchTab(ModuleBase_IPrefMgr* thePref, int thePageId)
227 int sketchTab = thePref->addPreference(QObject::tr("Sketch"), thePageId,
228 SUIT_PreferenceMgr::Auto, QString(), QString());
229 int allowChange = thePref->addPreference(
230 QObject::tr("Allow automatic constraint substitution/remove"),
231 sketchTab, SUIT_PreferenceMgr::GroupBox,
232 "Sketch", "allow_change_constraint");
233 thePref->addPreference(QObject::tr("Notify automatic constraint substitution/remove"),
234 allowChange, SUIT_PreferenceMgr::Bool, "Sketch", "notify_change_constraint");
237 void ModuleBase_Preferences::createCustomPage(ModuleBase_IPrefMgr* thePref, int thePageId)
239 SUIT_ResourceMgr* aResMgr = ModuleBase_Preferences::resourceMgr();
241 // Make a Tab from each section
242 std::list<std::string> aSections = Config_PropManager::getSections();
243 std::list<std::string>::const_iterator it;
244 for (it = aSections.cbegin(); it != aSections.cend(); ++it) {
245 Config_Properties aProps = Config_PropManager::getProperties(*it);
246 int aTab = thePref->prefMgr()->addItem(QString((*it).c_str()), thePageId);
247 thePref->prefMgr()->setItemProperty("columns", 2, aTab);
249 Config_Properties::const_iterator aIt;
250 for (aIt = aProps.cbegin(); aIt != aProps.cend(); ++aIt) {
251 Config_Prop* aProp = (*aIt);
252 // check that the property is defined
253 QString aSection(aProp->section().c_str());
254 QString aName(aProp->name().c_str());
255 if (!aResMgr->hasValue(aSection, aName))
256 aResMgr->setValue(aSection, aName, QString(aProp->value().c_str()));
259 if (aProp->type() != Config_Prop::Disabled) {
260 SUIT_PreferenceMgr::PrefItemType aPrefType = SUIT_PreferenceMgr::Auto;
261 switch (aProp->type()) {
262 case Config_Prop::Directory:
263 aPrefType = SUIT_PreferenceMgr::File;
265 case Config_Prop::Cursor:
266 aPrefType = SUIT_PreferenceMgr::Selector;
269 aPrefType = (SUIT_PreferenceMgr::PrefItemType) aProp->type();
272 int anId = thePref->addPreference(QObject::tr(aProp->title().c_str()), aTab, aPrefType,
273 QString::fromStdString(aProp->section()),
274 QString::fromStdString(aProp->name()));
276 switch (aProp->type()) {
277 case Config_Prop::Directory:
278 thePref->setItemProperty("path_type", Qtx::PT_Directory, anId);
280 case SUIT_PreferenceMgr::DblSpin:
281 if (aProp->min() != "") {
282 double aMin = QString(aProp->min().c_str()).toDouble();
283 thePref->setItemProperty("min", aMin, anId);
285 if (aProp->max() != "") {
286 double aMax = QString(aProp->max().c_str()).toDouble();
287 thePref->setItemProperty("max", aMax, anId);
290 case SUIT_PreferenceMgr::IntSpin:
291 if (aProp->min() != "") {
292 int aMin = QString(aProp->min().c_str()).toInt();
293 thePref->setItemProperty("min", aMin, anId);
295 if (aProp->max() != "") {
296 int aMax = QString(aProp->max().c_str()).toInt();
297 thePref->setItemProperty("max", aMax, anId);
300 case Config_Prop::Cursor:
302 QList<QVariant> aIndicesList;
303 QList<QVariant> aIconsList;
304 aIndicesList << 0 << 1 << 2;
305 aIconsList << QPixmap(":pictures/ArrowCursor.png") <<
306 QPixmap(":pictures/CrossCursor.png") <<
307 QPixmap(":pictures/HandCursor.png");
309 thePref->setItemProperty("indexes", aIndicesList, anId);
310 thePref->setItemProperty("icons", aIconsList, anId);
313 default: // [to avoid compilation warnings]
322 * Implementation of preferences manager interface
324 class ModuleBase_PrefMgr: public ModuleBase_IPrefMgr
328 /// \param theMgr a preferences manager
329 ModuleBase_PrefMgr(ModuleBase_PreferencesMgr* theMgr):myMgr(theMgr) {}
331 virtual int addPreference(const QString& theLbl, int pId,
332 SUIT_PreferenceMgr::PrefItemType theType,
333 const QString& theSection, const QString& theName )
335 return myMgr->addItem(theLbl, pId, theType, theSection, theName);
338 virtual void setItemProperty( const QString& thePropName, const QVariant& theValue,
339 const int theId = -1) {
340 myMgr->setItemProperty(thePropName, theValue, theId);
343 virtual SUIT_PreferenceMgr* prefMgr() const { return myMgr; }
346 ModuleBase_PreferencesMgr* myMgr;
349 //**********************************************************
350 //**********************************************************
351 //**********************************************************
352 ModuleBase_PreferencesDlg::ModuleBase_PreferencesDlg(SUIT_ResourceMgr* theResurces,
355 Qt::Dialog | Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::WindowCloseButtonHint),
358 setWindowTitle(tr("Edit preferences"));
360 QVBoxLayout* main = new QVBoxLayout(this);
364 myPreferences = new ModuleBase_PreferencesMgr(theResurces, this);
365 main->addWidget(myPreferences);
367 setFocusProxy(myPreferences);
368 myPreferences->setFrameStyle(QFrame::Box | QFrame::Sunken);
370 QDialogButtonBox* aBtnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel |
371 QDialogButtonBox::Reset,
372 Qt::Horizontal, this);
373 QPushButton* aDefaultButton = aBtnBox->button(QDialogButtonBox::Reset);
374 aDefaultButton->setText(tr("Default"));
375 connect(aDefaultButton, SIGNAL(clicked()), this, SLOT(onDefault()));
377 main->addWidget(aBtnBox);
378 connect(aBtnBox, SIGNAL(accepted()), this, SLOT(accept()));
379 connect(aBtnBox, SIGNAL(rejected()), this, SLOT(reject()));
382 myPreferences->retrieve();
385 ModuleBase_PreferencesDlg::~ModuleBase_PreferencesDlg()
389 void ModuleBase_PreferencesDlg::createEditors()
391 int aPage = myPreferences->addItem(tr("Desktop"));
392 myPreferences->setItemIcon(aPage, QIcon(":pictures/view_prefs.png"));
394 createMenuPage(aPage);
395 createViewerPage(aPage);
397 aPage = myPreferences->addItem(tr("Module"));
398 myPreferences->setItemIcon(aPage, QIcon(":pictures/module.png"));
400 ModuleBase_PrefMgr aMgr(myPreferences);
401 ModuleBase_Preferences::createEditContent(&aMgr, aPage);
404 void ModuleBase_PreferencesDlg::createViewerPage(int thePageId)
406 int viewTab = myPreferences->addItem(tr("Viewer"), thePageId);
408 QStringList gradList;
409 gradList << tr("Horizontal gradient") << tr("Vertical gradient") << tr("First diagonal gradient")
410 << tr("Second diagonal gradient") << tr("First corner gradient")
411 << tr("Second corner gradient") << tr("Third corner gradient")
412 << tr("Fourth corner gradient");
414 QList<QVariant> idList;
415 for (int i = 0; i < gradList.size(); i++)
418 int bgGroup = myPreferences->addItem(tr("Background"), viewTab);
420 QString aImgFiles("Image files (*.bmp *.gif *.pix *.xwd *.rgb *.rs)");
422 int bgId = myPreferences->addItem(tr("Viewer 3d"), bgGroup, SUIT_PreferenceMgr::Background,
423 ModuleBase_Preferences::VIEWER_SECTION, "background");
424 myPreferences->setItemProperty("gradient_names", gradList, bgId);
425 myPreferences->setItemProperty("gradient_ids", idList, bgId);
426 myPreferences->setItemProperty("texture_enabled", true, bgId);
427 myPreferences->setItemProperty("texture_center_enabled", true, bgId);
428 myPreferences->setItemProperty("texture_tile_enabled", true, bgId);
429 myPreferences->setItemProperty("texture_stretch_enabled", true, bgId);
430 myPreferences->setItemProperty("custom_enabled", false, bgId);
431 myPreferences->setItemProperty("image_formats", aImgFiles, bgId);
433 // Create other parameters group in viewer tab
434 int selectionGroup = myPreferences->addItem(tr("Default selection"), viewTab);
435 myPreferences->setItemProperty("columns", 3, selectionGroup);
436 myPreferences->addItem(tr("Faces"), selectionGroup,
437 SUIT_PreferenceMgr::Bool,
438 ModuleBase_Preferences::VIEWER_SECTION, "face-selection");
439 myPreferences->addItem(tr("Edges"), selectionGroup,
440 SUIT_PreferenceMgr::Bool,
441 ModuleBase_Preferences::VIEWER_SECTION, "edge-selection");
442 myPreferences->addItem(tr("Vertices"), selectionGroup,
443 SUIT_PreferenceMgr::Bool,
444 ModuleBase_Preferences::VIEWER_SECTION, "vertex-selection");
446 int sensitivityGroup = myPreferences->addItem(tr("Selection sensitivity"), viewTab);
447 myPreferences->setItemProperty("columns", 2, sensitivityGroup);
448 myPreferences->addItem(tr("Vertex"), sensitivityGroup, SUIT_PreferenceMgr::DblSpin,
449 ModuleBase_Preferences::VIEWER_SECTION, "point-selection-sensitivity");
450 myPreferences->addItem(tr("Edge"), sensitivityGroup, SUIT_PreferenceMgr::DblSpin,
451 ModuleBase_Preferences::VIEWER_SECTION, "edge-selection-sensitivity");
453 int highlightGroup = myPreferences->addItem(tr("Additional highlighting"), viewTab);
454 myPreferences->setItemProperty("columns", 2, highlightGroup);
455 myPreferences->addItem(tr("In 3d mode"), highlightGroup,
456 SUIT_PreferenceMgr::Bool, ModuleBase_Preferences::VIEWER_SECTION, "highlighting-3d");
457 myPreferences->addItem(tr("In 2d mode"), highlightGroup,
458 SUIT_PreferenceMgr::Bool, ModuleBase_Preferences::VIEWER_SECTION, "highlighting-2d");
460 int colorScaleGroup = myPreferences->addItem(tr("Color scale"), viewTab);
461 myPreferences->setItemProperty("columns", 4, colorScaleGroup);
462 int aItem = myPreferences->addItem(tr("X position"), colorScaleGroup,
463 SUIT_PreferenceMgr::Double, ModuleBase_Preferences::VIEWER_SECTION, "scalar_bar_x_position");
464 myPreferences->setItemProperty("min", 0, aItem);
465 myPreferences->setItemProperty("max", 1, aItem);
466 aItem = myPreferences->addItem(tr("Y position"), colorScaleGroup,
467 SUIT_PreferenceMgr::Double, ModuleBase_Preferences::VIEWER_SECTION, "scalar_bar_y_position");
468 myPreferences->setItemProperty("min", 0, aItem);
469 myPreferences->setItemProperty("max", 1, aItem);
470 aItem = myPreferences->addItem(tr("Width"), colorScaleGroup,
471 SUIT_PreferenceMgr::Double, ModuleBase_Preferences::VIEWER_SECTION, "scalar_bar_width");
472 myPreferences->setItemProperty("min", 0, aItem);
473 myPreferences->setItemProperty("max", 1, aItem);
474 aItem = myPreferences->addItem(tr("Height"), colorScaleGroup,
475 SUIT_PreferenceMgr::Double, ModuleBase_Preferences::VIEWER_SECTION, "scalar_bar_height");
476 myPreferences->setItemProperty("min", 0, aItem);
477 myPreferences->setItemProperty("max", 1, aItem);
478 aItem = myPreferences->addItem(tr("Intervals number"), colorScaleGroup,
479 SUIT_PreferenceMgr::Integer, ModuleBase_Preferences::VIEWER_SECTION, "scalar_bar_nb_intervals");
480 myPreferences->setItemProperty("min", 0, aItem);
481 myPreferences->setItemProperty("max", 100, aItem);
482 aItem = myPreferences->addItem(tr("Text height"), colorScaleGroup,
483 SUIT_PreferenceMgr::Integer, ModuleBase_Preferences::VIEWER_SECTION, "scalar_bar_text_height");
484 myPreferences->setItemProperty("min", 0, aItem);
485 myPreferences->setItemProperty("max", 100, aItem);
486 aItem = myPreferences->addItem(tr("Text color"), colorScaleGroup,
487 SUIT_PreferenceMgr::Color, ModuleBase_Preferences::VIEWER_SECTION, "scalar_bar_text_color");
490 void ModuleBase_PreferencesDlg::createMenuPage(int thePageId)
492 int aMenuTab = myPreferences->addItem(tr("Main menu"), thePageId);
494 int aSizeGroup = myPreferences->addItem(tr("Size"), aMenuTab);
495 myPreferences->setItemProperty("columns", 1, aSizeGroup);
497 int aRowsNb = myPreferences->addItem(tr("Number of rows"), aSizeGroup,
498 SUIT_PreferenceMgr::IntSpin,
499 ModuleBase_Preferences::MENU_SECTION,
501 myPreferences->setItemProperty("min", 1, aRowsNb);
502 myPreferences->setItemProperty("max", 6, aRowsNb);
504 myPreferences->addItem(tr("Show Status Bar"), aSizeGroup,
505 SUIT_PreferenceMgr::Bool, ModuleBase_Preferences::MENU_SECTION,
509 void ModuleBase_PreferencesDlg::accept()
511 myPreferences->store();
514 // Save custom properties
515 ModuleBase_Preferences::updateConfigByResources();
519 void ModuleBase_PreferencesDlg::modified(ModuleBase_Prefs& theModified) const
521 theModified = myPreferences->modified();
524 void ModuleBase_PreferencesDlg::onDefault()
526 // reset main resources. It throwns all resource manager items to the
527 // initial/default state. If there is no a default state of the item,
528 // it will be filled with an empty value. It concernerned to plugin
529 // config items, like visualization color. The main xml do not contains
530 // default values for them. So, it is important to reset the config
531 // properties after reseting the resources preferences.
532 ModuleBase_Preferences::resetResourcePreferences(myPreferences);
533 // reset plugin's resources. It fills the config resources with the default
534 // values, stores result in the resource manager and retrieve the preferences
535 // items with these values.
536 ModuleBase_Preferences::resetConfigPropPreferences(myPreferences);
539 void ModuleBase_PreferencesDlg::showEvent(QShowEvent* theEvent)
541 QDialog::showEvent(theEvent);
545 //**********************************************************
546 //**********************************************************
547 //**********************************************************
548 void ModuleBase_PreferencesMgr::changedResources(const ResourceMap& theMap)
551 ResourceMap::ConstIterator it;
553 for (it = theMap.begin(); it != theMap.end(); ++it) {
554 ModuleBase_Pref aPref;
555 it.key()->resource(aPref.first, aPref.second);
556 myModified.append(aPref);