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);
123 void ModuleBase_Preferences::resetResourcePreferences(SUIT_PreferenceMgr* thePref)
128 QtxResourceMgr::WorkingMode aPrev =
129 thePref->resourceMgr()->setWorkingMode(QtxResourceMgr::IgnoreUserValues);
131 thePref->resourceMgr()->setWorkingMode(aPrev);
134 void ModuleBase_Preferences::resetConfigPropPreferences(SUIT_PreferenceMgr* thePref)
137 updateResourcesByConfig();
139 // retrieve the reset resource values to the preferences items
140 Config_Properties aProps = Config_PropManager::getProperties();
141 Config_Properties::iterator aIt;
143 QStringList aSections;
144 for (aIt = aProps.begin(); aIt != aProps.end(); ++aIt) {
145 Config_Prop* aProp = (*aIt);
146 aValues.append(QString(aProp->name().c_str()));
147 if (!aSections.contains(aProp->section().c_str()))
148 aSections.append(aProp->section().c_str());
149 QtxPreferenceItem* anItem = thePref->findItem(QString(aProp->title().c_str()), true);
155 void ModuleBase_Preferences::createGeneralTab(ModuleBase_IPrefMgr* thePref, int thePageId)
157 int generalTab = thePref->addPreference(QObject::tr("General"), thePageId,
158 SUIT_PreferenceMgr::Auto, QString(), QString());
159 thePref->setItemProperty("columns", 2, generalTab);
161 QStringList actItemList;
162 actItemList << QObject::tr("Last part")
163 << QObject::tr("All parts")
164 << QObject::tr("No activation");
166 QList<QVariant> actIdList;
167 actIdList << 0 << 1 << 2;
169 // Group related to opening a study
170 int group = thePref->addPreference(QObject::tr("Opening a study"), generalTab,
171 SUIT_PreferenceMgr::Auto, QString(), QString());
173 int actId = thePref->addPreference(QObject::tr("Activate"), group, SUIT_PreferenceMgr::Selector,
174 ModuleBase_Preferences::GENERAL_SECTION,
175 "part_activation_study");
176 thePref->setItemProperty("strings", actItemList, actId);
177 thePref->setItemProperty("indexes", actIdList, actId);
179 QStringList visuItemList;
180 visuItemList << QObject::tr("As stored in HDF")
181 << QObject::tr("Last item in each folder")
182 << QObject::tr("All items")
183 << QObject::tr("No visualization");
185 QList<QVariant> visuIdList;
186 visuIdList << 0 << 1 << 2 << 3;
188 int visuId = thePref->addPreference(QObject::tr("Display"), group, SUIT_PreferenceMgr::Selector,
189 ModuleBase_Preferences::GENERAL_SECTION,
190 "part_visualization_study");
191 thePref->setItemProperty("strings", visuItemList, visuId);
192 thePref->setItemProperty("indexes", visuIdList, visuId);
194 // Group related to running a python script
195 group = thePref->addPreference(QObject::tr("Launching a python script"), generalTab,
196 SUIT_PreferenceMgr::Auto, QString(), QString());
198 visuItemList.clear();
199 visuItemList << QObject::tr("Last item in each folder")
200 << QObject::tr("All items")
201 << QObject::tr("No visualization");
204 visuIdList << 0 << 1 << 2;
206 visuId = thePref->addPreference(QObject::tr("Display"), group, SUIT_PreferenceMgr::Selector,
207 ModuleBase_Preferences::GENERAL_SECTION,
208 "part_visualization_script");
209 thePref->setItemProperty("strings", visuItemList, visuId);
210 thePref->setItemProperty("indexes", visuIdList, visuId);
213 void ModuleBase_Preferences::createCustomPage(ModuleBase_IPrefMgr* thePref, int thePageId)
215 SUIT_ResourceMgr* aResMgr = ModuleBase_Preferences::resourceMgr();
217 // Make a Tab from each section
218 std::list<std::string> aSections = Config_PropManager::getSections();
219 std::list<std::string>::const_iterator it;
220 for (it = aSections.cbegin(); it != aSections.cend(); ++it) {
221 Config_Properties aProps = Config_PropManager::getProperties(*it);
222 int aTab = thePref->prefMgr()->addItem(QString((*it).c_str()), thePageId);
223 thePref->prefMgr()->setItemProperty("columns", 2, aTab);
225 Config_Properties::const_iterator aIt;
226 for (aIt = aProps.cbegin(); aIt != aProps.cend(); ++aIt) {
227 Config_Prop* aProp = (*aIt);
228 // check that the property is defined
229 QString aSection(aProp->section().c_str());
230 QString aName(aProp->name().c_str());
231 if (!aResMgr->hasValue(aSection, aName))
232 aResMgr->setValue(aSection, aName, QString(aProp->value().c_str()));
235 if (aProp->type() != Config_Prop::Disabled) {
236 SUIT_PreferenceMgr::PrefItemType aPrefType = SUIT_PreferenceMgr::Auto;
237 switch (aProp->type()) {
238 case Config_Prop::Directory:
239 aPrefType = SUIT_PreferenceMgr::File;
241 case Config_Prop::Cursor:
242 aPrefType = SUIT_PreferenceMgr::Selector;
245 aPrefType = (SUIT_PreferenceMgr::PrefItemType) aProp->type();
248 int anId = thePref->addPreference(QObject::tr(aProp->title().c_str()), aTab, aPrefType,
249 QString::fromStdString(aProp->section()),
250 QString::fromStdString(aProp->name()));
252 switch (aProp->type()) {
253 case Config_Prop::Directory:
254 thePref->setItemProperty("path_type", Qtx::PT_Directory, anId);
256 case SUIT_PreferenceMgr::DblSpin:
257 if (aProp->min() != "") {
258 double aMin = QString(aProp->min().c_str()).toDouble();
259 thePref->setItemProperty("min", aMin, anId);
261 if (aProp->max() != "") {
262 double aMax = QString(aProp->max().c_str()).toDouble();
263 thePref->setItemProperty("max", aMax, anId);
266 case SUIT_PreferenceMgr::IntSpin:
267 if (aProp->min() != "") {
268 int aMin = QString(aProp->min().c_str()).toInt();
269 thePref->setItemProperty("min", aMin, anId);
271 if (aProp->max() != "") {
272 int aMax = QString(aProp->max().c_str()).toInt();
273 thePref->setItemProperty("max", aMax, anId);
276 case Config_Prop::Cursor:
278 QList<QVariant> aIndicesList;
279 QList<QVariant> aIconsList;
280 aIndicesList << 0 << 1 << 2;
281 aIconsList << QPixmap(":pictures/ArrowCursor.png") <<
282 QPixmap(":pictures/CrossCursor.png") <<
283 QPixmap(":pictures/HandCursor.png");
285 thePref->setItemProperty("indexes", aIndicesList, anId);
286 thePref->setItemProperty("icons", aIconsList, anId);
289 default: // [to avoid compilation warnings]
298 * Implementation of preferences manager interface
300 class ModuleBase_PrefMgr: public ModuleBase_IPrefMgr
304 /// \param theMgr a preferences manager
305 ModuleBase_PrefMgr(ModuleBase_PreferencesMgr* theMgr):myMgr(theMgr) {}
307 virtual int addPreference(const QString& theLbl, int pId,
308 SUIT_PreferenceMgr::PrefItemType theType,
309 const QString& theSection, const QString& theName )
311 return myMgr->addItem(theLbl, pId, theType, theSection, theName);
314 virtual void setItemProperty( const QString& thePropName, const QVariant& theValue,
315 const int theId = -1) {
316 myMgr->setItemProperty(thePropName, theValue, theId);
319 virtual SUIT_PreferenceMgr* prefMgr() const { return myMgr; }
322 ModuleBase_PreferencesMgr* myMgr;
325 //**********************************************************
326 //**********************************************************
327 //**********************************************************
328 ModuleBase_PreferencesDlg::ModuleBase_PreferencesDlg(SUIT_ResourceMgr* theResurces,
331 Qt::Dialog | Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::WindowCloseButtonHint),
334 setWindowTitle(tr("Edit preferences"));
336 QVBoxLayout* main = new QVBoxLayout(this);
340 myPreferences = new ModuleBase_PreferencesMgr(theResurces, this);
341 main->addWidget(myPreferences);
343 setFocusProxy(myPreferences);
344 myPreferences->setFrameStyle(QFrame::Box | QFrame::Sunken);
346 QDialogButtonBox* aBtnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel |
347 QDialogButtonBox::Reset,
348 Qt::Horizontal, this);
349 QPushButton* aDefaultButton = aBtnBox->button(QDialogButtonBox::Reset);
350 aDefaultButton->setText(tr("Default"));
351 connect(aDefaultButton, SIGNAL(clicked()), this, SLOT(onDefault()));
353 main->addWidget(aBtnBox);
354 connect(aBtnBox, SIGNAL(accepted()), this, SLOT(accept()));
355 connect(aBtnBox, SIGNAL(rejected()), this, SLOT(reject()));
358 myPreferences->retrieve();
361 ModuleBase_PreferencesDlg::~ModuleBase_PreferencesDlg()
365 void ModuleBase_PreferencesDlg::createEditors()
367 int aPage = myPreferences->addItem(tr("Desktop"));
368 myPreferences->setItemIcon(aPage, QIcon(":pictures/view_prefs.png"));
370 createMenuPage(aPage);
371 createViewerPage(aPage);
373 aPage = myPreferences->addItem(tr("Module"));
374 myPreferences->setItemIcon(aPage, QIcon(":pictures/module.png"));
376 ModuleBase_PrefMgr aMgr(myPreferences);
377 ModuleBase_Preferences::createEditContent(&aMgr, aPage);
380 void ModuleBase_PreferencesDlg::createViewerPage(int thePageId)
382 int viewTab = myPreferences->addItem(tr("Viewer"), thePageId);
384 QStringList gradList;
385 gradList << tr("Horizontal gradient") << tr("Vertical gradient") << tr("First diagonal gradient")
386 << tr("Second diagonal gradient") << tr("First corner gradient")
387 << tr("Second corner gradient") << tr("Third corner gradient")
388 << tr("Fourth corner gradient");
390 QList<QVariant> idList;
391 for (int i = 0; i < gradList.size(); i++)
394 int bgGroup = myPreferences->addItem(tr("Background"), viewTab);
396 QString aImgFiles("Image files (*.bmp *.gif *.pix *.xwd *.rgb *.rs)");
398 int bgId = myPreferences->addItem(tr("Viewer 3d"), bgGroup, SUIT_PreferenceMgr::Background,
399 ModuleBase_Preferences::VIEWER_SECTION, "background");
400 myPreferences->setItemProperty("gradient_names", gradList, bgId);
401 myPreferences->setItemProperty("gradient_ids", idList, bgId);
402 myPreferences->setItemProperty("texture_enabled", true, bgId);
403 myPreferences->setItemProperty("texture_center_enabled", true, bgId);
404 myPreferences->setItemProperty("texture_tile_enabled", true, bgId);
405 myPreferences->setItemProperty("texture_stretch_enabled", true, bgId);
406 myPreferences->setItemProperty("custom_enabled", false, bgId);
407 myPreferences->setItemProperty("image_formats", aImgFiles, bgId);
409 // Create other parameters group in viewer tab
410 int selectionGroup = myPreferences->addItem(tr("Default selection"), viewTab);
411 myPreferences->setItemProperty("columns", 3, selectionGroup);
412 myPreferences->addItem(tr("Faces"), selectionGroup,
413 SUIT_PreferenceMgr::Bool,
414 ModuleBase_Preferences::VIEWER_SECTION, "face-selection");
415 myPreferences->addItem(tr("Edges"), selectionGroup,
416 SUIT_PreferenceMgr::Bool,
417 ModuleBase_Preferences::VIEWER_SECTION, "edge-selection");
418 myPreferences->addItem(tr("Vertices"), selectionGroup,
419 SUIT_PreferenceMgr::Bool,
420 ModuleBase_Preferences::VIEWER_SECTION, "vertex-selection");
422 int sensitivityGroup = myPreferences->addItem(tr("Selection sensitivity"), viewTab);
423 myPreferences->setItemProperty("columns", 2, sensitivityGroup);
424 myPreferences->addItem(tr("Vertex"), sensitivityGroup, SUIT_PreferenceMgr::DblSpin,
425 ModuleBase_Preferences::VIEWER_SECTION, "point-selection-sensitivity");
426 myPreferences->addItem(tr("Edge"), sensitivityGroup, SUIT_PreferenceMgr::DblSpin,
427 ModuleBase_Preferences::VIEWER_SECTION, "edge-selection-sensitivity");
429 int highlightGroup = myPreferences->addItem(tr("Additional highlighting"), viewTab);
430 myPreferences->setItemProperty("columns", 2, highlightGroup);
431 myPreferences->addItem(tr("In 3d mode"), highlightGroup,
432 SUIT_PreferenceMgr::Bool, ModuleBase_Preferences::VIEWER_SECTION, "highlighting-3d");
433 myPreferences->addItem(tr("In 2d mode"), highlightGroup,
434 SUIT_PreferenceMgr::Bool, ModuleBase_Preferences::VIEWER_SECTION, "highlighting-2d");
436 int colorScaleGroup = myPreferences->addItem(tr("Color scale"), viewTab);
437 myPreferences->setItemProperty("columns", 4, colorScaleGroup);
438 int aItem = myPreferences->addItem(tr("X position"), colorScaleGroup,
439 SUIT_PreferenceMgr::Double, ModuleBase_Preferences::VIEWER_SECTION, "scalar_bar_x_position");
440 myPreferences->setItemProperty("min", 0, aItem);
441 myPreferences->setItemProperty("max", 1, aItem);
443 aItem = myPreferences->addItem(tr("Y position"), colorScaleGroup,
444 SUIT_PreferenceMgr::Double, ModuleBase_Preferences::VIEWER_SECTION, "scalar_bar_y_position");
445 myPreferences->setItemProperty("min", 0, aItem);
446 myPreferences->setItemProperty("max", 1, aItem);
448 aItem = myPreferences->addItem(tr("Width"), colorScaleGroup,
449 SUIT_PreferenceMgr::Double, ModuleBase_Preferences::VIEWER_SECTION, "scalar_bar_width");
450 myPreferences->setItemProperty("min", 0, aItem);
451 myPreferences->setItemProperty("max", 1, aItem);
453 aItem = myPreferences->addItem(tr("Height"), colorScaleGroup,
454 SUIT_PreferenceMgr::Double, ModuleBase_Preferences::VIEWER_SECTION, "scalar_bar_height");
455 myPreferences->setItemProperty("min", 0, aItem);
456 myPreferences->setItemProperty("max", 1, aItem);
458 aItem = myPreferences->addItem(tr("Intervals number"), colorScaleGroup,
459 SUIT_PreferenceMgr::Integer, ModuleBase_Preferences::VIEWER_SECTION, "scalar_bar_nb_intervals");
460 myPreferences->setItemProperty("min", 0, aItem);
461 myPreferences->setItemProperty("max", 100, aItem);
463 aItem = myPreferences->addItem(tr("Text height"), colorScaleGroup,
464 SUIT_PreferenceMgr::Integer, ModuleBase_Preferences::VIEWER_SECTION, "scalar_bar_text_height");
465 myPreferences->setItemProperty("min", 0, aItem);
466 myPreferences->setItemProperty("max", 100, aItem);
468 aItem = myPreferences->addItem(tr("Text color"), colorScaleGroup,
469 SUIT_PreferenceMgr::Color, ModuleBase_Preferences::VIEWER_SECTION, "scalar_bar_text_color");
472 void ModuleBase_PreferencesDlg::createMenuPage(int thePageId)
474 int aMenuTab = myPreferences->addItem(tr("Main menu"), thePageId);
476 int aSizeGroup = myPreferences->addItem(tr("Size"), aMenuTab);
477 myPreferences->setItemProperty("columns", 1, aSizeGroup);
479 int aRowsNb = myPreferences->addItem(tr("Number of rows"), aSizeGroup,
480 SUIT_PreferenceMgr::IntSpin,
481 ModuleBase_Preferences::MENU_SECTION,
483 myPreferences->setItemProperty("min", 1, aRowsNb);
484 myPreferences->setItemProperty("max", 6, aRowsNb);
486 myPreferences->addItem(tr("Show Status Bar"), aSizeGroup,
487 SUIT_PreferenceMgr::Bool, ModuleBase_Preferences::MENU_SECTION,
491 void ModuleBase_PreferencesDlg::accept()
493 myPreferences->store();
496 // Save custom properties
497 ModuleBase_Preferences::updateConfigByResources();
501 void ModuleBase_PreferencesDlg::modified(ModuleBase_Prefs& theModified) const
503 theModified = myPreferences->modified();
506 void ModuleBase_PreferencesDlg::onDefault()
508 // reset main resources. It throwns all resource manager items to the
509 // initial/default state. If there is no a default state of the item,
510 // it will be filled with an empty value. It concernerned to plugin
511 // config items, like visualization color. The main xml do not contains
512 // default values for them. So, it is important to reset the config
513 // properties after reseting the resources preferences.
514 ModuleBase_Preferences::resetResourcePreferences(myPreferences);
515 // reset plugin's resources. It fills the config resources with the default
516 // values, stores result in the resource manager and retrieve the preferences
517 // items with these values.
518 ModuleBase_Preferences::resetConfigPropPreferences(myPreferences);
521 void ModuleBase_PreferencesDlg::showEvent(QShowEvent* theEvent)
523 QDialog::showEvent(theEvent);
527 //**********************************************************
528 //**********************************************************
529 //**********************************************************
530 void ModuleBase_PreferencesMgr::changedResources(const ResourceMap& theMap)
533 ResourceMap::ConstIterator it;
535 for (it = theMap.begin(); it != theMap.end(); ++it) {
536 ModuleBase_Pref aPref;
537 it.key()->resource(aPref.first, aPref.second);
538 myModified.append(aPref);