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 opening a study
175 int group = thePref->addPreference(QObject::tr("Opening a study"), generalTab,
176 SUIT_PreferenceMgr::Auto, QString(), QString());
178 int actId = thePref->addPreference(QObject::tr("Activate"), group, SUIT_PreferenceMgr::Selector,
179 ModuleBase_Preferences::GENERAL_SECTION,
180 "part_activation_study");
181 thePref->setItemProperty("strings", actItemList, actId);
182 thePref->setItemProperty("indexes", actIdList, actId);
184 QStringList visuItemList;
185 visuItemList << QObject::tr("As stored in HDF")
186 << 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 << 3;
193 int visuId = thePref->addPreference(QObject::tr("Display"), group, SUIT_PreferenceMgr::Selector,
194 ModuleBase_Preferences::GENERAL_SECTION,
195 "part_visualization_study");
196 thePref->setItemProperty("strings", visuItemList, visuId);
197 thePref->setItemProperty("indexes", visuIdList, visuId);
199 // Group related to running a python script
200 group = thePref->addPreference(QObject::tr("Launching a python script"), generalTab,
201 SUIT_PreferenceMgr::Auto, QString(), QString());
203 visuItemList.clear();
204 visuItemList << QObject::tr("Last item in each folder")
205 << QObject::tr("All items")
206 << QObject::tr("No visualization");
209 visuIdList << 0 << 1 << 2;
211 visuId = thePref->addPreference(QObject::tr("Display"), group, SUIT_PreferenceMgr::Selector,
212 ModuleBase_Preferences::GENERAL_SECTION,
213 "part_visualization_script");
214 thePref->setItemProperty("strings", visuItemList, visuId);
215 thePref->setItemProperty("indexes", visuIdList, visuId);
218 void ModuleBase_Preferences::updateSketchTab(ModuleBase_IPrefMgr* thePref, int thePageId)
220 int sketchTab = thePref->addPreference(QObject::tr("Sketch"), thePageId,
221 SUIT_PreferenceMgr::Auto, QString(), QString());
222 int allowChange = thePref->addPreference(
223 QObject::tr("Allow automatic constraint substitution/remove"),
224 sketchTab, SUIT_PreferenceMgr::GroupBox,
225 "Sketch", "allow_change_constraint");
226 thePref->addPreference(QObject::tr("Notify automatic constraint substitution/remove"),
227 allowChange, SUIT_PreferenceMgr::Bool, "Sketch", "notify_change_constraint");
230 void ModuleBase_Preferences::createCustomPage(ModuleBase_IPrefMgr* thePref, int thePageId)
232 SUIT_ResourceMgr* aResMgr = ModuleBase_Preferences::resourceMgr();
234 // Make a Tab from each section
235 std::list<std::string> aSections = Config_PropManager::getSections();
236 std::list<std::string>::const_iterator it;
237 for (it = aSections.cbegin(); it != aSections.cend(); ++it) {
238 Config_Properties aProps = Config_PropManager::getProperties(*it);
239 int aTab = thePref->prefMgr()->addItem(QString((*it).c_str()), thePageId);
240 thePref->prefMgr()->setItemProperty("columns", 2, aTab);
242 Config_Properties::const_iterator aIt;
243 for (aIt = aProps.cbegin(); aIt != aProps.cend(); ++aIt) {
244 Config_Prop* aProp = (*aIt);
245 // check that the property is defined
246 QString aSection(aProp->section().c_str());
247 QString aName(aProp->name().c_str());
248 if (!aResMgr->hasValue(aSection, aName))
249 aResMgr->setValue(aSection, aName, QString(aProp->value().c_str()));
252 if (aProp->type() != Config_Prop::Disabled) {
253 SUIT_PreferenceMgr::PrefItemType aPrefType = SUIT_PreferenceMgr::Auto;
254 switch (aProp->type()) {
255 case Config_Prop::Directory:
256 aPrefType = SUIT_PreferenceMgr::File;
258 case Config_Prop::Cursor:
259 aPrefType = SUIT_PreferenceMgr::Selector;
262 aPrefType = (SUIT_PreferenceMgr::PrefItemType) aProp->type();
265 int anId = thePref->addPreference(QObject::tr(aProp->title().c_str()), aTab, aPrefType,
266 QString::fromStdString(aProp->section()),
267 QString::fromStdString(aProp->name()));
269 switch (aProp->type()) {
270 case Config_Prop::Directory:
271 thePref->setItemProperty("path_type", Qtx::PT_Directory, anId);
273 case SUIT_PreferenceMgr::DblSpin:
274 if (aProp->min() != "") {
275 double aMin = QString(aProp->min().c_str()).toDouble();
276 thePref->setItemProperty("min", aMin, anId);
278 if (aProp->max() != "") {
279 double aMax = QString(aProp->max().c_str()).toDouble();
280 thePref->setItemProperty("max", aMax, anId);
283 case SUIT_PreferenceMgr::IntSpin:
284 if (aProp->min() != "") {
285 int aMin = QString(aProp->min().c_str()).toInt();
286 thePref->setItemProperty("min", aMin, anId);
288 if (aProp->max() != "") {
289 int aMax = QString(aProp->max().c_str()).toInt();
290 thePref->setItemProperty("max", aMax, anId);
293 case Config_Prop::Cursor:
295 QList<QVariant> aIndicesList;
296 QList<QVariant> aIconsList;
297 aIndicesList << 0 << 1 << 2;
298 aIconsList << QPixmap(":pictures/ArrowCursor.png") <<
299 QPixmap(":pictures/CrossCursor.png") <<
300 QPixmap(":pictures/HandCursor.png");
302 thePref->setItemProperty("indexes", aIndicesList, anId);
303 thePref->setItemProperty("icons", aIconsList, anId);
306 default: // [to avoid compilation warnings]
315 * Implementation of preferences manager interface
317 class ModuleBase_PrefMgr: public ModuleBase_IPrefMgr
321 /// \param theMgr a preferences manager
322 ModuleBase_PrefMgr(ModuleBase_PreferencesMgr* theMgr):myMgr(theMgr) {}
324 virtual int addPreference(const QString& theLbl, int pId,
325 SUIT_PreferenceMgr::PrefItemType theType,
326 const QString& theSection, const QString& theName )
328 return myMgr->addItem(theLbl, pId, theType, theSection, theName);
331 virtual void setItemProperty( const QString& thePropName, const QVariant& theValue,
332 const int theId = -1) {
333 myMgr->setItemProperty(thePropName, theValue, theId);
336 virtual SUIT_PreferenceMgr* prefMgr() const { return myMgr; }
339 ModuleBase_PreferencesMgr* myMgr;
342 //**********************************************************
343 //**********************************************************
344 //**********************************************************
345 ModuleBase_PreferencesDlg::ModuleBase_PreferencesDlg(SUIT_ResourceMgr* theResurces,
348 Qt::Dialog | Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::WindowCloseButtonHint),
351 setWindowTitle(tr("Edit preferences"));
353 QVBoxLayout* main = new QVBoxLayout(this);
357 myPreferences = new ModuleBase_PreferencesMgr(theResurces, this);
358 main->addWidget(myPreferences);
360 setFocusProxy(myPreferences);
361 myPreferences->setFrameStyle(QFrame::Box | QFrame::Sunken);
363 QDialogButtonBox* aBtnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel |
364 QDialogButtonBox::Reset,
365 Qt::Horizontal, this);
366 QPushButton* aDefaultButton = aBtnBox->button(QDialogButtonBox::Reset);
367 aDefaultButton->setText(tr("Default"));
368 connect(aDefaultButton, SIGNAL(clicked()), this, SLOT(onDefault()));
370 main->addWidget(aBtnBox);
371 connect(aBtnBox, SIGNAL(accepted()), this, SLOT(accept()));
372 connect(aBtnBox, SIGNAL(rejected()), this, SLOT(reject()));
375 myPreferences->retrieve();
378 ModuleBase_PreferencesDlg::~ModuleBase_PreferencesDlg()
382 void ModuleBase_PreferencesDlg::createEditors()
384 int aPage = myPreferences->addItem(tr("Desktop"));
385 myPreferences->setItemIcon(aPage, QIcon(":pictures/view_prefs.png"));
387 createMenuPage(aPage);
388 createViewerPage(aPage);
390 aPage = myPreferences->addItem(tr("Module"));
391 myPreferences->setItemIcon(aPage, QIcon(":pictures/module.png"));
393 ModuleBase_PrefMgr aMgr(myPreferences);
394 ModuleBase_Preferences::createEditContent(&aMgr, aPage);
397 void ModuleBase_PreferencesDlg::createViewerPage(int thePageId)
399 int viewTab = myPreferences->addItem(tr("Viewer"), thePageId);
401 QStringList gradList;
402 gradList << tr("Horizontal gradient") << tr("Vertical gradient") << tr("First diagonal gradient")
403 << tr("Second diagonal gradient") << tr("First corner gradient")
404 << tr("Second corner gradient") << tr("Third corner gradient")
405 << tr("Fourth corner gradient");
407 QList<QVariant> idList;
408 for (int i = 0; i < gradList.size(); i++)
411 int bgGroup = myPreferences->addItem(tr("Background"), viewTab);
413 QString aImgFiles("Image files (*.bmp *.gif *.pix *.xwd *.rgb *.rs)");
415 int bgId = myPreferences->addItem(tr("Viewer 3d"), bgGroup, SUIT_PreferenceMgr::Background,
416 ModuleBase_Preferences::VIEWER_SECTION, "background");
417 myPreferences->setItemProperty("gradient_names", gradList, bgId);
418 myPreferences->setItemProperty("gradient_ids", idList, bgId);
419 myPreferences->setItemProperty("texture_enabled", true, bgId);
420 myPreferences->setItemProperty("texture_center_enabled", true, bgId);
421 myPreferences->setItemProperty("texture_tile_enabled", true, bgId);
422 myPreferences->setItemProperty("texture_stretch_enabled", true, bgId);
423 myPreferences->setItemProperty("custom_enabled", false, bgId);
424 myPreferences->setItemProperty("image_formats", aImgFiles, bgId);
426 // Create other parameters group in viewer tab
427 int selectionGroup = myPreferences->addItem(tr("Default selection"), viewTab);
428 myPreferences->setItemProperty("columns", 3, selectionGroup);
429 myPreferences->addItem(tr("Faces"), selectionGroup,
430 SUIT_PreferenceMgr::Bool,
431 ModuleBase_Preferences::VIEWER_SECTION, "face-selection");
432 myPreferences->addItem(tr("Edges"), selectionGroup,
433 SUIT_PreferenceMgr::Bool,
434 ModuleBase_Preferences::VIEWER_SECTION, "edge-selection");
435 myPreferences->addItem(tr("Vertices"), selectionGroup,
436 SUIT_PreferenceMgr::Bool,
437 ModuleBase_Preferences::VIEWER_SECTION, "vertex-selection");
439 int sensitivityGroup = myPreferences->addItem(tr("Selection sensitivity"), viewTab);
440 myPreferences->setItemProperty("columns", 2, sensitivityGroup);
441 myPreferences->addItem(tr("Vertex"), sensitivityGroup, SUIT_PreferenceMgr::DblSpin,
442 ModuleBase_Preferences::VIEWER_SECTION, "point-selection-sensitivity");
443 myPreferences->addItem(tr("Edge"), sensitivityGroup, SUIT_PreferenceMgr::DblSpin,
444 ModuleBase_Preferences::VIEWER_SECTION, "edge-selection-sensitivity");
446 int highlightGroup = myPreferences->addItem(tr("Additional highlighting"), viewTab);
447 myPreferences->setItemProperty("columns", 2, highlightGroup);
448 myPreferences->addItem(tr("In 3d mode"), highlightGroup,
449 SUIT_PreferenceMgr::Bool, ModuleBase_Preferences::VIEWER_SECTION, "highlighting-3d");
450 myPreferences->addItem(tr("In 2d mode"), highlightGroup,
451 SUIT_PreferenceMgr::Bool, ModuleBase_Preferences::VIEWER_SECTION, "highlighting-2d");
453 int colorScaleGroup = myPreferences->addItem(tr("Color scale"), viewTab);
454 myPreferences->setItemProperty("columns", 4, colorScaleGroup);
455 int aItem = myPreferences->addItem(tr("X position"), colorScaleGroup,
456 SUIT_PreferenceMgr::Double, ModuleBase_Preferences::VIEWER_SECTION, "scalar_bar_x_position");
457 myPreferences->setItemProperty("min", 0, aItem);
458 myPreferences->setItemProperty("max", 1, aItem);
460 aItem = myPreferences->addItem(tr("Y position"), colorScaleGroup,
461 SUIT_PreferenceMgr::Double, ModuleBase_Preferences::VIEWER_SECTION, "scalar_bar_y_position");
462 myPreferences->setItemProperty("min", 0, aItem);
463 myPreferences->setItemProperty("max", 1, aItem);
465 aItem = myPreferences->addItem(tr("Width"), colorScaleGroup,
466 SUIT_PreferenceMgr::Double, ModuleBase_Preferences::VIEWER_SECTION, "scalar_bar_width");
467 myPreferences->setItemProperty("min", 0, aItem);
468 myPreferences->setItemProperty("max", 1, aItem);
470 aItem = myPreferences->addItem(tr("Height"), colorScaleGroup,
471 SUIT_PreferenceMgr::Double, ModuleBase_Preferences::VIEWER_SECTION, "scalar_bar_height");
472 myPreferences->setItemProperty("min", 0, aItem);
473 myPreferences->setItemProperty("max", 1, aItem);
475 aItem = myPreferences->addItem(tr("Intervals number"), colorScaleGroup,
476 SUIT_PreferenceMgr::Integer, ModuleBase_Preferences::VIEWER_SECTION, "scalar_bar_nb_intervals");
477 myPreferences->setItemProperty("min", 0, aItem);
478 myPreferences->setItemProperty("max", 100, aItem);
480 aItem = myPreferences->addItem(tr("Text height"), colorScaleGroup,
481 SUIT_PreferenceMgr::Integer, ModuleBase_Preferences::VIEWER_SECTION, "scalar_bar_text_height");
482 myPreferences->setItemProperty("min", 0, aItem);
483 myPreferences->setItemProperty("max", 100, aItem);
485 aItem = myPreferences->addItem(tr("Text color"), colorScaleGroup,
486 SUIT_PreferenceMgr::Color, ModuleBase_Preferences::VIEWER_SECTION, "scalar_bar_text_color");
489 void ModuleBase_PreferencesDlg::createMenuPage(int thePageId)
491 int aMenuTab = myPreferences->addItem(tr("Main menu"), thePageId);
493 int aSizeGroup = myPreferences->addItem(tr("Size"), aMenuTab);
494 myPreferences->setItemProperty("columns", 1, aSizeGroup);
496 int aRowsNb = myPreferences->addItem(tr("Number of rows"), aSizeGroup,
497 SUIT_PreferenceMgr::IntSpin,
498 ModuleBase_Preferences::MENU_SECTION,
500 myPreferences->setItemProperty("min", 1, aRowsNb);
501 myPreferences->setItemProperty("max", 6, aRowsNb);
503 myPreferences->addItem(tr("Show Status Bar"), aSizeGroup,
504 SUIT_PreferenceMgr::Bool, ModuleBase_Preferences::MENU_SECTION,
508 void ModuleBase_PreferencesDlg::accept()
510 myPreferences->store();
513 // Save custom properties
514 ModuleBase_Preferences::updateConfigByResources();
518 void ModuleBase_PreferencesDlg::modified(ModuleBase_Prefs& theModified) const
520 theModified = myPreferences->modified();
523 void ModuleBase_PreferencesDlg::onDefault()
525 // reset main resources. It throwns all resource manager items to the
526 // initial/default state. If there is no a default state of the item,
527 // it will be filled with an empty value. It concernerned to plugin
528 // config items, like visualization color. The main xml do not contains
529 // default values for them. So, it is important to reset the config
530 // properties after reseting the resources preferences.
531 ModuleBase_Preferences::resetResourcePreferences(myPreferences);
532 // reset plugin's resources. It fills the config resources with the default
533 // values, stores result in the resource manager and retrieve the preferences
534 // items with these values.
535 ModuleBase_Preferences::resetConfigPropPreferences(myPreferences);
538 void ModuleBase_PreferencesDlg::showEvent(QShowEvent* theEvent)
540 QDialog::showEvent(theEvent);
544 //**********************************************************
545 //**********************************************************
546 //**********************************************************
547 void ModuleBase_PreferencesMgr::changedResources(const ResourceMap& theMap)
550 ResourceMap::ConstIterator it;
552 for (it = theMap.begin(); it != theMap.end(); ++it) {
553 ModuleBase_Pref aPref;
554 it.key()->resource(aPref.first, aPref.second);
555 myModified.append(aPref);