Salome HOME
Issue #2998: Add help description for automatic creation of constraints
[modules/shaper.git] / src / ModuleBase / ModuleBase_Preferences.cpp
1 // Copyright (C) 2014-2019  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
39 SUIT_ResourceMgr* ModuleBase_Preferences::myResourceMgr = 0;
40
41 SUIT_ResourceMgr* ModuleBase_Preferences::resourceMgr()
42 {
43   if (!myResourceMgr) {
44     myResourceMgr = new SUIT_ResourceMgr("SHAPER");
45     myResourceMgr->setCurrentFormat("xml");
46   }
47   return myResourceMgr;
48 }
49
50 bool ModuleBase_Preferences::editPreferences(ModuleBase_Prefs& theModified)
51 {
52   ModuleBase_PreferencesDlg aDlg(resourceMgr(), QApplication::activeWindow());
53   aDlg.exec();
54   if (aDlg.isChanged()) {
55     aDlg.modified(theModified);
56     resourceMgr()->save();
57     return true;
58   }
59   return false;
60 }
61
62 void ModuleBase_Preferences::updateConfigByResources()
63 {
64   Config_Properties aProps = Config_PropManager::getProperties();
65   Config_Properties::iterator aIt;
66   for (aIt = aProps.begin(); aIt != aProps.end(); ++aIt) {
67     Config_Prop* aProp = (*aIt);
68     QString aVal = myResourceMgr->stringValue(QString(aProp->section().c_str()),
69                                               QString(aProp->name().c_str()));
70     if (!aVal.isEmpty()) {
71       aProp->setValue(aVal.toStdString());
72     }
73   }
74 }
75
76 void ModuleBase_Preferences::updateResourcesByConfig()
77 {
78   Config_Properties aProps = Config_PropManager::getProperties();
79   Config_Properties::iterator aIt;
80   for (aIt = aProps.begin(); aIt != aProps.end(); ++aIt) {
81     Config_Prop* aProp = (*aIt);
82     myResourceMgr->setValue(QString(aProp->section().c_str()), QString(aProp->name().c_str()),
83                             QString(aProp->value().c_str()));
84   }
85 }
86
87 void ModuleBase_Preferences::resetConfig()
88 {
89   Config_Properties aProps = Config_PropManager::getProperties();
90   Config_Properties::iterator aIt;
91   for (aIt = aProps.begin(); aIt != aProps.end(); ++aIt) {
92     Config_Prop* aProp = (*aIt);
93     aProp->setValue(aProp->defaultValue());
94   }
95 }
96
97 void ModuleBase_Preferences::loadCustomProps()
98 {
99   if(!myResourceMgr)
100     return;
101   QStringList aSections = myResourceMgr->sections();
102   foreach (QString aSection, aSections)
103   {
104     QStringList aParams = myResourceMgr->parameters(aSection);
105     foreach (QString aParam, aParams)
106     {
107       Config_Prop* aProp = Config_PropManager::registerProp(aSection.toStdString(),
108                                        aParam.toStdString(), "", Config_Prop::Disabled);
109       aProp->setValue(myResourceMgr->stringValue(aSection, aParam).toStdString());
110     }
111   }
112 }
113
114
115 void ModuleBase_Preferences::createEditContent(ModuleBase_IPrefMgr* thePref, int thePage)
116 {
117   thePref->prefMgr()->setItemIcon(thePage, QIcon(":pictures/module.png"));
118   createCustomPage(thePref, thePage);
119 }
120
121 void ModuleBase_Preferences::resetResourcePreferences(SUIT_PreferenceMgr* thePref)
122 {
123   if (!thePref)
124     return;
125
126   QtxResourceMgr::WorkingMode aPrev =
127     thePref->resourceMgr()->setWorkingMode(QtxResourceMgr::IgnoreUserValues);
128   thePref->retrieve();
129   thePref->resourceMgr()->setWorkingMode(aPrev);
130 }
131
132 void ModuleBase_Preferences::resetConfigPropPreferences(SUIT_PreferenceMgr* thePref)
133 {
134   resetConfig();
135   updateResourcesByConfig();
136
137   // retrieve the reset resource values to the preferences items
138   Config_Properties aProps = Config_PropManager::getProperties();
139   Config_Properties::iterator aIt;
140   QStringList aValues;
141   QStringList aSections;
142   for (aIt = aProps.begin(); aIt != aProps.end(); ++aIt) {
143     Config_Prop* aProp = (*aIt);
144     aValues.append(QString(aProp->name().c_str()));
145     if (!aSections.contains(aProp->section().c_str()))
146       aSections.append(aProp->section().c_str());
147     QtxPreferenceItem* anItem = thePref->findItem(QString(aProp->title().c_str()), true);
148     if (anItem)
149       anItem->retrieve();
150   }
151 }
152
153 void ModuleBase_Preferences::createCustomPage(ModuleBase_IPrefMgr* thePref, int thePageId)
154 {
155   SUIT_ResourceMgr* aResMgr = ModuleBase_Preferences::resourceMgr();
156   bool isResModified = false;
157
158   // Make a Tab from each section
159   std::list<std::string> aSections = Config_PropManager::getSections();
160   std::list<std::string>::const_iterator it;
161   for (it = aSections.cbegin(); it != aSections.cend(); ++it) {
162     Config_Properties aProps = Config_PropManager::getProperties(*it);
163     int aTab = thePref->prefMgr()->addItem(QString((*it).c_str()), thePageId);
164     thePref->prefMgr()->setItemProperty("columns", 2, aTab);
165
166     Config_Properties::const_iterator aIt;
167     for (aIt = aProps.cbegin(); aIt != aProps.cend(); ++aIt) {
168       Config_Prop* aProp = (*aIt);
169       // check that the property is defined
170       QString aSection(aProp->section().c_str());
171       QString aName(aProp->name().c_str());
172       if (!aResMgr->hasValue(aSection, aName)) {
173         aResMgr->setValue(aSection, aName, QString(aProp->value().c_str()));
174         isResModified = true;
175       }
176       // Add item
177       if (aProp->type() != Config_Prop::Disabled) {
178         SUIT_PreferenceMgr::PrefItemType aPrefType = SUIT_PreferenceMgr::Auto;
179         if (aProp->type() == Config_Prop::Directory) {
180           aPrefType = SUIT_PreferenceMgr::File;
181         } else {
182           aPrefType = (SUIT_PreferenceMgr::PrefItemType) aProp->type();
183         }
184         int anId = thePref->addPreference(QObject::tr(aProp->title().c_str()), aTab, aPrefType,
185                                           QString::fromStdString(aProp->section()),
186                                           QString::fromStdString(aProp->name()));
187         if(aProp->type() == Config_Prop::Directory) {
188           thePref->setItemProperty("path_type", Qtx::PT_Directory, anId);
189         }
190         if (aPrefType == SUIT_PreferenceMgr::DblSpin) {
191           if (aProp->min() != "") {
192             double aMin = QString(aProp->min().c_str()).toDouble();
193             thePref->setItemProperty("min", aMin, anId);
194           }
195           if (aProp->max() != "") {
196             double aMax = QString(aProp->max().c_str()).toDouble();
197             thePref->setItemProperty("max", aMax, anId);
198           }
199         }
200         if (aPrefType == SUIT_PreferenceMgr::IntSpin) {
201           if (aProp->min() != "") {
202             int aMin = QString(aProp->min().c_str()).toInt();
203             thePref->setItemProperty("min", aMin, anId);
204           }
205           if (aProp->max() != "") {
206             int aMax = QString(aProp->max().c_str()).toInt();
207             thePref->setItemProperty("max", aMax, anId);
208           }
209         }
210       }
211     }
212   }
213 }
214
215 /**
216 * Implementation of preferences manager interface
217 */
218 class ModuleBase_PrefMgr: public ModuleBase_IPrefMgr
219 {
220 public:
221   /// Constructor
222   /// \param theMgr a preferences manager
223   ModuleBase_PrefMgr(ModuleBase_PreferencesMgr* theMgr):myMgr(theMgr) {}
224
225   virtual int addPreference(const QString& theLbl, int pId,
226                             SUIT_PreferenceMgr::PrefItemType theType,
227                             const QString& theSection, const QString& theName )
228   {
229     return myMgr->addItem(theLbl, pId, theType, theSection, theName);
230   }
231
232   virtual void setItemProperty( const QString& thePropName, const QVariant& theValue,
233                                const int theId = -1) {
234     myMgr->setItemProperty(thePropName, theValue, theId);
235   }
236
237   virtual SUIT_PreferenceMgr* prefMgr() const { return myMgr; }
238
239 private:
240   ModuleBase_PreferencesMgr* myMgr;
241 };
242
243 //**********************************************************
244 //**********************************************************
245 //**********************************************************
246 ModuleBase_PreferencesDlg::ModuleBase_PreferencesDlg(SUIT_ResourceMgr* theResurces,
247   QWidget* theParent)
248     : QDialog(theParent),
249       myIsChanged(false)
250 {
251   setWindowTitle(tr("Edit preferences"));
252
253   QVBoxLayout* main = new QVBoxLayout(this);
254   main->setMargin(5);
255   main->setSpacing(5);
256
257   myPreferences = new ModuleBase_PreferencesMgr(theResurces, this);
258   main->addWidget(myPreferences);
259
260   setFocusProxy(myPreferences);
261   myPreferences->setFrameStyle(QFrame::Box | QFrame::Sunken);
262
263   QDialogButtonBox* aBtnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel |
264                                                    QDialogButtonBox::Reset,
265                                                    Qt::Horizontal, this);
266   QPushButton* aDefaultButton = aBtnBox->button(QDialogButtonBox::Reset);
267   aDefaultButton->setText(tr("Default"));
268   connect(aDefaultButton, SIGNAL(clicked()), this, SLOT(onDefault()));
269
270   main->addWidget(aBtnBox);
271   connect(aBtnBox, SIGNAL(accepted()), this, SLOT(accept()));
272   connect(aBtnBox, SIGNAL(rejected()), this, SLOT(reject()));
273   createEditors();
274
275   myPreferences->retrieve();
276   setMinimumSize(820, 300);
277 }
278
279 ModuleBase_PreferencesDlg::~ModuleBase_PreferencesDlg()
280 {
281 }
282
283 void ModuleBase_PreferencesDlg::createEditors()
284 {
285   int aPage = myPreferences->addItem(tr("Desktop"));
286   myPreferences->setItemIcon(aPage, QIcon(":pictures/view_prefs.png"));
287
288   createMenuPage(aPage);
289   createViewerPage(aPage);
290
291   aPage = myPreferences->addItem(tr("Module"));
292   myPreferences->setItemIcon(aPage, QIcon(":pictures/module.png"));
293
294   ModuleBase_PrefMgr aMgr(myPreferences);
295   ModuleBase_Preferences::createEditContent(&aMgr, aPage);
296 }
297
298 void ModuleBase_PreferencesDlg::createViewerPage(int thePageId)
299 {
300   int viewTab = myPreferences->addItem(tr("Viewer"), thePageId);
301
302   QStringList gradList;
303   gradList << tr("Horizontal gradient") << tr("Vertical gradient") << tr("First diagonal gradient")
304            << tr("Second diagonal gradient") << tr("First corner gradient")
305            << tr("Second corner gradient") << tr("Third corner gradient")
306            << tr("Fourth corner gradient");
307
308   QList<QVariant> idList;
309   for (int i = 0; i < gradList.size(); i++)
310     idList << i;
311
312   int bgGroup = myPreferences->addItem(tr("Background"), viewTab);
313
314   QString aImgFiles("Image files (*.bmp *.gif *.pix *.xwd *.rgb *.rs)");
315
316   int bgId = myPreferences->addItem(tr("Viewer 3d"), bgGroup, SUIT_PreferenceMgr::Background,
317                                     ModuleBase_Preferences::VIEWER_SECTION, "background");
318   myPreferences->setItemProperty("gradient_names", gradList, bgId);
319   myPreferences->setItemProperty("gradient_ids", idList, bgId);
320   myPreferences->setItemProperty("texture_enabled", true, bgId);
321   myPreferences->setItemProperty("texture_center_enabled", true, bgId);
322   myPreferences->setItemProperty("texture_tile_enabled", true, bgId);
323   myPreferences->setItemProperty("texture_stretch_enabled", true, bgId);
324   myPreferences->setItemProperty("custom_enabled", false, bgId);
325   myPreferences->setItemProperty("image_formats", aImgFiles, bgId);
326
327   // Create other parameters group in viewer tab
328   int selectionGroup = myPreferences->addItem(tr("Default selection"), viewTab);
329   myPreferences->setItemProperty("columns", 3, selectionGroup);
330   myPreferences->addItem(tr("Faces"), selectionGroup,
331                          SUIT_PreferenceMgr::Bool,
332                          ModuleBase_Preferences::VIEWER_SECTION, "face-selection");
333   myPreferences->addItem(tr("Edges"), selectionGroup,
334                          SUIT_PreferenceMgr::Bool,
335                          ModuleBase_Preferences::VIEWER_SECTION, "edge-selection");
336   myPreferences->addItem(tr("Vertices"), selectionGroup,
337                          SUIT_PreferenceMgr::Bool,
338                          ModuleBase_Preferences::VIEWER_SECTION, "vertex-selection");
339
340   int sensitivityGroup = myPreferences->addItem(tr("Selection sensitivity"), viewTab);
341   myPreferences->setItemProperty("columns", 2, sensitivityGroup);
342   myPreferences->addItem(tr("Vertex"), sensitivityGroup, SUIT_PreferenceMgr::DblSpin,
343                          ModuleBase_Preferences::VIEWER_SECTION, "point-selection-sensitivity");
344   myPreferences->addItem(tr("Edge"), sensitivityGroup, SUIT_PreferenceMgr::DblSpin,
345                          ModuleBase_Preferences::VIEWER_SECTION, "edge-selection-sensitivity");
346
347   int highlightGroup = myPreferences->addItem(tr("Additional highlighting"), viewTab);
348   myPreferences->setItemProperty("columns", 2, highlightGroup);
349   myPreferences->addItem(tr("In 3d mode"), highlightGroup,
350     SUIT_PreferenceMgr::Bool, ModuleBase_Preferences::VIEWER_SECTION, "highlighting-3d");
351   myPreferences->addItem(tr("In 2d mode"), highlightGroup,
352     SUIT_PreferenceMgr::Bool, ModuleBase_Preferences::VIEWER_SECTION, "highlighting-2d");
353
354   int colorScaleGroup = myPreferences->addItem(tr("Color scale"), viewTab);
355   myPreferences->setItemProperty("columns", 4, colorScaleGroup);
356   int aItem = myPreferences->addItem(tr("X position"), colorScaleGroup,
357     SUIT_PreferenceMgr::Double, ModuleBase_Preferences::VIEWER_SECTION, "scalar_bar_x_position");
358   myPreferences->setItemProperty("min", 0, aItem);
359   myPreferences->setItemProperty("max", 1, aItem);
360
361   aItem = myPreferences->addItem(tr("Y position"), colorScaleGroup,
362     SUIT_PreferenceMgr::Double, ModuleBase_Preferences::VIEWER_SECTION, "scalar_bar_y_position");
363   myPreferences->setItemProperty("min", 0, aItem);
364   myPreferences->setItemProperty("max", 1, aItem);
365
366   aItem = myPreferences->addItem(tr("Width"), colorScaleGroup,
367     SUIT_PreferenceMgr::Double, ModuleBase_Preferences::VIEWER_SECTION, "scalar_bar_width");
368   myPreferences->setItemProperty("min", 0, aItem);
369   myPreferences->setItemProperty("max", 1, aItem);
370
371   aItem = myPreferences->addItem(tr("Height"), colorScaleGroup,
372     SUIT_PreferenceMgr::Double, ModuleBase_Preferences::VIEWER_SECTION, "scalar_bar_height");
373   myPreferences->setItemProperty("min", 0, aItem);
374   myPreferences->setItemProperty("max", 1, aItem);
375
376   aItem = myPreferences->addItem(tr("Intervals number"), colorScaleGroup,
377     SUIT_PreferenceMgr::Integer, ModuleBase_Preferences::VIEWER_SECTION, "scalar_bar_nb_intervals");
378   myPreferences->setItemProperty("min", 0, aItem);
379   myPreferences->setItemProperty("max", 100, aItem);
380
381   aItem = myPreferences->addItem(tr("Text height"), colorScaleGroup,
382     SUIT_PreferenceMgr::Integer, ModuleBase_Preferences::VIEWER_SECTION, "scalar_bar_text_height");
383   myPreferences->setItemProperty("min", 0, aItem);
384   myPreferences->setItemProperty("max", 100, aItem);
385
386   aItem = myPreferences->addItem(tr("Text color"), colorScaleGroup,
387     SUIT_PreferenceMgr::Color, ModuleBase_Preferences::VIEWER_SECTION, "scalar_bar_text_color");
388 }
389
390 void ModuleBase_PreferencesDlg::createMenuPage(int thePageId)
391 {
392   int aMenuTab = myPreferences->addItem(tr("Main menu"), thePageId);
393
394   int aSizeGroup = myPreferences->addItem(tr("Size"), aMenuTab);
395   myPreferences->setItemProperty("columns", 1, aSizeGroup);
396
397   int aRowsNb = myPreferences->addItem(tr("Number of rows"), aSizeGroup,
398                                        SUIT_PreferenceMgr::IntSpin,
399                                        ModuleBase_Preferences::MENU_SECTION,
400                                        "rows_number");
401   myPreferences->setItemProperty("min", 1, aRowsNb);
402   myPreferences->setItemProperty("max", 6, aRowsNb);
403
404   myPreferences->addItem(tr("Show Status Bar"), aSizeGroup,
405                          SUIT_PreferenceMgr::Bool, ModuleBase_Preferences::MENU_SECTION,
406                          "status_bar");
407 }
408
409 void ModuleBase_PreferencesDlg::accept()
410 {
411   myPreferences->store();
412   myIsChanged = true;
413
414   // Save custom properties
415   ModuleBase_Preferences::updateConfigByResources();
416   QDialog::accept();
417 }
418
419 void ModuleBase_PreferencesDlg::modified(ModuleBase_Prefs& theModified) const
420 {
421   theModified = myPreferences->modified();
422 }
423
424 void ModuleBase_PreferencesDlg::onDefault()
425 {
426   // reset main resources. It throwns all resource manager items to the
427   // initial/default state. If there is no a default state of the item,
428   // it will be filled with an empty value. It concernerned to plugin
429   // config items, like visualization color. The main xml do not contains
430   // default values for them. So, it is important to reset the config
431   // properties after reseting the resources preferences.
432   ModuleBase_Preferences::resetResourcePreferences(myPreferences);
433   // reset plugin's resources. It fills the config resources with the default
434   // values, stores result in the resource manager and retrieve the preferences
435   // items with these values.
436   ModuleBase_Preferences::resetConfigPropPreferences(myPreferences);
437 }
438
439 //**********************************************************
440 //**********************************************************
441 //**********************************************************
442 void ModuleBase_PreferencesMgr::changedResources(const ResourceMap& theMap)
443 {
444   myModified.clear();
445   ResourceMap::ConstIterator it;
446   QString sec, param;
447   for (it = theMap.begin(); it != theMap.end(); ++it) {
448     ModuleBase_Pref aPref;
449     it.key()->resource(aPref.first, aPref.second);
450     myModified.append(aPref);
451   }
452 }