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