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