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