1 // File: XGUI_Preferences.cpp
2 // Created: 07 Aug 2014
3 // Author: Vitaly SMETANNIKOV
5 #include "XGUI_Preferences.h"
6 #include "XGUI_Constants.h"
8 #include <Config_PropManager.h>
10 #include <SUIT_ResourceMgr.h>
11 #include <SUIT_PreferenceMgr.h>
14 #include <QApplication>
15 #include <QDialogButtonBox>
16 #include <QPushButton>
18 const QString XGUI_Preferences::VIEWER_SECTION = "Viewer";
19 const QString XGUI_Preferences::MENU_SECTION = "Menu";
21 SUIT_ResourceMgr* XGUI_Preferences::myResourceMgr = 0;
23 SUIT_ResourceMgr* XGUI_Preferences::resourceMgr()
26 myResourceMgr = new SUIT_ResourceMgr("NewGeom");
27 myResourceMgr->setCurrentFormat("xml");
32 bool XGUI_Preferences::editPreferences(XGUI_Prefs& theModified)
34 XGUI_PreferencesDlg aDlg(resourceMgr(), QApplication::activeWindow());
36 if (aDlg.isChanged()) {
37 aDlg.modified(theModified);
38 resourceMgr()->save();
44 void XGUI_Preferences::updateConfigByResources()
46 Config_Properties aProps = Config_PropManager::getProperties();
47 Config_Properties::iterator aIt;
48 for (aIt = aProps.begin(); aIt != aProps.end(); ++aIt) {
49 Config_Prop* aProp = (*aIt);
50 QString aVal = myResourceMgr->stringValue(QString(aProp->section().c_str()),
51 QString(aProp->name().c_str()));
52 if (!aVal.isEmpty()) {
53 aProp->setValue(aVal.toStdString());
58 void XGUI_Preferences::updateResourcesByConfig()
60 Config_Properties aProps = Config_PropManager::getProperties();
61 Config_Properties::iterator aIt;
62 for (aIt = aProps.begin(); aIt != aProps.end(); ++aIt) {
63 Config_Prop* aProp = (*aIt);
64 myResourceMgr->setValue(QString(aProp->section().c_str()), QString(aProp->name().c_str()),
65 QString(aProp->value().c_str()));
69 void XGUI_Preferences::resetConfig()
71 Config_Properties aProps = Config_PropManager::getProperties();
72 Config_Properties::iterator aIt;
73 for (aIt = aProps.begin(); aIt != aProps.end(); ++aIt) {
74 Config_Prop* aProp = (*aIt);
75 aProp->setValue(aProp->defaultValue());
79 void XGUI_Preferences::loadCustomProps()
83 QStringList aSections = myResourceMgr->sections();
84 foreach (QString aSection, aSections)
86 QStringList aParams = myResourceMgr->parameters(aSection);
87 foreach (QString aParam, aParams)
89 Config_Prop* aProp = Config_PropManager::registerProp(aSection.toStdString(),
90 aParam.toStdString(), "", Config_Prop::Disabled);
91 aProp->setValue(myResourceMgr->stringValue(aSection, aParam).toStdString());
97 void XGUI_Preferences::createEditContent(XGUI_IPrefMgr* thePref, int thePage)
99 thePref->prefMgr()->setItemIcon(thePage, QIcon(":pictures/module.png"));
100 createCustomPage(thePref, thePage);
104 void XGUI_Preferences::createCustomPage(XGUI_IPrefMgr* thePref, int thePageId)
106 SUIT_ResourceMgr* aResMgr = XGUI_Preferences::resourceMgr();
107 bool isResModified = false;
109 // Make a Tab from each section
110 std::list<std::string> aSections = Config_PropManager::getSections();
111 std::list<std::string>::const_iterator it;
112 for (it = aSections.cbegin(); it != aSections.cend(); ++it) {
113 Config_Properties aProps = Config_PropManager::getProperties(*it);
114 int aTab = thePref->prefMgr()->addItem(QString((*it).c_str()), thePageId);
115 thePref->prefMgr()->setItemProperty("columns", 2, aTab);
117 Config_Properties::const_iterator aIt;
118 for (aIt = aProps.cbegin(); aIt != aProps.cend(); ++aIt) {
119 Config_Prop* aProp = (*aIt);
120 // check that the property is defined
121 QString aSection(aProp->section().c_str());
122 QString aName(aProp->name().c_str());
123 if (!aResMgr->hasValue(aSection, aName)) {
124 aResMgr->setValue(aSection, aName, QString(aProp->value().c_str()));
125 isResModified = true;
128 if (aProp->type() != Config_Prop::Disabled)
129 thePref->addPreference(QObject::tr(aProp->title().c_str()), aTab,
130 (SUIT_PreferenceMgr::PrefItemType) aProp->type(),
131 QString(aProp->section().c_str()), QString(aProp->name().c_str()));
136 //**********************************************************
137 //**********************************************************
138 //**********************************************************
139 class XGUI_PrefMgr: public XGUI_IPrefMgr
142 XGUI_PrefMgr(XGUI_PreferencesMgr* theMgr):myMgr(theMgr) {}
144 virtual int addPreference(const QString& theLbl, int pId,
145 SUIT_PreferenceMgr::PrefItemType theType,
146 const QString& theSection, const QString& theName )
148 return myMgr->addItem(theLbl, pId, theType, theSection, theName);
151 virtual SUIT_PreferenceMgr* prefMgr() const { return myMgr; }
154 XGUI_PreferencesMgr* myMgr;
157 //**********************************************************
158 //**********************************************************
159 //**********************************************************
160 XGUI_PreferencesDlg::XGUI_PreferencesDlg(SUIT_ResourceMgr* theResurces, QWidget* theParent)
161 : QDialog(theParent),
164 setWindowTitle(tr("Edit preferences"));
166 QVBoxLayout* main = new QVBoxLayout(this);
170 myPreferences = new XGUI_PreferencesMgr(theResurces, this);
171 main->addWidget(myPreferences);
173 setFocusProxy(myPreferences);
174 myPreferences->setFrameStyle(QFrame::Box | QFrame::Sunken);
176 QDialogButtonBox* aBtnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel |
177 QDialogButtonBox::Reset,
178 Qt::Horizontal, this);
179 QPushButton* aDefaultButton = aBtnBox->button(QDialogButtonBox::Reset);
180 aDefaultButton->setText(tr("Default"));
181 connect(aDefaultButton, SIGNAL(clicked()), this, SLOT(onDefault()));
183 main->addWidget(aBtnBox);
184 connect(aBtnBox, SIGNAL(accepted()), this, SLOT(accept()));
185 connect(aBtnBox, SIGNAL(rejected()), this, SLOT(reject()));
188 myPreferences->retrieve();
189 setMinimumSize(800, 200);
192 XGUI_PreferencesDlg::~XGUI_PreferencesDlg()
196 void XGUI_PreferencesDlg::createEditors()
198 int aPage = myPreferences->addItem(tr("Desktop"));
199 myPreferences->setItemIcon(aPage, QIcon(":pictures/view_prefs.png"));
201 createMenuPage(aPage);
202 createViewerPage(aPage);
204 aPage = myPreferences->addItem(tr("Module"));
205 myPreferences->setItemIcon(aPage, QIcon(":pictures/module.png"));
207 XGUI_PrefMgr aMgr(myPreferences);
208 XGUI_Preferences::createEditContent(&aMgr, aPage);
211 void XGUI_PreferencesDlg::createViewerPage(int thePageId)
213 int viewTab = myPreferences->addItem(tr("Viewer"), thePageId);
215 QStringList gradList;
216 gradList << tr("Horizontal gradient") << tr("Vertical gradient") << tr("First diagonal gradient")
217 << tr("Second diagonal gradient") << tr("First corner gradient")
218 << tr("Second corner gradient") << tr("Third corner gradient")
219 << tr("Fourth corner gradient");
221 QList<QVariant> idList;
222 for (int i = 0; i < gradList.size(); i++)
225 int bgGroup = myPreferences->addItem(tr("Background"), viewTab);
227 QString aImgFiles("Image files (*.bmp *.gif *.pix *.xwd *.rgb *.rs)");
229 int bgId = myPreferences->addItem(tr("Viewer 3d"), bgGroup, SUIT_PreferenceMgr::Background,
230 XGUI_Preferences::VIEWER_SECTION, "background");
231 myPreferences->setItemProperty("gradient_names", gradList, bgId);
232 myPreferences->setItemProperty("gradient_ids", idList, bgId);
233 myPreferences->setItemProperty("texture_enabled", true, bgId);
234 myPreferences->setItemProperty("texture_center_enabled", true, bgId);
235 myPreferences->setItemProperty("texture_tile_enabled", true, bgId);
236 myPreferences->setItemProperty("texture_stretch_enabled", true, bgId);
237 myPreferences->setItemProperty("custom_enabled", false, bgId);
238 myPreferences->setItemProperty("image_formats", aImgFiles, bgId);
241 void XGUI_PreferencesDlg::createMenuPage(int thePageId)
243 int aMenuTab = myPreferences->addItem(tr("Main menu"), thePageId);
245 int aSizeGroup = myPreferences->addItem(tr("Size"), aMenuTab);
246 myPreferences->setItemProperty("columns", 1, aSizeGroup);
248 int aRowsNb = myPreferences->addItem(tr("Number of rows"), aSizeGroup,
249 SUIT_PreferenceMgr::IntSpin, XGUI_Preferences::MENU_SECTION,
251 myPreferences->setItemProperty("min", 1, aRowsNb);
252 myPreferences->setItemProperty("max", 6, aRowsNb);
255 void XGUI_PreferencesDlg::accept()
257 myPreferences->store();
260 // Save custom properties
261 XGUI_Preferences::updateConfigByResources();
265 void XGUI_PreferencesDlg::modified(XGUI_Prefs& theModified) const
267 theModified = myPreferences->modified();
270 void XGUI_PreferencesDlg::onDefault()
272 // reset main resources
273 #ifdef SALOME_750 // until SALOME 7.5.0 is released
274 QtxResourceMgr::WorkingMode aPrev =
275 myPreferences->resourceMgr()->setWorkingMode(QtxResourceMgr::IgnoreUserValues);
276 myPreferences->retrieve();
277 myPreferences->resourceMgr()->setWorkingMode(aPrev);
279 // reset plugin's resources
280 XGUI_Preferences::resetConfig();
281 XGUI_Preferences::updateResourcesByConfig();
283 myPreferences->retrieve();
286 //**********************************************************
287 //**********************************************************
288 //**********************************************************
289 void XGUI_PreferencesMgr::changedResources(const ResourceMap& theMap)
292 ResourceMap::ConstIterator it;
294 for (it = theMap.begin(); it != theMap.end(); ++it) {
296 it.key()->resource(aPref.first, aPref.second);
297 myModified.append(aPref);