]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_Preferences.cpp
Salome HOME
bos #26446 Customize study opening; bos #26447 Customize script execution
[modules/shaper.git] / src / ModuleBase / ModuleBase_Preferences.cpp
1 // Copyright (C) 2014-2021  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "ModuleBase_Preferences.h"
21 //#include "ModuleBase_Constants.h"
22
23 #include <Config_PropManager.h>
24
25 #include <TopAbs_ShapeEnum.hxx>
26
27 #include <SUIT_ResourceMgr.h>
28 #include <SUIT_PreferenceMgr.h>
29 #include <Qtx.h>
30
31 #include <QLayout>
32 #include <QApplication>
33 #include <QDialogButtonBox>
34 #include <QPushButton>
35
36 const QString ModuleBase_Preferences::VIEWER_SECTION = "Viewer";
37 const QString ModuleBase_Preferences::MENU_SECTION = "Menu";
38 const QString ModuleBase_Preferences::GENERAL_SECTION = "General";
39
40 SUIT_ResourceMgr* ModuleBase_Preferences::myResourceMgr = 0;
41
42 SUIT_ResourceMgr* ModuleBase_Preferences::resourceMgr()
43 {
44   if (!myResourceMgr) {
45     myResourceMgr = new SUIT_ResourceMgr("SHAPER");
46     myResourceMgr->setCurrentFormat("xml");
47   }
48   return myResourceMgr;
49 }
50
51 bool ModuleBase_Preferences::editPreferences(ModuleBase_Prefs& theModified)
52 {
53   ModuleBase_PreferencesDlg aDlg(resourceMgr(), QApplication::activeWindow());
54   aDlg.exec();
55   if (aDlg.isChanged()) {
56     aDlg.modified(theModified);
57     resourceMgr()->save();
58     return true;
59   }
60   return false;
61 }
62
63 void ModuleBase_Preferences::updateConfigByResources()
64 {
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());
73     }
74   }
75 }
76
77 void ModuleBase_Preferences::updateResourcesByConfig()
78 {
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()));
85   }
86 }
87
88 void ModuleBase_Preferences::resetConfig()
89 {
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());
95   }
96 }
97
98 void ModuleBase_Preferences::loadCustomProps()
99 {
100   if(!myResourceMgr)
101     return;
102   QStringList aSections = myResourceMgr->sections();
103   foreach (QString aSection, aSections)
104   {
105     QStringList aParams = myResourceMgr->parameters(aSection);
106     foreach (QString aParam, aParams)
107     {
108       Config_Prop* aProp = Config_PropManager::registerProp(aSection.toStdString(),
109                                        aParam.toStdString(), "", Config_Prop::Disabled);
110       aProp->setValue(myResourceMgr->stringValue(aSection, aParam).toStdString());
111     }
112   }
113 }
114
115
116 void ModuleBase_Preferences::createEditContent(ModuleBase_IPrefMgr* thePref, int thePage)
117 {
118   thePref->prefMgr()->setItemIcon(thePage, QIcon(":pictures/module.png"));
119   createGeneralTab(thePref, thePage);
120   createCustomPage(thePref, thePage);
121 }
122
123 void ModuleBase_Preferences::resetResourcePreferences(SUIT_PreferenceMgr* thePref)
124 {
125   if (!thePref)
126     return;
127
128   QtxResourceMgr::WorkingMode aPrev =
129     thePref->resourceMgr()->setWorkingMode(QtxResourceMgr::IgnoreUserValues);
130   thePref->retrieve();
131   thePref->resourceMgr()->setWorkingMode(aPrev);
132 }
133
134 void ModuleBase_Preferences::resetConfigPropPreferences(SUIT_PreferenceMgr* thePref)
135 {
136   resetConfig();
137   updateResourcesByConfig();
138
139   // retrieve the reset resource values to the preferences items
140   Config_Properties aProps = Config_PropManager::getProperties();
141   Config_Properties::iterator aIt;
142   QStringList aValues;
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);
150     if (anItem)
151       anItem->retrieve();
152   }
153 }
154
155 void ModuleBase_Preferences::createGeneralTab(ModuleBase_IPrefMgr* thePref, int thePageId)
156 {
157   int generalTab = thePref->addPreference(QObject::tr("General"), thePageId,
158                                           SUIT_PreferenceMgr::Auto, QString(), QString());
159   thePref->setItemProperty("columns", 2, generalTab);
160
161   QStringList actItemList;
162   actItemList << QObject::tr("Last part")
163               << QObject::tr("All parts")
164               << QObject::tr("No activation");
165
166   QList<QVariant> actIdList;
167   actIdList << 0 << 1 << 2;
168
169   // Group related to opening a study
170   int group = thePref->addPreference(QObject::tr("Opening a study"), generalTab,
171                                      SUIT_PreferenceMgr::Auto, QString(), QString());
172
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);
178
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");
184
185   QList<QVariant> visuIdList;
186   visuIdList << 0 << 1 << 2 << 3;
187
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);
193
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());
197
198   visuItemList.clear();
199   visuItemList << QObject::tr("Last item in each folder")
200                << QObject::tr("All items")
201                << QObject::tr("No visualization");
202
203   visuIdList.clear();
204   visuIdList << 0 << 1 << 2;
205
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);
211 }
212
213 void ModuleBase_Preferences::createCustomPage(ModuleBase_IPrefMgr* thePref, int thePageId)
214 {
215   SUIT_ResourceMgr* aResMgr = ModuleBase_Preferences::resourceMgr();
216
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);
224
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()));
233
234       // Add item
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;
240           break;
241         case Config_Prop::Cursor:
242           aPrefType = SUIT_PreferenceMgr::Selector;
243           break;
244         default:
245           aPrefType = (SUIT_PreferenceMgr::PrefItemType) aProp->type();
246         }
247
248         int anId = thePref->addPreference(QObject::tr(aProp->title().c_str()), aTab, aPrefType,
249                                           QString::fromStdString(aProp->section()),
250                                           QString::fromStdString(aProp->name()));
251
252         switch (aProp->type()) {
253         case Config_Prop::Directory:
254           thePref->setItemProperty("path_type", Qtx::PT_Directory, anId);
255           break;
256         case SUIT_PreferenceMgr::DblSpin:
257           if (aProp->min() != "") {
258             double aMin = QString(aProp->min().c_str()).toDouble();
259             thePref->setItemProperty("min", aMin, anId);
260           }
261           if (aProp->max() != "") {
262             double aMax = QString(aProp->max().c_str()).toDouble();
263             thePref->setItemProperty("max", aMax, anId);
264           }
265           break;
266         case SUIT_PreferenceMgr::IntSpin:
267           if (aProp->min() != "") {
268             int aMin = QString(aProp->min().c_str()).toInt();
269             thePref->setItemProperty("min", aMin, anId);
270           }
271           if (aProp->max() != "") {
272             int aMax = QString(aProp->max().c_str()).toInt();
273             thePref->setItemProperty("max", aMax, anId);
274           }
275           break;
276         case Config_Prop::Cursor:
277           {
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");
284
285             thePref->setItemProperty("indexes", aIndicesList, anId);
286             thePref->setItemProperty("icons", aIconsList, anId);
287           }
288           break;
289         default: // [to avoid compilation warnings]
290           break;
291         }
292       }
293     }
294   }
295 }
296
297 /**
298 * Implementation of preferences manager interface
299 */
300 class ModuleBase_PrefMgr: public ModuleBase_IPrefMgr
301 {
302 public:
303   /// Constructor
304   /// \param theMgr a preferences manager
305   ModuleBase_PrefMgr(ModuleBase_PreferencesMgr* theMgr):myMgr(theMgr) {}
306
307   virtual int addPreference(const QString& theLbl, int pId,
308                             SUIT_PreferenceMgr::PrefItemType theType,
309                             const QString& theSection, const QString& theName )
310   {
311     return myMgr->addItem(theLbl, pId, theType, theSection, theName);
312   }
313
314   virtual void setItemProperty( const QString& thePropName, const QVariant& theValue,
315                                const int theId = -1) {
316     myMgr->setItemProperty(thePropName, theValue, theId);
317   }
318
319   virtual SUIT_PreferenceMgr* prefMgr() const { return myMgr; }
320
321 private:
322   ModuleBase_PreferencesMgr* myMgr;
323 };
324
325 //**********************************************************
326 //**********************************************************
327 //**********************************************************
328 ModuleBase_PreferencesDlg::ModuleBase_PreferencesDlg(SUIT_ResourceMgr* theResurces,
329   QWidget* theParent)
330     : QDialog(theParent,
331       Qt::Dialog | Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::WindowCloseButtonHint),
332       myIsChanged(false)
333 {
334   setWindowTitle(tr("Edit preferences"));
335
336   QVBoxLayout* main = new QVBoxLayout(this);
337   main->setMargin(5);
338   main->setSpacing(5);
339
340   myPreferences = new ModuleBase_PreferencesMgr(theResurces, this);
341   main->addWidget(myPreferences);
342
343   setFocusProxy(myPreferences);
344   myPreferences->setFrameStyle(QFrame::Box | QFrame::Sunken);
345
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()));
352
353   main->addWidget(aBtnBox);
354   connect(aBtnBox, SIGNAL(accepted()), this, SLOT(accept()));
355   connect(aBtnBox, SIGNAL(rejected()), this, SLOT(reject()));
356   createEditors();
357
358   myPreferences->retrieve();
359 }
360
361 ModuleBase_PreferencesDlg::~ModuleBase_PreferencesDlg()
362 {
363 }
364
365 void ModuleBase_PreferencesDlg::createEditors()
366 {
367   int aPage = myPreferences->addItem(tr("Desktop"));
368   myPreferences->setItemIcon(aPage, QIcon(":pictures/view_prefs.png"));
369
370   createMenuPage(aPage);
371   createViewerPage(aPage);
372
373   aPage = myPreferences->addItem(tr("Module"));
374   myPreferences->setItemIcon(aPage, QIcon(":pictures/module.png"));
375
376   ModuleBase_PrefMgr aMgr(myPreferences);
377   ModuleBase_Preferences::createEditContent(&aMgr, aPage);
378 }
379
380 void ModuleBase_PreferencesDlg::createViewerPage(int thePageId)
381 {
382   int viewTab = myPreferences->addItem(tr("Viewer"), thePageId);
383
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");
389
390   QList<QVariant> idList;
391   for (int i = 0; i < gradList.size(); i++)
392     idList << i;
393
394   int bgGroup = myPreferences->addItem(tr("Background"), viewTab);
395
396   QString aImgFiles("Image files (*.bmp *.gif *.pix *.xwd *.rgb *.rs)");
397
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);
408
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");
421
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");
428
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");
435
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);
442
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);
447
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);
452
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);
457
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);
462
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);
467
468   aItem = myPreferences->addItem(tr("Text color"), colorScaleGroup,
469     SUIT_PreferenceMgr::Color, ModuleBase_Preferences::VIEWER_SECTION, "scalar_bar_text_color");
470 }
471
472 void ModuleBase_PreferencesDlg::createMenuPage(int thePageId)
473 {
474   int aMenuTab = myPreferences->addItem(tr("Main menu"), thePageId);
475
476   int aSizeGroup = myPreferences->addItem(tr("Size"), aMenuTab);
477   myPreferences->setItemProperty("columns", 1, aSizeGroup);
478
479   int aRowsNb = myPreferences->addItem(tr("Number of rows"), aSizeGroup,
480                                        SUIT_PreferenceMgr::IntSpin,
481                                        ModuleBase_Preferences::MENU_SECTION,
482                                        "rows_number");
483   myPreferences->setItemProperty("min", 1, aRowsNb);
484   myPreferences->setItemProperty("max", 6, aRowsNb);
485
486   myPreferences->addItem(tr("Show Status Bar"), aSizeGroup,
487                          SUIT_PreferenceMgr::Bool, ModuleBase_Preferences::MENU_SECTION,
488                          "status_bar");
489 }
490
491 void ModuleBase_PreferencesDlg::accept()
492 {
493   myPreferences->store();
494   myIsChanged = true;
495
496   // Save custom properties
497   ModuleBase_Preferences::updateConfigByResources();
498   QDialog::accept();
499 }
500
501 void ModuleBase_PreferencesDlg::modified(ModuleBase_Prefs& theModified) const
502 {
503   theModified = myPreferences->modified();
504 }
505
506 void ModuleBase_PreferencesDlg::onDefault()
507 {
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);
519 }
520
521 void ModuleBase_PreferencesDlg::showEvent(QShowEvent* theEvent)
522 {
523   QDialog::showEvent(theEvent);
524   adjustSize();
525 }
526
527 //**********************************************************
528 //**********************************************************
529 //**********************************************************
530 void ModuleBase_PreferencesMgr::changedResources(const ResourceMap& theMap)
531 {
532   myModified.clear();
533   ResourceMap::ConstIterator it;
534   QString sec, param;
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);
539   }
540 }