]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_Preferences.cpp
Salome HOME
Merge branch 'master' of newgeom:newgeom.git
[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", 2, aSizeGroup );
158
159   int aItemSizeItem = myPreferences->addItem( tr( "Item size" ), 
160                                               aSizeGroup, 
161                                               SUIT_PreferenceMgr::Selector,
162                                               XGUI_Preferences::MENU_SECTION, 
163                                               "item_size" );
164   QStringList aValuesList;
165   QList<QVariant> anIndicesList;
166   aValuesList << tr("Small") << tr("Medium") << tr("Large");
167   anIndicesList << 20 << 25 << 30;
168   myPreferences->setItemProperty( "strings", aValuesList,   aItemSizeItem );
169   myPreferences->setItemProperty( "indexes", anIndicesList, aItemSizeItem );
170
171   int aRowsNb = myPreferences->addItem( tr( "Number of rows" ), 
172                                         aSizeGroup,
173                                         SUIT_PreferenceMgr::IntSpin, 
174                                         XGUI_Preferences::MENU_SECTION, 
175                                         "rows_number" );
176   myPreferences->setItemProperty( "min", 1, aRowsNb );
177   myPreferences->setItemProperty( "max", 10, aRowsNb );
178 }
179
180
181 void XGUI_PreferencesDlg::createCustomPage(int thePageId)
182 {
183   SUIT_ResourceMgr* aResMgr = XGUI_Preferences::resourceMgr();
184   bool isResModified = false;
185
186   // Make a Tab from each section
187   std::list<std::string> aSections = Config_PropManager::getSections();
188   std::list<std::string>::const_iterator it;
189   for (it = aSections.cbegin(); it != aSections.cend(); ++it) {
190     Config_Properties aProps = Config_PropManager::getProperties(*it);
191     int aTab = myPreferences->addItem(QString((*it).c_str()), thePageId );
192     myPreferences->setItemProperty( "columns", 2, aTab );
193
194     Config_Properties::const_iterator aIt;
195     for (aIt = aProps.cbegin(); aIt != aProps.cend(); ++aIt) {
196       Config_Prop* aProp = (*aIt);
197       // check that the property is defined
198       QString aSection(aProp->section().c_str());
199       QString aName(aProp->name().c_str());
200       if (!aResMgr->hasValue(aSection, aName)) {
201         aResMgr->setValue(aSection, aName, QString(aProp->value().c_str()));
202         isResModified = true;
203       }
204       // Add item
205       if (aProp->type() != Config_Prop::Disabled)
206         myPreferences->addItem( tr(aProp->title().c_str()), aTab,
207                                 (SUIT_PreferenceMgr::PrefItemType)aProp->type(), 
208                                 QString(aProp->section().c_str()), 
209                                 QString(aProp->name().c_str()) );
210     }
211   }
212 }
213
214
215 void XGUI_PreferencesDlg::accept()
216 {
217   myPreferences->store();
218   myIsChanged = true;
219
220   // Save custom properties
221   XGUI_Preferences::updateCustomProps();
222   QDialog::accept();
223 }
224
225
226 void XGUI_PreferencesDlg::modified(XGUI_Prefs& theModified) const
227 {
228   theModified = myPreferences->modified();
229 }
230
231
232
233 //**********************************************************
234 //**********************************************************
235 //**********************************************************
236 void XGUI_PreferencesMgr::changedResources( const ResourceMap& theMap)
237 {
238   myModified.clear();
239   ResourceMap::ConstIterator it;
240   QString sec, param;
241   for (it = theMap.begin(); it != theMap.end(); ++it ) {
242     XGUI_Pref aPref;
243     it.key()->resource( aPref.first, aPref.second );
244     myModified.append(aPref);
245   }
246 }