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