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