Salome HOME
616e659e06c6a9c69bc0fbdb8ff39ea3470c54b0
[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("NewGeom");
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
107 void ModuleBase_Preferences::createCustomPage(ModuleBase_IPrefMgr* thePref, int thePageId)
108 {
109   SUIT_ResourceMgr* aResMgr = ModuleBase_Preferences::resourceMgr();
110   bool isResModified = false;
111
112   // Make a Tab from each section
113   std::list<std::string> aSections = Config_PropManager::getSections();
114   std::list<std::string>::const_iterator it;
115   for (it = aSections.cbegin(); it != aSections.cend(); ++it) {
116     Config_Properties aProps = Config_PropManager::getProperties(*it);
117     int aTab = thePref->prefMgr()->addItem(QString((*it).c_str()), thePageId);
118     thePref->prefMgr()->setItemProperty("columns", 2, aTab);
119
120     Config_Properties::const_iterator aIt;
121     for (aIt = aProps.cbegin(); aIt != aProps.cend(); ++aIt) {
122       Config_Prop* aProp = (*aIt);
123       // check that the property is defined
124       QString aSection(aProp->section().c_str());
125       QString aName(aProp->name().c_str());
126       if (!aResMgr->hasValue(aSection, aName)) {
127         aResMgr->setValue(aSection, aName, QString(aProp->value().c_str()));
128         isResModified = true;
129       }
130       // Add item
131       if (aProp->type() != Config_Prop::Disabled) {
132         SUIT_PreferenceMgr::PrefItemType aPrefType = SUIT_PreferenceMgr::Auto;
133         if (aProp->type() == Config_Prop::Directory) {
134           aPrefType = SUIT_PreferenceMgr::File;
135         } else {
136           aPrefType = (SUIT_PreferenceMgr::PrefItemType) aProp->type();
137         }
138         int anId = thePref->addPreference(QObject::tr(aProp->title().c_str()), aTab, aPrefType,
139                                           QString::fromStdString(aProp->section()),
140                                           QString::fromStdString(aProp->name()));
141         if(aProp->type() == Config_Prop::Directory) {
142           thePref->setItemProperty("path_type", Qtx::PT_Directory, anId);
143         }
144       }
145     }
146   }
147 }
148
149 /**
150 * Implementation of preferences manager interface
151 */
152 class ModuleBase_PrefMgr: public ModuleBase_IPrefMgr
153 {
154 public:
155   /// Constructor
156   /// \param theMgr a preferences manager
157   ModuleBase_PrefMgr(ModuleBase_PreferencesMgr* theMgr):myMgr(theMgr) {}
158
159   virtual int addPreference(const QString& theLbl, int pId, 
160                             SUIT_PreferenceMgr::PrefItemType theType,
161                             const QString& theSection, const QString& theName )
162   {
163     return myMgr->addItem(theLbl, pId, theType, theSection, theName);
164   }
165
166   virtual void setItemProperty( const QString& thePropName, const QVariant& theValue,
167                                const int theId = -1) {
168     myMgr->setItemProperty(thePropName, theValue, theId);
169   }
170
171   virtual SUIT_PreferenceMgr* prefMgr() const { return myMgr; }
172
173 private:
174   ModuleBase_PreferencesMgr* myMgr;
175 };
176
177 //**********************************************************
178 //**********************************************************
179 //**********************************************************
180 ModuleBase_PreferencesDlg::ModuleBase_PreferencesDlg(SUIT_ResourceMgr* theResurces, QWidget* theParent)
181     : QDialog(theParent),
182       myIsChanged(false)
183 {
184   setWindowTitle(tr("Edit preferences"));
185
186   QVBoxLayout* main = new QVBoxLayout(this);
187   main->setMargin(5);
188   main->setSpacing(5);
189
190   myPreferences = new ModuleBase_PreferencesMgr(theResurces, this);
191   main->addWidget(myPreferences);
192
193   setFocusProxy(myPreferences);
194   myPreferences->setFrameStyle(QFrame::Box | QFrame::Sunken);
195
196   QDialogButtonBox* aBtnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel |
197                                                    QDialogButtonBox::Reset,
198                                                    Qt::Horizontal, this);
199   QPushButton* aDefaultButton = aBtnBox->button(QDialogButtonBox::Reset);
200   aDefaultButton->setText(tr("Default"));
201   connect(aDefaultButton, SIGNAL(clicked()), this, SLOT(onDefault()));
202
203   main->addWidget(aBtnBox);
204   connect(aBtnBox, SIGNAL(accepted()), this, SLOT(accept()));
205   connect(aBtnBox, SIGNAL(rejected()), this, SLOT(reject()));
206   createEditors();
207
208   myPreferences->retrieve();
209   setMinimumSize(800, 200);
210 }
211
212 ModuleBase_PreferencesDlg::~ModuleBase_PreferencesDlg()
213 {
214 }
215
216 void ModuleBase_PreferencesDlg::createEditors()
217 {
218   int aPage = myPreferences->addItem(tr("Desktop"));
219   myPreferences->setItemIcon(aPage, QIcon(":pictures/view_prefs.png"));
220
221   createMenuPage(aPage);
222   createViewerPage(aPage);
223
224   aPage = myPreferences->addItem(tr("Module"));
225   myPreferences->setItemIcon(aPage, QIcon(":pictures/module.png"));
226
227   ModuleBase_PrefMgr aMgr(myPreferences);
228   ModuleBase_Preferences::createEditContent(&aMgr, aPage);
229 }
230
231 void ModuleBase_PreferencesDlg::createViewerPage(int thePageId)
232 {
233   int viewTab = myPreferences->addItem(tr("Viewer"), thePageId);
234
235   QStringList gradList;
236   gradList << tr("Horizontal gradient") << tr("Vertical gradient") << tr("First diagonal gradient")
237            << tr("Second diagonal gradient") << tr("First corner gradient")
238            << tr("Second corner gradient") << tr("Third corner gradient")
239            << tr("Fourth corner gradient");
240
241   QList<QVariant> idList;
242   for (int i = 0; i < gradList.size(); i++)
243     idList << i;
244
245   int bgGroup = myPreferences->addItem(tr("Background"), viewTab);
246
247   QString aImgFiles("Image files (*.bmp *.gif *.pix *.xwd *.rgb *.rs)");
248
249   int bgId = myPreferences->addItem(tr("Viewer 3d"), bgGroup, SUIT_PreferenceMgr::Background,
250                                     ModuleBase_Preferences::VIEWER_SECTION, "background");
251   myPreferences->setItemProperty("gradient_names", gradList, bgId);
252   myPreferences->setItemProperty("gradient_ids", idList, bgId);
253   myPreferences->setItemProperty("texture_enabled", true, bgId);
254   myPreferences->setItemProperty("texture_center_enabled", true, bgId);
255   myPreferences->setItemProperty("texture_tile_enabled", true, bgId);
256   myPreferences->setItemProperty("texture_stretch_enabled", true, bgId);
257   myPreferences->setItemProperty("custom_enabled", false, bgId);
258   myPreferences->setItemProperty("image_formats", aImgFiles, bgId);
259 }
260
261 void ModuleBase_PreferencesDlg::createMenuPage(int thePageId)
262 {
263   int aMenuTab = myPreferences->addItem(tr("Main menu"), thePageId);
264
265   int aSizeGroup = myPreferences->addItem(tr("Size"), aMenuTab);
266   myPreferences->setItemProperty("columns", 1, aSizeGroup);
267
268   int aRowsNb = myPreferences->addItem(tr("Number of rows"), aSizeGroup,
269                                        SUIT_PreferenceMgr::IntSpin, ModuleBase_Preferences::MENU_SECTION,
270                                        "rows_number");
271   myPreferences->setItemProperty("min", 1, aRowsNb);
272   myPreferences->setItemProperty("max", 6, aRowsNb);
273 }
274
275 void ModuleBase_PreferencesDlg::accept()
276 {
277   myPreferences->store();
278   myIsChanged = true;
279
280   // Save custom properties
281   ModuleBase_Preferences::updateConfigByResources();
282   QDialog::accept();
283 }
284
285 void ModuleBase_PreferencesDlg::modified(ModuleBase_Prefs& theModified) const
286 {
287   theModified = myPreferences->modified();
288 }
289
290 void ModuleBase_PreferencesDlg::onDefault()
291 {
292   // reset main resources
293 //#ifdef SALOME_750 // until SALOME 7.5.0 is released
294   QtxResourceMgr::WorkingMode aPrev =
295       myPreferences->resourceMgr()->setWorkingMode(QtxResourceMgr::IgnoreUserValues);
296   myPreferences->retrieve();
297   myPreferences->resourceMgr()->setWorkingMode(aPrev);
298 //#endif
299   // reset plugin's resources
300   ModuleBase_Preferences::resetConfig();
301   ModuleBase_Preferences::updateResourcesByConfig();
302
303   myPreferences->retrieve();
304 }
305
306 //**********************************************************
307 //**********************************************************
308 //**********************************************************
309 void ModuleBase_PreferencesMgr::changedResources(const ResourceMap& theMap)
310 {
311   myModified.clear();
312   ResourceMap::ConstIterator it;
313   QString sec, param;
314   for (it = theMap.begin(); it != theMap.end(); ++it) {
315     ModuleBase_Pref aPref;
316     it.key()->resource(aPref.first, aPref.second);
317     myModified.append(aPref);
318   }
319 }