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