Salome HOME
Task "Make the size of the selection area even bigger, especially for points"
[modules/shaper.git] / src / ModuleBase / ModuleBase_Preferences.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        ModuleBase_Preferences.cpp
4 // Created:     07 Aug 2014
5 // Author:      Vitaly SMETANNIKOV
6
7 #include "ModuleBase_Preferences.h"
8 //#include "ModuleBase_Constants.h"
9
10 #include <Config_PropManager.h>
11
12 #include <TopAbs_ShapeEnum.hxx>
13
14 #include <SUIT_ResourceMgr.h>
15 #include <SUIT_PreferenceMgr.h>
16 #include <Qtx.h>
17
18 #include <QLayout>
19 #include <QApplication>
20 #include <QDialogButtonBox>
21 #include <QPushButton>
22
23 const QString ModuleBase_Preferences::VIEWER_SECTION = "Viewer";
24 const QString ModuleBase_Preferences::MENU_SECTION = "Menu";
25
26 SUIT_ResourceMgr* ModuleBase_Preferences::myResourceMgr = 0;
27
28 SUIT_ResourceMgr* ModuleBase_Preferences::resourceMgr()
29 {
30   if (!myResourceMgr) {
31     myResourceMgr = new SUIT_ResourceMgr("SHAPER");
32     myResourceMgr->setCurrentFormat("xml");
33   }
34   return myResourceMgr;
35 }
36
37 bool ModuleBase_Preferences::editPreferences(ModuleBase_Prefs& theModified)
38 {
39   ModuleBase_PreferencesDlg aDlg(resourceMgr(), QApplication::activeWindow());
40   aDlg.exec();
41   if (aDlg.isChanged()) {
42     aDlg.modified(theModified);
43     resourceMgr()->save();
44     return true;
45   }
46   return false;
47 }
48
49 void ModuleBase_Preferences::updateConfigByResources()
50 {
51   Config_Properties aProps = Config_PropManager::getProperties();
52   Config_Properties::iterator aIt;
53   for (aIt = aProps.begin(); aIt != aProps.end(); ++aIt) {
54     Config_Prop* aProp = (*aIt);
55     QString aVal = myResourceMgr->stringValue(QString(aProp->section().c_str()),
56                                               QString(aProp->name().c_str()));
57     if (!aVal.isEmpty()) {
58       aProp->setValue(aVal.toStdString());
59     }
60   }
61 }
62
63 void ModuleBase_Preferences::updateResourcesByConfig()
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     myResourceMgr->setValue(QString(aProp->section().c_str()), QString(aProp->name().c_str()),
70                             QString(aProp->value().c_str()));
71   }
72 }
73
74 void ModuleBase_Preferences::resetConfig()
75 {
76   Config_Properties aProps = Config_PropManager::getProperties();
77   Config_Properties::iterator aIt;
78   for (aIt = aProps.begin(); aIt != aProps.end(); ++aIt) {
79     Config_Prop* aProp = (*aIt);
80     aProp->setValue(aProp->defaultValue());
81   }
82 }
83
84 void ModuleBase_Preferences::loadCustomProps()
85 {
86   if(!myResourceMgr)
87     return;
88   QStringList aSections = myResourceMgr->sections();
89   foreach (QString aSection, aSections)
90   {
91     QStringList aParams = myResourceMgr->parameters(aSection);
92     foreach (QString aParam, aParams)
93     {
94       Config_Prop* aProp = Config_PropManager::registerProp(aSection.toStdString(),
95                                        aParam.toStdString(), "", Config_Prop::Disabled);
96       aProp->setValue(myResourceMgr->stringValue(aSection, aParam).toStdString());
97     }
98   }
99 }
100
101
102 void ModuleBase_Preferences::createEditContent(ModuleBase_IPrefMgr* thePref, int thePage)
103 {
104   thePref->prefMgr()->setItemIcon(thePage, QIcon(":pictures/module.png"));
105   createCustomPage(thePref, thePage);
106 }
107
108 void ModuleBase_Preferences::resetResourcePreferences(SUIT_PreferenceMgr* thePref)
109 {
110   if (!thePref)
111     return;
112
113   QtxResourceMgr::WorkingMode aPrev =
114     thePref->resourceMgr()->setWorkingMode(QtxResourceMgr::IgnoreUserValues);
115   thePref->retrieve();
116   thePref->resourceMgr()->setWorkingMode(aPrev);
117 }
118
119 void ModuleBase_Preferences::resetConfigPropPreferences(SUIT_PreferenceMgr* thePref)
120 {
121   resetConfig();
122   updateResourcesByConfig();
123
124   // retrieve the reset resource values to the preferences items
125   Config_Properties aProps = Config_PropManager::getProperties();
126   Config_Properties::iterator aIt;
127   QStringList aValues;
128   QStringList aSections;
129   for (aIt = aProps.begin(); aIt != aProps.end(); ++aIt) {
130     Config_Prop* aProp = (*aIt);
131     aValues.append(QString(aProp->name().c_str()));
132     if (!aSections.contains(aProp->section().c_str()))
133       aSections.append(aProp->section().c_str());
134     QtxPreferenceItem* anItem = thePref->findItem(QString(aProp->title().c_str()), true);
135     if (anItem)
136       anItem->retrieve();
137   }
138 }
139
140 void ModuleBase_Preferences::createCustomPage(ModuleBase_IPrefMgr* thePref, int thePageId)
141 {
142   SUIT_ResourceMgr* aResMgr = ModuleBase_Preferences::resourceMgr();
143   bool isResModified = false;
144
145   // Make a Tab from each section
146   std::list<std::string> aSections = Config_PropManager::getSections();
147   std::list<std::string>::const_iterator it;
148   for (it = aSections.cbegin(); it != aSections.cend(); ++it) {
149     Config_Properties aProps = Config_PropManager::getProperties(*it);
150     int aTab = thePref->prefMgr()->addItem(QString((*it).c_str()), thePageId);
151     thePref->prefMgr()->setItemProperty("columns", 2, aTab);
152
153     Config_Properties::const_iterator aIt;
154     for (aIt = aProps.cbegin(); aIt != aProps.cend(); ++aIt) {
155       Config_Prop* aProp = (*aIt);
156       // check that the property is defined
157       QString aSection(aProp->section().c_str());
158       QString aName(aProp->name().c_str());
159       if (!aResMgr->hasValue(aSection, aName)) {
160         aResMgr->setValue(aSection, aName, QString(aProp->value().c_str()));
161         isResModified = true;
162       }
163       // Add item
164       if (aProp->type() != Config_Prop::Disabled) {
165         SUIT_PreferenceMgr::PrefItemType aPrefType = SUIT_PreferenceMgr::Auto;
166         if (aProp->type() == Config_Prop::Directory) {
167           aPrefType = SUIT_PreferenceMgr::File;
168         } else {
169           aPrefType = (SUIT_PreferenceMgr::PrefItemType) aProp->type();
170         }
171         int anId = thePref->addPreference(QObject::tr(aProp->title().c_str()), aTab, aPrefType,
172                                           QString::fromStdString(aProp->section()),
173                                           QString::fromStdString(aProp->name()));
174         if(aProp->type() == Config_Prop::Directory) {
175           thePref->setItemProperty("path_type", Qtx::PT_Directory, anId);
176         }
177       }
178     }
179   }
180 }
181
182 /**
183 * Implementation of preferences manager interface
184 */
185 class ModuleBase_PrefMgr: public ModuleBase_IPrefMgr
186 {
187 public:
188   /// Constructor
189   /// \param theMgr a preferences manager
190   ModuleBase_PrefMgr(ModuleBase_PreferencesMgr* theMgr):myMgr(theMgr) {}
191
192   virtual int addPreference(const QString& theLbl, int pId, 
193                             SUIT_PreferenceMgr::PrefItemType theType,
194                             const QString& theSection, const QString& theName )
195   {
196     return myMgr->addItem(theLbl, pId, theType, theSection, theName);
197   }
198
199   virtual void setItemProperty( const QString& thePropName, const QVariant& theValue,
200                                const int theId = -1) {
201     myMgr->setItemProperty(thePropName, theValue, theId);
202   }
203
204   virtual SUIT_PreferenceMgr* prefMgr() const { return myMgr; }
205
206 private:
207   ModuleBase_PreferencesMgr* myMgr;
208 };
209
210 //**********************************************************
211 //**********************************************************
212 //**********************************************************
213 ModuleBase_PreferencesDlg::ModuleBase_PreferencesDlg(SUIT_ResourceMgr* theResurces, QWidget* theParent)
214     : QDialog(theParent),
215       myIsChanged(false)
216 {
217   setWindowTitle(tr("Edit preferences"));
218
219   QVBoxLayout* main = new QVBoxLayout(this);
220   main->setMargin(5);
221   main->setSpacing(5);
222
223   myPreferences = new ModuleBase_PreferencesMgr(theResurces, this);
224   main->addWidget(myPreferences);
225
226   setFocusProxy(myPreferences);
227   myPreferences->setFrameStyle(QFrame::Box | QFrame::Sunken);
228
229   QDialogButtonBox* aBtnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel |
230                                                    QDialogButtonBox::Reset,
231                                                    Qt::Horizontal, this);
232   QPushButton* aDefaultButton = aBtnBox->button(QDialogButtonBox::Reset);
233   aDefaultButton->setText(tr("Default"));
234   connect(aDefaultButton, SIGNAL(clicked()), this, SLOT(onDefault()));
235
236   main->addWidget(aBtnBox);
237   connect(aBtnBox, SIGNAL(accepted()), this, SLOT(accept()));
238   connect(aBtnBox, SIGNAL(rejected()), this, SLOT(reject()));
239   createEditors();
240
241   myPreferences->retrieve();
242   setMinimumSize(800, 220);
243 }
244
245 ModuleBase_PreferencesDlg::~ModuleBase_PreferencesDlg()
246 {
247 }
248
249 void ModuleBase_PreferencesDlg::createEditors()
250 {
251   int aPage = myPreferences->addItem(tr("Desktop"));
252   myPreferences->setItemIcon(aPage, QIcon(":pictures/view_prefs.png"));
253
254   createMenuPage(aPage);
255   createViewerPage(aPage);
256
257   aPage = myPreferences->addItem(tr("Module"));
258   myPreferences->setItemIcon(aPage, QIcon(":pictures/module.png"));
259
260   ModuleBase_PrefMgr aMgr(myPreferences);
261   ModuleBase_Preferences::createEditContent(&aMgr, aPage);
262 }
263
264 void ModuleBase_PreferencesDlg::createViewerPage(int thePageId)
265 {
266   int viewTab = myPreferences->addItem(tr("Viewer"), thePageId);
267
268   QStringList gradList;
269   gradList << tr("Horizontal gradient") << tr("Vertical gradient") << tr("First diagonal gradient")
270            << tr("Second diagonal gradient") << tr("First corner gradient")
271            << tr("Second corner gradient") << tr("Third corner gradient")
272            << tr("Fourth corner gradient");
273
274   QList<QVariant> idList;
275   for (int i = 0; i < gradList.size(); i++)
276     idList << i;
277
278   int bgGroup = myPreferences->addItem(tr("Background"), viewTab);
279
280   QString aImgFiles("Image files (*.bmp *.gif *.pix *.xwd *.rgb *.rs)");
281
282   int bgId = myPreferences->addItem(tr("Viewer 3d"), bgGroup, SUIT_PreferenceMgr::Background,
283                                     ModuleBase_Preferences::VIEWER_SECTION, "background");
284   myPreferences->setItemProperty("gradient_names", gradList, bgId);
285   myPreferences->setItemProperty("gradient_ids", idList, bgId);
286   myPreferences->setItemProperty("texture_enabled", true, bgId);
287   myPreferences->setItemProperty("texture_center_enabled", true, bgId);
288   myPreferences->setItemProperty("texture_tile_enabled", true, bgId);
289   myPreferences->setItemProperty("texture_stretch_enabled", true, bgId);
290   myPreferences->setItemProperty("custom_enabled", false, bgId);
291   myPreferences->setItemProperty("image_formats", aImgFiles, bgId);
292
293   // Create other parameters group in viewer tab
294   int otherGroup = myPreferences->addItem(tr("Default selection"), viewTab);
295   myPreferences->setItemProperty("columns", 3, otherGroup);
296   myPreferences->addItem(tr("Faces"), otherGroup, 
297                          SUIT_PreferenceMgr::Bool,
298                          ModuleBase_Preferences::VIEWER_SECTION, "face-selection");
299   myPreferences->addItem(tr("Edges"), otherGroup, 
300                          SUIT_PreferenceMgr::Bool,
301                          ModuleBase_Preferences::VIEWER_SECTION, "edge-selection");
302   myPreferences->addItem(tr("Vertices"), otherGroup, 
303                          SUIT_PreferenceMgr::Bool,
304                          ModuleBase_Preferences::VIEWER_SECTION, "vertex-selection");
305
306   myPreferences->addItem(tr("Vertex selection sensitivity"), otherGroup, SUIT_PreferenceMgr::Double,
307                          ModuleBase_Preferences::VIEWER_SECTION, "point-selection-sensitivity");
308 }
309
310 void ModuleBase_PreferencesDlg::createMenuPage(int thePageId)
311 {
312   int aMenuTab = myPreferences->addItem(tr("Main menu"), thePageId);
313
314   int aSizeGroup = myPreferences->addItem(tr("Size"), aMenuTab);
315   myPreferences->setItemProperty("columns", 1, aSizeGroup);
316
317   int aRowsNb = myPreferences->addItem(tr("Number of rows"), aSizeGroup,
318                                        SUIT_PreferenceMgr::IntSpin, ModuleBase_Preferences::MENU_SECTION,
319                                        "rows_number");
320   myPreferences->setItemProperty("min", 1, aRowsNb);
321   myPreferences->setItemProperty("max", 6, aRowsNb);
322
323   myPreferences->addItem(tr("Show Status Bar"), aSizeGroup,
324                          SUIT_PreferenceMgr::Bool, ModuleBase_Preferences::MENU_SECTION,
325                          "status_bar");
326 }
327
328 void ModuleBase_PreferencesDlg::accept()
329 {
330   myPreferences->store();
331   myIsChanged = true;
332
333   // Save custom properties
334   ModuleBase_Preferences::updateConfigByResources();
335   QDialog::accept();
336 }
337
338 void ModuleBase_PreferencesDlg::modified(ModuleBase_Prefs& theModified) const
339 {
340   theModified = myPreferences->modified();
341 }
342
343 void ModuleBase_PreferencesDlg::onDefault()
344 {
345   // reset main resources. It throwns all resource manager items to the
346   // initial/default state. If there is no a default state of the item,
347   // it will be filled with an empty value. It concernerned to plugin
348   // config items, like visualization color. The main xml do not contains
349   // default values for them. So, it is important to reset the config
350   // properties after reseting the resources preferences.
351   ModuleBase_Preferences::resetResourcePreferences(myPreferences);
352   // reset plugin's resources. It fills the config resources with the default
353   // values, stores result in the resource manager and retrieve the preferences
354   // items with these values.
355   ModuleBase_Preferences::resetConfigPropPreferences(myPreferences);
356 }
357
358 //**********************************************************
359 //**********************************************************
360 //**********************************************************
361 void ModuleBase_PreferencesMgr::changedResources(const ResourceMap& theMap)
362 {
363   myModified.clear();
364   ResourceMap::ConstIterator it;
365   QString sec, param;
366   for (it = theMap.begin(); it != theMap.end(); ++it) {
367     ModuleBase_Pref aPref;
368     it.key()->resource(aPref.first, aPref.second);
369     myModified.append(aPref);
370   }
371 }