]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_Preferences.cpp
Salome HOME
Provide Min/Max for properties with Double Spin control
[modules/shaper.git] / src / ModuleBase / ModuleBase_Preferences.cpp
1 // Copyright (C) 2014-2017  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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "ModuleBase_Preferences.h"
22 //#include "ModuleBase_Constants.h"
23
24 #include <Config_PropManager.h>
25
26 #include <TopAbs_ShapeEnum.hxx>
27
28 #include <SUIT_ResourceMgr.h>
29 #include <SUIT_PreferenceMgr.h>
30 #include <Qtx.h>
31
32 #include <QLayout>
33 #include <QApplication>
34 #include <QDialogButtonBox>
35 #include <QPushButton>
36
37 const QString ModuleBase_Preferences::VIEWER_SECTION = "Viewer";
38 const QString ModuleBase_Preferences::MENU_SECTION = "Menu";
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   createCustomPage(thePref, thePage);
120 }
121
122 void ModuleBase_Preferences::resetResourcePreferences(SUIT_PreferenceMgr* thePref)
123 {
124   if (!thePref)
125     return;
126
127   QtxResourceMgr::WorkingMode aPrev =
128     thePref->resourceMgr()->setWorkingMode(QtxResourceMgr::IgnoreUserValues);
129   thePref->retrieve();
130   thePref->resourceMgr()->setWorkingMode(aPrev);
131 }
132
133 void ModuleBase_Preferences::resetConfigPropPreferences(SUIT_PreferenceMgr* thePref)
134 {
135   resetConfig();
136   updateResourcesByConfig();
137
138   // retrieve the reset resource values to the preferences items
139   Config_Properties aProps = Config_PropManager::getProperties();
140   Config_Properties::iterator aIt;
141   QStringList aValues;
142   QStringList aSections;
143   for (aIt = aProps.begin(); aIt != aProps.end(); ++aIt) {
144     Config_Prop* aProp = (*aIt);
145     aValues.append(QString(aProp->name().c_str()));
146     if (!aSections.contains(aProp->section().c_str()))
147       aSections.append(aProp->section().c_str());
148     QtxPreferenceItem* anItem = thePref->findItem(QString(aProp->title().c_str()), true);
149     if (anItem)
150       anItem->retrieve();
151   }
152 }
153
154 void ModuleBase_Preferences::createCustomPage(ModuleBase_IPrefMgr* thePref, int thePageId)
155 {
156   SUIT_ResourceMgr* aResMgr = ModuleBase_Preferences::resourceMgr();
157   bool isResModified = false;
158
159   // Make a Tab from each section
160   std::list<std::string> aSections = Config_PropManager::getSections();
161   std::list<std::string>::const_iterator it;
162   for (it = aSections.cbegin(); it != aSections.cend(); ++it) {
163     Config_Properties aProps = Config_PropManager::getProperties(*it);
164     int aTab = thePref->prefMgr()->addItem(QString((*it).c_str()), thePageId);
165     thePref->prefMgr()->setItemProperty("columns", 2, aTab);
166
167     Config_Properties::const_iterator aIt;
168     for (aIt = aProps.cbegin(); aIt != aProps.cend(); ++aIt) {
169       Config_Prop* aProp = (*aIt);
170       // check that the property is defined
171       QString aSection(aProp->section().c_str());
172       QString aName(aProp->name().c_str());
173       if (!aResMgr->hasValue(aSection, aName)) {
174         aResMgr->setValue(aSection, aName, QString(aProp->value().c_str()));
175         isResModified = true;
176       }
177       // Add item
178       if (aProp->type() != Config_Prop::Disabled) {
179         SUIT_PreferenceMgr::PrefItemType aPrefType = SUIT_PreferenceMgr::Auto;
180         if (aProp->type() == Config_Prop::Directory) {
181           aPrefType = SUIT_PreferenceMgr::File;
182         } else {
183           aPrefType = (SUIT_PreferenceMgr::PrefItemType) aProp->type();
184         }
185         int anId = thePref->addPreference(QObject::tr(aProp->title().c_str()), aTab, aPrefType,
186                                           QString::fromStdString(aProp->section()),
187                                           QString::fromStdString(aProp->name()));
188         if(aProp->type() == Config_Prop::Directory) {
189           thePref->setItemProperty("path_type", Qtx::PT_Directory, anId);
190         }
191         if (aPrefType == SUIT_PreferenceMgr::DblSpin) {
192           if (aProp->min() != "") {
193             double aMin = QString(aProp->min().c_str()).toDouble();
194             thePref->setItemProperty("min", aMin, anId);
195           }
196           if (aProp->max() != "") {
197             double aMax = QString(aProp->max().c_str()).toDouble();
198             thePref->setItemProperty("max", aMax, anId);
199           }
200         }
201       }
202     }
203   }
204 }
205
206 /**
207 * Implementation of preferences manager interface
208 */
209 class ModuleBase_PrefMgr: public ModuleBase_IPrefMgr
210 {
211 public:
212   /// Constructor
213   /// \param theMgr a preferences manager
214   ModuleBase_PrefMgr(ModuleBase_PreferencesMgr* theMgr):myMgr(theMgr) {}
215
216   virtual int addPreference(const QString& theLbl, int pId,
217                             SUIT_PreferenceMgr::PrefItemType theType,
218                             const QString& theSection, const QString& theName )
219   {
220     return myMgr->addItem(theLbl, pId, theType, theSection, theName);
221   }
222
223   virtual void setItemProperty( const QString& thePropName, const QVariant& theValue,
224                                const int theId = -1) {
225     myMgr->setItemProperty(thePropName, theValue, theId);
226   }
227
228   virtual SUIT_PreferenceMgr* prefMgr() const { return myMgr; }
229
230 private:
231   ModuleBase_PreferencesMgr* myMgr;
232 };
233
234 //**********************************************************
235 //**********************************************************
236 //**********************************************************
237 ModuleBase_PreferencesDlg::ModuleBase_PreferencesDlg(SUIT_ResourceMgr* theResurces,
238   QWidget* theParent)
239     : QDialog(theParent),
240       myIsChanged(false)
241 {
242   setWindowTitle(tr("Edit preferences"));
243
244   QVBoxLayout* main = new QVBoxLayout(this);
245   main->setMargin(5);
246   main->setSpacing(5);
247
248   myPreferences = new ModuleBase_PreferencesMgr(theResurces, this);
249   main->addWidget(myPreferences);
250
251   setFocusProxy(myPreferences);
252   myPreferences->setFrameStyle(QFrame::Box | QFrame::Sunken);
253
254   QDialogButtonBox* aBtnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel |
255                                                    QDialogButtonBox::Reset,
256                                                    Qt::Horizontal, this);
257   QPushButton* aDefaultButton = aBtnBox->button(QDialogButtonBox::Reset);
258   aDefaultButton->setText(tr("Default"));
259   connect(aDefaultButton, SIGNAL(clicked()), this, SLOT(onDefault()));
260
261   main->addWidget(aBtnBox);
262   connect(aBtnBox, SIGNAL(accepted()), this, SLOT(accept()));
263   connect(aBtnBox, SIGNAL(rejected()), this, SLOT(reject()));
264   createEditors();
265
266   myPreferences->retrieve();
267   setMinimumSize(800, 240);
268 }
269
270 ModuleBase_PreferencesDlg::~ModuleBase_PreferencesDlg()
271 {
272 }
273
274 void ModuleBase_PreferencesDlg::createEditors()
275 {
276   int aPage = myPreferences->addItem(tr("Desktop"));
277   myPreferences->setItemIcon(aPage, QIcon(":pictures/view_prefs.png"));
278
279   createMenuPage(aPage);
280   createViewerPage(aPage);
281
282   aPage = myPreferences->addItem(tr("Module"));
283   myPreferences->setItemIcon(aPage, QIcon(":pictures/module.png"));
284
285   ModuleBase_PrefMgr aMgr(myPreferences);
286   ModuleBase_Preferences::createEditContent(&aMgr, aPage);
287 }
288
289 void ModuleBase_PreferencesDlg::createViewerPage(int thePageId)
290 {
291   int viewTab = myPreferences->addItem(tr("Viewer"), thePageId);
292
293   QStringList gradList;
294   gradList << tr("Horizontal gradient") << tr("Vertical gradient") << tr("First diagonal gradient")
295            << tr("Second diagonal gradient") << tr("First corner gradient")
296            << tr("Second corner gradient") << tr("Third corner gradient")
297            << tr("Fourth corner gradient");
298
299   QList<QVariant> idList;
300   for (int i = 0; i < gradList.size(); i++)
301     idList << i;
302
303   int bgGroup = myPreferences->addItem(tr("Background"), viewTab);
304
305   QString aImgFiles("Image files (*.bmp *.gif *.pix *.xwd *.rgb *.rs)");
306
307   int bgId = myPreferences->addItem(tr("Viewer 3d"), bgGroup, SUIT_PreferenceMgr::Background,
308                                     ModuleBase_Preferences::VIEWER_SECTION, "background");
309   myPreferences->setItemProperty("gradient_names", gradList, bgId);
310   myPreferences->setItemProperty("gradient_ids", idList, bgId);
311   myPreferences->setItemProperty("texture_enabled", true, bgId);
312   myPreferences->setItemProperty("texture_center_enabled", true, bgId);
313   myPreferences->setItemProperty("texture_tile_enabled", true, bgId);
314   myPreferences->setItemProperty("texture_stretch_enabled", true, bgId);
315   myPreferences->setItemProperty("custom_enabled", false, bgId);
316   myPreferences->setItemProperty("image_formats", aImgFiles, bgId);
317
318   // Create other parameters group in viewer tab
319   int selectionGroup = myPreferences->addItem(tr("Default selection"), viewTab);
320   myPreferences->setItemProperty("columns", 3, selectionGroup);
321   myPreferences->addItem(tr("Faces"), selectionGroup,
322                          SUIT_PreferenceMgr::Bool,
323                          ModuleBase_Preferences::VIEWER_SECTION, "face-selection");
324   myPreferences->addItem(tr("Edges"), selectionGroup,
325                          SUIT_PreferenceMgr::Bool,
326                          ModuleBase_Preferences::VIEWER_SECTION, "edge-selection");
327   myPreferences->addItem(tr("Vertices"), selectionGroup,
328                          SUIT_PreferenceMgr::Bool,
329                          ModuleBase_Preferences::VIEWER_SECTION, "vertex-selection");
330
331   int sensitivityGroup = myPreferences->addItem(tr("Selection sensitivity"), viewTab);
332   myPreferences->setItemProperty("columns", 2, sensitivityGroup);
333   myPreferences->addItem(tr("Vertex"), sensitivityGroup, SUIT_PreferenceMgr::DblSpin,
334                          ModuleBase_Preferences::VIEWER_SECTION, "point-selection-sensitivity");
335   myPreferences->addItem(tr("Edge"), sensitivityGroup, SUIT_PreferenceMgr::DblSpin,
336                          ModuleBase_Preferences::VIEWER_SECTION, "edge-selection-sensitivity");
337 }
338
339 void ModuleBase_PreferencesDlg::createMenuPage(int thePageId)
340 {
341   int aMenuTab = myPreferences->addItem(tr("Main menu"), thePageId);
342
343   int aSizeGroup = myPreferences->addItem(tr("Size"), aMenuTab);
344   myPreferences->setItemProperty("columns", 1, aSizeGroup);
345
346   int aRowsNb = myPreferences->addItem(tr("Number of rows"), aSizeGroup,
347                                        SUIT_PreferenceMgr::IntSpin,
348                                        ModuleBase_Preferences::MENU_SECTION,
349                                        "rows_number");
350   myPreferences->setItemProperty("min", 1, aRowsNb);
351   myPreferences->setItemProperty("max", 6, aRowsNb);
352
353   myPreferences->addItem(tr("Show Status Bar"), aSizeGroup,
354                          SUIT_PreferenceMgr::Bool, ModuleBase_Preferences::MENU_SECTION,
355                          "status_bar");
356 }
357
358 void ModuleBase_PreferencesDlg::accept()
359 {
360   myPreferences->store();
361   myIsChanged = true;
362
363   // Save custom properties
364   ModuleBase_Preferences::updateConfigByResources();
365   QDialog::accept();
366 }
367
368 void ModuleBase_PreferencesDlg::modified(ModuleBase_Prefs& theModified) const
369 {
370   theModified = myPreferences->modified();
371 }
372
373 void ModuleBase_PreferencesDlg::onDefault()
374 {
375   // reset main resources. It throwns all resource manager items to the
376   // initial/default state. If there is no a default state of the item,
377   // it will be filled with an empty value. It concernerned to plugin
378   // config items, like visualization color. The main xml do not contains
379   // default values for them. So, it is important to reset the config
380   // properties after reseting the resources preferences.
381   ModuleBase_Preferences::resetResourcePreferences(myPreferences);
382   // reset plugin's resources. It fills the config resources with the default
383   // values, stores result in the resource manager and retrieve the preferences
384   // items with these values.
385   ModuleBase_Preferences::resetConfigPropPreferences(myPreferences);
386 }
387
388 //**********************************************************
389 //**********************************************************
390 //**********************************************************
391 void ModuleBase_PreferencesMgr::changedResources(const ResourceMap& theMap)
392 {
393   myModified.clear();
394   ResourceMap::ConstIterator it;
395   QString sec, param;
396   for (it = theMap.begin(); it != theMap.end(); ++it) {
397     ModuleBase_Pref aPref;
398     it.key()->resource(aPref.first, aPref.second);
399     myModified.append(aPref);
400   }
401 }