Salome HOME
Get rid of compilation warnings. Part I.
[modules/shaper.git] / src / ModuleBase / ModuleBase_Preferences.cpp
1 // Copyright (C) 2014-2020  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
157   // Make a Tab from each section
158   std::list<std::string> aSections = Config_PropManager::getSections();
159   std::list<std::string>::const_iterator it;
160   for (it = aSections.cbegin(); it != aSections.cend(); ++it) {
161     Config_Properties aProps = Config_PropManager::getProperties(*it);
162     int aTab = thePref->prefMgr()->addItem(QString((*it).c_str()), thePageId);
163     thePref->prefMgr()->setItemProperty("columns", 2, aTab);
164
165     Config_Properties::const_iterator aIt;
166     for (aIt = aProps.cbegin(); aIt != aProps.cend(); ++aIt) {
167       Config_Prop* aProp = (*aIt);
168       // check that the property is defined
169       QString aSection(aProp->section().c_str());
170       QString aName(aProp->name().c_str());
171       if (!aResMgr->hasValue(aSection, aName))
172         aResMgr->setValue(aSection, aName, QString(aProp->value().c_str()));
173
174       // Add item
175       if (aProp->type() != Config_Prop::Disabled) {
176         SUIT_PreferenceMgr::PrefItemType aPrefType = SUIT_PreferenceMgr::Auto;
177         switch (aProp->type()) {
178         case Config_Prop::Directory:
179           aPrefType = SUIT_PreferenceMgr::File;
180           break;
181         case Config_Prop::Cursor:
182           aPrefType = SUIT_PreferenceMgr::Selector;
183           break;
184         default:
185           aPrefType = (SUIT_PreferenceMgr::PrefItemType) aProp->type();
186         }
187
188         int anId = thePref->addPreference(QObject::tr(aProp->title().c_str()), aTab, aPrefType,
189                                           QString::fromStdString(aProp->section()),
190                                           QString::fromStdString(aProp->name()));
191
192         switch (aProp->type()) {
193         case Config_Prop::Directory:
194           thePref->setItemProperty("path_type", Qtx::PT_Directory, anId);
195           break;
196         case SUIT_PreferenceMgr::DblSpin:
197           if (aProp->min() != "") {
198             double aMin = QString(aProp->min().c_str()).toDouble();
199             thePref->setItemProperty("min", aMin, anId);
200           }
201           if (aProp->max() != "") {
202             double aMax = QString(aProp->max().c_str()).toDouble();
203             thePref->setItemProperty("max", aMax, anId);
204           }
205           break;
206         case SUIT_PreferenceMgr::IntSpin:
207           if (aProp->min() != "") {
208             int aMin = QString(aProp->min().c_str()).toInt();
209             thePref->setItemProperty("min", aMin, anId);
210           }
211           if (aProp->max() != "") {
212             int aMax = QString(aProp->max().c_str()).toInt();
213             thePref->setItemProperty("max", aMax, anId);
214           }
215           break;
216         case Config_Prop::Cursor:
217           {
218             QList<QVariant> aIndicesList;
219             QList<QVariant> aIconsList;
220             aIndicesList << 0 << 1 << 2;
221             aIconsList << QPixmap(":pictures/ArrowCursor.png") <<
222               QPixmap(":pictures/CrossCursor.png") <<
223               QPixmap(":pictures/HandCursor.png");
224
225             thePref->setItemProperty("indexes", aIndicesList, anId);
226             thePref->setItemProperty("icons", aIconsList, anId);
227           }
228           break;
229         default: // [to avoid compilation warnings]
230           break;
231         }
232       }
233     }
234   }
235 }
236
237 /**
238 * Implementation of preferences manager interface
239 */
240 class ModuleBase_PrefMgr: public ModuleBase_IPrefMgr
241 {
242 public:
243   /// Constructor
244   /// \param theMgr a preferences manager
245   ModuleBase_PrefMgr(ModuleBase_PreferencesMgr* theMgr):myMgr(theMgr) {}
246
247   virtual int addPreference(const QString& theLbl, int pId,
248                             SUIT_PreferenceMgr::PrefItemType theType,
249                             const QString& theSection, const QString& theName )
250   {
251     return myMgr->addItem(theLbl, pId, theType, theSection, theName);
252   }
253
254   virtual void setItemProperty( const QString& thePropName, const QVariant& theValue,
255                                const int theId = -1) {
256     myMgr->setItemProperty(thePropName, theValue, theId);
257   }
258
259   virtual SUIT_PreferenceMgr* prefMgr() const { return myMgr; }
260
261 private:
262   ModuleBase_PreferencesMgr* myMgr;
263 };
264
265 //**********************************************************
266 //**********************************************************
267 //**********************************************************
268 ModuleBase_PreferencesDlg::ModuleBase_PreferencesDlg(SUIT_ResourceMgr* theResurces,
269   QWidget* theParent)
270     : QDialog(theParent,
271       Qt::Dialog | Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::WindowCloseButtonHint),
272       myIsChanged(false)
273 {
274   setWindowTitle(tr("Edit preferences"));
275
276   QVBoxLayout* main = new QVBoxLayout(this);
277   main->setMargin(5);
278   main->setSpacing(5);
279
280   myPreferences = new ModuleBase_PreferencesMgr(theResurces, this);
281   main->addWidget(myPreferences);
282
283   setFocusProxy(myPreferences);
284   myPreferences->setFrameStyle(QFrame::Box | QFrame::Sunken);
285
286   QDialogButtonBox* aBtnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel |
287                                                    QDialogButtonBox::Reset,
288                                                    Qt::Horizontal, this);
289   QPushButton* aDefaultButton = aBtnBox->button(QDialogButtonBox::Reset);
290   aDefaultButton->setText(tr("Default"));
291   connect(aDefaultButton, SIGNAL(clicked()), this, SLOT(onDefault()));
292
293   main->addWidget(aBtnBox);
294   connect(aBtnBox, SIGNAL(accepted()), this, SLOT(accept()));
295   connect(aBtnBox, SIGNAL(rejected()), this, SLOT(reject()));
296   createEditors();
297
298   myPreferences->retrieve();
299 }
300
301 ModuleBase_PreferencesDlg::~ModuleBase_PreferencesDlg()
302 {
303 }
304
305 void ModuleBase_PreferencesDlg::createEditors()
306 {
307   int aPage = myPreferences->addItem(tr("Desktop"));
308   myPreferences->setItemIcon(aPage, QIcon(":pictures/view_prefs.png"));
309
310   createMenuPage(aPage);
311   createViewerPage(aPage);
312
313   aPage = myPreferences->addItem(tr("Module"));
314   myPreferences->setItemIcon(aPage, QIcon(":pictures/module.png"));
315
316   ModuleBase_PrefMgr aMgr(myPreferences);
317   ModuleBase_Preferences::createEditContent(&aMgr, aPage);
318 }
319
320 void ModuleBase_PreferencesDlg::createViewerPage(int thePageId)
321 {
322   int viewTab = myPreferences->addItem(tr("Viewer"), thePageId);
323
324   QStringList gradList;
325   gradList << tr("Horizontal gradient") << tr("Vertical gradient") << tr("First diagonal gradient")
326            << tr("Second diagonal gradient") << tr("First corner gradient")
327            << tr("Second corner gradient") << tr("Third corner gradient")
328            << tr("Fourth corner gradient");
329
330   QList<QVariant> idList;
331   for (int i = 0; i < gradList.size(); i++)
332     idList << i;
333
334   int bgGroup = myPreferences->addItem(tr("Background"), viewTab);
335
336   QString aImgFiles("Image files (*.bmp *.gif *.pix *.xwd *.rgb *.rs)");
337
338   int bgId = myPreferences->addItem(tr("Viewer 3d"), bgGroup, SUIT_PreferenceMgr::Background,
339                                     ModuleBase_Preferences::VIEWER_SECTION, "background");
340   myPreferences->setItemProperty("gradient_names", gradList, bgId);
341   myPreferences->setItemProperty("gradient_ids", idList, bgId);
342   myPreferences->setItemProperty("texture_enabled", true, bgId);
343   myPreferences->setItemProperty("texture_center_enabled", true, bgId);
344   myPreferences->setItemProperty("texture_tile_enabled", true, bgId);
345   myPreferences->setItemProperty("texture_stretch_enabled", true, bgId);
346   myPreferences->setItemProperty("custom_enabled", false, bgId);
347   myPreferences->setItemProperty("image_formats", aImgFiles, bgId);
348
349   // Create other parameters group in viewer tab
350   int selectionGroup = myPreferences->addItem(tr("Default selection"), viewTab);
351   myPreferences->setItemProperty("columns", 3, selectionGroup);
352   myPreferences->addItem(tr("Faces"), selectionGroup,
353                          SUIT_PreferenceMgr::Bool,
354                          ModuleBase_Preferences::VIEWER_SECTION, "face-selection");
355   myPreferences->addItem(tr("Edges"), selectionGroup,
356                          SUIT_PreferenceMgr::Bool,
357                          ModuleBase_Preferences::VIEWER_SECTION, "edge-selection");
358   myPreferences->addItem(tr("Vertices"), selectionGroup,
359                          SUIT_PreferenceMgr::Bool,
360                          ModuleBase_Preferences::VIEWER_SECTION, "vertex-selection");
361
362   int sensitivityGroup = myPreferences->addItem(tr("Selection sensitivity"), viewTab);
363   myPreferences->setItemProperty("columns", 2, sensitivityGroup);
364   myPreferences->addItem(tr("Vertex"), sensitivityGroup, SUIT_PreferenceMgr::DblSpin,
365                          ModuleBase_Preferences::VIEWER_SECTION, "point-selection-sensitivity");
366   myPreferences->addItem(tr("Edge"), sensitivityGroup, SUIT_PreferenceMgr::DblSpin,
367                          ModuleBase_Preferences::VIEWER_SECTION, "edge-selection-sensitivity");
368
369   int highlightGroup = myPreferences->addItem(tr("Additional highlighting"), viewTab);
370   myPreferences->setItemProperty("columns", 2, highlightGroup);
371   myPreferences->addItem(tr("In 3d mode"), highlightGroup,
372     SUIT_PreferenceMgr::Bool, ModuleBase_Preferences::VIEWER_SECTION, "highlighting-3d");
373   myPreferences->addItem(tr("In 2d mode"), highlightGroup,
374     SUIT_PreferenceMgr::Bool, ModuleBase_Preferences::VIEWER_SECTION, "highlighting-2d");
375
376   int colorScaleGroup = myPreferences->addItem(tr("Color scale"), viewTab);
377   myPreferences->setItemProperty("columns", 4, colorScaleGroup);
378   int aItem = myPreferences->addItem(tr("X position"), colorScaleGroup,
379     SUIT_PreferenceMgr::Double, ModuleBase_Preferences::VIEWER_SECTION, "scalar_bar_x_position");
380   myPreferences->setItemProperty("min", 0, aItem);
381   myPreferences->setItemProperty("max", 1, aItem);
382
383   aItem = myPreferences->addItem(tr("Y position"), colorScaleGroup,
384     SUIT_PreferenceMgr::Double, ModuleBase_Preferences::VIEWER_SECTION, "scalar_bar_y_position");
385   myPreferences->setItemProperty("min", 0, aItem);
386   myPreferences->setItemProperty("max", 1, aItem);
387
388   aItem = myPreferences->addItem(tr("Width"), colorScaleGroup,
389     SUIT_PreferenceMgr::Double, ModuleBase_Preferences::VIEWER_SECTION, "scalar_bar_width");
390   myPreferences->setItemProperty("min", 0, aItem);
391   myPreferences->setItemProperty("max", 1, aItem);
392
393   aItem = myPreferences->addItem(tr("Height"), colorScaleGroup,
394     SUIT_PreferenceMgr::Double, ModuleBase_Preferences::VIEWER_SECTION, "scalar_bar_height");
395   myPreferences->setItemProperty("min", 0, aItem);
396   myPreferences->setItemProperty("max", 1, aItem);
397
398   aItem = myPreferences->addItem(tr("Intervals number"), colorScaleGroup,
399     SUIT_PreferenceMgr::Integer, ModuleBase_Preferences::VIEWER_SECTION, "scalar_bar_nb_intervals");
400   myPreferences->setItemProperty("min", 0, aItem);
401   myPreferences->setItemProperty("max", 100, aItem);
402
403   aItem = myPreferences->addItem(tr("Text height"), colorScaleGroup,
404     SUIT_PreferenceMgr::Integer, ModuleBase_Preferences::VIEWER_SECTION, "scalar_bar_text_height");
405   myPreferences->setItemProperty("min", 0, aItem);
406   myPreferences->setItemProperty("max", 100, aItem);
407
408   aItem = myPreferences->addItem(tr("Text color"), colorScaleGroup,
409     SUIT_PreferenceMgr::Color, ModuleBase_Preferences::VIEWER_SECTION, "scalar_bar_text_color");
410 }
411
412 void ModuleBase_PreferencesDlg::createMenuPage(int thePageId)
413 {
414   int aMenuTab = myPreferences->addItem(tr("Main menu"), thePageId);
415
416   int aSizeGroup = myPreferences->addItem(tr("Size"), aMenuTab);
417   myPreferences->setItemProperty("columns", 1, aSizeGroup);
418
419   int aRowsNb = myPreferences->addItem(tr("Number of rows"), aSizeGroup,
420                                        SUIT_PreferenceMgr::IntSpin,
421                                        ModuleBase_Preferences::MENU_SECTION,
422                                        "rows_number");
423   myPreferences->setItemProperty("min", 1, aRowsNb);
424   myPreferences->setItemProperty("max", 6, aRowsNb);
425
426   myPreferences->addItem(tr("Show Status Bar"), aSizeGroup,
427                          SUIT_PreferenceMgr::Bool, ModuleBase_Preferences::MENU_SECTION,
428                          "status_bar");
429 }
430
431 void ModuleBase_PreferencesDlg::accept()
432 {
433   myPreferences->store();
434   myIsChanged = true;
435
436   // Save custom properties
437   ModuleBase_Preferences::updateConfigByResources();
438   QDialog::accept();
439 }
440
441 void ModuleBase_PreferencesDlg::modified(ModuleBase_Prefs& theModified) const
442 {
443   theModified = myPreferences->modified();
444 }
445
446 void ModuleBase_PreferencesDlg::onDefault()
447 {
448   // reset main resources. It throwns all resource manager items to the
449   // initial/default state. If there is no a default state of the item,
450   // it will be filled with an empty value. It concernerned to plugin
451   // config items, like visualization color. The main xml do not contains
452   // default values for them. So, it is important to reset the config
453   // properties after reseting the resources preferences.
454   ModuleBase_Preferences::resetResourcePreferences(myPreferences);
455   // reset plugin's resources. It fills the config resources with the default
456   // values, stores result in the resource manager and retrieve the preferences
457   // items with these values.
458   ModuleBase_Preferences::resetConfigPropPreferences(myPreferences);
459 }
460
461 void ModuleBase_PreferencesDlg::showEvent(QShowEvent* theEvent)
462 {
463   QDialog::showEvent(theEvent);
464   adjustSize();
465 }
466
467 //**********************************************************
468 //**********************************************************
469 //**********************************************************
470 void ModuleBase_PreferencesMgr::changedResources(const ResourceMap& theMap)
471 {
472   myModified.clear();
473   ResourceMap::ConstIterator it;
474   QString sec, param;
475   for (it = theMap.begin(); it != theMap.end(); ++it) {
476     ModuleBase_Pref aPref;
477     it.key()->resource(aPref.first, aPref.second);
478     myModified.append(aPref);
479   }
480 }