1 // Copyright (C) 2014-2021 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>
36 const QString ModuleBase_Preferences::VIEWER_SECTION = "Viewer";
37 const QString ModuleBase_Preferences::MENU_SECTION = "Menu";
38 const QString ModuleBase_Preferences::GENERAL_SECTION = "General";
40 SUIT_ResourceMgr* ModuleBase_Preferences::myResourceMgr = 0;
42 SUIT_ResourceMgr* ModuleBase_Preferences::resourceMgr()
45 myResourceMgr = new SUIT_ResourceMgr("SHAPER");
46 myResourceMgr->setCurrentFormat("xml");
51 bool ModuleBase_Preferences::editPreferences(ModuleBase_Prefs& theModified)
53 ModuleBase_PreferencesDlg aDlg(resourceMgr(), QApplication::activeWindow());
55 if (aDlg.isChanged()) {
56 aDlg.modified(theModified);
57 resourceMgr()->save();
63 void ModuleBase_Preferences::updateConfigByResources()
65 Config_Properties aProps = Config_PropManager::getProperties();
66 Config_Properties::iterator aIt;
67 for (aIt = aProps.begin(); aIt != aProps.end(); ++aIt) {
68 Config_Prop* aProp = (*aIt);
69 QString aVal = myResourceMgr->stringValue(QString(aProp->section().c_str()),
70 QString(aProp->name().c_str()));
71 if (!aVal.isEmpty()) {
72 aProp->setValue(aVal.toStdString());
77 void ModuleBase_Preferences::updateResourcesByConfig()
79 Config_Properties aProps = Config_PropManager::getProperties();
80 Config_Properties::iterator aIt;
81 for (aIt = aProps.begin(); aIt != aProps.end(); ++aIt) {
82 Config_Prop* aProp = (*aIt);
83 myResourceMgr->setValue(QString(aProp->section().c_str()), QString(aProp->name().c_str()),
84 QString(aProp->value().c_str()));
88 void ModuleBase_Preferences::resetConfig()
90 Config_Properties aProps = Config_PropManager::getProperties();
91 Config_Properties::iterator aIt;
92 for (aIt = aProps.begin(); aIt != aProps.end(); ++aIt) {
93 Config_Prop* aProp = (*aIt);
94 aProp->setValue(aProp->defaultValue());
98 void ModuleBase_Preferences::loadCustomProps()
102 QStringList aSections = myResourceMgr->sections();
103 foreach (QString aSection, aSections)
105 QStringList aParams = myResourceMgr->parameters(aSection);
106 foreach (QString aParam, aParams)
108 Config_Prop* aProp = Config_PropManager::registerProp(aSection.toStdString(),
109 aParam.toStdString(), "", Config_Prop::Disabled);
110 aProp->setValue(myResourceMgr->stringValue(aSection, aParam).toStdString());
116 void ModuleBase_Preferences::createEditContent(ModuleBase_IPrefMgr* thePref, int thePage)
118 thePref->prefMgr()->setItemIcon(thePage, QIcon(":pictures/module.png"));
119 createGeneralTab(thePref, thePage);
120 createCustomPage(thePref, thePage);
121 updateSketchTab(thePref, thePage);
124 void ModuleBase_Preferences::resetResourcePreferences(SUIT_PreferenceMgr* thePref)
129 QtxResourceMgr::WorkingMode aPrev =
130 thePref->resourceMgr()->setWorkingMode(QtxResourceMgr::IgnoreUserValues);
132 thePref->resourceMgr()->setWorkingMode(aPrev);
135 void ModuleBase_Preferences::resetConfigPropPreferences(SUIT_PreferenceMgr* thePref)
138 updateResourcesByConfig();
140 // retrieve the reset resource values to the preferences items
141 Config_Properties aProps = Config_PropManager::getProperties();
142 Config_Properties::iterator aIt;
144 QStringList aSections;
145 for (aIt = aProps.begin(); aIt != aProps.end(); ++aIt) {
146 Config_Prop* aProp = (*aIt);
147 aValues.append(QString(aProp->name().c_str()));
148 if (!aSections.contains(aProp->section().c_str()))
149 aSections.append(aProp->section().c_str());
150 QtxPreferenceItem* anItem = thePref->findItem(QString(aProp->title().c_str()), true);
156 void ModuleBase_Preferences::createGeneralTab(ModuleBase_IPrefMgr* thePref, int thePageId)
158 int generalTab = thePref->addPreference(QObject::tr("General"), thePageId,
159 SUIT_PreferenceMgr::Auto, QString(), QString());
160 thePref->setItemProperty("columns", 2, generalTab);
162 QStringList actItemList;
163 actItemList << QObject::tr("Last part")
164 << QObject::tr("All parts")
165 << QObject::tr("No activation");
167 QList<QVariant> actIdList;
168 actIdList << 0 << 1 << 2;
170 // Group related to opening a study
171 int group = thePref->addPreference(QObject::tr("Opening a study"), generalTab,
172 SUIT_PreferenceMgr::Auto, QString(), QString());
174 int actId = thePref->addPreference(QObject::tr("Activate"), group, SUIT_PreferenceMgr::Selector,
175 ModuleBase_Preferences::GENERAL_SECTION,
176 "part_activation_study");
177 thePref->setItemProperty("strings", actItemList, actId);
178 thePref->setItemProperty("indexes", actIdList, actId);
180 QStringList visuItemList;
181 visuItemList << QObject::tr("As stored in HDF")
182 << QObject::tr("Last item in each folder")
183 << QObject::tr("All items")
184 << QObject::tr("No visualization");
186 QList<QVariant> visuIdList;
187 visuIdList << 0 << 1 << 2 << 3;
189 int visuId = thePref->addPreference(QObject::tr("Display"), group, SUIT_PreferenceMgr::Selector,
190 ModuleBase_Preferences::GENERAL_SECTION,
191 "part_visualization_study");
192 thePref->setItemProperty("strings", visuItemList, visuId);
193 thePref->setItemProperty("indexes", visuIdList, visuId);
195 // Group related to running a python script
196 group = thePref->addPreference(QObject::tr("Launching a python script"), generalTab,
197 SUIT_PreferenceMgr::Auto, QString(), QString());
199 visuItemList.clear();
200 visuItemList << QObject::tr("Last item in each folder")
201 << QObject::tr("All items")
202 << QObject::tr("No visualization");
205 visuIdList << 0 << 1 << 2;
207 visuId = thePref->addPreference(QObject::tr("Display"), group, SUIT_PreferenceMgr::Selector,
208 ModuleBase_Preferences::GENERAL_SECTION,
209 "part_visualization_script");
210 thePref->setItemProperty("strings", visuItemList, visuId);
211 thePref->setItemProperty("indexes", visuIdList, visuId);
214 void ModuleBase_Preferences::updateSketchTab(ModuleBase_IPrefMgr* thePref, int thePageId)
216 int sketchTab = thePref->addPreference(QObject::tr("Sketch"), thePageId,
217 SUIT_PreferenceMgr::Auto, QString(), QString());
218 int allowChange = thePref->addPreference(
219 QObject::tr("Allow automatic constraint substitution/remove"),
220 sketchTab, SUIT_PreferenceMgr::GroupBox,
221 "Sketch", "allow_change_constraint");
222 thePref->addPreference(QObject::tr("Notify automatic constraint substitution/remove"),
223 allowChange, SUIT_PreferenceMgr::Bool, "Sketch", "notify_change_constraint");
226 void ModuleBase_Preferences::createCustomPage(ModuleBase_IPrefMgr* thePref, int thePageId)
228 SUIT_ResourceMgr* aResMgr = ModuleBase_Preferences::resourceMgr();
230 // Make a Tab from each section
231 std::list<std::string> aSections = Config_PropManager::getSections();
232 std::list<std::string>::const_iterator it;
233 for (it = aSections.cbegin(); it != aSections.cend(); ++it) {
234 Config_Properties aProps = Config_PropManager::getProperties(*it);
235 int aTab = thePref->prefMgr()->addItem(QString((*it).c_str()), thePageId);
236 thePref->prefMgr()->setItemProperty("columns", 2, aTab);
238 Config_Properties::const_iterator aIt;
239 for (aIt = aProps.cbegin(); aIt != aProps.cend(); ++aIt) {
240 Config_Prop* aProp = (*aIt);
241 // check that the property is defined
242 QString aSection(aProp->section().c_str());
243 QString aName(aProp->name().c_str());
244 if (!aResMgr->hasValue(aSection, aName))
245 aResMgr->setValue(aSection, aName, QString(aProp->value().c_str()));
248 if (aProp->type() != Config_Prop::Disabled) {
249 SUIT_PreferenceMgr::PrefItemType aPrefType = SUIT_PreferenceMgr::Auto;
250 switch (aProp->type()) {
251 case Config_Prop::Directory:
252 aPrefType = SUIT_PreferenceMgr::File;
254 case Config_Prop::Cursor:
255 aPrefType = SUIT_PreferenceMgr::Selector;
258 aPrefType = (SUIT_PreferenceMgr::PrefItemType) aProp->type();
261 int anId = thePref->addPreference(QObject::tr(aProp->title().c_str()), aTab, aPrefType,
262 QString::fromStdString(aProp->section()),
263 QString::fromStdString(aProp->name()));
265 switch (aProp->type()) {
266 case Config_Prop::Directory:
267 thePref->setItemProperty("path_type", Qtx::PT_Directory, anId);
269 case SUIT_PreferenceMgr::DblSpin:
270 if (aProp->min() != "") {
271 double aMin = QString(aProp->min().c_str()).toDouble();
272 thePref->setItemProperty("min", aMin, anId);
274 if (aProp->max() != "") {
275 double aMax = QString(aProp->max().c_str()).toDouble();
276 thePref->setItemProperty("max", aMax, anId);
279 case SUIT_PreferenceMgr::IntSpin:
280 if (aProp->min() != "") {
281 int aMin = QString(aProp->min().c_str()).toInt();
282 thePref->setItemProperty("min", aMin, anId);
284 if (aProp->max() != "") {
285 int aMax = QString(aProp->max().c_str()).toInt();
286 thePref->setItemProperty("max", aMax, anId);
289 case Config_Prop::Cursor:
291 QList<QVariant> aIndicesList;
292 QList<QVariant> aIconsList;
293 aIndicesList << 0 << 1 << 2;
294 aIconsList << QPixmap(":pictures/ArrowCursor.png") <<
295 QPixmap(":pictures/CrossCursor.png") <<
296 QPixmap(":pictures/HandCursor.png");
298 thePref->setItemProperty("indexes", aIndicesList, anId);
299 thePref->setItemProperty("icons", aIconsList, anId);
302 default: // [to avoid compilation warnings]
311 * Implementation of preferences manager interface
313 class ModuleBase_PrefMgr: public ModuleBase_IPrefMgr
317 /// \param theMgr a preferences manager
318 ModuleBase_PrefMgr(ModuleBase_PreferencesMgr* theMgr):myMgr(theMgr) {}
320 virtual int addPreference(const QString& theLbl, int pId,
321 SUIT_PreferenceMgr::PrefItemType theType,
322 const QString& theSection, const QString& theName )
324 return myMgr->addItem(theLbl, pId, theType, theSection, theName);
327 virtual void setItemProperty( const QString& thePropName, const QVariant& theValue,
328 const int theId = -1) {
329 myMgr->setItemProperty(thePropName, theValue, theId);
332 virtual SUIT_PreferenceMgr* prefMgr() const { return myMgr; }
335 ModuleBase_PreferencesMgr* myMgr;
338 //**********************************************************
339 //**********************************************************
340 //**********************************************************
341 ModuleBase_PreferencesDlg::ModuleBase_PreferencesDlg(SUIT_ResourceMgr* theResurces,
344 Qt::Dialog | Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::WindowCloseButtonHint),
347 setWindowTitle(tr("Edit preferences"));
349 QVBoxLayout* main = new QVBoxLayout(this);
353 myPreferences = new ModuleBase_PreferencesMgr(theResurces, this);
354 main->addWidget(myPreferences);
356 setFocusProxy(myPreferences);
357 myPreferences->setFrameStyle(QFrame::Box | QFrame::Sunken);
359 QDialogButtonBox* aBtnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel |
360 QDialogButtonBox::Reset,
361 Qt::Horizontal, this);
362 QPushButton* aDefaultButton = aBtnBox->button(QDialogButtonBox::Reset);
363 aDefaultButton->setText(tr("Default"));
364 connect(aDefaultButton, SIGNAL(clicked()), this, SLOT(onDefault()));
366 main->addWidget(aBtnBox);
367 connect(aBtnBox, SIGNAL(accepted()), this, SLOT(accept()));
368 connect(aBtnBox, SIGNAL(rejected()), this, SLOT(reject()));
371 myPreferences->retrieve();
374 ModuleBase_PreferencesDlg::~ModuleBase_PreferencesDlg()
378 void ModuleBase_PreferencesDlg::createEditors()
380 int aPage = myPreferences->addItem(tr("Desktop"));
381 myPreferences->setItemIcon(aPage, QIcon(":pictures/view_prefs.png"));
383 createMenuPage(aPage);
384 createViewerPage(aPage);
386 aPage = myPreferences->addItem(tr("Module"));
387 myPreferences->setItemIcon(aPage, QIcon(":pictures/module.png"));
389 ModuleBase_PrefMgr aMgr(myPreferences);
390 ModuleBase_Preferences::createEditContent(&aMgr, aPage);
393 void ModuleBase_PreferencesDlg::createViewerPage(int thePageId)
395 int viewTab = myPreferences->addItem(tr("Viewer"), thePageId);
397 QStringList gradList;
398 gradList << tr("Horizontal gradient") << tr("Vertical gradient") << tr("First diagonal gradient")
399 << tr("Second diagonal gradient") << tr("First corner gradient")
400 << tr("Second corner gradient") << tr("Third corner gradient")
401 << tr("Fourth corner gradient");
403 QList<QVariant> idList;
404 for (int i = 0; i < gradList.size(); i++)
407 int bgGroup = myPreferences->addItem(tr("Background"), viewTab);
409 QString aImgFiles("Image files (*.bmp *.gif *.pix *.xwd *.rgb *.rs)");
411 int bgId = myPreferences->addItem(tr("Viewer 3d"), bgGroup, SUIT_PreferenceMgr::Background,
412 ModuleBase_Preferences::VIEWER_SECTION, "background");
413 myPreferences->setItemProperty("gradient_names", gradList, bgId);
414 myPreferences->setItemProperty("gradient_ids", idList, bgId);
415 myPreferences->setItemProperty("texture_enabled", true, bgId);
416 myPreferences->setItemProperty("texture_center_enabled", true, bgId);
417 myPreferences->setItemProperty("texture_tile_enabled", true, bgId);
418 myPreferences->setItemProperty("texture_stretch_enabled", true, bgId);
419 myPreferences->setItemProperty("custom_enabled", false, bgId);
420 myPreferences->setItemProperty("image_formats", aImgFiles, bgId);
422 // Create other parameters group in viewer tab
423 int selectionGroup = myPreferences->addItem(tr("Default selection"), viewTab);
424 myPreferences->setItemProperty("columns", 3, selectionGroup);
425 myPreferences->addItem(tr("Faces"), selectionGroup,
426 SUIT_PreferenceMgr::Bool,
427 ModuleBase_Preferences::VIEWER_SECTION, "face-selection");
428 myPreferences->addItem(tr("Edges"), selectionGroup,
429 SUIT_PreferenceMgr::Bool,
430 ModuleBase_Preferences::VIEWER_SECTION, "edge-selection");
431 myPreferences->addItem(tr("Vertices"), selectionGroup,
432 SUIT_PreferenceMgr::Bool,
433 ModuleBase_Preferences::VIEWER_SECTION, "vertex-selection");
435 int sensitivityGroup = myPreferences->addItem(tr("Selection sensitivity"), viewTab);
436 myPreferences->setItemProperty("columns", 2, sensitivityGroup);
437 myPreferences->addItem(tr("Vertex"), sensitivityGroup, SUIT_PreferenceMgr::DblSpin,
438 ModuleBase_Preferences::VIEWER_SECTION, "point-selection-sensitivity");
439 myPreferences->addItem(tr("Edge"), sensitivityGroup, SUIT_PreferenceMgr::DblSpin,
440 ModuleBase_Preferences::VIEWER_SECTION, "edge-selection-sensitivity");
442 int highlightGroup = myPreferences->addItem(tr("Additional highlighting"), viewTab);
443 myPreferences->setItemProperty("columns", 2, highlightGroup);
444 myPreferences->addItem(tr("In 3d mode"), highlightGroup,
445 SUIT_PreferenceMgr::Bool, ModuleBase_Preferences::VIEWER_SECTION, "highlighting-3d");
446 myPreferences->addItem(tr("In 2d mode"), highlightGroup,
447 SUIT_PreferenceMgr::Bool, ModuleBase_Preferences::VIEWER_SECTION, "highlighting-2d");
449 int colorScaleGroup = myPreferences->addItem(tr("Color scale"), viewTab);
450 myPreferences->setItemProperty("columns", 4, colorScaleGroup);
451 int aItem = myPreferences->addItem(tr("X position"), colorScaleGroup,
452 SUIT_PreferenceMgr::Double, ModuleBase_Preferences::VIEWER_SECTION, "scalar_bar_x_position");
453 myPreferences->setItemProperty("min", 0, aItem);
454 myPreferences->setItemProperty("max", 1, aItem);
456 aItem = myPreferences->addItem(tr("Y position"), colorScaleGroup,
457 SUIT_PreferenceMgr::Double, ModuleBase_Preferences::VIEWER_SECTION, "scalar_bar_y_position");
458 myPreferences->setItemProperty("min", 0, aItem);
459 myPreferences->setItemProperty("max", 1, aItem);
461 aItem = myPreferences->addItem(tr("Width"), colorScaleGroup,
462 SUIT_PreferenceMgr::Double, ModuleBase_Preferences::VIEWER_SECTION, "scalar_bar_width");
463 myPreferences->setItemProperty("min", 0, aItem);
464 myPreferences->setItemProperty("max", 1, aItem);
466 aItem = myPreferences->addItem(tr("Height"), colorScaleGroup,
467 SUIT_PreferenceMgr::Double, ModuleBase_Preferences::VIEWER_SECTION, "scalar_bar_height");
468 myPreferences->setItemProperty("min", 0, aItem);
469 myPreferences->setItemProperty("max", 1, aItem);
471 aItem = myPreferences->addItem(tr("Intervals number"), colorScaleGroup,
472 SUIT_PreferenceMgr::Integer, ModuleBase_Preferences::VIEWER_SECTION, "scalar_bar_nb_intervals");
473 myPreferences->setItemProperty("min", 0, aItem);
474 myPreferences->setItemProperty("max", 100, aItem);
476 aItem = myPreferences->addItem(tr("Text height"), colorScaleGroup,
477 SUIT_PreferenceMgr::Integer, ModuleBase_Preferences::VIEWER_SECTION, "scalar_bar_text_height");
478 myPreferences->setItemProperty("min", 0, aItem);
479 myPreferences->setItemProperty("max", 100, aItem);
481 aItem = myPreferences->addItem(tr("Text color"), colorScaleGroup,
482 SUIT_PreferenceMgr::Color, ModuleBase_Preferences::VIEWER_SECTION, "scalar_bar_text_color");
485 void ModuleBase_PreferencesDlg::createMenuPage(int thePageId)
487 int aMenuTab = myPreferences->addItem(tr("Main menu"), thePageId);
489 int aSizeGroup = myPreferences->addItem(tr("Size"), aMenuTab);
490 myPreferences->setItemProperty("columns", 1, aSizeGroup);
492 int aRowsNb = myPreferences->addItem(tr("Number of rows"), aSizeGroup,
493 SUIT_PreferenceMgr::IntSpin,
494 ModuleBase_Preferences::MENU_SECTION,
496 myPreferences->setItemProperty("min", 1, aRowsNb);
497 myPreferences->setItemProperty("max", 6, aRowsNb);
499 myPreferences->addItem(tr("Show Status Bar"), aSizeGroup,
500 SUIT_PreferenceMgr::Bool, ModuleBase_Preferences::MENU_SECTION,
504 void ModuleBase_PreferencesDlg::accept()
506 myPreferences->store();
509 // Save custom properties
510 ModuleBase_Preferences::updateConfigByResources();
514 void ModuleBase_PreferencesDlg::modified(ModuleBase_Prefs& theModified) const
516 theModified = myPreferences->modified();
519 void ModuleBase_PreferencesDlg::onDefault()
521 // reset main resources. It throwns all resource manager items to the
522 // initial/default state. If there is no a default state of the item,
523 // it will be filled with an empty value. It concernerned to plugin
524 // config items, like visualization color. The main xml do not contains
525 // default values for them. So, it is important to reset the config
526 // properties after reseting the resources preferences.
527 ModuleBase_Preferences::resetResourcePreferences(myPreferences);
528 // reset plugin's resources. It fills the config resources with the default
529 // values, stores result in the resource manager and retrieve the preferences
530 // items with these values.
531 ModuleBase_Preferences::resetConfigPropPreferences(myPreferences);
534 void ModuleBase_PreferencesDlg::showEvent(QShowEvent* theEvent)
536 QDialog::showEvent(theEvent);
540 //**********************************************************
541 //**********************************************************
542 //**********************************************************
543 void ModuleBase_PreferencesMgr::changedResources(const ResourceMap& theMap)
546 ResourceMap::ConstIterator it;
548 for (it = theMap.begin(); it != theMap.end(); ++it) {
549 ModuleBase_Pref aPref;
550 it.key()->resource(aPref.first, aPref.second);
551 myModified.append(aPref);