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>
15 #include <QApplication>
16 #include <QDialogButtonBox>
17 #include <QPushButton>
19 const QString XGUI_Preferences::VIEWER_SECTION = "Viewer";
20 const QString XGUI_Preferences::MENU_SECTION = "Menu";
22 SUIT_ResourceMgr* XGUI_Preferences::myResourceMgr = 0;
24 SUIT_ResourceMgr* XGUI_Preferences::resourceMgr()
27 myResourceMgr = new SUIT_ResourceMgr("NewGeom");
28 myResourceMgr->setCurrentFormat("xml");
33 bool XGUI_Preferences::editPreferences(XGUI_Prefs& theModified)
35 XGUI_PreferencesDlg aDlg(resourceMgr(), QApplication::activeWindow());
37 if (aDlg.isChanged()) {
38 aDlg.modified(theModified);
39 resourceMgr()->save();
45 void XGUI_Preferences::updateConfigByResources()
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.isEmpty()) {
54 aProp->setValue(aVal.toStdString());
59 void XGUI_Preferences::updateResourcesByConfig()
61 Config_Properties aProps = Config_PropManager::getProperties();
62 Config_Properties::iterator aIt;
63 for (aIt = aProps.begin(); aIt != aProps.end(); ++aIt) {
64 Config_Prop* aProp = (*aIt);
65 myResourceMgr->setValue(QString(aProp->section().c_str()), QString(aProp->name().c_str()),
66 QString(aProp->value().c_str()));
70 void XGUI_Preferences::resetConfig()
72 Config_Properties aProps = Config_PropManager::getProperties();
73 Config_Properties::iterator aIt;
74 for (aIt = aProps.begin(); aIt != aProps.end(); ++aIt) {
75 Config_Prop* aProp = (*aIt);
76 aProp->setValue(aProp->defaultValue());
80 void XGUI_Preferences::loadCustomProps()
84 QStringList aSections = myResourceMgr->sections();
85 foreach (QString aSection, aSections)
87 QStringList aParams = myResourceMgr->parameters(aSection);
88 foreach (QString aParam, aParams)
90 Config_Prop* aProp = Config_PropManager::registerProp(aSection.toStdString(),
91 aParam.toStdString(), "", Config_Prop::Disabled);
92 aProp->setValue(myResourceMgr->stringValue(aSection, aParam).toStdString());
98 void XGUI_Preferences::createEditContent(XGUI_IPrefMgr* thePref, int thePage)
100 thePref->prefMgr()->setItemIcon(thePage, QIcon(":pictures/module.png"));
101 createCustomPage(thePref, thePage);
105 void XGUI_Preferences::createCustomPage(XGUI_IPrefMgr* thePref, int thePageId)
107 SUIT_ResourceMgr* aResMgr = XGUI_Preferences::resourceMgr();
108 bool isResModified = false;
110 // Make a Tab from each section
111 std::list<std::string> aSections = Config_PropManager::getSections();
112 std::list<std::string>::const_iterator it;
113 for (it = aSections.cbegin(); it != aSections.cend(); ++it) {
114 Config_Properties aProps = Config_PropManager::getProperties(*it);
115 int aTab = thePref->prefMgr()->addItem(QString((*it).c_str()), thePageId);
116 thePref->prefMgr()->setItemProperty("columns", 2, aTab);
118 Config_Properties::const_iterator aIt;
119 for (aIt = aProps.cbegin(); aIt != aProps.cend(); ++aIt) {
120 Config_Prop* aProp = (*aIt);
121 // check that the property is defined
122 QString aSection(aProp->section().c_str());
123 QString aName(aProp->name().c_str());
124 if (!aResMgr->hasValue(aSection, aName)) {
125 aResMgr->setValue(aSection, aName, QString(aProp->value().c_str()));
126 isResModified = true;
129 if (aProp->type() != Config_Prop::Disabled) {
130 SUIT_PreferenceMgr::PrefItemType aPrefType = SUIT_PreferenceMgr::Auto;
131 if (aProp->type() == Config_Prop::Directory) {
132 aPrefType = SUIT_PreferenceMgr::File;
134 aPrefType = (SUIT_PreferenceMgr::PrefItemType) aProp->type();
136 int anId = thePref->addPreference(QObject::tr(aProp->title().c_str()), aTab, aPrefType,
137 QString::fromStdString(aProp->section()),
138 QString::fromStdString(aProp->name()));
139 if(aProp->type() == Config_Prop::Directory) {
140 thePref->setItemProperty("path_type", Qtx::PT_Directory, anId);
147 //**********************************************************
148 //**********************************************************
149 //**********************************************************
150 class XGUI_PrefMgr: public XGUI_IPrefMgr
153 XGUI_PrefMgr(XGUI_PreferencesMgr* theMgr):myMgr(theMgr) {}
155 virtual int addPreference(const QString& theLbl, int pId,
156 SUIT_PreferenceMgr::PrefItemType theType,
157 const QString& theSection, const QString& theName )
159 return myMgr->addItem(theLbl, pId, theType, theSection, theName);
162 virtual void setItemProperty( const QString& thePropName, const QVariant& theValue,
163 const int theId = -1) {
164 myMgr->setItemProperty(thePropName, theValue, theId);
167 virtual SUIT_PreferenceMgr* prefMgr() const { return myMgr; }
170 XGUI_PreferencesMgr* myMgr;
173 //**********************************************************
174 //**********************************************************
175 //**********************************************************
176 XGUI_PreferencesDlg::XGUI_PreferencesDlg(SUIT_ResourceMgr* theResurces, QWidget* theParent)
177 : QDialog(theParent),
180 setWindowTitle(tr("Edit preferences"));
182 QVBoxLayout* main = new QVBoxLayout(this);
186 myPreferences = new XGUI_PreferencesMgr(theResurces, this);
187 main->addWidget(myPreferences);
189 setFocusProxy(myPreferences);
190 myPreferences->setFrameStyle(QFrame::Box | QFrame::Sunken);
192 QDialogButtonBox* aBtnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel |
193 QDialogButtonBox::Reset,
194 Qt::Horizontal, this);
195 QPushButton* aDefaultButton = aBtnBox->button(QDialogButtonBox::Reset);
196 aDefaultButton->setText(tr("Default"));
197 connect(aDefaultButton, SIGNAL(clicked()), this, SLOT(onDefault()));
199 main->addWidget(aBtnBox);
200 connect(aBtnBox, SIGNAL(accepted()), this, SLOT(accept()));
201 connect(aBtnBox, SIGNAL(rejected()), this, SLOT(reject()));
204 myPreferences->retrieve();
205 setMinimumSize(800, 200);
208 XGUI_PreferencesDlg::~XGUI_PreferencesDlg()
212 void XGUI_PreferencesDlg::createEditors()
214 int aPage = myPreferences->addItem(tr("Desktop"));
215 myPreferences->setItemIcon(aPage, QIcon(":pictures/view_prefs.png"));
217 createMenuPage(aPage);
218 createViewerPage(aPage);
220 aPage = myPreferences->addItem(tr("Module"));
221 myPreferences->setItemIcon(aPage, QIcon(":pictures/module.png"));
223 XGUI_PrefMgr aMgr(myPreferences);
224 XGUI_Preferences::createEditContent(&aMgr, aPage);
227 void XGUI_PreferencesDlg::createViewerPage(int thePageId)
229 int viewTab = myPreferences->addItem(tr("Viewer"), thePageId);
231 QStringList gradList;
232 gradList << tr("Horizontal gradient") << tr("Vertical gradient") << tr("First diagonal gradient")
233 << tr("Second diagonal gradient") << tr("First corner gradient")
234 << tr("Second corner gradient") << tr("Third corner gradient")
235 << tr("Fourth corner gradient");
237 QList<QVariant> idList;
238 for (int i = 0; i < gradList.size(); i++)
241 int bgGroup = myPreferences->addItem(tr("Background"), viewTab);
243 QString aImgFiles("Image files (*.bmp *.gif *.pix *.xwd *.rgb *.rs)");
245 int bgId = myPreferences->addItem(tr("Viewer 3d"), bgGroup, SUIT_PreferenceMgr::Background,
246 XGUI_Preferences::VIEWER_SECTION, "background");
247 myPreferences->setItemProperty("gradient_names", gradList, bgId);
248 myPreferences->setItemProperty("gradient_ids", idList, bgId);
249 myPreferences->setItemProperty("texture_enabled", true, bgId);
250 myPreferences->setItemProperty("texture_center_enabled", true, bgId);
251 myPreferences->setItemProperty("texture_tile_enabled", true, bgId);
252 myPreferences->setItemProperty("texture_stretch_enabled", true, bgId);
253 myPreferences->setItemProperty("custom_enabled", false, bgId);
254 myPreferences->setItemProperty("image_formats", aImgFiles, bgId);
257 void XGUI_PreferencesDlg::createMenuPage(int thePageId)
259 int aMenuTab = myPreferences->addItem(tr("Main menu"), thePageId);
261 int aSizeGroup = myPreferences->addItem(tr("Size"), aMenuTab);
262 myPreferences->setItemProperty("columns", 1, aSizeGroup);
264 int aRowsNb = myPreferences->addItem(tr("Number of rows"), aSizeGroup,
265 SUIT_PreferenceMgr::IntSpin, XGUI_Preferences::MENU_SECTION,
267 myPreferences->setItemProperty("min", 1, aRowsNb);
268 myPreferences->setItemProperty("max", 6, aRowsNb);
271 void XGUI_PreferencesDlg::accept()
273 myPreferences->store();
276 // Save custom properties
277 XGUI_Preferences::updateConfigByResources();
281 void XGUI_PreferencesDlg::modified(XGUI_Prefs& theModified) const
283 theModified = myPreferences->modified();
286 void XGUI_PreferencesDlg::onDefault()
288 // reset main resources
289 #ifdef SALOME_750 // until SALOME 7.5.0 is released
290 QtxResourceMgr::WorkingMode aPrev =
291 myPreferences->resourceMgr()->setWorkingMode(QtxResourceMgr::IgnoreUserValues);
292 myPreferences->retrieve();
293 myPreferences->resourceMgr()->setWorkingMode(aPrev);
295 // reset plugin's resources
296 XGUI_Preferences::resetConfig();
297 XGUI_Preferences::updateResourcesByConfig();
299 myPreferences->retrieve();
302 //**********************************************************
303 //**********************************************************
304 //**********************************************************
305 void XGUI_PreferencesMgr::changedResources(const ResourceMap& theMap)
308 ResourceMap::ConstIterator it;
310 for (it = theMap.begin(); it != theMap.end(); ++it) {
312 it.key()->resource(aPref.first, aPref.second);
313 myModified.append(aPref);