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