Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[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       }
192     }
193   }
194 }
195
196 /**
197 * Implementation of preferences manager interface
198 */
199 class ModuleBase_PrefMgr: public ModuleBase_IPrefMgr
200 {
201 public:
202   /// Constructor
203   /// \param theMgr a preferences manager
204   ModuleBase_PrefMgr(ModuleBase_PreferencesMgr* theMgr):myMgr(theMgr) {}
205
206   virtual int addPreference(const QString& theLbl, int pId,
207                             SUIT_PreferenceMgr::PrefItemType theType,
208                             const QString& theSection, const QString& theName )
209   {
210     return myMgr->addItem(theLbl, pId, theType, theSection, theName);
211   }
212
213   virtual void setItemProperty( const QString& thePropName, const QVariant& theValue,
214                                const int theId = -1) {
215     myMgr->setItemProperty(thePropName, theValue, theId);
216   }
217
218   virtual SUIT_PreferenceMgr* prefMgr() const { return myMgr; }
219
220 private:
221   ModuleBase_PreferencesMgr* myMgr;
222 };
223
224 //**********************************************************
225 //**********************************************************
226 //**********************************************************
227 ModuleBase_PreferencesDlg::ModuleBase_PreferencesDlg(SUIT_ResourceMgr* theResurces,
228   QWidget* theParent)
229     : QDialog(theParent),
230       myIsChanged(false)
231 {
232   setWindowTitle(tr("Edit preferences"));
233
234   QVBoxLayout* main = new QVBoxLayout(this);
235   main->setMargin(5);
236   main->setSpacing(5);
237
238   myPreferences = new ModuleBase_PreferencesMgr(theResurces, this);
239   main->addWidget(myPreferences);
240
241   setFocusProxy(myPreferences);
242   myPreferences->setFrameStyle(QFrame::Box | QFrame::Sunken);
243
244   QDialogButtonBox* aBtnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel |
245                                                    QDialogButtonBox::Reset,
246                                                    Qt::Horizontal, this);
247   QPushButton* aDefaultButton = aBtnBox->button(QDialogButtonBox::Reset);
248   aDefaultButton->setText(tr("Default"));
249   connect(aDefaultButton, SIGNAL(clicked()), this, SLOT(onDefault()));
250
251   main->addWidget(aBtnBox);
252   connect(aBtnBox, SIGNAL(accepted()), this, SLOT(accept()));
253   connect(aBtnBox, SIGNAL(rejected()), this, SLOT(reject()));
254   createEditors();
255
256   myPreferences->retrieve();
257   setMinimumSize(800, 240);
258 }
259
260 ModuleBase_PreferencesDlg::~ModuleBase_PreferencesDlg()
261 {
262 }
263
264 void ModuleBase_PreferencesDlg::createEditors()
265 {
266   int aPage = myPreferences->addItem(tr("Desktop"));
267   myPreferences->setItemIcon(aPage, QIcon(":pictures/view_prefs.png"));
268
269   createMenuPage(aPage);
270   createViewerPage(aPage);
271
272   aPage = myPreferences->addItem(tr("Module"));
273   myPreferences->setItemIcon(aPage, QIcon(":pictures/module.png"));
274
275   ModuleBase_PrefMgr aMgr(myPreferences);
276   ModuleBase_Preferences::createEditContent(&aMgr, aPage);
277 }
278
279 void ModuleBase_PreferencesDlg::createViewerPage(int thePageId)
280 {
281   int viewTab = myPreferences->addItem(tr("Viewer"), thePageId);
282
283   QStringList gradList;
284   gradList << tr("Horizontal gradient") << tr("Vertical gradient") << tr("First diagonal gradient")
285            << tr("Second diagonal gradient") << tr("First corner gradient")
286            << tr("Second corner gradient") << tr("Third corner gradient")
287            << tr("Fourth corner gradient");
288
289   QList<QVariant> idList;
290   for (int i = 0; i < gradList.size(); i++)
291     idList << i;
292
293   int bgGroup = myPreferences->addItem(tr("Background"), viewTab);
294
295   QString aImgFiles("Image files (*.bmp *.gif *.pix *.xwd *.rgb *.rs)");
296
297   int bgId = myPreferences->addItem(tr("Viewer 3d"), bgGroup, SUIT_PreferenceMgr::Background,
298                                     ModuleBase_Preferences::VIEWER_SECTION, "background");
299   myPreferences->setItemProperty("gradient_names", gradList, bgId);
300   myPreferences->setItemProperty("gradient_ids", idList, bgId);
301   myPreferences->setItemProperty("texture_enabled", true, bgId);
302   myPreferences->setItemProperty("texture_center_enabled", true, bgId);
303   myPreferences->setItemProperty("texture_tile_enabled", true, bgId);
304   myPreferences->setItemProperty("texture_stretch_enabled", true, bgId);
305   myPreferences->setItemProperty("custom_enabled", false, bgId);
306   myPreferences->setItemProperty("image_formats", aImgFiles, bgId);
307
308   // Create other parameters group in viewer tab
309   int selectionGroup = myPreferences->addItem(tr("Default selection"), viewTab);
310   myPreferences->setItemProperty("columns", 3, selectionGroup);
311   myPreferences->addItem(tr("Faces"), selectionGroup,
312                          SUIT_PreferenceMgr::Bool,
313                          ModuleBase_Preferences::VIEWER_SECTION, "face-selection");
314   myPreferences->addItem(tr("Edges"), selectionGroup,
315                          SUIT_PreferenceMgr::Bool,
316                          ModuleBase_Preferences::VIEWER_SECTION, "edge-selection");
317   myPreferences->addItem(tr("Vertices"), selectionGroup,
318                          SUIT_PreferenceMgr::Bool,
319                          ModuleBase_Preferences::VIEWER_SECTION, "vertex-selection");
320
321   int sensitivityGroup = myPreferences->addItem(tr("Selection sensitivity"), viewTab);
322   myPreferences->setItemProperty("columns", 2, sensitivityGroup);
323   myPreferences->addItem(tr("Vertex"), sensitivityGroup, SUIT_PreferenceMgr::Double,
324                          ModuleBase_Preferences::VIEWER_SECTION, "point-selection-sensitivity");
325   myPreferences->addItem(tr("Edge"), sensitivityGroup, SUIT_PreferenceMgr::Double,
326                          ModuleBase_Preferences::VIEWER_SECTION, "edge-selection-sensitivity");
327 }
328
329 void ModuleBase_PreferencesDlg::createMenuPage(int thePageId)
330 {
331   int aMenuTab = myPreferences->addItem(tr("Main menu"), thePageId);
332
333   int aSizeGroup = myPreferences->addItem(tr("Size"), aMenuTab);
334   myPreferences->setItemProperty("columns", 1, aSizeGroup);
335
336   int aRowsNb = myPreferences->addItem(tr("Number of rows"), aSizeGroup,
337                                        SUIT_PreferenceMgr::IntSpin,
338                                        ModuleBase_Preferences::MENU_SECTION,
339                                        "rows_number");
340   myPreferences->setItemProperty("min", 1, aRowsNb);
341   myPreferences->setItemProperty("max", 6, aRowsNb);
342
343   myPreferences->addItem(tr("Show Status Bar"), aSizeGroup,
344                          SUIT_PreferenceMgr::Bool, ModuleBase_Preferences::MENU_SECTION,
345                          "status_bar");
346 }
347
348 void ModuleBase_PreferencesDlg::accept()
349 {
350   myPreferences->store();
351   myIsChanged = true;
352
353   // Save custom properties
354   ModuleBase_Preferences::updateConfigByResources();
355   QDialog::accept();
356 }
357
358 void ModuleBase_PreferencesDlg::modified(ModuleBase_Prefs& theModified) const
359 {
360   theModified = myPreferences->modified();
361 }
362
363 void ModuleBase_PreferencesDlg::onDefault()
364 {
365   // reset main resources. It throwns all resource manager items to the
366   // initial/default state. If there is no a default state of the item,
367   // it will be filled with an empty value. It concernerned to plugin
368   // config items, like visualization color. The main xml do not contains
369   // default values for them. So, it is important to reset the config
370   // properties after reseting the resources preferences.
371   ModuleBase_Preferences::resetResourcePreferences(myPreferences);
372   // reset plugin's resources. It fills the config resources with the default
373   // values, stores result in the resource manager and retrieve the preferences
374   // items with these values.
375   ModuleBase_Preferences::resetConfigPropPreferences(myPreferences);
376 }
377
378 //**********************************************************
379 //**********************************************************
380 //**********************************************************
381 void ModuleBase_PreferencesMgr::changedResources(const ResourceMap& theMap)
382 {
383   myModified.clear();
384   ResourceMap::ConstIterator it;
385   QString sec, param;
386   for (it = theMap.begin(); it != theMap.end(); ++it) {
387     ModuleBase_Pref aPref;
388     it.key()->resource(aPref.first, aPref.second);
389     myModified.append(aPref);
390   }
391 }