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