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